1 /*- 2 * Copyright (c) 2008-2010 Rui Paulo 3 * Copyright (c) 2006 Marcel Moolenaar 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/disk.h> 32 #include <sys/param.h> 33 #include <sys/reboot.h> 34 #include <stdint.h> 35 #include <stand.h> 36 #include <string.h> 37 #include <setjmp.h> 38 #include <disk.h> 39 40 #include <efi.h> 41 #include <efilib.h> 42 43 #include <uuid.h> 44 45 #include <bootstrap.h> 46 #include <smbios.h> 47 48 #ifdef EFI_ZFS_BOOT 49 #include <libzfs.h> 50 #include "efizfs.h" 51 #endif 52 53 #include "loader_efi.h" 54 55 struct arch_switch archsw; /* MI/MD interface boundary */ 56 57 EFI_GUID acpi = ACPI_TABLE_GUID; 58 EFI_GUID acpi20 = ACPI_20_TABLE_GUID; 59 EFI_GUID devid = DEVICE_PATH_PROTOCOL; 60 EFI_GUID imgid = LOADED_IMAGE_PROTOCOL; 61 EFI_GUID mps = MPS_TABLE_GUID; 62 EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL; 63 EFI_GUID smbios = SMBIOS_TABLE_GUID; 64 EFI_GUID smbios3 = SMBIOS3_TABLE_GUID; 65 EFI_GUID dxe = DXE_SERVICES_TABLE_GUID; 66 EFI_GUID hoblist = HOB_LIST_TABLE_GUID; 67 EFI_GUID lzmadecomp = LZMA_DECOMPRESSION_GUID; 68 EFI_GUID mpcore = ARM_MP_CORE_INFO_TABLE_GUID; 69 EFI_GUID esrt = ESRT_TABLE_GUID; 70 EFI_GUID memtype = MEMORY_TYPE_INFORMATION_TABLE_GUID; 71 EFI_GUID debugimg = DEBUG_IMAGE_INFO_TABLE_GUID; 72 EFI_GUID fdtdtb = FDT_TABLE_GUID; 73 EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL; 74 75 /* 76 * Number of seconds to wait for a keystroke before exiting with failure 77 * in the event no currdev is found. -2 means always break, -1 means 78 * never break, 0 means poll once and then reboot, > 0 means wait for 79 * that many seconds. "fail_timeout" can be set in the environment as 80 * well. 81 */ 82 static int fail_timeout = 5; 83 84 static bool 85 has_keyboard(void) 86 { 87 EFI_STATUS status; 88 EFI_DEVICE_PATH *path; 89 EFI_HANDLE *hin, *hin_end, *walker; 90 UINTN sz; 91 bool retval = false; 92 93 /* 94 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and 95 * do the typical dance to get the right sized buffer. 96 */ 97 sz = 0; 98 hin = NULL; 99 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 0); 100 if (status == EFI_BUFFER_TOO_SMALL) { 101 hin = (EFI_HANDLE *)malloc(sz); 102 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 103 hin); 104 if (EFI_ERROR(status)) 105 free(hin); 106 } 107 if (EFI_ERROR(status)) 108 return retval; 109 110 /* 111 * Look at each of the handles. If it supports the device path protocol, 112 * use it to get the device path for this handle. Then see if that 113 * device path matches either the USB device path for keyboards or the 114 * legacy device path for keyboards. 115 */ 116 hin_end = &hin[sz / sizeof(*hin)]; 117 for (walker = hin; walker < hin_end; walker++) { 118 status = BS->HandleProtocol(*walker, &devid, (VOID **)&path); 119 if (EFI_ERROR(status)) 120 continue; 121 122 while (!IsDevicePathEnd(path)) { 123 /* 124 * Check for the ACPI keyboard node. All PNP3xx nodes 125 * are keyboards of different flavors. Note: It is 126 * unclear of there's always a keyboard node when 127 * there's a keyboard controller, or if there's only one 128 * when a keyboard is detected at boot. 129 */ 130 if (DevicePathType(path) == ACPI_DEVICE_PATH && 131 (DevicePathSubType(path) == ACPI_DP || 132 DevicePathSubType(path) == ACPI_EXTENDED_DP)) { 133 ACPI_HID_DEVICE_PATH *acpi; 134 135 acpi = (ACPI_HID_DEVICE_PATH *)(void *)path; 136 if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 0x300 && 137 (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) { 138 retval = true; 139 goto out; 140 } 141 /* 142 * Check for USB keyboard node, if present. Unlike a 143 * PS/2 keyboard, these definitely only appear when 144 * connected to the system. 145 */ 146 } else if (DevicePathType(path) == MESSAGING_DEVICE_PATH && 147 DevicePathSubType(path) == MSG_USB_CLASS_DP) { 148 USB_CLASS_DEVICE_PATH *usb; 149 150 usb = (USB_CLASS_DEVICE_PATH *)(void *)path; 151 if (usb->DeviceClass == 3 && /* HID */ 152 usb->DeviceSubClass == 1 && /* Boot devices */ 153 usb->DeviceProtocol == 1) { /* Boot keyboards */ 154 retval = true; 155 goto out; 156 } 157 } 158 path = NextDevicePathNode(path); 159 } 160 } 161 out: 162 free(hin); 163 return retval; 164 } 165 166 static void 167 set_currdev_devdesc(struct devdesc *currdev) 168 { 169 const char *devname; 170 171 devname = efi_fmtdev(currdev); 172 173 printf("Setting currdev to %s\n", devname); 174 175 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev, env_nounset); 176 env_setenv("loaddev", EV_VOLATILE, devname, env_noset, env_nounset); 177 } 178 179 static void 180 set_currdev_devsw(struct devsw *dev, int unit) 181 { 182 struct devdesc currdev; 183 184 currdev.d_dev = dev; 185 currdev.d_unit = unit; 186 187 set_currdev_devdesc(&currdev); 188 } 189 190 static void 191 set_currdev_pdinfo(pdinfo_t *dp) 192 { 193 194 /* 195 * Disks are special: they have partitions. if the parent 196 * pointer is non-null, we're a partition not a full disk 197 * and we need to adjust currdev appropriately. 198 */ 199 if (dp->pd_devsw->dv_type == DEVT_DISK) { 200 struct disk_devdesc currdev; 201 202 currdev.dd.d_dev = dp->pd_devsw; 203 if (dp->pd_parent == NULL) { 204 currdev.dd.d_unit = dp->pd_unit; 205 currdev.d_slice = -1; 206 currdev.d_partition = -1; 207 } else { 208 currdev.dd.d_unit = dp->pd_parent->pd_unit; 209 currdev.d_slice = dp->pd_unit; 210 currdev.d_partition = 255; /* Assumes GPT */ 211 } 212 set_currdev_devdesc((struct devdesc *)&currdev); 213 } else { 214 set_currdev_devsw(dp->pd_devsw, dp->pd_unit); 215 } 216 } 217 218 static bool 219 sanity_check_currdev(void) 220 { 221 struct stat st; 222 223 return (stat("/boot/defaults/loader.conf", &st) == 0 || 224 stat("/boot/kernel/kernel", &st) == 0); 225 } 226 227 #ifdef EFI_ZFS_BOOT 228 static bool 229 probe_zfs_currdev(uint64_t guid) 230 { 231 char *devname; 232 struct zfs_devdesc currdev; 233 234 currdev.dd.d_dev = &zfs_dev; 235 currdev.dd.d_unit = 0; 236 currdev.pool_guid = guid; 237 currdev.root_guid = 0; 238 set_currdev_devdesc((struct devdesc *)&currdev); 239 devname = efi_fmtdev(&currdev); 240 init_zfs_bootenv(devname); 241 242 return (sanity_check_currdev()); 243 } 244 #endif 245 246 static bool 247 try_as_currdev(pdinfo_t *hd, pdinfo_t *pp) 248 { 249 uint64_t guid; 250 251 #ifdef EFI_ZFS_BOOT 252 /* 253 * If there's a zpool on this device, try it as a ZFS 254 * filesystem, which has somewhat different setup than all 255 * other types of fs due to imperfect loader integration. 256 * This all stems from ZFS being both a device (zpool) and 257 * a filesystem, plus the boot env feature. 258 */ 259 if (efizfs_get_guid_by_handle(pp->pd_handle, &guid)) 260 return (probe_zfs_currdev(guid)); 261 #endif 262 /* 263 * All other filesystems just need the pdinfo 264 * initialized in the standard way. 265 */ 266 set_currdev_pdinfo(pp); 267 return (sanity_check_currdev()); 268 } 269 270 static int 271 find_currdev(EFI_LOADED_IMAGE *img) 272 { 273 pdinfo_t *dp, *pp; 274 EFI_DEVICE_PATH *devpath, *copy; 275 EFI_HANDLE h; 276 CHAR16 *text; 277 struct devsw *dev; 278 int unit; 279 uint64_t extra; 280 281 #ifdef EFI_ZFS_BOOT 282 /* 283 * Did efi_zfs_probe() detect the boot pool? If so, use the zpool 284 * it found, if it's sane. ZFS is the only thing that looks for 285 * disks and pools to boot. This may change in the future, however, 286 * if we allow specifying which pool to boot from via UEFI variables 287 * rather than the bootenv stuff that FreeBSD uses today. 288 */ 289 if (pool_guid != 0) { 290 printf("Trying ZFS pool\n"); 291 if (probe_zfs_currdev(pool_guid)) 292 return (0); 293 } 294 #endif /* EFI_ZFS_BOOT */ 295 296 /* 297 * Try to find the block device by its handle based on the 298 * image we're booting. If we can't find a sane partition, 299 * search all the other partitions of the disk. We do not 300 * search other disks because it's a violation of the UEFI 301 * boot protocol to do so. We fail and let UEFI go on to 302 * the next candidate. 303 */ 304 dp = efiblk_get_pdinfo_by_handle(img->DeviceHandle); 305 if (dp != NULL) { 306 text = efi_devpath_name(dp->pd_devpath); 307 if (text != NULL) { 308 printf("Trying ESP: %S\n", text); 309 efi_free_devpath_name(text); 310 } 311 set_currdev_pdinfo(dp); 312 if (sanity_check_currdev()) 313 return (0); 314 if (dp->pd_parent != NULL) { 315 dp = dp->pd_parent; 316 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) { 317 text = efi_devpath_name(pp->pd_devpath); 318 if (text != NULL) { 319 printf("And now the part: %S\n", text); 320 efi_free_devpath_name(text); 321 } 322 /* 323 * Roll up the ZFS special case 324 * for those partitions that have 325 * zpools on them 326 */ 327 if (try_as_currdev(dp, pp)) 328 return (0); 329 } 330 } 331 } else { 332 printf("Can't find device by handle\n"); 333 } 334 335 /* 336 * Try the device handle from our loaded image first. If that 337 * fails, use the device path from the loaded image and see if 338 * any of the nodes in that path match one of the enumerated 339 * handles. Currently, this handle list is only for netboot. 340 */ 341 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) { 342 set_currdev_devsw(dev, unit); 343 if (sanity_check_currdev()) 344 return (0); 345 } 346 347 copy = NULL; 348 devpath = efi_lookup_image_devpath(IH); 349 while (devpath != NULL) { 350 h = efi_devpath_handle(devpath); 351 if (h == NULL) 352 break; 353 354 free(copy); 355 copy = NULL; 356 357 if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) { 358 set_currdev_devsw(dev, unit); 359 if (sanity_check_currdev()) 360 return (0); 361 } 362 363 devpath = efi_lookup_devpath(h); 364 if (devpath != NULL) { 365 copy = efi_devpath_trim(devpath); 366 devpath = copy; 367 } 368 } 369 free(copy); 370 371 return (ENOENT); 372 } 373 374 static bool 375 interactive_interrupt(const char *msg) 376 { 377 time_t now, then, last; 378 379 last = 0; 380 now = then = getsecs(); 381 printf("%s\n", msg); 382 if (fail_timeout == -2) /* Always break to OK */ 383 return (true); 384 if (fail_timeout == -1) /* Never break to OK */ 385 return (false); 386 do { 387 if (last != now) { 388 printf("press any key to interrupt reboot in %d seconds\r", 389 fail_timeout - (int)(now - then)); 390 last = now; 391 } 392 393 /* XXX no pause or timeout wait for char */ 394 if (ischar()) 395 return (true); 396 now = getsecs(); 397 } while (now - then < fail_timeout); 398 return (false); 399 } 400 401 int 402 parse_args(int argc, CHAR16 *argv[], bool has_kbd) 403 { 404 int i, j, howto; 405 bool vargood; 406 char var[128]; 407 408 /* 409 * Parse the args to set the console settings, etc 410 * boot1.efi passes these in, if it can read /boot.config or /boot/config 411 * or iPXE may be setup to pass these in. Or the optional argument in the 412 * boot environment was used to pass these arguments in (in which case 413 * neither /boot.config nor /boot/config are consulted). 414 * 415 * Loop through the args, and for each one that contains an '=' that is 416 * not the first character, add it to the environment. This allows 417 * loader and kernel env vars to be passed on the command line. Convert 418 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied (though this 419 * method is flawed for non-ASCII characters). 420 */ 421 howto = 0; 422 for (i = 1; i < argc; i++) { 423 if (argv[i][0] == '-') { 424 for (j = 1; argv[i][j] != 0; j++) { 425 int ch; 426 427 ch = argv[i][j]; 428 switch (ch) { 429 case 'a': 430 howto |= RB_ASKNAME; 431 break; 432 case 'd': 433 howto |= RB_KDB; 434 break; 435 case 'D': 436 howto |= RB_MULTIPLE; 437 break; 438 case 'h': 439 howto |= RB_SERIAL; 440 break; 441 case 'm': 442 howto |= RB_MUTE; 443 break; 444 case 'p': 445 howto |= RB_PAUSE; 446 break; 447 case 'P': 448 if (!has_kbd) 449 howto |= RB_SERIAL | RB_MULTIPLE; 450 break; 451 case 'r': 452 howto |= RB_DFLTROOT; 453 break; 454 case 's': 455 howto |= RB_SINGLE; 456 break; 457 case 'S': 458 if (argv[i][j + 1] == 0) { 459 if (i + 1 == argc) { 460 setenv("comconsole_speed", "115200", 1); 461 } else { 462 cpy16to8(&argv[i + 1][0], var, 463 sizeof(var)); 464 setenv("comconsole_speed", var, 1); 465 } 466 i++; 467 break; 468 } else { 469 cpy16to8(&argv[i][j + 1], var, 470 sizeof(var)); 471 setenv("comconsole_speed", var, 1); 472 break; 473 } 474 case 'v': 475 howto |= RB_VERBOSE; 476 break; 477 } 478 } 479 } else { 480 vargood = false; 481 for (j = 0; argv[i][j] != 0; j++) { 482 if (j == sizeof(var)) { 483 vargood = false; 484 break; 485 } 486 if (j > 0 && argv[i][j] == '=') 487 vargood = true; 488 var[j] = (char)argv[i][j]; 489 } 490 if (vargood) { 491 var[j] = 0; 492 putenv(var); 493 } 494 } 495 } 496 return (howto); 497 } 498 499 500 EFI_STATUS 501 main(int argc, CHAR16 *argv[]) 502 { 503 EFI_GUID *guid; 504 int howto, i; 505 UINTN k; 506 bool has_kbd; 507 char *s; 508 EFI_DEVICE_PATH *imgpath; 509 CHAR16 *text; 510 EFI_STATUS status; 511 UINT16 boot_current; 512 size_t sz; 513 UINT16 boot_order[100]; 514 EFI_LOADED_IMAGE *img; 515 516 archsw.arch_autoload = efi_autoload; 517 archsw.arch_getdev = efi_getdev; 518 archsw.arch_copyin = efi_copyin; 519 archsw.arch_copyout = efi_copyout; 520 archsw.arch_readin = efi_readin; 521 #ifdef EFI_ZFS_BOOT 522 /* Note this needs to be set before ZFS init. */ 523 archsw.arch_zfs_probe = efi_zfs_probe; 524 #endif 525 526 /* Get our loaded image protocol interface structure. */ 527 BS->HandleProtocol(IH, &imgid, (VOID**)&img); 528 529 #ifdef EFI_ZFS_BOOT 530 /* Tell ZFS probe code where we booted from */ 531 efizfs_set_preferred(img->DeviceHandle); 532 #endif 533 /* Init the time source */ 534 efi_time_init(); 535 536 has_kbd = has_keyboard(); 537 538 /* 539 * XXX Chicken-and-egg problem; we want to have console output 540 * early, but some console attributes may depend on reading from 541 * eg. the boot device, which we can't do yet. We can use 542 * printf() etc. once this is done. 543 */ 544 cons_probe(); 545 546 /* 547 * Initialise the block cache. Set the upper limit. 548 */ 549 bcache_init(32768, 512); 550 551 howto = parse_args(argc, argv, has_kbd); 552 553 bootenv_set(howto); 554 555 /* 556 * XXX we need fallback to this stuff after looking at the ConIn, ConOut and ConErr variables 557 */ 558 if (howto & RB_MULTIPLE) { 559 if (howto & RB_SERIAL) 560 setenv("console", "comconsole efi" , 1); 561 else 562 setenv("console", "efi comconsole" , 1); 563 } else if (howto & RB_SERIAL) { 564 setenv("console", "comconsole" , 1); 565 } else 566 setenv("console", "efi", 1); 567 568 if (efi_copy_init()) { 569 printf("failed to allocate staging area\n"); 570 return (EFI_BUFFER_TOO_SMALL); 571 } 572 573 if ((s = getenv("fail_timeout")) != NULL) 574 fail_timeout = strtol(s, NULL, 10); 575 576 /* 577 * Scan the BLOCK IO MEDIA handles then 578 * march through the device switch probing for things. 579 */ 580 if ((i = efipart_inithandles()) == 0) { 581 for (i = 0; devsw[i] != NULL; i++) 582 if (devsw[i]->dv_init != NULL) 583 (devsw[i]->dv_init)(); 584 } else 585 printf("efipart_inithandles failed %d, expect failures", i); 586 587 printf("Command line arguments:"); 588 for (i = 0; i < argc; i++) 589 printf(" %S", argv[i]); 590 printf("\n"); 591 592 printf("Image base: 0x%lx\n", (u_long)img->ImageBase); 593 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16, 594 ST->Hdr.Revision & 0xffff); 595 printf("EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor, 596 ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); 597 598 printf("\n%s", bootprog_info); 599 600 /* Determine the devpath of our image so we can prefer it. */ 601 text = efi_devpath_name(img->FilePath); 602 if (text != NULL) { 603 printf(" Load Path: %S\n", text); 604 efi_setenv_freebsd_wcs("LoaderPath", text); 605 efi_free_devpath_name(text); 606 } 607 608 status = BS->HandleProtocol(img->DeviceHandle, &devid, (void **)&imgpath); 609 if (status == EFI_SUCCESS) { 610 text = efi_devpath_name(imgpath); 611 if (text != NULL) { 612 printf(" Load Device: %S\n", text); 613 efi_setenv_freebsd_wcs("LoaderDev", text); 614 efi_free_devpath_name(text); 615 } 616 } 617 618 boot_current = 0; 619 sz = sizeof(boot_current); 620 efi_global_getenv("BootCurrent", &boot_current, &sz); 621 printf(" BootCurrent: %04x\n", boot_current); 622 623 sz = sizeof(boot_order); 624 efi_global_getenv("BootOrder", &boot_order, &sz); 625 printf(" BootOrder:"); 626 for (i = 0; i < sz / sizeof(boot_order[0]); i++) 627 printf(" %04x%s", boot_order[i], 628 boot_order[i] == boot_current ? "[*]" : ""); 629 printf("\n"); 630 631 /* 632 * Disable the watchdog timer. By default the boot manager sets 633 * the timer to 5 minutes before invoking a boot option. If we 634 * want to return to the boot manager, we have to disable the 635 * watchdog timer and since we're an interactive program, we don't 636 * want to wait until the user types "quit". The timer may have 637 * fired by then. We don't care if this fails. It does not prevent 638 * normal functioning in any way... 639 */ 640 BS->SetWatchdogTimer(0, 0, 0, NULL); 641 642 /* 643 * Try and find a good currdev based on the image that was booted. 644 * It might be desirable here to have a short pause to allow falling 645 * through to the boot loader instead of returning instantly to follow 646 * the boot protocol and also allow an escape hatch for users wishing 647 * to try something different. 648 */ 649 if (find_currdev(img) != 0) 650 if (!interactive_interrupt("Failed to find bootable partition")) 651 return (EFI_NOT_FOUND); 652 653 efi_init_environment(); 654 setenv("LINES", "24", 1); /* optional */ 655 656 #if !defined(__arm__) 657 for (k = 0; k < ST->NumberOfTableEntries; k++) { 658 guid = &ST->ConfigurationTable[k].VendorGuid; 659 if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) { 660 char buf[40]; 661 662 snprintf(buf, sizeof(buf), "%p", 663 ST->ConfigurationTable[k].VendorTable); 664 setenv("hint.smbios.0.mem", buf, 1); 665 smbios_detect(ST->ConfigurationTable[k].VendorTable); 666 break; 667 } 668 } 669 #endif 670 671 interact(); /* doesn't return */ 672 673 return (EFI_SUCCESS); /* keep compiler happy */ 674 } 675 676 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 677 678 static int 679 command_reboot(int argc, char *argv[]) 680 { 681 int i; 682 683 for (i = 0; devsw[i] != NULL; ++i) 684 if (devsw[i]->dv_cleanup != NULL) 685 (devsw[i]->dv_cleanup)(); 686 687 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL); 688 689 /* NOTREACHED */ 690 return (CMD_ERROR); 691 } 692 693 COMMAND_SET(quit, "quit", "exit the loader", command_quit); 694 695 static int 696 command_quit(int argc, char *argv[]) 697 { 698 exit(0); 699 return (CMD_OK); 700 } 701 702 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap); 703 704 static int 705 command_memmap(int argc, char *argv[]) 706 { 707 UINTN sz; 708 EFI_MEMORY_DESCRIPTOR *map, *p; 709 UINTN key, dsz; 710 UINT32 dver; 711 EFI_STATUS status; 712 int i, ndesc; 713 char line[80]; 714 static char *types[] = { 715 "Reserved", 716 "LoaderCode", 717 "LoaderData", 718 "BootServicesCode", 719 "BootServicesData", 720 "RuntimeServicesCode", 721 "RuntimeServicesData", 722 "ConventionalMemory", 723 "UnusableMemory", 724 "ACPIReclaimMemory", 725 "ACPIMemoryNVS", 726 "MemoryMappedIO", 727 "MemoryMappedIOPortSpace", 728 "PalCode" 729 }; 730 731 sz = 0; 732 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver); 733 if (status != EFI_BUFFER_TOO_SMALL) { 734 printf("Can't determine memory map size\n"); 735 return (CMD_ERROR); 736 } 737 map = malloc(sz); 738 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver); 739 if (EFI_ERROR(status)) { 740 printf("Can't read memory map\n"); 741 return (CMD_ERROR); 742 } 743 744 ndesc = sz / dsz; 745 snprintf(line, sizeof(line), "%23s %12s %12s %8s %4s\n", 746 "Type", "Physical", "Virtual", "#Pages", "Attr"); 747 pager_open(); 748 if (pager_output(line)) { 749 pager_close(); 750 return (CMD_OK); 751 } 752 753 for (i = 0, p = map; i < ndesc; 754 i++, p = NextMemoryDescriptor(p, dsz)) { 755 printf("%23s %012jx %012jx %08jx ", types[p->Type], 756 (uintmax_t)p->PhysicalStart, (uintmax_t)p->VirtualStart, 757 (uintmax_t)p->NumberOfPages); 758 if (p->Attribute & EFI_MEMORY_UC) 759 printf("UC "); 760 if (p->Attribute & EFI_MEMORY_WC) 761 printf("WC "); 762 if (p->Attribute & EFI_MEMORY_WT) 763 printf("WT "); 764 if (p->Attribute & EFI_MEMORY_WB) 765 printf("WB "); 766 if (p->Attribute & EFI_MEMORY_UCE) 767 printf("UCE "); 768 if (p->Attribute & EFI_MEMORY_WP) 769 printf("WP "); 770 if (p->Attribute & EFI_MEMORY_RP) 771 printf("RP "); 772 if (p->Attribute & EFI_MEMORY_XP) 773 printf("XP "); 774 if (pager_output("\n")) 775 break; 776 } 777 778 pager_close(); 779 return (CMD_OK); 780 } 781 782 COMMAND_SET(configuration, "configuration", "print configuration tables", 783 command_configuration); 784 785 static const char * 786 guid_to_string(EFI_GUID *guid) 787 { 788 static char buf[40]; 789 790 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", 791 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0], 792 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4], 793 guid->Data4[5], guid->Data4[6], guid->Data4[7]); 794 return (buf); 795 } 796 797 static int 798 command_configuration(int argc, char *argv[]) 799 { 800 char line[80]; 801 UINTN i; 802 803 snprintf(line, sizeof(line), "NumberOfTableEntries=%lu\n", 804 (unsigned long)ST->NumberOfTableEntries); 805 pager_open(); 806 if (pager_output(line)) { 807 pager_close(); 808 return (CMD_OK); 809 } 810 811 for (i = 0; i < ST->NumberOfTableEntries; i++) { 812 EFI_GUID *guid; 813 814 printf(" "); 815 guid = &ST->ConfigurationTable[i].VendorGuid; 816 if (!memcmp(guid, &mps, sizeof(EFI_GUID))) 817 printf("MPS Table"); 818 else if (!memcmp(guid, &acpi, sizeof(EFI_GUID))) 819 printf("ACPI Table"); 820 else if (!memcmp(guid, &acpi20, sizeof(EFI_GUID))) 821 printf("ACPI 2.0 Table"); 822 else if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) 823 printf("SMBIOS Table %p", 824 ST->ConfigurationTable[i].VendorTable); 825 else if (!memcmp(guid, &smbios3, sizeof(EFI_GUID))) 826 printf("SMBIOS3 Table"); 827 else if (!memcmp(guid, &dxe, sizeof(EFI_GUID))) 828 printf("DXE Table"); 829 else if (!memcmp(guid, &hoblist, sizeof(EFI_GUID))) 830 printf("HOB List Table"); 831 else if (!memcmp(guid, &lzmadecomp, sizeof(EFI_GUID))) 832 printf("LZMA Compression"); 833 else if (!memcmp(guid, &mpcore, sizeof(EFI_GUID))) 834 printf("ARM MpCore Information Table"); 835 else if (!memcmp(guid, &esrt, sizeof(EFI_GUID))) 836 printf("ESRT Table"); 837 else if (!memcmp(guid, &memtype, sizeof(EFI_GUID))) 838 printf("Memory Type Information Table"); 839 else if (!memcmp(guid, &debugimg, sizeof(EFI_GUID))) 840 printf("Debug Image Info Table"); 841 else if (!memcmp(guid, &fdtdtb, sizeof(EFI_GUID))) 842 printf("FDT Table"); 843 else 844 printf("Unknown Table (%s)", guid_to_string(guid)); 845 snprintf(line, sizeof(line), " at %p\n", 846 ST->ConfigurationTable[i].VendorTable); 847 if (pager_output(line)) 848 break; 849 } 850 851 pager_close(); 852 return (CMD_OK); 853 } 854 855 856 COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode); 857 858 static int 859 command_mode(int argc, char *argv[]) 860 { 861 UINTN cols, rows; 862 unsigned int mode; 863 int i; 864 char *cp; 865 char rowenv[8]; 866 EFI_STATUS status; 867 SIMPLE_TEXT_OUTPUT_INTERFACE *conout; 868 extern void HO(void); 869 870 conout = ST->ConOut; 871 872 if (argc > 1) { 873 mode = strtol(argv[1], &cp, 0); 874 if (cp[0] != '\0') { 875 printf("Invalid mode\n"); 876 return (CMD_ERROR); 877 } 878 status = conout->QueryMode(conout, mode, &cols, &rows); 879 if (EFI_ERROR(status)) { 880 printf("invalid mode %d\n", mode); 881 return (CMD_ERROR); 882 } 883 status = conout->SetMode(conout, mode); 884 if (EFI_ERROR(status)) { 885 printf("couldn't set mode %d\n", mode); 886 return (CMD_ERROR); 887 } 888 sprintf(rowenv, "%u", (unsigned)rows); 889 setenv("LINES", rowenv, 1); 890 HO(); /* set cursor */ 891 return (CMD_OK); 892 } 893 894 printf("Current mode: %d\n", conout->Mode->Mode); 895 for (i = 0; i <= conout->Mode->MaxMode; i++) { 896 status = conout->QueryMode(conout, i, &cols, &rows); 897 if (EFI_ERROR(status)) 898 continue; 899 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols, 900 (unsigned)rows); 901 } 902 903 if (i != 0) 904 printf("Select a mode with the command \"mode <number>\"\n"); 905 906 return (CMD_OK); 907 } 908 909 #ifdef LOADER_FDT_SUPPORT 910 extern int command_fdt_internal(int argc, char *argv[]); 911 912 /* 913 * Since proper fdt command handling function is defined in fdt_loader_cmd.c, 914 * and declaring it as extern is in contradiction with COMMAND_SET() macro 915 * (which uses static pointer), we're defining wrapper function, which 916 * calls the proper fdt handling routine. 917 */ 918 static int 919 command_fdt(int argc, char *argv[]) 920 { 921 922 return (command_fdt_internal(argc, argv)); 923 } 924 925 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt); 926 #endif 927 928 /* 929 * Chain load another efi loader. 930 */ 931 static int 932 command_chain(int argc, char *argv[]) 933 { 934 EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL; 935 EFI_HANDLE loaderhandle; 936 EFI_LOADED_IMAGE *loaded_image; 937 EFI_STATUS status; 938 struct stat st; 939 struct devdesc *dev; 940 char *name, *path; 941 void *buf; 942 int fd; 943 944 if (argc < 2) { 945 command_errmsg = "wrong number of arguments"; 946 return (CMD_ERROR); 947 } 948 949 name = argv[1]; 950 951 if ((fd = open(name, O_RDONLY)) < 0) { 952 command_errmsg = "no such file"; 953 return (CMD_ERROR); 954 } 955 956 if (fstat(fd, &st) < -1) { 957 command_errmsg = "stat failed"; 958 close(fd); 959 return (CMD_ERROR); 960 } 961 962 status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf); 963 if (status != EFI_SUCCESS) { 964 command_errmsg = "failed to allocate buffer"; 965 close(fd); 966 return (CMD_ERROR); 967 } 968 if (read(fd, buf, st.st_size) != st.st_size) { 969 command_errmsg = "error while reading the file"; 970 (void)BS->FreePool(buf); 971 close(fd); 972 return (CMD_ERROR); 973 } 974 close(fd); 975 status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle); 976 (void)BS->FreePool(buf); 977 if (status != EFI_SUCCESS) { 978 command_errmsg = "LoadImage failed"; 979 return (CMD_ERROR); 980 } 981 status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID, 982 (void **)&loaded_image); 983 984 if (argc > 2) { 985 int i, len = 0; 986 CHAR16 *argp; 987 988 for (i = 2; i < argc; i++) 989 len += strlen(argv[i]) + 1; 990 991 len *= sizeof (*argp); 992 loaded_image->LoadOptions = argp = malloc (len); 993 loaded_image->LoadOptionsSize = len; 994 for (i = 2; i < argc; i++) { 995 char *ptr = argv[i]; 996 while (*ptr) 997 *(argp++) = *(ptr++); 998 *(argp++) = ' '; 999 } 1000 *(--argv) = 0; 1001 } 1002 1003 if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) { 1004 #ifdef EFI_ZFS_BOOT 1005 struct zfs_devdesc *z_dev; 1006 #endif 1007 struct disk_devdesc *d_dev; 1008 pdinfo_t *hd, *pd; 1009 1010 switch (dev->d_dev->dv_type) { 1011 #ifdef EFI_ZFS_BOOT 1012 case DEVT_ZFS: 1013 z_dev = (struct zfs_devdesc *)dev; 1014 loaded_image->DeviceHandle = 1015 efizfs_get_handle_by_guid(z_dev->pool_guid); 1016 break; 1017 #endif 1018 case DEVT_NET: 1019 loaded_image->DeviceHandle = 1020 efi_find_handle(dev->d_dev, dev->d_unit); 1021 break; 1022 default: 1023 hd = efiblk_get_pdinfo(dev); 1024 if (STAILQ_EMPTY(&hd->pd_part)) { 1025 loaded_image->DeviceHandle = hd->pd_handle; 1026 break; 1027 } 1028 d_dev = (struct disk_devdesc *)dev; 1029 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) { 1030 /* 1031 * d_partition should be 255 1032 */ 1033 if (pd->pd_unit == (uint32_t)d_dev->d_slice) { 1034 loaded_image->DeviceHandle = 1035 pd->pd_handle; 1036 break; 1037 } 1038 } 1039 break; 1040 } 1041 } 1042 1043 dev_cleanup(); 1044 status = BS->StartImage(loaderhandle, NULL, NULL); 1045 if (status != EFI_SUCCESS) { 1046 command_errmsg = "StartImage failed"; 1047 free(loaded_image->LoadOptions); 1048 loaded_image->LoadOptions = NULL; 1049 status = BS->UnloadImage(loaded_image); 1050 return (CMD_ERROR); 1051 } 1052 1053 return (CMD_ERROR); /* not reached */ 1054 } 1055 1056 COMMAND_SET(chain, "chain", "chain load file", command_chain); 1057