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