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)
82 (void)bs->FreePool(buf);
86 * nodes_match returns TRUE if the imgpath isn't NULL and the nodes match,
90 nodes_match(EFI_DEVICE_PATH *imgpath, EFI_DEVICE_PATH *devpath)
94 if (imgpath == NULL || imgpath->Type != devpath->Type ||
95 imgpath->SubType != devpath->SubType)
98 len = DevicePathNodeLength(imgpath);
99 if (len != DevicePathNodeLength(devpath))
102 return (memcmp(imgpath, devpath, (size_t)len) == 0);
106 * device_paths_match returns TRUE if the imgpath isn't NULL and all nodes
107 * in imgpath and devpath match up to their respective occurrences of a
108 * media node, FALSE otherwise.
111 device_paths_match(EFI_DEVICE_PATH *imgpath, EFI_DEVICE_PATH *devpath)
117 while (!IsDevicePathEnd(imgpath) && !IsDevicePathEnd(devpath)) {
118 if (IsDevicePathType(imgpath, MEDIA_DEVICE_PATH) &&
119 IsDevicePathType(devpath, MEDIA_DEVICE_PATH))
122 if (!nodes_match(imgpath, devpath))
125 imgpath = NextDevicePathNode(imgpath);
126 devpath = NextDevicePathNode(devpath);
133 * devpath_last returns the last non-path end node in devpath.
135 static EFI_DEVICE_PATH *
136 devpath_last(EFI_DEVICE_PATH *devpath)
139 while (!IsDevicePathEnd(NextDevicePathNode(devpath)))
140 devpath = NextDevicePathNode(devpath);
146 * devpath_node_str is a basic output method for a devpath node which
147 * only understands a subset of the available sub types.
149 * If we switch to UEFI 2.x then we should update it to use:
150 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
153 devpath_node_str(char *buf, size_t size, EFI_DEVICE_PATH *devpath)
156 switch (devpath->Type) {
157 case MESSAGING_DEVICE_PATH:
158 switch (devpath->SubType) {
160 ATAPI_DEVICE_PATH *atapi;
162 atapi = (ATAPI_DEVICE_PATH *)(void *)devpath;
163 return snprintf(buf, size, "ata(%s,%s,0x%x)",
164 (atapi->PrimarySecondary == 1) ? "Sec" : "Pri",
165 (atapi->SlaveMaster == 1) ? "Slave" : "Master",
169 USB_DEVICE_PATH *usb;
171 usb = (USB_DEVICE_PATH *)devpath;
172 return snprintf(buf, size, "usb(0x%02x,0x%02x)",
173 usb->ParentPortNumber, usb->InterfaceNumber);
176 SCSI_DEVICE_PATH *scsi;
178 scsi = (SCSI_DEVICE_PATH *)(void *)devpath;
179 return snprintf(buf, size, "scsi(0x%02x,0x%02x)",
180 scsi->Pun, scsi->Lun);
183 SATA_DEVICE_PATH *sata;
185 sata = (SATA_DEVICE_PATH *)(void *)devpath;
186 return snprintf(buf, size, "sata(0x%x,0x%x,0x%x)",
187 sata->HBAPortNumber, sata->PortMultiplierPortNumber,
191 return snprintf(buf, size, "msg(0x%02x)",
195 case HARDWARE_DEVICE_PATH:
196 switch (devpath->SubType) {
198 PCI_DEVICE_PATH *pci;
200 pci = (PCI_DEVICE_PATH *)devpath;
201 return snprintf(buf, size, "pci(0x%02x,0x%02x)",
202 pci->Device, pci->Function);
205 return snprintf(buf, size, "hw(0x%02x)",
209 case ACPI_DEVICE_PATH: {
210 ACPI_HID_DEVICE_PATH *acpi;
212 acpi = (ACPI_HID_DEVICE_PATH *)(void *)devpath;
213 if ((acpi->HID & PNP_EISA_ID_MASK) == PNP_EISA_ID_CONST) {
214 switch (EISA_ID_TO_NUM(acpi->HID)) {
216 return snprintf(buf, size, "pciroot(0x%x)",
219 return snprintf(buf, size, "pcieroot(0x%x)",
222 return snprintf(buf, size, "floppy(0x%x)",
225 return snprintf(buf, size, "keyboard(0x%x)",
228 return snprintf(buf, size, "serial(0x%x)",
231 return snprintf(buf, size, "parallelport(0x%x)",
234 return snprintf(buf, size, "acpi(pnp%04x,0x%x)",
235 EISA_ID_TO_NUM(acpi->HID), acpi->UID);
239 return snprintf(buf, size, "acpi(0x%08x,0x%x)", acpi->HID,
242 case MEDIA_DEVICE_PATH:
243 switch (devpath->SubType) {
244 case MEDIA_CDROM_DP: {
245 CDROM_DEVICE_PATH *cdrom;
247 cdrom = (CDROM_DEVICE_PATH *)(void *)devpath;
248 return snprintf(buf, size, "cdrom(%x)",
251 case MEDIA_HARDDRIVE_DP: {
252 HARDDRIVE_DEVICE_PATH *hd;
254 hd = (HARDDRIVE_DEVICE_PATH *)(void *)devpath;
255 return snprintf(buf, size, "hd(%x)",
256 hd->PartitionNumber);
259 return snprintf(buf, size, "media(0x%02x)",
262 case BBS_DEVICE_PATH:
263 return snprintf(buf, size, "bbs(0x%02x)", devpath->SubType);
264 case END_DEVICE_PATH_TYPE:
268 return snprintf(buf, size, "type(0x%02x, 0x%02x)", devpath->Type,
273 * devpath_strlcat appends a text description of devpath to buf but not more
274 * than size - 1 characters followed by NUL-terminator.
277 devpath_strlcat(char *buf, size_t size, EFI_DEVICE_PATH *devpath)
284 while (!IsDevicePathEnd(devpath)) {
285 len = snprintf(buf, size - used, "%s", sep);
291 len = devpath_node_str(buf, size - used, devpath);
296 devpath = NextDevicePathNode(devpath);
304 * devpath_str is convenience method which returns the text description of
305 * devpath using a static buffer, so it isn't thread safe!
308 devpath_str(EFI_DEVICE_PATH *devpath)
310 static char buf[256];
312 devpath_strlcat(buf, sizeof(buf), devpath);
318 * load_loader attempts to load the loader image data.
320 * It tries each module and its respective devices, identified by mod->probe,
321 * in order until a successful load occurs at which point it returns EFI_SUCCESS
322 * and EFI_NOT_FOUND otherwise.
324 * Only devices which have preferred matching the preferred parameter are tried.
327 load_loader(const boot_module_t **modp, dev_info_t **devinfop, void **bufp,
328 size_t *bufsize, BOOLEAN preferred)
332 const boot_module_t *mod;
334 for (i = 0; i < NUM_BOOT_MODULES; i++) {
335 mod = boot_modules[i];
336 for (dev = mod->devices(); dev != NULL; dev = dev->next) {
337 if (dev->preferred != preferred)
340 if (mod->load(PATH_LOADER_EFI, dev, bufp, bufsize) ==
344 return (EFI_SUCCESS);
349 return (EFI_NOT_FOUND);
353 * try_boot only returns if it fails to load the loader. If it succeeds
354 * it simply boots, otherwise it returns the status of last EFI call.
359 size_t bufsize, loadersize, cmdsize;
360 void *buf, *loaderbuf;
363 const boot_module_t *mod;
364 EFI_HANDLE loaderhandle;
365 EFI_LOADED_IMAGE *loaded_image;
368 status = load_loader(&mod, &dev, &loaderbuf, &loadersize, TRUE);
369 if (status != EFI_SUCCESS) {
370 status = load_loader(&mod, &dev, &loaderbuf, &loadersize,
372 if (status != EFI_SUCCESS) {
373 printf("Failed to load '%s'\n", PATH_LOADER_EFI);
379 * Read in and parse the command line from /boot.config or /boot/config,
380 * if present. We'll pass it the next stage via a simple ASCII
381 * string. loader.efi has a hack for ASCII strings, so we'll use that to
382 * keep the size down here. We only try to read the alternate file if
383 * we get EFI_NOT_FOUND because all other errors mean that the boot_module
384 * had troubles with the filesystem. We could return early, but we'll let
385 * loading the actual kernel sort all that out. Since these files are
386 * optional, we don't report errors in trying to read them.
390 status = mod->load(PATH_DOTCONFIG, dev, &buf, &bufsize);
391 if (status == EFI_NOT_FOUND)
392 status = mod->load(PATH_CONFIG, dev, &buf, &bufsize);
393 if (status == EFI_SUCCESS) {
394 cmdsize = bufsize + 1;
395 cmd = malloc(cmdsize);
398 memcpy(cmd, buf, bufsize);
404 if ((status = bs->LoadImage(TRUE, image, devpath_last(dev->devpath),
405 loaderbuf, loadersize, &loaderhandle)) != EFI_SUCCESS) {
406 printf("Failed to load image provided by %s, size: %zu, (%lu)\n",
407 mod->name, loadersize, EFI_ERROR_CODE(status));
411 if ((status = bs->HandleProtocol(loaderhandle, &LoadedImageGUID,
412 (VOID**)&loaded_image)) != EFI_SUCCESS) {
413 printf("Failed to query LoadedImage provided by %s (%lu)\n",
414 mod->name, EFI_ERROR_CODE(status));
419 printf(" command args: %s\n", cmd);
421 loaded_image->DeviceHandle = dev->devhandle;
422 loaded_image->LoadOptionsSize = cmdsize;
423 loaded_image->LoadOptions = cmd;
425 DPRINTF("Starting '%s' in 5 seconds...", PATH_LOADER_EFI);
437 if ((status = bs->StartImage(loaderhandle, NULL, NULL)) !=
439 printf("Failed to start image provided by %s (%lu)\n",
440 mod->name, EFI_ERROR_CODE(status));
441 loaded_image->LoadOptionsSize = 0;
442 loaded_image->LoadOptions = NULL;
450 if (loaderbuf != NULL)
457 * probe_handle determines if the passed handle represents a logical partition
458 * if it does it uses each module in order to probe it and if successful it
459 * returns EFI_SUCCESS.
462 probe_handle(EFI_HANDLE h, EFI_DEVICE_PATH *imgpath, BOOLEAN *preferred)
466 EFI_DEVICE_PATH *devpath;
470 /* Figure out if we're dealing with an actual partition. */
471 status = bs->HandleProtocol(h, &DevicePathGUID, (void **)&devpath);
472 if (status == EFI_UNSUPPORTED)
475 if (status != EFI_SUCCESS) {
476 DPRINTF("\nFailed to query DevicePath (%lu)\n",
477 EFI_ERROR_CODE(status));
481 DPRINTF("probing: %s\n", devpath_str(devpath));
483 status = bs->HandleProtocol(h, &BlockIoProtocolGUID, (void **)&blkio);
484 if (status == EFI_UNSUPPORTED)
487 if (status != EFI_SUCCESS) {
488 DPRINTF("\nFailed to query BlockIoProtocol (%lu)\n",
489 EFI_ERROR_CODE(status));
493 if (!blkio->Media->LogicalPartition)
494 return (EFI_UNSUPPORTED);
496 *preferred = device_paths_match(imgpath, devpath);
498 /* Run through each module, see if it can load this partition */
499 for (i = 0; i < NUM_BOOT_MODULES; i++) {
500 if ((status = bs->AllocatePool(EfiLoaderData,
501 sizeof(*devinfo), (void **)&devinfo)) !=
503 DPRINTF("\nFailed to allocate devinfo (%lu)\n",
504 EFI_ERROR_CODE(status));
507 devinfo->dev = blkio;
508 devinfo->devpath = devpath;
509 devinfo->devhandle = h;
510 devinfo->devdata = NULL;
511 devinfo->preferred = *preferred;
512 devinfo->next = NULL;
514 status = boot_modules[i]->probe(devinfo);
515 if (status == EFI_SUCCESS)
516 return (EFI_SUCCESS);
517 (void)bs->FreePool(devinfo);
520 return (EFI_UNSUPPORTED);
524 * probe_handle_status calls probe_handle and outputs the returned status
528 probe_handle_status(EFI_HANDLE h, EFI_DEVICE_PATH *imgpath)
533 status = probe_handle(h, imgpath, &preferred);
537 case EFI_UNSUPPORTED:
539 DPRINTF(" not supported\n");
544 DPRINTF(" supported (preferred)\n");
547 DPRINTF(" supported\n");
552 DPRINTF(" error (%lu)\n", EFI_ERROR_CODE(status));
559 efi_main(EFI_HANDLE Ximage, EFI_SYSTEM_TABLE *Xsystab)
562 EFI_LOADED_IMAGE *img;
563 EFI_DEVICE_PATH *imgpath;
565 EFI_CONSOLE_CONTROL_PROTOCOL *ConsoleControl = NULL;
566 SIMPLE_TEXT_OUTPUT_INTERFACE *conout = NULL;
567 UINTN i, max_dim, best_mode, cols, rows, hsize, nhandles;
569 /* Basic initialization*/
572 bs = Xsystab->BootServices;
574 /* Set up the console, so printf works. */
575 status = bs->LocateProtocol(&ConsoleControlGUID, NULL,
576 (VOID **)&ConsoleControl);
577 if (status == EFI_SUCCESS)
578 (void)ConsoleControl->SetMode(ConsoleControl,
579 EfiConsoleControlScreenText);
581 * Reset the console and find the best text mode.
583 conout = systab->ConOut;
584 conout->Reset(conout, TRUE);
585 max_dim = best_mode = 0;
587 status = conout->QueryMode(conout, i, &cols, &rows);
588 if (EFI_ERROR(status))
590 if (cols * rows > max_dim) {
591 max_dim = cols * rows;
596 conout->SetMode(conout, best_mode);
597 conout->EnableCursor(conout, TRUE);
598 conout->ClearScreen(conout);
600 printf("\n>> FreeBSD EFI boot block\n");
601 printf(" Loader path: %s\n\n", PATH_LOADER_EFI);
602 printf(" Initializing modules:");
603 for (i = 0; i < NUM_BOOT_MODULES; i++) {
604 printf(" %s", boot_modules[i]->name);
605 if (boot_modules[i]->init != NULL)
606 boot_modules[i]->init();
610 /* Get all the device handles */
611 hsize = (UINTN)NUM_HANDLES_INIT * sizeof(EFI_HANDLE);
612 if ((status = bs->AllocatePool(EfiLoaderData, hsize, (void **)&handles))
614 panic("Failed to allocate %d handles (%lu)", NUM_HANDLES_INIT,
615 EFI_ERROR_CODE(status));
617 status = bs->LocateHandle(ByProtocol, &BlockIoProtocolGUID, NULL,
622 case EFI_BUFFER_TOO_SMALL:
623 (void)bs->FreePool(handles);
624 if ((status = bs->AllocatePool(EfiLoaderData, hsize,
625 (void **)&handles)) != EFI_SUCCESS) {
626 panic("Failed to allocate %zu handles (%lu)", hsize /
627 sizeof(*handles), EFI_ERROR_CODE(status));
629 status = bs->LocateHandle(ByProtocol, &BlockIoProtocolGUID,
630 NULL, &hsize, handles);
631 if (status != EFI_SUCCESS)
632 panic("Failed to get device handles (%lu)\n",
633 EFI_ERROR_CODE(status));
636 panic("Failed to get device handles (%lu)",
637 EFI_ERROR_CODE(status));
640 /* Scan all partitions, probing with all modules. */
641 nhandles = hsize / sizeof(*handles);
642 printf(" Probing %zu block devices...", nhandles);
645 /* Determine the devpath of our image so we can prefer it. */
646 status = bs->HandleProtocol(image, &LoadedImageGUID, (VOID**)&img);
648 if (status == EFI_SUCCESS) {
649 status = bs->HandleProtocol(img->DeviceHandle, &DevicePathGUID,
651 if (status != EFI_SUCCESS)
652 DPRINTF("Failed to get image DevicePath (%lu)\n",
653 EFI_ERROR_CODE(status));
654 DPRINTF("boot1 imagepath: %s\n", devpath_str(imgpath));
657 for (i = 0; i < nhandles; i++)
658 probe_handle_status(handles[i], imgpath);
661 /* Status summary. */
662 for (i = 0; i < NUM_BOOT_MODULES; i++) {
664 boot_modules[i]->status();
669 /* If we get here, we're out of luck... */
670 panic("No bootable partitions found!");
674 * add_device adds a device to the passed devinfo list.
677 add_device(dev_info_t **devinfop, dev_info_t *devinfo)
681 if (*devinfop == NULL) {
686 for (dev = *devinfop; dev->next != NULL; dev = dev->next)
693 panic(const char *fmt, ...)
714 systab->ConOut->OutputString(systab->ConOut, buf);
718 systab->ConOut->OutputString(systab->ConOut, buf);