Merge tag 'xilinx-for-v2021.04-rc3' of https://gitlab.denx.de/u-boot/custodians/u...
[platform/kernel/u-boot.git] / lib / efi_loader / efi_device_path_to_text.c
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  */
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
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
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                 if (n > 0) {
71                         s += sprintf(s, ",");
72                         for (i = 0; i < n; ++i)
73                                 s += sprintf(s, "%02x", vdp->vendor_data[i]);
74                 }
75                 s += sprintf(s, ")");
76                 break;
77         }
78         default:
79                 s = dp_unknown(s, dp);
80                 break;
81         }
82         return s;
83 }
84
85 static char *dp_acpi(char *s, struct efi_device_path *dp)
86 {
87         switch (dp->sub_type) {
88         case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE: {
89                 struct efi_device_path_acpi_path *adp =
90                         (struct efi_device_path_acpi_path *)dp;
91
92                 s += sprintf(s, "Acpi(PNP%04X,%d)", EISA_PNP_NUM(adp->hid),
93                              adp->uid);
94                 break;
95         }
96         default:
97                 s = dp_unknown(s, dp);
98                 break;
99         }
100         return s;
101 }
102
103 static char *dp_msging(char *s, struct efi_device_path *dp)
104 {
105         switch (dp->sub_type) {
106         case DEVICE_PATH_SUB_TYPE_MSG_ATAPI: {
107                 struct efi_device_path_atapi *ide =
108                         (struct efi_device_path_atapi *)dp;
109                 s += sprintf(s, "Ata(%d,%d,%d)", ide->primary_secondary,
110                              ide->slave_master, ide->logical_unit_number);
111                 break;
112         }
113         case DEVICE_PATH_SUB_TYPE_MSG_SCSI: {
114                 struct efi_device_path_scsi *ide =
115                         (struct efi_device_path_scsi *)dp;
116                 s += sprintf(s, "Scsi(%u,%u)", ide->target_id,
117                              ide->logical_unit_number);
118                 break;
119         }
120         case DEVICE_PATH_SUB_TYPE_MSG_USB: {
121                 struct efi_device_path_usb *udp =
122                         (struct efi_device_path_usb *)dp;
123                 s += sprintf(s, "USB(0x%x,0x%x)", udp->parent_port_number,
124                              udp->usb_interface);
125                 break;
126         }
127         case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
128                 int i, n = sizeof(struct efi_mac_addr);
129                 struct efi_device_path_mac_addr *mdp =
130                         (struct efi_device_path_mac_addr *)dp;
131
132                 if (mdp->if_type <= 1)
133                         n = 6;
134                 s += sprintf(s, "MAC(");
135                 for (i = 0; i < n; ++i)
136                         s += sprintf(s, "%02x", mdp->mac.addr[i]);
137                 s += sprintf(s, ",%u)", mdp->if_type);
138
139                 break;
140         }
141         case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
142                 struct efi_device_path_usb_class *ucdp =
143                         (struct efi_device_path_usb_class *)dp;
144
145                 s += sprintf(s, "UsbClass(0x%x,0x%x,0x%x,0x%x,0x%x)",
146                         ucdp->vendor_id, ucdp->product_id,
147                         ucdp->device_class, ucdp->device_subclass,
148                         ucdp->device_protocol);
149
150                 break;
151         }
152         case DEVICE_PATH_SUB_TYPE_MSG_SATA: {
153                 struct efi_device_path_sata *sdp =
154                         (struct efi_device_path_sata *) dp;
155
156                 s += sprintf(s, "Sata(0x%x,0x%x,0x%x)",
157                              sdp->hba_port,
158                              sdp->port_multiplier_port,
159                              sdp->logical_unit_number);
160                 break;
161         }
162         case DEVICE_PATH_SUB_TYPE_MSG_NVME: {
163                 struct efi_device_path_nvme *ndp =
164                         (struct efi_device_path_nvme *)dp;
165                 u32 ns_id;
166                 int i;
167
168                 memcpy(&ns_id, &ndp->ns_id, sizeof(ns_id));
169                 s += sprintf(s, "NVMe(0x%x,", ns_id);
170                 for (i = 0; i < sizeof(ndp->eui64); ++i)
171                         s += sprintf(s, "%s%02x", i ? "-" : "",
172                                      ndp->eui64[i]);
173                 s += sprintf(s, ")");
174
175                 break;
176         }
177         case DEVICE_PATH_SUB_TYPE_MSG_SD:
178         case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
179                 const char *typename =
180                         (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
181                                         "SD" : "eMMC";
182                 struct efi_device_path_sd_mmc_path *sddp =
183                         (struct efi_device_path_sd_mmc_path *)dp;
184                 s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
185                 break;
186         }
187         default:
188                 s = dp_unknown(s, dp);
189                 break;
190         }
191         return s;
192 }
193
194 /*
195  * Convert a media device path node to text.
196  *
197  * @s           output buffer
198  * @dp          device path node
199  * @return      next unused buffer address
200  */
201 static char *dp_media(char *s, struct efi_device_path *dp)
202 {
203         switch (dp->sub_type) {
204         case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
205                 struct efi_device_path_hard_drive_path *hddp =
206                         (struct efi_device_path_hard_drive_path *)dp;
207                 void *sig = hddp->partition_signature;
208                 u64 start;
209                 u64 end;
210
211                 /* Copy from packed structure to aligned memory */
212                 memcpy(&start, &hddp->partition_start, sizeof(start));
213                 memcpy(&end, &hddp->partition_end, sizeof(end));
214
215                 switch (hddp->signature_type) {
216                 case SIG_TYPE_MBR: {
217                         u32 signature;
218
219                         memcpy(&signature, sig, sizeof(signature));
220                         s += sprintf(
221                                 s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
222                                 hddp->partition_number, signature, start, end);
223                         break;
224                         }
225                 case SIG_TYPE_GUID:
226                         s += sprintf(
227                                 s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
228                                 hddp->partition_number, sig, start, end);
229                         break;
230                 default:
231                         s += sprintf(
232                                 s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
233                                 hddp->partition_number, hddp->partmap_type,
234                                 start, end);
235                         break;
236                 }
237
238                 break;
239         }
240         case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
241                 struct efi_device_path_cdrom_path *cddp =
242                         (struct efi_device_path_cdrom_path *)dp;
243                 s += sprintf(s, "CDROM(%u,0x%llx,0x%llx)", cddp->boot_entry,
244                              cddp->partition_start, cddp->partition_size);
245                 break;
246         }
247         case DEVICE_PATH_SUB_TYPE_VENDOR_PATH: {
248                 int i, n;
249                 struct efi_device_path_vendor *vdp =
250                         (struct efi_device_path_vendor *)dp;
251
252                 s += sprintf(s, "VenMedia(%pUl", &vdp->guid);
253                 n = (int)vdp->dp.length - sizeof(struct efi_device_path_vendor);
254                 if (n > 0) {
255                         s += sprintf(s, ",");
256                         for (i = 0; i < n; ++i)
257                                 s += sprintf(s, "%02x", vdp->vendor_data[i]);
258                 }
259                 s += sprintf(s, ")");
260                 break;
261         }
262         case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
263                 struct efi_device_path_file_path *fp =
264                         (struct efi_device_path_file_path *)dp;
265                 int slen = (dp->length - sizeof(*dp)) / 2;
266                 if (slen > MAX_NODE_LEN - 2)
267                         slen = MAX_NODE_LEN - 2;
268                 s += sprintf(s, "%-.*ls", slen, fp->str);
269                 break;
270         }
271         default:
272                 s = dp_unknown(s, dp);
273                 break;
274         }
275         return s;
276 }
277
278 /*
279  * Converts a single node to a char string.
280  *
281  * @buffer              output buffer
282  * @dp                  device path or node
283  * @return              end of string
284  */
285 static char *efi_convert_single_device_node_to_text(
286                 char *buffer,
287                 struct efi_device_path *dp)
288 {
289         char *str = buffer;
290
291         switch (dp->type) {
292         case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
293                 str = dp_hardware(str, dp);
294                 break;
295         case DEVICE_PATH_TYPE_ACPI_DEVICE:
296                 str = dp_acpi(str, dp);
297                 break;
298         case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
299                 str = dp_msging(str, dp);
300                 break;
301         case DEVICE_PATH_TYPE_MEDIA_DEVICE:
302                 str = dp_media(str, dp);
303                 break;
304         case DEVICE_PATH_TYPE_END:
305                 break;
306         default:
307                 str = dp_unknown(str, dp);
308         }
309
310         *str = '\0';
311         return str;
312 }
313
314 /*
315  * This function implements the ConvertDeviceNodeToText service of the
316  * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
317  * See the Unified Extensible Firmware Interface (UEFI) specification
318  * for details.
319  *
320  * device_node          device node to be converted
321  * display_only         true if the shorter text representation shall be used
322  * allow_shortcuts      true if shortcut forms may be used
323  * @return              text representation of the device path
324  *                      NULL if out of memory of device_path is NULL
325  */
326 static uint16_t EFIAPI *efi_convert_device_node_to_text(
327                 struct efi_device_path *device_node,
328                 bool display_only,
329                 bool allow_shortcuts)
330 {
331         char str[MAX_NODE_LEN];
332         uint16_t *text = NULL;
333
334         EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
335
336         if (!device_node)
337                 goto out;
338         efi_convert_single_device_node_to_text(str, device_node);
339
340         text = efi_str_to_u16(str);
341
342 out:
343         EFI_EXIT(EFI_SUCCESS);
344         return text;
345 }
346
347 /*
348  * This function implements the ConvertDevicePathToText service of the
349  * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
350  * See the Unified Extensible Firmware Interface (UEFI) specification
351  * for details.
352  *
353  * device_path          device path to be converted
354  * display_only         true if the shorter text representation shall be used
355  * allow_shortcuts      true if shortcut forms may be used
356  * @return              text representation of the device path
357  *                      NULL if out of memory of device_path is NULL
358  */
359 static uint16_t EFIAPI *efi_convert_device_path_to_text(
360                 struct efi_device_path *device_path,
361                 bool display_only,
362                 bool allow_shortcuts)
363 {
364         uint16_t *text = NULL;
365         char buffer[MAX_PATH_LEN];
366         char *str = buffer;
367
368         EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
369
370         if (!device_path)
371                 goto out;
372         while (device_path && str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
373                 if (device_path->type == DEVICE_PATH_TYPE_END) {
374                         if (device_path->sub_type !=
375                             DEVICE_PATH_SUB_TYPE_INSTANCE_END)
376                                 break;
377                         *str++ = ',';
378                 } else {
379                         *str++ = '/';
380                         str = efi_convert_single_device_node_to_text(
381                                                         str, device_path);
382                 }
383                 *(u8 **)&device_path += device_path->length;
384         }
385
386         text = efi_str_to_u16(buffer);
387
388 out:
389         EFI_EXIT(EFI_SUCCESS);
390         return text;
391 }
392
393 /* helper for debug prints.. efi_free_pool() the result. */
394 uint16_t *efi_dp_str(struct efi_device_path *dp)
395 {
396         return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
397 }
398
399 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
400         .convert_device_node_to_text = efi_convert_device_node_to_text,
401         .convert_device_path_to_text = efi_convert_device_path_to_text,
402 };