1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * EFI device path from u-boot device-model mapping
4  *
5  * (C) Copyright 2017 Rob Clark
6  */
7 
8 #define LOG_CATEGORY LOGC_EFI
9 
10 #include <blk.h>
11 #include <dm.h>
12 #include <dm/root.h>
13 #include <efi_device_path.h>
14 #include <ide.h>
15 #include <log.h>
16 #include <net.h>
17 #include <usb.h>
18 #include <mmc.h>
19 #include <nvme.h>
20 #include <efi_loader.h>
21 #include <part.h>
22 #include <u-boot/uuid.h>
23 #include <asm-generic/unaligned.h>
24 #include <linux/compat.h> /* U16_MAX */
25 
26 /* template EFI_DP_END node: */
27 const struct efi_device_path EFI_DP_END = {
28 	.type     = DEVICE_PATH_TYPE_END,
29 	.sub_type = DEVICE_PATH_SUB_TYPE_END,
30 	.length   = sizeof(EFI_DP_END),
31 };
32 
33 #if defined(CONFIG_MMC)
34 /*
35  * Determine if an MMC device is an SD card.
36  *
37  * @desc	block device descriptor
38  * Return:	true if the device is an SD card
39  */
is_sd(struct blk_desc * desc)40 static bool is_sd(struct blk_desc *desc)
41 {
42 	struct mmc *mmc = find_mmc_device(desc->devnum);
43 
44 	if (!mmc)
45 		return false;
46 
47 	return IS_SD(mmc) != 0U;
48 }
49 #endif
50 
efi_dp_next(const struct efi_device_path * dp)51 struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
52 {
53 	if (dp == NULL)
54 		return NULL;
55 	if (dp->type == DEVICE_PATH_TYPE_END)
56 		return NULL;
57 	dp = ((void *)dp) + dp->length;
58 	if (dp->type == DEVICE_PATH_TYPE_END)
59 		return NULL;
60 	return (struct efi_device_path *)dp;
61 }
62 
efi_dp_match(const struct efi_device_path * a,const struct efi_device_path * b)63 int efi_dp_match(const struct efi_device_path *a,
64 		 const struct efi_device_path *b)
65 {
66 	while (1) {
67 		int ret;
68 
69 		ret = memcmp(&a->length, &b->length, sizeof(a->length));
70 		if (ret)
71 			return ret;
72 
73 		ret = memcmp(a, b, a->length);
74 		if (ret)
75 			return ret;
76 
77 		a = efi_dp_next(a);
78 		b = efi_dp_next(b);
79 
80 		if (!a || !b)
81 			return 0;
82 	}
83 }
84 
efi_dp_shorten(struct efi_device_path * dp)85 struct efi_device_path *efi_dp_shorten(struct efi_device_path *dp)
86 {
87 	while (dp) {
88 		if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_WWI) ||
89 		    EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
90 		    EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
91 			return dp;
92 
93 		dp = efi_dp_next(dp);
94 	}
95 
96 	return dp;
97 }
98 
99 /**
100  * find_handle() - find handle by device path and installed protocol
101  *
102  * If @rem is provided, the handle with the longest partial match is returned.
103  *
104  * @dp:		device path to search
105  * @guid:	GUID of protocol that must be installed on path or NULL
106  * @short_path:	use short form device path for matching
107  * @rem:	pointer to receive remaining device path
108  * Return:	matching handle
109  */
find_handle(struct efi_device_path * dp,const efi_guid_t * guid,bool short_path,struct efi_device_path ** rem)110 static efi_handle_t find_handle(struct efi_device_path *dp,
111 				const efi_guid_t *guid, bool short_path,
112 				struct efi_device_path **rem)
113 {
114 	efi_handle_t handle, best_handle = NULL;
115 	efi_uintn_t len, best_len = 0;
116 
117 	len = efi_dp_instance_size(dp);
118 
119 	list_for_each_entry(handle, &efi_obj_list, link) {
120 		struct efi_handler *handler;
121 		struct efi_device_path *dp_current;
122 		efi_uintn_t len_current;
123 		efi_status_t ret;
124 
125 		if (guid) {
126 			ret = efi_search_protocol(handle, guid, &handler);
127 			if (ret != EFI_SUCCESS)
128 				continue;
129 		}
130 		ret = efi_search_protocol(handle, &efi_guid_device_path,
131 					  &handler);
132 		if (ret != EFI_SUCCESS)
133 			continue;
134 		dp_current = handler->protocol_interface;
135 		if (short_path) {
136 			dp_current = efi_dp_shorten(dp_current);
137 			if (!dp_current)
138 				continue;
139 		}
140 		len_current = efi_dp_instance_size(dp_current);
141 		if (rem) {
142 			if (len_current > len)
143 				continue;
144 		} else {
145 			if (len_current != len)
146 				continue;
147 		}
148 		if (memcmp(dp_current, dp, len_current))
149 			continue;
150 		if (!rem)
151 			return handle;
152 		if (len_current > best_len) {
153 			best_len = len_current;
154 			best_handle = handle;
155 			*rem = (void*)((u8 *)dp + len_current);
156 		}
157 	}
158 	return best_handle;
159 }
160 
efi_dp_find_obj(struct efi_device_path * dp,const efi_guid_t * guid,struct efi_device_path ** rem)161 efi_handle_t efi_dp_find_obj(struct efi_device_path *dp,
162 			     const efi_guid_t *guid,
163 			     struct efi_device_path **rem)
164 {
165 	efi_handle_t handle;
166 
167 	handle = find_handle(dp, guid, false, rem);
168 	if (!handle)
169 		/* Match short form device path */
170 		handle = find_handle(dp, guid, true, rem);
171 
172 	return handle;
173 }
174 
efi_dp_last_node(const struct efi_device_path * dp)175 const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
176 {
177 	struct efi_device_path *ret;
178 
179 	if (!dp || dp->type == DEVICE_PATH_TYPE_END)
180 		return NULL;
181 	while (dp) {
182 		ret = (struct efi_device_path *)dp;
183 		dp = efi_dp_next(dp);
184 	}
185 	return ret;
186 }
187 
efi_dp_instance_size(const struct efi_device_path * dp)188 efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
189 {
190 	efi_uintn_t sz = 0;
191 
192 	if (!dp || dp->type == DEVICE_PATH_TYPE_END)
193 		return 0;
194 	while (dp) {
195 		sz += dp->length;
196 		dp = efi_dp_next(dp);
197 	}
198 
199 	return sz;
200 }
201 
efi_dp_size(const struct efi_device_path * dp)202 efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
203 {
204 	const struct efi_device_path *p = dp;
205 
206 	if (!p)
207 		return 0;
208 	while (p->type != DEVICE_PATH_TYPE_END ||
209 	       p->sub_type != DEVICE_PATH_SUB_TYPE_END)
210 		p = (void *)p + p->length;
211 
212 	return (void *)p - (void *)dp;
213 }
214 
efi_dp_dup(const struct efi_device_path * dp)215 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
216 {
217 	struct efi_device_path *ndp;
218 	size_t sz = efi_dp_size(dp) + sizeof(EFI_DP_END);
219 
220 	if (!dp)
221 		return NULL;
222 
223 	ndp = efi_alloc(sz);
224 	if (!ndp)
225 		return NULL;
226 	memcpy(ndp, dp, sz);
227 
228 	return ndp;
229 }
230 
231 struct
efi_dp_concat(const struct efi_device_path * dp1,const struct efi_device_path * dp2,size_t split_end_node)232 efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
233 			       const struct efi_device_path *dp2,
234 			       size_t split_end_node)
235 {
236 	struct efi_device_path *ret;
237 	size_t end_size;
238 
239 	if (!dp1 && !dp2) {
240 		/* return an end node */
241 		ret = efi_dp_dup(&EFI_DP_END);
242 	} else if (!dp1) {
243 		ret = efi_dp_dup(dp2);
244 	} else if (!dp2) {
245 		ret = efi_dp_dup(dp1);
246 	} else {
247 		/* both dp1 and dp2 are non-null */
248 		size_t sz1;
249 		size_t sz2 = efi_dp_size(dp2);
250 		void *p;
251 
252 		if (split_end_node < sizeof(struct efi_device_path))
253 			sz1 = efi_dp_size(dp1);
254 		else
255 			sz1 = split_end_node;
256 
257 		if (split_end_node)
258 			end_size = 2 * sizeof(EFI_DP_END);
259 		else
260 			end_size = sizeof(EFI_DP_END);
261 		p = efi_alloc(sz1 + sz2 + end_size);
262 		if (!p)
263 			return NULL;
264 		ret = p;
265 		memcpy(p, dp1, sz1);
266 		p += sz1;
267 
268 		if (split_end_node) {
269 			memcpy(p, &EFI_DP_END, sizeof(EFI_DP_END));
270 			p += sizeof(EFI_DP_END);
271 		}
272 
273 		/* the end node of the second device path has to be retained */
274 		memcpy(p, dp2, sz2);
275 		p += sz2;
276 		memcpy(p, &EFI_DP_END, sizeof(EFI_DP_END));
277 	}
278 
279 	return ret;
280 }
281 
efi_dp_append_node(const struct efi_device_path * dp,const struct efi_device_path * node)282 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
283 					   const struct efi_device_path *node)
284 {
285 	struct efi_device_path *ret;
286 
287 	if (!node && !dp) {
288 		ret = efi_dp_dup(&EFI_DP_END);
289 	} else if (!node) {
290 		ret = efi_dp_dup(dp);
291 	} else if (!dp) {
292 		size_t sz = node->length;
293 		void *p = efi_alloc(sz + sizeof(EFI_DP_END));
294 		if (!p)
295 			return NULL;
296 		memcpy(p, node, sz);
297 		memcpy(p + sz, &EFI_DP_END, sizeof(EFI_DP_END));
298 		ret = p;
299 	} else {
300 		/* both dp and node are non-null */
301 		size_t sz = efi_dp_size(dp);
302 		void *p = efi_alloc(sz + node->length + sizeof(EFI_DP_END));
303 		if (!p)
304 			return NULL;
305 		memcpy(p, dp, sz);
306 		memcpy(p + sz, node, node->length);
307 		memcpy(p + sz + node->length, &EFI_DP_END, sizeof(EFI_DP_END));
308 		ret = p;
309 	}
310 
311 	return ret;
312 }
313 
efi_dp_create_device_node(const u8 type,const u8 sub_type,const u16 length)314 struct efi_device_path *efi_dp_create_device_node(const u8 type,
315 						  const u8 sub_type,
316 						  const u16 length)
317 {
318 	struct efi_device_path *ret;
319 
320 	if (length < sizeof(struct efi_device_path))
321 		return NULL;
322 
323 	ret = efi_alloc(length);
324 	if (!ret)
325 		return ret;
326 	ret->type = type;
327 	ret->sub_type = sub_type;
328 	ret->length = length;
329 	return ret;
330 }
331 
efi_dp_append_instance(const struct efi_device_path * dp,const struct efi_device_path * dpi)332 struct efi_device_path *efi_dp_append_instance(
333 		const struct efi_device_path *dp,
334 		const struct efi_device_path *dpi)
335 {
336 	size_t sz, szi;
337 	struct efi_device_path *p, *ret;
338 
339 	if (!dpi)
340 		return NULL;
341 	if (!dp)
342 		return efi_dp_dup(dpi);
343 	sz = efi_dp_size(dp);
344 	szi = efi_dp_instance_size(dpi);
345 	p = efi_alloc(sz + szi + 2 * sizeof(EFI_DP_END));
346 	if (!p)
347 		return NULL;
348 	ret = p;
349 	memcpy(p, dp, sz + sizeof(EFI_DP_END));
350 	p = (void *)p + sz;
351 	p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
352 	p = (void *)p + sizeof(EFI_DP_END);
353 	memcpy(p, dpi, szi);
354 	p = (void *)p + szi;
355 	memcpy(p, &EFI_DP_END, sizeof(EFI_DP_END));
356 	return ret;
357 }
358 
efi_dp_get_next_instance(struct efi_device_path ** dp,efi_uintn_t * size)359 struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
360 						 efi_uintn_t *size)
361 {
362 	size_t sz;
363 	struct efi_device_path *p;
364 
365 	if (size)
366 		*size = 0;
367 	if (!dp || !*dp)
368 		return NULL;
369 	sz = efi_dp_instance_size(*dp);
370 	p = efi_alloc(sz + sizeof(EFI_DP_END));
371 	if (!p)
372 		return NULL;
373 	memcpy(p, *dp, sz + sizeof(EFI_DP_END));
374 	*dp = (void *)*dp + sz;
375 	if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
376 		*dp = (void *)*dp + sizeof(EFI_DP_END);
377 	else
378 		*dp = NULL;
379 	if (size)
380 		*size = sz + sizeof(EFI_DP_END);
381 	return p;
382 }
383 
efi_dp_is_multi_instance(const struct efi_device_path * dp)384 bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
385 {
386 	const struct efi_device_path *p = dp;
387 
388 	if (!p)
389 		return false;
390 	while (p->type != DEVICE_PATH_TYPE_END)
391 		p = (void *)p + p->length;
392 	return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
393 }
394 
dp_size(struct udevice * dev)395 __maybe_unused static unsigned int dp_size(struct udevice *dev)
396 {
397 	if (!dev || !dev->driver)
398 		return sizeof(struct efi_device_path_udevice);
399 
400 	switch (device_get_uclass_id(dev)) {
401 	case UCLASS_ROOT:
402 		/* stop traversing parents at this point: */
403 		return sizeof(struct efi_device_path_udevice);
404 	case UCLASS_ETH:
405 		return dp_size(dev->parent) +
406 			sizeof(struct efi_device_path_mac_addr);
407 	case UCLASS_BLK:
408 		switch (dev->parent->uclass->uc_drv->id) {
409 #ifdef CONFIG_IDE
410 		case UCLASS_IDE:
411 			return dp_size(dev->parent) +
412 				sizeof(struct efi_device_path_atapi);
413 #endif
414 #if defined(CONFIG_SCSI)
415 		case UCLASS_SCSI:
416 			return dp_size(dev->parent) +
417 				sizeof(struct efi_device_path_scsi);
418 #endif
419 #if defined(CONFIG_MMC)
420 		case UCLASS_MMC:
421 			return dp_size(dev->parent) +
422 				sizeof(struct efi_device_path_sd_mmc_path);
423 #endif
424 #if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
425 		case UCLASS_AHCI:
426 			return dp_size(dev->parent) +
427 				sizeof(struct efi_device_path_sata);
428 #endif
429 #if defined(CONFIG_NVME)
430 		case UCLASS_NVME:
431 			return dp_size(dev->parent) +
432 				sizeof(struct efi_device_path_nvme);
433 #endif
434 #ifdef CONFIG_USB
435 		case UCLASS_MASS_STORAGE:
436 			return dp_size(dev->parent)
437 				+ sizeof(struct efi_device_path_controller);
438 #endif
439 		default:
440 			/* UCLASS_BLKMAP, UCLASS_HOST, UCLASS_VIRTIO */
441 			return dp_size(dev->parent) +
442 				sizeof(struct efi_device_path_udevice);
443 		}
444 #if defined(CONFIG_MMC)
445 	case UCLASS_MMC:
446 		return dp_size(dev->parent) +
447 			sizeof(struct efi_device_path_sd_mmc_path);
448 #endif
449 	case UCLASS_MASS_STORAGE:
450 	case UCLASS_USB_HUB:
451 		return dp_size(dev->parent) +
452 			sizeof(struct efi_device_path_usb);
453 	default:
454 		return dp_size(dev->parent) +
455 			sizeof(struct efi_device_path_udevice);
456 	}
457 }
458 
459 /*
460  * Recursively build a device path.
461  *
462  * @buf		pointer to the end of the device path
463  * @dev		device
464  * Return:	pointer to the end of the device path
465  */
dp_fill(void * buf,struct udevice * dev)466 __maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
467 {
468 	enum uclass_id uclass_id;
469 
470 	if (!dev || !dev->driver)
471 		return buf;
472 
473 	uclass_id = device_get_uclass_id(dev);
474 	if (uclass_id != UCLASS_ROOT)
475 		buf = dp_fill(buf, dev->parent);
476 
477 	switch (uclass_id) {
478 #ifdef CONFIG_NETDEVICES
479 	case UCLASS_ETH: {
480 		struct efi_device_path_mac_addr *dp = buf;
481 		struct eth_pdata *pdata = dev_get_plat(dev);
482 
483 		dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
484 		dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
485 		dp->dp.length = sizeof(*dp);
486 		memset(&dp->mac, 0, sizeof(dp->mac));
487 		/* We only support IPv4 */
488 		memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
489 		/* Ethernet */
490 		dp->if_type = 1;
491 		return &dp[1];
492 	}
493 #endif
494 	case UCLASS_BLK:
495 		switch (device_get_uclass_id(dev->parent)) {
496 #ifdef CONFIG_IDE
497 		case UCLASS_IDE: {
498 			struct efi_device_path_atapi *dp = buf;
499 			struct blk_desc *desc = dev_get_uclass_plat(dev);
500 
501 			dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
502 			dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
503 			dp->dp.length = sizeof(*dp);
504 			dp->logical_unit_number = desc->devnum;
505 			dp->primary_secondary = IDE_BUS(desc->devnum);
506 			dp->slave_master = desc->devnum %
507 				(CONFIG_SYS_IDE_MAXDEVICE /
508 				 CONFIG_SYS_IDE_MAXBUS);
509 			return &dp[1];
510 			}
511 #endif
512 #if defined(CONFIG_SCSI)
513 		case UCLASS_SCSI: {
514 			struct efi_device_path_scsi *dp = buf;
515 			struct blk_desc *desc = dev_get_uclass_plat(dev);
516 
517 			dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
518 			dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
519 			dp->dp.length = sizeof(*dp);
520 			dp->logical_unit_number = desc->lun;
521 			dp->target_id = desc->target;
522 			return &dp[1];
523 			}
524 #endif
525 #if defined(CONFIG_MMC)
526 		case UCLASS_MMC: {
527 			struct efi_device_path_sd_mmc_path *sddp = buf;
528 			struct blk_desc *desc = dev_get_uclass_plat(dev);
529 
530 			sddp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
531 			sddp->dp.sub_type = is_sd(desc) ?
532 				DEVICE_PATH_SUB_TYPE_MSG_SD :
533 				DEVICE_PATH_SUB_TYPE_MSG_MMC;
534 			sddp->dp.length   = sizeof(*sddp);
535 			sddp->slot_number = dev_seq(dev);
536 			return &sddp[1];
537 			}
538 #endif
539 #if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
540 		case UCLASS_AHCI: {
541 			struct efi_device_path_sata *dp = buf;
542 			struct blk_desc *desc = dev_get_uclass_plat(dev);
543 
544 			dp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
545 			dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
546 			dp->dp.length   = sizeof(*dp);
547 			dp->hba_port = desc->devnum;
548 			/* default 0xffff implies no port multiplier */
549 			dp->port_multiplier_port = 0xffff;
550 			dp->logical_unit_number = desc->lun;
551 			return &dp[1];
552 			}
553 #endif
554 #if defined(CONFIG_NVME)
555 		case UCLASS_NVME: {
556 			struct efi_device_path_nvme *dp = buf;
557 			u32 ns_id;
558 
559 			dp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
560 			dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
561 			dp->dp.length   = sizeof(*dp);
562 			nvme_get_namespace_id(dev, &ns_id, dp->eui64);
563 			memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
564 			return &dp[1];
565 			}
566 #endif
567 #if defined(CONFIG_USB)
568 		case UCLASS_MASS_STORAGE: {
569 			struct blk_desc *desc = dev_get_uclass_plat(dev);
570 			struct efi_device_path_controller *dp = buf;
571 
572 			dp->dp.type	= DEVICE_PATH_TYPE_HARDWARE_DEVICE;
573 			dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CONTROLLER;
574 			dp->dp.length	= sizeof(*dp);
575 			dp->controller_number = desc->lun;
576 			return &dp[1];
577 		}
578 #endif
579 		default: {
580 			/* UCLASS_BLKMAP, UCLASS_HOST, UCLASS_VIRTIO */
581 			struct efi_device_path_udevice *dp = buf;
582 			struct blk_desc *desc = dev_get_uclass_plat(dev);
583 
584 			dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
585 			dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
586 			dp->dp.length = sizeof(*dp);
587 			memcpy(&dp->guid, &efi_u_boot_guid,
588 			       sizeof(efi_guid_t));
589 			dp->uclass_id = (UCLASS_BLK & 0xffff) |
590 					(desc->uclass_id << 16);
591 			dp->dev_number = desc->devnum;
592 
593 			return &dp[1];
594 		}
595 	}
596 #if defined(CONFIG_MMC)
597 	case UCLASS_MMC: {
598 		struct efi_device_path_sd_mmc_path *sddp = buf;
599 		struct mmc *mmc = mmc_get_mmc_dev(dev);
600 		struct blk_desc *desc = mmc_get_blk_desc(mmc);
601 
602 		sddp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
603 		sddp->dp.sub_type = is_sd(desc) ?
604 			DEVICE_PATH_SUB_TYPE_MSG_SD :
605 			DEVICE_PATH_SUB_TYPE_MSG_MMC;
606 		sddp->dp.length   = sizeof(*sddp);
607 		sddp->slot_number = dev_seq(dev);
608 
609 		return &sddp[1];
610 	}
611 #endif
612 	case UCLASS_MASS_STORAGE:
613 	case UCLASS_USB_HUB: {
614 		struct efi_device_path_usb *udp = buf;
615 
616 		switch (device_get_uclass_id(dev->parent)) {
617 		case UCLASS_USB_HUB: {
618 			struct usb_device *udev = dev_get_parent_priv(dev);
619 
620 			udp->parent_port_number = udev->portnr;
621 			break;
622 		}
623 		default:
624 			udp->parent_port_number = 0;
625 		}
626 		udp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
627 		udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
628 		udp->dp.length   = sizeof(*udp);
629 		udp->usb_interface = 0;
630 
631 		return &udp[1];
632 	}
633 	default: {
634 		struct efi_device_path_udevice *vdp = buf;
635 
636 		vdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
637 		vdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
638 		vdp->dp.length = sizeof(*vdp);
639 		memcpy(&vdp->guid, &efi_u_boot_guid, sizeof(efi_guid_t));
640 		vdp->uclass_id = uclass_id;
641 		vdp->dev_number = dev->seq_;
642 
643 		return &vdp[1];
644 	    }
645 	}
646 }
647 
dp_part_size(struct blk_desc * desc,int part)648 static unsigned dp_part_size(struct blk_desc *desc, int part)
649 {
650 	unsigned dpsize;
651 	struct udevice *dev = desc->bdev;
652 
653 	dpsize = dp_size(dev);
654 
655 	if (part == 0) /* the actual disk, not a partition */
656 		return dpsize;
657 
658 	if (desc->part_type == PART_TYPE_ISO)
659 		dpsize += sizeof(struct efi_device_path_cdrom_path);
660 	else
661 		dpsize += sizeof(struct efi_device_path_hard_drive_path);
662 
663 	return dpsize;
664 }
665 
666 /*
667  * Create a device node for a block device partition.
668  *
669  * @buf		buffer to which the device path is written
670  * @desc	block device descriptor
671  * @part	partition number, 0 identifies a block device
672  *
673  * Return:	pointer to position after the node
674  */
dp_part_node(void * buf,struct blk_desc * desc,int part)675 static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
676 {
677 	struct disk_partition info;
678 	int ret;
679 
680 	ret = part_get_info(desc, part, &info);
681 	if (ret < 0)
682 		return buf;
683 
684 	if (desc->part_type == PART_TYPE_ISO) {
685 		struct efi_device_path_cdrom_path *cddp = buf;
686 
687 		cddp->boot_entry = part;
688 		cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
689 		cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
690 		cddp->dp.length = sizeof(*cddp);
691 		cddp->partition_start = info.start;
692 		cddp->partition_size = info.size;
693 
694 		buf = &cddp[1];
695 	} else {
696 		struct efi_device_path_hard_drive_path *hddp = buf;
697 
698 		hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
699 		hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
700 		hddp->dp.length = sizeof(*hddp);
701 		hddp->partition_number = part;
702 		hddp->partition_start = info.start;
703 		hddp->partition_end = info.size;
704 		if (desc->part_type == PART_TYPE_EFI)
705 			hddp->partmap_type = 2;
706 		else
707 			hddp->partmap_type = 1;
708 
709 		switch (desc->sig_type) {
710 		case SIG_TYPE_NONE:
711 		default:
712 			hddp->signature_type = 0;
713 			memset(hddp->partition_signature, 0,
714 			       sizeof(hddp->partition_signature));
715 			break;
716 		case SIG_TYPE_MBR:
717 			hddp->signature_type = 1;
718 			memset(hddp->partition_signature, 0,
719 			       sizeof(hddp->partition_signature));
720 			memcpy(hddp->partition_signature, &desc->mbr_sig,
721 			       sizeof(desc->mbr_sig));
722 			break;
723 		case SIG_TYPE_GUID:
724 			hddp->signature_type = 2;
725 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
726 			/* info.uuid exists only with PARTITION_UUIDS */
727 			if (uuid_str_to_bin(info.uuid,
728 					    hddp->partition_signature,
729 					    UUID_STR_FORMAT_GUID)) {
730 				log_warning(
731 					"Partition %d: invalid GUID %s\n",
732 					part, info.uuid);
733 			}
734 #endif
735 			break;
736 		}
737 
738 		buf = &hddp[1];
739 	}
740 
741 	return buf;
742 }
743 
744 /*
745  * Create a device path for a block device or one of its partitions.
746  *
747  * @buf		buffer to which the device path is written
748  * @desc	block device descriptor
749  * @part	partition number, 0 identifies a block device
750  */
dp_part_fill(void * buf,struct blk_desc * desc,int part)751 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
752 {
753 	struct udevice *dev = desc->bdev;
754 
755 	buf = dp_fill(buf, dev);
756 
757 	if (part == 0) /* the actual disk, not a partition */
758 		return buf;
759 
760 	return dp_part_node(buf, desc, part);
761 }
762 
efi_dp_from_part(struct blk_desc * desc,int part)763 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
764 {
765 	void *buf, *start;
766 
767 	start = efi_alloc(dp_part_size(desc, part) + sizeof(EFI_DP_END));
768 	if (!start)
769 		return NULL;
770 
771 	buf = dp_part_fill(start, desc, part);
772 
773 	*((struct efi_device_path *)buf) = EFI_DP_END;
774 
775 	return start;
776 }
777 
efi_dp_part_node(struct blk_desc * desc,int part)778 struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
779 {
780 	efi_uintn_t dpsize;
781 	void *buf;
782 
783 	if (desc->part_type == PART_TYPE_ISO)
784 		dpsize = sizeof(struct efi_device_path_cdrom_path);
785 	else
786 		dpsize = sizeof(struct efi_device_path_hard_drive_path);
787 	buf = efi_alloc(dpsize);
788 
789 	if (buf)
790 		dp_part_node(buf, desc, part);
791 
792 	return buf;
793 }
794 
795 /**
796  * path_to_uefi() - convert UTF-8 path to an UEFI style path
797  *
798  * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
799  * separators and UTF-16).
800  *
801  * @src:	source buffer
802  * @uefi:	target buffer, possibly unaligned
803  */
path_to_uefi(void * uefi,const char * src)804 static void path_to_uefi(void *uefi, const char *src)
805 {
806 	u16 *pos = uefi;
807 
808 	/*
809 	 * efi_set_bootdev() calls this routine indirectly before the UEFI
810 	 * subsystem is initialized. So we cannot assume unaligned access to be
811 	 * enabled.
812 	 */
813 	allow_unaligned();
814 
815 	while (*src) {
816 		s32 code = utf8_get(&src);
817 
818 		if (code < 0)
819 			code = '?';
820 		else if (code == '/')
821 			code = '\\';
822 		utf16_put(code, &pos);
823 	}
824 	*pos = 0;
825 }
826 
efi_dp_from_file(const struct efi_device_path * dp,const char * path)827 struct efi_device_path *efi_dp_from_file(const struct efi_device_path *dp,
828 					 const char *path)
829 {
830 	struct efi_device_path_file_path *fp;
831 	void *buf, *pos;
832 	size_t dpsize, fpsize;
833 
834 	dpsize = efi_dp_size(dp);
835 	fpsize = sizeof(struct efi_device_path) +
836 		 2 * (utf8_utf16_strlen(path) + 1);
837 	if (fpsize > U16_MAX)
838 		return NULL;
839 
840 	buf = efi_alloc(dpsize + fpsize + sizeof(EFI_DP_END));
841 	if (!buf)
842 		return NULL;
843 
844 	memcpy(buf, dp, dpsize);
845 	pos = buf + dpsize;
846 
847 	/* add file-path: */
848 	if (*path) {
849 		fp = pos;
850 		fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
851 		fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
852 		fp->dp.length = (u16)fpsize;
853 		path_to_uefi(fp->str, path);
854 		pos += fpsize;
855 	}
856 
857 	memcpy(pos, &EFI_DP_END, sizeof(EFI_DP_END));
858 
859 	return buf;
860 }
861 
efi_dp_from_uart(void)862 struct efi_device_path *efi_dp_from_uart(void)
863 {
864 	void *buf, *pos;
865 	struct efi_device_path_uart *uart;
866 	size_t dpsize = dp_size(dm_root()) + sizeof(*uart) + sizeof(EFI_DP_END);
867 
868 	buf = efi_alloc(dpsize);
869 	if (!buf)
870 		return NULL;
871 	pos = dp_fill(buf, dm_root());
872 	uart = pos;
873 	uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
874 	uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
875 	uart->dp.length = sizeof(*uart);
876 	pos += sizeof(*uart);
877 	memcpy(pos, &EFI_DP_END, sizeof(EFI_DP_END));
878 
879 	return buf;
880 }
881 
efi_dp_from_eth(struct udevice * dev)882 struct efi_device_path __maybe_unused *efi_dp_from_eth(struct udevice *dev)
883 {
884 	void *buf, *start;
885 	unsigned dpsize = 0;
886 
887 	assert(dev);
888 
889 	dpsize += dp_size(dev);
890 
891 	start = efi_alloc(dpsize + sizeof(EFI_DP_END));
892 	if (!start)
893 		return NULL;
894 
895 	buf = dp_fill(start, dev);
896 
897 	*((struct efi_device_path *)buf) = EFI_DP_END;
898 
899 	return start;
900 }
901 
902 /**
903  * efi_dp_from_ipv4() - set device path from IPv4 address
904  *
905  * Set the device path to an ethernet device path as provided by
906  * efi_dp_from_eth() concatenated with a device path of subtype
907  * DEVICE_PATH_SUB_TYPE_MSG_IPV4, and an EFI_DP_END node.
908  *
909  * @ip:		IPv4 local address
910  * @mask:	network mask
911  * @srv:	IPv4 remote/server address
912  * @dev:	net udevice
913  * Return:	pointer to device path, NULL on error
914  */
efi_dp_from_ipv4(struct efi_ipv4_address * ip,struct efi_ipv4_address * mask,struct efi_ipv4_address * srv,struct udevice * dev)915 static struct efi_device_path *efi_dp_from_ipv4(struct efi_ipv4_address *ip,
916 					 struct efi_ipv4_address *mask,
917 					 struct efi_ipv4_address *srv,
918 					 struct udevice *dev)
919 {
920 	struct efi_device_path *dp1, *dp2, *pos;
921 	struct {
922 		struct efi_device_path_ipv4 ipv4dp;
923 		struct efi_device_path end;
924 	} dp;
925 
926 	memset(&dp.ipv4dp, 0, sizeof(dp.ipv4dp));
927 	dp.ipv4dp.dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
928 	dp.ipv4dp.dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_IPV4;
929 	dp.ipv4dp.dp.length = sizeof(dp.ipv4dp);
930 	dp.ipv4dp.protocol = 6;
931 	if (ip)
932 		memcpy(&dp.ipv4dp.local_ip_address, ip, sizeof(*ip));
933 	if (mask)
934 		memcpy(&dp.ipv4dp.subnet_mask, mask, sizeof(*mask));
935 	if (srv)
936 		memcpy(&dp.ipv4dp.remote_ip_address, srv, sizeof(*srv));
937 	pos = &dp.end;
938 	memcpy(pos, &EFI_DP_END, sizeof(EFI_DP_END));
939 
940 	dp1 = efi_dp_from_eth(dev);
941 	if (!dp1)
942 		return NULL;
943 
944 	dp2 = efi_dp_concat(dp1, (const struct efi_device_path *)&dp, 0);
945 
946 	efi_free_pool(dp1);
947 
948 	return dp2;
949 }
950 
efi_dp_from_http(const char * server,struct udevice * dev)951 struct efi_device_path *efi_dp_from_http(const char *server, struct udevice *dev)
952 {
953 	struct efi_device_path *dp1, *dp2;
954 	struct efi_device_path_uri *uridp;
955 	efi_uintn_t uridp_len;
956 	char *pos;
957 	char tmp[128];
958 	struct efi_ipv4_address ip;
959 	struct efi_ipv4_address mask;
960 
961 	if ((server && strlen("http://") + strlen(server) + 1  > sizeof(tmp)) ||
962 	    (!server && IS_ENABLED(CONFIG_NET_LWIP)))
963 		return NULL;
964 
965 	efi_net_get_addr(&ip, &mask, NULL, dev);
966 
967 	dp1 = efi_dp_from_ipv4(&ip, &mask, NULL, dev);
968 	if (!dp1)
969 		return NULL;
970 
971 
972 	strcpy(tmp, "http://");
973 
974 	if (server) {
975 		strlcat(tmp, server, sizeof(tmp));
976 #if !IS_ENABLED(CONFIG_NET_LWIP)
977 	} else {
978 		ip_to_string(net_server_ip, tmp + strlen("http://"));
979 #endif
980 	}
981 
982 	uridp_len = sizeof(struct efi_device_path) + strlen(tmp) + 1;
983 	uridp = efi_alloc(uridp_len + sizeof(EFI_DP_END));
984 	if (!uridp) {
985 		log_err("Out of memory\n");
986 		return NULL;
987 	}
988 	uridp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
989 	uridp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_URI;
990 	uridp->dp.length = uridp_len;
991 	debug("device path: setting uri device path to %s\n", tmp);
992 	memcpy(uridp->uri, tmp, strlen(tmp) + 1);
993 
994 	pos = (char *)uridp + uridp_len;
995 	memcpy(pos, &EFI_DP_END, sizeof(EFI_DP_END));
996 
997 	dp2 = efi_dp_concat(dp1, (const struct efi_device_path *)uridp, 0);
998 
999 	efi_free_pool(uridp);
1000 	efi_free_pool(dp1);
1001 
1002 	return dp2;
1003 }
1004 
1005 /* Construct a device-path for memory-mapped image */
efi_dp_from_mem(uint32_t memory_type,uint64_t start_address,size_t size)1006 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1007 					uint64_t start_address,
1008 					size_t size)
1009 {
1010 	struct efi_device_path_memory *mdp;
1011 	void *buf, *start;
1012 
1013 	start = efi_alloc(sizeof(*mdp) + sizeof(EFI_DP_END));
1014 	if (!start)
1015 		return NULL;
1016 
1017 	mdp = start;
1018 	mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1019 	mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1020 	mdp->dp.length = sizeof(*mdp);
1021 	mdp->memory_type = memory_type;
1022 	mdp->start_address = start_address;
1023 	mdp->end_address = start_address + size;
1024 	buf = &mdp[1];
1025 
1026 	*((struct efi_device_path *)buf) = EFI_DP_END;
1027 
1028 	return start;
1029 }
1030 
1031 /**
1032  * efi_dp_split_file_path() - split of relative file path from device path
1033  *
1034  * Given a device path indicating a file on a device, separate the device
1035  * path in two: the device path of the actual device and the file path
1036  * relative to this device.
1037  *
1038  * @full_path:		device path including device and file path
1039  * @device_path:	path of the device
1040  * @file_path:		relative path of the file or NULL if there is none
1041  * Return:		status code
1042  */
efi_dp_split_file_path(struct efi_device_path * full_path,struct efi_device_path ** device_path,struct efi_device_path ** file_path)1043 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1044 				    struct efi_device_path **device_path,
1045 				    struct efi_device_path **file_path)
1046 {
1047 	struct efi_device_path *p, *dp, *fp = NULL;
1048 
1049 	*device_path = NULL;
1050 	*file_path = NULL;
1051 	dp = efi_dp_dup(full_path);
1052 	if (!dp)
1053 		return EFI_OUT_OF_RESOURCES;
1054 	p = dp;
1055 	while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
1056 		p = efi_dp_next(p);
1057 		if (!p)
1058 			goto out;
1059 	}
1060 	fp = efi_dp_dup(p);
1061 	if (!fp)
1062 		return EFI_OUT_OF_RESOURCES;
1063 	p->type = DEVICE_PATH_TYPE_END;
1064 	p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1065 	p->length = sizeof(*p);
1066 
1067 out:
1068 	*device_path = dp;
1069 	*file_path = fp;
1070 	return EFI_SUCCESS;
1071 }
1072 
1073 /**
1074  * efi_dp_from_name() - convert U-Boot device and file path to device path
1075  *
1076  * @dev:	U-Boot device, e.g. 'mmc'
1077  * @devnr:	U-Boot device number, e.g. 1 for 'mmc:1'
1078  * @path:	file path relative to U-Boot device, may be NULL
1079  * @device:	pointer to receive device path of the device
1080  * @file:	pointer to receive device path for the file
1081  * Return:	status code
1082  */
efi_dp_from_name(const char * dev,const char * devnr,const char * path,struct efi_device_path ** device,struct efi_device_path ** file)1083 efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1084 			      const char *path,
1085 			      struct efi_device_path **device,
1086 			      struct efi_device_path **file)
1087 {
1088 	struct blk_desc *desc = NULL;
1089 	struct efi_device_path *dp;
1090 	struct disk_partition fs_partition;
1091 	size_t image_size;
1092 	void *image_addr;
1093 	int part = 0;
1094 
1095 	if (path && !file)
1096 		return EFI_INVALID_PARAMETER;
1097 
1098 	if (IS_ENABLED(CONFIG_EFI_BINARY_EXEC) &&
1099 	    (!strcmp(dev, "Mem") || !strcmp(dev, "hostfs")))  {
1100 		/* loadm command and semihosting */
1101 		efi_get_image_parameters(&image_addr, &image_size);
1102 
1103 		dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
1104 				     (uintptr_t)image_addr, image_size);
1105 	} else if (IS_ENABLED(CONFIG_NETDEVICES) &&
1106 	           (!strcmp(dev, "Net") || !strcmp(dev, "Http"))) {
1107 		efi_net_dp_from_dev(&dp, eth_get_dev(), false);
1108 	} else if (!strcmp(dev, "Uart")) {
1109 		dp = efi_dp_from_uart();
1110 	} else {
1111 		part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1112 					       1);
1113 		if (part < 0 || !desc)
1114 			return EFI_INVALID_PARAMETER;
1115 
1116 		dp = efi_dp_from_part(desc, part);
1117 	}
1118 	if (device)
1119 		*device = dp;
1120 
1121 	if (!path)
1122 		return EFI_SUCCESS;
1123 
1124 	*file = efi_dp_from_file(dp, path);
1125 	if (!*file)
1126 		return EFI_OUT_OF_RESOURCES;
1127 
1128 	return EFI_SUCCESS;
1129 }
1130 
1131 /**
1132  * efi_dp_check_length() - check length of a device path
1133  *
1134  * @dp:		pointer to device path
1135  * @maxlen:	maximum length of the device path
1136  * Return:
1137  * * length of the device path if it is less or equal @maxlen
1138  * * -1 if the device path is longer then @maxlen
1139  * * -1 if a device path node has a length of less than 4
1140  * * -EINVAL if maxlen exceeds SSIZE_MAX
1141  */
efi_dp_check_length(const struct efi_device_path * dp,const size_t maxlen)1142 ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1143 			    const size_t maxlen)
1144 {
1145 	ssize_t ret = 0;
1146 	u16 len;
1147 
1148 	if (maxlen > SSIZE_MAX)
1149 		return -EINVAL;
1150 	for (;;) {
1151 		len = dp->length;
1152 		if (len < 4)
1153 			return -1;
1154 		ret += len;
1155 		if (ret > maxlen)
1156 			return -1;
1157 		if (dp->type == DEVICE_PATH_TYPE_END &&
1158 		    dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1159 			return ret;
1160 		dp = (const struct efi_device_path *)((const u8 *)dp + len);
1161 	}
1162 }
1163 
1164 /**
1165  * efi_dp_from_lo() - get device-path from load option
1166  *
1167  * The load options in U-Boot may contain multiple concatenated device-paths.
1168  * The first device-path indicates the EFI binary to execute. Subsequent
1169  * device-paths start with a VenMedia node where the GUID identifies the
1170  * function (initrd or fdt).
1171  *
1172  * @lo:		EFI load option containing a valid device path
1173  * @guid:	GUID identifying device-path or NULL for the EFI binary
1174  *
1175  * Return:
1176  * device path excluding the matched VenMedia node or NULL.
1177  * Caller must free the returned value.
1178  */
1179 struct
efi_dp_from_lo(struct efi_load_option * lo,const efi_guid_t * guid)1180 efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
1181 				const efi_guid_t *guid)
1182 {
1183 	struct efi_device_path *fp = lo->file_path;
1184 	struct efi_device_path_vendor *vendor;
1185 	int lo_len = lo->file_path_length;
1186 
1187 	if (!guid)
1188 		return efi_dp_dup(fp);
1189 
1190 	for (; lo_len >=  sizeof(struct efi_device_path);
1191 	     lo_len -= fp->length, fp = (void *)fp + fp->length) {
1192 		if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1193 			break;
1194 		if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1195 		    fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1196 			continue;
1197 
1198 		vendor = (struct efi_device_path_vendor *)fp;
1199 		if (!guidcmp(&vendor->guid, guid))
1200 			return efi_dp_dup(efi_dp_next(fp));
1201 	}
1202 	log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1203 
1204 	return NULL;
1205 }
1206 
1207 /**
1208  * search_gpt_dp_node() - search gpt device path node
1209  *
1210  * @device_path:	device path
1211  *
1212  * Return:	pointer to the gpt device path node
1213  */
search_gpt_dp_node(struct efi_device_path * device_path)1214 struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1215 {
1216 	struct efi_device_path *dp = device_path;
1217 
1218 	while (dp) {
1219 		if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1220 		    dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1221 			struct efi_device_path_hard_drive_path *hd_dp =
1222 				(struct efi_device_path_hard_drive_path *)dp;
1223 
1224 			if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1225 			    hd_dp->signature_type == SIG_TYPE_GUID)
1226 				return dp;
1227 		}
1228 		dp = efi_dp_next(dp);
1229 	}
1230 
1231 	return NULL;
1232 }
1233