2 * Copyright (c) 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
34 #include "api_public.h"
38 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt,##args); } while (0)
40 #define debugf(fmt, args...)
43 /* Some random address used by U-Boot. */
44 extern long uboot_address;
47 valid_sig(struct api_signature *sig)
50 struct api_signature s;
55 * Clear the checksum field (in the local copy) so as to calculate the
56 * CRC with the same initial contents as at the time when the sig was
62 checksum = crc32((void *)&s, sizeof(struct api_signature));
64 if (checksum != sig->checksum)
71 * Checks to see if API signature's address was given to us as a command line
74 * returns 1/0 depending on found/not found result
77 api_parse_cmdline_sig(int argc, char **argv, struct api_signature **sig)
79 unsigned long api_address;
87 while ((c = getopt (argc, argv, "a:")) != -1)
90 api_address = strtoul(optarg, NULL, 16);
96 if (api_address != 0) {
97 *sig = (struct api_signature *)api_address;
106 * Searches for the U-Boot API signature
108 * returns 1/0 depending on found/not found result
111 api_search_sig(struct api_signature **sig)
113 unsigned char *sp, *spend;
118 if (uboot_address == 0)
119 uboot_address = 255 * 1024 * 1024;
121 sp = (void *)(uboot_address & API_SIG_SEARCH_MASK);
122 spend = sp + API_SIG_SEARCH_LEN - API_SIG_MAGLEN;
125 if (!bcmp(sp, API_SIG_MAGIC, API_SIG_MAGLEN)) {
126 *sig = (struct api_signature *)sp;
130 sp += API_SIG_MAGLEN;
137 /****************************************
141 ****************************************/
148 if (!syscall(API_GETC, NULL, &c))
159 if (!syscall(API_TSTC, NULL, &t))
166 ub_putc(const char c)
169 syscall(API_PUTC, NULL, &c);
173 ub_puts(const char *s)
176 syscall(API_PUTS, NULL, s);
179 /****************************************
183 ****************************************/
189 syscall(API_RESET, NULL);
190 while (1); /* fallback if API_RESET failed */
194 static struct mem_region mr[UB_MAX_MR];
195 static struct sys_info si;
198 ub_get_sys_info(void)
202 memset(&si, 0, sizeof(struct sys_info));
204 si.mr_no = UB_MAX_MR;
205 memset(&mr, 0, sizeof(mr));
207 if (!syscall(API_GET_SYS_INFO, &err, &si))
210 return ((err) ? NULL : &si);
213 /****************************************
217 ****************************************/
220 ub_udelay(unsigned long usec)
223 syscall(API_UDELAY, NULL, &usec);
227 ub_get_timer(unsigned long base)
231 if (!syscall(API_GET_TIMER, NULL, &cur, &base))
237 /****************************************************************************
241 * Devices are identified by handles: numbers 0, 1, 2, ..., UB_MAX_DEV-1
243 ***************************************************************************/
245 static struct device_info devices[UB_MAX_DEV];
251 return ((i < 0 || i >= UB_MAX_DEV) ? NULL : &devices[i]);
255 * Enumerates the devices: fills out device_info elements in the devices[]
258 * returns: number of devices found
263 struct device_info *di;
266 memset(&devices, 0, sizeof(struct device_info) * UB_MAX_DEV);
269 if (!syscall(API_DEV_ENUM, NULL, di))
272 while (di->cookie != NULL) {
274 if (++n >= UB_MAX_DEV)
277 /* take another device_info */
280 /* pass on the previous cookie */
281 di->cookie = devices[n - 1].cookie;
283 if (!syscall(API_DEV_ENUM, NULL, di))
291 * handle: 0-based id of the device
293 * returns: 0 when OK, err otherwise
296 ub_dev_open(int handle)
298 struct device_info *di;
301 if (handle < 0 || handle >= UB_MAX_DEV)
304 di = &devices[handle];
305 if (!syscall(API_DEV_OPEN, &err, di))
312 ub_dev_close(int handle)
314 struct device_info *di;
316 if (handle < 0 || handle >= UB_MAX_DEV)
319 di = &devices[handle];
320 if (!syscall(API_DEV_CLOSE, NULL, di))
327 * Validates device for read/write, it has to:
332 * returns: 0/1 accordingly
335 dev_valid(int handle)
338 if (handle < 0 || handle >= UB_MAX_DEV)
341 if (devices[handle].state != DEV_STA_OPEN)
348 dev_stor_valid(int handle)
351 if (!dev_valid(handle))
354 if (!(devices[handle].type & DEV_TYP_STOR))
361 ub_dev_read(int handle, void *buf, lbasize_t len, lbastart_t start,
364 struct device_info *di;
368 if (!dev_stor_valid(handle))
371 di = &devices[handle];
372 if (!syscall(API_DEV_READ, &err, di, buf, &len, &start, &act_len))
382 dev_net_valid(int handle)
385 if (!dev_valid(handle))
388 if (devices[handle].type != DEV_TYP_NET)
395 ub_dev_recv(int handle, void *buf, int len, int *rlen)
397 struct device_info *di;
398 int err = 0, act_len;
400 if (!dev_net_valid(handle))
403 di = &devices[handle];
404 if (!syscall(API_DEV_READ, &err, di, buf, &len, &act_len))
414 ub_dev_send(int handle, void *buf, int len)
416 struct device_info *di;
419 if (!dev_net_valid(handle))
422 di = &devices[handle];
423 if (!syscall(API_DEV_WRITE, &err, di, buf, &len))
430 ub_stor_type(int type)
433 if (type & DT_STOR_IDE)
436 if (type & DT_STOR_SCSI)
439 if (type & DT_STOR_USB)
442 if (type & DT_STOR_MMC)
445 if (type & DT_STOR_SATA)
452 ub_mem_type(int flags)
455 switch (flags & 0x000F) {
468 ub_dump_di(int handle)
470 struct device_info *di = ub_dev_get(handle);
473 printf("device info (%d):\n", handle);
474 printf(" cookie\t= %p\n", di->cookie);
475 printf(" type\t\t= 0x%08x\n", di->type);
477 if (di->type == DEV_TYP_NET) {
478 printf(" hwaddr\t= ");
479 for (i = 0; i < 6; i++)
480 printf("%02x ", di->di_net.hwaddr[i]);
484 } else if (di->type & DEV_TYP_STOR) {
485 printf(" type\t\t= %s\n", ub_stor_type(di->type));
486 printf(" blk size\t\t= %ld\n", di->di_stor.block_size);
487 printf(" blk count\t\t= %ld\n", di->di_stor.block_count);
492 ub_dump_si(struct sys_info *si)
496 printf("sys info:\n");
497 printf(" clkbus\t= %ld MHz\n", si->clk_bus / 1000 / 1000);
498 printf(" clkcpu\t= %ld MHz\n", si->clk_cpu / 1000 / 1000);
499 printf(" bar\t\t= 0x%08lx\n", si->bar);
502 for (i = 0; i < si->mr_no; i++) {
503 if (si->mr[i].flags == 0)
506 printf(" start\t= 0x%08lx\n", si->mr[i].start);
507 printf(" size\t= 0x%08lx\n", si->mr[i].size);
508 printf(" type\t= %s\n", ub_mem_type(si->mr[i].flags));
513 /****************************************
517 ****************************************/
520 ub_env_get(const char *name)
524 if (!syscall(API_ENV_GET, NULL, name, &value))
531 ub_env_set(const char *name, char *value)
534 syscall(API_ENV_SET, NULL, name, value);
537 static char env_name[256];
540 ub_env_enum(const char *last)
542 const char *env, *str;
546 * It's OK to pass only the name piece as last (and not the whole
547 * 'name=val' string), since the API_ENUM_ENV call uses envmatch()
548 * internally, which handles such case
551 if (!syscall(API_ENV_ENUM, NULL, last, &env))
554 if (env == NULL || last == env)
555 /* no more env. variables to enumerate */
558 /* next enumerated env var */
559 memset(env_name, 0, 256);
560 for (i = 0, str = env; *str != '=' && *str != '\0';)
561 env_name[i++] = *str++;