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"
37 static void efi_panic(EFI_STATUS s, const char *fmt, ...) __dead2 __printflike(2, 3);
39 static const boot_module_t *boot_modules[] =
49 #define NUM_BOOT_MODULES nitems(boot_modules)
50 /* The initial number of handles used to query EFI for partitions. */
51 #define NUM_HANDLES_INIT 24
53 static EFI_GUID BlockIoProtocolGUID = BLOCK_IO_PROTOCOL;
54 static EFI_GUID DevicePathGUID = DEVICE_PATH_PROTOCOL;
55 static EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL;
56 static EFI_GUID ConsoleControlGUID = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
57 static EFI_GUID FreeBSDBootVarGUID = FREEBSD_BOOT_VAR_GUID;
60 * Provide Malloc / Free backed by EFIs AllocatePool / FreePool which ensures
61 * memory is correctly aligned avoiding EFI_INVALID_PARAMETER returns from
65 Malloc(size_t len, const char *file __unused, int line __unused)
69 if (BS->AllocatePool(EfiLoaderData, len, &out) == EFI_SUCCESS)
76 Free(void *buf, const char *file __unused, int line __unused)
79 (void)BS->FreePool(buf);
83 efi_setenv_freebsd_wcs(const char *varname, CHAR16 *valstr)
89 utf8_to_ucs2(varname, &var, &len);
91 return (EFI_OUT_OF_RESOURCES);
92 rv = RS->SetVariable(var, &FreeBSDBootVarGUID,
93 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,
94 (ucs2len(valstr) + 1) * sizeof(efi_char), valstr);
100 * nodes_match returns TRUE if the imgpath isn't NULL and the nodes match,
104 nodes_match(EFI_DEVICE_PATH *imgpath, EFI_DEVICE_PATH *devpath)
108 if (imgpath == NULL || imgpath->Type != devpath->Type ||
109 imgpath->SubType != devpath->SubType)
112 len = DevicePathNodeLength(imgpath);
113 if (len != DevicePathNodeLength(devpath))
116 return (memcmp(imgpath, devpath, (size_t)len) == 0);
120 * device_paths_match returns TRUE if the imgpath isn't NULL and all nodes
121 * in imgpath and devpath match up to their respective occurrences of a
122 * media node, FALSE otherwise.
125 device_paths_match(EFI_DEVICE_PATH *imgpath, EFI_DEVICE_PATH *devpath)
131 while (!IsDevicePathEnd(imgpath) && !IsDevicePathEnd(devpath)) {
132 if (IsDevicePathType(imgpath, MEDIA_DEVICE_PATH) &&
133 IsDevicePathType(devpath, MEDIA_DEVICE_PATH))
136 if (!nodes_match(imgpath, devpath))
139 imgpath = NextDevicePathNode(imgpath);
140 devpath = NextDevicePathNode(devpath);
147 * devpath_last returns the last non-path end node in devpath.
149 static EFI_DEVICE_PATH *
150 devpath_last(EFI_DEVICE_PATH *devpath)
153 while (!IsDevicePathEnd(NextDevicePathNode(devpath)))
154 devpath = NextDevicePathNode(devpath);
160 * load_loader attempts to load the loader image data.
162 * It tries each module and its respective devices, identified by mod->probe,
163 * in order until a successful load occurs at which point it returns EFI_SUCCESS
164 * and EFI_NOT_FOUND otherwise.
166 * Only devices which have preferred matching the preferred parameter are tried.
169 load_loader(const boot_module_t **modp, dev_info_t **devinfop, void **bufp,
170 size_t *bufsize, BOOLEAN preferred)
174 const boot_module_t *mod;
176 for (i = 0; i < NUM_BOOT_MODULES; i++) {
177 mod = boot_modules[i];
178 for (dev = mod->devices(); dev != NULL; dev = dev->next) {
179 if (dev->preferred != preferred)
182 if (mod->load(PATH_LOADER_EFI, dev, bufp, bufsize) ==
186 return (EFI_SUCCESS);
191 return (EFI_NOT_FOUND);
195 * try_boot only returns if it fails to load the loader. If it succeeds
196 * it simply boots, otherwise it returns the status of last EFI call.
201 size_t bufsize, loadersize, cmdsize;
202 void *buf, *loaderbuf;
205 const boot_module_t *mod;
206 EFI_HANDLE loaderhandle;
207 EFI_LOADED_IMAGE *loaded_image;
210 status = load_loader(&mod, &dev, &loaderbuf, &loadersize, TRUE);
211 if (status != EFI_SUCCESS) {
212 status = load_loader(&mod, &dev, &loaderbuf, &loadersize,
214 if (status != EFI_SUCCESS) {
215 printf("Failed to load '%s'\n", PATH_LOADER_EFI);
221 * Read in and parse the command line from /boot.config or /boot/config,
222 * if present. We'll pass it the next stage via a simple ASCII
223 * string. loader.efi has a hack for ASCII strings, so we'll use that to
224 * keep the size down here. We only try to read the alternate file if
225 * we get EFI_NOT_FOUND because all other errors mean that the boot_module
226 * had troubles with the filesystem. We could return early, but we'll let
227 * loading the actual kernel sort all that out. Since these files are
228 * optional, we don't report errors in trying to read them.
232 status = mod->load(PATH_DOTCONFIG, dev, &buf, &bufsize);
233 if (status == EFI_NOT_FOUND)
234 status = mod->load(PATH_CONFIG, dev, &buf, &bufsize);
235 if (status == EFI_SUCCESS) {
236 cmdsize = bufsize + 1;
237 cmd = malloc(cmdsize);
240 memcpy(cmd, buf, bufsize);
246 if ((status = BS->LoadImage(TRUE, IH, devpath_last(dev->devpath),
247 loaderbuf, loadersize, &loaderhandle)) != EFI_SUCCESS) {
248 printf("Failed to load image provided by %s, size: %zu, (%lu)\n",
249 mod->name, loadersize, EFI_ERROR_CODE(status));
253 if ((status = BS->HandleProtocol(loaderhandle, &LoadedImageGUID,
254 (VOID**)&loaded_image)) != EFI_SUCCESS) {
255 printf("Failed to query LoadedImage provided by %s (%lu)\n",
256 mod->name, EFI_ERROR_CODE(status));
261 printf(" command args: %s\n", cmd);
263 loaded_image->DeviceHandle = dev->devhandle;
264 loaded_image->LoadOptionsSize = cmdsize;
265 loaded_image->LoadOptions = cmd;
267 DPRINTF("Starting '%s' in 5 seconds...", PATH_LOADER_EFI);
279 if ((status = BS->StartImage(loaderhandle, NULL, NULL)) !=
281 printf("Failed to start image provided by %s (%lu)\n",
282 mod->name, EFI_ERROR_CODE(status));
283 loaded_image->LoadOptionsSize = 0;
284 loaded_image->LoadOptions = NULL;
292 if (loaderbuf != NULL)
299 * probe_handle determines if the passed handle represents a logical partition
300 * if it does it uses each module in order to probe it and if successful it
301 * returns EFI_SUCCESS.
304 probe_handle(EFI_HANDLE h, EFI_DEVICE_PATH *imgpath, BOOLEAN *preferred)
308 EFI_DEVICE_PATH *devpath;
312 /* Figure out if we're dealing with an actual partition. */
313 status = BS->HandleProtocol(h, &DevicePathGUID, (void **)&devpath);
314 if (status == EFI_UNSUPPORTED)
317 if (status != EFI_SUCCESS) {
318 DPRINTF("\nFailed to query DevicePath (%lu)\n",
319 EFI_ERROR_CODE(status));
324 CHAR16 *text = efi_devpath_name(devpath);
325 DPRINTF("probing: %S\n", text);
326 efi_free_devpath_name(text);
329 status = BS->HandleProtocol(h, &BlockIoProtocolGUID, (void **)&blkio);
330 if (status == EFI_UNSUPPORTED)
333 if (status != EFI_SUCCESS) {
334 DPRINTF("\nFailed to query BlockIoProtocol (%lu)\n",
335 EFI_ERROR_CODE(status));
339 if (!blkio->Media->LogicalPartition)
340 return (EFI_UNSUPPORTED);
342 *preferred = device_paths_match(imgpath, devpath);
344 /* Run through each module, see if it can load this partition */
345 for (i = 0; i < NUM_BOOT_MODULES; i++) {
346 devinfo = malloc(sizeof(*devinfo));
347 if (devinfo == NULL) {
348 DPRINTF("\nFailed to allocate devinfo\n");
351 devinfo->dev = blkio;
352 devinfo->devpath = devpath;
353 devinfo->devhandle = h;
354 devinfo->devdata = NULL;
355 devinfo->preferred = *preferred;
356 devinfo->next = NULL;
358 status = boot_modules[i]->probe(devinfo);
359 if (status == EFI_SUCCESS)
360 return (EFI_SUCCESS);
364 return (EFI_UNSUPPORTED);
368 * probe_handle_status calls probe_handle and outputs the returned status
372 probe_handle_status(EFI_HANDLE h, EFI_DEVICE_PATH *imgpath)
378 status = probe_handle(h, imgpath, &preferred);
382 case EFI_UNSUPPORTED:
384 DPRINTF(" not supported\n");
389 DPRINTF(" supported (preferred)\n");
392 DPRINTF(" supported\n");
397 DPRINTF(" error (%lu)\n", EFI_ERROR_CODE(status));
404 efi_main(EFI_HANDLE Ximage, EFI_SYSTEM_TABLE *Xsystab)
407 EFI_LOADED_IMAGE *img;
408 EFI_DEVICE_PATH *imgpath;
410 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL;
411 SIMPLE_TEXT_OUTPUT_INTERFACE *conout = NULL;
412 UINTN i, max_dim, best_mode, cols, rows, hsize, nhandles;
415 /* Basic initialization*/
418 BS = ST->BootServices;
419 RS = ST->RuntimeServices;
421 /* Set up the console, so printf works. */
422 status = BS->LocateProtocol(&ConsoleControlGUID, NULL,
423 (VOID **)&ConsoleControl);
424 if (status == EFI_SUCCESS)
425 (void)ConsoleControl->SetMode(ConsoleControl,
426 EfiConsoleControlScreenText);
428 * Reset the console and find the best text mode.
431 conout->Reset(conout, TRUE);
432 max_dim = best_mode = 0;
434 status = conout->QueryMode(conout, i, &cols, &rows);
435 if (EFI_ERROR(status))
437 if (cols * rows > max_dim) {
438 max_dim = cols * rows;
443 conout->SetMode(conout, best_mode);
444 conout->EnableCursor(conout, TRUE);
445 conout->ClearScreen(conout);
447 printf("\n>> FreeBSD EFI boot block\n");
448 printf(" Loader path: %s\n\n", PATH_LOADER_EFI);
449 printf(" Initializing modules:");
450 for (i = 0; i < NUM_BOOT_MODULES; i++) {
451 printf(" %s", boot_modules[i]->name);
452 if (boot_modules[i]->init != NULL)
453 boot_modules[i]->init();
457 /* Determine the devpath of our image so we can prefer it. */
458 status = BS->HandleProtocol(IH, &LoadedImageGUID, (VOID**)&img);
460 if (status == EFI_SUCCESS) {
461 text = efi_devpath_name(img->FilePath);
463 printf(" Load Path: %S\n", text);
464 efi_setenv_freebsd_wcs("Boot1Path", text);
465 efi_free_devpath_name(text);
468 status = BS->HandleProtocol(img->DeviceHandle, &DevicePathGUID,
470 if (status != EFI_SUCCESS) {
471 DPRINTF("Failed to get image DevicePath (%lu)\n",
472 EFI_ERROR_CODE(status));
474 text = efi_devpath_name(imgpath);
476 printf(" Load Device: %S\n", text);
477 efi_setenv_freebsd_wcs("Boot1Dev", text);
478 efi_free_devpath_name(text);
483 /* Get all the device handles */
484 hsize = (UINTN)NUM_HANDLES_INIT * sizeof(EFI_HANDLE);
485 handles = malloc(hsize);
486 if (handles == NULL) {
487 printf("Failed to allocate %d handles\n", NUM_HANDLES_INIT);
490 status = BS->LocateHandle(ByProtocol, &BlockIoProtocolGUID, NULL,
495 case EFI_BUFFER_TOO_SMALL:
497 handles = malloc(hsize);
499 efi_panic(EFI_OUT_OF_RESOURCES, "Failed to allocate %d handles\n",
501 status = BS->LocateHandle(ByProtocol, &BlockIoProtocolGUID,
502 NULL, &hsize, handles);
503 if (status != EFI_SUCCESS)
504 efi_panic(status, "Failed to get device handles\n");
507 efi_panic(status, "Failed to get device handles\n");
511 /* Scan all partitions, probing with all modules. */
512 nhandles = hsize / sizeof(*handles);
513 printf(" Probing %zu block devices...", nhandles);
516 for (i = 0; i < nhandles; i++)
517 probe_handle_status(handles[i], imgpath);
520 /* Status summary. */
521 for (i = 0; i < NUM_BOOT_MODULES; i++) {
523 boot_modules[i]->status();
528 /* If we get here, we're out of luck... */
529 efi_panic(EFI_LOAD_ERROR, "No bootable partitions found!");
533 * add_device adds a device to the passed devinfo list.
536 add_device(dev_info_t **devinfop, dev_info_t *devinfo)
540 if (*devinfop == NULL) {
545 for (dev = *devinfop; dev->next != NULL; dev = dev->next)
552 * OK. We totally give up. Exit back to EFI with a sensible status so
553 * it can try the next option on the list.
556 efi_panic(EFI_STATUS s, const char *fmt, ...)
566 BS->Exit(IH, s, 0, NULL);
577 ST->ConOut->OutputString(ST->ConOut, buf);
581 ST->ConOut->OutputString(ST->ConOut, buf);