13e5a19beSJohn Baldwin /*-
23e5a19beSJohn Baldwin * Copyright (c) 2016 John Baldwin <jhb@FreeBSD.org>
33e5a19beSJohn Baldwin *
43e5a19beSJohn Baldwin * Redistribution and use in source and binary forms, with or without
53e5a19beSJohn Baldwin * modification, are permitted provided that the following conditions
63e5a19beSJohn Baldwin * are met:
73e5a19beSJohn Baldwin * 1. Redistributions of source code must retain the above copyright
83e5a19beSJohn Baldwin * notice, this list of conditions and the following disclaimer.
93e5a19beSJohn Baldwin * 2. Redistributions in binary form must reproduce the above copyright
103e5a19beSJohn Baldwin * notice, this list of conditions and the following disclaimer in the
113e5a19beSJohn Baldwin * documentation and/or other materials provided with the distribution.
123e5a19beSJohn Baldwin *
133e5a19beSJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
143e5a19beSJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
153e5a19beSJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
163e5a19beSJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
173e5a19beSJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
183e5a19beSJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
193e5a19beSJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
203e5a19beSJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
213e5a19beSJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
223e5a19beSJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
233e5a19beSJohn Baldwin * SUCH DAMAGE.
243e5a19beSJohn Baldwin */
253e5a19beSJohn Baldwin
263e5a19beSJohn Baldwin #include <efi.h>
273e5a19beSJohn Baldwin #include <efilib.h>
2816b07b25SWarner Losh #include <efichar.h>
295403ae84SToomas Soome #include <uuid.h>
305403ae84SToomas Soome #include <machine/_inttypes.h>
3143b8edb3SWarner Losh #include <Protocol/DevicePathToText.h>
3243b8edb3SWarner Losh #include <Protocol/DevicePathFromText.h>
333e5a19beSJohn Baldwin
343e5a19beSJohn Baldwin static EFI_GUID ImageDevicePathGUID =
353e5a19beSJohn Baldwin EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID;
363e5a19beSJohn Baldwin static EFI_GUID DevicePathGUID = DEVICE_PATH_PROTOCOL;
373e5a19beSJohn Baldwin static EFI_GUID DevicePathToTextGUID = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
3816b07b25SWarner Losh static EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *toTextProtocol;
395403ae84SToomas Soome static EFI_GUID DevicePathFromTextGUID =
405403ae84SToomas Soome EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL_GUID;
4116b07b25SWarner Losh static EFI_DEVICE_PATH_FROM_TEXT_PROTOCOL *fromTextProtocol;
423e5a19beSJohn Baldwin
433e5a19beSJohn Baldwin EFI_DEVICE_PATH *
efi_lookup_image_devpath(EFI_HANDLE handle)443e5a19beSJohn Baldwin efi_lookup_image_devpath(EFI_HANDLE handle)
453e5a19beSJohn Baldwin {
463e5a19beSJohn Baldwin EFI_DEVICE_PATH *devpath;
473e5a19beSJohn Baldwin EFI_STATUS status;
483e5a19beSJohn Baldwin
49110d56cbSToomas Soome status = OpenProtocolByHandle(handle, &ImageDevicePathGUID,
50110d56cbSToomas Soome (void **)&devpath);
513e5a19beSJohn Baldwin if (EFI_ERROR(status))
523e5a19beSJohn Baldwin devpath = NULL;
533e5a19beSJohn Baldwin return (devpath);
543e5a19beSJohn Baldwin }
553e5a19beSJohn Baldwin
563e5a19beSJohn Baldwin EFI_DEVICE_PATH *
efi_lookup_devpath(EFI_HANDLE handle)573e5a19beSJohn Baldwin efi_lookup_devpath(EFI_HANDLE handle)
583e5a19beSJohn Baldwin {
593e5a19beSJohn Baldwin EFI_DEVICE_PATH *devpath;
603e5a19beSJohn Baldwin EFI_STATUS status;
613e5a19beSJohn Baldwin
62110d56cbSToomas Soome status = OpenProtocolByHandle(handle, &DevicePathGUID,
63110d56cbSToomas Soome (void **)&devpath);
643e5a19beSJohn Baldwin if (EFI_ERROR(status))
653e5a19beSJohn Baldwin devpath = NULL;
663e5a19beSJohn Baldwin return (devpath);
673e5a19beSJohn Baldwin }
683e5a19beSJohn Baldwin
69523a713fSToomas Soome void
efi_close_devpath(EFI_HANDLE handle)70523a713fSToomas Soome efi_close_devpath(EFI_HANDLE handle)
71523a713fSToomas Soome {
72523a713fSToomas Soome EFI_STATUS status;
73523a713fSToomas Soome
74523a713fSToomas Soome status = BS->CloseProtocol(handle, &DevicePathGUID, IH, NULL);
75523a713fSToomas Soome if (EFI_ERROR(status))
7687795c6eSWarner Losh printf("CloseProtocol error: %lu\n", DECODE_ERROR(status));
77523a713fSToomas Soome }
78523a713fSToomas Soome
795403ae84SToomas Soome static char *
efi_make_tail(char * suffix)805403ae84SToomas Soome efi_make_tail(char *suffix)
815403ae84SToomas Soome {
825403ae84SToomas Soome char *tail;
835403ae84SToomas Soome
845403ae84SToomas Soome tail = NULL;
855403ae84SToomas Soome if (suffix != NULL)
865403ae84SToomas Soome (void)asprintf(&tail, "/%s", suffix);
875403ae84SToomas Soome else
885403ae84SToomas Soome tail = strdup("");
895403ae84SToomas Soome return (tail);
905403ae84SToomas Soome }
915403ae84SToomas Soome
925403ae84SToomas Soome typedef struct {
935403ae84SToomas Soome EFI_DEVICE_PATH Header;
945403ae84SToomas Soome EFI_GUID Guid;
955403ae84SToomas Soome UINT8 VendorDefinedData[1];
965403ae84SToomas Soome } __packed VENDOR_DEVICE_PATH_WITH_DATA;
975403ae84SToomas Soome
985403ae84SToomas Soome static char *
efi_vendor_path(const char * type,VENDOR_DEVICE_PATH * node,char * suffix)995403ae84SToomas Soome efi_vendor_path(const char *type, VENDOR_DEVICE_PATH *node, char *suffix)
1005403ae84SToomas Soome {
1015403ae84SToomas Soome uint32_t size = DevicePathNodeLength(&node->Header) - sizeof(*node);
1025403ae84SToomas Soome VENDOR_DEVICE_PATH_WITH_DATA *dp = (VENDOR_DEVICE_PATH_WITH_DATA *)node;
1035403ae84SToomas Soome char *name, *tail, *head;
1045403ae84SToomas Soome char *uuid;
1055403ae84SToomas Soome int rv;
1065403ae84SToomas Soome
1075403ae84SToomas Soome uuid_to_string((const uuid_t *)(void *)&node->Guid, &uuid, &rv);
1085403ae84SToomas Soome if (rv != uuid_s_ok)
1095403ae84SToomas Soome return (NULL);
1105403ae84SToomas Soome
1115403ae84SToomas Soome tail = efi_make_tail(suffix);
1125403ae84SToomas Soome rv = asprintf(&head, "%sVendor(%s)[%x:", type, uuid, size);
1135403ae84SToomas Soome free(uuid);
1145403ae84SToomas Soome if (rv < 0)
1155403ae84SToomas Soome return (NULL);
1165403ae84SToomas Soome
1175403ae84SToomas Soome if (DevicePathNodeLength(&node->Header) > sizeof(*node)) {
1185403ae84SToomas Soome for (uint32_t i = 0; i < size; i++) {
1195403ae84SToomas Soome rv = asprintf(&name, "%s%02x", head,
1205403ae84SToomas Soome dp->VendorDefinedData[i]);
1215403ae84SToomas Soome if (rv < 0) {
1225403ae84SToomas Soome free(tail);
1235403ae84SToomas Soome free(head);
1245403ae84SToomas Soome return (NULL);
1255403ae84SToomas Soome }
1265403ae84SToomas Soome free(head);
1275403ae84SToomas Soome head = name;
1285403ae84SToomas Soome }
1295403ae84SToomas Soome }
1305403ae84SToomas Soome
1315403ae84SToomas Soome if (asprintf(&name, "%s]%s", head, tail) < 0)
1325403ae84SToomas Soome name = NULL;
1335403ae84SToomas Soome free(head);
1345403ae84SToomas Soome free(tail);
1355403ae84SToomas Soome return (name);
1365403ae84SToomas Soome }
1375403ae84SToomas Soome
1385403ae84SToomas Soome static char *
efi_hw_dev_path(EFI_DEVICE_PATH * node,char * suffix)1395403ae84SToomas Soome efi_hw_dev_path(EFI_DEVICE_PATH *node, char *suffix)
1405403ae84SToomas Soome {
1415403ae84SToomas Soome uint8_t subtype = DevicePathSubType(node);
1425403ae84SToomas Soome char *name, *tail;
1435403ae84SToomas Soome
1445403ae84SToomas Soome tail = efi_make_tail(suffix);
1455403ae84SToomas Soome switch (subtype) {
1465403ae84SToomas Soome case HW_PCI_DP:
1475403ae84SToomas Soome if (asprintf(&name, "Pci(%x,%x)%s",
148454630c7SKyle Evans ((PCI_DEVICE_PATH *)node)->Device,
149454630c7SKyle Evans ((PCI_DEVICE_PATH *)node)->Function, tail) < 0)
1505403ae84SToomas Soome name = NULL;
1515403ae84SToomas Soome break;
1525403ae84SToomas Soome case HW_PCCARD_DP:
1535403ae84SToomas Soome if (asprintf(&name, "PCCARD(%x)%s",
1545403ae84SToomas Soome ((PCCARD_DEVICE_PATH *)node)->FunctionNumber, tail) < 0)
1555403ae84SToomas Soome name = NULL;
1565403ae84SToomas Soome break;
1575403ae84SToomas Soome case HW_MEMMAP_DP:
1585403ae84SToomas Soome if (asprintf(&name, "MMap(%x,%" PRIx64 ",%" PRIx64 ")%s",
1595403ae84SToomas Soome ((MEMMAP_DEVICE_PATH *)node)->MemoryType,
1605403ae84SToomas Soome ((MEMMAP_DEVICE_PATH *)node)->StartingAddress,
1615403ae84SToomas Soome ((MEMMAP_DEVICE_PATH *)node)->EndingAddress, tail) < 0)
1625403ae84SToomas Soome name = NULL;
1635403ae84SToomas Soome break;
1645403ae84SToomas Soome case HW_VENDOR_DP:
1655403ae84SToomas Soome name = efi_vendor_path("Hardware",
1665403ae84SToomas Soome (VENDOR_DEVICE_PATH *)node, tail);
1675403ae84SToomas Soome break;
1685403ae84SToomas Soome case HW_CONTROLLER_DP:
1695403ae84SToomas Soome if (asprintf(&name, "Ctrl(%x)%s",
17043b8edb3SWarner Losh ((CONTROLLER_DEVICE_PATH *)node)->ControllerNumber, tail) < 0)
1715403ae84SToomas Soome name = NULL;
1725403ae84SToomas Soome break;
1735403ae84SToomas Soome default:
1745403ae84SToomas Soome if (asprintf(&name, "UnknownHW(%x)%s", subtype, tail) < 0)
1755403ae84SToomas Soome name = NULL;
1765403ae84SToomas Soome break;
1775403ae84SToomas Soome }
1785403ae84SToomas Soome free(tail);
1795403ae84SToomas Soome return (name);
1805403ae84SToomas Soome }
1815403ae84SToomas Soome
1825403ae84SToomas Soome static char *
efi_acpi_dev_path(EFI_DEVICE_PATH * node,char * suffix)1835403ae84SToomas Soome efi_acpi_dev_path(EFI_DEVICE_PATH *node, char *suffix)
1845403ae84SToomas Soome {
1855403ae84SToomas Soome uint8_t subtype = DevicePathSubType(node);
1865403ae84SToomas Soome ACPI_HID_DEVICE_PATH *acpi = (ACPI_HID_DEVICE_PATH *)node;
1875403ae84SToomas Soome char *name, *tail;
1885403ae84SToomas Soome
1895403ae84SToomas Soome tail = efi_make_tail(suffix);
1905403ae84SToomas Soome switch (subtype) {
1915403ae84SToomas Soome case ACPI_DP:
1925403ae84SToomas Soome if ((acpi->HID & PNP_EISA_ID_MASK) == PNP_EISA_ID_CONST) {
1935403ae84SToomas Soome switch (EISA_ID_TO_NUM (acpi->HID)) {
1945403ae84SToomas Soome case 0x0a03:
1955403ae84SToomas Soome if (asprintf(&name, "PciRoot(%x)%s",
1965403ae84SToomas Soome acpi->UID, tail) < 0)
1975403ae84SToomas Soome name = NULL;
1985403ae84SToomas Soome break;
1995403ae84SToomas Soome case 0x0a08:
2005403ae84SToomas Soome if (asprintf(&name, "PcieRoot(%x)%s",
2015403ae84SToomas Soome acpi->UID, tail) < 0)
2025403ae84SToomas Soome name = NULL;
2035403ae84SToomas Soome break;
2045403ae84SToomas Soome case 0x0604:
2055403ae84SToomas Soome if (asprintf(&name, "Floppy(%x)%s",
2065403ae84SToomas Soome acpi->UID, tail) < 0)
2075403ae84SToomas Soome name = NULL;
2085403ae84SToomas Soome break;
2095403ae84SToomas Soome case 0x0301:
2105403ae84SToomas Soome if (asprintf(&name, "Keyboard(%x)%s",
2115403ae84SToomas Soome acpi->UID, tail) < 0)
2125403ae84SToomas Soome name = NULL;
2135403ae84SToomas Soome break;
2145403ae84SToomas Soome case 0x0501:
2155403ae84SToomas Soome if (asprintf(&name, "Serial(%x)%s",
2165403ae84SToomas Soome acpi->UID, tail) < 0)
2175403ae84SToomas Soome name = NULL;
2185403ae84SToomas Soome break;
2195403ae84SToomas Soome case 0x0401:
2205403ae84SToomas Soome if (asprintf(&name, "ParallelPort(%x)%s",
2215403ae84SToomas Soome acpi->UID, tail) < 0)
2225403ae84SToomas Soome name = NULL;
2235403ae84SToomas Soome break;
2245403ae84SToomas Soome default:
2255403ae84SToomas Soome if (asprintf(&name, "Acpi(PNP%04x,%x)%s",
2265403ae84SToomas Soome EISA_ID_TO_NUM(acpi->HID),
2275403ae84SToomas Soome acpi->UID, tail) < 0)
2285403ae84SToomas Soome name = NULL;
2295403ae84SToomas Soome break;
2305403ae84SToomas Soome }
2315403ae84SToomas Soome } else {
2325403ae84SToomas Soome if (asprintf(&name, "Acpi(%08x,%x)%s",
2335403ae84SToomas Soome acpi->HID, acpi->UID, tail) < 0)
2345403ae84SToomas Soome name = NULL;
2355403ae84SToomas Soome }
2365403ae84SToomas Soome break;
2375403ae84SToomas Soome case ACPI_EXTENDED_DP:
2385403ae84SToomas Soome default:
2395403ae84SToomas Soome if (asprintf(&name, "UnknownACPI(%x)%s", subtype, tail) < 0)
2405403ae84SToomas Soome name = NULL;
2415403ae84SToomas Soome break;
2425403ae84SToomas Soome }
2435403ae84SToomas Soome free(tail);
2445403ae84SToomas Soome return (name);
2455403ae84SToomas Soome }
2465403ae84SToomas Soome
2475403ae84SToomas Soome static char *
efi_messaging_dev_path(EFI_DEVICE_PATH * node,char * suffix)2485403ae84SToomas Soome efi_messaging_dev_path(EFI_DEVICE_PATH *node, char *suffix)
2495403ae84SToomas Soome {
2505403ae84SToomas Soome uint8_t subtype = DevicePathSubType(node);
2515403ae84SToomas Soome char *name;
2525403ae84SToomas Soome char *tail;
2535403ae84SToomas Soome
2545403ae84SToomas Soome tail = efi_make_tail(suffix);
2555403ae84SToomas Soome switch (subtype) {
2565403ae84SToomas Soome case MSG_ATAPI_DP:
2575403ae84SToomas Soome if (asprintf(&name, "ATA(%s,%s,%x)%s",
2585403ae84SToomas Soome ((ATAPI_DEVICE_PATH *)node)->PrimarySecondary == 1 ?
2595403ae84SToomas Soome "Secondary" : "Primary",
2605403ae84SToomas Soome ((ATAPI_DEVICE_PATH *)node)->SlaveMaster == 1 ?
2615403ae84SToomas Soome "Slave" : "Master",
2625403ae84SToomas Soome ((ATAPI_DEVICE_PATH *)node)->Lun, tail) < 0)
2635403ae84SToomas Soome name = NULL;
2645403ae84SToomas Soome break;
2655403ae84SToomas Soome case MSG_SCSI_DP:
2665403ae84SToomas Soome if (asprintf(&name, "SCSI(%x,%x)%s",
2675403ae84SToomas Soome ((SCSI_DEVICE_PATH *)node)->Pun,
2685403ae84SToomas Soome ((SCSI_DEVICE_PATH *)node)->Lun, tail) < 0)
2695403ae84SToomas Soome name = NULL;
2705403ae84SToomas Soome break;
2715403ae84SToomas Soome case MSG_FIBRECHANNEL_DP:
2725403ae84SToomas Soome if (asprintf(&name, "Fibre(%" PRIx64 ",%" PRIx64 ")%s",
2735403ae84SToomas Soome ((FIBRECHANNEL_DEVICE_PATH *)node)->WWN,
2745403ae84SToomas Soome ((FIBRECHANNEL_DEVICE_PATH *)node)->Lun, tail) < 0)
2755403ae84SToomas Soome name = NULL;
2765403ae84SToomas Soome break;
2775403ae84SToomas Soome case MSG_1394_DP:
2785403ae84SToomas Soome if (asprintf(&name, "I1394(%016" PRIx64 ")%s",
2795403ae84SToomas Soome ((F1394_DEVICE_PATH *)node)->Guid, tail) < 0)
2805403ae84SToomas Soome name = NULL;
2815403ae84SToomas Soome break;
2825403ae84SToomas Soome case MSG_USB_DP:
2835403ae84SToomas Soome if (asprintf(&name, "USB(%x,%x)%s",
2845403ae84SToomas Soome ((USB_DEVICE_PATH *)node)->ParentPortNumber,
2855403ae84SToomas Soome ((USB_DEVICE_PATH *)node)->InterfaceNumber, tail) < 0)
2865403ae84SToomas Soome name = NULL;
2875403ae84SToomas Soome break;
2885403ae84SToomas Soome case MSG_USB_CLASS_DP:
2895403ae84SToomas Soome if (asprintf(&name, "UsbClass(%x,%x,%x,%x,%x)%s",
2905403ae84SToomas Soome ((USB_CLASS_DEVICE_PATH *)node)->VendorId,
2915403ae84SToomas Soome ((USB_CLASS_DEVICE_PATH *)node)->ProductId,
2925403ae84SToomas Soome ((USB_CLASS_DEVICE_PATH *)node)->DeviceClass,
2935403ae84SToomas Soome ((USB_CLASS_DEVICE_PATH *)node)->DeviceSubClass,
2945403ae84SToomas Soome ((USB_CLASS_DEVICE_PATH *)node)->DeviceProtocol, tail) < 0)
2955403ae84SToomas Soome name = NULL;
2965403ae84SToomas Soome break;
2975403ae84SToomas Soome case MSG_MAC_ADDR_DP:
2985403ae84SToomas Soome if (asprintf(&name, "MAC(%02x:%02x:%02x:%02x:%02x:%02x,%x)%s",
2995403ae84SToomas Soome ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[0],
3005403ae84SToomas Soome ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[1],
3015403ae84SToomas Soome ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[2],
3025403ae84SToomas Soome ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[3],
3035403ae84SToomas Soome ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[4],
3045403ae84SToomas Soome ((MAC_ADDR_DEVICE_PATH *)node)->MacAddress.Addr[5],
3055403ae84SToomas Soome ((MAC_ADDR_DEVICE_PATH *)node)->IfType, tail) < 0)
3065403ae84SToomas Soome name = NULL;
3075403ae84SToomas Soome break;
3085403ae84SToomas Soome case MSG_VENDOR_DP:
3095403ae84SToomas Soome name = efi_vendor_path("Messaging",
3105403ae84SToomas Soome (VENDOR_DEVICE_PATH *)node, tail);
3115403ae84SToomas Soome break;
3125403ae84SToomas Soome case MSG_UART_DP:
3135403ae84SToomas Soome if (asprintf(&name, "UART(%" PRIu64 ",%u,%x,%x)%s",
3145403ae84SToomas Soome ((UART_DEVICE_PATH *)node)->BaudRate,
3155403ae84SToomas Soome ((UART_DEVICE_PATH *)node)->DataBits,
3165403ae84SToomas Soome ((UART_DEVICE_PATH *)node)->Parity,
3175403ae84SToomas Soome ((UART_DEVICE_PATH *)node)->StopBits, tail) < 0)
3185403ae84SToomas Soome name = NULL;
3195403ae84SToomas Soome break;
3205403ae84SToomas Soome case MSG_SATA_DP:
3215403ae84SToomas Soome if (asprintf(&name, "Sata(%x,%x,%x)%s",
3225403ae84SToomas Soome ((SATA_DEVICE_PATH *)node)->HBAPortNumber,
3235403ae84SToomas Soome ((SATA_DEVICE_PATH *)node)->PortMultiplierPortNumber,
3245403ae84SToomas Soome ((SATA_DEVICE_PATH *)node)->Lun, tail) < 0)
3255403ae84SToomas Soome name = NULL;
3265403ae84SToomas Soome break;
3275403ae84SToomas Soome default:
3285403ae84SToomas Soome if (asprintf(&name, "UnknownMessaging(%x)%s",
3295403ae84SToomas Soome subtype, tail) < 0)
3305403ae84SToomas Soome name = NULL;
3315403ae84SToomas Soome break;
3325403ae84SToomas Soome }
3335403ae84SToomas Soome free(tail);
3345403ae84SToomas Soome return (name);
3355403ae84SToomas Soome }
3365403ae84SToomas Soome
3375403ae84SToomas Soome static char *
efi_media_dev_path(EFI_DEVICE_PATH * node,char * suffix)3385403ae84SToomas Soome efi_media_dev_path(EFI_DEVICE_PATH *node, char *suffix)
3395403ae84SToomas Soome {
3405403ae84SToomas Soome uint8_t subtype = DevicePathSubType(node);
3415403ae84SToomas Soome HARDDRIVE_DEVICE_PATH *hd;
3425403ae84SToomas Soome char *name;
3435403ae84SToomas Soome char *str;
3445403ae84SToomas Soome char *tail;
3455403ae84SToomas Soome int rv;
3465403ae84SToomas Soome
3475403ae84SToomas Soome tail = efi_make_tail(suffix);
3485403ae84SToomas Soome name = NULL;
3495403ae84SToomas Soome switch (subtype) {
3505403ae84SToomas Soome case MEDIA_HARDDRIVE_DP:
3515403ae84SToomas Soome hd = (HARDDRIVE_DEVICE_PATH *)node;
3525403ae84SToomas Soome switch (hd->SignatureType) {
3535403ae84SToomas Soome case SIGNATURE_TYPE_MBR:
3545403ae84SToomas Soome if (asprintf(&name, "HD(%d,MBR,%08x,%" PRIx64
3555403ae84SToomas Soome ",%" PRIx64 ")%s",
3565403ae84SToomas Soome hd->PartitionNumber,
3575403ae84SToomas Soome *((uint32_t *)(uintptr_t)&hd->Signature[0]),
3585403ae84SToomas Soome hd->PartitionStart,
3595403ae84SToomas Soome hd->PartitionSize, tail) < 0)
3605403ae84SToomas Soome name = NULL;
3615403ae84SToomas Soome break;
3625403ae84SToomas Soome case SIGNATURE_TYPE_GUID:
3635403ae84SToomas Soome name = NULL;
3645403ae84SToomas Soome uuid_to_string((const uuid_t *)(void *)
3655403ae84SToomas Soome &hd->Signature[0], &str, &rv);
3665403ae84SToomas Soome if (rv != uuid_s_ok)
3675403ae84SToomas Soome break;
3685403ae84SToomas Soome rv = asprintf(&name, "HD(%d,GPT,%s,%" PRIx64 ",%"
3695403ae84SToomas Soome PRIx64 ")%s",
3705403ae84SToomas Soome hd->PartitionNumber, str,
3715403ae84SToomas Soome hd->PartitionStart, hd->PartitionSize, tail);
3725403ae84SToomas Soome free(str);
3735403ae84SToomas Soome break;
3745403ae84SToomas Soome default:
3755403ae84SToomas Soome if (asprintf(&name, "HD(%d,%d,0)%s",
3765403ae84SToomas Soome hd->PartitionNumber,
3775403ae84SToomas Soome hd->SignatureType, tail) < 0) {
3785403ae84SToomas Soome name = NULL;
3795403ae84SToomas Soome }
3805403ae84SToomas Soome break;
3815403ae84SToomas Soome }
3825403ae84SToomas Soome break;
3835403ae84SToomas Soome case MEDIA_CDROM_DP:
3845403ae84SToomas Soome if (asprintf(&name, "CD(%x,%" PRIx64 ",%" PRIx64 ")%s",
3855403ae84SToomas Soome ((CDROM_DEVICE_PATH *)node)->BootEntry,
3865403ae84SToomas Soome ((CDROM_DEVICE_PATH *)node)->PartitionStart,
3875403ae84SToomas Soome ((CDROM_DEVICE_PATH *)node)->PartitionSize, tail) < 0) {
3885403ae84SToomas Soome name = NULL;
3895403ae84SToomas Soome }
3905403ae84SToomas Soome break;
3915403ae84SToomas Soome case MEDIA_VENDOR_DP:
3925403ae84SToomas Soome name = efi_vendor_path("Media",
3935403ae84SToomas Soome (VENDOR_DEVICE_PATH *)node, tail);
3945403ae84SToomas Soome break;
3955403ae84SToomas Soome case MEDIA_FILEPATH_DP:
3965403ae84SToomas Soome name = NULL;
3975403ae84SToomas Soome str = NULL;
3985403ae84SToomas Soome if (ucs2_to_utf8(((FILEPATH_DEVICE_PATH *)node)->PathName,
3995403ae84SToomas Soome &str) == 0) {
4005403ae84SToomas Soome (void)asprintf(&name, "%s%s", str, tail);
4015403ae84SToomas Soome free(str);
4025403ae84SToomas Soome }
4035403ae84SToomas Soome break;
4045403ae84SToomas Soome case MEDIA_PROTOCOL_DP:
4055403ae84SToomas Soome name = NULL;
4065403ae84SToomas Soome uuid_to_string((const uuid_t *)(void *)
4075403ae84SToomas Soome &((MEDIA_PROTOCOL_DEVICE_PATH *)node)->Protocol,
4085403ae84SToomas Soome &str, &rv);
4095403ae84SToomas Soome if (rv != uuid_s_ok)
4105403ae84SToomas Soome break;
4115403ae84SToomas Soome rv = asprintf(&name, "Protocol(%s)%s", str, tail);
4125403ae84SToomas Soome free(str);
4135403ae84SToomas Soome break;
4145403ae84SToomas Soome default:
4155403ae84SToomas Soome if (asprintf(&name, "UnknownMedia(%x)%s",
4165403ae84SToomas Soome subtype, tail) < 0)
4175403ae84SToomas Soome name = NULL;
4185403ae84SToomas Soome }
4195403ae84SToomas Soome free(tail);
4205403ae84SToomas Soome return (name);
4215403ae84SToomas Soome }
4225403ae84SToomas Soome
4235403ae84SToomas Soome static char *
efi_translate_devpath(EFI_DEVICE_PATH * devpath)4245403ae84SToomas Soome efi_translate_devpath(EFI_DEVICE_PATH *devpath)
4255403ae84SToomas Soome {
4265403ae84SToomas Soome EFI_DEVICE_PATH *dp = NextDevicePathNode(devpath);
4275403ae84SToomas Soome char *name, *ptr;
4285403ae84SToomas Soome uint8_t type;
4295403ae84SToomas Soome
4305403ae84SToomas Soome if (!IsDevicePathEnd(devpath))
4315403ae84SToomas Soome name = efi_translate_devpath(dp);
4325403ae84SToomas Soome else
4335403ae84SToomas Soome return (NULL);
4345403ae84SToomas Soome
4355403ae84SToomas Soome ptr = NULL;
4365403ae84SToomas Soome type = DevicePathType(devpath);
4375403ae84SToomas Soome switch (type) {
4385403ae84SToomas Soome case HARDWARE_DEVICE_PATH:
4395403ae84SToomas Soome ptr = efi_hw_dev_path(devpath, name);
4405403ae84SToomas Soome break;
4415403ae84SToomas Soome case ACPI_DEVICE_PATH:
4425403ae84SToomas Soome ptr = efi_acpi_dev_path(devpath, name);
4435403ae84SToomas Soome break;
4445403ae84SToomas Soome case MESSAGING_DEVICE_PATH:
4455403ae84SToomas Soome ptr = efi_messaging_dev_path(devpath, name);
4465403ae84SToomas Soome break;
4475403ae84SToomas Soome case MEDIA_DEVICE_PATH:
4485403ae84SToomas Soome ptr = efi_media_dev_path(devpath, name);
4495403ae84SToomas Soome break;
4505403ae84SToomas Soome case BBS_DEVICE_PATH:
4515403ae84SToomas Soome default:
4525403ae84SToomas Soome if (asprintf(&ptr, "UnknownPath(%x)%s", type,
4535403ae84SToomas Soome name? name : "") < 0)
4545403ae84SToomas Soome ptr = NULL;
4555403ae84SToomas Soome break;
4565403ae84SToomas Soome }
4575403ae84SToomas Soome
4585403ae84SToomas Soome if (ptr != NULL) {
4595403ae84SToomas Soome free(name);
4605403ae84SToomas Soome name = ptr;
4615403ae84SToomas Soome }
4625403ae84SToomas Soome return (name);
4635403ae84SToomas Soome }
4645403ae84SToomas Soome
4655403ae84SToomas Soome static CHAR16 *
efi_devpath_to_name(EFI_DEVICE_PATH * devpath)4665403ae84SToomas Soome efi_devpath_to_name(EFI_DEVICE_PATH *devpath)
4675403ae84SToomas Soome {
4685403ae84SToomas Soome char *name = NULL;
4695403ae84SToomas Soome CHAR16 *ptr = NULL;
4705403ae84SToomas Soome size_t len;
4715403ae84SToomas Soome int rv;
4725403ae84SToomas Soome
4735403ae84SToomas Soome name = efi_translate_devpath(devpath);
4745403ae84SToomas Soome if (name == NULL)
4755403ae84SToomas Soome return (NULL);
4765403ae84SToomas Soome
4775403ae84SToomas Soome /*
4785403ae84SToomas Soome * We need to return memory from AllocatePool, so it can be freed
4795403ae84SToomas Soome * with FreePool() in efi_free_devpath_name().
4805403ae84SToomas Soome */
4815403ae84SToomas Soome rv = utf8_to_ucs2(name, &ptr, &len);
4825403ae84SToomas Soome free(name);
4835403ae84SToomas Soome if (rv == 0) {
4845403ae84SToomas Soome CHAR16 *out = NULL;
4855403ae84SToomas Soome EFI_STATUS status;
4865403ae84SToomas Soome
4875403ae84SToomas Soome status = BS->AllocatePool(EfiLoaderData, len, (void **)&out);
4885403ae84SToomas Soome if (EFI_ERROR(status)) {
4895403ae84SToomas Soome free(ptr);
4905403ae84SToomas Soome return (out);
4915403ae84SToomas Soome }
4925403ae84SToomas Soome memcpy(out, ptr, len);
4935403ae84SToomas Soome free(ptr);
4945403ae84SToomas Soome ptr = out;
4955403ae84SToomas Soome }
4965403ae84SToomas Soome
4975403ae84SToomas Soome return (ptr);
4985403ae84SToomas Soome }
4995403ae84SToomas Soome
5003e5a19beSJohn Baldwin CHAR16 *
efi_devpath_name(EFI_DEVICE_PATH * devpath)5013e5a19beSJohn Baldwin efi_devpath_name(EFI_DEVICE_PATH *devpath)
5023e5a19beSJohn Baldwin {
5033e5a19beSJohn Baldwin EFI_STATUS status;
5043e5a19beSJohn Baldwin
5053e5a19beSJohn Baldwin if (devpath == NULL)
5063e5a19beSJohn Baldwin return (NULL);
50716b07b25SWarner Losh if (toTextProtocol == NULL) {
5083e5a19beSJohn Baldwin status = BS->LocateProtocol(&DevicePathToTextGUID, NULL,
50916b07b25SWarner Losh (VOID **)&toTextProtocol);
5103e5a19beSJohn Baldwin if (EFI_ERROR(status))
51116b07b25SWarner Losh toTextProtocol = NULL;
5123e5a19beSJohn Baldwin }
51316b07b25SWarner Losh if (toTextProtocol == NULL)
5145403ae84SToomas Soome return (efi_devpath_to_name(devpath));
5153e5a19beSJohn Baldwin
51616b07b25SWarner Losh return (toTextProtocol->ConvertDevicePathToText(devpath, TRUE, TRUE));
5173e5a19beSJohn Baldwin }
5183e5a19beSJohn Baldwin
5193e5a19beSJohn Baldwin void
efi_free_devpath_name(CHAR16 * text)5203e5a19beSJohn Baldwin efi_free_devpath_name(CHAR16 *text)
5213e5a19beSJohn Baldwin {
5225403ae84SToomas Soome if (text != NULL)
5233e5a19beSJohn Baldwin BS->FreePool(text);
5243e5a19beSJohn Baldwin }
5253e5a19beSJohn Baldwin
5263e5a19beSJohn Baldwin EFI_DEVICE_PATH *
efi_name_to_devpath(const char * path)52716b07b25SWarner Losh efi_name_to_devpath(const char *path)
52816b07b25SWarner Losh {
52916b07b25SWarner Losh EFI_DEVICE_PATH *devpath;
53016b07b25SWarner Losh CHAR16 *uv;
53116b07b25SWarner Losh size_t ul;
53216b07b25SWarner Losh
53316b07b25SWarner Losh uv = NULL;
53416b07b25SWarner Losh if (utf8_to_ucs2(path, &uv, &ul) != 0)
53516b07b25SWarner Losh return (NULL);
53616b07b25SWarner Losh devpath = efi_name_to_devpath16(uv);
53716b07b25SWarner Losh free(uv);
53816b07b25SWarner Losh return (devpath);
53916b07b25SWarner Losh }
54016b07b25SWarner Losh
54116b07b25SWarner Losh EFI_DEVICE_PATH *
efi_name_to_devpath16(CHAR16 * path)54216b07b25SWarner Losh efi_name_to_devpath16(CHAR16 *path)
54316b07b25SWarner Losh {
54416b07b25SWarner Losh EFI_STATUS status;
54516b07b25SWarner Losh
54616b07b25SWarner Losh if (path == NULL)
54716b07b25SWarner Losh return (NULL);
54816b07b25SWarner Losh if (fromTextProtocol == NULL) {
54916b07b25SWarner Losh status = BS->LocateProtocol(&DevicePathFromTextGUID, NULL,
55016b07b25SWarner Losh (VOID **)&fromTextProtocol);
55116b07b25SWarner Losh if (EFI_ERROR(status))
55216b07b25SWarner Losh fromTextProtocol = NULL;
55316b07b25SWarner Losh }
55416b07b25SWarner Losh if (fromTextProtocol == NULL)
55516b07b25SWarner Losh return (NULL);
55616b07b25SWarner Losh
55716b07b25SWarner Losh return (fromTextProtocol->ConvertTextToDevicePath(path));
55816b07b25SWarner Losh }
55916b07b25SWarner Losh
efi_devpath_free(EFI_DEVICE_PATH * devpath)56016b07b25SWarner Losh void efi_devpath_free(EFI_DEVICE_PATH *devpath)
56116b07b25SWarner Losh {
56216b07b25SWarner Losh
56316b07b25SWarner Losh BS->FreePool(devpath);
56416b07b25SWarner Losh }
56516b07b25SWarner Losh
56616b07b25SWarner Losh EFI_DEVICE_PATH *
efi_devpath_last_node(EFI_DEVICE_PATH * devpath)5673e5a19beSJohn Baldwin efi_devpath_last_node(EFI_DEVICE_PATH *devpath)
5683e5a19beSJohn Baldwin {
5693e5a19beSJohn Baldwin
5703e5a19beSJohn Baldwin if (IsDevicePathEnd(devpath))
5713e5a19beSJohn Baldwin return (NULL);
5723e5a19beSJohn Baldwin while (!IsDevicePathEnd(NextDevicePathNode(devpath)))
5733e5a19beSJohn Baldwin devpath = NextDevicePathNode(devpath);
5743e5a19beSJohn Baldwin return (devpath);
5753e5a19beSJohn Baldwin }
5763e5a19beSJohn Baldwin
577f2b3bf5cSToomas Soome /*
578f2b3bf5cSToomas Soome * Walk device path nodes, return next instance or end node.
579f2b3bf5cSToomas Soome */
580f2b3bf5cSToomas Soome EFI_DEVICE_PATH *
efi_devpath_next_instance(EFI_DEVICE_PATH * devpath)581f2b3bf5cSToomas Soome efi_devpath_next_instance(EFI_DEVICE_PATH *devpath)
582f2b3bf5cSToomas Soome {
583f2b3bf5cSToomas Soome while (!IsDevicePathEnd(devpath)) {
584f2b3bf5cSToomas Soome devpath = NextDevicePathNode(devpath);
585f2b3bf5cSToomas Soome if (IsDevicePathEndType(devpath) &&
586f2b3bf5cSToomas Soome devpath->SubType == END_INSTANCE_DEVICE_PATH_SUBTYPE) {
587f2b3bf5cSToomas Soome devpath = NextDevicePathNode(devpath);
588f2b3bf5cSToomas Soome break;
589f2b3bf5cSToomas Soome }
590f2b3bf5cSToomas Soome }
591f2b3bf5cSToomas Soome return (devpath);
592f2b3bf5cSToomas Soome }
593f2b3bf5cSToomas Soome
5943e5a19beSJohn Baldwin EFI_DEVICE_PATH *
efi_devpath_trim(EFI_DEVICE_PATH * devpath)5953e5a19beSJohn Baldwin efi_devpath_trim(EFI_DEVICE_PATH *devpath)
5963e5a19beSJohn Baldwin {
5973e5a19beSJohn Baldwin EFI_DEVICE_PATH *node, *copy;
5983e5a19beSJohn Baldwin size_t prefix, len;
5993e5a19beSJohn Baldwin
6009c1cd3f5SToomas Soome if ((node = efi_devpath_last_node(devpath)) == NULL)
6019c1cd3f5SToomas Soome return (NULL);
6023e5a19beSJohn Baldwin prefix = (UINT8 *)node - (UINT8 *)devpath;
6033e5a19beSJohn Baldwin if (prefix == 0)
6043e5a19beSJohn Baldwin return (NULL);
6053e5a19beSJohn Baldwin len = prefix + DevicePathNodeLength(NextDevicePathNode(node));
6063e5a19beSJohn Baldwin copy = malloc(len);
6079c1cd3f5SToomas Soome if (copy != NULL) {
6083e5a19beSJohn Baldwin memcpy(copy, devpath, prefix);
6093e5a19beSJohn Baldwin node = (EFI_DEVICE_PATH *)((UINT8 *)copy + prefix);
6103e5a19beSJohn Baldwin SetDevicePathEndNode(node);
6119c1cd3f5SToomas Soome }
6123e5a19beSJohn Baldwin return (copy);
6133e5a19beSJohn Baldwin }
6143e5a19beSJohn Baldwin
6153e5a19beSJohn Baldwin EFI_HANDLE
efi_devpath_handle(EFI_DEVICE_PATH * devpath)6163e5a19beSJohn Baldwin efi_devpath_handle(EFI_DEVICE_PATH *devpath)
6173e5a19beSJohn Baldwin {
6183e5a19beSJohn Baldwin EFI_STATUS status;
6193e5a19beSJohn Baldwin EFI_HANDLE h;
6203e5a19beSJohn Baldwin
6213e5a19beSJohn Baldwin /*
6223e5a19beSJohn Baldwin * There isn't a standard way to locate a handle for a given
6233e5a19beSJohn Baldwin * device path. However, querying the EFI_DEVICE_PATH protocol
6243e5a19beSJohn Baldwin * for a given device path should give us a handle for the
6253e5a19beSJohn Baldwin * closest node in the path to the end that is valid.
6263e5a19beSJohn Baldwin */
6273e5a19beSJohn Baldwin status = BS->LocateDevicePath(&DevicePathGUID, &devpath, &h);
6283e5a19beSJohn Baldwin if (EFI_ERROR(status))
6293e5a19beSJohn Baldwin return (NULL);
6303e5a19beSJohn Baldwin return (h);
6313e5a19beSJohn Baldwin }
632467c82cbSToomas Soome
63359fcc285SToomas Soome bool
efi_devpath_match_node(EFI_DEVICE_PATH * devpath1,EFI_DEVICE_PATH * devpath2)63413850b36SWarner Losh efi_devpath_match_node(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
635467c82cbSToomas Soome {
636cbc1b3deSToomas Soome size_t len;
637467c82cbSToomas Soome
638467c82cbSToomas Soome if (devpath1 == NULL || devpath2 == NULL)
63959fcc285SToomas Soome return (false);
640467c82cbSToomas Soome if (DevicePathType(devpath1) != DevicePathType(devpath2) ||
641467c82cbSToomas Soome DevicePathSubType(devpath1) != DevicePathSubType(devpath2))
64259fcc285SToomas Soome return (false);
643467c82cbSToomas Soome len = DevicePathNodeLength(devpath1);
644467c82cbSToomas Soome if (len != DevicePathNodeLength(devpath2))
64559fcc285SToomas Soome return (false);
646cbc1b3deSToomas Soome if (memcmp(devpath1, devpath2, len) != 0)
64759fcc285SToomas Soome return (false);
64813850b36SWarner Losh return (true);
64913850b36SWarner Losh }
650467c82cbSToomas Soome
65183ffeb8bSWarner Losh static bool
_efi_devpath_match(EFI_DEVICE_PATH * devpath1,EFI_DEVICE_PATH * devpath2,bool ignore_media)65283ffeb8bSWarner Losh _efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2,
65383ffeb8bSWarner Losh bool ignore_media)
65413850b36SWarner Losh {
65513850b36SWarner Losh
65613850b36SWarner Losh if (devpath1 == NULL || devpath2 == NULL)
65713850b36SWarner Losh return (false);
65813850b36SWarner Losh
65913850b36SWarner Losh while (true) {
66083ffeb8bSWarner Losh if (ignore_media &&
66183ffeb8bSWarner Losh IsDevicePathType(devpath1, MEDIA_DEVICE_PATH) &&
66283ffeb8bSWarner Losh IsDevicePathType(devpath2, MEDIA_DEVICE_PATH))
66383ffeb8bSWarner Losh return (true);
66413850b36SWarner Losh if (!efi_devpath_match_node(devpath1, devpath2))
66513850b36SWarner Losh return false;
666467c82cbSToomas Soome if (IsDevicePathEnd(devpath1))
667467c82cbSToomas Soome break;
668467c82cbSToomas Soome devpath1 = NextDevicePathNode(devpath1);
669467c82cbSToomas Soome devpath2 = NextDevicePathNode(devpath2);
670467c82cbSToomas Soome }
67159fcc285SToomas Soome return (true);
672467c82cbSToomas Soome }
67383ffeb8bSWarner Losh /*
67483ffeb8bSWarner Losh * Are two devpaths identical?
67583ffeb8bSWarner Losh */
67683ffeb8bSWarner Losh bool
efi_devpath_match(EFI_DEVICE_PATH * devpath1,EFI_DEVICE_PATH * devpath2)67783ffeb8bSWarner Losh efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
67883ffeb8bSWarner Losh {
67983ffeb8bSWarner Losh return _efi_devpath_match(devpath1, devpath2, false);
68083ffeb8bSWarner Losh }
68183ffeb8bSWarner Losh
68283ffeb8bSWarner Losh /*
68383ffeb8bSWarner Losh * Like efi_devpath_match, but stops at when we hit the media device
68483ffeb8bSWarner Losh * path node that specifies the partition information. If we match
68583ffeb8bSWarner Losh * up to that point, then we're on the same disk.
68683ffeb8bSWarner Losh */
68783ffeb8bSWarner Losh bool
efi_devpath_same_disk(EFI_DEVICE_PATH * devpath1,EFI_DEVICE_PATH * devpath2)68883ffeb8bSWarner Losh efi_devpath_same_disk(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
68983ffeb8bSWarner Losh {
69083ffeb8bSWarner Losh return _efi_devpath_match(devpath1, devpath2, true);
69183ffeb8bSWarner Losh }
6922e1b4c6fSWarner Losh
693694dab3eSToomas Soome bool
efi_devpath_is_prefix(EFI_DEVICE_PATH * prefix,EFI_DEVICE_PATH * path)6942e1b4c6fSWarner Losh efi_devpath_is_prefix(EFI_DEVICE_PATH *prefix, EFI_DEVICE_PATH *path)
6952e1b4c6fSWarner Losh {
696694dab3eSToomas Soome size_t len;
6972e1b4c6fSWarner Losh
6982e1b4c6fSWarner Losh if (prefix == NULL || path == NULL)
699694dab3eSToomas Soome return (false);
7002e1b4c6fSWarner Losh
7012e1b4c6fSWarner Losh while (1) {
7022e1b4c6fSWarner Losh if (IsDevicePathEnd(prefix))
7032e1b4c6fSWarner Losh break;
7042e1b4c6fSWarner Losh
7052e1b4c6fSWarner Losh if (DevicePathType(prefix) != DevicePathType(path) ||
7062e1b4c6fSWarner Losh DevicePathSubType(prefix) != DevicePathSubType(path))
707694dab3eSToomas Soome return (false);
7082e1b4c6fSWarner Losh
7092e1b4c6fSWarner Losh len = DevicePathNodeLength(prefix);
7102e1b4c6fSWarner Losh if (len != DevicePathNodeLength(path))
711694dab3eSToomas Soome return (false);
7122e1b4c6fSWarner Losh
713694dab3eSToomas Soome if (memcmp(prefix, path, len) != 0)
714694dab3eSToomas Soome return (false);
7152e1b4c6fSWarner Losh
7162e1b4c6fSWarner Losh prefix = NextDevicePathNode(prefix);
7172e1b4c6fSWarner Losh path = NextDevicePathNode(path);
7182e1b4c6fSWarner Losh }
719694dab3eSToomas Soome return (true);
7202e1b4c6fSWarner Losh }
721ee4e1d58SWarner Losh
722ee4e1d58SWarner Losh /*
723ee4e1d58SWarner Losh * Skip over the 'prefix' part of path and return the part of the path
724ee4e1d58SWarner Losh * that starts with the first node that's a MEDIA_DEVICE_PATH.
725ee4e1d58SWarner Losh */
726ee4e1d58SWarner Losh EFI_DEVICE_PATH *
efi_devpath_to_media_path(EFI_DEVICE_PATH * path)727ee4e1d58SWarner Losh efi_devpath_to_media_path(EFI_DEVICE_PATH *path)
728ee4e1d58SWarner Losh {
729ee4e1d58SWarner Losh
730ee4e1d58SWarner Losh while (!IsDevicePathEnd(path)) {
731ee4e1d58SWarner Losh if (DevicePathType(path) == MEDIA_DEVICE_PATH)
732ee4e1d58SWarner Losh return (path);
733ee4e1d58SWarner Losh path = NextDevicePathNode(path);
734ee4e1d58SWarner Losh }
735ee4e1d58SWarner Losh return (NULL);
736ee4e1d58SWarner Losh }
737c6c2a73cSWarner Losh
738c6c2a73cSWarner Losh UINTN
efi_devpath_length(EFI_DEVICE_PATH * path)739c6c2a73cSWarner Losh efi_devpath_length(EFI_DEVICE_PATH *path)
740c6c2a73cSWarner Losh {
741c6c2a73cSWarner Losh EFI_DEVICE_PATH *start = path;
742c6c2a73cSWarner Losh
743c6c2a73cSWarner Losh while (!IsDevicePathEnd(path))
744c6c2a73cSWarner Losh path = NextDevicePathNode(path);
745c6c2a73cSWarner Losh return ((UINTN)path - (UINTN)start) + DevicePathNodeLength(path);
746c6c2a73cSWarner Losh }
747b9e19b07SWarner Losh
748b9e19b07SWarner Losh EFI_HANDLE
efi_devpath_to_handle(EFI_DEVICE_PATH * path,EFI_HANDLE * handles,unsigned nhandles)749b9e19b07SWarner Losh efi_devpath_to_handle(EFI_DEVICE_PATH *path, EFI_HANDLE *handles, unsigned nhandles)
750b9e19b07SWarner Losh {
751b9e19b07SWarner Losh unsigned i;
752b9e19b07SWarner Losh EFI_DEVICE_PATH *media, *devpath;
753b9e19b07SWarner Losh EFI_HANDLE h;
754b9e19b07SWarner Losh
755b9e19b07SWarner Losh media = efi_devpath_to_media_path(path);
756b9e19b07SWarner Losh if (media == NULL)
757b9e19b07SWarner Losh return (NULL);
758b9e19b07SWarner Losh for (i = 0; i < nhandles; i++) {
759b9e19b07SWarner Losh h = handles[i];
760b9e19b07SWarner Losh devpath = efi_lookup_devpath(h);
761b9e19b07SWarner Losh if (devpath == NULL)
762b9e19b07SWarner Losh continue;
763b9e19b07SWarner Losh if (!efi_devpath_match_node(media, efi_devpath_to_media_path(devpath)))
764b9e19b07SWarner Losh continue;
765b9e19b07SWarner Losh return (h);
766b9e19b07SWarner Losh }
767b9e19b07SWarner Losh return (NULL);
768b9e19b07SWarner Losh }
769