blob: 9d776a6d99fa33f0ad48021588df079400a2c44e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clarkf90cb9f2017-09-13 18:05:28 -04002/*
3 * EFI device path from u-boot device-model mapping
4 *
5 * (C) Copyright 2017 Rob Clark
Rob Clarkf90cb9f2017-09-13 18:05:28 -04006 */
7
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +01008#define LOG_CATEGORY LOGL_ERR
9
Rob Clarkf90cb9f2017-09-13 18:05:28 -040010#include <common.h>
11#include <blk.h>
12#include <dm.h>
13#include <usb.h>
14#include <mmc.h>
15#include <efi_loader.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040016#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
Patrick Wildtacf7bee2018-03-25 19:54:03 +020067 memset(buf, 0, sz);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040068 return buf;
69}
70
71/*
72 * Iterate to next block in device-path, terminating (returning NULL)
73 * at /End* node.
74 */
75struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
76{
77 if (dp == NULL)
78 return NULL;
79 if (dp->type == DEVICE_PATH_TYPE_END)
80 return NULL;
81 dp = ((void *)dp) + dp->length;
82 if (dp->type == DEVICE_PATH_TYPE_END)
83 return NULL;
84 return (struct efi_device_path *)dp;
85}
86
87/*
88 * Compare two device-paths, stopping when the shorter of the two hits
89 * an End* node. This is useful to, for example, compare a device-path
90 * representing a device with one representing a file on the device, or
91 * a device with a parent device.
92 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +020093int efi_dp_match(const struct efi_device_path *a,
94 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -040095{
96 while (1) {
97 int ret;
98
99 ret = memcmp(&a->length, &b->length, sizeof(a->length));
100 if (ret)
101 return ret;
102
103 ret = memcmp(a, b, a->length);
104 if (ret)
105 return ret;
106
107 a = efi_dp_next(a);
108 b = efi_dp_next(b);
109
110 if (!a || !b)
111 return 0;
112 }
113}
114
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400115/*
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;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200150 efi_uintn_t dp_size = efi_dp_instance_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) +
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200171 efi_dp_instance_size(obj_dp);
Alexander Graff9360fb2017-12-11 14:29:46 +0100172 return efiobj;
173 } else {
174 /* Only return on exact matches */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200175 if (efi_dp_instance_size(obj_dp) ==
176 dp_size)
Alexander Graff9360fb2017-12-11 14:29:46 +0100177 return efiobj;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400178 }
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100179 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400180
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100181 obj_dp = shorten_path(efi_dp_next(obj_dp));
182 } while (short_path && obj_dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400183 }
184
185 return NULL;
186}
187
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400188/*
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
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200231/* get size of the first device path instance excluding end node */
232efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400233{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200234 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400235
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200236 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
237 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400238 while (dp) {
239 sz += dp->length;
240 dp = efi_dp_next(dp);
241 }
242
243 return sz;
244}
245
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200246/* get size of multi-instance device path excluding end node */
247efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
248{
249 const struct efi_device_path *p = dp;
250
251 if (!p)
252 return 0;
253 while (p->type != DEVICE_PATH_TYPE_END ||
254 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
255 p = (void *)p + p->length;
256
257 return (void *)p - (void *)dp;
258}
259
260/* copy multi-instance device path */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400261struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
262{
263 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200264 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400265
266 if (!dp)
267 return NULL;
268
269 ndp = dp_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100270 if (!ndp)
271 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400272 memcpy(ndp, dp, sz);
273
274 return ndp;
275}
276
277struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
278 const struct efi_device_path *dp2)
279{
280 struct efi_device_path *ret;
281
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200282 if (!dp1 && !dp2) {
283 /* return an end node */
284 ret = efi_dp_dup(&END);
285 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400286 ret = efi_dp_dup(dp2);
287 } else if (!dp2) {
288 ret = efi_dp_dup(dp1);
289 } else {
290 /* both dp1 and dp2 are non-null */
291 unsigned sz1 = efi_dp_size(dp1);
292 unsigned sz2 = efi_dp_size(dp2);
293 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100294 if (!p)
295 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400296 memcpy(p, dp1, sz1);
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200297 /* the end node of the second device path has to be retained */
298 memcpy(p + sz1, dp2, sz2 + sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400299 ret = p;
300 }
301
302 return ret;
303}
304
305struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
306 const struct efi_device_path *node)
307{
308 struct efi_device_path *ret;
309
310 if (!node && !dp) {
311 ret = efi_dp_dup(&END);
312 } else if (!node) {
313 ret = efi_dp_dup(dp);
314 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200315 size_t sz = node->length;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400316 void *p = dp_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100317 if (!p)
318 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400319 memcpy(p, node, sz);
320 memcpy(p + sz, &END, sizeof(END));
321 ret = p;
322 } else {
323 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200324 size_t sz = efi_dp_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400325 void *p = dp_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100326 if (!p)
327 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400328 memcpy(p, dp, sz);
329 memcpy(p + sz, node, node->length);
330 memcpy(p + sz + node->length, &END, sizeof(END));
331 ret = p;
332 }
333
334 return ret;
335}
336
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200337struct efi_device_path *efi_dp_create_device_node(const u8 type,
338 const u8 sub_type,
339 const u16 length)
340{
341 struct efi_device_path *ret;
342
343 ret = dp_alloc(length);
344 if (!ret)
345 return ret;
346 ret->type = type;
347 ret->sub_type = sub_type;
348 ret->length = length;
349 return ret;
350}
351
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200352struct efi_device_path *efi_dp_append_instance(
353 const struct efi_device_path *dp,
354 const struct efi_device_path *dpi)
355{
356 size_t sz, szi;
357 struct efi_device_path *p, *ret;
358
359 if (!dpi)
360 return NULL;
361 if (!dp)
362 return efi_dp_dup(dpi);
363 sz = efi_dp_size(dp);
364 szi = efi_dp_instance_size(dpi);
365 p = dp_alloc(sz + szi + 2 * sizeof(END));
366 if (!p)
367 return NULL;
368 ret = p;
369 memcpy(p, dp, sz + sizeof(END));
370 p = (void *)p + sz;
371 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
372 p = (void *)p + sizeof(END);
373 memcpy(p, dpi, szi);
374 p = (void *)p + szi;
375 memcpy(p, &END, sizeof(END));
376 return ret;
377}
378
379struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
380 efi_uintn_t *size)
381{
382 size_t sz;
383 struct efi_device_path *p;
384
385 if (size)
386 *size = 0;
387 if (!dp || !*dp)
388 return NULL;
389 p = *dp;
390 sz = efi_dp_instance_size(*dp);
391 p = dp_alloc(sz + sizeof(END));
392 if (!p)
393 return NULL;
394 memcpy(p, *dp, sz + sizeof(END));
395 *dp = (void *)*dp + sz;
396 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
397 *dp = (void *)*dp + sizeof(END);
398 else
399 *dp = NULL;
400 if (size)
401 *size = sz + sizeof(END);
402 return p;
403}
404
405bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
406{
407 const struct efi_device_path *p = dp;
408
409 if (!p)
410 return false;
411 while (p->type != DEVICE_PATH_TYPE_END)
412 p = (void *)p + p->length;
413 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
414}
415
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400416#ifdef CONFIG_DM
417/* size of device-path not including END node for device and all parents
418 * up to the root device.
419 */
420static unsigned dp_size(struct udevice *dev)
421{
422 if (!dev || !dev->driver)
423 return sizeof(ROOT);
424
425 switch (dev->driver->id) {
426 case UCLASS_ROOT:
427 case UCLASS_SIMPLE_BUS:
428 /* stop traversing parents at this point: */
429 return sizeof(ROOT);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100430 case UCLASS_ETH:
431 return dp_size(dev->parent) +
432 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100433#ifdef CONFIG_BLK
434 case UCLASS_BLK:
435 switch (dev->parent->uclass->uc_drv->id) {
436#ifdef CONFIG_IDE
437 case UCLASS_IDE:
438 return dp_size(dev->parent) +
439 sizeof(struct efi_device_path_atapi);
440#endif
441#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
442 case UCLASS_SCSI:
443 return dp_size(dev->parent) +
444 sizeof(struct efi_device_path_scsi);
445#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100446#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
447 case UCLASS_MMC:
448 return dp_size(dev->parent) +
449 sizeof(struct efi_device_path_sd_mmc_path);
450#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100451 default:
452 return dp_size(dev->parent);
453 }
454#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100455#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400456 case UCLASS_MMC:
457 return dp_size(dev->parent) +
458 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100459#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400460 case UCLASS_MASS_STORAGE:
461 case UCLASS_USB_HUB:
462 return dp_size(dev->parent) +
463 sizeof(struct efi_device_path_usb_class);
464 default:
465 /* just skip over unknown classes: */
466 return dp_size(dev->parent);
467 }
468}
469
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100470/*
471 * Recursively build a device path.
472 *
473 * @buf pointer to the end of the device path
474 * @dev device
475 * @return pointer to the end of the device path
476 */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400477static void *dp_fill(void *buf, struct udevice *dev)
478{
479 if (!dev || !dev->driver)
480 return buf;
481
482 switch (dev->driver->id) {
483 case UCLASS_ROOT:
484 case UCLASS_SIMPLE_BUS: {
485 /* stop traversing parents at this point: */
486 struct efi_device_path_vendor *vdp = buf;
487 *vdp = ROOT;
488 return &vdp[1];
489 }
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100490#ifdef CONFIG_DM_ETH
491 case UCLASS_ETH: {
492 struct efi_device_path_mac_addr *dp =
493 dp_fill(buf, dev->parent);
494 struct eth_pdata *pdata = dev->platdata;
495
496 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
497 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
498 dp->dp.length = sizeof(*dp);
499 memset(&dp->mac, 0, sizeof(dp->mac));
500 /* We only support IPv4 */
501 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
502 /* Ethernet */
503 dp->if_type = 1;
504 return &dp[1];
505 }
506#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100507#ifdef CONFIG_BLK
508 case UCLASS_BLK:
509 switch (dev->parent->uclass->uc_drv->id) {
510#ifdef CONFIG_IDE
511 case UCLASS_IDE: {
512 struct efi_device_path_atapi *dp =
513 dp_fill(buf, dev->parent);
514 struct blk_desc *desc = dev_get_uclass_platdata(dev);
515
516 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
517 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
518 dp->dp.length = sizeof(*dp);
519 dp->logical_unit_number = desc->devnum;
520 dp->primary_secondary = IDE_BUS(desc->devnum);
521 dp->slave_master = desc->devnum %
522 (CONFIG_SYS_IDE_MAXDEVICE /
523 CONFIG_SYS_IDE_MAXBUS);
524 return &dp[1];
525 }
526#endif
527#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
528 case UCLASS_SCSI: {
529 struct efi_device_path_scsi *dp =
530 dp_fill(buf, dev->parent);
531 struct blk_desc *desc = dev_get_uclass_platdata(dev);
532
533 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
534 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
535 dp->dp.length = sizeof(*dp);
536 dp->logical_unit_number = desc->lun;
537 dp->target_id = desc->target;
538 return &dp[1];
539 }
540#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100541#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
542 case UCLASS_MMC: {
543 struct efi_device_path_sd_mmc_path *sddp =
544 dp_fill(buf, dev->parent);
545 struct blk_desc *desc = dev_get_uclass_platdata(dev);
546
547 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
548 sddp->dp.sub_type = is_sd(desc) ?
549 DEVICE_PATH_SUB_TYPE_MSG_SD :
550 DEVICE_PATH_SUB_TYPE_MSG_MMC;
551 sddp->dp.length = sizeof(*sddp);
552 sddp->slot_number = dev->seq;
553 return &sddp[1];
554 }
555#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100556 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100557 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
558 __FILE__, __LINE__, __func__,
559 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100560 return dp_fill(buf, dev->parent);
561 }
562#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400563#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
564 case UCLASS_MMC: {
565 struct efi_device_path_sd_mmc_path *sddp =
566 dp_fill(buf, dev->parent);
567 struct mmc *mmc = mmc_get_mmc_dev(dev);
568 struct blk_desc *desc = mmc_get_blk_desc(mmc);
569
570 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100571 sddp->dp.sub_type = is_sd(desc) ?
572 DEVICE_PATH_SUB_TYPE_MSG_SD :
573 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400574 sddp->dp.length = sizeof(*sddp);
575 sddp->slot_number = dev->seq;
576
577 return &sddp[1];
578 }
579#endif
580 case UCLASS_MASS_STORAGE:
581 case UCLASS_USB_HUB: {
582 struct efi_device_path_usb_class *udp =
583 dp_fill(buf, dev->parent);
584 struct usb_device *udev = dev_get_parent_priv(dev);
585 struct usb_device_descriptor *desc = &udev->descriptor;
586
587 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
588 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
589 udp->dp.length = sizeof(*udp);
590 udp->vendor_id = desc->idVendor;
591 udp->product_id = desc->idProduct;
592 udp->device_class = desc->bDeviceClass;
593 udp->device_subclass = desc->bDeviceSubClass;
594 udp->device_protocol = desc->bDeviceProtocol;
595
596 return &udp[1];
597 }
598 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100599 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
600 __FILE__, __LINE__, __func__,
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400601 dev->name, dev->driver->id);
602 return dp_fill(buf, dev->parent);
603 }
604}
605
606/* Construct a device-path from a device: */
607struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
608{
609 void *buf, *start;
610
611 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100612 if (!buf)
613 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400614 buf = dp_fill(buf, dev);
615 *((struct efi_device_path *)buf) = END;
616
617 return start;
618}
619#endif
620
621static unsigned dp_part_size(struct blk_desc *desc, int part)
622{
623 unsigned dpsize;
624
625#ifdef CONFIG_BLK
Heinrich Schuchardt3a615c82017-12-11 12:56:43 +0100626 {
627 struct udevice *dev;
628 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
629
630 if (ret)
631 dev = desc->bdev->parent;
632 dpsize = dp_size(dev);
633 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400634#else
635 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
636#endif
637
638 if (part == 0) /* the actual disk, not a partition */
639 return dpsize;
640
641 if (desc->part_type == PART_TYPE_ISO)
642 dpsize += sizeof(struct efi_device_path_cdrom_path);
643 else
644 dpsize += sizeof(struct efi_device_path_hard_drive_path);
645
646 return dpsize;
647}
648
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100649/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100650 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100651 *
652 * @buf buffer to which the device path is wirtten
653 * @desc block device descriptor
654 * @part partition number, 0 identifies a block device
655 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100656static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400657{
658 disk_partition_t info;
659
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400660 part_get_info(desc, part, &info);
661
662 if (desc->part_type == PART_TYPE_ISO) {
663 struct efi_device_path_cdrom_path *cddp = buf;
664
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100665 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400666 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
667 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
668 cddp->dp.length = sizeof(*cddp);
669 cddp->partition_start = info.start;
670 cddp->partition_end = info.size;
671
672 buf = &cddp[1];
673 } else {
674 struct efi_device_path_hard_drive_path *hddp = buf;
675
676 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
677 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
678 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100679 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400680 hddp->partition_start = info.start;
681 hddp->partition_end = info.size;
682 if (desc->part_type == PART_TYPE_EFI)
683 hddp->partmap_type = 2;
684 else
685 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100686
687 switch (desc->sig_type) {
688 case SIG_TYPE_NONE:
689 default:
690 hddp->signature_type = 0;
691 memset(hddp->partition_signature, 0,
692 sizeof(hddp->partition_signature));
693 break;
694 case SIG_TYPE_MBR:
695 hddp->signature_type = 1;
696 memset(hddp->partition_signature, 0,
697 sizeof(hddp->partition_signature));
698 memcpy(hddp->partition_signature, &desc->mbr_sig,
699 sizeof(desc->mbr_sig));
700 break;
701 case SIG_TYPE_GUID:
702 hddp->signature_type = 2;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400703 memcpy(hddp->partition_signature, &desc->guid_sig,
704 sizeof(hddp->partition_signature));
Jonathan Gray84b4d702017-11-22 14:18:59 +1100705 break;
706 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400707
708 buf = &hddp[1];
709 }
710
711 return buf;
712}
713
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100714/*
715 * Create a device path for a block device or one of its partitions.
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 */
721static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
722{
723#ifdef CONFIG_BLK
724 {
725 struct udevice *dev;
726 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
727
728 if (ret)
729 dev = desc->bdev->parent;
730 buf = dp_fill(buf, dev);
731 }
732#else
733 /*
734 * We *could* make a more accurate path, by looking at if_type
735 * and handling all the different cases like we do for non-
736 * legacy (ie CONFIG_BLK=y) case. But most important thing
737 * is just to have a unique device-path for if_type+devnum.
738 * So map things to a fictitious USB device.
739 */
740 struct efi_device_path_usb *udp;
741
742 memcpy(buf, &ROOT, sizeof(ROOT));
743 buf += sizeof(ROOT);
744
745 udp = buf;
746 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
747 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
748 udp->dp.length = sizeof(*udp);
749 udp->parent_port_number = desc->if_type;
750 udp->usb_interface = desc->devnum;
751 buf = &udp[1];
752#endif
753
754 if (part == 0) /* the actual disk, not a partition */
755 return buf;
756
757 return dp_part_node(buf, desc, part);
758}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400759
760/* Construct a device-path from a partition on a blk device: */
761struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
762{
763 void *buf, *start;
764
765 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100766 if (!buf)
767 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400768
769 buf = dp_part_fill(buf, desc, part);
770
771 *((struct efi_device_path *)buf) = END;
772
773 return start;
774}
775
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100776/*
777 * Create a device node for a block device partition.
778 *
779 * @buf buffer to which the device path is wirtten
780 * @desc block device descriptor
781 * @part partition number, 0 identifies a block device
782 */
783struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
784{
785 efi_uintn_t dpsize;
786 void *buf;
787
788 if (desc->part_type == PART_TYPE_ISO)
789 dpsize = sizeof(struct efi_device_path_cdrom_path);
790 else
791 dpsize = sizeof(struct efi_device_path_hard_drive_path);
792 buf = dp_alloc(dpsize);
793
794 dp_part_node(buf, desc, part);
795
796 return buf;
797}
798
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400799/* convert path to an UEFI style path (ie. DOS style backslashes and utf16) */
800static void path_to_uefi(u16 *uefi, const char *path)
801{
802 while (*path) {
803 char c = *(path++);
804 if (c == '/')
805 c = '\\';
806 *(uefi++) = c;
807 }
808 *uefi = '\0';
809}
810
811/*
812 * If desc is NULL, this creates a path with only the file component,
813 * otherwise it creates a full path with both device and file components
814 */
815struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
816 const char *path)
817{
818 struct efi_device_path_file_path *fp;
819 void *buf, *start;
820 unsigned dpsize = 0, fpsize;
821
822 if (desc)
823 dpsize = dp_part_size(desc, part);
824
825 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
826 dpsize += fpsize;
827
828 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100829 if (!buf)
830 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400831
832 if (desc)
833 buf = dp_part_fill(buf, desc, part);
834
835 /* add file-path: */
836 fp = buf;
837 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
838 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
839 fp->dp.length = fpsize;
840 path_to_uefi(fp->str, path);
841 buf += fpsize;
842
843 *((struct efi_device_path *)buf) = END;
844
845 return start;
846}
847
Joe Hershberger5277a972018-04-13 15:26:39 -0500848#ifdef CONFIG_NET
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400849struct efi_device_path *efi_dp_from_eth(void)
850{
Alexander Grafc4e983f2018-03-15 17:33:38 +0100851#ifndef CONFIG_DM_ETH
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400852 struct efi_device_path_mac_addr *ndp;
Alexander Grafc4e983f2018-03-15 17:33:38 +0100853#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400854 void *buf, *start;
855 unsigned dpsize = 0;
856
857 assert(eth_get_dev());
858
859#ifdef CONFIG_DM_ETH
860 dpsize += dp_size(eth_get_dev());
861#else
862 dpsize += sizeof(ROOT);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400863 dpsize += sizeof(*ndp);
Alexander Grafc4e983f2018-03-15 17:33:38 +0100864#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400865
866 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100867 if (!buf)
868 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400869
870#ifdef CONFIG_DM_ETH
871 buf = dp_fill(buf, eth_get_dev());
872#else
873 memcpy(buf, &ROOT, sizeof(ROOT));
874 buf += sizeof(ROOT);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400875
876 ndp = buf;
877 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
878 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
879 ndp->dp.length = sizeof(*ndp);
Alexander Grafc4e983f2018-03-15 17:33:38 +0100880 ndp->if_type = 1; /* Ethernet */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400881 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
882 buf = &ndp[1];
Alexander Grafc4e983f2018-03-15 17:33:38 +0100883#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400884
885 *((struct efi_device_path *)buf) = END;
886
887 return start;
888}
889#endif
890
Rob Clark18ceba72017-10-10 08:23:06 -0400891/* Construct a device-path for memory-mapped image */
892struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
893 uint64_t start_address,
894 uint64_t end_address)
895{
896 struct efi_device_path_memory *mdp;
897 void *buf, *start;
898
899 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100900 if (!buf)
901 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -0400902
903 mdp = buf;
904 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
905 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
906 mdp->dp.length = sizeof(*mdp);
907 mdp->memory_type = memory_type;
908 mdp->start_address = start_address;
909 mdp->end_address = end_address;
910 buf = &mdp[1];
911
912 *((struct efi_device_path *)buf) = END;
913
914 return start;
915}
916
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400917/*
918 * Helper to split a full device path (containing both device and file
919 * parts) into it's constituent parts.
920 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100921efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
922 struct efi_device_path **device_path,
923 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400924{
925 struct efi_device_path *p, *dp, *fp;
926
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100927 *device_path = NULL;
928 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400929 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100930 if (!dp)
931 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400932 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100933 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400934 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100935 if (!p)
936 return EFI_OUT_OF_RESOURCES;
937 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400938 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100939 if (!fp)
940 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400941 p->type = DEVICE_PATH_TYPE_END;
942 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
943 p->length = sizeof(*p);
944
945 *device_path = dp;
946 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100947 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400948}