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 <blk.h>
10 #include <efi_loader.h>
11
12 #define MAC_OUTPUT_LEN 22
13 #define UNKNOWN_OUTPUT_LEN 23
14
15 #define MAX_NODE_LEN 512
16 #define MAX_PATH_LEN 1024
17
18 const efi_guid_t efi_guid_device_path_to_text_protocol =
19 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
20
21 /**
22 * efi_str_to_u16() - convert ASCII string to UTF-16
23 *
24 * A u16 buffer is allocated from pool. The ASCII string is copied to the u16
25 * buffer.
26 *
27 * @str: ASCII string
28 * Return: UTF-16 string. NULL if out of memory.
29 */
efi_str_to_u16(char * str)30 static u16 *efi_str_to_u16(char *str)
31 {
32 efi_uintn_t len;
33 u16 *out, *dst;
34 efi_status_t ret;
35
36 len = sizeof(u16) * (utf8_utf16_strlen(str) + 1);
37 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, len, (void **)&out);
38 if (ret != EFI_SUCCESS)
39 return NULL;
40 dst = out;
41 utf8_utf16_strcpy(&dst, str);
42 return out;
43 }
44
dp_unknown(char * s,struct efi_device_path * dp)45 static char *dp_unknown(char *s, struct efi_device_path *dp)
46 {
47 s += sprintf(s, "UNKNOWN(%04x,%04x)", dp->type, dp->sub_type);
48 return s;
49 }
50
dp_hardware(char * s,struct efi_device_path * dp)51 static char *dp_hardware(char *s, struct efi_device_path *dp)
52 {
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)",
58 mdp->memory_type,
59 mdp->start_address,
60 mdp->end_address);
61 break;
62 }
63 case DEVICE_PATH_SUB_TYPE_VENDOR: {
64 int i, n;
65 struct efi_device_path_vendor *vdp =
66 (struct efi_device_path_vendor *)dp;
67
68 s += sprintf(s, "VenHw(%pUl", &vdp->guid);
69 n = (int)vdp->dp.length - sizeof(struct efi_device_path_vendor);
70 /* Node must fit into MAX_NODE_LEN) */
71 if (n > 0 && n < MAX_NODE_LEN / 2 - 22) {
72 s += sprintf(s, ",");
73 for (i = 0; i < n; ++i)
74 s += sprintf(s, "%02x", vdp->vendor_data[i]);
75 }
76 s += sprintf(s, ")");
77 break;
78 }
79 default:
80 s = dp_unknown(s, dp);
81 break;
82 }
83 return s;
84 }
85
dp_acpi(char * s,struct efi_device_path * dp)86 static char *dp_acpi(char *s, struct efi_device_path *dp)
87 {
88 switch (dp->sub_type) {
89 case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE: {
90 struct efi_device_path_acpi_path *adp =
91 (struct efi_device_path_acpi_path *)dp;
92
93 s += sprintf(s, "Acpi(PNP%04X,%d)", EISA_PNP_NUM(adp->hid),
94 adp->uid);
95 break;
96 }
97 default:
98 s = dp_unknown(s, dp);
99 break;
100 }
101 return s;
102 }
103
dp_msging(char * s,struct efi_device_path * dp)104 static char *dp_msging(char *s, struct efi_device_path *dp)
105 {
106 switch (dp->sub_type) {
107 case DEVICE_PATH_SUB_TYPE_MSG_ATAPI: {
108 struct efi_device_path_atapi *ide =
109 (struct efi_device_path_atapi *)dp;
110 s += sprintf(s, "Ata(%d,%d,%d)", ide->primary_secondary,
111 ide->slave_master, ide->logical_unit_number);
112 break;
113 }
114 case DEVICE_PATH_SUB_TYPE_MSG_SCSI: {
115 struct efi_device_path_scsi *ide =
116 (struct efi_device_path_scsi *)dp;
117 s += sprintf(s, "Scsi(%u,%u)", ide->target_id,
118 ide->logical_unit_number);
119 break;
120 }
121 case DEVICE_PATH_SUB_TYPE_MSG_UART: {
122 struct efi_device_path_uart *uart =
123 (struct efi_device_path_uart *)dp;
124 s += sprintf(s, "Uart(%lld,%d,%d,", uart->baud_rate,
125 uart->data_bits, uart->parity);
126 switch (uart->stop_bits) {
127 case 2:
128 s += sprintf(s, "1.5)");
129 break;
130 default:
131 s += sprintf(s, "%d)", uart->stop_bits);
132 break;
133 }
134 break;
135 }
136 case DEVICE_PATH_SUB_TYPE_MSG_USB: {
137 struct efi_device_path_usb *udp =
138 (struct efi_device_path_usb *)dp;
139 s += sprintf(s, "USB(0x%x,0x%x)", udp->parent_port_number,
140 udp->usb_interface);
141 break;
142 }
143 case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
144 int i, n = sizeof(struct efi_mac_addr);
145 struct efi_device_path_mac_addr *mdp =
146 (struct efi_device_path_mac_addr *)dp;
147
148 if (mdp->if_type <= 1)
149 n = 6;
150 s += sprintf(s, "MAC(");
151 for (i = 0; i < n; ++i)
152 s += sprintf(s, "%02x", mdp->mac.addr[i]);
153 s += sprintf(s, ",%u)", mdp->if_type);
154
155 break;
156 }
157 case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
158 struct efi_device_path_usb_class *ucdp =
159 (struct efi_device_path_usb_class *)dp;
160
161 s += sprintf(s, "UsbClass(0x%x,0x%x,0x%x,0x%x,0x%x)",
162 ucdp->vendor_id, ucdp->product_id,
163 ucdp->device_class, ucdp->device_subclass,
164 ucdp->device_protocol);
165
166 break;
167 }
168 case DEVICE_PATH_SUB_TYPE_MSG_SATA: {
169 struct efi_device_path_sata *sdp =
170 (struct efi_device_path_sata *) dp;
171
172 s += sprintf(s, "Sata(0x%x,0x%x,0x%x)",
173 sdp->hba_port,
174 sdp->port_multiplier_port,
175 sdp->logical_unit_number);
176 break;
177 }
178 case DEVICE_PATH_SUB_TYPE_MSG_NVME: {
179 struct efi_device_path_nvme *ndp =
180 (struct efi_device_path_nvme *)dp;
181 u32 ns_id;
182 int i;
183
184 memcpy(&ns_id, &ndp->ns_id, sizeof(ns_id));
185 s += sprintf(s, "NVMe(0x%x,", ns_id);
186 for (i = 0; i < sizeof(ndp->eui64); ++i)
187 s += sprintf(s, "%s%02x", i ? "-" : "",
188 ndp->eui64[i]);
189 s += sprintf(s, ")");
190
191 break;
192 }
193 case DEVICE_PATH_SUB_TYPE_MSG_URI: {
194 struct efi_device_path_uri *udp =
195 (struct efi_device_path_uri *)dp;
196 int n;
197
198 n = (int)udp->dp.length - sizeof(struct efi_device_path_uri);
199
200 s += sprintf(s, "Uri(");
201 if (n > 0 && n < MAX_NODE_LEN - 6)
202 s += snprintf(s, n, "%s", (char *)udp->uri);
203 s += sprintf(s, ")");
204 break;
205 }
206 case DEVICE_PATH_SUB_TYPE_MSG_SD:
207 case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
208 const char *typename =
209 (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
210 "SD" : "eMMC";
211 struct efi_device_path_sd_mmc_path *sddp =
212 (struct efi_device_path_sd_mmc_path *)dp;
213 s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
214 break;
215 }
216 default:
217 s = dp_unknown(s, dp);
218 break;
219 }
220 return s;
221 }
222
223 /*
224 * Convert a media device path node to text.
225 *
226 * @s output buffer
227 * @dp device path node
228 * @return next unused buffer address
229 */
dp_media(char * s,struct efi_device_path * dp)230 static char *dp_media(char *s, struct efi_device_path *dp)
231 {
232 switch (dp->sub_type) {
233 case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
234 struct efi_device_path_hard_drive_path *hddp =
235 (struct efi_device_path_hard_drive_path *)dp;
236 void *sig = hddp->partition_signature;
237 u64 start;
238 u64 end;
239
240 /* Copy from packed structure to aligned memory */
241 memcpy(&start, &hddp->partition_start, sizeof(start));
242 memcpy(&end, &hddp->partition_end, sizeof(end));
243
244 switch (hddp->signature_type) {
245 case SIG_TYPE_MBR: {
246 u32 signature;
247
248 memcpy(&signature, sig, sizeof(signature));
249 s += sprintf(
250 s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
251 hddp->partition_number, signature, start, end);
252 break;
253 }
254 case SIG_TYPE_GUID:
255 s += sprintf(
256 s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
257 hddp->partition_number, sig, start, end);
258 break;
259 default:
260 s += sprintf(
261 s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
262 hddp->partition_number, hddp->partmap_type,
263 start, end);
264 break;
265 }
266
267 break;
268 }
269 case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
270 struct efi_device_path_cdrom_path *cddp =
271 (struct efi_device_path_cdrom_path *)dp;
272 s += sprintf(s, "CDROM(%u,0x%llx,0x%llx)", cddp->boot_entry,
273 cddp->partition_start, cddp->partition_size);
274 break;
275 }
276 case DEVICE_PATH_SUB_TYPE_VENDOR_PATH: {
277 int i, n;
278 struct efi_device_path_vendor *vdp =
279 (struct efi_device_path_vendor *)dp;
280
281 s += sprintf(s, "VenMedia(%pUl", &vdp->guid);
282 n = (int)vdp->dp.length - sizeof(struct efi_device_path_vendor);
283 /* Node must fit into MAX_NODE_LEN) */
284 if (n > 0 && n < MAX_NODE_LEN / 2 - 24) {
285 s += sprintf(s, ",");
286 for (i = 0; i < n; ++i)
287 s += sprintf(s, "%02x", vdp->vendor_data[i]);
288 }
289 s += sprintf(s, ")");
290 break;
291 }
292 case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
293 struct efi_device_path_file_path *fp =
294 (struct efi_device_path_file_path *)dp;
295 int slen = (dp->length - sizeof(*dp)) / 2;
296 if (slen > MAX_NODE_LEN - 2)
297 slen = MAX_NODE_LEN - 2;
298 s += sprintf(s, "%-.*ls", slen, fp->str);
299 break;
300 }
301 default:
302 s = dp_unknown(s, dp);
303 break;
304 }
305 return s;
306 }
307
308 /*
309 * Converts a single node to a char string.
310 *
311 * @buffer output buffer
312 * @dp device path or node
313 * @return end of string
314 */
efi_convert_single_device_node_to_text(char * buffer,struct efi_device_path * dp)315 static char *efi_convert_single_device_node_to_text(
316 char *buffer,
317 struct efi_device_path *dp)
318 {
319 char *str = buffer;
320
321 switch (dp->type) {
322 case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
323 str = dp_hardware(str, dp);
324 break;
325 case DEVICE_PATH_TYPE_ACPI_DEVICE:
326 str = dp_acpi(str, dp);
327 break;
328 case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
329 str = dp_msging(str, dp);
330 break;
331 case DEVICE_PATH_TYPE_MEDIA_DEVICE:
332 str = dp_media(str, dp);
333 break;
334 case DEVICE_PATH_TYPE_END:
335 break;
336 default:
337 str = dp_unknown(str, dp);
338 }
339
340 *str = '\0';
341 return str;
342 }
343
344 /*
345 * This function implements the ConvertDeviceNodeToText service of the
346 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
347 * See the Unified Extensible Firmware Interface (UEFI) specification
348 * for details.
349 *
350 * device_node device node to be converted
351 * display_only true if the shorter text representation shall be used
352 * allow_shortcuts true if shortcut forms may be used
353 * @return text representation of the device path
354 * NULL if out of memory of device_path is NULL
355 */
efi_convert_device_node_to_text(struct efi_device_path * device_node,bool display_only,bool allow_shortcuts)356 static uint16_t EFIAPI *efi_convert_device_node_to_text(
357 struct efi_device_path *device_node,
358 bool display_only,
359 bool allow_shortcuts)
360 {
361 char str[MAX_NODE_LEN];
362 uint16_t *text = NULL;
363
364 EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
365
366 if (!device_node)
367 goto out;
368 efi_convert_single_device_node_to_text(str, device_node);
369
370 text = efi_str_to_u16(str);
371
372 out:
373 EFI_EXIT(EFI_SUCCESS);
374 return text;
375 }
376
377 /*
378 * This function implements the ConvertDevicePathToText service of the
379 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
380 * See the Unified Extensible Firmware Interface (UEFI) specification
381 * for details.
382 *
383 * device_path device path to be converted
384 * display_only true if the shorter text representation shall be used
385 * allow_shortcuts true if shortcut forms may be used
386 * @return text representation of the device path
387 * NULL if out of memory of device_path is NULL
388 */
efi_convert_device_path_to_text(struct efi_device_path * device_path,bool display_only,bool allow_shortcuts)389 static uint16_t EFIAPI *efi_convert_device_path_to_text(
390 struct efi_device_path *device_path,
391 bool display_only,
392 bool allow_shortcuts)
393 {
394 uint16_t *text = NULL;
395 char buffer[MAX_PATH_LEN];
396 char *str = buffer;
397
398 EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
399
400 if (!device_path)
401 goto out;
402 while (device_path && str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
403 if (device_path->type == DEVICE_PATH_TYPE_END) {
404 if (device_path->sub_type !=
405 DEVICE_PATH_SUB_TYPE_INSTANCE_END)
406 break;
407 *str++ = ',';
408 } else {
409 *str++ = '/';
410 str = efi_convert_single_device_node_to_text(
411 str, device_path);
412 }
413 *(u8 **)&device_path += device_path->length;
414 }
415
416 text = efi_str_to_u16(buffer);
417
418 out:
419 EFI_EXIT(EFI_SUCCESS);
420 return text;
421 }
422
423 /* helper for debug prints.. efi_free_pool() the result. */
efi_dp_str(struct efi_device_path * dp)424 uint16_t *efi_dp_str(struct efi_device_path *dp)
425 {
426 return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
427 }
428
429 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
430 .convert_device_node_to_text = efi_convert_device_node_to_text,
431 .convert_device_path_to_text = efi_convert_device_path_to_text,
432 };
433