2 * EFI device path from u-boot device-model mapping
4 * (C) Copyright 2017 Rob Clark
6 * SPDX-License-Identifier: GPL-2.0+
9 #define LOG_CATEGORY LOGL_ERR
16 #include <efi_loader.h>
20 /* template END node: */
21 static const struct efi_device_path END = {
22 .type = DEVICE_PATH_TYPE_END,
23 .sub_type = DEVICE_PATH_SUB_TYPE_END,
24 .length = sizeof(END),
28 EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
29 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b)
31 /* template ROOT node: */
32 static const struct efi_device_path_vendor ROOT = {
34 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
35 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
36 .length = sizeof(ROOT),
41 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
43 * Determine if an MMC device is an SD card.
45 * @desc block device descriptor
46 * @return true if the device is an SD card
48 static bool is_sd(struct blk_desc *desc)
50 struct mmc *mmc = find_mmc_device(desc->devnum);
55 return IS_SD(mmc) != 0U;
59 static void *dp_alloc(size_t sz)
63 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
65 debug("EFI: ERROR: out of memory in %s\n", __func__);
73 * Iterate to next block in device-path, terminating (returning NULL)
76 struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
80 if (dp->type == DEVICE_PATH_TYPE_END)
82 dp = ((void *)dp) + dp->length;
83 if (dp->type == DEVICE_PATH_TYPE_END)
85 return (struct efi_device_path *)dp;
89 * Compare two device-paths, stopping when the shorter of the two hits
90 * an End* node. This is useful to, for example, compare a device-path
91 * representing a device with one representing a file on the device, or
92 * a device with a parent device.
94 int efi_dp_match(const struct efi_device_path *a,
95 const struct efi_device_path *b)
100 ret = memcmp(&a->length, &b->length, sizeof(a->length));
104 ret = memcmp(a, b, a->length);
117 * See UEFI spec (section 3.1.2, about short-form device-paths..
118 * tl;dr: we can have a device-path that starts with a USB WWID
119 * or USB Class node, and a few other cases which don't encode
120 * the full device path with bus hierarchy:
122 * - MESSAGING:USB_WWID
123 * - MESSAGING:USB_CLASS
128 static struct efi_device_path *shorten_path(struct efi_device_path *dp)
132 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
133 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
134 * so not sure when we would see these other cases.
136 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
137 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
138 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
141 dp = efi_dp_next(dp);
147 static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
148 struct efi_device_path **rem)
150 struct efi_object *efiobj;
151 unsigned int dp_size = efi_dp_size(dp);
153 list_for_each_entry(efiobj, &efi_obj_list, link) {
154 struct efi_handler *handler;
155 struct efi_device_path *obj_dp;
158 ret = efi_search_protocol(efiobj->handle,
159 &efi_guid_device_path, &handler);
160 if (ret != EFI_SUCCESS)
162 obj_dp = handler->protocol_interface;
165 if (efi_dp_match(dp, obj_dp) == 0) {
168 * Allow partial matches, but inform
171 *rem = ((void *)dp) +
175 /* Only return on exact matches */
176 if (efi_dp_size(obj_dp) == dp_size)
181 obj_dp = shorten_path(efi_dp_next(obj_dp));
182 } while (short_path && obj_dp);
189 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
190 * remaining part of the device path after the matched object.
192 struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
193 struct efi_device_path **rem)
195 struct efi_object *efiobj;
197 /* Search for an exact match first */
198 efiobj = find_obj(dp, false, NULL);
200 /* Then for a fuzzy match */
202 efiobj = find_obj(dp, false, rem);
204 /* And now for a fuzzy short match */
206 efiobj = find_obj(dp, true, rem);
212 * Determine the last device path node that is not the end node.
215 * @return last node before the end node if it exists
218 const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
220 struct efi_device_path *ret;
222 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
225 ret = (struct efi_device_path *)dp;
226 dp = efi_dp_next(dp);
231 /* return size not including End node: */
232 unsigned efi_dp_size(const struct efi_device_path *dp)
238 dp = efi_dp_next(dp);
244 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
246 struct efi_device_path *ndp;
247 unsigned sz = efi_dp_size(dp) + sizeof(END);
260 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
261 const struct efi_device_path *dp2)
263 struct efi_device_path *ret;
266 ret = efi_dp_dup(dp2);
268 ret = efi_dp_dup(dp1);
270 /* both dp1 and dp2 are non-null */
271 unsigned sz1 = efi_dp_size(dp1);
272 unsigned sz2 = efi_dp_size(dp2);
273 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
277 memcpy(p + sz1, dp2, sz2);
278 memcpy(p + sz1 + sz2, &END, sizeof(END));
285 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
286 const struct efi_device_path *node)
288 struct efi_device_path *ret;
291 ret = efi_dp_dup(&END);
293 ret = efi_dp_dup(dp);
295 unsigned sz = node->length;
296 void *p = dp_alloc(sz + sizeof(END));
300 memcpy(p + sz, &END, sizeof(END));
303 /* both dp and node are non-null */
304 unsigned sz = efi_dp_size(dp);
305 void *p = dp_alloc(sz + node->length + sizeof(END));
309 memcpy(p + sz, node, node->length);
310 memcpy(p + sz + node->length, &END, sizeof(END));
318 /* size of device-path not including END node for device and all parents
319 * up to the root device.
321 static unsigned dp_size(struct udevice *dev)
323 if (!dev || !dev->driver)
326 switch (dev->driver->id) {
328 case UCLASS_SIMPLE_BUS:
329 /* stop traversing parents at this point: */
332 return dp_size(dev->parent) +
333 sizeof(struct efi_device_path_mac_addr);
336 switch (dev->parent->uclass->uc_drv->id) {
339 return dp_size(dev->parent) +
340 sizeof(struct efi_device_path_atapi);
342 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
344 return dp_size(dev->parent) +
345 sizeof(struct efi_device_path_scsi);
347 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
349 return dp_size(dev->parent) +
350 sizeof(struct efi_device_path_sd_mmc_path);
353 return dp_size(dev->parent);
356 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
358 return dp_size(dev->parent) +
359 sizeof(struct efi_device_path_sd_mmc_path);
361 case UCLASS_MASS_STORAGE:
363 return dp_size(dev->parent) +
364 sizeof(struct efi_device_path_usb_class);
366 /* just skip over unknown classes: */
367 return dp_size(dev->parent);
372 * Recursively build a device path.
374 * @buf pointer to the end of the device path
376 * @return pointer to the end of the device path
378 static void *dp_fill(void *buf, struct udevice *dev)
380 if (!dev || !dev->driver)
383 switch (dev->driver->id) {
385 case UCLASS_SIMPLE_BUS: {
386 /* stop traversing parents at this point: */
387 struct efi_device_path_vendor *vdp = buf;
393 struct efi_device_path_mac_addr *dp =
394 dp_fill(buf, dev->parent);
395 struct eth_pdata *pdata = dev->platdata;
397 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
398 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
399 dp->dp.length = sizeof(*dp);
400 memset(&dp->mac, 0, sizeof(dp->mac));
401 /* We only support IPv4 */
402 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
410 switch (dev->parent->uclass->uc_drv->id) {
413 struct efi_device_path_atapi *dp =
414 dp_fill(buf, dev->parent);
415 struct blk_desc *desc = dev_get_uclass_platdata(dev);
417 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
418 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
419 dp->dp.length = sizeof(*dp);
420 dp->logical_unit_number = desc->devnum;
421 dp->primary_secondary = IDE_BUS(desc->devnum);
422 dp->slave_master = desc->devnum %
423 (CONFIG_SYS_IDE_MAXDEVICE /
424 CONFIG_SYS_IDE_MAXBUS);
428 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
430 struct efi_device_path_scsi *dp =
431 dp_fill(buf, dev->parent);
432 struct blk_desc *desc = dev_get_uclass_platdata(dev);
434 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
435 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
436 dp->dp.length = sizeof(*dp);
437 dp->logical_unit_number = desc->lun;
438 dp->target_id = desc->target;
442 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
444 struct efi_device_path_sd_mmc_path *sddp =
445 dp_fill(buf, dev->parent);
446 struct blk_desc *desc = dev_get_uclass_platdata(dev);
448 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
449 sddp->dp.sub_type = is_sd(desc) ?
450 DEVICE_PATH_SUB_TYPE_MSG_SD :
451 DEVICE_PATH_SUB_TYPE_MSG_MMC;
452 sddp->dp.length = sizeof(*sddp);
453 sddp->slot_number = dev->seq;
458 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
459 __FILE__, __LINE__, __func__,
460 dev->name, dev->parent->uclass->uc_drv->id);
461 return dp_fill(buf, dev->parent);
464 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
466 struct efi_device_path_sd_mmc_path *sddp =
467 dp_fill(buf, dev->parent);
468 struct mmc *mmc = mmc_get_mmc_dev(dev);
469 struct blk_desc *desc = mmc_get_blk_desc(mmc);
471 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
472 sddp->dp.sub_type = is_sd(desc) ?
473 DEVICE_PATH_SUB_TYPE_MSG_SD :
474 DEVICE_PATH_SUB_TYPE_MSG_MMC;
475 sddp->dp.length = sizeof(*sddp);
476 sddp->slot_number = dev->seq;
481 case UCLASS_MASS_STORAGE:
482 case UCLASS_USB_HUB: {
483 struct efi_device_path_usb_class *udp =
484 dp_fill(buf, dev->parent);
485 struct usb_device *udev = dev_get_parent_priv(dev);
486 struct usb_device_descriptor *desc = &udev->descriptor;
488 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
489 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
490 udp->dp.length = sizeof(*udp);
491 udp->vendor_id = desc->idVendor;
492 udp->product_id = desc->idProduct;
493 udp->device_class = desc->bDeviceClass;
494 udp->device_subclass = desc->bDeviceSubClass;
495 udp->device_protocol = desc->bDeviceProtocol;
500 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
501 __FILE__, __LINE__, __func__,
502 dev->name, dev->driver->id);
503 return dp_fill(buf, dev->parent);
507 /* Construct a device-path from a device: */
508 struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
512 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
515 buf = dp_fill(buf, dev);
516 *((struct efi_device_path *)buf) = END;
522 static unsigned dp_part_size(struct blk_desc *desc, int part)
529 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
532 dev = desc->bdev->parent;
533 dpsize = dp_size(dev);
536 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
539 if (part == 0) /* the actual disk, not a partition */
542 if (desc->part_type == PART_TYPE_ISO)
543 dpsize += sizeof(struct efi_device_path_cdrom_path);
545 dpsize += sizeof(struct efi_device_path_hard_drive_path);
551 * Create a device node for a block device partition.
553 * @buf buffer to which the device path is wirtten
554 * @desc block device descriptor
555 * @part partition number, 0 identifies a block device
557 static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
559 disk_partition_t info;
561 part_get_info(desc, part, &info);
563 if (desc->part_type == PART_TYPE_ISO) {
564 struct efi_device_path_cdrom_path *cddp = buf;
566 cddp->boot_entry = part;
567 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
568 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
569 cddp->dp.length = sizeof(*cddp);
570 cddp->partition_start = info.start;
571 cddp->partition_end = info.size;
575 struct efi_device_path_hard_drive_path *hddp = buf;
577 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
578 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
579 hddp->dp.length = sizeof(*hddp);
580 hddp->partition_number = part;
581 hddp->partition_start = info.start;
582 hddp->partition_end = info.size;
583 if (desc->part_type == PART_TYPE_EFI)
584 hddp->partmap_type = 2;
586 hddp->partmap_type = 1;
588 switch (desc->sig_type) {
591 hddp->signature_type = 0;
592 memset(hddp->partition_signature, 0,
593 sizeof(hddp->partition_signature));
596 hddp->signature_type = 1;
597 memset(hddp->partition_signature, 0,
598 sizeof(hddp->partition_signature));
599 memcpy(hddp->partition_signature, &desc->mbr_sig,
600 sizeof(desc->mbr_sig));
603 hddp->signature_type = 2;
604 memcpy(hddp->partition_signature, &desc->guid_sig,
605 sizeof(hddp->partition_signature));
616 * Create a device path for a block device or one of its partitions.
618 * @buf buffer to which the device path is wirtten
619 * @desc block device descriptor
620 * @part partition number, 0 identifies a block device
622 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
627 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
630 dev = desc->bdev->parent;
631 buf = dp_fill(buf, dev);
635 * We *could* make a more accurate path, by looking at if_type
636 * and handling all the different cases like we do for non-
637 * legacy (ie CONFIG_BLK=y) case. But most important thing
638 * is just to have a unique device-path for if_type+devnum.
639 * So map things to a fictitious USB device.
641 struct efi_device_path_usb *udp;
643 memcpy(buf, &ROOT, sizeof(ROOT));
647 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
648 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
649 udp->dp.length = sizeof(*udp);
650 udp->parent_port_number = desc->if_type;
651 udp->usb_interface = desc->devnum;
655 if (part == 0) /* the actual disk, not a partition */
658 return dp_part_node(buf, desc, part);
661 /* Construct a device-path from a partition on a blk device: */
662 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
666 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
670 buf = dp_part_fill(buf, desc, part);
672 *((struct efi_device_path *)buf) = END;
678 * Create a device node for a block device partition.
680 * @buf buffer to which the device path is wirtten
681 * @desc block device descriptor
682 * @part partition number, 0 identifies a block device
684 struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
689 if (desc->part_type == PART_TYPE_ISO)
690 dpsize = sizeof(struct efi_device_path_cdrom_path);
692 dpsize = sizeof(struct efi_device_path_hard_drive_path);
693 buf = dp_alloc(dpsize);
695 dp_part_node(buf, desc, part);
700 /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
701 static void path_to_uefi(u16 *uefi, const char *path)
713 * If desc is NULL, this creates a path with only the file component,
714 * otherwise it creates a full path with both device and file components
716 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
719 struct efi_device_path_file_path *fp;
721 unsigned dpsize = 0, fpsize;
724 dpsize = dp_part_size(desc, part);
726 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
729 start = buf = dp_alloc(dpsize + sizeof(END));
734 buf = dp_part_fill(buf, desc, part);
738 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
739 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
740 fp->dp.length = fpsize;
741 path_to_uefi(fp->str, path);
744 *((struct efi_device_path *)buf) = END;
750 struct efi_device_path *efi_dp_from_eth(void)
752 struct efi_device_path_mac_addr *ndp;
756 assert(eth_get_dev());
759 dpsize += dp_size(eth_get_dev());
761 dpsize += sizeof(ROOT);
763 dpsize += sizeof(*ndp);
765 start = buf = dp_alloc(dpsize + sizeof(END));
770 buf = dp_fill(buf, eth_get_dev());
772 memcpy(buf, &ROOT, sizeof(ROOT));
777 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
778 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
779 ndp->dp.length = sizeof(*ndp);
780 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
783 *((struct efi_device_path *)buf) = END;
789 /* Construct a device-path for memory-mapped image */
790 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
791 uint64_t start_address,
792 uint64_t end_address)
794 struct efi_device_path_memory *mdp;
797 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
802 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
803 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
804 mdp->dp.length = sizeof(*mdp);
805 mdp->memory_type = memory_type;
806 mdp->start_address = start_address;
807 mdp->end_address = end_address;
810 *((struct efi_device_path *)buf) = END;
816 * Helper to split a full device path (containing both device and file
817 * parts) into it's constituent parts.
819 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
820 struct efi_device_path **device_path,
821 struct efi_device_path **file_path)
823 struct efi_device_path *p, *dp, *fp;
827 dp = efi_dp_dup(full_path);
829 return EFI_OUT_OF_RESOURCES;
831 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
834 return EFI_OUT_OF_RESOURCES;
838 return EFI_OUT_OF_RESOURCES;
839 p->type = DEVICE_PATH_TYPE_END;
840 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
841 p->length = sizeof(*p);