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;
129 unsigned int dp_size = efi_dp_size(dp);
131 list_for_each_entry(efiobj, &efi_obj_list, link) {
132 struct efi_handler *handler;
133 struct efi_device_path *obj_dp;
136 ret = efi_search_protocol(efiobj->handle,
137 &efi_guid_device_path, &handler);
138 if (ret != EFI_SUCCESS)
140 obj_dp = handler->protocol_interface;
143 if (efi_dp_match(dp, obj_dp) == 0) {
146 * Allow partial matches, but inform
149 *rem = ((void *)dp) +
153 /* Only return on exact matches */
154 if (efi_dp_size(obj_dp) == dp_size)
159 obj_dp = shorten_path(efi_dp_next(obj_dp));
160 } while (short_path && obj_dp);
168 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
169 * remaining part of the device path after the matched object.
171 struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
172 struct efi_device_path **rem)
174 struct efi_object *efiobj;
176 /* Search for an exact match first */
177 efiobj = find_obj(dp, false, NULL);
179 /* Then for a fuzzy match */
181 efiobj = find_obj(dp, false, rem);
183 /* And now for a fuzzy short match */
185 efiobj = find_obj(dp, true, rem);
190 /* return size not including End node: */
191 unsigned efi_dp_size(const struct efi_device_path *dp)
197 dp = efi_dp_next(dp);
203 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
205 struct efi_device_path *ndp;
206 unsigned sz = efi_dp_size(dp) + sizeof(END);
217 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
218 const struct efi_device_path *dp2)
220 struct efi_device_path *ret;
223 ret = efi_dp_dup(dp2);
225 ret = efi_dp_dup(dp1);
227 /* both dp1 and dp2 are non-null */
228 unsigned sz1 = efi_dp_size(dp1);
229 unsigned sz2 = efi_dp_size(dp2);
230 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
232 memcpy(p + sz1, dp2, sz2);
233 memcpy(p + sz1 + sz2, &END, sizeof(END));
240 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
241 const struct efi_device_path *node)
243 struct efi_device_path *ret;
246 ret = efi_dp_dup(&END);
248 ret = efi_dp_dup(dp);
250 unsigned sz = node->length;
251 void *p = dp_alloc(sz + sizeof(END));
253 memcpy(p + sz, &END, sizeof(END));
256 /* both dp and node are non-null */
257 unsigned sz = efi_dp_size(dp);
258 void *p = dp_alloc(sz + node->length + sizeof(END));
260 memcpy(p + sz, node, node->length);
261 memcpy(p + sz + node->length, &END, sizeof(END));
269 /* size of device-path not including END node for device and all parents
270 * up to the root device.
272 static unsigned dp_size(struct udevice *dev)
274 if (!dev || !dev->driver)
277 switch (dev->driver->id) {
279 case UCLASS_SIMPLE_BUS:
280 /* stop traversing parents at this point: */
283 return dp_size(dev->parent) +
284 sizeof(struct efi_device_path_sd_mmc_path);
285 case UCLASS_MASS_STORAGE:
287 return dp_size(dev->parent) +
288 sizeof(struct efi_device_path_usb_class);
290 /* just skip over unknown classes: */
291 return dp_size(dev->parent);
295 static void *dp_fill(void *buf, struct udevice *dev)
297 if (!dev || !dev->driver)
300 switch (dev->driver->id) {
302 case UCLASS_SIMPLE_BUS: {
303 /* stop traversing parents at this point: */
304 struct efi_device_path_vendor *vdp = buf;
308 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
310 struct efi_device_path_sd_mmc_path *sddp =
311 dp_fill(buf, dev->parent);
312 struct mmc *mmc = mmc_get_mmc_dev(dev);
313 struct blk_desc *desc = mmc_get_blk_desc(mmc);
315 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
316 sddp->dp.sub_type = (desc->if_type == IF_TYPE_MMC) ?
317 DEVICE_PATH_SUB_TYPE_MSG_MMC :
318 DEVICE_PATH_SUB_TYPE_MSG_SD;
319 sddp->dp.length = sizeof(*sddp);
320 sddp->slot_number = dev->seq;
325 case UCLASS_MASS_STORAGE:
326 case UCLASS_USB_HUB: {
327 struct efi_device_path_usb_class *udp =
328 dp_fill(buf, dev->parent);
329 struct usb_device *udev = dev_get_parent_priv(dev);
330 struct usb_device_descriptor *desc = &udev->descriptor;
332 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
333 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
334 udp->dp.length = sizeof(*udp);
335 udp->vendor_id = desc->idVendor;
336 udp->product_id = desc->idProduct;
337 udp->device_class = desc->bDeviceClass;
338 udp->device_subclass = desc->bDeviceSubClass;
339 udp->device_protocol = desc->bDeviceProtocol;
344 debug("unhandled device class: %s (%u)\n",
345 dev->name, dev->driver->id);
346 return dp_fill(buf, dev->parent);
350 /* Construct a device-path from a device: */
351 struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
355 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
356 buf = dp_fill(buf, dev);
357 *((struct efi_device_path *)buf) = END;
363 static unsigned dp_part_size(struct blk_desc *desc, int part)
368 dpsize = dp_size(desc->bdev->parent);
370 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
373 if (part == 0) /* the actual disk, not a partition */
376 if (desc->part_type == PART_TYPE_ISO)
377 dpsize += sizeof(struct efi_device_path_cdrom_path);
379 dpsize += sizeof(struct efi_device_path_hard_drive_path);
384 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
386 disk_partition_t info;
389 buf = dp_fill(buf, desc->bdev->parent);
392 * We *could* make a more accurate path, by looking at if_type
393 * and handling all the different cases like we do for non-
394 * legacy (ie CONFIG_BLK=y) case. But most important thing
395 * is just to have a unique device-path for if_type+devnum.
396 * So map things to a fictional USB device:
398 struct efi_device_path_usb *udp;
400 memcpy(buf, &ROOT, sizeof(ROOT));
404 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
405 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
406 udp->dp.length = sizeof(*udp);
407 udp->parent_port_number = desc->if_type;
408 udp->usb_interface = desc->devnum;
412 if (part == 0) /* the actual disk, not a partition */
415 part_get_info(desc, part, &info);
417 if (desc->part_type == PART_TYPE_ISO) {
418 struct efi_device_path_cdrom_path *cddp = buf;
420 cddp->boot_entry = part - 1;
421 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
422 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
423 cddp->dp.length = sizeof(*cddp);
424 cddp->partition_start = info.start;
425 cddp->partition_end = info.size;
429 struct efi_device_path_hard_drive_path *hddp = buf;
431 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
432 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
433 hddp->dp.length = sizeof(*hddp);
434 hddp->partition_number = part - 1;
435 hddp->partition_start = info.start;
436 hddp->partition_end = info.size;
437 if (desc->part_type == PART_TYPE_EFI)
438 hddp->partmap_type = 2;
440 hddp->partmap_type = 1;
442 switch (desc->sig_type) {
445 hddp->signature_type = 0;
446 memset(hddp->partition_signature, 0,
447 sizeof(hddp->partition_signature));
450 hddp->signature_type = 1;
451 memset(hddp->partition_signature, 0,
452 sizeof(hddp->partition_signature));
453 memcpy(hddp->partition_signature, &desc->mbr_sig,
454 sizeof(desc->mbr_sig));
457 hddp->signature_type = 2;
458 memcpy(hddp->partition_signature, &desc->guid_sig,
459 sizeof(hddp->partition_signature));
470 /* Construct a device-path from a partition on a blk device: */
471 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
475 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
477 buf = dp_part_fill(buf, desc, part);
479 *((struct efi_device_path *)buf) = END;
484 /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
485 static void path_to_uefi(u16 *uefi, const char *path)
497 * If desc is NULL, this creates a path with only the file component,
498 * otherwise it creates a full path with both device and file components
500 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
503 struct efi_device_path_file_path *fp;
505 unsigned dpsize = 0, fpsize;
508 dpsize = dp_part_size(desc, part);
510 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
513 start = buf = dp_alloc(dpsize + sizeof(END));
516 buf = dp_part_fill(buf, desc, part);
520 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
521 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
522 fp->dp.length = fpsize;
523 path_to_uefi(fp->str, path);
526 *((struct efi_device_path *)buf) = END;
532 struct efi_device_path *efi_dp_from_eth(void)
534 struct efi_device_path_mac_addr *ndp;
538 assert(eth_get_dev());
541 dpsize += dp_size(eth_get_dev());
543 dpsize += sizeof(ROOT);
545 dpsize += sizeof(*ndp);
547 start = buf = dp_alloc(dpsize + sizeof(END));
550 buf = dp_fill(buf, eth_get_dev());
552 memcpy(buf, &ROOT, sizeof(ROOT));
557 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
558 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
559 ndp->dp.length = sizeof(*ndp);
560 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
563 *((struct efi_device_path *)buf) = END;
569 /* Construct a device-path for memory-mapped image */
570 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
571 uint64_t start_address,
572 uint64_t end_address)
574 struct efi_device_path_memory *mdp;
577 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
580 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
581 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
582 mdp->dp.length = sizeof(*mdp);
583 mdp->memory_type = memory_type;
584 mdp->start_address = start_address;
585 mdp->end_address = end_address;
588 *((struct efi_device_path *)buf) = END;
594 * Helper to split a full device path (containing both device and file
595 * parts) into it's constituent parts.
597 void efi_dp_split_file_path(struct efi_device_path *full_path,
598 struct efi_device_path **device_path,
599 struct efi_device_path **file_path)
601 struct efi_device_path *p, *dp, *fp;
603 dp = efi_dp_dup(full_path);
605 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH))
609 p->type = DEVICE_PATH_TYPE_END;
610 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
611 p->length = sizeof(*p);