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;
20 static u16 *efi_str_to_u16(char *str)
26 len = strlen(str) + 1;
27 ret = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, len * sizeof(u16),
29 if (ret != EFI_SUCCESS)
31 ascii2unicode(out, str);
36 static char *dp_unknown(char *s, struct efi_device_path *dp)
38 s += sprintf(s, "UNKNOWN(%04x,%04x)", dp->type, dp->sub_type);
42 static char *dp_hardware(char *s, struct efi_device_path *dp)
44 switch (dp->sub_type) {
45 case DEVICE_PATH_SUB_TYPE_MEMORY: {
46 struct efi_device_path_memory *mdp =
47 (struct efi_device_path_memory *)dp;
48 s += sprintf(s, "MemoryMapped(0x%x,0x%llx,0x%llx)",
54 case DEVICE_PATH_SUB_TYPE_VENDOR: {
55 struct efi_device_path_vendor *vdp =
56 (struct efi_device_path_vendor *)dp;
57 s += sprintf(s, "VenHw(%pUl)", &vdp->guid);
61 s = dp_unknown(s, dp);
67 static char *dp_acpi(char *s, struct efi_device_path *dp)
69 switch (dp->sub_type) {
70 case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE: {
71 struct efi_device_path_acpi_path *adp =
72 (struct efi_device_path_acpi_path *)dp;
73 s += sprintf(s, "Acpi(PNP%04x", EISA_PNP_NUM(adp->hid));
75 s += sprintf(s, ",%d", adp->uid);
80 s = dp_unknown(s, dp);
86 static char *dp_msging(char *s, struct efi_device_path *dp)
88 switch (dp->sub_type) {
89 case DEVICE_PATH_SUB_TYPE_MSG_ATAPI: {
90 struct efi_device_path_atapi *ide =
91 (struct efi_device_path_atapi *)dp;
92 s += sprintf(s, "Ata(%d,%d,%d)", ide->primary_secondary,
93 ide->slave_master, ide->logical_unit_number);
96 case DEVICE_PATH_SUB_TYPE_MSG_SCSI: {
97 struct efi_device_path_scsi *ide =
98 (struct efi_device_path_scsi *)dp;
99 s += sprintf(s, "Scsi(%u,%u)", ide->target_id,
100 ide->logical_unit_number);
103 case DEVICE_PATH_SUB_TYPE_MSG_USB: {
104 struct efi_device_path_usb *udp =
105 (struct efi_device_path_usb *)dp;
106 s += sprintf(s, "USB(0x%x,0x%x)", udp->parent_port_number,
110 case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
111 struct efi_device_path_mac_addr *mdp =
112 (struct efi_device_path_mac_addr *)dp;
114 if (mdp->if_type != 0 && mdp->if_type != 1)
117 s += sprintf(s, "MAC(%02x%02x%02x%02x%02x%02x,0x%1x)",
118 mdp->mac.addr[0], mdp->mac.addr[1],
119 mdp->mac.addr[2], mdp->mac.addr[3],
120 mdp->mac.addr[4], mdp->mac.addr[5],
125 case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
126 struct efi_device_path_usb_class *ucdp =
127 (struct efi_device_path_usb_class *)dp;
129 s += sprintf(s, "USBClass(%x,%x,%x,%x,%x)",
130 ucdp->vendor_id, ucdp->product_id,
131 ucdp->device_class, ucdp->device_subclass,
132 ucdp->device_protocol);
136 case DEVICE_PATH_SUB_TYPE_MSG_SD:
137 case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
138 const char *typename =
139 (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
141 struct efi_device_path_sd_mmc_path *sddp =
142 (struct efi_device_path_sd_mmc_path *)dp;
143 s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
147 s = dp_unknown(s, dp);
154 * Convert a media device path node to text.
157 * @dp device path node
158 * @return next unused buffer address
160 static char *dp_media(char *s, struct efi_device_path *dp)
162 switch (dp->sub_type) {
163 case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
164 struct efi_device_path_hard_drive_path *hddp =
165 (struct efi_device_path_hard_drive_path *)dp;
166 void *sig = hddp->partition_signature;
170 /* Copy from packed structure to aligned memory */
171 memcpy(&start, &hddp->partition_start, sizeof(start));
172 memcpy(&end, &hddp->partition_end, sizeof(end));
174 switch (hddp->signature_type) {
178 memcpy(&signature, sig, sizeof(signature));
180 s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
181 hddp->partition_number, signature, start, end);
186 s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
187 hddp->partition_number, sig, start, end);
191 s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
192 hddp->partition_number, hddp->partmap_type,
199 case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
200 struct efi_device_path_cdrom_path *cddp =
201 (struct efi_device_path_cdrom_path *)dp;
202 s += sprintf(s, "CDROM(0x%x)", cddp->boot_entry);
205 case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
206 struct efi_device_path_file_path *fp =
207 (struct efi_device_path_file_path *)dp;
208 int slen = (dp->length - sizeof(*dp)) / 2;
209 if (slen > MAX_NODE_LEN - 2)
210 slen = MAX_NODE_LEN - 2;
211 s += sprintf(s, "%-.*ls", slen, fp->str);
215 s = dp_unknown(s, dp);
222 * Converts a single node to a char string.
224 * @buffer output buffer
225 * @dp device path or node
226 * @return end of string
228 static char *efi_convert_single_device_node_to_text(
230 struct efi_device_path *dp)
235 case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
236 str = dp_hardware(str, dp);
238 case DEVICE_PATH_TYPE_ACPI_DEVICE:
239 str = dp_acpi(str, dp);
241 case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
242 str = dp_msging(str, dp);
244 case DEVICE_PATH_TYPE_MEDIA_DEVICE:
245 str = dp_media(str, dp);
247 case DEVICE_PATH_TYPE_END:
250 str = dp_unknown(str, dp);
258 * This function implements the ConvertDeviceNodeToText service of the
259 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
260 * See the Unified Extensible Firmware Interface (UEFI) specification
263 * device_node device node to be converted
264 * display_only true if the shorter text represenation shall be used
265 * allow_shortcuts true if shortcut forms may be used
266 * @return text represenation of the device path
267 * NULL if out of memory of device_path is NULL
269 static uint16_t EFIAPI *efi_convert_device_node_to_text(
270 struct efi_device_path *device_node,
272 bool allow_shortcuts)
274 char str[MAX_NODE_LEN];
275 uint16_t *text = NULL;
277 EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
281 efi_convert_single_device_node_to_text(str, device_node);
283 text = efi_str_to_u16(str);
286 EFI_EXIT(EFI_SUCCESS);
291 * This function implements the ConvertDevicePathToText service of the
292 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
293 * See the Unified Extensible Firmware Interface (UEFI) specification
296 * device_path device path to be converted
297 * display_only true if the shorter text represenation shall be used
298 * allow_shortcuts true if shortcut forms may be used
299 * @return text represenation of the device path
300 * NULL if out of memory of device_path is NULL
302 static uint16_t EFIAPI *efi_convert_device_path_to_text(
303 struct efi_device_path *device_path,
305 bool allow_shortcuts)
307 uint16_t *text = NULL;
308 char buffer[MAX_PATH_LEN];
311 EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
315 while (device_path &&
316 str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
318 str = efi_convert_single_device_node_to_text(str, device_path);
319 device_path = efi_dp_next(device_path);
322 text = efi_str_to_u16(buffer);
325 EFI_EXIT(EFI_SUCCESS);
329 /* helper for debug prints.. efi_free_pool() the result. */
330 uint16_t *efi_dp_str(struct efi_device_path *dp)
332 return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
335 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
336 .convert_device_node_to_text = efi_convert_device_node_to_text,
337 .convert_device_path_to_text = efi_convert_device_path_to_text,