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