1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI device path from u-boot device-model mapping
5 * (C) Copyright 2017 Rob Clark
16 #include <efi_loader.h>
18 #include <sandboxblockdev.h>
19 #include <asm-generic/unaligned.h>
20 #include <linux/compat.h> /* U16_MAX */
23 const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
25 #ifdef CONFIG_VIRTIO_BLK
26 const efi_guid_t efi_guid_virtio_dev = U_BOOT_VIRTIO_DEV_GUID;
29 /* template END node: */
30 static const struct efi_device_path END = {
31 .type = DEVICE_PATH_TYPE_END,
32 .sub_type = DEVICE_PATH_SUB_TYPE_END,
33 .length = sizeof(END),
36 /* template ROOT node: */
37 static const struct efi_device_path_vendor ROOT = {
39 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
40 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
41 .length = sizeof(ROOT),
46 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
48 * Determine if an MMC device is an SD card.
50 * @desc block device descriptor
51 * @return true if the device is an SD card
53 static bool is_sd(struct blk_desc *desc)
55 struct mmc *mmc = find_mmc_device(desc->devnum);
60 return IS_SD(mmc) != 0U;
64 static void *dp_alloc(size_t sz)
68 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
70 debug("EFI: ERROR: out of memory in %s\n", __func__);
79 * Iterate to next block in device-path, terminating (returning NULL)
82 struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
86 if (dp->type == DEVICE_PATH_TYPE_END)
88 dp = ((void *)dp) + dp->length;
89 if (dp->type == DEVICE_PATH_TYPE_END)
91 return (struct efi_device_path *)dp;
95 * Compare two device-paths, stopping when the shorter of the two hits
96 * an End* node. This is useful to, for example, compare a device-path
97 * representing a device with one representing a file on the device, or
98 * a device with a parent device.
100 int efi_dp_match(const struct efi_device_path *a,
101 const struct efi_device_path *b)
106 ret = memcmp(&a->length, &b->length, sizeof(a->length));
110 ret = memcmp(a, b, a->length);
123 * We can have device paths that start with a USB WWID or a USB Class node,
124 * and a few other cases which don't encode the full device path with bus
127 * - MESSAGING:USB_WWID
128 * - MESSAGING:USB_CLASS
133 * See UEFI spec (section 3.1.2, about short-form device-paths)
135 static struct efi_device_path *shorten_path(struct efi_device_path *dp)
139 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
140 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
141 * so not sure when we would see these other cases.
143 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
144 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
145 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
148 dp = efi_dp_next(dp);
154 static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
155 struct efi_device_path **rem)
157 struct efi_object *efiobj;
158 efi_uintn_t dp_size = efi_dp_instance_size(dp);
160 list_for_each_entry(efiobj, &efi_obj_list, link) {
161 struct efi_handler *handler;
162 struct efi_device_path *obj_dp;
165 ret = efi_search_protocol(efiobj,
166 &efi_guid_device_path, &handler);
167 if (ret != EFI_SUCCESS)
169 obj_dp = handler->protocol_interface;
172 if (efi_dp_match(dp, obj_dp) == 0) {
175 * Allow partial matches, but inform
178 *rem = ((void *)dp) +
179 efi_dp_instance_size(obj_dp);
182 /* Only return on exact matches */
183 if (efi_dp_instance_size(obj_dp) ==
189 obj_dp = shorten_path(efi_dp_next(obj_dp));
190 } while (short_path && obj_dp);
197 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
198 * remaining part of the device path after the matched object.
200 struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
201 struct efi_device_path **rem)
203 struct efi_object *efiobj;
205 /* Search for an exact match first */
206 efiobj = find_obj(dp, false, NULL);
208 /* Then for a fuzzy match */
210 efiobj = find_obj(dp, false, rem);
212 /* And now for a fuzzy short match */
214 efiobj = find_obj(dp, true, rem);
220 * Determine the last device path node that is not the end node.
223 * @return last node before the end node if it exists
226 const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
228 struct efi_device_path *ret;
230 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
233 ret = (struct efi_device_path *)dp;
234 dp = efi_dp_next(dp);
239 /* get size of the first device path instance excluding end node */
240 efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
244 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
248 dp = efi_dp_next(dp);
254 /* get size of multi-instance device path excluding end node */
255 efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
257 const struct efi_device_path *p = dp;
261 while (p->type != DEVICE_PATH_TYPE_END ||
262 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
263 p = (void *)p + p->length;
265 return (void *)p - (void *)dp;
268 /* copy multi-instance device path */
269 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
271 struct efi_device_path *ndp;
272 size_t sz = efi_dp_size(dp) + sizeof(END);
285 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
286 const struct efi_device_path *dp2)
288 struct efi_device_path *ret;
291 /* return an end node */
292 ret = efi_dp_dup(&END);
294 ret = efi_dp_dup(dp2);
296 ret = efi_dp_dup(dp1);
298 /* both dp1 and dp2 are non-null */
299 unsigned sz1 = efi_dp_size(dp1);
300 unsigned sz2 = efi_dp_size(dp2);
301 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
305 /* the end node of the second device path has to be retained */
306 memcpy(p + sz1, dp2, sz2 + sizeof(END));
313 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
314 const struct efi_device_path *node)
316 struct efi_device_path *ret;
319 ret = efi_dp_dup(&END);
321 ret = efi_dp_dup(dp);
323 size_t sz = node->length;
324 void *p = dp_alloc(sz + sizeof(END));
328 memcpy(p + sz, &END, sizeof(END));
331 /* both dp and node are non-null */
332 size_t sz = efi_dp_size(dp);
333 void *p = dp_alloc(sz + node->length + sizeof(END));
337 memcpy(p + sz, node, node->length);
338 memcpy(p + sz + node->length, &END, sizeof(END));
345 struct efi_device_path *efi_dp_create_device_node(const u8 type,
349 struct efi_device_path *ret;
351 if (length < sizeof(struct efi_device_path))
354 ret = dp_alloc(length);
358 ret->sub_type = sub_type;
359 ret->length = length;
363 struct efi_device_path *efi_dp_append_instance(
364 const struct efi_device_path *dp,
365 const struct efi_device_path *dpi)
368 struct efi_device_path *p, *ret;
373 return efi_dp_dup(dpi);
374 sz = efi_dp_size(dp);
375 szi = efi_dp_instance_size(dpi);
376 p = dp_alloc(sz + szi + 2 * sizeof(END));
380 memcpy(p, dp, sz + sizeof(END));
382 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
383 p = (void *)p + sizeof(END);
386 memcpy(p, &END, sizeof(END));
390 struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
394 struct efi_device_path *p;
400 sz = efi_dp_instance_size(*dp);
401 p = dp_alloc(sz + sizeof(END));
404 memcpy(p, *dp, sz + sizeof(END));
405 *dp = (void *)*dp + sz;
406 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
407 *dp = (void *)*dp + sizeof(END);
411 *size = sz + sizeof(END);
415 bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
417 const struct efi_device_path *p = dp;
421 while (p->type != DEVICE_PATH_TYPE_END)
422 p = (void *)p + p->length;
423 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
427 /* size of device-path not including END node for device and all parents
428 * up to the root device.
430 __maybe_unused static unsigned int dp_size(struct udevice *dev)
432 if (!dev || !dev->driver)
435 switch (dev->driver->id) {
437 case UCLASS_SIMPLE_BUS:
438 /* stop traversing parents at this point: */
441 return dp_size(dev->parent) +
442 sizeof(struct efi_device_path_mac_addr);
445 switch (dev->parent->uclass->uc_drv->id) {
448 return dp_size(dev->parent) +
449 sizeof(struct efi_device_path_atapi);
451 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
453 return dp_size(dev->parent) +
454 sizeof(struct efi_device_path_scsi);
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 #if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
463 return dp_size(dev->parent) +
464 sizeof(struct efi_device_path_sata);
466 #if defined(CONFIG_NVME)
468 return dp_size(dev->parent) +
469 sizeof(struct efi_device_path_nvme);
471 #ifdef CONFIG_SANDBOX
474 * Sandbox's host device will be represented
475 * as vendor device with extra one byte for
478 return dp_size(dev->parent)
479 + sizeof(struct efi_device_path_vendor) + 1;
481 #ifdef CONFIG_VIRTIO_BLK
484 * Virtio devices will be represented as a vendor
485 * device node with an extra byte for the device
488 return dp_size(dev->parent)
489 + sizeof(struct efi_device_path_vendor) + 1;
492 return dp_size(dev->parent);
495 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
497 return dp_size(dev->parent) +
498 sizeof(struct efi_device_path_sd_mmc_path);
500 case UCLASS_MASS_STORAGE:
502 return dp_size(dev->parent) +
503 sizeof(struct efi_device_path_usb_class);
505 /* just skip over unknown classes: */
506 return dp_size(dev->parent);
511 * Recursively build a device path.
513 * @buf pointer to the end of the device path
515 * @return pointer to the end of the device path
517 __maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
519 if (!dev || !dev->driver)
522 switch (dev->driver->id) {
524 case UCLASS_SIMPLE_BUS: {
525 /* stop traversing parents at this point: */
526 struct efi_device_path_vendor *vdp = buf;
532 struct efi_device_path_mac_addr *dp =
533 dp_fill(buf, dev->parent);
534 struct eth_pdata *pdata = dev->platdata;
536 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
537 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
538 dp->dp.length = sizeof(*dp);
539 memset(&dp->mac, 0, sizeof(dp->mac));
540 /* We only support IPv4 */
541 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
549 switch (dev->parent->uclass->uc_drv->id) {
550 #ifdef CONFIG_SANDBOX
552 /* stop traversing parents at this point: */
553 struct efi_device_path_vendor *dp;
554 struct blk_desc *desc = dev_get_uclass_platdata(dev);
556 dp_fill(buf, dev->parent);
559 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
560 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
561 dp->dp.length = sizeof(*dp) + 1;
562 memcpy(&dp->guid, &efi_guid_host_dev,
564 dp->vendor_data[0] = desc->devnum;
565 return &dp->vendor_data[1];
568 #ifdef CONFIG_VIRTIO_BLK
569 case UCLASS_VIRTIO: {
570 struct efi_device_path_vendor *dp;
571 struct blk_desc *desc = dev_get_uclass_platdata(dev);
573 dp_fill(buf, dev->parent);
576 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
577 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
578 dp->dp.length = sizeof(*dp) + 1;
579 memcpy(&dp->guid, &efi_guid_virtio_dev,
581 dp->vendor_data[0] = desc->devnum;
582 return &dp->vendor_data[1];
587 struct efi_device_path_atapi *dp =
588 dp_fill(buf, dev->parent);
589 struct blk_desc *desc = dev_get_uclass_platdata(dev);
591 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
592 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
593 dp->dp.length = sizeof(*dp);
594 dp->logical_unit_number = desc->devnum;
595 dp->primary_secondary = IDE_BUS(desc->devnum);
596 dp->slave_master = desc->devnum %
597 (CONFIG_SYS_IDE_MAXDEVICE /
598 CONFIG_SYS_IDE_MAXBUS);
602 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
604 struct efi_device_path_scsi *dp =
605 dp_fill(buf, dev->parent);
606 struct blk_desc *desc = dev_get_uclass_platdata(dev);
608 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
609 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
610 dp->dp.length = sizeof(*dp);
611 dp->logical_unit_number = desc->lun;
612 dp->target_id = desc->target;
616 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
618 struct efi_device_path_sd_mmc_path *sddp =
619 dp_fill(buf, dev->parent);
620 struct blk_desc *desc = dev_get_uclass_platdata(dev);
622 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
623 sddp->dp.sub_type = is_sd(desc) ?
624 DEVICE_PATH_SUB_TYPE_MSG_SD :
625 DEVICE_PATH_SUB_TYPE_MSG_MMC;
626 sddp->dp.length = sizeof(*sddp);
627 sddp->slot_number = dev->seq;
631 #if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
633 struct efi_device_path_sata *dp =
634 dp_fill(buf, dev->parent);
635 struct blk_desc *desc = dev_get_uclass_platdata(dev);
637 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
638 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
639 dp->dp.length = sizeof(*dp);
640 dp->hba_port = desc->devnum;
641 /* default 0xffff implies no port multiplier */
642 dp->port_multiplier_port = 0xffff;
643 dp->logical_unit_number = desc->lun;
647 #if defined(CONFIG_NVME)
649 struct efi_device_path_nvme *dp =
650 dp_fill(buf, dev->parent);
653 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
654 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
655 dp->dp.length = sizeof(*dp);
656 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
657 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
662 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
663 __FILE__, __LINE__, __func__,
664 dev->name, dev->parent->uclass->uc_drv->id);
665 return dp_fill(buf, dev->parent);
668 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
670 struct efi_device_path_sd_mmc_path *sddp =
671 dp_fill(buf, dev->parent);
672 struct mmc *mmc = mmc_get_mmc_dev(dev);
673 struct blk_desc *desc = mmc_get_blk_desc(mmc);
675 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
676 sddp->dp.sub_type = is_sd(desc) ?
677 DEVICE_PATH_SUB_TYPE_MSG_SD :
678 DEVICE_PATH_SUB_TYPE_MSG_MMC;
679 sddp->dp.length = sizeof(*sddp);
680 sddp->slot_number = dev->seq;
685 case UCLASS_MASS_STORAGE:
686 case UCLASS_USB_HUB: {
687 struct efi_device_path_usb_class *udp =
688 dp_fill(buf, dev->parent);
689 struct usb_device *udev = dev_get_parent_priv(dev);
690 struct usb_device_descriptor *desc = &udev->descriptor;
692 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
693 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
694 udp->dp.length = sizeof(*udp);
695 udp->vendor_id = desc->idVendor;
696 udp->product_id = desc->idProduct;
697 udp->device_class = desc->bDeviceClass;
698 udp->device_subclass = desc->bDeviceSubClass;
699 udp->device_protocol = desc->bDeviceProtocol;
704 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
705 __FILE__, __LINE__, __func__,
706 dev->name, dev->driver->id);
707 return dp_fill(buf, dev->parent);
712 static unsigned dp_part_size(struct blk_desc *desc, int part)
719 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
722 dev = desc->bdev->parent;
723 dpsize = dp_size(dev);
726 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
729 if (part == 0) /* the actual disk, not a partition */
732 if (desc->part_type == PART_TYPE_ISO)
733 dpsize += sizeof(struct efi_device_path_cdrom_path);
735 dpsize += sizeof(struct efi_device_path_hard_drive_path);
741 * Create a device node for a block device partition.
743 * @buf buffer to which the device path is written
744 * @desc block device descriptor
745 * @part partition number, 0 identifies a block device
747 static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
749 struct disk_partition info;
751 part_get_info(desc, part, &info);
753 if (desc->part_type == PART_TYPE_ISO) {
754 struct efi_device_path_cdrom_path *cddp = buf;
756 cddp->boot_entry = part;
757 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
758 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
759 cddp->dp.length = sizeof(*cddp);
760 cddp->partition_start = info.start;
761 cddp->partition_size = info.size;
765 struct efi_device_path_hard_drive_path *hddp = buf;
767 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
768 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
769 hddp->dp.length = sizeof(*hddp);
770 hddp->partition_number = part;
771 hddp->partition_start = info.start;
772 hddp->partition_end = info.size;
773 if (desc->part_type == PART_TYPE_EFI)
774 hddp->partmap_type = 2;
776 hddp->partmap_type = 1;
778 switch (desc->sig_type) {
781 hddp->signature_type = 0;
782 memset(hddp->partition_signature, 0,
783 sizeof(hddp->partition_signature));
786 hddp->signature_type = 1;
787 memset(hddp->partition_signature, 0,
788 sizeof(hddp->partition_signature));
789 memcpy(hddp->partition_signature, &desc->mbr_sig,
790 sizeof(desc->mbr_sig));
793 hddp->signature_type = 2;
794 memcpy(hddp->partition_signature, &desc->guid_sig,
795 sizeof(hddp->partition_signature));
806 * Create a device path for a block device or one of its partitions.
808 * @buf buffer to which the device path is written
809 * @desc block device descriptor
810 * @part partition number, 0 identifies a block device
812 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
817 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
820 dev = desc->bdev->parent;
821 buf = dp_fill(buf, dev);
825 * We *could* make a more accurate path, by looking at if_type
826 * and handling all the different cases like we do for non-
827 * legacy (i.e. CONFIG_BLK=y) case. But most important thing
828 * is just to have a unique device-path for if_type+devnum.
829 * So map things to a fictitious USB device.
831 struct efi_device_path_usb *udp;
833 memcpy(buf, &ROOT, sizeof(ROOT));
837 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
838 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
839 udp->dp.length = sizeof(*udp);
840 udp->parent_port_number = desc->if_type;
841 udp->usb_interface = desc->devnum;
845 if (part == 0) /* the actual disk, not a partition */
848 return dp_part_node(buf, desc, part);
851 /* Construct a device-path from a partition on a block device: */
852 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
856 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
860 buf = dp_part_fill(buf, desc, part);
862 *((struct efi_device_path *)buf) = END;
868 * Create a device node for a block device partition.
870 * @buf buffer to which the device path is written
871 * @desc block device descriptor
872 * @part partition number, 0 identifies a block device
874 struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
879 if (desc->part_type == PART_TYPE_ISO)
880 dpsize = sizeof(struct efi_device_path_cdrom_path);
882 dpsize = sizeof(struct efi_device_path_hard_drive_path);
883 buf = dp_alloc(dpsize);
885 dp_part_node(buf, desc, part);
891 * path_to_uefi() - convert UTF-8 path to an UEFI style path
893 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
894 * separators and UTF-16).
896 * @src: source buffer
897 * @uefi: target buffer, possibly unaligned
899 static void path_to_uefi(void *uefi, const char *src)
904 * efi_set_bootdev() calls this routine indirectly before the UEFI
905 * subsystem is initialized. So we cannot assume unaligned access to be
911 s32 code = utf8_get(&src);
915 else if (code == '/')
917 utf16_put(code, &pos);
923 * If desc is NULL, this creates a path with only the file component,
924 * otherwise it creates a full path with both device and file components
926 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
929 struct efi_device_path_file_path *fp;
931 size_t dpsize = 0, fpsize;
934 dpsize = dp_part_size(desc, part);
936 fpsize = sizeof(struct efi_device_path) +
937 2 * (utf8_utf16_strlen(path) + 1);
938 if (fpsize > U16_MAX)
943 start = buf = dp_alloc(dpsize + sizeof(END));
948 buf = dp_part_fill(buf, desc, part);
952 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
953 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
954 fp->dp.length = (u16)fpsize;
955 path_to_uefi(fp->str, path);
958 *((struct efi_device_path *)buf) = END;
964 struct efi_device_path *efi_dp_from_eth(void)
966 #ifndef CONFIG_DM_ETH
967 struct efi_device_path_mac_addr *ndp;
972 assert(eth_get_dev());
975 dpsize += dp_size(eth_get_dev());
977 dpsize += sizeof(ROOT);
978 dpsize += sizeof(*ndp);
981 start = buf = dp_alloc(dpsize + sizeof(END));
986 buf = dp_fill(buf, eth_get_dev());
988 memcpy(buf, &ROOT, sizeof(ROOT));
992 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
993 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
994 ndp->dp.length = sizeof(*ndp);
995 ndp->if_type = 1; /* Ethernet */
996 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
1000 *((struct efi_device_path *)buf) = END;
1006 /* Construct a device-path for memory-mapped image */
1007 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1008 uint64_t start_address,
1009 uint64_t end_address)
1011 struct efi_device_path_memory *mdp;
1014 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
1019 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1020 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1021 mdp->dp.length = sizeof(*mdp);
1022 mdp->memory_type = memory_type;
1023 mdp->start_address = start_address;
1024 mdp->end_address = end_address;
1027 *((struct efi_device_path *)buf) = END;
1033 * efi_dp_split_file_path() - split of relative file path from device path
1035 * Given a device path indicating a file on a device, separate the device
1036 * path in two: the device path of the actual device and the file path
1037 * relative to this device.
1039 * @full_path: device path including device and file path
1040 * @device_path: path of the device
1041 * @file_path: relative path of the file or NULL if there is none
1042 * Return: status code
1044 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1045 struct efi_device_path **device_path,
1046 struct efi_device_path **file_path)
1048 struct efi_device_path *p, *dp, *fp = NULL;
1050 *device_path = NULL;
1052 dp = efi_dp_dup(full_path);
1054 return EFI_OUT_OF_RESOURCES;
1056 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
1063 return EFI_OUT_OF_RESOURCES;
1064 p->type = DEVICE_PATH_TYPE_END;
1065 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1066 p->length = sizeof(*p);
1075 * efi_dp_from_name() - convert U-Boot device and file path to device path
1077 * @dev: U-Boot device, e.g. 'mmc'
1078 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1079 * @path: file path relative to U-Boot device, may be NULL
1080 * @device: pointer to receive device path of the device
1081 * @file: pointer to receive device path for the file
1082 * Return: status code
1084 efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1086 struct efi_device_path **device,
1087 struct efi_device_path **file)
1090 struct blk_desc *desc = NULL;
1091 struct disk_partition fs_partition;
1093 char filename[32] = { 0 }; /* dp->str is u16[32] long */
1097 return EFI_INVALID_PARAMETER;
1099 is_net = !strcmp(dev, "Net");
1101 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1103 if (part < 0 || !desc)
1104 return EFI_INVALID_PARAMETER;
1107 *device = efi_dp_from_part(desc, part);
1111 *device = efi_dp_from_eth();
1118 snprintf(filename, sizeof(filename), "%s", path);
1119 /* DOS style file path: */
1121 while ((s = strchr(s, '/')))
1123 *file = efi_dp_from_file(is_net ? NULL : desc, part, filename);
1126 return EFI_INVALID_PARAMETER;