]> git.sur5r.net Git - u-boot/blob - lib/efi_loader/efi_device_path_to_text.c
Merge git://git.denx.de/u-boot-usb
[u-boot] / 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 const efi_guid_t efi_guid_device_path_to_text_protocol =
16                 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
17
18 static char *dp_unknown(char *s, struct efi_device_path *dp)
19 {
20         s += sprintf(s, "/UNKNOWN(%04x,%04x)", dp->type, dp->sub_type);
21         return s;
22 }
23
24 static char *dp_hardware(char *s, struct efi_device_path *dp)
25 {
26         switch (dp->sub_type) {
27         case DEVICE_PATH_SUB_TYPE_VENDOR: {
28                 struct efi_device_path_vendor *vdp =
29                         (struct efi_device_path_vendor *)dp;
30                 s += sprintf(s, "/VenHw(%pUl)", &vdp->guid);
31                 break;
32         }
33         default:
34                 s = dp_unknown(s, dp);
35                 break;
36         }
37         return s;
38 }
39
40 static char *dp_acpi(char *s, struct efi_device_path *dp)
41 {
42         switch (dp->sub_type) {
43         case DEVICE_PATH_SUB_TYPE_ACPI_DEVICE: {
44                 struct efi_device_path_acpi_path *adp =
45                         (struct efi_device_path_acpi_path *)dp;
46                 s += sprintf(s, "/Acpi(PNP%04x", EISA_PNP_NUM(adp->hid));
47                 if (adp->uid)
48                         s += sprintf(s, ",%d", adp->uid);
49                 s += sprintf(s, ")");
50                 break;
51         }
52         default:
53                 s = dp_unknown(s, dp);
54                 break;
55         }
56         return s;
57 }
58
59 static char *dp_msging(char *s, struct efi_device_path *dp)
60 {
61         switch (dp->sub_type) {
62         case DEVICE_PATH_SUB_TYPE_MSG_USB: {
63                 struct efi_device_path_usb *udp =
64                         (struct efi_device_path_usb *)dp;
65                 s += sprintf(s, "/Usb(0x%x,0x%x)", udp->parent_port_number,
66                              udp->usb_interface);
67                 break;
68         }
69         case DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR: {
70                 struct efi_device_path_mac_addr *mdp =
71                         (struct efi_device_path_mac_addr *)dp;
72
73                 if (mdp->if_type != 0 && mdp->if_type != 1)
74                         break;
75
76                 s += sprintf(s, "/MAC(%02x%02x%02x%02x%02x%02x,0x%1x)",
77                         mdp->mac.addr[0], mdp->mac.addr[1],
78                         mdp->mac.addr[2], mdp->mac.addr[3],
79                         mdp->mac.addr[4], mdp->mac.addr[5],
80                         mdp->if_type);
81
82                 break;
83         }
84         case DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS: {
85                 struct efi_device_path_usb_class *ucdp =
86                         (struct efi_device_path_usb_class *)dp;
87
88                 s += sprintf(s, "/USBClass(%x,%x,%x,%x,%x)",
89                         ucdp->vendor_id, ucdp->product_id,
90                         ucdp->device_class, ucdp->device_subclass,
91                         ucdp->device_protocol);
92
93                 break;
94         }
95         case DEVICE_PATH_SUB_TYPE_MSG_SD:
96         case DEVICE_PATH_SUB_TYPE_MSG_MMC: {
97                 const char *typename =
98                         (dp->sub_type == DEVICE_PATH_SUB_TYPE_MSG_SD) ?
99                                         "SDCard" : "MMC";
100                 struct efi_device_path_sd_mmc_path *sddp =
101                         (struct efi_device_path_sd_mmc_path *)dp;
102                 s += sprintf(s, "/%s(Slot%u)", typename, sddp->slot_number);
103                 break;
104         }
105         default:
106                 s = dp_unknown(s, dp);
107                 break;
108         }
109         return s;
110 }
111
112 static char *dp_media(char *s, struct efi_device_path *dp)
113 {
114         switch (dp->sub_type) {
115         case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
116                 struct efi_device_path_hard_drive_path *hddp =
117                         (struct efi_device_path_hard_drive_path *)dp;
118                 void *sig = hddp->partition_signature;
119
120                 switch (hddp->signature_type) {
121                 case SIG_TYPE_MBR:
122                         s += sprintf(s, "/HD(Part%d,Sig%08x)",
123                                      hddp->partition_number,
124                                      *(uint32_t *)sig);
125                         break;
126                 case SIG_TYPE_GUID:
127                         s += sprintf(s, "/HD(Part%d,Sig%pUl)",
128                                      hddp->partition_number, sig);
129                 default:
130                         s += sprintf(s, "/HD(Part%d,MBRType=%02x,SigType=%02x)",
131                                      hddp->partition_number, hddp->partmap_type,
132                                      hddp->signature_type);
133                 }
134
135                 break;
136         }
137         case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
138                 struct efi_device_path_cdrom_path *cddp =
139                         (struct efi_device_path_cdrom_path *)dp;
140                 s += sprintf(s, "/CDROM(0x%x)", cddp->boot_entry);
141                 break;
142         }
143         case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
144                 struct efi_device_path_file_path *fp =
145                         (struct efi_device_path_file_path *)dp;
146                 int slen = (dp->length - sizeof(*dp)) / 2;
147                 s += sprintf(s, "/%-*ls", slen, fp->str);
148                 break;
149         }
150         default:
151                 s = dp_unknown(s, dp);
152                 break;
153         }
154         return s;
155 }
156
157 static uint16_t *efi_convert_device_node_to_text(
158                 struct efi_device_path *dp,
159                 bool display_only,
160                 bool allow_shortcuts)
161 {
162         unsigned long len;
163         efi_status_t r;
164         char buf[512];  /* this ought be be big enough for worst case */
165         char *str = buf;
166         uint16_t *out;
167
168         while (dp) {
169                 switch (dp->type) {
170                 case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
171                         str = dp_hardware(str, dp);
172                         break;
173                 case DEVICE_PATH_TYPE_ACPI_DEVICE:
174                         str = dp_acpi(str, dp);
175                         break;
176                 case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
177                         str = dp_msging(str, dp);
178                         break;
179                 case DEVICE_PATH_TYPE_MEDIA_DEVICE:
180                         str = dp_media(str, dp);
181                         break;
182                 default:
183                         str = dp_unknown(str, dp);
184                 }
185
186                 dp = efi_dp_next(dp);
187         }
188
189         *str++ = '\0';
190
191         len = str - buf;
192         r = efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, 2 * len, (void **)&out);
193         if (r != EFI_SUCCESS)
194                 return NULL;
195
196         ascii2unicode(out, buf);
197         out[len - 1] = 0;
198
199         return out;
200 }
201
202 /* helper for debug prints.. efi_free_pool() the result. */
203 uint16_t *efi_dp_str(struct efi_device_path *dp)
204 {
205         return efi_convert_device_node_to_text(dp, true, true);
206 }
207
208
209 static uint16_t EFIAPI *efi_convert_device_node_to_text_ext(
210                 struct efi_device_path *device_node,
211                 bool display_only,
212                 bool allow_shortcuts)
213 {
214         uint16_t *buffer;
215
216         EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
217
218         buffer = efi_convert_device_node_to_text(device_node, display_only,
219                                                  allow_shortcuts);
220
221         EFI_EXIT(EFI_SUCCESS);
222         return buffer;
223 }
224
225 static uint16_t EFIAPI *efi_convert_device_path_to_text(
226                 struct efi_device_path *device_path,
227                 bool display_only,
228                 bool allow_shortcuts)
229 {
230         uint16_t *buffer;
231
232         EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
233
234         /*
235          * Our device paths are all of depth one. So its is sufficient to
236          * to convert the first node.
237          */
238         buffer = efi_convert_device_node_to_text(device_path, display_only,
239                                                  allow_shortcuts);
240
241         EFI_EXIT(EFI_SUCCESS);
242         return buffer;
243 }
244
245 const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
246         .convert_device_node_to_text = efi_convert_device_node_to_text_ext,
247         .convert_device_path_to_text = efi_convert_device_path_to_text,
248 };