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