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