2 * EFI device path from u-boot device-model mapping
4 * (C) Copyright 2017 Rob Clark
6 * SPDX-License-Identifier: GPL-2.0+
14 #include <efi_loader.h>
18 /* template END node: */
19 static const struct efi_device_path END
= {
20 .type
= DEVICE_PATH_TYPE_END
,
21 .sub_type
= DEVICE_PATH_SUB_TYPE_END
,
22 .length
= sizeof(END
),
26 EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
27 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b)
29 /* template ROOT node: */
30 static const struct efi_device_path_vendor ROOT
= {
32 .type
= DEVICE_PATH_TYPE_HARDWARE_DEVICE
,
33 .sub_type
= DEVICE_PATH_SUB_TYPE_VENDOR
,
34 .length
= sizeof(ROOT
),
39 static void *dp_alloc(size_t sz
)
43 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES
, sz
, &buf
) != EFI_SUCCESS
)
50 * Iterate to next block in device-path, terminating (returning NULL)
53 struct efi_device_path
*efi_dp_next(const struct efi_device_path
*dp
)
57 if (dp
->type
== DEVICE_PATH_TYPE_END
)
59 dp
= ((void *)dp
) + dp
->length
;
60 if (dp
->type
== DEVICE_PATH_TYPE_END
)
62 return (struct efi_device_path
*)dp
;
66 * Compare two device-paths, stopping when the shorter of the two hits
67 * an End* node. This is useful to, for example, compare a device-path
68 * representing a device with one representing a file on the device, or
69 * a device with a parent device.
71 int efi_dp_match(const struct efi_device_path
*a
,
72 const struct efi_device_path
*b
)
77 ret
= memcmp(&a
->length
, &b
->length
, sizeof(a
->length
));
81 ret
= memcmp(a
, b
, a
->length
);
95 * See UEFI spec (section 3.1.2, about short-form device-paths..
96 * tl;dr: we can have a device-path that starts with a USB WWID
97 * or USB Class node, and a few other cases which don't encode
98 * the full device path with bus hierarchy:
100 * - MESSAGING:USB_WWID
101 * - MESSAGING:USB_CLASS
106 static struct efi_device_path
*shorten_path(struct efi_device_path
*dp
)
110 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
111 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
112 * so not sure when we would see these other cases.
114 if (EFI_DP_TYPE(dp
, MESSAGING_DEVICE
, MSG_USB_CLASS
) ||
115 EFI_DP_TYPE(dp
, MEDIA_DEVICE
, HARD_DRIVE_PATH
) ||
116 EFI_DP_TYPE(dp
, MEDIA_DEVICE
, FILE_PATH
))
119 dp
= efi_dp_next(dp
);
125 static struct efi_object
*find_obj(struct efi_device_path
*dp
, bool short_path
,
126 struct efi_device_path
**rem
)
128 struct efi_object
*efiobj
;
130 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
133 for (i
= 0; i
< ARRAY_SIZE(efiobj
->protocols
); i
++) {
134 struct efi_handler
*handler
= &efiobj
->protocols
[i
];
135 struct efi_device_path
*obj_dp
;
140 if (guidcmp(handler
->guid
, &efi_guid_device_path
))
143 obj_dp
= handler
->protocol_interface
;
146 if (efi_dp_match(dp
, obj_dp
) == 0) {
148 *rem
= ((void *)dp
) +
154 obj_dp
= shorten_path(efi_dp_next(obj_dp
));
155 } while (short_path
&& obj_dp
);
164 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
165 * remaining part of the device path after the matched object.
167 struct efi_object
*efi_dp_find_obj(struct efi_device_path
*dp
,
168 struct efi_device_path
**rem
)
170 struct efi_object
*efiobj
;
172 efiobj
= find_obj(dp
, false, rem
);
175 efiobj
= find_obj(dp
, true, rem
);
180 /* return size not including End node: */
181 unsigned efi_dp_size(const struct efi_device_path
*dp
)
187 dp
= efi_dp_next(dp
);
193 struct efi_device_path
*efi_dp_dup(const struct efi_device_path
*dp
)
195 struct efi_device_path
*ndp
;
196 unsigned sz
= efi_dp_size(dp
) + sizeof(END
);
207 struct efi_device_path
*efi_dp_append(const struct efi_device_path
*dp1
,
208 const struct efi_device_path
*dp2
)
210 struct efi_device_path
*ret
;
213 ret
= efi_dp_dup(dp2
);
215 ret
= efi_dp_dup(dp1
);
217 /* both dp1 and dp2 are non-null */
218 unsigned sz1
= efi_dp_size(dp1
);
219 unsigned sz2
= efi_dp_size(dp2
);
220 void *p
= dp_alloc(sz1
+ sz2
+ sizeof(END
));
222 memcpy(p
+ sz1
, dp2
, sz2
);
223 memcpy(p
+ sz1
+ sz2
, &END
, sizeof(END
));
230 struct efi_device_path
*efi_dp_append_node(const struct efi_device_path
*dp
,
231 const struct efi_device_path
*node
)
233 struct efi_device_path
*ret
;
236 ret
= efi_dp_dup(&END
);
238 ret
= efi_dp_dup(dp
);
240 unsigned sz
= node
->length
;
241 void *p
= dp_alloc(sz
+ sizeof(END
));
243 memcpy(p
+ sz
, &END
, sizeof(END
));
246 /* both dp and node are non-null */
247 unsigned sz
= efi_dp_size(dp
);
248 void *p
= dp_alloc(sz
+ node
->length
+ sizeof(END
));
250 memcpy(p
+ sz
, node
, node
->length
);
251 memcpy(p
+ sz
+ node
->length
, &END
, sizeof(END
));
259 /* size of device-path not including END node for device and all parents
260 * up to the root device.
262 static unsigned dp_size(struct udevice
*dev
)
264 if (!dev
|| !dev
->driver
)
267 switch (dev
->driver
->id
) {
269 case UCLASS_SIMPLE_BUS
:
270 /* stop traversing parents at this point: */
273 return dp_size(dev
->parent
) +
274 sizeof(struct efi_device_path_sd_mmc_path
);
275 case UCLASS_MASS_STORAGE
:
277 return dp_size(dev
->parent
) +
278 sizeof(struct efi_device_path_usb_class
);
280 /* just skip over unknown classes: */
281 return dp_size(dev
->parent
);
285 static void *dp_fill(void *buf
, struct udevice
*dev
)
287 if (!dev
|| !dev
->driver
)
290 switch (dev
->driver
->id
) {
292 case UCLASS_SIMPLE_BUS
: {
293 /* stop traversing parents at this point: */
294 struct efi_device_path_vendor
*vdp
= buf
;
298 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
300 struct efi_device_path_sd_mmc_path
*sddp
=
301 dp_fill(buf
, dev
->parent
);
302 struct mmc
*mmc
= mmc_get_mmc_dev(dev
);
303 struct blk_desc
*desc
= mmc_get_blk_desc(mmc
);
305 sddp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
306 sddp
->dp
.sub_type
= (desc
->if_type
== IF_TYPE_MMC
) ?
307 DEVICE_PATH_SUB_TYPE_MSG_MMC
:
308 DEVICE_PATH_SUB_TYPE_MSG_SD
;
309 sddp
->dp
.length
= sizeof(*sddp
);
310 sddp
->slot_number
= dev
->seq
;
315 case UCLASS_MASS_STORAGE
:
316 case UCLASS_USB_HUB
: {
317 struct efi_device_path_usb_class
*udp
=
318 dp_fill(buf
, dev
->parent
);
319 struct usb_device
*udev
= dev_get_parent_priv(dev
);
320 struct usb_device_descriptor
*desc
= &udev
->descriptor
;
322 udp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
323 udp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS
;
324 udp
->dp
.length
= sizeof(*udp
);
325 udp
->vendor_id
= desc
->idVendor
;
326 udp
->product_id
= desc
->idProduct
;
327 udp
->device_class
= desc
->bDeviceClass
;
328 udp
->device_subclass
= desc
->bDeviceSubClass
;
329 udp
->device_protocol
= desc
->bDeviceProtocol
;
334 debug("unhandled device class: %s (%u)\n",
335 dev
->name
, dev
->driver
->id
);
336 return dp_fill(buf
, dev
->parent
);
340 /* Construct a device-path from a device: */
341 struct efi_device_path
*efi_dp_from_dev(struct udevice
*dev
)
345 start
= buf
= dp_alloc(dp_size(dev
) + sizeof(END
));
346 buf
= dp_fill(buf
, dev
);
347 *((struct efi_device_path
*)buf
) = END
;
353 static unsigned dp_part_size(struct blk_desc
*desc
, int part
)
358 dpsize
= dp_size(desc
->bdev
->parent
);
360 dpsize
= sizeof(ROOT
) + sizeof(struct efi_device_path_usb
);
363 if (part
== 0) /* the actual disk, not a partition */
366 if (desc
->part_type
== PART_TYPE_ISO
)
367 dpsize
+= sizeof(struct efi_device_path_cdrom_path
);
369 dpsize
+= sizeof(struct efi_device_path_hard_drive_path
);
374 static void *dp_part_fill(void *buf
, struct blk_desc
*desc
, int part
)
376 disk_partition_t info
;
379 buf
= dp_fill(buf
, desc
->bdev
->parent
);
382 * We *could* make a more accurate path, by looking at if_type
383 * and handling all the different cases like we do for non-
384 * legacy (ie CONFIG_BLK=y) case. But most important thing
385 * is just to have a unique device-path for if_type+devnum.
386 * So map things to a fictional USB device:
388 struct efi_device_path_usb
*udp
;
390 memcpy(buf
, &ROOT
, sizeof(ROOT
));
394 udp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
395 udp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MSG_USB
;
396 udp
->dp
.length
= sizeof(*udp
);
397 udp
->parent_port_number
= desc
->if_type
;
398 udp
->usb_interface
= desc
->devnum
;
402 if (part
== 0) /* the actual disk, not a partition */
405 part_get_info(desc
, part
, &info
);
407 if (desc
->part_type
== PART_TYPE_ISO
) {
408 struct efi_device_path_cdrom_path
*cddp
= buf
;
410 cddp
->boot_entry
= part
- 1;
411 cddp
->dp
.type
= DEVICE_PATH_TYPE_MEDIA_DEVICE
;
412 cddp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_CDROM_PATH
;
413 cddp
->dp
.length
= sizeof(*cddp
);
414 cddp
->partition_start
= info
.start
;
415 cddp
->partition_end
= info
.size
;
419 struct efi_device_path_hard_drive_path
*hddp
= buf
;
421 hddp
->dp
.type
= DEVICE_PATH_TYPE_MEDIA_DEVICE
;
422 hddp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH
;
423 hddp
->dp
.length
= sizeof(*hddp
);
424 hddp
->partition_number
= part
- 1;
425 hddp
->partition_start
= info
.start
;
426 hddp
->partition_end
= info
.size
;
427 if (desc
->part_type
== PART_TYPE_EFI
)
428 hddp
->partmap_type
= 2;
430 hddp
->partmap_type
= 1;
432 switch (desc
->sig_type
) {
435 hddp
->signature_type
= 0;
436 memset(hddp
->partition_signature
, 0,
437 sizeof(hddp
->partition_signature
));
440 hddp
->signature_type
= 1;
441 memset(hddp
->partition_signature
, 0,
442 sizeof(hddp
->partition_signature
));
443 memcpy(hddp
->partition_signature
, &desc
->mbr_sig
,
444 sizeof(desc
->mbr_sig
));
447 hddp
->signature_type
= 2;
448 memcpy(hddp
->partition_signature
, &desc
->guid_sig
,
449 sizeof(hddp
->partition_signature
));
460 /* Construct a device-path from a partition on a blk device: */
461 struct efi_device_path
*efi_dp_from_part(struct blk_desc
*desc
, int part
)
465 start
= buf
= dp_alloc(dp_part_size(desc
, part
) + sizeof(END
));
467 buf
= dp_part_fill(buf
, desc
, part
);
469 *((struct efi_device_path
*)buf
) = END
;
474 /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
475 static void path_to_uefi(u16
*uefi
, const char *path
)
487 * If desc is NULL, this creates a path with only the file component,
488 * otherwise it creates a full path with both device and file components
490 struct efi_device_path
*efi_dp_from_file(struct blk_desc
*desc
, int part
,
493 struct efi_device_path_file_path
*fp
;
495 unsigned dpsize
= 0, fpsize
;
498 dpsize
= dp_part_size(desc
, part
);
500 fpsize
= sizeof(struct efi_device_path
) + 2 * (strlen(path
) + 1);
503 start
= buf
= dp_alloc(dpsize
+ sizeof(END
));
506 buf
= dp_part_fill(buf
, desc
, part
);
510 fp
->dp
.type
= DEVICE_PATH_TYPE_MEDIA_DEVICE
;
511 fp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_FILE_PATH
;
512 fp
->dp
.length
= fpsize
;
513 path_to_uefi(fp
->str
, path
);
516 *((struct efi_device_path
*)buf
) = END
;
522 struct efi_device_path
*efi_dp_from_eth(void)
524 struct efi_device_path_mac_addr
*ndp
;
528 assert(eth_get_dev());
531 dpsize
+= dp_size(eth_get_dev());
533 dpsize
+= sizeof(ROOT
);
535 dpsize
+= sizeof(*ndp
);
537 start
= buf
= dp_alloc(dpsize
+ sizeof(END
));
540 buf
= dp_fill(buf
, eth_get_dev());
542 memcpy(buf
, &ROOT
, sizeof(ROOT
));
547 ndp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
548 ndp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR
;
549 ndp
->dp
.length
= sizeof(*ndp
);
550 memcpy(ndp
->mac
.addr
, eth_get_ethaddr(), ARP_HLEN
);
553 *((struct efi_device_path
*)buf
) = END
;
559 /* Construct a device-path for memory-mapped image */
560 struct efi_device_path
*efi_dp_from_mem(uint32_t memory_type
,
561 uint64_t start_address
,
562 uint64_t end_address
)
564 struct efi_device_path_memory
*mdp
;
567 start
= buf
= dp_alloc(sizeof(*mdp
) + sizeof(END
));
570 mdp
->dp
.type
= DEVICE_PATH_TYPE_HARDWARE_DEVICE
;
571 mdp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MEMORY
;
572 mdp
->dp
.length
= sizeof(*mdp
);
573 mdp
->memory_type
= memory_type
;
574 mdp
->start_address
= start_address
;
575 mdp
->end_address
= end_address
;
578 *((struct efi_device_path
*)buf
) = END
;
584 * Helper to split a full device path (containing both device and file
585 * parts) into it's constituent parts.
587 void efi_dp_split_file_path(struct efi_device_path
*full_path
,
588 struct efi_device_path
**device_path
,
589 struct efi_device_path
**file_path
)
591 struct efi_device_path
*p
, *dp
, *fp
;
593 dp
= efi_dp_dup(full_path
);
595 while (!EFI_DP_TYPE(p
, MEDIA_DEVICE
, FILE_PATH
))
599 p
->type
= DEVICE_PATH_TYPE_END
;
600 p
->sub_type
= DEVICE_PATH_SUB_TYPE_END
;
601 p
->length
= sizeof(*p
);