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