1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI device path from u-boot device-model mapping
5 * (C) Copyright 2017 Rob Clark
13 #include <efi_loader.h>
16 /* template END node: */
17 static const struct efi_device_path END = {
18 .type = DEVICE_PATH_TYPE_END,
19 .sub_type = DEVICE_PATH_SUB_TYPE_END,
20 .length = sizeof(END),
23 /* template ROOT node: */
24 static const struct efi_device_path_vendor ROOT = {
26 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
27 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
28 .length = sizeof(ROOT),
33 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
35 * Determine if an MMC device is an SD card.
37 * @desc block device descriptor
38 * @return true if the device is an SD card
40 static bool is_sd(struct blk_desc *desc)
42 struct mmc *mmc = find_mmc_device(desc->devnum);
47 return IS_SD(mmc) != 0U;
51 static void *dp_alloc(size_t sz)
55 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
57 debug("EFI: ERROR: out of memory in %s\n", __func__);
66 * Iterate to next block in device-path, terminating (returning NULL)
69 struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
73 if (dp->type == DEVICE_PATH_TYPE_END)
75 dp = ((void *)dp) + dp->length;
76 if (dp->type == DEVICE_PATH_TYPE_END)
78 return (struct efi_device_path *)dp;
82 * Compare two device-paths, stopping when the shorter of the two hits
83 * an End* node. This is useful to, for example, compare a device-path
84 * representing a device with one representing a file on the device, or
85 * a device with a parent device.
87 int efi_dp_match(const struct efi_device_path *a,
88 const struct efi_device_path *b)
93 ret = memcmp(&a->length, &b->length, sizeof(a->length));
97 ret = memcmp(a, b, a->length);
110 * We can have device paths that start with a USB WWID or a USB Class node,
111 * and a few other cases which don't encode the full device path with bus
114 * - MESSAGING:USB_WWID
115 * - MESSAGING:USB_CLASS
120 * See UEFI spec (section 3.1.2, about short-form device-paths)
122 static struct efi_device_path *shorten_path(struct efi_device_path *dp)
126 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
127 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
128 * so not sure when we would see these other cases.
130 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
131 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
132 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
135 dp = efi_dp_next(dp);
141 static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
142 struct efi_device_path **rem)
144 struct efi_object *efiobj;
145 efi_uintn_t dp_size = efi_dp_instance_size(dp);
147 list_for_each_entry(efiobj, &efi_obj_list, link) {
148 struct efi_handler *handler;
149 struct efi_device_path *obj_dp;
152 ret = efi_search_protocol(efiobj,
153 &efi_guid_device_path, &handler);
154 if (ret != EFI_SUCCESS)
156 obj_dp = handler->protocol_interface;
159 if (efi_dp_match(dp, obj_dp) == 0) {
162 * Allow partial matches, but inform
165 *rem = ((void *)dp) +
166 efi_dp_instance_size(obj_dp);
169 /* Only return on exact matches */
170 if (efi_dp_instance_size(obj_dp) ==
176 obj_dp = shorten_path(efi_dp_next(obj_dp));
177 } while (short_path && obj_dp);
184 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
185 * remaining part of the device path after the matched object.
187 struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
188 struct efi_device_path **rem)
190 struct efi_object *efiobj;
192 /* Search for an exact match first */
193 efiobj = find_obj(dp, false, NULL);
195 /* Then for a fuzzy match */
197 efiobj = find_obj(dp, false, rem);
199 /* And now for a fuzzy short match */
201 efiobj = find_obj(dp, true, rem);
207 * Determine the last device path node that is not the end node.
210 * @return last node before the end node if it exists
213 const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
215 struct efi_device_path *ret;
217 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
220 ret = (struct efi_device_path *)dp;
221 dp = efi_dp_next(dp);
226 /* get size of the first device path instance excluding end node */
227 efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
231 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
235 dp = efi_dp_next(dp);
241 /* get size of multi-instance device path excluding end node */
242 efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
244 const struct efi_device_path *p = dp;
248 while (p->type != DEVICE_PATH_TYPE_END ||
249 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
250 p = (void *)p + p->length;
252 return (void *)p - (void *)dp;
255 /* copy multi-instance device path */
256 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
258 struct efi_device_path *ndp;
259 size_t sz = efi_dp_size(dp) + sizeof(END);
272 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
273 const struct efi_device_path *dp2)
275 struct efi_device_path *ret;
278 /* return an end node */
279 ret = efi_dp_dup(&END);
281 ret = efi_dp_dup(dp2);
283 ret = efi_dp_dup(dp1);
285 /* both dp1 and dp2 are non-null */
286 unsigned sz1 = efi_dp_size(dp1);
287 unsigned sz2 = efi_dp_size(dp2);
288 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
292 /* the end node of the second device path has to be retained */
293 memcpy(p + sz1, dp2, sz2 + sizeof(END));
300 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
301 const struct efi_device_path *node)
303 struct efi_device_path *ret;
306 ret = efi_dp_dup(&END);
308 ret = efi_dp_dup(dp);
310 size_t sz = node->length;
311 void *p = dp_alloc(sz + sizeof(END));
315 memcpy(p + sz, &END, sizeof(END));
318 /* both dp and node are non-null */
319 size_t sz = efi_dp_size(dp);
320 void *p = dp_alloc(sz + node->length + sizeof(END));
324 memcpy(p + sz, node, node->length);
325 memcpy(p + sz + node->length, &END, sizeof(END));
332 struct efi_device_path *efi_dp_create_device_node(const u8 type,
336 struct efi_device_path *ret;
338 ret = dp_alloc(length);
342 ret->sub_type = sub_type;
343 ret->length = length;
347 struct efi_device_path *efi_dp_append_instance(
348 const struct efi_device_path *dp,
349 const struct efi_device_path *dpi)
352 struct efi_device_path *p, *ret;
357 return efi_dp_dup(dpi);
358 sz = efi_dp_size(dp);
359 szi = efi_dp_instance_size(dpi);
360 p = dp_alloc(sz + szi + 2 * sizeof(END));
364 memcpy(p, dp, sz + sizeof(END));
366 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
367 p = (void *)p + sizeof(END);
370 memcpy(p, &END, sizeof(END));
374 struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
378 struct efi_device_path *p;
384 sz = efi_dp_instance_size(*dp);
385 p = dp_alloc(sz + sizeof(END));
388 memcpy(p, *dp, sz + sizeof(END));
389 *dp = (void *)*dp + sz;
390 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
391 *dp = (void *)*dp + sizeof(END);
395 *size = sz + sizeof(END);
399 bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
401 const struct efi_device_path *p = dp;
405 while (p->type != DEVICE_PATH_TYPE_END)
406 p = (void *)p + p->length;
407 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
411 /* size of device-path not including END node for device and all parents
412 * up to the root device.
414 static unsigned dp_size(struct udevice *dev)
416 if (!dev || !dev->driver)
419 switch (dev->driver->id) {
421 case UCLASS_SIMPLE_BUS:
422 /* stop traversing parents at this point: */
425 return dp_size(dev->parent) +
426 sizeof(struct efi_device_path_mac_addr);
429 switch (dev->parent->uclass->uc_drv->id) {
432 return dp_size(dev->parent) +
433 sizeof(struct efi_device_path_atapi);
435 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
437 return dp_size(dev->parent) +
438 sizeof(struct efi_device_path_scsi);
440 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
442 return dp_size(dev->parent) +
443 sizeof(struct efi_device_path_sd_mmc_path);
446 return dp_size(dev->parent);
449 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
451 return dp_size(dev->parent) +
452 sizeof(struct efi_device_path_sd_mmc_path);
454 case UCLASS_MASS_STORAGE:
456 return dp_size(dev->parent) +
457 sizeof(struct efi_device_path_usb_class);
459 /* just skip over unknown classes: */
460 return dp_size(dev->parent);
465 * Recursively build a device path.
467 * @buf pointer to the end of the device path
469 * @return pointer to the end of the device path
471 static void *dp_fill(void *buf, struct udevice *dev)
473 if (!dev || !dev->driver)
476 switch (dev->driver->id) {
478 case UCLASS_SIMPLE_BUS: {
479 /* stop traversing parents at this point: */
480 struct efi_device_path_vendor *vdp = buf;
486 struct efi_device_path_mac_addr *dp =
487 dp_fill(buf, dev->parent);
488 struct eth_pdata *pdata = dev->platdata;
490 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
491 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
492 dp->dp.length = sizeof(*dp);
493 memset(&dp->mac, 0, sizeof(dp->mac));
494 /* We only support IPv4 */
495 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
503 switch (dev->parent->uclass->uc_drv->id) {
506 struct efi_device_path_atapi *dp =
507 dp_fill(buf, dev->parent);
508 struct blk_desc *desc = dev_get_uclass_platdata(dev);
510 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
511 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
512 dp->dp.length = sizeof(*dp);
513 dp->logical_unit_number = desc->devnum;
514 dp->primary_secondary = IDE_BUS(desc->devnum);
515 dp->slave_master = desc->devnum %
516 (CONFIG_SYS_IDE_MAXDEVICE /
517 CONFIG_SYS_IDE_MAXBUS);
521 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
523 struct efi_device_path_scsi *dp =
524 dp_fill(buf, dev->parent);
525 struct blk_desc *desc = dev_get_uclass_platdata(dev);
527 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
528 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
529 dp->dp.length = sizeof(*dp);
530 dp->logical_unit_number = desc->lun;
531 dp->target_id = desc->target;
535 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
537 struct efi_device_path_sd_mmc_path *sddp =
538 dp_fill(buf, dev->parent);
539 struct blk_desc *desc = dev_get_uclass_platdata(dev);
541 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
542 sddp->dp.sub_type = is_sd(desc) ?
543 DEVICE_PATH_SUB_TYPE_MSG_SD :
544 DEVICE_PATH_SUB_TYPE_MSG_MMC;
545 sddp->dp.length = sizeof(*sddp);
546 sddp->slot_number = dev->seq;
551 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
552 __FILE__, __LINE__, __func__,
553 dev->name, dev->parent->uclass->uc_drv->id);
554 return dp_fill(buf, dev->parent);
557 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
559 struct efi_device_path_sd_mmc_path *sddp =
560 dp_fill(buf, dev->parent);
561 struct mmc *mmc = mmc_get_mmc_dev(dev);
562 struct blk_desc *desc = mmc_get_blk_desc(mmc);
564 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
565 sddp->dp.sub_type = is_sd(desc) ?
566 DEVICE_PATH_SUB_TYPE_MSG_SD :
567 DEVICE_PATH_SUB_TYPE_MSG_MMC;
568 sddp->dp.length = sizeof(*sddp);
569 sddp->slot_number = dev->seq;
574 case UCLASS_MASS_STORAGE:
575 case UCLASS_USB_HUB: {
576 struct efi_device_path_usb_class *udp =
577 dp_fill(buf, dev->parent);
578 struct usb_device *udev = dev_get_parent_priv(dev);
579 struct usb_device_descriptor *desc = &udev->descriptor;
581 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
582 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
583 udp->dp.length = sizeof(*udp);
584 udp->vendor_id = desc->idVendor;
585 udp->product_id = desc->idProduct;
586 udp->device_class = desc->bDeviceClass;
587 udp->device_subclass = desc->bDeviceSubClass;
588 udp->device_protocol = desc->bDeviceProtocol;
593 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
594 __FILE__, __LINE__, __func__,
595 dev->name, dev->driver->id);
596 return dp_fill(buf, dev->parent);
600 /* Construct a device-path from a device: */
601 struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
605 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
608 buf = dp_fill(buf, dev);
609 *((struct efi_device_path *)buf) = END;
615 static unsigned dp_part_size(struct blk_desc *desc, int part)
622 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
625 dev = desc->bdev->parent;
626 dpsize = dp_size(dev);
629 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
632 if (part == 0) /* the actual disk, not a partition */
635 if (desc->part_type == PART_TYPE_ISO)
636 dpsize += sizeof(struct efi_device_path_cdrom_path);
638 dpsize += sizeof(struct efi_device_path_hard_drive_path);
644 * Create a device node for a block device partition.
646 * @buf buffer to which the device path is written
647 * @desc block device descriptor
648 * @part partition number, 0 identifies a block device
650 static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
652 disk_partition_t info;
654 part_get_info(desc, part, &info);
656 if (desc->part_type == PART_TYPE_ISO) {
657 struct efi_device_path_cdrom_path *cddp = buf;
659 cddp->boot_entry = part;
660 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
661 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
662 cddp->dp.length = sizeof(*cddp);
663 cddp->partition_start = info.start;
664 cddp->partition_end = info.size;
668 struct efi_device_path_hard_drive_path *hddp = buf;
670 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
671 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
672 hddp->dp.length = sizeof(*hddp);
673 hddp->partition_number = part;
674 hddp->partition_start = info.start;
675 hddp->partition_end = info.size;
676 if (desc->part_type == PART_TYPE_EFI)
677 hddp->partmap_type = 2;
679 hddp->partmap_type = 1;
681 switch (desc->sig_type) {
684 hddp->signature_type = 0;
685 memset(hddp->partition_signature, 0,
686 sizeof(hddp->partition_signature));
689 hddp->signature_type = 1;
690 memset(hddp->partition_signature, 0,
691 sizeof(hddp->partition_signature));
692 memcpy(hddp->partition_signature, &desc->mbr_sig,
693 sizeof(desc->mbr_sig));
696 hddp->signature_type = 2;
697 memcpy(hddp->partition_signature, &desc->guid_sig,
698 sizeof(hddp->partition_signature));
709 * Create a device path for a block device or one of its partitions.
711 * @buf buffer to which the device path is written
712 * @desc block device descriptor
713 * @part partition number, 0 identifies a block device
715 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
720 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
723 dev = desc->bdev->parent;
724 buf = dp_fill(buf, dev);
728 * We *could* make a more accurate path, by looking at if_type
729 * and handling all the different cases like we do for non-
730 * legacy (i.e. CONFIG_BLK=y) case. But most important thing
731 * is just to have a unique device-path for if_type+devnum.
732 * So map things to a fictitious USB device.
734 struct efi_device_path_usb *udp;
736 memcpy(buf, &ROOT, sizeof(ROOT));
740 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
741 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
742 udp->dp.length = sizeof(*udp);
743 udp->parent_port_number = desc->if_type;
744 udp->usb_interface = desc->devnum;
748 if (part == 0) /* the actual disk, not a partition */
751 return dp_part_node(buf, desc, part);
754 /* Construct a device-path from a partition on a block device: */
755 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
759 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
763 buf = dp_part_fill(buf, desc, part);
765 *((struct efi_device_path *)buf) = END;
771 * Create a device node for a block device partition.
773 * @buf buffer to which the device path is written
774 * @desc block device descriptor
775 * @part partition number, 0 identifies a block device
777 struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
782 if (desc->part_type == PART_TYPE_ISO)
783 dpsize = sizeof(struct efi_device_path_cdrom_path);
785 dpsize = sizeof(struct efi_device_path_hard_drive_path);
786 buf = dp_alloc(dpsize);
788 dp_part_node(buf, desc, part);
793 /* convert path to an UEFI style path (i.e. DOS style backslashes and UTF-16) */
794 static void path_to_uefi(u16 *uefi, const char *path)
806 * If desc is NULL, this creates a path with only the file component,
807 * otherwise it creates a full path with both device and file components
809 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
812 struct efi_device_path_file_path *fp;
814 unsigned dpsize = 0, fpsize;
817 dpsize = dp_part_size(desc, part);
819 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
822 start = buf = dp_alloc(dpsize + sizeof(END));
827 buf = dp_part_fill(buf, desc, part);
831 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
832 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
833 fp->dp.length = fpsize;
834 path_to_uefi(fp->str, path);
837 *((struct efi_device_path *)buf) = END;
843 struct efi_device_path *efi_dp_from_eth(void)
845 #ifndef CONFIG_DM_ETH
846 struct efi_device_path_mac_addr *ndp;
851 assert(eth_get_dev());
854 dpsize += dp_size(eth_get_dev());
856 dpsize += sizeof(ROOT);
857 dpsize += sizeof(*ndp);
860 start = buf = dp_alloc(dpsize + sizeof(END));
865 buf = dp_fill(buf, eth_get_dev());
867 memcpy(buf, &ROOT, sizeof(ROOT));
871 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
872 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
873 ndp->dp.length = sizeof(*ndp);
874 ndp->if_type = 1; /* Ethernet */
875 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
879 *((struct efi_device_path *)buf) = END;
885 /* Construct a device-path for memory-mapped image */
886 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
887 uint64_t start_address,
888 uint64_t end_address)
890 struct efi_device_path_memory *mdp;
893 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
898 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
899 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
900 mdp->dp.length = sizeof(*mdp);
901 mdp->memory_type = memory_type;
902 mdp->start_address = start_address;
903 mdp->end_address = end_address;
906 *((struct efi_device_path *)buf) = END;
912 * efi_dp_split_file_path() - split of relative file path from device path
914 * Given a device path indicating a file on a device, separate the device
915 * path in two: the device path of the actual device and the file path
916 * relative to this device.
918 * @full_path: device path including device and file path
919 * @device_path: path of the device
920 * @file_path: relative path of the file
921 * Return: status code
923 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
924 struct efi_device_path **device_path,
925 struct efi_device_path **file_path)
927 struct efi_device_path *p, *dp, *fp;
931 dp = efi_dp_dup(full_path);
933 return EFI_OUT_OF_RESOURCES;
935 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
938 return EFI_INVALID_PARAMETER;
942 return EFI_OUT_OF_RESOURCES;
943 p->type = DEVICE_PATH_TYPE_END;
944 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
945 p->length = sizeof(*p);
952 efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
954 struct efi_device_path **device,
955 struct efi_device_path **file)
958 struct blk_desc *desc = NULL;
959 disk_partition_t fs_partition;
961 char filename[32] = { 0 }; /* dp->str is u16[32] long */
965 return EFI_INVALID_PARAMETER;
967 is_net = !strcmp(dev, "Net");
969 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
971 if (part < 0 || !desc)
972 return EFI_INVALID_PARAMETER;
975 *device = efi_dp_from_part(desc, part);
979 *device = efi_dp_from_eth();
986 snprintf(filename, sizeof(filename), "%s", path);
987 /* DOS style file path: */
989 while ((s = strchr(s, '/')))
991 *file = efi_dp_from_file(((!is_net && device) ? desc : NULL),