1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI device path interface
5 * Copyright (c) 2017 Heinrich Schuchardt
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;
22 * efi_str_to_u16() - convert ASCII string to UTF-16
24 * A u16 buffer is allocated from pool. The ASCII string is copied to the u16
28 * Return: UTF-16 string. NULL if out of memory.
30 static u16 *efi_str_to_u16(char *str)
36 len = sizeof(u16) * (utf8_utf16_strlen(str) + 1);
37 ret = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, len, (void **)&out);
38 if (ret != EFI_SUCCESS)
41 utf8_utf16_strcpy(&dst, str);
45 static char *dp_unknown(char *s, struct efi_device_path *dp)
47 s += sprintf(s, "UNKNOWN(%04x,%04x)", dp->type, dp->sub_type);
51 static char *dp_hardware(char *s, struct efi_device_path *dp)
53 switch (dp->sub_type) {
54 case DEVICE_PATH_SUB_TYPE_MEMORY: {
55 struct efi_device_path_memory *mdp =
56 (struct efi_device_path_memory *)dp;
57 s += sprintf(s, "MemoryMapped(0x%x,0x%llx,0x%llx)",
63 case DEVICE_PATH_SUB_TYPE_VENDOR: {
65 struct efi_device_path_vendor *vdp =
66 (struct efi_device_path_vendor *)dp;
68 s += sprintf(s, "VenHw(%pUl", &vdp->guid);
69 n = (int)vdp->dp.length - sizeof(struct efi_device_path_vendor);
72 for (i = 0; i < n; ++i)
73 s += sprintf(s, "%02x", vdp->vendor_data[i]);
79 s = dp_unknown(s, dp);
85 static char *dp_acpi(char *s, struct efi_device_path *dp)
87 switch (dp->sub_type) {
88 case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE: {
89 struct efi_device_path_acpi_path *adp =
90 (struct efi_device_path_acpi_path *)dp;
92 s += sprintf(s, "Acpi(PNP%04X,%d)", EISA_PNP_NUM(adp->hid),
97 s = dp_unknown(s, dp);
103 static char *dp_msging(char *s, struct efi_device_path *dp)
105 switch (dp->sub_type) {
106 case DEVICE_PATH_SUB_TYPE_MSG_ATAPI: {
107 struct efi_device_path_atapi *ide =
108 (struct efi_device_path_atapi *)dp;
109 s += sprintf(s, "Ata(%d,%d,%d)", ide->primary_secondary,
110 ide->slave_master, ide->logical_unit_number);
113 case DEVICE_PATH_SUB_TYPE_MSG_SCSI: {
114 struct efi_device_path_scsi *ide =
115 (struct efi_device_path_scsi *)dp;
116 s += sprintf(s, "Scsi(%u,%u)", ide->target_id,
117 ide->logical_unit_number);
120 case DEVICE_PATH_SUB_TYPE_MSG_USB: {
121 struct efi_device_path_usb *udp =
122 (struct efi_device_path_usb *)dp;
123 s += sprintf(s, "USB(0x%x,0x%x)", udp->parent_port_number,
127 case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
128 int i, n = sizeof(struct efi_mac_addr);
129 struct efi_device_path_mac_addr *mdp =
130 (struct efi_device_path_mac_addr *)dp;
132 if (mdp->if_type <= 1)
134 s += sprintf(s, "MAC(");
135 for (i = 0; i < n; ++i)
136 s += sprintf(s, "%02x", mdp->mac.addr[i]);
137 s += sprintf(s, ",%u)", mdp->if_type);
141 case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
142 struct efi_device_path_usb_class *ucdp =
143 (struct efi_device_path_usb_class *)dp;
145 s += sprintf(s, "UsbClass(0x%x,0x%x,0x%x,0x%x,0x%x)",
146 ucdp->vendor_id, ucdp->product_id,
147 ucdp->device_class, ucdp->device_subclass,
148 ucdp->device_protocol);
152 case DEVICE_PATH_SUB_TYPE_MSG_SATA: {
153 struct efi_device_path_sata *sdp =
154 (struct efi_device_path_sata *) dp;
156 s += sprintf(s, "Sata(0x%x,0x%x,0x%x)",
158 sdp->port_multiplier_port,
159 sdp->logical_unit_number);
162 case DEVICE_PATH_SUB_TYPE_MSG_NVME: {
163 struct efi_device_path_nvme *ndp =
164 (struct efi_device_path_nvme *)dp;
168 memcpy(&ns_id, &ndp->ns_id, sizeof(ns_id));
169 s += sprintf(s, "NVMe(0x%x,", ns_id);
170 for (i = 0; i < sizeof(ndp->eui64); ++i)
171 s += sprintf(s, "%s%02x", i ? "-" : "",
173 s += sprintf(s, ")");
177 case DEVICE_PATH_SUB_TYPE_MSG_SD:
178 case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
179 const char *typename =
180 (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
182 struct efi_device_path_sd_mmc_path *sddp =
183 (struct efi_device_path_sd_mmc_path *)dp;
184 s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
188 s = dp_unknown(s, dp);
195 * Convert a media device path node to text.
198 * @dp device path node
199 * @return next unused buffer address
201 static char *dp_media(char *s, struct efi_device_path *dp)
203 switch (dp->sub_type) {
204 case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
205 struct efi_device_path_hard_drive_path *hddp =
206 (struct efi_device_path_hard_drive_path *)dp;
207 void *sig = hddp->partition_signature;
211 /* Copy from packed structure to aligned memory */
212 memcpy(&start, &hddp->partition_start, sizeof(start));
213 memcpy(&end, &hddp->partition_end, sizeof(end));
215 switch (hddp->signature_type) {
219 memcpy(&signature, sig, sizeof(signature));
221 s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
222 hddp->partition_number, signature, start, end);
227 s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
228 hddp->partition_number, sig, start, end);
232 s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
233 hddp->partition_number, hddp->partmap_type,
240 case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
241 struct efi_device_path_cdrom_path *cddp =
242 (struct efi_device_path_cdrom_path *)dp;
243 s += sprintf(s, "CDROM(%u,0x%llx,0x%llx)", cddp->boot_entry,
244 cddp->partition_start, cddp->partition_size);
247 case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
248 struct efi_device_path_file_path *fp =
249 (struct efi_device_path_file_path *)dp;
250 int slen = (dp->length - sizeof(*dp)) / 2;
251 if (slen > MAX_NODE_LEN - 2)
252 slen = MAX_NODE_LEN - 2;
253 s += sprintf(s, "%-.*ls", slen, fp->str);
257 s = dp_unknown(s, dp);
264 * Converts a single node to a char string.
266 * @buffer output buffer
267 * @dp device path or node
268 * @return end of string
270 static char *efi_convert_single_device_node_to_text(
272 struct efi_device_path *dp)
277 case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
278 str = dp_hardware(str, dp);
280 case DEVICE_PATH_TYPE_ACPI_DEVICE:
281 str = dp_acpi(str, dp);
283 case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
284 str = dp_msging(str, dp);
286 case DEVICE_PATH_TYPE_MEDIA_DEVICE:
287 str = dp_media(str, dp);
289 case DEVICE_PATH_TYPE_END:
292 str = dp_unknown(str, dp);
300 * This function implements the ConvertDeviceNodeToText service of the
301 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
302 * See the Unified Extensible Firmware Interface (UEFI) specification
305 * device_node device node to be converted
306 * display_only true if the shorter text representation shall be used
307 * allow_shortcuts true if shortcut forms may be used
308 * @return text representation of the device path
309 * NULL if out of memory of device_path is NULL
311 static uint16_t EFIAPI *efi_convert_device_node_to_text(
312 struct efi_device_path *device_node,
314 bool allow_shortcuts)
316 char str[MAX_NODE_LEN];
317 uint16_t *text = NULL;
319 EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
323 efi_convert_single_device_node_to_text(str, device_node);
325 text = efi_str_to_u16(str);
328 EFI_EXIT(EFI_SUCCESS);
333 * This function implements the ConvertDevicePathToText service of the
334 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
335 * See the Unified Extensible Firmware Interface (UEFI) specification
338 * device_path device path to be converted
339 * display_only true if the shorter text representation shall be used
340 * allow_shortcuts true if shortcut forms may be used
341 * @return text representation of the device path
342 * NULL if out of memory of device_path is NULL
344 static uint16_t EFIAPI *efi_convert_device_path_to_text(
345 struct efi_device_path *device_path,
347 bool allow_shortcuts)
349 uint16_t *text = NULL;
350 char buffer[MAX_PATH_LEN];
353 EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
357 while (device_path &&
358 str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
360 str = efi_convert_single_device_node_to_text(str, device_path);
361 device_path = efi_dp_next(device_path);
364 text = efi_str_to_u16(buffer);
367 EFI_EXIT(EFI_SUCCESS);
371 /* helper for debug prints.. efi_free_pool() the result. */
372 uint16_t *efi_dp_str(struct efi_device_path *dp)
374 return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
377 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
378 .convert_device_node_to_text = efi_convert_device_node_to_text,
379 .convert_device_path_to_text = efi_convert_device_path_to_text,