2 * Copyright (c) 2000 Benno Rice <benno@jeamland.net>
3 * Copyright (c) 2000 Stephane Potvin <sepotvin@videotron.ca>
4 * Copyright (c) 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
34 #include "api_public.h"
35 #include "bootstrap.h"
40 #define nitems(x) (sizeof((x)) / sizeof((x)[0]))
44 #define HEAP_SIZE (2 * 1024 * 1024)
47 struct uboot_devdesc currdev;
48 struct arch_switch archsw; /* MI/MD interface boundary */
51 uintptr_t uboot_heap_start;
52 uintptr_t uboot_heap_end;
58 { "disk", DEV_TYP_STOR },
59 { "ide", DEV_TYP_STOR | DT_STOR_IDE },
60 { "mmc", DEV_TYP_STOR | DT_STOR_MMC },
61 { "sata", DEV_TYP_STOR | DT_STOR_SATA },
62 { "scsi", DEV_TYP_STOR | DT_STOR_SCSI },
63 { "usb", DEV_TYP_STOR | DT_STOR_USB },
64 { "net", DEV_TYP_NET }
69 extern unsigned char _etext[];
70 extern unsigned char _edata[];
71 extern unsigned char __bss_start[];
72 extern unsigned char __sbss_start[];
73 extern unsigned char __sbss_end[];
74 extern unsigned char _end[];
76 #ifdef LOADER_FDT_SUPPORT
77 extern int command_fdt_internal(int argc, char *argv[]);
81 dump_sig(struct api_signature *sig)
84 printf("signature:\n");
85 printf(" version\t= %d\n", sig->version);
86 printf(" checksum\t= 0x%08x\n", sig->checksum);
87 printf(" sc entry\t= 0x%08x\n", sig->syscall);
95 printf("\naddresses info:\n");
96 printf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext);
97 printf(" _edata = 0x%08x\n", (uint32_t)_edata);
98 printf(" __sbss_start = 0x%08x\n", (uint32_t)__sbss_start);
99 printf(" __sbss_end = 0x%08x\n", (uint32_t)__sbss_end);
100 printf(" __sbss_start = 0x%08x\n", (uint32_t)__bss_start);
101 printf(" _end = 0x%08x\n", (uint32_t)_end);
102 printf(" syscall entry = 0x%08x\n", (uint32_t)syscall_ptr);
107 memsize(struct sys_info *si, int flags)
113 for (i = 0; i < si->mr_no; i++)
114 if (si->mr[i].flags == flags && si->mr[i].size)
115 size += (si->mr[i].size);
125 int t[3] = { MR_ATTR_DRAM, MR_ATTR_FLASH, MR_ATTR_SRAM };
128 if ((si = ub_get_sys_info()) == NULL)
129 panic("could not retrieve system info");
131 for (i = 0; i < 3; i++) {
132 size = memsize(si, t[i]);
134 printf("%s: %juMB\n", ub_mem_type(t[i]),
135 (uintmax_t)(size / 1024 / 1024));
140 get_device_type(const char *devstr, int *devtype)
144 struct device_type *dt;
147 for (i = 0; i < nitems(device_types); i++) {
148 dt = &device_types[i];
149 namelen = strlen(dt->name);
150 if (strncmp(dt->name, devstr, namelen) == 0) {
152 return (devstr + namelen);
155 printf("Unknown device type '%s'\n", devstr);
158 *devtype = DEV_TYP_NONE;
163 device_typename(int type)
167 for (i = 0; i < nitems(device_types); i++)
168 if (device_types[i].type == type)
169 return (device_types[i].name);
171 return ("<unknown>");
175 * Parse a device string into type, unit, slice and partition numbers. A
176 * returned value of -1 for type indicates a search should be done for the
177 * first loadable device, otherwise a returned value of -1 for unit
178 * indicates a search should be done for the first loadable device of the
181 * The returned values for slice and partition are interpreted by
184 * The device string can be a standard loader(8) disk specifier:
186 * disk<unit>s<slice> disk0s1
187 * disk<unit>s<slice><partition> disk1s2a
188 * disk<unit>p<partition> disk0p4
190 * or one of the following formats:
192 * Valid device strings: For device types:
194 * <type_name> DEV_TYP_STOR, DEV_TYP_NET
195 * <type_name><unit> DEV_TYP_STOR, DEV_TYP_NET
196 * <type_name><unit>: DEV_TYP_STOR, DEV_TYP_NET
197 * <type_name><unit>:<slice> DEV_TYP_STOR
198 * <type_name><unit>:<slice>. DEV_TYP_STOR
199 * <type_name><unit>:<slice>.<partition> DEV_TYP_STOR
201 * For valid type names, see the device_types array, above.
203 * Slice numbers are 1-based. 0 is a wildcard.
206 get_load_device(int *type, int *unit, int *slice, int *partition)
208 struct disk_devdesc *dev;
213 *type = DEV_TYP_NONE;
215 *slice = D_SLICEWILD;
216 *partition = D_PARTWILD;
218 devstr = ub_env_get("loaderdev");
219 if (devstr == NULL) {
220 printf("U-Boot env: loaderdev not set, will probe all devices.\n");
223 printf("U-Boot env: loaderdev='%s'\n", devstr);
225 p = get_device_type(devstr, type);
228 * If type is DEV_TYP_STOR we have a disk-like device. If the remainder
229 * of the string contains spaces, dots, or a colon in any location other
230 * than the last char, it's legacy format. Otherwise it might be
231 * standard loader(8) format (e.g., disk0s2a or mmc1p12), so try to
232 * parse the remainder of the string as such, and if it works, return
233 * those results. Otherwise we'll fall through to the code that parses
236 * disk_parsedev now assumes that it points to the start of the device
237 * name, but since it doesn't know about uboot's usage, just subtract 4
238 * since it always adds 4. This is the least-bad solution since it makes
239 * all the other loader code easier (might be better to create a fake
240 * 'disk...' string, but that's more work than uboot is worth).
242 if (*type & DEV_TYP_STOR) {
243 size_t len = strlen(p);
244 if (strcspn(p, " .") == len && strcspn(p, ":") >= len - 1 &&
245 disk_parsedev((struct devdesc **)&dev, p - 4, NULL) == 0) { /* Hack */
246 *unit = dev->dd.d_unit;
247 *slice = dev->d_slice;
248 *partition = dev->d_partition;
254 /* Ignore optional spaces after the device name. */
258 /* Unknown device name, or a known name without unit number. */
259 if ((*type == DEV_TYP_NONE) || (*p == '\0')) {
263 /* Malformed unit number. */
265 *type = DEV_TYP_NONE;
269 /* Guaranteed to extract a number from the string, as *p is a digit. */
270 *unit = strtol(p, &endp, 10);
273 /* Known device name with unit number and nothing else. */
278 /* Device string is malformed beyond unit number. */
280 *type = DEV_TYP_NONE;
287 /* No slice and partition specification. */
291 /* Only DEV_TYP_STOR devices can have a slice specification. */
292 if (!(*type & DEV_TYP_STOR)) {
293 *type = DEV_TYP_NONE;
298 *slice = strtoul(p, &endp, 10);
300 /* Malformed slice number. */
302 *type = DEV_TYP_NONE;
304 *slice = D_SLICEWILD;
310 /* No partition specification. */
314 /* Device string is malformed beyond slice number. */
316 *type = DEV_TYP_NONE;
318 *slice = D_SLICEWILD;
324 /* No partition specification. */
328 *partition = strtol(p, &endp, 10);
331 /* Full, valid device string. */
335 /* Junk beyond partition number. */
336 *type = DEV_TYP_NONE;
338 *slice = D_SLICEWILD;
339 *partition = D_PARTWILD;
343 print_disk_probe_info()
348 if (currdev.d_disk.d_slice == D_SLICENONE)
349 strlcpy(slice, "<none>", sizeof(slice));
350 else if (currdev.d_disk.d_slice == D_SLICEWILD)
351 strlcpy(slice, "<auto>", sizeof(slice));
353 snprintf(slice, sizeof(slice), "%d", currdev.d_disk.d_slice);
355 if (currdev.d_disk.d_partition == D_PARTNONE)
356 strlcpy(partition, "<none>", sizeof(partition));
357 else if (currdev.d_disk.d_partition == D_PARTWILD)
358 strlcpy(partition, "<auto>", sizeof(partition));
360 snprintf(partition, sizeof(partition), "%d",
361 currdev.d_disk.d_partition);
363 printf(" Checking unit=%d slice=%s partition=%s...",
364 currdev.dd.d_unit, slice, partition);
369 probe_disks(int devidx, int load_type, int load_unit, int load_slice,
372 int open_result, unit;
375 currdev.d_disk.d_slice = load_slice;
376 currdev.d_disk.d_partition = load_partition;
378 f.f_devdata = &currdev;
381 if (load_type == -1) {
382 printf(" Probing all disk devices...\n");
383 /* Try each disk in succession until one works. */
384 for (currdev.dd.d_unit = 0; currdev.dd.d_unit < UB_MAX_DEV;
385 currdev.dd.d_unit++) {
386 print_disk_probe_info();
387 open_result = devsw[devidx]->dv_open(&f, &currdev);
388 if (open_result == 0) {
397 if (load_unit == -1) {
398 printf(" Probing all %s devices...\n", device_typename(load_type));
399 /* Try each disk of given type in succession until one works. */
400 for (unit = 0; unit < UB_MAX_DEV; unit++) {
401 currdev.dd.d_unit = uboot_diskgetunit(load_type, unit);
402 if (currdev.dd.d_unit == -1)
404 print_disk_probe_info();
405 open_result = devsw[devidx]->dv_open(&f, &currdev);
406 if (open_result == 0) {
415 if ((currdev.dd.d_unit = uboot_diskgetunit(load_type, load_unit)) != -1) {
416 print_disk_probe_info();
417 open_result = devsw[devidx]->dv_open(&f,&currdev);
418 if (open_result == 0) {
425 printf(" Requested disk type/unit/slice/partition not found\n");
430 main(int argc, char **argv)
432 struct api_signature *sig = NULL;
433 int load_type, load_unit, load_slice, load_partition;
438 * We first check if a command line argument was passed to us containing
439 * API's signature address. If it wasn't then we try to search for the
440 * API signature via the usual hinted address.
441 * If we can't find the magic signature and related info, exit with a
442 * unique error code that U-Boot reports as "## Application terminated,
443 * rc = 0xnnbadab1". Hopefully 'badab1' looks enough like "bad api" to
444 * provide a clue. It's better than 0xffffffff anyway.
446 if (!api_parse_cmdline_sig(argc, argv, &sig) && !api_search_sig(&sig))
449 syscall_ptr = sig->syscall;
450 if (syscall_ptr == NULL)
453 if (sig->version > API_SIG_VERSION)
456 /* Clear BSS sections */
457 bzero(__sbss_start, __sbss_end - __sbss_start);
458 bzero(__bss_start, _end - __bss_start);
461 * Initialise the heap as early as possible. Once this is done,
462 * alloc() is usable. We are using the stack u-boot set up near the top
463 * of physical ram; hopefully there is sufficient space between the end
464 * of our bss and the bottom of the u-boot stack to avoid overlap.
466 uboot_heap_start = round_page((uintptr_t)end);
467 uboot_heap_end = uboot_heap_start + HEAP_SIZE;
468 setheap((void *)uboot_heap_start, (void *)uboot_heap_end);
474 printf("Compatible U-Boot API signature found @%p\n", sig);
476 printf("\n%s", bootprog_info);
484 archsw.arch_loadaddr = uboot_loadaddr;
485 archsw.arch_getdev = uboot_getdev;
486 archsw.arch_copyin = uboot_copyin;
487 archsw.arch_copyout = uboot_copyout;
488 archsw.arch_readin = uboot_readin;
489 archsw.arch_autoload = uboot_autoload;
491 /* Set up currdev variable to have hooks in place. */
492 env_setenv("currdev", EV_VOLATILE, "", uboot_setcurrdev, env_nounset);
495 * Enumerate U-Boot devices
497 if ((devs_no = ub_dev_enum()) == 0) {
498 printf("no U-Boot devices found");
501 printf("Number of U-Boot devices: %d\n", devs_no);
503 get_load_device(&load_type, &load_unit, &load_slice, &load_partition);
506 * March through the device switch probing for things.
508 for (i = 0; devsw[i] != NULL; i++) {
510 if (devsw[i]->dv_init == NULL)
512 if ((devsw[i]->dv_init)() != 0)
515 printf("Found U-Boot device: %s\n", devsw[i]->dv_name);
517 currdev.dd.d_dev = devsw[i];
518 currdev.dd.d_unit = 0;
520 if ((load_type == DEV_TYP_NONE || (load_type & DEV_TYP_STOR)) &&
521 strcmp(devsw[i]->dv_name, "disk") == 0) {
522 if (probe_disks(i, load_type, load_unit, load_slice,
523 load_partition) == 0)
527 if ((load_type == DEV_TYP_NONE || (load_type & DEV_TYP_NET)) &&
528 strcmp(devsw[i]->dv_name, "net") == 0)
533 * If we couldn't find a boot device, return an error to u-boot.
534 * U-boot may be running a boot script that can try something different
535 * so returning an error is better than forcing a reboot.
537 if (devsw[i] == NULL) {
538 printf("No boot device found!\n");
542 ldev = devformat(&currdev.dd);
543 env_setenv("currdev", EV_VOLATILE, ldev, uboot_setcurrdev, env_nounset);
544 env_setenv("loaddev", EV_VOLATILE, ldev, env_noset, env_nounset);
545 printf("Booting from %s\n", ldev);
548 setenv("LINES", "24", 1); /* optional */
549 setenv("prompt", "loader>", 1);
551 setenv("usefdt", "1", 1);
554 interact(); /* doesn't return */
560 COMMAND_SET(heap, "heap", "show heap usage", command_heap);
562 command_heap(int argc, char *argv[])
565 printf("heap base at %p, top at %p, used %td\n", end, sbrk(0),
571 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
573 command_reboot(int argc, char *argv[])
576 printf("Resetting...\n");
579 printf("Reset failed!\n");
584 COMMAND_SET(devinfo, "devinfo", "show U-Boot devices", command_devinfo);
586 command_devinfo(int argc, char *argv[])
590 if ((devs_no = ub_dev_enum()) == 0) {
591 command_errmsg = "no U-Boot devices found!?";
595 printf("U-Boot devices:\n");
596 for (i = 0; i < devs_no; i++) {
603 COMMAND_SET(sysinfo, "sysinfo", "show U-Boot system info", command_sysinfo);
605 command_sysinfo(int argc, char *argv[])
609 if ((si = ub_get_sys_info()) == NULL) {
610 command_errmsg = "could not retrieve U-Boot sys info!?";
614 printf("U-Boot system info:\n");
626 handle_uboot_env_var(enum ubenv_action action, const char * var)
634 * On an import with the variable name formatted as ldname=ubname,
635 * import the uboot variable ubname into the loader variable ldname,
636 * otherwise the historical behavior is to import to uboot.ubname.
638 if (action == UBENV_IMPORT) {
639 len = strcspn(var, "=");
641 printf("name cannot start with '=': '%s'\n", var);
645 strcpy(ldvar, "uboot.");
646 strncat(ldvar, var, sizeof(ldvar) - 7);
648 len = MIN(len, sizeof(ldvar) - 1);
649 strncpy(ldvar, var, len);
656 * If the user prepended "uboot." (which is how they usually see these
657 * names) strip it off as a convenience.
659 if (strncmp(var, "uboot.", 6) == 0) {
663 /* If there is no variable name left, punt. */
665 printf("empty variable name\n");
669 val = ub_env_get(var);
670 if (action == UBENV_SHOW) {
672 printf("uboot.%s is not set\n", var);
674 printf("uboot.%s=%s\n", var, val);
675 } else if (action == UBENV_IMPORT) {
677 setenv(ldvar, val, 1);
683 command_ubenv(int argc, char *argv[])
685 enum ubenv_action action;
689 action = UBENV_UNKNOWN;
691 if (strcasecmp(argv[1], "import") == 0)
692 action = UBENV_IMPORT;
693 else if (strcasecmp(argv[1], "show") == 0)
696 if (action == UBENV_UNKNOWN) {
697 command_errmsg = "usage: 'ubenv <import|show> [var ...]";
702 for (i = 2; i < argc; i++)
703 handle_uboot_env_var(action, argv[i]);
707 if ((var = ub_env_enum(var)) == NULL)
709 handle_uboot_env_var(action, var);
715 COMMAND_SET(ubenv, "ubenv", "show or import U-Boot env vars", command_ubenv);
717 #ifdef LOADER_FDT_SUPPORT
719 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
720 * and declaring it as extern is in contradiction with COMMAND_SET() macro
721 * (which uses static pointer), we're defining wrapper function, which
722 * calls the proper fdt handling routine.
725 command_fdt(int argc, char *argv[])
728 return (command_fdt_internal(argc, argv));
731 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);