]> git.sur5r.net Git - u-boot/blob - lib/efi_loader/efi_device_path.c
efi_loader: efi_dp_match should have const arguments
[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
130         list_for_each_entry(efiobj, &efi_obj_list, link) {
131                 int i;
132
133                 for (i = 0; i < ARRAY_SIZE(efiobj->protocols); i++) {
134                         struct efi_handler *handler = &efiobj->protocols[i];
135                         struct efi_device_path *obj_dp;
136
137                         if (!handler->guid)
138                                 break;
139
140                         if (guidcmp(handler->guid, &efi_guid_device_path))
141                                 continue;
142
143                         obj_dp = handler->protocol_interface;
144
145                         do {
146                                 if (efi_dp_match(dp, obj_dp) == 0) {
147                                         if (rem) {
148                                                 *rem = ((void *)dp) +
149                                                         efi_dp_size(obj_dp);
150                                         }
151                                         return efiobj;
152                                 }
153
154                                 obj_dp = shorten_path(efi_dp_next(obj_dp));
155                         } while (short_path && obj_dp);
156                 }
157         }
158
159         return NULL;
160 }
161
162
163 /*
164  * Find an efiobj from device-path, if 'rem' is not NULL, returns the
165  * remaining part of the device path after the matched object.
166  */
167 struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
168                                    struct efi_device_path **rem)
169 {
170         struct efi_object *efiobj;
171
172         efiobj = find_obj(dp, false, rem);
173
174         if (!efiobj)
175                 efiobj = find_obj(dp, true, rem);
176
177         return efiobj;
178 }
179
180 /* return size not including End node: */
181 unsigned efi_dp_size(const struct efi_device_path *dp)
182 {
183         unsigned sz = 0;
184
185         while (dp) {
186                 sz += dp->length;
187                 dp = efi_dp_next(dp);
188         }
189
190         return sz;
191 }
192
193 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
194 {
195         struct efi_device_path *ndp;
196         unsigned sz = efi_dp_size(dp) + sizeof(END);
197
198         if (!dp)
199                 return NULL;
200
201         ndp = dp_alloc(sz);
202         memcpy(ndp, dp, sz);
203
204         return ndp;
205 }
206
207 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
208                                       const struct efi_device_path *dp2)
209 {
210         struct efi_device_path *ret;
211
212         if (!dp1) {
213                 ret = efi_dp_dup(dp2);
214         } else if (!dp2) {
215                 ret = efi_dp_dup(dp1);
216         } else {
217                 /* both dp1 and dp2 are non-null */
218                 unsigned sz1 = efi_dp_size(dp1);
219                 unsigned sz2 = efi_dp_size(dp2);
220                 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
221                 memcpy(p, dp1, sz1);
222                 memcpy(p + sz1, dp2, sz2);
223                 memcpy(p + sz1 + sz2, &END, sizeof(END));
224                 ret = p;
225         }
226
227         return ret;
228 }
229
230 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
231                                            const struct efi_device_path *node)
232 {
233         struct efi_device_path *ret;
234
235         if (!node && !dp) {
236                 ret = efi_dp_dup(&END);
237         } else if (!node) {
238                 ret = efi_dp_dup(dp);
239         } else if (!dp) {
240                 unsigned sz = node->length;
241                 void *p = dp_alloc(sz + sizeof(END));
242                 memcpy(p, node, sz);
243                 memcpy(p + sz, &END, sizeof(END));
244                 ret = p;
245         } else {
246                 /* both dp and node are non-null */
247                 unsigned sz = efi_dp_size(dp);
248                 void *p = dp_alloc(sz + node->length + sizeof(END));
249                 memcpy(p, dp, sz);
250                 memcpy(p + sz, node, node->length);
251                 memcpy(p + sz + node->length, &END, sizeof(END));
252                 ret = p;
253         }
254
255         return ret;
256 }
257
258 #ifdef CONFIG_DM
259 /* size of device-path not including END node for device and all parents
260  * up to the root device.
261  */
262 static unsigned dp_size(struct udevice *dev)
263 {
264         if (!dev || !dev->driver)
265                 return sizeof(ROOT);
266
267         switch (dev->driver->id) {
268         case UCLASS_ROOT:
269         case UCLASS_SIMPLE_BUS:
270                 /* stop traversing parents at this point: */
271                 return sizeof(ROOT);
272         case UCLASS_MMC:
273                 return dp_size(dev->parent) +
274                         sizeof(struct efi_device_path_sd_mmc_path);
275         case UCLASS_MASS_STORAGE:
276         case UCLASS_USB_HUB:
277                 return dp_size(dev->parent) +
278                         sizeof(struct efi_device_path_usb_class);
279         default:
280                 /* just skip over unknown classes: */
281                 return dp_size(dev->parent);
282         }
283 }
284
285 static void *dp_fill(void *buf, struct udevice *dev)
286 {
287         if (!dev || !dev->driver)
288                 return buf;
289
290         switch (dev->driver->id) {
291         case UCLASS_ROOT:
292         case UCLASS_SIMPLE_BUS: {
293                 /* stop traversing parents at this point: */
294                 struct efi_device_path_vendor *vdp = buf;
295                 *vdp = ROOT;
296                 return &vdp[1];
297         }
298 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
299         case UCLASS_MMC: {
300                 struct efi_device_path_sd_mmc_path *sddp =
301                         dp_fill(buf, dev->parent);
302                 struct mmc *mmc = mmc_get_mmc_dev(dev);
303                 struct blk_desc *desc = mmc_get_blk_desc(mmc);
304
305                 sddp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
306                 sddp->dp.sub_type = (desc->if_type == IF_TYPE_MMC) ?
307                         DEVICE_PATH_SUB_TYPE_MSG_MMC :
308                         DEVICE_PATH_SUB_TYPE_MSG_SD;
309                 sddp->dp.length   = sizeof(*sddp);
310                 sddp->slot_number = dev->seq;
311
312                 return &sddp[1];
313         }
314 #endif
315         case UCLASS_MASS_STORAGE:
316         case UCLASS_USB_HUB: {
317                 struct efi_device_path_usb_class *udp =
318                         dp_fill(buf, dev->parent);
319                 struct usb_device *udev = dev_get_parent_priv(dev);
320                 struct usb_device_descriptor *desc = &udev->descriptor;
321
322                 udp->dp.type     = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
323                 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
324                 udp->dp.length   = sizeof(*udp);
325                 udp->vendor_id   = desc->idVendor;
326                 udp->product_id  = desc->idProduct;
327                 udp->device_class    = desc->bDeviceClass;
328                 udp->device_subclass = desc->bDeviceSubClass;
329                 udp->device_protocol = desc->bDeviceProtocol;
330
331                 return &udp[1];
332         }
333         default:
334                 debug("unhandled device class: %s (%u)\n",
335                       dev->name, dev->driver->id);
336                 return dp_fill(buf, dev->parent);
337         }
338 }
339
340 /* Construct a device-path from a device: */
341 struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
342 {
343         void *buf, *start;
344
345         start = buf = dp_alloc(dp_size(dev) + sizeof(END));
346         buf = dp_fill(buf, dev);
347         *((struct efi_device_path *)buf) = END;
348
349         return start;
350 }
351 #endif
352
353 static unsigned dp_part_size(struct blk_desc *desc, int part)
354 {
355         unsigned dpsize;
356
357 #ifdef CONFIG_BLK
358         dpsize = dp_size(desc->bdev->parent);
359 #else
360         dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
361 #endif
362
363         if (part == 0) /* the actual disk, not a partition */
364                 return dpsize;
365
366         if (desc->part_type == PART_TYPE_ISO)
367                 dpsize += sizeof(struct efi_device_path_cdrom_path);
368         else
369                 dpsize += sizeof(struct efi_device_path_hard_drive_path);
370
371         return dpsize;
372 }
373
374 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
375 {
376         disk_partition_t info;
377
378 #ifdef CONFIG_BLK
379         buf = dp_fill(buf, desc->bdev->parent);
380 #else
381         /*
382          * We *could* make a more accurate path, by looking at if_type
383          * and handling all the different cases like we do for non-
384          * legacy (ie CONFIG_BLK=y) case.  But most important thing
385          * is just to have a unique device-path for if_type+devnum.
386          * So map things to a fictional USB device:
387          */
388         struct efi_device_path_usb *udp;
389
390         memcpy(buf, &ROOT, sizeof(ROOT));
391         buf += sizeof(ROOT);
392
393         udp = buf;
394         udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
395         udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
396         udp->dp.length = sizeof(*udp);
397         udp->parent_port_number = desc->if_type;
398         udp->usb_interface = desc->devnum;
399         buf = &udp[1];
400 #endif
401
402         if (part == 0) /* the actual disk, not a partition */
403                 return buf;
404
405         part_get_info(desc, part, &info);
406
407         if (desc->part_type == PART_TYPE_ISO) {
408                 struct efi_device_path_cdrom_path *cddp = buf;
409
410                 cddp->boot_entry = part - 1;
411                 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
412                 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
413                 cddp->dp.length = sizeof(*cddp);
414                 cddp->partition_start = info.start;
415                 cddp->partition_end = info.size;
416
417                 buf = &cddp[1];
418         } else {
419                 struct efi_device_path_hard_drive_path *hddp = buf;
420
421                 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
422                 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
423                 hddp->dp.length = sizeof(*hddp);
424                 hddp->partition_number = part - 1;
425                 hddp->partition_start = info.start;
426                 hddp->partition_end = info.size;
427                 if (desc->part_type == PART_TYPE_EFI)
428                         hddp->partmap_type = 2;
429                 else
430                         hddp->partmap_type = 1;
431                 hddp->signature_type = desc->sig_type;
432                 if (hddp->signature_type != 0)
433                         memcpy(hddp->partition_signature, &desc->guid_sig,
434                                sizeof(hddp->partition_signature));
435
436                 buf = &hddp[1];
437         }
438
439         return buf;
440 }
441
442
443 /* Construct a device-path from a partition on a blk device: */
444 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
445 {
446         void *buf, *start;
447
448         start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
449
450         buf = dp_part_fill(buf, desc, part);
451
452         *((struct efi_device_path *)buf) = END;
453
454         return start;
455 }
456
457 /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
458 static void path_to_uefi(u16 *uefi, const char *path)
459 {
460         while (*path) {
461                 char c = *(path++);
462                 if (c == '/')
463                         c = '\\';
464                 *(uefi++) = c;
465         }
466         *uefi = '\0';
467 }
468
469 /*
470  * If desc is NULL, this creates a path with only the file component,
471  * otherwise it creates a full path with both device and file components
472  */
473 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
474                 const char *path)
475 {
476         struct efi_device_path_file_path *fp;
477         void *buf, *start;
478         unsigned dpsize = 0, fpsize;
479
480         if (desc)
481                 dpsize = dp_part_size(desc, part);
482
483         fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
484         dpsize += fpsize;
485
486         start = buf = dp_alloc(dpsize + sizeof(END));
487
488         if (desc)
489                 buf = dp_part_fill(buf, desc, part);
490
491         /* add file-path: */
492         fp = buf;
493         fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
494         fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
495         fp->dp.length = fpsize;
496         path_to_uefi(fp->str, path);
497         buf += fpsize;
498
499         *((struct efi_device_path *)buf) = END;
500
501         return start;
502 }
503
504 #ifdef CONFIG_NET
505 struct efi_device_path *efi_dp_from_eth(void)
506 {
507         struct efi_device_path_mac_addr *ndp;
508         void *buf, *start;
509         unsigned dpsize = 0;
510
511         assert(eth_get_dev());
512
513 #ifdef CONFIG_DM_ETH
514         dpsize += dp_size(eth_get_dev());
515 #else
516         dpsize += sizeof(ROOT);
517 #endif
518         dpsize += sizeof(*ndp);
519
520         start = buf = dp_alloc(dpsize + sizeof(END));
521
522 #ifdef CONFIG_DM_ETH
523         buf = dp_fill(buf, eth_get_dev());
524 #else
525         memcpy(buf, &ROOT, sizeof(ROOT));
526         buf += sizeof(ROOT);
527 #endif
528
529         ndp = buf;
530         ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
531         ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
532         ndp->dp.length = sizeof(*ndp);
533         memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
534         buf = &ndp[1];
535
536         *((struct efi_device_path *)buf) = END;
537
538         return start;
539 }
540 #endif
541
542 /* Construct a device-path for memory-mapped image */
543 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
544                                         uint64_t start_address,
545                                         uint64_t end_address)
546 {
547         struct efi_device_path_memory *mdp;
548         void *buf, *start;
549
550         start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
551
552         mdp = buf;
553         mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
554         mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
555         mdp->dp.length = sizeof(*mdp);
556         mdp->memory_type = memory_type;
557         mdp->start_address = start_address;
558         mdp->end_address = end_address;
559         buf = &mdp[1];
560
561         *((struct efi_device_path *)buf) = END;
562
563         return start;
564 }
565
566 /*
567  * Helper to split a full device path (containing both device and file
568  * parts) into it's constituent parts.
569  */
570 void efi_dp_split_file_path(struct efi_device_path *full_path,
571                             struct efi_device_path **device_path,
572                             struct efi_device_path **file_path)
573 {
574         struct efi_device_path *p, *dp, *fp;
575
576         dp = efi_dp_dup(full_path);
577         p = dp;
578         while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH))
579                 p = efi_dp_next(p);
580         fp = efi_dp_dup(p);
581
582         p->type = DEVICE_PATH_TYPE_END;
583         p->sub_type = DEVICE_PATH_SUB_TYPE_END;
584         p->length = sizeof(*p);
585
586         *device_path = dp;
587         *file_path = fp;
588 }