1 /*
2 * Copyright (c) 2008-2010 Rui Paulo
3 * Copyright (c) 2006 Marcel Moolenaar
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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.
15 *
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.
26 */
27
28 #include <sys/cdefs.h>
29
30 #include <sys/disk.h>
31 #include <sys/param.h>
32 #include <sys/reboot.h>
33 #include <sys/boot.h>
34 #include <sys/consplat.h>
35 #include <sys/zfs_bootenv.h>
36 #include <stand.h>
37 #include <inttypes.h>
38 #include <string.h>
39 #include <setjmp.h>
40 #include <disk.h>
41
42 #include <efi.h>
43 #include <efilib.h>
44 #include <efichar.h>
45 #include <eficonsctl.h>
46 #include <efidevp.h>
47 #include <Guid/SmBios.h>
48 #include <Protocol/DevicePath.h>
49 #include <Protocol/LoadedImage.h>
50 #include <Protocol/SerialIo.h>
51 #include <Protocol/SimpleTextIn.h>
52 #include <Uefi/UefiGpt.h>
53
54 #include <uuid.h>
55
56 #include <bootstrap.h>
57 #include <gfx_fb.h>
58 #include <smbios.h>
59
60 #include <libzfs.h>
61 #include <efizfs.h>
62
63 #include "loader_efi.h"
64
65 struct arch_switch archsw; /* MI/MD interface boundary */
66
67 EFI_GUID gEfiLoadedImageProtocolGuid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
68 EFI_GUID gEfiSmbiosTableGuid = SMBIOS_TABLE_GUID;
69 EFI_GUID gEfiSmbios3TableGuid = SMBIOS3_TABLE_GUID;
70
71 extern void efi_getsmap(void);
72
73 /*
74 * Number of seconds to wait for a keystroke before exiting with failure
75 * in the event no currdev is found. -2 means always break, -1 means
76 * never break, 0 means poll once and then reboot, > 0 means wait for
77 * that many seconds. "fail_timeout" can be set in the environment as
78 * well.
79 */
80 static int fail_timeout = 5;
81
82 bool
efi_zfs_is_preferred(EFI_HANDLE * h)83 efi_zfs_is_preferred(EFI_HANDLE *h)
84 {
85 EFI_DEVICE_PATH *devpath, *dp, *node;
86 HARDDRIVE_DEVICE_PATH *hd;
87 bool ret;
88 extern UINT64 start_sector; /* from mb_header.S */
89
90 /* This check is true for chainloader case. */
91 if (h == boot_img->DeviceHandle)
92 return (true);
93
94 /*
95 * Make sure the image was loaded from the hard disk.
96 */
97 devpath = efi_lookup_devpath(boot_img->DeviceHandle);
98 if (devpath == NULL)
99 return (false);
100 node = efi_devpath_last_node(devpath);
101 if (node == NULL)
102 return (false);
103 if (DevicePathType(node) != MEDIA_DEVICE_PATH ||
104 (DevicePathSubType(node) != MEDIA_FILEPATH_DP &&
105 DevicePathSubType(node) != MEDIA_HARDDRIVE_DP)) {
106 return (false);
107 }
108
109 /*
110 * XXX We ignore the MEDIA_FILEPATH_DP here for now as it is
111 * used on arm and we do not support arm.
112 */
113 ret = false;
114 dp = efi_devpath_trim(devpath);
115 devpath = NULL;
116 if (dp == NULL)
117 goto done;
118
119 devpath = efi_lookup_devpath(h);
120 if (devpath == NULL)
121 goto done;
122 hd = (HARDDRIVE_DEVICE_PATH *)efi_devpath_last_node(devpath);
123 if (hd == NULL) {
124 devpath = NULL;
125 goto done;
126 }
127 devpath = efi_devpath_trim(devpath);
128 if (devpath == NULL)
129 goto done;
130
131 if (!efi_devpath_match(dp, devpath))
132 goto done;
133
134 /* It is the same disk, do we have partition start? */
135 if (start_sector == 0)
136 ret = true;
137 else if (start_sector == hd->PartitionStart)
138 ret = true;
139
140 done:
141 free(dp);
142 free(devpath);
143 return (ret);
144 }
145
146 static bool
has_keyboard(void)147 has_keyboard(void)
148 {
149 EFI_STATUS status;
150 EFI_DEVICE_PATH *path;
151 EFI_HANDLE *hin;
152 uint_t i, nhandles;
153 bool retval = false;
154
155 /*
156 * Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and
157 * do the typical dance to get the right sized buffer.
158 */
159 status = efi_get_protocol_handles(&gEfiSimpleTextInProtocolGuid,
160 &nhandles, &hin);
161 if (EFI_ERROR(status))
162 return (retval);
163
164 /*
165 * Look at each of the handles. If it supports the device path protocol,
166 * use it to get the device path for this handle. Then see if that
167 * device path matches either the USB device path for keyboards or the
168 * legacy device path for keyboards.
169 */
170 for (i = 0; i < nhandles; i++) {
171 status = OpenProtocolByHandle(hin[i],
172 &gEfiDevicePathProtocolGuid, (void **)&path);
173 if (EFI_ERROR(status))
174 continue;
175
176 while (!IsDevicePathEnd(path)) {
177 /*
178 * Check for the ACPI keyboard node. All PNP3xx nodes
179 * are keyboards of different flavors. Note: It is
180 * unclear of there's always a keyboard node when
181 * there's a keyboard controller, or if there's only one
182 * when a keyboard is detected at boot.
183 */
184 if (DevicePathType(path) == ACPI_DEVICE_PATH &&
185 (DevicePathSubType(path) == ACPI_DP ||
186 DevicePathSubType(path) == ACPI_EXTENDED_DP)) {
187 ACPI_HID_DEVICE_PATH *acpi;
188
189 acpi = (ACPI_HID_DEVICE_PATH *)(void *)path;
190 if ((EISA_ID_TO_NUM(acpi->HID) & 0xff00) ==
191 0x300 &&
192 (acpi->HID & 0xffff) == PNP_EISA_ID_CONST) {
193 retval = true;
194 goto out;
195 }
196 /*
197 * Check for USB keyboard node, if present. Unlike a
198 * PS/2 keyboard, these definitely only appear when
199 * connected to the system.
200 */
201 } else if (DevicePathType(path) ==
202 MESSAGING_DEVICE_PATH &&
203 DevicePathSubType(path) == MSG_USB_CLASS_DP) {
204 USB_CLASS_DEVICE_PATH *usb;
205
206 /*
207 * Check for:
208 * DeviceClass: HID
209 * DeviceSubClass: Boot devices
210 * DeviceProtocol: Boot keyboards
211 */
212 usb = (USB_CLASS_DEVICE_PATH *)(void *)path;
213 if (usb->DeviceClass == 3 &&
214 usb->DeviceSubClass == 1 &&
215 usb->DeviceProtocol == 1) {
216 retval = true;
217 goto out;
218 }
219 }
220 path = NextDevicePathNode(path);
221 }
222 }
223 out:
224 free(hin);
225 return (retval);
226 }
227
228 static void
set_currdev(const char * devname)229 set_currdev(const char *devname)
230 {
231
232 /*
233 * Don't execute hooks here; we may need to try setting these more than
234 * once here if we're probing for the ZFS pool we're supposed to boot.
235 * The currdev hook is intended to just validate user input anyways,
236 * while the loaddev hook makes it immutable once we've determined what
237 * the proper currdev is.
238 */
239 env_setenv("currdev", EV_VOLATILE | EV_NOHOOK, devname, efi_setcurrdev,
240 env_nounset);
241 env_setenv("loaddev", EV_VOLATILE | EV_NOHOOK, devname, env_noset,
242 env_nounset);
243 }
244
245 static void
set_currdev_devdesc(struct devdesc * currdev)246 set_currdev_devdesc(struct devdesc *currdev)
247 {
248 char *devname;
249
250 devname = efi_fmtdev(currdev);
251
252 printf("Setting currdev to %s\n", devname);
253 set_currdev(devname);
254 }
255
256 static void
set_currdev_devsw(struct devsw * dev,int unit)257 set_currdev_devsw(struct devsw *dev, int unit)
258 {
259 struct devdesc currdev;
260
261 currdev.d_dev = dev;
262 currdev.d_unit = unit;
263
264 set_currdev_devdesc(&currdev);
265 }
266
267 static void
set_currdev_pdinfo(pdinfo_t * dp)268 set_currdev_pdinfo(pdinfo_t *dp)
269 {
270
271 /*
272 * Disks are special: they have partitions. if the parent
273 * pointer is non-null, we're a partition not a full disk
274 * and we need to adjust currdev appropriately.
275 */
276 if (dp->pd_devsw->dv_type == DEVT_DISK) {
277 struct disk_devdesc currdev;
278
279 currdev.dd.d_dev = dp->pd_devsw;
280 if (dp->pd_parent == NULL) {
281 currdev.dd.d_unit = dp->pd_unit;
282 currdev.d_slice = D_SLICENONE;
283 currdev.d_partition = D_PARTNONE;
284 } else {
285 currdev.dd.d_unit = dp->pd_parent->pd_unit;
286 currdev.d_slice = dp->pd_unit;
287 currdev.d_partition = D_PARTISGPT; /* Assumes GPT */
288 }
289 set_currdev_devdesc((struct devdesc *)&currdev);
290 } else {
291 set_currdev_devsw(dp->pd_devsw, dp->pd_unit);
292 }
293 }
294
295 static bool
sanity_check_currdev(void)296 sanity_check_currdev(void)
297 {
298 struct stat st;
299
300 return (stat("/boot/defaults/loader.conf", &st) == 0);
301 }
302
303 static bool
probe_zfs_currdev(uint64_t guid)304 probe_zfs_currdev(uint64_t guid)
305 {
306 struct zfs_devdesc currdev;
307 char *bootonce;
308 bool rv;
309
310 currdev.dd.d_dev = &zfs_dev;
311 currdev.dd.d_unit = 0;
312 currdev.pool_guid = guid;
313 currdev.root_guid = 0;
314 set_currdev_devdesc((struct devdesc *)&currdev);
315
316 rv = sanity_check_currdev();
317 if (rv) {
318 bootonce = malloc(VDEV_PAD_SIZE);
319 if (bootonce != NULL) {
320 if (zfs_get_bootonce(&currdev, OS_BOOTONCE, bootonce,
321 VDEV_PAD_SIZE) == 0) {
322 printf("zfs bootonce: %s\n", bootonce);
323 set_currdev(bootonce);
324 setenv("zfs-bootonce", bootonce, 1);
325 }
326 free(bootonce);
327 (void) zfs_attach_nvstore(&currdev);
328 } else {
329 printf("Failed to process bootonce data: %s\n",
330 strerror(errno));
331 }
332 }
333 return (rv);
334 }
335
336 static bool
try_as_currdev(pdinfo_t * pp)337 try_as_currdev(pdinfo_t *pp)
338 {
339 uint64_t guid;
340
341 /*
342 * If there's a zpool on this device, try it as a ZFS
343 * filesystem, which has somewhat different setup than all
344 * other types of fs due to imperfect loader integration.
345 * This all stems from ZFS being both a device (zpool) and
346 * a filesystem, plus the boot env feature.
347 */
348 if (efizfs_get_guid_by_handle(pp->pd_handle, &guid))
349 return (probe_zfs_currdev(guid));
350
351 /*
352 * All other filesystems just need the pdinfo
353 * initialized in the standard way.
354 */
355 set_currdev_pdinfo(pp);
356 return (sanity_check_currdev());
357 }
358
359 static bool
find_currdev(EFI_LOADED_IMAGE_PROTOCOL * img)360 find_currdev(EFI_LOADED_IMAGE_PROTOCOL *img)
361 {
362 pdinfo_t *dp, *pp;
363 EFI_DEVICE_PATH *devpath, *copy;
364 EFI_HANDLE h;
365 CHAR16 *text;
366 struct devsw *dev;
367 int unit;
368 uint64_t extra;
369
370 /*
371 * Did efi_zfs_probe() detect the boot pool? If so, use the zpool
372 * it found, if it's sane. ZFS is the only thing that looks for
373 * disks and pools to boot.
374 */
375 if (pool_guid != 0) {
376 printf("Trying ZFS pool\n");
377 if (probe_zfs_currdev(pool_guid))
378 return (true);
379 }
380
381 /*
382 * Try to find the block device by its handle based on the
383 * image we're booting. If we can't find a sane partition,
384 * search all the other partitions of the disk. We do not
385 * search other disks because it's a violation of the UEFI
386 * boot protocol to do so. We fail and let UEFI go on to
387 * the next candidate.
388 */
389 dp = efiblk_get_pdinfo_by_handle(img->DeviceHandle);
390 if (dp != NULL) {
391 text = efi_devpath_name(dp->pd_devpath);
392 if (text != NULL) {
393 printf("Trying ESP: %S\n", text);
394 efi_free_devpath_name(text);
395 }
396 set_currdev_pdinfo(dp);
397 if (sanity_check_currdev())
398 return (true);
399 if (dp->pd_parent != NULL) {
400 dp = dp->pd_parent;
401 STAILQ_FOREACH(pp, &dp->pd_part, pd_link) {
402 text = efi_devpath_name(pp->pd_devpath);
403 if (text != NULL) {
404 printf("And now the part: %S\n", text);
405 efi_free_devpath_name(text);
406 }
407 /*
408 * Roll up the ZFS special case
409 * for those partitions that have
410 * zpools on them
411 */
412 if (try_as_currdev(pp))
413 return (true);
414 }
415 }
416 }
417
418 /*
419 * Try the device handle from our loaded image first. If that
420 * fails, use the device path from the loaded image and see if
421 * any of the nodes in that path match one of the enumerated
422 * handles. Currently, this handle list is only for netboot.
423 */
424 if (efi_handle_lookup(img->DeviceHandle, &dev, &unit, &extra) == 0) {
425 set_currdev_devsw(dev, unit);
426 if (sanity_check_currdev())
427 return (true);
428 }
429
430 copy = NULL;
431 devpath = efi_lookup_image_devpath(IH);
432 while (devpath != NULL) {
433 h = efi_devpath_handle(devpath);
434 if (h == NULL)
435 break;
436
437 free(copy);
438 copy = NULL;
439
440 if (efi_handle_lookup(h, &dev, &unit, &extra) == 0) {
441 set_currdev_devsw(dev, unit);
442 if (sanity_check_currdev())
443 return (true);
444 }
445
446 devpath = efi_lookup_devpath(h);
447 if (devpath != NULL) {
448 copy = efi_devpath_trim(devpath);
449 devpath = copy;
450 }
451 }
452 free(copy);
453
454 return (false);
455 }
456
457 static bool
interactive_interrupt(const char * msg)458 interactive_interrupt(const char *msg)
459 {
460 time_t now, then, last;
461
462 last = 0;
463 now = then = getsecs();
464 printf("%s\n", msg);
465 if (fail_timeout == -2) /* Always break to OK */
466 return (true);
467 if (fail_timeout == -1) /* Never break to OK */
468 return (false);
469 do {
470 if (last != now) {
471 printf("press any key to interrupt reboot "
472 "in %d seconds\r",
473 fail_timeout - (int)(now - then));
474 last = now;
475 }
476
477 /* XXX no pause or timeout wait for char */
478 if (ischar())
479 return (true);
480 now = getsecs();
481 } while (now - then < fail_timeout);
482 return (false);
483 }
484
485 static void
setenv_int(const char * key,int val)486 setenv_int(const char *key, int val)
487 {
488 char buf[20];
489
490 (void) snprintf(buf, sizeof (buf), "%d", val);
491 (void) setenv(key, buf, 1);
492 }
493
494 /*
495 * Parse ConOut (the list of consoles active) and see if we can find a
496 * serial port and/or a video port. It would be nice to also walk the
497 * ACPI name space to map the UID for the serial port to a port. The
498 * latter is especially hard.
499 */
500 static int
parse_uefi_con_out(void)501 parse_uefi_con_out(void)
502 {
503 int how, rv;
504 int vid_seen = 0, com_seen = 0, seen = 0;
505 size_t sz;
506 char buf[4096], *ep;
507 EFI_DEVICE_PATH *node;
508 ACPI_HID_DEVICE_PATH *acpi;
509 UART_DEVICE_PATH *uart;
510 bool pci_pending = false;
511
512 how = 0;
513 sz = sizeof (buf);
514 rv = efi_global_getenv("ConOut", buf, &sz);
515 if (rv != EFI_SUCCESS)
516 rv = efi_global_getenv("ConOutDev", buf, &sz);
517 if (rv != EFI_SUCCESS) {
518 /*
519 * If we don't have any ConOut default to video.
520 * non-server systems may not have serial.
521 */
522 goto out;
523 }
524 ep = buf + sz;
525 node = (EFI_DEVICE_PATH *)buf;
526 while ((char *)node < ep) {
527 if (IsDevicePathEndType(node)) {
528 if (pci_pending && vid_seen == 0)
529 vid_seen = ++seen;
530 }
531 pci_pending = false;
532 if (DevicePathType(node) == ACPI_DEVICE_PATH &&
533 (DevicePathSubType(node) == ACPI_DP ||
534 DevicePathSubType(node) == ACPI_EXTENDED_DP)) {
535 /* Check for Serial node */
536 acpi = (void *)node;
537 if (EISA_ID_TO_NUM(acpi->HID) == 0x501) {
538 setenv_int("efi_8250_uid", acpi->UID);
539 com_seen = ++seen;
540 }
541 } else if (DevicePathType(node) == MESSAGING_DEVICE_PATH &&
542 DevicePathSubType(node) == MSG_UART_DP) {
543 com_seen = ++seen;
544 uart = (void *)node;
545 setenv_int("efi_com_speed", uart->BaudRate);
546 } else if (DevicePathType(node) == ACPI_DEVICE_PATH &&
547 DevicePathSubType(node) == ACPI_ADR_DP) {
548 /* Check for AcpiAdr() Node for video */
549 vid_seen = ++seen;
550 } else if (DevicePathType(node) == HARDWARE_DEVICE_PATH &&
551 DevicePathSubType(node) == HW_PCI_DP) {
552 /*
553 * Note, vmware fusion has a funky console device
554 * PciRoot(0x0)/Pci(0xf,0x0)
555 * which we can only detect at the end since we also
556 * have to cope with:
557 * PciRoot(0x0)/Pci(0x1f,0x0)/Serial(0x1)
558 * so only match it if it's last.
559 */
560 pci_pending = true;
561 }
562 node = NextDevicePathNode(node); /* Skip the end node */
563 }
564
565 /*
566 * Truth table for RB_MULTIPLE | RB_SERIAL
567 * Value Result
568 * 0 Use only video console
569 * RB_SERIAL Use only serial console
570 * RB_MULTIPLE Use both video and serial console
571 * (but video is primary so gets rc messages)
572 * both Use both video and serial console
573 * (but serial is primary so gets rc messages)
574 *
575 * Try to honor this as best we can. If only one of serial / video
576 * found, then use that. Otherwise, use the first one we found.
577 * This also implies if we found nothing, default to video.
578 */
579 how = 0;
580 if (vid_seen && com_seen) {
581 how |= RB_MULTIPLE;
582 if (com_seen < vid_seen)
583 how |= RB_SERIAL;
584 } else if (com_seen)
585 how |= RB_SERIAL;
586 out:
587 return (how);
588 }
589
590 caddr_t
ptov(uintptr_t x)591 ptov(uintptr_t x)
592 {
593 return ((caddr_t)x);
594 }
595
596 uintptr_t
vtop(caddr_t x)597 vtop(caddr_t x)
598 {
599 return ((uintptr_t)x);
600 }
601
602 static int
efi_serial_get_uid(EFI_DEVICE_PATH * devpath)603 efi_serial_get_uid(EFI_DEVICE_PATH *devpath)
604 {
605 ACPI_HID_DEVICE_PATH *acpi;
606
607 while (!IsDevicePathEnd(devpath)) {
608 if (DevicePathType(devpath) == ACPI_DEVICE_PATH &&
609 (DevicePathSubType(devpath) == ACPI_DP ||
610 DevicePathSubType(devpath) == ACPI_EXTENDED_DP)) {
611 acpi = (ACPI_HID_DEVICE_PATH *)devpath;
612 if (EISA_ID_TO_NUM(acpi->HID) == 0x501) {
613 return (acpi->UID);
614 }
615 }
616
617 devpath = NextDevicePathNode(devpath);
618 }
619 return (-1);
620 }
621
622 /*
623 * Walk serialio protocol handle array and find index for serial console
624 * device. The problem is, we check for acpi UID value, but we can not be sure,
625 * if it will start from 0 or 1.
626 */
627 static const char *
uefi_serial_console(void)628 uefi_serial_console(void)
629 {
630 EFI_STATUS status;
631 EFI_HANDLE *handles;
632 uint_t i, nhandles;
633 unsigned long uid, lowest;
634 char *env, *ep;
635
636 env = getenv("efi_8250_uid");
637 if (env == NULL)
638 return (NULL);
639 (void) unsetenv("efi_8250_uid");
640 errno = 0;
641 uid = strtoul(env, &ep, 10);
642 if (errno != 0 || *ep != '\0')
643 return (NULL);
644
645 /* if uid is 0, this is first serial port */
646 if (uid == 0)
647 return ("ttya");
648
649 status = efi_get_protocol_handles(&gEfiSerialIoProtocolGuid,
650 &nhandles, &handles);
651 if (EFI_ERROR(status)) {
652 return (NULL);
653 }
654
655 lowest = 255; /* high enough value */
656 for (i = 0; i < nhandles; i++) {
657 EFI_DEVICE_PATH *devpath;
658 unsigned long _uid;
659
660 devpath = efi_lookup_devpath(handles[i]);
661 _uid = efi_serial_get_uid(devpath);
662 if (_uid < lowest)
663 lowest = _uid;
664 }
665 free(handles);
666 switch (uid - lowest) {
667 case 0:
668 return ("ttya");
669 case 1:
670 return ("ttyb");
671 case 2:
672 return ("ttyc");
673 case 3:
674 return ("ttyd");
675 }
676 return (NULL);
677 }
678
679 EFI_STATUS
main(int argc,CHAR16 * argv[])680 main(int argc, CHAR16 *argv[])
681 {
682 char var[128];
683 int i, j, howto;
684 bool vargood;
685 void *ptr;
686 bool has_kbd;
687 char *s;
688 const char *serial;
689 EFI_DEVICE_PATH *imgpath;
690 CHAR16 *text;
691 EFI_STATUS status;
692 UINT16 boot_current;
693 size_t sz;
694 UINT16 boot_order[100];
695
696 archsw.arch_autoload = efi_autoload;
697 archsw.arch_getdev = efi_getdev;
698 archsw.arch_copyin = efi_copyin;
699 archsw.arch_copyout = efi_copyout;
700 archsw.arch_readin = efi_readin;
701 archsw.arch_loadaddr = efi_loadaddr;
702 archsw.arch_free_loadaddr = efi_free_loadaddr;
703 #if defined(__amd64) || defined(__i386)
704 archsw.arch_hypervisor = x86_hypervisor;
705 #endif
706 /* Note this needs to be set before ZFS init. */
707 archsw.arch_zfs_probe = efi_zfs_probe;
708
709 /*
710 * XXX Chicken-and-egg problem; we want to have console output
711 * early, but some console attributes may depend on reading from
712 * eg. the boot device, which we can't do yet. We can use
713 * printf() etc. once this is done.
714 */
715 setenv("console", "text", 1);
716 howto = parse_uefi_con_out();
717 serial = uefi_serial_console();
718 cons_probe();
719 efi_getsmap();
720
721 if ((s = getenv("efi_com_speed")) != NULL) {
722 char *name;
723
724 (void) snprintf(var, sizeof (var), "%s,8,n,1,-", s);
725 if (asprintf(&name, "%s-mode", serial) > 0) {
726 (void) setenv(name, var, 1);
727 free(name);
728 }
729 if (asprintf(&name, "%s-spcr-mode", serial) > 0) {
730 (void) setenv(name, var, 1);
731 free(name);
732 }
733 (void) unsetenv("efi_com_speed");
734 }
735
736 /* Init the time source */
737 efi_time_init();
738
739 /*
740 * Initialise the block cache. Set the upper limit.
741 */
742 bcache_init(32768, 512);
743
744 has_kbd = has_keyboard();
745
746 /*
747 * Parse the args to set the console settings, etc
748 * iPXE may be setup to pass these in. Or the optional argument in the
749 * boot environment was used to pass these arguments in (in which case
750 * neither /boot.config nor /boot/config are consulted).
751 *
752 * Loop through the args, and for each one that contains an '=' that is
753 * not the first character, add it to the environment. This allows
754 * loader and kernel env vars to be passed on the command line. Convert
755 * args from UCS-2 to ASCII (16 to 8 bit) as they are copied (though
756 * this method is flawed for non-ASCII characters).
757 */
758 for (i = 1; i < argc; i++) {
759 if (argv[i][0] == '-') {
760 for (j = 1; argv[i][j] != 0; j++) {
761 int ch;
762
763 ch = argv[i][j];
764 switch (ch) {
765 case 'a':
766 howto |= RB_ASKNAME;
767 break;
768 case 'd':
769 howto |= RB_KDB;
770 break;
771 case 'D':
772 howto |= RB_MULTIPLE;
773 break;
774 case 'h':
775 howto |= RB_SERIAL;
776 break;
777 case 'm':
778 howto |= RB_MUTE;
779 break;
780 case 'p':
781 howto |= RB_PAUSE;
782 break;
783 case 'P':
784 if (!has_kbd) {
785 howto |= RB_SERIAL;
786 howto |= RB_MULTIPLE;
787 }
788 break;
789 case 'r':
790 howto |= RB_DFLTROOT;
791 break;
792 case 's':
793 howto |= RB_SINGLE;
794 break;
795 case 'S':
796 if (argv[i][j + 1] == 0) {
797 if (i + 1 == argc) {
798 strncpy(var, "115200",
799 sizeof (var));
800 } else {
801 CHAR16 *ptr;
802 ptr = &argv[i + 1][0];
803 cpy16to8(ptr, var,
804 sizeof (var));
805 }
806 i++;
807 } else {
808 cpy16to8(&argv[i][j + 1], var,
809 sizeof (var));
810 }
811 strncat(var, ",8,n,1,-", sizeof (var));
812 setenv("ttya-mode", var, 1);
813 break;
814 case 'v':
815 howto |= RB_VERBOSE;
816 break;
817 }
818 }
819 } else {
820 vargood = false;
821 for (j = 0; argv[i][j] != 0; j++) {
822 if (j == sizeof (var)) {
823 vargood = false;
824 break;
825 }
826 if (j > 0 && argv[i][j] == '=')
827 vargood = true;
828 var[j] = (char)argv[i][j];
829 }
830 if (vargood) {
831 var[j] = 0;
832 putenv(var);
833 }
834 }
835 }
836 for (i = 0; howto_names[i].ev != NULL; i++)
837 if (howto & howto_names[i].mask)
838 setenv(howto_names[i].ev, "YES", 1);
839
840 /*
841 * XXX we need fallback to this stuff after looking at the ConIn,
842 * ConOut and ConErr variables.
843 */
844 if (howto & RB_MULTIPLE) {
845 if (howto & RB_SERIAL)
846 (void) snprintf(var, sizeof (var), "%s text", serial);
847 else
848 (void) snprintf(var, sizeof (var), "text %s", serial);
849 } else if (howto & RB_SERIAL) {
850 (void) snprintf(var, sizeof (var), "%s", serial);
851 } else {
852 (void) snprintf(var, sizeof (var), "text");
853 }
854 (void) setenv("console", var, 1);
855
856 if ((s = getenv("fail_timeout")) != NULL)
857 fail_timeout = strtol(s, NULL, 10);
858
859 /*
860 * Scan the BLOCK IO MEDIA handles then
861 * march through the device switch probing for things.
862 */
863 if ((i = efipart_inithandles()) == 0) {
864 for (i = 0; devsw[i] != NULL; i++)
865 if (devsw[i]->dv_init != NULL)
866 (devsw[i]->dv_init)();
867 } else
868 printf("efipart_inithandles failed %d, expect failures", i);
869
870 printf("Command line arguments:");
871 for (i = 0; i < argc; i++) {
872 printf(" %S", argv[i]);
873 }
874 printf("\n");
875
876 printf("Image base: 0x%lx\n", (unsigned long)boot_img->ImageBase);
877 printf("EFI version: %d.%02d\n", ST->Hdr.Revision >> 16,
878 ST->Hdr.Revision & 0xffff);
879 printf("EFI Firmware: %S (rev %d.%02d)\n", ST->FirmwareVendor,
880 ST->FirmwareRevision >> 16, ST->FirmwareRevision & 0xffff);
881
882 printf("\n%s", bootprog_info);
883
884 /* Determine the devpath of our image so we can prefer it. */
885 text = efi_devpath_name(boot_img->FilePath);
886 if (text != NULL) {
887 printf(" Load Path: %S\n", text);
888 efi_setenv_illumos_wcs("LoaderPath", text);
889 efi_free_devpath_name(text);
890 }
891
892 status = OpenProtocolByHandle(boot_img->DeviceHandle,
893 &gEfiDevicePathProtocolGuid, (void **)&imgpath);
894 if (status == EFI_SUCCESS) {
895 text = efi_devpath_name(imgpath);
896 if (text != NULL) {
897 printf(" Load Device: %S\n", text);
898 efi_setenv_illumos_wcs("LoaderDev", text);
899 efi_free_devpath_name(text);
900 }
901 }
902
903 boot_current = 0;
904 sz = sizeof (boot_current);
905 efi_global_getenv("BootCurrent", &boot_current, &sz);
906 printf(" BootCurrent: %04x\n", boot_current);
907
908 sz = sizeof (boot_order);
909 efi_global_getenv("BootOrder", &boot_order, &sz);
910 printf(" BootOrder:");
911 for (i = 0; i < sz / sizeof (boot_order[0]); i++)
912 printf(" %04x%s", boot_order[i],
913 boot_order[i] == boot_current ? "[*]" : "");
914 printf("\n");
915
916 /*
917 * Disable the watchdog timer. By default the boot manager sets
918 * the timer to 5 minutes before invoking a boot option. If we
919 * want to return to the boot manager, we have to disable the
920 * watchdog timer and since we're an interactive program, we don't
921 * want to wait until the user types "quit". The timer may have
922 * fired by then. We don't care if this fails. It does not prevent
923 * normal functioning in any way...
924 */
925 BS->SetWatchdogTimer(0, 0, 0, NULL);
926
927 /*
928 * Try and find a good currdev based on the image that was booted.
929 * It might be desirable here to have a short pause to allow falling
930 * through to the boot loader instead of returning instantly to follow
931 * the boot protocol and also allow an escape hatch for users wishing
932 * to try something different.
933 */
934 if (!find_currdev(boot_img))
935 if (!interactive_interrupt("Failed to find bootable partition"))
936 return (EFI_NOT_FOUND);
937
938 autoload_font(false); /* Set up the font list for console. */
939 efi_init_environment();
940 bi_isadir(); /* set ISADIR */
941 acpi_detect();
942
943 if ((ptr = efi_get_table(&gEfiSmbios3TableGuid)) == NULL)
944 ptr = efi_get_table(&gEfiSmbiosTableGuid);
945 smbios_detect(ptr);
946
947 interact(NULL); /* doesn't return */
948
949 return (EFI_SUCCESS); /* keep compiler happy */
950 }
951
952 COMMAND_SET(reboot, "reboot", "reboot the system", command_reboot);
953
954 static void
fw_setup(void)955 fw_setup(void)
956 {
957 uint64_t os_indications;
958 size_t size;
959 EFI_STATUS status;
960
961 size = sizeof (os_indications);
962 status = efi_global_getenv("OsIndicationsSupported",
963 &os_indications, &size);
964 if (EFI_ERROR(status) || size != sizeof (os_indications) ||
965 (os_indications & EFI_OS_INDICATIONS_BOOT_TO_FW_UI) == 0) {
966 printf("Booting to Firmware UI is not supported in "
967 "this system.");
968 for (int i = 0; i < 3; i++) {
969 delay(1000 * 1000); /* 1 second */
970 if (ischar())
971 break;
972 }
973 return;
974 }
975
976 os_indications = EFI_OS_INDICATIONS_BOOT_TO_FW_UI;
977
978 status = efi_global_setenv("OsIndications", &os_indications,
979 sizeof (os_indications));
980 }
981
982 static int
command_reboot(int argc,char * argv[])983 command_reboot(int argc, char *argv[])
984 {
985 int i, ch;
986 bool fw = false;
987
988 optind = 1;
989 optreset = 1;
990
991 while ((ch = getopt(argc, argv, "fh")) != -1) {
992 switch (ch) {
993 case 'f':
994 fw = true;
995 break;
996 case 'h':
997 printf("Usage: reboot [-f]\n");
998 return (CMD_OK);
999 case '?':
1000 default:
1001 return (CMD_OK);
1002 }
1003 }
1004
1005 if (fw || getenv("BOOT_TO_FW_UI") != NULL)
1006 fw_setup();
1007
1008 for (i = 0; devsw[i] != NULL; ++i)
1009 if (devsw[i]->dv_cleanup != NULL)
1010 (devsw[i]->dv_cleanup)();
1011
1012 RS->ResetSystem(EfiResetCold, EFI_SUCCESS, 0, NULL);
1013
1014 /* NOTREACHED */
1015 return (CMD_ERROR);
1016 }
1017
1018 COMMAND_SET(poweroff, "poweroff", "power off the system", command_poweroff);
1019
1020 static int
command_poweroff(int argc __unused,char * argv[]__unused)1021 command_poweroff(int argc __unused, char *argv[] __unused)
1022 {
1023 int i;
1024
1025 for (i = 0; devsw[i] != NULL; ++i)
1026 if (devsw[i]->dv_cleanup != NULL)
1027 (devsw[i]->dv_cleanup)();
1028
1029 RS->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
1030
1031 /* NOTREACHED */
1032 return (CMD_ERROR);
1033 }
1034
1035 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap);
1036
1037 static int
command_memmap(int argc __unused,char * argv[]__unused)1038 command_memmap(int argc __unused, char *argv[] __unused)
1039 {
1040 UINTN sz;
1041 EFI_MEMORY_DESCRIPTOR *map, *p;
1042 UINTN key, dsz;
1043 UINT32 dver;
1044 EFI_STATUS status;
1045 int i, ndesc;
1046 int rv = 0;
1047 char line[80];
1048
1049 sz = 0;
1050 status = BS->GetMemoryMap(&sz, 0, &key, &dsz, &dver);
1051 if (status != EFI_BUFFER_TOO_SMALL) {
1052 printf("Can't determine memory map size\n");
1053 return (CMD_ERROR);
1054 }
1055 map = malloc(sz);
1056 status = BS->GetMemoryMap(&sz, map, &key, &dsz, &dver);
1057 if (EFI_ERROR(status)) {
1058 printf("Can't read memory map\n");
1059 return (CMD_ERROR);
1060 }
1061
1062 ndesc = sz / dsz;
1063 snprintf(line, 80, "%23s %12s %12s %8s %4s\n",
1064 "Type", "Physical", "Virtual", "#Pages", "Attr");
1065 pager_open();
1066 rv = pager_output(line);
1067 if (rv) {
1068 pager_close();
1069 return (CMD_OK);
1070 }
1071
1072 for (i = 0, p = map; i < ndesc;
1073 i++, p = NextMemoryDescriptor(p, dsz)) {
1074 snprintf(line, 80, "%23s %012jx %012jx %08jx ",
1075 efi_memory_type(p->Type), p->PhysicalStart,
1076 p->VirtualStart, p->NumberOfPages);
1077 rv = pager_output(line);
1078 if (rv)
1079 break;
1080
1081 if (p->Attribute & EFI_MEMORY_UC)
1082 printf("UC ");
1083 if (p->Attribute & EFI_MEMORY_WC)
1084 printf("WC ");
1085 if (p->Attribute & EFI_MEMORY_WT)
1086 printf("WT ");
1087 if (p->Attribute & EFI_MEMORY_WB)
1088 printf("WB ");
1089 if (p->Attribute & EFI_MEMORY_UCE)
1090 printf("UCE ");
1091 if (p->Attribute & EFI_MEMORY_WP)
1092 printf("WP ");
1093 if (p->Attribute & EFI_MEMORY_RP)
1094 printf("RP ");
1095 if (p->Attribute & EFI_MEMORY_XP)
1096 printf("XP ");
1097 if (p->Attribute & EFI_MEMORY_NV)
1098 printf("NV ");
1099 if (p->Attribute & EFI_MEMORY_MORE_RELIABLE)
1100 printf("MR ");
1101 if (p->Attribute & EFI_MEMORY_RO)
1102 printf("RO ");
1103 rv = pager_output("\n");
1104 if (rv)
1105 break;
1106 }
1107
1108 pager_close();
1109 return (CMD_OK);
1110 }
1111
1112 COMMAND_SET(configuration, "configuration", "print configuration tables",
1113 command_configuration);
1114
1115 static int
command_configuration(int argc __unused,char * argv[]__unused)1116 command_configuration(int argc __unused, char *argv[] __unused)
1117 {
1118 UINTN i;
1119 char *name;
1120
1121 printf("NumberOfTableEntries=%lu\n",
1122 (unsigned long)ST->NumberOfTableEntries);
1123 for (i = 0; i < ST->NumberOfTableEntries; i++) {
1124 EFI_GUID *guid;
1125
1126 printf(" ");
1127 guid = &ST->ConfigurationTable[i].VendorGuid;
1128
1129 if (efi_guid_to_name(guid, &name) == true) {
1130 printf(name);
1131 free(name);
1132 } else {
1133 printf("Error while translating UUID to name");
1134 }
1135 printf(" at %p\n", ST->ConfigurationTable[i].VendorTable);
1136 }
1137
1138 return (CMD_OK);
1139 }
1140
1141
1142 COMMAND_SET(mode, "mode", "change or display EFI text modes", command_mode);
1143
1144 static int
command_mode(int argc,char * argv[])1145 command_mode(int argc, char *argv[])
1146 {
1147 UINTN cols, rows;
1148 unsigned int mode;
1149 int i;
1150 char *cp;
1151 EFI_STATUS status;
1152 SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
1153 EFI_CONSOLE_CONTROL_SCREEN_MODE sm;
1154
1155 if (plat_stdout_is_framebuffer())
1156 sm = EfiConsoleControlScreenGraphics;
1157 else
1158 sm = EfiConsoleControlScreenText;
1159
1160 conout = ST->ConOut;
1161
1162 if (argc > 1) {
1163 mode = strtol(argv[1], &cp, 0);
1164 if (cp[0] != '\0') {
1165 printf("Invalid mode\n");
1166 return (CMD_ERROR);
1167 }
1168 status = conout->QueryMode(conout, mode, &cols, &rows);
1169 if (EFI_ERROR(status)) {
1170 printf("invalid mode %d\n", mode);
1171 return (CMD_ERROR);
1172 }
1173 status = conout->SetMode(conout, mode);
1174 if (EFI_ERROR(status)) {
1175 printf("couldn't set mode %d\n", mode);
1176 return (CMD_ERROR);
1177 }
1178 plat_cons_update_mode(sm);
1179 return (CMD_OK);
1180 }
1181
1182 printf("Current mode: %d\n", conout->Mode->Mode);
1183 for (i = 0; i <= conout->Mode->MaxMode; i++) {
1184 status = conout->QueryMode(conout, i, &cols, &rows);
1185 if (EFI_ERROR(status))
1186 continue;
1187 printf("Mode %d: %u columns, %u rows\n", i, (unsigned)cols,
1188 (unsigned)rows);
1189 }
1190
1191 if (i != 0)
1192 printf("Select a mode with the command \"mode <number>\"\n");
1193
1194 return (CMD_OK);
1195 }
1196
1197 COMMAND_SET(lsefi, "lsefi", "list EFI handles", command_lsefi);
1198
1199 static int
command_lsefi(int argc __unused,char * argv[]__unused)1200 command_lsefi(int argc __unused, char *argv[] __unused)
1201 {
1202 char *name;
1203 EFI_HANDLE *buffer = NULL;
1204 EFI_HANDLE handle;
1205 UINTN bufsz = 0, i, j;
1206 EFI_STATUS status;
1207 int ret = 0;
1208
1209 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
1210 if (status != EFI_BUFFER_TOO_SMALL) {
1211 snprintf(command_errbuf, sizeof (command_errbuf),
1212 "unexpected error: %lld", (long long)status);
1213 return (CMD_ERROR);
1214 }
1215 if ((buffer = malloc(bufsz)) == NULL) {
1216 sprintf(command_errbuf, "out of memory");
1217 return (CMD_ERROR);
1218 }
1219
1220 status = BS->LocateHandle(AllHandles, NULL, NULL, &bufsz, buffer);
1221 if (EFI_ERROR(status)) {
1222 free(buffer);
1223 snprintf(command_errbuf, sizeof (command_errbuf),
1224 "LocateHandle() error: %lld", (long long)status);
1225 return (CMD_ERROR);
1226 }
1227
1228 pager_open();
1229 for (i = 0; i < (bufsz / sizeof (EFI_HANDLE)); i++) {
1230 UINTN nproto = 0;
1231 EFI_GUID **protocols = NULL;
1232 EFI_DEVICE_PATH *dp;
1233 CHAR16 *text;
1234
1235 handle = buffer[i];
1236 printf("Handle %p", handle);
1237 if (pager_output("\n"))
1238 break;
1239
1240 ret = 0;
1241 dp = efi_lookup_devpath(handle);
1242 if (dp != NULL) {
1243 text = efi_devpath_name(dp);
1244 if (text != NULL) {
1245 printf(" %S", text);
1246 efi_free_devpath_name(text);
1247 ret = pager_output("\n");
1248 }
1249 efi_close_devpath(handle);
1250 }
1251 if (ret != 0)
1252 break;
1253
1254 status = BS->ProtocolsPerHandle(handle, &protocols, &nproto);
1255 if (EFI_ERROR(status)) {
1256 snprintf(command_errbuf, sizeof (command_errbuf),
1257 "ProtocolsPerHandle() error: %lld",
1258 (long long)status);
1259 continue;
1260 }
1261
1262 for (j = 0; j < nproto; j++) {
1263 if (efi_guid_to_name(protocols[j], &name) == true) {
1264 printf(" %s", name);
1265 free(name);
1266 } else {
1267 printf("Error while translating UUID to name");
1268 }
1269 if ((ret = pager_output("\n")) != 0)
1270 break;
1271 }
1272 BS->FreePool(protocols);
1273 if (ret != 0)
1274 break;
1275 }
1276 pager_close();
1277 free(buffer);
1278 return (CMD_OK);
1279 }
1280
1281 #ifdef LOADER_FDT_SUPPORT
1282 extern int command_fdt_internal(int argc, char *argv[]);
1283
1284 /*
1285 * Since proper fdt command handling function is defined in fdt_loader_cmd.c,
1286 * and declaring it as extern is in contradiction with COMMAND_SET() macro
1287 * (which uses static pointer), we're defining wrapper function, which
1288 * calls the proper fdt handling routine.
1289 */
1290 static int
command_fdt(int argc,char * argv[])1291 command_fdt(int argc, char *argv[])
1292 {
1293 return (command_fdt_internal(argc, argv));
1294 }
1295
1296 COMMAND_SET(fdt, "fdt", "flattened device tree handling", command_fdt);
1297 #endif
1298
1299 /*
1300 * Chain load another efi loader.
1301 */
1302 static int
command_chain(int argc,char * argv[])1303 command_chain(int argc, char *argv[])
1304 {
1305 EFI_HANDLE loaderhandle;
1306 EFI_LOADED_IMAGE_PROTOCOL *loaded_image;
1307 EFI_STATUS status;
1308 struct stat st;
1309 struct devdesc *dev;
1310 char *name, *path;
1311 void *buf;
1312 int fd;
1313
1314 if (argc < 2) {
1315 command_errmsg = "wrong number of arguments";
1316 return (CMD_ERROR);
1317 }
1318
1319 name = argv[1];
1320
1321 if ((fd = open(name, O_RDONLY)) < 0) {
1322 command_errmsg = "no such file";
1323 return (CMD_ERROR);
1324 }
1325
1326 if (fstat(fd, &st) < -1) {
1327 command_errmsg = "stat failed";
1328 close(fd);
1329 return (CMD_ERROR);
1330 }
1331
1332 status = BS->AllocatePool(EfiLoaderCode, (UINTN)st.st_size, &buf);
1333 if (status != EFI_SUCCESS) {
1334 command_errmsg = "failed to allocate buffer";
1335 close(fd);
1336 return (CMD_ERROR);
1337 }
1338 if (read(fd, buf, st.st_size) != st.st_size) {
1339 command_errmsg = "error while reading the file";
1340 (void) BS->FreePool(buf);
1341 close(fd);
1342 return (CMD_ERROR);
1343 }
1344 close(fd);
1345 status = BS->LoadImage(FALSE, IH, NULL, buf, st.st_size, &loaderhandle);
1346 (void) BS->FreePool(buf);
1347 if (status != EFI_SUCCESS) {
1348 printf("LoadImage failed: status code: %lu\n",
1349 DECODE_ERROR(status));
1350 return (CMD_ERROR);
1351 }
1352 status = OpenProtocolByHandle(loaderhandle,
1353 &gEfiLoadedImageProtocolGuid, (void **)&loaded_image);
1354
1355 if (argc > 2) {
1356 int i, len = 0;
1357 CHAR16 *argp;
1358
1359 for (i = 2; i < argc; i++)
1360 len += strlen(argv[i]) + 1;
1361
1362 len *= sizeof (*argp);
1363 loaded_image->LoadOptions = argp = malloc(len);
1364 if (loaded_image->LoadOptions == NULL) {
1365 command_errmsg = "Adding LoadOptions: out of memory";
1366 (void) BS->UnloadImage(loaded_image);
1367 return (CMD_ERROR);
1368 }
1369 loaded_image->LoadOptionsSize = len;
1370 for (i = 2; i < argc; i++) {
1371 char *ptr = argv[i];
1372 while (*ptr)
1373 *(argp++) = *(ptr++);
1374 *(argp++) = ' ';
1375 }
1376 *(--argv) = 0;
1377 }
1378
1379 if (efi_getdev((void **)&dev, name, (const char **)&path) == 0) {
1380 struct zfs_devdesc *z_dev;
1381 struct disk_devdesc *d_dev;
1382 pdinfo_t *hd, *pd;
1383
1384 switch (dev->d_dev->dv_type) {
1385 case DEVT_ZFS:
1386 z_dev = (struct zfs_devdesc *)dev;
1387 loaded_image->DeviceHandle =
1388 efizfs_get_handle_by_guid(z_dev->pool_guid);
1389 break;
1390 case DEVT_NET:
1391 loaded_image->DeviceHandle =
1392 efi_find_handle(dev->d_dev, dev->d_unit);
1393 break;
1394 default:
1395 hd = efiblk_get_pdinfo(dev);
1396 if (STAILQ_EMPTY(&hd->pd_part)) {
1397 loaded_image->DeviceHandle = hd->pd_handle;
1398 break;
1399 }
1400 d_dev = (struct disk_devdesc *)dev;
1401 STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
1402 /*
1403 * d_partition should be 255
1404 */
1405 if (pd->pd_unit == d_dev->d_slice) {
1406 loaded_image->DeviceHandle =
1407 pd->pd_handle;
1408 break;
1409 }
1410 }
1411 break;
1412 }
1413 }
1414
1415 dev_cleanup();
1416 status = BS->StartImage(loaderhandle, NULL, NULL);
1417 if (status != EFI_SUCCESS) {
1418 printf("StartImage failed: status code: %lu\n",
1419 DECODE_ERROR(status));
1420 free(loaded_image->LoadOptions);
1421 loaded_image->LoadOptions = NULL;
1422 status = BS->UnloadImage(loaded_image);
1423 return (CMD_ERROR);
1424 }
1425
1426 return (CMD_ERROR);
1427 }
1428
1429 COMMAND_SET(chain, "chain", "chain load file", command_chain);
1430