]> git.sur5r.net Git - u-boot/blob - lib/efi_loader/efi_device_path.c
24a4f40c004dd2f8890d76633d7c983ff3197545
[u-boot] / lib / efi_loader / efi_device_path.c
1 /*
2  * EFI device path from u-boot device-model mapping
3  *
4  * (C) Copyright 2017 Rob Clark
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #include <common.h>
10 #include <blk.h>
11 #include <dm.h>
12 #include <usb.h>
13 #include <mmc.h>
14 #include <efi_loader.h>
15 #include <inttypes.h>
16 #include <part.h>
17
18 /* template END node: */
19 static const struct efi_device_path END = {
20         .type     = DEVICE_PATH_TYPE_END,
21         .sub_type = DEVICE_PATH_SUB_TYPE_END,
22         .length   = sizeof(END),
23 };
24
25 #define U_BOOT_GUID \
26         EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
27                  0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b)
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 static void *dp_alloc(size_t sz)
40 {
41         void *buf;
42
43         if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) != EFI_SUCCESS)
44                 return NULL;
45
46         return buf;
47 }
48
49 /*
50  * Iterate to next block in device-path, terminating (returning NULL)
51  * at /End* node.
52  */
53 struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
54 {
55         if (dp == NULL)
56                 return NULL;
57         if (dp->type == DEVICE_PATH_TYPE_END)
58                 return NULL;
59         dp = ((void *)dp) + dp->length;
60         if (dp->type == DEVICE_PATH_TYPE_END)
61                 return NULL;
62         return (struct efi_device_path *)dp;
63 }
64
65 /*
66  * Compare two device-paths, stopping when the shorter of the two hits
67  * an End* node.  This is useful to, for example, compare a device-path
68  * representing a device with one representing a file on the device, or
69  * a device with a parent device.
70  */
71 int efi_dp_match(const struct efi_device_path *a,
72                  const struct efi_device_path *b)
73 {
74         while (1) {
75                 int ret;
76
77                 ret = memcmp(&a->length, &b->length, sizeof(a->length));
78                 if (ret)
79                         return ret;
80
81                 ret = memcmp(a, b, a->length);
82                 if (ret)
83                         return ret;
84
85                 a = efi_dp_next(a);
86                 b = efi_dp_next(b);
87
88                 if (!a || !b)
89                         return 0;
90         }
91 }
92
93
94 /*
95  * See UEFI spec (section 3.1.2, about short-form device-paths..
96  * tl;dr: we can have a device-path that starts with a USB WWID
97  * or USB Class node, and a few other cases which don't encode
98  * the full device path with bus hierarchy:
99  *
100  *   - MESSAGING:USB_WWID
101  *   - MESSAGING:USB_CLASS
102  *   - MEDIA:FILE_PATH
103  *   - MEDIA:HARD_DRIVE
104  *   - MESSAGING:URI
105  */
106 static struct efi_device_path *shorten_path(struct efi_device_path *dp)
107 {
108         while (dp) {
109                 /*
110                  * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
111                  * in practice fallback.efi just uses MEDIA:HARD_DRIVE
112                  * so not sure when we would see these other cases.
113                  */
114                 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
115                     EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
116                     EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
117                         return dp;
118
119                 dp = efi_dp_next(dp);
120         }
121
122         return dp;
123 }
124
125 static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
126                                    struct efi_device_path **rem)
127 {
128         struct efi_object *efiobj;
129         unsigned int dp_size = efi_dp_size(dp);
130
131         list_for_each_entry(efiobj, &efi_obj_list, link) {
132                 struct efi_handler *handler;
133                 struct efi_device_path *obj_dp;
134                 efi_status_t ret;
135
136                 ret = efi_search_protocol(efiobj->handle,
137                                           &efi_guid_device_path, &handler);
138                 if (ret != EFI_SUCCESS)
139                         continue;
140                 obj_dp = handler->protocol_interface;
141
142                 do {
143                         if (efi_dp_match(dp, obj_dp) == 0) {
144                                 if (rem) {
145                                         /*
146                                          * Allow partial matches, but inform
147                                          * the caller.
148                                          */
149                                         *rem = ((void *)dp) +
150                                                 efi_dp_size(obj_dp);
151                                         return efiobj;
152                                 } else {
153                                         /* Only return on exact matches */
154                                         if (efi_dp_size(obj_dp) == dp_size)
155                                                 return efiobj;
156                                 }
157                         }
158
159                         obj_dp = shorten_path(efi_dp_next(obj_dp));
160                 } while (short_path && obj_dp);
161         }
162
163         return NULL;
164 }
165
166
167 /*
168  * Find an efiobj from device-path, if 'rem' is not NULL, returns the
169  * remaining part of the device path after the matched object.
170  */
171 struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
172                                    struct efi_device_path **rem)
173 {
174         struct efi_object *efiobj;
175
176         /* Search for an exact match first */
177         efiobj = find_obj(dp, false, NULL);
178
179         /* Then for a fuzzy match */
180         if (!efiobj)
181                 efiobj = find_obj(dp, false, rem);
182
183         /* And now for a fuzzy short match */
184         if (!efiobj)
185                 efiobj = find_obj(dp, true, rem);
186
187         return efiobj;
188 }
189
190 /* return size not including End node: */
191 unsigned efi_dp_size(const struct efi_device_path *dp)
192 {
193         unsigned sz = 0;
194
195         while (dp) {
196                 sz += dp->length;
197                 dp = efi_dp_next(dp);
198         }
199
200         return sz;
201 }
202
203 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
204 {
205         struct efi_device_path *ndp;
206         unsigned sz = efi_dp_size(dp) + sizeof(END);
207
208         if (!dp)
209                 return NULL;
210
211         ndp = dp_alloc(sz);
212         memcpy(ndp, dp, sz);
213
214         return ndp;
215 }
216
217 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
218                                       const struct efi_device_path *dp2)
219 {
220         struct efi_device_path *ret;
221
222         if (!dp1) {
223                 ret = efi_dp_dup(dp2);
224         } else if (!dp2) {
225                 ret = efi_dp_dup(dp1);
226         } else {
227                 /* both dp1 and dp2 are non-null */
228                 unsigned sz1 = efi_dp_size(dp1);
229                 unsigned sz2 = efi_dp_size(dp2);
230                 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
231                 memcpy(p, dp1, sz1);
232                 memcpy(p + sz1, dp2, sz2);
233                 memcpy(p + sz1 + sz2, &END, sizeof(END));
234                 ret = p;
235         }
236
237         return ret;
238 }
239
240 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
241                                            const struct efi_device_path *node)
242 {
243         struct efi_device_path *ret;
244
245         if (!node && !dp) {
246                 ret = efi_dp_dup(&END);
247         } else if (!node) {
248                 ret = efi_dp_dup(dp);
249         } else if (!dp) {
250                 unsigned sz = node->length;
251                 void *p = dp_alloc(sz + sizeof(END));
252                 memcpy(p, node, sz);
253                 memcpy(p + sz, &END, sizeof(END));
254                 ret = p;
255         } else {
256                 /* both dp and node are non-null */
257                 unsigned sz = efi_dp_size(dp);
258                 void *p = dp_alloc(sz + node->length + sizeof(END));
259                 memcpy(p, dp, sz);
260                 memcpy(p + sz, node, node->length);
261                 memcpy(p + sz + node->length, &END, sizeof(END));
262                 ret = p;
263         }
264
265         return ret;
266 }
267
268 #ifdef CONFIG_DM
269 /* size of device-path not including END node for device and all parents
270  * up to the root device.
271  */
272 static unsigned dp_size(struct udevice *dev)
273 {
274         if (!dev || !dev->driver)
275                 return sizeof(ROOT);
276
277         switch (dev->driver->id) {
278         case UCLASS_ROOT:
279         case UCLASS_SIMPLE_BUS:
280                 /* stop traversing parents at this point: */
281                 return sizeof(ROOT);
282         case UCLASS_MMC:
283                 return dp_size(dev->parent) +
284                         sizeof(struct efi_device_path_sd_mmc_path);
285         case UCLASS_MASS_STORAGE:
286         case UCLASS_USB_HUB:
287                 return dp_size(dev->parent) +
288                         sizeof(struct efi_device_path_usb_class);
289         default:
290                 /* just skip over unknown classes: */
291                 return dp_size(dev->parent);
292         }
293 }
294
295 static void *dp_fill(void *buf, struct udevice *dev)
296 {
297         if (!dev || !dev->driver)
298                 return buf;
299
300         switch (dev->driver->id) {
301         case UCLASS_ROOT:
302         case UCLASS_SIMPLE_BUS: {
303                 /* stop traversing parents at this point: */
304                 struct efi_device_path_vendor *vdp = buf;
305                 *vdp = ROOT;
306                 return &vdp[1];
307         }
308 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
309         case UCLASS_MMC: {
310                 struct efi_device_path_sd_mmc_path *sddp =
311                         dp_fill(buf, dev->parent);
312                 struct mmc *mmc = mmc_get_mmc_dev(dev);
313                 struct blk_desc *desc = mmc_get_blk_desc(mmc);
314
315                 sddp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
316                 sddp->dp.sub_type = (desc->if_type == IF_TYPE_MMC) ?
317                         DEVICE_PATH_SUB_TYPE_MSG_MMC :
318                         DEVICE_PATH_SUB_TYPE_MSG_SD;
319                 sddp->dp.length   = sizeof(*sddp);
320                 sddp->slot_number = dev->seq;
321
322                 return &sddp[1];
323         }
324 #endif
325         case UCLASS_MASS_STORAGE:
326         case UCLASS_USB_HUB: {
327                 struct efi_device_path_usb_class *udp =
328                         dp_fill(buf, dev->parent);
329                 struct usb_device *udev = dev_get_parent_priv(dev);
330                 struct usb_device_descriptor *desc = &udev->descriptor;
331
332                 udp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
333                 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
334                 udp->dp.length   = sizeof(*udp);
335                 udp->vendor_id   = desc->idVendor;
336                 udp->product_id  = desc->idProduct;
337                 udp->device_class    = desc->bDeviceClass;
338                 udp->device_subclass = desc->bDeviceSubClass;
339                 udp->device_protocol = desc->bDeviceProtocol;
340
341                 return &udp[1];
342         }
343         default:
344                 debug("unhandled device class: %s (%u)\n",
345                       dev->name, dev->driver->id);
346                 return dp_fill(buf, dev->parent);
347         }
348 }
349
350 /* Construct a device-path from a device: */
351 struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
352 {
353         void *buf, *start;
354
355         start = buf = dp_alloc(dp_size(dev) + sizeof(END));
356         buf = dp_fill(buf, dev);
357         *((struct efi_device_path *)buf) = END;
358
359         return start;
360 }
361 #endif
362
363 static unsigned dp_part_size(struct blk_desc *desc, int part)
364 {
365         unsigned dpsize;
366
367 #ifdef CONFIG_BLK
368         dpsize = dp_size(desc->bdev->parent);
369 #else
370         dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
371 #endif
372
373         if (part == 0) /* the actual disk, not a partition */
374                 return dpsize;
375
376         if (desc->part_type == PART_TYPE_ISO)
377                 dpsize += sizeof(struct efi_device_path_cdrom_path);
378         else
379                 dpsize += sizeof(struct efi_device_path_hard_drive_path);
380
381         return dpsize;
382 }
383
384 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
385 {
386         disk_partition_t info;
387
388 #ifdef CONFIG_BLK
389         buf = dp_fill(buf, desc->bdev->parent);
390 #else
391         /*
392          * We *could* make a more accurate path, by looking at if_type
393          * and handling all the different cases like we do for non-
394          * legacy (ie CONFIG_BLK=y) case.  But most important thing
395          * is just to have a unique device-path for if_type+devnum.
396          * So map things to a fictional USB device:
397          */
398         struct efi_device_path_usb *udp;
399
400         memcpy(buf, &ROOT, sizeof(ROOT));
401         buf += sizeof(ROOT);
402
403         udp = buf;
404         udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
405         udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
406         udp->dp.length = sizeof(*udp);
407         udp->parent_port_number = desc->if_type;
408         udp->usb_interface = desc->devnum;
409         buf = &udp[1];
410 #endif
411
412         if (part == 0) /* the actual disk, not a partition */
413                 return buf;
414
415         part_get_info(desc, part, &info);
416
417         if (desc->part_type == PART_TYPE_ISO) {
418                 struct efi_device_path_cdrom_path *cddp = buf;
419
420                 cddp->boot_entry = part - 1;
421                 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
422                 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
423                 cddp->dp.length = sizeof(*cddp);
424                 cddp->partition_start = info.start;
425                 cddp->partition_end = info.size;
426
427                 buf = &cddp[1];
428         } else {
429                 struct efi_device_path_hard_drive_path *hddp = buf;
430
431                 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
432                 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
433                 hddp->dp.length = sizeof(*hddp);
434                 hddp->partition_number = part - 1;
435                 hddp->partition_start = info.start;
436                 hddp->partition_end = info.size;
437                 if (desc->part_type == PART_TYPE_EFI)
438                         hddp->partmap_type = 2;
439                 else
440                         hddp->partmap_type = 1;
441
442                 switch (desc->sig_type) {
443                 case SIG_TYPE_NONE:
444                 default:
445                         hddp->signature_type = 0;
446                         memset(hddp->partition_signature, 0,
447                                sizeof(hddp->partition_signature));
448                         break;
449                 case SIG_TYPE_MBR:
450                         hddp->signature_type = 1;
451                         memset(hddp->partition_signature, 0,
452                                sizeof(hddp->partition_signature));
453                         memcpy(hddp->partition_signature, &desc->mbr_sig,
454                                sizeof(desc->mbr_sig));
455                         break;
456                 case SIG_TYPE_GUID:
457                         hddp->signature_type = 2;
458                         memcpy(hddp->partition_signature, &desc->guid_sig,
459                                sizeof(hddp->partition_signature));
460                         break;
461                 }
462
463                 buf = &hddp[1];
464         }
465
466         return buf;
467 }
468
469
470 /* Construct a device-path from a partition on a blk device: */
471 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
472 {
473         void *buf, *start;
474
475         start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
476
477         buf = dp_part_fill(buf, desc, part);
478
479         *((struct efi_device_path *)buf) = END;
480
481         return start;
482 }
483
484 /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
485 static void path_to_uefi(u16 *uefi, const char *path)
486 {
487         while (*path) {
488                 char c = *(path++);
489                 if (c == '/')
490                         c = '\\';
491                 *(uefi++) = c;
492         }
493         *uefi = '\0';
494 }
495
496 /*
497  * If desc is NULL, this creates a path with only the file component,
498  * otherwise it creates a full path with both device and file components
499  */
500 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
501                 const char *path)
502 {
503         struct efi_device_path_file_path *fp;
504         void *buf, *start;
505         unsigned dpsize = 0, fpsize;
506
507         if (desc)
508                 dpsize = dp_part_size(desc, part);
509
510         fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
511         dpsize += fpsize;
512
513         start = buf = dp_alloc(dpsize + sizeof(END));
514
515         if (desc)
516                 buf = dp_part_fill(buf, desc, part);
517
518         /* add file-path: */
519         fp = buf;
520         fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
521         fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
522         fp->dp.length = fpsize;
523         path_to_uefi(fp->str, path);
524         buf += fpsize;
525
526         *((struct efi_device_path *)buf) = END;
527
528         return start;
529 }
530
531 #ifdef CONFIG_NET
532 struct efi_device_path *efi_dp_from_eth(void)
533 {
534         struct efi_device_path_mac_addr *ndp;
535         void *buf, *start;
536         unsigned dpsize = 0;
537
538         assert(eth_get_dev());
539
540 #ifdef CONFIG_DM_ETH
541         dpsize += dp_size(eth_get_dev());
542 #else
543         dpsize += sizeof(ROOT);
544 #endif
545         dpsize += sizeof(*ndp);
546
547         start = buf = dp_alloc(dpsize + sizeof(END));
548
549 #ifdef CONFIG_DM_ETH
550         buf = dp_fill(buf, eth_get_dev());
551 #else
552         memcpy(buf, &ROOT, sizeof(ROOT));
553         buf += sizeof(ROOT);
554 #endif
555
556         ndp = buf;
557         ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
558         ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
559         ndp->dp.length = sizeof(*ndp);
560         memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
561         buf = &ndp[1];
562
563         *((struct efi_device_path *)buf) = END;
564
565         return start;
566 }
567 #endif
568
569 /* Construct a device-path for memory-mapped image */
570 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
571                                         uint64_t start_address,
572                                         uint64_t end_address)
573 {
574         struct efi_device_path_memory *mdp;
575         void *buf, *start;
576
577         start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
578
579         mdp = buf;
580         mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
581         mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
582         mdp->dp.length = sizeof(*mdp);
583         mdp->memory_type = memory_type;
584         mdp->start_address = start_address;
585         mdp->end_address = end_address;
586         buf = &mdp[1];
587
588         *((struct efi_device_path *)buf) = END;
589
590         return start;
591 }
592
593 /*
594  * Helper to split a full device path (containing both device and file
595  * parts) into it's constituent parts.
596  */
597 void efi_dp_split_file_path(struct efi_device_path *full_path,
598                             struct efi_device_path **device_path,
599                             struct efi_device_path **file_path)
600 {
601         struct efi_device_path *p, *dp, *fp;
602
603         dp = efi_dp_dup(full_path);
604         p = dp;
605         while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH))
606                 p = efi_dp_next(p);
607         fp = efi_dp_dup(p);
608
609         p->type = DEVICE_PATH_TYPE_END;
610         p->sub_type = DEVICE_PATH_SUB_TYPE_END;
611         p->length = sizeof(*p);
612
613         *device_path = dp;
614         *file_path = fp;
615 }