f049b9e0cd0843e19e3f67238b5fb7b37878d739
[oweals/u-boot.git] / lib / efi_loader / efi_device_path.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * EFI device path from u-boot device-model mapping
4  *
5  * (C) Copyright 2017 Rob Clark
6  */
7
8 #include <common.h>
9 #include <blk.h>
10 #include <dm.h>
11 #include <log.h>
12 #include <net.h>
13 #include <usb.h>
14 #include <mmc.h>
15 #include <nvme.h>
16 #include <efi_loader.h>
17 #include <part.h>
18 #include <sandboxblockdev.h>
19 #include <asm-generic/unaligned.h>
20 #include <linux/compat.h> /* U16_MAX */
21
22 #ifdef CONFIG_SANDBOX
23 const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
24 #endif
25 #ifdef CONFIG_VIRTIO_BLK
26 const efi_guid_t efi_guid_virtio_dev = U_BOOT_VIRTIO_DEV_GUID;
27 #endif
28
29 /* template END node: */
30 static const struct efi_device_path END = {
31         .type     = DEVICE_PATH_TYPE_END,
32         .sub_type = DEVICE_PATH_SUB_TYPE_END,
33         .length   = sizeof(END),
34 };
35
36 /* template ROOT node: */
37 static const struct efi_device_path_vendor ROOT = {
38         .dp = {
39                 .type     = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
40                 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
41                 .length   = sizeof(ROOT),
42         },
43         .guid = U_BOOT_GUID,
44 };
45
46 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
47 /*
48  * Determine if an MMC device is an SD card.
49  *
50  * @desc        block device descriptor
51  * @return      true if the device is an SD card
52  */
53 static bool is_sd(struct blk_desc *desc)
54 {
55         struct mmc *mmc = find_mmc_device(desc->devnum);
56
57         if (!mmc)
58                 return false;
59
60         return IS_SD(mmc) != 0U;
61 }
62 #endif
63
64 static void *dp_alloc(size_t sz)
65 {
66         void *buf;
67
68         if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
69             EFI_SUCCESS) {
70                 debug("EFI: ERROR: out of memory in %s\n", __func__);
71                 return NULL;
72         }
73
74         memset(buf, 0, sz);
75         return buf;
76 }
77
78 /*
79  * Iterate to next block in device-path, terminating (returning NULL)
80  * at /End* node.
81  */
82 struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
83 {
84         if (dp == NULL)
85                 return NULL;
86         if (dp->type == DEVICE_PATH_TYPE_END)
87                 return NULL;
88         dp = ((void *)dp) + dp->length;
89         if (dp->type == DEVICE_PATH_TYPE_END)
90                 return NULL;
91         return (struct efi_device_path *)dp;
92 }
93
94 /*
95  * Compare two device-paths, stopping when the shorter of the two hits
96  * an End* node. This is useful to, for example, compare a device-path
97  * representing a device with one representing a file on the device, or
98  * a device with a parent device.
99  */
100 int efi_dp_match(const struct efi_device_path *a,
101                  const struct efi_device_path *b)
102 {
103         while (1) {
104                 int ret;
105
106                 ret = memcmp(&a->length, &b->length, sizeof(a->length));
107                 if (ret)
108                         return ret;
109
110                 ret = memcmp(a, b, a->length);
111                 if (ret)
112                         return ret;
113
114                 a = efi_dp_next(a);
115                 b = efi_dp_next(b);
116
117                 if (!a || !b)
118                         return 0;
119         }
120 }
121
122 /*
123  * We can have device paths that start with a USB WWID or a USB Class node,
124  * and a few other cases which don't encode the full device path with bus
125  * hierarchy:
126  *
127  *   - MESSAGING:USB_WWID
128  *   - MESSAGING:USB_CLASS
129  *   - MEDIA:FILE_PATH
130  *   - MEDIA:HARD_DRIVE
131  *   - MESSAGING:URI
132  *
133  * See UEFI spec (section 3.1.2, about short-form device-paths)
134  */
135 static struct efi_device_path *shorten_path(struct efi_device_path *dp)
136 {
137         while (dp) {
138                 /*
139                  * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
140                  * in practice fallback.efi just uses MEDIA:HARD_DRIVE
141                  * so not sure when we would see these other cases.
142                  */
143                 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
144                     EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
145                     EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
146                         return dp;
147
148                 dp = efi_dp_next(dp);
149         }
150
151         return dp;
152 }
153
154 static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
155                                    struct efi_device_path **rem)
156 {
157         struct efi_object *efiobj;
158         efi_uintn_t dp_size = efi_dp_instance_size(dp);
159
160         list_for_each_entry(efiobj, &efi_obj_list, link) {
161                 struct efi_handler *handler;
162                 struct efi_device_path *obj_dp;
163                 efi_status_t ret;
164
165                 ret = efi_search_protocol(efiobj,
166                                           &efi_guid_device_path, &handler);
167                 if (ret != EFI_SUCCESS)
168                         continue;
169                 obj_dp = handler->protocol_interface;
170
171                 do {
172                         if (efi_dp_match(dp, obj_dp) == 0) {
173                                 if (rem) {
174                                         /*
175                                          * Allow partial matches, but inform
176                                          * the caller.
177                                          */
178                                         *rem = ((void *)dp) +
179                                                 efi_dp_instance_size(obj_dp);
180                                         return efiobj;
181                                 } else {
182                                         /* Only return on exact matches */
183                                         if (efi_dp_instance_size(obj_dp) ==
184                                             dp_size)
185                                                 return efiobj;
186                                 }
187                         }
188
189                         obj_dp = shorten_path(efi_dp_next(obj_dp));
190                 } while (short_path && obj_dp);
191         }
192
193         return NULL;
194 }
195
196 /*
197  * Find an efiobj from device-path, if 'rem' is not NULL, returns the
198  * remaining part of the device path after the matched object.
199  */
200 struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
201                                    struct efi_device_path **rem)
202 {
203         struct efi_object *efiobj;
204
205         /* Search for an exact match first */
206         efiobj = find_obj(dp, false, NULL);
207
208         /* Then for a fuzzy match */
209         if (!efiobj)
210                 efiobj = find_obj(dp, false, rem);
211
212         /* And now for a fuzzy short match */
213         if (!efiobj)
214                 efiobj = find_obj(dp, true, rem);
215
216         return efiobj;
217 }
218
219 /*
220  * Determine the last device path node that is not the end node.
221  *
222  * @dp          device path
223  * @return      last node before the end node if it exists
224  *              otherwise NULL
225  */
226 const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
227 {
228         struct efi_device_path *ret;
229
230         if (!dp || dp->type == DEVICE_PATH_TYPE_END)
231                 return NULL;
232         while (dp) {
233                 ret = (struct efi_device_path *)dp;
234                 dp = efi_dp_next(dp);
235         }
236         return ret;
237 }
238
239 /* get size of the first device path instance excluding end node */
240 efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
241 {
242         efi_uintn_t sz = 0;
243
244         if (!dp || dp->type == DEVICE_PATH_TYPE_END)
245                 return 0;
246         while (dp) {
247                 sz += dp->length;
248                 dp = efi_dp_next(dp);
249         }
250
251         return sz;
252 }
253
254 /* get size of multi-instance device path excluding end node */
255 efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
256 {
257         const struct efi_device_path *p = dp;
258
259         if (!p)
260                 return 0;
261         while (p->type != DEVICE_PATH_TYPE_END ||
262                p->sub_type != DEVICE_PATH_SUB_TYPE_END)
263                 p = (void *)p + p->length;
264
265         return (void *)p - (void *)dp;
266 }
267
268 /* copy multi-instance device path */
269 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
270 {
271         struct efi_device_path *ndp;
272         size_t sz = efi_dp_size(dp) + sizeof(END);
273
274         if (!dp)
275                 return NULL;
276
277         ndp = dp_alloc(sz);
278         if (!ndp)
279                 return NULL;
280         memcpy(ndp, dp, sz);
281
282         return ndp;
283 }
284
285 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
286                                       const struct efi_device_path *dp2)
287 {
288         struct efi_device_path *ret;
289
290         if (!dp1 && !dp2) {
291                 /* return an end node */
292                 ret = efi_dp_dup(&END);
293         } else if (!dp1) {
294                 ret = efi_dp_dup(dp2);
295         } else if (!dp2) {
296                 ret = efi_dp_dup(dp1);
297         } else {
298                 /* both dp1 and dp2 are non-null */
299                 unsigned sz1 = efi_dp_size(dp1);
300                 unsigned sz2 = efi_dp_size(dp2);
301                 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
302                 if (!p)
303                         return NULL;
304                 memcpy(p, dp1, sz1);
305                 /* the end node of the second device path has to be retained */
306                 memcpy(p + sz1, dp2, sz2 + sizeof(END));
307                 ret = p;
308         }
309
310         return ret;
311 }
312
313 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
314                                            const struct efi_device_path *node)
315 {
316         struct efi_device_path *ret;
317
318         if (!node && !dp) {
319                 ret = efi_dp_dup(&END);
320         } else if (!node) {
321                 ret = efi_dp_dup(dp);
322         } else if (!dp) {
323                 size_t sz = node->length;
324                 void *p = dp_alloc(sz + sizeof(END));
325                 if (!p)
326                         return NULL;
327                 memcpy(p, node, sz);
328                 memcpy(p + sz, &END, sizeof(END));
329                 ret = p;
330         } else {
331                 /* both dp and node are non-null */
332                 size_t sz = efi_dp_size(dp);
333                 void *p = dp_alloc(sz + node->length + sizeof(END));
334                 if (!p)
335                         return NULL;
336                 memcpy(p, dp, sz);
337                 memcpy(p + sz, node, node->length);
338                 memcpy(p + sz + node->length, &END, sizeof(END));
339                 ret = p;
340         }
341
342         return ret;
343 }
344
345 struct efi_device_path *efi_dp_create_device_node(const u8 type,
346                                                   const u8 sub_type,
347                                                   const u16 length)
348 {
349         struct efi_device_path *ret;
350
351         if (length < sizeof(struct efi_device_path))
352                 return NULL;
353
354         ret = dp_alloc(length);
355         if (!ret)
356                 return ret;
357         ret->type = type;
358         ret->sub_type = sub_type;
359         ret->length = length;
360         return ret;
361 }
362
363 struct efi_device_path *efi_dp_append_instance(
364                 const struct efi_device_path *dp,
365                 const struct efi_device_path *dpi)
366 {
367         size_t sz, szi;
368         struct efi_device_path *p, *ret;
369
370         if (!dpi)
371                 return NULL;
372         if (!dp)
373                 return efi_dp_dup(dpi);
374         sz = efi_dp_size(dp);
375         szi = efi_dp_instance_size(dpi);
376         p = dp_alloc(sz + szi + 2 * sizeof(END));
377         if (!p)
378                 return NULL;
379         ret = p;
380         memcpy(p, dp, sz + sizeof(END));
381         p = (void *)p + sz;
382         p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
383         p = (void *)p + sizeof(END);
384         memcpy(p, dpi, szi);
385         p = (void *)p + szi;
386         memcpy(p, &END, sizeof(END));
387         return ret;
388 }
389
390 struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
391                                                  efi_uintn_t *size)
392 {
393         size_t sz;
394         struct efi_device_path *p;
395
396         if (size)
397                 *size = 0;
398         if (!dp || !*dp)
399                 return NULL;
400         sz = efi_dp_instance_size(*dp);
401         p = dp_alloc(sz + sizeof(END));
402         if (!p)
403                 return NULL;
404         memcpy(p, *dp, sz + sizeof(END));
405         *dp = (void *)*dp + sz;
406         if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
407                 *dp = (void *)*dp + sizeof(END);
408         else
409                 *dp = NULL;
410         if (size)
411                 *size = sz + sizeof(END);
412         return p;
413 }
414
415 bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
416 {
417         const struct efi_device_path *p = dp;
418
419         if (!p)
420                 return false;
421         while (p->type != DEVICE_PATH_TYPE_END)
422                 p = (void *)p + p->length;
423         return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
424 }
425
426 #ifdef CONFIG_DM
427 /* size of device-path not including END node for device and all parents
428  * up to the root device.
429  */
430 __maybe_unused static unsigned int dp_size(struct udevice *dev)
431 {
432         if (!dev || !dev->driver)
433                 return sizeof(ROOT);
434
435         switch (dev->driver->id) {
436         case UCLASS_ROOT:
437         case UCLASS_SIMPLE_BUS:
438                 /* stop traversing parents at this point: */
439                 return sizeof(ROOT);
440         case UCLASS_ETH:
441                 return dp_size(dev->parent) +
442                         sizeof(struct efi_device_path_mac_addr);
443 #ifdef CONFIG_BLK
444         case UCLASS_BLK:
445                 switch (dev->parent->uclass->uc_drv->id) {
446 #ifdef CONFIG_IDE
447                 case UCLASS_IDE:
448                         return dp_size(dev->parent) +
449                                 sizeof(struct efi_device_path_atapi);
450 #endif
451 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
452                 case UCLASS_SCSI:
453                         return dp_size(dev->parent) +
454                                 sizeof(struct efi_device_path_scsi);
455 #endif
456 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
457                 case UCLASS_MMC:
458                         return dp_size(dev->parent) +
459                                 sizeof(struct efi_device_path_sd_mmc_path);
460 #endif
461 #if defined(CONFIG_NVME)
462                 case UCLASS_NVME:
463                         return dp_size(dev->parent) +
464                                 sizeof(struct efi_device_path_nvme);
465 #endif
466 #ifdef CONFIG_SANDBOX
467                 case UCLASS_ROOT:
468                          /*
469                           * Sandbox's host device will be represented
470                           * as vendor device with extra one byte for
471                           * device number
472                           */
473                         return dp_size(dev->parent)
474                                 + sizeof(struct efi_device_path_vendor) + 1;
475 #endif
476 #ifdef CONFIG_VIRTIO_BLK
477                 case UCLASS_VIRTIO:
478                          /*
479                           * Virtio devices will be represented as a vendor
480                           * device node with an extra byte for the device
481                           * number.
482                           */
483                         return dp_size(dev->parent)
484                                 + sizeof(struct efi_device_path_vendor) + 1;
485 #endif
486                 default:
487                         return dp_size(dev->parent);
488                 }
489 #endif
490 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
491         case UCLASS_MMC:
492                 return dp_size(dev->parent) +
493                         sizeof(struct efi_device_path_sd_mmc_path);
494 #endif
495         case UCLASS_MASS_STORAGE:
496         case UCLASS_USB_HUB:
497                 return dp_size(dev->parent) +
498                         sizeof(struct efi_device_path_usb_class);
499         default:
500                 /* just skip over unknown classes: */
501                 return dp_size(dev->parent);
502         }
503 }
504
505 /*
506  * Recursively build a device path.
507  *
508  * @buf         pointer to the end of the device path
509  * @dev         device
510  * @return      pointer to the end of the device path
511  */
512 __maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
513 {
514         if (!dev || !dev->driver)
515                 return buf;
516
517         switch (dev->driver->id) {
518         case UCLASS_ROOT:
519         case UCLASS_SIMPLE_BUS: {
520                 /* stop traversing parents at this point: */
521                 struct efi_device_path_vendor *vdp = buf;
522                 *vdp = ROOT;
523                 return &vdp[1];
524         }
525 #ifdef CONFIG_DM_ETH
526         case UCLASS_ETH: {
527                 struct efi_device_path_mac_addr *dp =
528                         dp_fill(buf, dev->parent);
529                 struct eth_pdata *pdata = dev->platdata;
530
531                 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
532                 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
533                 dp->dp.length = sizeof(*dp);
534                 memset(&dp->mac, 0, sizeof(dp->mac));
535                 /* We only support IPv4 */
536                 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
537                 /* Ethernet */
538                 dp->if_type = 1;
539                 return &dp[1];
540         }
541 #endif
542 #ifdef CONFIG_BLK
543         case UCLASS_BLK:
544                 switch (dev->parent->uclass->uc_drv->id) {
545 #ifdef CONFIG_SANDBOX
546                 case UCLASS_ROOT: {
547                         /* stop traversing parents at this point: */
548                         struct efi_device_path_vendor *dp;
549                         struct blk_desc *desc = dev_get_uclass_platdata(dev);
550
551                         dp_fill(buf, dev->parent);
552                         dp = buf;
553                         ++dp;
554                         dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
555                         dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
556                         dp->dp.length = sizeof(*dp) + 1;
557                         memcpy(&dp->guid, &efi_guid_host_dev,
558                                sizeof(efi_guid_t));
559                         dp->vendor_data[0] = desc->devnum;
560                         return &dp->vendor_data[1];
561                         }
562 #endif
563 #ifdef CONFIG_VIRTIO_BLK
564                 case UCLASS_VIRTIO: {
565                         struct efi_device_path_vendor *dp;
566                         struct blk_desc *desc = dev_get_uclass_platdata(dev);
567
568                         dp_fill(buf, dev->parent);
569                         dp = buf;
570                         ++dp;
571                         dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
572                         dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
573                         dp->dp.length = sizeof(*dp) + 1;
574                         memcpy(&dp->guid, &efi_guid_virtio_dev,
575                                sizeof(efi_guid_t));
576                         dp->vendor_data[0] = desc->devnum;
577                         return &dp->vendor_data[1];
578                         }
579 #endif
580 #ifdef CONFIG_IDE
581                 case UCLASS_IDE: {
582                         struct efi_device_path_atapi *dp =
583                         dp_fill(buf, dev->parent);
584                         struct blk_desc *desc = dev_get_uclass_platdata(dev);
585
586                         dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
587                         dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
588                         dp->dp.length = sizeof(*dp);
589                         dp->logical_unit_number = desc->devnum;
590                         dp->primary_secondary = IDE_BUS(desc->devnum);
591                         dp->slave_master = desc->devnum %
592                                 (CONFIG_SYS_IDE_MAXDEVICE /
593                                  CONFIG_SYS_IDE_MAXBUS);
594                         return &dp[1];
595                         }
596 #endif
597 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
598                 case UCLASS_SCSI: {
599                         struct efi_device_path_scsi *dp =
600                                 dp_fill(buf, dev->parent);
601                         struct blk_desc *desc = dev_get_uclass_platdata(dev);
602
603                         dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
604                         dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
605                         dp->dp.length = sizeof(*dp);
606                         dp->logical_unit_number = desc->lun;
607                         dp->target_id = desc->target;
608                         return &dp[1];
609                         }
610 #endif
611 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
612                 case UCLASS_MMC: {
613                         struct efi_device_path_sd_mmc_path *sddp =
614                                 dp_fill(buf, dev->parent);
615                         struct blk_desc *desc = dev_get_uclass_platdata(dev);
616
617                         sddp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
618                         sddp->dp.sub_type = is_sd(desc) ?
619                                 DEVICE_PATH_SUB_TYPE_MSG_SD :
620                                 DEVICE_PATH_SUB_TYPE_MSG_MMC;
621                         sddp->dp.length   = sizeof(*sddp);
622                         sddp->slot_number = dev->seq;
623                         return &sddp[1];
624                         }
625 #endif
626 #if defined(CONFIG_NVME)
627                 case UCLASS_NVME: {
628                         struct efi_device_path_nvme *dp =
629                                 dp_fill(buf, dev->parent);
630                         u32 ns_id;
631
632                         dp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
633                         dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
634                         dp->dp.length   = sizeof(*dp);
635                         nvme_get_namespace_id(dev, &ns_id, dp->eui64);
636                         memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
637                         return &dp[1];
638                         }
639 #endif
640                 default:
641                         debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
642                               __FILE__, __LINE__, __func__,
643                               dev->name, dev->parent->uclass->uc_drv->id);
644                         return dp_fill(buf, dev->parent);
645                 }
646 #endif
647 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
648         case UCLASS_MMC: {
649                 struct efi_device_path_sd_mmc_path *sddp =
650                         dp_fill(buf, dev->parent);
651                 struct mmc *mmc = mmc_get_mmc_dev(dev);
652                 struct blk_desc *desc = mmc_get_blk_desc(mmc);
653
654                 sddp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
655                 sddp->dp.sub_type = is_sd(desc) ?
656                         DEVICE_PATH_SUB_TYPE_MSG_SD :
657                         DEVICE_PATH_SUB_TYPE_MSG_MMC;
658                 sddp->dp.length   = sizeof(*sddp);
659                 sddp->slot_number = dev->seq;
660
661                 return &sddp[1];
662         }
663 #endif
664         case UCLASS_MASS_STORAGE:
665         case UCLASS_USB_HUB: {
666                 struct efi_device_path_usb_class *udp =
667                         dp_fill(buf, dev->parent);
668                 struct usb_device *udev = dev_get_parent_priv(dev);
669                 struct usb_device_descriptor *desc = &udev->descriptor;
670
671                 udp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
672                 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
673                 udp->dp.length   = sizeof(*udp);
674                 udp->vendor_id   = desc->idVendor;
675                 udp->product_id  = desc->idProduct;
676                 udp->device_class    = desc->bDeviceClass;
677                 udp->device_subclass = desc->bDeviceSubClass;
678                 udp->device_protocol = desc->bDeviceProtocol;
679
680                 return &udp[1];
681         }
682         default:
683                 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
684                       __FILE__, __LINE__, __func__,
685                       dev->name, dev->driver->id);
686                 return dp_fill(buf, dev->parent);
687         }
688 }
689 #endif
690
691 static unsigned dp_part_size(struct blk_desc *desc, int part)
692 {
693         unsigned dpsize;
694
695 #ifdef CONFIG_BLK
696         {
697                 struct udevice *dev;
698                 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
699
700                 if (ret)
701                         dev = desc->bdev->parent;
702                 dpsize = dp_size(dev);
703         }
704 #else
705         dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
706 #endif
707
708         if (part == 0) /* the actual disk, not a partition */
709                 return dpsize;
710
711         if (desc->part_type == PART_TYPE_ISO)
712                 dpsize += sizeof(struct efi_device_path_cdrom_path);
713         else
714                 dpsize += sizeof(struct efi_device_path_hard_drive_path);
715
716         return dpsize;
717 }
718
719 /*
720  * Create a device node for a block device partition.
721  *
722  * @buf         buffer to which the device path is written
723  * @desc        block device descriptor
724  * @part        partition number, 0 identifies a block device
725  */
726 static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
727 {
728         struct disk_partition info;
729
730         part_get_info(desc, part, &info);
731
732         if (desc->part_type == PART_TYPE_ISO) {
733                 struct efi_device_path_cdrom_path *cddp = buf;
734
735                 cddp->boot_entry = part;
736                 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
737                 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
738                 cddp->dp.length = sizeof(*cddp);
739                 cddp->partition_start = info.start;
740                 cddp->partition_size = info.size;
741
742                 buf = &cddp[1];
743         } else {
744                 struct efi_device_path_hard_drive_path *hddp = buf;
745
746                 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
747                 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
748                 hddp->dp.length = sizeof(*hddp);
749                 hddp->partition_number = part;
750                 hddp->partition_start = info.start;
751                 hddp->partition_end = info.size;
752                 if (desc->part_type == PART_TYPE_EFI)
753                         hddp->partmap_type = 2;
754                 else
755                         hddp->partmap_type = 1;
756
757                 switch (desc->sig_type) {
758                 case SIG_TYPE_NONE:
759                 default:
760                         hddp->signature_type = 0;
761                         memset(hddp->partition_signature, 0,
762                                sizeof(hddp->partition_signature));
763                         break;
764                 case SIG_TYPE_MBR:
765                         hddp->signature_type = 1;
766                         memset(hddp->partition_signature, 0,
767                                sizeof(hddp->partition_signature));
768                         memcpy(hddp->partition_signature, &desc->mbr_sig,
769                                sizeof(desc->mbr_sig));
770                         break;
771                 case SIG_TYPE_GUID:
772                         hddp->signature_type = 2;
773                         memcpy(hddp->partition_signature, &desc->guid_sig,
774                                sizeof(hddp->partition_signature));
775                         break;
776                 }
777
778                 buf = &hddp[1];
779         }
780
781         return buf;
782 }
783
784 /*
785  * Create a device path for a block device or one of its partitions.
786  *
787  * @buf         buffer to which the device path is written
788  * @desc        block device descriptor
789  * @part        partition number, 0 identifies a block device
790  */
791 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
792 {
793 #ifdef CONFIG_BLK
794         {
795                 struct udevice *dev;
796                 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
797
798                 if (ret)
799                         dev = desc->bdev->parent;
800                 buf = dp_fill(buf, dev);
801         }
802 #else
803         /*
804          * We *could* make a more accurate path, by looking at if_type
805          * and handling all the different cases like we do for non-
806          * legacy (i.e. CONFIG_BLK=y) case. But most important thing
807          * is just to have a unique device-path for if_type+devnum.
808          * So map things to a fictitious USB device.
809          */
810         struct efi_device_path_usb *udp;
811
812         memcpy(buf, &ROOT, sizeof(ROOT));
813         buf += sizeof(ROOT);
814
815         udp = buf;
816         udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
817         udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
818         udp->dp.length = sizeof(*udp);
819         udp->parent_port_number = desc->if_type;
820         udp->usb_interface = desc->devnum;
821         buf = &udp[1];
822 #endif
823
824         if (part == 0) /* the actual disk, not a partition */
825                 return buf;
826
827         return dp_part_node(buf, desc, part);
828 }
829
830 /* Construct a device-path from a partition on a block device: */
831 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
832 {
833         void *buf, *start;
834
835         start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
836         if (!buf)
837                 return NULL;
838
839         buf = dp_part_fill(buf, desc, part);
840
841         *((struct efi_device_path *)buf) = END;
842
843         return start;
844 }
845
846 /*
847  * Create a device node for a block device partition.
848  *
849  * @buf         buffer to which the device path is written
850  * @desc        block device descriptor
851  * @part        partition number, 0 identifies a block device
852  */
853 struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
854 {
855         efi_uintn_t dpsize;
856         void *buf;
857
858         if (desc->part_type == PART_TYPE_ISO)
859                 dpsize = sizeof(struct efi_device_path_cdrom_path);
860         else
861                 dpsize = sizeof(struct efi_device_path_hard_drive_path);
862         buf = dp_alloc(dpsize);
863
864         dp_part_node(buf, desc, part);
865
866         return buf;
867 }
868
869 /**
870  * path_to_uefi() - convert UTF-8 path to an UEFI style path
871  *
872  * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
873  * separators and UTF-16).
874  *
875  * @src:        source buffer
876  * @uefi:       target buffer, possibly unaligned
877  */
878 static void path_to_uefi(void *uefi, const char *src)
879 {
880         u16 *pos = uefi;
881
882         /*
883          * efi_set_bootdev() calls this routine indirectly before the UEFI
884          * subsystem is initialized. So we cannot assume unaligned access to be
885          * enabled.
886          */
887         allow_unaligned();
888
889         while (*src) {
890                 s32 code = utf8_get(&src);
891
892                 if (code < 0)
893                         code = '?';
894                 else if (code == '/')
895                         code = '\\';
896                 utf16_put(code, &pos);
897         }
898         *pos = 0;
899 }
900
901 /*
902  * If desc is NULL, this creates a path with only the file component,
903  * otherwise it creates a full path with both device and file components
904  */
905 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
906                 const char *path)
907 {
908         struct efi_device_path_file_path *fp;
909         void *buf, *start;
910         size_t dpsize = 0, fpsize;
911
912         if (desc)
913                 dpsize = dp_part_size(desc, part);
914
915         fpsize = sizeof(struct efi_device_path) +
916                  2 * (utf8_utf16_strlen(path) + 1);
917         if (fpsize > U16_MAX)
918                 return NULL;
919
920         dpsize += fpsize;
921
922         start = buf = dp_alloc(dpsize + sizeof(END));
923         if (!buf)
924                 return NULL;
925
926         if (desc)
927                 buf = dp_part_fill(buf, desc, part);
928
929         /* add file-path: */
930         fp = buf;
931         fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
932         fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
933         fp->dp.length = (u16)fpsize;
934         path_to_uefi(fp->str, path);
935         buf += fpsize;
936
937         *((struct efi_device_path *)buf) = END;
938
939         return start;
940 }
941
942 #ifdef CONFIG_NET
943 struct efi_device_path *efi_dp_from_eth(void)
944 {
945 #ifndef CONFIG_DM_ETH
946         struct efi_device_path_mac_addr *ndp;
947 #endif
948         void *buf, *start;
949         unsigned dpsize = 0;
950
951         assert(eth_get_dev());
952
953 #ifdef CONFIG_DM_ETH
954         dpsize += dp_size(eth_get_dev());
955 #else
956         dpsize += sizeof(ROOT);
957         dpsize += sizeof(*ndp);
958 #endif
959
960         start = buf = dp_alloc(dpsize + sizeof(END));
961         if (!buf)
962                 return NULL;
963
964 #ifdef CONFIG_DM_ETH
965         buf = dp_fill(buf, eth_get_dev());
966 #else
967         memcpy(buf, &ROOT, sizeof(ROOT));
968         buf += sizeof(ROOT);
969
970         ndp = buf;
971         ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
972         ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
973         ndp->dp.length = sizeof(*ndp);
974         ndp->if_type = 1; /* Ethernet */
975         memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
976         buf = &ndp[1];
977 #endif
978
979         *((struct efi_device_path *)buf) = END;
980
981         return start;
982 }
983 #endif
984
985 /* Construct a device-path for memory-mapped image */
986 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
987                                         uint64_t start_address,
988                                         uint64_t end_address)
989 {
990         struct efi_device_path_memory *mdp;
991         void *buf, *start;
992
993         start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
994         if (!buf)
995                 return NULL;
996
997         mdp = buf;
998         mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
999         mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1000         mdp->dp.length = sizeof(*mdp);
1001         mdp->memory_type = memory_type;
1002         mdp->start_address = start_address;
1003         mdp->end_address = end_address;
1004         buf = &mdp[1];
1005
1006         *((struct efi_device_path *)buf) = END;
1007
1008         return start;
1009 }
1010
1011 /**
1012  * efi_dp_split_file_path() - split of relative file path from device path
1013  *
1014  * Given a device path indicating a file on a device, separate the device
1015  * path in two: the device path of the actual device and the file path
1016  * relative to this device.
1017  *
1018  * @full_path:          device path including device and file path
1019  * @device_path:        path of the device
1020  * @file_path:          relative path of the file or NULL if there is none
1021  * Return:              status code
1022  */
1023 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1024                                     struct efi_device_path **device_path,
1025                                     struct efi_device_path **file_path)
1026 {
1027         struct efi_device_path *p, *dp, *fp = NULL;
1028
1029         *device_path = NULL;
1030         *file_path = NULL;
1031         dp = efi_dp_dup(full_path);
1032         if (!dp)
1033                 return EFI_OUT_OF_RESOURCES;
1034         p = dp;
1035         while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
1036                 p = efi_dp_next(p);
1037                 if (!p)
1038                         goto out;
1039         }
1040         fp = efi_dp_dup(p);
1041         if (!fp)
1042                 return EFI_OUT_OF_RESOURCES;
1043         p->type = DEVICE_PATH_TYPE_END;
1044         p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1045         p->length = sizeof(*p);
1046
1047 out:
1048         *device_path = dp;
1049         *file_path = fp;
1050         return EFI_SUCCESS;
1051 }
1052
1053 /**
1054  * efi_dp_from_name() - convert U-Boot device and file path to device path
1055  *
1056  * @dev:        U-Boot device, e.g. 'mmc'
1057  * @devnr:      U-Boot device number, e.g. 1 for 'mmc:1'
1058  * @path:       file path relative to U-Boot device, may be NULL
1059  * @device:     pointer to receive device path of the device
1060  * @file:       pointer to receive device path for the file
1061  * Return:      status code
1062  */
1063 efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1064                               const char *path,
1065                               struct efi_device_path **device,
1066                               struct efi_device_path **file)
1067 {
1068         int is_net;
1069         struct blk_desc *desc = NULL;
1070         struct disk_partition fs_partition;
1071         int part = 0;
1072         char filename[32] = { 0 }; /* dp->str is u16[32] long */
1073         char *s;
1074
1075         if (path && !file)
1076                 return EFI_INVALID_PARAMETER;
1077
1078         is_net = !strcmp(dev, "Net");
1079         if (!is_net) {
1080                 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1081                                                1);
1082                 if (part < 0 || !desc)
1083                         return EFI_INVALID_PARAMETER;
1084
1085                 if (device)
1086                         *device = efi_dp_from_part(desc, part);
1087         } else {
1088 #ifdef CONFIG_NET
1089                 if (device)
1090                         *device = efi_dp_from_eth();
1091 #endif
1092         }
1093
1094         if (!path)
1095                 return EFI_SUCCESS;
1096
1097         snprintf(filename, sizeof(filename), "%s", path);
1098         /* DOS style file path: */
1099         s = filename;
1100         while ((s = strchr(s, '/')))
1101                 *s++ = '\\';
1102         *file = efi_dp_from_file(is_net ? NULL : desc, part, filename);
1103
1104         if (!*file)
1105                 return EFI_INVALID_PARAMETER;
1106
1107         return EFI_SUCCESS;
1108 }