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/param.h> 30 31 #include <stand.h> 32 33 #include "api_public.h" 34 #include "bootstrap.h" 35 #include "glue.h" 36 #include "libuboot.h" 37 38 #ifndef HEAP_SIZE 39 #define HEAP_SIZE (2 * 1024 * 1024) 40 #endif 41 42 struct uboot_devdesc currdev; 43 struct arch_switch archsw = { /* MI/MD interface boundary */ 44 .arch_getdev = uboot_getdev, 45 .arch_copyin = uboot_copyin, 46 .arch_copyout = uboot_copyout, 47 .arch_readin = uboot_readin, 48 .arch_autoload = uboot_autoload, 49 }; 50 51 int devs_no; 52 53 uintptr_t uboot_heap_start; 54 uintptr_t uboot_heap_end; 55 56 struct device_type { 57 const char *name; 58 int type; 59 } device_types[] = { 60 { "disk", DEV_TYP_STOR }, 61 { "ide", DEV_TYP_STOR | DT_STOR_IDE }, 62 { "mmc", DEV_TYP_STOR | DT_STOR_MMC }, 63 { "sata", DEV_TYP_STOR | DT_STOR_SATA }, 64 { "scsi", DEV_TYP_STOR | DT_STOR_SCSI }, 65 { "usb", DEV_TYP_STOR | DT_STOR_USB }, 66 { "net", DEV_TYP_NET } 67 }; 68 69 extern char end[]; 70 71 extern unsigned char __bss_start[]; 72 extern unsigned char __sbss_start[]; 73 extern unsigned char __sbss_end[]; 74 extern unsigned char _end[]; 75 76 #ifdef LOADER_FDT_SUPPORT 77 extern int command_fdt_internal(int argc, char *argv[]); 78 #endif 79 80 static void 81 dump_sig(struct api_signature *sig) 82 { 83 #ifdef DEBUG 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); 88 #endif 89 } 90 91 static void 92 dump_addr_info(void) 93 { 94 #ifdef DEBUG 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); 103 #endif 104 } 105 106 static uint64_t 107 memsize(struct sys_info *si, int flags) 108 { 109 uint64_t size; 110 int i; 111 112 size = 0; 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); 116 117 return (size); 118 } 119 120 static void 121 meminfo(void) 122 { 123 uint64_t size; 124 struct sys_info *si; 125 int t[3] = { MR_ATTR_DRAM, MR_ATTR_FLASH, MR_ATTR_SRAM }; 126 int i; 127 128 if ((si = ub_get_sys_info()) == NULL) 129 panic("could not retrieve system info"); 130 131 for (i = 0; i < 3; i++) { 132 size = memsize(si, t[i]); 133 if (size > 0) 134 printf("%s: %juMB\n", ub_mem_type(t[i]), 135 (uintmax_t)(size / 1024 / 1024)); 136 } 137 } 138 139 static const char * 140 get_device_type(const char *devstr, int *devtype) 141 { 142 int i; 143 int namelen; 144 struct device_type *dt; 145 146 if (devstr) { 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) { 151 *devtype = dt->type; 152 return (devstr + namelen); 153 } 154 } 155 printf("Unknown device type '%s'\n", devstr); 156 } 157 158 *devtype = DEV_TYP_NONE; 159 return (NULL); 160 } 161 162 static const char * 163 device_typename(int type) 164 { 165 int i; 166 167 for (i = 0; i < nitems(device_types); i++) 168 if (device_types[i].type == type) 169 return (device_types[i].name); 170 171 return ("<unknown>"); 172 } 173 174 /* 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 179 * given type. 180 * 181 * The returned values for slice and partition are interpreted by 182 * disk_open(). 183 * 184 * The device string can be a standard loader(8) disk specifier: 185 * 186 * disk<unit>s<slice> disk0s1 187 * disk<unit>s<slice><partition> disk1s2a 188 * disk<unit>p<partition> disk0p4 189 * 190 * or one of the following formats: 191 * 192 * Valid device strings: For device types: 193 * 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 200 * 201 * For valid type names, see the device_types array, above. 202 * 203 * Slice numbers are 1-based. 0 is a wildcard. 204 */ 205 static void 206 get_load_device(int *type, int *unit, int *slice, int *partition) 207 { 208 struct disk_devdesc *dev; 209 char *devstr; 210 const char *p; 211 char *endp; 212 213 *type = DEV_TYP_NONE; 214 *unit = -1; 215 *slice = D_SLICEWILD; 216 *partition = D_PARTWILD; 217 218 devstr = ub_env_get("loaderdev"); 219 if (devstr == NULL) { 220 printf("U-Boot env: loaderdev not set, will probe all devices.\n"); 221 return; 222 } 223 printf("U-Boot env: loaderdev='%s'\n", devstr); 224 225 p = get_device_type(devstr, type); 226 227 /* 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 234 * the legacy format. 235 * 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). 241 */ 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; 249 free(dev); 250 return; 251 } 252 } 253 254 /* Ignore optional spaces after the device name. */ 255 while (*p == ' ') 256 p++; 257 258 /* Unknown device name, or a known name without unit number. */ 259 if ((*type == DEV_TYP_NONE) || (*p == '\0')) { 260 return; 261 } 262 263 /* Malformed unit number. */ 264 if (!isdigit(*p)) { 265 *type = DEV_TYP_NONE; 266 return; 267 } 268 269 /* Guaranteed to extract a number from the string, as *p is a digit. */ 270 *unit = strtol(p, &endp, 10); 271 p = endp; 272 273 /* Known device name with unit number and nothing else. */ 274 if (*p == '\0') { 275 return; 276 } 277 278 /* Device string is malformed beyond unit number. */ 279 if (*p != ':') { 280 *type = DEV_TYP_NONE; 281 *unit = -1; 282 return; 283 } 284 285 p++; 286 287 /* No slice and partition specification. */ 288 if ('\0' == *p ) 289 return; 290 291 /* Only DEV_TYP_STOR devices can have a slice specification. */ 292 if (!(*type & DEV_TYP_STOR)) { 293 *type = DEV_TYP_NONE; 294 *unit = -1; 295 return; 296 } 297 298 *slice = strtoul(p, &endp, 10); 299 300 /* Malformed slice number. */ 301 if (p == endp) { 302 *type = DEV_TYP_NONE; 303 *unit = -1; 304 *slice = D_SLICEWILD; 305 return; 306 } 307 308 p = endp; 309 310 /* No partition specification. */ 311 if (*p == '\0') 312 return; 313 314 /* Device string is malformed beyond slice number. */ 315 if (*p != '.') { 316 *type = DEV_TYP_NONE; 317 *unit = -1; 318 *slice = D_SLICEWILD; 319 return; 320 } 321 322 p++; 323 324 /* No partition specification. */ 325 if (*p == '\0') 326 return; 327 328 *partition = strtol(p, &endp, 10); 329 p = endp; 330 331 /* Full, valid device string. */ 332 if (*endp == '\0') 333 return; 334 335 /* Junk beyond partition number. */ 336 *type = DEV_TYP_NONE; 337 *unit = -1; 338 *slice = D_SLICEWILD; 339 *partition = D_PARTWILD; 340 } 341 342 static void 343 print_disk_probe_info(void) 344 { 345 char slice[32]; 346 char partition[32]; 347 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)); 352 else 353 snprintf(slice, sizeof(slice), "%d", currdev.d_disk.d_slice); 354 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)); 359 else 360 snprintf(partition, sizeof(partition), "%d", 361 currdev.d_disk.d_partition); 362 363 printf(" Checking unit=%d slice=%s partition=%s...", 364 currdev.dd.d_unit, slice, partition); 365 366 } 367 368 static int 369 probe_disks(int devidx, int load_type, int load_unit, int load_slice, 370 int load_partition) 371 { 372 int open_result, unit; 373 struct open_file f; 374 375 currdev.d_disk.d_slice = load_slice; 376 currdev.d_disk.d_partition = load_partition; 377 378 f.f_devdata = &currdev; 379 open_result = -1; 380 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) { 389 printf(" good.\n"); 390 return (0); 391 } 392 printf("\n"); 393 } 394 return (-1); 395 } 396 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) 403 break; 404 print_disk_probe_info(); 405 open_result = devsw[devidx]->dv_open(&f, &currdev); 406 if (open_result == 0) { 407 printf(" good.\n"); 408 return (0); 409 } 410 printf("\n"); 411 } 412 return (-1); 413 } 414 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) { 419 printf(" good.\n"); 420 return (0); 421 } 422 printf("\n"); 423 } 424 425 printf(" Requested disk type/unit/slice/partition not found\n"); 426 return (-1); 427 } 428 429 int 430 main(int argc, char **argv) 431 { 432 struct api_signature *sig = NULL; 433 int load_type, load_unit, load_slice, load_partition; 434 int i; 435 const char *ldev; 436 437 /* 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. 445 */ 446 if (!api_parse_cmdline_sig(argc, argv, &sig) && !api_search_sig(&sig)) 447 return (0x01badab1); 448 449 syscall_ptr = sig->syscall; 450 if (syscall_ptr == NULL) 451 return (0x02badab1); 452 453 if (sig->version > API_SIG_VERSION) 454 return (0x03badab1); 455 456 /* Clear BSS sections */ 457 bzero(__sbss_start, __sbss_end - __sbss_start); 458 bzero(__bss_start, _end - __bss_start); 459 460 /* 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. 465 */ 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); 469 470 /* 471 * Set up console. 472 */ 473 cons_probe(); 474 printf("Compatible U-Boot API signature found @%p\n", sig); 475 476 printf("\n%s", bootprog_info); 477 printf("\n"); 478 479 dump_sig(sig); 480 dump_addr_info(); 481 482 meminfo(); 483 484 /* Set up currdev variable to have hooks in place. */ 485 env_setenv("currdev", EV_VOLATILE, "", uboot_setcurrdev, env_nounset); 486 487 /* 488 * Enumerate U-Boot devices 489 */ 490 if ((devs_no = ub_dev_enum()) == 0) { 491 printf("no U-Boot devices found"); 492 goto do_interact; 493 } 494 printf("Number of U-Boot devices: %d\n", devs_no); 495 496 get_load_device(&load_type, &load_unit, &load_slice, &load_partition); 497 498 /* 499 * March through the device switch probing for things. 500 */ 501 for (i = 0; devsw[i] != NULL; i++) { 502 503 if (devsw[i]->dv_init == NULL) 504 continue; 505 if ((devsw[i]->dv_init)() != 0) 506 continue; 507 508 printf("Found U-Boot device: %s\n", devsw[i]->dv_name); 509 510 currdev.dd.d_dev = devsw[i]; 511 currdev.dd.d_unit = 0; 512 513 if ((load_type == DEV_TYP_NONE || (load_type & DEV_TYP_STOR)) && 514 strcmp(devsw[i]->dv_name, "disk") == 0) { 515 if (probe_disks(i, load_type, load_unit, load_slice, 516 load_partition) == 0) 517 break; 518 } 519 520 if ((load_type == DEV_TYP_NONE || (load_type & DEV_TYP_NET)) && 521 strcmp(devsw[i]->dv_name, "net") == 0) 522 break; 523 } 524 525 /* 526 * If we couldn't find a boot device, return an error to u-boot. 527 * U-boot may be running a boot script that can try something different 528 * so returning an error is better than forcing a reboot. 529 */ 530 if (devsw[i] == NULL) { 531 printf("No boot device found!\n"); 532 return (0xbadef1ce); 533 } 534 535 ldev = devformat(&currdev.dd); 536 env_setenv("currdev", EV_VOLATILE, ldev, uboot_setcurrdev, env_nounset); 537 env_setenv("loaddev", EV_VOLATILE, ldev, env_noset, env_nounset); 538 printf("Booting from %s\n", ldev); 539 540 do_interact: 541 setenv("LINES", "24", 1); /* optional */ 542 setenv("prompt", "loader>", 1); 543 #ifdef __powerpc__ 544 setenv("usefdt", "1", 1); 545 #endif 546 547 interact(); /* doesn't return */ 548 549 return (0); 550 } 551 552 553 COMMAND_SET(heap, "heap", "show heap usage", command_heap); 554 static int 555 command_heap(int argc, char *argv[]) 556 { 557 558 printf("heap base at %p, top at %p, used %td\n", end, sbrk(0), 559 sbrk(0) - end); 560 561 return (CMD_OK); 562 } 563 564 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 565 static int 566 command_reboot(int argc, char *argv[]) 567 { 568 569 printf("Resetting...\n"); 570 ub_reset(); 571 572 printf("Reset failed!\n"); 573 while (1); 574 __unreachable(); 575 } 576 577 COMMAND_SET(devinfo, "devinfo", "show U-Boot devices", command_devinfo); 578 static int 579 command_devinfo(int argc, char *argv[]) 580 { 581 int i; 582 583 if ((devs_no = ub_dev_enum()) == 0) { 584 command_errmsg = "no U-Boot devices found!?"; 585 return (CMD_ERROR); 586 } 587 588 printf("U-Boot devices:\n"); 589 for (i = 0; i < devs_no; i++) { 590 ub_dump_di(i); 591 printf("\n"); 592 } 593 return (CMD_OK); 594 } 595 596 COMMAND_SET(sysinfo, "sysinfo", "show U-Boot system info", command_sysinfo); 597 static int 598 command_sysinfo(int argc, char *argv[]) 599 { 600 struct sys_info *si; 601 602 if ((si = ub_get_sys_info()) == NULL) { 603 command_errmsg = "could not retrieve U-Boot sys info!?"; 604 return (CMD_ERROR); 605 } 606 607 printf("U-Boot system info:\n"); 608 ub_dump_si(si); 609 return (CMD_OK); 610 } 611 612 enum ubenv_action { 613 UBENV_UNKNOWN, 614 UBENV_SHOW, 615 UBENV_IMPORT 616 }; 617 618 static void 619 handle_uboot_env_var(enum ubenv_action action, const char * var) 620 { 621 char ldvar[128]; 622 const char *val; 623 char *wrk; 624 int len; 625 626 /* 627 * On an import with the variable name formatted as ldname=ubname, 628 * import the uboot variable ubname into the loader variable ldname, 629 * otherwise the historical behavior is to import to uboot.ubname. 630 */ 631 if (action == UBENV_IMPORT) { 632 len = strcspn(var, "="); 633 if (len == 0) { 634 printf("name cannot start with '=': '%s'\n", var); 635 return; 636 } 637 if (var[len] == 0) { 638 strcpy(ldvar, "uboot."); 639 strncat(ldvar, var, sizeof(ldvar) - 7); 640 } else { 641 len = MIN(len, sizeof(ldvar) - 1); 642 strncpy(ldvar, var, len); 643 ldvar[len] = 0; 644 var = &var[len + 1]; 645 } 646 } 647 648 /* 649 * If the user prepended "uboot." (which is how they usually see these 650 * names) strip it off as a convenience. 651 */ 652 if (strncmp(var, "uboot.", 6) == 0) { 653 var = &var[6]; 654 } 655 656 /* If there is no variable name left, punt. */ 657 if (var[0] == 0) { 658 printf("empty variable name\n"); 659 return; 660 } 661 662 val = ub_env_get(var); 663 if (action == UBENV_SHOW) { 664 if (val == NULL) 665 printf("uboot.%s is not set\n", var); 666 else 667 printf("uboot.%s=%s\n", var, val); 668 } else if (action == UBENV_IMPORT) { 669 if (val != NULL) { 670 setenv(ldvar, val, 1); 671 } 672 } 673 } 674 675 static int 676 command_ubenv(int argc, char *argv[]) 677 { 678 enum ubenv_action action; 679 const char *var; 680 int i; 681 682 action = UBENV_UNKNOWN; 683 if (argc > 1) { 684 if (strcasecmp(argv[1], "import") == 0) 685 action = UBENV_IMPORT; 686 else if (strcasecmp(argv[1], "show") == 0) 687 action = UBENV_SHOW; 688 } 689 if (action == UBENV_UNKNOWN) { 690 command_errmsg = "usage: 'ubenv <import|show> [var ...]"; 691 return (CMD_ERROR); 692 } 693 694 if (argc > 2) { 695 for (i = 2; i < argc; i++) 696 handle_uboot_env_var(action, argv[i]); 697 } else { 698 var = NULL; 699 for (;;) { 700 if ((var = ub_env_enum(var)) == NULL) 701 break; 702 handle_uboot_env_var(action, var); 703 } 704 } 705 706 return (CMD_OK); 707 } 708 COMMAND_SET(ubenv, "ubenv", "show or import U-Boot env vars", command_ubenv); 709 710 #ifdef LOADER_FDT_SUPPORT 711 /* 712 * Since proper fdt command handling function is defined in fdt_loader_cmd.c, 713 * and declaring it as extern is in contradiction with COMMAND_SET() macro 714 * (which uses static pointer), we're defining wrapper function, which 715 * calls the proper fdt handling routine. 716 */ 717 static int 718 command_fdt(int argc, char *argv[]) 719 { 720 721 return (command_fdt_internal(argc, argv)); 722 } 723 724 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt); 725 #endif 726