2 * Copyright (c) 1998 Robert Nordier
4 * Copyright (c) 2001 Robert Drehmel
6 * Copyright (c) 2014 Nathan Whitehorn
8 * Copyright (c) 2015 Eric McCorkle
11 * Redistribution and use in source and binary forms are freely
12 * permitted provided that the above copyright notice and this
13 * paragraph and the following disclaimer are duplicated in all
16 * This software is provided "AS IS" and without any express or
17 * implied warranties, including, without limitation, the implied
18 * warranties of merchantability and fitness for a particular
22 #include <sys/cdefs.h>
23 __FBSDID("$FreeBSD$");
25 #include <sys/param.h>
26 #include <machine/elf.h>
27 #include <machine/stdarg.h>
31 #include <eficonsctl.h>
34 #include "boot_module.h"
38 static EFI_GUID BlockIoProtocolGUID = BLOCK_IO_PROTOCOL;
39 static EFI_GUID DevicePathGUID = DEVICE_PATH_PROTOCOL;
41 static const char *prio_str[] = {
49 * probe_handle determines if the passed handle represents a logical partition
50 * if it does it uses each module in order to probe it and if successful it
51 * returns EFI_SUCCESS.
54 probe_handle(EFI_HANDLE h, EFI_DEVICE_PATH *imgpath)
58 EFI_DEVICE_PATH *devpath;
63 /* Figure out if we're dealing with an actual partition. */
64 status = OpenProtocolByHandle(h, &DevicePathGUID, (void **)&devpath);
65 if (status == EFI_UNSUPPORTED)
68 if (status != EFI_SUCCESS) {
69 DPRINTF("\nFailed to query DevicePath (%lu)\n",
70 EFI_ERROR_CODE(status));
75 CHAR16 *text = efi_devpath_name(devpath);
76 DPRINTF("probing: %S ", text);
77 efi_free_devpath_name(text);
80 status = OpenProtocolByHandle(h, &BlockIoProtocolGUID, (void **)&blkio);
81 if (status == EFI_UNSUPPORTED)
84 if (status != EFI_SUCCESS) {
85 DPRINTF("\nFailed to query BlockIoProtocol (%lu)\n",
86 EFI_ERROR_CODE(status));
90 if (!blkio->Media->LogicalPartition)
93 preferred = efi_devpath_same_disk(imgpath, devpath);
95 /* Run through each module, see if it can load this partition */
96 devinfo = malloc(sizeof(*devinfo));
97 if (devinfo == NULL) {
98 DPRINTF("\nFailed to allocate devinfo\n");
101 devinfo->dev = blkio;
102 devinfo->devpath = devpath;
103 devinfo->devhandle = h;
104 devinfo->preferred = preferred;
105 devinfo->next = NULL;
107 for (i = 0; i < num_boot_modules; i++) {
108 devinfo->devdata = NULL;
110 status = boot_modules[i]->probe(devinfo);
111 if (status == EFI_SUCCESS)
112 return (preferred + 1);
120 * load_loader attempts to load the loader image data.
122 * It tries each module and its respective devices, identified by mod->probe,
123 * in order until a successful load occurs at which point it returns EFI_SUCCESS
124 * and EFI_NOT_FOUND otherwise.
126 * Only devices which have preferred matching the preferred parameter are tried.
129 load_loader(const boot_module_t **modp, dev_info_t **devinfop, void **bufp,
130 size_t *bufsize, int preferred)
134 const boot_module_t *mod;
136 for (i = 0; i < num_boot_modules; i++) {
137 mod = boot_modules[i];
138 for (dev = mod->devices(); dev != NULL; dev = dev->next) {
139 if (dev->preferred != preferred)
142 if (mod->load(PATH_LOADER_EFI, dev, bufp, bufsize) ==
146 return (EFI_SUCCESS);
151 return (EFI_NOT_FOUND);
155 choice_protocol(EFI_HANDLE *handles, UINTN nhandles, EFI_DEVICE_PATH *imgpath)
159 UINT16 boot_order[100];
163 const boot_module_t *mod;
168 /* Report UEFI Boot Manager Protocol details */
170 sz = sizeof(boot_current);
171 if (efi_global_getenv("BootCurrent", &boot_current, &sz) == EFI_SUCCESS) {
172 printf(" BootCurrent: %04x\n", boot_current);
174 sz = sizeof(boot_order);
175 if (efi_global_getenv("BootOrder", &boot_order, &sz) == EFI_SUCCESS) {
176 printf(" BootOrder:");
177 for (i = 0; i < sz / sizeof(boot_order[0]); i++)
178 printf(" %04x%s", boot_order[i],
179 boot_order[i] == boot_current ? "[*]" : "");
186 * For testing failover scenarios, it's nice to be able to fail fast.
187 * Define TEST_FAILURE to create a boot1.efi that always fails after
188 * reporting the boot manager protocol details.
190 BS->Exit(IH, EFI_OUT_OF_RESOURCES, 0, NULL);
193 /* Scan all partitions, probing with all modules. */
194 printf(" Probing %zu block devices...", nhandles);
196 for (i = 0; i < nhandles; i++) {
197 rv = probe_handle(handles[i], imgpath);
199 printf("%c", "x.+*"[rv + 1]);
201 printf("%s\n", prio_str[rv + 1]);
207 /* Status summary. */
208 for (i = 0; i < num_boot_modules; i++) {
210 boot_modules[i]->status();
213 status = load_loader(&mod, &dev, &loaderbuf, &loadersize, 1);
214 if (status != EFI_SUCCESS) {
215 status = load_loader(&mod, &dev, &loaderbuf, &loadersize, 0);
216 if (status != EFI_SUCCESS) {
217 printf("Failed to load '%s'\n", PATH_LOADER_EFI);
222 try_boot(mod, dev, loaderbuf, loadersize);