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(struct efi_device_path
*a
, struct efi_device_path
*b
)
76 ret
= memcmp(&a
->length
, &b
->length
, sizeof(a
->length
));
80 ret
= memcmp(a
, b
, a
->length
);
94 * See UEFI spec (section 3.1.2, about short-form device-paths..
95 * tl;dr: we can have a device-path that starts with a USB WWID
96 * or USB Class node, and a few other cases which don't encode
97 * the full device path with bus hierarchy:
99 * - MESSAGING:USB_WWID
100 * - MESSAGING:USB_CLASS
105 static struct efi_device_path
*shorten_path(struct efi_device_path
*dp
)
109 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
110 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
111 * so not sure when we would see these other cases.
113 if (EFI_DP_TYPE(dp
, MESSAGING_DEVICE
, MSG_USB_CLASS
) ||
114 EFI_DP_TYPE(dp
, MEDIA_DEVICE
, HARD_DRIVE_PATH
) ||
115 EFI_DP_TYPE(dp
, MEDIA_DEVICE
, FILE_PATH
))
118 dp
= efi_dp_next(dp
);
124 static struct efi_object
*find_obj(struct efi_device_path
*dp
, bool short_path
,
125 struct efi_device_path
**rem
)
127 struct efi_object
*efiobj
;
129 list_for_each_entry(efiobj
, &efi_obj_list
, link
) {
132 for (i
= 0; i
< ARRAY_SIZE(efiobj
->protocols
); i
++) {
133 struct efi_handler
*handler
= &efiobj
->protocols
[i
];
134 struct efi_device_path
*obj_dp
;
139 if (guidcmp(handler
->guid
, &efi_guid_device_path
))
142 obj_dp
= handler
->protocol_interface
;
145 if (efi_dp_match(dp
, obj_dp
) == 0) {
147 *rem
= ((void *)dp
) +
153 obj_dp
= shorten_path(efi_dp_next(obj_dp
));
154 } while (short_path
&& obj_dp
);
163 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
164 * remaining part of the device path after the matched object.
166 struct efi_object
*efi_dp_find_obj(struct efi_device_path
*dp
,
167 struct efi_device_path
**rem
)
169 struct efi_object
*efiobj
;
171 efiobj
= find_obj(dp
, false, rem
);
174 efiobj
= find_obj(dp
, true, rem
);
179 /* return size not including End node: */
180 unsigned efi_dp_size(const struct efi_device_path
*dp
)
186 dp
= efi_dp_next(dp
);
192 struct efi_device_path
*efi_dp_dup(const struct efi_device_path
*dp
)
194 struct efi_device_path
*ndp
;
195 unsigned sz
= efi_dp_size(dp
) + sizeof(END
);
206 struct efi_device_path
*efi_dp_append(const struct efi_device_path
*dp1
,
207 const struct efi_device_path
*dp2
)
209 struct efi_device_path
*ret
;
212 ret
= efi_dp_dup(dp2
);
214 ret
= efi_dp_dup(dp1
);
216 /* both dp1 and dp2 are non-null */
217 unsigned sz1
= efi_dp_size(dp1
);
218 unsigned sz2
= efi_dp_size(dp2
);
219 void *p
= dp_alloc(sz1
+ sz2
+ sizeof(END
));
221 memcpy(p
+ sz1
, dp2
, sz2
);
222 memcpy(p
+ sz1
+ sz2
, &END
, sizeof(END
));
229 struct efi_device_path
*efi_dp_append_node(const struct efi_device_path
*dp
,
230 const struct efi_device_path
*node
)
232 struct efi_device_path
*ret
;
235 ret
= efi_dp_dup(&END
);
237 ret
= efi_dp_dup(dp
);
239 unsigned sz
= node
->length
;
240 void *p
= dp_alloc(sz
+ sizeof(END
));
242 memcpy(p
+ sz
, &END
, sizeof(END
));
245 /* both dp and node are non-null */
246 unsigned sz
= efi_dp_size(dp
);
247 void *p
= dp_alloc(sz
+ node
->length
+ sizeof(END
));
249 memcpy(p
+ sz
, node
, node
->length
);
250 memcpy(p
+ sz
+ node
->length
, &END
, sizeof(END
));
258 /* size of device-path not including END node for device and all parents
259 * up to the root device.
261 static unsigned dp_size(struct udevice
*dev
)
263 if (!dev
|| !dev
->driver
)
266 switch (dev
->driver
->id
) {
268 case UCLASS_SIMPLE_BUS
:
269 /* stop traversing parents at this point: */
272 return dp_size(dev
->parent
) +
273 sizeof(struct efi_device_path_sd_mmc_path
);
274 case UCLASS_MASS_STORAGE
:
276 return dp_size(dev
->parent
) +
277 sizeof(struct efi_device_path_usb_class
);
279 /* just skip over unknown classes: */
280 return dp_size(dev
->parent
);
284 static void *dp_fill(void *buf
, struct udevice
*dev
)
286 if (!dev
|| !dev
->driver
)
289 switch (dev
->driver
->id
) {
291 case UCLASS_SIMPLE_BUS
: {
292 /* stop traversing parents at this point: */
293 struct efi_device_path_vendor
*vdp
= buf
;
297 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
299 struct efi_device_path_sd_mmc_path
*sddp
=
300 dp_fill(buf
, dev
->parent
);
301 struct mmc
*mmc
= mmc_get_mmc_dev(dev
);
302 struct blk_desc
*desc
= mmc_get_blk_desc(mmc
);
304 sddp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
305 sddp
->dp
.sub_type
= (desc
->if_type
== IF_TYPE_MMC
) ?
306 DEVICE_PATH_SUB_TYPE_MSG_MMC
:
307 DEVICE_PATH_SUB_TYPE_MSG_SD
;
308 sddp
->dp
.length
= sizeof(*sddp
);
309 sddp
->slot_number
= dev
->seq
;
314 case UCLASS_MASS_STORAGE
:
315 case UCLASS_USB_HUB
: {
316 struct efi_device_path_usb_class
*udp
=
317 dp_fill(buf
, dev
->parent
);
318 struct usb_device
*udev
= dev_get_parent_priv(dev
);
319 struct usb_device_descriptor
*desc
= &udev
->descriptor
;
321 udp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
322 udp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS
;
323 udp
->dp
.length
= sizeof(*udp
);
324 udp
->vendor_id
= desc
->idVendor
;
325 udp
->product_id
= desc
->idProduct
;
326 udp
->device_class
= desc
->bDeviceClass
;
327 udp
->device_subclass
= desc
->bDeviceSubClass
;
328 udp
->device_protocol
= desc
->bDeviceProtocol
;
333 debug("unhandled device class: %s (%u)\n",
334 dev
->name
, dev
->driver
->id
);
335 return dp_fill(buf
, dev
->parent
);
339 /* Construct a device-path from a device: */
340 struct efi_device_path
*efi_dp_from_dev(struct udevice
*dev
)
344 start
= buf
= dp_alloc(dp_size(dev
) + sizeof(END
));
345 buf
= dp_fill(buf
, dev
);
346 *((struct efi_device_path
*)buf
) = END
;
352 static unsigned dp_part_size(struct blk_desc
*desc
, int part
)
357 dpsize
= dp_size(desc
->bdev
->parent
);
359 dpsize
= sizeof(ROOT
) + sizeof(struct efi_device_path_usb
);
362 if (part
== 0) /* the actual disk, not a partition */
365 if (desc
->part_type
== PART_TYPE_ISO
)
366 dpsize
+= sizeof(struct efi_device_path_cdrom_path
);
368 dpsize
+= sizeof(struct efi_device_path_hard_drive_path
);
373 static void *dp_part_fill(void *buf
, struct blk_desc
*desc
, int part
)
375 disk_partition_t info
;
378 buf
= dp_fill(buf
, desc
->bdev
->parent
);
381 * We *could* make a more accurate path, by looking at if_type
382 * and handling all the different cases like we do for non-
383 * legacy (ie CONFIG_BLK=y) case. But most important thing
384 * is just to have a unique device-path for if_type+devnum.
385 * So map things to a fictional USB device:
387 struct efi_device_path_usb
*udp
;
389 memcpy(buf
, &ROOT
, sizeof(ROOT
));
393 udp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
394 udp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MSG_USB
;
395 udp
->dp
.length
= sizeof(*udp
);
396 udp
->parent_port_number
= desc
->if_type
;
397 udp
->usb_interface
= desc
->devnum
;
401 if (part
== 0) /* the actual disk, not a partition */
404 part_get_info(desc
, part
, &info
);
406 if (desc
->part_type
== PART_TYPE_ISO
) {
407 struct efi_device_path_cdrom_path
*cddp
= buf
;
409 cddp
->boot_entry
= part
- 1;
410 cddp
->dp
.type
= DEVICE_PATH_TYPE_MEDIA_DEVICE
;
411 cddp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_CDROM_PATH
;
412 cddp
->dp
.length
= sizeof(*cddp
);
413 cddp
->partition_start
= info
.start
;
414 cddp
->partition_end
= info
.size
;
418 struct efi_device_path_hard_drive_path
*hddp
= buf
;
420 hddp
->dp
.type
= DEVICE_PATH_TYPE_MEDIA_DEVICE
;
421 hddp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH
;
422 hddp
->dp
.length
= sizeof(*hddp
);
423 hddp
->partition_number
= part
- 1;
424 hddp
->partition_start
= info
.start
;
425 hddp
->partition_end
= info
.size
;
426 if (desc
->part_type
== PART_TYPE_EFI
)
427 hddp
->partmap_type
= 2;
429 hddp
->partmap_type
= 1;
430 hddp
->signature_type
= desc
->sig_type
;
431 if (hddp
->signature_type
!= 0)
432 memcpy(hddp
->partition_signature
, &desc
->guid_sig
,
433 sizeof(hddp
->partition_signature
));
442 /* Construct a device-path from a partition on a blk device: */
443 struct efi_device_path
*efi_dp_from_part(struct blk_desc
*desc
, int part
)
447 start
= buf
= dp_alloc(dp_part_size(desc
, part
) + sizeof(END
));
449 buf
= dp_part_fill(buf
, desc
, part
);
451 *((struct efi_device_path
*)buf
) = END
;
456 /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
457 static void path_to_uefi(u16
*uefi
, const char *path
)
469 * If desc is NULL, this creates a path with only the file component,
470 * otherwise it creates a full path with both device and file components
472 struct efi_device_path
*efi_dp_from_file(struct blk_desc
*desc
, int part
,
475 struct efi_device_path_file_path
*fp
;
477 unsigned dpsize
= 0, fpsize
;
480 dpsize
= dp_part_size(desc
, part
);
482 fpsize
= sizeof(struct efi_device_path
) + 2 * (strlen(path
) + 1);
485 start
= buf
= dp_alloc(dpsize
+ sizeof(END
));
488 buf
= dp_part_fill(buf
, desc
, part
);
492 fp
->dp
.type
= DEVICE_PATH_TYPE_MEDIA_DEVICE
;
493 fp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_FILE_PATH
;
494 fp
->dp
.length
= fpsize
;
495 path_to_uefi(fp
->str
, path
);
498 *((struct efi_device_path
*)buf
) = END
;
504 struct efi_device_path
*efi_dp_from_eth(void)
506 struct efi_device_path_mac_addr
*ndp
;
510 assert(eth_get_dev());
513 dpsize
+= dp_size(eth_get_dev());
515 dpsize
+= sizeof(ROOT
);
517 dpsize
+= sizeof(*ndp
);
519 start
= buf
= dp_alloc(dpsize
+ sizeof(END
));
522 buf
= dp_fill(buf
, eth_get_dev());
524 memcpy(buf
, &ROOT
, sizeof(ROOT
));
529 ndp
->dp
.type
= DEVICE_PATH_TYPE_MESSAGING_DEVICE
;
530 ndp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR
;
531 ndp
->dp
.length
= sizeof(*ndp
);
532 memcpy(ndp
->mac
.addr
, eth_get_ethaddr(), ARP_HLEN
);
535 *((struct efi_device_path
*)buf
) = END
;
541 /* Construct a device-path for memory-mapped image */
542 struct efi_device_path
*efi_dp_from_mem(uint32_t memory_type
,
543 uint64_t start_address
,
544 uint64_t end_address
)
546 struct efi_device_path_memory
*mdp
;
549 start
= buf
= dp_alloc(sizeof(*mdp
) + sizeof(END
));
552 mdp
->dp
.type
= DEVICE_PATH_TYPE_HARDWARE_DEVICE
;
553 mdp
->dp
.sub_type
= DEVICE_PATH_SUB_TYPE_MEMORY
;
554 mdp
->dp
.length
= sizeof(*mdp
);
555 mdp
->memory_type
= memory_type
;
556 mdp
->start_address
= start_address
;
557 mdp
->end_address
= end_address
;
560 *((struct efi_device_path
*)buf
) = END
;
566 * Helper to split a full device path (containing both device and file
567 * parts) into it's constituent parts.
569 void efi_dp_split_file_path(struct efi_device_path
*full_path
,
570 struct efi_device_path
**device_path
,
571 struct efi_device_path
**file_path
)
573 struct efi_device_path
*p
, *dp
, *fp
;
575 dp
= efi_dp_dup(full_path
);
577 while (!EFI_DP_TYPE(p
, MEDIA_DEVICE
, FILE_PATH
))
581 p
->type
= DEVICE_PATH_TYPE_END
;
582 p
->sub_type
= DEVICE_PATH_SUB_TYPE_END
;
583 p
->length
= sizeof(*p
);