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>
20 #include <sandboxblockdev.h>
22 #include <asm-generic/unaligned.h>
23 #include <linux/compat.h> /* U16_MAX */
26 const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
28 #ifdef CONFIG_VIRTIO_BLK
29 const efi_guid_t efi_guid_virtio_dev = U_BOOT_VIRTIO_DEV_GUID;
32 /* template END node: */
33 const struct efi_device_path END = {
34 .type = DEVICE_PATH_TYPE_END,
35 .sub_type = DEVICE_PATH_SUB_TYPE_END,
36 .length = sizeof(END),
39 /* template ROOT node: */
40 static const struct efi_device_path_vendor ROOT = {
42 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
43 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
44 .length = sizeof(ROOT),
49 #if defined(CONFIG_MMC)
51 * Determine if an MMC device is an SD card.
53 * @desc block device descriptor
54 * Return: true if the device is an SD card
56 static bool is_sd(struct blk_desc *desc)
58 struct mmc *mmc = find_mmc_device(desc->devnum);
63 return IS_SD(mmc) != 0U;
67 static void *dp_alloc(size_t sz)
71 if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, sz, &buf) !=
73 debug("EFI: ERROR: out of memory in %s\n", __func__);
82 * Iterate to next block in device-path, terminating (returning NULL)
85 struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
89 if (dp->type == DEVICE_PATH_TYPE_END)
91 dp = ((void *)dp) + dp->length;
92 if (dp->type == DEVICE_PATH_TYPE_END)
94 return (struct efi_device_path *)dp;
98 * Compare two device-paths, stopping when the shorter of the two hits
99 * an End* node. This is useful to, for example, compare a device-path
100 * representing a device with one representing a file on the device, or
101 * a device with a parent device.
103 int efi_dp_match(const struct efi_device_path *a,
104 const struct efi_device_path *b)
109 ret = memcmp(&a->length, &b->length, sizeof(a->length));
113 ret = memcmp(a, b, a->length);
126 * efi_dp_shorten() - shorten device-path
128 * We can have device paths that start with a USB WWID or a USB Class node,
129 * and a few other cases which don't encode the full device path with bus
132 * * MESSAGING:USB_WWID
133 * * MESSAGING:USB_CLASS
138 * See UEFI spec (section 3.1.2, about short-form device-paths)
140 * @dp: original device-path
141 * @Return: shortened device-path or NULL
143 struct efi_device_path *efi_dp_shorten(struct efi_device_path *dp)
147 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
148 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
149 * so not sure when we would see these other cases.
151 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
152 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
153 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
156 dp = efi_dp_next(dp);
163 * find_handle() - find handle by device path and installed protocol
165 * If @rem is provided, the handle with the longest partial match is returned.
167 * @dp: device path to search
168 * @guid: GUID of protocol that must be installed on path or NULL
169 * @short_path: use short form device path for matching
170 * @rem: pointer to receive remaining device path
171 * Return: matching handle
173 static efi_handle_t find_handle(struct efi_device_path *dp,
174 const efi_guid_t *guid, bool short_path,
175 struct efi_device_path **rem)
177 efi_handle_t handle, best_handle = NULL;
178 efi_uintn_t len, best_len = 0;
180 len = efi_dp_instance_size(dp);
182 list_for_each_entry(handle, &efi_obj_list, link) {
183 struct efi_handler *handler;
184 struct efi_device_path *dp_current;
185 efi_uintn_t len_current;
189 ret = efi_search_protocol(handle, guid, &handler);
190 if (ret != EFI_SUCCESS)
193 ret = efi_search_protocol(handle, &efi_guid_device_path,
195 if (ret != EFI_SUCCESS)
197 dp_current = handler->protocol_interface;
199 dp_current = efi_dp_shorten(dp_current);
203 len_current = efi_dp_instance_size(dp_current);
205 if (len_current > len)
208 if (len_current != len)
211 if (memcmp(dp_current, dp, len_current))
215 if (len_current > best_len) {
216 best_len = len_current;
217 best_handle = handle;
218 *rem = (void*)((u8 *)dp + len_current);
225 * efi_dp_find_obj() - find handle by device path
227 * If @rem is provided, the handle with the longest partial match is returned.
229 * @dp: device path to search
230 * @guid: GUID of protocol that must be installed on path or NULL
231 * @rem: pointer to receive remaining device path
232 * Return: matching handle
234 efi_handle_t efi_dp_find_obj(struct efi_device_path *dp,
235 const efi_guid_t *guid,
236 struct efi_device_path **rem)
240 handle = find_handle(dp, guid, false, rem);
242 /* Match short form device path */
243 handle = find_handle(dp, guid, true, rem);
249 * Determine the last device path node that is not the end node.
252 * Return: last node before the end node if it exists
255 const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
257 struct efi_device_path *ret;
259 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
262 ret = (struct efi_device_path *)dp;
263 dp = efi_dp_next(dp);
268 /* get size of the first device path instance excluding end node */
269 efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
273 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
277 dp = efi_dp_next(dp);
283 /* get size of multi-instance device path excluding end node */
284 efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
286 const struct efi_device_path *p = dp;
290 while (p->type != DEVICE_PATH_TYPE_END ||
291 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
292 p = (void *)p + p->length;
294 return (void *)p - (void *)dp;
297 /* copy multi-instance device path */
298 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
300 struct efi_device_path *ndp;
301 size_t sz = efi_dp_size(dp) + sizeof(END);
315 * efi_dp_append_or_concatenate() - Append or concatenate two device paths.
316 * Concatenated device path will be separated
317 * by a sub-type 0xff end node
319 * @dp1: First device path
320 * @dp2: Second device path
321 * @concat: If true the two device paths will be concatenated and separated
322 * by an end of entrire device path sub-type 0xff end node.
323 * If true the second device path will be appended to the first and
324 * terminated by an end node
327 * concatenated device path or NULL. Caller must free the returned value
330 efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
331 const struct efi_device_path *dp2,
334 struct efi_device_path *ret;
335 size_t end_size = sizeof(END);
338 end_size = 2 * sizeof(END);
340 /* return an end node */
341 ret = efi_dp_dup(&END);
343 ret = efi_dp_dup(dp2);
345 ret = efi_dp_dup(dp1);
347 /* both dp1 and dp2 are non-null */
348 unsigned sz1 = efi_dp_size(dp1);
349 unsigned sz2 = efi_dp_size(dp2);
350 void *p = dp_alloc(sz1 + sz2 + end_size);
358 memcpy(p, &END, sizeof(END));
362 /* the end node of the second device path has to be retained */
365 memcpy(p, &END, sizeof(END));
372 * efi_dp_append() - Append a device to an existing device path.
374 * @dp1: First device path
375 * @dp2: Second device path
378 * concatenated device path or NULL. Caller must free the returned value
380 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
381 const struct efi_device_path *dp2)
383 return efi_dp_append_or_concatenate(dp1, dp2, false);
387 * efi_dp_concat() - Concatenate 2 device paths. The final device path will
388 * contain two device paths separated by and end node (0xff).
390 * @dp1: First device path
391 * @dp2: Second device path
394 * concatenated device path or NULL. Caller must free the returned value
396 struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
397 const struct efi_device_path *dp2)
399 return efi_dp_append_or_concatenate(dp1, dp2, true);
402 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
403 const struct efi_device_path *node)
405 struct efi_device_path *ret;
408 ret = efi_dp_dup(&END);
410 ret = efi_dp_dup(dp);
412 size_t sz = node->length;
413 void *p = dp_alloc(sz + sizeof(END));
417 memcpy(p + sz, &END, sizeof(END));
420 /* both dp and node are non-null */
421 size_t sz = efi_dp_size(dp);
422 void *p = dp_alloc(sz + node->length + sizeof(END));
426 memcpy(p + sz, node, node->length);
427 memcpy(p + sz + node->length, &END, sizeof(END));
434 struct efi_device_path *efi_dp_create_device_node(const u8 type,
438 struct efi_device_path *ret;
440 if (length < sizeof(struct efi_device_path))
443 ret = dp_alloc(length);
447 ret->sub_type = sub_type;
448 ret->length = length;
452 struct efi_device_path *efi_dp_append_instance(
453 const struct efi_device_path *dp,
454 const struct efi_device_path *dpi)
457 struct efi_device_path *p, *ret;
462 return efi_dp_dup(dpi);
463 sz = efi_dp_size(dp);
464 szi = efi_dp_instance_size(dpi);
465 p = dp_alloc(sz + szi + 2 * sizeof(END));
469 memcpy(p, dp, sz + sizeof(END));
471 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
472 p = (void *)p + sizeof(END);
475 memcpy(p, &END, sizeof(END));
479 struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
483 struct efi_device_path *p;
489 sz = efi_dp_instance_size(*dp);
490 p = dp_alloc(sz + sizeof(END));
493 memcpy(p, *dp, sz + sizeof(END));
494 *dp = (void *)*dp + sz;
495 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
496 *dp = (void *)*dp + sizeof(END);
500 *size = sz + sizeof(END);
504 bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
506 const struct efi_device_path *p = dp;
510 while (p->type != DEVICE_PATH_TYPE_END)
511 p = (void *)p + p->length;
512 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
515 /* size of device-path not including END node for device and all parents
516 * up to the root device.
518 __maybe_unused static unsigned int dp_size(struct udevice *dev)
520 if (!dev || !dev->driver)
523 switch (device_get_uclass_id(dev)) {
525 case UCLASS_SIMPLE_BUS:
526 /* stop traversing parents at this point: */
529 return dp_size(dev->parent) +
530 sizeof(struct efi_device_path_mac_addr);
532 switch (dev->parent->uclass->uc_drv->id) {
535 return dp_size(dev->parent) +
536 sizeof(struct efi_device_path_atapi);
538 #if defined(CONFIG_SCSI)
540 return dp_size(dev->parent) +
541 sizeof(struct efi_device_path_scsi);
543 #if defined(CONFIG_MMC)
545 return dp_size(dev->parent) +
546 sizeof(struct efi_device_path_sd_mmc_path);
548 #if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
550 return dp_size(dev->parent) +
551 sizeof(struct efi_device_path_sata);
553 #if defined(CONFIG_NVME)
555 return dp_size(dev->parent) +
556 sizeof(struct efi_device_path_nvme);
558 #ifdef CONFIG_SANDBOX
561 * Sandbox's host device will be represented
562 * as vendor device with extra one byte for
565 return dp_size(dev->parent)
566 + sizeof(struct efi_device_path_vendor) + 1;
568 #ifdef CONFIG_VIRTIO_BLK
571 * Virtio devices will be represented as a vendor
572 * device node with an extra byte for the device
575 return dp_size(dev->parent)
576 + sizeof(struct efi_device_path_vendor) + 1;
579 return dp_size(dev->parent);
581 #if defined(CONFIG_MMC)
583 return dp_size(dev->parent) +
584 sizeof(struct efi_device_path_sd_mmc_path);
586 case UCLASS_MASS_STORAGE:
588 return dp_size(dev->parent) +
589 sizeof(struct efi_device_path_usb_class);
591 /* just skip over unknown classes: */
592 return dp_size(dev->parent);
597 * Recursively build a device path.
599 * @buf pointer to the end of the device path
601 * Return: pointer to the end of the device path
603 __maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
605 if (!dev || !dev->driver)
608 switch (device_get_uclass_id(dev)) {
610 case UCLASS_SIMPLE_BUS: {
611 /* stop traversing parents at this point: */
612 struct efi_device_path_vendor *vdp = buf;
618 struct efi_device_path_mac_addr *dp =
619 dp_fill(buf, dev->parent);
620 struct eth_pdata *pdata = dev_get_plat(dev);
622 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
623 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
624 dp->dp.length = sizeof(*dp);
625 memset(&dp->mac, 0, sizeof(dp->mac));
626 /* We only support IPv4 */
627 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
634 switch (dev->parent->uclass->uc_drv->id) {
635 #ifdef CONFIG_SANDBOX
637 /* stop traversing parents at this point: */
638 struct efi_device_path_vendor *dp;
639 struct blk_desc *desc = dev_get_uclass_plat(dev);
641 dp_fill(buf, dev->parent);
644 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
645 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
646 dp->dp.length = sizeof(*dp) + 1;
647 memcpy(&dp->guid, &efi_guid_host_dev,
649 dp->vendor_data[0] = desc->devnum;
650 return &dp->vendor_data[1];
653 #ifdef CONFIG_VIRTIO_BLK
654 case UCLASS_VIRTIO: {
655 struct efi_device_path_vendor *dp;
656 struct blk_desc *desc = dev_get_uclass_plat(dev);
658 dp_fill(buf, dev->parent);
661 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
662 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
663 dp->dp.length = sizeof(*dp) + 1;
664 memcpy(&dp->guid, &efi_guid_virtio_dev,
666 dp->vendor_data[0] = desc->devnum;
667 return &dp->vendor_data[1];
672 struct efi_device_path_atapi *dp =
673 dp_fill(buf, dev->parent);
674 struct blk_desc *desc = dev_get_uclass_plat(dev);
676 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
677 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
678 dp->dp.length = sizeof(*dp);
679 dp->logical_unit_number = desc->devnum;
680 dp->primary_secondary = IDE_BUS(desc->devnum);
681 dp->slave_master = desc->devnum %
682 (CONFIG_SYS_IDE_MAXDEVICE /
683 CONFIG_SYS_IDE_MAXBUS);
687 #if defined(CONFIG_SCSI)
689 struct efi_device_path_scsi *dp =
690 dp_fill(buf, dev->parent);
691 struct blk_desc *desc = dev_get_uclass_plat(dev);
693 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
694 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
695 dp->dp.length = sizeof(*dp);
696 dp->logical_unit_number = desc->lun;
697 dp->target_id = desc->target;
701 #if defined(CONFIG_MMC)
703 struct efi_device_path_sd_mmc_path *sddp =
704 dp_fill(buf, dev->parent);
705 struct blk_desc *desc = dev_get_uclass_plat(dev);
707 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
708 sddp->dp.sub_type = is_sd(desc) ?
709 DEVICE_PATH_SUB_TYPE_MSG_SD :
710 DEVICE_PATH_SUB_TYPE_MSG_MMC;
711 sddp->dp.length = sizeof(*sddp);
712 sddp->slot_number = dev_seq(dev);
716 #if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
718 struct efi_device_path_sata *dp =
719 dp_fill(buf, dev->parent);
720 struct blk_desc *desc = dev_get_uclass_plat(dev);
722 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
723 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
724 dp->dp.length = sizeof(*dp);
725 dp->hba_port = desc->devnum;
726 /* default 0xffff implies no port multiplier */
727 dp->port_multiplier_port = 0xffff;
728 dp->logical_unit_number = desc->lun;
732 #if defined(CONFIG_NVME)
734 struct efi_device_path_nvme *dp =
735 dp_fill(buf, dev->parent);
738 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
739 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
740 dp->dp.length = sizeof(*dp);
741 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
742 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
747 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
748 __FILE__, __LINE__, __func__,
749 dev->name, dev->parent->uclass->uc_drv->id);
750 return dp_fill(buf, dev->parent);
752 #if defined(CONFIG_MMC)
754 struct efi_device_path_sd_mmc_path *sddp =
755 dp_fill(buf, dev->parent);
756 struct mmc *mmc = mmc_get_mmc_dev(dev);
757 struct blk_desc *desc = mmc_get_blk_desc(mmc);
759 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
760 sddp->dp.sub_type = is_sd(desc) ?
761 DEVICE_PATH_SUB_TYPE_MSG_SD :
762 DEVICE_PATH_SUB_TYPE_MSG_MMC;
763 sddp->dp.length = sizeof(*sddp);
764 sddp->slot_number = dev_seq(dev);
769 case UCLASS_MASS_STORAGE:
770 case UCLASS_USB_HUB: {
771 struct efi_device_path_usb_class *udp =
772 dp_fill(buf, dev->parent);
773 struct usb_device *udev = dev_get_parent_priv(dev);
774 struct usb_device_descriptor *desc = &udev->descriptor;
776 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
777 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
778 udp->dp.length = sizeof(*udp);
779 udp->vendor_id = desc->idVendor;
780 udp->product_id = desc->idProduct;
781 udp->device_class = desc->bDeviceClass;
782 udp->device_subclass = desc->bDeviceSubClass;
783 udp->device_protocol = desc->bDeviceProtocol;
788 /* If the uclass driver is missing, this will show NULL */
789 log_debug("unhandled device class: %s (%s)\n", dev->name,
790 dev_get_uclass_name(dev));
791 return dp_fill(buf, dev->parent);
795 static unsigned dp_part_size(struct blk_desc *desc, int part)
798 struct udevice *dev = desc->bdev;
800 dpsize = dp_size(dev);
802 if (part == 0) /* the actual disk, not a partition */
805 if (desc->part_type == PART_TYPE_ISO)
806 dpsize += sizeof(struct efi_device_path_cdrom_path);
808 dpsize += sizeof(struct efi_device_path_hard_drive_path);
814 * Create a device node for a block device partition.
816 * @buf buffer to which the device path is written
817 * @desc block device descriptor
818 * @part partition number, 0 identifies a block device
820 static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
822 struct disk_partition info;
824 part_get_info(desc, part, &info);
826 if (desc->part_type == PART_TYPE_ISO) {
827 struct efi_device_path_cdrom_path *cddp = buf;
829 cddp->boot_entry = part;
830 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
831 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
832 cddp->dp.length = sizeof(*cddp);
833 cddp->partition_start = info.start;
834 cddp->partition_size = info.size;
838 struct efi_device_path_hard_drive_path *hddp = buf;
840 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
841 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
842 hddp->dp.length = sizeof(*hddp);
843 hddp->partition_number = part;
844 hddp->partition_start = info.start;
845 hddp->partition_end = info.size;
846 if (desc->part_type == PART_TYPE_EFI)
847 hddp->partmap_type = 2;
849 hddp->partmap_type = 1;
851 switch (desc->sig_type) {
854 hddp->signature_type = 0;
855 memset(hddp->partition_signature, 0,
856 sizeof(hddp->partition_signature));
859 hddp->signature_type = 1;
860 memset(hddp->partition_signature, 0,
861 sizeof(hddp->partition_signature));
862 memcpy(hddp->partition_signature, &desc->mbr_sig,
863 sizeof(desc->mbr_sig));
866 hddp->signature_type = 2;
867 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
868 /* info.uuid exists only with PARTITION_UUIDS */
869 if (uuid_str_to_bin(info.uuid,
870 hddp->partition_signature,
871 UUID_STR_FORMAT_GUID)) {
873 "Partition %d: invalid GUID %s\n",
887 * Create a device path for a block device or one of its partitions.
889 * @buf buffer to which the device path is written
890 * @desc block device descriptor
891 * @part partition number, 0 identifies a block device
893 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
895 struct udevice *dev = desc->bdev;
897 buf = dp_fill(buf, dev);
899 if (part == 0) /* the actual disk, not a partition */
902 return dp_part_node(buf, desc, part);
905 /* Construct a device-path from a partition on a block device: */
906 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
910 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
914 buf = dp_part_fill(buf, desc, part);
916 *((struct efi_device_path *)buf) = END;
922 * Create a device node for a block device partition.
924 * @buf buffer to which the device path is written
925 * @desc block device descriptor
926 * @part partition number, 0 identifies a block device
928 struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
933 if (desc->part_type == PART_TYPE_ISO)
934 dpsize = sizeof(struct efi_device_path_cdrom_path);
936 dpsize = sizeof(struct efi_device_path_hard_drive_path);
937 buf = dp_alloc(dpsize);
939 dp_part_node(buf, desc, part);
945 * path_to_uefi() - convert UTF-8 path to an UEFI style path
947 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
948 * separators and UTF-16).
950 * @src: source buffer
951 * @uefi: target buffer, possibly unaligned
953 static void path_to_uefi(void *uefi, const char *src)
958 * efi_set_bootdev() calls this routine indirectly before the UEFI
959 * subsystem is initialized. So we cannot assume unaligned access to be
965 s32 code = utf8_get(&src);
969 else if (code == '/')
971 utf16_put(code, &pos);
977 * efi_dp_from_file() - create device path for file
979 * The function creates a device path from the block descriptor @desc and the
980 * partition number @part and appends a device path node created describing the
983 * If @desc is NULL, the device path will not contain nodes describing the
985 * If @path is an empty string "", the device path will not contain a node
988 * @desc: block device descriptor or NULL
989 * @part: partition number
990 * @path: file path on partition or ""
991 * Return: device path or NULL in case of an error
993 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
996 struct efi_device_path_file_path *fp;
998 size_t dpsize = 0, fpsize;
1001 dpsize = dp_part_size(desc, part);
1003 fpsize = sizeof(struct efi_device_path) +
1004 2 * (utf8_utf16_strlen(path) + 1);
1005 if (fpsize > U16_MAX)
1010 start = buf = dp_alloc(dpsize + sizeof(END));
1015 buf = dp_part_fill(buf, desc, part);
1017 /* add file-path: */
1020 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
1021 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
1022 fp->dp.length = (u16)fpsize;
1023 path_to_uefi(fp->str, path);
1027 *((struct efi_device_path *)buf) = END;
1032 struct efi_device_path *efi_dp_from_uart(void)
1035 struct efi_device_path_uart *uart;
1036 size_t dpsize = sizeof(ROOT) + sizeof(*uart) + sizeof(END);
1038 buf = dp_alloc(dpsize);
1042 memcpy(pos, &ROOT, sizeof(ROOT));
1043 pos += sizeof(ROOT);
1045 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1046 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
1047 uart->dp.length = sizeof(*uart);
1048 pos += sizeof(*uart);
1049 memcpy(pos, &END, sizeof(END));
1055 struct efi_device_path *efi_dp_from_eth(void)
1058 unsigned dpsize = 0;
1060 assert(eth_get_dev());
1062 dpsize += dp_size(eth_get_dev());
1064 start = buf = dp_alloc(dpsize + sizeof(END));
1068 buf = dp_fill(buf, eth_get_dev());
1070 *((struct efi_device_path *)buf) = END;
1076 /* Construct a device-path for memory-mapped image */
1077 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1078 uint64_t start_address,
1079 uint64_t end_address)
1081 struct efi_device_path_memory *mdp;
1084 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
1089 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1090 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1091 mdp->dp.length = sizeof(*mdp);
1092 mdp->memory_type = memory_type;
1093 mdp->start_address = start_address;
1094 mdp->end_address = end_address;
1097 *((struct efi_device_path *)buf) = END;
1103 * efi_dp_split_file_path() - split of relative file path from device path
1105 * Given a device path indicating a file on a device, separate the device
1106 * path in two: the device path of the actual device and the file path
1107 * relative to this device.
1109 * @full_path: device path including device and file path
1110 * @device_path: path of the device
1111 * @file_path: relative path of the file or NULL if there is none
1112 * Return: status code
1114 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1115 struct efi_device_path **device_path,
1116 struct efi_device_path **file_path)
1118 struct efi_device_path *p, *dp, *fp = NULL;
1120 *device_path = NULL;
1122 dp = efi_dp_dup(full_path);
1124 return EFI_OUT_OF_RESOURCES;
1126 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
1133 return EFI_OUT_OF_RESOURCES;
1134 p->type = DEVICE_PATH_TYPE_END;
1135 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1136 p->length = sizeof(*p);
1145 * efi_dp_from_name() - convert U-Boot device and file path to device path
1147 * @dev: U-Boot device, e.g. 'mmc'
1148 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1149 * @path: file path relative to U-Boot device, may be NULL
1150 * @device: pointer to receive device path of the device
1151 * @file: pointer to receive device path for the file
1152 * Return: status code
1154 efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1156 struct efi_device_path **device,
1157 struct efi_device_path **file)
1159 struct blk_desc *desc = NULL;
1160 struct disk_partition fs_partition;
1168 return EFI_INVALID_PARAMETER;
1170 if (!strcmp(dev, "Net")) {
1173 *device = efi_dp_from_eth();
1175 } else if (!strcmp(dev, "Uart")) {
1177 *device = efi_dp_from_uart();
1178 } else if (!strcmp(dev, "Mem")) {
1179 efi_get_image_parameters(&image_addr, &image_size);
1182 *device = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
1183 (uintptr_t)image_addr,
1186 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1188 if (part < 0 || !desc)
1189 return EFI_INVALID_PARAMETER;
1192 *device = efi_dp_from_part(desc, part);
1198 filename = calloc(1, strlen(path) + 1);
1200 return EFI_OUT_OF_RESOURCES;
1202 sprintf(filename, "%s", path);
1203 /* DOS style file path: */
1205 while ((s = strchr(s, '/')))
1207 *file = efi_dp_from_file(desc, part, filename);
1211 return EFI_INVALID_PARAMETER;
1217 * efi_dp_check_length() - check length of a device path
1219 * @dp: pointer to device path
1220 * @maxlen: maximum length of the device path
1222 * * length of the device path if it is less or equal @maxlen
1223 * * -1 if the device path is longer then @maxlen
1224 * * -1 if a device path node has a length of less than 4
1225 * * -EINVAL if maxlen exceeds SSIZE_MAX
1227 ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1228 const size_t maxlen)
1233 if (maxlen > SSIZE_MAX)
1242 if (dp->type == DEVICE_PATH_TYPE_END &&
1243 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1245 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1250 * efi_dp_from_lo() - Get the instance of a VenMedia node in a
1251 * multi-instance device path that matches
1252 * a specific GUID. This kind of device paths
1253 * is found in Boot#### options describing an
1256 * @lo: EFI_LOAD_OPTION containing a valid device path
1257 * @guid: guid to search for
1260 * device path including the VenMedia node or NULL.
1261 * Caller must free the returned value.
1264 efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
1265 const efi_guid_t *guid)
1267 struct efi_device_path *fp = lo->file_path;
1268 struct efi_device_path_vendor *vendor;
1269 int lo_len = lo->file_path_length;
1271 for (; lo_len >= sizeof(struct efi_device_path);
1272 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1273 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1275 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1276 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1279 vendor = (struct efi_device_path_vendor *)fp;
1280 if (!guidcmp(&vendor->guid, guid))
1281 return efi_dp_dup(efi_dp_next(fp));
1283 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1289 * search_gpt_dp_node() - search gpt device path node
1291 * @device_path: device path
1293 * Return: pointer to the gpt device path node
1295 struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1297 struct efi_device_path *dp = device_path;
1300 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1301 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1302 struct efi_device_path_hard_drive_path *hd_dp =
1303 (struct efi_device_path_hard_drive_path *)dp;
1305 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1306 hd_dp->signature_type == SIG_TYPE_GUID)
1309 dp = efi_dp_next(dp);