loader.efi: chain loader should provide proper device handle
[unleashed.git] / usr / src / boot / sys / boot / efi / loader / main.c
blob3570c4fd07bf4a9ea2fa517afe923c9eb4d60d08
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 extern char bootprog_info[];
56 struct arch_switch archsw; /* MI/MD interface boundary */
58 EFI_GUID devid = DEVICE_PATH_PROTOCOL;
59 EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
60 EFI_GUID smbios = SMBIOS_TABLE_GUID;
61 EFI_GUID smbios3 = SMBIOS3_TABLE_GUID;
62 EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL;
64 extern void acpi_detect(void);
65 extern void efi_getsmap(void);
67 static EFI_LOADED_IMAGE *img;
69 bool
70 efi_zfs_is_preferred(EFI_HANDLE *h)
72 return (h == img->DeviceHandle);
75 static int
76 has_keyboard(void)
78 EFI_STATUS status;
79 EFI_DEVICE_PATH *path;
80 EFI_HANDLE *hin, *hin_end, *walker;
81 UINTN sz;
82 int retval = 0;
85 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and
86 * do the typical dance to get the right sized buffer.
88 sz = 0;
89 hin = NULL;
90 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz, 0);
91 if (status == EFI_BUFFER_TOO_SMALL) {
92 hin = (EFI_HANDLE *)malloc(sz);
93 status = BS->LocateHandle(ByProtocol, &inputid, 0, &sz,
94 hin);
95 if (EFI_ERROR(status))
96 free(hin);
98 if (EFI_ERROR(status))
99 return retval;
102 * Look at each of the handles. If it supports the device path protocol,
103 * use it to get the device path for this handle. Then see if that
104 * device path matches either the USB device path for keyboards or the
105 * legacy device path for keyboards.
107 hin_end = &hin[sz / sizeof(*hin)];
108 for (walker = hin; walker < hin_end; walker++) {
109 status = BS->HandleProtocol(*walker, &devid, (VOID **)&path);
110 if (EFI_ERROR(status))
111 continue;
113 while (!IsDevicePathEnd(path)) {
115 * Check for the ACPI keyboard node. All PNP3xx nodes
116 * are keyboards of different flavors. Note: It is
117 * unclear of there's always a keyboard node when
118 * there's a keyboard controller, or if there's only one
119 * when a keyboard is detected at boot.
121 if (DevicePathType(path) == ACPI_DEVICE_PATH &&
122 (DevicePathSubType(path) == ACPI_DP ||
123 DevicePathSubType(path) == ACPI_EXTENDED_DP)) {
124 ACPI_HID_DEVICE_PATH *acpi;
126 acpi = (ACPI_HID_DEVICE_PATH *)(void *)path;
127 if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) == 0x300 &&
128 (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) {
129 retval = 1;
130 goto out;
133 * Check for USB keyboard node, if present. Unlike a
134 * PS/2 keyboard, these definitely only appear when
135 * connected to the system.
137 } else if (DevicePathType(path) == MESSAGING_DEVICE_PATH &&
138 DevicePathSubType(path) == MSG_USB_CLASS_DP) {
139 USB_CLASS_DEVICE_PATH *usb;
141 usb = (USB_CLASS_DEVICE_PATH *)(void *)path;
142 if (usb->DeviceClass == 3 && /* HID */
143 usb->DeviceSubClass == 1 && /* Boot devices */
144 usb->DeviceProtocol == 1) { /* Boot keyboards */
145 retval = 1;
146 goto out;
149 path = NextDevicePathNode(path);
152 out:
153 free(hin);
154 return retval;
157 static void
158 set_devdesc_currdev(struct devsw *dev, int unit)
160 struct devdesc currdev;
161 char *devname;
163 currdev.d_dev = dev;
164 currdev.d_type = currdev.d_dev->dv_type;
165 currdev.d_unit = unit;
166 currdev.d_opendata = NULL;
167 devname = efi_fmtdev(&currdev);
169 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
170 env_nounset);
171 env_setenv("loaddev", EV_VOLATILE, devname, env_noset, env_nounset);
174 static int
175 find_currdev(EFI_LOADED_IMAGE *img)
177 pdinfo_list_t *pdi_list;
178 pdinfo_t *dp, *pp;
179 EFI_DEVICE_PATH *devpath, *copy;
180 EFI_HANDLE h;
181 char *devname;
182 struct devsw *dev;
183 int unit;
184 uint64_t extra;
186 /* Did efi_zfs_probe() detect the boot pool? */
187 if (pool_guid != 0) {
188 struct zfs_devdesc currdev;
190 currdev.d_dev = &zfs_dev;
191 currdev.d_unit = 0;
192 currdev.d_type = currdev.d_dev->dv_type;
193 currdev.d_opendata = NULL;
194 currdev.pool_guid = pool_guid;
195 currdev.root_guid = 0;
196 devname = efi_fmtdev(&currdev);
198 env_setenv("currdev", EV_VOLATILE, devname, efi_setcurrdev,
199 env_nounset);
200 env_setenv("loaddev", EV_VOLATILE, devname, env_noset,
201 env_nounset);
202 return (0);
205 /* We have device lists for hd, cd, fd, walk them all. */
206 pdi_list = efiblk_get_pdinfo_list(&efipart_hddev);
207 STAILQ_FOREACH(dp, pdi_list, pd_link) {
208 struct disk_devdesc currdev;
210 currdev.d_dev = &efipart_hddev;
211 currdev.d_type = currdev.d_dev->dv_type;
212 currdev.d_unit = dp->pd_unit;
213 currdev.d_opendata = NULL;
214 currdev.d_slice = -1;
215 currdev.d_partition = -1;
217 if (dp->pd_handle == img->DeviceHandle) {
218 devname = efi_fmtdev(&currdev);
220 env_setenv("currdev", EV_VOLATILE, devname,
221 efi_setcurrdev, env_nounset);
222 env_setenv("loaddev", EV_VOLATILE, devname,
223 env_noset, env_nounset);
224 return (0);
226 /* Assuming GPT partitioning. */
227 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) {
228 if (pp->pd_handle == img->DeviceHandle) {
229 currdev.d_slice = pp->pd_unit;
230 currdev.d_partition = 255;
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);
237 return (0);
242 pdi_list = efiblk_get_pdinfo_list(&efipart_cddev);
243 STAILQ_FOREACH(dp, pdi_list, pd_link) {
244 if (dp->pd_handle == img->DeviceHandle ||
245 dp->pd_alias == img->DeviceHandle) {
246 set_devdesc_currdev(&efipart_cddev, dp->pd_unit);
247 return (0);
251 pdi_list = efiblk_get_pdinfo_list(&efipart_fddev);
252 STAILQ_FOREACH(dp, pdi_list, pd_link) {
253 if (dp->pd_handle == img->DeviceHandle) {
254 set_devdesc_currdev(&efipart_fddev, dp->pd_unit);
255 return (0);
260 * Try the device handle from our loaded image first. If that
261 * fails, use the device path from the loaded image and see if
262 * any of the nodes in that path match one of the enumerated
263 * handles.
265 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) {
266 set_devdesc_currdev(dev, unit);
267 return (0);
270 copy = NULL;
271 devpath = efi_lookup_image_devpath(IH);
272 while (devpath != NULL) {
273 h = efi_devpath_handle(devpath);
274 if (h == NULL)
275 break;
277 free(copy);
278 copy = NULL;
280 if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) {
281 set_devdesc_currdev(dev, unit);
282 return (0);
285 devpath = efi_lookup_devpath(h);
286 if (devpath != NULL) {
287 copy = efi_devpath_trim(devpath);
288 devpath = copy;
291 free(copy);
293 return (ENOENT);
296 EFI_STATUS
297 main(int argc, CHAR16 *argv[])
299 char var[128];
300 EFI_GUID *guid;
301 int i, j, vargood, howto;
302 void *ptr;
303 UINTN k;
304 int has_kbd;
306 archsw.arch_autoload = efi_autoload;
307 archsw.arch_getdev = efi_getdev;
308 archsw.arch_copyin = efi_copyin;
309 archsw.arch_copyout = efi_copyout;
310 archsw.arch_readin = efi_readin;
311 archsw.arch_loadaddr = efi_loadaddr;
312 archsw.arch_free_loadaddr = efi_free_loadaddr;
313 /* Note this needs to be set before ZFS init. */
314 archsw.arch_zfs_probe = efi_zfs_probe;
316 /* Get our loaded image protocol interface structure. */
317 BS->HandleProtocol(IH, &imgid, (VOID**)&img);
319 /* Init the time source */
320 efi_time_init();
322 has_kbd = has_keyboard();
325 * XXX Chicken-and-egg problem; we want to have console output
326 * early, but some console attributes may depend on reading from
327 * eg. the boot device, which we can't do yet. We can use
328 * printf() etc. once this is done.
330 cons_probe();
331 efi_getsmap();
334 * Initialise the block cache. Set the upper limit.
336 bcache_init(32768, 512);
339 * Parse the args to set the console settings, etc
340 * boot1.efi passes these in, if it can read /boot.config or /boot/config
341 * or iPXE may be setup to pass these in.
343 * Loop through the args, and for each one that contains an '=' that is
344 * not the first character, add it to the environment. This allows
345 * loader and kernel env vars to be passed on the command line. Convert
346 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied.
348 howto = 0;
349 for (i = 1; i < argc; i++) {
350 if (argv[i][0] == '-') {
351 for (j = 1; argv[i][j] != 0; j++) {
352 int ch;
354 ch = argv[i][j];
355 switch (ch) {
356 case 'a':
357 howto |= RB_ASKNAME;
358 break;
359 case 'd':
360 howto |= RB_KDB;
361 break;
362 case 'D':
363 howto |= RB_MULTIPLE;
364 break;
365 case 'h':
366 howto |= RB_SERIAL;
367 break;
368 case 'm':
369 howto |= RB_MUTE;
370 break;
371 case 'p':
372 howto |= RB_PAUSE;
373 break;
374 case 'P':
375 if (!has_kbd)
376 howto |= RB_SERIAL | RB_MULTIPLE;
377 break;
378 case 'r':
379 howto |= RB_DFLTROOT;
380 break;
381 case 's':
382 howto |= RB_SINGLE;
383 break;
384 case 'S':
385 if (argv[i][j + 1] == 0) {
386 if (i + 1 == argc) {
387 strncpy(var, "115200",
388 sizeof(var));
389 } else {
390 CHAR16 *ptr;
391 ptr = &argv[i + 1][0];
392 cpy16to8(ptr, var,
393 sizeof(var));
395 i++;
396 } else {
397 cpy16to8(&argv[i][j + 1], var,
398 sizeof(var));
400 strncat(var, ",8,n,1,-", sizeof(var));
401 setenv("ttya-mode", var, 1);
402 break;
403 case 'v':
404 howto |= RB_VERBOSE;
405 break;
408 } else {
409 vargood = 0;
410 for (j = 0; argv[i][j] != 0; j++) {
411 if (j == sizeof(var)) {
412 vargood = 0;
413 break;
415 if (j > 0 && argv[i][j] == '=')
416 vargood = 1;
417 var[j] = (char)argv[i][j];
419 if (vargood) {
420 var[j] = 0;
421 putenv(var);
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", "ttya text" , 1);
431 else
432 setenv("console", "text ttya" , 1);
433 } else if (howto & RB_SERIAL) {
434 setenv("console", "ttya" , 1);
438 * March through the device switch probing for things.
440 for (i = 0; devsw[i] != NULL; i++)
441 if (devsw[i]->dv_init != NULL)
442 (devsw[i]->dv_init)();
444 printf("Command line arguments:");
445 for (i = 0; i < argc; i++) {
446 printf(" %S", argv[i]);
448 printf("\n");
450 printf("Image base: 0x%lx\n", (u_long)img->ImageBase);
451 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
452 ST->Hdr.Revision & 0xffff);
453 printf("EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor,
454 ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
456 printf("\n%s", bootprog_info);
459 * Disable the watchdog timer. By default the boot manager sets
460 * the timer to 5 minutes before invoking a boot option. If we
461 * want to return to the boot manager, we have to disable the
462 * watchdog timer and since we're an interactive program, we don't
463 * want to wait until the user types "quit". The timer may have
464 * fired by then. We don't care if this fails. It does not prevent
465 * normal functioning in any way...
467 BS->SetWatchdogTimer(0, 0, 0, NULL);
469 if (find_currdev(img) != 0)
470 return (EFI_NOT_FOUND);
472 efi_init_environment();
473 setenv("ISADIR", "amd64", 1); /* we only build 64bit */
474 acpi_detect();
476 if ((ptr = efi_get_table(&smbios3)) == NULL)
477 ptr = efi_get_table(&smbios);
478 smbios_detect(ptr);
480 interact(NULL); /* doesn't return */
482 return (EFI_SUCCESS); /* keep compiler happy */
485 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
487 static int
488 command_reboot(int argc __unused, char *argv[] __unused)
490 int i;
492 for (i = 0; devsw[i] != NULL; ++i)
493 if (devsw[i]->dv_cleanup != NULL)
494 (devsw[i]->dv_cleanup)();
496 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
498 /* NOTREACHED */
499 return (CMD_ERROR);
502 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
504 static int
505 command_memmap(int argc __unused, char *argv[] __unused)
507 UINTN sz;
508 EFI_MEMORY_DESCRIPTOR *map, *p;
509 UINTN key, dsz;
510 UINT32 dver;
511 EFI_STATUS status;
512 int i, ndesc;
513 int rv = 0;
514 char line[80];
516 sz = 0;
517 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
518 if (status != EFI_BUFFER_TOO_SMALL) {
519 printf("Can't determine memory map size\n");
520 return (CMD_ERROR);
522 map = malloc(sz);
523 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
524 if (EFI_ERROR(status)) {
525 printf("Can't read memory map\n");
526 return (CMD_ERROR);
529 ndesc = sz / dsz;
530 snprintf(line, 80, "%23s %12s %12s %8s %4s\n",
531 "Type", "Physical", "Virtual", "#Pages", "Attr");
532 pager_open();
533 rv = pager_output(line);
534 if (rv) {
535 pager_close();
536 return (CMD_OK);
539 for (i = 0, p = map; i < ndesc;
540 i++, p = NextMemoryDescriptor(p, dsz)) {
541 snprintf(line, 80, "%23s %012lx %012lx %08lx ",
542 efi_memory_type(p->Type),
543 p->PhysicalStart,
544 p->VirtualStart,
545 p->NumberOfPages);
546 rv = pager_output(line);
547 if (rv)
548 break;
550 if (p->Attribute & EFI_MEMORY_UC)
551 printf("UC ");
552 if (p->Attribute & EFI_MEMORY_WC)
553 printf("WC ");
554 if (p->Attribute & EFI_MEMORY_WT)
555 printf("WT ");
556 if (p->Attribute & EFI_MEMORY_WB)
557 printf("WB ");
558 if (p->Attribute & EFI_MEMORY_UCE)
559 printf("UCE ");
560 if (p->Attribute & EFI_MEMORY_WP)
561 printf("WP ");
562 if (p->Attribute & EFI_MEMORY_RP)
563 printf("RP ");
564 if (p->Attribute & EFI_MEMORY_XP)
565 printf("XP ");
566 if (p->Attribute & EFI_MEMORY_NV)
567 printf("NV ");
568 if (p->Attribute & EFI_MEMORY_MORE_RELIABLE)
569 printf("MR ");
570 if (p->Attribute & EFI_MEMORY_RO)
571 printf("RO ");
572 rv = pager_output("\n");
573 if (rv)
574 break;
577 pager_close();
578 return (CMD_OK);
581 COMMAND_SET(configuration, "configuration", "print configuration tables",
582 command_configuration);
584 static int
585 command_configuration(int argc __unused, char *argv[] __unused)
587 UINTN i;
588 char *name;
590 printf("NumberOfTableEntries=%lu\n",
591 (unsigned long)ST->NumberOfTableEntries);
592 for (i = 0; i < ST->NumberOfTableEntries; i++) {
593 EFI_GUID *guid;
595 printf(" ");
596 guid = &ST->ConfigurationTable[i].VendorGuid;
598 if (efi_guid_to_name(guid, &name) == true) {
599 printf(name);
600 free(name);
601 } else {
602 printf("Error while translating UUID to name");
604 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
607 return (CMD_OK);
611 COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
613 static int
614 command_mode(int argc, char *argv[])
616 UINTN cols, rows;
617 unsigned int mode;
618 int i;
619 char *cp;
620 char rowenv[8];
621 EFI_STATUS status;
622 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
623 extern void HO(void);
625 conout = ST->ConOut;
627 if (argc > 1) {
628 mode = strtol(argv[1], &cp, 0);
629 if (cp[0] != '\0') {
630 printf("Invalid mode\n");
631 return (CMD_ERROR);
633 status = conout->QueryMode(conout, mode, &cols, &rows);
634 if (EFI_ERROR(status)) {
635 printf("invalid mode %d\n", mode);
636 return (CMD_ERROR);
638 status = conout->SetMode(conout, mode);
639 if (EFI_ERROR(status)) {
640 printf("couldn't set mode %d\n", mode);
641 return (CMD_ERROR);
643 sprintf(rowenv, "%u", (unsigned)rows);
644 setenv("LINES", rowenv, 1);
645 sprintf(rowenv, "%u", (unsigned)cols);
646 setenv("COLUMNS", rowenv, 1);
647 HO(); /* set cursor */
648 return (CMD_OK);
651 printf("Current mode: %d\n", conout->Mode->Mode);
652 for (i = 0; i <= conout->Mode->MaxMode; i++) {
653 status = conout->QueryMode(conout, i, &cols, &rows);
654 if (EFI_ERROR(status))
655 continue;
656 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
657 (unsigned)rows);
660 if (i != 0)
661 printf("Select a mode with the command \"mode <number>\"\n");
663 return (CMD_OK);
666 COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi);
668 static int
669 command_lsefi(int argc __unused, char *argv[] __unused)
671 char *name;
672 EFI_HANDLE *buffer = NULL;
673 EFI_HANDLE handle;
674 UINTN bufsz = 0, i, j;
675 EFI_STATUS status;
676 int ret;
678 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
679 if (status != EFI_BUFFER_TOO_SMALL) {
680 snprintf(command_errbuf, sizeof (command_errbuf),
681 "unexpected error: %lld", (long long)status);
682 return (CMD_ERROR);
684 if ((buffer = malloc(bufsz)) == NULL) {
685 sprintf(command_errbuf, "out of memory");
686 return (CMD_ERROR);
689 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
690 if (EFI_ERROR(status)) {
691 free(buffer);
692 snprintf(command_errbuf, sizeof (command_errbuf),
693 "LocateHandle() error: %lld", (long long)status);
694 return (CMD_ERROR);
697 pager_open();
698 for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) {
699 UINTN nproto = 0;
700 EFI_GUID **protocols = NULL;
702 handle = buffer[i];
703 printf("Handle %p", handle);
704 if (pager_output("\n"))
705 break;
706 /* device path */
708 status = BS->ProtocolsPerHandle(handle, &protocols, &nproto);
709 if (EFI_ERROR(status)) {
710 snprintf(command_errbuf, sizeof (command_errbuf),
711 "ProtocolsPerHandle() error: %lld",
712 (long long)status);
713 continue;
716 for (j = 0; j < nproto; j++) {
717 if (efi_guid_to_name(protocols[j], &name) == true) {
718 printf(" %s", name);
719 free(name);
720 } else {
721 printf("Error while translating UUID to name");
723 if ((ret = pager_output("\n")) != 0)
724 break;
726 BS->FreePool(protocols);
727 if (ret != 0)
728 break;
730 pager_close();
731 free(buffer);
732 return (CMD_OK);
735 COMMAND_SET(lszfs, "lszfs", "list child datasets of a zfs dataset",
736 command_lszfs);
738 static int
739 command_lszfs(int argc, char *argv[])
741 int err;
743 if (argc != 2) {
744 command_errmsg = "wrong number of arguments";
745 return (CMD_ERROR);
748 err = zfs_list(argv[1]);
749 if (err != 0) {
750 command_errmsg = strerror(err);
751 return (CMD_ERROR);
753 return (CMD_OK);
756 #ifdef __FreeBSD__
757 COMMAND_SET(reloadbe, "reloadbe", "refresh the list of ZFS Boot Environments",
758 command_reloadbe);
760 static int
761 command_reloadbe(int argc, char *argv[])
763 int err;
764 char *root;
766 if (argc > 2) {
767 command_errmsg = "wrong number of arguments";
768 return (CMD_ERROR);
771 if (argc == 2) {
772 err = zfs_bootenv(argv[1]);
773 } else {
774 root = getenv("zfs_be_root");
775 if (root == NULL) {
776 return (CMD_OK);
778 err = zfs_bootenv(root);
781 if (err != 0) {
782 command_errmsg = strerror(err);
783 return (CMD_ERROR);
786 return (CMD_OK);
788 #endif /* __FreeBSD__ */
790 #ifdef LOADER_FDT_SUPPORT
791 extern int command_fdt_internal(int argc, char *argv[]);
794 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
795 * and declaring it as extern is in contradiction with COMMAND_SET() macro
796 * (which uses static pointer), we're defining wrapper function, which
797 * calls the proper fdt handling routine.
799 static int
800 command_fdt(int argc, char *argv[])
802 return (command_fdt_internal(argc, argv));
805 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
806 #endif
809 * Chain load another efi loader.
811 static int
812 command_chain(int argc, char *argv[])
814 EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL;
815 EFI_HANDLE loaderhandle;
816 EFI_LOADED_IMAGE *loaded_image;
817 EFI_STATUS status;
818 struct stat st;
819 struct devdesc *dev;
820 char *name, *path;
821 void *buf;
822 int fd;
824 if (argc < 2) {
825 command_errmsg = "wrong number of arguments";
826 return (CMD_ERROR);
829 name = argv[1];
831 if ((fd = open(name, O_RDONLY)) < 0) {
832 command_errmsg = "no such file";
833 return (CMD_ERROR);
836 if (fstat(fd, &st) < -1) {
837 command_errmsg = "stat failed";
838 close(fd);
839 return (CMD_ERROR);
842 status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf);
843 if (status != EFI_SUCCESS) {
844 command_errmsg = "failed to allocate buffer";
845 close(fd);
846 return (CMD_ERROR);
848 if (read(fd, buf, st.st_size) != st.st_size) {
849 command_errmsg = "error while reading the file";
850 (void)BS->FreePool(buf);
851 close(fd);
852 return (CMD_ERROR);
854 close(fd);
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";
859 return (CMD_ERROR);
861 status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID,
862 (void **)&loaded_image);
864 if (argc > 2) {
865 int i, len = 0;
866 CHAR16 *argp;
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 if (loaded_image->LoadOptions == NULL) {
874 (void) BS->UnloadImage(loaded_image);
875 return (CMD_ERROR);
877 loaded_image->LoadOptionsSize = len;
878 for (i = 2; i < argc; i++) {
879 char *ptr = argv[i];
880 while (*ptr)
881 *(argp++) = *(ptr++);
882 *(argp++) = ' ';
884 *(--argv) = 0;
887 if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) {
888 struct zfs_devdesc *z_dev;
889 struct disk_devdesc *d_dev;
890 pdinfo_t *hd, *pd;
892 switch (dev->d_type) {
893 case DEVT_ZFS:
894 z_dev = (struct zfs_devdesc *)dev;
895 loaded_image->DeviceHandle =
896 efizfs_get_handle_by_guid(z_dev->pool_guid);
897 break;
898 case DEVT_NET:
899 loaded_image->DeviceHandle =
900 efi_find_handle(dev->d_dev, dev->d_unit);
901 break;
902 default:
903 hd = efiblk_get_pdinfo(dev);
904 if (STAILQ_EMPTY(&hd->pd_part)) {
905 loaded_image->DeviceHandle = hd->pd_handle;
906 break;
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 == d_dev->d_slice) {
914 loaded_image->DeviceHandle =
915 pd->pd_handle;
916 break;
919 break;
923 dev_cleanup();
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);
930 return (CMD_ERROR);
933 return (CMD_ERROR); /* not reached */
936 COMMAND_SET(chain, "chain", "chain load file", command_chain);