blob: c941ea7717b29f58723fb736cec219c16c6d7741 [file] [log] [blame]
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001/*
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: */
19static 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: */
30static 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
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010039#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
40/*
41 * Determine if an MMC device is an SD card.
42 *
43 * @desc block device descriptor
44 * @return true if the device is an SD card
45 */
46static bool is_sd(struct blk_desc *desc)
47{
48 struct mmc *mmc = find_mmc_device(desc->devnum);
49
50 if (!mmc)
51 return false;
52
53 return IS_SD(mmc) != 0U;
54}
55#endif
56
Rob Clarkf90cb9f2017-09-13 18:05:28 -040057static void *dp_alloc(size_t sz)
58{
59 void *buf;
60
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010061 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
62 EFI_SUCCESS) {
63 debug("EFI: ERROR: out of memory in %s\n", __func__);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040064 return NULL;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010065 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -040066
67 return buf;
68}
69
70/*
71 * Iterate to next block in device-path, terminating (returning NULL)
72 * at /End* node.
73 */
74struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
75{
76 if (dp == NULL)
77 return NULL;
78 if (dp->type == DEVICE_PATH_TYPE_END)
79 return NULL;
80 dp = ((void *)dp) + dp->length;
81 if (dp->type == DEVICE_PATH_TYPE_END)
82 return NULL;
83 return (struct efi_device_path *)dp;
84}
85
86/*
87 * Compare two device-paths, stopping when the shorter of the two hits
88 * an End* node. This is useful to, for example, compare a device-path
89 * representing a device with one representing a file on the device, or
90 * a device with a parent device.
91 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +020092int efi_dp_match(const struct efi_device_path *a,
93 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -040094{
95 while (1) {
96 int ret;
97
98 ret = memcmp(&a->length, &b->length, sizeof(a->length));
99 if (ret)
100 return ret;
101
102 ret = memcmp(a, b, a->length);
103 if (ret)
104 return ret;
105
106 a = efi_dp_next(a);
107 b = efi_dp_next(b);
108
109 if (!a || !b)
110 return 0;
111 }
112}
113
114
115/*
116 * See UEFI spec (section 3.1.2, about short-form device-paths..
117 * tl;dr: we can have a device-path that starts with a USB WWID
118 * or USB Class node, and a few other cases which don't encode
119 * the full device path with bus hierarchy:
120 *
121 * - MESSAGING:USB_WWID
122 * - MESSAGING:USB_CLASS
123 * - MEDIA:FILE_PATH
124 * - MEDIA:HARD_DRIVE
125 * - MESSAGING:URI
126 */
127static struct efi_device_path *shorten_path(struct efi_device_path *dp)
128{
129 while (dp) {
130 /*
131 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
132 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
133 * so not sure when we would see these other cases.
134 */
135 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
136 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
137 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
138 return dp;
139
140 dp = efi_dp_next(dp);
141 }
142
143 return dp;
144}
145
146static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
147 struct efi_device_path **rem)
148{
149 struct efi_object *efiobj;
Alexander Graff9360fb2017-12-11 14:29:46 +0100150 unsigned int dp_size = efi_dp_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400151
152 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100153 struct efi_handler *handler;
154 struct efi_device_path *obj_dp;
155 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400156
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100157 ret = efi_search_protocol(efiobj->handle,
158 &efi_guid_device_path, &handler);
159 if (ret != EFI_SUCCESS)
160 continue;
161 obj_dp = handler->protocol_interface;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400162
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100163 do {
164 if (efi_dp_match(dp, obj_dp) == 0) {
165 if (rem) {
Alexander Graff9360fb2017-12-11 14:29:46 +0100166 /*
167 * Allow partial matches, but inform
168 * the caller.
169 */
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100170 *rem = ((void *)dp) +
171 efi_dp_size(obj_dp);
Alexander Graff9360fb2017-12-11 14:29:46 +0100172 return efiobj;
173 } else {
174 /* Only return on exact matches */
175 if (efi_dp_size(obj_dp) == dp_size)
176 return efiobj;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400177 }
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100178 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400179
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100180 obj_dp = shorten_path(efi_dp_next(obj_dp));
181 } while (short_path && obj_dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400182 }
183
184 return NULL;
185}
186
187
188/*
189 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
190 * remaining part of the device path after the matched object.
191 */
192struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
193 struct efi_device_path **rem)
194{
195 struct efi_object *efiobj;
196
Alexander Graff9360fb2017-12-11 14:29:46 +0100197 /* Search for an exact match first */
198 efiobj = find_obj(dp, false, NULL);
199
200 /* Then for a fuzzy match */
201 if (!efiobj)
202 efiobj = find_obj(dp, false, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400203
Alexander Graff9360fb2017-12-11 14:29:46 +0100204 /* And now for a fuzzy short match */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400205 if (!efiobj)
206 efiobj = find_obj(dp, true, rem);
207
208 return efiobj;
209}
210
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100211/*
212 * Determine the last device path node that is not the end node.
213 *
214 * @dp device path
215 * @return last node before the end node if it exists
216 * otherwise NULL
217 */
218const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
219{
220 struct efi_device_path *ret;
221
222 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
223 return NULL;
224 while (dp) {
225 ret = (struct efi_device_path *)dp;
226 dp = efi_dp_next(dp);
227 }
228 return ret;
229}
230
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400231/* return size not including End node: */
232unsigned efi_dp_size(const struct efi_device_path *dp)
233{
234 unsigned sz = 0;
235
236 while (dp) {
237 sz += dp->length;
238 dp = efi_dp_next(dp);
239 }
240
241 return sz;
242}
243
244struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
245{
246 struct efi_device_path *ndp;
247 unsigned sz = efi_dp_size(dp) + sizeof(END);
248
249 if (!dp)
250 return NULL;
251
252 ndp = dp_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100253 if (!ndp)
254 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400255 memcpy(ndp, dp, sz);
256
257 return ndp;
258}
259
260struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
261 const struct efi_device_path *dp2)
262{
263 struct efi_device_path *ret;
264
265 if (!dp1) {
266 ret = efi_dp_dup(dp2);
267 } else if (!dp2) {
268 ret = efi_dp_dup(dp1);
269 } else {
270 /* both dp1 and dp2 are non-null */
271 unsigned sz1 = efi_dp_size(dp1);
272 unsigned sz2 = efi_dp_size(dp2);
273 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100274 if (!p)
275 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400276 memcpy(p, dp1, sz1);
277 memcpy(p + sz1, dp2, sz2);
278 memcpy(p + sz1 + sz2, &END, sizeof(END));
279 ret = p;
280 }
281
282 return ret;
283}
284
285struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
286 const struct efi_device_path *node)
287{
288 struct efi_device_path *ret;
289
290 if (!node && !dp) {
291 ret = efi_dp_dup(&END);
292 } else if (!node) {
293 ret = efi_dp_dup(dp);
294 } else if (!dp) {
295 unsigned sz = node->length;
296 void *p = dp_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100297 if (!p)
298 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400299 memcpy(p, node, sz);
300 memcpy(p + sz, &END, sizeof(END));
301 ret = p;
302 } else {
303 /* both dp and node are non-null */
304 unsigned sz = efi_dp_size(dp);
305 void *p = dp_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100306 if (!p)
307 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400308 memcpy(p, dp, sz);
309 memcpy(p + sz, node, node->length);
310 memcpy(p + sz + node->length, &END, sizeof(END));
311 ret = p;
312 }
313
314 return ret;
315}
316
317#ifdef CONFIG_DM
318/* size of device-path not including END node for device and all parents
319 * up to the root device.
320 */
321static unsigned dp_size(struct udevice *dev)
322{
323 if (!dev || !dev->driver)
324 return sizeof(ROOT);
325
326 switch (dev->driver->id) {
327 case UCLASS_ROOT:
328 case UCLASS_SIMPLE_BUS:
329 /* stop traversing parents at this point: */
330 return sizeof(ROOT);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100331#ifdef CONFIG_BLK
332 case UCLASS_BLK:
333 switch (dev->parent->uclass->uc_drv->id) {
334#ifdef CONFIG_IDE
335 case UCLASS_IDE:
336 return dp_size(dev->parent) +
337 sizeof(struct efi_device_path_atapi);
338#endif
339#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
340 case UCLASS_SCSI:
341 return dp_size(dev->parent) +
342 sizeof(struct efi_device_path_scsi);
343#endif
344 default:
345 return dp_size(dev->parent);
346 }
347#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400348 case UCLASS_MMC:
349 return dp_size(dev->parent) +
350 sizeof(struct efi_device_path_sd_mmc_path);
351 case UCLASS_MASS_STORAGE:
352 case UCLASS_USB_HUB:
353 return dp_size(dev->parent) +
354 sizeof(struct efi_device_path_usb_class);
355 default:
356 /* just skip over unknown classes: */
357 return dp_size(dev->parent);
358 }
359}
360
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100361/*
362 * Recursively build a device path.
363 *
364 * @buf pointer to the end of the device path
365 * @dev device
366 * @return pointer to the end of the device path
367 */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400368static void *dp_fill(void *buf, struct udevice *dev)
369{
370 if (!dev || !dev->driver)
371 return buf;
372
373 switch (dev->driver->id) {
374 case UCLASS_ROOT:
375 case UCLASS_SIMPLE_BUS: {
376 /* stop traversing parents at this point: */
377 struct efi_device_path_vendor *vdp = buf;
378 *vdp = ROOT;
379 return &vdp[1];
380 }
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100381#ifdef CONFIG_BLK
382 case UCLASS_BLK:
383 switch (dev->parent->uclass->uc_drv->id) {
384#ifdef CONFIG_IDE
385 case UCLASS_IDE: {
386 struct efi_device_path_atapi *dp =
387 dp_fill(buf, dev->parent);
388 struct blk_desc *desc = dev_get_uclass_platdata(dev);
389
390 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
391 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
392 dp->dp.length = sizeof(*dp);
393 dp->logical_unit_number = desc->devnum;
394 dp->primary_secondary = IDE_BUS(desc->devnum);
395 dp->slave_master = desc->devnum %
396 (CONFIG_SYS_IDE_MAXDEVICE /
397 CONFIG_SYS_IDE_MAXBUS);
398 return &dp[1];
399 }
400#endif
401#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
402 case UCLASS_SCSI: {
403 struct efi_device_path_scsi *dp =
404 dp_fill(buf, dev->parent);
405 struct blk_desc *desc = dev_get_uclass_platdata(dev);
406
407 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
408 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
409 dp->dp.length = sizeof(*dp);
410 dp->logical_unit_number = desc->lun;
411 dp->target_id = desc->target;
412 return &dp[1];
413 }
414#endif
415 default:
416 printf("unhandled parent class: %s (%u)\n",
417 dev->name, dev->driver->id);
418 return dp_fill(buf, dev->parent);
419 }
420#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400421#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
422 case UCLASS_MMC: {
423 struct efi_device_path_sd_mmc_path *sddp =
424 dp_fill(buf, dev->parent);
425 struct mmc *mmc = mmc_get_mmc_dev(dev);
426 struct blk_desc *desc = mmc_get_blk_desc(mmc);
427
428 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100429 sddp->dp.sub_type = is_sd(desc) ?
430 DEVICE_PATH_SUB_TYPE_MSG_SD :
431 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400432 sddp->dp.length = sizeof(*sddp);
433 sddp->slot_number = dev->seq;
434
435 return &sddp[1];
436 }
437#endif
438 case UCLASS_MASS_STORAGE:
439 case UCLASS_USB_HUB: {
440 struct efi_device_path_usb_class *udp =
441 dp_fill(buf, dev->parent);
442 struct usb_device *udev = dev_get_parent_priv(dev);
443 struct usb_device_descriptor *desc = &udev->descriptor;
444
445 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
446 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
447 udp->dp.length = sizeof(*udp);
448 udp->vendor_id = desc->idVendor;
449 udp->product_id = desc->idProduct;
450 udp->device_class = desc->bDeviceClass;
451 udp->device_subclass = desc->bDeviceSubClass;
452 udp->device_protocol = desc->bDeviceProtocol;
453
454 return &udp[1];
455 }
456 default:
457 debug("unhandled device class: %s (%u)\n",
458 dev->name, dev->driver->id);
459 return dp_fill(buf, dev->parent);
460 }
461}
462
463/* Construct a device-path from a device: */
464struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
465{
466 void *buf, *start;
467
468 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100469 if (!buf)
470 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400471 buf = dp_fill(buf, dev);
472 *((struct efi_device_path *)buf) = END;
473
474 return start;
475}
476#endif
477
478static unsigned dp_part_size(struct blk_desc *desc, int part)
479{
480 unsigned dpsize;
481
482#ifdef CONFIG_BLK
Heinrich Schuchardt3a615c82017-12-11 12:56:43 +0100483 {
484 struct udevice *dev;
485 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
486
487 if (ret)
488 dev = desc->bdev->parent;
489 dpsize = dp_size(dev);
490 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400491#else
492 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
493#endif
494
495 if (part == 0) /* the actual disk, not a partition */
496 return dpsize;
497
498 if (desc->part_type == PART_TYPE_ISO)
499 dpsize += sizeof(struct efi_device_path_cdrom_path);
500 else
501 dpsize += sizeof(struct efi_device_path_hard_drive_path);
502
503 return dpsize;
504}
505
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100506/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100507 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100508 *
509 * @buf buffer to which the device path is wirtten
510 * @desc block device descriptor
511 * @part partition number, 0 identifies a block device
512 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100513static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400514{
515 disk_partition_t info;
516
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400517 part_get_info(desc, part, &info);
518
519 if (desc->part_type == PART_TYPE_ISO) {
520 struct efi_device_path_cdrom_path *cddp = buf;
521
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100522 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400523 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
524 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
525 cddp->dp.length = sizeof(*cddp);
526 cddp->partition_start = info.start;
527 cddp->partition_end = info.size;
528
529 buf = &cddp[1];
530 } else {
531 struct efi_device_path_hard_drive_path *hddp = buf;
532
533 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
534 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
535 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100536 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400537 hddp->partition_start = info.start;
538 hddp->partition_end = info.size;
539 if (desc->part_type == PART_TYPE_EFI)
540 hddp->partmap_type = 2;
541 else
542 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100543
544 switch (desc->sig_type) {
545 case SIG_TYPE_NONE:
546 default:
547 hddp->signature_type = 0;
548 memset(hddp->partition_signature, 0,
549 sizeof(hddp->partition_signature));
550 break;
551 case SIG_TYPE_MBR:
552 hddp->signature_type = 1;
553 memset(hddp->partition_signature, 0,
554 sizeof(hddp->partition_signature));
555 memcpy(hddp->partition_signature, &desc->mbr_sig,
556 sizeof(desc->mbr_sig));
557 break;
558 case SIG_TYPE_GUID:
559 hddp->signature_type = 2;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400560 memcpy(hddp->partition_signature, &desc->guid_sig,
561 sizeof(hddp->partition_signature));
Jonathan Gray84b4d702017-11-22 14:18:59 +1100562 break;
563 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400564
565 buf = &hddp[1];
566 }
567
568 return buf;
569}
570
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100571/*
572 * Create a device path for a block device or one of its partitions.
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 */
578static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
579{
580#ifdef CONFIG_BLK
581 {
582 struct udevice *dev;
583 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
584
585 if (ret)
586 dev = desc->bdev->parent;
587 buf = dp_fill(buf, dev);
588 }
589#else
590 /*
591 * We *could* make a more accurate path, by looking at if_type
592 * and handling all the different cases like we do for non-
593 * legacy (ie CONFIG_BLK=y) case. But most important thing
594 * is just to have a unique device-path for if_type+devnum.
595 * So map things to a fictitious USB device.
596 */
597 struct efi_device_path_usb *udp;
598
599 memcpy(buf, &ROOT, sizeof(ROOT));
600 buf += sizeof(ROOT);
601
602 udp = buf;
603 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
604 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
605 udp->dp.length = sizeof(*udp);
606 udp->parent_port_number = desc->if_type;
607 udp->usb_interface = desc->devnum;
608 buf = &udp[1];
609#endif
610
611 if (part == 0) /* the actual disk, not a partition */
612 return buf;
613
614 return dp_part_node(buf, desc, part);
615}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400616
617/* Construct a device-path from a partition on a blk device: */
618struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
619{
620 void *buf, *start;
621
622 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100623 if (!buf)
624 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400625
626 buf = dp_part_fill(buf, desc, part);
627
628 *((struct efi_device_path *)buf) = END;
629
630 return start;
631}
632
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100633/*
634 * Create a device node for a block device partition.
635 *
636 * @buf buffer to which the device path is wirtten
637 * @desc block device descriptor
638 * @part partition number, 0 identifies a block device
639 */
640struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
641{
642 efi_uintn_t dpsize;
643 void *buf;
644
645 if (desc->part_type == PART_TYPE_ISO)
646 dpsize = sizeof(struct efi_device_path_cdrom_path);
647 else
648 dpsize = sizeof(struct efi_device_path_hard_drive_path);
649 buf = dp_alloc(dpsize);
650
651 dp_part_node(buf, desc, part);
652
653 return buf;
654}
655
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400656/* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
657static void path_to_uefi(u16 *uefi, const char *path)
658{
659 while (*path) {
660 char c = *(path++);
661 if (c == '/')
662 c = '\\';
663 *(uefi++) = c;
664 }
665 *uefi = '\0';
666}
667
668/*
669 * If desc is NULL, this creates a path with only the file component,
670 * otherwise it creates a full path with both device and file components
671 */
672struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
673 const char *path)
674{
675 struct efi_device_path_file_path *fp;
676 void *buf, *start;
677 unsigned dpsize = 0, fpsize;
678
679 if (desc)
680 dpsize = dp_part_size(desc, part);
681
682 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
683 dpsize += fpsize;
684
685 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100686 if (!buf)
687 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400688
689 if (desc)
690 buf = dp_part_fill(buf, desc, part);
691
692 /* add file-path: */
693 fp = buf;
694 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
695 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
696 fp->dp.length = fpsize;
697 path_to_uefi(fp->str, path);
698 buf += fpsize;
699
700 *((struct efi_device_path *)buf) = END;
701
702 return start;
703}
704
705#ifdef CONFIG_NET
706struct efi_device_path *efi_dp_from_eth(void)
707{
708 struct efi_device_path_mac_addr *ndp;
709 void *buf, *start;
710 unsigned dpsize = 0;
711
712 assert(eth_get_dev());
713
714#ifdef CONFIG_DM_ETH
715 dpsize += dp_size(eth_get_dev());
716#else
717 dpsize += sizeof(ROOT);
718#endif
719 dpsize += sizeof(*ndp);
720
721 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100722 if (!buf)
723 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400724
725#ifdef CONFIG_DM_ETH
726 buf = dp_fill(buf, eth_get_dev());
727#else
728 memcpy(buf, &ROOT, sizeof(ROOT));
729 buf += sizeof(ROOT);
730#endif
731
732 ndp = buf;
733 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
734 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
735 ndp->dp.length = sizeof(*ndp);
736 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
737 buf = &ndp[1];
738
739 *((struct efi_device_path *)buf) = END;
740
741 return start;
742}
743#endif
744
Rob Clark18ceba72017-10-10 08:23:06 -0400745/* Construct a device-path for memory-mapped image */
746struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
747 uint64_t start_address,
748 uint64_t end_address)
749{
750 struct efi_device_path_memory *mdp;
751 void *buf, *start;
752
753 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100754 if (!buf)
755 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -0400756
757 mdp = buf;
758 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
759 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
760 mdp->dp.length = sizeof(*mdp);
761 mdp->memory_type = memory_type;
762 mdp->start_address = start_address;
763 mdp->end_address = end_address;
764 buf = &mdp[1];
765
766 *((struct efi_device_path *)buf) = END;
767
768 return start;
769}
770
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400771/*
772 * Helper to split a full device path (containing both device and file
773 * parts) into it's constituent parts.
774 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100775efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
776 struct efi_device_path **device_path,
777 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400778{
779 struct efi_device_path *p, *dp, *fp;
780
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100781 *device_path = NULL;
782 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400783 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100784 if (!dp)
785 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400786 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100787 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400788 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100789 if (!p)
790 return EFI_OUT_OF_RESOURCES;
791 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400792 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100793 if (!fp)
794 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400795 p->type = DEVICE_PATH_TYPE_END;
796 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
797 p->length = sizeof(*p);
798
799 *device_path = dp;
800 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100801 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400802}