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 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
35 #include "api_public.h"
36 #include "bootstrap.h"
41 #define nitems(x) (sizeof((x)) / sizeof((x)[0]))
44 struct uboot_devdesc currdev;
45 struct arch_switch archsw; /* MI/MD interface boundary */
48 uintptr_t uboot_heap_start;
49 uintptr_t uboot_heap_end;
55 { "disk", DEV_TYP_STOR },
56 { "ide", DEV_TYP_STOR | DT_STOR_IDE },
57 { "mmc", DEV_TYP_STOR | DT_STOR_MMC },
58 { "sata", DEV_TYP_STOR | DT_STOR_SATA },
59 { "scsi", DEV_TYP_STOR | DT_STOR_SCSI },
60 { "usb", DEV_TYP_STOR | DT_STOR_USB },
61 { "net", DEV_TYP_NET }
65 extern char bootprog_info[];
67 extern unsigned char _etext[];
68 extern unsigned char _edata[];
69 extern unsigned char __bss_start[];
70 extern unsigned char __sbss_start[];
71 extern unsigned char __sbss_end[];
72 extern unsigned char _end[];
74 #ifdef LOADER_FDT_SUPPORT
75 extern int command_fdt_internal(int argc, char *argv[]);
79 dump_sig(struct api_signature *sig)
82 printf("signature:\n");
83 printf(" version\t= %d\n", sig->version);
84 printf(" checksum\t= 0x%08x\n", sig->checksum);
85 printf(" sc entry\t= 0x%08x\n", sig->syscall);
93 printf("\naddresses info:\n");
94 printf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext);
95 printf(" _edata = 0x%08x\n", (uint32_t)_edata);
96 printf(" __sbss_start = 0x%08x\n", (uint32_t)__sbss_start);
97 printf(" __sbss_end = 0x%08x\n", (uint32_t)__sbss_end);
98 printf(" __sbss_start = 0x%08x\n", (uint32_t)__bss_start);
99 printf(" _end = 0x%08x\n", (uint32_t)_end);
100 printf(" syscall entry = 0x%08x\n", (uint32_t)syscall_ptr);
105 memsize(struct sys_info *si, int flags)
111 for (i = 0; i < si->mr_no; i++)
112 if (si->mr[i].flags == flags && si->mr[i].size)
113 size += (si->mr[i].size);
123 int t[3] = { MR_ATTR_DRAM, MR_ATTR_FLASH, MR_ATTR_SRAM };
126 if ((si = ub_get_sys_info()) == NULL)
127 panic("could not retrieve system info");
129 for (i = 0; i < 3; i++) {
130 size = memsize(si, t[i]);
132 printf("%s: %juMB\n", ub_mem_type(t[i]),
133 (uintmax_t)(size / 1024 / 1024));
138 get_device_type(const char *devstr, int *devtype)
142 struct device_type *dt;
145 for (i = 0; i < nitems(device_types); i++) {
146 dt = &device_types[i];
147 namelen = strlen(dt->name);
148 if (strncmp(dt->name, devstr, namelen) == 0) {
150 return (devstr + namelen);
153 printf("Unknown device type '%s'\n", devstr);
161 device_typename(int type)
165 for (i = 0; i < nitems(device_types); i++)
166 if (device_types[i].type == type)
167 return (device_types[i].name);
169 return ("<unknown>");
173 * Parse a device string into type, unit, slice and partition numbers. A
174 * returned value of -1 for type indicates a search should be done for the
175 * first loadable device, otherwise a returned value of -1 for unit
176 * indicates a search should be done for the first loadable device of the
179 * The returned values for slice and partition are interpreted by
182 * Valid device strings: For device types:
184 * <type_name> DEV_TYP_STOR, DEV_TYP_NET
185 * <type_name><unit> DEV_TYP_STOR, DEV_TYP_NET
186 * <type_name><unit>: DEV_TYP_STOR, DEV_TYP_NET
187 * <type_name><unit>:<slice> DEV_TYP_STOR
188 * <type_name><unit>:<slice>. DEV_TYP_STOR
189 * <type_name><unit>:<slice>.<partition> DEV_TYP_STOR
191 * For valid type names, see the device_types array, above.
193 * Slice numbers are 1-based. 0 is a wildcard.
196 get_load_device(int *type, int *unit, int *slice, int *partition)
207 devstr = ub_env_get("loaderdev");
208 if (devstr == NULL) {
209 printf("U-Boot env: loaderdev not set, will probe all devices.\n");
212 printf("U-Boot env: loaderdev='%s'\n", devstr);
214 p = get_device_type(devstr, type);
216 /* Ignore optional spaces after the device name. */
220 /* Unknown device name, or a known name without unit number. */
221 if ((*type == -1) || (*p == '\0')) {
225 /* Malformed unit number. */
231 /* Guaranteed to extract a number from the string, as *p is a digit. */
232 *unit = strtol(p, &endp, 10);
235 /* Known device name with unit number and nothing else. */
240 /* Device string is malformed beyond unit number. */
249 /* No slice and partition specification. */
253 /* Only DEV_TYP_STOR devices can have a slice specification. */
254 if (!(*type & DEV_TYP_STOR)) {
260 *slice = strtoul(p, &endp, 10);
262 /* Malformed slice number. */
272 /* No partition specification. */
276 /* Device string is malformed beyond slice number. */
286 /* No partition specification. */
290 *partition = strtol(p, &endp, 10);
293 /* Full, valid device string. */
297 /* Junk beyond partition number. */
305 print_disk_probe_info()
310 if (currdev.d_disk.slice > 0)
311 sprintf(slice, "%d", currdev.d_disk.slice);
313 strcpy(slice, "<auto>");
315 if (currdev.d_disk.partition >= 0)
316 sprintf(partition, "%d", currdev.d_disk.partition);
318 strcpy(partition, "<auto>");
320 printf(" Checking unit=%d slice=%s partition=%s...",
321 currdev.d_unit, slice, partition);
326 probe_disks(int devidx, int load_type, int load_unit, int load_slice,
329 int open_result, unit;
332 currdev.d_disk.slice = load_slice;
333 currdev.d_disk.partition = load_partition;
335 f.f_devdata = &currdev;
338 if (load_type == -1) {
339 printf(" Probing all disk devices...\n");
340 /* Try each disk in succession until one works. */
341 for (currdev.d_unit = 0; currdev.d_unit < UB_MAX_DEV;
343 print_disk_probe_info();
344 open_result = devsw[devidx]->dv_open(&f, &currdev);
345 if (open_result == 0) {
354 if (load_unit == -1) {
355 printf(" Probing all %s devices...\n", device_typename(load_type));
356 /* Try each disk of given type in succession until one works. */
357 for (unit = 0; unit < UB_MAX_DEV; unit++) {
358 currdev.d_unit = uboot_diskgetunit(load_type, unit);
359 if (currdev.d_unit == -1)
361 print_disk_probe_info();
362 open_result = devsw[devidx]->dv_open(&f, &currdev);
363 if (open_result == 0) {
372 if ((currdev.d_unit = uboot_diskgetunit(load_type, load_unit)) != -1) {
373 print_disk_probe_info();
374 open_result = devsw[devidx]->dv_open(&f,&currdev);
375 if (open_result == 0) {
382 printf(" Requested disk type/unit/slice/partition not found\n");
387 main(int argc, char **argv)
389 struct api_signature *sig = NULL;
390 int load_type, load_unit, load_slice, load_partition;
395 * We first check if a command line argument was passed to us containing
396 * API's signature address. If it wasn't then we try to search for the
397 * API signature via the usual hinted address.
398 * If we can't find the magic signature and related info, exit with a
399 * unique error code that U-Boot reports as "## Application terminated,
400 * rc = 0xnnbadab1". Hopefully 'badab1' looks enough like "bad api" to
401 * provide a clue. It's better than 0xffffffff anyway.
403 if (!api_parse_cmdline_sig(argc, argv, &sig) && !api_search_sig(&sig))
406 syscall_ptr = sig->syscall;
407 if (syscall_ptr == NULL)
410 if (sig->version > API_SIG_VERSION)
413 /* Clear BSS sections */
414 bzero(__sbss_start, __sbss_end - __sbss_start);
415 bzero(__bss_start, _end - __bss_start);
418 * Initialise the heap as early as possible. Once this is done,
419 * alloc() is usable. The stack is buried inside us, so this is safe.
421 uboot_heap_start = round_page((uintptr_t)end);
422 uboot_heap_end = uboot_heap_start + 512 * 1024;
423 setheap((void *)uboot_heap_start, (void *)uboot_heap_end);
429 printf("Compatible U-Boot API signature found @%p\n", sig);
431 printf("\n%s", bootprog_info);
440 * Enumerate U-Boot devices
442 if ((devs_no = ub_dev_enum()) == 0)
443 panic("no U-Boot devices found");
444 printf("Number of U-Boot devices: %d\n", devs_no);
446 get_load_device(&load_type, &load_unit, &load_slice, &load_partition);
449 * March through the device switch probing for things.
451 for (i = 0; devsw[i] != NULL; i++) {
453 if (devsw[i]->dv_init == NULL)
455 if ((devsw[i]->dv_init)() != 0)
458 printf("Found U-Boot device: %s\n", devsw[i]->dv_name);
460 currdev.d_dev = devsw[i];
461 currdev.d_type = currdev.d_dev->dv_type;
464 if ((load_type == -1 || (load_type & DEV_TYP_STOR)) &&
465 strcmp(devsw[i]->dv_name, "disk") == 0) {
466 if (probe_disks(i, load_type, load_unit, load_slice,
467 load_partition) == 0)
471 if ((load_type == -1 || (load_type & DEV_TYP_NET)) &&
472 strcmp(devsw[i]->dv_name, "net") == 0)
477 * If we couldn't find a boot device, return an error to u-boot.
478 * U-boot may be running a boot script that can try something different
479 * so returning an error is better than forcing a reboot.
481 if (devsw[i] == NULL) {
482 printf("No boot device found!\n");
486 ldev = uboot_fmtdev(&currdev);
487 env_setenv("currdev", EV_VOLATILE, ldev, uboot_setcurrdev, env_nounset);
488 env_setenv("loaddev", EV_VOLATILE, ldev, env_noset, env_nounset);
489 printf("Booting from %s\n", ldev);
491 setenv("LINES", "24", 1); /* optional */
492 setenv("prompt", "loader>", 1);
494 archsw.arch_loadaddr = uboot_loadaddr;
495 archsw.arch_getdev = uboot_getdev;
496 archsw.arch_copyin = uboot_copyin;
497 archsw.arch_copyout = uboot_copyout;
498 archsw.arch_readin = uboot_readin;
499 archsw.arch_autoload = uboot_autoload;
501 interact(NULL); /* doesn't return */
507 COMMAND_SET(heap, "heap", "show heap usage", command_heap);
509 command_heap(int argc, char *argv[])
512 printf("heap base at %p, top at %p, used %td\n", end, sbrk(0),
518 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
520 command_reboot(int argc, char *argv[])
523 printf("Resetting...\n");
526 printf("Reset failed!\n");
530 COMMAND_SET(devinfo, "devinfo", "show U-Boot devices", command_devinfo);
532 command_devinfo(int argc, char *argv[])
536 if ((devs_no = ub_dev_enum()) == 0) {
537 command_errmsg = "no U-Boot devices found!?";
541 printf("U-Boot devices:\n");
542 for (i = 0; i < devs_no; i++) {
549 COMMAND_SET(sysinfo, "sysinfo", "show U-Boot system info", command_sysinfo);
551 command_sysinfo(int argc, char *argv[])
555 if ((si = ub_get_sys_info()) == NULL) {
556 command_errmsg = "could not retrieve U-Boot sys info!?";
560 printf("U-Boot system info:\n");
572 handle_uboot_env_var(enum ubenv_action action, const char * var)
580 * On an import with the variable name formatted as ldname=ubname,
581 * import the uboot variable ubname into the loader variable ldname,
582 * otherwise the historical behavior is to import to uboot.ubname.
584 if (action == UBENV_IMPORT) {
585 len = strcspn(var, "=");
587 printf("name cannot start with '=': '%s'\n", var);
591 strcpy(ldvar, "uboot.");
592 strncat(ldvar, var, sizeof(ldvar) - 7);
594 len = MIN(len, sizeof(ldvar) - 1);
595 strncpy(ldvar, var, len);
602 * If the user prepended "uboot." (which is how they usually see these
603 * names) strip it off as a convenience.
605 if (strncmp(var, "uboot.", 6) == 0) {
609 /* If there is no variable name left, punt. */
611 printf("empty variable name\n");
615 val = ub_env_get(var);
616 if (action == UBENV_SHOW) {
618 printf("uboot.%s is not set\n", var);
620 printf("uboot.%s=%s\n", var, val);
621 } else if (action == UBENV_IMPORT) {
623 setenv(ldvar, val, 1);
629 command_ubenv(int argc, char *argv[])
631 enum ubenv_action action;
635 action = UBENV_UNKNOWN;
637 if (strcasecmp(argv[1], "import") == 0)
638 action = UBENV_IMPORT;
639 else if (strcasecmp(argv[1], "show") == 0)
642 if (action == UBENV_UNKNOWN) {
643 command_errmsg = "usage: 'ubenv <import|show> [var ...]";
648 for (i = 2; i < argc; i++)
649 handle_uboot_env_var(action, argv[i]);
653 if ((var = ub_env_enum(var)) == NULL)
655 handle_uboot_env_var(action, var);
661 COMMAND_SET(ubenv, "ubenv", "show or import U-Boot env vars", command_ubenv);
663 #ifdef LOADER_FDT_SUPPORT
665 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
666 * and declaring it as extern is in contradiction with COMMAND_SET() macro
667 * (which uses static pointer), we're defining wrapper function, which
668 * calls the proper fdt handling routine.
671 command_fdt(int argc, char *argv[])
674 return (command_fdt_internal(argc, argv));
677 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);