spi: zynqmp_gqspi: fix set_speed bug on multiple runs
[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_FILE_PATH: {
248                 struct efi_device_path_file_path *fp =
249                         (struct efi_device_path_file_path *)dp;
250                 int slen = (dp->length - sizeof(*dp)) / 2;
251                 if (slen > MAX_NODE_LEN - 2)
252                         slen = MAX_NODE_LEN - 2;
253                 s += sprintf(s, "%-.*ls", slen, fp->str);
254                 break;
255         }
256         default:
257                 s = dp_unknown(s, dp);
258                 break;
259         }
260         return s;
261 }
262
263 /*
264  * Converts a single node to a char string.
265  *
266  * @buffer              output buffer
267  * @dp                  device path or node
268  * @return              end of string
269  */
270 static char *efi_convert_single_device_node_to_text(
271                 char *buffer,
272                 struct efi_device_path *dp)
273 {
274         char *str = buffer;
275
276         switch (dp->type) {
277         case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
278                 str = dp_hardware(str, dp);
279                 break;
280         case DEVICE_PATH_TYPE_ACPI_DEVICE:
281                 str = dp_acpi(str, dp);
282                 break;
283         case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
284                 str = dp_msging(str, dp);
285                 break;
286         case DEVICE_PATH_TYPE_MEDIA_DEVICE:
287                 str = dp_media(str, dp);
288                 break;
289         case DEVICE_PATH_TYPE_END:
290                 break;
291         default:
292                 str = dp_unknown(str, dp);
293         }
294
295         *str = '\0';
296         return str;
297 }
298
299 /*
300  * This function implements the ConvertDeviceNodeToText service of the
301  * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
302  * See the Unified Extensible Firmware Interface (UEFI) specification
303  * for details.
304  *
305  * device_node          device node to be converted
306  * display_only         true if the shorter text representation shall be used
307  * allow_shortcuts      true if shortcut forms may be used
308  * @return              text representation of the device path
309  *                      NULL if out of memory of device_path is NULL
310  */
311 static uint16_t EFIAPI *efi_convert_device_node_to_text(
312                 struct efi_device_path *device_node,
313                 bool display_only,
314                 bool allow_shortcuts)
315 {
316         char str[MAX_NODE_LEN];
317         uint16_t *text = NULL;
318
319         EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
320
321         if (!device_node)
322                 goto out;
323         efi_convert_single_device_node_to_text(str, device_node);
324
325         text = efi_str_to_u16(str);
326
327 out:
328         EFI_EXIT(EFI_SUCCESS);
329         return text;
330 }
331
332 /*
333  * This function implements the ConvertDevicePathToText service of the
334  * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
335  * See the Unified Extensible Firmware Interface (UEFI) specification
336  * for details.
337  *
338  * device_path          device path to be converted
339  * display_only         true if the shorter text representation shall be used
340  * allow_shortcuts      true if shortcut forms may be used
341  * @return              text representation of the device path
342  *                      NULL if out of memory of device_path is NULL
343  */
344 static uint16_t EFIAPI *efi_convert_device_path_to_text(
345                 struct efi_device_path *device_path,
346                 bool display_only,
347                 bool allow_shortcuts)
348 {
349         uint16_t *text = NULL;
350         char buffer[MAX_PATH_LEN];
351         char *str = buffer;
352
353         EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
354
355         if (!device_path)
356                 goto out;
357         while (device_path &&
358                str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
359                 *str++ = '/';
360                 str = efi_convert_single_device_node_to_text(str, device_path);
361                 device_path = efi_dp_next(device_path);
362         }
363
364         text = efi_str_to_u16(buffer);
365
366 out:
367         EFI_EXIT(EFI_SUCCESS);
368         return text;
369 }
370
371 /* helper for debug prints.. efi_free_pool() the result. */
372 uint16_t *efi_dp_str(struct efi_device_path *dp)
373 {
374         return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
375 }
376
377 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
378         .convert_device_node_to_text = efi_convert_device_node_to_text,
379         .convert_device_path_to_text = efi_convert_device_path_to_text,
380 };