2 * Copyright (c) 2008-2010 Rui Paulo
3 * Copyright (c) 2006 Marcel Moolenaar
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/reboot.h>
47 #include <bootstrap.h>
55 #include "loader_efi.h"
57 struct arch_switch archsw; /* MI/MD interface boundary */
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;
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
84 static int fail_timeout = 5;
90 EFI_DEVICE_PATH *path;
91 EFI_HANDLE *hin, *hin_end, *walker;
96 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and
97 * do the typical dance to get the right sized buffer.
101 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 0);
102 if (status == EFI_BUFFER_TOO_SMALL) {
103 hin = (EFI_HANDLE *)malloc(sz);
104 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz,
106 if (EFI_ERROR(status))
109 if (EFI_ERROR(status))
113 * Look at each of the handles. If it supports the device path protocol,
114 * use it to get the device path for this handle. Then see if that
115 * device path matches either the USB device path for keyboards or the
116 * legacy device path for keyboards.
118 hin_end = &hin[sz / sizeof(*hin)];
119 for (walker = hin; walker < hin_end; walker++) {
120 status = BS->HandleProtocol(*walker, &devid, (VOID **)&path);
121 if (EFI_ERROR(status))
124 while (!IsDevicePathEnd(path)) {
126 * Check for the ACPI keyboard node. All PNP3xx nodes
127 * are keyboards of different flavors. Note: It is
128 * unclear of there's always a keyboard node when
129 * there's a keyboard controller, or if there's only one
130 * when a keyboard is detected at boot.
132 if (DevicePathType(path) == ACPI_DEVICE_PATH &&
133 (DevicePathSubType(path) == ACPI_DP ||
134 DevicePathSubType(path) == ACPI_EXTENDED_DP)) {
135 ACPI_HID_DEVICE_PATH *acpi;
137 acpi = (ACPI_HID_DEVICE_PATH *)(void *)path;
138 if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 0x300 &&
139 (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) {
144 * Check for USB keyboard node, if present. Unlike a
145 * PS/2 keyboard, these definitely only appear when
146 * connected to the system.
148 } else if (DevicePathType(path) == MESSAGING_DEVICE_PATH &&
149 DevicePathSubType(path) == MSG_USB_CLASS_DP) {
150 USB_CLASS_DEVICE_PATH *usb;
152 usb = (USB_CLASS_DEVICE_PATH *)(void *)path;
153 if (usb->DeviceClass == 3 && /* HID */
154 usb->DeviceSubClass == 1 && /* Boot devices */
155 usb->DeviceProtocol == 1) { /* Boot keyboards */
160 path = NextDevicePathNode(path);
169 set_currdev(const char *devname)
172 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev, env_nounset);
173 env_setenv("loaddev", EV_VOLATILE, devname, env_noset, env_nounset);
177 set_currdev_devdesc(struct devdesc *currdev)
181 devname = efi_fmtdev(currdev);
182 printf("Setting currdev to %s\n", devname);
183 set_currdev(devname);
187 set_currdev_devsw(struct devsw *dev, int unit)
189 struct devdesc currdev;
192 currdev.d_unit = unit;
194 set_currdev_devdesc(&currdev);
198 set_currdev_pdinfo(pdinfo_t *dp)
202 * Disks are special: they have partitions. if the parent
203 * pointer is non-null, we're a partition not a full disk
204 * and we need to adjust currdev appropriately.
206 if (dp->pd_devsw->dv_type == DEVT_DISK) {
207 struct disk_devdesc currdev;
209 currdev.dd.d_dev = dp->pd_devsw;
210 if (dp->pd_parent == NULL) {
211 currdev.dd.d_unit = dp->pd_unit;
212 currdev.d_slice = -1;
213 currdev.d_partition = -1;
215 currdev.dd.d_unit = dp->pd_parent->pd_unit;
216 currdev.d_slice = dp->pd_unit;
217 currdev.d_partition = 255; /* Assumes GPT */
219 set_currdev_devdesc((struct devdesc *)&currdev);
221 set_currdev_devsw(dp->pd_devsw, dp->pd_unit);
226 sanity_check_currdev(void)
230 return (stat("/boot/defaults/loader.conf", &st) == 0 ||
231 stat("/boot/kernel/kernel", &st) == 0);
236 probe_zfs_currdev(uint64_t guid)
239 struct zfs_devdesc currdev;
241 currdev.dd.d_dev = &zfs_dev;
242 currdev.dd.d_unit = 0;
243 currdev.pool_guid = guid;
244 currdev.root_guid = 0;
245 set_currdev_devdesc((struct devdesc *)&currdev);
246 devname = efi_fmtdev(&currdev);
247 init_zfs_bootenv(devname);
249 return (sanity_check_currdev());
254 try_as_currdev(pdinfo_t *hd, pdinfo_t *pp)
260 * If there's a zpool on this device, try it as a ZFS
261 * filesystem, which has somewhat different setup than all
262 * other types of fs due to imperfect loader integration.
263 * This all stems from ZFS being both a device (zpool) and
264 * a filesystem, plus the boot env feature.
266 if (efizfs_get_guid_by_handle(pp->pd_handle, &guid))
267 return (probe_zfs_currdev(guid));
270 * All other filesystems just need the pdinfo
271 * initialized in the standard way.
273 set_currdev_pdinfo(pp);
274 return (sanity_check_currdev());
278 find_currdev(EFI_LOADED_IMAGE *img)
281 EFI_DEVICE_PATH *devpath, *copy;
289 rootdev = getenv("rootdev");
290 if (rootdev != NULL) {
291 printf("Setting currdev to configured rootdev %s\n", rootdev);
292 set_currdev(rootdev);
298 * Did efi_zfs_probe() detect the boot pool? If so, use the zpool
299 * it found, if it's sane. ZFS is the only thing that looks for
300 * disks and pools to boot. This may change in the future, however,
301 * if we allow specifying which pool to boot from via UEFI variables
302 * rather than the bootenv stuff that FreeBSD uses today.
304 if (pool_guid != 0) {
305 printf("Trying ZFS pool\n");
306 if (probe_zfs_currdev(pool_guid))
309 #endif /* EFI_ZFS_BOOT */
312 * Try to find the block device by its handle based on the
313 * image we're booting. If we can't find a sane partition,
314 * search all the other partitions of the disk. We do not
315 * search other disks because it's a violation of the UEFI
316 * boot protocol to do so. We fail and let UEFI go on to
317 * the next candidate.
319 dp = efiblk_get_pdinfo_by_handle(img->DeviceHandle);
321 text = efi_devpath_name(dp->pd_devpath);
323 printf("Trying ESP: %S\n", text);
324 efi_free_devpath_name(text);
326 set_currdev_pdinfo(dp);
327 if (sanity_check_currdev())
329 if (dp->pd_parent != NULL) {
331 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) {
333 * Roll up the ZFS special case
334 * for those partitions that have
337 if (try_as_currdev(dp, pp))
342 printf("Can't find device by handle\n");
346 * Try the device handle from our loaded image first. If that
347 * fails, use the device path from the loaded image and see if
348 * any of the nodes in that path match one of the enumerated
349 * handles. Currently, this handle list is only for netboot.
351 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) {
352 set_currdev_devsw(dev, unit);
353 if (sanity_check_currdev())
358 devpath = efi_lookup_image_devpath(IH);
359 while (devpath != NULL) {
360 h = efi_devpath_handle(devpath);
367 if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) {
368 set_currdev_devsw(dev, unit);
369 if (sanity_check_currdev())
373 devpath = efi_lookup_devpath(h);
374 if (devpath != NULL) {
375 copy = efi_devpath_trim(devpath);
385 interactive_interrupt(const char *msg)
387 time_t now, then, last;
390 now = then = getsecs();
392 if (fail_timeout == -2) /* Always break to OK */
394 if (fail_timeout == -1) /* Never break to OK */
398 printf("press any key to interrupt reboot in %d seconds\r",
399 fail_timeout - (int)(now - then));
403 /* XXX no pause or timeout wait for char */
407 } while (now - then < fail_timeout);
412 parse_args(int argc, CHAR16 *argv[])
419 * Parse the args to set the console settings, etc
420 * boot1.efi passes these in, if it can read /boot.config or /boot/config
421 * or iPXE may be setup to pass these in. Or the optional argument in the
422 * boot environment was used to pass these arguments in (in which case
423 * neither /boot.config nor /boot/config are consulted).
425 * Loop through the args, and for each one that contains an '=' that is
426 * not the first character, add it to the environment. This allows
427 * loader and kernel env vars to be passed on the command line. Convert
428 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied (though this
429 * method is flawed for non-ASCII characters).
432 for (i = 1; i < argc; i++) {
433 cpy16to8(argv[i], var, sizeof(var));
434 howto |= boot_parse_arg(var);
441 * Parse ConOut (the list of consoles active) and see if we can find a
442 * serial port and/or a video port. It would be nice to also walk the
443 * ACPI name space to map the UID for the serial port to a port. The
444 * latter is especially hard.
447 parse_uefi_con_out(void)
450 int vid_seen = 0, com_seen = 0, seen = 0;
453 EFI_DEVICE_PATH *node;
454 ACPI_HID_DEVICE_PATH *acpi;
455 UART_DEVICE_PATH *uart;
460 rv = efi_global_getenv("ConOut", buf, &sz);
461 if (rv != EFI_SUCCESS)
464 node = (EFI_DEVICE_PATH *)buf;
465 while ((char *)node < ep) {
467 if (DevicePathType(node) == ACPI_DEVICE_PATH &&
468 DevicePathSubType(node) == ACPI_DP) {
469 /* Check for Serial node */
471 if (EISA_ID_TO_NUM(acpi->HID) == 0x501)
473 } else if (DevicePathType(node) == MESSAGING_DEVICE_PATH &&
474 DevicePathSubType(node) == MSG_UART_DP) {
478 snprintf(bd, sizeof(bd), "%d", uart->BaudRate);
479 setenv("efi_com_speed", bd, 1);
480 } else if (DevicePathType(node) == ACPI_DEVICE_PATH &&
481 DevicePathSubType(node) == ACPI_ADR_DP) {
482 /* Check for AcpiAdr() Node for video */
484 } else if (DevicePathType(node) == HARDWARE_DEVICE_PATH &&
485 DevicePathSubType(node) == HW_PCI_DP) {
487 * Note, vmware fusion has a funky console device
488 * PciRoot(0x0)/Pci(0xf,0x0)
489 * which we can only detect at the end since we also
491 * PciRoot(0x0)/Pci(0x1f,0x0)/Serial(0x1)
492 * so only match it if it's last.
496 node = NextDevicePathNode(node); /* Skip the end node */
498 if (pci_pending && vid_seen == 0)
502 * Truth table for RB_MULTIPLE | RB_SERIAL
504 * 0 Use only video console
505 * RB_SERIAL Use only serial console
506 * RB_MULTIPLE Use both video and serial console
507 * (but video is primary so gets rc messages)
508 * both Use both video and serial console
509 * (but serial is primary so gets rc messages)
511 * Try to honor this as best we can. If only one of serial / video
512 * found, then use that. Otherwise, use the first one we found.
513 * This also implies if we found nothing, default to video.
516 if (vid_seen && com_seen) {
518 if (com_seen < vid_seen)
527 main(int argc, CHAR16 *argv[])
530 int howto, i, uhowto;
534 EFI_DEVICE_PATH *imgpath;
539 UINT16 boot_order[100];
540 EFI_LOADED_IMAGE *img;
542 archsw.arch_autoload = efi_autoload;
543 archsw.arch_getdev = efi_getdev;
544 archsw.arch_copyin = efi_copyin;
545 archsw.arch_copyout = efi_copyout;
546 archsw.arch_readin = efi_readin;
548 /* Note this needs to be set before ZFS init. */
549 archsw.arch_zfs_probe = efi_zfs_probe;
552 /* Get our loaded image protocol interface structure. */
553 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
556 /* Tell ZFS probe code where we booted from */
557 efizfs_set_preferred(img->DeviceHandle);
559 /* Init the time source */
562 has_kbd = has_keyboard();
565 * XXX Chicken-and-egg problem; we want to have console output
566 * early, but some console attributes may depend on reading from
567 * eg. the boot device, which we can't do yet. We can use
568 * printf() etc. once this is done.
570 setenv("console", "efi", 1);
574 * Initialise the block cache. Set the upper limit.
576 bcache_init(32768, 512);
578 howto = parse_args(argc, argv);
579 if (!has_kbd && (howto & RB_PROBE))
580 howto |= RB_SERIAL | RB_MULTIPLE;
582 uhowto = parse_uefi_con_out();
585 * We now have two notions of console. howto should be viewed as
586 * overrides. If console is already set, don't set it again.
589 #define SERIAL_ONLY RB_SERIAL
590 #define VID_SER_BOTH RB_MULTIPLE
591 #define SER_VID_BOTH (RB_SERIAL | RB_MULTIPLE)
592 #define CON_MASK (RB_SERIAL | RB_MULTIPLE)
593 if (strcmp(getenv("console"), "efi") == 0) {
594 if ((howto & CON_MASK) == 0) {
595 /* No override, uhowto is controlling and efi cons is perfect */
596 howto = howto | (uhowto & CON_MASK);
597 setenv("console", "efi", 1);
598 } else if ((howto & CON_MASK) == (uhowto & CON_MASK)) {
599 /* override matches what UEFI told us, efi console is perfect */
600 setenv("console", "efi", 1);
601 } else if ((uhowto & (CON_MASK)) != 0) {
603 * We detected a serial console on ConOut. All possible
604 * overrides include serial. We can't really override what efi
605 * gives us, so we use it knowing it's the best choice.
607 setenv("console", "efi", 1);
610 * We detected some kind of serial in the override, but ConOut
611 * has no serial, so we have to sort out which case it really is.
613 switch (howto & CON_MASK) {
615 setenv("console", "comconsole", 1);
618 setenv("console", "efi comconsole", 1);
621 setenv("console", "comconsole efi", 1);
623 /* case VIDEO_ONLY can't happen -- it's the first if above */
628 * howto is set now how we want to export the flags to the kernel, so
629 * set the env based on it.
631 boot_howto_to_env(howto);
633 if (efi_copy_init()) {
634 printf("failed to allocate staging area\n");
635 return (EFI_BUFFER_TOO_SMALL);
638 if ((s = getenv("fail_timeout")) != NULL)
639 fail_timeout = strtol(s, NULL, 10);
642 * Scan the BLOCK IO MEDIA handles then
643 * march through the device switch probing for things.
645 if ((i = efipart_inithandles()) == 0) {
646 for (i = 0; devsw[i] != NULL; i++)
647 if (devsw[i]->dv_init != NULL)
648 (devsw[i]->dv_init)();
650 printf("efipart_inithandles failed %d, expect failures", i);
652 printf("%s\n", bootprog_info);
653 printf(" Command line arguments:");
654 for (i = 0; i < argc; i++)
655 printf(" %S", argv[i]);
658 printf(" EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
659 ST->Hdr.Revision & 0xffff);
660 printf(" EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor,
661 ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
664 /* Determine the devpath of our image so we can prefer it. */
665 text = efi_devpath_name(img->FilePath);
667 printf(" Load Path: %S\n", text);
668 efi_setenv_freebsd_wcs("LoaderPath", text);
669 efi_free_devpath_name(text);
672 status = BS->HandleProtocol(img->DeviceHandle, &devid, (void **)&imgpath);
673 if (status == EFI_SUCCESS) {
674 text = efi_devpath_name(imgpath);
676 printf(" Load Device: %S\n", text);
677 efi_setenv_freebsd_wcs("LoaderDev", text);
678 efi_free_devpath_name(text);
683 sz = sizeof(boot_current);
684 efi_global_getenv("BootCurrent", &boot_current, &sz);
685 printf(" BootCurrent: %04x\n", boot_current);
687 sz = sizeof(boot_order);
688 efi_global_getenv("BootOrder", &boot_order, &sz);
689 printf(" BootOrder:");
690 for (i = 0; i < sz / sizeof(boot_order[0]); i++)
691 printf(" %04x%s", boot_order[i],
692 boot_order[i] == boot_current ? "[*]" : "");
696 * Disable the watchdog timer. By default the boot manager sets
697 * the timer to 5 minutes before invoking a boot option. If we
698 * want to return to the boot manager, we have to disable the
699 * watchdog timer and since we're an interactive program, we don't
700 * want to wait until the user types "quit". The timer may have
701 * fired by then. We don't care if this fails. It does not prevent
702 * normal functioning in any way...
704 BS->SetWatchdogTimer(0, 0, 0, NULL);
707 * Try and find a good currdev based on the image that was booted.
708 * It might be desirable here to have a short pause to allow falling
709 * through to the boot loader instead of returning instantly to follow
710 * the boot protocol and also allow an escape hatch for users wishing
711 * to try something different.
713 if (find_currdev(img) != 0)
714 if (!interactive_interrupt("Failed to find bootable partition"))
715 return (EFI_NOT_FOUND);
717 efi_init_environment();
719 #if !defined(__arm__)
720 for (k = 0; k < ST->NumberOfTableEntries; k++) {
721 guid = &ST->ConfigurationTable[k].VendorGuid;
722 if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) {
725 snprintf(buf, sizeof(buf), "%p",
726 ST->ConfigurationTable[k].VendorTable);
727 setenv("hint.smbios.0.mem", buf, 1);
728 smbios_detect(ST->ConfigurationTable[k].VendorTable);
734 interact(); /* doesn't return */
736 return (EFI_SUCCESS); /* keep compiler happy */
739 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
742 command_reboot(int argc, char *argv[])
746 for (i = 0; devsw[i] != NULL; ++i)
747 if (devsw[i]->dv_cleanup != NULL)
748 (devsw[i]->dv_cleanup)();
750 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
756 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
759 command_quit(int argc, char *argv[])
765 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
768 command_memmap(int argc, char *argv[])
771 EFI_MEMORY_DESCRIPTOR *map, *p;
777 static char *types[] = {
783 "RuntimeServicesCode",
784 "RuntimeServicesData",
785 "ConventionalMemory",
790 "MemoryMappedIOPortSpace",
795 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
796 if (status != EFI_BUFFER_TOO_SMALL) {
797 printf("Can't determine memory map size\n");
801 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
802 if (EFI_ERROR(status)) {
803 printf("Can't read memory map\n");
808 snprintf(line, sizeof(line), "%23s %12s %12s %8s %4s\n",
809 "Type", "Physical", "Virtual", "#Pages", "Attr");
811 if (pager_output(line)) {
816 for (i = 0, p = map; i < ndesc;
817 i++, p = NextMemoryDescriptor(p, dsz)) {
818 printf("%23s %012jx %012jx %08jx ", types[p->Type],
819 (uintmax_t)p->PhysicalStart, (uintmax_t)p->VirtualStart,
820 (uintmax_t)p->NumberOfPages);
821 if (p->Attribute & EFI_MEMORY_UC)
823 if (p->Attribute & EFI_MEMORY_WC)
825 if (p->Attribute & EFI_MEMORY_WT)
827 if (p->Attribute & EFI_MEMORY_WB)
829 if (p->Attribute & EFI_MEMORY_UCE)
831 if (p->Attribute & EFI_MEMORY_WP)
833 if (p->Attribute & EFI_MEMORY_RP)
835 if (p->Attribute & EFI_MEMORY_XP)
837 if (pager_output("\n"))
845 COMMAND_SET(configuration, "configuration", "print configuration tables",
846 command_configuration);
849 guid_to_string(EFI_GUID *guid)
853 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
854 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
855 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
856 guid->Data4[5], guid->Data4[6], guid->Data4[7]);
861 command_configuration(int argc, char *argv[])
866 snprintf(line, sizeof(line), "NumberOfTableEntries=%lu\n",
867 (unsigned long)ST->NumberOfTableEntries);
869 if (pager_output(line)) {
874 for (i = 0; i < ST->NumberOfTableEntries; i++) {
878 guid = &ST->ConfigurationTable[i].VendorGuid;
879 if (!memcmp(guid, &mps, sizeof(EFI_GUID)))
881 else if (!memcmp(guid, &acpi, sizeof(EFI_GUID)))
882 printf("ACPI Table");
883 else if (!memcmp(guid, &acpi20, sizeof(EFI_GUID)))
884 printf("ACPI 2.0 Table");
885 else if (!memcmp(guid, &smbios, sizeof(EFI_GUID)))
886 printf("SMBIOS Table %p",
887 ST->ConfigurationTable[i].VendorTable);
888 else if (!memcmp(guid, &smbios3, sizeof(EFI_GUID)))
889 printf("SMBIOS3 Table");
890 else if (!memcmp(guid, &dxe, sizeof(EFI_GUID)))
892 else if (!memcmp(guid, &hoblist, sizeof(EFI_GUID)))
893 printf("HOB List Table");
894 else if (!memcmp(guid, &lzmadecomp, sizeof(EFI_GUID)))
895 printf("LZMA Compression");
896 else if (!memcmp(guid, &mpcore, sizeof(EFI_GUID)))
897 printf("ARM MpCore Information Table");
898 else if (!memcmp(guid, &esrt, sizeof(EFI_GUID)))
899 printf("ESRT Table");
900 else if (!memcmp(guid, &memtype, sizeof(EFI_GUID)))
901 printf("Memory Type Information Table");
902 else if (!memcmp(guid, &debugimg, sizeof(EFI_GUID)))
903 printf("Debug Image Info Table");
904 else if (!memcmp(guid, &fdtdtb, sizeof(EFI_GUID)))
907 printf("Unknown Table (%s)", guid_to_string(guid));
908 snprintf(line, sizeof(line), " at %p\n",
909 ST->ConfigurationTable[i].VendorTable);
910 if (pager_output(line))
919 COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
922 command_mode(int argc, char *argv[])
930 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
931 extern void HO(void);
936 mode = strtol(argv[1], &cp, 0);
938 printf("Invalid mode\n");
941 status = conout->QueryMode(conout, mode, &cols, &rows);
942 if (EFI_ERROR(status)) {
943 printf("invalid mode %d\n", mode);
946 status = conout->SetMode(conout, mode);
947 if (EFI_ERROR(status)) {
948 printf("couldn't set mode %d\n", mode);
951 sprintf(rowenv, "%u", (unsigned)rows);
952 setenv("LINES", rowenv, 1);
953 HO(); /* set cursor */
957 printf("Current mode: %d\n", conout->Mode->Mode);
958 for (i = 0; i <= conout->Mode->MaxMode; i++) {
959 status = conout->QueryMode(conout, i, &cols, &rows);
960 if (EFI_ERROR(status))
962 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
967 printf("Select a mode with the command \"mode <number>\"\n");
972 #ifdef LOADER_FDT_SUPPORT
973 extern int command_fdt_internal(int argc, char *argv[]);
976 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
977 * and declaring it as extern is in contradiction with COMMAND_SET() macro
978 * (which uses static pointer), we're defining wrapper function, which
979 * calls the proper fdt handling routine.
982 command_fdt(int argc, char *argv[])
985 return (command_fdt_internal(argc, argv));
988 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
992 * Chain load another efi loader.
995 command_chain(int argc, char *argv[])
997 EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL;
998 EFI_HANDLE loaderhandle;
999 EFI_LOADED_IMAGE *loaded_image;
1002 struct devdesc *dev;
1008 command_errmsg = "wrong number of arguments";
1014 if ((fd = open(name, O_RDONLY)) < 0) {
1015 command_errmsg = "no such file";
1019 if (fstat(fd, &st) < -1) {
1020 command_errmsg = "stat failed";
1025 status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf);
1026 if (status != EFI_SUCCESS) {
1027 command_errmsg = "failed to allocate buffer";
1031 if (read(fd, buf, st.st_size) != st.st_size) {
1032 command_errmsg = "error while reading the file";
1033 (void)BS->FreePool(buf);
1038 status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle);
1039 (void)BS->FreePool(buf);
1040 if (status != EFI_SUCCESS) {
1041 command_errmsg = "LoadImage failed";
1044 status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID,
1045 (void **)&loaded_image);
1051 for (i = 2; i < argc; i++)
1052 len += strlen(argv[i]) + 1;
1054 len *= sizeof (*argp);
1055 loaded_image->LoadOptions = argp = malloc (len);
1056 loaded_image->LoadOptionsSize = len;
1057 for (i = 2; i < argc; i++) {
1058 char *ptr = argv[i];
1060 *(argp++) = *(ptr++);
1066 if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) {
1068 struct zfs_devdesc *z_dev;
1070 struct disk_devdesc *d_dev;
1073 switch (dev->d_dev->dv_type) {
1076 z_dev = (struct zfs_devdesc *)dev;
1077 loaded_image->DeviceHandle =
1078 efizfs_get_handle_by_guid(z_dev->pool_guid);
1082 loaded_image->DeviceHandle =
1083 efi_find_handle(dev->d_dev, dev->d_unit);
1086 hd = efiblk_get_pdinfo(dev);
1087 if (STAILQ_EMPTY(&hd->pd_part)) {
1088 loaded_image->DeviceHandle = hd->pd_handle;
1091 d_dev = (struct disk_devdesc *)dev;
1092 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
1094 * d_partition should be 255
1096 if (pd->pd_unit == (uint32_t)d_dev->d_slice) {
1097 loaded_image->DeviceHandle =
1107 status = BS->StartImage(loaderhandle, NULL, NULL);
1108 if (status != EFI_SUCCESS) {
1109 command_errmsg = "StartImage failed";
1110 free(loaded_image->LoadOptions);
1111 loaded_image->LoadOptions = NULL;
1112 status = BS->UnloadImage(loaded_image);
1116 return (CMD_ERROR); /* not reached */
1119 COMMAND_SET(chain, "chain", "chain load file", command_chain);