main.c revision 337815
1316485Sdavidcs/*- 2316485Sdavidcs * Copyright (c) 2000 Benno Rice <benno@jeamland.net> 3316485Sdavidcs * Copyright (c) 2000 Stephane Potvin <sepotvin@videotron.ca> 4316485Sdavidcs * Copyright (c) 2007-2008 Semihalf, Rafal Jaworowski <raj@semihalf.com> 5316485Sdavidcs * All rights reserved. 6316485Sdavidcs * 7316485Sdavidcs * Redistribution and use in source and binary forms, with or without 8316485Sdavidcs * modification, are permitted provided that the following conditions 9316485Sdavidcs * are met: 10316485Sdavidcs * 1. Redistributions of source code must retain the above copyright 11316485Sdavidcs * notice, this list of conditions and the following disclaimer. 12316485Sdavidcs * 2. Redistributions in binary form must reproduce the above copyright 13316485Sdavidcs * notice, this list of conditions and the following disclaimer in the 14316485Sdavidcs * documentation and/or other materials provided with the distribution. 15316485Sdavidcs * 16316485Sdavidcs * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17316485Sdavidcs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18316485Sdavidcs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19316485Sdavidcs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20316485Sdavidcs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21316485Sdavidcs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22316485Sdavidcs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23316485Sdavidcs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24316485Sdavidcs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25316485Sdavidcs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26316485Sdavidcs * SUCH DAMAGE. 27316485Sdavidcs */ 28316485Sdavidcs 29316485Sdavidcs#include <sys/cdefs.h> 30316485Sdavidcs__FBSDID("$FreeBSD: stable/11/stand/uboot/common/main.c 337815 2018-08-14 19:42:18Z kevans $"); 31316485Sdavidcs#include <sys/param.h> 32316485Sdavidcs 33316485Sdavidcs#include <stand.h> 34316485Sdavidcs 35316485Sdavidcs#include "api_public.h" 36316485Sdavidcs#include "bootstrap.h" 37316485Sdavidcs#include "glue.h" 38316485Sdavidcs#include "libuboot.h" 39316485Sdavidcs 40316485Sdavidcs#ifndef nitems 41316485Sdavidcs#define nitems(x) (sizeof((x)) / sizeof((x)[0])) 42316485Sdavidcs#endif 43316485Sdavidcs 44316485Sdavidcs#ifndef HEAP_SIZE 45316485Sdavidcs#define HEAP_SIZE (2 * 1024 * 1024) 46316485Sdavidcs#endif 47316485Sdavidcs 48316485Sdavidcsstruct uboot_devdesc currdev; 49316485Sdavidcsstruct arch_switch archsw; /* MI/MD interface boundary */ 50316485Sdavidcsint devs_no; 51316485Sdavidcs 52316485Sdavidcsuintptr_t uboot_heap_start; 53316485Sdavidcsuintptr_t uboot_heap_end; 54316485Sdavidcs 55316485Sdavidcsstruct device_type { 56316485Sdavidcs const char *name; 57316485Sdavidcs int type; 58316485Sdavidcs} device_types[] = { 59316485Sdavidcs { "disk", DEV_TYP_STOR }, 60316485Sdavidcs { "ide", DEV_TYP_STOR | DT_STOR_IDE }, 61316485Sdavidcs { "mmc", DEV_TYP_STOR | DT_STOR_MMC }, 62316485Sdavidcs { "sata", DEV_TYP_STOR | DT_STOR_SATA }, 63316485Sdavidcs { "scsi", DEV_TYP_STOR | DT_STOR_SCSI }, 64316485Sdavidcs { "usb", DEV_TYP_STOR | DT_STOR_USB }, 65316485Sdavidcs { "net", DEV_TYP_NET } 66316485Sdavidcs}; 67316485Sdavidcs 68316485Sdavidcsextern char end[]; 69316485Sdavidcsextern char bootprog_info[]; 70316485Sdavidcs 71316485Sdavidcsextern unsigned char _etext[]; 72316485Sdavidcsextern unsigned char _edata[]; 73316485Sdavidcsextern unsigned char __bss_start[]; 74316485Sdavidcsextern unsigned char __sbss_start[]; 75316485Sdavidcsextern unsigned char __sbss_end[]; 76316485Sdavidcsextern unsigned char _end[]; 77316485Sdavidcs 78316485Sdavidcs#ifdef LOADER_FDT_SUPPORT 79316485Sdavidcsextern int command_fdt_internal(int argc, char *argv[]); 80316485Sdavidcs#endif 81316485Sdavidcs 82316485Sdavidcsstatic void 83316485Sdavidcsdump_sig(struct api_signature *sig) 84316485Sdavidcs{ 85316485Sdavidcs#ifdef DEBUG 86316485Sdavidcs printf("signature:\n"); 87316485Sdavidcs printf(" version\t= %d\n", sig->version); 88316485Sdavidcs printf(" checksum\t= 0x%08x\n", sig->checksum); 89316485Sdavidcs printf(" sc entry\t= 0x%08x\n", sig->syscall); 90316485Sdavidcs#endif 91316485Sdavidcs} 92316485Sdavidcs 93316485Sdavidcsstatic void 94316485Sdavidcsdump_addr_info(void) 95316485Sdavidcs{ 96316485Sdavidcs#ifdef DEBUG 97316485Sdavidcs printf("\naddresses info:\n"); 98316485Sdavidcs printf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext); 99316485Sdavidcs printf(" _edata = 0x%08x\n", (uint32_t)_edata); 100316485Sdavidcs printf(" __sbss_start = 0x%08x\n", (uint32_t)__sbss_start); 101316485Sdavidcs printf(" __sbss_end = 0x%08x\n", (uint32_t)__sbss_end); 102316485Sdavidcs printf(" __sbss_start = 0x%08x\n", (uint32_t)__bss_start); 103316485Sdavidcs printf(" _end = 0x%08x\n", (uint32_t)_end); 104316485Sdavidcs printf(" syscall entry = 0x%08x\n", (uint32_t)syscall_ptr); 105316485Sdavidcs#endif 106316485Sdavidcs} 107316485Sdavidcs 108316485Sdavidcsstatic uint64_t 109316485Sdavidcsmemsize(struct sys_info *si, int flags) 110316485Sdavidcs{ 111316485Sdavidcs uint64_t size; 112316485Sdavidcs int i; 113316485Sdavidcs 114316485Sdavidcs size = 0; 115316485Sdavidcs for (i = 0; i < si->mr_no; i++) 116316485Sdavidcs if (si->mr[i].flags == flags && si->mr[i].size) 117316485Sdavidcs size += (si->mr[i].size); 118316485Sdavidcs 119316485Sdavidcs return (size); 120316485Sdavidcs} 121316485Sdavidcs 122316485Sdavidcsstatic void 123316485Sdavidcsmeminfo(void) 124316485Sdavidcs{ 125316485Sdavidcs uint64_t size; 126316485Sdavidcs struct sys_info *si; 127316485Sdavidcs int t[3] = { MR_ATTR_DRAM, MR_ATTR_FLASH, MR_ATTR_SRAM }; 128316485Sdavidcs int i; 129316485Sdavidcs 130316485Sdavidcs if ((si = ub_get_sys_info()) == NULL) 131316485Sdavidcs panic("could not retrieve system info"); 132316485Sdavidcs 133316485Sdavidcs for (i = 0; i < 3; i++) { 134316485Sdavidcs size = memsize(si, t[i]); 135316485Sdavidcs if (size > 0) 136316485Sdavidcs printf("%s: %juMB\n", ub_mem_type(t[i]), 137316485Sdavidcs (uintmax_t)(size / 1024 / 1024)); 138316485Sdavidcs } 139316485Sdavidcs} 140316485Sdavidcs 141316485Sdavidcsstatic const char * 142316485Sdavidcsget_device_type(const char *devstr, int *devtype) 143316485Sdavidcs{ 144316485Sdavidcs int i; 145316485Sdavidcs int namelen; 146316485Sdavidcs struct device_type *dt; 147316485Sdavidcs 148316485Sdavidcs if (devstr) { 149316485Sdavidcs for (i = 0; i < nitems(device_types); i++) { 150316485Sdavidcs dt = &device_types[i]; 151316485Sdavidcs namelen = strlen(dt->name); 152316485Sdavidcs if (strncmp(dt->name, devstr, namelen) == 0) { 153316485Sdavidcs *devtype = dt->type; 154316485Sdavidcs return (devstr + namelen); 155316485Sdavidcs } 156316485Sdavidcs } 157316485Sdavidcs printf("Unknown device type '%s'\n", devstr); 158316485Sdavidcs } 159316485Sdavidcs 160316485Sdavidcs *devtype = -1; 161316485Sdavidcs return (NULL); 162316485Sdavidcs} 163316485Sdavidcs 164316485Sdavidcsstatic const char * 165316485Sdavidcsdevice_typename(int type) 166316485Sdavidcs{ 167316485Sdavidcs int i; 168316485Sdavidcs 169316485Sdavidcs for (i = 0; i < nitems(device_types); i++) 170316485Sdavidcs if (device_types[i].type == type) 171316485Sdavidcs return (device_types[i].name); 172316485Sdavidcs 173316485Sdavidcs return ("<unknown>"); 174316485Sdavidcs} 175316485Sdavidcs 176316485Sdavidcs/* 177316485Sdavidcs * Parse a device string into type, unit, slice and partition numbers. A 178316485Sdavidcs * returned value of -1 for type indicates a search should be done for the 179316485Sdavidcs * first loadable device, otherwise a returned value of -1 for unit 180316485Sdavidcs * indicates a search should be done for the first loadable device of the 181316485Sdavidcs * given type. 182316485Sdavidcs * 183316485Sdavidcs * The returned values for slice and partition are interpreted by 184316485Sdavidcs * disk_open(). 185316485Sdavidcs * 186316485Sdavidcs * Valid device strings: For device types: 187316485Sdavidcs * 188316485Sdavidcs * <type_name> DEV_TYP_STOR, DEV_TYP_NET 189316485Sdavidcs * <type_name><unit> DEV_TYP_STOR, DEV_TYP_NET 190316485Sdavidcs * <type_name><unit>: DEV_TYP_STOR, DEV_TYP_NET 191316485Sdavidcs * <type_name><unit>:<slice> DEV_TYP_STOR 192316485Sdavidcs * <type_name><unit>:<slice>. DEV_TYP_STOR 193316485Sdavidcs * <type_name><unit>:<slice>.<partition> DEV_TYP_STOR 194316485Sdavidcs * 195316485Sdavidcs * For valid type names, see the device_types array, above. 196316485Sdavidcs * 197316485Sdavidcs * Slice numbers are 1-based. 0 is a wildcard. 198316485Sdavidcs */ 199316485Sdavidcsstatic void 200316485Sdavidcsget_load_device(int *type, int *unit, int *slice, int *partition) 201316485Sdavidcs{ 202316485Sdavidcs char *devstr; 203316485Sdavidcs const char *p; 204316485Sdavidcs char *endp; 205316485Sdavidcs 206316485Sdavidcs *type = -1; 207316485Sdavidcs *unit = -1; 208316485Sdavidcs *slice = 0; 209316485Sdavidcs *partition = -1; 210316485Sdavidcs 211316485Sdavidcs devstr = ub_env_get("loaderdev"); 212316485Sdavidcs if (devstr == NULL) { 213316485Sdavidcs printf("U-Boot env: loaderdev not set, will probe all devices.\n"); 214316485Sdavidcs return; 215316485Sdavidcs } 216316485Sdavidcs printf("U-Boot env: loaderdev='%s'\n", devstr); 217316485Sdavidcs 218316485Sdavidcs p = get_device_type(devstr, type); 219316485Sdavidcs 220316485Sdavidcs /* Ignore optional spaces after the device name. */ 221316485Sdavidcs while (*p == ' ') 222316485Sdavidcs p++; 223316485Sdavidcs 224316485Sdavidcs /* Unknown device name, or a known name without unit number. */ 225316485Sdavidcs if ((*type == -1) || (*p == '\0')) { 226316485Sdavidcs return; 227316485Sdavidcs } 228316485Sdavidcs 229316485Sdavidcs /* Malformed unit number. */ 230316485Sdavidcs if (!isdigit(*p)) { 231316485Sdavidcs *type = -1; 232316485Sdavidcs return; 233316485Sdavidcs } 234316485Sdavidcs 235316485Sdavidcs /* Guaranteed to extract a number from the string, as *p is a digit. */ 236316485Sdavidcs *unit = strtol(p, &endp, 10); 237316485Sdavidcs p = endp; 238316485Sdavidcs 239316485Sdavidcs /* Known device name with unit number and nothing else. */ 240316485Sdavidcs if (*p == '\0') { 241316485Sdavidcs return; 242337517Sdavidcs } 243337517Sdavidcs 244337517Sdavidcs /* Device string is malformed beyond unit number. */ 245337517Sdavidcs if (*p != ':') { 246337517Sdavidcs *type = -1; 247337517Sdavidcs *unit = -1; 248337517Sdavidcs return; 249337517Sdavidcs } 250337517Sdavidcs 251337517Sdavidcs p++; 252337517Sdavidcs 253337517Sdavidcs /* No slice and partition specification. */ 254337517Sdavidcs if ('\0' == *p ) 255337517Sdavidcs return; 256337517Sdavidcs 257337517Sdavidcs /* Only DEV_TYP_STOR devices can have a slice specification. */ 258337517Sdavidcs if (!(*type & DEV_TYP_STOR)) { 259337517Sdavidcs *type = -1; 260337517Sdavidcs *unit = -1; 261337517Sdavidcs return; 262337517Sdavidcs } 263337517Sdavidcs 264337517Sdavidcs *slice = strtoul(p, &endp, 10); 265337517Sdavidcs 266337517Sdavidcs /* Malformed slice number. */ 267337517Sdavidcs if (p == endp) { 268337517Sdavidcs *type = -1; 269337517Sdavidcs *unit = -1; 270337517Sdavidcs *slice = 0; 271337517Sdavidcs return; 272316485Sdavidcs } 273316485Sdavidcs 274316485Sdavidcs p = endp; 275316485Sdavidcs 276316485Sdavidcs /* No partition specification. */ 277316485Sdavidcs if (*p == '\0') 278316485Sdavidcs return; 279316485Sdavidcs 280316485Sdavidcs /* Device string is malformed beyond slice number. */ 281316485Sdavidcs if (*p != '.') { 282316485Sdavidcs *type = -1; 283316485Sdavidcs *unit = -1; 284316485Sdavidcs *slice = 0; 285316485Sdavidcs return; 286316485Sdavidcs } 287316485Sdavidcs 288316485Sdavidcs p++; 289316485Sdavidcs 290316485Sdavidcs /* No partition specification. */ 291316485Sdavidcs if (*p == '\0') 292316485Sdavidcs return; 293316485Sdavidcs 294316485Sdavidcs *partition = strtol(p, &endp, 10); 295316485Sdavidcs p = endp; 296316485Sdavidcs 297316485Sdavidcs /* Full, valid device string. */ 298316485Sdavidcs if (*endp == '\0') 299316485Sdavidcs return; 300316485Sdavidcs 301316485Sdavidcs /* Junk beyond partition number. */ 302316485Sdavidcs *type = -1; 303316485Sdavidcs *unit = -1; 304316485Sdavidcs *slice = 0; 305316485Sdavidcs *partition = -1; 306316485Sdavidcs} 307316485Sdavidcs 308316485Sdavidcsstatic void 309316485Sdavidcsprint_disk_probe_info() 310316485Sdavidcs{ 311316485Sdavidcs char slice[32]; 312316485Sdavidcs char partition[32]; 313316485Sdavidcs 314316485Sdavidcs if (currdev.d_disk.slice > 0) 315316485Sdavidcs sprintf(slice, "%d", currdev.d_disk.slice); 316316485Sdavidcs else 317316485Sdavidcs strcpy(slice, "<auto>"); 318316485Sdavidcs 319316485Sdavidcs if (currdev.d_disk.partition >= 0) 320316485Sdavidcs sprintf(partition, "%d", currdev.d_disk.partition); 321316485Sdavidcs else 322316485Sdavidcs strcpy(partition, "<auto>"); 323316485Sdavidcs 324316485Sdavidcs printf(" Checking unit=%d slice=%s partition=%s...", 325316485Sdavidcs currdev.dd.d_unit, slice, partition); 326316485Sdavidcs 327316485Sdavidcs} 328316485Sdavidcs 329316485Sdavidcsstatic int 330316485Sdavidcsprobe_disks(int devidx, int load_type, int load_unit, int load_slice, 331316485Sdavidcs int load_partition) 332316485Sdavidcs{ 333316485Sdavidcs int open_result, unit; 334316485Sdavidcs struct open_file f; 335316485Sdavidcs 336316485Sdavidcs currdev.d_disk.slice = load_slice; 337316485Sdavidcs currdev.d_disk.partition = load_partition; 338316485Sdavidcs 339316485Sdavidcs f.f_devdata = &currdev; 340316485Sdavidcs open_result = -1; 341316485Sdavidcs 342337517Sdavidcs if (load_type == -1) { 343337517Sdavidcs printf(" Probing all disk devices...\n"); 344337517Sdavidcs /* Try each disk in succession until one works. */ 345337517Sdavidcs for (currdev.dd.d_unit = 0; currdev.dd.d_unit < UB_MAX_DEV; 346316485Sdavidcs currdev.dd.d_unit++) { 347316485Sdavidcs print_disk_probe_info(); 348 open_result = devsw[devidx]->dv_open(&f, &currdev); 349 if (open_result == 0) { 350 printf(" good.\n"); 351 return (0); 352 } 353 printf("\n"); 354 } 355 return (-1); 356 } 357 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) 364 break; 365 print_disk_probe_info(); 366 open_result = devsw[devidx]->dv_open(&f, &currdev); 367 if (open_result == 0) { 368 printf(" good.\n"); 369 return (0); 370 } 371 printf("\n"); 372 } 373 return (-1); 374 } 375 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) { 380 printf(" good.\n"); 381 return (0); 382 } 383 printf("\n"); 384 } 385 386 printf(" Requested disk type/unit/slice/partition not found\n"); 387 return (-1); 388} 389 390int 391main(int argc, char **argv) 392{ 393 struct api_signature *sig = NULL; 394 int load_type, load_unit, load_slice, load_partition; 395 int i; 396 const char *ldev; 397 398 /* 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. 406 */ 407 if (!api_parse_cmdline_sig(argc, argv, &sig) && !api_search_sig(&sig)) 408 return (0x01badab1); 409 410 syscall_ptr = sig->syscall; 411 if (syscall_ptr == NULL) 412 return (0x02badab1); 413 414 if (sig->version > API_SIG_VERSION) 415 return (0x03badab1); 416 417 /* Clear BSS sections */ 418 bzero(__sbss_start, __sbss_end - __sbss_start); 419 bzero(__bss_start, _end - __bss_start); 420 421 /* 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. 426 */ 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); 430 431 /* 432 * Set up console. 433 */ 434 cons_probe(); 435 printf("Compatible U-Boot API signature found @%p\n", sig); 436 437 printf("\n%s", bootprog_info); 438 printf("\n"); 439 440 dump_sig(sig); 441 dump_addr_info(); 442 443 meminfo(); 444 445 /* 446 * Enumerate U-Boot devices 447 */ 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); 451 452 get_load_device(&load_type, &load_unit, &load_slice, &load_partition); 453 454 /* 455 * March through the device switch probing for things. 456 */ 457 for (i = 0; devsw[i] != NULL; i++) { 458 459 if (devsw[i]->dv_init == NULL) 460 continue; 461 if ((devsw[i]->dv_init)() != 0) 462 continue; 463 464 printf("Found U-Boot device: %s\n", devsw[i]->dv_name); 465 466 currdev.dd.d_dev = devsw[i]; 467 currdev.dd.d_unit = 0; 468 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) 473 break; 474 } 475 476 if ((load_type == -1 || (load_type & DEV_TYP_NET)) && 477 strcmp(devsw[i]->dv_name, "net") == 0) 478 break; 479 } 480 481 /* 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. 485 */ 486 if (devsw[i] == NULL) { 487 printf("No boot device found!\n"); 488 return (0xbadef1ce); 489 } 490 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); 495 496 setenv("LINES", "24", 1); /* optional */ 497 setenv("prompt", "loader>", 1); 498 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; 505 506 interact(); /* doesn't return */ 507 508 return (0); 509} 510 511 512COMMAND_SET(heap, "heap", "show heap usage", command_heap); 513static int 514command_heap(int argc, char *argv[]) 515{ 516 517 printf("heap base at %p, top at %p, used %td\n", end, sbrk(0), 518 sbrk(0) - end); 519 520 return (CMD_OK); 521} 522 523COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 524static int 525command_reboot(int argc, char *argv[]) 526{ 527 528 printf("Resetting...\n"); 529 ub_reset(); 530 531 printf("Reset failed!\n"); 532 while (1); 533 __unreachable(); 534} 535 536COMMAND_SET(devinfo, "devinfo", "show U-Boot devices", command_devinfo); 537static int 538command_devinfo(int argc, char *argv[]) 539{ 540 int i; 541 542 if ((devs_no = ub_dev_enum()) == 0) { 543 command_errmsg = "no U-Boot devices found!?"; 544 return (CMD_ERROR); 545 } 546 547 printf("U-Boot devices:\n"); 548 for (i = 0; i < devs_no; i++) { 549 ub_dump_di(i); 550 printf("\n"); 551 } 552 return (CMD_OK); 553} 554 555COMMAND_SET(sysinfo, "sysinfo", "show U-Boot system info", command_sysinfo); 556static int 557command_sysinfo(int argc, char *argv[]) 558{ 559 struct sys_info *si; 560 561 if ((si = ub_get_sys_info()) == NULL) { 562 command_errmsg = "could not retrieve U-Boot sys info!?"; 563 return (CMD_ERROR); 564 } 565 566 printf("U-Boot system info:\n"); 567 ub_dump_si(si); 568 return (CMD_OK); 569} 570 571enum ubenv_action { 572 UBENV_UNKNOWN, 573 UBENV_SHOW, 574 UBENV_IMPORT 575}; 576 577static void 578handle_uboot_env_var(enum ubenv_action action, const char * var) 579{ 580 char ldvar[128]; 581 const char *val; 582 char *wrk; 583 int len; 584 585 /* 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. 589 */ 590 if (action == UBENV_IMPORT) { 591 len = strcspn(var, "="); 592 if (len == 0) { 593 printf("name cannot start with '=': '%s'\n", var); 594 return; 595 } 596 if (var[len] == 0) { 597 strcpy(ldvar, "uboot."); 598 strncat(ldvar, var, sizeof(ldvar) - 7); 599 } else { 600 len = MIN(len, sizeof(ldvar) - 1); 601 strncpy(ldvar, var, len); 602 ldvar[len] = 0; 603 var = &var[len + 1]; 604 } 605 } 606 607 /* 608 * If the user prepended "uboot." (which is how they usually see these 609 * names) strip it off as a convenience. 610 */ 611 if (strncmp(var, "uboot.", 6) == 0) { 612 var = &var[6]; 613 } 614 615 /* If there is no variable name left, punt. */ 616 if (var[0] == 0) { 617 printf("empty variable name\n"); 618 return; 619 } 620 621 val = ub_env_get(var); 622 if (action == UBENV_SHOW) { 623 if (val == NULL) 624 printf("uboot.%s is not set\n", var); 625 else 626 printf("uboot.%s=%s\n", var, val); 627 } else if (action == UBENV_IMPORT) { 628 if (val != NULL) { 629 setenv(ldvar, val, 1); 630 } 631 } 632} 633 634static int 635command_ubenv(int argc, char *argv[]) 636{ 637 enum ubenv_action action; 638 const char *var; 639 int i; 640 641 action = UBENV_UNKNOWN; 642 if (argc > 1) { 643 if (strcasecmp(argv[1], "import") == 0) 644 action = UBENV_IMPORT; 645 else if (strcasecmp(argv[1], "show") == 0) 646 action = UBENV_SHOW; 647 } 648 if (action == UBENV_UNKNOWN) { 649 command_errmsg = "usage: 'ubenv <import|show> [var ...]"; 650 return (CMD_ERROR); 651 } 652 653 if (argc > 2) { 654 for (i = 2; i < argc; i++) 655 handle_uboot_env_var(action, argv[i]); 656 } else { 657 var = NULL; 658 for (;;) { 659 if ((var = ub_env_enum(var)) == NULL) 660 break; 661 handle_uboot_env_var(action, var); 662 } 663 } 664 665 return (CMD_OK); 666} 667COMMAND_SET(ubenv, "ubenv", "show or import U-Boot env vars", command_ubenv); 668 669#ifdef LOADER_FDT_SUPPORT 670/* 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. 675 */ 676static int 677command_fdt(int argc, char *argv[]) 678{ 679 680 return (command_fdt_internal(argc, argv)); 681} 682 683COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt); 684#endif 685