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