1
2
3
4
5
6
7
8#define LOG_CATEGORY LOGC_EFI
9
10#include <common.h>
11#include <blk.h>
12#include <dm.h>
13#include <log.h>
14#include <net.h>
15#include <usb.h>
16#include <mmc.h>
17#include <nvme.h>
18#include <efi_loader.h>
19#include <part.h>
20#include <sandboxblockdev.h>
21#include <uuid.h>
22#include <asm-generic/unaligned.h>
23#include <linux/compat.h>
24
25#ifdef CONFIG_SANDBOX
26const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
27#endif
28#ifdef CONFIG_VIRTIO_BLK
29const efi_guid_t efi_guid_virtio_dev = U_BOOT_VIRTIO_DEV_GUID;
30#endif
31
32
33static const struct efi_device_path END = {
34 .type = DEVICE_PATH_TYPE_END,
35 .sub_type = DEVICE_PATH_SUB_TYPE_END,
36 .length = sizeof(END),
37};
38
39
40static const struct efi_device_path_vendor ROOT = {
41 .dp = {
42 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
43 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
44 .length = sizeof(ROOT),
45 },
46 .guid = U_BOOT_GUID,
47};
48
49#if defined(CONFIG_MMC)
50
51
52
53
54
55
56static bool is_sd(struct blk_desc *desc)
57{
58 struct mmc *mmc = find_mmc_device(desc->devnum);
59
60 if (!mmc)
61 return false;
62
63 return IS_SD(mmc) != 0U;
64}
65#endif
66
67static void *dp_alloc(size_t sz)
68{
69 void *buf;
70
71 if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, sz, &buf) !=
72 EFI_SUCCESS) {
73 debug("EFI: ERROR: out of memory in %s\n", __func__);
74 return NULL;
75 }
76
77 memset(buf, 0, sz);
78 return buf;
79}
80
81
82
83
84
85struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
86{
87 if (dp == NULL)
88 return NULL;
89 if (dp->type == DEVICE_PATH_TYPE_END)
90 return NULL;
91 dp = ((void *)dp) + dp->length;
92 if (dp->type == DEVICE_PATH_TYPE_END)
93 return NULL;
94 return (struct efi_device_path *)dp;
95}
96
97
98
99
100
101
102
103int efi_dp_match(const struct efi_device_path *a,
104 const struct efi_device_path *b)
105{
106 while (1) {
107 int ret;
108
109 ret = memcmp(&a->length, &b->length, sizeof(a->length));
110 if (ret)
111 return ret;
112
113 ret = memcmp(a, b, a->length);
114 if (ret)
115 return ret;
116
117 a = efi_dp_next(a);
118 b = efi_dp_next(b);
119
120 if (!a || !b)
121 return 0;
122 }
123}
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138static struct efi_device_path *shorten_path(struct efi_device_path *dp)
139{
140 while (dp) {
141
142
143
144
145
146 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
147 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
148 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
149 return dp;
150
151 dp = efi_dp_next(dp);
152 }
153
154 return dp;
155}
156
157static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
158 struct efi_device_path **rem)
159{
160 struct efi_object *efiobj;
161 efi_uintn_t dp_size = efi_dp_instance_size(dp);
162
163 list_for_each_entry(efiobj, &efi_obj_list, link) {
164 struct efi_handler *handler;
165 struct efi_device_path *obj_dp;
166 efi_status_t ret;
167
168 ret = efi_search_protocol(efiobj,
169 &efi_guid_device_path, &handler);
170 if (ret != EFI_SUCCESS)
171 continue;
172 obj_dp = handler->protocol_interface;
173
174 do {
175 if (efi_dp_match(dp, obj_dp) == 0) {
176 if (rem) {
177
178
179
180
181 *rem = ((void *)dp) +
182 efi_dp_instance_size(obj_dp);
183 return efiobj;
184 } else {
185
186 if (efi_dp_instance_size(obj_dp) ==
187 dp_size)
188 return efiobj;
189 }
190 }
191
192 obj_dp = shorten_path(efi_dp_next(obj_dp));
193 } while (short_path && obj_dp);
194 }
195
196 return NULL;
197}
198
199
200
201
202
203struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
204 struct efi_device_path **rem)
205{
206 struct efi_object *efiobj;
207
208
209 efiobj = find_obj(dp, false, NULL);
210
211
212 if (!efiobj)
213 efiobj = find_obj(dp, false, rem);
214
215
216 if (!efiobj)
217 efiobj = find_obj(dp, true, rem);
218
219 return efiobj;
220}
221
222
223
224
225
226
227
228
229const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
230{
231 struct efi_device_path *ret;
232
233 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
234 return NULL;
235 while (dp) {
236 ret = (struct efi_device_path *)dp;
237 dp = efi_dp_next(dp);
238 }
239 return ret;
240}
241
242
243efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
244{
245 efi_uintn_t sz = 0;
246
247 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
248 return 0;
249 while (dp) {
250 sz += dp->length;
251 dp = efi_dp_next(dp);
252 }
253
254 return sz;
255}
256
257
258efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
259{
260 const struct efi_device_path *p = dp;
261
262 if (!p)
263 return 0;
264 while (p->type != DEVICE_PATH_TYPE_END ||
265 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
266 p = (void *)p + p->length;
267
268 return (void *)p - (void *)dp;
269}
270
271
272struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
273{
274 struct efi_device_path *ndp;
275 size_t sz = efi_dp_size(dp) + sizeof(END);
276
277 if (!dp)
278 return NULL;
279
280 ndp = dp_alloc(sz);
281 if (!ndp)
282 return NULL;
283 memcpy(ndp, dp, sz);
284
285 return ndp;
286}
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303static struct
304efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
305 const struct efi_device_path *dp2,
306 bool concat)
307{
308 struct efi_device_path *ret;
309 size_t end_size = sizeof(END);
310
311 if (concat)
312 end_size = 2 * sizeof(END);
313 if (!dp1 && !dp2) {
314
315 ret = efi_dp_dup(&END);
316 } else if (!dp1) {
317 ret = efi_dp_dup(dp2);
318 } else if (!dp2) {
319 ret = efi_dp_dup(dp1);
320 } else {
321
322 unsigned sz1 = efi_dp_size(dp1);
323 unsigned sz2 = efi_dp_size(dp2);
324 void *p = dp_alloc(sz1 + sz2 + end_size);
325 if (!p)
326 return NULL;
327 ret = p;
328 memcpy(p, dp1, sz1);
329 p += sz1;
330
331 if (concat) {
332 memcpy(p, &END, sizeof(END));
333 p += sizeof(END);
334 }
335
336
337 memcpy(p, dp2, sz2);
338 p += sz2;
339 memcpy(p, &END, sizeof(END));
340 }
341
342 return ret;
343}
344
345
346
347
348
349
350
351
352
353
354struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
355 const struct efi_device_path *dp2)
356{
357 return efi_dp_append_or_concatenate(dp1, dp2, false);
358}
359
360
361
362
363
364
365
366
367
368
369
370struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
371 const struct efi_device_path *dp2)
372{
373 return efi_dp_append_or_concatenate(dp1, dp2, true);
374}
375
376struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
377 const struct efi_device_path *node)
378{
379 struct efi_device_path *ret;
380
381 if (!node && !dp) {
382 ret = efi_dp_dup(&END);
383 } else if (!node) {
384 ret = efi_dp_dup(dp);
385 } else if (!dp) {
386 size_t sz = node->length;
387 void *p = dp_alloc(sz + sizeof(END));
388 if (!p)
389 return NULL;
390 memcpy(p, node, sz);
391 memcpy(p + sz, &END, sizeof(END));
392 ret = p;
393 } else {
394
395 size_t sz = efi_dp_size(dp);
396 void *p = dp_alloc(sz + node->length + sizeof(END));
397 if (!p)
398 return NULL;
399 memcpy(p, dp, sz);
400 memcpy(p + sz, node, node->length);
401 memcpy(p + sz + node->length, &END, sizeof(END));
402 ret = p;
403 }
404
405 return ret;
406}
407
408struct efi_device_path *efi_dp_create_device_node(const u8 type,
409 const u8 sub_type,
410 const u16 length)
411{
412 struct efi_device_path *ret;
413
414 if (length < sizeof(struct efi_device_path))
415 return NULL;
416
417 ret = dp_alloc(length);
418 if (!ret)
419 return ret;
420 ret->type = type;
421 ret->sub_type = sub_type;
422 ret->length = length;
423 return ret;
424}
425
426struct efi_device_path *efi_dp_append_instance(
427 const struct efi_device_path *dp,
428 const struct efi_device_path *dpi)
429{
430 size_t sz, szi;
431 struct efi_device_path *p, *ret;
432
433 if (!dpi)
434 return NULL;
435 if (!dp)
436 return efi_dp_dup(dpi);
437 sz = efi_dp_size(dp);
438 szi = efi_dp_instance_size(dpi);
439 p = dp_alloc(sz + szi + 2 * sizeof(END));
440 if (!p)
441 return NULL;
442 ret = p;
443 memcpy(p, dp, sz + sizeof(END));
444 p = (void *)p + sz;
445 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
446 p = (void *)p + sizeof(END);
447 memcpy(p, dpi, szi);
448 p = (void *)p + szi;
449 memcpy(p, &END, sizeof(END));
450 return ret;
451}
452
453struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
454 efi_uintn_t *size)
455{
456 size_t sz;
457 struct efi_device_path *p;
458
459 if (size)
460 *size = 0;
461 if (!dp || !*dp)
462 return NULL;
463 sz = efi_dp_instance_size(*dp);
464 p = dp_alloc(sz + sizeof(END));
465 if (!p)
466 return NULL;
467 memcpy(p, *dp, sz + sizeof(END));
468 *dp = (void *)*dp + sz;
469 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
470 *dp = (void *)*dp + sizeof(END);
471 else
472 *dp = NULL;
473 if (size)
474 *size = sz + sizeof(END);
475 return p;
476}
477
478bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
479{
480 const struct efi_device_path *p = dp;
481
482 if (!p)
483 return false;
484 while (p->type != DEVICE_PATH_TYPE_END)
485 p = (void *)p + p->length;
486 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
487}
488
489
490
491
492__maybe_unused static unsigned int dp_size(struct udevice *dev)
493{
494 if (!dev || !dev->driver)
495 return sizeof(ROOT);
496
497 switch (dev->driver->id) {
498 case UCLASS_ROOT:
499 case UCLASS_SIMPLE_BUS:
500
501 return sizeof(ROOT);
502 case UCLASS_ETH:
503 return dp_size(dev->parent) +
504 sizeof(struct efi_device_path_mac_addr);
505 case UCLASS_BLK:
506 switch (dev->parent->uclass->uc_drv->id) {
507#ifdef CONFIG_IDE
508 case UCLASS_IDE:
509 return dp_size(dev->parent) +
510 sizeof(struct efi_device_path_atapi);
511#endif
512#if defined(CONFIG_SCSI)
513 case UCLASS_SCSI:
514 return dp_size(dev->parent) +
515 sizeof(struct efi_device_path_scsi);
516#endif
517#if defined(CONFIG_MMC)
518 case UCLASS_MMC:
519 return dp_size(dev->parent) +
520 sizeof(struct efi_device_path_sd_mmc_path);
521#endif
522#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
523 case UCLASS_AHCI:
524 return dp_size(dev->parent) +
525 sizeof(struct efi_device_path_sata);
526#endif
527#if defined(CONFIG_NVME)
528 case UCLASS_NVME:
529 return dp_size(dev->parent) +
530 sizeof(struct efi_device_path_nvme);
531#endif
532#ifdef CONFIG_SANDBOX
533 case UCLASS_ROOT:
534
535
536
537
538
539 return dp_size(dev->parent)
540 + sizeof(struct efi_device_path_vendor) + 1;
541#endif
542#ifdef CONFIG_VIRTIO_BLK
543 case UCLASS_VIRTIO:
544
545
546
547
548
549 return dp_size(dev->parent)
550 + sizeof(struct efi_device_path_vendor) + 1;
551#endif
552 default:
553 return dp_size(dev->parent);
554 }
555#if defined(CONFIG_MMC)
556 case UCLASS_MMC:
557 return dp_size(dev->parent) +
558 sizeof(struct efi_device_path_sd_mmc_path);
559#endif
560 case UCLASS_MASS_STORAGE:
561 case UCLASS_USB_HUB:
562 return dp_size(dev->parent) +
563 sizeof(struct efi_device_path_usb_class);
564 default:
565
566 return dp_size(dev->parent);
567 }
568}
569
570
571
572
573
574
575
576
577__maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
578{
579 if (!dev || !dev->driver)
580 return buf;
581
582 switch (dev->driver->id) {
583 case UCLASS_ROOT:
584 case UCLASS_SIMPLE_BUS: {
585
586 struct efi_device_path_vendor *vdp = buf;
587 *vdp = ROOT;
588 return &vdp[1];
589 }
590#ifdef CONFIG_NET
591 case UCLASS_ETH: {
592 struct efi_device_path_mac_addr *dp =
593 dp_fill(buf, dev->parent);
594 struct eth_pdata *pdata = dev_get_plat(dev);
595
596 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
597 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
598 dp->dp.length = sizeof(*dp);
599 memset(&dp->mac, 0, sizeof(dp->mac));
600
601 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
602
603 dp->if_type = 1;
604 return &dp[1];
605 }
606#endif
607 case UCLASS_BLK:
608 switch (dev->parent->uclass->uc_drv->id) {
609#ifdef CONFIG_SANDBOX
610 case UCLASS_ROOT: {
611
612 struct efi_device_path_vendor *dp;
613 struct blk_desc *desc = dev_get_uclass_plat(dev);
614
615 dp_fill(buf, dev->parent);
616 dp = buf;
617 ++dp;
618 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
619 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
620 dp->dp.length = sizeof(*dp) + 1;
621 memcpy(&dp->guid, &efi_guid_host_dev,
622 sizeof(efi_guid_t));
623 dp->vendor_data[0] = desc->devnum;
624 return &dp->vendor_data[1];
625 }
626#endif
627#ifdef CONFIG_VIRTIO_BLK
628 case UCLASS_VIRTIO: {
629 struct efi_device_path_vendor *dp;
630 struct blk_desc *desc = dev_get_uclass_plat(dev);
631
632 dp_fill(buf, dev->parent);
633 dp = buf;
634 ++dp;
635 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
636 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
637 dp->dp.length = sizeof(*dp) + 1;
638 memcpy(&dp->guid, &efi_guid_virtio_dev,
639 sizeof(efi_guid_t));
640 dp->vendor_data[0] = desc->devnum;
641 return &dp->vendor_data[1];
642 }
643#endif
644#ifdef CONFIG_IDE
645 case UCLASS_IDE: {
646 struct efi_device_path_atapi *dp =
647 dp_fill(buf, dev->parent);
648 struct blk_desc *desc = dev_get_uclass_plat(dev);
649
650 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
651 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
652 dp->dp.length = sizeof(*dp);
653 dp->logical_unit_number = desc->devnum;
654 dp->primary_secondary = IDE_BUS(desc->devnum);
655 dp->slave_master = desc->devnum %
656 (CONFIG_SYS_IDE_MAXDEVICE /
657 CONFIG_SYS_IDE_MAXBUS);
658 return &dp[1];
659 }
660#endif
661#if defined(CONFIG_SCSI)
662 case UCLASS_SCSI: {
663 struct efi_device_path_scsi *dp =
664 dp_fill(buf, dev->parent);
665 struct blk_desc *desc = dev_get_uclass_plat(dev);
666
667 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
668 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
669 dp->dp.length = sizeof(*dp);
670 dp->logical_unit_number = desc->lun;
671 dp->target_id = desc->target;
672 return &dp[1];
673 }
674#endif
675#if defined(CONFIG_MMC)
676 case UCLASS_MMC: {
677 struct efi_device_path_sd_mmc_path *sddp =
678 dp_fill(buf, dev->parent);
679 struct blk_desc *desc = dev_get_uclass_plat(dev);
680
681 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
682 sddp->dp.sub_type = is_sd(desc) ?
683 DEVICE_PATH_SUB_TYPE_MSG_SD :
684 DEVICE_PATH_SUB_TYPE_MSG_MMC;
685 sddp->dp.length = sizeof(*sddp);
686 sddp->slot_number = dev_seq(dev);
687 return &sddp[1];
688 }
689#endif
690#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
691 case UCLASS_AHCI: {
692 struct efi_device_path_sata *dp =
693 dp_fill(buf, dev->parent);
694 struct blk_desc *desc = dev_get_uclass_plat(dev);
695
696 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
697 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
698 dp->dp.length = sizeof(*dp);
699 dp->hba_port = desc->devnum;
700
701 dp->port_multiplier_port = 0xffff;
702 dp->logical_unit_number = desc->lun;
703 return &dp[1];
704 }
705#endif
706#if defined(CONFIG_NVME)
707 case UCLASS_NVME: {
708 struct efi_device_path_nvme *dp =
709 dp_fill(buf, dev->parent);
710 u32 ns_id;
711
712 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
713 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
714 dp->dp.length = sizeof(*dp);
715 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
716 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
717 return &dp[1];
718 }
719#endif
720 default:
721 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
722 __FILE__, __LINE__, __func__,
723 dev->name, dev->parent->uclass->uc_drv->id);
724 return dp_fill(buf, dev->parent);
725 }
726#if defined(CONFIG_MMC)
727 case UCLASS_MMC: {
728 struct efi_device_path_sd_mmc_path *sddp =
729 dp_fill(buf, dev->parent);
730 struct mmc *mmc = mmc_get_mmc_dev(dev);
731 struct blk_desc *desc = mmc_get_blk_desc(mmc);
732
733 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
734 sddp->dp.sub_type = is_sd(desc) ?
735 DEVICE_PATH_SUB_TYPE_MSG_SD :
736 DEVICE_PATH_SUB_TYPE_MSG_MMC;
737 sddp->dp.length = sizeof(*sddp);
738 sddp->slot_number = dev_seq(dev);
739
740 return &sddp[1];
741 }
742#endif
743 case UCLASS_MASS_STORAGE:
744 case UCLASS_USB_HUB: {
745 struct efi_device_path_usb_class *udp =
746 dp_fill(buf, dev->parent);
747 struct usb_device *udev = dev_get_parent_priv(dev);
748 struct usb_device_descriptor *desc = &udev->descriptor;
749
750 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
751 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
752 udp->dp.length = sizeof(*udp);
753 udp->vendor_id = desc->idVendor;
754 udp->product_id = desc->idProduct;
755 udp->device_class = desc->bDeviceClass;
756 udp->device_subclass = desc->bDeviceSubClass;
757 udp->device_protocol = desc->bDeviceProtocol;
758
759 return &udp[1];
760 }
761 default:
762 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
763 __FILE__, __LINE__, __func__,
764 dev->name, dev->driver->id);
765 return dp_fill(buf, dev->parent);
766 }
767}
768
769static unsigned dp_part_size(struct blk_desc *desc, int part)
770{
771 unsigned dpsize;
772 struct udevice *dev;
773 int ret;
774
775 ret = blk_find_device(desc->if_type, desc->devnum, &dev);
776
777 if (ret)
778 dev = desc->bdev->parent;
779 dpsize = dp_size(dev);
780
781 if (part == 0)
782 return dpsize;
783
784 if (desc->part_type == PART_TYPE_ISO)
785 dpsize += sizeof(struct efi_device_path_cdrom_path);
786 else
787 dpsize += sizeof(struct efi_device_path_hard_drive_path);
788
789 return dpsize;
790}
791
792
793
794
795
796
797
798
799static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
800{
801 struct disk_partition info;
802
803 part_get_info(desc, part, &info);
804
805 if (desc->part_type == PART_TYPE_ISO) {
806 struct efi_device_path_cdrom_path *cddp = buf;
807
808 cddp->boot_entry = part;
809 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
810 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
811 cddp->dp.length = sizeof(*cddp);
812 cddp->partition_start = info.start;
813 cddp->partition_size = info.size;
814
815 buf = &cddp[1];
816 } else {
817 struct efi_device_path_hard_drive_path *hddp = buf;
818
819 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
820 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
821 hddp->dp.length = sizeof(*hddp);
822 hddp->partition_number = part;
823 hddp->partition_start = info.start;
824 hddp->partition_end = info.size;
825 if (desc->part_type == PART_TYPE_EFI)
826 hddp->partmap_type = 2;
827 else
828 hddp->partmap_type = 1;
829
830 switch (desc->sig_type) {
831 case SIG_TYPE_NONE:
832 default:
833 hddp->signature_type = 0;
834 memset(hddp->partition_signature, 0,
835 sizeof(hddp->partition_signature));
836 break;
837 case SIG_TYPE_MBR:
838 hddp->signature_type = 1;
839 memset(hddp->partition_signature, 0,
840 sizeof(hddp->partition_signature));
841 memcpy(hddp->partition_signature, &desc->mbr_sig,
842 sizeof(desc->mbr_sig));
843 break;
844 case SIG_TYPE_GUID:
845 hddp->signature_type = 2;
846 if (uuid_str_to_bin(info.uuid,
847 hddp->partition_signature, 1))
848 log_warning(
849 "Partition no. %d: invalid guid: %s\n",
850 part, info.uuid);
851 break;
852 }
853
854 buf = &hddp[1];
855 }
856
857 return buf;
858}
859
860
861
862
863
864
865
866
867static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
868{
869 struct udevice *dev;
870 int ret;
871
872 ret = blk_find_device(desc->if_type, desc->devnum, &dev);
873
874 if (ret)
875 dev = desc->bdev->parent;
876 buf = dp_fill(buf, dev);
877
878 if (part == 0)
879 return buf;
880
881 return dp_part_node(buf, desc, part);
882}
883
884
885struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
886{
887 void *buf, *start;
888
889 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
890 if (!buf)
891 return NULL;
892
893 buf = dp_part_fill(buf, desc, part);
894
895 *((struct efi_device_path *)buf) = END;
896
897 return start;
898}
899
900
901
902
903
904
905
906
907struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
908{
909 efi_uintn_t dpsize;
910 void *buf;
911
912 if (desc->part_type == PART_TYPE_ISO)
913 dpsize = sizeof(struct efi_device_path_cdrom_path);
914 else
915 dpsize = sizeof(struct efi_device_path_hard_drive_path);
916 buf = dp_alloc(dpsize);
917
918 dp_part_node(buf, desc, part);
919
920 return buf;
921}
922
923
924
925
926
927
928
929
930
931
932static void path_to_uefi(void *uefi, const char *src)
933{
934 u16 *pos = uefi;
935
936
937
938
939
940
941 allow_unaligned();
942
943 while (*src) {
944 s32 code = utf8_get(&src);
945
946 if (code < 0)
947 code = '?';
948 else if (code == '/')
949 code = '\\';
950 utf16_put(code, &pos);
951 }
952 *pos = 0;
953}
954
955
956
957
958
959struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
960 const char *path)
961{
962 struct efi_device_path_file_path *fp;
963 void *buf, *start;
964 size_t dpsize = 0, fpsize;
965
966 if (desc)
967 dpsize = dp_part_size(desc, part);
968
969 fpsize = sizeof(struct efi_device_path) +
970 2 * (utf8_utf16_strlen(path) + 1);
971 if (fpsize > U16_MAX)
972 return NULL;
973
974 dpsize += fpsize;
975
976 start = buf = dp_alloc(dpsize + sizeof(END));
977 if (!buf)
978 return NULL;
979
980 if (desc)
981 buf = dp_part_fill(buf, desc, part);
982
983
984 fp = buf;
985 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
986 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
987 fp->dp.length = (u16)fpsize;
988 path_to_uefi(fp->str, path);
989 buf += fpsize;
990
991 *((struct efi_device_path *)buf) = END;
992
993 return start;
994}
995
996struct efi_device_path *efi_dp_from_uart(void)
997{
998 void *buf, *pos;
999 struct efi_device_path_uart *uart;
1000 size_t dpsize = sizeof(ROOT) + sizeof(*uart) + sizeof(END);
1001
1002 buf = dp_alloc(dpsize);
1003 if (!buf)
1004 return NULL;
1005 pos = buf;
1006 memcpy(pos, &ROOT, sizeof(ROOT));
1007 pos += sizeof(ROOT);
1008 uart = pos;
1009 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1010 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
1011 uart->dp.length = sizeof(*uart);
1012 pos += sizeof(*uart);
1013 memcpy(pos, &END, sizeof(END));
1014
1015 return buf;
1016}
1017
1018#ifdef CONFIG_NET
1019struct efi_device_path *efi_dp_from_eth(void)
1020{
1021 void *buf, *start;
1022 unsigned dpsize = 0;
1023
1024 assert(eth_get_dev());
1025
1026 dpsize += dp_size(eth_get_dev());
1027
1028 start = buf = dp_alloc(dpsize + sizeof(END));
1029 if (!buf)
1030 return NULL;
1031
1032 buf = dp_fill(buf, eth_get_dev());
1033
1034 *((struct efi_device_path *)buf) = END;
1035
1036 return start;
1037}
1038#endif
1039
1040
1041struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1042 uint64_t start_address,
1043 uint64_t end_address)
1044{
1045 struct efi_device_path_memory *mdp;
1046 void *buf, *start;
1047
1048 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
1049 if (!buf)
1050 return NULL;
1051
1052 mdp = buf;
1053 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1054 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1055 mdp->dp.length = sizeof(*mdp);
1056 mdp->memory_type = memory_type;
1057 mdp->start_address = start_address;
1058 mdp->end_address = end_address;
1059 buf = &mdp[1];
1060
1061 *((struct efi_device_path *)buf) = END;
1062
1063 return start;
1064}
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1079 struct efi_device_path **device_path,
1080 struct efi_device_path **file_path)
1081{
1082 struct efi_device_path *p, *dp, *fp = NULL;
1083
1084 *device_path = NULL;
1085 *file_path = NULL;
1086 dp = efi_dp_dup(full_path);
1087 if (!dp)
1088 return EFI_OUT_OF_RESOURCES;
1089 p = dp;
1090 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
1091 p = efi_dp_next(p);
1092 if (!p)
1093 goto out;
1094 }
1095 fp = efi_dp_dup(p);
1096 if (!fp)
1097 return EFI_OUT_OF_RESOURCES;
1098 p->type = DEVICE_PATH_TYPE_END;
1099 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1100 p->length = sizeof(*p);
1101
1102out:
1103 *device_path = dp;
1104 *file_path = fp;
1105 return EFI_SUCCESS;
1106}
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1119 const char *path,
1120 struct efi_device_path **device,
1121 struct efi_device_path **file)
1122{
1123 struct blk_desc *desc = NULL;
1124 struct disk_partition fs_partition;
1125 int part = 0;
1126 char *filename;
1127 char *s;
1128
1129 if (path && !file)
1130 return EFI_INVALID_PARAMETER;
1131
1132 if (!strcmp(dev, "Net")) {
1133#ifdef CONFIG_NET
1134 if (device)
1135 *device = efi_dp_from_eth();
1136#endif
1137 } else if (!strcmp(dev, "Uart")) {
1138 if (device)
1139 *device = efi_dp_from_uart();
1140 } else {
1141 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1142 1);
1143 if (part < 0 || !desc)
1144 return EFI_INVALID_PARAMETER;
1145
1146 if (device)
1147 *device = efi_dp_from_part(desc, part);
1148 }
1149
1150 if (!path)
1151 return EFI_SUCCESS;
1152
1153 filename = calloc(1, strlen(path) + 1);
1154 if (!filename)
1155 return EFI_OUT_OF_RESOURCES;
1156
1157 sprintf(filename, "%s", path);
1158
1159 s = filename;
1160 while ((s = strchr(s, '/')))
1161 *s++ = '\\';
1162 *file = efi_dp_from_file(desc, part, filename);
1163 free(filename);
1164
1165 if (!*file)
1166 return EFI_INVALID_PARAMETER;
1167
1168 return EFI_SUCCESS;
1169}
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1183 const size_t maxlen)
1184{
1185 ssize_t ret = 0;
1186 u16 len;
1187
1188 if (maxlen > SSIZE_MAX)
1189 return -EINVAL;
1190 for (;;) {
1191 len = dp->length;
1192 if (len < 4)
1193 return -1;
1194 ret += len;
1195 if (ret > maxlen)
1196 return -1;
1197 if (dp->type == DEVICE_PATH_TYPE_END &&
1198 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1199 return ret;
1200 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1201 }
1202}
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218struct
1219efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
1220 const efi_guid_t *guid)
1221{
1222 struct efi_device_path *fp = lo->file_path;
1223 struct efi_device_path_vendor *vendor;
1224 int lo_len = lo->file_path_length;
1225
1226 for (; lo_len >= sizeof(struct efi_device_path);
1227 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1228 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1229 break;
1230 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1231 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1232 continue;
1233
1234 vendor = (struct efi_device_path_vendor *)fp;
1235 if (!guidcmp(&vendor->guid, guid))
1236 return efi_dp_dup(efi_dp_next(fp));
1237 }
1238 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1239
1240 return NULL;
1241}
1242
1243
1244
1245
1246
1247
1248
1249
1250struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1251{
1252 struct efi_device_path *dp = device_path;
1253
1254 while (dp) {
1255 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1256 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1257 struct efi_device_path_hard_drive_path *hd_dp =
1258 (struct efi_device_path_hard_drive_path *)dp;
1259
1260 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1261 hd_dp->signature_type == SIG_TYPE_GUID)
1262 return dp;
1263 }
1264 dp = efi_dp_next(dp);
1265 }
1266
1267 return NULL;
1268}
1269