loader: UEFI loader needs to set ISADIR based on hardware
[unleashed.git] / usr / src / boot / sys / boot / efi / loader / main.c
blobdffdfe32a0c97b471df904a80b9687934b4867fb
1 /*
2 * Copyright (c) 2008-2010 Rui Paulo
3 * Copyright (c) 2006 Marcel Moolenaar
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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>
30 #include <sys/disk.h>
31 #include <sys/param.h>
32 #include <sys/reboot.h>
33 #include <sys/boot.h>
34 #include <stand.h>
35 #include <inttypes.h>
36 #include <string.h>
37 #include <setjmp.h>
38 #include <disk.h>
40 #include <efi.h>
41 #include <efilib.h>
42 #include <efigpt.h>
44 #include <uuid.h>
46 #include <bootstrap.h>
47 #include <smbios.h>
49 #include <libzfs.h>
50 #include <efizfs.h>
52 #include "loader_efi.h"
54 struct arch_switch archsw; /* MI/MD interface boundary */
56 EFI_GUID devid = DEVICE_PATH_PROTOCOL;
57 EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
58 EFI_GUID smbios = SMBIOS_TABLE_GUID;
59 EFI_GUID smbios3 = SMBIOS3_TABLE_GUID;
60 EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL;
62 extern void acpi_detect(void);
63 extern void efi_getsmap(void);
65 static EFI_LOADED_IMAGE *img;
67 bool
68 efi_zfs_is_preferred(EFI_HANDLE *h)
70 return (h == img->DeviceHandle);
73 static bool
74 has_keyboard(void)
76 EFI_STATUS status;
77 EFI_DEVICE_PATH *path;
78 EFI_HANDLE *hin, *hin_end, *walker;
79 UINTN sz;
80 bool retval = false;
83 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and
84 * do the typical dance to get the right sized buffer.
86 sz = 0;
87 hin = NULL;
88 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 0);
89 if (status == EFI_BUFFER_TOO_SMALL) {
90 hin = (EFI_HANDLE *)malloc(sz);
91 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz,
92 hin);
93 if (EFI_ERROR(status))
94 free(hin);
96 if (EFI_ERROR(status))
97 return (retval);
100 * Look at each of the handles. If it supports the device path protocol,
101 * use it to get the device path for this handle. Then see if that
102 * device path matches either the USB device path for keyboards or the
103 * legacy device path for keyboards.
105 hin_end = &hin[sz / sizeof(*hin)];
106 for (walker = hin; walker < hin_end; walker++) {
107 status = BS->HandleProtocol(*walker, &devid, (VOID **)&path);
108 if (EFI_ERROR(status))
109 continue;
111 while (!IsDevicePathEnd(path)) {
113 * Check for the ACPI keyboard node. All PNP3xx nodes
114 * are keyboards of different flavors. Note: It is
115 * unclear of there's always a keyboard node when
116 * there's a keyboard controller, or if there's only one
117 * when a keyboard is detected at boot.
119 if (DevicePathType(path) == ACPI_DEVICE_PATH &&
120 (DevicePathSubType(path) == ACPI_DP ||
121 DevicePathSubType(path) == ACPI_EXTENDED_DP)) {
122 ACPI_HID_DEVICE_PATH *acpi;
124 acpi = (ACPI_HID_DEVICE_PATH *)(void *)path;
125 if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 0x300 &&
126 (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) {
127 retval = true;
128 goto out;
131 * Check for USB keyboard node, if present. Unlike a
132 * PS/2 keyboard, these definitely only appear when
133 * connected to the system.
135 } else if (DevicePathType(path) == MESSAGING_DEVICE_PATH &&
136 DevicePathSubType(path) == MSG_USB_CLASS_DP) {
137 USB_CLASS_DEVICE_PATH *usb;
139 usb = (USB_CLASS_DEVICE_PATH *)(void *)path;
140 if (usb->DeviceClass == 3 && /* HID */
141 usb->DeviceSubClass == 1 && /* Boot devices */
142 usb->DeviceProtocol == 1) { /* Boot keyboards */
143 retval = true;
144 goto out;
147 path = NextDevicePathNode(path);
150 out:
151 free(hin);
152 return (retval);
155 static void
156 set_devdesc_currdev(struct devsw *dev, int unit)
158 struct devdesc currdev;
159 char *devname;
161 currdev.d_dev = dev;
162 currdev.d_unit = unit;
163 devname = efi_fmtdev(&currdev);
165 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
166 env_nounset);
167 env_setenv("loaddev", EV_VOLATILE, devname, env_noset, env_nounset);
170 static int
171 find_currdev(EFI_LOADED_IMAGE *img)
173 pdinfo_list_t *pdi_list;
174 pdinfo_t *dp, *pp;
175 EFI_DEVICE_PATH *devpath, *copy;
176 EFI_HANDLE h;
177 char *devname;
178 struct devsw *dev;
179 int unit;
180 uint64_t extra;
182 /* Did efi_zfs_probe() detect the boot pool? */
183 if (pool_guid != 0) {
184 struct zfs_devdesc currdev;
186 currdev.dd.d_dev = &zfs_dev;
187 currdev.dd.d_unit = 0;
188 currdev.pool_guid = pool_guid;
189 currdev.root_guid = 0;
190 devname = efi_fmtdev(&currdev);
192 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
193 env_nounset);
194 env_setenv("loaddev", EV_VOLATILE, devname, env_noset,
195 env_nounset);
196 return (0);
199 /* We have device lists for hd, cd, fd, walk them all. */
200 pdi_list = efiblk_get_pdinfo_list(&efipart_hddev);
201 STAILQ_FOREACH(dp, pdi_list, pd_link) {
202 struct disk_devdesc currdev;
204 currdev.dd.d_dev = &efipart_hddev;
205 currdev.dd.d_unit = dp->pd_unit;
206 currdev.d_slice = -1;
207 currdev.d_partition = -1;
209 if (dp->pd_handle == img->DeviceHandle) {
210 devname = efi_fmtdev(&currdev);
212 env_setenv("currdev", EV_VOLATILE, devname,
213 efi_setcurrdev, env_nounset);
214 env_setenv("loaddev", EV_VOLATILE, devname,
215 env_noset, env_nounset);
216 return (0);
218 /* Assuming GPT partitioning. */
219 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) {
220 if (pp->pd_handle == img->DeviceHandle) {
221 currdev.d_slice = pp->pd_unit;
222 currdev.d_partition = 255;
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);
229 return (0);
234 pdi_list = efiblk_get_pdinfo_list(&efipart_cddev);
235 STAILQ_FOREACH(dp, pdi_list, pd_link) {
236 if (dp->pd_handle == img->DeviceHandle ||
237 dp->pd_alias == img->DeviceHandle) {
238 set_devdesc_currdev(&efipart_cddev, dp->pd_unit);
239 return (0);
243 pdi_list = efiblk_get_pdinfo_list(&efipart_fddev);
244 STAILQ_FOREACH(dp, pdi_list, pd_link) {
245 if (dp->pd_handle == img->DeviceHandle) {
246 set_devdesc_currdev(&efipart_fddev, dp->pd_unit);
247 return (0);
252 * Try the device handle from our loaded image first. If that
253 * fails, use the device path from the loaded image and see if
254 * any of the nodes in that path match one of the enumerated
255 * handles.
257 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) {
258 set_devdesc_currdev(dev, unit);
259 return (0);
262 copy = NULL;
263 devpath = efi_lookup_image_devpath(IH);
264 while (devpath != NULL) {
265 h = efi_devpath_handle(devpath);
266 if (h == NULL)
267 break;
269 free(copy);
270 copy = NULL;
272 if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) {
273 set_devdesc_currdev(dev, unit);
274 return (0);
277 devpath = efi_lookup_devpath(h);
278 if (devpath != NULL) {
279 copy = efi_devpath_trim(devpath);
280 devpath = copy;
283 free(copy);
285 return (ENOENT);
288 EFI_STATUS
289 main(int argc, CHAR16 *argv[])
291 char var[128];
292 EFI_GUID *guid;
293 int i, j, howto;
294 bool vargood;
295 void *ptr;
296 UINTN k;
297 bool has_kbd;
299 archsw.arch_autoload = efi_autoload;
300 archsw.arch_getdev = efi_getdev;
301 archsw.arch_copyin = efi_copyin;
302 archsw.arch_copyout = efi_copyout;
303 archsw.arch_readin = efi_readin;
304 archsw.arch_loadaddr = efi_loadaddr;
305 archsw.arch_free_loadaddr = efi_free_loadaddr;
306 /* Note this needs to be set before ZFS init. */
307 archsw.arch_zfs_probe = efi_zfs_probe;
309 /* Get our loaded image protocol interface structure. */
310 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
312 /* Init the time source */
313 efi_time_init();
315 has_kbd = has_keyboard();
318 * XXX Chicken-and-egg problem; we want to have console output
319 * early, but some console attributes may depend on reading from
320 * eg. the boot device, which we can't do yet. We can use
321 * printf() etc. once this is done.
323 cons_probe();
324 efi_getsmap();
327 * Initialise the block cache. Set the upper limit.
329 bcache_init(32768, 512);
332 * Parse the args to set the console settings, etc
333 * boot1.efi passes these in, if it can read /boot.config or /boot/config
334 * or iPXE may be setup to pass these in.
336 * Loop through the args, and for each one that contains an '=' that is
337 * not the first character, add it to the environment. This allows
338 * loader and kernel env vars to be passed on the command line. Convert
339 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied.
341 howto = 0;
342 for (i = 1; i < argc; i++) {
343 if (argv[i][0] == '-') {
344 for (j = 1; argv[i][j] != 0; j++) {
345 int ch;
347 ch = argv[i][j];
348 switch (ch) {
349 case 'a':
350 howto |= RB_ASKNAME;
351 break;
352 case 'd':
353 howto |= RB_KDB;
354 break;
355 case 'D':
356 howto |= RB_MULTIPLE;
357 break;
358 case 'h':
359 howto |= RB_SERIAL;
360 break;
361 case 'm':
362 howto |= RB_MUTE;
363 break;
364 case 'p':
365 howto |= RB_PAUSE;
366 break;
367 case 'P':
368 if (!has_kbd)
369 howto |= RB_SERIAL | RB_MULTIPLE;
370 break;
371 case 'r':
372 howto |= RB_DFLTROOT;
373 break;
374 case 's':
375 howto |= RB_SINGLE;
376 break;
377 case 'S':
378 if (argv[i][j + 1] == 0) {
379 if (i + 1 == argc) {
380 strncpy(var, "115200",
381 sizeof(var));
382 } else {
383 CHAR16 *ptr;
384 ptr = &argv[i + 1][0];
385 cpy16to8(ptr, var,
386 sizeof(var));
388 i++;
389 } else {
390 cpy16to8(&argv[i][j + 1], var,
391 sizeof(var));
393 strncat(var, ",8,n,1,-", sizeof(var));
394 setenv("ttya-mode", var, 1);
395 break;
396 case 'v':
397 howto |= RB_VERBOSE;
398 break;
401 } else {
402 vargood = false;
403 for (j = 0; argv[i][j] != 0; j++) {
404 if (j == sizeof(var)) {
405 vargood = false;
406 break;
408 if (j > 0 && argv[i][j] == '=')
409 vargood = true;
410 var[j] = (char)argv[i][j];
412 if (vargood) {
413 var[j] = 0;
414 putenv(var);
418 for (i = 0; howto_names[i].ev != NULL; i++)
419 if (howto & howto_names[i].mask)
420 setenv(howto_names[i].ev, "YES", 1);
421 if (howto & RB_MULTIPLE) {
422 if (howto & RB_SERIAL)
423 setenv("console", "ttya text" , 1);
424 else
425 setenv("console", "text ttya" , 1);
426 } else if (howto & RB_SERIAL) {
427 setenv("console", "ttya" , 1);
431 * Scan the BLOCK IO MEDIA handles then
432 * march through the device switch probing for things.
434 if ((i = efipart_inithandles()) == 0) {
435 for (i = 0; devsw[i] != NULL; i++)
436 if (devsw[i]->dv_init != NULL)
437 (devsw[i]->dv_init)();
438 } else
439 printf("efipart_inithandles failed %d, expect failures", i);
441 printf("Command line arguments:");
442 for (i = 0; i < argc; i++) {
443 printf(" %S", argv[i]);
445 printf("\n");
447 printf("Image base: 0x%lx\n", (u_long)img->ImageBase);
448 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
449 ST->Hdr.Revision & 0xffff);
450 printf("EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor,
451 ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
453 printf("\n%s", bootprog_info);
456 * Disable the watchdog timer. By default the boot manager sets
457 * the timer to 5 minutes before invoking a boot option. If we
458 * want to return to the boot manager, we have to disable the
459 * watchdog timer and since we're an interactive program, we don't
460 * want to wait until the user types "quit". The timer may have
461 * fired by then. We don't care if this fails. It does not prevent
462 * normal functioning in any way...
464 BS->SetWatchdogTimer(0, 0, 0, NULL);
466 if (find_currdev(img) != 0)
467 return (EFI_NOT_FOUND);
469 efi_init_environment();
470 setenv("ISADIR", "amd64", 1); /* we only build 64bit */
471 bi_isadir(); /* set ISADIR */
472 acpi_detect();
474 if ((ptr = efi_get_table(&smbios3)) == NULL)
475 ptr = efi_get_table(&smbios);
476 smbios_detect(ptr);
478 interact(NULL); /* doesn't return */
480 return (EFI_SUCCESS); /* keep compiler happy */
483 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
485 static int
486 command_reboot(int argc __unused, char *argv[] __unused)
488 int i;
490 for (i = 0; devsw[i] != NULL; ++i)
491 if (devsw[i]->dv_cleanup != NULL)
492 (devsw[i]->dv_cleanup)();
494 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
496 /* NOTREACHED */
497 return (CMD_ERROR);
500 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
502 static int
503 command_memmap(int argc __unused, char *argv[] __unused)
505 UINTN sz;
506 EFI_MEMORY_DESCRIPTOR *map, *p;
507 UINTN key, dsz;
508 UINT32 dver;
509 EFI_STATUS status;
510 int i, ndesc;
511 int rv = 0;
512 char line[80];
514 sz = 0;
515 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
516 if (status != EFI_BUFFER_TOO_SMALL) {
517 printf("Can't determine memory map size\n");
518 return (CMD_ERROR);
520 map = malloc(sz);
521 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
522 if (EFI_ERROR(status)) {
523 printf("Can't read memory map\n");
524 return (CMD_ERROR);
527 ndesc = sz / dsz;
528 snprintf(line, 80, "%23s %12s %12s %8s %4s\n",
529 "Type", "Physical", "Virtual", "#Pages", "Attr");
530 pager_open();
531 rv = pager_output(line);
532 if (rv) {
533 pager_close();
534 return (CMD_OK);
537 for (i = 0, p = map; i < ndesc;
538 i++, p = NextMemoryDescriptor(p, dsz)) {
539 snprintf(line, 80, "%23s %012lx %012lx %08lx ",
540 efi_memory_type(p->Type),
541 (unsigned long)p->PhysicalStart,
542 (unsigned long)p->VirtualStart,
543 (unsigned long)p->NumberOfPages);
544 rv = pager_output(line);
545 if (rv)
546 break;
548 if (p->Attribute & EFI_MEMORY_UC)
549 printf("UC ");
550 if (p->Attribute & EFI_MEMORY_WC)
551 printf("WC ");
552 if (p->Attribute & EFI_MEMORY_WT)
553 printf("WT ");
554 if (p->Attribute & EFI_MEMORY_WB)
555 printf("WB ");
556 if (p->Attribute & EFI_MEMORY_UCE)
557 printf("UCE ");
558 if (p->Attribute & EFI_MEMORY_WP)
559 printf("WP ");
560 if (p->Attribute & EFI_MEMORY_RP)
561 printf("RP ");
562 if (p->Attribute & EFI_MEMORY_XP)
563 printf("XP ");
564 if (p->Attribute & EFI_MEMORY_NV)
565 printf("NV ");
566 if (p->Attribute & EFI_MEMORY_MORE_RELIABLE)
567 printf("MR ");
568 if (p->Attribute & EFI_MEMORY_RO)
569 printf("RO ");
570 rv = pager_output("\n");
571 if (rv)
572 break;
575 pager_close();
576 return (CMD_OK);
579 COMMAND_SET(configuration, "configuration", "print configuration tables",
580 command_configuration);
582 static int
583 command_configuration(int argc __unused, char *argv[] __unused)
585 UINTN i;
586 char *name;
588 printf("NumberOfTableEntries=%lu\n",
589 (unsigned long)ST->NumberOfTableEntries);
590 for (i = 0; i < ST->NumberOfTableEntries; i++) {
591 EFI_GUID *guid;
593 printf(" ");
594 guid = &ST->ConfigurationTable[i].VendorGuid;
596 if (efi_guid_to_name(guid, &name) == true) {
597 printf(name);
598 free(name);
599 } else {
600 printf("Error while translating UUID to name");
602 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
605 return (CMD_OK);
609 COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
611 static int
612 command_mode(int argc, char *argv[])
614 UINTN cols, rows;
615 unsigned int mode;
616 int i;
617 char *cp;
618 char rowenv[8];
619 EFI_STATUS status;
620 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
621 extern void HO(void);
623 conout = ST->ConOut;
625 if (argc > 1) {
626 mode = strtol(argv[1], &cp, 0);
627 if (cp[0] != '\0') {
628 printf("Invalid mode\n");
629 return (CMD_ERROR);
631 status = conout->QueryMode(conout, mode, &cols, &rows);
632 if (EFI_ERROR(status)) {
633 printf("invalid mode %d\n", mode);
634 return (CMD_ERROR);
636 status = conout->SetMode(conout, mode);
637 if (EFI_ERROR(status)) {
638 printf("couldn't set mode %d\n", mode);
639 return (CMD_ERROR);
641 sprintf(rowenv, "%u", (unsigned)rows);
642 setenv("LINES", rowenv, 1);
643 sprintf(rowenv, "%u", (unsigned)cols);
644 setenv("COLUMNS", rowenv, 1);
645 HO(); /* set cursor */
646 return (CMD_OK);
649 printf("Current mode: %d\n", conout->Mode->Mode);
650 for (i = 0; i <= conout->Mode->MaxMode; i++) {
651 status = conout->QueryMode(conout, i, &cols, &rows);
652 if (EFI_ERROR(status))
653 continue;
654 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
655 (unsigned)rows);
658 if (i != 0)
659 printf("Select a mode with the command \"mode <number>\"\n");
661 return (CMD_OK);
664 COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi);
666 static int
667 command_lsefi(int argc __unused, char *argv[] __unused)
669 char *name;
670 EFI_HANDLE *buffer = NULL;
671 EFI_HANDLE handle;
672 UINTN bufsz = 0, i, j;
673 EFI_STATUS status;
674 int ret;
676 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
677 if (status != EFI_BUFFER_TOO_SMALL) {
678 snprintf(command_errbuf, sizeof (command_errbuf),
679 "unexpected error: %lld", (long long)status);
680 return (CMD_ERROR);
682 if ((buffer = malloc(bufsz)) == NULL) {
683 sprintf(command_errbuf, "out of memory");
684 return (CMD_ERROR);
687 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
688 if (EFI_ERROR(status)) {
689 free(buffer);
690 snprintf(command_errbuf, sizeof (command_errbuf),
691 "LocateHandle() error: %lld", (long long)status);
692 return (CMD_ERROR);
695 pager_open();
696 for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) {
697 UINTN nproto = 0;
698 EFI_GUID **protocols = NULL;
700 handle = buffer[i];
701 printf("Handle %p", handle);
702 if (pager_output("\n"))
703 break;
704 /* device path */
706 status = BS->ProtocolsPerHandle(handle, &protocols, &nproto);
707 if (EFI_ERROR(status)) {
708 snprintf(command_errbuf, sizeof (command_errbuf),
709 "ProtocolsPerHandle() error: %lld",
710 (long long)status);
711 continue;
714 for (j = 0; j < nproto; j++) {
715 if (efi_guid_to_name(protocols[j], &name) == true) {
716 printf(" %s", name);
717 free(name);
718 } else {
719 printf("Error while translating UUID to name");
721 if ((ret = pager_output("\n")) != 0)
722 break;
724 BS->FreePool(protocols);
725 if (ret != 0)
726 break;
728 pager_close();
729 free(buffer);
730 return (CMD_OK);
733 COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
734 command_lszfs);
736 static int
737 command_lszfs(int argc, char *argv[])
739 int err;
741 if (argc != 2) {
742 command_errmsg = "wrong number of arguments";
743 return (CMD_ERROR);
746 err = zfs_list(argv[1]);
747 if (err != 0) {
748 command_errmsg = strerror(err);
749 return (CMD_ERROR);
751 return (CMD_OK);
754 #ifdef __FreeBSD__
755 COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
756 command_reloadbe);
758 static int
759 command_reloadbe(int argc, char *argv[])
761 int err;
762 char *root;
764 if (argc > 2) {
765 command_errmsg = "wrong number of arguments";
766 return (CMD_ERROR);
769 if (argc == 2) {
770 err = zfs_bootenv(argv[1]);
771 } else {
772 root = getenv("zfs_be_root");
773 if (root == NULL) {
774 return (CMD_OK);
776 err = zfs_bootenv(root);
779 if (err != 0) {
780 command_errmsg = strerror(err);
781 return (CMD_ERROR);
784 return (CMD_OK);
786 #endif /* __FreeBSD__ */
788 #ifdef LOADER_FDT_SUPPORT
789 extern int command_fdt_internal(int argc, char *argv[]);
792 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
793 * and declaring it as extern is in contradiction with COMMAND_SET() macro
794 * (which uses static pointer), we're defining wrapper function, which
795 * calls the proper fdt handling routine.
797 static int
798 command_fdt(int argc, char *argv[])
800 return (command_fdt_internal(argc, argv));
803 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
804 #endif
807 * Chain load another efi loader.
809 static int
810 command_chain(int argc, char *argv[])
812 EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL;
813 EFI_HANDLE loaderhandle;
814 EFI_LOADED_IMAGE *loaded_image;
815 EFI_STATUS status;
816 struct stat st;
817 struct devdesc *dev;
818 char *name, *path;
819 void *buf;
820 int fd;
822 if (argc < 2) {
823 command_errmsg = "wrong number of arguments";
824 return (CMD_ERROR);
827 name = argv[1];
829 if ((fd = open(name, O_RDONLY)) < 0) {
830 command_errmsg = "no such file";
831 return (CMD_ERROR);
834 if (fstat(fd, &st) < -1) {
835 command_errmsg = "stat failed";
836 close(fd);
837 return (CMD_ERROR);
840 status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf);
841 if (status != EFI_SUCCESS) {
842 command_errmsg = "failed to allocate buffer";
843 close(fd);
844 return (CMD_ERROR);
846 if (read(fd, buf, st.st_size) != st.st_size) {
847 command_errmsg = "error while reading the file";
848 (void)BS->FreePool(buf);
849 close(fd);
850 return (CMD_ERROR);
852 close(fd);
853 status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle);
854 (void)BS->FreePool(buf);
855 if (status != EFI_SUCCESS) {
856 command_errmsg = "LoadImage failed";
857 return (CMD_ERROR);
859 status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID,
860 (void **)&loaded_image);
862 if (argc > 2) {
863 int i, len = 0;
864 CHAR16 *argp;
866 for (i = 2; i < argc; i++)
867 len += strlen(argv[i]) + 1;
869 len *= sizeof (*argp);
870 loaded_image->LoadOptions = argp = malloc (len);
871 if (loaded_image->LoadOptions == NULL) {
872 (void) BS->UnloadImage(loaded_image);
873 return (CMD_ERROR);
875 loaded_image->LoadOptionsSize = len;
876 for (i = 2; i < argc; i++) {
877 char *ptr = argv[i];
878 while (*ptr)
879 *(argp++) = *(ptr++);
880 *(argp++) = ' ';
882 *(--argv) = 0;
885 if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) {
886 struct zfs_devdesc *z_dev;
887 struct disk_devdesc *d_dev;
888 pdinfo_t *hd, *pd;
890 switch (dev->d_dev->dv_type) {
891 case DEVT_ZFS:
892 z_dev = (struct zfs_devdesc *)dev;
893 loaded_image->DeviceHandle =
894 efizfs_get_handle_by_guid(z_dev->pool_guid);
895 break;
896 case DEVT_NET:
897 loaded_image->DeviceHandle =
898 efi_find_handle(dev->d_dev, dev->d_unit);
899 break;
900 default:
901 hd = efiblk_get_pdinfo(dev);
902 if (STAILQ_EMPTY(&hd->pd_part)) {
903 loaded_image->DeviceHandle = hd->pd_handle;
904 break;
906 d_dev = (struct disk_devdesc *)dev;
907 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
909 * d_partition should be 255
911 if (pd->pd_unit == d_dev->d_slice) {
912 loaded_image->DeviceHandle =
913 pd->pd_handle;
914 break;
917 break;
921 dev_cleanup();
922 status = BS->StartImage(loaderhandle, NULL, NULL);
923 if (status != EFI_SUCCESS) {
924 command_errmsg = "StartImage failed";
925 free(loaded_image->LoadOptions);
926 loaded_image->LoadOptions = NULL;
927 status = BS->UnloadImage(loaded_image);
928 return (CMD_ERROR);
931 return (CMD_ERROR); /* not reached */
934 COMMAND_SET(chain, "chain", "chain load file", command_chain);