]> git.ipfire.org Git - people/ms/u-boot.git/blob - lib/efi_loader/efi_device_path.c
efi_loader: initialise partition_signature memory
[people/ms/u-boot.git] / 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
432 switch (desc->sig_type) {
433 case SIG_TYPE_NONE:
434 default:
435 hddp->signature_type = 0;
436 memset(hddp->partition_signature, 0,
437 sizeof(hddp->partition_signature));
438 break;
439 case SIG_TYPE_MBR:
440 hddp->signature_type = 1;
441 memset(hddp->partition_signature, 0,
442 sizeof(hddp->partition_signature));
443 memcpy(hddp->partition_signature, &desc->mbr_sig,
444 sizeof(desc->mbr_sig));
445 break;
446 case SIG_TYPE_GUID:
447 hddp->signature_type = 2;
448 memcpy(hddp->partition_signature, &desc->guid_sig,
449 sizeof(hddp->partition_signature));
450 break;
451 }
452
453 buf = &hddp[1];
454 }
455
456 return buf;
457 }
458
459
460 /* Construct a device-path from a partition on a blk device: */
461 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
462 {
463 void *buf, *start;
464
465 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
466
467 buf = dp_part_fill(buf, desc, part);
468
469 *((struct efi_device_path *)buf) = END;
470
471 return start;
472 }
473
474 /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
475 static void path_to_uefi(u16 *uefi, const char *path)
476 {
477 while (*path) {
478 char c = *(path++);
479 if (c == '/')
480 c = '\\';
481 *(uefi++) = c;
482 }
483 *uefi = '\0';
484 }
485
486 /*
487 * If desc is NULL, this creates a path with only the file component,
488 * otherwise it creates a full path with both device and file components
489 */
490 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
491 const char *path)
492 {
493 struct efi_device_path_file_path *fp;
494 void *buf, *start;
495 unsigned dpsize = 0, fpsize;
496
497 if (desc)
498 dpsize = dp_part_size(desc, part);
499
500 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
501 dpsize += fpsize;
502
503 start = buf = dp_alloc(dpsize + sizeof(END));
504
505 if (desc)
506 buf = dp_part_fill(buf, desc, part);
507
508 /* add file-path: */
509 fp = buf;
510 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
511 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
512 fp->dp.length = fpsize;
513 path_to_uefi(fp->str, path);
514 buf += fpsize;
515
516 *((struct efi_device_path *)buf) = END;
517
518 return start;
519 }
520
521 #ifdef CONFIG_NET
522 struct efi_device_path *efi_dp_from_eth(void)
523 {
524 struct efi_device_path_mac_addr *ndp;
525 void *buf, *start;
526 unsigned dpsize = 0;
527
528 assert(eth_get_dev());
529
530 #ifdef CONFIG_DM_ETH
531 dpsize += dp_size(eth_get_dev());
532 #else
533 dpsize += sizeof(ROOT);
534 #endif
535 dpsize += sizeof(*ndp);
536
537 start = buf = dp_alloc(dpsize + sizeof(END));
538
539 #ifdef CONFIG_DM_ETH
540 buf = dp_fill(buf, eth_get_dev());
541 #else
542 memcpy(buf, &ROOT, sizeof(ROOT));
543 buf += sizeof(ROOT);
544 #endif
545
546 ndp = buf;
547 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
548 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
549 ndp->dp.length = sizeof(*ndp);
550 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
551 buf = &ndp[1];
552
553 *((struct efi_device_path *)buf) = END;
554
555 return start;
556 }
557 #endif
558
559 /* Construct a device-path for memory-mapped image */
560 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
561 uint64_t start_address,
562 uint64_t end_address)
563 {
564 struct efi_device_path_memory *mdp;
565 void *buf, *start;
566
567 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
568
569 mdp = buf;
570 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
571 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
572 mdp->dp.length = sizeof(*mdp);
573 mdp->memory_type = memory_type;
574 mdp->start_address = start_address;
575 mdp->end_address = end_address;
576 buf = &mdp[1];
577
578 *((struct efi_device_path *)buf) = END;
579
580 return start;
581 }
582
583 /*
584 * Helper to split a full device path (containing both device and file
585 * parts) into it's constituent parts.
586 */
587 void efi_dp_split_file_path(struct efi_device_path *full_path,
588 struct efi_device_path **device_path,
589 struct efi_device_path **file_path)
590 {
591 struct efi_device_path *p, *dp, *fp;
592
593 dp = efi_dp_dup(full_path);
594 p = dp;
595 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH))
596 p = efi_dp_next(p);
597 fp = efi_dp_dup(p);
598
599 p->type = DEVICE_PATH_TYPE_END;
600 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
601 p->length = sizeof(*p);
602
603 *device_path = dp;
604 *file_path = fp;
605 }