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),
25 /* template ROOT node: */
26 static const struct efi_device_path_vendor ROOT = {
28 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
29 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
30 .length = sizeof(ROOT),
35 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
37 * Determine if an MMC device is an SD card.
39 * @desc block device descriptor
40 * @return true if the device is an SD card
42 static bool is_sd(struct blk_desc *desc)
44 struct mmc *mmc = find_mmc_device(desc->devnum);
49 return IS_SD(mmc) != 0U;
53 static void *dp_alloc(size_t sz)
57 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
59 debug("EFI: ERROR: out of memory in %s\n", __func__);
68 * Iterate to next block in device-path, terminating (returning NULL)
71 struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
75 if (dp->type == DEVICE_PATH_TYPE_END)
77 dp = ((void *)dp) + dp->length;
78 if (dp->type == DEVICE_PATH_TYPE_END)
80 return (struct efi_device_path *)dp;
84 * Compare two device-paths, stopping when the shorter of the two hits
85 * an End* node. This is useful to, for example, compare a device-path
86 * representing a device with one representing a file on the device, or
87 * a device with a parent device.
89 int efi_dp_match(const struct efi_device_path *a,
90 const struct efi_device_path *b)
95 ret = memcmp(&a->length, &b->length, sizeof(a->length));
99 ret = memcmp(a, b, a->length);
112 * We can have device paths that start with a USB WWID or a USB Class node,
113 * and a few other cases which don't encode the full device path with bus
116 * - MESSAGING:USB_WWID
117 * - MESSAGING:USB_CLASS
122 * See UEFI spec (section 3.1.2, about short-form device-paths)
124 static struct efi_device_path *shorten_path(struct efi_device_path *dp)
128 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
129 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
130 * so not sure when we would see these other cases.
132 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
133 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
134 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
137 dp = efi_dp_next(dp);
143 static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
144 struct efi_device_path **rem)
146 struct efi_object *efiobj;
147 efi_uintn_t dp_size = efi_dp_instance_size(dp);
149 list_for_each_entry(efiobj, &efi_obj_list, link) {
150 struct efi_handler *handler;
151 struct efi_device_path *obj_dp;
154 ret = efi_search_protocol(efiobj,
155 &efi_guid_device_path, &handler);
156 if (ret != EFI_SUCCESS)
158 obj_dp = handler->protocol_interface;
161 if (efi_dp_match(dp, obj_dp) == 0) {
164 * Allow partial matches, but inform
167 *rem = ((void *)dp) +
168 efi_dp_instance_size(obj_dp);
171 /* Only return on exact matches */
172 if (efi_dp_instance_size(obj_dp) ==
178 obj_dp = shorten_path(efi_dp_next(obj_dp));
179 } while (short_path && obj_dp);
186 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
187 * remaining part of the device path after the matched object.
189 struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
190 struct efi_device_path **rem)
192 struct efi_object *efiobj;
194 /* Search for an exact match first */
195 efiobj = find_obj(dp, false, NULL);
197 /* Then for a fuzzy match */
199 efiobj = find_obj(dp, false, rem);
201 /* And now for a fuzzy short match */
203 efiobj = find_obj(dp, true, rem);
209 * Determine the last device path node that is not the end node.
212 * @return last node before the end node if it exists
215 const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
217 struct efi_device_path *ret;
219 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
222 ret = (struct efi_device_path *)dp;
223 dp = efi_dp_next(dp);
228 /* get size of the first device path instance excluding end node */
229 efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
233 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
237 dp = efi_dp_next(dp);
243 /* get size of multi-instance device path excluding end node */
244 efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
246 const struct efi_device_path *p = dp;
250 while (p->type != DEVICE_PATH_TYPE_END ||
251 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
252 p = (void *)p + p->length;
254 return (void *)p - (void *)dp;
257 /* copy multi-instance device path */
258 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
260 struct efi_device_path *ndp;
261 size_t sz = efi_dp_size(dp) + sizeof(END);
274 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
275 const struct efi_device_path *dp2)
277 struct efi_device_path *ret;
280 /* return an end node */
281 ret = efi_dp_dup(&END);
283 ret = efi_dp_dup(dp2);
285 ret = efi_dp_dup(dp1);
287 /* both dp1 and dp2 are non-null */
288 unsigned sz1 = efi_dp_size(dp1);
289 unsigned sz2 = efi_dp_size(dp2);
290 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
294 /* the end node of the second device path has to be retained */
295 memcpy(p + sz1, dp2, sz2 + sizeof(END));
302 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
303 const struct efi_device_path *node)
305 struct efi_device_path *ret;
308 ret = efi_dp_dup(&END);
310 ret = efi_dp_dup(dp);
312 size_t sz = node->length;
313 void *p = dp_alloc(sz + sizeof(END));
317 memcpy(p + sz, &END, sizeof(END));
320 /* both dp and node are non-null */
321 size_t sz = efi_dp_size(dp);
322 void *p = dp_alloc(sz + node->length + sizeof(END));
326 memcpy(p + sz, node, node->length);
327 memcpy(p + sz + node->length, &END, sizeof(END));
334 struct efi_device_path *efi_dp_create_device_node(const u8 type,
338 struct efi_device_path *ret;
340 ret = dp_alloc(length);
344 ret->sub_type = sub_type;
345 ret->length = length;
349 struct efi_device_path *efi_dp_append_instance(
350 const struct efi_device_path *dp,
351 const struct efi_device_path *dpi)
354 struct efi_device_path *p, *ret;
359 return efi_dp_dup(dpi);
360 sz = efi_dp_size(dp);
361 szi = efi_dp_instance_size(dpi);
362 p = dp_alloc(sz + szi + 2 * sizeof(END));
366 memcpy(p, dp, sz + sizeof(END));
368 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
369 p = (void *)p + sizeof(END);
372 memcpy(p, &END, sizeof(END));
376 struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
380 struct efi_device_path *p;
386 sz = efi_dp_instance_size(*dp);
387 p = dp_alloc(sz + sizeof(END));
390 memcpy(p, *dp, sz + sizeof(END));
391 *dp = (void *)*dp + sz;
392 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
393 *dp = (void *)*dp + sizeof(END);
397 *size = sz + sizeof(END);
401 bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
403 const struct efi_device_path *p = dp;
407 while (p->type != DEVICE_PATH_TYPE_END)
408 p = (void *)p + p->length;
409 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
413 /* size of device-path not including END node for device and all parents
414 * up to the root device.
416 static unsigned dp_size(struct udevice *dev)
418 if (!dev || !dev->driver)
421 switch (dev->driver->id) {
423 case UCLASS_SIMPLE_BUS:
424 /* stop traversing parents at this point: */
427 return dp_size(dev->parent) +
428 sizeof(struct efi_device_path_mac_addr);
431 switch (dev->parent->uclass->uc_drv->id) {
434 return dp_size(dev->parent) +
435 sizeof(struct efi_device_path_atapi);
437 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
439 return dp_size(dev->parent) +
440 sizeof(struct efi_device_path_scsi);
442 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
444 return dp_size(dev->parent) +
445 sizeof(struct efi_device_path_sd_mmc_path);
448 return dp_size(dev->parent);
451 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
453 return dp_size(dev->parent) +
454 sizeof(struct efi_device_path_sd_mmc_path);
456 case UCLASS_MASS_STORAGE:
458 return dp_size(dev->parent) +
459 sizeof(struct efi_device_path_usb_class);
461 /* just skip over unknown classes: */
462 return dp_size(dev->parent);
467 * Recursively build a device path.
469 * @buf pointer to the end of the device path
471 * @return pointer to the end of the device path
473 static void *dp_fill(void *buf, struct udevice *dev)
475 if (!dev || !dev->driver)
478 switch (dev->driver->id) {
480 case UCLASS_SIMPLE_BUS: {
481 /* stop traversing parents at this point: */
482 struct efi_device_path_vendor *vdp = buf;
488 struct efi_device_path_mac_addr *dp =
489 dp_fill(buf, dev->parent);
490 struct eth_pdata *pdata = dev->platdata;
492 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
493 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
494 dp->dp.length = sizeof(*dp);
495 memset(&dp->mac, 0, sizeof(dp->mac));
496 /* We only support IPv4 */
497 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
505 switch (dev->parent->uclass->uc_drv->id) {
508 struct efi_device_path_atapi *dp =
509 dp_fill(buf, dev->parent);
510 struct blk_desc *desc = dev_get_uclass_platdata(dev);
512 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
513 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
514 dp->dp.length = sizeof(*dp);
515 dp->logical_unit_number = desc->devnum;
516 dp->primary_secondary = IDE_BUS(desc->devnum);
517 dp->slave_master = desc->devnum %
518 (CONFIG_SYS_IDE_MAXDEVICE /
519 CONFIG_SYS_IDE_MAXBUS);
523 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
525 struct efi_device_path_scsi *dp =
526 dp_fill(buf, dev->parent);
527 struct blk_desc *desc = dev_get_uclass_platdata(dev);
529 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
530 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
531 dp->dp.length = sizeof(*dp);
532 dp->logical_unit_number = desc->lun;
533 dp->target_id = desc->target;
537 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
539 struct efi_device_path_sd_mmc_path *sddp =
540 dp_fill(buf, dev->parent);
541 struct blk_desc *desc = dev_get_uclass_platdata(dev);
543 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
544 sddp->dp.sub_type = is_sd(desc) ?
545 DEVICE_PATH_SUB_TYPE_MSG_SD :
546 DEVICE_PATH_SUB_TYPE_MSG_MMC;
547 sddp->dp.length = sizeof(*sddp);
548 sddp->slot_number = dev->seq;
553 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
554 __FILE__, __LINE__, __func__,
555 dev->name, dev->parent->uclass->uc_drv->id);
556 return dp_fill(buf, dev->parent);
559 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
561 struct efi_device_path_sd_mmc_path *sddp =
562 dp_fill(buf, dev->parent);
563 struct mmc *mmc = mmc_get_mmc_dev(dev);
564 struct blk_desc *desc = mmc_get_blk_desc(mmc);
566 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
567 sddp->dp.sub_type = is_sd(desc) ?
568 DEVICE_PATH_SUB_TYPE_MSG_SD :
569 DEVICE_PATH_SUB_TYPE_MSG_MMC;
570 sddp->dp.length = sizeof(*sddp);
571 sddp->slot_number = dev->seq;
576 case UCLASS_MASS_STORAGE:
577 case UCLASS_USB_HUB: {
578 struct efi_device_path_usb_class *udp =
579 dp_fill(buf, dev->parent);
580 struct usb_device *udev = dev_get_parent_priv(dev);
581 struct usb_device_descriptor *desc = &udev->descriptor;
583 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
584 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
585 udp->dp.length = sizeof(*udp);
586 udp->vendor_id = desc->idVendor;
587 udp->product_id = desc->idProduct;
588 udp->device_class = desc->bDeviceClass;
589 udp->device_subclass = desc->bDeviceSubClass;
590 udp->device_protocol = desc->bDeviceProtocol;
595 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
596 __FILE__, __LINE__, __func__,
597 dev->name, dev->driver->id);
598 return dp_fill(buf, dev->parent);
602 /* Construct a device-path from a device: */
603 struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
607 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
610 buf = dp_fill(buf, dev);
611 *((struct efi_device_path *)buf) = END;
617 static unsigned dp_part_size(struct blk_desc *desc, int part)
624 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
627 dev = desc->bdev->parent;
628 dpsize = dp_size(dev);
631 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
634 if (part == 0) /* the actual disk, not a partition */
637 if (desc->part_type == PART_TYPE_ISO)
638 dpsize += sizeof(struct efi_device_path_cdrom_path);
640 dpsize += sizeof(struct efi_device_path_hard_drive_path);
646 * Create a device node for a block device partition.
648 * @buf buffer to which the device path is written
649 * @desc block device descriptor
650 * @part partition number, 0 identifies a block device
652 static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
654 disk_partition_t info;
656 part_get_info(desc, part, &info);
658 if (desc->part_type == PART_TYPE_ISO) {
659 struct efi_device_path_cdrom_path *cddp = buf;
661 cddp->boot_entry = part;
662 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
663 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
664 cddp->dp.length = sizeof(*cddp);
665 cddp->partition_start = info.start;
666 cddp->partition_end = info.size;
670 struct efi_device_path_hard_drive_path *hddp = buf;
672 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
673 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
674 hddp->dp.length = sizeof(*hddp);
675 hddp->partition_number = part;
676 hddp->partition_start = info.start;
677 hddp->partition_end = info.size;
678 if (desc->part_type == PART_TYPE_EFI)
679 hddp->partmap_type = 2;
681 hddp->partmap_type = 1;
683 switch (desc->sig_type) {
686 hddp->signature_type = 0;
687 memset(hddp->partition_signature, 0,
688 sizeof(hddp->partition_signature));
691 hddp->signature_type = 1;
692 memset(hddp->partition_signature, 0,
693 sizeof(hddp->partition_signature));
694 memcpy(hddp->partition_signature, &desc->mbr_sig,
695 sizeof(desc->mbr_sig));
698 hddp->signature_type = 2;
699 memcpy(hddp->partition_signature, &desc->guid_sig,
700 sizeof(hddp->partition_signature));
711 * Create a device path for a block device or one of its partitions.
713 * @buf buffer to which the device path is written
714 * @desc block device descriptor
715 * @part partition number, 0 identifies a block device
717 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
722 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
725 dev = desc->bdev->parent;
726 buf = dp_fill(buf, dev);
730 * We *could* make a more accurate path, by looking at if_type
731 * and handling all the different cases like we do for non-
732 * legacy (i.e. CONFIG_BLK=y) case. But most important thing
733 * is just to have a unique device-path for if_type+devnum.
734 * So map things to a fictitious USB device.
736 struct efi_device_path_usb *udp;
738 memcpy(buf, &ROOT, sizeof(ROOT));
742 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
743 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
744 udp->dp.length = sizeof(*udp);
745 udp->parent_port_number = desc->if_type;
746 udp->usb_interface = desc->devnum;
750 if (part == 0) /* the actual disk, not a partition */
753 return dp_part_node(buf, desc, part);
756 /* Construct a device-path from a partition on a block device: */
757 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
761 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
765 buf = dp_part_fill(buf, desc, part);
767 *((struct efi_device_path *)buf) = END;
773 * Create a device node for a block device partition.
775 * @buf buffer to which the device path is written
776 * @desc block device descriptor
777 * @part partition number, 0 identifies a block device
779 struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
784 if (desc->part_type == PART_TYPE_ISO)
785 dpsize = sizeof(struct efi_device_path_cdrom_path);
787 dpsize = sizeof(struct efi_device_path_hard_drive_path);
788 buf = dp_alloc(dpsize);
790 dp_part_node(buf, desc, part);
795 /* convert path to an UEFI style path (i.e. DOS style backslashes and UTF-16) */
796 static void path_to_uefi(u16 *uefi, const char *path)
808 * If desc is NULL, this creates a path with only the file component,
809 * otherwise it creates a full path with both device and file components
811 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
814 struct efi_device_path_file_path *fp;
816 unsigned dpsize = 0, fpsize;
819 dpsize = dp_part_size(desc, part);
821 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
824 start = buf = dp_alloc(dpsize + sizeof(END));
829 buf = dp_part_fill(buf, desc, part);
833 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
834 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
835 fp->dp.length = fpsize;
836 path_to_uefi(fp->str, path);
839 *((struct efi_device_path *)buf) = END;
845 struct efi_device_path *efi_dp_from_eth(void)
847 #ifndef CONFIG_DM_ETH
848 struct efi_device_path_mac_addr *ndp;
853 assert(eth_get_dev());
856 dpsize += dp_size(eth_get_dev());
858 dpsize += sizeof(ROOT);
859 dpsize += sizeof(*ndp);
862 start = buf = dp_alloc(dpsize + sizeof(END));
867 buf = dp_fill(buf, eth_get_dev());
869 memcpy(buf, &ROOT, sizeof(ROOT));
873 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
874 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
875 ndp->dp.length = sizeof(*ndp);
876 ndp->if_type = 1; /* Ethernet */
877 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
881 *((struct efi_device_path *)buf) = END;
887 /* Construct a device-path for memory-mapped image */
888 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
889 uint64_t start_address,
890 uint64_t end_address)
892 struct efi_device_path_memory *mdp;
895 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
900 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
901 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
902 mdp->dp.length = sizeof(*mdp);
903 mdp->memory_type = memory_type;
904 mdp->start_address = start_address;
905 mdp->end_address = end_address;
908 *((struct efi_device_path *)buf) = END;
914 * efi_dp_split_file_path() - split of relative file path from device path
916 * Given a device path indicating a file on a device, separate the device
917 * path in two: the device path of the actual device and the file path
918 * relative to this device.
920 * @full_path: device path including device and file path
921 * @device_path: path of the device
922 * @file_path: relative path of the file
923 * Return: status code
925 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
926 struct efi_device_path **device_path,
927 struct efi_device_path **file_path)
929 struct efi_device_path *p, *dp, *fp;
933 dp = efi_dp_dup(full_path);
935 return EFI_OUT_OF_RESOURCES;
937 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
940 return EFI_INVALID_PARAMETER;
944 return EFI_OUT_OF_RESOURCES;
945 p->type = DEVICE_PATH_TYPE_END;
946 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
947 p->length = sizeof(*p);
954 efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
956 struct efi_device_path **device,
957 struct efi_device_path **file)
960 struct blk_desc *desc = NULL;
961 disk_partition_t fs_partition;
963 char filename[32] = { 0 }; /* dp->str is u16[32] long */
967 return EFI_INVALID_PARAMETER;
969 is_net = !strcmp(dev, "Net");
971 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
974 return EFI_INVALID_PARAMETER;
977 *device = efi_dp_from_part(desc, part);
981 *device = efi_dp_from_eth();
989 /* Add leading / to fs paths, because they're absolute */
990 snprintf(filename, sizeof(filename), "/%s", path);
992 snprintf(filename, sizeof(filename), "%s", path);
994 /* DOS style file path: */
996 while ((s = strchr(s, '/')))
998 *file = efi_dp_from_file(((!is_net && device) ? desc : NULL),