1 /*- 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> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 #include <sys/param.h> 32 33 #include <stand.h> 34 35 #include "api_public.h" 36 #include "bootstrap.h" 37 #include "glue.h" 38 #include "libuboot.h" 39 40 #ifndef nitems 41 #define nitems(x) (sizeof((x)) / sizeof((x)[0])) 42 #endif 43 44 #ifndef HEAP_SIZE 45 #define HEAP_SIZE (2 * 1024 * 1024) 46 #endif 47 48 struct uboot_devdesc currdev; 49 struct arch_switch archsw; /* MI/MD interface boundary */ 50 int devs_no; 51 52 uintptr_t uboot_heap_start; 53 uintptr_t uboot_heap_end; 54 55 struct device_type { 56 const char *name; 57 int type; 58 } device_types[] = { 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 } 66 }; 67 68 extern char end[]; 69 70 extern unsigned char _etext[]; 71 extern unsigned char _edata[]; 72 extern unsigned char __bss_start[]; 73 extern unsigned char __sbss_start[]; 74 extern unsigned char __sbss_end[]; 75 extern unsigned char _end[]; 76 77 #ifdef LOADER_FDT_SUPPORT 78 extern int command_fdt_internal(int argc, char *argv[]); 79 #endif 80 81 static void 82 dump_sig(struct api_signature *sig) 83 { 84 #ifdef DEBUG 85 printf("signature:\n"); 86 printf(" version\t= %d\n", sig->version); 87 printf(" checksum\t= 0x%08x\n", sig->checksum); 88 printf(" sc entry\t= 0x%08x\n", sig->syscall); 89 #endif 90 } 91 92 static void 93 dump_addr_info(void) 94 { 95 #ifdef DEBUG 96 printf("\naddresses info:\n"); 97 printf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext); 98 printf(" _edata = 0x%08x\n", (uint32_t)_edata); 99 printf(" __sbss_start = 0x%08x\n", (uint32_t)__sbss_start); 100 printf(" __sbss_end = 0x%08x\n", (uint32_t)__sbss_end); 101 printf(" __sbss_start = 0x%08x\n", (uint32_t)__bss_start); 102 printf(" _end = 0x%08x\n", (uint32_t)_end); 103 printf(" syscall entry = 0x%08x\n", (uint32_t)syscall_ptr); 104 #endif 105 } 106 107 static uint64_t 108 memsize(struct sys_info *si, int flags) 109 { 110 uint64_t size; 111 int i; 112 113 size = 0; 114 for (i = 0; i < si->mr_no; i++) 115 if (si->mr[i].flags == flags && si->mr[i].size) 116 size += (si->mr[i].size); 117 118 return (size); 119 } 120 121 static void 122 meminfo(void) 123 { 124 uint64_t size; 125 struct sys_info *si; 126 int t[3] = { MR_ATTR_DRAM, MR_ATTR_FLASH, MR_ATTR_SRAM }; 127 int i; 128 129 if ((si = ub_get_sys_info()) == NULL) 130 panic("could not retrieve system info"); 131 132 for (i = 0; i < 3; i++) { 133 size = memsize(si, t[i]); 134 if (size > 0) 135 printf("%s: %juMB\n", ub_mem_type(t[i]), 136 (uintmax_t)(size / 1024 / 1024)); 137 } 138 } 139 140 static const char * 141 get_device_type(const char *devstr, int *devtype) 142 { 143 int i; 144 int namelen; 145 struct device_type *dt; 146 147 if (devstr) { 148 for (i = 0; i < nitems(device_types); i++) { 149 dt = &device_types[i]; 150 namelen = strlen(dt->name); 151 if (strncmp(dt->name, devstr, namelen) == 0) { 152 *devtype = dt->type; 153 return (devstr + namelen); 154 } 155 } 156 printf("Unknown device type '%s'\n", devstr); 157 } 158 159 *devtype = DEV_TYP_NONE; 160 return (NULL); 161 } 162 163 static const char * 164 device_typename(int type) 165 { 166 int i; 167 168 for (i = 0; i < nitems(device_types); i++) 169 if (device_types[i].type == type) 170 return (device_types[i].name); 171 172 return ("<unknown>"); 173 } 174 175 /* 176 * Parse a device string into type, unit, slice and partition numbers. A 177 * returned value of -1 for type indicates a search should be done for the 178 * first loadable device, otherwise a returned value of -1 for unit 179 * indicates a search should be done for the first loadable device of the 180 * given type. 181 * 182 * The returned values for slice and partition are interpreted by 183 * disk_open(). 184 * 185 * The device string can be a standard loader(8) disk specifier: 186 * 187 * disk<unit>s<slice> disk0s1 188 * disk<unit>s<slice><partition> disk1s2a 189 * disk<unit>p<partition> disk0p4 190 * 191 * or one of the following formats: 192 * 193 * Valid device strings: For device types: 194 * 195 * <type_name> DEV_TYP_STOR, DEV_TYP_NET 196 * <type_name><unit> DEV_TYP_STOR, DEV_TYP_NET 197 * <type_name><unit>: DEV_TYP_STOR, DEV_TYP_NET 198 * <type_name><unit>:<slice> DEV_TYP_STOR 199 * <type_name><unit>:<slice>. DEV_TYP_STOR 200 * <type_name><unit>:<slice>.<partition> DEV_TYP_STOR 201 * 202 * For valid type names, see the device_types array, above. 203 * 204 * Slice numbers are 1-based. 0 is a wildcard. 205 */ 206 static void 207 get_load_device(int *type, int *unit, int *slice, int *partition) 208 { 209 struct disk_devdesc *dev; 210 char *devstr; 211 const char *p; 212 char *endp; 213 214 *type = DEV_TYP_NONE; 215 *unit = -1; 216 *slice = D_SLICEWILD; 217 *partition = D_PARTWILD; 218 219 devstr = ub_env_get("loaderdev"); 220 if (devstr == NULL) { 221 printf("U-Boot env: loaderdev not set, will probe all devices.\n"); 222 return; 223 } 224 printf("U-Boot env: loaderdev='%s'\n", devstr); 225 226 p = get_device_type(devstr, type); 227 228 /* 229 * If type is DEV_TYP_STOR we have a disk-like device. If the remainder 230 * of the string contains spaces, dots, or a colon in any location other 231 * than the last char, it's legacy format. Otherwise it might be 232 * standard loader(8) format (e.g., disk0s2a or mmc1p12), so try to 233 * parse the remainder of the string as such, and if it works, return 234 * those results. Otherwise we'll fall through to the code that parses 235 * the legacy format. 236 * 237 * disk_parsedev now assumes that it points to the start of the device 238 * name, but since it doesn't know about uboot's usage, just subtract 4 239 * since it always adds 4. This is the least-bad solution since it makes 240 * all the other loader code easier (might be better to create a fake 241 * 'disk...' string, but that's more work than uboot is worth). 242 */ 243 if (*type & DEV_TYP_STOR) { 244 size_t len = strlen(p); 245 if (strcspn(p, " .") == len && strcspn(p, ":") >= len - 1 && 246 disk_parsedev((struct devdesc **)&dev, p - 4, NULL) == 0) { /* Hack */ 247 *unit = dev->dd.d_unit; 248 *slice = dev->d_slice; 249 *partition = dev->d_partition; 250 free(dev); 251 return; 252 } 253 } 254 255 /* Ignore optional spaces after the device name. */ 256 while (*p == ' ') 257 p++; 258 259 /* Unknown device name, or a known name without unit number. */ 260 if ((*type == DEV_TYP_NONE) || (*p == '\0')) { 261 return; 262 } 263 264 /* Malformed unit number. */ 265 if (!isdigit(*p)) { 266 *type = DEV_TYP_NONE; 267 return; 268 } 269 270 /* Guaranteed to extract a number from the string, as *p is a digit. */ 271 *unit = strtol(p, &endp, 10); 272 p = endp; 273 274 /* Known device name with unit number and nothing else. */ 275 if (*p == '\0') { 276 return; 277 } 278 279 /* Device string is malformed beyond unit number. */ 280 if (*p != ':') { 281 *type = DEV_TYP_NONE; 282 *unit = -1; 283 return; 284 } 285 286 p++; 287 288 /* No slice and partition specification. */ 289 if ('\0' == *p ) 290 return; 291 292 /* Only DEV_TYP_STOR devices can have a slice specification. */ 293 if (!(*type & DEV_TYP_STOR)) { 294 *type = DEV_TYP_NONE; 295 *unit = -1; 296 return; 297 } 298 299 *slice = strtoul(p, &endp, 10); 300 301 /* Malformed slice number. */ 302 if (p == endp) { 303 *type = DEV_TYP_NONE; 304 *unit = -1; 305 *slice = D_SLICEWILD; 306 return; 307 } 308 309 p = endp; 310 311 /* No partition specification. */ 312 if (*p == '\0') 313 return; 314 315 /* Device string is malformed beyond slice number. */ 316 if (*p != '.') { 317 *type = DEV_TYP_NONE; 318 *unit = -1; 319 *slice = D_SLICEWILD; 320 return; 321 } 322 323 p++; 324 325 /* No partition specification. */ 326 if (*p == '\0') 327 return; 328 329 *partition = strtol(p, &endp, 10); 330 p = endp; 331 332 /* Full, valid device string. */ 333 if (*endp == '\0') 334 return; 335 336 /* Junk beyond partition number. */ 337 *type = DEV_TYP_NONE; 338 *unit = -1; 339 *slice = D_SLICEWILD; 340 *partition = D_PARTWILD; 341 } 342 343 static void 344 print_disk_probe_info() 345 { 346 char slice[32]; 347 char partition[32]; 348 349 if (currdev.d_disk.d_slice == D_SLICENONE) 350 strlcpy(slice, "<none>", sizeof(slice)); 351 else if (currdev.d_disk.d_slice == D_SLICEWILD) 352 strlcpy(slice, "<auto>", sizeof(slice)); 353 else 354 snprintf(slice, sizeof(slice), "%d", currdev.d_disk.d_slice); 355 356 if (currdev.d_disk.d_partition == D_PARTNONE) 357 strlcpy(partition, "<none>", sizeof(partition)); 358 else if (currdev.d_disk.d_partition == D_PARTWILD) 359 strlcpy(partition, "<auto>", sizeof(partition)); 360 else 361 snprintf(partition, sizeof(partition), "%d", 362 currdev.d_disk.d_partition); 363 364 printf(" Checking unit=%d slice=%s partition=%s...", 365 currdev.dd.d_unit, slice, partition); 366 367 } 368 369 static int 370 probe_disks(int devidx, int load_type, int load_unit, int load_slice, 371 int load_partition) 372 { 373 int open_result, unit; 374 struct open_file f; 375 376 currdev.d_disk.d_slice = load_slice; 377 currdev.d_disk.d_partition = load_partition; 378 379 f.f_devdata = &currdev; 380 open_result = -1; 381 382 if (load_type == -1) { 383 printf(" Probing all disk devices...\n"); 384 /* Try each disk in succession until one works. */ 385 for (currdev.dd.d_unit = 0; currdev.dd.d_unit < UB_MAX_DEV; 386 currdev.dd.d_unit++) { 387 print_disk_probe_info(); 388 open_result = devsw[devidx]->dv_open(&f, &currdev); 389 if (open_result == 0) { 390 printf(" good.\n"); 391 return (0); 392 } 393 printf("\n"); 394 } 395 return (-1); 396 } 397 398 if (load_unit == -1) { 399 printf(" Probing all %s devices...\n", device_typename(load_type)); 400 /* Try each disk of given type in succession until one works. */ 401 for (unit = 0; unit < UB_MAX_DEV; unit++) { 402 currdev.dd.d_unit = uboot_diskgetunit(load_type, unit); 403 if (currdev.dd.d_unit == -1) 404 break; 405 print_disk_probe_info(); 406 open_result = devsw[devidx]->dv_open(&f, &currdev); 407 if (open_result == 0) { 408 printf(" good.\n"); 409 return (0); 410 } 411 printf("\n"); 412 } 413 return (-1); 414 } 415 416 if ((currdev.dd.d_unit = uboot_diskgetunit(load_type, load_unit)) != -1) { 417 print_disk_probe_info(); 418 open_result = devsw[devidx]->dv_open(&f,&currdev); 419 if (open_result == 0) { 420 printf(" good.\n"); 421 return (0); 422 } 423 printf("\n"); 424 } 425 426 printf(" Requested disk type/unit/slice/partition not found\n"); 427 return (-1); 428 } 429 430 int 431 main(int argc, char **argv) 432 { 433 struct api_signature *sig = NULL; 434 int load_type, load_unit, load_slice, load_partition; 435 int i; 436 const char *ldev; 437 438 /* 439 * We first check if a command line argument was passed to us containing 440 * API's signature address. If it wasn't then we try to search for the 441 * API signature via the usual hinted address. 442 * If we can't find the magic signature and related info, exit with a 443 * unique error code that U-Boot reports as "## Application terminated, 444 * rc = 0xnnbadab1". Hopefully 'badab1' looks enough like "bad api" to 445 * provide a clue. It's better than 0xffffffff anyway. 446 */ 447 if (!api_parse_cmdline_sig(argc, argv, &sig) && !api_search_sig(&sig)) 448 return (0x01badab1); 449 450 syscall_ptr = sig->syscall; 451 if (syscall_ptr == NULL) 452 return (0x02badab1); 453 454 if (sig->version > API_SIG_VERSION) 455 return (0x03badab1); 456 457 /* Clear BSS sections */ 458 bzero(__sbss_start, __sbss_end - __sbss_start); 459 bzero(__bss_start, _end - __bss_start); 460 461 /* 462 * Initialise the heap as early as possible. Once this is done, 463 * alloc() is usable. We are using the stack u-boot set up near the top 464 * of physical ram; hopefully there is sufficient space between the end 465 * of our bss and the bottom of the u-boot stack to avoid overlap. 466 */ 467 uboot_heap_start = round_page((uintptr_t)end); 468 uboot_heap_end = uboot_heap_start + HEAP_SIZE; 469 setheap((void *)uboot_heap_start, (void *)uboot_heap_end); 470 471 /* 472 * Set up console. 473 */ 474 cons_probe(); 475 printf("Compatible U-Boot API signature found @%p\n", sig); 476 477 printf("\n%s", bootprog_info); 478 printf("\n"); 479 480 dump_sig(sig); 481 dump_addr_info(); 482 483 meminfo(); 484 485 archsw.arch_loadaddr = uboot_loadaddr; 486 archsw.arch_getdev = uboot_getdev; 487 archsw.arch_copyin = uboot_copyin; 488 archsw.arch_copyout = uboot_copyout; 489 archsw.arch_readin = uboot_readin; 490 archsw.arch_autoload = uboot_autoload; 491 492 /* Set up currdev variable to have hooks in place. */ 493 env_setenv("currdev", EV_VOLATILE, "", uboot_setcurrdev, env_nounset); 494 495 /* 496 * Enumerate U-Boot devices 497 */ 498 if ((devs_no = ub_dev_enum()) == 0) { 499 printf("no U-Boot devices found"); 500 goto do_interact; 501 } 502 printf("Number of U-Boot devices: %d\n", devs_no); 503 504 get_load_device(&load_type, &load_unit, &load_slice, &load_partition); 505 506 /* 507 * March through the device switch probing for things. 508 */ 509 for (i = 0; devsw[i] != NULL; i++) { 510 511 if (devsw[i]->dv_init == NULL) 512 continue; 513 if ((devsw[i]->dv_init)() != 0) 514 continue; 515 516 printf("Found U-Boot device: %s\n", devsw[i]->dv_name); 517 518 currdev.dd.d_dev = devsw[i]; 519 currdev.dd.d_unit = 0; 520 521 if ((load_type == DEV_TYP_NONE || (load_type & DEV_TYP_STOR)) && 522 strcmp(devsw[i]->dv_name, "disk") == 0) { 523 if (probe_disks(i, load_type, load_unit, load_slice, 524 load_partition) == 0) 525 break; 526 } 527 528 if ((load_type == DEV_TYP_NONE || (load_type & DEV_TYP_NET)) && 529 strcmp(devsw[i]->dv_name, "net") == 0) 530 break; 531 } 532 533 /* 534 * If we couldn't find a boot device, return an error to u-boot. 535 * U-boot may be running a boot script that can try something different 536 * so returning an error is better than forcing a reboot. 537 */ 538 if (devsw[i] == NULL) { 539 printf("No boot device found!\n"); 540 return (0xbadef1ce); 541 } 542 543 ldev = devformat(&currdev.dd); 544 env_setenv("currdev", EV_VOLATILE, ldev, uboot_setcurrdev, env_nounset); 545 env_setenv("loaddev", EV_VOLATILE, ldev, env_noset, env_nounset); 546 printf("Booting from %s\n", ldev); 547 548 do_interact: 549 setenv("LINES", "24", 1); /* optional */ 550 setenv("prompt", "loader>", 1); 551 #ifdef __powerpc__ 552 setenv("usefdt", "1", 1); 553 #endif 554 555 interact(); /* doesn't return */ 556 557 return (0); 558 } 559 560 561 COMMAND_SET(heap, "heap", "show heap usage", command_heap); 562 static int 563 command_heap(int argc, char *argv[]) 564 { 565 566 printf("heap base at %p, top at %p, used %td\n", end, sbrk(0), 567 sbrk(0) - end); 568 569 return (CMD_OK); 570 } 571 572 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 573 static int 574 command_reboot(int argc, char *argv[]) 575 { 576 577 printf("Resetting...\n"); 578 ub_reset(); 579 580 printf("Reset failed!\n"); 581 while (1); 582 __unreachable(); 583 } 584 585 COMMAND_SET(devinfo, "devinfo", "show U-Boot devices", command_devinfo); 586 static int 587 command_devinfo(int argc, char *argv[]) 588 { 589 int i; 590 591 if ((devs_no = ub_dev_enum()) == 0) { 592 command_errmsg = "no U-Boot devices found!?"; 593 return (CMD_ERROR); 594 } 595 596 printf("U-Boot devices:\n"); 597 for (i = 0; i < devs_no; i++) { 598 ub_dump_di(i); 599 printf("\n"); 600 } 601 return (CMD_OK); 602 } 603 604 COMMAND_SET(sysinfo, "sysinfo", "show U-Boot system info", command_sysinfo); 605 static int 606 command_sysinfo(int argc, char *argv[]) 607 { 608 struct sys_info *si; 609 610 if ((si = ub_get_sys_info()) == NULL) { 611 command_errmsg = "could not retrieve U-Boot sys info!?"; 612 return (CMD_ERROR); 613 } 614 615 printf("U-Boot system info:\n"); 616 ub_dump_si(si); 617 return (CMD_OK); 618 } 619 620 enum ubenv_action { 621 UBENV_UNKNOWN, 622 UBENV_SHOW, 623 UBENV_IMPORT 624 }; 625 626 static void 627 handle_uboot_env_var(enum ubenv_action action, const char * var) 628 { 629 char ldvar[128]; 630 const char *val; 631 char *wrk; 632 int len; 633 634 /* 635 * On an import with the variable name formatted as ldname=ubname, 636 * import the uboot variable ubname into the loader variable ldname, 637 * otherwise the historical behavior is to import to uboot.ubname. 638 */ 639 if (action == UBENV_IMPORT) { 640 len = strcspn(var, "="); 641 if (len == 0) { 642 printf("name cannot start with '=': '%s'\n", var); 643 return; 644 } 645 if (var[len] == 0) { 646 strcpy(ldvar, "uboot."); 647 strncat(ldvar, var, sizeof(ldvar) - 7); 648 } else { 649 len = MIN(len, sizeof(ldvar) - 1); 650 strncpy(ldvar, var, len); 651 ldvar[len] = 0; 652 var = &var[len + 1]; 653 } 654 } 655 656 /* 657 * If the user prepended "uboot." (which is how they usually see these 658 * names) strip it off as a convenience. 659 */ 660 if (strncmp(var, "uboot.", 6) == 0) { 661 var = &var[6]; 662 } 663 664 /* If there is no variable name left, punt. */ 665 if (var[0] == 0) { 666 printf("empty variable name\n"); 667 return; 668 } 669 670 val = ub_env_get(var); 671 if (action == UBENV_SHOW) { 672 if (val == NULL) 673 printf("uboot.%s is not set\n", var); 674 else 675 printf("uboot.%s=%s\n", var, val); 676 } else if (action == UBENV_IMPORT) { 677 if (val != NULL) { 678 setenv(ldvar, val, 1); 679 } 680 } 681 } 682 683 static int 684 command_ubenv(int argc, char *argv[]) 685 { 686 enum ubenv_action action; 687 const char *var; 688 int i; 689 690 action = UBENV_UNKNOWN; 691 if (argc > 1) { 692 if (strcasecmp(argv[1], "import") == 0) 693 action = UBENV_IMPORT; 694 else if (strcasecmp(argv[1], "show") == 0) 695 action = UBENV_SHOW; 696 } 697 if (action == UBENV_UNKNOWN) { 698 command_errmsg = "usage: 'ubenv <import|show> [var ...]"; 699 return (CMD_ERROR); 700 } 701 702 if (argc > 2) { 703 for (i = 2; i < argc; i++) 704 handle_uboot_env_var(action, argv[i]); 705 } else { 706 var = NULL; 707 for (;;) { 708 if ((var = ub_env_enum(var)) == NULL) 709 break; 710 handle_uboot_env_var(action, var); 711 } 712 } 713 714 return (CMD_OK); 715 } 716 COMMAND_SET(ubenv, "ubenv", "show or import U-Boot env vars", command_ubenv); 717 718 #ifdef LOADER_FDT_SUPPORT 719 /* 720 * Since proper fdt command handling function is defined in fdt_loader_cmd.c, 721 * and declaring it as extern is in contradiction with COMMAND_SET() macro 722 * (which uses static pointer), we're defining wrapper function, which 723 * calls the proper fdt handling routine. 724 */ 725 static int 726 command_fdt(int argc, char *argv[]) 727 { 728 729 return (command_fdt_internal(argc, argv)); 730 } 731 732 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt); 733 #endif 734