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 = sizeof(u16) * (utf8_utf16_strlen(str) + 1);
36 ret = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, len, (void **)&out);
37 if (ret != EFI_SUCCESS)
40 utf8_utf16_strcpy(&dst, 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_NVME: {
152 struct efi_device_path_nvme *ndp =
153 (struct efi_device_path_nvme *)dp;
157 memcpy(&ns_id, &ndp->ns_id, sizeof(ns_id));
158 s += sprintf(s, "NVMe(0x%x,", ns_id);
159 for (i = 0; i < sizeof(ndp->eui64); ++i)
160 s += sprintf(s, "%s%02x", i ? "-" : "",
162 s += sprintf(s, ")");
166 case DEVICE_PATH_SUB_TYPE_MSG_SD:
167 case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
168 const char *typename =
169 (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
171 struct efi_device_path_sd_mmc_path *sddp =
172 (struct efi_device_path_sd_mmc_path *)dp;
173 s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
177 s = dp_unknown(s, dp);
184 * Convert a media device path node to text.
187 * @dp device path node
188 * @return next unused buffer address
190 static char *dp_media(char *s, struct efi_device_path *dp)
192 switch (dp->sub_type) {
193 case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
194 struct efi_device_path_hard_drive_path *hddp =
195 (struct efi_device_path_hard_drive_path *)dp;
196 void *sig = hddp->partition_signature;
200 /* Copy from packed structure to aligned memory */
201 memcpy(&start, &hddp->partition_start, sizeof(start));
202 memcpy(&end, &hddp->partition_end, sizeof(end));
204 switch (hddp->signature_type) {
208 memcpy(&signature, sig, sizeof(signature));
210 s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
211 hddp->partition_number, signature, start, end);
216 s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
217 hddp->partition_number, sig, start, end);
221 s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
222 hddp->partition_number, hddp->partmap_type,
229 case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
230 struct efi_device_path_cdrom_path *cddp =
231 (struct efi_device_path_cdrom_path *)dp;
232 s += sprintf(s, "CDROM(%u,0x%llx,0x%llx)", cddp->boot_entry,
233 cddp->partition_start, cddp->partition_size);
236 case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
237 struct efi_device_path_file_path *fp =
238 (struct efi_device_path_file_path *)dp;
239 int slen = (dp->length - sizeof(*dp)) / 2;
240 if (slen > MAX_NODE_LEN - 2)
241 slen = MAX_NODE_LEN - 2;
242 s += sprintf(s, "%-.*ls", slen, fp->str);
246 s = dp_unknown(s, dp);
253 * Converts a single node to a char string.
255 * @buffer output buffer
256 * @dp device path or node
257 * @return end of string
259 static char *efi_convert_single_device_node_to_text(
261 struct efi_device_path *dp)
266 case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
267 str = dp_hardware(str, dp);
269 case DEVICE_PATH_TYPE_ACPI_DEVICE:
270 str = dp_acpi(str, dp);
272 case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
273 str = dp_msging(str, dp);
275 case DEVICE_PATH_TYPE_MEDIA_DEVICE:
276 str = dp_media(str, dp);
278 case DEVICE_PATH_TYPE_END:
281 str = dp_unknown(str, dp);
289 * This function implements the ConvertDeviceNodeToText service of the
290 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
291 * See the Unified Extensible Firmware Interface (UEFI) specification
294 * device_node device node to be converted
295 * display_only true if the shorter text representation shall be used
296 * allow_shortcuts true if shortcut forms may be used
297 * @return text representation of the device path
298 * NULL if out of memory of device_path is NULL
300 static uint16_t EFIAPI *efi_convert_device_node_to_text(
301 struct efi_device_path *device_node,
303 bool allow_shortcuts)
305 char str[MAX_NODE_LEN];
306 uint16_t *text = NULL;
308 EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
312 efi_convert_single_device_node_to_text(str, device_node);
314 text = efi_str_to_u16(str);
317 EFI_EXIT(EFI_SUCCESS);
322 * This function implements the ConvertDevicePathToText service of the
323 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
324 * See the Unified Extensible Firmware Interface (UEFI) specification
327 * device_path device path to be converted
328 * display_only true if the shorter text representation shall be used
329 * allow_shortcuts true if shortcut forms may be used
330 * @return text representation of the device path
331 * NULL if out of memory of device_path is NULL
333 static uint16_t EFIAPI *efi_convert_device_path_to_text(
334 struct efi_device_path *device_path,
336 bool allow_shortcuts)
338 uint16_t *text = NULL;
339 char buffer[MAX_PATH_LEN];
342 EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
346 while (device_path &&
347 str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
349 str = efi_convert_single_device_node_to_text(str, device_path);
350 device_path = efi_dp_next(device_path);
353 text = efi_str_to_u16(buffer);
356 EFI_EXIT(EFI_SUCCESS);
360 /* helper for debug prints.. efi_free_pool() the result. */
361 uint16_t *efi_dp_str(struct efi_device_path *dp)
363 return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
366 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
367 .convert_device_node_to_text = efi_convert_device_node_to_text,
368 .convert_device_path_to_text = efi_convert_device_path_to_text,