1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI device path from u-boot device-model mapping
5 * (C) Copyright 2017 Rob Clark
8 #define LOG_CATEGORY LOGL_ERR
15 #include <efi_loader.h>
18 /* template END node: */
19 static const struct efi_device_path END = {
20 .type = DEVICE_PATH_TYPE_END,
21 .sub_type = DEVICE_PATH_SUB_TYPE_END,
22 .length = sizeof(END),
26 EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
27 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b)
29 /* template ROOT node: */
30 static const struct efi_device_path_vendor ROOT = {
32 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
33 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
34 .length = sizeof(ROOT),
39 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
41 * Determine if an MMC device is an SD card.
43 * @desc block device descriptor
44 * @return true if the device is an SD card
46 static bool is_sd(struct blk_desc *desc)
48 struct mmc *mmc = find_mmc_device(desc->devnum);
53 return IS_SD(mmc) != 0U;
57 static void *dp_alloc(size_t sz)
61 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
63 debug("EFI: ERROR: out of memory in %s\n", __func__);
72 * Iterate to next block in device-path, terminating (returning NULL)
75 struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
79 if (dp->type == DEVICE_PATH_TYPE_END)
81 dp = ((void *)dp) + dp->length;
82 if (dp->type == DEVICE_PATH_TYPE_END)
84 return (struct efi_device_path *)dp;
88 * Compare two device-paths, stopping when the shorter of the two hits
89 * an End* node. This is useful to, for example, compare a device-path
90 * representing a device with one representing a file on the device, or
91 * a device with a parent device.
93 int efi_dp_match(const struct efi_device_path *a,
94 const struct efi_device_path *b)
99 ret = memcmp(&a->length, &b->length, sizeof(a->length));
103 ret = memcmp(a, b, a->length);
116 * See UEFI spec (section 3.1.2, about short-form device-paths..
117 * tl;dr: we can have a device-path that starts with a USB WWID
118 * or USB Class node, and a few other cases which don't encode
119 * the full device path with bus hierarchy:
121 * - MESSAGING:USB_WWID
122 * - MESSAGING:USB_CLASS
127 static struct efi_device_path *shorten_path(struct efi_device_path *dp)
131 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
132 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
133 * so not sure when we would see these other cases.
135 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
136 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
137 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
140 dp = efi_dp_next(dp);
146 static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
147 struct efi_device_path **rem)
149 struct efi_object *efiobj;
150 efi_uintn_t dp_size = efi_dp_instance_size(dp);
152 list_for_each_entry(efiobj, &efi_obj_list, link) {
153 struct efi_handler *handler;
154 struct efi_device_path *obj_dp;
157 ret = efi_search_protocol(efiobj->handle,
158 &efi_guid_device_path, &handler);
159 if (ret != EFI_SUCCESS)
161 obj_dp = handler->protocol_interface;
164 if (efi_dp_match(dp, obj_dp) == 0) {
167 * Allow partial matches, but inform
170 *rem = ((void *)dp) +
171 efi_dp_instance_size(obj_dp);
174 /* Only return on exact matches */
175 if (efi_dp_instance_size(obj_dp) ==
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 /* get size of the first device path instance excluding end node */
232 efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
236 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
240 dp = efi_dp_next(dp);
246 /* get size of multi-instance device path excluding end node */
247 efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
249 const struct efi_device_path *p = dp;
253 while (p->type != DEVICE_PATH_TYPE_END ||
254 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
255 p = (void *)p + p->length;
257 return (void *)p - (void *)dp;
260 /* copy multi-instance device path */
261 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
263 struct efi_device_path *ndp;
264 size_t sz = efi_dp_size(dp) + sizeof(END);
277 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
278 const struct efi_device_path *dp2)
280 struct efi_device_path *ret;
283 /* return an end node */
284 ret = efi_dp_dup(&END);
286 ret = efi_dp_dup(dp2);
288 ret = efi_dp_dup(dp1);
290 /* both dp1 and dp2 are non-null */
291 unsigned sz1 = efi_dp_size(dp1);
292 unsigned sz2 = efi_dp_size(dp2);
293 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
297 /* the end node of the second device path has to be retained */
298 memcpy(p + sz1, dp2, sz2 + sizeof(END));
305 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
306 const struct efi_device_path *node)
308 struct efi_device_path *ret;
311 ret = efi_dp_dup(&END);
313 ret = efi_dp_dup(dp);
315 size_t sz = node->length;
316 void *p = dp_alloc(sz + sizeof(END));
320 memcpy(p + sz, &END, sizeof(END));
323 /* both dp and node are non-null */
324 size_t sz = efi_dp_size(dp);
325 void *p = dp_alloc(sz + node->length + sizeof(END));
329 memcpy(p + sz, node, node->length);
330 memcpy(p + sz + node->length, &END, sizeof(END));
337 struct efi_device_path *efi_dp_create_device_node(const u8 type,
341 struct efi_device_path *ret;
343 ret = dp_alloc(length);
347 ret->sub_type = sub_type;
348 ret->length = length;
352 struct efi_device_path *efi_dp_append_instance(
353 const struct efi_device_path *dp,
354 const struct efi_device_path *dpi)
357 struct efi_device_path *p, *ret;
362 return efi_dp_dup(dpi);
363 sz = efi_dp_size(dp);
364 szi = efi_dp_instance_size(dpi);
365 p = dp_alloc(sz + szi + 2 * sizeof(END));
369 memcpy(p, dp, sz + sizeof(END));
371 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
372 p = (void *)p + sizeof(END);
375 memcpy(p, &END, sizeof(END));
379 struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
383 struct efi_device_path *p;
390 sz = efi_dp_instance_size(*dp);
391 p = dp_alloc(sz + sizeof(END));
394 memcpy(p, *dp, sz + sizeof(END));
395 *dp = (void *)*dp + sz;
396 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
397 *dp = (void *)*dp + sizeof(END);
401 *size = sz + sizeof(END);
405 bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
407 const struct efi_device_path *p = dp;
411 while (p->type != DEVICE_PATH_TYPE_END)
412 p = (void *)p + p->length;
413 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
417 /* size of device-path not including END node for device and all parents
418 * up to the root device.
420 static unsigned dp_size(struct udevice *dev)
422 if (!dev || !dev->driver)
425 switch (dev->driver->id) {
427 case UCLASS_SIMPLE_BUS:
428 /* stop traversing parents at this point: */
431 return dp_size(dev->parent) +
432 sizeof(struct efi_device_path_mac_addr);
435 switch (dev->parent->uclass->uc_drv->id) {
438 return dp_size(dev->parent) +
439 sizeof(struct efi_device_path_atapi);
441 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
443 return dp_size(dev->parent) +
444 sizeof(struct efi_device_path_scsi);
446 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
448 return dp_size(dev->parent) +
449 sizeof(struct efi_device_path_sd_mmc_path);
452 return dp_size(dev->parent);
455 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
457 return dp_size(dev->parent) +
458 sizeof(struct efi_device_path_sd_mmc_path);
460 case UCLASS_MASS_STORAGE:
462 return dp_size(dev->parent) +
463 sizeof(struct efi_device_path_usb_class);
465 /* just skip over unknown classes: */
466 return dp_size(dev->parent);
471 * Recursively build a device path.
473 * @buf pointer to the end of the device path
475 * @return pointer to the end of the device path
477 static void *dp_fill(void *buf, struct udevice *dev)
479 if (!dev || !dev->driver)
482 switch (dev->driver->id) {
484 case UCLASS_SIMPLE_BUS: {
485 /* stop traversing parents at this point: */
486 struct efi_device_path_vendor *vdp = buf;
492 struct efi_device_path_mac_addr *dp =
493 dp_fill(buf, dev->parent);
494 struct eth_pdata *pdata = dev->platdata;
496 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
497 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
498 dp->dp.length = sizeof(*dp);
499 memset(&dp->mac, 0, sizeof(dp->mac));
500 /* We only support IPv4 */
501 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
509 switch (dev->parent->uclass->uc_drv->id) {
512 struct efi_device_path_atapi *dp =
513 dp_fill(buf, dev->parent);
514 struct blk_desc *desc = dev_get_uclass_platdata(dev);
516 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
517 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
518 dp->dp.length = sizeof(*dp);
519 dp->logical_unit_number = desc->devnum;
520 dp->primary_secondary = IDE_BUS(desc->devnum);
521 dp->slave_master = desc->devnum %
522 (CONFIG_SYS_IDE_MAXDEVICE /
523 CONFIG_SYS_IDE_MAXBUS);
527 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
529 struct efi_device_path_scsi *dp =
530 dp_fill(buf, dev->parent);
531 struct blk_desc *desc = dev_get_uclass_platdata(dev);
533 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
534 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
535 dp->dp.length = sizeof(*dp);
536 dp->logical_unit_number = desc->lun;
537 dp->target_id = desc->target;
541 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
543 struct efi_device_path_sd_mmc_path *sddp =
544 dp_fill(buf, dev->parent);
545 struct blk_desc *desc = dev_get_uclass_platdata(dev);
547 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
548 sddp->dp.sub_type = is_sd(desc) ?
549 DEVICE_PATH_SUB_TYPE_MSG_SD :
550 DEVICE_PATH_SUB_TYPE_MSG_MMC;
551 sddp->dp.length = sizeof(*sddp);
552 sddp->slot_number = dev->seq;
557 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
558 __FILE__, __LINE__, __func__,
559 dev->name, dev->parent->uclass->uc_drv->id);
560 return dp_fill(buf, dev->parent);
563 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
565 struct efi_device_path_sd_mmc_path *sddp =
566 dp_fill(buf, dev->parent);
567 struct mmc *mmc = mmc_get_mmc_dev(dev);
568 struct blk_desc *desc = mmc_get_blk_desc(mmc);
570 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
571 sddp->dp.sub_type = is_sd(desc) ?
572 DEVICE_PATH_SUB_TYPE_MSG_SD :
573 DEVICE_PATH_SUB_TYPE_MSG_MMC;
574 sddp->dp.length = sizeof(*sddp);
575 sddp->slot_number = dev->seq;
580 case UCLASS_MASS_STORAGE:
581 case UCLASS_USB_HUB: {
582 struct efi_device_path_usb_class *udp =
583 dp_fill(buf, dev->parent);
584 struct usb_device *udev = dev_get_parent_priv(dev);
585 struct usb_device_descriptor *desc = &udev->descriptor;
587 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
588 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
589 udp->dp.length = sizeof(*udp);
590 udp->vendor_id = desc->idVendor;
591 udp->product_id = desc->idProduct;
592 udp->device_class = desc->bDeviceClass;
593 udp->device_subclass = desc->bDeviceSubClass;
594 udp->device_protocol = desc->bDeviceProtocol;
599 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
600 __FILE__, __LINE__, __func__,
601 dev->name, dev->driver->id);
602 return dp_fill(buf, dev->parent);
606 /* Construct a device-path from a device: */
607 struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
611 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
614 buf = dp_fill(buf, dev);
615 *((struct efi_device_path *)buf) = END;
621 static unsigned dp_part_size(struct blk_desc *desc, int part)
628 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
631 dev = desc->bdev->parent;
632 dpsize = dp_size(dev);
635 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
638 if (part == 0) /* the actual disk, not a partition */
641 if (desc->part_type == PART_TYPE_ISO)
642 dpsize += sizeof(struct efi_device_path_cdrom_path);
644 dpsize += sizeof(struct efi_device_path_hard_drive_path);
650 * Create a device node for a block device partition.
652 * @buf buffer to which the device path is wirtten
653 * @desc block device descriptor
654 * @part partition number, 0 identifies a block device
656 static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
658 disk_partition_t info;
660 part_get_info(desc, part, &info);
662 if (desc->part_type == PART_TYPE_ISO) {
663 struct efi_device_path_cdrom_path *cddp = buf;
665 cddp->boot_entry = part;
666 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
667 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
668 cddp->dp.length = sizeof(*cddp);
669 cddp->partition_start = info.start;
670 cddp->partition_end = info.size;
674 struct efi_device_path_hard_drive_path *hddp = buf;
676 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
677 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
678 hddp->dp.length = sizeof(*hddp);
679 hddp->partition_number = part;
680 hddp->partition_start = info.start;
681 hddp->partition_end = info.size;
682 if (desc->part_type == PART_TYPE_EFI)
683 hddp->partmap_type = 2;
685 hddp->partmap_type = 1;
687 switch (desc->sig_type) {
690 hddp->signature_type = 0;
691 memset(hddp->partition_signature, 0,
692 sizeof(hddp->partition_signature));
695 hddp->signature_type = 1;
696 memset(hddp->partition_signature, 0,
697 sizeof(hddp->partition_signature));
698 memcpy(hddp->partition_signature, &desc->mbr_sig,
699 sizeof(desc->mbr_sig));
702 hddp->signature_type = 2;
703 memcpy(hddp->partition_signature, &desc->guid_sig,
704 sizeof(hddp->partition_signature));
715 * Create a device path for a block device or one of its partitions.
717 * @buf buffer to which the device path is wirtten
718 * @desc block device descriptor
719 * @part partition number, 0 identifies a block device
721 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
726 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
729 dev = desc->bdev->parent;
730 buf = dp_fill(buf, dev);
734 * We *could* make a more accurate path, by looking at if_type
735 * and handling all the different cases like we do for non-
736 * legacy (ie CONFIG_BLK=y) case. But most important thing
737 * is just to have a unique device-path for if_type+devnum.
738 * So map things to a fictitious USB device.
740 struct efi_device_path_usb *udp;
742 memcpy(buf, &ROOT, sizeof(ROOT));
746 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
747 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
748 udp->dp.length = sizeof(*udp);
749 udp->parent_port_number = desc->if_type;
750 udp->usb_interface = desc->devnum;
754 if (part == 0) /* the actual disk, not a partition */
757 return dp_part_node(buf, desc, part);
760 /* Construct a device-path from a partition on a blk device: */
761 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
765 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
769 buf = dp_part_fill(buf, desc, part);
771 *((struct efi_device_path *)buf) = END;
777 * Create a device node for a block device partition.
779 * @buf buffer to which the device path is wirtten
780 * @desc block device descriptor
781 * @part partition number, 0 identifies a block device
783 struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
788 if (desc->part_type == PART_TYPE_ISO)
789 dpsize = sizeof(struct efi_device_path_cdrom_path);
791 dpsize = sizeof(struct efi_device_path_hard_drive_path);
792 buf = dp_alloc(dpsize);
794 dp_part_node(buf, desc, part);
799 /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
800 static void path_to_uefi(u16 *uefi, const char *path)
812 * If desc is NULL, this creates a path with only the file component,
813 * otherwise it creates a full path with both device and file components
815 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
818 struct efi_device_path_file_path *fp;
820 unsigned dpsize = 0, fpsize;
823 dpsize = dp_part_size(desc, part);
825 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
828 start = buf = dp_alloc(dpsize + sizeof(END));
833 buf = dp_part_fill(buf, desc, part);
837 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
838 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
839 fp->dp.length = fpsize;
840 path_to_uefi(fp->str, path);
843 *((struct efi_device_path *)buf) = END;
849 struct efi_device_path *efi_dp_from_eth(void)
851 #ifndef CONFIG_DM_ETH
852 struct efi_device_path_mac_addr *ndp;
857 assert(eth_get_dev());
860 dpsize += dp_size(eth_get_dev());
862 dpsize += sizeof(ROOT);
863 dpsize += sizeof(*ndp);
866 start = buf = dp_alloc(dpsize + sizeof(END));
871 buf = dp_fill(buf, eth_get_dev());
873 memcpy(buf, &ROOT, sizeof(ROOT));
877 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
878 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
879 ndp->dp.length = sizeof(*ndp);
880 ndp->if_type = 1; /* Ethernet */
881 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
885 *((struct efi_device_path *)buf) = END;
891 /* Construct a device-path for memory-mapped image */
892 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
893 uint64_t start_address,
894 uint64_t end_address)
896 struct efi_device_path_memory *mdp;
899 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
904 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
905 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
906 mdp->dp.length = sizeof(*mdp);
907 mdp->memory_type = memory_type;
908 mdp->start_address = start_address;
909 mdp->end_address = end_address;
912 *((struct efi_device_path *)buf) = END;
918 * Helper to split a full device path (containing both device and file
919 * parts) into it's constituent parts.
921 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
922 struct efi_device_path **device_path,
923 struct efi_device_path **file_path)
925 struct efi_device_path *p, *dp, *fp;
929 dp = efi_dp_dup(full_path);
931 return EFI_OUT_OF_RESOURCES;
933 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
936 return EFI_OUT_OF_RESOURCES;
940 return EFI_OUT_OF_RESOURCES;
941 p->type = DEVICE_PATH_TYPE_END;
942 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
943 p->length = sizeof(*p);