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 30 #include <sys/disk.h> 31 #include <sys/param.h> 32 #include <sys/reboot.h> 33 #include <sys/boot.h> 34 #include <sys/consplat.h> 35 #include <sys/zfs_bootenv.h> 36 #include <stand.h> 37 #include <inttypes.h> 38 #include <string.h> 39 #include <setjmp.h> 40 #include <disk.h> 41 42 #include <efi.h> 43 #include <efilib.h> 44 #include <efigpt.h> 45 #include <efichar.h> 46 47 #include <uuid.h> 48 49 #include <bootstrap.h> 50 #include <gfx_fb.h> 51 #include <smbios.h> 52 53 #include <libzfs.h> 54 #include <efizfs.h> 55 56 #include "loader_efi.h" 57 58 struct arch_switch archsw; /* MI/MD interface boundary */ 59 60 EFI_GUID devid = DEVICE_PATH_PROTOCOL; 61 EFI_GUID imgid = LOADED_IMAGE_PROTOCOL; 62 EFI_GUID smbios = SMBIOS_TABLE_GUID; 63 EFI_GUID smbios3 = SMBIOS3_TABLE_GUID; 64 EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL; 65 EFI_GUID serialio = SERIAL_IO_PROTOCOL; 66 67 extern void acpi_detect(void); 68 extern void efi_getsmap(void); 69 70 static EFI_LOADED_IMAGE *img; 71 72 /* 73 * Number of seconds to wait for a keystroke before exiting with failure 74 * in the event no currdev is found. -2 means always break, -1 means 75 * never break, 0 means poll once and then reboot, > 0 means wait for 76 * that many seconds. "fail_timeout" can be set in the environment as 77 * well. 78 */ 79 static int fail_timeout = 5; 80 81 bool 82 efi_zfs_is_preferred(EFI_HANDLE *h) 83 { 84 EFI_DEVICE_PATH *devpath, *dp, *node; 85 HARDDRIVE_DEVICE_PATH *hd; 86 bool ret; 87 extern UINT64 start_sector; /* from mb_header.S */ 88 89 /* This check is true for chainloader case. */ 90 if (h == img->DeviceHandle) 91 return (true); 92 93 /* 94 * Make sure the image was loaded from the hard disk. 95 */ 96 devpath = efi_lookup_devpath(img->DeviceHandle); 97 if (devpath == NULL) 98 return (false); 99 node = efi_devpath_last_node(devpath); 100 if (node == NULL) 101 return (false); 102 if (DevicePathType(node) != MEDIA_DEVICE_PATH || 103 (DevicePathSubType(node) != MEDIA_FILEPATH_DP && 104 DevicePathSubType(node) != MEDIA_HARDDRIVE_DP)) { 105 return (false); 106 } 107 108 /* 109 * XXX We ignore the MEDIA_FILEPATH_DP here for now as it is 110 * used on arm and we do not support arm. 111 */ 112 ret = false; 113 dp = efi_devpath_trim(devpath); 114 devpath = NULL; 115 if (dp == NULL) 116 goto done; 117 118 devpath = efi_lookup_devpath(h); 119 if (devpath == NULL) 120 goto done; 121 hd = (HARDDRIVE_DEVICE_PATH *)efi_devpath_last_node(devpath); 122 if (hd == NULL) { 123 devpath = NULL; 124 goto done; 125 } 126 devpath = efi_devpath_trim(devpath); 127 if (devpath == NULL) 128 goto done; 129 130 if (!efi_devpath_match(dp, devpath)) 131 goto done; 132 133 /* It is the same disk, do we have partition start? */ 134 if (start_sector == 0) 135 ret = true; 136 else if (start_sector == hd->PartitionStart) 137 ret = true; 138 139 done: 140 free(dp); 141 free(devpath); 142 return (ret); 143 } 144 145 static bool 146 has_keyboard(void) 147 { 148 EFI_STATUS status; 149 EFI_DEVICE_PATH *path; 150 EFI_HANDLE *hin; 151 uint_t i, nhandles; 152 bool retval = false; 153 154 /* 155 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and 156 * do the typical dance to get the right sized buffer. 157 */ 158 status = efi_get_protocol_handles(&inputid, &nhandles, &hin); 159 if (EFI_ERROR(status)) 160 return (retval); 161 162 /* 163 * Look at each of the handles. If it supports the device path protocol, 164 * use it to get the device path for this handle. Then see if that 165 * device path matches either the USB device path for keyboards or the 166 * legacy device path for keyboards. 167 */ 168 for (i = 0; i < nhandles; i++) { 169 status = OpenProtocolByHandle(hin[i], &devid, (void **)&path); 170 if (EFI_ERROR(status)) 171 continue; 172 173 while (!IsDevicePathEnd(path)) { 174 /* 175 * Check for the ACPI keyboard node. All PNP3xx nodes 176 * are keyboards of different flavors. Note: It is 177 * unclear of there's always a keyboard node when 178 * there's a keyboard controller, or if there's only one 179 * when a keyboard is detected at boot. 180 */ 181 if (DevicePathType(path) == ACPI_DEVICE_PATH && 182 (DevicePathSubType(path) == ACPI_DP || 183 DevicePathSubType(path) == ACPI_EXTENDED_DP)) { 184 ACPI_HID_DEVICE_PATH *acpi; 185 186 acpi = (ACPI_HID_DEVICE_PATH *)(void *)path; 187 if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 188 0x300 && 189 (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) { 190 retval = true; 191 goto out; 192 } 193 /* 194 * Check for USB keyboard node, if present. Unlike a 195 * PS/2 keyboard, these definitely only appear when 196 * connected to the system. 197 */ 198 } else if (DevicePathType(path) == 199 MESSAGING_DEVICE_PATH && 200 DevicePathSubType(path) == MSG_USB_CLASS_DP) { 201 USB_CLASS_DEVICE_PATH *usb; 202 203 /* 204 * Check for: 205 * DeviceClass: HID 206 * DeviceSubClass: Boot devices 207 * DeviceProtocol: Boot keyboards 208 */ 209 usb = (USB_CLASS_DEVICE_PATH *)(void *)path; 210 if (usb->DeviceClass == 3 && 211 usb->DeviceSubClass == 1 && 212 usb->DeviceProtocol == 1) { 213 retval = true; 214 goto out; 215 } 216 } 217 path = NextDevicePathNode(path); 218 } 219 } 220 out: 221 free(hin); 222 return (retval); 223 } 224 225 static void 226 set_currdev(const char *devname) 227 { 228 229 /* 230 * Don't execute hooks here; we may need to try setting these more than 231 * once here if we're probing for the ZFS pool we're supposed to boot. 232 * The currdev hook is intended to just validate user input anyways, 233 * while the loaddev hook makes it immutable once we've determined what 234 * the proper currdev is. 235 */ 236 env_setenv("currdev", EV_VOLATILE | EV_NOHOOK, devname, efi_setcurrdev, 237 env_nounset); 238 env_setenv("loaddev", EV_VOLATILE | EV_NOHOOK, devname, env_noset, 239 env_nounset); 240 } 241 242 static void 243 set_currdev_devdesc(struct devdesc *currdev) 244 { 245 char *devname; 246 247 devname = efi_fmtdev(currdev); 248 249 printf("Setting currdev to %s\n", devname); 250 set_currdev(devname); 251 } 252 253 static void 254 set_currdev_devsw(struct devsw *dev, int unit) 255 { 256 struct devdesc currdev; 257 258 currdev.d_dev = dev; 259 currdev.d_unit = unit; 260 261 set_currdev_devdesc(&currdev); 262 } 263 264 static void 265 set_currdev_pdinfo(pdinfo_t *dp) 266 { 267 268 /* 269 * Disks are special: they have partitions. if the parent 270 * pointer is non-null, we're a partition not a full disk 271 * and we need to adjust currdev appropriately. 272 */ 273 if (dp->pd_devsw->dv_type == DEVT_DISK) { 274 struct disk_devdesc currdev; 275 276 currdev.dd.d_dev = dp->pd_devsw; 277 if (dp->pd_parent == NULL) { 278 currdev.dd.d_unit = dp->pd_unit; 279 currdev.d_slice = D_SLICENONE; 280 currdev.d_partition = D_PARTNONE; 281 } else { 282 currdev.dd.d_unit = dp->pd_parent->pd_unit; 283 currdev.d_slice = dp->pd_unit; 284 currdev.d_partition = D_PARTISGPT; /* Assumes GPT */ 285 } 286 set_currdev_devdesc((struct devdesc *)&currdev); 287 } else { 288 set_currdev_devsw(dp->pd_devsw, dp->pd_unit); 289 } 290 } 291 292 static bool 293 sanity_check_currdev(void) 294 { 295 struct stat st; 296 297 return (stat("/boot/defaults/loader.conf", &st) == 0); 298 } 299 300 static bool 301 probe_zfs_currdev(uint64_t guid) 302 { 303 struct zfs_devdesc currdev; 304 char *bootonce; 305 bool rv; 306 307 currdev.dd.d_dev = &zfs_dev; 308 currdev.dd.d_unit = 0; 309 currdev.pool_guid = guid; 310 currdev.root_guid = 0; 311 set_currdev_devdesc((struct devdesc *)&currdev); 312 313 rv = sanity_check_currdev(); 314 if (rv) { 315 bootonce = malloc(VDEV_PAD_SIZE); 316 if (bootonce != NULL) { 317 if (zfs_get_bootonce(&currdev, OS_BOOTONCE, bootonce, 318 VDEV_PAD_SIZE) == 0) { 319 printf("zfs bootonce: %s\n", bootonce); 320 set_currdev(bootonce); 321 setenv("zfs-bootonce", bootonce, 1); 322 } 323 free(bootonce); 324 (void) zfs_attach_nvstore(&currdev); 325 } else { 326 printf("Failed to process bootonce data: %s\n", 327 strerror(errno)); 328 } 329 } 330 return (rv); 331 } 332 333 static bool 334 try_as_currdev(pdinfo_t *pp) 335 { 336 uint64_t guid; 337 338 /* 339 * If there's a zpool on this device, try it as a ZFS 340 * filesystem, which has somewhat different setup than all 341 * other types of fs due to imperfect loader integration. 342 * This all stems from ZFS being both a device (zpool) and 343 * a filesystem, plus the boot env feature. 344 */ 345 if (efizfs_get_guid_by_handle(pp->pd_handle, &guid)) 346 return (probe_zfs_currdev(guid)); 347 348 /* 349 * All other filesystems just need the pdinfo 350 * initialized in the standard way. 351 */ 352 set_currdev_pdinfo(pp); 353 return (sanity_check_currdev()); 354 } 355 356 static bool 357 find_currdev(EFI_LOADED_IMAGE *img) 358 { 359 pdinfo_t *dp, *pp; 360 EFI_DEVICE_PATH *devpath, *copy; 361 EFI_HANDLE h; 362 CHAR16 *text; 363 struct devsw *dev; 364 int unit; 365 uint64_t extra; 366 367 /* 368 * Did efi_zfs_probe() detect the boot pool? If so, use the zpool 369 * it found, if it's sane. ZFS is the only thing that looks for 370 * disks and pools to boot. 371 */ 372 if (pool_guid != 0) { 373 printf("Trying ZFS pool\n"); 374 if (probe_zfs_currdev(pool_guid)) 375 return (true); 376 } 377 378 /* 379 * Try to find the block device by its handle based on the 380 * image we're booting. If we can't find a sane partition, 381 * search all the other partitions of the disk. We do not 382 * search other disks because it's a violation of the UEFI 383 * boot protocol to do so. We fail and let UEFI go on to 384 * the next candidate. 385 */ 386 dp = efiblk_get_pdinfo_by_handle(img->DeviceHandle); 387 if (dp != NULL) { 388 text = efi_devpath_name(dp->pd_devpath); 389 if (text != NULL) { 390 printf("Trying ESP: %S\n", text); 391 efi_free_devpath_name(text); 392 } 393 set_currdev_pdinfo(dp); 394 if (sanity_check_currdev()) 395 return (true); 396 if (dp->pd_parent != NULL) { 397 dp = dp->pd_parent; 398 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) { 399 text = efi_devpath_name(pp->pd_devpath); 400 if (text != NULL) { 401 printf("And now the part: %S\n", text); 402 efi_free_devpath_name(text); 403 } 404 /* 405 * Roll up the ZFS special case 406 * for those partitions that have 407 * zpools on them 408 */ 409 if (try_as_currdev(pp)) 410 return (true); 411 } 412 } 413 } else { 414 printf("Can't find device by handle\n"); 415 } 416 417 /* 418 * Try the device handle from our loaded image first. If that 419 * fails, use the device path from the loaded image and see if 420 * any of the nodes in that path match one of the enumerated 421 * handles. Currently, this handle list is only for netboot. 422 */ 423 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) { 424 set_currdev_devsw(dev, unit); 425 if (sanity_check_currdev()) 426 return (true); 427 } 428 429 copy = NULL; 430 devpath = efi_lookup_image_devpath(IH); 431 while (devpath != NULL) { 432 h = efi_devpath_handle(devpath); 433 if (h == NULL) 434 break; 435 436 free(copy); 437 copy = NULL; 438 439 if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) { 440 set_currdev_devsw(dev, unit); 441 if (sanity_check_currdev()) 442 return (true); 443 } 444 445 devpath = efi_lookup_devpath(h); 446 if (devpath != NULL) { 447 copy = efi_devpath_trim(devpath); 448 devpath = copy; 449 } 450 } 451 free(copy); 452 453 return (false); 454 } 455 456 static bool 457 interactive_interrupt(const char *msg) 458 { 459 time_t now, then, last; 460 461 last = 0; 462 now = then = getsecs(); 463 printf("%s\n", msg); 464 if (fail_timeout == -2) /* Always break to OK */ 465 return (true); 466 if (fail_timeout == -1) /* Never break to OK */ 467 return (false); 468 do { 469 if (last != now) { 470 printf("press any key to interrupt reboot " 471 "in %d seconds\r", 472 fail_timeout - (int)(now - then)); 473 last = now; 474 } 475 476 /* XXX no pause or timeout wait for char */ 477 if (ischar()) 478 return (true); 479 now = getsecs(); 480 } while (now - then < fail_timeout); 481 return (false); 482 } 483 484 static void 485 setenv_int(const char *key, int val) 486 { 487 char buf[20]; 488 489 (void) snprintf(buf, sizeof (buf), "%d", val); 490 (void) setenv(key, buf, 1); 491 } 492 493 /* 494 * Parse ConOut (the list of consoles active) and see if we can find a 495 * serial port and/or a video port. It would be nice to also walk the 496 * ACPI name space to map the UID for the serial port to a port. The 497 * latter is especially hard. 498 */ 499 static int 500 parse_uefi_con_out(void) 501 { 502 int how, rv; 503 int vid_seen = 0, com_seen = 0, seen = 0; 504 size_t sz; 505 char buf[4096], *ep; 506 EFI_DEVICE_PATH *node; 507 ACPI_HID_DEVICE_PATH *acpi; 508 UART_DEVICE_PATH *uart; 509 bool pci_pending = false; 510 511 how = 0; 512 sz = sizeof (buf); 513 rv = efi_global_getenv("ConOut", buf, &sz); 514 if (rv != EFI_SUCCESS) 515 rv = efi_global_getenv("ConOutDev", buf, &sz); 516 if (rv != EFI_SUCCESS) { 517 /* 518 * If we don't have any ConOut default to video. 519 * non-server systems may not have serial. 520 */ 521 goto out; 522 } 523 ep = buf + sz; 524 node = (EFI_DEVICE_PATH *)buf; 525 while ((char *)node < ep) { 526 if (IsDevicePathEndType(node)) { 527 if (pci_pending && vid_seen == 0) 528 vid_seen = ++seen; 529 } 530 pci_pending = false; 531 if (DevicePathType(node) == ACPI_DEVICE_PATH && 532 (DevicePathSubType(node) == ACPI_DP || 533 DevicePathSubType(node) == ACPI_EXTENDED_DP)) { 534 /* Check for Serial node */ 535 acpi = (void *)node; 536 if (EISA_ID_TO_NUM(acpi->HID) == 0x501) { 537 setenv_int("efi_8250_uid", acpi->UID); 538 com_seen = ++seen; 539 } 540 } else if (DevicePathType(node) == MESSAGING_DEVICE_PATH && 541 DevicePathSubType(node) == MSG_UART_DP) { 542 com_seen = ++seen; 543 uart = (void *)node; 544 setenv_int("efi_com_speed", uart->BaudRate); 545 } else if (DevicePathType(node) == ACPI_DEVICE_PATH && 546 DevicePathSubType(node) == ACPI_ADR_DP) { 547 /* Check for AcpiAdr() Node for video */ 548 vid_seen = ++seen; 549 } else if (DevicePathType(node) == HARDWARE_DEVICE_PATH && 550 DevicePathSubType(node) == HW_PCI_DP) { 551 /* 552 * Note, vmware fusion has a funky console device 553 * PciRoot(0x0)/Pci(0xf,0x0) 554 * which we can only detect at the end since we also 555 * have to cope with: 556 * PciRoot(0x0)/Pci(0x1f,0x0)/Serial(0x1) 557 * so only match it if it's last. 558 */ 559 pci_pending = true; 560 } 561 node = NextDevicePathNode(node); /* Skip the end node */ 562 } 563 564 /* 565 * Truth table for RB_MULTIPLE | RB_SERIAL 566 * Value Result 567 * 0 Use only video console 568 * RB_SERIAL Use only serial console 569 * RB_MULTIPLE Use both video and serial console 570 * (but video is primary so gets rc messages) 571 * both Use both video and serial console 572 * (but serial is primary so gets rc messages) 573 * 574 * Try to honor this as best we can. If only one of serial / video 575 * found, then use that. Otherwise, use the first one we found. 576 * This also implies if we found nothing, default to video. 577 */ 578 how = 0; 579 if (vid_seen && com_seen) { 580 how |= RB_MULTIPLE; 581 if (com_seen < vid_seen) 582 how |= RB_SERIAL; 583 } else if (com_seen) 584 how |= RB_SERIAL; 585 out: 586 return (how); 587 } 588 589 caddr_t 590 ptov(uintptr_t x) 591 { 592 return ((caddr_t)x); 593 } 594 595 static int 596 efi_serial_get_uid(EFI_DEVICE_PATH *devpath) 597 { 598 ACPI_HID_DEVICE_PATH *acpi; 599 600 while (!IsDevicePathEnd(devpath)) { 601 if (DevicePathType(devpath) == ACPI_DEVICE_PATH && 602 (DevicePathSubType(devpath) == ACPI_DP || 603 DevicePathSubType(devpath) == ACPI_EXTENDED_DP)) { 604 acpi = (ACPI_HID_DEVICE_PATH *)devpath; 605 if (EISA_ID_TO_NUM(acpi->HID) == 0x501) { 606 return (acpi->UID); 607 } 608 } 609 610 devpath = NextDevicePathNode(devpath); 611 } 612 return (-1); 613 } 614 615 /* 616 * Walk serialio protocol handle array and find index for serial console 617 * device. The problem is, we check for acpi UID value, but we can not be sure, 618 * if it will start from 0 or 1. 619 */ 620 static const char * 621 uefi_serial_console(void) 622 { 623 EFI_STATUS status; 624 EFI_HANDLE *handles; 625 uint_t i, nhandles; 626 unsigned long uid, lowest; 627 char *env, *ep; 628 extern struct console ttya; 629 extern struct console ttyb; 630 extern struct console ttyc; 631 extern struct console ttyd; 632 633 env = getenv("efi_8250_uid"); 634 if (env == NULL) 635 return (NULL); 636 (void) unsetenv("efi_8250_uid"); 637 errno = 0; 638 uid = strtoul(env, &ep, 10); 639 if (errno != 0 || *ep != '\0') 640 return (NULL); 641 642 /* if uid is 0, this is first serial port */ 643 if (uid == 0) 644 return (ttya.c_name); 645 646 status = efi_get_protocol_handles(&serialio, &nhandles, &handles); 647 if (EFI_ERROR(status)) { 648 return (NULL); 649 } 650 651 lowest = 255; /* high enough value */ 652 for (i = 0; i < nhandles; i++) { 653 EFI_DEVICE_PATH *devpath; 654 unsigned long _uid; 655 656 devpath = efi_lookup_devpath(handles[i]); 657 _uid = efi_serial_get_uid(devpath); 658 if (_uid < lowest) 659 lowest = _uid; 660 } 661 free(handles); 662 switch (uid - lowest) { 663 case 0: 664 return (ttya.c_name); 665 case 1: 666 return (ttyb.c_name); 667 case 2: 668 return (ttyc.c_name); 669 case 3: 670 return (ttyd.c_name); 671 } 672 return (NULL); 673 } 674 675 EFI_STATUS 676 main(int argc, CHAR16 *argv[]) 677 { 678 char var[128]; 679 int i, j, howto; 680 bool vargood; 681 void *ptr; 682 bool has_kbd; 683 char *s; 684 const char *serial; 685 EFI_DEVICE_PATH *imgpath; 686 CHAR16 *text; 687 EFI_STATUS status; 688 UINT16 boot_current; 689 size_t sz; 690 UINT16 boot_order[100]; 691 692 archsw.arch_autoload = efi_autoload; 693 archsw.arch_getdev = efi_getdev; 694 archsw.arch_copyin = efi_copyin; 695 archsw.arch_copyout = efi_copyout; 696 archsw.arch_readin = efi_readin; 697 archsw.arch_loadaddr = efi_loadaddr; 698 archsw.arch_free_loadaddr = efi_free_loadaddr; 699 #if defined(__amd64) || defined(__i386) 700 archsw.arch_hypervisor = x86_hypervisor; 701 #endif 702 /* Note this needs to be set before ZFS init. */ 703 archsw.arch_zfs_probe = efi_zfs_probe; 704 705 /* Get our loaded image protocol interface structure. */ 706 (void) OpenProtocolByHandle(IH, &imgid, (void **)&img); 707 708 /* 709 * XXX Chicken-and-egg problem; we want to have console output 710 * early, but some console attributes may depend on reading from 711 * eg. the boot device, which we can't do yet. We can use 712 * printf() etc. once this is done. 713 */ 714 setenv("console", "text", 1); 715 howto = parse_uefi_con_out(); 716 serial = uefi_serial_console(); 717 cons_probe(); 718 efi_getsmap(); 719 720 if ((s = getenv("efi_com_speed")) != NULL) { 721 char *name; 722 723 (void) snprintf(var, sizeof (var), "%s,8,n,1,-", s); 724 if (asprintf(&name, "%s-mode", serial) > 0) { 725 (void) setenv(name, var, 1); 726 free(name); 727 } 728 if (asprintf(&name, "%s-spcr-mode", serial) > 0) { 729 (void) setenv(name, var, 1); 730 free(name); 731 } 732 (void) unsetenv("efi_com_speed"); 733 } 734 735 /* Init the time source */ 736 efi_time_init(); 737 738 /* 739 * Initialise the block cache. Set the upper limit. 740 */ 741 bcache_init(32768, 512); 742 743 has_kbd = has_keyboard(); 744 745 /* 746 * Parse the args to set the console settings, etc 747 * iPXE may be setup to pass these in. Or the optional argument in the 748 * boot environment was used to pass these arguments in (in which case 749 * neither /boot.config nor /boot/config are consulted). 750 * 751 * Loop through the args, and for each one that contains an '=' that is 752 * not the first character, add it to the environment. This allows 753 * loader and kernel env vars to be passed on the command line. Convert 754 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied (though 755 * this method is flawed for non-ASCII characters). 756 */ 757 for (i = 1; i < argc; i++) { 758 if (argv[i][0] == '-') { 759 for (j = 1; argv[i][j] != 0; j++) { 760 int ch; 761 762 ch = argv[i][j]; 763 switch (ch) { 764 case 'a': 765 howto |= RB_ASKNAME; 766 break; 767 case 'd': 768 howto |= RB_KDB; 769 break; 770 case 'D': 771 howto |= RB_MULTIPLE; 772 break; 773 case 'h': 774 howto |= RB_SERIAL; 775 break; 776 case 'm': 777 howto |= RB_MUTE; 778 break; 779 case 'p': 780 howto |= RB_PAUSE; 781 break; 782 case 'P': 783 if (!has_kbd) { 784 howto |= RB_SERIAL; 785 howto |= RB_MULTIPLE; 786 } 787 break; 788 case 'r': 789 howto |= RB_DFLTROOT; 790 break; 791 case 's': 792 howto |= RB_SINGLE; 793 break; 794 case 'S': 795 if (argv[i][j + 1] == 0) { 796 if (i + 1 == argc) { 797 strncpy(var, "115200", 798 sizeof (var)); 799 } else { 800 CHAR16 *ptr; 801 ptr = &argv[i + 1][0]; 802 cpy16to8(ptr, var, 803 sizeof (var)); 804 } 805 i++; 806 } else { 807 cpy16to8(&argv[i][j + 1], var, 808 sizeof (var)); 809 } 810 strncat(var, ",8,n,1,-", sizeof (var)); 811 setenv("ttya-mode", var, 1); 812 break; 813 case 'v': 814 howto |= RB_VERBOSE; 815 break; 816 } 817 } 818 } else { 819 vargood = false; 820 for (j = 0; argv[i][j] != 0; j++) { 821 if (j == sizeof (var)) { 822 vargood = false; 823 break; 824 } 825 if (j > 0 && argv[i][j] == '=') 826 vargood = true; 827 var[j] = (char)argv[i][j]; 828 } 829 if (vargood) { 830 var[j] = 0; 831 putenv(var); 832 } 833 } 834 } 835 for (i = 0; howto_names[i].ev != NULL; i++) 836 if (howto & howto_names[i].mask) 837 setenv(howto_names[i].ev, "YES", 1); 838 839 /* 840 * XXX we need fallback to this stuff after looking at the ConIn, 841 * ConOut and ConErr variables. 842 */ 843 if (howto & RB_MULTIPLE) { 844 if (howto & RB_SERIAL) 845 (void) snprintf(var, sizeof (var), "%s text", serial); 846 else 847 (void) snprintf(var, sizeof (var), "text %s", serial); 848 } else if (howto & RB_SERIAL) { 849 (void) snprintf(var, sizeof (var), "%s", serial); 850 } else { 851 (void) snprintf(var, sizeof (var), "text"); 852 } 853 (void) setenv("console", var, 1); 854 855 if ((s = getenv("fail_timeout")) != NULL) 856 fail_timeout = strtol(s, NULL, 10); 857 858 /* 859 * Scan the BLOCK IO MEDIA handles then 860 * march through the device switch probing for things. 861 */ 862 if ((i = efipart_inithandles()) == 0) { 863 for (i = 0; devsw[i] != NULL; i++) 864 if (devsw[i]->dv_init != NULL) 865 (devsw[i]->dv_init)(); 866 } else 867 printf("efipart_inithandles failed %d, expect failures", i); 868 869 printf("Command line arguments:"); 870 for (i = 0; i < argc; i++) { 871 printf(" %S", argv[i]); 872 } 873 printf("\n"); 874 875 printf("Image base: 0x%lx\n", (unsigned long)img->ImageBase); 876 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16, 877 ST->Hdr.Revision & 0xffff); 878 printf("EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor, 879 ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); 880 881 printf("\n%s", bootprog_info); 882 883 /* Determine the devpath of our image so we can prefer it. */ 884 text = efi_devpath_name(img->FilePath); 885 if (text != NULL) { 886 printf(" Load Path: %S\n", text); 887 efi_setenv_illumos_wcs("LoaderPath", text); 888 efi_free_devpath_name(text); 889 } 890 891 status = OpenProtocolByHandle(img->DeviceHandle, &devid, 892 (void **)&imgpath); 893 if (status == EFI_SUCCESS) { 894 text = efi_devpath_name(imgpath); 895 if (text != NULL) { 896 printf(" Load Device: %S\n", text); 897 efi_setenv_illumos_wcs("LoaderDev", text); 898 efi_free_devpath_name(text); 899 } 900 } 901 902 boot_current = 0; 903 sz = sizeof (boot_current); 904 efi_global_getenv("BootCurrent", &boot_current, &sz); 905 printf(" BootCurrent: %04x\n", boot_current); 906 907 sz = sizeof (boot_order); 908 efi_global_getenv("BootOrder", &boot_order, &sz); 909 printf(" BootOrder:"); 910 for (i = 0; i < sz / sizeof (boot_order[0]); i++) 911 printf(" %04x%s", boot_order[i], 912 boot_order[i] == boot_current ? "[*]" : ""); 913 printf("\n"); 914 915 /* 916 * Disable the watchdog timer. By default the boot manager sets 917 * the timer to 5 minutes before invoking a boot option. If we 918 * want to return to the boot manager, we have to disable the 919 * watchdog timer and since we're an interactive program, we don't 920 * want to wait until the user types "quit". The timer may have 921 * fired by then. We don't care if this fails. It does not prevent 922 * normal functioning in any way... 923 */ 924 BS->SetWatchdogTimer(0, 0, 0, NULL); 925 926 /* 927 * Try and find a good currdev based on the image that was booted. 928 * It might be desirable here to have a short pause to allow falling 929 * through to the boot loader instead of returning instantly to follow 930 * the boot protocol and also allow an escape hatch for users wishing 931 * to try something different. 932 */ 933 if (!find_currdev(img)) 934 if (!interactive_interrupt("Failed to find bootable partition")) 935 return (EFI_NOT_FOUND); 936 937 autoload_font(false); /* Set up the font list for console. */ 938 efi_init_environment(); 939 bi_isadir(); /* set ISADIR */ 940 acpi_detect(); 941 942 if ((ptr = efi_get_table(&smbios3)) == NULL) 943 ptr = efi_get_table(&smbios); 944 smbios_detect(ptr); 945 946 interact(NULL); /* doesn't return */ 947 948 return (EFI_SUCCESS); /* keep compiler happy */ 949 } 950 951 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 952 953 static void 954 fw_setup(void) 955 { 956 uint64_t os_indications; 957 size_t size; 958 EFI_STATUS status; 959 960 size = sizeof (os_indications); 961 status = efi_global_getenv("OsIndicationsSupported", 962 &os_indications, &size); 963 if (EFI_ERROR(status) || size != sizeof (os_indications) || 964 (os_indications & EFI_OS_INDICATIONS_BOOT_TO_FW_UI) == 0) { 965 printf("Booting to Firmware UI is not supported in " 966 "this system."); 967 for (int i = 0; i < 3; i++) { 968 delay(1000 * 1000); /* 1 second */ 969 if (ischar()) 970 break; 971 } 972 return; 973 } 974 975 os_indications = EFI_OS_INDICATIONS_BOOT_TO_FW_UI; 976 977 status = efi_global_setenv("OsIndications", &os_indications, 978 sizeof (os_indications)); 979 } 980 981 static int 982 command_reboot(int argc, char *argv[]) 983 { 984 int i, ch; 985 bool fw = false; 986 987 optind = 1; 988 optreset = 1; 989 990 while ((ch = getopt(argc, argv, "fh")) != -1) { 991 switch (ch) { 992 case 'f': 993 fw = true; 994 break; 995 case 'h': 996 printf("Usage: reboot [-f]\n"); 997 return (CMD_OK); 998 case '?': 999 default: 1000 return (CMD_OK); 1001 } 1002 } 1003 1004 if (fw || getenv("BOOT_TO_FW_UI") != NULL) 1005 fw_setup(); 1006 1007 for (i = 0; devsw[i] != NULL; ++i) 1008 if (devsw[i]->dv_cleanup != NULL) 1009 (devsw[i]->dv_cleanup)(); 1010 1011 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL); 1012 1013 /* NOTREACHED */ 1014 return (CMD_ERROR); 1015 } 1016 1017 COMMAND_SET(poweroff, "poweroff", "power off the system", command_poweroff); 1018 1019 static int 1020 command_poweroff(int argc __unused, char *argv[] __unused) 1021 { 1022 int i; 1023 1024 for (i = 0; devsw[i] != NULL; ++i) 1025 if (devsw[i]->dv_cleanup != NULL) 1026 (devsw[i]->dv_cleanup)(); 1027 1028 RS->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL); 1029 1030 /* NOTREACHED */ 1031 return (CMD_ERROR); 1032 } 1033 1034 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap); 1035 1036 static int 1037 command_memmap(int argc __unused, char *argv[] __unused) 1038 { 1039 UINTN sz; 1040 EFI_MEMORY_DESCRIPTOR *map, *p; 1041 UINTN key, dsz; 1042 UINT32 dver; 1043 EFI_STATUS status; 1044 int i, ndesc; 1045 int rv = 0; 1046 char line[80]; 1047 1048 sz = 0; 1049 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver); 1050 if (status != EFI_BUFFER_TOO_SMALL) { 1051 printf("Can't determine memory map size\n"); 1052 return (CMD_ERROR); 1053 } 1054 map = malloc(sz); 1055 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver); 1056 if (EFI_ERROR(status)) { 1057 printf("Can't read memory map\n"); 1058 return (CMD_ERROR); 1059 } 1060 1061 ndesc = sz / dsz; 1062 snprintf(line, 80, "%23s %12s %12s %8s %4s\n", 1063 "Type", "Physical", "Virtual", "#Pages", "Attr"); 1064 pager_open(); 1065 rv = pager_output(line); 1066 if (rv) { 1067 pager_close(); 1068 return (CMD_OK); 1069 } 1070 1071 for (i = 0, p = map; i < ndesc; 1072 i++, p = NextMemoryDescriptor(p, dsz)) { 1073 snprintf(line, 80, "%23s %012jx %012jx %08jx ", 1074 efi_memory_type(p->Type), p->PhysicalStart, 1075 p->VirtualStart, p->NumberOfPages); 1076 rv = pager_output(line); 1077 if (rv) 1078 break; 1079 1080 if (p->Attribute & EFI_MEMORY_UC) 1081 printf("UC "); 1082 if (p->Attribute & EFI_MEMORY_WC) 1083 printf("WC "); 1084 if (p->Attribute & EFI_MEMORY_WT) 1085 printf("WT "); 1086 if (p->Attribute & EFI_MEMORY_WB) 1087 printf("WB "); 1088 if (p->Attribute & EFI_MEMORY_UCE) 1089 printf("UCE "); 1090 if (p->Attribute & EFI_MEMORY_WP) 1091 printf("WP "); 1092 if (p->Attribute & EFI_MEMORY_RP) 1093 printf("RP "); 1094 if (p->Attribute & EFI_MEMORY_XP) 1095 printf("XP "); 1096 if (p->Attribute & EFI_MEMORY_NV) 1097 printf("NV "); 1098 if (p->Attribute & EFI_MEMORY_MORE_RELIABLE) 1099 printf("MR "); 1100 if (p->Attribute & EFI_MEMORY_RO) 1101 printf("RO "); 1102 rv = pager_output("\n"); 1103 if (rv) 1104 break; 1105 } 1106 1107 pager_close(); 1108 return (CMD_OK); 1109 } 1110 1111 COMMAND_SET(configuration, "configuration", "print configuration tables", 1112 command_configuration); 1113 1114 static int 1115 command_configuration(int argc __unused, char *argv[] __unused) 1116 { 1117 UINTN i; 1118 char *name; 1119 1120 printf("NumberOfTableEntries=%lu\n", 1121 (unsigned long)ST->NumberOfTableEntries); 1122 for (i = 0; i < ST->NumberOfTableEntries; i++) { 1123 EFI_GUID *guid; 1124 1125 printf(" "); 1126 guid = &ST->ConfigurationTable[i].VendorGuid; 1127 1128 if (efi_guid_to_name(guid, &name) == true) { 1129 printf(name); 1130 free(name); 1131 } else { 1132 printf("Error while translating UUID to name"); 1133 } 1134 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable); 1135 } 1136 1137 return (CMD_OK); 1138 } 1139 1140 1141 COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode); 1142 1143 static int 1144 command_mode(int argc, char *argv[]) 1145 { 1146 UINTN cols, rows; 1147 unsigned int mode; 1148 int i; 1149 char *cp; 1150 EFI_STATUS status; 1151 SIMPLE_TEXT_OUTPUT_INTERFACE *conout; 1152 EFI_CONSOLE_CONTROL_SCREEN_MODE sm; 1153 1154 if (plat_stdout_is_framebuffer()) 1155 sm = EfiConsoleControlScreenGraphics; 1156 else 1157 sm = EfiConsoleControlScreenText; 1158 1159 conout = ST->ConOut; 1160 1161 if (argc > 1) { 1162 mode = strtol(argv[1], &cp, 0); 1163 if (cp[0] != '\0') { 1164 printf("Invalid mode\n"); 1165 return (CMD_ERROR); 1166 } 1167 status = conout->QueryMode(conout, mode, &cols, &rows); 1168 if (EFI_ERROR(status)) { 1169 printf("invalid mode %d\n", mode); 1170 return (CMD_ERROR); 1171 } 1172 status = conout->SetMode(conout, mode); 1173 if (EFI_ERROR(status)) { 1174 printf("couldn't set mode %d\n", mode); 1175 return (CMD_ERROR); 1176 } 1177 plat_cons_update_mode(sm); 1178 return (CMD_OK); 1179 } 1180 1181 printf("Current mode: %d\n", conout->Mode->Mode); 1182 for (i = 0; i <= conout->Mode->MaxMode; i++) { 1183 status = conout->QueryMode(conout, i, &cols, &rows); 1184 if (EFI_ERROR(status)) 1185 continue; 1186 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols, 1187 (unsigned)rows); 1188 } 1189 1190 if (i != 0) 1191 printf("Select a mode with the command \"mode <number>\"\n"); 1192 1193 return (CMD_OK); 1194 } 1195 1196 COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi); 1197 1198 static int 1199 command_lsefi(int argc __unused, char *argv[] __unused) 1200 { 1201 char *name; 1202 EFI_HANDLE *buffer = NULL; 1203 EFI_HANDLE handle; 1204 UINTN bufsz = 0, i, j; 1205 EFI_STATUS status; 1206 int ret = 0; 1207 1208 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer); 1209 if (status != EFI_BUFFER_TOO_SMALL) { 1210 snprintf(command_errbuf, sizeof (command_errbuf), 1211 "unexpected error: %lld", (long long)status); 1212 return (CMD_ERROR); 1213 } 1214 if ((buffer = malloc(bufsz)) == NULL) { 1215 sprintf(command_errbuf, "out of memory"); 1216 return (CMD_ERROR); 1217 } 1218 1219 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer); 1220 if (EFI_ERROR(status)) { 1221 free(buffer); 1222 snprintf(command_errbuf, sizeof (command_errbuf), 1223 "LocateHandle() error: %lld", (long long)status); 1224 return (CMD_ERROR); 1225 } 1226 1227 pager_open(); 1228 for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) { 1229 UINTN nproto = 0; 1230 EFI_GUID **protocols = NULL; 1231 EFI_DEVICE_PATH *dp; 1232 CHAR16 *text; 1233 1234 handle = buffer[i]; 1235 printf("Handle %p", handle); 1236 if (pager_output("\n")) 1237 break; 1238 1239 ret = 0; 1240 dp = efi_lookup_devpath(handle); 1241 if (dp != NULL) { 1242 text = efi_devpath_name(dp); 1243 if (text != NULL) { 1244 printf(" %S", text); 1245 efi_free_devpath_name(text); 1246 ret = pager_output("\n"); 1247 } 1248 efi_close_devpath(handle); 1249 } 1250 if (ret != 0) 1251 break; 1252 1253 status = BS->ProtocolsPerHandle(handle, &protocols, &nproto); 1254 if (EFI_ERROR(status)) { 1255 snprintf(command_errbuf, sizeof (command_errbuf), 1256 "ProtocolsPerHandle() error: %lld", 1257 (long long)status); 1258 continue; 1259 } 1260 1261 for (j = 0; j < nproto; j++) { 1262 if (efi_guid_to_name(protocols[j], &name) == true) { 1263 printf(" %s", name); 1264 free(name); 1265 } else { 1266 printf("Error while translating UUID to name"); 1267 } 1268 if ((ret = pager_output("\n")) != 0) 1269 break; 1270 } 1271 BS->FreePool(protocols); 1272 if (ret != 0) 1273 break; 1274 } 1275 pager_close(); 1276 free(buffer); 1277 return (CMD_OK); 1278 } 1279 1280 #ifdef LOADER_FDT_SUPPORT 1281 extern int command_fdt_internal(int argc, char *argv[]); 1282 1283 /* 1284 * Since proper fdt command handling function is defined in fdt_loader_cmd.c, 1285 * and declaring it as extern is in contradiction with COMMAND_SET() macro 1286 * (which uses static pointer), we're defining wrapper function, which 1287 * calls the proper fdt handling routine. 1288 */ 1289 static int 1290 command_fdt(int argc, char *argv[]) 1291 { 1292 return (command_fdt_internal(argc, argv)); 1293 } 1294 1295 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt); 1296 #endif 1297 1298 /* 1299 * Chain load another efi loader. 1300 */ 1301 static int 1302 command_chain(int argc, char *argv[]) 1303 { 1304 EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL; 1305 EFI_HANDLE loaderhandle; 1306 EFI_LOADED_IMAGE *loaded_image; 1307 EFI_STATUS status; 1308 struct stat st; 1309 struct devdesc *dev; 1310 char *name, *path; 1311 void *buf; 1312 int fd; 1313 1314 if (argc < 2) { 1315 command_errmsg = "wrong number of arguments"; 1316 return (CMD_ERROR); 1317 } 1318 1319 name = argv[1]; 1320 1321 if ((fd = open(name, O_RDONLY)) < 0) { 1322 command_errmsg = "no such file"; 1323 return (CMD_ERROR); 1324 } 1325 1326 if (fstat(fd, &st) < -1) { 1327 command_errmsg = "stat failed"; 1328 close(fd); 1329 return (CMD_ERROR); 1330 } 1331 1332 status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf); 1333 if (status != EFI_SUCCESS) { 1334 command_errmsg = "failed to allocate buffer"; 1335 close(fd); 1336 return (CMD_ERROR); 1337 } 1338 if (read(fd, buf, st.st_size) != st.st_size) { 1339 command_errmsg = "error while reading the file"; 1340 (void) BS->FreePool(buf); 1341 close(fd); 1342 return (CMD_ERROR); 1343 } 1344 close(fd); 1345 status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle); 1346 (void) BS->FreePool(buf); 1347 if (status != EFI_SUCCESS) { 1348 command_errmsg = "LoadImage failed"; 1349 return (CMD_ERROR); 1350 } 1351 status = OpenProtocolByHandle(loaderhandle, &LoadedImageGUID, 1352 (void **)&loaded_image); 1353 1354 if (argc > 2) { 1355 int i, len = 0; 1356 CHAR16 *argp; 1357 1358 for (i = 2; i < argc; i++) 1359 len += strlen(argv[i]) + 1; 1360 1361 len *= sizeof (*argp); 1362 loaded_image->LoadOptions = argp = malloc(len); 1363 if (loaded_image->LoadOptions == NULL) { 1364 (void) BS->UnloadImage(loaded_image); 1365 return (CMD_ERROR); 1366 } 1367 loaded_image->LoadOptionsSize = len; 1368 for (i = 2; i < argc; i++) { 1369 char *ptr = argv[i]; 1370 while (*ptr) 1371 *(argp++) = *(ptr++); 1372 *(argp++) = ' '; 1373 } 1374 *(--argv) = 0; 1375 } 1376 1377 if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) { 1378 struct zfs_devdesc *z_dev; 1379 struct disk_devdesc *d_dev; 1380 pdinfo_t *hd, *pd; 1381 1382 switch (dev->d_dev->dv_type) { 1383 case DEVT_ZFS: 1384 z_dev = (struct zfs_devdesc *)dev; 1385 loaded_image->DeviceHandle = 1386 efizfs_get_handle_by_guid(z_dev->pool_guid); 1387 break; 1388 case DEVT_NET: 1389 loaded_image->DeviceHandle = 1390 efi_find_handle(dev->d_dev, dev->d_unit); 1391 break; 1392 default: 1393 hd = efiblk_get_pdinfo(dev); 1394 if (STAILQ_EMPTY(&hd->pd_part)) { 1395 loaded_image->DeviceHandle = hd->pd_handle; 1396 break; 1397 } 1398 d_dev = (struct disk_devdesc *)dev; 1399 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) { 1400 /* 1401 * d_partition should be 255 1402 */ 1403 if (pd->pd_unit == d_dev->d_slice) { 1404 loaded_image->DeviceHandle = 1405 pd->pd_handle; 1406 break; 1407 } 1408 } 1409 break; 1410 } 1411 } 1412 1413 dev_cleanup(); 1414 status = BS->StartImage(loaderhandle, NULL, NULL); 1415 if (status != EFI_SUCCESS) { 1416 command_errmsg = "StartImage failed"; 1417 free(loaded_image->LoadOptions); 1418 loaded_image->LoadOptions = NULL; 1419 status = BS->UnloadImage(loaded_image); 1420 return (CMD_ERROR); 1421 } 1422 1423 return (CMD_ERROR); /* not reached */ 1424 } 1425 1426 COMMAND_SET(chain, "chain", "chain load file", command_chain); 1427