blob: 10f890f44f660153be523726dfc02b18a489a3bd [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
8#include <common.h>
9#include <blk.h>
10#include <dm.h>
11#include <usb.h>
12#include <mmc.h>
13#include <efi_loader.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040014#include <part.h>
15
16/* template END node: */
17static const struct efi_device_path END = {
18 .type = DEVICE_PATH_TYPE_END,
19 .sub_type = DEVICE_PATH_SUB_TYPE_END,
20 .length = sizeof(END),
21};
22
Rob Clarkf90cb9f2017-09-13 18:05:28 -040023/* template ROOT node: */
24static const struct efi_device_path_vendor ROOT = {
25 .dp = {
26 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
27 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
28 .length = sizeof(ROOT),
29 },
30 .guid = U_BOOT_GUID,
31};
32
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010033#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
34/*
35 * Determine if an MMC device is an SD card.
36 *
37 * @desc block device descriptor
38 * @return true if the device is an SD card
39 */
40static bool is_sd(struct blk_desc *desc)
41{
42 struct mmc *mmc = find_mmc_device(desc->devnum);
43
44 if (!mmc)
45 return false;
46
47 return IS_SD(mmc) != 0U;
48}
49#endif
50
Rob Clarkf90cb9f2017-09-13 18:05:28 -040051static void *dp_alloc(size_t sz)
52{
53 void *buf;
54
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010055 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
56 EFI_SUCCESS) {
57 debug("EFI: ERROR: out of memory in %s\n", __func__);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040058 return NULL;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010059 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -040060
Patrick Wildtacf7bee2018-03-25 19:54:03 +020061 memset(buf, 0, sz);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040062 return buf;
63}
64
65/*
66 * Iterate to next block in device-path, terminating (returning NULL)
67 * at /End* node.
68 */
69struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
70{
71 if (dp == NULL)
72 return NULL;
73 if (dp->type == DEVICE_PATH_TYPE_END)
74 return NULL;
75 dp = ((void *)dp) + dp->length;
76 if (dp->type == DEVICE_PATH_TYPE_END)
77 return NULL;
78 return (struct efi_device_path *)dp;
79}
80
81/*
82 * Compare two device-paths, stopping when the shorter of the two hits
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +020083 * an End* node. This is useful to, for example, compare a device-path
Rob Clarkf90cb9f2017-09-13 18:05:28 -040084 * representing a device with one representing a file on the device, or
85 * a device with a parent device.
86 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +020087int efi_dp_match(const struct efi_device_path *a,
88 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -040089{
90 while (1) {
91 int ret;
92
93 ret = memcmp(&a->length, &b->length, sizeof(a->length));
94 if (ret)
95 return ret;
96
97 ret = memcmp(a, b, a->length);
98 if (ret)
99 return ret;
100
101 a = efi_dp_next(a);
102 b = efi_dp_next(b);
103
104 if (!a || !b)
105 return 0;
106 }
107}
108
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400109/*
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200110 * We can have device paths that start with a USB WWID or a USB Class node,
111 * and a few other cases which don't encode the full device path with bus
112 * hierarchy:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400113 *
114 * - MESSAGING:USB_WWID
115 * - MESSAGING:USB_CLASS
116 * - MEDIA:FILE_PATH
117 * - MEDIA:HARD_DRIVE
118 * - MESSAGING:URI
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200119 *
120 * See UEFI spec (section 3.1.2, about short-form device-paths)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400121 */
122static struct efi_device_path *shorten_path(struct efi_device_path *dp)
123{
124 while (dp) {
125 /*
126 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
127 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
128 * so not sure when we would see these other cases.
129 */
130 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
131 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
132 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
133 return dp;
134
135 dp = efi_dp_next(dp);
136 }
137
138 return dp;
139}
140
141static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
142 struct efi_device_path **rem)
143{
144 struct efi_object *efiobj;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200145 efi_uintn_t dp_size = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400146
147 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100148 struct efi_handler *handler;
149 struct efi_device_path *obj_dp;
150 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400151
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200152 ret = efi_search_protocol(efiobj,
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100153 &efi_guid_device_path, &handler);
154 if (ret != EFI_SUCCESS)
155 continue;
156 obj_dp = handler->protocol_interface;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400157
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100158 do {
159 if (efi_dp_match(dp, obj_dp) == 0) {
160 if (rem) {
Alexander Graff9360fb2017-12-11 14:29:46 +0100161 /*
162 * Allow partial matches, but inform
163 * the caller.
164 */
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100165 *rem = ((void *)dp) +
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200166 efi_dp_instance_size(obj_dp);
Alexander Graff9360fb2017-12-11 14:29:46 +0100167 return efiobj;
168 } else {
169 /* Only return on exact matches */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200170 if (efi_dp_instance_size(obj_dp) ==
171 dp_size)
Alexander Graff9360fb2017-12-11 14:29:46 +0100172 return efiobj;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400173 }
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100174 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400175
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100176 obj_dp = shorten_path(efi_dp_next(obj_dp));
177 } while (short_path && obj_dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400178 }
179
180 return NULL;
181}
182
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400183/*
184 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
185 * remaining part of the device path after the matched object.
186 */
187struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
188 struct efi_device_path **rem)
189{
190 struct efi_object *efiobj;
191
Alexander Graff9360fb2017-12-11 14:29:46 +0100192 /* Search for an exact match first */
193 efiobj = find_obj(dp, false, NULL);
194
195 /* Then for a fuzzy match */
196 if (!efiobj)
197 efiobj = find_obj(dp, false, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400198
Alexander Graff9360fb2017-12-11 14:29:46 +0100199 /* And now for a fuzzy short match */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400200 if (!efiobj)
201 efiobj = find_obj(dp, true, rem);
202
203 return efiobj;
204}
205
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100206/*
207 * Determine the last device path node that is not the end node.
208 *
209 * @dp device path
210 * @return last node before the end node if it exists
211 * otherwise NULL
212 */
213const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
214{
215 struct efi_device_path *ret;
216
217 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
218 return NULL;
219 while (dp) {
220 ret = (struct efi_device_path *)dp;
221 dp = efi_dp_next(dp);
222 }
223 return ret;
224}
225
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200226/* get size of the first device path instance excluding end node */
227efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400228{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200229 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400230
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200231 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
232 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400233 while (dp) {
234 sz += dp->length;
235 dp = efi_dp_next(dp);
236 }
237
238 return sz;
239}
240
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200241/* get size of multi-instance device path excluding end node */
242efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
243{
244 const struct efi_device_path *p = dp;
245
246 if (!p)
247 return 0;
248 while (p->type != DEVICE_PATH_TYPE_END ||
249 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
250 p = (void *)p + p->length;
251
252 return (void *)p - (void *)dp;
253}
254
255/* copy multi-instance device path */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400256struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
257{
258 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200259 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400260
261 if (!dp)
262 return NULL;
263
264 ndp = dp_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100265 if (!ndp)
266 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400267 memcpy(ndp, dp, sz);
268
269 return ndp;
270}
271
272struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
273 const struct efi_device_path *dp2)
274{
275 struct efi_device_path *ret;
276
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200277 if (!dp1 && !dp2) {
278 /* return an end node */
279 ret = efi_dp_dup(&END);
280 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400281 ret = efi_dp_dup(dp2);
282 } else if (!dp2) {
283 ret = efi_dp_dup(dp1);
284 } else {
285 /* both dp1 and dp2 are non-null */
286 unsigned sz1 = efi_dp_size(dp1);
287 unsigned sz2 = efi_dp_size(dp2);
288 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100289 if (!p)
290 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400291 memcpy(p, dp1, sz1);
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200292 /* the end node of the second device path has to be retained */
293 memcpy(p + sz1, dp2, sz2 + sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400294 ret = p;
295 }
296
297 return ret;
298}
299
300struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
301 const struct efi_device_path *node)
302{
303 struct efi_device_path *ret;
304
305 if (!node && !dp) {
306 ret = efi_dp_dup(&END);
307 } else if (!node) {
308 ret = efi_dp_dup(dp);
309 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200310 size_t sz = node->length;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400311 void *p = dp_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100312 if (!p)
313 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400314 memcpy(p, node, sz);
315 memcpy(p + sz, &END, sizeof(END));
316 ret = p;
317 } else {
318 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200319 size_t sz = efi_dp_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400320 void *p = dp_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100321 if (!p)
322 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400323 memcpy(p, dp, sz);
324 memcpy(p + sz, node, node->length);
325 memcpy(p + sz + node->length, &END, sizeof(END));
326 ret = p;
327 }
328
329 return ret;
330}
331
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200332struct efi_device_path *efi_dp_create_device_node(const u8 type,
333 const u8 sub_type,
334 const u16 length)
335{
336 struct efi_device_path *ret;
337
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200338 if (length < sizeof(struct efi_device_path))
339 return NULL;
340
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200341 ret = dp_alloc(length);
342 if (!ret)
343 return ret;
344 ret->type = type;
345 ret->sub_type = sub_type;
346 ret->length = length;
347 return ret;
348}
349
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200350struct efi_device_path *efi_dp_append_instance(
351 const struct efi_device_path *dp,
352 const struct efi_device_path *dpi)
353{
354 size_t sz, szi;
355 struct efi_device_path *p, *ret;
356
357 if (!dpi)
358 return NULL;
359 if (!dp)
360 return efi_dp_dup(dpi);
361 sz = efi_dp_size(dp);
362 szi = efi_dp_instance_size(dpi);
363 p = dp_alloc(sz + szi + 2 * sizeof(END));
364 if (!p)
365 return NULL;
366 ret = p;
367 memcpy(p, dp, sz + sizeof(END));
368 p = (void *)p + sz;
369 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
370 p = (void *)p + sizeof(END);
371 memcpy(p, dpi, szi);
372 p = (void *)p + szi;
373 memcpy(p, &END, sizeof(END));
374 return ret;
375}
376
377struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
378 efi_uintn_t *size)
379{
380 size_t sz;
381 struct efi_device_path *p;
382
383 if (size)
384 *size = 0;
385 if (!dp || !*dp)
386 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200387 sz = efi_dp_instance_size(*dp);
388 p = dp_alloc(sz + sizeof(END));
389 if (!p)
390 return NULL;
391 memcpy(p, *dp, sz + sizeof(END));
392 *dp = (void *)*dp + sz;
393 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
394 *dp = (void *)*dp + sizeof(END);
395 else
396 *dp = NULL;
397 if (size)
398 *size = sz + sizeof(END);
399 return p;
400}
401
402bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
403{
404 const struct efi_device_path *p = dp;
405
406 if (!p)
407 return false;
408 while (p->type != DEVICE_PATH_TYPE_END)
409 p = (void *)p + p->length;
410 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
411}
412
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400413#ifdef CONFIG_DM
414/* size of device-path not including END node for device and all parents
415 * up to the root device.
416 */
417static unsigned dp_size(struct udevice *dev)
418{
419 if (!dev || !dev->driver)
420 return sizeof(ROOT);
421
422 switch (dev->driver->id) {
423 case UCLASS_ROOT:
424 case UCLASS_SIMPLE_BUS:
425 /* stop traversing parents at this point: */
426 return sizeof(ROOT);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100427 case UCLASS_ETH:
428 return dp_size(dev->parent) +
429 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100430#ifdef CONFIG_BLK
431 case UCLASS_BLK:
432 switch (dev->parent->uclass->uc_drv->id) {
433#ifdef CONFIG_IDE
434 case UCLASS_IDE:
435 return dp_size(dev->parent) +
436 sizeof(struct efi_device_path_atapi);
437#endif
438#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
439 case UCLASS_SCSI:
440 return dp_size(dev->parent) +
441 sizeof(struct efi_device_path_scsi);
442#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100443#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
444 case UCLASS_MMC:
445 return dp_size(dev->parent) +
446 sizeof(struct efi_device_path_sd_mmc_path);
447#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100448 default:
449 return dp_size(dev->parent);
450 }
451#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100452#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400453 case UCLASS_MMC:
454 return dp_size(dev->parent) +
455 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100456#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400457 case UCLASS_MASS_STORAGE:
458 case UCLASS_USB_HUB:
459 return dp_size(dev->parent) +
460 sizeof(struct efi_device_path_usb_class);
461 default:
462 /* just skip over unknown classes: */
463 return dp_size(dev->parent);
464 }
465}
466
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100467/*
468 * Recursively build a device path.
469 *
470 * @buf pointer to the end of the device path
471 * @dev device
472 * @return pointer to the end of the device path
473 */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400474static void *dp_fill(void *buf, struct udevice *dev)
475{
476 if (!dev || !dev->driver)
477 return buf;
478
479 switch (dev->driver->id) {
480 case UCLASS_ROOT:
481 case UCLASS_SIMPLE_BUS: {
482 /* stop traversing parents at this point: */
483 struct efi_device_path_vendor *vdp = buf;
484 *vdp = ROOT;
485 return &vdp[1];
486 }
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100487#ifdef CONFIG_DM_ETH
488 case UCLASS_ETH: {
489 struct efi_device_path_mac_addr *dp =
490 dp_fill(buf, dev->parent);
491 struct eth_pdata *pdata = dev->platdata;
492
493 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
494 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
495 dp->dp.length = sizeof(*dp);
496 memset(&dp->mac, 0, sizeof(dp->mac));
497 /* We only support IPv4 */
498 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
499 /* Ethernet */
500 dp->if_type = 1;
501 return &dp[1];
502 }
503#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100504#ifdef CONFIG_BLK
505 case UCLASS_BLK:
506 switch (dev->parent->uclass->uc_drv->id) {
507#ifdef CONFIG_IDE
508 case UCLASS_IDE: {
509 struct efi_device_path_atapi *dp =
510 dp_fill(buf, dev->parent);
511 struct blk_desc *desc = dev_get_uclass_platdata(dev);
512
513 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
514 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
515 dp->dp.length = sizeof(*dp);
516 dp->logical_unit_number = desc->devnum;
517 dp->primary_secondary = IDE_BUS(desc->devnum);
518 dp->slave_master = desc->devnum %
519 (CONFIG_SYS_IDE_MAXDEVICE /
520 CONFIG_SYS_IDE_MAXBUS);
521 return &dp[1];
522 }
523#endif
524#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
525 case UCLASS_SCSI: {
526 struct efi_device_path_scsi *dp =
527 dp_fill(buf, dev->parent);
528 struct blk_desc *desc = dev_get_uclass_platdata(dev);
529
530 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
531 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
532 dp->dp.length = sizeof(*dp);
533 dp->logical_unit_number = desc->lun;
534 dp->target_id = desc->target;
535 return &dp[1];
536 }
537#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100538#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
539 case UCLASS_MMC: {
540 struct efi_device_path_sd_mmc_path *sddp =
541 dp_fill(buf, dev->parent);
542 struct blk_desc *desc = dev_get_uclass_platdata(dev);
543
544 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
545 sddp->dp.sub_type = is_sd(desc) ?
546 DEVICE_PATH_SUB_TYPE_MSG_SD :
547 DEVICE_PATH_SUB_TYPE_MSG_MMC;
548 sddp->dp.length = sizeof(*sddp);
549 sddp->slot_number = dev->seq;
550 return &sddp[1];
551 }
552#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100553 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100554 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
555 __FILE__, __LINE__, __func__,
556 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100557 return dp_fill(buf, dev->parent);
558 }
559#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400560#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
561 case UCLASS_MMC: {
562 struct efi_device_path_sd_mmc_path *sddp =
563 dp_fill(buf, dev->parent);
564 struct mmc *mmc = mmc_get_mmc_dev(dev);
565 struct blk_desc *desc = mmc_get_blk_desc(mmc);
566
567 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100568 sddp->dp.sub_type = is_sd(desc) ?
569 DEVICE_PATH_SUB_TYPE_MSG_SD :
570 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400571 sddp->dp.length = sizeof(*sddp);
572 sddp->slot_number = dev->seq;
573
574 return &sddp[1];
575 }
576#endif
577 case UCLASS_MASS_STORAGE:
578 case UCLASS_USB_HUB: {
579 struct efi_device_path_usb_class *udp =
580 dp_fill(buf, dev->parent);
581 struct usb_device *udev = dev_get_parent_priv(dev);
582 struct usb_device_descriptor *desc = &udev->descriptor;
583
584 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
585 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
586 udp->dp.length = sizeof(*udp);
587 udp->vendor_id = desc->idVendor;
588 udp->product_id = desc->idProduct;
589 udp->device_class = desc->bDeviceClass;
590 udp->device_subclass = desc->bDeviceSubClass;
591 udp->device_protocol = desc->bDeviceProtocol;
592
593 return &udp[1];
594 }
595 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100596 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
597 __FILE__, __LINE__, __func__,
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400598 dev->name, dev->driver->id);
599 return dp_fill(buf, dev->parent);
600 }
601}
602
603/* Construct a device-path from a device: */
604struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
605{
606 void *buf, *start;
607
608 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100609 if (!buf)
610 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400611 buf = dp_fill(buf, dev);
612 *((struct efi_device_path *)buf) = END;
613
614 return start;
615}
616#endif
617
618static unsigned dp_part_size(struct blk_desc *desc, int part)
619{
620 unsigned dpsize;
621
622#ifdef CONFIG_BLK
Heinrich Schuchardt3a615c82017-12-11 12:56:43 +0100623 {
624 struct udevice *dev;
625 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
626
627 if (ret)
628 dev = desc->bdev->parent;
629 dpsize = dp_size(dev);
630 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400631#else
632 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
633#endif
634
635 if (part == 0) /* the actual disk, not a partition */
636 return dpsize;
637
638 if (desc->part_type == PART_TYPE_ISO)
639 dpsize += sizeof(struct efi_device_path_cdrom_path);
640 else
641 dpsize += sizeof(struct efi_device_path_hard_drive_path);
642
643 return dpsize;
644}
645
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100646/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100647 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100648 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200649 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100650 * @desc block device descriptor
651 * @part partition number, 0 identifies a block device
652 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100653static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400654{
655 disk_partition_t info;
656
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400657 part_get_info(desc, part, &info);
658
659 if (desc->part_type == PART_TYPE_ISO) {
660 struct efi_device_path_cdrom_path *cddp = buf;
661
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100662 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400663 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
664 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
665 cddp->dp.length = sizeof(*cddp);
666 cddp->partition_start = info.start;
667 cddp->partition_end = info.size;
668
669 buf = &cddp[1];
670 } else {
671 struct efi_device_path_hard_drive_path *hddp = buf;
672
673 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
674 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
675 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100676 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400677 hddp->partition_start = info.start;
678 hddp->partition_end = info.size;
679 if (desc->part_type == PART_TYPE_EFI)
680 hddp->partmap_type = 2;
681 else
682 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100683
684 switch (desc->sig_type) {
685 case SIG_TYPE_NONE:
686 default:
687 hddp->signature_type = 0;
688 memset(hddp->partition_signature, 0,
689 sizeof(hddp->partition_signature));
690 break;
691 case SIG_TYPE_MBR:
692 hddp->signature_type = 1;
693 memset(hddp->partition_signature, 0,
694 sizeof(hddp->partition_signature));
695 memcpy(hddp->partition_signature, &desc->mbr_sig,
696 sizeof(desc->mbr_sig));
697 break;
698 case SIG_TYPE_GUID:
699 hddp->signature_type = 2;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400700 memcpy(hddp->partition_signature, &desc->guid_sig,
701 sizeof(hddp->partition_signature));
Jonathan Gray84b4d702017-11-22 14:18:59 +1100702 break;
703 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400704
705 buf = &hddp[1];
706 }
707
708 return buf;
709}
710
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100711/*
712 * Create a device path for a block device or one of its partitions.
713 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200714 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100715 * @desc block device descriptor
716 * @part partition number, 0 identifies a block device
717 */
718static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
719{
720#ifdef CONFIG_BLK
721 {
722 struct udevice *dev;
723 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
724
725 if (ret)
726 dev = desc->bdev->parent;
727 buf = dp_fill(buf, dev);
728 }
729#else
730 /*
731 * We *could* make a more accurate path, by looking at if_type
732 * and handling all the different cases like we do for non-
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200733 * legacy (i.e. CONFIG_BLK=y) case. But most important thing
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100734 * is just to have a unique device-path for if_type+devnum.
735 * So map things to a fictitious USB device.
736 */
737 struct efi_device_path_usb *udp;
738
739 memcpy(buf, &ROOT, sizeof(ROOT));
740 buf += sizeof(ROOT);
741
742 udp = buf;
743 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
744 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
745 udp->dp.length = sizeof(*udp);
746 udp->parent_port_number = desc->if_type;
747 udp->usb_interface = desc->devnum;
748 buf = &udp[1];
749#endif
750
751 if (part == 0) /* the actual disk, not a partition */
752 return buf;
753
754 return dp_part_node(buf, desc, part);
755}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400756
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200757/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400758struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
759{
760 void *buf, *start;
761
762 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100763 if (!buf)
764 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400765
766 buf = dp_part_fill(buf, desc, part);
767
768 *((struct efi_device_path *)buf) = END;
769
770 return start;
771}
772
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100773/*
774 * Create a device node for a block device partition.
775 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200776 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100777 * @desc block device descriptor
778 * @part partition number, 0 identifies a block device
779 */
780struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
781{
782 efi_uintn_t dpsize;
783 void *buf;
784
785 if (desc->part_type == PART_TYPE_ISO)
786 dpsize = sizeof(struct efi_device_path_cdrom_path);
787 else
788 dpsize = sizeof(struct efi_device_path_hard_drive_path);
789 buf = dp_alloc(dpsize);
790
791 dp_part_node(buf, desc, part);
792
793 return buf;
794}
795
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200796/* convert path to an UEFI style path (i.e. DOS style backslashes and UTF-16) */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400797static void path_to_uefi(u16 *uefi, const char *path)
798{
799 while (*path) {
800 char c = *(path++);
801 if (c == '/')
802 c = '\\';
803 *(uefi++) = c;
804 }
805 *uefi = '\0';
806}
807
808/*
809 * If desc is NULL, this creates a path with only the file component,
810 * otherwise it creates a full path with both device and file components
811 */
812struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
813 const char *path)
814{
815 struct efi_device_path_file_path *fp;
816 void *buf, *start;
817 unsigned dpsize = 0, fpsize;
818
819 if (desc)
820 dpsize = dp_part_size(desc, part);
821
822 fpsize = sizeof(struct efi_device_path) + 2 * (strlen(path) + 1);
823 dpsize += fpsize;
824
825 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100826 if (!buf)
827 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400828
829 if (desc)
830 buf = dp_part_fill(buf, desc, part);
831
832 /* add file-path: */
833 fp = buf;
834 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
835 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
836 fp->dp.length = fpsize;
837 path_to_uefi(fp->str, path);
838 buf += fpsize;
839
840 *((struct efi_device_path *)buf) = END;
841
842 return start;
843}
844
Joe Hershberger5277a972018-04-13 15:26:39 -0500845#ifdef CONFIG_NET
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400846struct efi_device_path *efi_dp_from_eth(void)
847{
Alexander Grafc4e983f2018-03-15 17:33:38 +0100848#ifndef CONFIG_DM_ETH
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400849 struct efi_device_path_mac_addr *ndp;
Alexander Grafc4e983f2018-03-15 17:33:38 +0100850#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400851 void *buf, *start;
852 unsigned dpsize = 0;
853
854 assert(eth_get_dev());
855
856#ifdef CONFIG_DM_ETH
857 dpsize += dp_size(eth_get_dev());
858#else
859 dpsize += sizeof(ROOT);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400860 dpsize += sizeof(*ndp);
Alexander Grafc4e983f2018-03-15 17:33:38 +0100861#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400862
863 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100864 if (!buf)
865 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400866
867#ifdef CONFIG_DM_ETH
868 buf = dp_fill(buf, eth_get_dev());
869#else
870 memcpy(buf, &ROOT, sizeof(ROOT));
871 buf += sizeof(ROOT);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400872
873 ndp = buf;
874 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
875 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
876 ndp->dp.length = sizeof(*ndp);
Alexander Grafc4e983f2018-03-15 17:33:38 +0100877 ndp->if_type = 1; /* Ethernet */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400878 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
879 buf = &ndp[1];
Alexander Grafc4e983f2018-03-15 17:33:38 +0100880#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400881
882 *((struct efi_device_path *)buf) = END;
883
884 return start;
885}
886#endif
887
Rob Clark18ceba72017-10-10 08:23:06 -0400888/* Construct a device-path for memory-mapped image */
889struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
890 uint64_t start_address,
891 uint64_t end_address)
892{
893 struct efi_device_path_memory *mdp;
894 void *buf, *start;
895
896 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100897 if (!buf)
898 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -0400899
900 mdp = buf;
901 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
902 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
903 mdp->dp.length = sizeof(*mdp);
904 mdp->memory_type = memory_type;
905 mdp->start_address = start_address;
906 mdp->end_address = end_address;
907 buf = &mdp[1];
908
909 *((struct efi_device_path *)buf) = END;
910
911 return start;
912}
913
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +0100914/**
915 * efi_dp_split_file_path() - split of relative file path from device path
916 *
917 * Given a device path indicating a file on a device, separate the device
918 * path in two: the device path of the actual device and the file path
919 * relative to this device.
920 *
921 * @full_path: device path including device and file path
922 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +0200923 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +0100924 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400925 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100926efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
927 struct efi_device_path **device_path,
928 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400929{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +0200930 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400931
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100932 *device_path = NULL;
933 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400934 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100935 if (!dp)
936 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400937 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100938 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400939 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100940 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +0200941 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100942 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400943 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100944 if (!fp)
945 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400946 p->type = DEVICE_PATH_TYPE_END;
947 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
948 p->length = sizeof(*p);
949
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +0200950out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400951 *device_path = dp;
952 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100953 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400954}
AKASHI Takahiro035fb012018-10-17 16:32:03 +0900955
956efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
957 const char *path,
958 struct efi_device_path **device,
959 struct efi_device_path **file)
960{
961 int is_net;
962 struct blk_desc *desc = NULL;
963 disk_partition_t fs_partition;
964 int part = 0;
965 char filename[32] = { 0 }; /* dp->str is u16[32] long */
966 char *s;
967
AKASHI Takahiro39844412018-11-05 18:06:40 +0900968 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +0900969 return EFI_INVALID_PARAMETER;
970
971 is_net = !strcmp(dev, "Net");
972 if (!is_net) {
973 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
974 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +0200975 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +0900976 return EFI_INVALID_PARAMETER;
977
AKASHI Takahiro39844412018-11-05 18:06:40 +0900978 if (device)
979 *device = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +0900980 } else {
981#ifdef CONFIG_NET
AKASHI Takahiro39844412018-11-05 18:06:40 +0900982 if (device)
983 *device = efi_dp_from_eth();
AKASHI Takahiro035fb012018-10-17 16:32:03 +0900984#endif
985 }
986
987 if (!path)
988 return EFI_SUCCESS;
989
Heinrich Schuchardt7d698072019-02-23 11:20:23 +0100990 snprintf(filename, sizeof(filename), "%s", path);
AKASHI Takahiro035fb012018-10-17 16:32:03 +0900991 /* DOS style file path: */
992 s = filename;
993 while ((s = strchr(s, '/')))
994 *s++ = '\\';
AKASHI Takahiro39844412018-11-05 18:06:40 +0900995 *file = efi_dp_from_file(((!is_net && device) ? desc : NULL),
996 part, filename);
AKASHI Takahiro035fb012018-10-17 16:32:03 +0900997
998 return EFI_SUCCESS;
999}