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 static 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 * We can have device paths that start with a USB WWID or a USB Class node,
127 * and a few other cases which don't encode the full device path with bus
130 * - MESSAGING:USB_WWID
131 * - MESSAGING:USB_CLASS
136 * See UEFI spec (section 3.1.2, about short-form device-paths)
138 static struct efi_device_path *shorten_path(struct efi_device_path *dp)
142 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
143 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
144 * so not sure when we would see these other cases.
146 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
147 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
148 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
151 dp = efi_dp_next(dp);
157 static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
158 struct efi_device_path **rem)
160 struct efi_object *efiobj;
161 efi_uintn_t dp_size = efi_dp_instance_size(dp);
163 list_for_each_entry(efiobj, &efi_obj_list, link) {
164 struct efi_handler *handler;
165 struct efi_device_path *obj_dp;
168 ret = efi_search_protocol(efiobj,
169 &efi_guid_device_path, &handler);
170 if (ret != EFI_SUCCESS)
172 obj_dp = handler->protocol_interface;
175 if (efi_dp_match(dp, obj_dp) == 0) {
178 * Allow partial matches, but inform
181 *rem = ((void *)dp) +
182 efi_dp_instance_size(obj_dp);
185 /* Only return on exact matches */
186 if (efi_dp_instance_size(obj_dp) ==
192 obj_dp = shorten_path(efi_dp_next(obj_dp));
193 } while (short_path && obj_dp);
200 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
201 * remaining part of the device path after the matched object.
203 struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
204 struct efi_device_path **rem)
206 struct efi_object *efiobj;
208 /* Search for an exact match first */
209 efiobj = find_obj(dp, false, NULL);
211 /* Then for a fuzzy match */
213 efiobj = find_obj(dp, false, rem);
215 /* And now for a fuzzy short match */
217 efiobj = find_obj(dp, true, rem);
223 * Determine the last device path node that is not the end node.
226 * Return: last node before the end node if it exists
229 const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
231 struct efi_device_path *ret;
233 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
236 ret = (struct efi_device_path *)dp;
237 dp = efi_dp_next(dp);
242 /* get size of the first device path instance excluding end node */
243 efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
247 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
251 dp = efi_dp_next(dp);
257 /* get size of multi-instance device path excluding end node */
258 efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
260 const struct efi_device_path *p = dp;
264 while (p->type != DEVICE_PATH_TYPE_END ||
265 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
266 p = (void *)p + p->length;
268 return (void *)p - (void *)dp;
271 /* copy multi-instance device path */
272 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
274 struct efi_device_path *ndp;
275 size_t sz = efi_dp_size(dp) + sizeof(END);
289 * efi_dp_append_or_concatenate() - Append or concatenate two device paths.
290 * Concatenated device path will be separated
291 * by a sub-type 0xff end node
293 * @dp1: First device path
294 * @dp2: Second device path
295 * @concat: If true the two device paths will be concatenated and separated
296 * by an end of entrire device path sub-type 0xff end node.
297 * If true the second device path will be appended to the first and
298 * terminated by an end node
301 * concatenated device path or NULL. Caller must free the returned value
304 efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
305 const struct efi_device_path *dp2,
308 struct efi_device_path *ret;
309 size_t end_size = sizeof(END);
312 end_size = 2 * sizeof(END);
314 /* return an end node */
315 ret = efi_dp_dup(&END);
317 ret = efi_dp_dup(dp2);
319 ret = efi_dp_dup(dp1);
321 /* both dp1 and dp2 are non-null */
322 unsigned sz1 = efi_dp_size(dp1);
323 unsigned sz2 = efi_dp_size(dp2);
324 void *p = dp_alloc(sz1 + sz2 + end_size);
332 memcpy(p, &END, sizeof(END));
336 /* the end node of the second device path has to be retained */
339 memcpy(p, &END, sizeof(END));
346 * efi_dp_append() - Append a device to an existing device path.
348 * @dp1: First device path
349 * @dp2: Second device path
352 * concatenated device path or NULL. Caller must free the returned value
354 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
355 const struct efi_device_path *dp2)
357 return efi_dp_append_or_concatenate(dp1, dp2, false);
361 * efi_dp_concat() - Concatenate 2 device paths. The final device path will
362 * contain two device paths separated by and end node (0xff).
364 * @dp1: First device path
365 * @dp2: Second device path
368 * concatenated device path or NULL. Caller must free the returned value
370 struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
371 const struct efi_device_path *dp2)
373 return efi_dp_append_or_concatenate(dp1, dp2, true);
376 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
377 const struct efi_device_path *node)
379 struct efi_device_path *ret;
382 ret = efi_dp_dup(&END);
384 ret = efi_dp_dup(dp);
386 size_t sz = node->length;
387 void *p = dp_alloc(sz + sizeof(END));
391 memcpy(p + sz, &END, sizeof(END));
394 /* both dp and node are non-null */
395 size_t sz = efi_dp_size(dp);
396 void *p = dp_alloc(sz + node->length + sizeof(END));
400 memcpy(p + sz, node, node->length);
401 memcpy(p + sz + node->length, &END, sizeof(END));
408 struct efi_device_path *efi_dp_create_device_node(const u8 type,
412 struct efi_device_path *ret;
414 if (length < sizeof(struct efi_device_path))
417 ret = dp_alloc(length);
421 ret->sub_type = sub_type;
422 ret->length = length;
426 struct efi_device_path *efi_dp_append_instance(
427 const struct efi_device_path *dp,
428 const struct efi_device_path *dpi)
431 struct efi_device_path *p, *ret;
436 return efi_dp_dup(dpi);
437 sz = efi_dp_size(dp);
438 szi = efi_dp_instance_size(dpi);
439 p = dp_alloc(sz + szi + 2 * sizeof(END));
443 memcpy(p, dp, sz + sizeof(END));
445 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
446 p = (void *)p + sizeof(END);
449 memcpy(p, &END, sizeof(END));
453 struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
457 struct efi_device_path *p;
463 sz = efi_dp_instance_size(*dp);
464 p = dp_alloc(sz + sizeof(END));
467 memcpy(p, *dp, sz + sizeof(END));
468 *dp = (void *)*dp + sz;
469 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
470 *dp = (void *)*dp + sizeof(END);
474 *size = sz + sizeof(END);
478 bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
480 const struct efi_device_path *p = dp;
484 while (p->type != DEVICE_PATH_TYPE_END)
485 p = (void *)p + p->length;
486 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
489 /* size of device-path not including END node for device and all parents
490 * up to the root device.
492 __maybe_unused static unsigned int dp_size(struct udevice *dev)
494 if (!dev || !dev->driver)
497 switch (device_get_uclass_id(dev)) {
499 case UCLASS_SIMPLE_BUS:
500 /* stop traversing parents at this point: */
503 return dp_size(dev->parent) +
504 sizeof(struct efi_device_path_mac_addr);
506 switch (dev->parent->uclass->uc_drv->id) {
509 return dp_size(dev->parent) +
510 sizeof(struct efi_device_path_atapi);
512 #if defined(CONFIG_SCSI)
514 return dp_size(dev->parent) +
515 sizeof(struct efi_device_path_scsi);
517 #if defined(CONFIG_MMC)
519 return dp_size(dev->parent) +
520 sizeof(struct efi_device_path_sd_mmc_path);
522 #if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
524 return dp_size(dev->parent) +
525 sizeof(struct efi_device_path_sata);
527 #if defined(CONFIG_NVME)
529 return dp_size(dev->parent) +
530 sizeof(struct efi_device_path_nvme);
532 #ifdef CONFIG_SANDBOX
535 * Sandbox's host device will be represented
536 * as vendor device with extra one byte for
539 return dp_size(dev->parent)
540 + sizeof(struct efi_device_path_vendor) + 1;
542 #ifdef CONFIG_VIRTIO_BLK
545 * Virtio devices will be represented as a vendor
546 * device node with an extra byte for the device
549 return dp_size(dev->parent)
550 + sizeof(struct efi_device_path_vendor) + 1;
553 return dp_size(dev->parent);
555 #if defined(CONFIG_MMC)
557 return dp_size(dev->parent) +
558 sizeof(struct efi_device_path_sd_mmc_path);
560 case UCLASS_MASS_STORAGE:
562 return dp_size(dev->parent) +
563 sizeof(struct efi_device_path_usb_class);
565 /* just skip over unknown classes: */
566 return dp_size(dev->parent);
571 * Recursively build a device path.
573 * @buf pointer to the end of the device path
575 * Return: pointer to the end of the device path
577 __maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
579 if (!dev || !dev->driver)
582 switch (device_get_uclass_id(dev)) {
584 case UCLASS_SIMPLE_BUS: {
585 /* stop traversing parents at this point: */
586 struct efi_device_path_vendor *vdp = buf;
592 struct efi_device_path_mac_addr *dp =
593 dp_fill(buf, dev->parent);
594 struct eth_pdata *pdata = dev_get_plat(dev);
596 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
597 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
598 dp->dp.length = sizeof(*dp);
599 memset(&dp->mac, 0, sizeof(dp->mac));
600 /* We only support IPv4 */
601 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
608 switch (dev->parent->uclass->uc_drv->id) {
609 #ifdef CONFIG_SANDBOX
611 /* stop traversing parents at this point: */
612 struct efi_device_path_vendor *dp;
613 struct blk_desc *desc = dev_get_uclass_plat(dev);
615 dp_fill(buf, dev->parent);
618 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
619 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
620 dp->dp.length = sizeof(*dp) + 1;
621 memcpy(&dp->guid, &efi_guid_host_dev,
623 dp->vendor_data[0] = desc->devnum;
624 return &dp->vendor_data[1];
627 #ifdef CONFIG_VIRTIO_BLK
628 case UCLASS_VIRTIO: {
629 struct efi_device_path_vendor *dp;
630 struct blk_desc *desc = dev_get_uclass_plat(dev);
632 dp_fill(buf, dev->parent);
635 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
636 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
637 dp->dp.length = sizeof(*dp) + 1;
638 memcpy(&dp->guid, &efi_guid_virtio_dev,
640 dp->vendor_data[0] = desc->devnum;
641 return &dp->vendor_data[1];
646 struct efi_device_path_atapi *dp =
647 dp_fill(buf, dev->parent);
648 struct blk_desc *desc = dev_get_uclass_plat(dev);
650 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
651 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
652 dp->dp.length = sizeof(*dp);
653 dp->logical_unit_number = desc->devnum;
654 dp->primary_secondary = IDE_BUS(desc->devnum);
655 dp->slave_master = desc->devnum %
656 (CONFIG_SYS_IDE_MAXDEVICE /
657 CONFIG_SYS_IDE_MAXBUS);
661 #if defined(CONFIG_SCSI)
663 struct efi_device_path_scsi *dp =
664 dp_fill(buf, dev->parent);
665 struct blk_desc *desc = dev_get_uclass_plat(dev);
667 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
668 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
669 dp->dp.length = sizeof(*dp);
670 dp->logical_unit_number = desc->lun;
671 dp->target_id = desc->target;
675 #if defined(CONFIG_MMC)
677 struct efi_device_path_sd_mmc_path *sddp =
678 dp_fill(buf, dev->parent);
679 struct blk_desc *desc = dev_get_uclass_plat(dev);
681 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
682 sddp->dp.sub_type = is_sd(desc) ?
683 DEVICE_PATH_SUB_TYPE_MSG_SD :
684 DEVICE_PATH_SUB_TYPE_MSG_MMC;
685 sddp->dp.length = sizeof(*sddp);
686 sddp->slot_number = dev_seq(dev);
690 #if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
692 struct efi_device_path_sata *dp =
693 dp_fill(buf, dev->parent);
694 struct blk_desc *desc = dev_get_uclass_plat(dev);
696 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
697 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
698 dp->dp.length = sizeof(*dp);
699 dp->hba_port = desc->devnum;
700 /* default 0xffff implies no port multiplier */
701 dp->port_multiplier_port = 0xffff;
702 dp->logical_unit_number = desc->lun;
706 #if defined(CONFIG_NVME)
708 struct efi_device_path_nvme *dp =
709 dp_fill(buf, dev->parent);
712 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
713 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
714 dp->dp.length = sizeof(*dp);
715 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
716 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
721 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
722 __FILE__, __LINE__, __func__,
723 dev->name, dev->parent->uclass->uc_drv->id);
724 return dp_fill(buf, dev->parent);
726 #if defined(CONFIG_MMC)
728 struct efi_device_path_sd_mmc_path *sddp =
729 dp_fill(buf, dev->parent);
730 struct mmc *mmc = mmc_get_mmc_dev(dev);
731 struct blk_desc *desc = mmc_get_blk_desc(mmc);
733 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
734 sddp->dp.sub_type = is_sd(desc) ?
735 DEVICE_PATH_SUB_TYPE_MSG_SD :
736 DEVICE_PATH_SUB_TYPE_MSG_MMC;
737 sddp->dp.length = sizeof(*sddp);
738 sddp->slot_number = dev_seq(dev);
743 case UCLASS_MASS_STORAGE:
744 case UCLASS_USB_HUB: {
745 struct efi_device_path_usb_class *udp =
746 dp_fill(buf, dev->parent);
747 struct usb_device *udev = dev_get_parent_priv(dev);
748 struct usb_device_descriptor *desc = &udev->descriptor;
750 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
751 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
752 udp->dp.length = sizeof(*udp);
753 udp->vendor_id = desc->idVendor;
754 udp->product_id = desc->idProduct;
755 udp->device_class = desc->bDeviceClass;
756 udp->device_subclass = desc->bDeviceSubClass;
757 udp->device_protocol = desc->bDeviceProtocol;
762 /* If the uclass driver is missing, this will show NULL */
763 log_debug("unhandled device class: %s (%s)\n", dev->name,
764 dev_get_uclass_name(dev));
765 return dp_fill(buf, dev->parent);
769 static unsigned dp_part_size(struct blk_desc *desc, int part)
772 struct udevice *dev = desc->bdev;
774 dpsize = dp_size(dev);
776 if (part == 0) /* the actual disk, not a partition */
779 if (desc->part_type == PART_TYPE_ISO)
780 dpsize += sizeof(struct efi_device_path_cdrom_path);
782 dpsize += sizeof(struct efi_device_path_hard_drive_path);
788 * Create a device node for a block device partition.
790 * @buf buffer to which the device path is written
791 * @desc block device descriptor
792 * @part partition number, 0 identifies a block device
794 static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
796 struct disk_partition info;
798 part_get_info(desc, part, &info);
800 if (desc->part_type == PART_TYPE_ISO) {
801 struct efi_device_path_cdrom_path *cddp = buf;
803 cddp->boot_entry = part;
804 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
805 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
806 cddp->dp.length = sizeof(*cddp);
807 cddp->partition_start = info.start;
808 cddp->partition_size = info.size;
812 struct efi_device_path_hard_drive_path *hddp = buf;
814 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
815 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
816 hddp->dp.length = sizeof(*hddp);
817 hddp->partition_number = part;
818 hddp->partition_start = info.start;
819 hddp->partition_end = info.size;
820 if (desc->part_type == PART_TYPE_EFI)
821 hddp->partmap_type = 2;
823 hddp->partmap_type = 1;
825 switch (desc->sig_type) {
828 hddp->signature_type = 0;
829 memset(hddp->partition_signature, 0,
830 sizeof(hddp->partition_signature));
833 hddp->signature_type = 1;
834 memset(hddp->partition_signature, 0,
835 sizeof(hddp->partition_signature));
836 memcpy(hddp->partition_signature, &desc->mbr_sig,
837 sizeof(desc->mbr_sig));
840 hddp->signature_type = 2;
841 if (uuid_str_to_bin(info.uuid,
842 hddp->partition_signature, 1))
844 "Partition no. %d: invalid guid: %s\n",
856 * Create a device path for a block device or one of its partitions.
858 * @buf buffer to which the device path is written
859 * @desc block device descriptor
860 * @part partition number, 0 identifies a block device
862 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
864 struct udevice *dev = desc->bdev;
866 buf = dp_fill(buf, dev);
868 if (part == 0) /* the actual disk, not a partition */
871 return dp_part_node(buf, desc, part);
874 /* Construct a device-path from a partition on a block device: */
875 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
879 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
883 buf = dp_part_fill(buf, desc, part);
885 *((struct efi_device_path *)buf) = END;
891 * Create a device node for a block device partition.
893 * @buf buffer to which the device path is written
894 * @desc block device descriptor
895 * @part partition number, 0 identifies a block device
897 struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
902 if (desc->part_type == PART_TYPE_ISO)
903 dpsize = sizeof(struct efi_device_path_cdrom_path);
905 dpsize = sizeof(struct efi_device_path_hard_drive_path);
906 buf = dp_alloc(dpsize);
908 dp_part_node(buf, desc, part);
914 * path_to_uefi() - convert UTF-8 path to an UEFI style path
916 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
917 * separators and UTF-16).
919 * @src: source buffer
920 * @uefi: target buffer, possibly unaligned
922 static void path_to_uefi(void *uefi, const char *src)
927 * efi_set_bootdev() calls this routine indirectly before the UEFI
928 * subsystem is initialized. So we cannot assume unaligned access to be
934 s32 code = utf8_get(&src);
938 else if (code == '/')
940 utf16_put(code, &pos);
946 * If desc is NULL, this creates a path with only the file component,
947 * otherwise it creates a full path with both device and file components
949 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
952 struct efi_device_path_file_path *fp;
954 size_t dpsize = 0, fpsize;
957 dpsize = dp_part_size(desc, part);
959 fpsize = sizeof(struct efi_device_path) +
960 2 * (utf8_utf16_strlen(path) + 1);
961 if (fpsize > U16_MAX)
966 start = buf = dp_alloc(dpsize + sizeof(END));
971 buf = dp_part_fill(buf, desc, part);
975 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
976 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
977 fp->dp.length = (u16)fpsize;
978 path_to_uefi(fp->str, path);
981 *((struct efi_device_path *)buf) = END;
986 struct efi_device_path *efi_dp_from_uart(void)
989 struct efi_device_path_uart *uart;
990 size_t dpsize = sizeof(ROOT) + sizeof(*uart) + sizeof(END);
992 buf = dp_alloc(dpsize);
996 memcpy(pos, &ROOT, sizeof(ROOT));
999 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1000 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
1001 uart->dp.length = sizeof(*uart);
1002 pos += sizeof(*uart);
1003 memcpy(pos, &END, sizeof(END));
1009 struct efi_device_path *efi_dp_from_eth(void)
1012 unsigned dpsize = 0;
1014 assert(eth_get_dev());
1016 dpsize += dp_size(eth_get_dev());
1018 start = buf = dp_alloc(dpsize + sizeof(END));
1022 buf = dp_fill(buf, eth_get_dev());
1024 *((struct efi_device_path *)buf) = END;
1030 /* Construct a device-path for memory-mapped image */
1031 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1032 uint64_t start_address,
1033 uint64_t end_address)
1035 struct efi_device_path_memory *mdp;
1038 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
1043 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1044 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1045 mdp->dp.length = sizeof(*mdp);
1046 mdp->memory_type = memory_type;
1047 mdp->start_address = start_address;
1048 mdp->end_address = end_address;
1051 *((struct efi_device_path *)buf) = END;
1057 * efi_dp_split_file_path() - split of relative file path from device path
1059 * Given a device path indicating a file on a device, separate the device
1060 * path in two: the device path of the actual device and the file path
1061 * relative to this device.
1063 * @full_path: device path including device and file path
1064 * @device_path: path of the device
1065 * @file_path: relative path of the file or NULL if there is none
1066 * Return: status code
1068 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1069 struct efi_device_path **device_path,
1070 struct efi_device_path **file_path)
1072 struct efi_device_path *p, *dp, *fp = NULL;
1074 *device_path = NULL;
1076 dp = efi_dp_dup(full_path);
1078 return EFI_OUT_OF_RESOURCES;
1080 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
1087 return EFI_OUT_OF_RESOURCES;
1088 p->type = DEVICE_PATH_TYPE_END;
1089 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1090 p->length = sizeof(*p);
1099 * efi_dp_from_name() - convert U-Boot device and file path to device path
1101 * @dev: U-Boot device, e.g. 'mmc'
1102 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1103 * @path: file path relative to U-Boot device, may be NULL
1104 * @device: pointer to receive device path of the device
1105 * @file: pointer to receive device path for the file
1106 * Return: status code
1108 efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1110 struct efi_device_path **device,
1111 struct efi_device_path **file)
1113 struct blk_desc *desc = NULL;
1114 struct disk_partition fs_partition;
1120 return EFI_INVALID_PARAMETER;
1122 if (!strcmp(dev, "Net")) {
1125 *device = efi_dp_from_eth();
1127 } else if (!strcmp(dev, "Uart")) {
1129 *device = efi_dp_from_uart();
1131 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1133 if (part < 0 || !desc)
1134 return EFI_INVALID_PARAMETER;
1137 *device = efi_dp_from_part(desc, part);
1143 filename = calloc(1, strlen(path) + 1);
1145 return EFI_OUT_OF_RESOURCES;
1147 sprintf(filename, "%s", path);
1148 /* DOS style file path: */
1150 while ((s = strchr(s, '/')))
1152 *file = efi_dp_from_file(desc, part, filename);
1156 return EFI_INVALID_PARAMETER;
1162 * efi_dp_check_length() - check length of a device path
1164 * @dp: pointer to device path
1165 * @maxlen: maximum length of the device path
1167 * * length of the device path if it is less or equal @maxlen
1168 * * -1 if the device path is longer then @maxlen
1169 * * -1 if a device path node has a length of less than 4
1170 * * -EINVAL if maxlen exceeds SSIZE_MAX
1172 ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1173 const size_t maxlen)
1178 if (maxlen > SSIZE_MAX)
1187 if (dp->type == DEVICE_PATH_TYPE_END &&
1188 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1190 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1195 * efi_dp_from_lo() - Get the instance of a VenMedia node in a
1196 * multi-instance device path that matches
1197 * a specific GUID. This kind of device paths
1198 * is found in Boot#### options describing an
1201 * @lo: EFI_LOAD_OPTION containing a valid device path
1202 * @guid: guid to search for
1205 * device path including the VenMedia node or NULL.
1206 * Caller must free the returned value.
1209 efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
1210 const efi_guid_t *guid)
1212 struct efi_device_path *fp = lo->file_path;
1213 struct efi_device_path_vendor *vendor;
1214 int lo_len = lo->file_path_length;
1216 for (; lo_len >= sizeof(struct efi_device_path);
1217 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1218 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1220 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1221 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1224 vendor = (struct efi_device_path_vendor *)fp;
1225 if (!guidcmp(&vendor->guid, guid))
1226 return efi_dp_dup(efi_dp_next(fp));
1228 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1234 * search_gpt_dp_node() - search gpt device path node
1236 * @device_path: device path
1238 * Return: pointer to the gpt device path node
1240 struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1242 struct efi_device_path *dp = device_path;
1245 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1246 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1247 struct efi_device_path_hard_drive_path *hd_dp =
1248 (struct efi_device_path_hard_drive_path *)dp;
1250 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1251 hd_dp->signature_type == SIG_TYPE_GUID)
1254 dp = efi_dp_next(dp);