blob: 4b20859b2558f19f92fd8eaa5406ab090a12ff72 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <net.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040013#include <usb.h>
14#include <mmc.h>
Patrick Wildta3ca37e2019-10-03 16:24:17 +020015#include <nvme.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040016#include <efi_loader.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040017#include <part.h>
AKASHI Takahiro659a6262019-09-12 13:52:35 +090018#include <sandboxblockdev.h>
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +020019#include <asm-generic/unaligned.h>
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +090020#include <linux/compat.h> /* U16_MAX */
Rob Clarkf90cb9f2017-09-13 18:05:28 -040021
AKASHI Takahiro659a6262019-09-12 13:52:35 +090022#ifdef CONFIG_SANDBOX
23const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
24#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +020025#ifdef CONFIG_VIRTIO_BLK
26const efi_guid_t efi_guid_virtio_dev = U_BOOT_VIRTIO_DEV_GUID;
27#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +090028
Rob Clarkf90cb9f2017-09-13 18:05:28 -040029/* template END node: */
30static const struct efi_device_path END = {
31 .type = DEVICE_PATH_TYPE_END,
32 .sub_type = DEVICE_PATH_SUB_TYPE_END,
33 .length = sizeof(END),
34};
35
Rob Clarkf90cb9f2017-09-13 18:05:28 -040036/* template ROOT node: */
37static const struct efi_device_path_vendor ROOT = {
38 .dp = {
39 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
40 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
41 .length = sizeof(ROOT),
42 },
43 .guid = U_BOOT_GUID,
44};
45
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010046#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
47/*
48 * Determine if an MMC device is an SD card.
49 *
50 * @desc block device descriptor
51 * @return true if the device is an SD card
52 */
53static bool is_sd(struct blk_desc *desc)
54{
55 struct mmc *mmc = find_mmc_device(desc->devnum);
56
57 if (!mmc)
58 return false;
59
60 return IS_SD(mmc) != 0U;
61}
62#endif
63
Rob Clarkf90cb9f2017-09-13 18:05:28 -040064static void *dp_alloc(size_t sz)
65{
66 void *buf;
67
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010068 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
69 EFI_SUCCESS) {
70 debug("EFI: ERROR: out of memory in %s\n", __func__);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040071 return NULL;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010072 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -040073
Patrick Wildtacf7bee2018-03-25 19:54:03 +020074 memset(buf, 0, sz);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040075 return buf;
76}
77
78/*
79 * Iterate to next block in device-path, terminating (returning NULL)
80 * at /End* node.
81 */
82struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
83{
84 if (dp == NULL)
85 return NULL;
86 if (dp->type == DEVICE_PATH_TYPE_END)
87 return NULL;
88 dp = ((void *)dp) + dp->length;
89 if (dp->type == DEVICE_PATH_TYPE_END)
90 return NULL;
91 return (struct efi_device_path *)dp;
92}
93
94/*
95 * Compare two device-paths, stopping when the shorter of the two hits
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +020096 * an End* node. This is useful to, for example, compare a device-path
Rob Clarkf90cb9f2017-09-13 18:05:28 -040097 * representing a device with one representing a file on the device, or
98 * a device with a parent device.
99 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +0200100int efi_dp_match(const struct efi_device_path *a,
101 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400102{
103 while (1) {
104 int ret;
105
106 ret = memcmp(&a->length, &b->length, sizeof(a->length));
107 if (ret)
108 return ret;
109
110 ret = memcmp(a, b, a->length);
111 if (ret)
112 return ret;
113
114 a = efi_dp_next(a);
115 b = efi_dp_next(b);
116
117 if (!a || !b)
118 return 0;
119 }
120}
121
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400122/*
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200123 * We can have device paths that start with a USB WWID or a USB Class node,
124 * and a few other cases which don't encode the full device path with bus
125 * hierarchy:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400126 *
127 * - MESSAGING:USB_WWID
128 * - MESSAGING:USB_CLASS
129 * - MEDIA:FILE_PATH
130 * - MEDIA:HARD_DRIVE
131 * - MESSAGING:URI
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200132 *
133 * See UEFI spec (section 3.1.2, about short-form device-paths)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400134 */
135static struct efi_device_path *shorten_path(struct efi_device_path *dp)
136{
137 while (dp) {
138 /*
139 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
140 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
141 * so not sure when we would see these other cases.
142 */
143 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
144 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
145 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
146 return dp;
147
148 dp = efi_dp_next(dp);
149 }
150
151 return dp;
152}
153
154static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
155 struct efi_device_path **rem)
156{
157 struct efi_object *efiobj;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200158 efi_uintn_t dp_size = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400159
160 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100161 struct efi_handler *handler;
162 struct efi_device_path *obj_dp;
163 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400164
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200165 ret = efi_search_protocol(efiobj,
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100166 &efi_guid_device_path, &handler);
167 if (ret != EFI_SUCCESS)
168 continue;
169 obj_dp = handler->protocol_interface;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400170
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100171 do {
172 if (efi_dp_match(dp, obj_dp) == 0) {
173 if (rem) {
Alexander Graff9360fb2017-12-11 14:29:46 +0100174 /*
175 * Allow partial matches, but inform
176 * the caller.
177 */
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100178 *rem = ((void *)dp) +
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200179 efi_dp_instance_size(obj_dp);
Alexander Graff9360fb2017-12-11 14:29:46 +0100180 return efiobj;
181 } else {
182 /* Only return on exact matches */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200183 if (efi_dp_instance_size(obj_dp) ==
184 dp_size)
Alexander Graff9360fb2017-12-11 14:29:46 +0100185 return efiobj;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400186 }
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100187 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400188
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100189 obj_dp = shorten_path(efi_dp_next(obj_dp));
190 } while (short_path && obj_dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400191 }
192
193 return NULL;
194}
195
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400196/*
197 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
198 * remaining part of the device path after the matched object.
199 */
200struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
201 struct efi_device_path **rem)
202{
203 struct efi_object *efiobj;
204
Alexander Graff9360fb2017-12-11 14:29:46 +0100205 /* Search for an exact match first */
206 efiobj = find_obj(dp, false, NULL);
207
208 /* Then for a fuzzy match */
209 if (!efiobj)
210 efiobj = find_obj(dp, false, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400211
Alexander Graff9360fb2017-12-11 14:29:46 +0100212 /* And now for a fuzzy short match */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400213 if (!efiobj)
214 efiobj = find_obj(dp, true, rem);
215
216 return efiobj;
217}
218
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100219/*
220 * Determine the last device path node that is not the end node.
221 *
222 * @dp device path
223 * @return last node before the end node if it exists
224 * otherwise NULL
225 */
226const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
227{
228 struct efi_device_path *ret;
229
230 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
231 return NULL;
232 while (dp) {
233 ret = (struct efi_device_path *)dp;
234 dp = efi_dp_next(dp);
235 }
236 return ret;
237}
238
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200239/* get size of the first device path instance excluding end node */
240efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400241{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200242 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400243
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200244 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
245 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400246 while (dp) {
247 sz += dp->length;
248 dp = efi_dp_next(dp);
249 }
250
251 return sz;
252}
253
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200254/* get size of multi-instance device path excluding end node */
255efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
256{
257 const struct efi_device_path *p = dp;
258
259 if (!p)
260 return 0;
261 while (p->type != DEVICE_PATH_TYPE_END ||
262 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
263 p = (void *)p + p->length;
264
265 return (void *)p - (void *)dp;
266}
267
268/* copy multi-instance device path */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400269struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
270{
271 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200272 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400273
274 if (!dp)
275 return NULL;
276
277 ndp = dp_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100278 if (!ndp)
279 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400280 memcpy(ndp, dp, sz);
281
282 return ndp;
283}
284
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200285/**
286 * efi_dp_append_or_concatenate() - Append or concatenate two device paths.
287 * Concatenated device path will be separated
288 * by a sub-type 0xff end node
289 *
290 * @dp1: First device path
291 * @dp2: Second device path
292 * @concat: If true the two device paths will be concatenated and separated
293 * by an end of entrire device path sub-type 0xff end node.
294 * If true the second device path will be appended to the first and
295 * terminated by an end node
296 *
297 * Return:
298 * concatenated device path or NULL. Caller must free the returned value
299 */
300static struct
301efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
302 const struct efi_device_path *dp2,
303 bool concat)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400304{
305 struct efi_device_path *ret;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200306 size_t end_size = sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400307
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200308 if (concat)
309 end_size = 2 * sizeof(END);
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200310 if (!dp1 && !dp2) {
311 /* return an end node */
312 ret = efi_dp_dup(&END);
313 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400314 ret = efi_dp_dup(dp2);
315 } else if (!dp2) {
316 ret = efi_dp_dup(dp1);
317 } else {
318 /* both dp1 and dp2 are non-null */
319 unsigned sz1 = efi_dp_size(dp1);
320 unsigned sz2 = efi_dp_size(dp2);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200321 void *p = dp_alloc(sz1 + sz2 + end_size);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100322 if (!p)
323 return NULL;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200324 ret = p;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400325 memcpy(p, dp1, sz1);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200326 p += sz1;
327
328 if (concat) {
329 memcpy(p, &END, sizeof(END));
330 p += sizeof(END);
331 }
332
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200333 /* the end node of the second device path has to be retained */
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200334 memcpy(p, dp2, sz2);
335 p += sz2;
336 memcpy(p, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400337 }
338
339 return ret;
340}
341
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200342/**
343 * efi_dp_append() - Append a device to an existing device path.
344 *
345 * @dp1: First device path
346 * @dp2: Second device path
347 *
348 * Return:
349 * concatenated device path or NULL. Caller must free the returned value
350 */
351struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
352 const struct efi_device_path *dp2)
353{
354 return efi_dp_append_or_concatenate(dp1, dp2, false);
355}
356
357/**
358 * efi_dp_concat() - Concatenate 2 device paths. The final device path will
359 * contain two device paths separated by and end node (0xff).
360 *
361 * @dp1: First device path
362 * @dp2: Second device path
363 *
364 * Return:
365 * concatenated device path or NULL. Caller must free the returned value
366 */
367struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
368 const struct efi_device_path *dp2)
369{
370 return efi_dp_append_or_concatenate(dp1, dp2, true);
371}
372
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400373struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
374 const struct efi_device_path *node)
375{
376 struct efi_device_path *ret;
377
378 if (!node && !dp) {
379 ret = efi_dp_dup(&END);
380 } else if (!node) {
381 ret = efi_dp_dup(dp);
382 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200383 size_t sz = node->length;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400384 void *p = dp_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100385 if (!p)
386 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400387 memcpy(p, node, sz);
388 memcpy(p + sz, &END, sizeof(END));
389 ret = p;
390 } else {
391 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200392 size_t sz = efi_dp_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400393 void *p = dp_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100394 if (!p)
395 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400396 memcpy(p, dp, sz);
397 memcpy(p + sz, node, node->length);
398 memcpy(p + sz + node->length, &END, sizeof(END));
399 ret = p;
400 }
401
402 return ret;
403}
404
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200405struct efi_device_path *efi_dp_create_device_node(const u8 type,
406 const u8 sub_type,
407 const u16 length)
408{
409 struct efi_device_path *ret;
410
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200411 if (length < sizeof(struct efi_device_path))
412 return NULL;
413
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200414 ret = dp_alloc(length);
415 if (!ret)
416 return ret;
417 ret->type = type;
418 ret->sub_type = sub_type;
419 ret->length = length;
420 return ret;
421}
422
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200423struct efi_device_path *efi_dp_append_instance(
424 const struct efi_device_path *dp,
425 const struct efi_device_path *dpi)
426{
427 size_t sz, szi;
428 struct efi_device_path *p, *ret;
429
430 if (!dpi)
431 return NULL;
432 if (!dp)
433 return efi_dp_dup(dpi);
434 sz = efi_dp_size(dp);
435 szi = efi_dp_instance_size(dpi);
436 p = dp_alloc(sz + szi + 2 * sizeof(END));
437 if (!p)
438 return NULL;
439 ret = p;
440 memcpy(p, dp, sz + sizeof(END));
441 p = (void *)p + sz;
442 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
443 p = (void *)p + sizeof(END);
444 memcpy(p, dpi, szi);
445 p = (void *)p + szi;
446 memcpy(p, &END, sizeof(END));
447 return ret;
448}
449
450struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
451 efi_uintn_t *size)
452{
453 size_t sz;
454 struct efi_device_path *p;
455
456 if (size)
457 *size = 0;
458 if (!dp || !*dp)
459 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200460 sz = efi_dp_instance_size(*dp);
461 p = dp_alloc(sz + sizeof(END));
462 if (!p)
463 return NULL;
464 memcpy(p, *dp, sz + sizeof(END));
465 *dp = (void *)*dp + sz;
466 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
467 *dp = (void *)*dp + sizeof(END);
468 else
469 *dp = NULL;
470 if (size)
471 *size = sz + sizeof(END);
472 return p;
473}
474
475bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
476{
477 const struct efi_device_path *p = dp;
478
479 if (!p)
480 return false;
481 while (p->type != DEVICE_PATH_TYPE_END)
482 p = (void *)p + p->length;
483 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
484}
485
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400486#ifdef CONFIG_DM
487/* size of device-path not including END node for device and all parents
488 * up to the root device.
489 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100490__maybe_unused static unsigned int dp_size(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400491{
492 if (!dev || !dev->driver)
493 return sizeof(ROOT);
494
495 switch (dev->driver->id) {
496 case UCLASS_ROOT:
497 case UCLASS_SIMPLE_BUS:
498 /* stop traversing parents at this point: */
499 return sizeof(ROOT);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100500 case UCLASS_ETH:
501 return dp_size(dev->parent) +
502 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100503#ifdef CONFIG_BLK
504 case UCLASS_BLK:
505 switch (dev->parent->uclass->uc_drv->id) {
506#ifdef CONFIG_IDE
507 case UCLASS_IDE:
508 return dp_size(dev->parent) +
509 sizeof(struct efi_device_path_atapi);
510#endif
511#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
512 case UCLASS_SCSI:
513 return dp_size(dev->parent) +
514 sizeof(struct efi_device_path_scsi);
515#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100516#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
517 case UCLASS_MMC:
518 return dp_size(dev->parent) +
519 sizeof(struct efi_device_path_sd_mmc_path);
520#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200521#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
522 case UCLASS_AHCI:
523 return dp_size(dev->parent) +
524 sizeof(struct efi_device_path_sata);
525#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200526#if defined(CONFIG_NVME)
527 case UCLASS_NVME:
528 return dp_size(dev->parent) +
529 sizeof(struct efi_device_path_nvme);
530#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900531#ifdef CONFIG_SANDBOX
532 case UCLASS_ROOT:
533 /*
534 * Sandbox's host device will be represented
535 * as vendor device with extra one byte for
536 * device number
537 */
538 return dp_size(dev->parent)
539 + sizeof(struct efi_device_path_vendor) + 1;
540#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200541#ifdef CONFIG_VIRTIO_BLK
542 case UCLASS_VIRTIO:
543 /*
544 * Virtio devices will be represented as a vendor
545 * device node with an extra byte for the device
546 * number.
547 */
548 return dp_size(dev->parent)
549 + sizeof(struct efi_device_path_vendor) + 1;
550#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100551 default:
552 return dp_size(dev->parent);
553 }
554#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100555#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400556 case UCLASS_MMC:
557 return dp_size(dev->parent) +
558 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100559#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400560 case UCLASS_MASS_STORAGE:
561 case UCLASS_USB_HUB:
562 return dp_size(dev->parent) +
563 sizeof(struct efi_device_path_usb_class);
564 default:
565 /* just skip over unknown classes: */
566 return dp_size(dev->parent);
567 }
568}
569
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100570/*
571 * Recursively build a device path.
572 *
573 * @buf pointer to the end of the device path
574 * @dev device
575 * @return pointer to the end of the device path
576 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100577__maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400578{
579 if (!dev || !dev->driver)
580 return buf;
581
582 switch (dev->driver->id) {
583 case UCLASS_ROOT:
584 case UCLASS_SIMPLE_BUS: {
585 /* stop traversing parents at this point: */
586 struct efi_device_path_vendor *vdp = buf;
587 *vdp = ROOT;
588 return &vdp[1];
589 }
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100590#ifdef CONFIG_DM_ETH
591 case UCLASS_ETH: {
592 struct efi_device_path_mac_addr *dp =
593 dp_fill(buf, dev->parent);
Simon Glass95588622020-12-22 19:30:28 -0700594 struct eth_pdata *pdata = dev_get_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100595
596 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
597 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
598 dp->dp.length = sizeof(*dp);
599 memset(&dp->mac, 0, sizeof(dp->mac));
600 /* We only support IPv4 */
601 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
602 /* Ethernet */
603 dp->if_type = 1;
604 return &dp[1];
605 }
606#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100607#ifdef CONFIG_BLK
608 case UCLASS_BLK:
609 switch (dev->parent->uclass->uc_drv->id) {
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900610#ifdef CONFIG_SANDBOX
611 case UCLASS_ROOT: {
612 /* stop traversing parents at this point: */
Heinrich Schuchardt1e3beaf2020-05-06 01:28:08 +0200613 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700614 struct blk_desc *desc = dev_get_uclass_plat(dev);
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900615
616 dp_fill(buf, dev->parent);
617 dp = buf;
618 ++dp;
619 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
620 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
621 dp->dp.length = sizeof(*dp) + 1;
622 memcpy(&dp->guid, &efi_guid_host_dev,
623 sizeof(efi_guid_t));
624 dp->vendor_data[0] = desc->devnum;
625 return &dp->vendor_data[1];
626 }
627#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200628#ifdef CONFIG_VIRTIO_BLK
629 case UCLASS_VIRTIO: {
630 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700631 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200632
633 dp_fill(buf, dev->parent);
634 dp = buf;
635 ++dp;
636 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
637 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
638 dp->dp.length = sizeof(*dp) + 1;
639 memcpy(&dp->guid, &efi_guid_virtio_dev,
640 sizeof(efi_guid_t));
641 dp->vendor_data[0] = desc->devnum;
642 return &dp->vendor_data[1];
643 }
644#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100645#ifdef CONFIG_IDE
646 case UCLASS_IDE: {
647 struct efi_device_path_atapi *dp =
648 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700649 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100650
651 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
652 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
653 dp->dp.length = sizeof(*dp);
654 dp->logical_unit_number = desc->devnum;
655 dp->primary_secondary = IDE_BUS(desc->devnum);
656 dp->slave_master = desc->devnum %
657 (CONFIG_SYS_IDE_MAXDEVICE /
658 CONFIG_SYS_IDE_MAXBUS);
659 return &dp[1];
660 }
661#endif
662#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
663 case UCLASS_SCSI: {
664 struct efi_device_path_scsi *dp =
665 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700666 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100667
668 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
669 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
670 dp->dp.length = sizeof(*dp);
671 dp->logical_unit_number = desc->lun;
672 dp->target_id = desc->target;
673 return &dp[1];
674 }
675#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100676#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
677 case UCLASS_MMC: {
678 struct efi_device_path_sd_mmc_path *sddp =
679 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700680 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100681
682 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
683 sddp->dp.sub_type = is_sd(desc) ?
684 DEVICE_PATH_SUB_TYPE_MSG_SD :
685 DEVICE_PATH_SUB_TYPE_MSG_MMC;
686 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700687 sddp->slot_number = dev_seq(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100688 return &sddp[1];
689 }
690#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200691#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
692 case UCLASS_AHCI: {
693 struct efi_device_path_sata *dp =
694 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700695 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200696
697 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
698 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
699 dp->dp.length = sizeof(*dp);
700 dp->hba_port = desc->devnum;
701 /* default 0xffff implies no port multiplier */
702 dp->port_multiplier_port = 0xffff;
703 dp->logical_unit_number = desc->lun;
704 return &dp[1];
705 }
706#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200707#if defined(CONFIG_NVME)
708 case UCLASS_NVME: {
709 struct efi_device_path_nvme *dp =
710 dp_fill(buf, dev->parent);
711 u32 ns_id;
712
713 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
714 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
715 dp->dp.length = sizeof(*dp);
716 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
717 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
718 return &dp[1];
719 }
720#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100721 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100722 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
723 __FILE__, __LINE__, __func__,
724 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100725 return dp_fill(buf, dev->parent);
726 }
727#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400728#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
729 case UCLASS_MMC: {
730 struct efi_device_path_sd_mmc_path *sddp =
731 dp_fill(buf, dev->parent);
732 struct mmc *mmc = mmc_get_mmc_dev(dev);
733 struct blk_desc *desc = mmc_get_blk_desc(mmc);
734
735 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100736 sddp->dp.sub_type = is_sd(desc) ?
737 DEVICE_PATH_SUB_TYPE_MSG_SD :
738 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400739 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700740 sddp->slot_number = dev_seq(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400741
742 return &sddp[1];
743 }
744#endif
745 case UCLASS_MASS_STORAGE:
746 case UCLASS_USB_HUB: {
747 struct efi_device_path_usb_class *udp =
748 dp_fill(buf, dev->parent);
749 struct usb_device *udev = dev_get_parent_priv(dev);
750 struct usb_device_descriptor *desc = &udev->descriptor;
751
752 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
753 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
754 udp->dp.length = sizeof(*udp);
755 udp->vendor_id = desc->idVendor;
756 udp->product_id = desc->idProduct;
757 udp->device_class = desc->bDeviceClass;
758 udp->device_subclass = desc->bDeviceSubClass;
759 udp->device_protocol = desc->bDeviceProtocol;
760
761 return &udp[1];
762 }
763 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100764 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
765 __FILE__, __LINE__, __func__,
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400766 dev->name, dev->driver->id);
767 return dp_fill(buf, dev->parent);
768 }
769}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400770#endif
771
772static unsigned dp_part_size(struct blk_desc *desc, int part)
773{
774 unsigned dpsize;
775
776#ifdef CONFIG_BLK
Heinrich Schuchardt3a615c82017-12-11 12:56:43 +0100777 {
778 struct udevice *dev;
779 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
780
781 if (ret)
782 dev = desc->bdev->parent;
783 dpsize = dp_size(dev);
784 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400785#else
786 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
787#endif
788
789 if (part == 0) /* the actual disk, not a partition */
790 return dpsize;
791
792 if (desc->part_type == PART_TYPE_ISO)
793 dpsize += sizeof(struct efi_device_path_cdrom_path);
794 else
795 dpsize += sizeof(struct efi_device_path_hard_drive_path);
796
797 return dpsize;
798}
799
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100800/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100801 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100802 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200803 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100804 * @desc block device descriptor
805 * @part partition number, 0 identifies a block device
806 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100807static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400808{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600809 struct disk_partition info;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400810
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400811 part_get_info(desc, part, &info);
812
813 if (desc->part_type == PART_TYPE_ISO) {
814 struct efi_device_path_cdrom_path *cddp = buf;
815
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100816 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400817 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
818 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
819 cddp->dp.length = sizeof(*cddp);
820 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200821 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400822
823 buf = &cddp[1];
824 } else {
825 struct efi_device_path_hard_drive_path *hddp = buf;
826
827 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
828 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
829 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100830 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400831 hddp->partition_start = info.start;
832 hddp->partition_end = info.size;
833 if (desc->part_type == PART_TYPE_EFI)
834 hddp->partmap_type = 2;
835 else
836 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100837
838 switch (desc->sig_type) {
839 case SIG_TYPE_NONE:
840 default:
841 hddp->signature_type = 0;
842 memset(hddp->partition_signature, 0,
843 sizeof(hddp->partition_signature));
844 break;
845 case SIG_TYPE_MBR:
846 hddp->signature_type = 1;
847 memset(hddp->partition_signature, 0,
848 sizeof(hddp->partition_signature));
849 memcpy(hddp->partition_signature, &desc->mbr_sig,
850 sizeof(desc->mbr_sig));
851 break;
852 case SIG_TYPE_GUID:
853 hddp->signature_type = 2;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400854 memcpy(hddp->partition_signature, &desc->guid_sig,
855 sizeof(hddp->partition_signature));
Jonathan Gray84b4d702017-11-22 14:18:59 +1100856 break;
857 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400858
859 buf = &hddp[1];
860 }
861
862 return buf;
863}
864
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100865/*
866 * Create a device path for a block device or one of its partitions.
867 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200868 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100869 * @desc block device descriptor
870 * @part partition number, 0 identifies a block device
871 */
872static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
873{
874#ifdef CONFIG_BLK
875 {
876 struct udevice *dev;
877 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
878
879 if (ret)
880 dev = desc->bdev->parent;
881 buf = dp_fill(buf, dev);
882 }
883#else
884 /*
885 * We *could* make a more accurate path, by looking at if_type
886 * and handling all the different cases like we do for non-
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200887 * legacy (i.e. CONFIG_BLK=y) case. But most important thing
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100888 * is just to have a unique device-path for if_type+devnum.
889 * So map things to a fictitious USB device.
890 */
891 struct efi_device_path_usb *udp;
892
893 memcpy(buf, &ROOT, sizeof(ROOT));
894 buf += sizeof(ROOT);
895
896 udp = buf;
897 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
898 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
899 udp->dp.length = sizeof(*udp);
900 udp->parent_port_number = desc->if_type;
901 udp->usb_interface = desc->devnum;
902 buf = &udp[1];
903#endif
904
905 if (part == 0) /* the actual disk, not a partition */
906 return buf;
907
908 return dp_part_node(buf, desc, part);
909}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400910
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200911/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400912struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
913{
914 void *buf, *start;
915
916 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100917 if (!buf)
918 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400919
920 buf = dp_part_fill(buf, desc, part);
921
922 *((struct efi_device_path *)buf) = END;
923
924 return start;
925}
926
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100927/*
928 * Create a device node for a block device partition.
929 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200930 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100931 * @desc block device descriptor
932 * @part partition number, 0 identifies a block device
933 */
934struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
935{
936 efi_uintn_t dpsize;
937 void *buf;
938
939 if (desc->part_type == PART_TYPE_ISO)
940 dpsize = sizeof(struct efi_device_path_cdrom_path);
941 else
942 dpsize = sizeof(struct efi_device_path_hard_drive_path);
943 buf = dp_alloc(dpsize);
944
945 dp_part_node(buf, desc, part);
946
947 return buf;
948}
949
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200950/**
951 * path_to_uefi() - convert UTF-8 path to an UEFI style path
952 *
953 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
954 * separators and UTF-16).
955 *
956 * @src: source buffer
957 * @uefi: target buffer, possibly unaligned
958 */
959static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400960{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200961 u16 *pos = uefi;
962
963 /*
964 * efi_set_bootdev() calls this routine indirectly before the UEFI
965 * subsystem is initialized. So we cannot assume unaligned access to be
966 * enabled.
967 */
968 allow_unaligned();
969
970 while (*src) {
971 s32 code = utf8_get(&src);
972
973 if (code < 0)
974 code = '?';
975 else if (code == '/')
976 code = '\\';
977 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400978 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200979 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400980}
981
982/*
983 * If desc is NULL, this creates a path with only the file component,
984 * otherwise it creates a full path with both device and file components
985 */
986struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
987 const char *path)
988{
989 struct efi_device_path_file_path *fp;
990 void *buf, *start;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900991 size_t dpsize = 0, fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400992
993 if (desc)
994 dpsize = dp_part_size(desc, part);
995
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200996 fpsize = sizeof(struct efi_device_path) +
997 2 * (utf8_utf16_strlen(path) + 1);
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900998 if (fpsize > U16_MAX)
999 return NULL;
1000
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001001 dpsize += fpsize;
1002
1003 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001004 if (!buf)
1005 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001006
1007 if (desc)
1008 buf = dp_part_fill(buf, desc, part);
1009
1010 /* add file-path: */
1011 fp = buf;
1012 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
1013 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001014 fp->dp.length = (u16)fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001015 path_to_uefi(fp->str, path);
1016 buf += fpsize;
1017
1018 *((struct efi_device_path *)buf) = END;
1019
1020 return start;
1021}
1022
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001023struct efi_device_path *efi_dp_from_uart(void)
1024{
1025 void *buf, *pos;
1026 struct efi_device_path_uart *uart;
1027 size_t dpsize = sizeof(ROOT) + sizeof(*uart) + sizeof(END);
1028
1029 buf = dp_alloc(dpsize);
1030 if (!buf)
1031 return NULL;
1032 pos = buf;
1033 memcpy(pos, &ROOT, sizeof(ROOT));
1034 pos += sizeof(ROOT);
1035 uart = pos;
1036 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1037 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
1038 uart->dp.length = sizeof(*uart);
1039 pos += sizeof(*uart);
1040 memcpy(pos, &END, sizeof(END));
1041
1042 return buf;
1043}
1044
Joe Hershberger5277a972018-04-13 15:26:39 -05001045#ifdef CONFIG_NET
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001046struct efi_device_path *efi_dp_from_eth(void)
1047{
Alexander Grafc4e983f2018-03-15 17:33:38 +01001048#ifndef CONFIG_DM_ETH
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001049 struct efi_device_path_mac_addr *ndp;
Alexander Grafc4e983f2018-03-15 17:33:38 +01001050#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001051 void *buf, *start;
1052 unsigned dpsize = 0;
1053
1054 assert(eth_get_dev());
1055
1056#ifdef CONFIG_DM_ETH
1057 dpsize += dp_size(eth_get_dev());
1058#else
1059 dpsize += sizeof(ROOT);
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001060 dpsize += sizeof(*ndp);
Alexander Grafc4e983f2018-03-15 17:33:38 +01001061#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001062
1063 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001064 if (!buf)
1065 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001066
1067#ifdef CONFIG_DM_ETH
1068 buf = dp_fill(buf, eth_get_dev());
1069#else
1070 memcpy(buf, &ROOT, sizeof(ROOT));
1071 buf += sizeof(ROOT);
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001072
1073 ndp = buf;
1074 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1075 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
1076 ndp->dp.length = sizeof(*ndp);
Alexander Grafc4e983f2018-03-15 17:33:38 +01001077 ndp->if_type = 1; /* Ethernet */
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001078 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
1079 buf = &ndp[1];
Alexander Grafc4e983f2018-03-15 17:33:38 +01001080#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001081
1082 *((struct efi_device_path *)buf) = END;
1083
1084 return start;
1085}
1086#endif
1087
Rob Clark18ceba72017-10-10 08:23:06 -04001088/* Construct a device-path for memory-mapped image */
1089struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1090 uint64_t start_address,
1091 uint64_t end_address)
1092{
1093 struct efi_device_path_memory *mdp;
1094 void *buf, *start;
1095
1096 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001097 if (!buf)
1098 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -04001099
1100 mdp = buf;
1101 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1102 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1103 mdp->dp.length = sizeof(*mdp);
1104 mdp->memory_type = memory_type;
1105 mdp->start_address = start_address;
1106 mdp->end_address = end_address;
1107 buf = &mdp[1];
1108
1109 *((struct efi_device_path *)buf) = END;
1110
1111 return start;
1112}
1113
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001114/**
1115 * efi_dp_split_file_path() - split of relative file path from device path
1116 *
1117 * Given a device path indicating a file on a device, separate the device
1118 * path in two: the device path of the actual device and the file path
1119 * relative to this device.
1120 *
1121 * @full_path: device path including device and file path
1122 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001123 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001124 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001125 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001126efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1127 struct efi_device_path **device_path,
1128 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001129{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001130 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001131
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001132 *device_path = NULL;
1133 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001134 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001135 if (!dp)
1136 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001137 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001138 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001139 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001140 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001141 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001142 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001143 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001144 if (!fp)
1145 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001146 p->type = DEVICE_PATH_TYPE_END;
1147 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1148 p->length = sizeof(*p);
1149
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001150out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001151 *device_path = dp;
1152 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001153 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001154}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001155
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001156/**
1157 * efi_dp_from_name() - convert U-Boot device and file path to device path
1158 *
1159 * @dev: U-Boot device, e.g. 'mmc'
1160 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1161 * @path: file path relative to U-Boot device, may be NULL
1162 * @device: pointer to receive device path of the device
1163 * @file: pointer to receive device path for the file
1164 * Return: status code
1165 */
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001166efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1167 const char *path,
1168 struct efi_device_path **device,
1169 struct efi_device_path **file)
1170{
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001171 struct blk_desc *desc = NULL;
Simon Glassc1c4a8f2020-05-10 11:39:57 -06001172 struct disk_partition fs_partition;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001173 int part = 0;
1174 char filename[32] = { 0 }; /* dp->str is u16[32] long */
1175 char *s;
1176
AKASHI Takahiro39844412018-11-05 18:06:40 +09001177 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001178 return EFI_INVALID_PARAMETER;
1179
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001180 if (!strcmp(dev, "Net")) {
1181#ifdef CONFIG_NET
1182 if (device)
1183 *device = efi_dp_from_eth();
1184#endif
1185 } else if (!strcmp(dev, "Uart")) {
1186 if (device)
1187 *device = efi_dp_from_uart();
1188 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001189 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1190 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001191 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001192 return EFI_INVALID_PARAMETER;
1193
AKASHI Takahiro39844412018-11-05 18:06:40 +09001194 if (device)
1195 *device = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001196 }
1197
1198 if (!path)
1199 return EFI_SUCCESS;
1200
Heinrich Schuchardt7d698072019-02-23 11:20:23 +01001201 snprintf(filename, sizeof(filename), "%s", path);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001202 /* DOS style file path: */
1203 s = filename;
1204 while ((s = strchr(s, '/')))
1205 *s++ = '\\';
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001206 *file = efi_dp_from_file(desc, part, filename);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001207
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001208 if (!*file)
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001209 return EFI_INVALID_PARAMETER;
1210
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001211 return EFI_SUCCESS;
1212}
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001213
1214/**
1215 * efi_dp_check_length() - check length of a device path
1216 *
1217 * @dp: pointer to device path
1218 * @maxlen: maximum length of the device path
1219 * Return:
1220 * * length of the device path if it is less or equal @maxlen
1221 * * -1 if the device path is longer then @maxlen
1222 * * -1 if a device path node has a length of less than 4
1223 * * -EINVAL if maxlen exceeds SSIZE_MAX
1224 */
1225ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1226 const size_t maxlen)
1227{
1228 ssize_t ret = 0;
1229 u16 len;
1230
1231 if (maxlen > SSIZE_MAX)
1232 return -EINVAL;
1233 for (;;) {
1234 len = dp->length;
1235 if (len < 4)
1236 return -1;
1237 ret += len;
1238 if (ret > maxlen)
1239 return -1;
1240 if (dp->type == DEVICE_PATH_TYPE_END &&
1241 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1242 return ret;
1243 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1244 }
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001245}
1246
1247/**
1248 * efi_dp_from_lo() - Get the instance of a VenMedia node in a
1249 * multi-instance device path that matches
1250 * a specific GUID. This kind of device paths
1251 * is found in Boot#### options describing an
1252 * initrd location
1253 *
1254 * @lo: EFI_LOAD_OPTION containing a valid device path
1255 * @size: size of the discovered device path
1256 * @guid: guid to search for
1257 *
1258 * Return:
1259 * device path including the VenMedia node or NULL.
1260 * Caller must free the returned value.
1261 */
1262struct
1263efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
1264 efi_uintn_t *size, efi_guid_t guid)
1265{
1266 struct efi_device_path *fp = lo->file_path;
1267 struct efi_device_path_vendor *vendor;
1268 int lo_len = lo->file_path_length;
1269
1270 for (; lo_len >= sizeof(struct efi_device_path);
1271 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1272 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1273 break;
1274 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1275 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1276 continue;
1277
1278 vendor = (struct efi_device_path_vendor *)fp;
1279 if (!guidcmp(&vendor->guid, &guid))
1280 return efi_dp_dup(fp);
1281 }
1282 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1283
1284 return NULL;
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001285}