1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * EFI device path interface
4 *
5 * Copyright (c) 2017 Heinrich Schuchardt
6 */
7
8 #include <common.h>
9 #include <efi_loader.h>
10
11 #define MAC_OUTPUT_LEN 22
12 #define UNKNOWN_OUTPUT_LEN 23
13
14 #define MAX_NODE_LEN 512
15 #define MAX_PATH_LEN 1024
16
17 const efi_guid_t efi_guid_device_path_to_text_protocol =
18 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
19
20 /**
21 * efi_str_to_u16() - convert ASCII string to UTF-16
22 *
23 * A u16 buffer is allocated from pool. The ASCII string is copied to the u16
24 * buffer.
25 *
26 * @str: ASCII string
27 * Return: UTF-16 string. NULL if out of memory.
28 */
efi_str_to_u16(char * str)29 static u16 *efi_str_to_u16(char *str)
30 {
31 efi_uintn_t len;
32 u16 *out, *dst;
33 efi_status_t ret;
34
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)
38 return NULL;
39 dst = out;
40 utf8_utf16_strcpy(&dst, str);
41 return out;
42 }
43
dp_unknown(char * s,struct efi_device_path * dp)44 static char *dp_unknown(char *s, struct efi_device_path *dp)
45 {
46 s += sprintf(s, "UNKNOWN(%04x,%04x)", dp->type, dp->sub_type);
47 return s;
48 }
49
dp_hardware(char * s,struct efi_device_path * dp)50 static char *dp_hardware(char *s, struct efi_device_path *dp)
51 {
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)",
57 mdp->memory_type,
58 mdp->start_address,
59 mdp->end_address);
60 break;
61 }
62 case DEVICE_PATH_SUB_TYPE_VENDOR: {
63 int i, n;
64 struct efi_device_path_vendor *vdp =
65 (struct efi_device_path_vendor *)dp;
66
67 s += sprintf(s, "VenHw(%pUl", &vdp->guid);
68 n = (int)vdp->dp.length - sizeof(struct efi_device_path_vendor);
69 if (n > 0) {
70 s += sprintf(s, ",");
71 for (i = 0; i < n; ++i)
72 s += sprintf(s, "%02x", vdp->vendor_data[i]);
73 }
74 s += sprintf(s, ")");
75 break;
76 }
77 default:
78 s = dp_unknown(s, dp);
79 break;
80 }
81 return s;
82 }
83
dp_acpi(char * s,struct efi_device_path * dp)84 static char *dp_acpi(char *s, struct efi_device_path *dp)
85 {
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;
90
91 s += sprintf(s, "Acpi(PNP%04X,%d)", EISA_PNP_NUM(adp->hid),
92 adp->uid);
93 break;
94 }
95 default:
96 s = dp_unknown(s, dp);
97 break;
98 }
99 return s;
100 }
101
dp_msging(char * s,struct efi_device_path * dp)102 static char *dp_msging(char *s, struct efi_device_path *dp)
103 {
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);
110 break;
111 }
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);
117 break;
118 }
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,
123 udp->usb_interface);
124 break;
125 }
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;
130
131 if (mdp->if_type <= 1)
132 n = 6;
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);
137
138 break;
139 }
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;
143
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);
148
149 break;
150 }
151 case DEVICE_PATH_SUB_TYPE_MSG_NVME: {
152 struct efi_device_path_nvme *ndp =
153 (struct efi_device_path_nvme *)dp;
154 u32 ns_id;
155 int i;
156
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 ? "-" : "",
161 ndp->eui64[i]);
162 s += sprintf(s, ")");
163
164 break;
165 }
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) ?
170 "SD" : "eMMC";
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);
174 break;
175 }
176 default:
177 s = dp_unknown(s, dp);
178 break;
179 }
180 return s;
181 }
182
183 /*
184 * Convert a media device path node to text.
185 *
186 * @s output buffer
187 * @dp device path node
188 * @return next unused buffer address
189 */
dp_media(char * s,struct efi_device_path * dp)190 static char *dp_media(char *s, struct efi_device_path *dp)
191 {
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;
197 u64 start;
198 u64 end;
199
200 /* Copy from packed structure to aligned memory */
201 memcpy(&start, &hddp->partition_start, sizeof(start));
202 memcpy(&end, &hddp->partition_end, sizeof(end));
203
204 switch (hddp->signature_type) {
205 case SIG_TYPE_MBR: {
206 u32 signature;
207
208 memcpy(&signature, sig, sizeof(signature));
209 s += sprintf(
210 s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
211 hddp->partition_number, signature, start, end);
212 break;
213 }
214 case SIG_TYPE_GUID:
215 s += sprintf(
216 s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
217 hddp->partition_number, sig, start, end);
218 break;
219 default:
220 s += sprintf(
221 s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
222 hddp->partition_number, hddp->partmap_type,
223 start, end);
224 break;
225 }
226
227 break;
228 }
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);
234 break;
235 }
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);
243 break;
244 }
245 default:
246 s = dp_unknown(s, dp);
247 break;
248 }
249 return s;
250 }
251
252 /*
253 * Converts a single node to a char string.
254 *
255 * @buffer output buffer
256 * @dp device path or node
257 * @return end of string
258 */
efi_convert_single_device_node_to_text(char * buffer,struct efi_device_path * dp)259 static char *efi_convert_single_device_node_to_text(
260 char *buffer,
261 struct efi_device_path *dp)
262 {
263 char *str = buffer;
264
265 switch (dp->type) {
266 case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
267 str = dp_hardware(str, dp);
268 break;
269 case DEVICE_PATH_TYPE_ACPI_DEVICE:
270 str = dp_acpi(str, dp);
271 break;
272 case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
273 str = dp_msging(str, dp);
274 break;
275 case DEVICE_PATH_TYPE_MEDIA_DEVICE:
276 str = dp_media(str, dp);
277 break;
278 case DEVICE_PATH_TYPE_END:
279 break;
280 default:
281 str = dp_unknown(str, dp);
282 }
283
284 *str = '\0';
285 return str;
286 }
287
288 /*
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
292 * for details.
293 *
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
299 */
efi_convert_device_node_to_text(struct efi_device_path * device_node,bool display_only,bool allow_shortcuts)300 static uint16_t EFIAPI *efi_convert_device_node_to_text(
301 struct efi_device_path *device_node,
302 bool display_only,
303 bool allow_shortcuts)
304 {
305 char str[MAX_NODE_LEN];
306 uint16_t *text = NULL;
307
308 EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
309
310 if (!device_node)
311 goto out;
312 efi_convert_single_device_node_to_text(str, device_node);
313
314 text = efi_str_to_u16(str);
315
316 out:
317 EFI_EXIT(EFI_SUCCESS);
318 return text;
319 }
320
321 /*
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
325 * for details.
326 *
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
332 */
efi_convert_device_path_to_text(struct efi_device_path * device_path,bool display_only,bool allow_shortcuts)333 static uint16_t EFIAPI *efi_convert_device_path_to_text(
334 struct efi_device_path *device_path,
335 bool display_only,
336 bool allow_shortcuts)
337 {
338 uint16_t *text = NULL;
339 char buffer[MAX_PATH_LEN];
340 char *str = buffer;
341
342 EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
343
344 if (!device_path)
345 goto out;
346 while (device_path &&
347 str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
348 *str++ = '/';
349 str = efi_convert_single_device_node_to_text(str, device_path);
350 device_path = efi_dp_next(device_path);
351 }
352
353 text = efi_str_to_u16(buffer);
354
355 out:
356 EFI_EXIT(EFI_SUCCESS);
357 return text;
358 }
359
360 /* helper for debug prints.. efi_free_pool() the result. */
efi_dp_str(struct efi_device_path * dp)361 uint16_t *efi_dp_str(struct efi_device_path *dp)
362 {
363 return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
364 }
365
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,
369 };
370