]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/efi_loader/efi_device_path_to_text.c
Merge git://git.denx.de/u-boot-sunxi
[people/ms/u-boot.git] / lib / efi_loader / efi_device_path_to_text.c
CommitLineData
cc5b7081 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
09c5ab90 12#define MAC_OUTPUT_LEN 22
13#define UNKNOWN_OUTPUT_LEN 23
cc5b7081 14
61aba193
HS
15#define MAX_NODE_LEN 512
16#define MAX_PATH_LEN 1024
17
cc5b7081 18const efi_guid_t efi_guid_device_path_to_text_protocol =
19 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
20
61aba193
HS
21static 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
adae4313
RC
37static char *dp_unknown(char *s, struct efi_device_path *dp)
38{
61aba193 39 s += sprintf(s, "UNKNOWN(%04x,%04x)", dp->type, dp->sub_type);
adae4313
RC
40 return s;
41}
42
43static char *dp_hardware(char *s, struct efi_device_path *dp)
44{
45 switch (dp->sub_type) {
bf19273e
RC
46 case DEVICE_PATH_SUB_TYPE_MEMORY: {
47 struct efi_device_path_memory *mdp =
48 (struct efi_device_path_memory *)dp;
61aba193 49 s += sprintf(s, "MemoryMapped(0x%x,0x%llx,0x%llx)",
bf19273e
RC
50 mdp->memory_type,
51 mdp->start_address,
52 mdp->end_address);
53 break;
54 }
adae4313
RC
55 case DEVICE_PATH_SUB_TYPE_VENDOR: {
56 struct efi_device_path_vendor *vdp =
57 (struct efi_device_path_vendor *)dp;
61aba193 58 s += sprintf(s, "VenHw(%pUl)", &vdp->guid);
adae4313
RC
59 break;
60 }
61 default:
62 s = dp_unknown(s, dp);
63 break;
64 }
65 return s;
66}
67
68static 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;
61aba193 74 s += sprintf(s, "Acpi(PNP%04x", EISA_PNP_NUM(adp->hid));
adae4313
RC
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
87static 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;
6ea8b580 93 s += sprintf(s, "USB(0x%x,0x%x)", udp->parent_port_number,
adae4313
RC
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
61aba193 104 s += sprintf(s, "MAC(%02x%02x%02x%02x%02x%02x,0x%1x)",
adae4313
RC
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
61aba193 116 s += sprintf(s, "USBClass(%x,%x,%x,%x,%x)",
adae4313
RC
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) ?
6ea8b580 127 "SD" : "eMMC";
adae4313
RC
128 struct efi_device_path_sd_mmc_path *sddp =
129 (struct efi_device_path_sd_mmc_path *)dp;
6ea8b580 130 s += sprintf(s, "%s(%u)", typename, sddp->slot_number);
adae4313
RC
131 break;
132 }
133 default:
134 s = dp_unknown(s, dp);
135 break;
136 }
137 return s;
138}
139
6ea8b580
HS
140/*
141 * Convert a media device path node to text.
142 *
143 * @s output buffer
144 * @dp device path node
145 * @return next unused buffer address
146 */
adae4313
RC
147static char *dp_media(char *s, struct efi_device_path *dp)
148{
149 switch (dp->sub_type) {
150 case DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH: {
151 struct efi_device_path_hard_drive_path *hddp =
152 (struct efi_device_path_hard_drive_path *)dp;
153 void *sig = hddp->partition_signature;
6ea8b580
HS
154 u64 start;
155 u64 end;
156
157 /* Copy from packed structure to aligned memory */
158 memcpy(&start, &hddp->partition_start, sizeof(start));
159 memcpy(&end, &hddp->partition_end, sizeof(end));
adae4313
RC
160
161 switch (hddp->signature_type) {
6ea8b580
HS
162 case SIG_TYPE_MBR: {
163 u32 signature;
164
165 memcpy(&signature, sig, sizeof(signature));
166 s += sprintf(
167 s, "HD(%d,MBR,0x%08x,0x%llx,0x%llx)",
168 hddp->partition_number, signature, start, end);
adae4313 169 break;
6ea8b580 170 }
adae4313 171 case SIG_TYPE_GUID:
6ea8b580
HS
172 s += sprintf(
173 s, "HD(%d,GPT,%pUl,0x%llx,0x%llx)",
174 hddp->partition_number, sig, start, end);
1a0b4d22 175 break;
adae4313 176 default:
6ea8b580
HS
177 s += sprintf(
178 s, "HD(%d,0x%02x,0,0x%llx,0x%llx)",
179 hddp->partition_number, hddp->partmap_type,
180 start, end);
1a0b4d22 181 break;
adae4313
RC
182 }
183
184 break;
185 }
186 case DEVICE_PATH_SUB_TYPE_CDROM_PATH: {
187 struct efi_device_path_cdrom_path *cddp =
188 (struct efi_device_path_cdrom_path *)dp;
61aba193 189 s += sprintf(s, "CDROM(0x%x)", cddp->boot_entry);
adae4313
RC
190 break;
191 }
192 case DEVICE_PATH_SUB_TYPE_FILE_PATH: {
193 struct efi_device_path_file_path *fp =
194 (struct efi_device_path_file_path *)dp;
195 int slen = (dp->length - sizeof(*dp)) / 2;
61aba193
HS
196 if (slen > MAX_NODE_LEN - 2)
197 slen = MAX_NODE_LEN - 2;
198 s += sprintf(s, "%-.*ls", slen, fp->str);
adae4313
RC
199 break;
200 }
201 default:
202 s = dp_unknown(s, dp);
203 break;
204 }
205 return s;
206}
207
61aba193
HS
208/*
209 * Converts a single node to a char string.
210 *
211 * @buffer output buffer
212 * @dp device path or node
213 * @return end of string
214 */
215static char *efi_convert_single_device_node_to_text(
216 char *buffer,
217 struct efi_device_path *dp)
cc5b7081 218{
61aba193 219 char *str = buffer;
cc5b7081 220
61aba193
HS
221 switch (dp->type) {
222 case DEVICE_PATH_TYPE_HARDWARE_DEVICE:
223 str = dp_hardware(str, dp);
224 break;
225 case DEVICE_PATH_TYPE_ACPI_DEVICE:
226 str = dp_acpi(str, dp);
227 break;
228 case DEVICE_PATH_TYPE_MESSAGING_DEVICE:
229 str = dp_msging(str, dp);
230 break;
231 case DEVICE_PATH_TYPE_MEDIA_DEVICE:
232 str = dp_media(str, dp);
233 break;
234 default:
235 str = dp_unknown(str, dp);
cc5b7081 236 }
237
61aba193
HS
238 *str = '\0';
239 return str;
cc5b7081 240}
241
c4574208
HS
242/*
243 * This function implements the ConvertDeviceNodeToText service of the
244 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
245 * See the Unified Extensible Firmware Interface (UEFI) specification
246 * for details.
247 *
248 * device_node device node to be converted
249 * display_only true if the shorter text represenation shall be used
250 * allow_shortcuts true if shortcut forms may be used
251 * @return text represenation of the device path
252 * NULL if out of memory of device_path is NULL
253 */
61aba193 254static uint16_t EFIAPI *efi_convert_device_node_to_text(
9309a1b7 255 struct efi_device_path *device_node,
09c5ab90 256 bool display_only,
257 bool allow_shortcuts)
258{
61aba193
HS
259 char str[MAX_NODE_LEN];
260 uint16_t *text = NULL;
09c5ab90 261
262 EFI_ENTRY("%p, %d, %d", device_node, display_only, allow_shortcuts);
263
61aba193
HS
264 if (!device_node)
265 goto out;
266 efi_convert_single_device_node_to_text(str, device_node);
267
268 text = efi_str_to_u16(str);
09c5ab90 269
61aba193 270out:
09c5ab90 271 EFI_EXIT(EFI_SUCCESS);
61aba193 272 return text;
09c5ab90 273}
274
c4574208
HS
275/*
276 * This function implements the ConvertDevicePathToText service of the
277 * EFI_DEVICE_PATH_TO_TEXT_PROTOCOL.
278 * See the Unified Extensible Firmware Interface (UEFI) specification
279 * for details.
280 *
281 * device_path device path to be converted
282 * display_only true if the shorter text represenation shall be used
283 * allow_shortcuts true if shortcut forms may be used
284 * @return text represenation of the device path
285 * NULL if out of memory of device_path is NULL
286 */
09c5ab90 287static uint16_t EFIAPI *efi_convert_device_path_to_text(
9309a1b7 288 struct efi_device_path *device_path,
09c5ab90 289 bool display_only,
290 bool allow_shortcuts)
291{
61aba193
HS
292 uint16_t *text = NULL;
293 char buffer[MAX_PATH_LEN];
294 char *str = buffer;
09c5ab90 295
296 EFI_ENTRY("%p, %d, %d", device_path, display_only, allow_shortcuts);
297
61aba193
HS
298 if (!device_path)
299 goto out;
300 while (device_path &&
301 str + MAX_NODE_LEN < buffer + MAX_PATH_LEN) {
302 *str++ = '/';
303 str = efi_convert_single_device_node_to_text(str, device_path);
304 device_path = efi_dp_next(device_path);
305 }
306
307 text = efi_str_to_u16(buffer);
09c5ab90 308
61aba193 309out:
09c5ab90 310 EFI_EXIT(EFI_SUCCESS);
61aba193 311 return text;
09c5ab90 312}
313
908cf9a3
HS
314/* helper for debug prints.. efi_free_pool() the result. */
315uint16_t *efi_dp_str(struct efi_device_path *dp)
316{
317 return EFI_CALL(efi_convert_device_path_to_text(dp, true, true));
318}
319
cc5b7081 320const struct efi_device_path_to_text_protocol efi_device_path_to_text = {
61aba193 321 .convert_device_node_to_text = efi_convert_device_node_to_text,
cc5b7081 322 .convert_device_path_to_text = efi_convert_device_path_to_text,
323};