efi_loader: comments for EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
[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_USB: {
91                 struct efi_device_path_usb *udp =
92                         (struct efi_device_path_usb *)dp;
93                 s += sprintf(s, "Usb(0x%x,0x%x)", udp->parent_port_number,
94                              udp->usb_interface);
95                 break;
96         }
97         case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
98                 struct efi_device_path_mac_addr *mdp =
99                         (struct efi_device_path_mac_addr *)dp;
100
101                 if (mdp->if_type != 0 && mdp->if_type != 1)
102                         break;
103
104                 s += sprintf(s, "MAC(%02x%02x%02x%02x%02x%02x,0x%1x)",
105                         mdp->mac.addr[0], mdp->mac.addr[1],
106                         mdp->mac.addr[2], mdp->mac.addr[3],
107                         mdp->mac.addr[4], mdp->mac.addr[5],
108                         mdp->if_type);
109
110                 break;
111         }
112         case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
113                 struct efi_device_path_usb_class *ucdp =
114                         (struct efi_device_path_usb_class *)dp;
115
116                 s += sprintf(s, "USBClass(%x,%x,%x,%x,%x)",
117                         ucdp->vendor_id, ucdp->product_id,
118                         ucdp->device_class, ucdp->device_subclass,
119                         ucdp->device_protocol);
120
121                 break;
122         }
123         case DEVICE_PATH_SUB_TYPE_MSG_SD:
124         case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
125                 const char *typename =
126                         (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
127                                         "SDCard" : "MMC";
128                 struct efi_device_path_sd_mmc_path *sddp =
129                         (struct efi_device_path_sd_mmc_path *)dp;
130                 s += sprintf(s, "%s(Slot%u)", typename, sddp->slot_number);
131                 break;
132         }
133         default:
134                 s = dp_unknown(s, dp);
135                 break;
136         }
137         return s;
138 }
139
140 static char *dp_media(char *s, struct efi_device_path *dp)
141 {
142         switch (dp->sub_type) {
143         case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
144                 struct efi_device_path_hard_drive_path *hddp =
145                         (struct efi_device_path_hard_drive_path *)dp;
146                 void *sig = hddp->partition_signature;
147
148                 switch (hddp->signature_type) {
149                 case SIG_TYPE_MBR:
150                         s += sprintf(s, "HD(Part%d,Sig%08x)",
151                                      hddp->partition_number,
152                                      *(uint32_t *)sig);
153                         break;
154                 case SIG_TYPE_GUID:
155                         s += sprintf(s, "HD(Part%d,Sig%pUl)",
156                                      hddp->partition_number, sig);
157                 default:
158                         s += sprintf(s, "HD(Part%d,MBRType=%02x,SigType=%02x)",
159                                      hddp->partition_number, hddp->partmap_type,
160                                      hddp->signature_type);
161                 }
162
163                 break;
164         }
165         case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
166                 struct efi_device_path_cdrom_path *cddp =
167                         (struct efi_device_path_cdrom_path *)dp;
168                 s += sprintf(s, "CDROM(0x%x)", cddp->boot_entry);
169                 break;
170         }
171         case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
172                 struct efi_device_path_file_path *fp =
173                         (struct efi_device_path_file_path *)dp;
174                 int slen = (dp->length - sizeof(*dp)) / 2;
175                 if (slen > MAX_NODE_LEN - 2)
176                         slen = MAX_NODE_LEN - 2;
177                 s += sprintf(s, "%-.*ls", slen, fp->str);
178                 break;
179         }
180         default:
181                 s = dp_unknown(s, dp);
182                 break;
183         }
184         return s;
185 }
186
187 /*
188  * Converts a single node to a char string.
189  *
190  * @buffer              output buffer
191  * @dp                  device path or node
192  * @return              end of string
193  */
194 static char *efi_convert_single_device_node_to_text(
195                 char *buffer,
196                 struct efi_device_path *dp)
197 {
198         char *str = buffer;
199
200         switch (dp->type) {
201         case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
202                 str = dp_hardware(str, dp);
203                 break;
204         case DEVICE_PATH_TYPE_ACPI_DEVICE:
205                 str = dp_acpi(str, dp);
206                 break;
207         case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
208                 str = dp_msging(str, dp);
209                 break;
210         case DEVICE_PATH_TYPE_MEDIA_DEVICE:
211                 str = dp_media(str, dp);
212                 break;
213         default:
214                 str = dp_unknown(str, dp);
215         }
216
217         *str = '\0';
218         return str;
219 }
220
221 /*
222  * This function implements the ConvertDeviceNodeToText service of the
223  * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
224  * See the Unified Extensible Firmware Interface (UEFI) specification
225  * for details.
226  *
227  * device_node          device node to be converted
228  * display_only         true if the shorter text represenation shall be used
229  * allow_shortcuts      true if shortcut forms may be used
230  * @return              text represenation of the device path
231  *                      NULL if out of memory of device_path is NULL
232  */
233 static uint16_t EFIAPI *efi_convert_device_node_to_text(
234                 struct efi_device_path *device_node,
235                 bool display_only,
236                 bool allow_shortcuts)
237 {
238         char str[MAX_NODE_LEN];
239         uint16_t *text = NULL;
240
241         EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
242
243         if (!device_node)
244                 goto out;
245         efi_convert_single_device_node_to_text(str, device_node);
246
247         text = efi_str_to_u16(str);
248
249 out:
250         EFI_EXIT(EFI_SUCCESS);
251         return text;
252 }
253
254 /*
255  * This function implements the ConvertDevicePathToText service of the
256  * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
257  * See the Unified Extensible Firmware Interface (UEFI) specification
258  * for details.
259  *
260  * device_path          device path to be converted
261  * display_only         true if the shorter text represenation shall be used
262  * allow_shortcuts      true if shortcut forms may be used
263  * @return              text represenation of the device path
264  *                      NULL if out of memory of device_path is NULL
265  */
266 static uint16_t EFIAPI *efi_convert_device_path_to_text(
267                 struct efi_device_path *device_path,
268                 bool display_only,
269                 bool allow_shortcuts)
270 {
271         uint16_t *text = NULL;
272         char buffer[MAX_PATH_LEN];
273         char *str = buffer;
274
275         EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
276
277         if (!device_path)
278                 goto out;
279         while (device_path &&
280                str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
281                 *str++ = '/';
282                 str = efi_convert_single_device_node_to_text(str, device_path);
283                 device_path = efi_dp_next(device_path);
284         }
285
286         text = efi_str_to_u16(buffer);
287
288 out:
289         EFI_EXIT(EFI_SUCCESS);
290         return text;
291 }
292
293 /* helper for debug prints.. efi_free_pool() the result. */
294 uint16_t *efi_dp_str(struct efi_device_path *dp)
295 {
296         return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
297 }
298
299 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
300         .convert_device_node_to_text = efi_convert_device_node_to_text,
301         .convert_device_path_to_text = efi_convert_device_path_to_text,
302 };