blob: ada0a9c268274802e10e37ba6c22bf61caaa42f6 [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;
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +0200152 efi_uintn_t dp_size = efi_dp_instance_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) +
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +0200173 efi_dp_instance_size(obj_dp);
Alexander Graf905cb9e2017-12-11 14:29:46 +0100174 return efiobj;
175 } else {
176 /* Only return on exact matches */
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +0200177 if (efi_dp_instance_size(obj_dp) ==
178 dp_size)
Alexander Graf905cb9e2017-12-11 14:29:46 +0100179 return efiobj;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400180 }
Heinrich Schuchardt72292ab2017-11-26 14:05:16 +0100181 }
Rob Clarkb66c60d2017-09-13 18:05:28 -0400182
Heinrich Schuchardt72292ab2017-11-26 14:05:16 +0100183 obj_dp = shorten_path(efi_dp_next(obj_dp));
184 } while (short_path && obj_dp);
Rob Clarkb66c60d2017-09-13 18:05:28 -0400185 }
186
187 return NULL;
188}
189
Rob Clarkb66c60d2017-09-13 18:05:28 -0400190/*
191 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
192 * remaining part of the device path after the matched object.
193 */
194struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
195 struct efi_device_path **rem)
196{
197 struct efi_object *efiobj;
198
Alexander Graf905cb9e2017-12-11 14:29:46 +0100199 /* Search for an exact match first */
200 efiobj = find_obj(dp, false, NULL);
Rob Clarkb66c60d2017-09-13 18:05:28 -0400201
Alexander Graf905cb9e2017-12-11 14:29:46 +0100202 /* Then for a fuzzy match */
203 if (!efiobj)
204 efiobj = find_obj(dp, false, rem);
205
206 /* And now for a fuzzy short match */
Rob Clarkb66c60d2017-09-13 18:05:28 -0400207 if (!efiobj)
208 efiobj = find_obj(dp, true, rem);
209
210 return efiobj;
211}
212
Heinrich Schuchardt65436f92018-01-19 20:24:49 +0100213/*
214 * Determine the last device path node that is not the end node.
215 *
216 * @dp device path
217 * @return last node before the end node if it exists
218 * otherwise NULL
219 */
220const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
221{
222 struct efi_device_path *ret;
223
224 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
225 return NULL;
226 while (dp) {
227 ret = (struct efi_device_path *)dp;
228 dp = efi_dp_next(dp);
229 }
230 return ret;
231}
232
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +0200233/* get size of the first device path instance excluding end node */
234efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkb66c60d2017-09-13 18:05:28 -0400235{
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +0200236 efi_uintn_t sz = 0;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400237
Heinrich Schuchardtadb57512018-04-16 07:59:07 +0200238 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
239 return 0;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400240 while (dp) {
241 sz += dp->length;
242 dp = efi_dp_next(dp);
243 }
244
245 return sz;
246}
247
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +0200248/* get size of multi-instance device path excluding end node */
249efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
250{
251 const struct efi_device_path *p = dp;
252
253 if (!p)
254 return 0;
255 while (p->type != DEVICE_PATH_TYPE_END ||
256 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
257 p = (void *)p + p->length;
258
259 return (void *)p - (void *)dp;
260}
261
262/* copy multi-instance device path */
Rob Clarkb66c60d2017-09-13 18:05:28 -0400263struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
264{
265 struct efi_device_path *ndp;
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +0200266 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkb66c60d2017-09-13 18:05:28 -0400267
268 if (!dp)
269 return NULL;
270
271 ndp = dp_alloc(sz);
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100272 if (!ndp)
273 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400274 memcpy(ndp, dp, sz);
275
276 return ndp;
277}
278
279struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
280 const struct efi_device_path *dp2)
281{
282 struct efi_device_path *ret;
283
Heinrich Schuchardt07836342018-04-16 07:59:06 +0200284 if (!dp1 && !dp2) {
285 /* return an end node */
286 ret = efi_dp_dup(&END);
287 } else if (!dp1) {
Rob Clarkb66c60d2017-09-13 18:05:28 -0400288 ret = efi_dp_dup(dp2);
289 } else if (!dp2) {
290 ret = efi_dp_dup(dp1);
291 } else {
292 /* both dp1 and dp2 are non-null */
293 unsigned sz1 = efi_dp_size(dp1);
294 unsigned sz2 = efi_dp_size(dp2);
295 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100296 if (!p)
297 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400298 memcpy(p, dp1, sz1);
Heinrich Schuchardt07836342018-04-16 07:59:06 +0200299 /* the end node of the second device path has to be retained */
300 memcpy(p + sz1, dp2, sz2 + sizeof(END));
Rob Clarkb66c60d2017-09-13 18:05:28 -0400301 ret = p;
302 }
303
304 return ret;
305}
306
307struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
308 const struct efi_device_path *node)
309{
310 struct efi_device_path *ret;
311
312 if (!node && !dp) {
313 ret = efi_dp_dup(&END);
314 } else if (!node) {
315 ret = efi_dp_dup(dp);
316 } else if (!dp) {
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +0200317 size_t sz = node->length;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400318 void *p = dp_alloc(sz + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100319 if (!p)
320 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400321 memcpy(p, node, sz);
322 memcpy(p + sz, &END, sizeof(END));
323 ret = p;
324 } else {
325 /* both dp and node are non-null */
Heinrich Schuchardtf6dd3f32018-04-16 07:59:08 +0200326 size_t sz = efi_dp_size(dp);
Rob Clarkb66c60d2017-09-13 18:05:28 -0400327 void *p = dp_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100328 if (!p)
329 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400330 memcpy(p, dp, sz);
331 memcpy(p + sz, node, node->length);
332 memcpy(p + sz + node->length, &END, sizeof(END));
333 ret = p;
334 }
335
336 return ret;
337}
338
Heinrich Schuchardt211314c2018-04-16 07:59:05 +0200339struct efi_device_path *efi_dp_create_device_node(const u8 type,
340 const u8 sub_type,
341 const u16 length)
342{
343 struct efi_device_path *ret;
344
345 ret = dp_alloc(length);
346 if (!ret)
347 return ret;
348 ret->type = type;
349 ret->sub_type = sub_type;
350 ret->length = length;
351 return ret;
352}
353
Rob Clarkb66c60d2017-09-13 18:05:28 -0400354#ifdef CONFIG_DM
355/* size of device-path not including END node for device and all parents
356 * up to the root device.
357 */
358static unsigned dp_size(struct udevice *dev)
359{
360 if (!dev || !dev->driver)
361 return sizeof(ROOT);
362
363 switch (dev->driver->id) {
364 case UCLASS_ROOT:
365 case UCLASS_SIMPLE_BUS:
366 /* stop traversing parents at this point: */
367 return sizeof(ROOT);
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100368 case UCLASS_ETH:
369 return dp_size(dev->parent) +
370 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardtaf3106a2017-12-11 12:56:44 +0100371#ifdef CONFIG_BLK
372 case UCLASS_BLK:
373 switch (dev->parent->uclass->uc_drv->id) {
374#ifdef CONFIG_IDE
375 case UCLASS_IDE:
376 return dp_size(dev->parent) +
377 sizeof(struct efi_device_path_atapi);
378#endif
379#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
380 case UCLASS_SCSI:
381 return dp_size(dev->parent) +
382 sizeof(struct efi_device_path_scsi);
383#endif
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100384#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
385 case UCLASS_MMC:
386 return dp_size(dev->parent) +
387 sizeof(struct efi_device_path_sd_mmc_path);
388#endif
Heinrich Schuchardtaf3106a2017-12-11 12:56:44 +0100389 default:
390 return dp_size(dev->parent);
391 }
392#endif
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100393#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
Rob Clarkb66c60d2017-09-13 18:05:28 -0400394 case UCLASS_MMC:
395 return dp_size(dev->parent) +
396 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100397#endif
Rob Clarkb66c60d2017-09-13 18:05:28 -0400398 case UCLASS_MASS_STORAGE:
399 case UCLASS_USB_HUB:
400 return dp_size(dev->parent) +
401 sizeof(struct efi_device_path_usb_class);
402 default:
403 /* just skip over unknown classes: */
404 return dp_size(dev->parent);
405 }
406}
407
Heinrich Schuchardtaf3106a2017-12-11 12:56:44 +0100408/*
409 * Recursively build a device path.
410 *
411 * @buf pointer to the end of the device path
412 * @dev device
413 * @return pointer to the end of the device path
414 */
Rob Clarkb66c60d2017-09-13 18:05:28 -0400415static void *dp_fill(void *buf, struct udevice *dev)
416{
417 if (!dev || !dev->driver)
418 return buf;
419
420 switch (dev->driver->id) {
421 case UCLASS_ROOT:
422 case UCLASS_SIMPLE_BUS: {
423 /* stop traversing parents at this point: */
424 struct efi_device_path_vendor *vdp = buf;
425 *vdp = ROOT;
426 return &vdp[1];
427 }
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100428#ifdef CONFIG_DM_ETH
429 case UCLASS_ETH: {
430 struct efi_device_path_mac_addr *dp =
431 dp_fill(buf, dev->parent);
432 struct eth_pdata *pdata = dev->platdata;
433
434 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
435 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
436 dp->dp.length = sizeof(*dp);
437 memset(&dp->mac, 0, sizeof(dp->mac));
438 /* We only support IPv4 */
439 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
440 /* Ethernet */
441 dp->if_type = 1;
442 return &dp[1];
443 }
444#endif
Heinrich Schuchardtaf3106a2017-12-11 12:56:44 +0100445#ifdef CONFIG_BLK
446 case UCLASS_BLK:
447 switch (dev->parent->uclass->uc_drv->id) {
448#ifdef CONFIG_IDE
449 case UCLASS_IDE: {
450 struct efi_device_path_atapi *dp =
451 dp_fill(buf, dev->parent);
452 struct blk_desc *desc = dev_get_uclass_platdata(dev);
453
454 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
455 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
456 dp->dp.length = sizeof(*dp);
457 dp->logical_unit_number = desc->devnum;
458 dp->primary_secondary = IDE_BUS(desc->devnum);
459 dp->slave_master = desc->devnum %
460 (CONFIG_SYS_IDE_MAXDEVICE /
461 CONFIG_SYS_IDE_MAXBUS);
462 return &dp[1];
463 }
464#endif
465#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
466 case UCLASS_SCSI: {
467 struct efi_device_path_scsi *dp =
468 dp_fill(buf, dev->parent);
469 struct blk_desc *desc = dev_get_uclass_platdata(dev);
470
471 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
472 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
473 dp->dp.length = sizeof(*dp);
474 dp->logical_unit_number = desc->lun;
475 dp->target_id = desc->target;
476 return &dp[1];
477 }
478#endif
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100479#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
480 case UCLASS_MMC: {
481 struct efi_device_path_sd_mmc_path *sddp =
482 dp_fill(buf, dev->parent);
483 struct blk_desc *desc = dev_get_uclass_platdata(dev);
484
485 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
486 sddp->dp.sub_type = is_sd(desc) ?
487 DEVICE_PATH_SUB_TYPE_MSG_SD :
488 DEVICE_PATH_SUB_TYPE_MSG_MMC;
489 sddp->dp.length = sizeof(*sddp);
490 sddp->slot_number = dev->seq;
491 return &sddp[1];
492 }
493#endif
Heinrich Schuchardtaf3106a2017-12-11 12:56:44 +0100494 default:
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100495 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
496 __FILE__, __LINE__, __func__,
497 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardtaf3106a2017-12-11 12:56:44 +0100498 return dp_fill(buf, dev->parent);
499 }
500#endif
Rob Clarkb66c60d2017-09-13 18:05:28 -0400501#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
502 case UCLASS_MMC: {
503 struct efi_device_path_sd_mmc_path *sddp =
504 dp_fill(buf, dev->parent);
505 struct mmc *mmc = mmc_get_mmc_dev(dev);
506 struct blk_desc *desc = mmc_get_blk_desc(mmc);
507
508 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt66b051d2017-12-11 12:56:39 +0100509 sddp->dp.sub_type = is_sd(desc) ?
510 DEVICE_PATH_SUB_TYPE_MSG_SD :
511 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400512 sddp->dp.length = sizeof(*sddp);
513 sddp->slot_number = dev->seq;
514
515 return &sddp[1];
516 }
517#endif
518 case UCLASS_MASS_STORAGE:
519 case UCLASS_USB_HUB: {
520 struct efi_device_path_usb_class *udp =
521 dp_fill(buf, dev->parent);
522 struct usb_device *udev = dev_get_parent_priv(dev);
523 struct usb_device_descriptor *desc = &udev->descriptor;
524
525 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
526 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
527 udp->dp.length = sizeof(*udp);
528 udp->vendor_id = desc->idVendor;
529 udp->product_id = desc->idProduct;
530 udp->device_class = desc->bDeviceClass;
531 udp->device_subclass = desc->bDeviceSubClass;
532 udp->device_protocol = desc->bDeviceProtocol;
533
534 return &udp[1];
535 }
536 default:
Heinrich Schuchardt9dfd84d2018-01-20 21:02:18 +0100537 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
538 __FILE__, __LINE__, __func__,
Rob Clarkb66c60d2017-09-13 18:05:28 -0400539 dev->name, dev->driver->id);
540 return dp_fill(buf, dev->parent);
541 }
542}
543
544/* Construct a device-path from a device: */
545struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
546{
547 void *buf, *start;
548
549 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100550 if (!buf)
551 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400552 buf = dp_fill(buf, dev);
553 *((struct efi_device_path *)buf) = END;
554
555 return start;
556}
557#endif
558
559static unsigned dp_part_size(struct blk_desc *desc, int part)
560{
561 unsigned dpsize;
562
563#ifdef CONFIG_BLK
Heinrich Schuchardt2bc61b82017-12-11 12:56:43 +0100564 {
565 struct udevice *dev;
566 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
567
568 if (ret)
569 dev = desc->bdev->parent;
570 dpsize = dp_size(dev);
571 }
Rob Clarkb66c60d2017-09-13 18:05:28 -0400572#else
573 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
574#endif
575
576 if (part == 0) /* the actual disk, not a partition */
577 return dpsize;
578
579 if (desc->part_type == PART_TYPE_ISO)
580 dpsize += sizeof(struct efi_device_path_cdrom_path);
581 else
582 dpsize += sizeof(struct efi_device_path_hard_drive_path);
583
584 return dpsize;
585}
586
Heinrich Schuchardtbde6bfe2017-12-11 12:56:42 +0100587/*
Heinrich Schuchardt98d48bd2018-01-19 20:24:46 +0100588 * Create a device node for a block device partition.
Heinrich Schuchardtbde6bfe2017-12-11 12:56:42 +0100589 *
590 * @buf buffer to which the device path is wirtten
591 * @desc block device descriptor
592 * @part partition number, 0 identifies a block device
593 */
Heinrich Schuchardt98d48bd2018-01-19 20:24:46 +0100594static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkb66c60d2017-09-13 18:05:28 -0400595{
596 disk_partition_t info;
597
Rob Clarkb66c60d2017-09-13 18:05:28 -0400598 part_get_info(desc, part, &info);
599
600 if (desc->part_type == PART_TYPE_ISO) {
601 struct efi_device_path_cdrom_path *cddp = buf;
602
Heinrich Schuchardt7b982f02017-12-11 12:56:40 +0100603 cddp->boot_entry = part;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400604 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
605 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
606 cddp->dp.length = sizeof(*cddp);
607 cddp->partition_start = info.start;
608 cddp->partition_end = info.size;
609
610 buf = &cddp[1];
611 } else {
612 struct efi_device_path_hard_drive_path *hddp = buf;
613
614 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
615 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
616 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt7b982f02017-12-11 12:56:40 +0100617 hddp->partition_number = part;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400618 hddp->partition_start = info.start;
619 hddp->partition_end = info.size;
620 if (desc->part_type == PART_TYPE_EFI)
621 hddp->partmap_type = 2;
622 else
623 hddp->partmap_type = 1;
Jonathan Grayb6e9e092017-11-22 14:18:59 +1100624
625 switch (desc->sig_type) {
626 case SIG_TYPE_NONE:
627 default:
628 hddp->signature_type = 0;
629 memset(hddp->partition_signature, 0,
630 sizeof(hddp->partition_signature));
631 break;
632 case SIG_TYPE_MBR:
633 hddp->signature_type = 1;
634 memset(hddp->partition_signature, 0,
635 sizeof(hddp->partition_signature));
636 memcpy(hddp->partition_signature, &desc->mbr_sig,
637 sizeof(desc->mbr_sig));
638 break;
639 case SIG_TYPE_GUID:
640 hddp->signature_type = 2;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400641 memcpy(hddp->partition_signature, &desc->guid_sig,
642 sizeof(hddp->partition_signature));
Jonathan Grayb6e9e092017-11-22 14:18:59 +1100643 break;
644 }
Rob Clarkb66c60d2017-09-13 18:05:28 -0400645
646 buf = &hddp[1];
647 }
648
649 return buf;
650}
651
Heinrich Schuchardt98d48bd2018-01-19 20:24:46 +0100652/*
653 * Create a device path for a block device or one of its partitions.
654 *
655 * @buf buffer to which the device path is wirtten
656 * @desc block device descriptor
657 * @part partition number, 0 identifies a block device
658 */
659static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
660{
661#ifdef CONFIG_BLK
662 {
663 struct udevice *dev;
664 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
665
666 if (ret)
667 dev = desc->bdev->parent;
668 buf = dp_fill(buf, dev);
669 }
670#else
671 /*
672 * We *could* make a more accurate path, by looking at if_type
673 * and handling all the different cases like we do for non-
674 * legacy (ie CONFIG_BLK=y) case. But most important thing
675 * is just to have a unique device-path for if_type+devnum.
676 * So map things to a fictitious USB device.
677 */
678 struct efi_device_path_usb *udp;
679
680 memcpy(buf, &ROOT, sizeof(ROOT));
681 buf += sizeof(ROOT);
682
683 udp = buf;
684 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
685 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
686 udp->dp.length = sizeof(*udp);
687 udp->parent_port_number = desc->if_type;
688 udp->usb_interface = desc->devnum;
689 buf = &udp[1];
690#endif
691
692 if (part == 0) /* the actual disk, not a partition */
693 return buf;
694
695 return dp_part_node(buf, desc, part);
696}
Rob Clarkb66c60d2017-09-13 18:05:28 -0400697
698/* Construct a device-path from a partition on a blk device: */
699struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
700{
701 void *buf, *start;
702
703 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100704 if (!buf)
705 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400706
707 buf = dp_part_fill(buf, desc, part);
708
709 *((struct efi_device_path *)buf) = END;
710
711 return start;
712}
713
Heinrich Schuchardt98d48bd2018-01-19 20:24:46 +0100714/*
715 * Create a device node for a block device partition.
716 *
717 * @buf buffer to which the device path is wirtten
718 * @desc block device descriptor
719 * @part partition number, 0 identifies a block device
720 */
721struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
722{
723 efi_uintn_t dpsize;
724 void *buf;
725
726 if (desc->part_type == PART_TYPE_ISO)
727 dpsize = sizeof(struct efi_device_path_cdrom_path);
728 else
729 dpsize = sizeof(struct efi_device_path_hard_drive_path);
730 buf = dp_alloc(dpsize);
731
732 dp_part_node(buf, desc, part);
733
734 return buf;
735}
736
Rob Clarkb66c60d2017-09-13 18:05:28 -0400737/* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
738static void path_to_uefi(u16 *uefi, const char *path)
739{
740 while (*path) {
741 char c = *(path++);
742 if (c == '/')
743 c = '\\';
744 *(uefi++) = c;
745 }
746 *uefi = '\0';
747}
748
749/*
750 * If desc is NULL, this creates a path with only the file component,
751 * otherwise it creates a full path with both device and file components
752 */
753struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
754 const char *path)
755{
756 struct efi_device_path_file_path *fp;
757 void *buf, *start;
758 unsigned dpsize = 0, fpsize;
759
760 if (desc)
761 dpsize = dp_part_size(desc, part);
762
763 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
764 dpsize += fpsize;
765
766 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100767 if (!buf)
768 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400769
770 if (desc)
771 buf = dp_part_fill(buf, desc, part);
772
773 /* add file-path: */
774 fp = buf;
775 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
776 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
777 fp->dp.length = fpsize;
778 path_to_uefi(fp->str, path);
779 buf += fpsize;
780
781 *((struct efi_device_path *)buf) = END;
782
783 return start;
784}
785
Joe Hershberger092f2f32018-04-13 15:26:39 -0500786#ifdef CONFIG_NET
Rob Clarkb66c60d2017-09-13 18:05:28 -0400787struct efi_device_path *efi_dp_from_eth(void)
788{
Alexander Graff9cfad12018-03-15 17:33:38 +0100789#ifndef CONFIG_DM_ETH
Rob Clarkb66c60d2017-09-13 18:05:28 -0400790 struct efi_device_path_mac_addr *ndp;
Alexander Graff9cfad12018-03-15 17:33:38 +0100791#endif
Rob Clarkb66c60d2017-09-13 18:05:28 -0400792 void *buf, *start;
793 unsigned dpsize = 0;
794
795 assert(eth_get_dev());
796
797#ifdef CONFIG_DM_ETH
798 dpsize += dp_size(eth_get_dev());
799#else
800 dpsize += sizeof(ROOT);
Rob Clarkb66c60d2017-09-13 18:05:28 -0400801 dpsize += sizeof(*ndp);
Alexander Graff9cfad12018-03-15 17:33:38 +0100802#endif
Rob Clarkb66c60d2017-09-13 18:05:28 -0400803
804 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100805 if (!buf)
806 return NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400807
808#ifdef CONFIG_DM_ETH
809 buf = dp_fill(buf, eth_get_dev());
810#else
811 memcpy(buf, &ROOT, sizeof(ROOT));
812 buf += sizeof(ROOT);
Rob Clarkb66c60d2017-09-13 18:05:28 -0400813
814 ndp = buf;
815 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
816 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
817 ndp->dp.length = sizeof(*ndp);
Alexander Graff9cfad12018-03-15 17:33:38 +0100818 ndp->if_type = 1; /* Ethernet */
Rob Clarkb66c60d2017-09-13 18:05:28 -0400819 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
820 buf = &ndp[1];
Alexander Graff9cfad12018-03-15 17:33:38 +0100821#endif
Rob Clarkb66c60d2017-09-13 18:05:28 -0400822
823 *((struct efi_device_path *)buf) = END;
824
825 return start;
826}
827#endif
828
Rob Clarkbf192732017-10-10 08:23:06 -0400829/* Construct a device-path for memory-mapped image */
830struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
831 uint64_t start_address,
832 uint64_t end_address)
833{
834 struct efi_device_path_memory *mdp;
835 void *buf, *start;
836
837 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100838 if (!buf)
839 return NULL;
Rob Clarkbf192732017-10-10 08:23:06 -0400840
841 mdp = buf;
842 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
843 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
844 mdp->dp.length = sizeof(*mdp);
845 mdp->memory_type = memory_type;
846 mdp->start_address = start_address;
847 mdp->end_address = end_address;
848 buf = &mdp[1];
849
850 *((struct efi_device_path *)buf) = END;
851
852 return start;
853}
854
Rob Clarkb66c60d2017-09-13 18:05:28 -0400855/*
856 * Helper to split a full device path (containing both device and file
857 * parts) into it's constituent parts.
858 */
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100859efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
860 struct efi_device_path **device_path,
861 struct efi_device_path **file_path)
Rob Clarkb66c60d2017-09-13 18:05:28 -0400862{
863 struct efi_device_path *p, *dp, *fp;
864
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100865 *device_path = NULL;
866 *file_path = NULL;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400867 dp = efi_dp_dup(full_path);
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100868 if (!dp)
869 return EFI_OUT_OF_RESOURCES;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400870 p = dp;
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100871 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkb66c60d2017-09-13 18:05:28 -0400872 p = efi_dp_next(p);
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100873 if (!p)
874 return EFI_OUT_OF_RESOURCES;
875 }
Rob Clarkb66c60d2017-09-13 18:05:28 -0400876 fp = efi_dp_dup(p);
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100877 if (!fp)
878 return EFI_OUT_OF_RESOURCES;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400879 p->type = DEVICE_PATH_TYPE_END;
880 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
881 p->length = sizeof(*p);
882
883 *device_path = dp;
884 *file_path = fp;
Heinrich Schuchardt04298682018-01-19 20:24:37 +0100885 return EFI_SUCCESS;
Rob Clarkb66c60d2017-09-13 18:05:28 -0400886}