blob: dc787b4d3dded0143421deb30e13a6e62244dd4b [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
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +02008#define LOG_CATEGORY LOGC_EFI
9
Rob Clarkf90cb9f2017-09-13 18:05:28 -040010#include <common.h>
11#include <blk.h>
12#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <net.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040015#include <usb.h>
16#include <mmc.h>
Patrick Wildta3ca37e2019-10-03 16:24:17 +020017#include <nvme.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040018#include <efi_loader.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040019#include <part.h>
AKASHI Takahiro659a6262019-09-12 13:52:35 +090020#include <sandboxblockdev.h>
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +020021#include <uuid.h>
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +020022#include <asm-generic/unaligned.h>
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +090023#include <linux/compat.h> /* U16_MAX */
Rob Clarkf90cb9f2017-09-13 18:05:28 -040024
AKASHI Takahiro659a6262019-09-12 13:52:35 +090025#ifdef CONFIG_SANDBOX
26const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
27#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +020028#ifdef CONFIG_VIRTIO_BLK
29const efi_guid_t efi_guid_virtio_dev = U_BOOT_VIRTIO_DEV_GUID;
30#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +090031
Rob Clarkf90cb9f2017-09-13 18:05:28 -040032/* template END node: */
33static const struct efi_device_path END = {
34 .type = DEVICE_PATH_TYPE_END,
35 .sub_type = DEVICE_PATH_SUB_TYPE_END,
36 .length = sizeof(END),
37};
38
Rob Clarkf90cb9f2017-09-13 18:05:28 -040039/* template ROOT node: */
40static const struct efi_device_path_vendor ROOT = {
41 .dp = {
42 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
43 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
44 .length = sizeof(ROOT),
45 },
46 .guid = U_BOOT_GUID,
47};
48
Simon Glass222f3cb2021-09-24 18:30:17 -060049#if defined(CONFIG_MMC)
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010050/*
51 * Determine if an MMC device is an SD card.
52 *
53 * @desc block device descriptor
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010054 * Return: true if the device is an SD card
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010055 */
56static bool is_sd(struct blk_desc *desc)
57{
58 struct mmc *mmc = find_mmc_device(desc->devnum);
59
60 if (!mmc)
61 return false;
62
63 return IS_SD(mmc) != 0U;
64}
65#endif
66
Rob Clarkf90cb9f2017-09-13 18:05:28 -040067static void *dp_alloc(size_t sz)
68{
69 void *buf;
70
Heinrich Schuchardt16546012021-08-17 15:15:23 +020071 if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, sz, &buf) !=
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010072 EFI_SUCCESS) {
73 debug("EFI: ERROR: out of memory in %s\n", __func__);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040074 return NULL;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010075 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -040076
Patrick Wildtacf7bee2018-03-25 19:54:03 +020077 memset(buf, 0, sz);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040078 return buf;
79}
80
81/*
82 * Iterate to next block in device-path, terminating (returning NULL)
83 * at /End* node.
84 */
85struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
86{
87 if (dp == NULL)
88 return NULL;
89 if (dp->type == DEVICE_PATH_TYPE_END)
90 return NULL;
91 dp = ((void *)dp) + dp->length;
92 if (dp->type == DEVICE_PATH_TYPE_END)
93 return NULL;
94 return (struct efi_device_path *)dp;
95}
96
97/*
98 * Compare two device-paths, stopping when the shorter of the two hits
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +020099 * an End* node. This is useful to, for example, compare a device-path
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400100 * representing a device with one representing a file on the device, or
101 * a device with a parent device.
102 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +0200103int efi_dp_match(const struct efi_device_path *a,
104 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400105{
106 while (1) {
107 int ret;
108
109 ret = memcmp(&a->length, &b->length, sizeof(a->length));
110 if (ret)
111 return ret;
112
113 ret = memcmp(a, b, a->length);
114 if (ret)
115 return ret;
116
117 a = efi_dp_next(a);
118 b = efi_dp_next(b);
119
120 if (!a || !b)
121 return 0;
122 }
123}
124
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400125/*
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200126 * We can have device paths that start with a USB WWID or a USB Class node,
127 * and a few other cases which don't encode the full device path with bus
128 * hierarchy:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400129 *
130 * - MESSAGING:USB_WWID
131 * - MESSAGING:USB_CLASS
132 * - MEDIA:FILE_PATH
133 * - MEDIA:HARD_DRIVE
134 * - MESSAGING:URI
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200135 *
136 * See UEFI spec (section 3.1.2, about short-form device-paths)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400137 */
138static struct efi_device_path *shorten_path(struct efi_device_path *dp)
139{
140 while (dp) {
141 /*
142 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
143 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
144 * so not sure when we would see these other cases.
145 */
146 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
147 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
148 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
149 return dp;
150
151 dp = efi_dp_next(dp);
152 }
153
154 return dp;
155}
156
157static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
158 struct efi_device_path **rem)
159{
160 struct efi_object *efiobj;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200161 efi_uintn_t dp_size = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400162
163 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100164 struct efi_handler *handler;
165 struct efi_device_path *obj_dp;
166 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400167
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200168 ret = efi_search_protocol(efiobj,
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100169 &efi_guid_device_path, &handler);
170 if (ret != EFI_SUCCESS)
171 continue;
172 obj_dp = handler->protocol_interface;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400173
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100174 do {
175 if (efi_dp_match(dp, obj_dp) == 0) {
176 if (rem) {
Alexander Graff9360fb2017-12-11 14:29:46 +0100177 /*
178 * Allow partial matches, but inform
179 * the caller.
180 */
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100181 *rem = ((void *)dp) +
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200182 efi_dp_instance_size(obj_dp);
Alexander Graff9360fb2017-12-11 14:29:46 +0100183 return efiobj;
184 } else {
185 /* Only return on exact matches */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200186 if (efi_dp_instance_size(obj_dp) ==
187 dp_size)
Alexander Graff9360fb2017-12-11 14:29:46 +0100188 return efiobj;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400189 }
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100190 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400191
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100192 obj_dp = shorten_path(efi_dp_next(obj_dp));
193 } while (short_path && obj_dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400194 }
195
196 return NULL;
197}
198
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400199/*
200 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
201 * remaining part of the device path after the matched object.
202 */
203struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
204 struct efi_device_path **rem)
205{
206 struct efi_object *efiobj;
207
Alexander Graff9360fb2017-12-11 14:29:46 +0100208 /* Search for an exact match first */
209 efiobj = find_obj(dp, false, NULL);
210
211 /* Then for a fuzzy match */
212 if (!efiobj)
213 efiobj = find_obj(dp, false, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400214
Alexander Graff9360fb2017-12-11 14:29:46 +0100215 /* And now for a fuzzy short match */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400216 if (!efiobj)
217 efiobj = find_obj(dp, true, rem);
218
219 return efiobj;
220}
221
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100222/*
223 * Determine the last device path node that is not the end node.
224 *
225 * @dp device path
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100226 * Return: last node before the end node if it exists
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100227 * otherwise NULL
228 */
229const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
230{
231 struct efi_device_path *ret;
232
233 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
234 return NULL;
235 while (dp) {
236 ret = (struct efi_device_path *)dp;
237 dp = efi_dp_next(dp);
238 }
239 return ret;
240}
241
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200242/* get size of the first device path instance excluding end node */
243efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400244{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200245 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400246
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200247 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
248 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400249 while (dp) {
250 sz += dp->length;
251 dp = efi_dp_next(dp);
252 }
253
254 return sz;
255}
256
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200257/* get size of multi-instance device path excluding end node */
258efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
259{
260 const struct efi_device_path *p = dp;
261
262 if (!p)
263 return 0;
264 while (p->type != DEVICE_PATH_TYPE_END ||
265 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
266 p = (void *)p + p->length;
267
268 return (void *)p - (void *)dp;
269}
270
271/* copy multi-instance device path */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400272struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
273{
274 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200275 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400276
277 if (!dp)
278 return NULL;
279
280 ndp = dp_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100281 if (!ndp)
282 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400283 memcpy(ndp, dp, sz);
284
285 return ndp;
286}
287
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200288/**
289 * efi_dp_append_or_concatenate() - Append or concatenate two device paths.
290 * Concatenated device path will be separated
291 * by a sub-type 0xff end node
292 *
293 * @dp1: First device path
294 * @dp2: Second device path
295 * @concat: If true the two device paths will be concatenated and separated
296 * by an end of entrire device path sub-type 0xff end node.
297 * If true the second device path will be appended to the first and
298 * terminated by an end node
299 *
300 * Return:
301 * concatenated device path or NULL. Caller must free the returned value
302 */
303static struct
304efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
305 const struct efi_device_path *dp2,
306 bool concat)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400307{
308 struct efi_device_path *ret;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200309 size_t end_size = sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400310
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200311 if (concat)
312 end_size = 2 * sizeof(END);
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200313 if (!dp1 && !dp2) {
314 /* return an end node */
315 ret = efi_dp_dup(&END);
316 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400317 ret = efi_dp_dup(dp2);
318 } else if (!dp2) {
319 ret = efi_dp_dup(dp1);
320 } else {
321 /* both dp1 and dp2 are non-null */
322 unsigned sz1 = efi_dp_size(dp1);
323 unsigned sz2 = efi_dp_size(dp2);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200324 void *p = dp_alloc(sz1 + sz2 + end_size);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100325 if (!p)
326 return NULL;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200327 ret = p;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400328 memcpy(p, dp1, sz1);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200329 p += sz1;
330
331 if (concat) {
332 memcpy(p, &END, sizeof(END));
333 p += sizeof(END);
334 }
335
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200336 /* the end node of the second device path has to be retained */
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200337 memcpy(p, dp2, sz2);
338 p += sz2;
339 memcpy(p, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400340 }
341
342 return ret;
343}
344
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200345/**
346 * efi_dp_append() - Append a device to an existing device path.
347 *
348 * @dp1: First device path
349 * @dp2: Second device path
350 *
351 * Return:
352 * concatenated device path or NULL. Caller must free the returned value
353 */
354struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
355 const struct efi_device_path *dp2)
356{
357 return efi_dp_append_or_concatenate(dp1, dp2, false);
358}
359
360/**
361 * efi_dp_concat() - Concatenate 2 device paths. The final device path will
362 * contain two device paths separated by and end node (0xff).
363 *
364 * @dp1: First device path
365 * @dp2: Second device path
366 *
367 * Return:
368 * concatenated device path or NULL. Caller must free the returned value
369 */
370struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
371 const struct efi_device_path *dp2)
372{
373 return efi_dp_append_or_concatenate(dp1, dp2, true);
374}
375
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400376struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
377 const struct efi_device_path *node)
378{
379 struct efi_device_path *ret;
380
381 if (!node && !dp) {
382 ret = efi_dp_dup(&END);
383 } else if (!node) {
384 ret = efi_dp_dup(dp);
385 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200386 size_t sz = node->length;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400387 void *p = dp_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100388 if (!p)
389 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400390 memcpy(p, node, sz);
391 memcpy(p + sz, &END, sizeof(END));
392 ret = p;
393 } else {
394 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200395 size_t sz = efi_dp_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400396 void *p = dp_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100397 if (!p)
398 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400399 memcpy(p, dp, sz);
400 memcpy(p + sz, node, node->length);
401 memcpy(p + sz + node->length, &END, sizeof(END));
402 ret = p;
403 }
404
405 return ret;
406}
407
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200408struct efi_device_path *efi_dp_create_device_node(const u8 type,
409 const u8 sub_type,
410 const u16 length)
411{
412 struct efi_device_path *ret;
413
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200414 if (length < sizeof(struct efi_device_path))
415 return NULL;
416
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200417 ret = dp_alloc(length);
418 if (!ret)
419 return ret;
420 ret->type = type;
421 ret->sub_type = sub_type;
422 ret->length = length;
423 return ret;
424}
425
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200426struct efi_device_path *efi_dp_append_instance(
427 const struct efi_device_path *dp,
428 const struct efi_device_path *dpi)
429{
430 size_t sz, szi;
431 struct efi_device_path *p, *ret;
432
433 if (!dpi)
434 return NULL;
435 if (!dp)
436 return efi_dp_dup(dpi);
437 sz = efi_dp_size(dp);
438 szi = efi_dp_instance_size(dpi);
439 p = dp_alloc(sz + szi + 2 * sizeof(END));
440 if (!p)
441 return NULL;
442 ret = p;
443 memcpy(p, dp, sz + sizeof(END));
444 p = (void *)p + sz;
445 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
446 p = (void *)p + sizeof(END);
447 memcpy(p, dpi, szi);
448 p = (void *)p + szi;
449 memcpy(p, &END, sizeof(END));
450 return ret;
451}
452
453struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
454 efi_uintn_t *size)
455{
456 size_t sz;
457 struct efi_device_path *p;
458
459 if (size)
460 *size = 0;
461 if (!dp || !*dp)
462 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200463 sz = efi_dp_instance_size(*dp);
464 p = dp_alloc(sz + sizeof(END));
465 if (!p)
466 return NULL;
467 memcpy(p, *dp, sz + sizeof(END));
468 *dp = (void *)*dp + sz;
469 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
470 *dp = (void *)*dp + sizeof(END);
471 else
472 *dp = NULL;
473 if (size)
474 *size = sz + sizeof(END);
475 return p;
476}
477
478bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
479{
480 const struct efi_device_path *p = dp;
481
482 if (!p)
483 return false;
484 while (p->type != DEVICE_PATH_TYPE_END)
485 p = (void *)p + p->length;
486 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
487}
488
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400489/* size of device-path not including END node for device and all parents
490 * up to the root device.
491 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100492__maybe_unused static unsigned int dp_size(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400493{
494 if (!dev || !dev->driver)
495 return sizeof(ROOT);
496
Simon Glass56ada7b2022-01-29 14:58:38 -0700497 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400498 case UCLASS_ROOT:
499 case UCLASS_SIMPLE_BUS:
500 /* stop traversing parents at this point: */
501 return sizeof(ROOT);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100502 case UCLASS_ETH:
503 return dp_size(dev->parent) +
504 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100505 case UCLASS_BLK:
506 switch (dev->parent->uclass->uc_drv->id) {
507#ifdef CONFIG_IDE
508 case UCLASS_IDE:
509 return dp_size(dev->parent) +
510 sizeof(struct efi_device_path_atapi);
511#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600512#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100513 case UCLASS_SCSI:
514 return dp_size(dev->parent) +
515 sizeof(struct efi_device_path_scsi);
516#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600517#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100518 case UCLASS_MMC:
519 return dp_size(dev->parent) +
520 sizeof(struct efi_device_path_sd_mmc_path);
521#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200522#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
523 case UCLASS_AHCI:
524 return dp_size(dev->parent) +
525 sizeof(struct efi_device_path_sata);
526#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200527#if defined(CONFIG_NVME)
528 case UCLASS_NVME:
529 return dp_size(dev->parent) +
530 sizeof(struct efi_device_path_nvme);
531#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900532#ifdef CONFIG_SANDBOX
533 case UCLASS_ROOT:
534 /*
535 * Sandbox's host device will be represented
536 * as vendor device with extra one byte for
537 * device number
538 */
539 return dp_size(dev->parent)
540 + sizeof(struct efi_device_path_vendor) + 1;
541#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200542#ifdef CONFIG_VIRTIO_BLK
543 case UCLASS_VIRTIO:
544 /*
545 * Virtio devices will be represented as a vendor
546 * device node with an extra byte for the device
547 * number.
548 */
549 return dp_size(dev->parent)
550 + sizeof(struct efi_device_path_vendor) + 1;
551#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100552 default:
553 return dp_size(dev->parent);
554 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600555#if 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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100575 * Return: pointer to the end of the device path
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100576 */
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
Simon Glass56ada7b2022-01-29 14:58:38 -0700582 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400583 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 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600590#ifdef CONFIG_NET
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100591 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 case UCLASS_BLK:
608 switch (dev->parent->uclass->uc_drv->id) {
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900609#ifdef CONFIG_SANDBOX
610 case UCLASS_ROOT: {
611 /* stop traversing parents at this point: */
Heinrich Schuchardt1e3beaf2020-05-06 01:28:08 +0200612 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700613 struct blk_desc *desc = dev_get_uclass_plat(dev);
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900614
615 dp_fill(buf, dev->parent);
616 dp = buf;
617 ++dp;
618 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
619 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
620 dp->dp.length = sizeof(*dp) + 1;
621 memcpy(&dp->guid, &efi_guid_host_dev,
622 sizeof(efi_guid_t));
623 dp->vendor_data[0] = desc->devnum;
624 return &dp->vendor_data[1];
625 }
626#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200627#ifdef CONFIG_VIRTIO_BLK
628 case UCLASS_VIRTIO: {
629 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700630 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200631
632 dp_fill(buf, dev->parent);
633 dp = buf;
634 ++dp;
635 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
636 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
637 dp->dp.length = sizeof(*dp) + 1;
638 memcpy(&dp->guid, &efi_guid_virtio_dev,
639 sizeof(efi_guid_t));
640 dp->vendor_data[0] = desc->devnum;
641 return &dp->vendor_data[1];
642 }
643#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100644#ifdef CONFIG_IDE
645 case UCLASS_IDE: {
646 struct efi_device_path_atapi *dp =
647 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700648 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100649
650 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
651 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
652 dp->dp.length = sizeof(*dp);
653 dp->logical_unit_number = desc->devnum;
654 dp->primary_secondary = IDE_BUS(desc->devnum);
655 dp->slave_master = desc->devnum %
656 (CONFIG_SYS_IDE_MAXDEVICE /
657 CONFIG_SYS_IDE_MAXBUS);
658 return &dp[1];
659 }
660#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600661#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100662 case UCLASS_SCSI: {
663 struct efi_device_path_scsi *dp =
664 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700665 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100666
667 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
668 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
669 dp->dp.length = sizeof(*dp);
670 dp->logical_unit_number = desc->lun;
671 dp->target_id = desc->target;
672 return &dp[1];
673 }
674#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600675#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100676 case UCLASS_MMC: {
677 struct efi_device_path_sd_mmc_path *sddp =
678 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700679 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100680
681 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
682 sddp->dp.sub_type = is_sd(desc) ?
683 DEVICE_PATH_SUB_TYPE_MSG_SD :
684 DEVICE_PATH_SUB_TYPE_MSG_MMC;
685 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700686 sddp->slot_number = dev_seq(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100687 return &sddp[1];
688 }
689#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200690#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
691 case UCLASS_AHCI: {
692 struct efi_device_path_sata *dp =
693 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700694 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200695
696 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
697 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
698 dp->dp.length = sizeof(*dp);
699 dp->hba_port = desc->devnum;
700 /* default 0xffff implies no port multiplier */
701 dp->port_multiplier_port = 0xffff;
702 dp->logical_unit_number = desc->lun;
703 return &dp[1];
704 }
705#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200706#if defined(CONFIG_NVME)
707 case UCLASS_NVME: {
708 struct efi_device_path_nvme *dp =
709 dp_fill(buf, dev->parent);
710 u32 ns_id;
711
712 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
713 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
714 dp->dp.length = sizeof(*dp);
715 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
716 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
717 return &dp[1];
718 }
719#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100720 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100721 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
722 __FILE__, __LINE__, __func__,
723 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100724 return dp_fill(buf, dev->parent);
725 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600726#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400727 case UCLASS_MMC: {
728 struct efi_device_path_sd_mmc_path *sddp =
729 dp_fill(buf, dev->parent);
730 struct mmc *mmc = mmc_get_mmc_dev(dev);
731 struct blk_desc *desc = mmc_get_blk_desc(mmc);
732
733 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100734 sddp->dp.sub_type = is_sd(desc) ?
735 DEVICE_PATH_SUB_TYPE_MSG_SD :
736 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400737 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700738 sddp->slot_number = dev_seq(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400739
740 return &sddp[1];
741 }
742#endif
743 case UCLASS_MASS_STORAGE:
744 case UCLASS_USB_HUB: {
745 struct efi_device_path_usb_class *udp =
746 dp_fill(buf, dev->parent);
747 struct usb_device *udev = dev_get_parent_priv(dev);
748 struct usb_device_descriptor *desc = &udev->descriptor;
749
750 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
751 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
752 udp->dp.length = sizeof(*udp);
753 udp->vendor_id = desc->idVendor;
754 udp->product_id = desc->idProduct;
755 udp->device_class = desc->bDeviceClass;
756 udp->device_subclass = desc->bDeviceSubClass;
757 udp->device_protocol = desc->bDeviceProtocol;
758
759 return &udp[1];
760 }
761 default:
Simon Glass56ada7b2022-01-29 14:58:38 -0700762 /* If the uclass driver is missing, this will show NULL */
763 log_debug("unhandled device class: %s (%s)\n", dev->name,
764 dev_get_uclass_name(dev));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400765 return dp_fill(buf, dev->parent);
766 }
767}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400768
769static unsigned dp_part_size(struct blk_desc *desc, int part)
770{
771 unsigned dpsize;
Simon Glassec209a72022-01-29 14:58:39 -0700772 struct udevice *dev = desc->bdev;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400773
Simon Glass222f3cb2021-09-24 18:30:17 -0600774 dpsize = dp_size(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400775
776 if (part == 0) /* the actual disk, not a partition */
777 return dpsize;
778
779 if (desc->part_type == PART_TYPE_ISO)
780 dpsize += sizeof(struct efi_device_path_cdrom_path);
781 else
782 dpsize += sizeof(struct efi_device_path_hard_drive_path);
783
784 return dpsize;
785}
786
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100787/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100788 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100789 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200790 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100791 * @desc block device descriptor
792 * @part partition number, 0 identifies a block device
793 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100794static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400795{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600796 struct disk_partition info;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400797
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400798 part_get_info(desc, part, &info);
799
800 if (desc->part_type == PART_TYPE_ISO) {
801 struct efi_device_path_cdrom_path *cddp = buf;
802
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100803 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400804 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
805 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
806 cddp->dp.length = sizeof(*cddp);
807 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200808 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400809
810 buf = &cddp[1];
811 } else {
812 struct efi_device_path_hard_drive_path *hddp = buf;
813
814 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
815 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
816 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100817 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400818 hddp->partition_start = info.start;
819 hddp->partition_end = info.size;
820 if (desc->part_type == PART_TYPE_EFI)
821 hddp->partmap_type = 2;
822 else
823 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100824
825 switch (desc->sig_type) {
826 case SIG_TYPE_NONE:
827 default:
828 hddp->signature_type = 0;
829 memset(hddp->partition_signature, 0,
830 sizeof(hddp->partition_signature));
831 break;
832 case SIG_TYPE_MBR:
833 hddp->signature_type = 1;
834 memset(hddp->partition_signature, 0,
835 sizeof(hddp->partition_signature));
836 memcpy(hddp->partition_signature, &desc->mbr_sig,
837 sizeof(desc->mbr_sig));
838 break;
839 case SIG_TYPE_GUID:
840 hddp->signature_type = 2;
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200841 if (uuid_str_to_bin(info.uuid,
842 hddp->partition_signature, 1))
843 log_warning(
844 "Partition no. %d: invalid guid: %s\n",
845 part, info.uuid);
Jonathan Gray84b4d702017-11-22 14:18:59 +1100846 break;
847 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400848
849 buf = &hddp[1];
850 }
851
852 return buf;
853}
854
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100855/*
856 * Create a device path for a block device or one of its partitions.
857 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200858 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100859 * @desc block device descriptor
860 * @part partition number, 0 identifies a block device
861 */
862static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
863{
Simon Glassec209a72022-01-29 14:58:39 -0700864 struct udevice *dev = desc->bdev;
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100865
Simon Glass222f3cb2021-09-24 18:30:17 -0600866 buf = dp_fill(buf, dev);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100867
868 if (part == 0) /* the actual disk, not a partition */
869 return buf;
870
871 return dp_part_node(buf, desc, part);
872}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400873
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200874/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400875struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
876{
877 void *buf, *start;
878
879 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100880 if (!buf)
881 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400882
883 buf = dp_part_fill(buf, desc, part);
884
885 *((struct efi_device_path *)buf) = END;
886
887 return start;
888}
889
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100890/*
891 * Create a device node for a block device partition.
892 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200893 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100894 * @desc block device descriptor
895 * @part partition number, 0 identifies a block device
896 */
897struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
898{
899 efi_uintn_t dpsize;
900 void *buf;
901
902 if (desc->part_type == PART_TYPE_ISO)
903 dpsize = sizeof(struct efi_device_path_cdrom_path);
904 else
905 dpsize = sizeof(struct efi_device_path_hard_drive_path);
906 buf = dp_alloc(dpsize);
907
908 dp_part_node(buf, desc, part);
909
910 return buf;
911}
912
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200913/**
914 * path_to_uefi() - convert UTF-8 path to an UEFI style path
915 *
916 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
917 * separators and UTF-16).
918 *
919 * @src: source buffer
920 * @uefi: target buffer, possibly unaligned
921 */
922static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400923{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200924 u16 *pos = uefi;
925
926 /*
927 * efi_set_bootdev() calls this routine indirectly before the UEFI
928 * subsystem is initialized. So we cannot assume unaligned access to be
929 * enabled.
930 */
931 allow_unaligned();
932
933 while (*src) {
934 s32 code = utf8_get(&src);
935
936 if (code < 0)
937 code = '?';
938 else if (code == '/')
939 code = '\\';
940 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400941 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200942 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400943}
944
945/*
946 * If desc is NULL, this creates a path with only the file component,
947 * otherwise it creates a full path with both device and file components
948 */
949struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
950 const char *path)
951{
952 struct efi_device_path_file_path *fp;
953 void *buf, *start;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900954 size_t dpsize = 0, fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400955
956 if (desc)
957 dpsize = dp_part_size(desc, part);
958
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200959 fpsize = sizeof(struct efi_device_path) +
960 2 * (utf8_utf16_strlen(path) + 1);
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900961 if (fpsize > U16_MAX)
962 return NULL;
963
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400964 dpsize += fpsize;
965
966 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100967 if (!buf)
968 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400969
970 if (desc)
971 buf = dp_part_fill(buf, desc, part);
972
973 /* add file-path: */
974 fp = buf;
975 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
976 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900977 fp->dp.length = (u16)fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400978 path_to_uefi(fp->str, path);
979 buf += fpsize;
980
981 *((struct efi_device_path *)buf) = END;
982
983 return start;
984}
985
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100986struct efi_device_path *efi_dp_from_uart(void)
987{
988 void *buf, *pos;
989 struct efi_device_path_uart *uart;
990 size_t dpsize = sizeof(ROOT) + sizeof(*uart) + sizeof(END);
991
992 buf = dp_alloc(dpsize);
993 if (!buf)
994 return NULL;
995 pos = buf;
996 memcpy(pos, &ROOT, sizeof(ROOT));
997 pos += sizeof(ROOT);
998 uart = pos;
999 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1000 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
1001 uart->dp.length = sizeof(*uart);
1002 pos += sizeof(*uart);
1003 memcpy(pos, &END, sizeof(END));
1004
1005 return buf;
1006}
1007
Joe Hershberger5277a972018-04-13 15:26:39 -05001008#ifdef CONFIG_NET
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001009struct efi_device_path *efi_dp_from_eth(void)
1010{
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001011 void *buf, *start;
1012 unsigned dpsize = 0;
1013
1014 assert(eth_get_dev());
1015
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001016 dpsize += dp_size(eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001017
1018 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001019 if (!buf)
1020 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001021
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001022 buf = dp_fill(buf, eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001023
1024 *((struct efi_device_path *)buf) = END;
1025
1026 return start;
1027}
1028#endif
1029
Rob Clark18ceba72017-10-10 08:23:06 -04001030/* Construct a device-path for memory-mapped image */
1031struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1032 uint64_t start_address,
1033 uint64_t end_address)
1034{
1035 struct efi_device_path_memory *mdp;
1036 void *buf, *start;
1037
1038 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001039 if (!buf)
1040 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -04001041
1042 mdp = buf;
1043 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1044 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1045 mdp->dp.length = sizeof(*mdp);
1046 mdp->memory_type = memory_type;
1047 mdp->start_address = start_address;
1048 mdp->end_address = end_address;
1049 buf = &mdp[1];
1050
1051 *((struct efi_device_path *)buf) = END;
1052
1053 return start;
1054}
1055
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001056/**
1057 * efi_dp_split_file_path() - split of relative file path from device path
1058 *
1059 * Given a device path indicating a file on a device, separate the device
1060 * path in two: the device path of the actual device and the file path
1061 * relative to this device.
1062 *
1063 * @full_path: device path including device and file path
1064 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001065 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001066 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001067 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001068efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1069 struct efi_device_path **device_path,
1070 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001071{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001072 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001073
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001074 *device_path = NULL;
1075 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001076 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001077 if (!dp)
1078 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001079 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001080 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001081 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001082 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001083 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001084 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001085 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001086 if (!fp)
1087 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001088 p->type = DEVICE_PATH_TYPE_END;
1089 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1090 p->length = sizeof(*p);
1091
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001092out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001093 *device_path = dp;
1094 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001095 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001096}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001097
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001098/**
1099 * efi_dp_from_name() - convert U-Boot device and file path to device path
1100 *
1101 * @dev: U-Boot device, e.g. 'mmc'
1102 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1103 * @path: file path relative to U-Boot device, may be NULL
1104 * @device: pointer to receive device path of the device
1105 * @file: pointer to receive device path for the file
1106 * Return: status code
1107 */
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001108efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1109 const char *path,
1110 struct efi_device_path **device,
1111 struct efi_device_path **file)
1112{
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001113 struct blk_desc *desc = NULL;
Simon Glassc1c4a8f2020-05-10 11:39:57 -06001114 struct disk_partition fs_partition;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001115 int part = 0;
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001116 char *filename;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001117 char *s;
1118
AKASHI Takahiro39844412018-11-05 18:06:40 +09001119 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001120 return EFI_INVALID_PARAMETER;
1121
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001122 if (!strcmp(dev, "Net")) {
1123#ifdef CONFIG_NET
1124 if (device)
1125 *device = efi_dp_from_eth();
1126#endif
1127 } else if (!strcmp(dev, "Uart")) {
1128 if (device)
1129 *device = efi_dp_from_uart();
1130 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001131 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1132 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001133 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001134 return EFI_INVALID_PARAMETER;
1135
AKASHI Takahiro39844412018-11-05 18:06:40 +09001136 if (device)
1137 *device = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001138 }
1139
1140 if (!path)
1141 return EFI_SUCCESS;
1142
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001143 filename = calloc(1, strlen(path) + 1);
1144 if (!filename)
1145 return EFI_OUT_OF_RESOURCES;
1146
1147 sprintf(filename, "%s", path);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001148 /* DOS style file path: */
1149 s = filename;
1150 while ((s = strchr(s, '/')))
1151 *s++ = '\\';
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001152 *file = efi_dp_from_file(desc, part, filename);
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001153 free(filename);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001154
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001155 if (!*file)
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001156 return EFI_INVALID_PARAMETER;
1157
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001158 return EFI_SUCCESS;
1159}
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001160
1161/**
1162 * efi_dp_check_length() - check length of a device path
1163 *
1164 * @dp: pointer to device path
1165 * @maxlen: maximum length of the device path
1166 * Return:
1167 * * length of the device path if it is less or equal @maxlen
1168 * * -1 if the device path is longer then @maxlen
1169 * * -1 if a device path node has a length of less than 4
1170 * * -EINVAL if maxlen exceeds SSIZE_MAX
1171 */
1172ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1173 const size_t maxlen)
1174{
1175 ssize_t ret = 0;
1176 u16 len;
1177
1178 if (maxlen > SSIZE_MAX)
1179 return -EINVAL;
1180 for (;;) {
1181 len = dp->length;
1182 if (len < 4)
1183 return -1;
1184 ret += len;
1185 if (ret > maxlen)
1186 return -1;
1187 if (dp->type == DEVICE_PATH_TYPE_END &&
1188 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1189 return ret;
1190 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1191 }
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001192}
1193
1194/**
1195 * efi_dp_from_lo() - Get the instance of a VenMedia node in a
1196 * multi-instance device path that matches
1197 * a specific GUID. This kind of device paths
1198 * is found in Boot#### options describing an
1199 * initrd location
1200 *
1201 * @lo: EFI_LOAD_OPTION containing a valid device path
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001202 * @guid: guid to search for
1203 *
1204 * Return:
1205 * device path including the VenMedia node or NULL.
1206 * Caller must free the returned value.
1207 */
1208struct
1209efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001210 const efi_guid_t *guid)
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001211{
1212 struct efi_device_path *fp = lo->file_path;
1213 struct efi_device_path_vendor *vendor;
1214 int lo_len = lo->file_path_length;
1215
1216 for (; lo_len >= sizeof(struct efi_device_path);
1217 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1218 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1219 break;
1220 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1221 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1222 continue;
1223
1224 vendor = (struct efi_device_path_vendor *)fp;
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001225 if (!guidcmp(&vendor->guid, guid))
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +02001226 return efi_dp_dup(efi_dp_next(fp));
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001227 }
1228 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1229
1230 return NULL;
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001231}
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001232
1233/**
1234 * search_gpt_dp_node() - search gpt device path node
1235 *
1236 * @device_path: device path
1237 *
1238 * Return: pointer to the gpt device path node
1239 */
1240struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1241{
1242 struct efi_device_path *dp = device_path;
1243
1244 while (dp) {
1245 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1246 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1247 struct efi_device_path_hard_drive_path *hd_dp =
1248 (struct efi_device_path_hard_drive_path *)dp;
1249
1250 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1251 hd_dp->signature_type == SIG_TYPE_GUID)
1252 return dp;
1253 }
1254 dp = efi_dp_next(dp);
1255 }
1256
1257 return NULL;
1258}