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_ALLOCATE_ANY_PAGES, 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_SD:
194 case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
195 const char *typename =
196 (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
197 "SD" : "eMMC";
198 struct efi_device_path_sd_mmc_path *sddp =
199 (struct efi_device_path_sd_mmc_path *)dp;
200 s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
201 break;
202 }
203 default:
204 s = dp_unknown(s, dp);
205 break;
206 }
207 return s;
208 }
209
210 /*
211 * Convert a media device path node to text.
212 *
213 * @s output buffer
214 * @dp device path node
215 * @return next unused buffer address
216 */
dp_media(char * s,struct efi_device_path * dp)217 static char *dp_media(char *s, struct efi_device_path *dp)
218 {
219 switch (dp->sub_type) {
220 case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
221 struct efi_device_path_hard_drive_path *hddp =
222 (struct efi_device_path_hard_drive_path *)dp;
223 void *sig = hddp->partition_signature;
224 u64 start;
225 u64 end;
226
227 /* Copy from packed structure to aligned memory */
228 memcpy(&start, &hddp->partition_start, sizeof(start));
229 memcpy(&end, &hddp->partition_end, sizeof(end));
230
231 switch (hddp->signature_type) {
232 case SIG_TYPE_MBR: {
233 u32 signature;
234
235 memcpy(&signature, sig, sizeof(signature));
236 s += sprintf(
237 s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
238 hddp->partition_number, signature, start, end);
239 break;
240 }
241 case SIG_TYPE_GUID:
242 s += sprintf(
243 s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
244 hddp->partition_number, sig, start, end);
245 break;
246 default:
247 s += sprintf(
248 s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
249 hddp->partition_number, hddp->partmap_type,
250 start, end);
251 break;
252 }
253
254 break;
255 }
256 case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
257 struct efi_device_path_cdrom_path *cddp =
258 (struct efi_device_path_cdrom_path *)dp;
259 s += sprintf(s, "CDROM(%u,0x%llx,0x%llx)", cddp->boot_entry,
260 cddp->partition_start, cddp->partition_size);
261 break;
262 }
263 case DEVICE_PATH_SUB_TYPE_VENDOR_PATH: {
264 int i, n;
265 struct efi_device_path_vendor *vdp =
266 (struct efi_device_path_vendor *)dp;
267
268 s += sprintf(s, "VenMedia(%pUl", &vdp->guid);
269 n = (int)vdp->dp.length - sizeof(struct efi_device_path_vendor);
270 /* Node must fit into MAX_NODE_LEN) */
271 if (n > 0 && n < MAX_NODE_LEN / 2 - 24) {
272 s += sprintf(s, ",");
273 for (i = 0; i < n; ++i)
274 s += sprintf(s, "%02x", vdp->vendor_data[i]);
275 }
276 s += sprintf(s, ")");
277 break;
278 }
279 case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
280 struct efi_device_path_file_path *fp =
281 (struct efi_device_path_file_path *)dp;
282 int slen = (dp->length - sizeof(*dp)) / 2;
283 if (slen > MAX_NODE_LEN - 2)
284 slen = MAX_NODE_LEN - 2;
285 s += sprintf(s, "%-.*ls", slen, fp->str);
286 break;
287 }
288 default:
289 s = dp_unknown(s, dp);
290 break;
291 }
292 return s;
293 }
294
295 /*
296 * Converts a single node to a char string.
297 *
298 * @buffer output buffer
299 * @dp device path or node
300 * @return end of string
301 */
efi_convert_single_device_node_to_text(char * buffer,struct efi_device_path * dp)302 static char *efi_convert_single_device_node_to_text(
303 char *buffer,
304 struct efi_device_path *dp)
305 {
306 char *str = buffer;
307
308 switch (dp->type) {
309 case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
310 str = dp_hardware(str, dp);
311 break;
312 case DEVICE_PATH_TYPE_ACPI_DEVICE:
313 str = dp_acpi(str, dp);
314 break;
315 case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
316 str = dp_msging(str, dp);
317 break;
318 case DEVICE_PATH_TYPE_MEDIA_DEVICE:
319 str = dp_media(str, dp);
320 break;
321 case DEVICE_PATH_TYPE_END:
322 break;
323 default:
324 str = dp_unknown(str, dp);
325 }
326
327 *str = '\0';
328 return str;
329 }
330
331 /*
332 * This function implements the ConvertDeviceNodeToText service of the
333 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
334 * See the Unified Extensible Firmware Interface (UEFI) specification
335 * for details.
336 *
337 * device_node device node to be converted
338 * display_only true if the shorter text representation shall be used
339 * allow_shortcuts true if shortcut forms may be used
340 * @return text representation of the device path
341 * NULL if out of memory of device_path is NULL
342 */
efi_convert_device_node_to_text(struct efi_device_path * device_node,bool display_only,bool allow_shortcuts)343 static uint16_t EFIAPI *efi_convert_device_node_to_text(
344 struct efi_device_path *device_node,
345 bool display_only,
346 bool allow_shortcuts)
347 {
348 char str[MAX_NODE_LEN];
349 uint16_t *text = NULL;
350
351 EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
352
353 if (!device_node)
354 goto out;
355 efi_convert_single_device_node_to_text(str, device_node);
356
357 text = efi_str_to_u16(str);
358
359 out:
360 EFI_EXIT(EFI_SUCCESS);
361 return text;
362 }
363
364 /*
365 * This function implements the ConvertDevicePathToText service of the
366 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
367 * See the Unified Extensible Firmware Interface (UEFI) specification
368 * for details.
369 *
370 * device_path device path to be converted
371 * display_only true if the shorter text representation shall be used
372 * allow_shortcuts true if shortcut forms may be used
373 * @return text representation of the device path
374 * NULL if out of memory of device_path is NULL
375 */
efi_convert_device_path_to_text(struct efi_device_path * device_path,bool display_only,bool allow_shortcuts)376 static uint16_t EFIAPI *efi_convert_device_path_to_text(
377 struct efi_device_path *device_path,
378 bool display_only,
379 bool allow_shortcuts)
380 {
381 uint16_t *text = NULL;
382 char buffer[MAX_PATH_LEN];
383 char *str = buffer;
384
385 EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
386
387 if (!device_path)
388 goto out;
389 while (device_path && str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
390 if (device_path->type == DEVICE_PATH_TYPE_END) {
391 if (device_path->sub_type !=
392 DEVICE_PATH_SUB_TYPE_INSTANCE_END)
393 break;
394 *str++ = ',';
395 } else {
396 *str++ = '/';
397 str = efi_convert_single_device_node_to_text(
398 str, device_path);
399 }
400 *(u8 **)&device_path += device_path->length;
401 }
402
403 text = efi_str_to_u16(buffer);
404
405 out:
406 EFI_EXIT(EFI_SUCCESS);
407 return text;
408 }
409
410 /* helper for debug prints.. efi_free_pool() the result. */
efi_dp_str(struct efi_device_path * dp)411 uint16_t *efi_dp_str(struct efi_device_path *dp)
412 {
413 return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
414 }
415
416 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
417 .convert_device_node_to_text = efi_convert_device_node_to_text,
418 .convert_device_path_to_text = efi_convert_device_path_to_text,
419 };
420