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>
19 /* template END node: */
20 static const struct efi_device_path END = {
21 .type = DEVICE_PATH_TYPE_END,
22 .sub_type = DEVICE_PATH_SUB_TYPE_END,
23 .length = sizeof(END),
27 EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
28 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b)
30 /* template ROOT node: */
31 static const struct efi_device_path_vendor ROOT = {
33 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
34 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
35 .length = sizeof(ROOT),
40 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
42 * Determine if an MMC device is an SD card.
44 * @desc block device descriptor
45 * @return true if the device is an SD card
47 static bool is_sd(struct blk_desc *desc)
49 struct mmc *mmc = find_mmc_device(desc->devnum);
54 return IS_SD(mmc) != 0U;
58 static void *dp_alloc(size_t sz)
62 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
64 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 efi_uintn_t dp_size = efi_dp_instance_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) +
172 efi_dp_instance_size(obj_dp);
175 /* Only return on exact matches */
176 if (efi_dp_instance_size(obj_dp) ==
182 obj_dp = shorten_path(efi_dp_next(obj_dp));
183 } while (short_path && obj_dp);
190 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
191 * remaining part of the device path after the matched object.
193 struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
194 struct efi_device_path **rem)
196 struct efi_object *efiobj;
198 /* Search for an exact match first */
199 efiobj = find_obj(dp, false, NULL);
201 /* Then for a fuzzy match */
203 efiobj = find_obj(dp, false, rem);
205 /* And now for a fuzzy short match */
207 efiobj = find_obj(dp, true, rem);
213 * Determine the last device path node that is not the end node.
216 * @return last node before the end node if it exists
219 const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
221 struct efi_device_path *ret;
223 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
226 ret = (struct efi_device_path *)dp;
227 dp = efi_dp_next(dp);
232 /* get size of the first device path instance excluding end node */
233 efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
237 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
241 dp = efi_dp_next(dp);
247 /* get size of multi-instance device path excluding end node */
248 efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
250 const struct efi_device_path *p = dp;
254 while (p->type != DEVICE_PATH_TYPE_END ||
255 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
256 p = (void *)p + p->length;
258 return (void *)p - (void *)dp;
261 /* copy multi-instance device path */
262 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
264 struct efi_device_path *ndp;
265 size_t sz = efi_dp_size(dp) + sizeof(END);
278 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
279 const struct efi_device_path *dp2)
281 struct efi_device_path *ret;
284 /* return an end node */
285 ret = efi_dp_dup(&END);
287 ret = efi_dp_dup(dp2);
289 ret = efi_dp_dup(dp1);
291 /* both dp1 and dp2 are non-null */
292 unsigned sz1 = efi_dp_size(dp1);
293 unsigned sz2 = efi_dp_size(dp2);
294 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
298 /* the end node of the second device path has to be retained */
299 memcpy(p + sz1, dp2, sz2 + sizeof(END));
306 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
307 const struct efi_device_path *node)
309 struct efi_device_path *ret;
312 ret = efi_dp_dup(&END);
314 ret = efi_dp_dup(dp);
316 size_t sz = node->length;
317 void *p = dp_alloc(sz + sizeof(END));
321 memcpy(p + sz, &END, sizeof(END));
324 /* both dp and node are non-null */
325 size_t sz = efi_dp_size(dp);
326 void *p = dp_alloc(sz + node->length + sizeof(END));
330 memcpy(p + sz, node, node->length);
331 memcpy(p + sz + node->length, &END, sizeof(END));
338 struct efi_device_path *efi_dp_create_device_node(const u8 type,
342 struct efi_device_path *ret;
344 ret = dp_alloc(length);
348 ret->sub_type = sub_type;
349 ret->length = length;
353 struct efi_device_path *efi_dp_append_instance(
354 const struct efi_device_path *dp,
355 const struct efi_device_path *dpi)
358 struct efi_device_path *p, *ret;
363 return efi_dp_dup(dpi);
364 sz = efi_dp_size(dp);
365 szi = efi_dp_instance_size(dpi);
366 p = dp_alloc(sz + szi + 2 * sizeof(END));
370 memcpy(p, dp, sz + sizeof(END));
372 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
373 p = (void *)p + sizeof(END);
376 memcpy(p, &END, sizeof(END));
380 struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
384 struct efi_device_path *p;
391 sz = efi_dp_instance_size(*dp);
392 p = dp_alloc(sz + sizeof(END));
395 memcpy(p, *dp, sz + sizeof(END));
396 *dp = (void *)*dp + sz;
397 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
398 *dp = (void *)*dp + sizeof(END);
402 *size = sz + sizeof(END);
406 bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
408 const struct efi_device_path *p = dp;
412 while (p->type != DEVICE_PATH_TYPE_END)
413 p = (void *)p + p->length;
414 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
418 /* size of device-path not including END node for device and all parents
419 * up to the root device.
421 static unsigned dp_size(struct udevice *dev)
423 if (!dev || !dev->driver)
426 switch (dev->driver->id) {
428 case UCLASS_SIMPLE_BUS:
429 /* stop traversing parents at this point: */
432 return dp_size(dev->parent) +
433 sizeof(struct efi_device_path_mac_addr);
436 switch (dev->parent->uclass->uc_drv->id) {
439 return dp_size(dev->parent) +
440 sizeof(struct efi_device_path_atapi);
442 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
444 return dp_size(dev->parent) +
445 sizeof(struct efi_device_path_scsi);
447 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
449 return dp_size(dev->parent) +
450 sizeof(struct efi_device_path_sd_mmc_path);
453 return dp_size(dev->parent);
456 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
458 return dp_size(dev->parent) +
459 sizeof(struct efi_device_path_sd_mmc_path);
461 case UCLASS_MASS_STORAGE:
463 return dp_size(dev->parent) +
464 sizeof(struct efi_device_path_usb_class);
466 /* just skip over unknown classes: */
467 return dp_size(dev->parent);
472 * Recursively build a device path.
474 * @buf pointer to the end of the device path
476 * @return pointer to the end of the device path
478 static void *dp_fill(void *buf, struct udevice *dev)
480 if (!dev || !dev->driver)
483 switch (dev->driver->id) {
485 case UCLASS_SIMPLE_BUS: {
486 /* stop traversing parents at this point: */
487 struct efi_device_path_vendor *vdp = buf;
493 struct efi_device_path_mac_addr *dp =
494 dp_fill(buf, dev->parent);
495 struct eth_pdata *pdata = dev->platdata;
497 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
498 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
499 dp->dp.length = sizeof(*dp);
500 memset(&dp->mac, 0, sizeof(dp->mac));
501 /* We only support IPv4 */
502 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
510 switch (dev->parent->uclass->uc_drv->id) {
513 struct efi_device_path_atapi *dp =
514 dp_fill(buf, dev->parent);
515 struct blk_desc *desc = dev_get_uclass_platdata(dev);
517 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
518 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
519 dp->dp.length = sizeof(*dp);
520 dp->logical_unit_number = desc->devnum;
521 dp->primary_secondary = IDE_BUS(desc->devnum);
522 dp->slave_master = desc->devnum %
523 (CONFIG_SYS_IDE_MAXDEVICE /
524 CONFIG_SYS_IDE_MAXBUS);
528 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
530 struct efi_device_path_scsi *dp =
531 dp_fill(buf, dev->parent);
532 struct blk_desc *desc = dev_get_uclass_platdata(dev);
534 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
535 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
536 dp->dp.length = sizeof(*dp);
537 dp->logical_unit_number = desc->lun;
538 dp->target_id = desc->target;
542 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
544 struct efi_device_path_sd_mmc_path *sddp =
545 dp_fill(buf, dev->parent);
546 struct blk_desc *desc = dev_get_uclass_platdata(dev);
548 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
549 sddp->dp.sub_type = is_sd(desc) ?
550 DEVICE_PATH_SUB_TYPE_MSG_SD :
551 DEVICE_PATH_SUB_TYPE_MSG_MMC;
552 sddp->dp.length = sizeof(*sddp);
553 sddp->slot_number = dev->seq;
558 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
559 __FILE__, __LINE__, __func__,
560 dev->name, dev->parent->uclass->uc_drv->id);
561 return dp_fill(buf, dev->parent);
564 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
566 struct efi_device_path_sd_mmc_path *sddp =
567 dp_fill(buf, dev->parent);
568 struct mmc *mmc = mmc_get_mmc_dev(dev);
569 struct blk_desc *desc = mmc_get_blk_desc(mmc);
571 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
572 sddp->dp.sub_type = is_sd(desc) ?
573 DEVICE_PATH_SUB_TYPE_MSG_SD :
574 DEVICE_PATH_SUB_TYPE_MSG_MMC;
575 sddp->dp.length = sizeof(*sddp);
576 sddp->slot_number = dev->seq;
581 case UCLASS_MASS_STORAGE:
582 case UCLASS_USB_HUB: {
583 struct efi_device_path_usb_class *udp =
584 dp_fill(buf, dev->parent);
585 struct usb_device *udev = dev_get_parent_priv(dev);
586 struct usb_device_descriptor *desc = &udev->descriptor;
588 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
589 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
590 udp->dp.length = sizeof(*udp);
591 udp->vendor_id = desc->idVendor;
592 udp->product_id = desc->idProduct;
593 udp->device_class = desc->bDeviceClass;
594 udp->device_subclass = desc->bDeviceSubClass;
595 udp->device_protocol = desc->bDeviceProtocol;
600 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
601 __FILE__, __LINE__, __func__,
602 dev->name, dev->driver->id);
603 return dp_fill(buf, dev->parent);
607 /* Construct a device-path from a device: */
608 struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
612 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
615 buf = dp_fill(buf, dev);
616 *((struct efi_device_path *)buf) = END;
622 static unsigned dp_part_size(struct blk_desc *desc, int part)
629 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
632 dev = desc->bdev->parent;
633 dpsize = dp_size(dev);
636 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
639 if (part == 0) /* the actual disk, not a partition */
642 if (desc->part_type == PART_TYPE_ISO)
643 dpsize += sizeof(struct efi_device_path_cdrom_path);
645 dpsize += sizeof(struct efi_device_path_hard_drive_path);
651 * Create a device node for a block device partition.
653 * @buf buffer to which the device path is wirtten
654 * @desc block device descriptor
655 * @part partition number, 0 identifies a block device
657 static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
659 disk_partition_t info;
661 part_get_info(desc, part, &info);
663 if (desc->part_type == PART_TYPE_ISO) {
664 struct efi_device_path_cdrom_path *cddp = buf;
666 cddp->boot_entry = part;
667 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
668 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
669 cddp->dp.length = sizeof(*cddp);
670 cddp->partition_start = info.start;
671 cddp->partition_end = info.size;
675 struct efi_device_path_hard_drive_path *hddp = buf;
677 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
678 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
679 hddp->dp.length = sizeof(*hddp);
680 hddp->partition_number = part;
681 hddp->partition_start = info.start;
682 hddp->partition_end = info.size;
683 if (desc->part_type == PART_TYPE_EFI)
684 hddp->partmap_type = 2;
686 hddp->partmap_type = 1;
688 switch (desc->sig_type) {
691 hddp->signature_type = 0;
692 memset(hddp->partition_signature, 0,
693 sizeof(hddp->partition_signature));
696 hddp->signature_type = 1;
697 memset(hddp->partition_signature, 0,
698 sizeof(hddp->partition_signature));
699 memcpy(hddp->partition_signature, &desc->mbr_sig,
700 sizeof(desc->mbr_sig));
703 hddp->signature_type = 2;
704 memcpy(hddp->partition_signature, &desc->guid_sig,
705 sizeof(hddp->partition_signature));
716 * Create a device path for a block device or one of its partitions.
718 * @buf buffer to which the device path is wirtten
719 * @desc block device descriptor
720 * @part partition number, 0 identifies a block device
722 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
727 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
730 dev = desc->bdev->parent;
731 buf = dp_fill(buf, dev);
735 * We *could* make a more accurate path, by looking at if_type
736 * and handling all the different cases like we do for non-
737 * legacy (ie CONFIG_BLK=y) case. But most important thing
738 * is just to have a unique device-path for if_type+devnum.
739 * So map things to a fictitious USB device.
741 struct efi_device_path_usb *udp;
743 memcpy(buf, &ROOT, sizeof(ROOT));
747 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
748 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
749 udp->dp.length = sizeof(*udp);
750 udp->parent_port_number = desc->if_type;
751 udp->usb_interface = desc->devnum;
755 if (part == 0) /* the actual disk, not a partition */
758 return dp_part_node(buf, desc, part);
761 /* Construct a device-path from a partition on a blk device: */
762 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
766 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
770 buf = dp_part_fill(buf, desc, part);
772 *((struct efi_device_path *)buf) = END;
778 * Create a device node for a block device partition.
780 * @buf buffer to which the device path is wirtten
781 * @desc block device descriptor
782 * @part partition number, 0 identifies a block device
784 struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
789 if (desc->part_type == PART_TYPE_ISO)
790 dpsize = sizeof(struct efi_device_path_cdrom_path);
792 dpsize = sizeof(struct efi_device_path_hard_drive_path);
793 buf = dp_alloc(dpsize);
795 dp_part_node(buf, desc, part);
800 /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
801 static void path_to_uefi(u16 *uefi, const char *path)
813 * If desc is NULL, this creates a path with only the file component,
814 * otherwise it creates a full path with both device and file components
816 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
819 struct efi_device_path_file_path *fp;
821 unsigned dpsize = 0, fpsize;
824 dpsize = dp_part_size(desc, part);
826 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
829 start = buf = dp_alloc(dpsize + sizeof(END));
834 buf = dp_part_fill(buf, desc, part);
838 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
839 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
840 fp->dp.length = fpsize;
841 path_to_uefi(fp->str, path);
844 *((struct efi_device_path *)buf) = END;
850 struct efi_device_path *efi_dp_from_eth(void)
852 #ifndef CONFIG_DM_ETH
853 struct efi_device_path_mac_addr *ndp;
858 assert(eth_get_dev());
861 dpsize += dp_size(eth_get_dev());
863 dpsize += sizeof(ROOT);
864 dpsize += sizeof(*ndp);
867 start = buf = dp_alloc(dpsize + sizeof(END));
872 buf = dp_fill(buf, eth_get_dev());
874 memcpy(buf, &ROOT, sizeof(ROOT));
878 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
879 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
880 ndp->dp.length = sizeof(*ndp);
881 ndp->if_type = 1; /* Ethernet */
882 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
886 *((struct efi_device_path *)buf) = END;
892 /* Construct a device-path for memory-mapped image */
893 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
894 uint64_t start_address,
895 uint64_t end_address)
897 struct efi_device_path_memory *mdp;
900 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
905 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
906 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
907 mdp->dp.length = sizeof(*mdp);
908 mdp->memory_type = memory_type;
909 mdp->start_address = start_address;
910 mdp->end_address = end_address;
913 *((struct efi_device_path *)buf) = END;
919 * Helper to split a full device path (containing both device and file
920 * parts) into it's constituent parts.
922 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
923 struct efi_device_path **device_path,
924 struct efi_device_path **file_path)
926 struct efi_device_path *p, *dp, *fp;
930 dp = efi_dp_dup(full_path);
932 return EFI_OUT_OF_RESOURCES;
934 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
937 return EFI_OUT_OF_RESOURCES;
941 return EFI_OUT_OF_RESOURCES;
942 p->type = DEVICE_PATH_TYPE_END;
943 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
944 p->length = sizeof(*p);