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 #define MAX_NODE_LEN 512
16 #define MAX_PATH_LEN 1024
18 const efi_guid_t efi_guid_device_path_to_text_protocol =
19 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
21 static u16 *efi_str_to_u16(char *str)
27 len = strlen(str) + 1;
28 ret = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, len * sizeof(u16),
30 if (ret != EFI_SUCCESS)
32 ascii2unicode(out, str);
37 static char *dp_unknown(char *s, struct efi_device_path *dp)
39 s += sprintf(s, "UNKNOWN(%04x,%04x)", dp->type, dp->sub_type);
43 static char *dp_hardware(char *s, struct efi_device_path *dp)
45 switch (dp->sub_type) {
46 case DEVICE_PATH_SUB_TYPE_MEMORY: {
47 struct efi_device_path_memory *mdp =
48 (struct efi_device_path_memory *)dp;
49 s += sprintf(s, "MemoryMapped(0x%x,0x%llx,0x%llx)",
55 case DEVICE_PATH_SUB_TYPE_VENDOR: {
56 struct efi_device_path_vendor *vdp =
57 (struct efi_device_path_vendor *)dp;
58 s += sprintf(s, "VenHw(%pUl)", &vdp->guid);
62 s = dp_unknown(s, dp);
68 static char *dp_acpi(char *s, struct efi_device_path *dp)
70 switch (dp->sub_type) {
71 case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE: {
72 struct efi_device_path_acpi_path *adp =
73 (struct efi_device_path_acpi_path *)dp;
74 s += sprintf(s, "Acpi(PNP%04x", EISA_PNP_NUM(adp->hid));
76 s += sprintf(s, ",%d", adp->uid);
81 s = dp_unknown(s, dp);
87 static char *dp_msging(char *s, struct efi_device_path *dp)
89 switch (dp->sub_type) {
90 case DEVICE_PATH_SUB_TYPE_MSG_ATAPI: {
91 struct efi_device_path_atapi *ide =
92 (struct efi_device_path_atapi *)dp;
93 s += sprintf(s, "Ata(%d,%d,%d)", ide->primary_secondary,
94 ide->slave_master, ide->logical_unit_number);
97 case DEVICE_PATH_SUB_TYPE_MSG_SCSI: {
98 struct efi_device_path_scsi *ide =
99 (struct efi_device_path_scsi *)dp;
100 s += sprintf(s, "Scsi(%u,%u)", ide->target_id,
101 ide->logical_unit_number);
104 case DEVICE_PATH_SUB_TYPE_MSG_USB: {
105 struct efi_device_path_usb *udp =
106 (struct efi_device_path_usb *)dp;
107 s += sprintf(s, "USB(0x%x,0x%x)", udp->parent_port_number,
111 case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
112 struct efi_device_path_mac_addr *mdp =
113 (struct efi_device_path_mac_addr *)dp;
115 if (mdp->if_type != 0 && mdp->if_type != 1)
118 s += sprintf(s, "MAC(%02x%02x%02x%02x%02x%02x,0x%1x)",
119 mdp->mac.addr[0], mdp->mac.addr[1],
120 mdp->mac.addr[2], mdp->mac.addr[3],
121 mdp->mac.addr[4], mdp->mac.addr[5],
126 case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
127 struct efi_device_path_usb_class *ucdp =
128 (struct efi_device_path_usb_class *)dp;
130 s += sprintf(s, "USBClass(%x,%x,%x,%x,%x)",
131 ucdp->vendor_id, ucdp->product_id,
132 ucdp->device_class, ucdp->device_subclass,
133 ucdp->device_protocol);
137 case DEVICE_PATH_SUB_TYPE_MSG_SD:
138 case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
139 const char *typename =
140 (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
142 struct efi_device_path_sd_mmc_path *sddp =
143 (struct efi_device_path_sd_mmc_path *)dp;
144 s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
148 s = dp_unknown(s, dp);
155 * Convert a media device path node to text.
158 * @dp device path node
159 * @return next unused buffer address
161 static char *dp_media(char *s, struct efi_device_path *dp)
163 switch (dp->sub_type) {
164 case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
165 struct efi_device_path_hard_drive_path *hddp =
166 (struct efi_device_path_hard_drive_path *)dp;
167 void *sig = hddp->partition_signature;
171 /* Copy from packed structure to aligned memory */
172 memcpy(&start, &hddp->partition_start, sizeof(start));
173 memcpy(&end, &hddp->partition_end, sizeof(end));
175 switch (hddp->signature_type) {
179 memcpy(&signature, sig, sizeof(signature));
181 s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
182 hddp->partition_number, signature, start, end);
187 s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
188 hddp->partition_number, sig, start, end);
192 s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
193 hddp->partition_number, hddp->partmap_type,
200 case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
201 struct efi_device_path_cdrom_path *cddp =
202 (struct efi_device_path_cdrom_path *)dp;
203 s += sprintf(s, "CDROM(0x%x)", cddp->boot_entry);
206 case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
207 struct efi_device_path_file_path *fp =
208 (struct efi_device_path_file_path *)dp;
209 int slen = (dp->length - sizeof(*dp)) / 2;
210 if (slen > MAX_NODE_LEN - 2)
211 slen = MAX_NODE_LEN - 2;
212 s += sprintf(s, "%-.*ls", slen, fp->str);
216 s = dp_unknown(s, dp);
223 * Converts a single node to a char string.
225 * @buffer output buffer
226 * @dp device path or node
227 * @return end of string
229 static char *efi_convert_single_device_node_to_text(
231 struct efi_device_path *dp)
236 case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
237 str = dp_hardware(str, dp);
239 case DEVICE_PATH_TYPE_ACPI_DEVICE:
240 str = dp_acpi(str, dp);
242 case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
243 str = dp_msging(str, dp);
245 case DEVICE_PATH_TYPE_MEDIA_DEVICE:
246 str = dp_media(str, dp);
248 case DEVICE_PATH_TYPE_END:
251 str = dp_unknown(str, dp);
259 * This function implements the ConvertDeviceNodeToText service of the
260 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
261 * See the Unified Extensible Firmware Interface (UEFI) specification
264 * device_node device node to be converted
265 * display_only true if the shorter text represenation shall be used
266 * allow_shortcuts true if shortcut forms may be used
267 * @return text represenation of the device path
268 * NULL if out of memory of device_path is NULL
270 static uint16_t EFIAPI *efi_convert_device_node_to_text(
271 struct efi_device_path *device_node,
273 bool allow_shortcuts)
275 char str[MAX_NODE_LEN];
276 uint16_t *text = NULL;
278 EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
282 efi_convert_single_device_node_to_text(str, device_node);
284 text = efi_str_to_u16(str);
287 EFI_EXIT(EFI_SUCCESS);
292 * This function implements the ConvertDevicePathToText service of the
293 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
294 * See the Unified Extensible Firmware Interface (UEFI) specification
297 * device_path device path to be converted
298 * display_only true if the shorter text represenation shall be used
299 * allow_shortcuts true if shortcut forms may be used
300 * @return text represenation of the device path
301 * NULL if out of memory of device_path is NULL
303 static uint16_t EFIAPI *efi_convert_device_path_to_text(
304 struct efi_device_path *device_path,
306 bool allow_shortcuts)
308 uint16_t *text = NULL;
309 char buffer[MAX_PATH_LEN];
312 EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
316 while (device_path &&
317 str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
319 str = efi_convert_single_device_node_to_text(str, device_path);
320 device_path = efi_dp_next(device_path);
323 text = efi_str_to_u16(buffer);
326 EFI_EXIT(EFI_SUCCESS);
330 /* helper for debug prints.. efi_free_pool() the result. */
331 uint16_t *efi_dp_str(struct efi_device_path *dp)
333 return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
336 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
337 .convert_device_node_to_text = efi_convert_device_node_to_text,
338 .convert_device_path_to_text = efi_convert_device_path_to_text,