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: {
63 struct efi_device_path_vendor *vdp =
64 (struct efi_device_path_vendor *)dp;
65 s += sprintf(s, "VenHw(%pUl)", &vdp->guid);
69 s = dp_unknown(s, dp);
75 static char *dp_acpi(char *s, struct efi_device_path *dp)
77 switch (dp->sub_type) {
78 case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE: {
79 struct efi_device_path_acpi_path *adp =
80 (struct efi_device_path_acpi_path *)dp;
82 s += sprintf(s, "Acpi(PNP%04X,%d)", EISA_PNP_NUM(adp->hid),
87 s = dp_unknown(s, dp);
93 static char *dp_msging(char *s, struct efi_device_path *dp)
95 switch (dp->sub_type) {
96 case DEVICE_PATH_SUB_TYPE_MSG_ATAPI: {
97 struct efi_device_path_atapi *ide =
98 (struct efi_device_path_atapi *)dp;
99 s += sprintf(s, "Ata(%d,%d,%d)", ide->primary_secondary,
100 ide->slave_master, ide->logical_unit_number);
103 case DEVICE_PATH_SUB_TYPE_MSG_SCSI: {
104 struct efi_device_path_scsi *ide =
105 (struct efi_device_path_scsi *)dp;
106 s += sprintf(s, "Scsi(%u,%u)", ide->target_id,
107 ide->logical_unit_number);
110 case DEVICE_PATH_SUB_TYPE_MSG_USB: {
111 struct efi_device_path_usb *udp =
112 (struct efi_device_path_usb *)dp;
113 s += sprintf(s, "USB(0x%x,0x%x)", udp->parent_port_number,
117 case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
118 struct efi_device_path_mac_addr *mdp =
119 (struct efi_device_path_mac_addr *)dp;
121 if (mdp->if_type != 0 && mdp->if_type != 1)
124 s += sprintf(s, "MAC(%02x%02x%02x%02x%02x%02x,0x%1x)",
125 mdp->mac.addr[0], mdp->mac.addr[1],
126 mdp->mac.addr[2], mdp->mac.addr[3],
127 mdp->mac.addr[4], mdp->mac.addr[5],
132 case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
133 struct efi_device_path_usb_class *ucdp =
134 (struct efi_device_path_usb_class *)dp;
136 s += sprintf(s, "USBClass(%x,%x,%x,%x,%x)",
137 ucdp->vendor_id, ucdp->product_id,
138 ucdp->device_class, ucdp->device_subclass,
139 ucdp->device_protocol);
143 case DEVICE_PATH_SUB_TYPE_MSG_SD:
144 case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
145 const char *typename =
146 (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
148 struct efi_device_path_sd_mmc_path *sddp =
149 (struct efi_device_path_sd_mmc_path *)dp;
150 s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
154 s = dp_unknown(s, dp);
161 * Convert a media device path node to text.
164 * @dp device path node
165 * @return next unused buffer address
167 static char *dp_media(char *s, struct efi_device_path *dp)
169 switch (dp->sub_type) {
170 case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
171 struct efi_device_path_hard_drive_path *hddp =
172 (struct efi_device_path_hard_drive_path *)dp;
173 void *sig = hddp->partition_signature;
177 /* Copy from packed structure to aligned memory */
178 memcpy(&start, &hddp->partition_start, sizeof(start));
179 memcpy(&end, &hddp->partition_end, sizeof(end));
181 switch (hddp->signature_type) {
185 memcpy(&signature, sig, sizeof(signature));
187 s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
188 hddp->partition_number, signature, start, end);
193 s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
194 hddp->partition_number, sig, start, end);
198 s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
199 hddp->partition_number, hddp->partmap_type,
206 case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
207 struct efi_device_path_cdrom_path *cddp =
208 (struct efi_device_path_cdrom_path *)dp;
209 s += sprintf(s, "CDROM(0x%x)", cddp->boot_entry);
212 case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
213 struct efi_device_path_file_path *fp =
214 (struct efi_device_path_file_path *)dp;
215 int slen = (dp->length - sizeof(*dp)) / 2;
216 if (slen > MAX_NODE_LEN - 2)
217 slen = MAX_NODE_LEN - 2;
218 s += sprintf(s, "%-.*ls", slen, fp->str);
222 s = dp_unknown(s, dp);
229 * Converts a single node to a char string.
231 * @buffer output buffer
232 * @dp device path or node
233 * @return end of string
235 static char *efi_convert_single_device_node_to_text(
237 struct efi_device_path *dp)
242 case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
243 str = dp_hardware(str, dp);
245 case DEVICE_PATH_TYPE_ACPI_DEVICE:
246 str = dp_acpi(str, dp);
248 case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
249 str = dp_msging(str, dp);
251 case DEVICE_PATH_TYPE_MEDIA_DEVICE:
252 str = dp_media(str, dp);
254 case DEVICE_PATH_TYPE_END:
257 str = dp_unknown(str, dp);
265 * This function implements the ConvertDeviceNodeToText service of the
266 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
267 * See the Unified Extensible Firmware Interface (UEFI) specification
270 * device_node device node to be converted
271 * display_only true if the shorter text representation shall be used
272 * allow_shortcuts true if shortcut forms may be used
273 * @return text representation of the device path
274 * NULL if out of memory of device_path is NULL
276 static uint16_t EFIAPI *efi_convert_device_node_to_text(
277 struct efi_device_path *device_node,
279 bool allow_shortcuts)
281 char str[MAX_NODE_LEN];
282 uint16_t *text = NULL;
284 EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
288 efi_convert_single_device_node_to_text(str, device_node);
290 text = efi_str_to_u16(str);
293 EFI_EXIT(EFI_SUCCESS);
298 * This function implements the ConvertDevicePathToText service of the
299 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
300 * See the Unified Extensible Firmware Interface (UEFI) specification
303 * device_path device path to be converted
304 * display_only true if the shorter text representation shall be used
305 * allow_shortcuts true if shortcut forms may be used
306 * @return text representation of the device path
307 * NULL if out of memory of device_path is NULL
309 static uint16_t EFIAPI *efi_convert_device_path_to_text(
310 struct efi_device_path *device_path,
312 bool allow_shortcuts)
314 uint16_t *text = NULL;
315 char buffer[MAX_PATH_LEN];
318 EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
322 while (device_path &&
323 str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
325 str = efi_convert_single_device_node_to_text(str, device_path);
326 device_path = efi_dp_next(device_path);
329 text = efi_str_to_u16(buffer);
332 EFI_EXIT(EFI_SUCCESS);
336 /* helper for debug prints.. efi_free_pool() the result. */
337 uint16_t *efi_dp_str(struct efi_device_path *dp)
339 return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
342 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
343 .convert_device_node_to_text = efi_convert_device_node_to_text,
344 .convert_device_path_to_text = efi_convert_device_path_to_text,