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