2 * EFI device path interface
4 * Copyright (c) 2017 Heinrich Schuchardt
6 * SPDX-License-Identifier: GPL-2.0+
10 #include <efi_loader.h>
12 #define MAC_OUTPUT_LEN 22
13 #define UNKNOWN_OUTPUT_LEN 23
15 const efi_guid_t efi_guid_device_path_to_text_protocol =
16 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
18 static char *dp_unknown(char *s, struct efi_device_path *dp)
20 s += sprintf(s, "/UNKNOWN(%04x,%04x)", dp->type, dp->sub_type);
24 static char *dp_hardware(char *s, struct efi_device_path *dp)
26 switch (dp->sub_type) {
27 case DEVICE_PATH_SUB_TYPE_VENDOR: {
28 struct efi_device_path_vendor *vdp =
29 (struct efi_device_path_vendor *)dp;
30 s += sprintf(s, "/VenHw(%pUl)", &vdp->guid);
34 s = dp_unknown(s, dp);
40 static char *dp_acpi(char *s, struct efi_device_path *dp)
42 switch (dp->sub_type) {
43 case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE: {
44 struct efi_device_path_acpi_path *adp =
45 (struct efi_device_path_acpi_path *)dp;
46 s += sprintf(s, "/Acpi(PNP%04x", EISA_PNP_NUM(adp->hid));
48 s += sprintf(s, ",%d", adp->uid);
53 s = dp_unknown(s, dp);
59 static char *dp_msging(char *s, struct efi_device_path *dp)
61 switch (dp->sub_type) {
62 case DEVICE_PATH_SUB_TYPE_MSG_USB: {
63 struct efi_device_path_usb *udp =
64 (struct efi_device_path_usb *)dp;
65 s += sprintf(s, "/Usb(0x%x,0x%x)", udp->parent_port_number,
69 case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
70 struct efi_device_path_mac_addr *mdp =
71 (struct efi_device_path_mac_addr *)dp;
73 if (mdp->if_type != 0 && mdp->if_type != 1)
76 s += sprintf(s, "/MAC(%02x%02x%02x%02x%02x%02x,0x%1x)",
77 mdp->mac.addr[0], mdp->mac.addr[1],
78 mdp->mac.addr[2], mdp->mac.addr[3],
79 mdp->mac.addr[4], mdp->mac.addr[5],
84 case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
85 struct efi_device_path_usb_class *ucdp =
86 (struct efi_device_path_usb_class *)dp;
88 s += sprintf(s, "/USBClass(%x,%x,%x,%x,%x)",
89 ucdp->vendor_id, ucdp->product_id,
90 ucdp->device_class, ucdp->device_subclass,
91 ucdp->device_protocol);
95 case DEVICE_PATH_SUB_TYPE_MSG_SD:
96 case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
97 const char *typename =
98 (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
100 struct efi_device_path_sd_mmc_path *sddp =
101 (struct efi_device_path_sd_mmc_path *)dp;
102 s += sprintf(s, "/%s(Slot%u)", typename, sddp->slot_number);
106 s = dp_unknown(s, dp);
112 static char *dp_media(char *s, struct efi_device_path *dp)
114 switch (dp->sub_type) {
115 case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
116 struct efi_device_path_hard_drive_path *hddp =
117 (struct efi_device_path_hard_drive_path *)dp;
118 void *sig = hddp->partition_signature;
120 switch (hddp->signature_type) {
122 s += sprintf(s, "/HD(Part%d,Sig%08x)",
123 hddp->partition_number,
127 s += sprintf(s, "/HD(Part%d,Sig%pUl)",
128 hddp->partition_number, sig);
130 s += sprintf(s, "/HD(Part%d,MBRType=%02x,SigType=%02x)",
131 hddp->partition_number, hddp->partmap_type,
132 hddp->signature_type);
137 case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
138 struct efi_device_path_cdrom_path *cddp =
139 (struct efi_device_path_cdrom_path *)dp;
140 s += sprintf(s, "/CDROM(0x%x)", cddp->boot_entry);
143 case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
144 struct efi_device_path_file_path *fp =
145 (struct efi_device_path_file_path *)dp;
146 int slen = (dp->length - sizeof(*dp)) / 2;
147 s += sprintf(s, "/%-*ls", slen, fp->str);
151 s = dp_unknown(s, dp);
157 static uint16_t *efi_convert_device_node_to_text(
158 struct efi_device_path *dp,
160 bool allow_shortcuts)
164 char buf[512]; /* this ought be be big enough for worst case */
170 case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
171 str = dp_hardware(str, dp);
173 case DEVICE_PATH_TYPE_ACPI_DEVICE:
174 str = dp_acpi(str, dp);
176 case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
177 str = dp_msging(str, dp);
179 case DEVICE_PATH_TYPE_MEDIA_DEVICE:
180 str = dp_media(str, dp);
183 str = dp_unknown(str, dp);
186 dp = efi_dp_next(dp);
192 r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, 2 * len, (void **)&out);
193 if (r != EFI_SUCCESS)
196 ascii2unicode(out, buf);
202 /* helper for debug prints.. efi_free_pool() the result. */
203 uint16_t *efi_dp_str(struct efi_device_path *dp)
205 return efi_convert_device_node_to_text(dp, true, true);
209 static uint16_t EFIAPI *efi_convert_device_node_to_text_ext(
210 struct efi_device_path *device_node,
212 bool allow_shortcuts)
216 EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
218 buffer = efi_convert_device_node_to_text(device_node, display_only,
221 EFI_EXIT(EFI_SUCCESS);
225 static uint16_t EFIAPI *efi_convert_device_path_to_text(
226 struct efi_device_path *device_path,
228 bool allow_shortcuts)
232 EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
235 * Our device paths are all of depth one. So its is sufficient to
236 * to convert the first node.
238 buffer = efi_convert_device_node_to_text(device_path, display_only,
241 EFI_EXIT(EFI_SUCCESS);
245 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
246 .convert_device_node_to_text = efi_convert_device_node_to_text_ext,
247 .convert_device_path_to_text = efi_convert_device_path_to_text,