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>
33 #include "boot_module.h"
36 static const boot_module_t *boot_modules[] =
46 #define NUM_BOOT_MODULES nitems(boot_modules)
47 /* The initial number of handles used to query EFI for partitions. */
48 #define NUM_HANDLES_INIT 24
51 EFI_STATUS efi_main(EFI_HANDLE Ximage, EFI_SYSTEM_TABLE* Xsystab);
53 EFI_SYSTEM_TABLE *systab;
54 EFI_BOOT_SERVICES *bs;
55 static EFI_HANDLE *image;
57 static EFI_GUID BlockIoProtocolGUID = BLOCK_IO_PROTOCOL;
58 static EFI_GUID DevicePathGUID = DEVICE_PATH_PROTOCOL;
59 static EFI_GUID LoadedImageGUID = LOADED_IMAGE_PROTOCOL;
60 static EFI_GUID ConsoleControlGUID = EFI_CONSOLE_CONTROL_PROTOCOL_GUID;
63 * Provide Malloc / Free backed by EFIs AllocatePool / FreePool which ensures
64 * memory is correctly aligned avoiding EFI_INVALID_PARAMETER returns from
68 Malloc(size_t len, const char *file __unused, int line __unused)
72 if (bs->AllocatePool(EfiLoaderData, len, &out) == EFI_SUCCESS)
79 Free(void *buf, const char *file __unused, int line __unused)
81 (void)bs->FreePool(buf);
85 * nodes_match returns TRUE if the imgpath isn't NULL and the nodes match,
89 nodes_match(EFI_DEVICE_PATH *imgpath, EFI_DEVICE_PATH *devpath)
93 if (imgpath == NULL || imgpath->Type != devpath->Type ||
94 imgpath->SubType != devpath->SubType)
97 len = DevicePathNodeLength(imgpath);
98 if (len != DevicePathNodeLength(devpath))
101 return (memcmp(imgpath, devpath, (size_t)len) == 0);
105 * device_paths_match returns TRUE if the imgpath isn't NULL and all nodes
106 * in imgpath and devpath match up to their respective occurrences of a
107 * media node, FALSE otherwise.
110 device_paths_match(EFI_DEVICE_PATH *imgpath, EFI_DEVICE_PATH *devpath)
116 while (!IsDevicePathEnd(imgpath) && !IsDevicePathEnd(devpath)) {
117 if (IsDevicePathType(imgpath, MEDIA_DEVICE_PATH) &&
118 IsDevicePathType(devpath, MEDIA_DEVICE_PATH))
121 if (!nodes_match(imgpath, devpath))
124 imgpath = NextDevicePathNode(imgpath);
125 devpath = NextDevicePathNode(devpath);
132 * devpath_last returns the last non-path end node in devpath.
134 static EFI_DEVICE_PATH *
135 devpath_last(EFI_DEVICE_PATH *devpath)
138 while (!IsDevicePathEnd(NextDevicePathNode(devpath)))
139 devpath = NextDevicePathNode(devpath);
145 * devpath_node_str is a basic output method for a devpath node which
146 * only understands a subset of the available sub types.
148 * If we switch to UEFI 2.x then we should update it to use:
149 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
152 devpath_node_str(char *buf, size_t size, EFI_DEVICE_PATH *devpath)
155 switch (devpath->Type) {
156 case MESSAGING_DEVICE_PATH:
157 switch (devpath->SubType) {
159 ATAPI_DEVICE_PATH *atapi;
161 atapi = (ATAPI_DEVICE_PATH *)(void *)devpath;
162 return snprintf(buf, size, "ata(%s,%s,0x%x)",
163 (atapi->PrimarySecondary == 1) ? "Sec" : "Pri",
164 (atapi->SlaveMaster == 1) ? "Slave" : "Master",
168 USB_DEVICE_PATH *usb;
170 usb = (USB_DEVICE_PATH *)devpath;
171 return snprintf(buf, size, "usb(0x%02x,0x%02x)",
172 usb->ParentPortNumber, usb->InterfaceNumber);
175 SCSI_DEVICE_PATH *scsi;
177 scsi = (SCSI_DEVICE_PATH *)(void *)devpath;
178 return snprintf(buf, size, "scsi(0x%02x,0x%02x)",
179 scsi->Pun, scsi->Lun);
182 SATA_DEVICE_PATH *sata;
184 sata = (SATA_DEVICE_PATH *)(void *)devpath;
185 return snprintf(buf, size, "sata(0x%x,0x%x,0x%x)",
186 sata->HBAPortNumber, sata->PortMultiplierPortNumber,
190 return snprintf(buf, size, "msg(0x%02x)",
194 case HARDWARE_DEVICE_PATH:
195 switch (devpath->SubType) {
197 PCI_DEVICE_PATH *pci;
199 pci = (PCI_DEVICE_PATH *)devpath;
200 return snprintf(buf, size, "pci(0x%02x,0x%02x)",
201 pci->Device, pci->Function);
204 return snprintf(buf, size, "hw(0x%02x)",
208 case ACPI_DEVICE_PATH: {
209 ACPI_HID_DEVICE_PATH *acpi;
211 acpi = (ACPI_HID_DEVICE_PATH *)(void *)devpath;
212 if ((acpi->HID & PNP_EISA_ID_MASK) == PNP_EISA_ID_CONST) {
213 switch (EISA_ID_TO_NUM(acpi->HID)) {
215 return snprintf(buf, size, "pciroot(0x%x)",
218 return snprintf(buf, size, "pcieroot(0x%x)",
221 return snprintf(buf, size, "floppy(0x%x)",
224 return snprintf(buf, size, "keyboard(0x%x)",
227 return snprintf(buf, size, "serial(0x%x)",
230 return snprintf(buf, size, "parallelport(0x%x)",
233 return snprintf(buf, size, "acpi(pnp%04x,0x%x)",
234 EISA_ID_TO_NUM(acpi->HID), acpi->UID);
238 return snprintf(buf, size, "acpi(0x%08x,0x%x)", acpi->HID,
241 case MEDIA_DEVICE_PATH:
242 switch (devpath->SubType) {
243 case MEDIA_CDROM_DP: {
244 CDROM_DEVICE_PATH *cdrom;
246 cdrom = (CDROM_DEVICE_PATH *)(void *)devpath;
247 return snprintf(buf, size, "cdrom(%x)",
250 case MEDIA_HARDDRIVE_DP: {
251 HARDDRIVE_DEVICE_PATH *hd;
253 hd = (HARDDRIVE_DEVICE_PATH *)(void *)devpath;
254 return snprintf(buf, size, "hd(%x)",
255 hd->PartitionNumber);
258 return snprintf(buf, size, "media(0x%02x)",
261 case BBS_DEVICE_PATH:
262 return snprintf(buf, size, "bbs(0x%02x)", devpath->SubType);
263 case END_DEVICE_PATH_TYPE:
267 return snprintf(buf, size, "type(0x%02x, 0x%02x)", devpath->Type,
272 * devpath_strlcat appends a text description of devpath to buf but not more
273 * than size - 1 characters followed by NUL-terminator.
276 devpath_strlcat(char *buf, size_t size, EFI_DEVICE_PATH *devpath)
283 while (!IsDevicePathEnd(devpath)) {
284 len = snprintf(buf, size - used, "%s", sep);
290 len = devpath_node_str(buf, size - used, devpath);
295 devpath = NextDevicePathNode(devpath);
303 * devpath_str is convenience method which returns the text description of
304 * devpath using a static buffer, so it isn't thread safe!
307 devpath_str(EFI_DEVICE_PATH *devpath)
309 static char buf[256];
311 devpath_strlcat(buf, sizeof(buf), devpath);
317 * load_loader attempts to load the loader image data.
319 * It tries each module and its respective devices, identified by mod->probe,
320 * in order until a successful load occurs at which point it returns EFI_SUCCESS
321 * and EFI_NOT_FOUND otherwise.
323 * Only devices which have preferred matching the preferred parameter are tried.
326 load_loader(const boot_module_t **modp, dev_info_t **devinfop, void **bufp,
327 size_t *bufsize, BOOLEAN preferred)
331 const boot_module_t *mod;
333 for (i = 0; i < NUM_BOOT_MODULES; i++) {
334 mod = boot_modules[i];
335 for (dev = mod->devices(); dev != NULL; dev = dev->next) {
336 if (dev->preferred != preferred)
339 if (mod->load(PATH_LOADER_EFI, dev, bufp, bufsize) ==
343 return (EFI_SUCCESS);
348 return (EFI_NOT_FOUND);
352 * try_boot only returns if it fails to load the loader. If it succeeds
353 * it simply boots, otherwise it returns the status of last EFI call.
358 size_t bufsize, loadersize, cmdsize;
359 void *buf, *loaderbuf;
362 const boot_module_t *mod;
363 EFI_HANDLE loaderhandle;
364 EFI_LOADED_IMAGE *loaded_image;
367 status = load_loader(&mod, &dev, &loaderbuf, &loadersize, TRUE);
368 if (status != EFI_SUCCESS) {
369 status = load_loader(&mod, &dev, &loaderbuf, &loadersize,
371 if (status != EFI_SUCCESS) {
372 printf("Failed to load '%s'\n", PATH_LOADER_EFI);
378 * Read in and parse the command line from /boot.config or /boot/config,
379 * if present. We'll pass it the next stage via a simple ASCII
380 * string. loader.efi has a hack for ASCII strings, so we'll use that to
381 * keep the size down here. We only try to read the alternate file if
382 * we get EFI_NOT_FOUND because all other errors mean that the boot_module
383 * had troubles with the filesystem. We could return early, but we'll let
384 * loading the actual kernel sort all that out. Since these files are
385 * optional, we don't report errors in trying to read them.
389 status = mod->load(PATH_DOTCONFIG, dev, &buf, &bufsize);
390 if (status == EFI_NOT_FOUND)
391 status = mod->load(PATH_CONFIG, dev, &buf, &bufsize);
392 if (status == EFI_SUCCESS) {
393 cmdsize = bufsize + 1;
394 cmd = malloc(cmdsize);
397 memcpy(cmd, buf, bufsize);
403 if ((status = bs->LoadImage(TRUE, image, devpath_last(dev->devpath),
404 loaderbuf, loadersize, &loaderhandle)) != EFI_SUCCESS) {
405 printf("Failed to load image provided by %s, size: %zu, (%lu)\n",
406 mod->name, loadersize, EFI_ERROR_CODE(status));
410 if ((status = bs->HandleProtocol(loaderhandle, &LoadedImageGUID,
411 (VOID**)&loaded_image)) != EFI_SUCCESS) {
412 printf("Failed to query LoadedImage provided by %s (%lu)\n",
413 mod->name, EFI_ERROR_CODE(status));
418 printf(" command args: %s\n", cmd);
420 loaded_image->DeviceHandle = dev->devhandle;
421 loaded_image->LoadOptionsSize = cmdsize;
422 loaded_image->LoadOptions = cmd;
424 DPRINTF("Starting '%s' in 5 seconds...", PATH_LOADER_EFI);
436 if ((status = bs->StartImage(loaderhandle, NULL, NULL)) !=
438 printf("Failed to start image provided by %s (%lu)\n",
439 mod->name, EFI_ERROR_CODE(status));
440 loaded_image->LoadOptionsSize = 0;
441 loaded_image->LoadOptions = NULL;
449 if (loaderbuf != NULL)
456 * probe_handle determines if the passed handle represents a logical partition
457 * if it does it uses each module in order to probe it and if successful it
458 * returns EFI_SUCCESS.
461 probe_handle(EFI_HANDLE h, EFI_DEVICE_PATH *imgpath, BOOLEAN *preferred)
465 EFI_DEVICE_PATH *devpath;
469 /* Figure out if we're dealing with an actual partition. */
470 status = bs->HandleProtocol(h, &DevicePathGUID, (void **)&devpath);
471 if (status == EFI_UNSUPPORTED)
474 if (status != EFI_SUCCESS) {
475 DPRINTF("\nFailed to query DevicePath (%lu)\n",
476 EFI_ERROR_CODE(status));
480 DPRINTF("probing: %s\n", devpath_str(devpath));
482 status = bs->HandleProtocol(h, &BlockIoProtocolGUID, (void **)&blkio);
483 if (status == EFI_UNSUPPORTED)
486 if (status != EFI_SUCCESS) {
487 DPRINTF("\nFailed to query BlockIoProtocol (%lu)\n",
488 EFI_ERROR_CODE(status));
492 if (!blkio->Media->LogicalPartition)
493 return (EFI_UNSUPPORTED);
495 *preferred = device_paths_match(imgpath, devpath);
497 /* Run through each module, see if it can load this partition */
498 for (i = 0; i < NUM_BOOT_MODULES; i++) {
499 if ((status = bs->AllocatePool(EfiLoaderData,
500 sizeof(*devinfo), (void **)&devinfo)) !=
502 DPRINTF("\nFailed to allocate devinfo (%lu)\n",
503 EFI_ERROR_CODE(status));
506 devinfo->dev = blkio;
507 devinfo->devpath = devpath;
508 devinfo->devhandle = h;
509 devinfo->devdata = NULL;
510 devinfo->preferred = *preferred;
511 devinfo->next = NULL;
513 status = boot_modules[i]->probe(devinfo);
514 if (status == EFI_SUCCESS)
515 return (EFI_SUCCESS);
516 (void)bs->FreePool(devinfo);
519 return (EFI_UNSUPPORTED);
523 * probe_handle_status calls probe_handle and outputs the returned status
527 probe_handle_status(EFI_HANDLE h, EFI_DEVICE_PATH *imgpath)
532 status = probe_handle(h, imgpath, &preferred);
536 case EFI_UNSUPPORTED:
538 DPRINTF(" not supported\n");
543 DPRINTF(" supported (preferred)\n");
546 DPRINTF(" supported\n");
551 DPRINTF(" error (%lu)\n", EFI_ERROR_CODE(status));
558 efi_main(EFI_HANDLE Ximage, EFI_SYSTEM_TABLE *Xsystab)
561 EFI_LOADED_IMAGE *img;
562 EFI_DEVICE_PATH *imgpath;
564 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL;
565 SIMPLE_TEXT_OUTPUT_INTERFACE *conout = NULL;
566 UINTN i, max_dim, best_mode, cols, rows, hsize, nhandles;
568 /* Basic initialization*/
571 bs = Xsystab->BootServices;
573 /* Set up the console, so printf works. */
574 status = bs->LocateProtocol(&ConsoleControlGUID, NULL,
575 (VOID **)&ConsoleControl);
576 if (status == EFI_SUCCESS)
577 (void)ConsoleControl->SetMode(ConsoleControl,
578 EfiConsoleControlScreenText);
580 * Reset the console and find the best text mode.
582 conout = systab->ConOut;
583 conout->Reset(conout, TRUE);
584 max_dim = best_mode = 0;
586 status = conout->QueryMode(conout, i, &cols, &rows);
587 if (EFI_ERROR(status))
589 if (cols * rows > max_dim) {
590 max_dim = cols * rows;
595 conout->SetMode(conout, best_mode);
596 conout->EnableCursor(conout, TRUE);
597 conout->ClearScreen(conout);
599 printf("\n>> FreeBSD EFI boot block\n");
600 printf(" Loader path: %s\n\n", PATH_LOADER_EFI);
601 printf(" Initializing modules:");
602 for (i = 0; i < NUM_BOOT_MODULES; i++) {
603 printf(" %s", boot_modules[i]->name);
604 if (boot_modules[i]->init != NULL)
605 boot_modules[i]->init();
609 /* Get all the device handles */
610 hsize = (UINTN)NUM_HANDLES_INIT * sizeof(EFI_HANDLE);
611 if ((status = bs->AllocatePool(EfiLoaderData, hsize, (void **)&handles))
613 panic("Failed to allocate %d handles (%lu)", NUM_HANDLES_INIT,
614 EFI_ERROR_CODE(status));
616 status = bs->LocateHandle(ByProtocol, &BlockIoProtocolGUID, NULL,
621 case EFI_BUFFER_TOO_SMALL:
622 (void)bs->FreePool(handles);
623 if ((status = bs->AllocatePool(EfiLoaderData, hsize,
624 (void **)&handles)) != EFI_SUCCESS) {
625 panic("Failed to allocate %zu handles (%lu)", hsize /
626 sizeof(*handles), EFI_ERROR_CODE(status));
628 status = bs->LocateHandle(ByProtocol, &BlockIoProtocolGUID,
629 NULL, &hsize, handles);
630 if (status != EFI_SUCCESS)
631 panic("Failed to get device handles (%lu)\n",
632 EFI_ERROR_CODE(status));
635 panic("Failed to get device handles (%lu)",
636 EFI_ERROR_CODE(status));
639 /* Scan all partitions, probing with all modules. */
640 nhandles = hsize / sizeof(*handles);
641 printf(" Probing %zu block devices...", nhandles);
644 /* Determine the devpath of our image so we can prefer it. */
645 status = bs->HandleProtocol(image, &LoadedImageGUID, (VOID**)&img);
647 if (status == EFI_SUCCESS) {
648 status = bs->HandleProtocol(img->DeviceHandle, &DevicePathGUID,
650 if (status != EFI_SUCCESS)
651 DPRINTF("Failed to get image DevicePath (%lu)\n",
652 EFI_ERROR_CODE(status));
653 DPRINTF("boot1 imagepath: %s\n", devpath_str(imgpath));
656 for (i = 0; i < nhandles; i++)
657 probe_handle_status(handles[i], imgpath);
660 /* Status summary. */
661 for (i = 0; i < NUM_BOOT_MODULES; i++) {
663 boot_modules[i]->status();
668 /* If we get here, we're out of luck... */
669 panic("No bootable partitions found!");
673 * add_device adds a device to the passed devinfo list.
676 add_device(dev_info_t **devinfop, dev_info_t *devinfo)
680 if (*devinfop == NULL) {
685 for (dev = *devinfop; dev->next != NULL; dev = dev->next)
692 panic(const char *fmt, ...)
713 systab->ConOut->OutputString(systab->ConOut, buf);
717 systab->ConOut->OutputString(systab->ConOut, buf);