blob: 634dacf41b79d1ae9ea669eb4b4252a06d182a95 [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
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +01009#define LOG_CATEGORY LOGL_ERR
10
Rob Clarkf90cb9f2017-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 Schuchardt7d569db2017-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 Clarkf90cb9f2017-09-13 18:05:28 -040059static void *dp_alloc(size_t sz)
60{
61 void *buf;
62
Heinrich Schuchardt468bf972018-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 Clarkf90cb9f2017-09-13 18:05:28 -040066 return NULL;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010067 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -040068
Patrick Wildtacf7bee2018-03-25 19:54:03 +020069 memset(buf, 0, sz);
Rob Clarkf90cb9f2017-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 Schuchardt753e2482017-10-26 19:25:48 +020095int efi_dp_match(const struct efi_device_path *a,
96 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-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 Clarkf90cb9f2017-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 Schuchardt51ca4f52018-04-16 07:59:08 +0200152 efi_uintn_t dp_size = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400153
154 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100155 struct efi_handler *handler;
156 struct efi_device_path *obj_dp;
157 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400158
Heinrich Schuchardt6953c102017-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 Clarkf90cb9f2017-09-13 18:05:28 -0400164
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100165 do {
166 if (efi_dp_match(dp, obj_dp) == 0) {
167 if (rem) {
Alexander Graff9360fb2017-12-11 14:29:46 +0100168 /*
169 * Allow partial matches, but inform
170 * the caller.
171 */
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100172 *rem = ((void *)dp) +
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200173 efi_dp_instance_size(obj_dp);
Alexander Graff9360fb2017-12-11 14:29:46 +0100174 return efiobj;
175 } else {
176 /* Only return on exact matches */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200177 if (efi_dp_instance_size(obj_dp) ==
178 dp_size)
Alexander Graff9360fb2017-12-11 14:29:46 +0100179 return efiobj;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400180 }
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100181 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400182
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100183 obj_dp = shorten_path(efi_dp_next(obj_dp));
184 } while (short_path && obj_dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400185 }
186
187 return NULL;
188}
189
Rob Clarkf90cb9f2017-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 Graff9360fb2017-12-11 14:29:46 +0100199 /* Search for an exact match first */
200 efiobj = find_obj(dp, false, NULL);
201
202 /* Then for a fuzzy match */
203 if (!efiobj)
204 efiobj = find_obj(dp, false, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400205
Alexander Graff9360fb2017-12-11 14:29:46 +0100206 /* And now for a fuzzy short match */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400207 if (!efiobj)
208 efiobj = find_obj(dp, true, rem);
209
210 return efiobj;
211}
212
Heinrich Schuchardt0976f8b2018-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 Schuchardt51ca4f52018-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 Clarkf90cb9f2017-09-13 18:05:28 -0400235{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200236 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400237
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200238 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
239 return 0;
Rob Clarkf90cb9f2017-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 Schuchardt51ca4f52018-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 Clarkf90cb9f2017-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 Schuchardt51ca4f52018-04-16 07:59:08 +0200266 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400267
268 if (!dp)
269 return NULL;
270
271 ndp = dp_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100272 if (!ndp)
273 return NULL;
Rob Clarkf90cb9f2017-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 Schuchardt60b5ab22018-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 Clarkf90cb9f2017-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 Schuchardt468bf972018-01-19 20:24:37 +0100296 if (!p)
297 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400298 memcpy(p, dp1, sz1);
Heinrich Schuchardt60b5ab22018-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 Clarkf90cb9f2017-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 Schuchardt51ca4f52018-04-16 07:59:08 +0200317 size_t sz = node->length;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400318 void *p = dp_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100319 if (!p)
320 return NULL;
Rob Clarkf90cb9f2017-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 Schuchardt51ca4f52018-04-16 07:59:08 +0200326 size_t sz = efi_dp_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400327 void *p = dp_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100328 if (!p)
329 return NULL;
Rob Clarkf90cb9f2017-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 Schuchardtb41c8e22018-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
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200354struct efi_device_path *efi_dp_append_instance(
355 const struct efi_device_path *dp,
356 const struct efi_device_path *dpi)
357{
358 size_t sz, szi;
359 struct efi_device_path *p, *ret;
360
361 if (!dpi)
362 return NULL;
363 if (!dp)
364 return efi_dp_dup(dpi);
365 sz = efi_dp_size(dp);
366 szi = efi_dp_instance_size(dpi);
367 p = dp_alloc(sz + szi + 2 * sizeof(END));
368 if (!p)
369 return NULL;
370 ret = p;
371 memcpy(p, dp, sz + sizeof(END));
372 p = (void *)p + sz;
373 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
374 p = (void *)p + sizeof(END);
375 memcpy(p, dpi, szi);
376 p = (void *)p + szi;
377 memcpy(p, &END, sizeof(END));
378 return ret;
379}
380
381struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
382 efi_uintn_t *size)
383{
384 size_t sz;
385 struct efi_device_path *p;
386
387 if (size)
388 *size = 0;
389 if (!dp || !*dp)
390 return NULL;
391 p = *dp;
392 sz = efi_dp_instance_size(*dp);
393 p = dp_alloc(sz + sizeof(END));
394 if (!p)
395 return NULL;
396 memcpy(p, *dp, sz + sizeof(END));
397 *dp = (void *)*dp + sz;
398 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
399 *dp = (void *)*dp + sizeof(END);
400 else
401 *dp = NULL;
402 if (size)
403 *size = sz + sizeof(END);
404 return p;
405}
406
407bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
408{
409 const struct efi_device_path *p = dp;
410
411 if (!p)
412 return false;
413 while (p->type != DEVICE_PATH_TYPE_END)
414 p = (void *)p + p->length;
415 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
416}
417
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400418#ifdef CONFIG_DM
419/* size of device-path not including END node for device and all parents
420 * up to the root device.
421 */
422static unsigned dp_size(struct udevice *dev)
423{
424 if (!dev || !dev->driver)
425 return sizeof(ROOT);
426
427 switch (dev->driver->id) {
428 case UCLASS_ROOT:
429 case UCLASS_SIMPLE_BUS:
430 /* stop traversing parents at this point: */
431 return sizeof(ROOT);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100432 case UCLASS_ETH:
433 return dp_size(dev->parent) +
434 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100435#ifdef CONFIG_BLK
436 case UCLASS_BLK:
437 switch (dev->parent->uclass->uc_drv->id) {
438#ifdef CONFIG_IDE
439 case UCLASS_IDE:
440 return dp_size(dev->parent) +
441 sizeof(struct efi_device_path_atapi);
442#endif
443#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
444 case UCLASS_SCSI:
445 return dp_size(dev->parent) +
446 sizeof(struct efi_device_path_scsi);
447#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100448#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
449 case UCLASS_MMC:
450 return dp_size(dev->parent) +
451 sizeof(struct efi_device_path_sd_mmc_path);
452#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100453 default:
454 return dp_size(dev->parent);
455 }
456#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100457#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400458 case UCLASS_MMC:
459 return dp_size(dev->parent) +
460 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100461#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400462 case UCLASS_MASS_STORAGE:
463 case UCLASS_USB_HUB:
464 return dp_size(dev->parent) +
465 sizeof(struct efi_device_path_usb_class);
466 default:
467 /* just skip over unknown classes: */
468 return dp_size(dev->parent);
469 }
470}
471
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100472/*
473 * Recursively build a device path.
474 *
475 * @buf pointer to the end of the device path
476 * @dev device
477 * @return pointer to the end of the device path
478 */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400479static void *dp_fill(void *buf, struct udevice *dev)
480{
481 if (!dev || !dev->driver)
482 return buf;
483
484 switch (dev->driver->id) {
485 case UCLASS_ROOT:
486 case UCLASS_SIMPLE_BUS: {
487 /* stop traversing parents at this point: */
488 struct efi_device_path_vendor *vdp = buf;
489 *vdp = ROOT;
490 return &vdp[1];
491 }
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100492#ifdef CONFIG_DM_ETH
493 case UCLASS_ETH: {
494 struct efi_device_path_mac_addr *dp =
495 dp_fill(buf, dev->parent);
496 struct eth_pdata *pdata = dev->platdata;
497
498 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
499 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
500 dp->dp.length = sizeof(*dp);
501 memset(&dp->mac, 0, sizeof(dp->mac));
502 /* We only support IPv4 */
503 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
504 /* Ethernet */
505 dp->if_type = 1;
506 return &dp[1];
507 }
508#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100509#ifdef CONFIG_BLK
510 case UCLASS_BLK:
511 switch (dev->parent->uclass->uc_drv->id) {
512#ifdef CONFIG_IDE
513 case UCLASS_IDE: {
514 struct efi_device_path_atapi *dp =
515 dp_fill(buf, dev->parent);
516 struct blk_desc *desc = dev_get_uclass_platdata(dev);
517
518 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
519 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
520 dp->dp.length = sizeof(*dp);
521 dp->logical_unit_number = desc->devnum;
522 dp->primary_secondary = IDE_BUS(desc->devnum);
523 dp->slave_master = desc->devnum %
524 (CONFIG_SYS_IDE_MAXDEVICE /
525 CONFIG_SYS_IDE_MAXBUS);
526 return &dp[1];
527 }
528#endif
529#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
530 case UCLASS_SCSI: {
531 struct efi_device_path_scsi *dp =
532 dp_fill(buf, dev->parent);
533 struct blk_desc *desc = dev_get_uclass_platdata(dev);
534
535 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
536 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
537 dp->dp.length = sizeof(*dp);
538 dp->logical_unit_number = desc->lun;
539 dp->target_id = desc->target;
540 return &dp[1];
541 }
542#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100543#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
544 case UCLASS_MMC: {
545 struct efi_device_path_sd_mmc_path *sddp =
546 dp_fill(buf, dev->parent);
547 struct blk_desc *desc = dev_get_uclass_platdata(dev);
548
549 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
550 sddp->dp.sub_type = is_sd(desc) ?
551 DEVICE_PATH_SUB_TYPE_MSG_SD :
552 DEVICE_PATH_SUB_TYPE_MSG_MMC;
553 sddp->dp.length = sizeof(*sddp);
554 sddp->slot_number = dev->seq;
555 return &sddp[1];
556 }
557#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100558 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100559 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
560 __FILE__, __LINE__, __func__,
561 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100562 return dp_fill(buf, dev->parent);
563 }
564#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400565#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
566 case UCLASS_MMC: {
567 struct efi_device_path_sd_mmc_path *sddp =
568 dp_fill(buf, dev->parent);
569 struct mmc *mmc = mmc_get_mmc_dev(dev);
570 struct blk_desc *desc = mmc_get_blk_desc(mmc);
571
572 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100573 sddp->dp.sub_type = is_sd(desc) ?
574 DEVICE_PATH_SUB_TYPE_MSG_SD :
575 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400576 sddp->dp.length = sizeof(*sddp);
577 sddp->slot_number = dev->seq;
578
579 return &sddp[1];
580 }
581#endif
582 case UCLASS_MASS_STORAGE:
583 case UCLASS_USB_HUB: {
584 struct efi_device_path_usb_class *udp =
585 dp_fill(buf, dev->parent);
586 struct usb_device *udev = dev_get_parent_priv(dev);
587 struct usb_device_descriptor *desc = &udev->descriptor;
588
589 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
590 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
591 udp->dp.length = sizeof(*udp);
592 udp->vendor_id = desc->idVendor;
593 udp->product_id = desc->idProduct;
594 udp->device_class = desc->bDeviceClass;
595 udp->device_subclass = desc->bDeviceSubClass;
596 udp->device_protocol = desc->bDeviceProtocol;
597
598 return &udp[1];
599 }
600 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100601 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
602 __FILE__, __LINE__, __func__,
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400603 dev->name, dev->driver->id);
604 return dp_fill(buf, dev->parent);
605 }
606}
607
608/* Construct a device-path from a device: */
609struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
610{
611 void *buf, *start;
612
613 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100614 if (!buf)
615 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400616 buf = dp_fill(buf, dev);
617 *((struct efi_device_path *)buf) = END;
618
619 return start;
620}
621#endif
622
623static unsigned dp_part_size(struct blk_desc *desc, int part)
624{
625 unsigned dpsize;
626
627#ifdef CONFIG_BLK
Heinrich Schuchardt3a615c82017-12-11 12:56:43 +0100628 {
629 struct udevice *dev;
630 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
631
632 if (ret)
633 dev = desc->bdev->parent;
634 dpsize = dp_size(dev);
635 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400636#else
637 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
638#endif
639
640 if (part == 0) /* the actual disk, not a partition */
641 return dpsize;
642
643 if (desc->part_type == PART_TYPE_ISO)
644 dpsize += sizeof(struct efi_device_path_cdrom_path);
645 else
646 dpsize += sizeof(struct efi_device_path_hard_drive_path);
647
648 return dpsize;
649}
650
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100651/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100652 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100653 *
654 * @buf buffer to which the device path is wirtten
655 * @desc block device descriptor
656 * @part partition number, 0 identifies a block device
657 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100658static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400659{
660 disk_partition_t info;
661
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400662 part_get_info(desc, part, &info);
663
664 if (desc->part_type == PART_TYPE_ISO) {
665 struct efi_device_path_cdrom_path *cddp = buf;
666
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100667 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400668 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
669 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
670 cddp->dp.length = sizeof(*cddp);
671 cddp->partition_start = info.start;
672 cddp->partition_end = info.size;
673
674 buf = &cddp[1];
675 } else {
676 struct efi_device_path_hard_drive_path *hddp = buf;
677
678 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
679 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
680 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100681 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400682 hddp->partition_start = info.start;
683 hddp->partition_end = info.size;
684 if (desc->part_type == PART_TYPE_EFI)
685 hddp->partmap_type = 2;
686 else
687 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100688
689 switch (desc->sig_type) {
690 case SIG_TYPE_NONE:
691 default:
692 hddp->signature_type = 0;
693 memset(hddp->partition_signature, 0,
694 sizeof(hddp->partition_signature));
695 break;
696 case SIG_TYPE_MBR:
697 hddp->signature_type = 1;
698 memset(hddp->partition_signature, 0,
699 sizeof(hddp->partition_signature));
700 memcpy(hddp->partition_signature, &desc->mbr_sig,
701 sizeof(desc->mbr_sig));
702 break;
703 case SIG_TYPE_GUID:
704 hddp->signature_type = 2;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400705 memcpy(hddp->partition_signature, &desc->guid_sig,
706 sizeof(hddp->partition_signature));
Jonathan Gray84b4d702017-11-22 14:18:59 +1100707 break;
708 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400709
710 buf = &hddp[1];
711 }
712
713 return buf;
714}
715
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100716/*
717 * Create a device path for a block device or one of its partitions.
718 *
719 * @buf buffer to which the device path is wirtten
720 * @desc block device descriptor
721 * @part partition number, 0 identifies a block device
722 */
723static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
724{
725#ifdef CONFIG_BLK
726 {
727 struct udevice *dev;
728 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
729
730 if (ret)
731 dev = desc->bdev->parent;
732 buf = dp_fill(buf, dev);
733 }
734#else
735 /*
736 * We *could* make a more accurate path, by looking at if_type
737 * and handling all the different cases like we do for non-
738 * legacy (ie CONFIG_BLK=y) case. But most important thing
739 * is just to have a unique device-path for if_type+devnum.
740 * So map things to a fictitious USB device.
741 */
742 struct efi_device_path_usb *udp;
743
744 memcpy(buf, &ROOT, sizeof(ROOT));
745 buf += sizeof(ROOT);
746
747 udp = buf;
748 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
749 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
750 udp->dp.length = sizeof(*udp);
751 udp->parent_port_number = desc->if_type;
752 udp->usb_interface = desc->devnum;
753 buf = &udp[1];
754#endif
755
756 if (part == 0) /* the actual disk, not a partition */
757 return buf;
758
759 return dp_part_node(buf, desc, part);
760}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400761
762/* Construct a device-path from a partition on a blk device: */
763struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
764{
765 void *buf, *start;
766
767 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100768 if (!buf)
769 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400770
771 buf = dp_part_fill(buf, desc, part);
772
773 *((struct efi_device_path *)buf) = END;
774
775 return start;
776}
777
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100778/*
779 * Create a device node for a block device partition.
780 *
781 * @buf buffer to which the device path is wirtten
782 * @desc block device descriptor
783 * @part partition number, 0 identifies a block device
784 */
785struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
786{
787 efi_uintn_t dpsize;
788 void *buf;
789
790 if (desc->part_type == PART_TYPE_ISO)
791 dpsize = sizeof(struct efi_device_path_cdrom_path);
792 else
793 dpsize = sizeof(struct efi_device_path_hard_drive_path);
794 buf = dp_alloc(dpsize);
795
796 dp_part_node(buf, desc, part);
797
798 return buf;
799}
800
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400801/* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
802static void path_to_uefi(u16 *uefi, const char *path)
803{
804 while (*path) {
805 char c = *(path++);
806 if (c == '/')
807 c = '\\';
808 *(uefi++) = c;
809 }
810 *uefi = '\0';
811}
812
813/*
814 * If desc is NULL, this creates a path with only the file component,
815 * otherwise it creates a full path with both device and file components
816 */
817struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
818 const char *path)
819{
820 struct efi_device_path_file_path *fp;
821 void *buf, *start;
822 unsigned dpsize = 0, fpsize;
823
824 if (desc)
825 dpsize = dp_part_size(desc, part);
826
827 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
828 dpsize += fpsize;
829
830 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100831 if (!buf)
832 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400833
834 if (desc)
835 buf = dp_part_fill(buf, desc, part);
836
837 /* add file-path: */
838 fp = buf;
839 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
840 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
841 fp->dp.length = fpsize;
842 path_to_uefi(fp->str, path);
843 buf += fpsize;
844
845 *((struct efi_device_path *)buf) = END;
846
847 return start;
848}
849
Joe Hershberger5277a972018-04-13 15:26:39 -0500850#ifdef CONFIG_NET
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400851struct efi_device_path *efi_dp_from_eth(void)
852{
Alexander Grafc4e983f2018-03-15 17:33:38 +0100853#ifndef CONFIG_DM_ETH
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400854 struct efi_device_path_mac_addr *ndp;
Alexander Grafc4e983f2018-03-15 17:33:38 +0100855#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400856 void *buf, *start;
857 unsigned dpsize = 0;
858
859 assert(eth_get_dev());
860
861#ifdef CONFIG_DM_ETH
862 dpsize += dp_size(eth_get_dev());
863#else
864 dpsize += sizeof(ROOT);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400865 dpsize += sizeof(*ndp);
Alexander Grafc4e983f2018-03-15 17:33:38 +0100866#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400867
868 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100869 if (!buf)
870 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400871
872#ifdef CONFIG_DM_ETH
873 buf = dp_fill(buf, eth_get_dev());
874#else
875 memcpy(buf, &ROOT, sizeof(ROOT));
876 buf += sizeof(ROOT);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400877
878 ndp = buf;
879 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
880 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
881 ndp->dp.length = sizeof(*ndp);
Alexander Grafc4e983f2018-03-15 17:33:38 +0100882 ndp->if_type = 1; /* Ethernet */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400883 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
884 buf = &ndp[1];
Alexander Grafc4e983f2018-03-15 17:33:38 +0100885#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400886
887 *((struct efi_device_path *)buf) = END;
888
889 return start;
890}
891#endif
892
Rob Clark18ceba72017-10-10 08:23:06 -0400893/* Construct a device-path for memory-mapped image */
894struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
895 uint64_t start_address,
896 uint64_t end_address)
897{
898 struct efi_device_path_memory *mdp;
899 void *buf, *start;
900
901 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100902 if (!buf)
903 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -0400904
905 mdp = buf;
906 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
907 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
908 mdp->dp.length = sizeof(*mdp);
909 mdp->memory_type = memory_type;
910 mdp->start_address = start_address;
911 mdp->end_address = end_address;
912 buf = &mdp[1];
913
914 *((struct efi_device_path *)buf) = END;
915
916 return start;
917}
918
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400919/*
920 * Helper to split a full device path (containing both device and file
921 * parts) into it's constituent parts.
922 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100923efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
924 struct efi_device_path **device_path,
925 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400926{
927 struct efi_device_path *p, *dp, *fp;
928
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100929 *device_path = NULL;
930 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400931 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100932 if (!dp)
933 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400934 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100935 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400936 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100937 if (!p)
938 return EFI_OUT_OF_RESOURCES;
939 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400940 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100941 if (!fp)
942 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400943 p->type = DEVICE_PATH_TYPE_END;
944 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
945 p->length = sizeof(*p);
946
947 *device_path = dp;
948 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100949 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400950}