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