1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI device path from u-boot device-model mapping
5 * (C) Copyright 2017 Rob Clark
14 #include <efi_loader.h>
16 #include <sandboxblockdev.h>
17 #include <asm-generic/unaligned.h>
18 #include <linux/compat.h> /* U16_MAX */
21 const efi_guid_t efi_guid_host_dev
= U_BOOT_HOST_DEV_GUID
;
24 /* template END node: */
25 static const struct efi_device_path END
= {
26 .type
= DEVICE_PATH_TYPE_END
,
27 .sub_type
= DEVICE_PATH_SUB_TYPE_END
,
28 .length
= sizeof(END
),
31 /* template ROOT node: */
32 static const struct efi_device_path_vendor ROOT
= {
34 .type
= DEVICE_PATH_TYPE_HARDWARE_DEVICE
,
35 .sub_type
= DEVICE_PATH_SUB_TYPE_VENDOR
,
36 .length
= sizeof(ROOT
),
41 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
43 * Determine if an MMC device is an SD card.
45 * @desc block device descriptor
46 * @return true if the device is an SD card
48 static bool is_sd(struct blk_desc
*desc
)
50 struct mmc
*mmc
= find_mmc_device(desc
->devnum
);
55 return IS_SD(mmc
) != 0U;
59 static void *dp_alloc(size_t sz
)
63 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES
, sz
, &buf
) !=
65 debug("EFI: ERROR: out of memory in %s\n", __func__
);
74 * Iterate to next block in device-path, terminating (returning NULL)
77 struct efi_device_path
*efi_dp_next(const struct efi_device_path
*dp
)
81 if (dp
->type
== DEVICE_PATH_TYPE_END
)
83 dp
= ((void *)dp
) + dp
->length
;
84 if (dp
->type
== DEVICE_PATH_TYPE_END
)
86 return (struct efi_device_path
*)dp
;
90 * Compare two device-paths, stopping when the shorter of the two hits
91 * an End* node. This is useful to, for example, compare a device-path
92 * representing a device with one representing a file on the device, or
93 * a device with a parent device.
95 int efi_dp_match(const struct efi_device_path
*a
,
96 const struct efi_device_path
*b
)
101 ret
= memcmp(&a
->length
, &b
->length
, sizeof(a
->length
));
105 ret
= memcmp(a
, b
, a
->length
);
118 * We can have device paths that start with a USB WWID or a USB Class node,
119 * and a few other cases which don't encode the full device path with bus
122 * - MESSAGING:USB_WWID
123 * - MESSAGING:USB_CLASS
128 * See UEFI spec (section 3.1.2, about short-form device-paths)
130 static struct efi_device_path
*shorten_path(struct efi_device_path
*dp
)
134 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
135 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
136 * so not sure when we would see these other cases.
138 if (EFI_DP_TYPE(dp
, MESSAGING_DEVICE
, MSG_USB_CLASS
) ||
139 EFI_DP_TYPE(dp
, MEDIA_DEVICE
, HARD_DRIVE_PATH
) ||
140 EFI_DP_TYPE(dp
, MEDIA_DEVICE
, FILE_PATH
))
143 dp
= efi_dp_next(dp
);
149 static struct efi_object
*find_obj(struct efi_device_path
*dp
, bool short_path
,
150 struct efi_device_path
**rem
)
152 struct efi_object
*efiobj
;
153 efi_uintn_t dp_size
= efi_dp_instance_size(dp
);
155 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
156 struct efi_handler
*handler
;
157 struct efi_device_path
*obj_dp
;
160 ret
= efi_search_protocol(efiobj
,
161 &efi_guid_device_path
, &handler
);
162 if (ret
!= EFI_SUCCESS
)
164 obj_dp
= handler
->protocol_interface
;
167 if (efi_dp_match(dp
, obj_dp
) == 0) {
170 * Allow partial matches, but inform
173 *rem
= ((void *)dp
) +
174 efi_dp_instance_size(obj_dp
);
177 /* Only return on exact matches */
178 if (efi_dp_instance_size(obj_dp
) ==
184 obj_dp
= shorten_path(efi_dp_next(obj_dp
));
185 } while (short_path
&& obj_dp
);
192 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
193 * remaining part of the device path after the matched object.
195 struct efi_object
*efi_dp_find_obj(struct efi_device_path
*dp
,
196 struct efi_device_path
**rem
)
198 struct efi_object
*efiobj
;
200 /* Search for an exact match first */
201 efiobj
= find_obj(dp
, false, NULL
);
203 /* Then for a fuzzy match */
205 efiobj
= find_obj(dp
, false, rem
);
207 /* And now for a fuzzy short match */
209 efiobj
= find_obj(dp
, true, rem
);
215 * Determine the last device path node that is not the end node.
218 * @return last node before the end node if it exists
221 const struct efi_device_path
*efi_dp_last_node(const struct efi_device_path
*dp
)
223 struct efi_device_path
*ret
;
225 if (!dp
|| dp
->type
== DEVICE_PATH_TYPE_END
)
228 ret
= (struct efi_device_path
*)dp
;
229 dp
= efi_dp_next(dp
);
234 /* get size of the first device path instance excluding end node */
235 efi_uintn_t
efi_dp_instance_size(const struct efi_device_path
*dp
)
239 if (!dp
|| dp
->type
== DEVICE_PATH_TYPE_END
)
243 dp
= efi_dp_next(dp
);
249 /* get size of multi-instance device path excluding end node */
250 efi_uintn_t
efi_dp_size(const struct efi_device_path
*dp
)
252 const struct efi_device_path
*p
= dp
;
256 while (p
->type
!= DEVICE_PATH_TYPE_END
||
257 p
->sub_type
!= DEVICE_PATH_SUB_TYPE_END
)
258 p
= (void *)p
+ p
->length
;
260 return (void *)p
- (void *)dp
;
263 /* copy multi-instance device path */
264 struct efi_device_path
*efi_dp_dup(const struct efi_device_path
*dp
)
266 struct efi_device_path
*ndp
;
267 size_t sz
= efi_dp_size(dp
) + sizeof(END
);
280 struct efi_device_path
*efi_dp_append(const struct efi_device_path
*dp1
,
281 const struct efi_device_path
*dp2
)
283 struct efi_device_path
*ret
;
286 /* return an end node */
287 ret
= efi_dp_dup(&END
);
289 ret
= efi_dp_dup(dp2
);
291 ret
= efi_dp_dup(dp1
);
293 /* both dp1 and dp2 are non-null */
294 unsigned sz1
= efi_dp_size(dp1
);
295 unsigned sz2
= efi_dp_size(dp2
);
296 void *p
= dp_alloc(sz1
+ sz2
+ sizeof(END
));
300 /* the end node of the second device path has to be retained */
301 memcpy(p
+ sz1
, dp2
, sz2
+ sizeof(END
));
308 struct efi_device_path
*efi_dp_append_node(const struct efi_device_path
*dp
,
309 const struct efi_device_path
*node
)
311 struct efi_device_path
*ret
;
314 ret
= efi_dp_dup(&END
);
316 ret
= efi_dp_dup(dp
);
318 size_t sz
= node
->length
;
319 void *p
= dp_alloc(sz
+ sizeof(END
));
323 memcpy(p
+ sz
, &END
, sizeof(END
));
326 /* both dp and node are non-null */
327 size_t sz
= efi_dp_size(dp
);
328 void *p
= dp_alloc(sz
+ node
->length
+ sizeof(END
));
332 memcpy(p
+ sz
, node
, node
->length
);
333 memcpy(p
+ sz
+ node
->length
, &END
, sizeof(END
));
340 struct efi_device_path
*efi_dp_create_device_node(const u8 type
,
344 struct efi_device_path
*ret
;
346 if (length
< sizeof(struct efi_device_path
))
349 ret
= dp_alloc(length
);
353 ret
->sub_type
= sub_type
;
354 ret
->length
= length
;
358 struct efi_device_path
*efi_dp_append_instance(
359 const struct efi_device_path
*dp
,
360 const struct efi_device_path
*dpi
)
363 struct efi_device_path
*p
, *ret
;
368 return efi_dp_dup(dpi
);
369 sz
= efi_dp_size(dp
);
370 szi
= efi_dp_instance_size(dpi
);
371 p
= dp_alloc(sz
+ szi
+ 2 * sizeof(END
));
375 memcpy(p
, dp
, sz
+ sizeof(END
));
377 p
->sub_type
= DEVICE_PATH_SUB_TYPE_INSTANCE_END
;
378 p
= (void *)p
+ sizeof(END
);
381 memcpy(p
, &END
, sizeof(END
));
385 struct efi_device_path
*efi_dp_get_next_instance(struct efi_device_path
**dp
,
389 struct efi_device_path
*p
;
395 sz
= efi_dp_instance_size(*dp
);
396 p
= dp_alloc(sz
+ sizeof(END
));
399 memcpy(p
, *dp
, sz
+ sizeof(END
));
400 *dp
= (void *)*dp
+ sz
;
401 if ((*dp
)->sub_type
== DEVICE_PATH_SUB_TYPE_INSTANCE_END
)
402 *dp
= (void *)*dp
+ sizeof(END
);
406 *size
= sz
+ sizeof(END
);
410 bool efi_dp_is_multi_instance(const struct efi_device_path
*dp
)
412 const struct efi_device_path
*p
= dp
;
416 while (p
->type
!= DEVICE_PATH_TYPE_END
)
417 p
= (void *)p
+ p
->length
;
418 return p
->sub_type
== DEVICE_PATH_SUB_TYPE_INSTANCE_END
;
422 /* size of device-path not including END node for device and all parents
423 * up to the root device.
425 static unsigned dp_size(struct udevice
*dev
)
427 if (!dev
|| !dev
->driver
)
430 switch (dev
->driver
->id
) {
432 case UCLASS_SIMPLE_BUS
:
433 /* stop traversing parents at this point: */
436 return dp_size(dev
->parent
) +
437 sizeof(struct efi_device_path_mac_addr
);
440 switch (dev
->parent
->uclass
->uc_drv
->id
) {
443 return dp_size(dev
->parent
) +
444 sizeof(struct efi_device_path_atapi
);
446 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
448 return dp_size(dev
->parent
) +
449 sizeof(struct efi_device_path_scsi
);
451 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
453 return dp_size(dev
->parent
) +
454 sizeof(struct efi_device_path_sd_mmc_path
);
456 #if defined(CONFIG_NVME)
458 return dp_size(dev
->parent
) +
459 sizeof(struct efi_device_path_nvme
);
461 #ifdef CONFIG_SANDBOX
464 * Sandbox's host device will be represented
465 * as vendor device with extra one byte for
468 return dp_size(dev
->parent
)
469 + sizeof(struct efi_device_path_vendor
) + 1;
472 return dp_size(dev
->parent
);
475 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
477 return dp_size(dev
->parent
) +
478 sizeof(struct efi_device_path_sd_mmc_path
);
480 case UCLASS_MASS_STORAGE
:
482 return dp_size(dev
->parent
) +
483 sizeof(struct efi_device_path_usb_class
);
485 /* just skip over unknown classes: */
486 return dp_size(dev
->parent
);
491 * Recursively build a device path.
493 * @buf pointer to the end of the device path
495 * @return pointer to the end of the device path
497 static void *dp_fill(void *buf
, struct udevice
*dev
)
499 if (!dev
|| !dev
->driver
)
502 switch (dev
->driver
->id
) {
504 case UCLASS_SIMPLE_BUS
: {
505 /* stop traversing parents at this point: */
506 struct efi_device_path_vendor
*vdp
= buf
;
512 struct efi_device_path_mac_addr
*dp
=
513 dp_fill(buf
, dev
->parent
);
514 struct eth_pdata
*pdata
= dev
->platdata
;
516 dp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
517 dp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR
;
518 dp
->dp
.length
= sizeof(*dp
);
519 memset(&dp
->mac
, 0, sizeof(dp
->mac
));
520 /* We only support IPv4 */
521 memcpy(&dp
->mac
, &pdata
->enetaddr
, ARP_HLEN
);
529 switch (dev
->parent
->uclass
->uc_drv
->id
) {
530 #ifdef CONFIG_SANDBOX
532 /* stop traversing parents at this point: */
533 struct efi_device_path_vendor
*dp
= buf
;
534 struct blk_desc
*desc
= dev_get_uclass_platdata(dev
);
536 dp_fill(buf
, dev
->parent
);
539 dp
->dp
.type
= DEVICE_PATH_TYPE_HARDWARE_DEVICE
;
540 dp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_VENDOR
;
541 dp
->dp
.length
= sizeof(*dp
) + 1;
542 memcpy(&dp
->guid
, &efi_guid_host_dev
,
544 dp
->vendor_data
[0] = desc
->devnum
;
545 return &dp
->vendor_data
[1];
550 struct efi_device_path_atapi
*dp
=
551 dp_fill(buf
, dev
->parent
);
552 struct blk_desc
*desc
= dev_get_uclass_platdata(dev
);
554 dp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
555 dp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MSG_ATAPI
;
556 dp
->dp
.length
= sizeof(*dp
);
557 dp
->logical_unit_number
= desc
->devnum
;
558 dp
->primary_secondary
= IDE_BUS(desc
->devnum
);
559 dp
->slave_master
= desc
->devnum
%
560 (CONFIG_SYS_IDE_MAXDEVICE
/
561 CONFIG_SYS_IDE_MAXBUS
);
565 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
567 struct efi_device_path_scsi
*dp
=
568 dp_fill(buf
, dev
->parent
);
569 struct blk_desc
*desc
= dev_get_uclass_platdata(dev
);
571 dp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
572 dp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MSG_SCSI
;
573 dp
->dp
.length
= sizeof(*dp
);
574 dp
->logical_unit_number
= desc
->lun
;
575 dp
->target_id
= desc
->target
;
579 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
581 struct efi_device_path_sd_mmc_path
*sddp
=
582 dp_fill(buf
, dev
->parent
);
583 struct blk_desc
*desc
= dev_get_uclass_platdata(dev
);
585 sddp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
586 sddp
->dp
.sub_type
= is_sd(desc
) ?
587 DEVICE_PATH_SUB_TYPE_MSG_SD
:
588 DEVICE_PATH_SUB_TYPE_MSG_MMC
;
589 sddp
->dp
.length
= sizeof(*sddp
);
590 sddp
->slot_number
= dev
->seq
;
594 #if defined(CONFIG_NVME)
596 struct efi_device_path_nvme
*dp
=
597 dp_fill(buf
, dev
->parent
);
600 dp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
601 dp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MSG_NVME
;
602 dp
->dp
.length
= sizeof(*dp
);
603 nvme_get_namespace_id(dev
, &ns_id
, dp
->eui64
);
604 memcpy(&dp
->ns_id
, &ns_id
, sizeof(ns_id
));
609 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
610 __FILE__
, __LINE__
, __func__
,
611 dev
->name
, dev
->parent
->uclass
->uc_drv
->id
);
612 return dp_fill(buf
, dev
->parent
);
615 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
617 struct efi_device_path_sd_mmc_path
*sddp
=
618 dp_fill(buf
, dev
->parent
);
619 struct mmc
*mmc
= mmc_get_mmc_dev(dev
);
620 struct blk_desc
*desc
= mmc_get_blk_desc(mmc
);
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
;
632 case UCLASS_MASS_STORAGE
:
633 case UCLASS_USB_HUB
: {
634 struct efi_device_path_usb_class
*udp
=
635 dp_fill(buf
, dev
->parent
);
636 struct usb_device
*udev
= dev_get_parent_priv(dev
);
637 struct usb_device_descriptor
*desc
= &udev
->descriptor
;
639 udp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
640 udp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS
;
641 udp
->dp
.length
= sizeof(*udp
);
642 udp
->vendor_id
= desc
->idVendor
;
643 udp
->product_id
= desc
->idProduct
;
644 udp
->device_class
= desc
->bDeviceClass
;
645 udp
->device_subclass
= desc
->bDeviceSubClass
;
646 udp
->device_protocol
= desc
->bDeviceProtocol
;
651 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
652 __FILE__
, __LINE__
, __func__
,
653 dev
->name
, dev
->driver
->id
);
654 return dp_fill(buf
, dev
->parent
);
658 /* Construct a device-path from a device: */
659 struct efi_device_path
*efi_dp_from_dev(struct udevice
*dev
)
663 start
= buf
= dp_alloc(dp_size(dev
) + sizeof(END
));
666 buf
= dp_fill(buf
, dev
);
667 *((struct efi_device_path
*)buf
) = END
;
673 static unsigned dp_part_size(struct blk_desc
*desc
, int part
)
680 int ret
= blk_find_device(desc
->if_type
, desc
->devnum
, &dev
);
683 dev
= desc
->bdev
->parent
;
684 dpsize
= dp_size(dev
);
687 dpsize
= sizeof(ROOT
) + sizeof(struct efi_device_path_usb
);
690 if (part
== 0) /* the actual disk, not a partition */
693 if (desc
->part_type
== PART_TYPE_ISO
)
694 dpsize
+= sizeof(struct efi_device_path_cdrom_path
);
696 dpsize
+= sizeof(struct efi_device_path_hard_drive_path
);
702 * Create a device node for a block device partition.
704 * @buf buffer to which the device path is written
705 * @desc block device descriptor
706 * @part partition number, 0 identifies a block device
708 static void *dp_part_node(void *buf
, struct blk_desc
*desc
, int part
)
710 disk_partition_t info
;
712 part_get_info(desc
, part
, &info
);
714 if (desc
->part_type
== PART_TYPE_ISO
) {
715 struct efi_device_path_cdrom_path
*cddp
= buf
;
717 cddp
->boot_entry
= part
;
718 cddp
->dp
.type
= DEVICE_PATH_TYPE_MEDIA_DEVICE
;
719 cddp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_CDROM_PATH
;
720 cddp
->dp
.length
= sizeof(*cddp
);
721 cddp
->partition_start
= info
.start
;
722 cddp
->partition_size
= info
.size
;
726 struct efi_device_path_hard_drive_path
*hddp
= buf
;
728 hddp
->dp
.type
= DEVICE_PATH_TYPE_MEDIA_DEVICE
;
729 hddp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH
;
730 hddp
->dp
.length
= sizeof(*hddp
);
731 hddp
->partition_number
= part
;
732 hddp
->partition_start
= info
.start
;
733 hddp
->partition_end
= info
.size
;
734 if (desc
->part_type
== PART_TYPE_EFI
)
735 hddp
->partmap_type
= 2;
737 hddp
->partmap_type
= 1;
739 switch (desc
->sig_type
) {
742 hddp
->signature_type
= 0;
743 memset(hddp
->partition_signature
, 0,
744 sizeof(hddp
->partition_signature
));
747 hddp
->signature_type
= 1;
748 memset(hddp
->partition_signature
, 0,
749 sizeof(hddp
->partition_signature
));
750 memcpy(hddp
->partition_signature
, &desc
->mbr_sig
,
751 sizeof(desc
->mbr_sig
));
754 hddp
->signature_type
= 2;
755 memcpy(hddp
->partition_signature
, &desc
->guid_sig
,
756 sizeof(hddp
->partition_signature
));
767 * Create a device path for a block device or one of its partitions.
769 * @buf buffer to which the device path is written
770 * @desc block device descriptor
771 * @part partition number, 0 identifies a block device
773 static void *dp_part_fill(void *buf
, struct blk_desc
*desc
, int part
)
778 int ret
= blk_find_device(desc
->if_type
, desc
->devnum
, &dev
);
781 dev
= desc
->bdev
->parent
;
782 buf
= dp_fill(buf
, dev
);
786 * We *could* make a more accurate path, by looking at if_type
787 * and handling all the different cases like we do for non-
788 * legacy (i.e. CONFIG_BLK=y) case. But most important thing
789 * is just to have a unique device-path for if_type+devnum.
790 * So map things to a fictitious USB device.
792 struct efi_device_path_usb
*udp
;
794 memcpy(buf
, &ROOT
, sizeof(ROOT
));
798 udp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
799 udp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MSG_USB
;
800 udp
->dp
.length
= sizeof(*udp
);
801 udp
->parent_port_number
= desc
->if_type
;
802 udp
->usb_interface
= desc
->devnum
;
806 if (part
== 0) /* the actual disk, not a partition */
809 return dp_part_node(buf
, desc
, part
);
812 /* Construct a device-path from a partition on a block device: */
813 struct efi_device_path
*efi_dp_from_part(struct blk_desc
*desc
, int part
)
817 start
= buf
= dp_alloc(dp_part_size(desc
, part
) + sizeof(END
));
821 buf
= dp_part_fill(buf
, desc
, part
);
823 *((struct efi_device_path
*)buf
) = END
;
829 * Create a device node for a block device partition.
831 * @buf buffer to which the device path is written
832 * @desc block device descriptor
833 * @part partition number, 0 identifies a block device
835 struct efi_device_path
*efi_dp_part_node(struct blk_desc
*desc
, int part
)
840 if (desc
->part_type
== PART_TYPE_ISO
)
841 dpsize
= sizeof(struct efi_device_path_cdrom_path
);
843 dpsize
= sizeof(struct efi_device_path_hard_drive_path
);
844 buf
= dp_alloc(dpsize
);
846 dp_part_node(buf
, desc
, part
);
852 * path_to_uefi() - convert UTF-8 path to an UEFI style path
854 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
855 * separators and UTF-16).
857 * @src: source buffer
858 * @uefi: target buffer, possibly unaligned
860 static void path_to_uefi(void *uefi
, const char *src
)
865 * efi_set_bootdev() calls this routine indirectly before the UEFI
866 * subsystem is initialized. So we cannot assume unaligned access to be
872 s32 code
= utf8_get(&src
);
876 else if (code
== '/')
878 utf16_put(code
, &pos
);
884 * If desc is NULL, this creates a path with only the file component,
885 * otherwise it creates a full path with both device and file components
887 struct efi_device_path
*efi_dp_from_file(struct blk_desc
*desc
, int part
,
890 struct efi_device_path_file_path
*fp
;
892 size_t dpsize
= 0, fpsize
;
895 dpsize
= dp_part_size(desc
, part
);
897 fpsize
= sizeof(struct efi_device_path
) +
898 2 * (utf8_utf16_strlen(path
) + 1);
899 if (fpsize
> U16_MAX
)
904 start
= buf
= dp_alloc(dpsize
+ sizeof(END
));
909 buf
= dp_part_fill(buf
, desc
, part
);
913 fp
->dp
.type
= DEVICE_PATH_TYPE_MEDIA_DEVICE
;
914 fp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_FILE_PATH
;
915 fp
->dp
.length
= (u16
)fpsize
;
916 path_to_uefi(fp
->str
, path
);
919 *((struct efi_device_path
*)buf
) = END
;
925 struct efi_device_path
*efi_dp_from_eth(void)
927 #ifndef CONFIG_DM_ETH
928 struct efi_device_path_mac_addr
*ndp
;
933 assert(eth_get_dev());
936 dpsize
+= dp_size(eth_get_dev());
938 dpsize
+= sizeof(ROOT
);
939 dpsize
+= sizeof(*ndp
);
942 start
= buf
= dp_alloc(dpsize
+ sizeof(END
));
947 buf
= dp_fill(buf
, eth_get_dev());
949 memcpy(buf
, &ROOT
, sizeof(ROOT
));
953 ndp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
954 ndp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR
;
955 ndp
->dp
.length
= sizeof(*ndp
);
956 ndp
->if_type
= 1; /* Ethernet */
957 memcpy(ndp
->mac
.addr
, eth_get_ethaddr(), ARP_HLEN
);
961 *((struct efi_device_path
*)buf
) = END
;
967 /* Construct a device-path for memory-mapped image */
968 struct efi_device_path
*efi_dp_from_mem(uint32_t memory_type
,
969 uint64_t start_address
,
970 uint64_t end_address
)
972 struct efi_device_path_memory
*mdp
;
975 start
= buf
= dp_alloc(sizeof(*mdp
) + sizeof(END
));
980 mdp
->dp
.type
= DEVICE_PATH_TYPE_HARDWARE_DEVICE
;
981 mdp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MEMORY
;
982 mdp
->dp
.length
= sizeof(*mdp
);
983 mdp
->memory_type
= memory_type
;
984 mdp
->start_address
= start_address
;
985 mdp
->end_address
= end_address
;
988 *((struct efi_device_path
*)buf
) = END
;
994 * efi_dp_split_file_path() - split of relative file path from device path
996 * Given a device path indicating a file on a device, separate the device
997 * path in two: the device path of the actual device and the file path
998 * relative to this device.
1000 * @full_path: device path including device and file path
1001 * @device_path: path of the device
1002 * @file_path: relative path of the file or NULL if there is none
1003 * Return: status code
1005 efi_status_t
efi_dp_split_file_path(struct efi_device_path
*full_path
,
1006 struct efi_device_path
**device_path
,
1007 struct efi_device_path
**file_path
)
1009 struct efi_device_path
*p
, *dp
, *fp
= NULL
;
1011 *device_path
= NULL
;
1013 dp
= efi_dp_dup(full_path
);
1015 return EFI_OUT_OF_RESOURCES
;
1017 while (!EFI_DP_TYPE(p
, MEDIA_DEVICE
, FILE_PATH
)) {
1024 return EFI_OUT_OF_RESOURCES
;
1025 p
->type
= DEVICE_PATH_TYPE_END
;
1026 p
->sub_type
= DEVICE_PATH_SUB_TYPE_END
;
1027 p
->length
= sizeof(*p
);
1035 efi_status_t
efi_dp_from_name(const char *dev
, const char *devnr
,
1037 struct efi_device_path
**device
,
1038 struct efi_device_path
**file
)
1041 struct blk_desc
*desc
= NULL
;
1042 disk_partition_t fs_partition
;
1044 char filename
[32] = { 0 }; /* dp->str is u16[32] long */
1048 return EFI_INVALID_PARAMETER
;
1050 is_net
= !strcmp(dev
, "Net");
1052 part
= blk_get_device_part_str(dev
, devnr
, &desc
, &fs_partition
,
1054 if (part
< 0 || !desc
)
1055 return EFI_INVALID_PARAMETER
;
1058 *device
= efi_dp_from_part(desc
, part
);
1062 *device
= efi_dp_from_eth();
1069 snprintf(filename
, sizeof(filename
), "%s", path
);
1070 /* DOS style file path: */
1072 while ((s
= strchr(s
, '/')))
1074 *file
= efi_dp_from_file(((!is_net
&& device
) ? desc
: NULL
),
1078 return EFI_INVALID_PARAMETER
;