blob: 0cd0b459e3dec6ac2601a6bcc66f99936a4ed529 [file] [log] [blame]
Rob Clarkb66c60d2017-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
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +01009#define LOG_CATEGORY LOGL_ERR
10
Rob Clarkb66c60d2017-09-13 18:05:28 -040011#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: */
21static 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: */
32static 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
Heinrich Schuchardt66b051d2017-12-11 12:56:39 +010041#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 */
48static 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
Rob Clarkb66c60d2017-09-13 18:05:28 -040059static void *dp_alloc(size_t sz)
60{
61 void *buf;
62
Heinrich Schuchardt04298682018-01-19 20:24:37 +010063 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
64 EFI_SUCCESS) {
65 debug("EFI: ERROR: out of memory in %s\n", __func__);
Rob Clarkb66c60d2017-09-13 18:05:28 -040066 return NULL;
Heinrich Schuchardt04298682018-01-19 20:24:37 +010067 }
Rob Clarkb66c60d2017-09-13 18:05:28 -040068
Patrick Wildteab2dc32018-03-25 19:54:03 +020069 memset(buf, 0, sz);
Rob Clarkb66c60d2017-09-13 18:05:28 -040070 return buf;
71}
72
73/*
74 * Iterate to next block in device-path, terminating (returning NULL)
75 * at /End* node.
76 */
77struct 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 */
Heinrich Schuchardtff401d32017-10-26 19:25:48 +020095int efi_dp_match(const struct efi_device_path *a,
96 const struct efi_device_path *b)
Rob Clarkb66c60d2017-09-13 18:05:28 -040097{
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
Rob Clarkb66c60d2017-09-13 18:05:28 -0400117/*
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 */
129static 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
148static 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;
Alexander Graf905cb9e2017-12-11 14:29:46 +0100152 unsigned int dp_size = efi_dp_size(dp);
Rob Clarkb66c60d2017-09-13 18:05:28 -0400153
154 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt72292ab2017-11-26 14:05:16 +0100155 struct efi_handler *handler;
156 struct efi_device_path *obj_dp;
157 efi_status_t ret;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400158
Heinrich Schuchardt72292ab2017-11-26 14:05:16 +0100159 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;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400164
Heinrich Schuchardt72292ab2017-11-26 14:05:16 +0100165 do {
166 if (efi_dp_match(dp, obj_dp) == 0) {
167 if (rem) {
Alexander Graf905cb9e2017-12-11 14:29:46 +0100168 /*
169 * Allow partial matches, but inform
170 * the caller.
171 */
Heinrich Schuchardt72292ab2017-11-26 14:05:16 +0100172 *rem = ((void *)dp) +
173 efi_dp_size(obj_dp);
Alexander Graf905cb9e2017-12-11 14:29:46 +0100174 return efiobj;
175 } else {
176 /* Only return on exact matches */
177 if (efi_dp_size(obj_dp) == dp_size)
178 return efiobj;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400179 }
Heinrich Schuchardt72292ab2017-11-26 14:05:16 +0100180 }
Rob Clarkb66c60d2017-09-13 18:05:28 -0400181
Heinrich Schuchardt72292ab2017-11-26 14:05:16 +0100182 obj_dp = shorten_path(efi_dp_next(obj_dp));
183 } while (short_path && obj_dp);
Rob Clarkb66c60d2017-09-13 18:05:28 -0400184 }
185
186 return NULL;
187}
188
Rob Clarkb66c60d2017-09-13 18:05:28 -0400189/*
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 */
193struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
194 struct efi_device_path **rem)
195{
196 struct efi_object *efiobj;
197
Alexander Graf905cb9e2017-12-11 14:29:46 +0100198 /* Search for an exact match first */
199 efiobj = find_obj(dp, false, NULL);
Rob Clarkb66c60d2017-09-13 18:05:28 -0400200
Alexander Graf905cb9e2017-12-11 14:29:46 +0100201 /* Then for a fuzzy match */
202 if (!efiobj)
203 efiobj = find_obj(dp, false, rem);
204
205 /* And now for a fuzzy short match */
Rob Clarkb66c60d2017-09-13 18:05:28 -0400206 if (!efiobj)
207 efiobj = find_obj(dp, true, rem);
208
209 return efiobj;
210}
211
Heinrich Schuchardt65436f92018-01-19 20:24:49 +0100212/*
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 */
219const 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
Rob Clarkb66c60d2017-09-13 18:05:28 -0400232/* return size not including End node: */
233unsigned efi_dp_size(const struct efi_device_path *dp)
234{
235 unsigned sz = 0;
236
237 while (dp) {
238 sz += dp->length;
239 dp = efi_dp_next(dp);
240 }
241
242 return sz;
243}
244
245struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
246{
247 struct efi_device_path *ndp;
248 unsigned sz = efi_dp_size(dp) + sizeof(END);
249
250 if (!dp)
251 return NULL;
252
253 ndp = dp_alloc(sz);
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100254 if (!ndp)
255 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400256 memcpy(ndp, dp, sz);
257
258 return ndp;
259}
260
261struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
262 const struct efi_device_path *dp2)
263{
264 struct efi_device_path *ret;
265
266 if (!dp1) {
267 ret = efi_dp_dup(dp2);
268 } else if (!dp2) {
269 ret = efi_dp_dup(dp1);
270 } else {
271 /* both dp1 and dp2 are non-null */
272 unsigned sz1 = efi_dp_size(dp1);
273 unsigned sz2 = efi_dp_size(dp2);
274 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100275 if (!p)
276 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400277 memcpy(p, dp1, sz1);
278 memcpy(p + sz1, dp2, sz2);
279 memcpy(p + sz1 + sz2, &END, sizeof(END));
280 ret = p;
281 }
282
283 return ret;
284}
285
286struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
287 const struct efi_device_path *node)
288{
289 struct efi_device_path *ret;
290
291 if (!node && !dp) {
292 ret = efi_dp_dup(&END);
293 } else if (!node) {
294 ret = efi_dp_dup(dp);
295 } else if (!dp) {
296 unsigned sz = node->length;
297 void *p = dp_alloc(sz + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100298 if (!p)
299 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400300 memcpy(p, node, sz);
301 memcpy(p + sz, &END, sizeof(END));
302 ret = p;
303 } else {
304 /* both dp and node are non-null */
305 unsigned sz = efi_dp_size(dp);
306 void *p = dp_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100307 if (!p)
308 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400309 memcpy(p, dp, sz);
310 memcpy(p + sz, node, node->length);
311 memcpy(p + sz + node->length, &END, sizeof(END));
312 ret = p;
313 }
314
315 return ret;
316}
317
Heinrich Schuchardt211314c2018-04-16 07:59:05 +0200318struct efi_device_path *efi_dp_create_device_node(const u8 type,
319 const u8 sub_type,
320 const u16 length)
321{
322 struct efi_device_path *ret;
323
324 ret = dp_alloc(length);
325 if (!ret)
326 return ret;
327 ret->type = type;
328 ret->sub_type = sub_type;
329 ret->length = length;
330 return ret;
331}
332
Rob Clarkb66c60d2017-09-13 18:05:28 -0400333#ifdef CONFIG_DM
334/* size of device-path not including END node for device and all parents
335 * up to the root device.
336 */
337static unsigned dp_size(struct udevice *dev)
338{
339 if (!dev || !dev->driver)
340 return sizeof(ROOT);
341
342 switch (dev->driver->id) {
343 case UCLASS_ROOT:
344 case UCLASS_SIMPLE_BUS:
345 /* stop traversing parents at this point: */
346 return sizeof(ROOT);
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100347 case UCLASS_ETH:
348 return dp_size(dev->parent) +
349 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardtaf3106a2017-12-11 12:56:44 +0100350#ifdef CONFIG_BLK
351 case UCLASS_BLK:
352 switch (dev->parent->uclass->uc_drv->id) {
353#ifdef CONFIG_IDE
354 case UCLASS_IDE:
355 return dp_size(dev->parent) +
356 sizeof(struct efi_device_path_atapi);
357#endif
358#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
359 case UCLASS_SCSI:
360 return dp_size(dev->parent) +
361 sizeof(struct efi_device_path_scsi);
362#endif
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100363#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
364 case UCLASS_MMC:
365 return dp_size(dev->parent) +
366 sizeof(struct efi_device_path_sd_mmc_path);
367#endif
Heinrich Schuchardtaf3106a2017-12-11 12:56:44 +0100368 default:
369 return dp_size(dev->parent);
370 }
371#endif
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100372#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
Rob Clarkb66c60d2017-09-13 18:05:28 -0400373 case UCLASS_MMC:
374 return dp_size(dev->parent) +
375 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100376#endif
Rob Clarkb66c60d2017-09-13 18:05:28 -0400377 case UCLASS_MASS_STORAGE:
378 case UCLASS_USB_HUB:
379 return dp_size(dev->parent) +
380 sizeof(struct efi_device_path_usb_class);
381 default:
382 /* just skip over unknown classes: */
383 return dp_size(dev->parent);
384 }
385}
386
Heinrich Schuchardtaf3106a2017-12-11 12:56:44 +0100387/*
388 * Recursively build a device path.
389 *
390 * @buf pointer to the end of the device path
391 * @dev device
392 * @return pointer to the end of the device path
393 */
Rob Clarkb66c60d2017-09-13 18:05:28 -0400394static void *dp_fill(void *buf, struct udevice *dev)
395{
396 if (!dev || !dev->driver)
397 return buf;
398
399 switch (dev->driver->id) {
400 case UCLASS_ROOT:
401 case UCLASS_SIMPLE_BUS: {
402 /* stop traversing parents at this point: */
403 struct efi_device_path_vendor *vdp = buf;
404 *vdp = ROOT;
405 return &vdp[1];
406 }
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100407#ifdef CONFIG_DM_ETH
408 case UCLASS_ETH: {
409 struct efi_device_path_mac_addr *dp =
410 dp_fill(buf, dev->parent);
411 struct eth_pdata *pdata = dev->platdata;
412
413 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
414 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
415 dp->dp.length = sizeof(*dp);
416 memset(&dp->mac, 0, sizeof(dp->mac));
417 /* We only support IPv4 */
418 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
419 /* Ethernet */
420 dp->if_type = 1;
421 return &dp[1];
422 }
423#endif
Heinrich Schuchardtaf3106a2017-12-11 12:56:44 +0100424#ifdef CONFIG_BLK
425 case UCLASS_BLK:
426 switch (dev->parent->uclass->uc_drv->id) {
427#ifdef CONFIG_IDE
428 case UCLASS_IDE: {
429 struct efi_device_path_atapi *dp =
430 dp_fill(buf, dev->parent);
431 struct blk_desc *desc = dev_get_uclass_platdata(dev);
432
433 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
434 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
435 dp->dp.length = sizeof(*dp);
436 dp->logical_unit_number = desc->devnum;
437 dp->primary_secondary = IDE_BUS(desc->devnum);
438 dp->slave_master = desc->devnum %
439 (CONFIG_SYS_IDE_MAXDEVICE /
440 CONFIG_SYS_IDE_MAXBUS);
441 return &dp[1];
442 }
443#endif
444#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
445 case UCLASS_SCSI: {
446 struct efi_device_path_scsi *dp =
447 dp_fill(buf, dev->parent);
448 struct blk_desc *desc = dev_get_uclass_platdata(dev);
449
450 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
451 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
452 dp->dp.length = sizeof(*dp);
453 dp->logical_unit_number = desc->lun;
454 dp->target_id = desc->target;
455 return &dp[1];
456 }
457#endif
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100458#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
459 case UCLASS_MMC: {
460 struct efi_device_path_sd_mmc_path *sddp =
461 dp_fill(buf, dev->parent);
462 struct blk_desc *desc = dev_get_uclass_platdata(dev);
463
464 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
465 sddp->dp.sub_type = is_sd(desc) ?
466 DEVICE_PATH_SUB_TYPE_MSG_SD :
467 DEVICE_PATH_SUB_TYPE_MSG_MMC;
468 sddp->dp.length = sizeof(*sddp);
469 sddp->slot_number = dev->seq;
470 return &sddp[1];
471 }
472#endif
Heinrich Schuchardtaf3106a2017-12-11 12:56:44 +0100473 default:
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100474 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
475 __FILE__, __LINE__, __func__,
476 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardtaf3106a2017-12-11 12:56:44 +0100477 return dp_fill(buf, dev->parent);
478 }
479#endif
Rob Clarkb66c60d2017-09-13 18:05:28 -0400480#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
481 case UCLASS_MMC: {
482 struct efi_device_path_sd_mmc_path *sddp =
483 dp_fill(buf, dev->parent);
484 struct mmc *mmc = mmc_get_mmc_dev(dev);
485 struct blk_desc *desc = mmc_get_blk_desc(mmc);
486
487 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt66b051d2017-12-11 12:56:39 +0100488 sddp->dp.sub_type = is_sd(desc) ?
489 DEVICE_PATH_SUB_TYPE_MSG_SD :
490 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400491 sddp->dp.length = sizeof(*sddp);
492 sddp->slot_number = dev->seq;
493
494 return &sddp[1];
495 }
496#endif
497 case UCLASS_MASS_STORAGE:
498 case UCLASS_USB_HUB: {
499 struct efi_device_path_usb_class *udp =
500 dp_fill(buf, dev->parent);
501 struct usb_device *udev = dev_get_parent_priv(dev);
502 struct usb_device_descriptor *desc = &udev->descriptor;
503
504 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
505 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
506 udp->dp.length = sizeof(*udp);
507 udp->vendor_id = desc->idVendor;
508 udp->product_id = desc->idProduct;
509 udp->device_class = desc->bDeviceClass;
510 udp->device_subclass = desc->bDeviceSubClass;
511 udp->device_protocol = desc->bDeviceProtocol;
512
513 return &udp[1];
514 }
515 default:
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100516 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
517 __FILE__, __LINE__, __func__,
Rob Clarkb66c60d2017-09-13 18:05:28 -0400518 dev->name, dev->driver->id);
519 return dp_fill(buf, dev->parent);
520 }
521}
522
523/* Construct a device-path from a device: */
524struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
525{
526 void *buf, *start;
527
528 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100529 if (!buf)
530 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400531 buf = dp_fill(buf, dev);
532 *((struct efi_device_path *)buf) = END;
533
534 return start;
535}
536#endif
537
538static unsigned dp_part_size(struct blk_desc *desc, int part)
539{
540 unsigned dpsize;
541
542#ifdef CONFIG_BLK
Heinrich Schuchardt2bc61b82017-12-11 12:56:43 +0100543 {
544 struct udevice *dev;
545 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
546
547 if (ret)
548 dev = desc->bdev->parent;
549 dpsize = dp_size(dev);
550 }
Rob Clarkb66c60d2017-09-13 18:05:28 -0400551#else
552 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
553#endif
554
555 if (part == 0) /* the actual disk, not a partition */
556 return dpsize;
557
558 if (desc->part_type == PART_TYPE_ISO)
559 dpsize += sizeof(struct efi_device_path_cdrom_path);
560 else
561 dpsize += sizeof(struct efi_device_path_hard_drive_path);
562
563 return dpsize;
564}
565
Heinrich Schuchardtbde6bfe2017-12-11 12:56:42 +0100566/*
Heinrich Schuchardt98d48bd2018-01-19 20:24:46 +0100567 * Create a device node for a block device partition.
Heinrich Schuchardtbde6bfe2017-12-11 12:56:42 +0100568 *
569 * @buf buffer to which the device path is wirtten
570 * @desc block device descriptor
571 * @part partition number, 0 identifies a block device
572 */
Heinrich Schuchardt98d48bd2018-01-19 20:24:46 +0100573static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkb66c60d2017-09-13 18:05:28 -0400574{
575 disk_partition_t info;
576
Rob Clarkb66c60d2017-09-13 18:05:28 -0400577 part_get_info(desc, part, &info);
578
579 if (desc->part_type == PART_TYPE_ISO) {
580 struct efi_device_path_cdrom_path *cddp = buf;
581
Heinrich Schuchardt7b982f02017-12-11 12:56:40 +0100582 cddp->boot_entry = part;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400583 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
584 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
585 cddp->dp.length = sizeof(*cddp);
586 cddp->partition_start = info.start;
587 cddp->partition_end = info.size;
588
589 buf = &cddp[1];
590 } else {
591 struct efi_device_path_hard_drive_path *hddp = buf;
592
593 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
594 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
595 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt7b982f02017-12-11 12:56:40 +0100596 hddp->partition_number = part;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400597 hddp->partition_start = info.start;
598 hddp->partition_end = info.size;
599 if (desc->part_type == PART_TYPE_EFI)
600 hddp->partmap_type = 2;
601 else
602 hddp->partmap_type = 1;
Jonathan Grayb6e9e092017-11-22 14:18:59 +1100603
604 switch (desc->sig_type) {
605 case SIG_TYPE_NONE:
606 default:
607 hddp->signature_type = 0;
608 memset(hddp->partition_signature, 0,
609 sizeof(hddp->partition_signature));
610 break;
611 case SIG_TYPE_MBR:
612 hddp->signature_type = 1;
613 memset(hddp->partition_signature, 0,
614 sizeof(hddp->partition_signature));
615 memcpy(hddp->partition_signature, &desc->mbr_sig,
616 sizeof(desc->mbr_sig));
617 break;
618 case SIG_TYPE_GUID:
619 hddp->signature_type = 2;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400620 memcpy(hddp->partition_signature, &desc->guid_sig,
621 sizeof(hddp->partition_signature));
Jonathan Grayb6e9e092017-11-22 14:18:59 +1100622 break;
623 }
Rob Clarkb66c60d2017-09-13 18:05:28 -0400624
625 buf = &hddp[1];
626 }
627
628 return buf;
629}
630
Heinrich Schuchardt98d48bd2018-01-19 20:24:46 +0100631/*
632 * Create a device path for a block device or one of its partitions.
633 *
634 * @buf buffer to which the device path is wirtten
635 * @desc block device descriptor
636 * @part partition number, 0 identifies a block device
637 */
638static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
639{
640#ifdef CONFIG_BLK
641 {
642 struct udevice *dev;
643 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
644
645 if (ret)
646 dev = desc->bdev->parent;
647 buf = dp_fill(buf, dev);
648 }
649#else
650 /*
651 * We *could* make a more accurate path, by looking at if_type
652 * and handling all the different cases like we do for non-
653 * legacy (ie CONFIG_BLK=y) case. But most important thing
654 * is just to have a unique device-path for if_type+devnum.
655 * So map things to a fictitious USB device.
656 */
657 struct efi_device_path_usb *udp;
658
659 memcpy(buf, &ROOT, sizeof(ROOT));
660 buf += sizeof(ROOT);
661
662 udp = buf;
663 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
664 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
665 udp->dp.length = sizeof(*udp);
666 udp->parent_port_number = desc->if_type;
667 udp->usb_interface = desc->devnum;
668 buf = &udp[1];
669#endif
670
671 if (part == 0) /* the actual disk, not a partition */
672 return buf;
673
674 return dp_part_node(buf, desc, part);
675}
Rob Clarkb66c60d2017-09-13 18:05:28 -0400676
677/* Construct a device-path from a partition on a blk device: */
678struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
679{
680 void *buf, *start;
681
682 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100683 if (!buf)
684 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400685
686 buf = dp_part_fill(buf, desc, part);
687
688 *((struct efi_device_path *)buf) = END;
689
690 return start;
691}
692
Heinrich Schuchardt98d48bd2018-01-19 20:24:46 +0100693/*
694 * Create a device node for a block device partition.
695 *
696 * @buf buffer to which the device path is wirtten
697 * @desc block device descriptor
698 * @part partition number, 0 identifies a block device
699 */
700struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
701{
702 efi_uintn_t dpsize;
703 void *buf;
704
705 if (desc->part_type == PART_TYPE_ISO)
706 dpsize = sizeof(struct efi_device_path_cdrom_path);
707 else
708 dpsize = sizeof(struct efi_device_path_hard_drive_path);
709 buf = dp_alloc(dpsize);
710
711 dp_part_node(buf, desc, part);
712
713 return buf;
714}
715
Rob Clarkb66c60d2017-09-13 18:05:28 -0400716/* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
717static void path_to_uefi(u16 *uefi, const char *path)
718{
719 while (*path) {
720 char c = *(path++);
721 if (c == '/')
722 c = '\\';
723 *(uefi++) = c;
724 }
725 *uefi = '\0';
726}
727
728/*
729 * If desc is NULL, this creates a path with only the file component,
730 * otherwise it creates a full path with both device and file components
731 */
732struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
733 const char *path)
734{
735 struct efi_device_path_file_path *fp;
736 void *buf, *start;
737 unsigned dpsize = 0, fpsize;
738
739 if (desc)
740 dpsize = dp_part_size(desc, part);
741
742 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
743 dpsize += fpsize;
744
745 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100746 if (!buf)
747 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400748
749 if (desc)
750 buf = dp_part_fill(buf, desc, part);
751
752 /* add file-path: */
753 fp = buf;
754 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
755 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
756 fp->dp.length = fpsize;
757 path_to_uefi(fp->str, path);
758 buf += fpsize;
759
760 *((struct efi_device_path *)buf) = END;
761
762 return start;
763}
764
Joe Hershberger092f2f32018-04-13 15:26:39 -0500765#ifdef CONFIG_NET
Rob Clarkb66c60d2017-09-13 18:05:28 -0400766struct efi_device_path *efi_dp_from_eth(void)
767{
Alexander Graff9cfad12018-03-15 17:33:38 +0100768#ifndef CONFIG_DM_ETH
Rob Clarkb66c60d2017-09-13 18:05:28 -0400769 struct efi_device_path_mac_addr *ndp;
Alexander Graff9cfad12018-03-15 17:33:38 +0100770#endif
Rob Clarkb66c60d2017-09-13 18:05:28 -0400771 void *buf, *start;
772 unsigned dpsize = 0;
773
774 assert(eth_get_dev());
775
776#ifdef CONFIG_DM_ETH
777 dpsize += dp_size(eth_get_dev());
778#else
779 dpsize += sizeof(ROOT);
Rob Clarkb66c60d2017-09-13 18:05:28 -0400780 dpsize += sizeof(*ndp);
Alexander Graff9cfad12018-03-15 17:33:38 +0100781#endif
Rob Clarkb66c60d2017-09-13 18:05:28 -0400782
783 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100784 if (!buf)
785 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400786
787#ifdef CONFIG_DM_ETH
788 buf = dp_fill(buf, eth_get_dev());
789#else
790 memcpy(buf, &ROOT, sizeof(ROOT));
791 buf += sizeof(ROOT);
Rob Clarkb66c60d2017-09-13 18:05:28 -0400792
793 ndp = buf;
794 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
795 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
796 ndp->dp.length = sizeof(*ndp);
Alexander Graff9cfad12018-03-15 17:33:38 +0100797 ndp->if_type = 1; /* Ethernet */
Rob Clarkb66c60d2017-09-13 18:05:28 -0400798 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
799 buf = &ndp[1];
Alexander Graff9cfad12018-03-15 17:33:38 +0100800#endif
Rob Clarkb66c60d2017-09-13 18:05:28 -0400801
802 *((struct efi_device_path *)buf) = END;
803
804 return start;
805}
806#endif
807
Rob Clarkbf192732017-10-10 08:23:06 -0400808/* Construct a device-path for memory-mapped image */
809struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
810 uint64_t start_address,
811 uint64_t end_address)
812{
813 struct efi_device_path_memory *mdp;
814 void *buf, *start;
815
816 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100817 if (!buf)
818 return NULL;
Rob Clarkbf192732017-10-10 08:23:06 -0400819
820 mdp = buf;
821 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
822 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
823 mdp->dp.length = sizeof(*mdp);
824 mdp->memory_type = memory_type;
825 mdp->start_address = start_address;
826 mdp->end_address = end_address;
827 buf = &mdp[1];
828
829 *((struct efi_device_path *)buf) = END;
830
831 return start;
832}
833
Rob Clarkb66c60d2017-09-13 18:05:28 -0400834/*
835 * Helper to split a full device path (containing both device and file
836 * parts) into it's constituent parts.
837 */
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100838efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
839 struct efi_device_path **device_path,
840 struct efi_device_path **file_path)
Rob Clarkb66c60d2017-09-13 18:05:28 -0400841{
842 struct efi_device_path *p, *dp, *fp;
843
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100844 *device_path = NULL;
845 *file_path = NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400846 dp = efi_dp_dup(full_path);
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100847 if (!dp)
848 return EFI_OUT_OF_RESOURCES;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400849 p = dp;
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100850 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkb66c60d2017-09-13 18:05:28 -0400851 p = efi_dp_next(p);
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100852 if (!p)
853 return EFI_OUT_OF_RESOURCES;
854 }
Rob Clarkb66c60d2017-09-13 18:05:28 -0400855 fp = efi_dp_dup(p);
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100856 if (!fp)
857 return EFI_OUT_OF_RESOURCES;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400858 p->type = DEVICE_PATH_TYPE_END;
859 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
860 p->length = sizeof(*p);
861
862 *device_path = dp;
863 *file_path = fp;
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100864 return EFI_SUCCESS;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400865}