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]))
45 #define HEAP_SIZE (1 * 1024 * 1024)
48 struct uboot_devdesc currdev;
49 struct arch_switch archsw; /* MI/MD interface boundary */
52 uintptr_t uboot_heap_start;
53 uintptr_t uboot_heap_end;
59 { "disk", DEV_TYP_STOR },
60 { "ide", DEV_TYP_STOR | DT_STOR_IDE },
61 { "mmc", DEV_TYP_STOR | DT_STOR_MMC },
62 { "sata", DEV_TYP_STOR | DT_STOR_SATA },
63 { "scsi", DEV_TYP_STOR | DT_STOR_SCSI },
64 { "usb", DEV_TYP_STOR | DT_STOR_USB },
65 { "net", DEV_TYP_NET }
69 extern char bootprog_info[];
71 extern unsigned char _etext[];
72 extern unsigned char _edata[];
73 extern unsigned char __bss_start[];
74 extern unsigned char __sbss_start[];
75 extern unsigned char __sbss_end[];
76 extern unsigned char _end[];
78 #ifdef LOADER_FDT_SUPPORT
79 extern int command_fdt_internal(int argc, char *argv[]);
83 dump_sig(struct api_signature *sig)
86 printf("signature:\n");
87 printf(" version\t= %d\n", sig->version);
88 printf(" checksum\t= 0x%08x\n", sig->checksum);
89 printf(" sc entry\t= 0x%08x\n", sig->syscall);
97 printf("\naddresses info:\n");
98 printf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext);
99 printf(" _edata = 0x%08x\n", (uint32_t)_edata);
100 printf(" __sbss_start = 0x%08x\n", (uint32_t)__sbss_start);
101 printf(" __sbss_end = 0x%08x\n", (uint32_t)__sbss_end);
102 printf(" __sbss_start = 0x%08x\n", (uint32_t)__bss_start);
103 printf(" _end = 0x%08x\n", (uint32_t)_end);
104 printf(" syscall entry = 0x%08x\n", (uint32_t)syscall_ptr);
109 memsize(struct sys_info *si, int flags)
115 for (i = 0; i < si->mr_no; i++)
116 if (si->mr[i].flags == flags && si->mr[i].size)
117 size += (si->mr[i].size);
127 int t[3] = { MR_ATTR_DRAM, MR_ATTR_FLASH, MR_ATTR_SRAM };
130 if ((si = ub_get_sys_info()) == NULL)
131 panic("could not retrieve system info");
133 for (i = 0; i < 3; i++) {
134 size = memsize(si, t[i]);
136 printf("%s: %juMB\n", ub_mem_type(t[i]),
137 (uintmax_t)(size / 1024 / 1024));
142 get_device_type(const char *devstr, int *devtype)
146 struct device_type *dt;
149 for (i = 0; i < nitems(device_types); i++) {
150 dt = &device_types[i];
151 namelen = strlen(dt->name);
152 if (strncmp(dt->name, devstr, namelen) == 0) {
154 return (devstr + namelen);
157 printf("Unknown device type '%s'\n", devstr);
165 device_typename(int type)
169 for (i = 0; i < nitems(device_types); i++)
170 if (device_types[i].type == type)
171 return (device_types[i].name);
173 return ("<unknown>");
177 * Parse a device string into type, unit, slice and partition numbers. A
178 * returned value of -1 for type indicates a search should be done for the
179 * first loadable device, otherwise a returned value of -1 for unit
180 * indicates a search should be done for the first loadable device of the
183 * The returned values for slice and partition are interpreted by
186 * Valid device strings: For device types:
188 * <type_name> DEV_TYP_STOR, DEV_TYP_NET
189 * <type_name><unit> DEV_TYP_STOR, DEV_TYP_NET
190 * <type_name><unit>: DEV_TYP_STOR, DEV_TYP_NET
191 * <type_name><unit>:<slice> DEV_TYP_STOR
192 * <type_name><unit>:<slice>. DEV_TYP_STOR
193 * <type_name><unit>:<slice>.<partition> DEV_TYP_STOR
195 * For valid type names, see the device_types array, above.
197 * Slice numbers are 1-based. 0 is a wildcard.
200 get_load_device(int *type, int *unit, int *slice, int *partition)
211 devstr = ub_env_get("loaderdev");
212 if (devstr == NULL) {
213 printf("U-Boot env: loaderdev not set, will probe all devices.\n");
216 printf("U-Boot env: loaderdev='%s'\n", devstr);
218 p = get_device_type(devstr, type);
220 /* Ignore optional spaces after the device name. */
224 /* Unknown device name, or a known name without unit number. */
225 if ((*type == -1) || (*p == '\0')) {
229 /* Malformed unit number. */
235 /* Guaranteed to extract a number from the string, as *p is a digit. */
236 *unit = strtol(p, &endp, 10);
239 /* Known device name with unit number and nothing else. */
244 /* Device string is malformed beyond unit number. */
253 /* No slice and partition specification. */
257 /* Only DEV_TYP_STOR devices can have a slice specification. */
258 if (!(*type & DEV_TYP_STOR)) {
264 *slice = strtoul(p, &endp, 10);
266 /* Malformed slice number. */
276 /* No partition specification. */
280 /* Device string is malformed beyond slice number. */
290 /* No partition specification. */
294 *partition = strtol(p, &endp, 10);
297 /* Full, valid device string. */
301 /* Junk beyond partition number. */
309 print_disk_probe_info()
314 if (currdev.d_disk.slice > 0)
315 sprintf(slice, "%d", currdev.d_disk.slice);
317 strcpy(slice, "<auto>");
319 if (currdev.d_disk.partition >= 0)
320 sprintf(partition, "%d", currdev.d_disk.partition);
322 strcpy(partition, "<auto>");
324 printf(" Checking unit=%d slice=%s partition=%s...",
325 currdev.dd.d_unit, slice, partition);
330 probe_disks(int devidx, int load_type, int load_unit, int load_slice,
333 int open_result, unit;
336 currdev.d_disk.slice = load_slice;
337 currdev.d_disk.partition = load_partition;
339 f.f_devdata = &currdev;
342 if (load_type == -1) {
343 printf(" Probing all disk devices...\n");
344 /* Try each disk in succession until one works. */
345 for (currdev.dd.d_unit = 0; currdev.dd.d_unit < UB_MAX_DEV;
346 currdev.dd.d_unit++) {
347 print_disk_probe_info();
348 open_result = devsw[devidx]->dv_open(&f, &currdev);
349 if (open_result == 0) {
358 if (load_unit == -1) {
359 printf(" Probing all %s devices...\n", device_typename(load_type));
360 /* Try each disk of given type in succession until one works. */
361 for (unit = 0; unit < UB_MAX_DEV; unit++) {
362 currdev.dd.d_unit = uboot_diskgetunit(load_type, unit);
363 if (currdev.dd.d_unit == -1)
365 print_disk_probe_info();
366 open_result = devsw[devidx]->dv_open(&f, &currdev);
367 if (open_result == 0) {
376 if ((currdev.dd.d_unit = uboot_diskgetunit(load_type, load_unit)) != -1) {
377 print_disk_probe_info();
378 open_result = devsw[devidx]->dv_open(&f,&currdev);
379 if (open_result == 0) {
386 printf(" Requested disk type/unit/slice/partition not found\n");
391 main(int argc, char **argv)
393 struct api_signature *sig = NULL;
394 int load_type, load_unit, load_slice, load_partition;
399 * We first check if a command line argument was passed to us containing
400 * API's signature address. If it wasn't then we try to search for the
401 * API signature via the usual hinted address.
402 * If we can't find the magic signature and related info, exit with a
403 * unique error code that U-Boot reports as "## Application terminated,
404 * rc = 0xnnbadab1". Hopefully 'badab1' looks enough like "bad api" to
405 * provide a clue. It's better than 0xffffffff anyway.
407 if (!api_parse_cmdline_sig(argc, argv, &sig) && !api_search_sig(&sig))
410 syscall_ptr = sig->syscall;
411 if (syscall_ptr == NULL)
414 if (sig->version > API_SIG_VERSION)
417 /* Clear BSS sections */
418 bzero(__sbss_start, __sbss_end - __sbss_start);
419 bzero(__bss_start, _end - __bss_start);
422 * Initialise the heap as early as possible. Once this is done,
423 * alloc() is usable. We are using the stack u-boot set up near the top
424 * of physical ram; hopefully there is sufficient space between the end
425 * of our bss and the bottom of the u-boot stack to avoid overlap.
427 uboot_heap_start = round_page((uintptr_t)end);
428 uboot_heap_end = uboot_heap_start + HEAP_SIZE;
429 setheap((void *)uboot_heap_start, (void *)uboot_heap_end);
435 printf("Compatible U-Boot API signature found @%p\n", sig);
437 printf("\n%s", bootprog_info);
446 * Enumerate U-Boot devices
448 if ((devs_no = ub_dev_enum()) == 0)
449 panic("no U-Boot devices found");
450 printf("Number of U-Boot devices: %d\n", devs_no);
452 get_load_device(&load_type, &load_unit, &load_slice, &load_partition);
455 * March through the device switch probing for things.
457 for (i = 0; devsw[i] != NULL; i++) {
459 if (devsw[i]->dv_init == NULL)
461 if ((devsw[i]->dv_init)() != 0)
464 printf("Found U-Boot device: %s\n", devsw[i]->dv_name);
466 currdev.dd.d_dev = devsw[i];
467 currdev.dd.d_unit = 0;
469 if ((load_type == -1 || (load_type & DEV_TYP_STOR)) &&
470 strcmp(devsw[i]->dv_name, "disk") == 0) {
471 if (probe_disks(i, load_type, load_unit, load_slice,
472 load_partition) == 0)
476 if ((load_type == -1 || (load_type & DEV_TYP_NET)) &&
477 strcmp(devsw[i]->dv_name, "net") == 0)
482 * If we couldn't find a boot device, return an error to u-boot.
483 * U-boot may be running a boot script that can try something different
484 * so returning an error is better than forcing a reboot.
486 if (devsw[i] == NULL) {
487 printf("No boot device found!\n");
491 ldev = uboot_fmtdev(&currdev);
492 env_setenv("currdev", EV_VOLATILE, ldev, uboot_setcurrdev, env_nounset);
493 env_setenv("loaddev", EV_VOLATILE, ldev, env_noset, env_nounset);
494 printf("Booting from %s\n", ldev);
496 setenv("LINES", "24", 1); /* optional */
497 setenv("prompt", "loader>", 1);
499 archsw.arch_loadaddr = uboot_loadaddr;
500 archsw.arch_getdev = uboot_getdev;
501 archsw.arch_copyin = uboot_copyin;
502 archsw.arch_copyout = uboot_copyout;
503 archsw.arch_readin = uboot_readin;
504 archsw.arch_autoload = uboot_autoload;
506 interact(); /* doesn't return */
512 COMMAND_SET(heap, "heap", "show heap usage", command_heap);
514 command_heap(int argc, char *argv[])
517 printf("heap base at %p, top at %p, used %td\n", end, sbrk(0),
523 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
525 command_reboot(int argc, char *argv[])
528 printf("Resetting...\n");
531 printf("Reset failed!\n");
536 COMMAND_SET(devinfo, "devinfo", "show U-Boot devices", command_devinfo);
538 command_devinfo(int argc, char *argv[])
542 if ((devs_no = ub_dev_enum()) == 0) {
543 command_errmsg = "no U-Boot devices found!?";
547 printf("U-Boot devices:\n");
548 for (i = 0; i < devs_no; i++) {
555 COMMAND_SET(sysinfo, "sysinfo", "show U-Boot system info", command_sysinfo);
557 command_sysinfo(int argc, char *argv[])
561 if ((si = ub_get_sys_info()) == NULL) {
562 command_errmsg = "could not retrieve U-Boot sys info!?";
566 printf("U-Boot system info:\n");
578 handle_uboot_env_var(enum ubenv_action action, const char * var)
586 * On an import with the variable name formatted as ldname=ubname,
587 * import the uboot variable ubname into the loader variable ldname,
588 * otherwise the historical behavior is to import to uboot.ubname.
590 if (action == UBENV_IMPORT) {
591 len = strcspn(var, "=");
593 printf("name cannot start with '=': '%s'\n", var);
597 strcpy(ldvar, "uboot.");
598 strncat(ldvar, var, sizeof(ldvar) - 7);
600 len = MIN(len, sizeof(ldvar) - 1);
601 strncpy(ldvar, var, len);
608 * If the user prepended "uboot." (which is how they usually see these
609 * names) strip it off as a convenience.
611 if (strncmp(var, "uboot.", 6) == 0) {
615 /* If there is no variable name left, punt. */
617 printf("empty variable name\n");
621 val = ub_env_get(var);
622 if (action == UBENV_SHOW) {
624 printf("uboot.%s is not set\n", var);
626 printf("uboot.%s=%s\n", var, val);
627 } else if (action == UBENV_IMPORT) {
629 setenv(ldvar, val, 1);
635 command_ubenv(int argc, char *argv[])
637 enum ubenv_action action;
641 action = UBENV_UNKNOWN;
643 if (strcasecmp(argv[1], "import") == 0)
644 action = UBENV_IMPORT;
645 else if (strcasecmp(argv[1], "show") == 0)
648 if (action == UBENV_UNKNOWN) {
649 command_errmsg = "usage: 'ubenv <import|show> [var ...]";
654 for (i = 2; i < argc; i++)
655 handle_uboot_env_var(action, argv[i]);
659 if ((var = ub_env_enum(var)) == NULL)
661 handle_uboot_env_var(action, var);
667 COMMAND_SET(ubenv, "ubenv", "show or import U-Boot env vars", command_ubenv);
669 #ifdef LOADER_FDT_SUPPORT
671 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
672 * and declaring it as extern is in contradiction with COMMAND_SET() macro
673 * (which uses static pointer), we're defining wrapper function, which
674 * calls the proper fdt handling routine.
677 command_fdt(int argc, char *argv[])
680 return (command_fdt_internal(argc, argv));
683 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);