2 * EFI device path from u-boot device-model mapping
4 * (C) Copyright 2017 Rob Clark
6 * SPDX-License-Identifier: GPL-2.0+
9 #define LOG_CATEGORY LOGL_ERR
16 #include <efi_loader.h>
20 /* template END node: */
21 static const struct efi_device_path END = {
22 .type = DEVICE_PATH_TYPE_END,
23 .sub_type = DEVICE_PATH_SUB_TYPE_END,
24 .length = sizeof(END),
28 EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
29 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b)
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 * See UEFI spec (section 3.1.2, about short-form device-paths..
119 * tl;dr: we can have a device-path that starts with a USB WWID
120 * or USB Class node, and a few other cases which don't encode
121 * the full device path with bus hierarchy:
123 * - MESSAGING:USB_WWID
124 * - MESSAGING:USB_CLASS
129 static struct efi_device_path *shorten_path(struct efi_device_path *dp)
133 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
134 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
135 * so not sure when we would see these other cases.
137 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
138 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
139 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
142 dp = efi_dp_next(dp);
148 static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
149 struct efi_device_path **rem)
151 struct efi_object *efiobj;
152 unsigned int dp_size = efi_dp_size(dp);
154 list_for_each_entry(efiobj, &efi_obj_list, link) {
155 struct efi_handler *handler;
156 struct efi_device_path *obj_dp;
159 ret = efi_search_protocol(efiobj->handle,
160 &efi_guid_device_path, &handler);
161 if (ret != EFI_SUCCESS)
163 obj_dp = handler->protocol_interface;
166 if (efi_dp_match(dp, obj_dp) == 0) {
169 * Allow partial matches, but inform
172 *rem = ((void *)dp) +
176 /* Only return on exact matches */
177 if (efi_dp_size(obj_dp) == dp_size)
182 obj_dp = shorten_path(efi_dp_next(obj_dp));
183 } while (short_path && obj_dp);
190 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
191 * remaining part of the device path after the matched object.
193 struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
194 struct efi_device_path **rem)
196 struct efi_object *efiobj;
198 /* Search for an exact match first */
199 efiobj = find_obj(dp, false, NULL);
201 /* Then for a fuzzy match */
203 efiobj = find_obj(dp, false, rem);
205 /* And now for a fuzzy short match */
207 efiobj = find_obj(dp, true, rem);
213 * Determine the last device path node that is not the end node.
216 * @return last node before the end node if it exists
219 const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
221 struct efi_device_path *ret;
223 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
226 ret = (struct efi_device_path *)dp;
227 dp = efi_dp_next(dp);
232 /* return size not including End node: */
233 unsigned efi_dp_size(const struct efi_device_path *dp)
239 dp = efi_dp_next(dp);
245 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
247 struct efi_device_path *ndp;
248 unsigned sz = efi_dp_size(dp) + sizeof(END);
261 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
262 const struct efi_device_path *dp2)
264 struct efi_device_path *ret;
267 ret = efi_dp_dup(dp2);
269 ret = efi_dp_dup(dp1);
271 /* both dp1 and dp2 are non-null */
272 unsigned sz1 = efi_dp_size(dp1);
273 unsigned sz2 = efi_dp_size(dp2);
274 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
278 memcpy(p + sz1, dp2, sz2);
279 memcpy(p + sz1 + sz2, &END, sizeof(END));
286 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
287 const struct efi_device_path *node)
289 struct efi_device_path *ret;
292 ret = efi_dp_dup(&END);
294 ret = efi_dp_dup(dp);
296 unsigned sz = node->length;
297 void *p = dp_alloc(sz + sizeof(END));
301 memcpy(p + sz, &END, sizeof(END));
304 /* both dp and node are non-null */
305 unsigned sz = efi_dp_size(dp);
306 void *p = dp_alloc(sz + node->length + sizeof(END));
310 memcpy(p + sz, node, node->length);
311 memcpy(p + sz + node->length, &END, sizeof(END));
318 struct efi_device_path *efi_dp_create_device_node(const u8 type,
322 struct efi_device_path *ret;
324 ret = dp_alloc(length);
328 ret->sub_type = sub_type;
329 ret->length = length;
334 /* size of device-path not including END node for device and all parents
335 * up to the root device.
337 static unsigned dp_size(struct udevice *dev)
339 if (!dev || !dev->driver)
342 switch (dev->driver->id) {
344 case UCLASS_SIMPLE_BUS:
345 /* stop traversing parents at this point: */
348 return dp_size(dev->parent) +
349 sizeof(struct efi_device_path_mac_addr);
352 switch (dev->parent->uclass->uc_drv->id) {
355 return dp_size(dev->parent) +
356 sizeof(struct efi_device_path_atapi);
358 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
360 return dp_size(dev->parent) +
361 sizeof(struct efi_device_path_scsi);
363 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
365 return dp_size(dev->parent) +
366 sizeof(struct efi_device_path_sd_mmc_path);
369 return dp_size(dev->parent);
372 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
374 return dp_size(dev->parent) +
375 sizeof(struct efi_device_path_sd_mmc_path);
377 case UCLASS_MASS_STORAGE:
379 return dp_size(dev->parent) +
380 sizeof(struct efi_device_path_usb_class);
382 /* just skip over unknown classes: */
383 return dp_size(dev->parent);
388 * Recursively build a device path.
390 * @buf pointer to the end of the device path
392 * @return pointer to the end of the device path
394 static void *dp_fill(void *buf, struct udevice *dev)
396 if (!dev || !dev->driver)
399 switch (dev->driver->id) {
401 case UCLASS_SIMPLE_BUS: {
402 /* stop traversing parents at this point: */
403 struct efi_device_path_vendor *vdp = buf;
409 struct efi_device_path_mac_addr *dp =
410 dp_fill(buf, dev->parent);
411 struct eth_pdata *pdata = dev->platdata;
413 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
414 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
415 dp->dp.length = sizeof(*dp);
416 memset(&dp->mac, 0, sizeof(dp->mac));
417 /* We only support IPv4 */
418 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
426 switch (dev->parent->uclass->uc_drv->id) {
429 struct efi_device_path_atapi *dp =
430 dp_fill(buf, dev->parent);
431 struct blk_desc *desc = dev_get_uclass_platdata(dev);
433 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
434 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
435 dp->dp.length = sizeof(*dp);
436 dp->logical_unit_number = desc->devnum;
437 dp->primary_secondary = IDE_BUS(desc->devnum);
438 dp->slave_master = desc->devnum %
439 (CONFIG_SYS_IDE_MAXDEVICE /
440 CONFIG_SYS_IDE_MAXBUS);
444 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
446 struct efi_device_path_scsi *dp =
447 dp_fill(buf, dev->parent);
448 struct blk_desc *desc = dev_get_uclass_platdata(dev);
450 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
451 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
452 dp->dp.length = sizeof(*dp);
453 dp->logical_unit_number = desc->lun;
454 dp->target_id = desc->target;
458 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
460 struct efi_device_path_sd_mmc_path *sddp =
461 dp_fill(buf, dev->parent);
462 struct blk_desc *desc = dev_get_uclass_platdata(dev);
464 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
465 sddp->dp.sub_type = is_sd(desc) ?
466 DEVICE_PATH_SUB_TYPE_MSG_SD :
467 DEVICE_PATH_SUB_TYPE_MSG_MMC;
468 sddp->dp.length = sizeof(*sddp);
469 sddp->slot_number = dev->seq;
474 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
475 __FILE__, __LINE__, __func__,
476 dev->name, dev->parent->uclass->uc_drv->id);
477 return dp_fill(buf, dev->parent);
480 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
482 struct efi_device_path_sd_mmc_path *sddp =
483 dp_fill(buf, dev->parent);
484 struct mmc *mmc = mmc_get_mmc_dev(dev);
485 struct blk_desc *desc = mmc_get_blk_desc(mmc);
487 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
488 sddp->dp.sub_type = is_sd(desc) ?
489 DEVICE_PATH_SUB_TYPE_MSG_SD :
490 DEVICE_PATH_SUB_TYPE_MSG_MMC;
491 sddp->dp.length = sizeof(*sddp);
492 sddp->slot_number = dev->seq;
497 case UCLASS_MASS_STORAGE:
498 case UCLASS_USB_HUB: {
499 struct efi_device_path_usb_class *udp =
500 dp_fill(buf, dev->parent);
501 struct usb_device *udev = dev_get_parent_priv(dev);
502 struct usb_device_descriptor *desc = &udev->descriptor;
504 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
505 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
506 udp->dp.length = sizeof(*udp);
507 udp->vendor_id = desc->idVendor;
508 udp->product_id = desc->idProduct;
509 udp->device_class = desc->bDeviceClass;
510 udp->device_subclass = desc->bDeviceSubClass;
511 udp->device_protocol = desc->bDeviceProtocol;
516 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
517 __FILE__, __LINE__, __func__,
518 dev->name, dev->driver->id);
519 return dp_fill(buf, dev->parent);
523 /* Construct a device-path from a device: */
524 struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
528 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
531 buf = dp_fill(buf, dev);
532 *((struct efi_device_path *)buf) = END;
538 static unsigned dp_part_size(struct blk_desc *desc, int part)
545 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
548 dev = desc->bdev->parent;
549 dpsize = dp_size(dev);
552 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
555 if (part == 0) /* the actual disk, not a partition */
558 if (desc->part_type == PART_TYPE_ISO)
559 dpsize += sizeof(struct efi_device_path_cdrom_path);
561 dpsize += sizeof(struct efi_device_path_hard_drive_path);
567 * Create a device node for a block device partition.
569 * @buf buffer to which the device path is wirtten
570 * @desc block device descriptor
571 * @part partition number, 0 identifies a block device
573 static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
575 disk_partition_t info;
577 part_get_info(desc, part, &info);
579 if (desc->part_type == PART_TYPE_ISO) {
580 struct efi_device_path_cdrom_path *cddp = buf;
582 cddp->boot_entry = part;
583 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
584 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
585 cddp->dp.length = sizeof(*cddp);
586 cddp->partition_start = info.start;
587 cddp->partition_end = info.size;
591 struct efi_device_path_hard_drive_path *hddp = buf;
593 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
594 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
595 hddp->dp.length = sizeof(*hddp);
596 hddp->partition_number = part;
597 hddp->partition_start = info.start;
598 hddp->partition_end = info.size;
599 if (desc->part_type == PART_TYPE_EFI)
600 hddp->partmap_type = 2;
602 hddp->partmap_type = 1;
604 switch (desc->sig_type) {
607 hddp->signature_type = 0;
608 memset(hddp->partition_signature, 0,
609 sizeof(hddp->partition_signature));
612 hddp->signature_type = 1;
613 memset(hddp->partition_signature, 0,
614 sizeof(hddp->partition_signature));
615 memcpy(hddp->partition_signature, &desc->mbr_sig,
616 sizeof(desc->mbr_sig));
619 hddp->signature_type = 2;
620 memcpy(hddp->partition_signature, &desc->guid_sig,
621 sizeof(hddp->partition_signature));
632 * Create a device path for a block device or one of its partitions.
634 * @buf buffer to which the device path is wirtten
635 * @desc block device descriptor
636 * @part partition number, 0 identifies a block device
638 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
643 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
646 dev = desc->bdev->parent;
647 buf = dp_fill(buf, dev);
651 * We *could* make a more accurate path, by looking at if_type
652 * and handling all the different cases like we do for non-
653 * legacy (ie CONFIG_BLK=y) case. But most important thing
654 * is just to have a unique device-path for if_type+devnum.
655 * So map things to a fictitious USB device.
657 struct efi_device_path_usb *udp;
659 memcpy(buf, &ROOT, sizeof(ROOT));
663 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
664 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
665 udp->dp.length = sizeof(*udp);
666 udp->parent_port_number = desc->if_type;
667 udp->usb_interface = desc->devnum;
671 if (part == 0) /* the actual disk, not a partition */
674 return dp_part_node(buf, desc, part);
677 /* Construct a device-path from a partition on a blk device: */
678 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
682 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
686 buf = dp_part_fill(buf, desc, part);
688 *((struct efi_device_path *)buf) = END;
694 * Create a device node for a block device partition.
696 * @buf buffer to which the device path is wirtten
697 * @desc block device descriptor
698 * @part partition number, 0 identifies a block device
700 struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
705 if (desc->part_type == PART_TYPE_ISO)
706 dpsize = sizeof(struct efi_device_path_cdrom_path);
708 dpsize = sizeof(struct efi_device_path_hard_drive_path);
709 buf = dp_alloc(dpsize);
711 dp_part_node(buf, desc, part);
716 /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
717 static void path_to_uefi(u16 *uefi, const char *path)
729 * If desc is NULL, this creates a path with only the file component,
730 * otherwise it creates a full path with both device and file components
732 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
735 struct efi_device_path_file_path *fp;
737 unsigned dpsize = 0, fpsize;
740 dpsize = dp_part_size(desc, part);
742 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
745 start = buf = dp_alloc(dpsize + sizeof(END));
750 buf = dp_part_fill(buf, desc, part);
754 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
755 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
756 fp->dp.length = fpsize;
757 path_to_uefi(fp->str, path);
760 *((struct efi_device_path *)buf) = END;
766 struct efi_device_path *efi_dp_from_eth(void)
768 #ifndef CONFIG_DM_ETH
769 struct efi_device_path_mac_addr *ndp;
774 assert(eth_get_dev());
777 dpsize += dp_size(eth_get_dev());
779 dpsize += sizeof(ROOT);
780 dpsize += sizeof(*ndp);
783 start = buf = dp_alloc(dpsize + sizeof(END));
788 buf = dp_fill(buf, eth_get_dev());
790 memcpy(buf, &ROOT, sizeof(ROOT));
794 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
795 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
796 ndp->dp.length = sizeof(*ndp);
797 ndp->if_type = 1; /* Ethernet */
798 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
802 *((struct efi_device_path *)buf) = END;
808 /* Construct a device-path for memory-mapped image */
809 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
810 uint64_t start_address,
811 uint64_t end_address)
813 struct efi_device_path_memory *mdp;
816 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
821 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
822 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
823 mdp->dp.length = sizeof(*mdp);
824 mdp->memory_type = memory_type;
825 mdp->start_address = start_address;
826 mdp->end_address = end_address;
829 *((struct efi_device_path *)buf) = END;
835 * Helper to split a full device path (containing both device and file
836 * parts) into it's constituent parts.
838 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
839 struct efi_device_path **device_path,
840 struct efi_device_path **file_path)
842 struct efi_device_path *p, *dp, *fp;
846 dp = efi_dp_dup(full_path);
848 return EFI_OUT_OF_RESOURCES;
850 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
853 return EFI_OUT_OF_RESOURCES;
857 return EFI_OUT_OF_RESOURCES;
858 p->type = DEVICE_PATH_TYPE_END;
859 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
860 p->length = sizeof(*p);