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 LOGC_EFI
18 #include <efi_loader.h>
21 #include <asm-generic/unaligned.h>
22 #include <linux/compat.h> /* U16_MAX */
25 const efi_guid_t efi_guid_blkmap_dev = U_BOOT_BLKMAP_DEV_GUID;
28 const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
30 #ifdef CONFIG_VIRTIO_BLK
31 const efi_guid_t efi_guid_virtio_dev = U_BOOT_VIRTIO_DEV_GUID;
34 /* template END node: */
35 const struct efi_device_path END = {
36 .type = DEVICE_PATH_TYPE_END,
37 .sub_type = DEVICE_PATH_SUB_TYPE_END,
38 .length = sizeof(END),
41 /* template ROOT node: */
42 static const struct efi_device_path_vendor ROOT = {
44 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
45 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
46 .length = sizeof(ROOT),
51 #if defined(CONFIG_MMC)
53 * Determine if an MMC device is an SD card.
55 * @desc block device descriptor
56 * Return: true if the device is an SD card
58 static bool is_sd(struct blk_desc *desc)
60 struct mmc *mmc = find_mmc_device(desc->devnum);
65 return IS_SD(mmc) != 0U;
70 * Iterate to next block in device-path, terminating (returning NULL)
73 struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
77 if (dp->type == DEVICE_PATH_TYPE_END)
79 dp = ((void *)dp) + dp->length;
80 if (dp->type == DEVICE_PATH_TYPE_END)
82 return (struct efi_device_path *)dp;
86 * Compare two device-paths, stopping when the shorter of the two hits
87 * an End* node. This is useful to, for example, compare a device-path
88 * representing a device with one representing a file on the device, or
89 * a device with a parent device.
91 int efi_dp_match(const struct efi_device_path *a,
92 const struct efi_device_path *b)
97 ret = memcmp(&a->length, &b->length, sizeof(a->length));
101 ret = memcmp(a, b, a->length);
114 * efi_dp_shorten() - shorten device-path
116 * When creating a short boot option we want to use a device-path that is
117 * independent of the location where the block device is plugged in.
119 * UsbWwi() nodes contain a serial number, hard drive paths a partition
120 * UUID. Both should be unique.
122 * See UEFI spec, section 3.1.2 for "short-form device path".
124 * @dp: original device-path
125 * @Return: shortened device-path or NULL
127 struct efi_device_path *efi_dp_shorten(struct efi_device_path *dp)
130 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_WWI) ||
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);
142 * find_handle() - find handle by device path and installed protocol
144 * If @rem is provided, the handle with the longest partial match is returned.
146 * @dp: device path to search
147 * @guid: GUID of protocol that must be installed on path or NULL
148 * @short_path: use short form device path for matching
149 * @rem: pointer to receive remaining device path
150 * Return: matching handle
152 static efi_handle_t find_handle(struct efi_device_path *dp,
153 const efi_guid_t *guid, bool short_path,
154 struct efi_device_path **rem)
156 efi_handle_t handle, best_handle = NULL;
157 efi_uintn_t len, best_len = 0;
159 len = efi_dp_instance_size(dp);
161 list_for_each_entry(handle, &efi_obj_list, link) {
162 struct efi_handler *handler;
163 struct efi_device_path *dp_current;
164 efi_uintn_t len_current;
168 ret = efi_search_protocol(handle, guid, &handler);
169 if (ret != EFI_SUCCESS)
172 ret = efi_search_protocol(handle, &efi_guid_device_path,
174 if (ret != EFI_SUCCESS)
176 dp_current = handler->protocol_interface;
178 dp_current = efi_dp_shorten(dp_current);
182 len_current = efi_dp_instance_size(dp_current);
184 if (len_current > len)
187 if (len_current != len)
190 if (memcmp(dp_current, dp, len_current))
194 if (len_current > best_len) {
195 best_len = len_current;
196 best_handle = handle;
197 *rem = (void*)((u8 *)dp + len_current);
204 * efi_dp_find_obj() - find handle by device path
206 * If @rem is provided, the handle with the longest partial match is returned.
208 * @dp: device path to search
209 * @guid: GUID of protocol that must be installed on path or NULL
210 * @rem: pointer to receive remaining device path
211 * Return: matching handle
213 efi_handle_t efi_dp_find_obj(struct efi_device_path *dp,
214 const efi_guid_t *guid,
215 struct efi_device_path **rem)
219 handle = find_handle(dp, guid, false, rem);
221 /* Match short form device path */
222 handle = find_handle(dp, guid, true, rem);
228 * Determine the last device path node that is not the end node.
231 * Return: last node before the end node if it exists
234 const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
236 struct efi_device_path *ret;
238 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
241 ret = (struct efi_device_path *)dp;
242 dp = efi_dp_next(dp);
247 /* get size of the first device path instance excluding end node */
248 efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
252 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
256 dp = efi_dp_next(dp);
262 /* get size of multi-instance device path excluding end node */
263 efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
265 const struct efi_device_path *p = dp;
269 while (p->type != DEVICE_PATH_TYPE_END ||
270 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
271 p = (void *)p + p->length;
273 return (void *)p - (void *)dp;
276 /* copy multi-instance device path */
277 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
279 struct efi_device_path *ndp;
280 size_t sz = efi_dp_size(dp) + sizeof(END);
294 * efi_dp_append_or_concatenate() - Append or concatenate two device paths.
295 * Concatenated device path will be separated
296 * by a sub-type 0xff end node
298 * @dp1: First device path
299 * @dp2: Second device path
300 * @concat: If true the two device paths will be concatenated and separated
301 * by an end of entrire device path sub-type 0xff end node.
302 * If true the second device path will be appended to the first and
303 * terminated by an end node
306 * concatenated device path or NULL. Caller must free the returned value
309 efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
310 const struct efi_device_path *dp2,
313 struct efi_device_path *ret;
314 size_t end_size = sizeof(END);
317 end_size = 2 * sizeof(END);
319 /* return an end node */
320 ret = efi_dp_dup(&END);
322 ret = efi_dp_dup(dp2);
324 ret = efi_dp_dup(dp1);
326 /* both dp1 and dp2 are non-null */
327 unsigned sz1 = efi_dp_size(dp1);
328 unsigned sz2 = efi_dp_size(dp2);
329 void *p = efi_alloc(sz1 + sz2 + end_size);
337 memcpy(p, &END, sizeof(END));
341 /* the end node of the second device path has to be retained */
344 memcpy(p, &END, sizeof(END));
351 * efi_dp_append() - Append a device to an existing device path.
353 * @dp1: First device path
354 * @dp2: Second device path
357 * concatenated device path or NULL. Caller must free the returned value
359 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
360 const struct efi_device_path *dp2)
362 return efi_dp_append_or_concatenate(dp1, dp2, false);
366 * efi_dp_concat() - Concatenate 2 device paths. The final device path will
367 * contain two device paths separated by and end node (0xff).
369 * @dp1: First device path
370 * @dp2: Second device path
373 * concatenated device path or NULL. Caller must free the returned value
375 struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
376 const struct efi_device_path *dp2)
378 return efi_dp_append_or_concatenate(dp1, dp2, true);
381 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
382 const struct efi_device_path *node)
384 struct efi_device_path *ret;
387 ret = efi_dp_dup(&END);
389 ret = efi_dp_dup(dp);
391 size_t sz = node->length;
392 void *p = efi_alloc(sz + sizeof(END));
396 memcpy(p + sz, &END, sizeof(END));
399 /* both dp and node are non-null */
400 size_t sz = efi_dp_size(dp);
401 void *p = efi_alloc(sz + node->length + sizeof(END));
405 memcpy(p + sz, node, node->length);
406 memcpy(p + sz + node->length, &END, sizeof(END));
413 struct efi_device_path *efi_dp_create_device_node(const u8 type,
417 struct efi_device_path *ret;
419 if (length < sizeof(struct efi_device_path))
422 ret = efi_alloc(length);
426 ret->sub_type = sub_type;
427 ret->length = length;
431 struct efi_device_path *efi_dp_append_instance(
432 const struct efi_device_path *dp,
433 const struct efi_device_path *dpi)
436 struct efi_device_path *p, *ret;
441 return efi_dp_dup(dpi);
442 sz = efi_dp_size(dp);
443 szi = efi_dp_instance_size(dpi);
444 p = efi_alloc(sz + szi + 2 * sizeof(END));
448 memcpy(p, dp, sz + sizeof(END));
450 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
451 p = (void *)p + sizeof(END);
454 memcpy(p, &END, sizeof(END));
458 struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
462 struct efi_device_path *p;
468 sz = efi_dp_instance_size(*dp);
469 p = efi_alloc(sz + sizeof(END));
472 memcpy(p, *dp, sz + sizeof(END));
473 *dp = (void *)*dp + sz;
474 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
475 *dp = (void *)*dp + sizeof(END);
479 *size = sz + sizeof(END);
483 bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
485 const struct efi_device_path *p = dp;
489 while (p->type != DEVICE_PATH_TYPE_END)
490 p = (void *)p + p->length;
491 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
494 /* size of device-path not including END node for device and all parents
495 * up to the root device.
497 __maybe_unused static unsigned int dp_size(struct udevice *dev)
499 if (!dev || !dev->driver)
502 switch (device_get_uclass_id(dev)) {
504 case UCLASS_SIMPLE_BUS:
505 /* stop traversing parents at this point: */
508 return dp_size(dev->parent) +
509 sizeof(struct efi_device_path_mac_addr);
511 switch (dev->parent->uclass->uc_drv->id) {
514 return dp_size(dev->parent) +
515 sizeof(struct efi_device_path_atapi);
517 #if defined(CONFIG_SCSI)
519 return dp_size(dev->parent) +
520 sizeof(struct efi_device_path_scsi);
522 #if defined(CONFIG_MMC)
524 return dp_size(dev->parent) +
525 sizeof(struct efi_device_path_sd_mmc_path);
527 #if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
529 return dp_size(dev->parent) +
530 sizeof(struct efi_device_path_sata);
532 #if defined(CONFIG_NVME)
534 return dp_size(dev->parent) +
535 sizeof(struct efi_device_path_nvme);
537 #ifdef CONFIG_SANDBOX
540 * Sandbox's host device will be represented
541 * as vendor device with extra one byte for
544 return dp_size(dev->parent)
545 + sizeof(struct efi_device_path_vendor) + 1;
548 case UCLASS_MASS_STORAGE:
549 return dp_size(dev->parent)
550 + sizeof(struct efi_device_path_controller);
552 #ifdef CONFIG_VIRTIO_BLK
555 * Virtio devices will be represented as a vendor
556 * device node with an extra byte for the device
559 return dp_size(dev->parent)
560 + sizeof(struct efi_device_path_vendor) + 1;
565 * blkmap devices will be represented as a vendor
566 * device node with an extra byte for the device
569 return dp_size(dev->parent)
570 + sizeof(struct efi_device_path_vendor) + 1;
573 return dp_size(dev->parent);
575 #if defined(CONFIG_MMC)
577 return dp_size(dev->parent) +
578 sizeof(struct efi_device_path_sd_mmc_path);
580 case UCLASS_MASS_STORAGE:
582 return dp_size(dev->parent) +
583 sizeof(struct efi_device_path_usb);
585 /* just skip over unknown classes: */
586 return dp_size(dev->parent);
591 * Recursively build a device path.
593 * @buf pointer to the end of the device path
595 * Return: pointer to the end of the device path
597 __maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
599 if (!dev || !dev->driver)
602 switch (device_get_uclass_id(dev)) {
604 case UCLASS_SIMPLE_BUS: {
605 /* stop traversing parents at this point: */
606 struct efi_device_path_vendor *vdp = buf;
610 #ifdef CONFIG_NETDEVICES
612 struct efi_device_path_mac_addr *dp =
613 dp_fill(buf, dev->parent);
614 struct eth_pdata *pdata = dev_get_plat(dev);
616 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
617 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
618 dp->dp.length = sizeof(*dp);
619 memset(&dp->mac, 0, sizeof(dp->mac));
620 /* We only support IPv4 */
621 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
628 switch (dev->parent->uclass->uc_drv->id) {
630 case UCLASS_BLKMAP: {
631 struct efi_device_path_vendor *dp;
632 struct blk_desc *desc = dev_get_uclass_plat(dev);
634 dp_fill(buf, dev->parent);
637 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
638 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
639 dp->dp.length = sizeof(*dp) + 1;
640 memcpy(&dp->guid, &efi_guid_blkmap_dev,
642 dp->vendor_data[0] = desc->devnum;
643 return &dp->vendor_data[1];
646 #ifdef CONFIG_SANDBOX
648 /* stop traversing parents at this point: */
649 struct efi_device_path_vendor *dp;
650 struct blk_desc *desc = dev_get_uclass_plat(dev);
652 dp_fill(buf, dev->parent);
655 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
656 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
657 dp->dp.length = sizeof(*dp) + 1;
658 memcpy(&dp->guid, &efi_guid_host_dev,
660 dp->vendor_data[0] = desc->devnum;
661 return &dp->vendor_data[1];
664 #ifdef CONFIG_VIRTIO_BLK
665 case UCLASS_VIRTIO: {
666 struct efi_device_path_vendor *dp;
667 struct blk_desc *desc = dev_get_uclass_plat(dev);
669 dp_fill(buf, dev->parent);
672 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
673 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
674 dp->dp.length = sizeof(*dp) + 1;
675 memcpy(&dp->guid, &efi_guid_virtio_dev,
677 dp->vendor_data[0] = desc->devnum;
678 return &dp->vendor_data[1];
683 struct efi_device_path_atapi *dp =
684 dp_fill(buf, dev->parent);
685 struct blk_desc *desc = dev_get_uclass_plat(dev);
687 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
688 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
689 dp->dp.length = sizeof(*dp);
690 dp->logical_unit_number = desc->devnum;
691 dp->primary_secondary = IDE_BUS(desc->devnum);
692 dp->slave_master = desc->devnum %
693 (CONFIG_SYS_IDE_MAXDEVICE /
694 CONFIG_SYS_IDE_MAXBUS);
698 #if defined(CONFIG_SCSI)
700 struct efi_device_path_scsi *dp =
701 dp_fill(buf, dev->parent);
702 struct blk_desc *desc = dev_get_uclass_plat(dev);
704 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
705 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
706 dp->dp.length = sizeof(*dp);
707 dp->logical_unit_number = desc->lun;
708 dp->target_id = desc->target;
712 #if defined(CONFIG_MMC)
714 struct efi_device_path_sd_mmc_path *sddp =
715 dp_fill(buf, dev->parent);
716 struct blk_desc *desc = dev_get_uclass_plat(dev);
718 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
719 sddp->dp.sub_type = is_sd(desc) ?
720 DEVICE_PATH_SUB_TYPE_MSG_SD :
721 DEVICE_PATH_SUB_TYPE_MSG_MMC;
722 sddp->dp.length = sizeof(*sddp);
723 sddp->slot_number = dev_seq(dev);
727 #if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
729 struct efi_device_path_sata *dp =
730 dp_fill(buf, dev->parent);
731 struct blk_desc *desc = dev_get_uclass_plat(dev);
733 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
734 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
735 dp->dp.length = sizeof(*dp);
736 dp->hba_port = desc->devnum;
737 /* default 0xffff implies no port multiplier */
738 dp->port_multiplier_port = 0xffff;
739 dp->logical_unit_number = desc->lun;
743 #if defined(CONFIG_NVME)
745 struct efi_device_path_nvme *dp =
746 dp_fill(buf, dev->parent);
749 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
750 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
751 dp->dp.length = sizeof(*dp);
752 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
753 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
757 #if defined(CONFIG_USB)
758 case UCLASS_MASS_STORAGE: {
759 struct blk_desc *desc = dev_get_uclass_plat(dev);
760 struct efi_device_path_controller *dp =
761 dp_fill(buf, dev->parent);
763 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
764 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CONTROLLER;
765 dp->dp.length = sizeof(*dp);
766 dp->controller_number = desc->lun;
771 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
772 __FILE__, __LINE__, __func__,
773 dev->name, dev->parent->uclass->uc_drv->id);
774 return dp_fill(buf, dev->parent);
776 #if defined(CONFIG_MMC)
778 struct efi_device_path_sd_mmc_path *sddp =
779 dp_fill(buf, dev->parent);
780 struct mmc *mmc = mmc_get_mmc_dev(dev);
781 struct blk_desc *desc = mmc_get_blk_desc(mmc);
783 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
784 sddp->dp.sub_type = is_sd(desc) ?
785 DEVICE_PATH_SUB_TYPE_MSG_SD :
786 DEVICE_PATH_SUB_TYPE_MSG_MMC;
787 sddp->dp.length = sizeof(*sddp);
788 sddp->slot_number = dev_seq(dev);
793 case UCLASS_MASS_STORAGE:
794 case UCLASS_USB_HUB: {
795 struct efi_device_path_usb *udp = dp_fill(buf, dev->parent);
797 switch (device_get_uclass_id(dev->parent)) {
798 case UCLASS_USB_HUB: {
799 struct usb_device *udev = dev_get_parent_priv(dev);
801 udp->parent_port_number = udev->portnr;
805 udp->parent_port_number = 0;
807 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
808 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
809 udp->dp.length = sizeof(*udp);
810 udp->usb_interface = 0;
815 /* If the uclass driver is missing, this will show NULL */
816 log_debug("unhandled device class: %s (%s)\n", dev->name,
817 dev_get_uclass_name(dev));
818 return dp_fill(buf, dev->parent);
822 static unsigned dp_part_size(struct blk_desc *desc, int part)
825 struct udevice *dev = desc->bdev;
827 dpsize = dp_size(dev);
829 if (part == 0) /* the actual disk, not a partition */
832 if (desc->part_type == PART_TYPE_ISO)
833 dpsize += sizeof(struct efi_device_path_cdrom_path);
835 dpsize += sizeof(struct efi_device_path_hard_drive_path);
841 * Create a device node for a block device partition.
843 * @buf buffer to which the device path is written
844 * @desc block device descriptor
845 * @part partition number, 0 identifies a block device
847 * Return: pointer to position after the node
849 static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
851 struct disk_partition info;
854 ret = part_get_info(desc, part, &info);
858 if (desc->part_type == PART_TYPE_ISO) {
859 struct efi_device_path_cdrom_path *cddp = buf;
861 cddp->boot_entry = part;
862 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
863 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
864 cddp->dp.length = sizeof(*cddp);
865 cddp->partition_start = info.start;
866 cddp->partition_size = info.size;
870 struct efi_device_path_hard_drive_path *hddp = buf;
872 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
873 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
874 hddp->dp.length = sizeof(*hddp);
875 hddp->partition_number = part;
876 hddp->partition_start = info.start;
877 hddp->partition_end = info.size;
878 if (desc->part_type == PART_TYPE_EFI)
879 hddp->partmap_type = 2;
881 hddp->partmap_type = 1;
883 switch (desc->sig_type) {
886 hddp->signature_type = 0;
887 memset(hddp->partition_signature, 0,
888 sizeof(hddp->partition_signature));
891 hddp->signature_type = 1;
892 memset(hddp->partition_signature, 0,
893 sizeof(hddp->partition_signature));
894 memcpy(hddp->partition_signature, &desc->mbr_sig,
895 sizeof(desc->mbr_sig));
898 hddp->signature_type = 2;
899 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
900 /* info.uuid exists only with PARTITION_UUIDS */
901 if (uuid_str_to_bin(info.uuid,
902 hddp->partition_signature,
903 UUID_STR_FORMAT_GUID)) {
905 "Partition %d: invalid GUID %s\n",
919 * Create a device path for a block device or one of its partitions.
921 * @buf buffer to which the device path is written
922 * @desc block device descriptor
923 * @part partition number, 0 identifies a block device
925 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
927 struct udevice *dev = desc->bdev;
929 buf = dp_fill(buf, dev);
931 if (part == 0) /* the actual disk, not a partition */
934 return dp_part_node(buf, desc, part);
937 /* Construct a device-path from a partition on a block device: */
938 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
942 start = buf = efi_alloc(dp_part_size(desc, part) + sizeof(END));
946 buf = dp_part_fill(buf, desc, part);
948 *((struct efi_device_path *)buf) = END;
954 * Create a device node for a block device partition.
956 * @buf buffer to which the device path is written
957 * @desc block device descriptor
958 * @part partition number, 0 identifies a block device
960 struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
965 if (desc->part_type == PART_TYPE_ISO)
966 dpsize = sizeof(struct efi_device_path_cdrom_path);
968 dpsize = sizeof(struct efi_device_path_hard_drive_path);
969 buf = efi_alloc(dpsize);
972 dp_part_node(buf, desc, part);
978 * path_to_uefi() - convert UTF-8 path to an UEFI style path
980 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
981 * separators and UTF-16).
983 * @src: source buffer
984 * @uefi: target buffer, possibly unaligned
986 static void path_to_uefi(void *uefi, const char *src)
991 * efi_set_bootdev() calls this routine indirectly before the UEFI
992 * subsystem is initialized. So we cannot assume unaligned access to be
998 s32 code = utf8_get(&src);
1002 else if (code == '/')
1004 utf16_put(code, &pos);
1010 * efi_dp_from_file() - append file path node to device path.
1012 * @dp: device path or NULL
1013 * @path: file path or NULL
1014 * Return: device path or NULL in case of an error
1016 struct efi_device_path *efi_dp_from_file(const struct efi_device_path *dp,
1019 struct efi_device_path_file_path *fp;
1021 size_t dpsize, fpsize;
1023 dpsize = efi_dp_size(dp);
1024 fpsize = sizeof(struct efi_device_path) +
1025 2 * (utf8_utf16_strlen(path) + 1);
1026 if (fpsize > U16_MAX)
1029 buf = efi_alloc(dpsize + fpsize + sizeof(END));
1033 memcpy(buf, dp, dpsize);
1036 /* add file-path: */
1039 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
1040 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
1041 fp->dp.length = (u16)fpsize;
1042 path_to_uefi(fp->str, path);
1046 memcpy(pos, &END, sizeof(END));
1051 struct efi_device_path *efi_dp_from_uart(void)
1054 struct efi_device_path_uart *uart;
1055 size_t dpsize = sizeof(ROOT) + sizeof(*uart) + sizeof(END);
1057 buf = efi_alloc(dpsize);
1061 memcpy(pos, &ROOT, sizeof(ROOT));
1062 pos += sizeof(ROOT);
1064 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1065 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
1066 uart->dp.length = sizeof(*uart);
1067 pos += sizeof(*uart);
1068 memcpy(pos, &END, sizeof(END));
1073 struct efi_device_path __maybe_unused *efi_dp_from_eth(void)
1076 unsigned dpsize = 0;
1078 assert(eth_get_dev());
1080 dpsize += dp_size(eth_get_dev());
1082 start = buf = efi_alloc(dpsize + sizeof(END));
1086 buf = dp_fill(buf, eth_get_dev());
1088 *((struct efi_device_path *)buf) = END;
1093 /* Construct a device-path for memory-mapped image */
1094 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1095 uint64_t start_address,
1096 uint64_t end_address)
1098 struct efi_device_path_memory *mdp;
1101 start = buf = efi_alloc(sizeof(*mdp) + sizeof(END));
1106 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1107 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1108 mdp->dp.length = sizeof(*mdp);
1109 mdp->memory_type = memory_type;
1110 mdp->start_address = start_address;
1111 mdp->end_address = end_address;
1114 *((struct efi_device_path *)buf) = END;
1120 * efi_dp_split_file_path() - split of relative file path from device path
1122 * Given a device path indicating a file on a device, separate the device
1123 * path in two: the device path of the actual device and the file path
1124 * relative to this device.
1126 * @full_path: device path including device and file path
1127 * @device_path: path of the device
1128 * @file_path: relative path of the file or NULL if there is none
1129 * Return: status code
1131 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1132 struct efi_device_path **device_path,
1133 struct efi_device_path **file_path)
1135 struct efi_device_path *p, *dp, *fp = NULL;
1137 *device_path = NULL;
1139 dp = efi_dp_dup(full_path);
1141 return EFI_OUT_OF_RESOURCES;
1143 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
1150 return EFI_OUT_OF_RESOURCES;
1151 p->type = DEVICE_PATH_TYPE_END;
1152 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1153 p->length = sizeof(*p);
1162 * efi_dp_from_name() - convert U-Boot device and file path to device path
1164 * @dev: U-Boot device, e.g. 'mmc'
1165 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1166 * @path: file path relative to U-Boot device, may be NULL
1167 * @device: pointer to receive device path of the device
1168 * @file: pointer to receive device path for the file
1169 * Return: status code
1171 efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1173 struct efi_device_path **device,
1174 struct efi_device_path **file)
1176 struct blk_desc *desc = NULL;
1177 struct efi_device_path *dp;
1178 struct disk_partition fs_partition;
1184 return EFI_INVALID_PARAMETER;
1186 if (!strcmp(dev, "Mem") || !strcmp(dev, "hostfs")) {
1187 /* loadm command and semihosting */
1188 efi_get_image_parameters(&image_addr, &image_size);
1190 dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
1191 (uintptr_t)image_addr, image_size);
1192 } else if (IS_ENABLED(CONFIG_NETDEVICES) && !strcmp(dev, "Net")) {
1193 dp = efi_dp_from_eth();
1194 } else if (!strcmp(dev, "Uart")) {
1195 dp = efi_dp_from_uart();
1197 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1199 if (part < 0 || !desc)
1200 return EFI_INVALID_PARAMETER;
1202 dp = efi_dp_from_part(desc, part);
1210 *file = efi_dp_from_file(dp, path);
1212 return EFI_OUT_OF_RESOURCES;
1218 * efi_dp_check_length() - check length of a device path
1220 * @dp: pointer to device path
1221 * @maxlen: maximum length of the device path
1223 * * length of the device path if it is less or equal @maxlen
1224 * * -1 if the device path is longer then @maxlen
1225 * * -1 if a device path node has a length of less than 4
1226 * * -EINVAL if maxlen exceeds SSIZE_MAX
1228 ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1229 const size_t maxlen)
1234 if (maxlen > SSIZE_MAX)
1243 if (dp->type == DEVICE_PATH_TYPE_END &&
1244 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1246 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1251 * efi_dp_from_lo() - Get the instance of a VenMedia node in a
1252 * multi-instance device path that matches
1253 * a specific GUID. This kind of device paths
1254 * is found in Boot#### options describing an
1257 * @lo: EFI_LOAD_OPTION containing a valid device path
1258 * @guid: guid to search for
1261 * device path including the VenMedia node or NULL.
1262 * Caller must free the returned value.
1265 efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
1266 const efi_guid_t *guid)
1268 struct efi_device_path *fp = lo->file_path;
1269 struct efi_device_path_vendor *vendor;
1270 int lo_len = lo->file_path_length;
1272 for (; lo_len >= sizeof(struct efi_device_path);
1273 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1274 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1276 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1277 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1280 vendor = (struct efi_device_path_vendor *)fp;
1281 if (!guidcmp(&vendor->guid, guid))
1282 return efi_dp_dup(efi_dp_next(fp));
1284 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1290 * search_gpt_dp_node() - search gpt device path node
1292 * @device_path: device path
1294 * Return: pointer to the gpt device path node
1296 struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1298 struct efi_device_path *dp = device_path;
1301 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1302 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1303 struct efi_device_path_hard_drive_path *hd_dp =
1304 (struct efi_device_path_hard_drive_path *)dp;
1306 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1307 hd_dp->signature_type == SIG_TYPE_GUID)
1310 dp = efi_dp_next(dp);