1 /*- 2 * Copyright (c) 2008-2010 Rui Paulo 3 * Copyright (c) 2006 Marcel Moolenaar 4 * All rights reserved. 5 * 6 * Copyright (c) 2018 Netflix, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <stand.h> 34 35 #include <sys/disk.h> 36 #include <sys/param.h> 37 #include <sys/reboot.h> 38 #include <sys/boot.h> 39 #include <stdint.h> 40 #include <string.h> 41 #include <setjmp.h> 42 #include <disk.h> 43 44 #include <efi.h> 45 #include <efilib.h> 46 #include <efichar.h> 47 48 #include <uuid.h> 49 50 #include <bootstrap.h> 51 #include <smbios.h> 52 53 #include "efizfs.h" 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 /* 87 * Current boot variable 88 */ 89 UINT16 boot_current; 90 91 static bool 92 has_keyboard(void) 93 { 94 EFI_STATUS status; 95 EFI_DEVICE_PATH *path; 96 EFI_HANDLE *hin, *hin_end, *walker; 97 UINTN sz; 98 bool retval = false; 99 100 /* 101 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and 102 * do the typical dance to get the right sized buffer. 103 */ 104 sz = 0; 105 hin = NULL; 106 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 0); 107 if (status == EFI_BUFFER_TOO_SMALL) { 108 hin = (EFI_HANDLE *)malloc(sz); 109 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 110 hin); 111 if (EFI_ERROR(status)) 112 free(hin); 113 } 114 if (EFI_ERROR(status)) 115 return retval; 116 117 /* 118 * Look at each of the handles. If it supports the device path protocol, 119 * use it to get the device path for this handle. Then see if that 120 * device path matches either the USB device path for keyboards or the 121 * legacy device path for keyboards. 122 */ 123 hin_end = &hin[sz / sizeof(*hin)]; 124 for (walker = hin; walker < hin_end; walker++) { 125 status = BS->HandleProtocol(*walker, &devid, (VOID **)&path); 126 if (EFI_ERROR(status)) 127 continue; 128 129 while (!IsDevicePathEnd(path)) { 130 /* 131 * Check for the ACPI keyboard node. All PNP3xx nodes 132 * are keyboards of different flavors. Note: It is 133 * unclear of there's always a keyboard node when 134 * there's a keyboard controller, or if there's only one 135 * when a keyboard is detected at boot. 136 */ 137 if (DevicePathType(path) == ACPI_DEVICE_PATH && 138 (DevicePathSubType(path) == ACPI_DP || 139 DevicePathSubType(path) == ACPI_EXTENDED_DP)) { 140 ACPI_HID_DEVICE_PATH *acpi; 141 142 acpi = (ACPI_HID_DEVICE_PATH *)(void *)path; 143 if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 0x300 && 144 (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) { 145 retval = true; 146 goto out; 147 } 148 /* 149 * Check for USB keyboard node, if present. Unlike a 150 * PS/2 keyboard, these definitely only appear when 151 * connected to the system. 152 */ 153 } else if (DevicePathType(path) == MESSAGING_DEVICE_PATH && 154 DevicePathSubType(path) == MSG_USB_CLASS_DP) { 155 USB_CLASS_DEVICE_PATH *usb; 156 157 usb = (USB_CLASS_DEVICE_PATH *)(void *)path; 158 if (usb->DeviceClass == 3 && /* HID */ 159 usb->DeviceSubClass == 1 && /* Boot devices */ 160 usb->DeviceProtocol == 1) { /* Boot keyboards */ 161 retval = true; 162 goto out; 163 } 164 } 165 path = NextDevicePathNode(path); 166 } 167 } 168 out: 169 free(hin); 170 return retval; 171 } 172 173 static void 174 set_currdev(const char *devname) 175 { 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_devdesc(struct devdesc *currdev) 183 { 184 const char *devname; 185 186 devname = efi_fmtdev(currdev); 187 printf("Setting currdev to %s\n", devname); 188 set_currdev(devname); 189 } 190 191 static void 192 set_currdev_devsw(struct devsw *dev, int unit) 193 { 194 struct devdesc currdev; 195 196 currdev.d_dev = dev; 197 currdev.d_unit = unit; 198 199 set_currdev_devdesc(&currdev); 200 } 201 202 static void 203 set_currdev_pdinfo(pdinfo_t *dp) 204 { 205 206 /* 207 * Disks are special: they have partitions. if the parent 208 * pointer is non-null, we're a partition not a full disk 209 * and we need to adjust currdev appropriately. 210 */ 211 if (dp->pd_devsw->dv_type == DEVT_DISK) { 212 struct disk_devdesc currdev; 213 214 currdev.dd.d_dev = dp->pd_devsw; 215 if (dp->pd_parent == NULL) { 216 currdev.dd.d_unit = dp->pd_unit; 217 currdev.d_slice = D_SLICENONE; 218 currdev.d_partition = D_PARTNONE; 219 } else { 220 currdev.dd.d_unit = dp->pd_parent->pd_unit; 221 currdev.d_slice = dp->pd_unit; 222 currdev.d_partition = D_PARTISGPT; /* XXX Assumes GPT */ 223 } 224 set_currdev_devdesc((struct devdesc *)&currdev); 225 } else { 226 set_currdev_devsw(dp->pd_devsw, dp->pd_unit); 227 } 228 } 229 230 static bool 231 sanity_check_currdev(void) 232 { 233 struct stat st; 234 235 return (stat("/boot/defaults/loader.conf", &st) == 0 || 236 stat("/boot/kernel/kernel", &st) == 0); 237 } 238 239 #ifdef EFI_ZFS_BOOT 240 static bool 241 probe_zfs_currdev(uint64_t guid) 242 { 243 char *devname; 244 struct zfs_devdesc currdev; 245 246 currdev.dd.d_dev = &zfs_dev; 247 currdev.dd.d_unit = 0; 248 currdev.pool_guid = guid; 249 currdev.root_guid = 0; 250 set_currdev_devdesc((struct devdesc *)&currdev); 251 devname = efi_fmtdev(&currdev); 252 init_zfs_bootenv(devname); 253 254 return (sanity_check_currdev()); 255 } 256 #endif 257 258 static bool 259 try_as_currdev(pdinfo_t *hd, pdinfo_t *pp) 260 { 261 uint64_t guid; 262 263 #ifdef EFI_ZFS_BOOT 264 /* 265 * If there's a zpool on this device, try it as a ZFS 266 * filesystem, which has somewhat different setup than all 267 * other types of fs due to imperfect loader integration. 268 * This all stems from ZFS being both a device (zpool) and 269 * a filesystem, plus the boot env feature. 270 */ 271 if (efizfs_get_guid_by_handle(pp->pd_handle, &guid)) 272 return (probe_zfs_currdev(guid)); 273 #endif 274 /* 275 * All other filesystems just need the pdinfo 276 * initialized in the standard way. 277 */ 278 set_currdev_pdinfo(pp); 279 return (sanity_check_currdev()); 280 } 281 282 /* 283 * Sometimes we get filenames that are all upper case 284 * and/or have backslashes in them. Filter all this out 285 * if it looks like we need to do so. 286 */ 287 static void 288 fix_dosisms(char *p) 289 { 290 while (*p) { 291 if (isupper(*p)) 292 *p = tolower(*p); 293 else if (*p == '\\') 294 *p = '/'; 295 p++; 296 } 297 } 298 299 #define SIZE(dp, edp) (size_t)((intptr_t)(void *)edp - (intptr_t)(void *)dp) 300 301 enum { BOOT_INFO_OK = 0, BAD_CHOICE = 1, NOT_SPECIFIC = 2 }; 302 static int 303 match_boot_info(EFI_LOADED_IMAGE *img __unused, char *boot_info, size_t bisz) 304 { 305 uint32_t attr; 306 uint16_t fplen; 307 size_t len; 308 char *walker, *ep; 309 EFI_DEVICE_PATH *dp, *edp, *first_dp, *last_dp; 310 pdinfo_t *pp; 311 CHAR16 *descr; 312 char *kernel = NULL; 313 FILEPATH_DEVICE_PATH *fp; 314 struct stat st; 315 CHAR16 *text; 316 317 /* 318 * FreeBSD encodes it's boot loading path into the boot loader 319 * BootXXXX variable. We look for the last one in the path 320 * and use that to load the kernel. However, if we only fine 321 * one DEVICE_PATH, then there's nothing specific and we should 322 * fall back. 323 * 324 * In an ideal world, we'd look at the image handle we were 325 * passed, match up with the loader we are and then return the 326 * next one in the path. This would be most flexible and cover 327 * many chain booting scenarios where you need to use this 328 * boot loader to get to the next boot loader. However, that 329 * doesn't work. We rarely have the path to the image booted 330 * (just the device) so we can't count on that. So, we do the 331 * enxt best thing, we look through the device path(s) passed 332 * in the BootXXXX varaible. If there's only one, we return 333 * NOT_SPECIFIC. Otherwise, we look at the last one and try to 334 * load that. If we can, we return BOOT_INFO_OK. Otherwise we 335 * return BAD_CHOICE for the caller to sort out. 336 */ 337 if (bisz < sizeof(attr) + sizeof(fplen) + sizeof(CHAR16)) 338 return NOT_SPECIFIC; 339 walker = boot_info; 340 ep = walker + bisz; 341 memcpy(&attr, walker, sizeof(attr)); 342 walker += sizeof(attr); 343 memcpy(&fplen, walker, sizeof(fplen)); 344 walker += sizeof(fplen); 345 descr = (CHAR16 *)(intptr_t)walker; 346 len = ucs2len(descr); 347 walker += (len + 1) * sizeof(CHAR16); 348 last_dp = first_dp = dp = (EFI_DEVICE_PATH *)walker; 349 edp = (EFI_DEVICE_PATH *)(walker + fplen); 350 if ((char *)edp > ep) 351 return NOT_SPECIFIC; 352 while (dp < edp && SIZE(dp, edp) > sizeof(EFI_DEVICE_PATH)) { 353 text = efi_devpath_name(dp); 354 if (text != NULL) { 355 printf(" BootInfo Path: %S\n", text); 356 efi_free_devpath_name(text); 357 } 358 last_dp = dp; 359 dp = (EFI_DEVICE_PATH *)((char *)dp + efi_devpath_length(dp)); 360 } 361 362 /* 363 * If there's only one item in the list, then nothing was 364 * specified. Or if the last path doesn't have a media 365 * path in it. Those show up as various VenHw() nodes 366 * which are basically opaque to us. Don't count those 367 * as something specifc. 368 */ 369 if (last_dp == first_dp) { 370 printf("Ignoring Boot%04x: Only one DP found\n", boot_current); 371 return NOT_SPECIFIC; 372 } 373 if (efi_devpath_to_media_path(last_dp) == NULL) { 374 printf("Ignoring Boot%04x: No Media Path\n", boot_current); 375 return NOT_SPECIFIC; 376 } 377 378 /* 379 * OK. At this point we either have a good path or a bad one. 380 * Let's check. 381 */ 382 pp = efiblk_get_pdinfo_by_device_path(last_dp); 383 if (pp == NULL) { 384 printf("Ignoring Boot%04x: Device Path not found\n", boot_current); 385 return BAD_CHOICE; 386 } 387 set_currdev_pdinfo(pp); 388 if (!sanity_check_currdev()) { 389 printf("Ignoring Boot%04x: sanity check failed\n", boot_current); 390 return BAD_CHOICE; 391 } 392 393 /* 394 * OK. We've found a device that matches, next we need to check the last 395 * component of the path. If it's a file, then we set the default kernel 396 * to that. Otherwise, just use this as the default root. 397 * 398 * Reminder: we're running very early, before we've parsed the defaults 399 * file, so we may need to have a hack override. 400 */ 401 dp = efi_devpath_last_node(last_dp); 402 if (DevicePathType(dp) != MEDIA_DEVICE_PATH || 403 DevicePathSubType(dp) != MEDIA_FILEPATH_DP) { 404 printf("Using Boot%04x for root partition\n", boot_current); 405 return (BOOT_INFO_OK); /* use currdir, default kernel */ 406 } 407 fp = (FILEPATH_DEVICE_PATH *)dp; 408 ucs2_to_utf8(fp->PathName, &kernel); 409 if (kernel == NULL) { 410 printf("Not using Boot%04x: can't decode kernel\n", boot_current); 411 return (BAD_CHOICE); 412 } 413 if (*kernel == '\\' || isupper(*kernel)) 414 fix_dosisms(kernel); 415 if (stat(kernel, &st) != 0) { 416 free(kernel); 417 printf("Not using Boot%04x: can't find %s\n", boot_current, 418 kernel); 419 return (BAD_CHOICE); 420 } 421 setenv("kernel", kernel, 1); 422 free(kernel); 423 text = efi_devpath_name(last_dp); 424 if (text) { 425 printf("Using Boot%04x %S + %s\n", boot_current, text, 426 kernel); 427 efi_free_devpath_name(text); 428 } 429 430 return (BOOT_INFO_OK); 431 } 432 433 /* 434 * Look at the passed-in boot_info, if any. If we find it then we need 435 * to see if we can find ourselves in the boot chain. If we can, and 436 * there's another specified thing to boot next, assume that the file 437 * is loaded from / and use that for the root filesystem. If can't 438 * find the specified thing, we must fail the boot. If we're last on 439 * the list, then we fallback to looking for the first available / 440 * candidate (ZFS, if there's a bootable zpool, otherwise a UFS 441 * partition that has either /boot/defaults/loader.conf on it or 442 * /boot/kernel/kernel (the default kernel) that we can use. 443 * 444 * We always fail if we can't find the right thing. However, as 445 * a concession to buggy UEFI implementations, like u-boot, if 446 * we have determined that the host is violating the UEFI boot 447 * manager protocol, we'll signal the rest of the program that 448 * a drop to the OK boot loader prompt is possible. 449 */ 450 static int 451 find_currdev(EFI_LOADED_IMAGE *img, bool do_bootmgr, bool is_last, 452 char *boot_info, size_t boot_info_sz) 453 { 454 pdinfo_t *dp, *pp; 455 EFI_DEVICE_PATH *devpath, *copy; 456 EFI_HANDLE h; 457 CHAR16 *text; 458 struct devsw *dev; 459 int unit; 460 uint64_t extra; 461 int rv; 462 char *rootdev; 463 464 /* 465 * First choice: if rootdev is already set, use that, even if 466 * it's wrong. 467 */ 468 rootdev = getenv("rootdev"); 469 if (rootdev != NULL) { 470 printf("Setting currdev to configured rootdev %s\n", rootdev); 471 set_currdev(rootdev); 472 return (0); 473 } 474 475 /* 476 * Second choice: If we can find out image boot_info, and there's 477 * a follow-on boot image in that boot_info, use that. In this 478 * case root will be the partition specified in that image and 479 * we'll load the kernel specified by the file path. Should there 480 * not be a filepath, we use the default. This filepath overrides 481 * loader.conf. 482 */ 483 if (do_bootmgr) { 484 rv = match_boot_info(img, boot_info, boot_info_sz); 485 switch (rv) { 486 case BOOT_INFO_OK: /* We found it */ 487 return (0); 488 case BAD_CHOICE: /* specified file not found -> error */ 489 /* XXX do we want to have an escape hatch for last in boot order? */ 490 return (ENOENT); 491 } /* Nothing specified, try normal match */ 492 } 493 494 #ifdef EFI_ZFS_BOOT 495 /* 496 * Did efi_zfs_probe() detect the boot pool? If so, use the zpool 497 * it found, if it's sane. ZFS is the only thing that looks for 498 * disks and pools to boot. This may change in the future, however, 499 * if we allow specifying which pool to boot from via UEFI variables 500 * rather than the bootenv stuff that FreeBSD uses today. 501 */ 502 if (pool_guid != 0) { 503 printf("Trying ZFS pool\n"); 504 if (probe_zfs_currdev(pool_guid)) 505 return (0); 506 } 507 #endif /* EFI_ZFS_BOOT */ 508 509 /* 510 * Try to find the block device by its handle based on the 511 * image we're booting. If we can't find a sane partition, 512 * search all the other partitions of the disk. We do not 513 * search other disks because it's a violation of the UEFI 514 * boot protocol to do so. We fail and let UEFI go on to 515 * the next candidate. 516 */ 517 dp = efiblk_get_pdinfo_by_handle(img->DeviceHandle); 518 if (dp != NULL) { 519 text = efi_devpath_name(dp->pd_devpath); 520 if (text != NULL) { 521 printf("Trying ESP: %S\n", text); 522 efi_free_devpath_name(text); 523 } 524 set_currdev_pdinfo(dp); 525 if (sanity_check_currdev()) 526 return (0); 527 if (dp->pd_parent != NULL) { 528 pdinfo_t *espdp = dp; 529 dp = dp->pd_parent; 530 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) { 531 /* Already tried the ESP */ 532 if (espdp == pp) 533 continue; 534 /* 535 * Roll up the ZFS special case 536 * for those partitions that have 537 * zpools on them. 538 */ 539 text = efi_devpath_name(pp->pd_devpath); 540 if (text != NULL) { 541 printf("Trying: %S\n", text); 542 efi_free_devpath_name(text); 543 } 544 if (try_as_currdev(dp, pp)) 545 return (0); 546 } 547 } 548 } 549 550 /* 551 * Try the device handle from our loaded image first. If that 552 * fails, use the device path from the loaded image and see if 553 * any of the nodes in that path match one of the enumerated 554 * handles. Currently, this handle list is only for netboot. 555 */ 556 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) { 557 set_currdev_devsw(dev, unit); 558 if (sanity_check_currdev()) 559 return (0); 560 } 561 562 copy = NULL; 563 devpath = efi_lookup_image_devpath(IH); 564 while (devpath != NULL) { 565 h = efi_devpath_handle(devpath); 566 if (h == NULL) 567 break; 568 569 free(copy); 570 copy = NULL; 571 572 if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) { 573 set_currdev_devsw(dev, unit); 574 if (sanity_check_currdev()) 575 return (0); 576 } 577 578 devpath = efi_lookup_devpath(h); 579 if (devpath != NULL) { 580 copy = efi_devpath_trim(devpath); 581 devpath = copy; 582 } 583 } 584 free(copy); 585 586 return (ENOENT); 587 } 588 589 static bool 590 interactive_interrupt(const char *msg) 591 { 592 time_t now, then, last; 593 594 last = 0; 595 now = then = getsecs(); 596 printf("%s\n", msg); 597 if (fail_timeout == -2) /* Always break to OK */ 598 return (true); 599 if (fail_timeout == -1) /* Never break to OK */ 600 return (false); 601 do { 602 if (last != now) { 603 printf("press any key to interrupt reboot in %d seconds\r", 604 fail_timeout - (int)(now - then)); 605 last = now; 606 } 607 608 /* XXX no pause or timeout wait for char */ 609 if (ischar()) 610 return (true); 611 now = getsecs(); 612 } while (now - then < fail_timeout); 613 return (false); 614 } 615 616 static int 617 parse_args(int argc, CHAR16 *argv[]) 618 { 619 int i, j, howto; 620 bool vargood; 621 char var[128]; 622 623 /* 624 * Parse the args to set the console settings, etc 625 * boot1.efi passes these in, if it can read /boot.config or /boot/config 626 * or iPXE may be setup to pass these in. Or the optional argument in the 627 * boot environment was used to pass these arguments in (in which case 628 * neither /boot.config nor /boot/config are consulted). 629 * 630 * Loop through the args, and for each one that contains an '=' that is 631 * not the first character, add it to the environment. This allows 632 * loader and kernel env vars to be passed on the command line. Convert 633 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied (though this 634 * method is flawed for non-ASCII characters). 635 */ 636 howto = 0; 637 for (i = 1; i < argc; i++) { 638 cpy16to8(argv[i], var, sizeof(var)); 639 howto |= boot_parse_arg(var); 640 } 641 642 return (howto); 643 } 644 645 static void 646 setenv_int(const char *key, int val) 647 { 648 char buf[20]; 649 650 snprintf(buf, sizeof(buf), "%d", val); 651 setenv(key, buf, 1); 652 } 653 654 /* 655 * Parse ConOut (the list of consoles active) and see if we can find a 656 * serial port and/or a video port. It would be nice to also walk the 657 * ACPI name space to map the UID for the serial port to a port. The 658 * latter is especially hard. 659 */ 660 static int 661 parse_uefi_con_out(void) 662 { 663 int how, rv; 664 int vid_seen = 0, com_seen = 0, seen = 0; 665 size_t sz; 666 char buf[4096], *ep; 667 EFI_DEVICE_PATH *node; 668 ACPI_HID_DEVICE_PATH *acpi; 669 UART_DEVICE_PATH *uart; 670 bool pci_pending; 671 672 how = 0; 673 sz = sizeof(buf); 674 rv = efi_global_getenv("ConOut", buf, &sz); 675 if (rv != EFI_SUCCESS) 676 goto out; 677 ep = buf + sz; 678 node = (EFI_DEVICE_PATH *)buf; 679 while ((char *)node < ep) { 680 pci_pending = false; 681 if (DevicePathType(node) == ACPI_DEVICE_PATH && 682 DevicePathSubType(node) == ACPI_DP) { 683 /* Check for Serial node */ 684 acpi = (void *)node; 685 if (EISA_ID_TO_NUM(acpi->HID) == 0x501) { 686 setenv_int("efi_8250_uid", acpi->UID); 687 com_seen = ++seen; 688 } 689 } else if (DevicePathType(node) == MESSAGING_DEVICE_PATH && 690 DevicePathSubType(node) == MSG_UART_DP) { 691 692 uart = (void *)node; 693 setenv_int("efi_com_speed", uart->BaudRate); 694 } else if (DevicePathType(node) == ACPI_DEVICE_PATH && 695 DevicePathSubType(node) == ACPI_ADR_DP) { 696 /* Check for AcpiAdr() Node for video */ 697 vid_seen = ++seen; 698 } else if (DevicePathType(node) == HARDWARE_DEVICE_PATH && 699 DevicePathSubType(node) == HW_PCI_DP) { 700 /* 701 * Note, vmware fusion has a funky console device 702 * PciRoot(0x0)/Pci(0xf,0x0) 703 * which we can only detect at the end since we also 704 * have to cope with: 705 * PciRoot(0x0)/Pci(0x1f,0x0)/Serial(0x1) 706 * so only match it if it's last. 707 */ 708 pci_pending = true; 709 } 710 node = NextDevicePathNode(node); /* Skip the end node */ 711 } 712 if (pci_pending && vid_seen == 0) 713 vid_seen = ++seen; 714 715 /* 716 * Truth table for RB_MULTIPLE | RB_SERIAL 717 * Value Result 718 * 0 Use only video console 719 * RB_SERIAL Use only serial console 720 * RB_MULTIPLE Use both video and serial console 721 * (but video is primary so gets rc messages) 722 * both Use both video and serial console 723 * (but serial is primary so gets rc messages) 724 * 725 * Try to honor this as best we can. If only one of serial / video 726 * found, then use that. Otherwise, use the first one we found. 727 * This also implies if we found nothing, default to video. 728 */ 729 how = 0; 730 if (vid_seen && com_seen) { 731 how |= RB_MULTIPLE; 732 if (com_seen < vid_seen) 733 how |= RB_SERIAL; 734 } else if (com_seen) 735 how |= RB_SERIAL; 736 out: 737 return (how); 738 } 739 740 EFI_STATUS 741 main(int argc, CHAR16 *argv[]) 742 { 743 EFI_GUID *guid; 744 int howto, i, uhowto; 745 UINTN k; 746 bool has_kbd, is_last; 747 char *s; 748 EFI_DEVICE_PATH *imgpath; 749 CHAR16 *text; 750 EFI_STATUS rv; 751 size_t sz, bosz = 0, bisz = 0; 752 UINT16 boot_order[100]; 753 char boot_info[4096]; 754 EFI_LOADED_IMAGE *img; 755 char buf[32]; 756 bool uefi_boot_mgr; 757 758 archsw.arch_autoload = efi_autoload; 759 archsw.arch_getdev = efi_getdev; 760 archsw.arch_copyin = efi_copyin; 761 archsw.arch_copyout = efi_copyout; 762 archsw.arch_readin = efi_readin; 763 archsw.arch_zfs_probe = efi_zfs_probe; 764 765 /* Get our loaded image protocol interface structure. */ 766 BS->HandleProtocol(IH, &imgid, (VOID**)&img); 767 768 /* Tell ZFS probe code where we booted from, if zfs configured */ 769 efizfs_set_preferred(img->DeviceHandle); 770 771 /* Init the time source */ 772 efi_time_init(); 773 774 has_kbd = has_keyboard(); 775 776 /* 777 * XXX Chicken-and-egg problem; we want to have console output 778 * early, but some console attributes may depend on reading from 779 * eg. the boot device, which we can't do yet. We can use 780 * printf() etc. once this is done. 781 */ 782 setenv("console", "efi", 1); 783 cons_probe(); 784 785 /* 786 * Initialise the block cache. Set the upper limit. 787 */ 788 bcache_init(32768, 512); 789 790 howto = parse_args(argc, argv); 791 if (!has_kbd && (howto & RB_PROBE)) 792 howto |= RB_SERIAL | RB_MULTIPLE; 793 howto &= ~RB_PROBE; 794 uhowto = parse_uefi_con_out(); 795 796 /* 797 * We now have two notions of console. howto should be viewed as 798 * overrides. If console is already set, don't set it again. 799 */ 800 #define VIDEO_ONLY 0 801 #define SERIAL_ONLY RB_SERIAL 802 #define VID_SER_BOTH RB_MULTIPLE 803 #define SER_VID_BOTH (RB_SERIAL | RB_MULTIPLE) 804 #define CON_MASK (RB_SERIAL | RB_MULTIPLE) 805 if (strcmp(getenv("console"), "efi") == 0) { 806 if ((howto & CON_MASK) == 0) { 807 /* No override, uhowto is controlling and efi cons is perfect */ 808 howto = howto | (uhowto & CON_MASK); 809 setenv("console", "efi", 1); 810 } else if ((howto & CON_MASK) == (uhowto & CON_MASK)) { 811 /* override matches what UEFI told us, efi console is perfect */ 812 setenv("console", "efi", 1); 813 } else if ((uhowto & (CON_MASK)) != 0) { 814 /* 815 * We detected a serial console on ConOut. All possible 816 * overrides include serial. We can't really override what efi 817 * gives us, so we use it knowing it's the best choice. 818 */ 819 setenv("console", "efi", 1); 820 } else { 821 /* 822 * We detected some kind of serial in the override, but ConOut 823 * has no serial, so we have to sort out which case it really is. 824 */ 825 switch (howto & CON_MASK) { 826 case SERIAL_ONLY: 827 setenv("console", "comconsole", 1); 828 break; 829 case VID_SER_BOTH: 830 setenv("console", "efi comconsole", 1); 831 break; 832 case SER_VID_BOTH: 833 setenv("console", "comconsole efi", 1); 834 break; 835 /* case VIDEO_ONLY can't happen -- it's the first if above */ 836 } 837 } 838 } 839 840 /* 841 * howto is set now how we want to export the flags to the kernel, so 842 * set the env based on it. 843 */ 844 boot_howto_to_env(howto); 845 846 if (efi_copy_init()) { 847 printf("failed to allocate staging area\n"); 848 return (EFI_BUFFER_TOO_SMALL); 849 } 850 851 if ((s = getenv("fail_timeout")) != NULL) 852 fail_timeout = strtol(s, NULL, 10); 853 854 /* 855 * Scan the BLOCK IO MEDIA handles then 856 * march through the device switch probing for things. 857 */ 858 i = efipart_inithandles(); 859 if (i != 0 && i != ENOENT) { 860 printf("efipart_inithandles failed with ERRNO %d, expect " 861 "failures\n", i); 862 } 863 864 for (i = 0; devsw[i] != NULL; i++) 865 if (devsw[i]->dv_init != NULL) 866 (devsw[i]->dv_init)(); 867 868 printf("%s\n", bootprog_info); 869 printf(" Command line arguments:"); 870 for (i = 0; i < argc; i++) 871 printf(" %S", argv[i]); 872 printf("\n"); 873 874 printf(" EFI version: %d.%02d\n", ST->Hdr.Revision >> 16, 875 ST->Hdr.Revision & 0xffff); 876 printf(" EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor, 877 ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff); 878 printf(" Console: %s (%#x)\n", getenv("console"), howto); 879 880 881 882 /* Determine the devpath of our image so we can prefer it. */ 883 text = efi_devpath_name(img->FilePath); 884 if (text != NULL) { 885 printf(" Load Path: %S\n", text); 886 efi_setenv_freebsd_wcs("LoaderPath", text); 887 efi_free_devpath_name(text); 888 } 889 890 rv = BS->HandleProtocol(img->DeviceHandle, &devid, (void **)&imgpath); 891 if (rv == EFI_SUCCESS) { 892 text = efi_devpath_name(imgpath); 893 if (text != NULL) { 894 printf(" Load Device: %S\n", text); 895 efi_setenv_freebsd_wcs("LoaderDev", text); 896 efi_free_devpath_name(text); 897 } 898 } 899 900 uefi_boot_mgr = true; 901 boot_current = 0; 902 sz = sizeof(boot_current); 903 rv = efi_global_getenv("BootCurrent", &boot_current, &sz); 904 if (rv == EFI_SUCCESS) 905 printf(" BootCurrent: %04x\n", boot_current); 906 else { 907 boot_current = 0xffff; 908 uefi_boot_mgr = false; 909 } 910 911 sz = sizeof(boot_order); 912 rv = efi_global_getenv("BootOrder", &boot_order, &sz); 913 if (rv == EFI_SUCCESS) { 914 printf(" BootOrder:"); 915 for (i = 0; i < sz / sizeof(boot_order[0]); i++) 916 printf(" %04x%s", boot_order[i], 917 boot_order[i] == boot_current ? "[*]" : ""); 918 printf("\n"); 919 is_last = boot_order[(sz / sizeof(boot_order[0])) - 1] == boot_current; 920 bosz = sz; 921 } else if (uefi_boot_mgr) { 922 /* 923 * u-boot doesn't set BootOrder, but otherwise participates in the 924 * boot manager protocol. So we fake it here and don't consider it 925 * a failure. 926 */ 927 bosz = sizeof(boot_order[0]); 928 boot_order[0] = boot_current; 929 is_last = true; 930 } 931 932 /* 933 * Next, find the boot info structure the UEFI boot manager is 934 * supposed to setup. We need this so we can walk through it to 935 * find where we are in the booting process and what to try to 936 * boot next. 937 */ 938 if (uefi_boot_mgr) { 939 snprintf(buf, sizeof(buf), "Boot%04X", boot_current); 940 sz = sizeof(boot_info); 941 rv = efi_global_getenv(buf, &boot_info, &sz); 942 if (rv == EFI_SUCCESS) 943 bisz = sz; 944 else 945 uefi_boot_mgr = false; 946 } 947 948 /* 949 * Disable the watchdog timer. By default the boot manager sets 950 * the timer to 5 minutes before invoking a boot option. If we 951 * want to return to the boot manager, we have to disable the 952 * watchdog timer and since we're an interactive program, we don't 953 * want to wait until the user types "quit". The timer may have 954 * fired by then. We don't care if this fails. It does not prevent 955 * normal functioning in any way... 956 */ 957 BS->SetWatchdogTimer(0, 0, 0, NULL); 958 959 /* 960 * Initialize the trusted/forbidden certificates from UEFI. 961 * They will be later used to verify the manifest(s), 962 * which should contain hashes of verified files. 963 * This needs to be initialized before any configuration files 964 * are loaded. 965 */ 966 #ifdef EFI_SECUREBOOT 967 ve_efi_init(); 968 #endif 969 970 /* 971 * Try and find a good currdev based on the image that was booted. 972 * It might be desirable here to have a short pause to allow falling 973 * through to the boot loader instead of returning instantly to follow 974 * the boot protocol and also allow an escape hatch for users wishing 975 * to try something different. 976 */ 977 if (find_currdev(img, uefi_boot_mgr, is_last, boot_info, bisz) != 0) 978 if (!interactive_interrupt("Failed to find bootable partition")) 979 return (EFI_NOT_FOUND); 980 981 efi_init_environment(); 982 983 #if !defined(__arm__) 984 for (k = 0; k < ST->NumberOfTableEntries; k++) { 985 guid = &ST->ConfigurationTable[k].VendorGuid; 986 if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) { 987 char buf[40]; 988 989 snprintf(buf, sizeof(buf), "%p", 990 ST->ConfigurationTable[k].VendorTable); 991 setenv("hint.smbios.0.mem", buf, 1); 992 smbios_detect(ST->ConfigurationTable[k].VendorTable); 993 break; 994 } 995 } 996 #endif 997 998 interact(); /* doesn't return */ 999 1000 return (EFI_SUCCESS); /* keep compiler happy */ 1001 } 1002 1003 COMMAND_SET(poweroff, "poweroff", "power off the system", command_poweroff); 1004 1005 static int 1006 command_poweroff(int argc __unused, char *argv[] __unused) 1007 { 1008 int i; 1009 1010 for (i = 0; devsw[i] != NULL; ++i) 1011 if (devsw[i]->dv_cleanup != NULL) 1012 (devsw[i]->dv_cleanup)(); 1013 1014 RS->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL); 1015 1016 /* NOTREACHED */ 1017 return (CMD_ERROR); 1018 } 1019 1020 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot); 1021 1022 static int 1023 command_reboot(int argc, char *argv[]) 1024 { 1025 int i; 1026 1027 for (i = 0; devsw[i] != NULL; ++i) 1028 if (devsw[i]->dv_cleanup != NULL) 1029 (devsw[i]->dv_cleanup)(); 1030 1031 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL); 1032 1033 /* NOTREACHED */ 1034 return (CMD_ERROR); 1035 } 1036 1037 COMMAND_SET(quit, "quit", "exit the loader", command_quit); 1038 1039 static int 1040 command_quit(int argc, char *argv[]) 1041 { 1042 exit(0); 1043 return (CMD_OK); 1044 } 1045 1046 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap); 1047 1048 static int 1049 command_memmap(int argc __unused, char *argv[] __unused) 1050 { 1051 UINTN sz; 1052 EFI_MEMORY_DESCRIPTOR *map, *p; 1053 UINTN key, dsz; 1054 UINT32 dver; 1055 EFI_STATUS status; 1056 int i, ndesc; 1057 char line[80]; 1058 1059 sz = 0; 1060 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver); 1061 if (status != EFI_BUFFER_TOO_SMALL) { 1062 printf("Can't determine memory map size\n"); 1063 return (CMD_ERROR); 1064 } 1065 map = malloc(sz); 1066 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver); 1067 if (EFI_ERROR(status)) { 1068 printf("Can't read memory map\n"); 1069 return (CMD_ERROR); 1070 } 1071 1072 ndesc = sz / dsz; 1073 snprintf(line, sizeof(line), "%23s %12s %12s %8s %4s\n", 1074 "Type", "Physical", "Virtual", "#Pages", "Attr"); 1075 pager_open(); 1076 if (pager_output(line)) { 1077 pager_close(); 1078 return (CMD_OK); 1079 } 1080 1081 for (i = 0, p = map; i < ndesc; 1082 i++, p = NextMemoryDescriptor(p, dsz)) { 1083 snprintf(line, sizeof(line), "%23s %012jx %012jx %08jx ", 1084 efi_memory_type(p->Type), (uintmax_t)p->PhysicalStart, 1085 (uintmax_t)p->VirtualStart, (uintmax_t)p->NumberOfPages); 1086 if (pager_output(line)) 1087 break; 1088 1089 if (p->Attribute & EFI_MEMORY_UC) 1090 printf("UC "); 1091 if (p->Attribute & EFI_MEMORY_WC) 1092 printf("WC "); 1093 if (p->Attribute & EFI_MEMORY_WT) 1094 printf("WT "); 1095 if (p->Attribute & EFI_MEMORY_WB) 1096 printf("WB "); 1097 if (p->Attribute & EFI_MEMORY_UCE) 1098 printf("UCE "); 1099 if (p->Attribute & EFI_MEMORY_WP) 1100 printf("WP "); 1101 if (p->Attribute & EFI_MEMORY_RP) 1102 printf("RP "); 1103 if (p->Attribute & EFI_MEMORY_XP) 1104 printf("XP "); 1105 if (p->Attribute & EFI_MEMORY_NV) 1106 printf("NV "); 1107 if (p->Attribute & EFI_MEMORY_MORE_RELIABLE) 1108 printf("MR "); 1109 if (p->Attribute & EFI_MEMORY_RO) 1110 printf("RO "); 1111 if (pager_output("\n")) 1112 break; 1113 } 1114 1115 pager_close(); 1116 return (CMD_OK); 1117 } 1118 1119 COMMAND_SET(configuration, "configuration", "print configuration tables", 1120 command_configuration); 1121 1122 static int 1123 command_configuration(int argc, char *argv[]) 1124 { 1125 UINTN i; 1126 char *name; 1127 1128 printf("NumberOfTableEntries=%lu\n", 1129 (unsigned long)ST->NumberOfTableEntries); 1130 1131 for (i = 0; i < ST->NumberOfTableEntries; i++) { 1132 EFI_GUID *guid; 1133 1134 printf(" "); 1135 guid = &ST->ConfigurationTable[i].VendorGuid; 1136 1137 if (efi_guid_to_name(guid, &name) == true) { 1138 printf(name); 1139 free(name); 1140 } else { 1141 printf("Error while translating UUID to name"); 1142 } 1143 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable); 1144 } 1145 1146 return (CMD_OK); 1147 } 1148 1149 1150 COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode); 1151 1152 static int 1153 command_mode(int argc, char *argv[]) 1154 { 1155 UINTN cols, rows; 1156 unsigned int mode; 1157 int i; 1158 char *cp; 1159 char rowenv[8]; 1160 EFI_STATUS status; 1161 SIMPLE_TEXT_OUTPUT_INTERFACE *conout; 1162 extern void HO(void); 1163 1164 conout = ST->ConOut; 1165 1166 if (argc > 1) { 1167 mode = strtol(argv[1], &cp, 0); 1168 if (cp[0] != '\0') { 1169 printf("Invalid mode\n"); 1170 return (CMD_ERROR); 1171 } 1172 status = conout->QueryMode(conout, mode, &cols, &rows); 1173 if (EFI_ERROR(status)) { 1174 printf("invalid mode %d\n", mode); 1175 return (CMD_ERROR); 1176 } 1177 status = conout->SetMode(conout, mode); 1178 if (EFI_ERROR(status)) { 1179 printf("couldn't set mode %d\n", mode); 1180 return (CMD_ERROR); 1181 } 1182 sprintf(rowenv, "%u", (unsigned)rows); 1183 setenv("LINES", rowenv, 1); 1184 HO(); /* set cursor */ 1185 return (CMD_OK); 1186 } 1187 1188 printf("Current mode: %d\n", conout->Mode->Mode); 1189 for (i = 0; i <= conout->Mode->MaxMode; i++) { 1190 status = conout->QueryMode(conout, i, &cols, &rows); 1191 if (EFI_ERROR(status)) 1192 continue; 1193 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols, 1194 (unsigned)rows); 1195 } 1196 1197 if (i != 0) 1198 printf("Select a mode with the command \"mode <number>\"\n"); 1199 1200 return (CMD_OK); 1201 } 1202 1203 COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi); 1204 1205 static int 1206 command_lsefi(int argc __unused, char *argv[] __unused) 1207 { 1208 char *name; 1209 EFI_HANDLE *buffer = NULL; 1210 EFI_HANDLE handle; 1211 UINTN bufsz = 0, i, j; 1212 EFI_STATUS status; 1213 int ret = 0; 1214 1215 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer); 1216 if (status != EFI_BUFFER_TOO_SMALL) { 1217 snprintf(command_errbuf, sizeof (command_errbuf), 1218 "unexpected error: %lld", (long long)status); 1219 return (CMD_ERROR); 1220 } 1221 if ((buffer = malloc(bufsz)) == NULL) { 1222 sprintf(command_errbuf, "out of memory"); 1223 return (CMD_ERROR); 1224 } 1225 1226 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer); 1227 if (EFI_ERROR(status)) { 1228 free(buffer); 1229 snprintf(command_errbuf, sizeof (command_errbuf), 1230 "LocateHandle() error: %lld", (long long)status); 1231 return (CMD_ERROR); 1232 } 1233 1234 pager_open(); 1235 for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) { 1236 UINTN nproto = 0; 1237 EFI_GUID **protocols = NULL; 1238 1239 handle = buffer[i]; 1240 printf("Handle %p", handle); 1241 if (pager_output("\n")) 1242 break; 1243 /* device path */ 1244 1245 status = BS->ProtocolsPerHandle(handle, &protocols, &nproto); 1246 if (EFI_ERROR(status)) { 1247 snprintf(command_errbuf, sizeof (command_errbuf), 1248 "ProtocolsPerHandle() error: %lld", 1249 (long long)status); 1250 continue; 1251 } 1252 1253 for (j = 0; j < nproto; j++) { 1254 if (efi_guid_to_name(protocols[j], &name) == true) { 1255 printf(" %s", name); 1256 free(name); 1257 } else { 1258 printf("Error while translating UUID to name"); 1259 } 1260 if ((ret = pager_output("\n")) != 0) 1261 break; 1262 } 1263 BS->FreePool(protocols); 1264 if (ret != 0) 1265 break; 1266 } 1267 pager_close(); 1268 free(buffer); 1269 return (CMD_OK); 1270 } 1271 1272 #ifdef LOADER_FDT_SUPPORT 1273 extern int command_fdt_internal(int argc, char *argv[]); 1274 1275 /* 1276 * Since proper fdt command handling function is defined in fdt_loader_cmd.c, 1277 * and declaring it as extern is in contradiction with COMMAND_SET() macro 1278 * (which uses static pointer), we're defining wrapper function, which 1279 * calls the proper fdt handling routine. 1280 */ 1281 static int 1282 command_fdt(int argc, char *argv[]) 1283 { 1284 1285 return (command_fdt_internal(argc, argv)); 1286 } 1287 1288 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt); 1289 #endif 1290 1291 /* 1292 * Chain load another efi loader. 1293 */ 1294 static int 1295 command_chain(int argc, char *argv[]) 1296 { 1297 EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL; 1298 EFI_HANDLE loaderhandle; 1299 EFI_LOADED_IMAGE *loaded_image; 1300 EFI_STATUS status; 1301 struct stat st; 1302 struct devdesc *dev; 1303 char *name, *path; 1304 void *buf; 1305 int fd; 1306 1307 if (argc < 2) { 1308 command_errmsg = "wrong number of arguments"; 1309 return (CMD_ERROR); 1310 } 1311 1312 name = argv[1]; 1313 1314 if ((fd = open(name, O_RDONLY)) < 0) { 1315 command_errmsg = "no such file"; 1316 return (CMD_ERROR); 1317 } 1318 1319 if (fstat(fd, &st) < -1) { 1320 command_errmsg = "stat failed"; 1321 close(fd); 1322 return (CMD_ERROR); 1323 } 1324 1325 status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf); 1326 if (status != EFI_SUCCESS) { 1327 command_errmsg = "failed to allocate buffer"; 1328 close(fd); 1329 return (CMD_ERROR); 1330 } 1331 if (read(fd, buf, st.st_size) != st.st_size) { 1332 command_errmsg = "error while reading the file"; 1333 (void)BS->FreePool(buf); 1334 close(fd); 1335 return (CMD_ERROR); 1336 } 1337 close(fd); 1338 status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle); 1339 (void)BS->FreePool(buf); 1340 if (status != EFI_SUCCESS) { 1341 command_errmsg = "LoadImage failed"; 1342 return (CMD_ERROR); 1343 } 1344 status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID, 1345 (void **)&loaded_image); 1346 1347 if (argc > 2) { 1348 int i, len = 0; 1349 CHAR16 *argp; 1350 1351 for (i = 2; i < argc; i++) 1352 len += strlen(argv[i]) + 1; 1353 1354 len *= sizeof (*argp); 1355 loaded_image->LoadOptions = argp = malloc (len); 1356 loaded_image->LoadOptionsSize = len; 1357 for (i = 2; i < argc; i++) { 1358 char *ptr = argv[i]; 1359 while (*ptr) 1360 *(argp++) = *(ptr++); 1361 *(argp++) = ' '; 1362 } 1363 *(--argv) = 0; 1364 } 1365 1366 if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) { 1367 #ifdef EFI_ZFS_BOOT 1368 struct zfs_devdesc *z_dev; 1369 #endif 1370 struct disk_devdesc *d_dev; 1371 pdinfo_t *hd, *pd; 1372 1373 switch (dev->d_dev->dv_type) { 1374 #ifdef EFI_ZFS_BOOT 1375 case DEVT_ZFS: 1376 z_dev = (struct zfs_devdesc *)dev; 1377 loaded_image->DeviceHandle = 1378 efizfs_get_handle_by_guid(z_dev->pool_guid); 1379 break; 1380 #endif 1381 case DEVT_NET: 1382 loaded_image->DeviceHandle = 1383 efi_find_handle(dev->d_dev, dev->d_unit); 1384 break; 1385 default: 1386 hd = efiblk_get_pdinfo(dev); 1387 if (STAILQ_EMPTY(&hd->pd_part)) { 1388 loaded_image->DeviceHandle = hd->pd_handle; 1389 break; 1390 } 1391 d_dev = (struct disk_devdesc *)dev; 1392 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) { 1393 /* 1394 * d_partition should be 255 1395 */ 1396 if (pd->pd_unit == (uint32_t)d_dev->d_slice) { 1397 loaded_image->DeviceHandle = 1398 pd->pd_handle; 1399 break; 1400 } 1401 } 1402 break; 1403 } 1404 } 1405 1406 dev_cleanup(); 1407 status = BS->StartImage(loaderhandle, NULL, NULL); 1408 if (status != EFI_SUCCESS) { 1409 command_errmsg = "StartImage failed"; 1410 free(loaded_image->LoadOptions); 1411 loaded_image->LoadOptions = NULL; 1412 status = BS->UnloadImage(loaded_image); 1413 return (CMD_ERROR); 1414 } 1415 1416 return (CMD_ERROR); /* not reached */ 1417 } 1418 1419 COMMAND_SET(chain, "chain", "chain load file", command_chain); 1420