1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI device path interface
5 * Copyright (c) 2017 Heinrich Schuchardt
9 #include <efi_loader.h>
11 #define MAC_OUTPUT_LEN 22
12 #define UNKNOWN_OUTPUT_LEN 23
14 #define MAX_NODE_LEN 512
15 #define MAX_PATH_LEN 1024
17 const efi_guid_t efi_guid_device_path_to_text_protocol =
18 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
21 * efi_str_to_u16() - convert ASCII string to UTF-16
23 * A u16 buffer is allocated from pool. The ASCII string is copied to the u16
27 * Return: UTF-16 string. NULL if out of memory.
29 static u16 *efi_str_to_u16(char *str)
35 len = strlen(str) + 1;
36 ret = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, len * sizeof(u16),
38 if (ret != EFI_SUCCESS)
40 ascii2unicode(out, str);
44 static char *dp_unknown(char *s, struct efi_device_path *dp)
46 s += sprintf(s, "UNKNOWN(%04x,%04x)", dp->type, dp->sub_type);
50 static char *dp_hardware(char *s, struct efi_device_path *dp)
52 switch (dp->sub_type) {
53 case DEVICE_PATH_SUB_TYPE_MEMORY: {
54 struct efi_device_path_memory *mdp =
55 (struct efi_device_path_memory *)dp;
56 s += sprintf(s, "MemoryMapped(0x%x,0x%llx,0x%llx)",
62 case DEVICE_PATH_SUB_TYPE_VENDOR: {
64 struct efi_device_path_vendor *vdp =
65 (struct efi_device_path_vendor *)dp;
67 s += sprintf(s, "VenHw(%pUl", &vdp->guid);
68 n = (int)vdp->dp.length - sizeof(struct efi_device_path_vendor);
71 for (i = 0; i < n; ++i)
72 s += sprintf(s, "%02x", vdp->vendor_data[i]);
78 s = dp_unknown(s, dp);
84 static char *dp_acpi(char *s, struct efi_device_path *dp)
86 switch (dp->sub_type) {
87 case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE: {
88 struct efi_device_path_acpi_path *adp =
89 (struct efi_device_path_acpi_path *)dp;
91 s += sprintf(s, "Acpi(PNP%04X,%d)", EISA_PNP_NUM(adp->hid),
96 s = dp_unknown(s, dp);
102 static char *dp_msging(char *s, struct efi_device_path *dp)
104 switch (dp->sub_type) {
105 case DEVICE_PATH_SUB_TYPE_MSG_ATAPI: {
106 struct efi_device_path_atapi *ide =
107 (struct efi_device_path_atapi *)dp;
108 s += sprintf(s, "Ata(%d,%d,%d)", ide->primary_secondary,
109 ide->slave_master, ide->logical_unit_number);
112 case DEVICE_PATH_SUB_TYPE_MSG_SCSI: {
113 struct efi_device_path_scsi *ide =
114 (struct efi_device_path_scsi *)dp;
115 s += sprintf(s, "Scsi(%u,%u)", ide->target_id,
116 ide->logical_unit_number);
119 case DEVICE_PATH_SUB_TYPE_MSG_USB: {
120 struct efi_device_path_usb *udp =
121 (struct efi_device_path_usb *)dp;
122 s += sprintf(s, "USB(0x%x,0x%x)", udp->parent_port_number,
126 case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
127 int i, n = sizeof(struct efi_mac_addr);
128 struct efi_device_path_mac_addr *mdp =
129 (struct efi_device_path_mac_addr *)dp;
131 if (mdp->if_type <= 1)
133 s += sprintf(s, "MAC(");
134 for (i = 0; i < n; ++i)
135 s += sprintf(s, "%02x", mdp->mac.addr[i]);
136 s += sprintf(s, ",%u)", mdp->if_type);
140 case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
141 struct efi_device_path_usb_class *ucdp =
142 (struct efi_device_path_usb_class *)dp;
144 s += sprintf(s, "UsbClass(0x%x,0x%x,0x%x,0x%x,0x%x)",
145 ucdp->vendor_id, ucdp->product_id,
146 ucdp->device_class, ucdp->device_subclass,
147 ucdp->device_protocol);
151 case DEVICE_PATH_SUB_TYPE_MSG_SD:
152 case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
153 const char *typename =
154 (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
156 struct efi_device_path_sd_mmc_path *sddp =
157 (struct efi_device_path_sd_mmc_path *)dp;
158 s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
162 s = dp_unknown(s, dp);
169 * Convert a media device path node to text.
172 * @dp device path node
173 * @return next unused buffer address
175 static char *dp_media(char *s, struct efi_device_path *dp)
177 switch (dp->sub_type) {
178 case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
179 struct efi_device_path_hard_drive_path *hddp =
180 (struct efi_device_path_hard_drive_path *)dp;
181 void *sig = hddp->partition_signature;
185 /* Copy from packed structure to aligned memory */
186 memcpy(&start, &hddp->partition_start, sizeof(start));
187 memcpy(&end, &hddp->partition_end, sizeof(end));
189 switch (hddp->signature_type) {
193 memcpy(&signature, sig, sizeof(signature));
195 s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
196 hddp->partition_number, signature, start, end);
201 s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
202 hddp->partition_number, sig, start, end);
206 s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
207 hddp->partition_number, hddp->partmap_type,
214 case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
215 struct efi_device_path_cdrom_path *cddp =
216 (struct efi_device_path_cdrom_path *)dp;
217 s += sprintf(s, "CDROM(%u,0x%llx,0x%llx)", cddp->boot_entry,
218 cddp->partition_start, cddp->partition_size);
221 case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
222 struct efi_device_path_file_path *fp =
223 (struct efi_device_path_file_path *)dp;
224 int slen = (dp->length - sizeof(*dp)) / 2;
225 if (slen > MAX_NODE_LEN - 2)
226 slen = MAX_NODE_LEN - 2;
227 s += sprintf(s, "%-.*ls", slen, fp->str);
231 s = dp_unknown(s, dp);
238 * Converts a single node to a char string.
240 * @buffer output buffer
241 * @dp device path or node
242 * @return end of string
244 static char *efi_convert_single_device_node_to_text(
246 struct efi_device_path *dp)
251 case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
252 str = dp_hardware(str, dp);
254 case DEVICE_PATH_TYPE_ACPI_DEVICE:
255 str = dp_acpi(str, dp);
257 case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
258 str = dp_msging(str, dp);
260 case DEVICE_PATH_TYPE_MEDIA_DEVICE:
261 str = dp_media(str, dp);
263 case DEVICE_PATH_TYPE_END:
266 str = dp_unknown(str, dp);
274 * This function implements the ConvertDeviceNodeToText service of the
275 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
276 * See the Unified Extensible Firmware Interface (UEFI) specification
279 * device_node device node to be converted
280 * display_only true if the shorter text representation shall be used
281 * allow_shortcuts true if shortcut forms may be used
282 * @return text representation of the device path
283 * NULL if out of memory of device_path is NULL
285 static uint16_t EFIAPI *efi_convert_device_node_to_text(
286 struct efi_device_path *device_node,
288 bool allow_shortcuts)
290 char str[MAX_NODE_LEN];
291 uint16_t *text = NULL;
293 EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
297 efi_convert_single_device_node_to_text(str, device_node);
299 text = efi_str_to_u16(str);
302 EFI_EXIT(EFI_SUCCESS);
307 * This function implements the ConvertDevicePathToText service of the
308 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
309 * See the Unified Extensible Firmware Interface (UEFI) specification
312 * device_path device path to be converted
313 * display_only true if the shorter text representation shall be used
314 * allow_shortcuts true if shortcut forms may be used
315 * @return text representation of the device path
316 * NULL if out of memory of device_path is NULL
318 static uint16_t EFIAPI *efi_convert_device_path_to_text(
319 struct efi_device_path *device_path,
321 bool allow_shortcuts)
323 uint16_t *text = NULL;
324 char buffer[MAX_PATH_LEN];
327 EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
331 while (device_path &&
332 str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
334 str = efi_convert_single_device_node_to_text(str, device_path);
335 device_path = efi_dp_next(device_path);
338 text = efi_str_to_u16(buffer);
341 EFI_EXIT(EFI_SUCCESS);
345 /* helper for debug prints.. efi_free_pool() the result. */
346 uint16_t *efi_dp_str(struct efi_device_path *dp)
348 return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
351 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
352 .convert_device_node_to_text = efi_convert_device_node_to_text,
353 .convert_device_path_to_text = efi_convert_device_path_to_text,