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$");
31 #include <sys/param.h>
32 #include <sys/reboot.h>
45 #include <bootstrap.h>
52 #include "loader_efi.h"
54 extern char bootprog_info[];
56 struct arch_switch archsw; /* MI/MD interface boundary */
58 EFI_GUID acpi = ACPI_TABLE_GUID;
59 EFI_GUID acpi20 = ACPI_20_TABLE_GUID;
60 EFI_GUID devid = DEVICE_PATH_PROTOCOL;
61 EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
62 EFI_GUID mps = MPS_TABLE_GUID;
63 EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL;
64 EFI_GUID smbios = SMBIOS_TABLE_GUID;
65 EFI_GUID dxe = DXE_SERVICES_TABLE_GUID;
66 EFI_GUID hoblist = HOB_LIST_TABLE_GUID;
67 EFI_GUID memtype = MEMORY_TYPE_INFORMATION_TABLE_GUID;
68 EFI_GUID debugimg = DEBUG_IMAGE_INFO_TABLE_GUID;
69 EFI_GUID fdtdtb = FDT_TABLE_GUID;
70 EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL;
73 static void efi_zfs_probe(void);
74 static uint64_t pool_guid;
81 EFI_DEVICE_PATH *path;
82 EFI_HANDLE *hin, *hin_end, *walker;
87 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and
88 * do the typical dance to get the right sized buffer.
92 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 0);
93 if (status == EFI_BUFFER_TOO_SMALL) {
94 hin = (EFI_HANDLE *)malloc(sz);
95 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz,
97 if (EFI_ERROR(status))
100 if (EFI_ERROR(status))
104 * Look at each of the handles. If it supports the device path protocol,
105 * use it to get the device path for this handle. Then see if that
106 * device path matches either the USB device path for keyboards or the
107 * legacy device path for keyboards.
109 hin_end = &hin[sz / sizeof(*hin)];
110 for (walker = hin; walker < hin_end; walker++) {
111 status = BS->HandleProtocol(*walker, &devid, (VOID **)&path);
112 if (EFI_ERROR(status))
115 while (!IsDevicePathEnd(path)) {
117 * Check for the ACPI keyboard node. All PNP3xx nodes
118 * are keyboards of different flavors. Note: It is
119 * unclear of there's always a keyboard node when
120 * there's a keyboard controller, or if there's only one
121 * when a keyboard is detected at boot.
123 if (DevicePathType(path) == ACPI_DEVICE_PATH &&
124 (DevicePathSubType(path) == ACPI_DP ||
125 DevicePathSubType(path) == ACPI_EXTENDED_DP)) {
126 ACPI_HID_DEVICE_PATH *acpi;
128 acpi = (ACPI_HID_DEVICE_PATH *)(void *)path;
129 if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 0x300 &&
130 (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) {
135 * Check for USB keyboard node, if present. Unlike a
136 * PS/2 keyboard, these definitely only appear when
137 * connected to the system.
139 } else if (DevicePathType(path) == MESSAGING_DEVICE_PATH &&
140 DevicePathSubType(path) == MSG_USB_CLASS_DP) {
141 USB_CLASS_DEVICE_PATH *usb;
143 usb = (USB_CLASS_DEVICE_PATH *)(void *)path;
144 if (usb->DeviceClass == 3 && /* HID */
145 usb->DeviceSubClass == 1 && /* Boot devices */
146 usb->DeviceProtocol == 1) { /* Boot keyboards */
151 path = NextDevicePathNode(path);
160 set_devdesc_currdev(struct devsw *dev, int unit)
162 struct devdesc currdev;
166 currdev.d_type = currdev.d_dev->dv_type;
167 currdev.d_unit = unit;
168 currdev.d_opendata = NULL;
169 devname = efi_fmtdev(&currdev);
171 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
173 env_setenv("loaddev", EV_VOLATILE, devname, env_noset, env_nounset);
177 find_currdev(EFI_LOADED_IMAGE *img)
179 pdinfo_list_t *pdi_list;
181 EFI_DEVICE_PATH *devpath, *copy;
189 /* Did efi_zfs_probe() detect the boot pool? */
190 if (pool_guid != 0) {
191 struct zfs_devdesc currdev;
193 currdev.d_dev = &zfs_dev;
195 currdev.d_type = currdev.d_dev->dv_type;
196 currdev.d_opendata = NULL;
197 currdev.pool_guid = pool_guid;
198 currdev.root_guid = 0;
199 devname = efi_fmtdev(&currdev);
201 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
203 env_setenv("loaddev", EV_VOLATILE, devname, env_noset,
205 init_zfs_bootenv(devname);
208 #endif /* EFI_ZFS_BOOT */
210 /* We have device lists for hd, cd, fd, walk them all. */
211 pdi_list = efiblk_get_pdinfo_list(&efipart_hddev);
212 STAILQ_FOREACH(dp, pdi_list, pd_link) {
213 struct disk_devdesc currdev;
215 currdev.d_dev = &efipart_hddev;
216 currdev.d_type = currdev.d_dev->dv_type;
217 currdev.d_unit = dp->pd_unit;
218 currdev.d_opendata = NULL;
219 currdev.d_slice = -1;
220 currdev.d_partition = -1;
222 if (dp->pd_handle == img->DeviceHandle) {
223 devname = efi_fmtdev(&currdev);
225 env_setenv("currdev", EV_VOLATILE, devname,
226 efi_setcurrdev, env_nounset);
227 env_setenv("loaddev", EV_VOLATILE, devname,
228 env_noset, env_nounset);
231 /* Assuming GPT partitioning. */
232 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) {
233 if (pp->pd_handle == img->DeviceHandle) {
234 currdev.d_slice = pp->pd_unit;
235 currdev.d_partition = 255;
236 devname = efi_fmtdev(&currdev);
238 env_setenv("currdev", EV_VOLATILE, devname,
239 efi_setcurrdev, env_nounset);
240 env_setenv("loaddev", EV_VOLATILE, devname,
241 env_noset, env_nounset);
247 pdi_list = efiblk_get_pdinfo_list(&efipart_cddev);
248 STAILQ_FOREACH(dp, pdi_list, pd_link) {
249 if (dp->pd_handle == img->DeviceHandle ||
250 dp->pd_alias == img->DeviceHandle) {
251 set_devdesc_currdev(&efipart_cddev, dp->pd_unit);
256 pdi_list = efiblk_get_pdinfo_list(&efipart_fddev);
257 STAILQ_FOREACH(dp, pdi_list, pd_link) {
258 if (dp->pd_handle == img->DeviceHandle) {
259 set_devdesc_currdev(&efipart_fddev, dp->pd_unit);
265 * Try the device handle from our loaded image first. If that
266 * fails, use the device path from the loaded image and see if
267 * any of the nodes in that path match one of the enumerated
270 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) {
271 set_devdesc_currdev(dev, unit);
276 devpath = efi_lookup_image_devpath(IH);
277 while (devpath != NULL) {
278 h = efi_devpath_handle(devpath);
285 if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) {
286 set_devdesc_currdev(dev, unit);
290 devpath = efi_lookup_devpath(h);
291 if (devpath != NULL) {
292 copy = efi_devpath_trim(devpath);
302 main(int argc, CHAR16 *argv[])
305 EFI_LOADED_IMAGE *img;
307 int i, j, vargood, howto;
312 archsw.arch_autoload = efi_autoload;
313 archsw.arch_getdev = efi_getdev;
314 archsw.arch_copyin = efi_copyin;
315 archsw.arch_copyout = efi_copyout;
316 archsw.arch_readin = efi_readin;
318 /* Note this needs to be set before ZFS init. */
319 archsw.arch_zfs_probe = efi_zfs_probe;
322 /* Init the time source */
325 has_kbd = has_keyboard();
328 * XXX Chicken-and-egg problem; we want to have console output
329 * early, but some console attributes may depend on reading from
330 * eg. the boot device, which we can't do yet. We can use
331 * printf() etc. once this is done.
336 * Initialise the block cache. Set the upper limit.
338 bcache_init(32768, 512);
341 * Parse the args to set the console settings, etc
342 * boot1.efi passes these in, if it can read /boot.config or /boot/config
343 * or iPXE may be setup to pass these in.
345 * Loop through the args, and for each one that contains an '=' that is
346 * not the first character, add it to the environment. This allows
347 * loader and kernel env vars to be passed on the command line. Convert
348 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied.
351 for (i = 1; i < argc; i++) {
352 if (argv[i][0] == '-') {
353 for (j = 1; argv[i][j] != 0; j++) {
365 howto |= RB_MULTIPLE;
378 howto |= RB_SERIAL | RB_MULTIPLE;
381 howto |= RB_DFLTROOT;
387 if (argv[i][j + 1] == 0) {
389 setenv("comconsole_speed", "115200", 1);
391 cpy16to8(&argv[i + 1][0], var,
393 setenv("comconsole_speed", var, 1);
398 cpy16to8(&argv[i][j + 1], var,
400 setenv("comconsole_speed", var, 1);
410 for (j = 0; argv[i][j] != 0; j++) {
411 if (j == sizeof(var)) {
415 if (j > 0 && argv[i][j] == '=')
417 var[j] = (char)argv[i][j];
425 for (i = 0; howto_names[i].ev != NULL; i++)
426 if (howto & howto_names[i].mask)
427 setenv(howto_names[i].ev, "YES", 1);
428 if (howto & RB_MULTIPLE) {
429 if (howto & RB_SERIAL)
430 setenv("console", "comconsole efi" , 1);
432 setenv("console", "efi comconsole" , 1);
433 } else if (howto & RB_SERIAL) {
434 setenv("console", "comconsole" , 1);
437 if (efi_copy_init()) {
438 printf("failed to allocate staging area\n");
439 return (EFI_BUFFER_TOO_SMALL);
443 * March through the device switch probing for things.
445 for (i = 0; devsw[i] != NULL; i++)
446 if (devsw[i]->dv_init != NULL)
447 (devsw[i]->dv_init)();
449 /* Get our loaded image protocol interface structure. */
450 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
452 printf("Command line arguments:");
453 for (i = 0; i < argc; i++)
454 printf(" %S", argv[i]);
457 printf("Image base: 0x%lx\n", (u_long)img->ImageBase);
458 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
459 ST->Hdr.Revision & 0xffff);
460 printf("EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor,
461 ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
463 printf("\n%s", bootprog_info);
466 * Disable the watchdog timer. By default the boot manager sets
467 * the timer to 5 minutes before invoking a boot option. If we
468 * want to return to the boot manager, we have to disable the
469 * watchdog timer and since we're an interactive program, we don't
470 * want to wait until the user types "quit". The timer may have
471 * fired by then. We don't care if this fails. It does not prevent
472 * normal functioning in any way...
474 BS->SetWatchdogTimer(0, 0, 0, NULL);
476 if (find_currdev(img) != 0)
477 return (EFI_NOT_FOUND);
479 efi_init_environment();
480 setenv("LINES", "24", 1); /* optional */
482 for (k = 0; k < ST->NumberOfTableEntries; k++) {
483 guid = &ST->ConfigurationTable[k].VendorGuid;
484 if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) {
485 snprintf(buf, sizeof(buf), "%p",
486 ST->ConfigurationTable[k].VendorTable);
487 setenv("hint.smbios.0.mem", buf, 1);
488 smbios_detect(ST->ConfigurationTable[k].VendorTable);
493 interact(NULL); /* doesn't return */
495 return (EFI_SUCCESS); /* keep compiler happy */
498 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
501 command_reboot(int argc, char *argv[])
505 for (i = 0; devsw[i] != NULL; ++i)
506 if (devsw[i]->dv_cleanup != NULL)
507 (devsw[i]->dv_cleanup)();
509 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 23,
510 (CHAR16 *)"Reboot from the loader");
516 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
519 command_quit(int argc, char *argv[])
525 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
528 command_memmap(int argc, char *argv[])
531 EFI_MEMORY_DESCRIPTOR *map, *p;
537 static char *types[] = {
543 "RuntimeServicesCode",
544 "RuntimeServicesData",
545 "ConventionalMemory",
550 "MemoryMappedIOPortSpace",
555 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
556 if (status != EFI_BUFFER_TOO_SMALL) {
557 printf("Can't determine memory map size\n");
561 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
562 if (EFI_ERROR(status)) {
563 printf("Can't read memory map\n");
568 snprintf(line, sizeof(line), "%23s %12s %12s %8s %4s\n",
569 "Type", "Physical", "Virtual", "#Pages", "Attr");
571 if (pager_output(line)) {
576 for (i = 0, p = map; i < ndesc;
577 i++, p = NextMemoryDescriptor(p, dsz)) {
578 printf("%23s %012jx %012jx %08jx ", types[p->Type],
579 (uintmax_t)p->PhysicalStart, (uintmax_t)p->VirtualStart,
580 (uintmax_t)p->NumberOfPages);
581 if (p->Attribute & EFI_MEMORY_UC)
583 if (p->Attribute & EFI_MEMORY_WC)
585 if (p->Attribute & EFI_MEMORY_WT)
587 if (p->Attribute & EFI_MEMORY_WB)
589 if (p->Attribute & EFI_MEMORY_UCE)
591 if (p->Attribute & EFI_MEMORY_WP)
593 if (p->Attribute & EFI_MEMORY_RP)
595 if (p->Attribute & EFI_MEMORY_XP)
597 if (pager_output("\n"))
605 COMMAND_SET(configuration, "configuration", "print configuration tables",
606 command_configuration);
609 guid_to_string(EFI_GUID *guid)
613 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
614 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
615 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
616 guid->Data4[5], guid->Data4[6], guid->Data4[7]);
621 command_configuration(int argc, char *argv[])
626 snprintf(line, sizeof(line), "NumberOfTableEntries=%lu\n",
627 (unsigned long)ST->NumberOfTableEntries);
629 if (pager_output(line)) {
634 for (i = 0; i < ST->NumberOfTableEntries; i++) {
638 guid = &ST->ConfigurationTable[i].VendorGuid;
639 if (!memcmp(guid, &mps, sizeof(EFI_GUID)))
641 else if (!memcmp(guid, &acpi, sizeof(EFI_GUID)))
642 printf("ACPI Table");
643 else if (!memcmp(guid, &acpi20, sizeof(EFI_GUID)))
644 printf("ACPI 2.0 Table");
645 else if (!memcmp(guid, &smbios, sizeof(EFI_GUID)))
646 printf("SMBIOS Table %p",
647 ST->ConfigurationTable[i].VendorTable);
648 else if (!memcmp(guid, &dxe, sizeof(EFI_GUID)))
650 else if (!memcmp(guid, &hoblist, sizeof(EFI_GUID)))
651 printf("HOB List Table");
652 else if (!memcmp(guid, &memtype, sizeof(EFI_GUID)))
653 printf("Memory Type Information Table");
654 else if (!memcmp(guid, &debugimg, sizeof(EFI_GUID)))
655 printf("Debug Image Info Table");
656 else if (!memcmp(guid, &fdtdtb, sizeof(EFI_GUID)))
659 printf("Unknown Table (%s)", guid_to_string(guid));
660 snprintf(line, sizeof(line), " at %p\n",
661 ST->ConfigurationTable[i].VendorTable);
662 if (pager_output(line))
671 COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
674 command_mode(int argc, char *argv[])
682 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
683 extern void HO(void);
688 mode = strtol(argv[1], &cp, 0);
690 printf("Invalid mode\n");
693 status = conout->QueryMode(conout, mode, &cols, &rows);
694 if (EFI_ERROR(status)) {
695 printf("invalid mode %d\n", mode);
698 status = conout->SetMode(conout, mode);
699 if (EFI_ERROR(status)) {
700 printf("couldn't set mode %d\n", mode);
703 sprintf(rowenv, "%u", (unsigned)rows);
704 setenv("LINES", rowenv, 1);
705 HO(); /* set cursor */
709 printf("Current mode: %d\n", conout->Mode->Mode);
710 for (i = 0; i <= conout->Mode->MaxMode; i++) {
711 status = conout->QueryMode(conout, i, &cols, &rows);
712 if (EFI_ERROR(status))
714 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
719 printf("Select a mode with the command \"mode <number>\"\n");
725 COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
729 command_lszfs(int argc, char *argv[])
734 command_errmsg = "wrong number of arguments";
738 err = zfs_list(argv[1]);
740 command_errmsg = strerror(err);
746 COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
750 command_reloadbe(int argc, char *argv[])
756 command_errmsg = "wrong number of arguments";
761 err = zfs_bootenv(argv[1]);
763 root = getenv("zfs_be_root");
767 err = zfs_bootenv(root);
771 command_errmsg = strerror(err);
779 #ifdef LOADER_FDT_SUPPORT
780 extern int command_fdt_internal(int argc, char *argv[]);
783 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
784 * and declaring it as extern is in contradiction with COMMAND_SET() macro
785 * (which uses static pointer), we're defining wrapper function, which
786 * calls the proper fdt handling routine.
789 command_fdt(int argc, char *argv[])
792 return (command_fdt_internal(argc, argv));
795 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
803 pdinfo_t *hd, *pd = NULL;
804 EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
805 EFI_LOADED_IMAGE *img;
806 EFI_HANDLE boot_disk = NULL;
807 char devname[SPECNAMELEN + 1];
808 uint64_t *guidp = NULL;
810 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
812 /* Find the handle for the boot disk. */
813 hdi = efiblk_get_pdinfo_list(&efipart_hddev);
814 STAILQ_FOREACH(hd, hdi, pd_link) {
815 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
816 if (pd->pd_handle == img->DeviceHandle)
817 boot_disk = hd->pd_handle;
822 * We provide non-NULL guid pointer if the disk was used for boot,
823 * and reset after the first found pool.
824 * Technically this solution is not very correct, we assume the boot
825 * pool is the first pool on this disk.
828 STAILQ_FOREACH(hd, hdi, pd_link) {
829 if (hd->pd_handle == boot_disk)
832 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
833 snprintf(devname, sizeof(devname), "%s%dp%d:",
834 efipart_hddev.dv_name, hd->pd_unit, pd->pd_unit);
835 (void) zfs_probe_dev(devname, guidp);
836 if (guidp != NULL && pool_guid != 0)