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>
41 #include <bootstrap.h>
48 #include "loader_efi.h"
50 extern char bootprog_name[];
51 extern char bootprog_rev[];
52 extern char bootprog_date[];
53 extern char bootprog_maker[];
55 struct arch_switch archsw; /* MI/MD interface boundary */
57 EFI_GUID acpi = ACPI_TABLE_GUID;
58 EFI_GUID acpi20 = ACPI_20_TABLE_GUID;
59 EFI_GUID devid = DEVICE_PATH_PROTOCOL;
60 EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
61 EFI_GUID mps = MPS_TABLE_GUID;
62 EFI_GUID netid = EFI_SIMPLE_NETWORK_PROTOCOL;
63 EFI_GUID smbios = SMBIOS_TABLE_GUID;
64 EFI_GUID dxe = DXE_SERVICES_TABLE_GUID;
65 EFI_GUID hoblist = HOB_LIST_TABLE_GUID;
66 EFI_GUID memtype = MEMORY_TYPE_INFORMATION_TABLE_GUID;
67 EFI_GUID debugimg = DEBUG_IMAGE_INFO_TABLE_GUID;
68 EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL;
71 static void efi_zfs_probe(void);
75 * Need this because EFI uses UTF-16 unicode string constants, but we
76 * use UTF-8. We can't use printf due to the possiblity of \0 and we
77 * don't support support wide characters either.
80 print_str16(const CHAR16 *str)
84 for (i = 0; str[i]; i++)
85 printf("%c", (char)str[i]);
89 cp16to8(const CHAR16 *src, char *dst, size_t len)
93 for (i = 0; i < len && src[i]; i++)
94 dst[i] = (char)src[i];
101 EFI_DEVICE_PATH *path;
102 EFI_HANDLE *hin, *hin_end, *walker;
107 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and
108 * do the typical dance to get the right sized buffer.
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,
117 if (EFI_ERROR(status))
120 if (EFI_ERROR(status))
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.
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))
135 while (!IsDevicePathEnd(path)) {
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.
143 if (DevicePathType(path) == ACPI_DEVICE_PATH &&
144 (DevicePathSubType(path) == ACPI_DP ||
145 DevicePathSubType(path) == ACPI_EXTENDED_DP)) {
146 ACPI_HID_DEVICE_PATH *acpi;
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) {
155 * Check for USB keyboard node, if present. Unlike a
156 * PS/2 keyboard, these definitely only appear when
157 * connected to the system.
159 } else if (DevicePathType(path) == MESSAGING_DEVICE_PATH &&
160 DevicePathSubType(path) == MSG_USB_CLASS_DP) {
161 USB_CLASS_DEVICE_PATH *usb;
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 */
171 path = NextDevicePathNode(path);
180 main(int argc, CHAR16 *argv[])
183 EFI_LOADED_IMAGE *img;
185 int i, j, vargood, unit, howto;
191 archsw.arch_autoload = efi_autoload;
192 archsw.arch_getdev = efi_getdev;
193 archsw.arch_copyin = efi_copyin;
194 archsw.arch_copyout = efi_copyout;
195 archsw.arch_readin = efi_readin;
197 /* Note this needs to be set before ZFS init. */
198 archsw.arch_zfs_probe = efi_zfs_probe;
201 has_kbd = has_keyboard();
204 * XXX Chicken-and-egg problem; we want to have console output
205 * early, but some console attributes may depend on reading from
206 * eg. the boot device, which we can't do yet. We can use
207 * printf() etc. once this is done.
212 * Parse the args to set the console settings, etc
213 * boot1.efi passes these in, if it can read /boot.config or /boot/config
214 * or iPXE may be setup to pass these in.
216 * Loop through the args, and for each one that contains an '=' that is
217 * not the first character, add it to the environment. This allows
218 * loader and kernel env vars to be passed on the command line. Convert
219 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied.
222 for (i = 1; i < argc; i++) {
223 if (argv[i][0] == '-') {
224 for (j = 1; argv[i][j] != 0; j++) {
236 howto |= RB_MULTIPLE;
249 howto |= RB_SERIAL | RB_MULTIPLE;
252 howto |= RB_DFLTROOT;
258 if (argv[i][j + 1] == 0) {
260 setenv("comconsole_speed", "115200", 1);
262 cp16to8(&argv[i + 1][0], var,
264 setenv("comconsole_speedspeed", var, 1);
269 cp16to8(&argv[i][j + 1], var,
271 setenv("comconsole_speed", var, 1);
281 for (j = 0; argv[i][j] != 0; j++) {
282 if (j == sizeof(var)) {
286 if (j > 0 && argv[i][j] == '=')
288 var[j] = (char)argv[i][j];
296 for (i = 0; howto_names[i].ev != NULL; i++)
297 if (howto & howto_names[i].mask)
298 setenv(howto_names[i].ev, "YES", 1);
299 if (howto & RB_MULTIPLE) {
300 if (howto & RB_SERIAL)
301 setenv("console", "comconsole efi" , 1);
303 setenv("console", "efi comconsole" , 1);
304 } else if (howto & RB_SERIAL) {
305 setenv("console", "comconsole" , 1);
308 if (efi_copy_init()) {
309 printf("failed to allocate staging area\n");
310 return (EFI_BUFFER_TOO_SMALL);
314 * March through the device switch probing for things.
316 for (i = 0; devsw[i] != NULL; i++)
317 if (devsw[i]->dv_init != NULL)
318 (devsw[i]->dv_init)();
320 /* Get our loaded image protocol interface structure. */
321 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
323 printf("Command line arguments:");
324 for (i = 0; i < argc; i++) {
326 print_str16(argv[i]);
330 printf("Image base: 0x%lx\n", (u_long)img->ImageBase);
331 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
332 ST->Hdr.Revision & 0xffff);
333 printf("EFI Firmware: ");
334 /* printf doesn't understand EFI Unicode */
335 ST->ConOut->OutputString(ST->ConOut, ST->FirmwareVendor);
336 printf(" (rev %d.%02d)\n", ST->FirmwareRevision >> 16,
337 ST->FirmwareRevision & 0xffff);
340 printf("%s, Revision %s\n", bootprog_name, bootprog_rev);
341 printf("(%s, %s)\n", bootprog_maker, bootprog_date);
344 * Disable the watchdog timer. By default the boot manager sets
345 * the timer to 5 minutes before invoking a boot option. If we
346 * want to return to the boot manager, we have to disable the
347 * watchdog timer and since we're an interactive program, we don't
348 * want to wait until the user types "quit". The timer may have
349 * fired by then. We don't care if this fails. It does not prevent
350 * normal functioning in any way...
352 BS->SetWatchdogTimer(0, 0, 0, NULL);
354 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &pool_guid) != 0)
355 return (EFI_NOT_FOUND);
357 switch (dev->dv_type) {
360 struct zfs_devdesc currdev;
363 currdev.d_unit = unit;
364 currdev.d_type = currdev.d_dev->dv_type;
365 currdev.d_opendata = NULL;
366 currdev.pool_guid = pool_guid;
367 currdev.root_guid = 0;
368 env_setenv("currdev", EV_VOLATILE, efi_fmtdev(&currdev),
369 efi_setcurrdev, env_nounset);
370 env_setenv("loaddev", EV_VOLATILE, efi_fmtdev(&currdev), env_noset,
372 init_zfs_bootenv(zfs_fmtdev(&currdev));
377 struct devdesc currdev;
380 currdev.d_unit = unit;
381 currdev.d_opendata = NULL;
382 currdev.d_type = currdev.d_dev->dv_type;
383 env_setenv("currdev", EV_VOLATILE, efi_fmtdev(&currdev),
384 efi_setcurrdev, env_nounset);
385 env_setenv("loaddev", EV_VOLATILE, efi_fmtdev(&currdev), env_noset,
391 setenv("LINES", "24", 1); /* optional */
393 for (k = 0; k < ST->NumberOfTableEntries; k++) {
394 guid = &ST->ConfigurationTable[k].VendorGuid;
395 if (!memcmp(guid, &smbios, sizeof(EFI_GUID))) {
396 smbios_detect(ST->ConfigurationTable[k].VendorTable);
401 interact(); /* doesn't return */
403 return (EFI_SUCCESS); /* keep compiler happy */
406 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
409 command_reboot(int argc, char *argv[])
413 for (i = 0; devsw[i] != NULL; ++i)
414 if (devsw[i]->dv_cleanup != NULL)
415 (devsw[i]->dv_cleanup)();
417 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 23,
418 (CHAR16 *)"Reboot from the loader");
424 COMMAND_SET(quit, "quit", "exit the loader", command_quit);
427 command_quit(int argc, char *argv[])
433 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
436 command_memmap(int argc, char *argv[])
439 EFI_MEMORY_DESCRIPTOR *map, *p;
444 static char *types[] = {
450 "RuntimeServicesCode",
451 "RuntimeServicesData",
452 "ConventionalMemory",
457 "MemoryMappedIOPortSpace",
462 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
463 if (status != EFI_BUFFER_TOO_SMALL) {
464 printf("Can't determine memory map size\n");
468 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
469 if (EFI_ERROR(status)) {
470 printf("Can't read memory map\n");
475 printf("%23s %12s %12s %8s %4s\n",
476 "Type", "Physical", "Virtual", "#Pages", "Attr");
478 for (i = 0, p = map; i < ndesc;
479 i++, p = NextMemoryDescriptor(p, dsz)) {
480 printf("%23s %012jx %012jx %08jx ", types[p->Type],
481 (uintmax_t)p->PhysicalStart, (uintmax_t)p->VirtualStart,
482 (uintmax_t)p->NumberOfPages);
483 if (p->Attribute & EFI_MEMORY_UC)
485 if (p->Attribute & EFI_MEMORY_WC)
487 if (p->Attribute & EFI_MEMORY_WT)
489 if (p->Attribute & EFI_MEMORY_WB)
491 if (p->Attribute & EFI_MEMORY_UCE)
493 if (p->Attribute & EFI_MEMORY_WP)
495 if (p->Attribute & EFI_MEMORY_RP)
497 if (p->Attribute & EFI_MEMORY_XP)
505 COMMAND_SET(configuration, "configuration", "print configuration tables",
506 command_configuration);
509 guid_to_string(EFI_GUID *guid)
513 sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
514 guid->Data1, guid->Data2, guid->Data3, guid->Data4[0],
515 guid->Data4[1], guid->Data4[2], guid->Data4[3], guid->Data4[4],
516 guid->Data4[5], guid->Data4[6], guid->Data4[7]);
521 command_configuration(int argc, char *argv[])
525 printf("NumberOfTableEntries=%lu\n",
526 (unsigned long)ST->NumberOfTableEntries);
527 for (i = 0; i < ST->NumberOfTableEntries; i++) {
531 guid = &ST->ConfigurationTable[i].VendorGuid;
532 if (!memcmp(guid, &mps, sizeof(EFI_GUID)))
534 else if (!memcmp(guid, &acpi, sizeof(EFI_GUID)))
535 printf("ACPI Table");
536 else if (!memcmp(guid, &acpi20, sizeof(EFI_GUID)))
537 printf("ACPI 2.0 Table");
538 else if (!memcmp(guid, &smbios, sizeof(EFI_GUID)))
539 printf("SMBIOS Table");
540 else if (!memcmp(guid, &dxe, sizeof(EFI_GUID)))
542 else if (!memcmp(guid, &hoblist, sizeof(EFI_GUID)))
543 printf("HOB List Table");
544 else if (!memcmp(guid, &memtype, sizeof(EFI_GUID)))
545 printf("Memory Type Information Table");
546 else if (!memcmp(guid, &debugimg, sizeof(EFI_GUID)))
547 printf("Debug Image Info Table");
549 printf("Unknown Table (%s)", guid_to_string(guid));
550 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
557 COMMAND_SET(mode, "mode", "change or display text modes", command_mode);
560 command_mode(int argc, char *argv[])
568 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
569 extern void HO(void);
574 mode = strtol(argv[1], &cp, 0);
576 printf("Invalid mode\n");
579 status = conout->QueryMode(conout, mode, &cols, &rows);
580 if (EFI_ERROR(status)) {
581 printf("invalid mode %d\n", mode);
584 status = conout->SetMode(conout, mode);
585 if (EFI_ERROR(status)) {
586 printf("couldn't set mode %d\n", mode);
589 sprintf(rowenv, "%u", (unsigned)rows);
590 setenv("LINES", rowenv, 1);
591 HO(); /* set cursor */
595 printf("Current mode: %d\n", conout->Mode->Mode);
596 for (i = 0; i <= conout->Mode->MaxMode; i++) {
597 status = conout->QueryMode(conout, i, &cols, &rows);
598 if (EFI_ERROR(status))
600 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
605 printf("Choose the mode with \"col <mode number>\"\n");
611 COMMAND_SET(nvram, "nvram", "get or set NVRAM variables", command_nvram);
614 command_nvram(int argc, char *argv[])
619 EFI_GUID varguid = { 0,0,0,{0,0,0,0,0,0,0,0} };
620 UINTN varsz, datasz, i;
621 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
625 /* Initiate the search */
626 status = RS->GetNextVariableName(&varsz, NULL, NULL);
628 for (; status != EFI_NOT_FOUND; ) {
629 status = RS->GetNextVariableName(&varsz, var, &varguid);
630 //if (EFI_ERROR(status))
633 conout->OutputString(conout, var);
636 status = RS->GetVariable(var, &varguid, NULL, &datasz, NULL);
637 /* XXX: check status */
638 data = malloc(datasz);
639 status = RS->GetVariable(var, &varguid, NULL, &datasz, data);
640 if (EFI_ERROR(status))
641 printf("<error retrieving variable>");
643 for (i = 0; i < datasz; i++) {
644 if (isalnum(data[i]) || isspace(data[i]))
645 printf("%c", data[i]);
647 printf("\\x%02x", data[i]);
659 COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
663 command_lszfs(int argc, char *argv[])
668 command_errmsg = "wrong number of arguments";
672 err = zfs_list(argv[1]);
674 command_errmsg = strerror(err);
680 COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
684 command_reloadbe(int argc, char *argv[])
690 command_errmsg = "wrong number of arguments";
695 err = zfs_bootenv(argv[1]);
697 root = getenv("zfs_be_root");
701 err = zfs_bootenv(root);
705 command_errmsg = strerror(err);
720 char dname[SPECNAMELEN + 1];
724 h = efi_find_handle(&efipart_dev, 0);
725 for (i = 0; h != NULL; h = efi_find_handle(&efipart_dev, ++i)) {
726 snprintf(dname, sizeof(dname), "%s%d:", efipart_dev.dv_name, i);
727 if (zfs_probe_dev(dname, &guid) == 0)
728 (void)efi_handle_update_dev(h, &zfs_dev, unit++, guid);