]> git.ipfire.org Git - thirdparty/u-boot.git/blob - lib/efi_loader/efi_device_path.c
efi_loader: correctly determine length of empty device path
[thirdparty/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 #define LOG_CATEGORY LOGL_ERR
10
11 #include <common.h>
12 #include <blk.h>
13 #include <dm.h>
14 #include <usb.h>
15 #include <mmc.h>
16 #include <efi_loader.h>
17 #include <inttypes.h>
18 #include <part.h>
19
20 /* template END node: */
21 static const struct efi_device_path END = {
22 .type = DEVICE_PATH_TYPE_END,
23 .sub_type = DEVICE_PATH_SUB_TYPE_END,
24 .length = sizeof(END),
25 };
26
27 #define U_BOOT_GUID \
28 EFI_GUID(0xe61d73b9, 0xa384, 0x4acc, \
29 0xae, 0xab, 0x82, 0xe8, 0x28, 0xf3, 0x62, 0x8b)
30
31 /* template ROOT node: */
32 static const struct efi_device_path_vendor ROOT = {
33 .dp = {
34 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
35 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
36 .length = sizeof(ROOT),
37 },
38 .guid = U_BOOT_GUID,
39 };
40
41 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
42 /*
43 * Determine if an MMC device is an SD card.
44 *
45 * @desc block device descriptor
46 * @return true if the device is an SD card
47 */
48 static bool is_sd(struct blk_desc *desc)
49 {
50 struct mmc *mmc = find_mmc_device(desc->devnum);
51
52 if (!mmc)
53 return false;
54
55 return IS_SD(mmc) != 0U;
56 }
57 #endif
58
59 static void *dp_alloc(size_t sz)
60 {
61 void *buf;
62
63 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
64 EFI_SUCCESS) {
65 debug("EFI: ERROR: out of memory in %s\n", __func__);
66 return NULL;
67 }
68
69 memset(buf, 0, sz);
70 return buf;
71 }
72
73 /*
74 * Iterate to next block in device-path, terminating (returning NULL)
75 * at /End* node.
76 */
77 struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
78 {
79 if (dp == NULL)
80 return NULL;
81 if (dp->type == DEVICE_PATH_TYPE_END)
82 return NULL;
83 dp = ((void *)dp) + dp->length;
84 if (dp->type == DEVICE_PATH_TYPE_END)
85 return NULL;
86 return (struct efi_device_path *)dp;
87 }
88
89 /*
90 * Compare two device-paths, stopping when the shorter of the two hits
91 * an End* node. This is useful to, for example, compare a device-path
92 * representing a device with one representing a file on the device, or
93 * a device with a parent device.
94 */
95 int efi_dp_match(const struct efi_device_path *a,
96 const struct efi_device_path *b)
97 {
98 while (1) {
99 int ret;
100
101 ret = memcmp(&a->length, &b->length, sizeof(a->length));
102 if (ret)
103 return ret;
104
105 ret = memcmp(a, b, a->length);
106 if (ret)
107 return ret;
108
109 a = efi_dp_next(a);
110 b = efi_dp_next(b);
111
112 if (!a || !b)
113 return 0;
114 }
115 }
116
117 /*
118 * See UEFI spec (section 3.1.2, about short-form device-paths..
119 * tl;dr: we can have a device-path that starts with a USB WWID
120 * or USB Class node, and a few other cases which don't encode
121 * the full device path with bus hierarchy:
122 *
123 * - MESSAGING:USB_WWID
124 * - MESSAGING:USB_CLASS
125 * - MEDIA:FILE_PATH
126 * - MEDIA:HARD_DRIVE
127 * - MESSAGING:URI
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 unsigned int dp_size = efi_dp_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->handle,
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_size(obj_dp);
174 return efiobj;
175 } else {
176 /* Only return on exact matches */
177 if (efi_dp_size(obj_dp) == dp_size)
178 return efiobj;
179 }
180 }
181
182 obj_dp = shorten_path(efi_dp_next(obj_dp));
183 } while (short_path && obj_dp);
184 }
185
186 return NULL;
187 }
188
189 /*
190 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
191 * remaining part of the device path after the matched object.
192 */
193 struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
194 struct efi_device_path **rem)
195 {
196 struct efi_object *efiobj;
197
198 /* Search for an exact match first */
199 efiobj = find_obj(dp, false, NULL);
200
201 /* Then for a fuzzy match */
202 if (!efiobj)
203 efiobj = find_obj(dp, false, rem);
204
205 /* And now for a fuzzy short match */
206 if (!efiobj)
207 efiobj = find_obj(dp, true, rem);
208
209 return efiobj;
210 }
211
212 /*
213 * Determine the last device path node that is not the end node.
214 *
215 * @dp device path
216 * @return last node before the end node if it exists
217 * otherwise NULL
218 */
219 const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
220 {
221 struct efi_device_path *ret;
222
223 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
224 return NULL;
225 while (dp) {
226 ret = (struct efi_device_path *)dp;
227 dp = efi_dp_next(dp);
228 }
229 return ret;
230 }
231
232 /* return size not including End node: */
233 unsigned efi_dp_size(const struct efi_device_path *dp)
234 {
235 unsigned sz = 0;
236
237 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
238 return 0;
239 while (dp) {
240 sz += dp->length;
241 dp = efi_dp_next(dp);
242 }
243
244 return sz;
245 }
246
247 struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
248 {
249 struct efi_device_path *ndp;
250 unsigned sz = efi_dp_size(dp) + sizeof(END);
251
252 if (!dp)
253 return NULL;
254
255 ndp = dp_alloc(sz);
256 if (!ndp)
257 return NULL;
258 memcpy(ndp, dp, sz);
259
260 return ndp;
261 }
262
263 struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
264 const struct efi_device_path *dp2)
265 {
266 struct efi_device_path *ret;
267
268 if (!dp1 && !dp2) {
269 /* return an end node */
270 ret = efi_dp_dup(&END);
271 } else if (!dp1) {
272 ret = efi_dp_dup(dp2);
273 } else if (!dp2) {
274 ret = efi_dp_dup(dp1);
275 } else {
276 /* both dp1 and dp2 are non-null */
277 unsigned sz1 = efi_dp_size(dp1);
278 unsigned sz2 = efi_dp_size(dp2);
279 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
280 if (!p)
281 return NULL;
282 memcpy(p, dp1, sz1);
283 /* the end node of the second device path has to be retained */
284 memcpy(p + sz1, dp2, sz2 + sizeof(END));
285 ret = p;
286 }
287
288 return ret;
289 }
290
291 struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
292 const struct efi_device_path *node)
293 {
294 struct efi_device_path *ret;
295
296 if (!node && !dp) {
297 ret = efi_dp_dup(&END);
298 } else if (!node) {
299 ret = efi_dp_dup(dp);
300 } else if (!dp) {
301 unsigned sz = node->length;
302 void *p = dp_alloc(sz + sizeof(END));
303 if (!p)
304 return NULL;
305 memcpy(p, node, sz);
306 memcpy(p + sz, &END, sizeof(END));
307 ret = p;
308 } else {
309 /* both dp and node are non-null */
310 unsigned sz = efi_dp_size(dp);
311 void *p = dp_alloc(sz + node->length + sizeof(END));
312 if (!p)
313 return NULL;
314 memcpy(p, dp, sz);
315 memcpy(p + sz, node, node->length);
316 memcpy(p + sz + node->length, &END, sizeof(END));
317 ret = p;
318 }
319
320 return ret;
321 }
322
323 struct efi_device_path *efi_dp_create_device_node(const u8 type,
324 const u8 sub_type,
325 const u16 length)
326 {
327 struct efi_device_path *ret;
328
329 ret = dp_alloc(length);
330 if (!ret)
331 return ret;
332 ret->type = type;
333 ret->sub_type = sub_type;
334 ret->length = length;
335 return ret;
336 }
337
338 #ifdef CONFIG_DM
339 /* size of device-path not including END node for device and all parents
340 * up to the root device.
341 */
342 static unsigned dp_size(struct udevice *dev)
343 {
344 if (!dev || !dev->driver)
345 return sizeof(ROOT);
346
347 switch (dev->driver->id) {
348 case UCLASS_ROOT:
349 case UCLASS_SIMPLE_BUS:
350 /* stop traversing parents at this point: */
351 return sizeof(ROOT);
352 case UCLASS_ETH:
353 return dp_size(dev->parent) +
354 sizeof(struct efi_device_path_mac_addr);
355 #ifdef CONFIG_BLK
356 case UCLASS_BLK:
357 switch (dev->parent->uclass->uc_drv->id) {
358 #ifdef CONFIG_IDE
359 case UCLASS_IDE:
360 return dp_size(dev->parent) +
361 sizeof(struct efi_device_path_atapi);
362 #endif
363 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
364 case UCLASS_SCSI:
365 return dp_size(dev->parent) +
366 sizeof(struct efi_device_path_scsi);
367 #endif
368 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
369 case UCLASS_MMC:
370 return dp_size(dev->parent) +
371 sizeof(struct efi_device_path_sd_mmc_path);
372 #endif
373 default:
374 return dp_size(dev->parent);
375 }
376 #endif
377 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
378 case UCLASS_MMC:
379 return dp_size(dev->parent) +
380 sizeof(struct efi_device_path_sd_mmc_path);
381 #endif
382 case UCLASS_MASS_STORAGE:
383 case UCLASS_USB_HUB:
384 return dp_size(dev->parent) +
385 sizeof(struct efi_device_path_usb_class);
386 default:
387 /* just skip over unknown classes: */
388 return dp_size(dev->parent);
389 }
390 }
391
392 /*
393 * Recursively build a device path.
394 *
395 * @buf pointer to the end of the device path
396 * @dev device
397 * @return pointer to the end of the device path
398 */
399 static void *dp_fill(void *buf, struct udevice *dev)
400 {
401 if (!dev || !dev->driver)
402 return buf;
403
404 switch (dev->driver->id) {
405 case UCLASS_ROOT:
406 case UCLASS_SIMPLE_BUS: {
407 /* stop traversing parents at this point: */
408 struct efi_device_path_vendor *vdp = buf;
409 *vdp = ROOT;
410 return &vdp[1];
411 }
412 #ifdef CONFIG_DM_ETH
413 case UCLASS_ETH: {
414 struct efi_device_path_mac_addr *dp =
415 dp_fill(buf, dev->parent);
416 struct eth_pdata *pdata = dev->platdata;
417
418 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
419 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
420 dp->dp.length = sizeof(*dp);
421 memset(&dp->mac, 0, sizeof(dp->mac));
422 /* We only support IPv4 */
423 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
424 /* Ethernet */
425 dp->if_type = 1;
426 return &dp[1];
427 }
428 #endif
429 #ifdef CONFIG_BLK
430 case UCLASS_BLK:
431 switch (dev->parent->uclass->uc_drv->id) {
432 #ifdef CONFIG_IDE
433 case UCLASS_IDE: {
434 struct efi_device_path_atapi *dp =
435 dp_fill(buf, dev->parent);
436 struct blk_desc *desc = dev_get_uclass_platdata(dev);
437
438 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
439 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
440 dp->dp.length = sizeof(*dp);
441 dp->logical_unit_number = desc->devnum;
442 dp->primary_secondary = IDE_BUS(desc->devnum);
443 dp->slave_master = desc->devnum %
444 (CONFIG_SYS_IDE_MAXDEVICE /
445 CONFIG_SYS_IDE_MAXBUS);
446 return &dp[1];
447 }
448 #endif
449 #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
450 case UCLASS_SCSI: {
451 struct efi_device_path_scsi *dp =
452 dp_fill(buf, dev->parent);
453 struct blk_desc *desc = dev_get_uclass_platdata(dev);
454
455 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
456 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
457 dp->dp.length = sizeof(*dp);
458 dp->logical_unit_number = desc->lun;
459 dp->target_id = desc->target;
460 return &dp[1];
461 }
462 #endif
463 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
464 case UCLASS_MMC: {
465 struct efi_device_path_sd_mmc_path *sddp =
466 dp_fill(buf, dev->parent);
467 struct blk_desc *desc = dev_get_uclass_platdata(dev);
468
469 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
470 sddp->dp.sub_type = is_sd(desc) ?
471 DEVICE_PATH_SUB_TYPE_MSG_SD :
472 DEVICE_PATH_SUB_TYPE_MSG_MMC;
473 sddp->dp.length = sizeof(*sddp);
474 sddp->slot_number = dev->seq;
475 return &sddp[1];
476 }
477 #endif
478 default:
479 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
480 __FILE__, __LINE__, __func__,
481 dev->name, dev->parent->uclass->uc_drv->id);
482 return dp_fill(buf, dev->parent);
483 }
484 #endif
485 #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
486 case UCLASS_MMC: {
487 struct efi_device_path_sd_mmc_path *sddp =
488 dp_fill(buf, dev->parent);
489 struct mmc *mmc = mmc_get_mmc_dev(dev);
490 struct blk_desc *desc = mmc_get_blk_desc(mmc);
491
492 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
493 sddp->dp.sub_type = is_sd(desc) ?
494 DEVICE_PATH_SUB_TYPE_MSG_SD :
495 DEVICE_PATH_SUB_TYPE_MSG_MMC;
496 sddp->dp.length = sizeof(*sddp);
497 sddp->slot_number = dev->seq;
498
499 return &sddp[1];
500 }
501 #endif
502 case UCLASS_MASS_STORAGE:
503 case UCLASS_USB_HUB: {
504 struct efi_device_path_usb_class *udp =
505 dp_fill(buf, dev->parent);
506 struct usb_device *udev = dev_get_parent_priv(dev);
507 struct usb_device_descriptor *desc = &udev->descriptor;
508
509 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
510 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
511 udp->dp.length = sizeof(*udp);
512 udp->vendor_id = desc->idVendor;
513 udp->product_id = desc->idProduct;
514 udp->device_class = desc->bDeviceClass;
515 udp->device_subclass = desc->bDeviceSubClass;
516 udp->device_protocol = desc->bDeviceProtocol;
517
518 return &udp[1];
519 }
520 default:
521 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
522 __FILE__, __LINE__, __func__,
523 dev->name, dev->driver->id);
524 return dp_fill(buf, dev->parent);
525 }
526 }
527
528 /* Construct a device-path from a device: */
529 struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
530 {
531 void *buf, *start;
532
533 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
534 if (!buf)
535 return NULL;
536 buf = dp_fill(buf, dev);
537 *((struct efi_device_path *)buf) = END;
538
539 return start;
540 }
541 #endif
542
543 static unsigned dp_part_size(struct blk_desc *desc, int part)
544 {
545 unsigned dpsize;
546
547 #ifdef CONFIG_BLK
548 {
549 struct udevice *dev;
550 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
551
552 if (ret)
553 dev = desc->bdev->parent;
554 dpsize = dp_size(dev);
555 }
556 #else
557 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
558 #endif
559
560 if (part == 0) /* the actual disk, not a partition */
561 return dpsize;
562
563 if (desc->part_type == PART_TYPE_ISO)
564 dpsize += sizeof(struct efi_device_path_cdrom_path);
565 else
566 dpsize += sizeof(struct efi_device_path_hard_drive_path);
567
568 return dpsize;
569 }
570
571 /*
572 * Create a device node for a block device partition.
573 *
574 * @buf buffer to which the device path is wirtten
575 * @desc block device descriptor
576 * @part partition number, 0 identifies a block device
577 */
578 static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
579 {
580 disk_partition_t info;
581
582 part_get_info(desc, part, &info);
583
584 if (desc->part_type == PART_TYPE_ISO) {
585 struct efi_device_path_cdrom_path *cddp = buf;
586
587 cddp->boot_entry = part;
588 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
589 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
590 cddp->dp.length = sizeof(*cddp);
591 cddp->partition_start = info.start;
592 cddp->partition_end = info.size;
593
594 buf = &cddp[1];
595 } else {
596 struct efi_device_path_hard_drive_path *hddp = buf;
597
598 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
599 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
600 hddp->dp.length = sizeof(*hddp);
601 hddp->partition_number = part;
602 hddp->partition_start = info.start;
603 hddp->partition_end = info.size;
604 if (desc->part_type == PART_TYPE_EFI)
605 hddp->partmap_type = 2;
606 else
607 hddp->partmap_type = 1;
608
609 switch (desc->sig_type) {
610 case SIG_TYPE_NONE:
611 default:
612 hddp->signature_type = 0;
613 memset(hddp->partition_signature, 0,
614 sizeof(hddp->partition_signature));
615 break;
616 case SIG_TYPE_MBR:
617 hddp->signature_type = 1;
618 memset(hddp->partition_signature, 0,
619 sizeof(hddp->partition_signature));
620 memcpy(hddp->partition_signature, &desc->mbr_sig,
621 sizeof(desc->mbr_sig));
622 break;
623 case SIG_TYPE_GUID:
624 hddp->signature_type = 2;
625 memcpy(hddp->partition_signature, &desc->guid_sig,
626 sizeof(hddp->partition_signature));
627 break;
628 }
629
630 buf = &hddp[1];
631 }
632
633 return buf;
634 }
635
636 /*
637 * Create a device path for a block device or one of its partitions.
638 *
639 * @buf buffer to which the device path is wirtten
640 * @desc block device descriptor
641 * @part partition number, 0 identifies a block device
642 */
643 static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
644 {
645 #ifdef CONFIG_BLK
646 {
647 struct udevice *dev;
648 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
649
650 if (ret)
651 dev = desc->bdev->parent;
652 buf = dp_fill(buf, dev);
653 }
654 #else
655 /*
656 * We *could* make a more accurate path, by looking at if_type
657 * and handling all the different cases like we do for non-
658 * legacy (ie CONFIG_BLK=y) case. But most important thing
659 * is just to have a unique device-path for if_type+devnum.
660 * So map things to a fictitious USB device.
661 */
662 struct efi_device_path_usb *udp;
663
664 memcpy(buf, &ROOT, sizeof(ROOT));
665 buf += sizeof(ROOT);
666
667 udp = buf;
668 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
669 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
670 udp->dp.length = sizeof(*udp);
671 udp->parent_port_number = desc->if_type;
672 udp->usb_interface = desc->devnum;
673 buf = &udp[1];
674 #endif
675
676 if (part == 0) /* the actual disk, not a partition */
677 return buf;
678
679 return dp_part_node(buf, desc, part);
680 }
681
682 /* Construct a device-path from a partition on a blk device: */
683 struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
684 {
685 void *buf, *start;
686
687 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
688 if (!buf)
689 return NULL;
690
691 buf = dp_part_fill(buf, desc, part);
692
693 *((struct efi_device_path *)buf) = END;
694
695 return start;
696 }
697
698 /*
699 * Create a device node for a block device partition.
700 *
701 * @buf buffer to which the device path is wirtten
702 * @desc block device descriptor
703 * @part partition number, 0 identifies a block device
704 */
705 struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
706 {
707 efi_uintn_t dpsize;
708 void *buf;
709
710 if (desc->part_type == PART_TYPE_ISO)
711 dpsize = sizeof(struct efi_device_path_cdrom_path);
712 else
713 dpsize = sizeof(struct efi_device_path_hard_drive_path);
714 buf = dp_alloc(dpsize);
715
716 dp_part_node(buf, desc, part);
717
718 return buf;
719 }
720
721 /* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
722 static void path_to_uefi(u16 *uefi, const char *path)
723 {
724 while (*path) {
725 char c = *(path++);
726 if (c == '/')
727 c = '\\';
728 *(uefi++) = c;
729 }
730 *uefi = '\0';
731 }
732
733 /*
734 * If desc is NULL, this creates a path with only the file component,
735 * otherwise it creates a full path with both device and file components
736 */
737 struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
738 const char *path)
739 {
740 struct efi_device_path_file_path *fp;
741 void *buf, *start;
742 unsigned dpsize = 0, fpsize;
743
744 if (desc)
745 dpsize = dp_part_size(desc, part);
746
747 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
748 dpsize += fpsize;
749
750 start = buf = dp_alloc(dpsize + sizeof(END));
751 if (!buf)
752 return NULL;
753
754 if (desc)
755 buf = dp_part_fill(buf, desc, part);
756
757 /* add file-path: */
758 fp = buf;
759 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
760 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
761 fp->dp.length = fpsize;
762 path_to_uefi(fp->str, path);
763 buf += fpsize;
764
765 *((struct efi_device_path *)buf) = END;
766
767 return start;
768 }
769
770 #ifdef CONFIG_NET
771 struct efi_device_path *efi_dp_from_eth(void)
772 {
773 #ifndef CONFIG_DM_ETH
774 struct efi_device_path_mac_addr *ndp;
775 #endif
776 void *buf, *start;
777 unsigned dpsize = 0;
778
779 assert(eth_get_dev());
780
781 #ifdef CONFIG_DM_ETH
782 dpsize += dp_size(eth_get_dev());
783 #else
784 dpsize += sizeof(ROOT);
785 dpsize += sizeof(*ndp);
786 #endif
787
788 start = buf = dp_alloc(dpsize + sizeof(END));
789 if (!buf)
790 return NULL;
791
792 #ifdef CONFIG_DM_ETH
793 buf = dp_fill(buf, eth_get_dev());
794 #else
795 memcpy(buf, &ROOT, sizeof(ROOT));
796 buf += sizeof(ROOT);
797
798 ndp = buf;
799 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
800 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
801 ndp->dp.length = sizeof(*ndp);
802 ndp->if_type = 1; /* Ethernet */
803 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
804 buf = &ndp[1];
805 #endif
806
807 *((struct efi_device_path *)buf) = END;
808
809 return start;
810 }
811 #endif
812
813 /* Construct a device-path for memory-mapped image */
814 struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
815 uint64_t start_address,
816 uint64_t end_address)
817 {
818 struct efi_device_path_memory *mdp;
819 void *buf, *start;
820
821 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
822 if (!buf)
823 return NULL;
824
825 mdp = buf;
826 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
827 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
828 mdp->dp.length = sizeof(*mdp);
829 mdp->memory_type = memory_type;
830 mdp->start_address = start_address;
831 mdp->end_address = end_address;
832 buf = &mdp[1];
833
834 *((struct efi_device_path *)buf) = END;
835
836 return start;
837 }
838
839 /*
840 * Helper to split a full device path (containing both device and file
841 * parts) into it's constituent parts.
842 */
843 efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
844 struct efi_device_path **device_path,
845 struct efi_device_path **file_path)
846 {
847 struct efi_device_path *p, *dp, *fp;
848
849 *device_path = NULL;
850 *file_path = NULL;
851 dp = efi_dp_dup(full_path);
852 if (!dp)
853 return EFI_OUT_OF_RESOURCES;
854 p = dp;
855 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
856 p = efi_dp_next(p);
857 if (!p)
858 return EFI_OUT_OF_RESOURCES;
859 }
860 fp = efi_dp_dup(p);
861 if (!fp)
862 return EFI_OUT_OF_RESOURCES;
863 p->type = DEVICE_PATH_TYPE_END;
864 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
865 p->length = sizeof(*p);
866
867 *device_path = dp;
868 *file_path = fp;
869 return EFI_SUCCESS;
870 }