2 * Copyright (c) 2016 John Baldwin <jhb@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
33 #include <machine/_inttypes.h>
35 static EFI_GUID ImageDevicePathGUID =
36 EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID;
37 static EFI_GUID DevicePathGUID = DEVICE_PATH_PROTOCOL;
38 static EFI_GUID DevicePathToTextGUID = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
39 static EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *toTextProtocol;
40 static EFI_GUID DevicePathFromTextGUID =
41 EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL_GUID;
42 static EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *fromTextProtocol;
45 efi_lookup_image_devpath(EFI_HANDLE handle)
47 EFI_DEVICE_PATH *devpath;
50 status = OpenProtocolByHandle(handle, &ImageDevicePathGUID,
52 if (EFI_ERROR(status))
58 efi_lookup_devpath(EFI_HANDLE handle)
60 EFI_DEVICE_PATH *devpath;
63 status = OpenProtocolByHandle(handle, &DevicePathGUID,
65 if (EFI_ERROR(status))
71 efi_close_devpath(EFI_HANDLE handle)
75 status = BS->CloseProtocol(handle, &DevicePathGUID, IH, NULL);
76 if (EFI_ERROR(status))
77 printf("CloseProtocol error: %lu\n", EFI_ERROR_CODE(status));
81 efi_make_tail(char *suffix)
87 (void)asprintf(&tail, "/%s", suffix);
94 EFI_DEVICE_PATH Header;
96 UINT8 VendorDefinedData[1];
97 } __packed VENDOR_DEVICE_PATH_WITH_DATA;
100 efi_vendor_path(const char *type, VENDOR_DEVICE_PATH *node, char *suffix)
102 uint32_t size = DevicePathNodeLength(&node->Header) - sizeof(*node);
103 VENDOR_DEVICE_PATH_WITH_DATA *dp = (VENDOR_DEVICE_PATH_WITH_DATA *)node;
104 char *name, *tail, *head;
108 uuid_to_string((const uuid_t *)(void *)&node->Guid, &uuid, &rv);
112 tail = efi_make_tail(suffix);
113 rv = asprintf(&head, "%sVendor(%s)[%x:", type, uuid, size);
118 if (DevicePathNodeLength(&node->Header) > sizeof(*node)) {
119 for (uint32_t i = 0; i < size; i++) {
120 rv = asprintf(&name, "%s%02x", head,
121 dp->VendorDefinedData[i]);
132 if (asprintf(&name, "%s]%s", head, tail) < 0)
140 efi_hw_dev_path(EFI_DEVICE_PATH *node, char *suffix)
142 uint8_t subtype = DevicePathSubType(node);
145 tail = efi_make_tail(suffix);
148 if (asprintf(&name, "Pci(%x,%x)%s",
149 ((PCI_DEVICE_PATH *)node)->Function,
150 ((PCI_DEVICE_PATH *)node)->Device, tail) < 0)
154 if (asprintf(&name, "PCCARD(%x)%s",
155 ((PCCARD_DEVICE_PATH *)node)->FunctionNumber, tail) < 0)
159 if (asprintf(&name, "MMap(%x,%" PRIx64 ",%" PRIx64 ")%s",
160 ((MEMMAP_DEVICE_PATH *)node)->MemoryType,
161 ((MEMMAP_DEVICE_PATH *)node)->StartingAddress,
162 ((MEMMAP_DEVICE_PATH *)node)->EndingAddress, tail) < 0)
166 name = efi_vendor_path("Hardware",
167 (VENDOR_DEVICE_PATH *)node, tail);
169 case HW_CONTROLLER_DP:
170 if (asprintf(&name, "Ctrl(%x)%s",
171 ((CONTROLLER_DEVICE_PATH *)node)->Controller, tail) < 0)
175 if (asprintf(&name, "UnknownHW(%x)%s", subtype, tail) < 0)
184 efi_acpi_dev_path(EFI_DEVICE_PATH *node, char *suffix)
186 uint8_t subtype = DevicePathSubType(node);
187 ACPI_HID_DEVICE_PATH *acpi = (ACPI_HID_DEVICE_PATH *)node;
190 tail = efi_make_tail(suffix);
193 if ((acpi->HID & PNP_EISA_ID_MASK) == PNP_EISA_ID_CONST) {
194 switch (EISA_ID_TO_NUM (acpi->HID)) {
196 if (asprintf(&name, "PciRoot(%x)%s",
197 acpi->UID, tail) < 0)
201 if (asprintf(&name, "PcieRoot(%x)%s",
202 acpi->UID, tail) < 0)
206 if (asprintf(&name, "Floppy(%x)%s",
207 acpi->UID, tail) < 0)
211 if (asprintf(&name, "Keyboard(%x)%s",
212 acpi->UID, tail) < 0)
216 if (asprintf(&name, "Serial(%x)%s",
217 acpi->UID, tail) < 0)
221 if (asprintf(&name, "ParallelPort(%x)%s",
222 acpi->UID, tail) < 0)
226 if (asprintf(&name, "Acpi(PNP%04x,%x)%s",
227 EISA_ID_TO_NUM(acpi->HID),
228 acpi->UID, tail) < 0)
233 if (asprintf(&name, "Acpi(%08x,%x)%s",
234 acpi->HID, acpi->UID, tail) < 0)
238 case ACPI_EXTENDED_DP:
240 if (asprintf(&name, "UnknownACPI(%x)%s", subtype, tail) < 0)
249 efi_messaging_dev_path(EFI_DEVICE_PATH *node, char *suffix)
251 uint8_t subtype = DevicePathSubType(node);
255 tail = efi_make_tail(suffix);
258 if (asprintf(&name, "ATA(%s,%s,%x)%s",
259 ((ATAPI_DEVICE_PATH *)node)->PrimarySecondary == 1 ?
260 "Secondary" : "Primary",
261 ((ATAPI_DEVICE_PATH *)node)->SlaveMaster == 1 ?
263 ((ATAPI_DEVICE_PATH *)node)->Lun, tail) < 0)
267 if (asprintf(&name, "SCSI(%x,%x)%s",
268 ((SCSI_DEVICE_PATH *)node)->Pun,
269 ((SCSI_DEVICE_PATH *)node)->Lun, tail) < 0)
272 case MSG_FIBRECHANNEL_DP:
273 if (asprintf(&name, "Fibre(%" PRIx64 ",%" PRIx64 ")%s",
274 ((FIBRECHANNEL_DEVICE_PATH *)node)->WWN,
275 ((FIBRECHANNEL_DEVICE_PATH *)node)->Lun, tail) < 0)
279 if (asprintf(&name, "I1394(%016" PRIx64 ")%s",
280 ((F1394_DEVICE_PATH *)node)->Guid, tail) < 0)
284 if (asprintf(&name, "USB(%x,%x)%s",
285 ((USB_DEVICE_PATH *)node)->ParentPortNumber,
286 ((USB_DEVICE_PATH *)node)->InterfaceNumber, tail) < 0)
289 case MSG_USB_CLASS_DP:
290 if (asprintf(&name, "UsbClass(%x,%x,%x,%x,%x)%s",
291 ((USB_CLASS_DEVICE_PATH *)node)->VendorId,
292 ((USB_CLASS_DEVICE_PATH *)node)->ProductId,
293 ((USB_CLASS_DEVICE_PATH *)node)->DeviceClass,
294 ((USB_CLASS_DEVICE_PATH *)node)->DeviceSubClass,
295 ((USB_CLASS_DEVICE_PATH *)node)->DeviceProtocol, tail) < 0)
298 case MSG_MAC_ADDR_DP:
299 if (asprintf(&name, "MAC(%02x:%02x:%02x:%02x:%02x:%02x,%x)%s",
300 ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[0],
301 ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[1],
302 ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[2],
303 ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[3],
304 ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[4],
305 ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[5],
306 ((MAC_ADDR_DEVICE_PATH *)node)->IfType, tail) < 0)
310 name = efi_vendor_path("Messaging",
311 (VENDOR_DEVICE_PATH *)node, tail);
314 if (asprintf(&name, "UART(%" PRIu64 ",%u,%x,%x)%s",
315 ((UART_DEVICE_PATH *)node)->BaudRate,
316 ((UART_DEVICE_PATH *)node)->DataBits,
317 ((UART_DEVICE_PATH *)node)->Parity,
318 ((UART_DEVICE_PATH *)node)->StopBits, tail) < 0)
322 if (asprintf(&name, "Sata(%x,%x,%x)%s",
323 ((SATA_DEVICE_PATH *)node)->HBAPortNumber,
324 ((SATA_DEVICE_PATH *)node)->PortMultiplierPortNumber,
325 ((SATA_DEVICE_PATH *)node)->Lun, tail) < 0)
329 if (asprintf(&name, "UnknownMessaging(%x)%s",
339 efi_media_dev_path(EFI_DEVICE_PATH *node, char *suffix)
341 uint8_t subtype = DevicePathSubType(node);
342 HARDDRIVE_DEVICE_PATH *hd;
348 tail = efi_make_tail(suffix);
351 case MEDIA_HARDDRIVE_DP:
352 hd = (HARDDRIVE_DEVICE_PATH *)node;
353 switch (hd->SignatureType) {
354 case SIGNATURE_TYPE_MBR:
355 if (asprintf(&name, "HD(%d,MBR,%08x,%" PRIx64
358 *((uint32_t *)(uintptr_t)&hd->Signature[0]),
360 hd->PartitionSize, tail) < 0)
363 case SIGNATURE_TYPE_GUID:
365 uuid_to_string((const uuid_t *)(void *)
366 &hd->Signature[0], &str, &rv);
369 rv = asprintf(&name, "HD(%d,GPT,%s,%" PRIx64 ",%"
371 hd->PartitionNumber, str,
372 hd->PartitionStart, hd->PartitionSize, tail);
376 if (asprintf(&name, "HD(%d,%d,0)%s",
378 hd->SignatureType, tail) < 0) {
385 if (asprintf(&name, "CD(%x,%" PRIx64 ",%" PRIx64 ")%s",
386 ((CDROM_DEVICE_PATH *)node)->BootEntry,
387 ((CDROM_DEVICE_PATH *)node)->PartitionStart,
388 ((CDROM_DEVICE_PATH *)node)->PartitionSize, tail) < 0) {
392 case MEDIA_VENDOR_DP:
393 name = efi_vendor_path("Media",
394 (VENDOR_DEVICE_PATH *)node, tail);
396 case MEDIA_FILEPATH_DP:
399 if (ucs2_to_utf8(((FILEPATH_DEVICE_PATH *)node)->PathName,
401 (void)asprintf(&name, "%s%s", str, tail);
405 case MEDIA_PROTOCOL_DP:
407 uuid_to_string((const uuid_t *)(void *)
408 &((MEDIA_PROTOCOL_DEVICE_PATH *)node)->Protocol,
412 rv = asprintf(&name, "Protocol(%s)%s", str, tail);
416 if (asprintf(&name, "UnknownMedia(%x)%s",
425 efi_translate_devpath(EFI_DEVICE_PATH *devpath)
427 EFI_DEVICE_PATH *dp = NextDevicePathNode(devpath);
431 if (!IsDevicePathEnd(devpath))
432 name = efi_translate_devpath(dp);
437 type = DevicePathType(devpath);
439 case HARDWARE_DEVICE_PATH:
440 ptr = efi_hw_dev_path(devpath, name);
442 case ACPI_DEVICE_PATH:
443 ptr = efi_acpi_dev_path(devpath, name);
445 case MESSAGING_DEVICE_PATH:
446 ptr = efi_messaging_dev_path(devpath, name);
448 case MEDIA_DEVICE_PATH:
449 ptr = efi_media_dev_path(devpath, name);
451 case BBS_DEVICE_PATH:
453 if (asprintf(&ptr, "UnknownPath(%x)%s", type,
454 name? name : "") < 0)
467 efi_devpath_to_name(EFI_DEVICE_PATH *devpath)
474 name = efi_translate_devpath(devpath);
479 * We need to return memory from AllocatePool, so it can be freed
480 * with FreePool() in efi_free_devpath_name().
482 rv = utf8_to_ucs2(name, &ptr, &len);
488 status = BS->AllocatePool(EfiLoaderData, len, (void **)&out);
489 if (EFI_ERROR(status)) {
493 memcpy(out, ptr, len);
502 efi_devpath_name(EFI_DEVICE_PATH *devpath)
508 if (toTextProtocol == NULL) {
509 status = BS->LocateProtocol(&DevicePathToTextGUID, NULL,
510 (VOID **)&toTextProtocol);
511 if (EFI_ERROR(status))
512 toTextProtocol = NULL;
514 if (toTextProtocol == NULL)
515 return (efi_devpath_to_name(devpath));
517 return (toTextProtocol->ConvertDevicePathToText(devpath, TRUE, TRUE));
521 efi_free_devpath_name(CHAR16 *text)
528 efi_name_to_devpath(const char *path)
530 EFI_DEVICE_PATH *devpath;
535 if (utf8_to_ucs2(path, &uv, &ul) != 0)
537 devpath = efi_name_to_devpath16(uv);
543 efi_name_to_devpath16(CHAR16 *path)
549 if (fromTextProtocol == NULL) {
550 status = BS->LocateProtocol(&DevicePathFromTextGUID, NULL,
551 (VOID **)&fromTextProtocol);
552 if (EFI_ERROR(status))
553 fromTextProtocol = NULL;
555 if (fromTextProtocol == NULL)
558 return (fromTextProtocol->ConvertTextToDevicePath(path));
561 void efi_devpath_free(EFI_DEVICE_PATH *devpath)
564 BS->FreePool(devpath);
568 efi_devpath_last_node(EFI_DEVICE_PATH *devpath)
571 if (IsDevicePathEnd(devpath))
573 while (!IsDevicePathEnd(NextDevicePathNode(devpath)))
574 devpath = NextDevicePathNode(devpath);
579 efi_devpath_trim(EFI_DEVICE_PATH *devpath)
581 EFI_DEVICE_PATH *node, *copy;
584 if ((node = efi_devpath_last_node(devpath)) == NULL)
586 prefix = (UINT8 *)node - (UINT8 *)devpath;
589 len = prefix + DevicePathNodeLength(NextDevicePathNode(node));
592 memcpy(copy, devpath, prefix);
593 node = (EFI_DEVICE_PATH *)((UINT8 *)copy + prefix);
594 SetDevicePathEndNode(node);
600 efi_devpath_handle(EFI_DEVICE_PATH *devpath)
606 * There isn't a standard way to locate a handle for a given
607 * device path. However, querying the EFI_DEVICE_PATH protocol
608 * for a given device path should give us a handle for the
609 * closest node in the path to the end that is valid.
611 status = BS->LocateDevicePath(&DevicePathGUID, &devpath, &h);
612 if (EFI_ERROR(status))
618 efi_devpath_match_node(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
622 if (devpath1 == NULL || devpath2 == NULL)
624 if (DevicePathType(devpath1) != DevicePathType(devpath2) ||
625 DevicePathSubType(devpath1) != DevicePathSubType(devpath2))
627 len = DevicePathNodeLength(devpath1);
628 if (len != DevicePathNodeLength(devpath2))
630 if (memcmp(devpath1, devpath2, len) != 0)
636 _efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2,
640 if (devpath1 == NULL || devpath2 == NULL)
645 IsDevicePathType(devpath1, MEDIA_DEVICE_PATH) &&
646 IsDevicePathType(devpath2, MEDIA_DEVICE_PATH))
648 if (!efi_devpath_match_node(devpath1, devpath2))
650 if (IsDevicePathEnd(devpath1))
652 devpath1 = NextDevicePathNode(devpath1);
653 devpath2 = NextDevicePathNode(devpath2);
658 * Are two devpaths identical?
661 efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
663 return _efi_devpath_match(devpath1, devpath2, false);
667 * Like efi_devpath_match, but stops at when we hit the media device
668 * path node that specifies the partition information. If we match
669 * up to that point, then we're on the same disk.
672 efi_devpath_same_disk(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
674 return _efi_devpath_match(devpath1, devpath2, true);
678 efi_devpath_is_prefix(EFI_DEVICE_PATH *prefix, EFI_DEVICE_PATH *path)
682 if (prefix == NULL || path == NULL)
686 if (IsDevicePathEnd(prefix))
689 if (DevicePathType(prefix) != DevicePathType(path) ||
690 DevicePathSubType(prefix) != DevicePathSubType(path))
693 len = DevicePathNodeLength(prefix);
694 if (len != DevicePathNodeLength(path))
697 if (memcmp(prefix, path, len) != 0)
700 prefix = NextDevicePathNode(prefix);
701 path = NextDevicePathNode(path);
707 * Skip over the 'prefix' part of path and return the part of the path
708 * that starts with the first node that's a MEDIA_DEVICE_PATH.
711 efi_devpath_to_media_path(EFI_DEVICE_PATH *path)
714 while (!IsDevicePathEnd(path)) {
715 if (DevicePathType(path) == MEDIA_DEVICE_PATH)
717 path = NextDevicePathNode(path);
723 efi_devpath_length(EFI_DEVICE_PATH *path)
725 EFI_DEVICE_PATH *start = path;
727 while (!IsDevicePathEnd(path))
728 path = NextDevicePathNode(path);
729 return ((UINTN)path - (UINTN)start) + DevicePathNodeLength(path);
733 efi_devpath_to_handle(EFI_DEVICE_PATH *path, EFI_HANDLE *handles, unsigned nhandles)
736 EFI_DEVICE_PATH *media, *devpath;
739 media = efi_devpath_to_media_path(path);
742 for (i = 0; i < nhandles; i++) {
744 devpath = efi_lookup_devpath(h);
747 if (!efi_devpath_match_node(media, efi_devpath_to_media_path(devpath)))