6e27508fba4ef9167923a2bef8f4e33b93124d1d
[oweals/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 <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  */
29 static u16 *efi_str_to_u16(char *str)
30 {
31         efi_uintn_t len;
32         u16 *out;
33         efi_status_t ret;
34
35         len = strlen(str) + 1;
36         ret = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, len * sizeof(u16),
37                                 (void **)&out);
38         if (ret != EFI_SUCCESS)
39                 return NULL;
40         ascii2unicode(out, str);
41         return out;
42 }
43
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
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
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
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(%x,%x,%x,%x,%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_SD:
152         case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
153                 const char *typename =
154                         (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
155                                         "SD" : "eMMC";
156                 struct efi_device_path_sd_mmc_path *sddp =
157                         (struct efi_device_path_sd_mmc_path *)dp;
158                 s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
159                 break;
160         }
161         default:
162                 s = dp_unknown(s, dp);
163                 break;
164         }
165         return s;
166 }
167
168 /*
169  * Convert a media device path node to text.
170  *
171  * @s           output buffer
172  * @dp          device path node
173  * @return      next unused buffer address
174  */
175 static char *dp_media(char *s, struct efi_device_path *dp)
176 {
177         switch (dp->sub_type) {
178         case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
179                 struct efi_device_path_hard_drive_path *hddp =
180                         (struct efi_device_path_hard_drive_path *)dp;
181                 void *sig = hddp->partition_signature;
182                 u64 start;
183                 u64 end;
184
185                 /* Copy from packed structure to aligned memory */
186                 memcpy(&start, &hddp->partition_start, sizeof(start));
187                 memcpy(&end, &hddp->partition_end, sizeof(end));
188
189                 switch (hddp->signature_type) {
190                 case SIG_TYPE_MBR: {
191                         u32 signature;
192
193                         memcpy(&signature, sig, sizeof(signature));
194                         s += sprintf(
195                                 s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
196                                 hddp->partition_number, signature, start, end);
197                         break;
198                         }
199                 case SIG_TYPE_GUID:
200                         s += sprintf(
201                                 s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
202                                 hddp->partition_number, sig, start, end);
203                         break;
204                 default:
205                         s += sprintf(
206                                 s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
207                                 hddp->partition_number, hddp->partmap_type,
208                                 start, end);
209                         break;
210                 }
211
212                 break;
213         }
214         case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
215                 struct efi_device_path_cdrom_path *cddp =
216                         (struct efi_device_path_cdrom_path *)dp;
217                 s += sprintf(s, "CDROM(%u,0x%llx,0x%llx)", cddp->boot_entry,
218                              cddp->partition_start, cddp->partition_size);
219                 break;
220         }
221         case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
222                 struct efi_device_path_file_path *fp =
223                         (struct efi_device_path_file_path *)dp;
224                 int slen = (dp->length - sizeof(*dp)) / 2;
225                 if (slen > MAX_NODE_LEN - 2)
226                         slen = MAX_NODE_LEN - 2;
227                 s += sprintf(s, "%-.*ls", slen, fp->str);
228                 break;
229         }
230         default:
231                 s = dp_unknown(s, dp);
232                 break;
233         }
234         return s;
235 }
236
237 /*
238  * Converts a single node to a char string.
239  *
240  * @buffer              output buffer
241  * @dp                  device path or node
242  * @return              end of string
243  */
244 static char *efi_convert_single_device_node_to_text(
245                 char *buffer,
246                 struct efi_device_path *dp)
247 {
248         char *str = buffer;
249
250         switch (dp->type) {
251         case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
252                 str = dp_hardware(str, dp);
253                 break;
254         case DEVICE_PATH_TYPE_ACPI_DEVICE:
255                 str = dp_acpi(str, dp);
256                 break;
257         case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
258                 str = dp_msging(str, dp);
259                 break;
260         case DEVICE_PATH_TYPE_MEDIA_DEVICE:
261                 str = dp_media(str, dp);
262                 break;
263         case DEVICE_PATH_TYPE_END:
264                 break;
265         default:
266                 str = dp_unknown(str, dp);
267         }
268
269         *str = '\0';
270         return str;
271 }
272
273 /*
274  * This function implements the ConvertDeviceNodeToText service of the
275  * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
276  * See the Unified Extensible Firmware Interface (UEFI) specification
277  * for details.
278  *
279  * device_node          device node to be converted
280  * display_only         true if the shorter text representation shall be used
281  * allow_shortcuts      true if shortcut forms may be used
282  * @return              text representation of the device path
283  *                      NULL if out of memory of device_path is NULL
284  */
285 static uint16_t EFIAPI *efi_convert_device_node_to_text(
286                 struct efi_device_path *device_node,
287                 bool display_only,
288                 bool allow_shortcuts)
289 {
290         char str[MAX_NODE_LEN];
291         uint16_t *text = NULL;
292
293         EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
294
295         if (!device_node)
296                 goto out;
297         efi_convert_single_device_node_to_text(str, device_node);
298
299         text = efi_str_to_u16(str);
300
301 out:
302         EFI_EXIT(EFI_SUCCESS);
303         return text;
304 }
305
306 /*
307  * This function implements the ConvertDevicePathToText service of the
308  * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
309  * See the Unified Extensible Firmware Interface (UEFI) specification
310  * for details.
311  *
312  * device_path          device path to be converted
313  * display_only         true if the shorter text representation shall be used
314  * allow_shortcuts      true if shortcut forms may be used
315  * @return              text representation of the device path
316  *                      NULL if out of memory of device_path is NULL
317  */
318 static uint16_t EFIAPI *efi_convert_device_path_to_text(
319                 struct efi_device_path *device_path,
320                 bool display_only,
321                 bool allow_shortcuts)
322 {
323         uint16_t *text = NULL;
324         char buffer[MAX_PATH_LEN];
325         char *str = buffer;
326
327         EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
328
329         if (!device_path)
330                 goto out;
331         while (device_path &&
332                str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
333                 *str++ = '/';
334                 str = efi_convert_single_device_node_to_text(str, device_path);
335                 device_path = efi_dp_next(device_path);
336         }
337
338         text = efi_str_to_u16(buffer);
339
340 out:
341         EFI_EXIT(EFI_SUCCESS);
342         return text;
343 }
344
345 /* helper for debug prints.. efi_free_pool() the result. */
346 uint16_t *efi_dp_str(struct efi_device_path *dp)
347 {
348         return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
349 }
350
351 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
352         .convert_device_node_to_text = efi_convert_device_node_to_text,
353         .convert_device_path_to_text = efi_convert_device_path_to_text,
354 };