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$");
32 #include <sys/param.h>
33 #include <sys/reboot.h>
46 #include <bootstrap.h>
55 #include "loader_efi.h"
57 extern char bootprog_info[];
59 struct arch_switch archsw; /* MI/MD interface boundary */
61 EFI_GUID acpi = ACPI_TABLE_GUID;
62 EFI_GUID acpi20 = ACPI_20_TABLE_GUID;
63 EFI_GUID devid = DEVICE_PATH_PROTOCOL;
64 EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
65 EFI_GUID mps = MPS_TABLE_GUID;
66 EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL;
67 EFI_GUID smbios = SMBIOS_TABLE_GUID;
68 EFI_GUID dxe = DXE_SERVICES_TABLE_GUID;
69 EFI_GUID hoblist = HOB_LIST_TABLE_GUID;
70 EFI_GUID memtype = MEMORY_TYPE_INFORMATION_TABLE_GUID;
71 EFI_GUID debugimg = DEBUG_IMAGE_INFO_TABLE_GUID;
72 EFI_GUID fdtdtb = FDT_TABLE_GUID;
73 EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL;
75 static EFI_LOADED_IMAGE *img;
79 efi_zfs_is_preferred(EFI_HANDLE *h)
81 return (h == img->DeviceHandle);
89 EFI_DEVICE_PATH *path;
90 EFI_HANDLE *hin, *hin_end, *walker;
95 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and
96 * do the typical dance to get the right sized buffer.
100 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 0);
101 if (status == EFI_BUFFER_TOO_SMALL) {
102 hin = (EFI_HANDLE *)malloc(sz);
103 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz,
105 if (EFI_ERROR(status))
108 if (EFI_ERROR(status))
112 * Look at each of the handles. If it supports the device path protocol,
113 * use it to get the device path for this handle. Then see if that
114 * device path matches either the USB device path for keyboards or the
115 * legacy device path for keyboards.
117 hin_end = &hin[sz / sizeof(*hin)];
118 for (walker = hin; walker < hin_end; walker++) {
119 status = BS->HandleProtocol(*walker, &devid, (VOID **)&path);
120 if (EFI_ERROR(status))
123 while (!IsDevicePathEnd(path)) {
125 * Check for the ACPI keyboard node. All PNP3xx nodes
126 * are keyboards of different flavors. Note: It is
127 * unclear of there's always a keyboard node when
128 * there's a keyboard controller, or if there's only one
129 * when a keyboard is detected at boot.
131 if (DevicePathType(path) == ACPI_DEVICE_PATH &&
132 (DevicePathSubType(path) == ACPI_DP ||
133 DevicePathSubType(path) == ACPI_EXTENDED_DP)) {
134 ACPI_HID_DEVICE_PATH *acpi;
136 acpi = (ACPI_HID_DEVICE_PATH *)(void *)path;
137 if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 0x300 &&
138 (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) {
143 * Check for USB keyboard node, if present. Unlike a
144 * PS/2 keyboard, these definitely only appear when
145 * connected to the system.
147 } else if (DevicePathType(path) == MESSAGING_DEVICE_PATH &&
148 DevicePathSubType(path) == MSG_USB_CLASS_DP) {
149 USB_CLASS_DEVICE_PATH *usb;
151 usb = (USB_CLASS_DEVICE_PATH *)(void *)path;
152 if (usb->DeviceClass == 3 && /* HID */
153 usb->DeviceSubClass == 1 && /* Boot devices */
154 usb->DeviceProtocol == 1) { /* Boot keyboards */
159 path = NextDevicePathNode(path);
168 set_devdesc_currdev(struct devsw *dev, int unit)
170 struct devdesc currdev;
174 currdev.d_type = currdev.d_dev->dv_type;
175 currdev.d_unit = unit;
176 currdev.d_opendata = NULL;
177 devname = efi_fmtdev(&currdev);
179 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
181 env_setenv("loaddev", EV_VOLATILE, devname, env_noset, env_nounset);
185 find_currdev(EFI_LOADED_IMAGE *img)
187 pdinfo_list_t *pdi_list;
189 EFI_DEVICE_PATH *devpath, *copy;
197 /* Did efi_zfs_probe() detect the boot pool? */
198 if (pool_guid != 0) {
199 struct zfs_devdesc currdev;
201 currdev.d_dev = &zfs_dev;
203 currdev.d_type = currdev.d_dev->dv_type;
204 currdev.d_opendata = NULL;
205 currdev.pool_guid = pool_guid;
206 currdev.root_guid = 0;
207 devname = efi_fmtdev(&currdev);
209 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
211 env_setenv("loaddev", EV_VOLATILE, devname, env_noset,
213 init_zfs_bootenv(devname);
216 #endif /* EFI_ZFS_BOOT */
218 /* We have device lists for hd, cd, fd, walk them all. */
219 pdi_list = efiblk_get_pdinfo_list(&efipart_hddev);
220 STAILQ_FOREACH(dp, pdi_list, pd_link) {
221 struct disk_devdesc currdev;
223 currdev.d_dev = &efipart_hddev;
224 currdev.d_type = currdev.d_dev->dv_type;
225 currdev.d_unit = dp->pd_unit;
226 currdev.d_opendata = NULL;
227 currdev.d_slice = -1;
228 currdev.d_partition = -1;
230 if (dp->pd_handle == img->DeviceHandle) {
231 devname = efi_fmtdev(&currdev);
233 env_setenv("currdev", EV_VOLATILE, devname,
234 efi_setcurrdev, env_nounset);
235 env_setenv("loaddev", EV_VOLATILE, devname,
236 env_noset, env_nounset);
239 /* Assuming GPT partitioning. */
240 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) {
241 if (pp->pd_handle == img->DeviceHandle) {
242 currdev.d_slice = pp->pd_unit;
243 currdev.d_partition = 255;
244 devname = efi_fmtdev(&currdev);
246 env_setenv("currdev", EV_VOLATILE, devname,
247 efi_setcurrdev, env_nounset);
248 env_setenv("loaddev", EV_VOLATILE, devname,
249 env_noset, env_nounset);
255 pdi_list = efiblk_get_pdinfo_list(&efipart_cddev);
256 STAILQ_FOREACH(dp, pdi_list, pd_link) {
257 if (dp->pd_handle == img->DeviceHandle ||
258 dp->pd_alias == img->DeviceHandle) {
259 set_devdesc_currdev(&efipart_cddev, dp->pd_unit);
264 pdi_list = efiblk_get_pdinfo_list(&efipart_fddev);
265 STAILQ_FOREACH(dp, pdi_list, pd_link) {
266 if (dp->pd_handle == img->DeviceHandle) {
267 set_devdesc_currdev(&efipart_fddev, dp->pd_unit);
273 * Try the device handle from our loaded image first. If that
274 * fails, use the device path from the loaded image and see if
275 * any of the nodes in that path match one of the enumerated
278 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) {
279 set_devdesc_currdev(dev, unit);
284 devpath = efi_lookup_image_devpath(IH);
285 while (devpath != NULL) {
286 h = efi_devpath_handle(devpath);
293 if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) {
294 set_devdesc_currdev(dev, unit);
298 devpath = efi_lookup_devpath(h);
299 if (devpath != NULL) {
300 copy = efi_devpath_trim(devpath);
310 main(int argc, CHAR16 *argv[])
314 int i, j, vargood, howto;
317 #if !defined(__arm__)
321 archsw.arch_autoload = efi_autoload;
322 archsw.arch_getdev = efi_getdev;
323 archsw.arch_copyin = efi_copyin;
324 archsw.arch_copyout = efi_copyout;
325 archsw.arch_readin = efi_readin;
327 /* Note this needs to be set before ZFS init. */
328 archsw.arch_zfs_probe = efi_zfs_probe;
331 /* Get our loaded image protocol interface structure. */
332 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
334 /* Init the time source */
337 has_kbd = has_keyboard();
340 * XXX Chicken-and-egg problem; we want to have console output
341 * early, but some console attributes may depend on reading from
342 * eg. the boot device, which we can't do yet. We can use
343 * printf() etc. once this is done.
348 * Initialise the block cache. Set the upper limit.
350 bcache_init(32768, 512);
353 * Parse the args to set the console settings, etc
354 * boot1.efi passes these in, if it can read /boot.config or /boot/config
355 * or iPXE may be setup to pass these in.
357 * Loop through the args, and for each one that contains an '=' that is
358 * not the first character, add it to the environment. This allows
359 * loader and kernel env vars to be passed on the command line. Convert
360 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied.
363 for (i = 1; i < argc; i++) {
364 if (argv[i][0] == '-') {
365 for (j = 1; argv[i][j] != 0; j++) {
377 howto |= RB_MULTIPLE;
390 howto |= RB_SERIAL | RB_MULTIPLE;
393 howto |= RB_DFLTROOT;
399 if (argv[i][j + 1] == 0) {
401 setenv("comconsole_speed", "115200", 1);
403 cpy16to8(&argv[i + 1][0], var,
405 setenv("comconsole_speed", var, 1);
410 cpy16to8(&argv[i][j + 1], var,
412 setenv("comconsole_speed", var, 1);
422 for (j = 0; argv[i][j] != 0; j++) {
423 if (j == sizeof(var)) {
427 if (j > 0 && argv[i][j] == '=')
429 var[j] = (char)argv[i][j];
437 for (i = 0; howto_names[i].ev != NULL; i++)
438 if (howto & howto_names[i].mask)
439 setenv(howto_names[i].ev, "YES", 1);
440 if (howto & RB_MULTIPLE) {
441 if (howto & RB_SERIAL)
442 setenv("console", "comconsole efi" , 1);
444 setenv("console", "efi comconsole" , 1);
445 } else if (howto & RB_SERIAL) {
446 setenv("console", "comconsole" , 1);
449 if (efi_copy_init()) {
450 printf("failed to allocate staging area\n");
451 return (EFI_BUFFER_TOO_SMALL);
455 * March through the device switch probing for things.
457 for (i = 0; devsw[i] != NULL; i++)
458 if (devsw[i]->dv_init != NULL)
459 (devsw[i]->dv_init)();
461 printf("Command line arguments:");
462 for (i = 0; i < argc; i++)
463 printf(" %S", argv[i]);
466 printf("Image base: 0x%lx\n", (u_long)img->ImageBase);
467 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
468 ST->Hdr.Revision & 0xffff);
469 printf("EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor,
470 ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
472 printf("\n%s", bootprog_info);
475 * Disable the watchdog timer. By default the boot manager sets
476 * the timer to 5 minutes before invoking a boot option. If we
477 * want to return to the boot manager, we have to disable the
478 * watchdog timer and since we're an interactive program, we don't
479 * want to wait until the user types "quit". The timer may have
480 * fired by then. We don't care if this fails. It does not prevent
481 * normal functioning in any way...
483 BS->SetWatchdogTimer(0, 0, 0, NULL);
485 if (find_currdev(img) != 0)
486 return (EFI_NOT_FOUND);
488 efi_init_environment();
489 setenv("LINES", "24", 1); /* optional */
491 for (k = 0; k < ST->NumberOfTableEntries; k++) {
492 guid = &ST->ConfigurationTable[k].VendorGuid;
493 #if !defined(__arm__)
494 if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) {
495 snprintf(buf, sizeof(buf), "%p",
496 ST->ConfigurationTable[k].VendorTable);
497 setenv("hint.smbios.0.mem", buf, 1);
498 smbios_detect(ST->ConfigurationTable[k].VendorTable);
504 interact(NULL); /* doesn't return */
506 return (EFI_SUCCESS); /* keep compiler happy */
509 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
512 command_reboot(int argc, char *argv[])
516 for (i = 0; devsw[i] != NULL; ++i)
517 if (devsw[i]->dv_cleanup != NULL)
518 (devsw[i]->dv_cleanup)();
520 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
526 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
529 command_quit(int argc, char *argv[])
535 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
538 command_memmap(int argc, char *argv[])
541 EFI_MEMORY_DESCRIPTOR *map, *p;
547 static char *types[] = {
553 "RuntimeServicesCode",
554 "RuntimeServicesData",
555 "ConventionalMemory",
560 "MemoryMappedIOPortSpace",
565 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
566 if (status != EFI_BUFFER_TOO_SMALL) {
567 printf("Can't determine memory map size\n");
571 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
572 if (EFI_ERROR(status)) {
573 printf("Can't read memory map\n");
578 snprintf(line, sizeof(line), "%23s %12s %12s %8s %4s\n",
579 "Type", "Physical", "Virtual", "#Pages", "Attr");
581 if (pager_output(line)) {
586 for (i = 0, p = map; i < ndesc;
587 i++, p = NextMemoryDescriptor(p, dsz)) {
588 printf("%23s %012jx %012jx %08jx ", types[p->Type],
589 (uintmax_t)p->PhysicalStart, (uintmax_t)p->VirtualStart,
590 (uintmax_t)p->NumberOfPages);
591 if (p->Attribute & EFI_MEMORY_UC)
593 if (p->Attribute & EFI_MEMORY_WC)
595 if (p->Attribute & EFI_MEMORY_WT)
597 if (p->Attribute & EFI_MEMORY_WB)
599 if (p->Attribute & EFI_MEMORY_UCE)
601 if (p->Attribute & EFI_MEMORY_WP)
603 if (p->Attribute & EFI_MEMORY_RP)
605 if (p->Attribute & EFI_MEMORY_XP)
607 if (pager_output("\n"))
615 COMMAND_SET(configuration, "configuration", "print configuration tables",
616 command_configuration);
619 guid_to_string(EFI_GUID *guid)
623 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
624 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
625 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
626 guid->Data4[5], guid->Data4[6], guid->Data4[7]);
631 command_configuration(int argc, char *argv[])
636 snprintf(line, sizeof(line), "NumberOfTableEntries=%lu\n",
637 (unsigned long)ST->NumberOfTableEntries);
639 if (pager_output(line)) {
644 for (i = 0; i < ST->NumberOfTableEntries; i++) {
648 guid = &ST->ConfigurationTable[i].VendorGuid;
649 if (!memcmp(guid, &mps, sizeof(EFI_GUID)))
651 else if (!memcmp(guid, &acpi, sizeof(EFI_GUID)))
652 printf("ACPI Table");
653 else if (!memcmp(guid, &acpi20, sizeof(EFI_GUID)))
654 printf("ACPI 2.0 Table");
655 else if (!memcmp(guid, &smbios, sizeof(EFI_GUID)))
656 printf("SMBIOS Table %p",
657 ST->ConfigurationTable[i].VendorTable);
658 else if (!memcmp(guid, &dxe, sizeof(EFI_GUID)))
660 else if (!memcmp(guid, &hoblist, sizeof(EFI_GUID)))
661 printf("HOB List Table");
662 else if (!memcmp(guid, &memtype, sizeof(EFI_GUID)))
663 printf("Memory Type Information Table");
664 else if (!memcmp(guid, &debugimg, sizeof(EFI_GUID)))
665 printf("Debug Image Info Table");
666 else if (!memcmp(guid, &fdtdtb, sizeof(EFI_GUID)))
669 printf("Unknown Table (%s)", guid_to_string(guid));
670 snprintf(line, sizeof(line), " at %p\n",
671 ST->ConfigurationTable[i].VendorTable);
672 if (pager_output(line))
681 COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
684 command_mode(int argc, char *argv[])
692 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
693 extern void HO(void);
698 mode = strtol(argv[1], &cp, 0);
700 printf("Invalid mode\n");
703 status = conout->QueryMode(conout, mode, &cols, &rows);
704 if (EFI_ERROR(status)) {
705 printf("invalid mode %d\n", mode);
708 status = conout->SetMode(conout, mode);
709 if (EFI_ERROR(status)) {
710 printf("couldn't set mode %d\n", mode);
713 sprintf(rowenv, "%u", (unsigned)rows);
714 setenv("LINES", rowenv, 1);
715 HO(); /* set cursor */
719 printf("Current mode: %d\n", conout->Mode->Mode);
720 for (i = 0; i <= conout->Mode->MaxMode; i++) {
721 status = conout->QueryMode(conout, i, &cols, &rows);
722 if (EFI_ERROR(status))
724 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
729 printf("Select a mode with the command \"mode <number>\"\n");
735 COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
739 command_lszfs(int argc, char *argv[])
744 command_errmsg = "wrong number of arguments";
748 err = zfs_list(argv[1]);
750 command_errmsg = strerror(err);
756 COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
760 command_reloadbe(int argc, char *argv[])
766 command_errmsg = "wrong number of arguments";
771 err = zfs_bootenv(argv[1]);
773 root = getenv("zfs_be_root");
777 err = zfs_bootenv(root);
781 command_errmsg = strerror(err);
789 #ifdef LOADER_FDT_SUPPORT
790 extern int command_fdt_internal(int argc, char *argv[]);
793 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
794 * and declaring it as extern is in contradiction with COMMAND_SET() macro
795 * (which uses static pointer), we're defining wrapper function, which
796 * calls the proper fdt handling routine.
799 command_fdt(int argc, char *argv[])
802 return (command_fdt_internal(argc, argv));
805 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
809 * Chain load another efi loader.
812 command_chain(int argc, char *argv[])
814 EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL;
815 EFI_HANDLE loaderhandle;
816 EFI_LOADED_IMAGE *loaded_image;
825 command_errmsg = "wrong number of arguments";
831 if ((fd = open(name, O_RDONLY)) < 0) {
832 command_errmsg = "no such file";
836 if (fstat(fd, &st) < -1) {
837 command_errmsg = "stat failed";
842 status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf);
843 if (status != EFI_SUCCESS) {
844 command_errmsg = "failed to allocate buffer";
848 if (read(fd, buf, st.st_size) != st.st_size) {
849 command_errmsg = "error while reading the file";
850 (void)BS->FreePool(buf);
855 status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle);
856 (void)BS->FreePool(buf);
857 if (status != EFI_SUCCESS) {
858 command_errmsg = "LoadImage failed";
861 status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID,
862 (void **)&loaded_image);
868 for (i = 2; i < argc; i++)
869 len += strlen(argv[i]) + 1;
871 len *= sizeof (*argp);
872 loaded_image->LoadOptions = argp = malloc (len);
873 loaded_image->LoadOptionsSize = len;
874 for (i = 2; i < argc; i++) {
877 *(argp++) = *(ptr++);
883 if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) {
885 struct zfs_devdesc *z_dev;
887 struct disk_devdesc *d_dev;
890 switch (dev->d_type) {
893 z_dev = (struct zfs_devdesc *)dev;
894 loaded_image->DeviceHandle =
895 efizfs_get_handle_by_guid(z_dev->pool_guid);
899 loaded_image->DeviceHandle =
900 efi_find_handle(dev->d_dev, dev->d_unit);
903 hd = efiblk_get_pdinfo(dev);
904 if (STAILQ_EMPTY(&hd->pd_part)) {
905 loaded_image->DeviceHandle = hd->pd_handle;
908 d_dev = (struct disk_devdesc *)dev;
909 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
911 * d_partition should be 255
913 if (pd->pd_unit == (uint32_t)d_dev->d_slice) {
914 loaded_image->DeviceHandle =
924 status = BS->StartImage(loaderhandle, NULL, NULL);
925 if (status != EFI_SUCCESS) {
926 command_errmsg = "StartImage failed";
927 free(loaded_image->LoadOptions);
928 loaded_image->LoadOptions = NULL;
929 status = BS->UnloadImage(loaded_image);
933 return (CMD_ERROR); /* not reached */
936 COMMAND_SET(chain, "chain", "chain load file", command_chain);