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 <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 57 struct arch_switch archsw; /* MI/MD interface boundary */ 58 59 EFI_GUID acpi = ACPI_TABLE_GUID; 60 EFI_GUID acpi20 = ACPI_20_TABLE_GUID; 61 EFI_GUID devid = DEVICE_PATH_PROTOCOL; 62 EFI_GUID imgid = LOADED_IMAGE_PROTOCOL; 63 EFI_GUID mps = MPS_TABLE_GUID; 64 EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL; 65 EFI_GUID smbios = SMBIOS_TABLE_GUID; 66 EFI_GUID smbios3 = SMBIOS3_TABLE_GUID; 67 EFI_GUID dxe = DXE_SERVICES_TABLE_GUID; 68 EFI_GUID hoblist = HOB_LIST_TABLE_GUID; 69 EFI_GUID lzmadecomp = LZMA_DECOMPRESSION_GUID; 70 EFI_GUID mpcore = ARM_MP_CORE_INFO_TABLE_GUID; 71 EFI_GUID esrt = ESRT_TABLE_GUID; 72 EFI_GUID memtype = MEMORY_TYPE_INFORMATION_TABLE_GUID; 73 EFI_GUID debugimg = DEBUG_IMAGE_INFO_TABLE_GUID; 74 EFI_GUID fdtdtb = FDT_TABLE_GUID; 75 EFI_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 */ 84 static int fail_timeout = 5; 85 86 static bool 87 has_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 } 163 out: 164 free(hin); 165 return retval; 166 } 167 168 static void 169 set_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 181 static void 182 set_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 192 static void 193 set_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 220 static bool 221 sanity_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 230 static bool 231 probe_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 248 static bool 249 try_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 272 static int 273 find_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 /* 320 * Roll up the ZFS special case 321 * for those partitions that have 322 * zpools on them 323 */ 324 if (try_as_currdev(dp, pp)) 325 return (0); 326 } 327 } 328 } else { 329 printf("Can't find device by handle\n"); 330 } 331 332 /* 333 * Try the device handle from our loaded image first. If that 334 * fails, use the device path from the loaded image and see if 335 * any of the nodes in that path match one of the enumerated 336 * handles. Currently, this handle list is only for netboot. 337 */ 338 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) { 339 set_currdev_devsw(dev, unit); 340 if (sanity_check_currdev()) 341 return (0); 342 } 343 344 copy = NULL; 345 devpath = efi_lookup_image_devpath(IH); 346 while (devpath != NULL) { 347 h = efi_devpath_handle(devpath); 348 if (h == NULL) 349 break; 350 351 free(copy); 352 copy = NULL; 353 354 if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) { 355 set_currdev_devsw(dev, unit); 356 if (sanity_check_currdev()) 357 return (0); 358 } 359 360 devpath = efi_lookup_devpath(h); 361 if (devpath != NULL) { 362 copy = efi_devpath_trim(devpath); 363 devpath = copy; 364 } 365 } 366 free(copy); 367 368 return (ENOENT); 369 } 370 371 static bool 372 interactive_interrupt(const char *msg) 373 { 374 time_t now, then, last; 375 376 last = 0; 377 now = then = getsecs(); 378 printf("%s\n", msg); 379 if (fail_timeout == -2) /* Always break to OK */ 380 return (true); 381 if (fail_timeout == -1) /* Never break to OK */ 382 return (false); 383 do { 384 if (last != now) { 385 printf("press any key to interrupt reboot in %d seconds\r", 386 fail_timeout - (int)(now - then)); 387 last = now; 388 } 389 390 /* XXX no pause or timeout wait for char */ 391 if (ischar()) 392 return (true); 393 now = getsecs(); 394 } while (now - then < fail_timeout); 395 return (false); 396 } 397 398 static int 399 parse_args(int argc, CHAR16 *argv[]) 400 { 401 int i, j, howto; 402 bool vargood; 403 char var[128]; 404 405 /* 406 * Parse the args to set the console settings, etc 407 * boot1.efi passes these in, if it can read /boot.config or /boot/config 408 * or iPXE may be setup to pass these in. Or the optional argument in the 409 * boot environment was used to pass these arguments in (in which case 410 * neither /boot.config nor /boot/config are consulted). 411 * 412 * Loop through the args, and for each one that contains an '=' that is 413 * not the first character, add it to the environment. This allows 414 * loader and kernel env vars to be passed on the command line. Convert 415 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied (though this 416 * method is flawed for non-ASCII characters). 417 */ 418 howto = 0; 419 for (i = 1; i < argc; i++) { 420 cpy16to8(argv[i], var, sizeof(var)); 421 howto |= boot_parse_arg(var); 422 } 423 424 return (howto); 425 } 426 427 /* 428 * Parse ConOut (the list of consoles active) and see if we can find a 429 * serial port and/or a video port. It would be nice to also walk the 430 * ACPI name space to map the UID for the serial port to a port. The 431 * latter is especially hard. 432 */ 433 static int 434 parse_uefi_con_out(void) 435 { 436 int how, rv; 437 int vid_seen = 0, com_seen = 0, seen = 0; 438 size_t sz; 439 char buf[4096], *ep; 440 EFI_DEVICE_PATH *node; 441 ACPI_HID_DEVICE_PATH *acpi; 442 UART_DEVICE_PATH *uart; 443 bool pci_pending; 444 445 how = 0; 446 sz = sizeof(buf); 447 rv = efi_global_getenv("ConOut", buf, &sz); 448 if (rv != EFI_SUCCESS) 449 goto out; 450 ep = buf + sz; 451 node = (EFI_DEVICE_PATH *)buf; 452 while ((char *)node < ep) { 453 pci_pending = false; 454 if (DevicePathType(node) == ACPI_DEVICE_PATH && 455 DevicePathSubType(node) == ACPI_DP) { 456 /* Check for Serial node */ 457 acpi = (void *)node; 458 if (EISA_ID_TO_NUM(acpi->HID) == 0x501) 459 com_seen = ++seen; 460 } else if (DevicePathType(node) == MESSAGING_DEVICE_PATH && 461 DevicePathSubType(node) == MSG_UART_DP) { 462 char bd[16]; 463 464 uart = (void *)node; 465 snprintf(bd, sizeof(bd), "%d", uart->BaudRate); 466 setenv("efi_com_speed", bd, 1); 467 } else if (DevicePathType(node) == ACPI_DEVICE_PATH && 468 DevicePathSubType(node) == ACPI_ADR_DP) { 469 /* Check for AcpiAdr() Node for video */ 470 vid_seen = ++seen; 471 } else if (DevicePathType(node) == HARDWARE_DEVICE_PATH && 472 DevicePathSubType(node) == HW_PCI_DP) { 473 /* 474 * Note, vmware fusion has a funky console device 475 * PciRoot(0x0)/Pci(0xf,0x0) 476 * which we can only detect at the end since we also 477 * have to cope with: 478 * PciRoot(0x0)/Pci(0x1f,0x0)/Serial(0x1) 479 * so only match it if it's last. 480 */ 481 pci_pending = true; 482 } 483 node = NextDevicePathNode(node); /* Skip the end node */ 484 } 485 if (pci_pending && vid_seen == 0) 486 vid_seen = ++seen; 487 488 /* 489 * Truth table for RB_MULTIPLE | RB_SERIAL 490 * Value Result 491 * 0 Use only video console 492 * RB_SERIAL Use only serial console 493 * RB_MULTIPLE Use both video and serial console 494 * (but video is primary so gets rc messages) 495 * both Use both video and serial console 496 * (but serial is primary so gets rc messages) 497 * 498 * Try to honor this as best we can. If only one of serial / video 499 * found, then use that. Otherwise, use the first one we found. 500 * This also implies if we found nothing, default to video. 501 */ 502 how = 0; 503 if (vid_seen && com_seen) { 504 how |= RB_MULTIPLE; 505 if (com_seen < vid_seen) 506 how |= RB_SERIAL; 507 } else if (com_seen) 508 how |= RB_SERIAL; 509 out: 510 return (how); 511 } 512 513 EFI_STATUS 514 main(int argc, CHAR16 *argv[]) 515 { 516 EFI_GUID *guid; 517 int howto, i, uhowto; 518 UINTN k; 519 bool has_kbd; 520 char *s; 521 EFI_DEVICE_PATH *imgpath; 522 CHAR16 *text; 523 EFI_STATUS status; 524 UINT16 boot_current; 525 size_t sz; 526 UINT16 boot_order[100]; 527 EFI_LOADED_IMAGE *img; 528 529 archsw.arch_autoload = efi_autoload; 530 archsw.arch_getdev = efi_getdev; 531 archsw.arch_copyin = efi_copyin; 532 archsw.arch_copyout = efi_copyout; 533 archsw.arch_readin = efi_readin; 534 #ifdef EFI_ZFS_BOOT 535 /* Note this needs to be set before ZFS init. */ 536 archsw.arch_zfs_probe = efi_zfs_probe; 537 #endif 538 539 /* Get our loaded image protocol interface structure. */ 540 BS->HandleProtocol(IH, &imgid, (VOID**)&img); 541 542 #ifdef EFI_ZFS_BOOT 543 /* Tell ZFS probe code where we booted from */ 544 efizfs_set_preferred(img->DeviceHandle); 545 #endif 546 /* Init the time source */ 547 efi_time_init(); 548 549 has_kbd = has_keyboard(); 550 551 /* 552 * XXX Chicken-and-egg problem; we want to have console output 553 * early, but some console attributes may depend on reading from 554 * eg. the boot device, which we can't do yet. We can use 555 * printf() etc. once this is done. 556 */ 557 setenv("console", "efi", 1); 558 cons_probe(); 559 560 /* 561 * Initialise the block cache. Set the upper limit. 562 */ 563 bcache_init(32768, 512); 564 565 howto = parse_args(argc, argv); 566 if (!has_kbd && (howto & RB_PROBE)) 567 howto |= RB_SERIAL | RB_MULTIPLE; 568 howto &= ~RB_PROBE; 569 570 uhowto = parse_uefi_con_out(); 571 572 /* 573 * We now have two notions of console. howto should be viewed as 574 * overrides. 575 */ 576 #define VIDEO_ONLY 0 577 #define SERIAL_ONLY RB_SERIAL 578 #define VID_SER_BOTH RB_MULTIPLE 579 #define SER_VID_BOTH (RB_SERIAL | RB_MULTIPLE) 580 #define CON_MASK (RB_SERIAL | RB_MULTIPLE) 581 582 if ((howto & CON_MASK) == 0) { 583 /* No override, uhowto is controlling and efi cons is perfect */ 584 howto = howto | (uhowto & CON_MASK); 585 setenv("console", "efi", 1); 586 } else if ((howto & CON_MASK) == (uhowto & CON_MASK)) { 587 /* override matches what UEFI told us, efi console is perfect */ 588 setenv("console", "efi", 1); 589 } else if ((uhowto & (CON_MASK)) != 0) { 590 /* 591 * We detected a serial console on ConOut. All possible 592 * overrides include serial. We can't really override what efi 593 * gives us, so we use it knowing it's the best choice. 594 */ 595 setenv("console", "efi", 1); 596 } else { 597 /* 598 * We detected some kind of serial in the override, but ConOut 599 * has no serial, so we have to sort out which case it really is. 600 */ 601 switch (howto & CON_MASK) { 602 case SERIAL_ONLY: 603 setenv("console", "comconsole", 1); 604 break; 605 case VID_SER_BOTH: 606 setenv("console", "efi comconsole", 1); 607 break; 608 case SER_VID_BOTH: 609 setenv("console", "comconsole efi", 1); 610 break; 611 /* case VIDEO_ONLY can't happen -- it's the first if above */ 612 } 613 } 614 /* 615 * howto is set now how we want to export the flags to the kernel, so 616 * set the env based on it. 617 */ 618 boot_howto_to_env(howto); 619 620 if (efi_copy_init()) { 621 printf("failed to allocate staging area\n"); 622 return (EFI_BUFFER_TOO_SMALL); 623 } 624 625 if ((s = getenv("fail_timeout")) != NULL) 626 fail_timeout = strtol(s, NULL, 10); 627 628 /* 629 * Scan the BLOCK IO MEDIA handles then 630 * march through the device switch probing for things. 631 */ 632 if ((i = efipart_inithandles()) == 0) { 633 for (i = 0; devsw[i] != NULL; i++) 634 if (devsw[i]->dv_init != NULL) 635 (devsw[i]->dv_init)(); 636 } else 637 printf("efipart_inithandles failed %d, expect failures", i); 638 639 printf("%s\n", bootprog_info); 640 printf(" Command line arguments:"); 641 for (i = 0; i < argc; i++) 642 printf(" %S", argv[i]); 643 printf("\n"); 644 645 printf(" EFI version: %d.%02d\n", ST->Hdr.Revision >> 16, 646 ST->Hdr.Revision & 0xffff); 647 printf(" EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor, 648 ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); 649 650 651 /* Determine the devpath of our image so we can prefer it. */ 652 text = efi_devpath_name(img->FilePath); 653 if (text != NULL) { 654 printf(" Load Path: %S\n", text); 655 efi_setenv_freebsd_wcs("LoaderPath", text); 656 efi_free_devpath_name(text); 657 } 658 659 status = BS->HandleProtocol(img->DeviceHandle, &devid, (void **)&imgpath); 660 if (status == EFI_SUCCESS) { 661 text = efi_devpath_name(imgpath); 662 if (text != NULL) { 663 printf(" Load Device: %S\n", text); 664 efi_setenv_freebsd_wcs("LoaderDev", text); 665 efi_free_devpath_name(text); 666 } 667 } 668 669 boot_current = 0; 670 sz = sizeof(boot_current); 671 efi_global_getenv("BootCurrent", &boot_current, &sz); 672 printf(" BootCurrent: %04x\n", boot_current); 673 674 sz = sizeof(boot_order); 675 efi_global_getenv("BootOrder", &boot_order, &sz); 676 printf(" BootOrder:"); 677 for (i = 0; i < sz / sizeof(boot_order[0]); i++) 678 printf(" %04x%s", boot_order[i], 679 boot_order[i] == boot_current ? "[*]" : ""); 680 printf("\n"); 681 682 /* 683 * Disable the watchdog timer. By default the boot manager sets 684 * the timer to 5 minutes before invoking a boot option. If we 685 * want to return to the boot manager, we have to disable the 686 * watchdog timer and since we're an interactive program, we don't 687 * want to wait until the user types "quit". The timer may have 688 * fired by then. We don't care if this fails. It does not prevent 689 * normal functioning in any way... 690 */ 691 BS->SetWatchdogTimer(0, 0, 0, NULL); 692 693 /* 694 * Try and find a good currdev based on the image that was booted. 695 * It might be desirable here to have a short pause to allow falling 696 * through to the boot loader instead of returning instantly to follow 697 * the boot protocol and also allow an escape hatch for users wishing 698 * to try something different. 699 */ 700 if (find_currdev(img) != 0) 701 if (!interactive_interrupt("Failed to find bootable partition")) 702 return (EFI_NOT_FOUND); 703 704 efi_init_environment(); 705 706 #if !defined(__arm__) 707 for (k = 0; k < ST->NumberOfTableEntries; k++) { 708 guid = &ST->ConfigurationTable[k].VendorGuid; 709 if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) { 710 char buf[40]; 711 712 snprintf(buf, sizeof(buf), "%p", 713 ST->ConfigurationTable[k].VendorTable); 714 setenv("hint.smbios.0.mem", buf, 1); 715 smbios_detect(ST->ConfigurationTable[k].VendorTable); 716 break; 717 } 718 } 719 #endif 720 721 interact(); /* doesn't return */ 722 723 return (EFI_SUCCESS); /* keep compiler happy */ 724 } 725 726 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 727 728 static int 729 command_reboot(int argc, char *argv[]) 730 { 731 int i; 732 733 for (i = 0; devsw[i] != NULL; ++i) 734 if (devsw[i]->dv_cleanup != NULL) 735 (devsw[i]->dv_cleanup)(); 736 737 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL); 738 739 /* NOTREACHED */ 740 return (CMD_ERROR); 741 } 742 743 COMMAND_SET(quit, "quit", "exit the loader", command_quit); 744 745 static int 746 command_quit(int argc, char *argv[]) 747 { 748 exit(0); 749 return (CMD_OK); 750 } 751 752 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap); 753 754 static int 755 command_memmap(int argc, char *argv[]) 756 { 757 UINTN sz; 758 EFI_MEMORY_DESCRIPTOR *map, *p; 759 UINTN key, dsz; 760 UINT32 dver; 761 EFI_STATUS status; 762 int i, ndesc; 763 char line[80]; 764 static char *types[] = { 765 "Reserved", 766 "LoaderCode", 767 "LoaderData", 768 "BootServicesCode", 769 "BootServicesData", 770 "RuntimeServicesCode", 771 "RuntimeServicesData", 772 "ConventionalMemory", 773 "UnusableMemory", 774 "ACPIReclaimMemory", 775 "ACPIMemoryNVS", 776 "MemoryMappedIO", 777 "MemoryMappedIOPortSpace", 778 "PalCode" 779 }; 780 781 sz = 0; 782 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver); 783 if (status != EFI_BUFFER_TOO_SMALL) { 784 printf("Can't determine memory map size\n"); 785 return (CMD_ERROR); 786 } 787 map = malloc(sz); 788 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver); 789 if (EFI_ERROR(status)) { 790 printf("Can't read memory map\n"); 791 return (CMD_ERROR); 792 } 793 794 ndesc = sz / dsz; 795 snprintf(line, sizeof(line), "%23s %12s %12s %8s %4s\n", 796 "Type", "Physical", "Virtual", "#Pages", "Attr"); 797 pager_open(); 798 if (pager_output(line)) { 799 pager_close(); 800 return (CMD_OK); 801 } 802 803 for (i = 0, p = map; i < ndesc; 804 i++, p = NextMemoryDescriptor(p, dsz)) { 805 printf("%23s %012jx %012jx %08jx ", types[p->Type], 806 (uintmax_t)p->PhysicalStart, (uintmax_t)p->VirtualStart, 807 (uintmax_t)p->NumberOfPages); 808 if (p->Attribute & EFI_MEMORY_UC) 809 printf("UC "); 810 if (p->Attribute & EFI_MEMORY_WC) 811 printf("WC "); 812 if (p->Attribute & EFI_MEMORY_WT) 813 printf("WT "); 814 if (p->Attribute & EFI_MEMORY_WB) 815 printf("WB "); 816 if (p->Attribute & EFI_MEMORY_UCE) 817 printf("UCE "); 818 if (p->Attribute & EFI_MEMORY_WP) 819 printf("WP "); 820 if (p->Attribute & EFI_MEMORY_RP) 821 printf("RP "); 822 if (p->Attribute & EFI_MEMORY_XP) 823 printf("XP "); 824 if (pager_output("\n")) 825 break; 826 } 827 828 pager_close(); 829 return (CMD_OK); 830 } 831 832 COMMAND_SET(configuration, "configuration", "print configuration tables", 833 command_configuration); 834 835 static const char * 836 guid_to_string(EFI_GUID *guid) 837 { 838 static char buf[40]; 839 840 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", 841 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0], 842 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4], 843 guid->Data4[5], guid->Data4[6], guid->Data4[7]); 844 return (buf); 845 } 846 847 static int 848 command_configuration(int argc, char *argv[]) 849 { 850 char line[80]; 851 UINTN i; 852 853 snprintf(line, sizeof(line), "NumberOfTableEntries=%lu\n", 854 (unsigned long)ST->NumberOfTableEntries); 855 pager_open(); 856 if (pager_output(line)) { 857 pager_close(); 858 return (CMD_OK); 859 } 860 861 for (i = 0; i < ST->NumberOfTableEntries; i++) { 862 EFI_GUID *guid; 863 864 printf(" "); 865 guid = &ST->ConfigurationTable[i].VendorGuid; 866 if (!memcmp(guid, &mps, sizeof(EFI_GUID))) 867 printf("MPS Table"); 868 else if (!memcmp(guid, &acpi, sizeof(EFI_GUID))) 869 printf("ACPI Table"); 870 else if (!memcmp(guid, &acpi20, sizeof(EFI_GUID))) 871 printf("ACPI 2.0 Table"); 872 else if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) 873 printf("SMBIOS Table %p", 874 ST->ConfigurationTable[i].VendorTable); 875 else if (!memcmp(guid, &smbios3, sizeof(EFI_GUID))) 876 printf("SMBIOS3 Table"); 877 else if (!memcmp(guid, &dxe, sizeof(EFI_GUID))) 878 printf("DXE Table"); 879 else if (!memcmp(guid, &hoblist, sizeof(EFI_GUID))) 880 printf("HOB List Table"); 881 else if (!memcmp(guid, &lzmadecomp, sizeof(EFI_GUID))) 882 printf("LZMA Compression"); 883 else if (!memcmp(guid, &mpcore, sizeof(EFI_GUID))) 884 printf("ARM MpCore Information Table"); 885 else if (!memcmp(guid, &esrt, sizeof(EFI_GUID))) 886 printf("ESRT Table"); 887 else if (!memcmp(guid, &memtype, sizeof(EFI_GUID))) 888 printf("Memory Type Information Table"); 889 else if (!memcmp(guid, &debugimg, sizeof(EFI_GUID))) 890 printf("Debug Image Info Table"); 891 else if (!memcmp(guid, &fdtdtb, sizeof(EFI_GUID))) 892 printf("FDT Table"); 893 else 894 printf("Unknown Table (%s)", guid_to_string(guid)); 895 snprintf(line, sizeof(line), " at %p\n", 896 ST->ConfigurationTable[i].VendorTable); 897 if (pager_output(line)) 898 break; 899 } 900 901 pager_close(); 902 return (CMD_OK); 903 } 904 905 906 COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode); 907 908 static int 909 command_mode(int argc, char *argv[]) 910 { 911 UINTN cols, rows; 912 unsigned int mode; 913 int i; 914 char *cp; 915 char rowenv[8]; 916 EFI_STATUS status; 917 SIMPLE_TEXT_OUTPUT_INTERFACE *conout; 918 extern void HO(void); 919 920 conout = ST->ConOut; 921 922 if (argc > 1) { 923 mode = strtol(argv[1], &cp, 0); 924 if (cp[0] != '\0') { 925 printf("Invalid mode\n"); 926 return (CMD_ERROR); 927 } 928 status = conout->QueryMode(conout, mode, &cols, &rows); 929 if (EFI_ERROR(status)) { 930 printf("invalid mode %d\n", mode); 931 return (CMD_ERROR); 932 } 933 status = conout->SetMode(conout, mode); 934 if (EFI_ERROR(status)) { 935 printf("couldn't set mode %d\n", mode); 936 return (CMD_ERROR); 937 } 938 sprintf(rowenv, "%u", (unsigned)rows); 939 setenv("LINES", rowenv, 1); 940 HO(); /* set cursor */ 941 return (CMD_OK); 942 } 943 944 printf("Current mode: %d\n", conout->Mode->Mode); 945 for (i = 0; i <= conout->Mode->MaxMode; i++) { 946 status = conout->QueryMode(conout, i, &cols, &rows); 947 if (EFI_ERROR(status)) 948 continue; 949 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols, 950 (unsigned)rows); 951 } 952 953 if (i != 0) 954 printf("Select a mode with the command \"mode <number>\"\n"); 955 956 return (CMD_OK); 957 } 958 959 #ifdef LOADER_FDT_SUPPORT 960 extern int command_fdt_internal(int argc, char *argv[]); 961 962 /* 963 * Since proper fdt command handling function is defined in fdt_loader_cmd.c, 964 * and declaring it as extern is in contradiction with COMMAND_SET() macro 965 * (which uses static pointer), we're defining wrapper function, which 966 * calls the proper fdt handling routine. 967 */ 968 static int 969 command_fdt(int argc, char *argv[]) 970 { 971 972 return (command_fdt_internal(argc, argv)); 973 } 974 975 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt); 976 #endif 977 978 /* 979 * Chain load another efi loader. 980 */ 981 static int 982 command_chain(int argc, char *argv[]) 983 { 984 EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL; 985 EFI_HANDLE loaderhandle; 986 EFI_LOADED_IMAGE *loaded_image; 987 EFI_STATUS status; 988 struct stat st; 989 struct devdesc *dev; 990 char *name, *path; 991 void *buf; 992 int fd; 993 994 if (argc < 2) { 995 command_errmsg = "wrong number of arguments"; 996 return (CMD_ERROR); 997 } 998 999 name = argv[1]; 1000 1001 if ((fd = open(name, O_RDONLY)) < 0) { 1002 command_errmsg = "no such file"; 1003 return (CMD_ERROR); 1004 } 1005 1006 if (fstat(fd, &st) < -1) { 1007 command_errmsg = "stat failed"; 1008 close(fd); 1009 return (CMD_ERROR); 1010 } 1011 1012 status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf); 1013 if (status != EFI_SUCCESS) { 1014 command_errmsg = "failed to allocate buffer"; 1015 close(fd); 1016 return (CMD_ERROR); 1017 } 1018 if (read(fd, buf, st.st_size) != st.st_size) { 1019 command_errmsg = "error while reading the file"; 1020 (void)BS->FreePool(buf); 1021 close(fd); 1022 return (CMD_ERROR); 1023 } 1024 close(fd); 1025 status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle); 1026 (void)BS->FreePool(buf); 1027 if (status != EFI_SUCCESS) { 1028 command_errmsg = "LoadImage failed"; 1029 return (CMD_ERROR); 1030 } 1031 status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID, 1032 (void **)&loaded_image); 1033 1034 if (argc > 2) { 1035 int i, len = 0; 1036 CHAR16 *argp; 1037 1038 for (i = 2; i < argc; i++) 1039 len += strlen(argv[i]) + 1; 1040 1041 len *= sizeof (*argp); 1042 loaded_image->LoadOptions = argp = malloc (len); 1043 loaded_image->LoadOptionsSize = len; 1044 for (i = 2; i < argc; i++) { 1045 char *ptr = argv[i]; 1046 while (*ptr) 1047 *(argp++) = *(ptr++); 1048 *(argp++) = ' '; 1049 } 1050 *(--argv) = 0; 1051 } 1052 1053 if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) { 1054 #ifdef EFI_ZFS_BOOT 1055 struct zfs_devdesc *z_dev; 1056 #endif 1057 struct disk_devdesc *d_dev; 1058 pdinfo_t *hd, *pd; 1059 1060 switch (dev->d_dev->dv_type) { 1061 #ifdef EFI_ZFS_BOOT 1062 case DEVT_ZFS: 1063 z_dev = (struct zfs_devdesc *)dev; 1064 loaded_image->DeviceHandle = 1065 efizfs_get_handle_by_guid(z_dev->pool_guid); 1066 break; 1067 #endif 1068 case DEVT_NET: 1069 loaded_image->DeviceHandle = 1070 efi_find_handle(dev->d_dev, dev->d_unit); 1071 break; 1072 default: 1073 hd = efiblk_get_pdinfo(dev); 1074 if (STAILQ_EMPTY(&hd->pd_part)) { 1075 loaded_image->DeviceHandle = hd->pd_handle; 1076 break; 1077 } 1078 d_dev = (struct disk_devdesc *)dev; 1079 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) { 1080 /* 1081 * d_partition should be 255 1082 */ 1083 if (pd->pd_unit == (uint32_t)d_dev->d_slice) { 1084 loaded_image->DeviceHandle = 1085 pd->pd_handle; 1086 break; 1087 } 1088 } 1089 break; 1090 } 1091 } 1092 1093 dev_cleanup(); 1094 status = BS->StartImage(loaderhandle, NULL, NULL); 1095 if (status != EFI_SUCCESS) { 1096 command_errmsg = "StartImage failed"; 1097 free(loaded_image->LoadOptions); 1098 loaded_image->LoadOptions = NULL; 1099 status = BS->UnloadImage(loaded_image); 1100 return (CMD_ERROR); 1101 } 1102 1103 return (CMD_ERROR); /* not reached */ 1104 } 1105 1106 COMMAND_SET(chain, "chain", "chain load file", command_chain); 1107