blob: 66a951a8c1bb5814df032060c06ca29127c61c8a [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 <blk.h>
11#include <dm.h>
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +020012#include <dm/root.h>
Tom Riniefa387d2025-05-21 16:51:19 -060013#include <ide.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <net.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040016#include <usb.h>
17#include <mmc.h>
Patrick Wildta3ca37e2019-10-03 16:24:17 +020018#include <nvme.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040019#include <efi_loader.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040020#include <part.h>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010021#include <u-boot/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
25/* template END node: */
Masahisa Kojima97bd9da2022-06-19 13:55:59 +090026const struct efi_device_path END = {
Rob Clarkf90cb9f2017-09-13 18:05:28 -040027 .type = DEVICE_PATH_TYPE_END,
28 .sub_type = DEVICE_PATH_SUB_TYPE_END,
29 .length = sizeof(END),
30};
31
Simon Glass222f3cb2021-09-24 18:30:17 -060032#if defined(CONFIG_MMC)
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010033/*
34 * Determine if an MMC device is an SD card.
35 *
36 * @desc block device descriptor
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010037 * Return: true if the device is an SD card
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010038 */
39static bool is_sd(struct blk_desc *desc)
40{
41 struct mmc *mmc = find_mmc_device(desc->devnum);
42
43 if (!mmc)
44 return false;
45
46 return IS_SD(mmc) != 0U;
47}
48#endif
49
Rob Clarkf90cb9f2017-09-13 18:05:28 -040050/*
51 * Iterate to next block in device-path, terminating (returning NULL)
52 * at /End* node.
53 */
54struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
55{
56 if (dp == NULL)
57 return NULL;
58 if (dp->type == DEVICE_PATH_TYPE_END)
59 return NULL;
60 dp = ((void *)dp) + dp->length;
61 if (dp->type == DEVICE_PATH_TYPE_END)
62 return NULL;
63 return (struct efi_device_path *)dp;
64}
65
66/*
67 * Compare two device-paths, stopping when the shorter of the two hits
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +020068 * an End* node. This is useful to, for example, compare a device-path
Rob Clarkf90cb9f2017-09-13 18:05:28 -040069 * representing a device with one representing a file on the device, or
70 * a device with a parent device.
71 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +020072int efi_dp_match(const struct efi_device_path *a,
73 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -040074{
75 while (1) {
76 int ret;
77
78 ret = memcmp(&a->length, &b->length, sizeof(a->length));
79 if (ret)
80 return ret;
81
82 ret = memcmp(a, b, a->length);
83 if (ret)
84 return ret;
85
86 a = efi_dp_next(a);
87 b = efi_dp_next(b);
88
89 if (!a || !b)
90 return 0;
91 }
92}
93
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +010094/**
95 * efi_dp_shorten() - shorten device-path
96 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +020097 * When creating a short boot option we want to use a device-path that is
98 * independent of the location where the block device is plugged in.
Rob Clarkf90cb9f2017-09-13 18:05:28 -040099 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200100 * UsbWwi() nodes contain a serial number, hard drive paths a partition
101 * UUID. Both should be unique.
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200102 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200103 * See UEFI spec, section 3.1.2 for "short-form device path".
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100104 *
Heinrich Schuchardtdb17dbc2022-03-21 08:26:48 +0100105 * @dp: original device-path
Heinrich Schuchardt2e670852024-04-26 12:09:38 +0200106 * Return: shortened device-path or NULL
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400107 */
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100108struct efi_device_path *efi_dp_shorten(struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400109{
110 while (dp) {
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200111 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_WWI) ||
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400112 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
113 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
114 return dp;
115
116 dp = efi_dp_next(dp);
117 }
118
119 return dp;
120}
121
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100122/**
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100123 * find_handle() - find handle by device path and installed protocol
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100124 *
125 * If @rem is provided, the handle with the longest partial match is returned.
126 *
127 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100128 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100129 * @short_path: use short form device path for matching
130 * @rem: pointer to receive remaining device path
131 * Return: matching handle
132 */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100133static efi_handle_t find_handle(struct efi_device_path *dp,
134 const efi_guid_t *guid, bool short_path,
135 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400136{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100137 efi_handle_t handle, best_handle = NULL;
138 efi_uintn_t len, best_len = 0;
139
140 len = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400141
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100142 list_for_each_entry(handle, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100143 struct efi_handler *handler;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100144 struct efi_device_path *dp_current;
145 efi_uintn_t len_current;
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100146 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400147
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100148 if (guid) {
149 ret = efi_search_protocol(handle, guid, &handler);
150 if (ret != EFI_SUCCESS)
151 continue;
152 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100153 ret = efi_search_protocol(handle, &efi_guid_device_path,
154 &handler);
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100155 if (ret != EFI_SUCCESS)
156 continue;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100157 dp_current = handler->protocol_interface;
158 if (short_path) {
159 dp_current = efi_dp_shorten(dp_current);
160 if (!dp_current)
161 continue;
162 }
163 len_current = efi_dp_instance_size(dp_current);
164 if (rem) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100165 if (len_current > len)
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100166 continue;
167 } else {
168 if (len_current != len)
169 continue;
170 }
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100171 if (memcmp(dp_current, dp, len_current))
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100172 continue;
173 if (!rem)
174 return handle;
175 if (len_current > best_len) {
176 best_len = len_current;
177 best_handle = handle;
178 *rem = (void*)((u8 *)dp + len_current);
179 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400180 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100181 return best_handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400182}
183
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100184/**
185 * efi_dp_find_obj() - find handle by device path
186 *
187 * If @rem is provided, the handle with the longest partial match is returned.
188 *
189 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100190 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100191 * @rem: pointer to receive remaining device path
192 * Return: matching handle
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400193 */
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100194efi_handle_t efi_dp_find_obj(struct efi_device_path *dp,
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100195 const efi_guid_t *guid,
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100196 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400197{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100198 efi_handle_t handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400199
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100200 handle = find_handle(dp, guid, false, rem);
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100201 if (!handle)
202 /* Match short form device path */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100203 handle = find_handle(dp, guid, true, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400204
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100205 return handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400206}
207
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100208/*
209 * Determine the last device path node that is not the end node.
210 *
211 * @dp device path
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100212 * Return: last node before the end node if it exists
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100213 * otherwise NULL
214 */
215const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
216{
217 struct efi_device_path *ret;
218
219 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
220 return NULL;
221 while (dp) {
222 ret = (struct efi_device_path *)dp;
223 dp = efi_dp_next(dp);
224 }
225 return ret;
226}
227
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200228/* get size of the first device path instance excluding end node */
229efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400230{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200231 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400232
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200233 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
234 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400235 while (dp) {
236 sz += dp->length;
237 dp = efi_dp_next(dp);
238 }
239
240 return sz;
241}
242
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200243/* get size of multi-instance device path excluding end node */
244efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
245{
246 const struct efi_device_path *p = dp;
247
248 if (!p)
249 return 0;
250 while (p->type != DEVICE_PATH_TYPE_END ||
251 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
252 p = (void *)p + p->length;
253
254 return (void *)p - (void *)dp;
255}
256
257/* copy multi-instance device path */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400258struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
259{
260 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200261 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400262
263 if (!dp)
264 return NULL;
265
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100266 ndp = efi_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100267 if (!ndp)
268 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400269 memcpy(ndp, dp, sz);
270
271 return ndp;
272}
273
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200274/**
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200275 * efi_dp_concat() - Concatenate two device paths and add and terminate them
276 * with an end node.
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200277 *
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200278 * @dp1: First device path
279 * @dp2: Second device path
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +0200280 * @split_end_node:
281 * * 0 to concatenate
282 * * 1 to concatenate with end node added as separator
283 * * size of dp1 excluding last end node to concatenate with end node as
284 * separator in case dp1 contains an end node
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200285 *
286 * Return:
287 * concatenated device path or NULL. Caller must free the returned value
288 */
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200289struct
290efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
291 const struct efi_device_path *dp2,
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +0200292 size_t split_end_node)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400293{
294 struct efi_device_path *ret;
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200295 size_t end_size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400296
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200297 if (!dp1 && !dp2) {
298 /* return an end node */
299 ret = efi_dp_dup(&END);
300 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400301 ret = efi_dp_dup(dp2);
302 } else if (!dp2) {
303 ret = efi_dp_dup(dp1);
304 } else {
305 /* both dp1 and dp2 are non-null */
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +0200306 size_t sz1;
307 size_t sz2 = efi_dp_size(dp2);
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200308 void *p;
309
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +0200310 if (split_end_node < sizeof(struct efi_device_path))
311 sz1 = efi_dp_size(dp1);
312 else
313 sz1 = split_end_node;
314
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200315 if (split_end_node)
316 end_size = 2 * sizeof(END);
317 else
318 end_size = sizeof(END);
319 p = efi_alloc(sz1 + sz2 + end_size);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100320 if (!p)
321 return NULL;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200322 ret = p;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400323 memcpy(p, dp1, sz1);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200324 p += sz1;
325
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200326 if (split_end_node) {
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200327 memcpy(p, &END, sizeof(END));
328 p += sizeof(END);
329 }
330
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200331 /* the end node of the second device path has to be retained */
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200332 memcpy(p, dp2, sz2);
333 p += sz2;
334 memcpy(p, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400335 }
336
337 return ret;
338}
339
340struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
341 const struct efi_device_path *node)
342{
343 struct efi_device_path *ret;
344
345 if (!node && !dp) {
346 ret = efi_dp_dup(&END);
347 } else if (!node) {
348 ret = efi_dp_dup(dp);
349 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200350 size_t sz = node->length;
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100351 void *p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100352 if (!p)
353 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400354 memcpy(p, node, sz);
355 memcpy(p + sz, &END, sizeof(END));
356 ret = p;
357 } else {
358 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200359 size_t sz = efi_dp_size(dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100360 void *p = efi_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100361 if (!p)
362 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400363 memcpy(p, dp, sz);
364 memcpy(p + sz, node, node->length);
365 memcpy(p + sz + node->length, &END, sizeof(END));
366 ret = p;
367 }
368
369 return ret;
370}
371
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200372struct efi_device_path *efi_dp_create_device_node(const u8 type,
373 const u8 sub_type,
374 const u16 length)
375{
376 struct efi_device_path *ret;
377
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200378 if (length < sizeof(struct efi_device_path))
379 return NULL;
380
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100381 ret = efi_alloc(length);
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200382 if (!ret)
383 return ret;
384 ret->type = type;
385 ret->sub_type = sub_type;
386 ret->length = length;
387 return ret;
388}
389
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200390struct efi_device_path *efi_dp_append_instance(
391 const struct efi_device_path *dp,
392 const struct efi_device_path *dpi)
393{
394 size_t sz, szi;
395 struct efi_device_path *p, *ret;
396
397 if (!dpi)
398 return NULL;
399 if (!dp)
400 return efi_dp_dup(dpi);
401 sz = efi_dp_size(dp);
402 szi = efi_dp_instance_size(dpi);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100403 p = efi_alloc(sz + szi + 2 * sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200404 if (!p)
405 return NULL;
406 ret = p;
407 memcpy(p, dp, sz + sizeof(END));
408 p = (void *)p + sz;
409 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
410 p = (void *)p + sizeof(END);
411 memcpy(p, dpi, szi);
412 p = (void *)p + szi;
413 memcpy(p, &END, sizeof(END));
414 return ret;
415}
416
417struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
418 efi_uintn_t *size)
419{
420 size_t sz;
421 struct efi_device_path *p;
422
423 if (size)
424 *size = 0;
425 if (!dp || !*dp)
426 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200427 sz = efi_dp_instance_size(*dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100428 p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200429 if (!p)
430 return NULL;
431 memcpy(p, *dp, sz + sizeof(END));
432 *dp = (void *)*dp + sz;
433 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
434 *dp = (void *)*dp + sizeof(END);
435 else
436 *dp = NULL;
437 if (size)
438 *size = sz + sizeof(END);
439 return p;
440}
441
442bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
443{
444 const struct efi_device_path *p = dp;
445
446 if (!p)
447 return false;
448 while (p->type != DEVICE_PATH_TYPE_END)
449 p = (void *)p + p->length;
450 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
451}
452
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400453/* size of device-path not including END node for device and all parents
454 * up to the root device.
455 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100456__maybe_unused static unsigned int dp_size(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400457{
458 if (!dev || !dev->driver)
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200459 return sizeof(struct efi_device_path_udevice);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400460
Simon Glass56ada7b2022-01-29 14:58:38 -0700461 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400462 case UCLASS_ROOT:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400463 /* stop traversing parents at this point: */
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200464 return sizeof(struct efi_device_path_udevice);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100465 case UCLASS_ETH:
466 return dp_size(dev->parent) +
467 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100468 case UCLASS_BLK:
469 switch (dev->parent->uclass->uc_drv->id) {
470#ifdef CONFIG_IDE
471 case UCLASS_IDE:
472 return dp_size(dev->parent) +
473 sizeof(struct efi_device_path_atapi);
474#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600475#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100476 case UCLASS_SCSI:
477 return dp_size(dev->parent) +
478 sizeof(struct efi_device_path_scsi);
479#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600480#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100481 case UCLASS_MMC:
482 return dp_size(dev->parent) +
483 sizeof(struct efi_device_path_sd_mmc_path);
484#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200485#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
486 case UCLASS_AHCI:
487 return dp_size(dev->parent) +
488 sizeof(struct efi_device_path_sata);
489#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200490#if defined(CONFIG_NVME)
491 case UCLASS_NVME:
492 return dp_size(dev->parent) +
493 sizeof(struct efi_device_path_nvme);
494#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100495#ifdef CONFIG_USB
496 case UCLASS_MASS_STORAGE:
497 return dp_size(dev->parent)
498 + sizeof(struct efi_device_path_controller);
499#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100500 default:
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200501 /* UCLASS_BLKMAP, UCLASS_HOST, UCLASS_VIRTIO */
502 return dp_size(dev->parent) +
503 sizeof(struct efi_device_path_udevice);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100504 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600505#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400506 case UCLASS_MMC:
507 return dp_size(dev->parent) +
508 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100509#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400510 case UCLASS_MASS_STORAGE:
511 case UCLASS_USB_HUB:
512 return dp_size(dev->parent) +
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100513 sizeof(struct efi_device_path_usb);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400514 default:
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200515 return dp_size(dev->parent) +
516 sizeof(struct efi_device_path_udevice);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400517 }
518}
519
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100520/*
521 * Recursively build a device path.
522 *
523 * @buf pointer to the end of the device path
524 * @dev device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100525 * Return: pointer to the end of the device path
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100526 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100527__maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400528{
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200529 enum uclass_id uclass_id;
530
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400531 if (!dev || !dev->driver)
532 return buf;
533
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200534 uclass_id = device_get_uclass_id(dev);
535 if (uclass_id != UCLASS_ROOT)
536 buf = dp_fill(buf, dev->parent);
537
538 switch (uclass_id) {
Jan Kiszkaf1389822022-10-14 18:10:06 +0200539#ifdef CONFIG_NETDEVICES
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100540 case UCLASS_ETH: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200541 struct efi_device_path_mac_addr *dp = buf;
Simon Glass95588622020-12-22 19:30:28 -0700542 struct eth_pdata *pdata = dev_get_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100543
544 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
545 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
546 dp->dp.length = sizeof(*dp);
547 memset(&dp->mac, 0, sizeof(dp->mac));
548 /* We only support IPv4 */
549 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
550 /* Ethernet */
551 dp->if_type = 1;
552 return &dp[1];
553 }
554#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100555 case UCLASS_BLK:
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200556 switch (device_get_uclass_id(dev->parent)) {
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100557#ifdef CONFIG_IDE
558 case UCLASS_IDE: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200559 struct efi_device_path_atapi *dp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700560 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100561
562 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
563 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
564 dp->dp.length = sizeof(*dp);
565 dp->logical_unit_number = desc->devnum;
566 dp->primary_secondary = IDE_BUS(desc->devnum);
567 dp->slave_master = desc->devnum %
568 (CONFIG_SYS_IDE_MAXDEVICE /
569 CONFIG_SYS_IDE_MAXBUS);
570 return &dp[1];
571 }
572#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600573#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100574 case UCLASS_SCSI: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200575 struct efi_device_path_scsi *dp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700576 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100577
578 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
579 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
580 dp->dp.length = sizeof(*dp);
581 dp->logical_unit_number = desc->lun;
582 dp->target_id = desc->target;
583 return &dp[1];
584 }
585#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600586#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100587 case UCLASS_MMC: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200588 struct efi_device_path_sd_mmc_path *sddp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700589 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100590
591 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
592 sddp->dp.sub_type = is_sd(desc) ?
593 DEVICE_PATH_SUB_TYPE_MSG_SD :
594 DEVICE_PATH_SUB_TYPE_MSG_MMC;
595 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700596 sddp->slot_number = dev_seq(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100597 return &sddp[1];
598 }
599#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200600#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
601 case UCLASS_AHCI: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200602 struct efi_device_path_sata *dp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700603 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200604
605 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
606 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
607 dp->dp.length = sizeof(*dp);
608 dp->hba_port = desc->devnum;
609 /* default 0xffff implies no port multiplier */
610 dp->port_multiplier_port = 0xffff;
611 dp->logical_unit_number = desc->lun;
612 return &dp[1];
613 }
614#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200615#if defined(CONFIG_NVME)
616 case UCLASS_NVME: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200617 struct efi_device_path_nvme *dp = buf;
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200618 u32 ns_id;
619
620 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
621 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
622 dp->dp.length = sizeof(*dp);
623 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
624 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
625 return &dp[1];
626 }
627#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100628#if defined(CONFIG_USB)
629 case UCLASS_MASS_STORAGE: {
Heinrich Schuchardt232c9db2023-04-01 07:21:55 +0200630 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200631 struct efi_device_path_controller *dp = buf;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100632
633 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
634 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CONTROLLER;
635 dp->dp.length = sizeof(*dp);
636 dp->controller_number = desc->lun;
637 return &dp[1];
638 }
639#endif
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200640 default: {
641 /* UCLASS_BLKMAP, UCLASS_HOST, UCLASS_VIRTIO */
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200642 struct efi_device_path_udevice *dp = buf;
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200643 struct blk_desc *desc = dev_get_uclass_plat(dev);
644
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200645 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
646 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
647 dp->dp.length = sizeof(*dp);
648 memcpy(&dp->guid, &efi_u_boot_guid,
649 sizeof(efi_guid_t));
650 dp->uclass_id = (UCLASS_BLK & 0xffff) |
651 (desc->uclass_id << 16);
652 dp->dev_number = desc->devnum;
653
654 return &dp[1];
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100655 }
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200656 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600657#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400658 case UCLASS_MMC: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200659 struct efi_device_path_sd_mmc_path *sddp = buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400660 struct mmc *mmc = mmc_get_mmc_dev(dev);
661 struct blk_desc *desc = mmc_get_blk_desc(mmc);
662
663 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100664 sddp->dp.sub_type = is_sd(desc) ?
665 DEVICE_PATH_SUB_TYPE_MSG_SD :
666 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400667 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700668 sddp->slot_number = dev_seq(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400669
670 return &sddp[1];
671 }
672#endif
673 case UCLASS_MASS_STORAGE:
674 case UCLASS_USB_HUB: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200675 struct efi_device_path_usb *udp = buf;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100676
677 switch (device_get_uclass_id(dev->parent)) {
678 case UCLASS_USB_HUB: {
679 struct usb_device *udev = dev_get_parent_priv(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400680
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100681 udp->parent_port_number = udev->portnr;
682 break;
683 }
684 default:
685 udp->parent_port_number = 0;
686 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400687 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100688 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400689 udp->dp.length = sizeof(*udp);
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100690 udp->usb_interface = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400691
692 return &udp[1];
693 }
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200694 default: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200695 struct efi_device_path_udevice *vdp = buf;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200696
697 vdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
698 vdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
699 vdp->dp.length = sizeof(*vdp);
700 memcpy(&vdp->guid, &efi_u_boot_guid, sizeof(efi_guid_t));
701 vdp->uclass_id = uclass_id;
702 vdp->dev_number = dev->seq_;
703
704 return &vdp[1];
705 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400706 }
707}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400708
709static unsigned dp_part_size(struct blk_desc *desc, int part)
710{
711 unsigned dpsize;
Simon Glassec209a72022-01-29 14:58:39 -0700712 struct udevice *dev = desc->bdev;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400713
Simon Glass222f3cb2021-09-24 18:30:17 -0600714 dpsize = dp_size(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400715
716 if (part == 0) /* the actual disk, not a partition */
717 return dpsize;
718
719 if (desc->part_type == PART_TYPE_ISO)
720 dpsize += sizeof(struct efi_device_path_cdrom_path);
721 else
722 dpsize += sizeof(struct efi_device_path_hard_drive_path);
723
724 return dpsize;
725}
726
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100727/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100728 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100729 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200730 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100731 * @desc block device descriptor
732 * @part partition number, 0 identifies a block device
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200733 *
734 * Return: pointer to position after the node
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100735 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100736static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400737{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600738 struct disk_partition info;
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200739 int ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400740
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200741 ret = part_get_info(desc, part, &info);
742 if (ret < 0)
743 return buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400744
745 if (desc->part_type == PART_TYPE_ISO) {
746 struct efi_device_path_cdrom_path *cddp = buf;
747
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100748 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400749 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
750 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
751 cddp->dp.length = sizeof(*cddp);
752 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200753 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400754
755 buf = &cddp[1];
756 } else {
757 struct efi_device_path_hard_drive_path *hddp = buf;
758
759 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
760 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
761 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100762 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400763 hddp->partition_start = info.start;
764 hddp->partition_end = info.size;
765 if (desc->part_type == PART_TYPE_EFI)
766 hddp->partmap_type = 2;
767 else
768 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100769
770 switch (desc->sig_type) {
771 case SIG_TYPE_NONE:
772 default:
773 hddp->signature_type = 0;
774 memset(hddp->partition_signature, 0,
775 sizeof(hddp->partition_signature));
776 break;
777 case SIG_TYPE_MBR:
778 hddp->signature_type = 1;
779 memset(hddp->partition_signature, 0,
780 sizeof(hddp->partition_signature));
781 memcpy(hddp->partition_signature, &desc->mbr_sig,
782 sizeof(desc->mbr_sig));
783 break;
784 case SIG_TYPE_GUID:
785 hddp->signature_type = 2;
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900786#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
787 /* info.uuid exists only with PARTITION_UUIDS */
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200788 if (uuid_str_to_bin(info.uuid,
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900789 hddp->partition_signature,
790 UUID_STR_FORMAT_GUID)) {
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200791 log_warning(
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900792 "Partition %d: invalid GUID %s\n",
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200793 part, info.uuid);
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900794 }
795#endif
Jonathan Gray84b4d702017-11-22 14:18:59 +1100796 break;
797 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400798
799 buf = &hddp[1];
800 }
801
802 return buf;
803}
804
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100805/*
806 * Create a device path for a block device or one of its partitions.
807 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200808 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100809 * @desc block device descriptor
810 * @part partition number, 0 identifies a block device
811 */
812static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
813{
Simon Glassec209a72022-01-29 14:58:39 -0700814 struct udevice *dev = desc->bdev;
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100815
Simon Glass222f3cb2021-09-24 18:30:17 -0600816 buf = dp_fill(buf, dev);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100817
818 if (part == 0) /* the actual disk, not a partition */
819 return buf;
820
821 return dp_part_node(buf, desc, part);
822}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400823
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200824/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400825struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
826{
827 void *buf, *start;
828
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100829 start = buf = efi_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100830 if (!buf)
831 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400832
833 buf = dp_part_fill(buf, desc, part);
834
835 *((struct efi_device_path *)buf) = END;
836
837 return start;
838}
839
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100840/*
841 * Create a device node for a block device partition.
842 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200843 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100844 * @desc block device descriptor
845 * @part partition number, 0 identifies a block device
846 */
847struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
848{
849 efi_uintn_t dpsize;
850 void *buf;
851
852 if (desc->part_type == PART_TYPE_ISO)
853 dpsize = sizeof(struct efi_device_path_cdrom_path);
854 else
855 dpsize = sizeof(struct efi_device_path_hard_drive_path);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100856 buf = efi_alloc(dpsize);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100857
Heinrich Schuchardte29fbb32022-10-06 13:36:02 +0200858 if (buf)
859 dp_part_node(buf, desc, part);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100860
861 return buf;
862}
863
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200864/**
865 * path_to_uefi() - convert UTF-8 path to an UEFI style path
866 *
867 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
868 * separators and UTF-16).
869 *
870 * @src: source buffer
871 * @uefi: target buffer, possibly unaligned
872 */
873static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400874{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200875 u16 *pos = uefi;
876
877 /*
878 * efi_set_bootdev() calls this routine indirectly before the UEFI
879 * subsystem is initialized. So we cannot assume unaligned access to be
880 * enabled.
881 */
882 allow_unaligned();
883
884 while (*src) {
885 s32 code = utf8_get(&src);
886
887 if (code < 0)
888 code = '?';
889 else if (code == '/')
890 code = '\\';
891 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400892 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200893 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400894}
895
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000896/**
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200897 * efi_dp_from_file() - append file path node to device path.
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000898 *
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200899 * @dp: device path or NULL
900 * @path: file path or NULL
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000901 * Return: device path or NULL in case of an error
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400902 */
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200903struct efi_device_path *efi_dp_from_file(const struct efi_device_path *dp,
904 const char *path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400905{
906 struct efi_device_path_file_path *fp;
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200907 void *buf, *pos;
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200908 size_t dpsize, fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400909
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200910 dpsize = efi_dp_size(dp);
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200911 fpsize = sizeof(struct efi_device_path) +
912 2 * (utf8_utf16_strlen(path) + 1);
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900913 if (fpsize > U16_MAX)
914 return NULL;
915
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200916 buf = efi_alloc(dpsize + fpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100917 if (!buf)
918 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400919
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200920 memcpy(buf, dp, dpsize);
921 pos = buf + dpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400922
923 /* add file-path: */
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000924 if (*path) {
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200925 fp = pos;
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000926 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
927 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
928 fp->dp.length = (u16)fpsize;
929 path_to_uefi(fp->str, path);
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200930 pos += fpsize;
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000931 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400932
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200933 memcpy(pos, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400934
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200935 return buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400936}
937
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100938struct efi_device_path *efi_dp_from_uart(void)
939{
940 void *buf, *pos;
941 struct efi_device_path_uart *uart;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200942 size_t dpsize = dp_size(dm_root()) + sizeof(*uart) + sizeof(END);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100943
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100944 buf = efi_alloc(dpsize);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100945 if (!buf)
946 return NULL;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200947 pos = dp_fill(buf, dm_root());
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100948 uart = pos;
949 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
950 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
951 uart->dp.length = sizeof(*uart);
952 pos += sizeof(*uart);
953 memcpy(pos, &END, sizeof(END));
954
955 return buf;
956}
957
Adriano Cordova1d370312025-03-03 11:13:14 -0300958struct efi_device_path __maybe_unused *efi_dp_from_eth(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400959{
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400960 void *buf, *start;
961 unsigned dpsize = 0;
962
Adriano Cordova1d370312025-03-03 11:13:14 -0300963 assert(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400964
Adriano Cordova1d370312025-03-03 11:13:14 -0300965 dpsize += dp_size(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400966
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100967 start = buf = efi_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100968 if (!buf)
969 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400970
Adriano Cordova1d370312025-03-03 11:13:14 -0300971 buf = dp_fill(buf, dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400972
973 *((struct efi_device_path *)buf) = END;
974
975 return start;
976}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400977
Adriano Cordovae9be2da2024-12-04 00:05:18 -0300978/**
979 * efi_dp_from_ipv4() - set device path from IPv4 address
980 *
981 * Set the device path to an ethernet device path as provided by
982 * efi_dp_from_eth() concatenated with a device path of subtype
983 * DEVICE_PATH_SUB_TYPE_MSG_IPV4, and an END node.
984 *
985 * @ip: IPv4 local address
986 * @mask: network mask
987 * @srv: IPv4 remote/server address
Adriano Cordova1d370312025-03-03 11:13:14 -0300988 * @dev: net udevice
Adriano Cordovae9be2da2024-12-04 00:05:18 -0300989 * Return: pointer to device path, NULL on error
990 */
991static struct efi_device_path *efi_dp_from_ipv4(struct efi_ipv4_address *ip,
992 struct efi_ipv4_address *mask,
Adriano Cordova1d370312025-03-03 11:13:14 -0300993 struct efi_ipv4_address *srv,
994 struct udevice *dev)
Adriano Cordovae9be2da2024-12-04 00:05:18 -0300995{
996 struct efi_device_path *dp1, *dp2, *pos;
997 struct {
998 struct efi_device_path_ipv4 ipv4dp;
999 struct efi_device_path end;
1000 } dp;
1001
1002 memset(&dp.ipv4dp, 0, sizeof(dp.ipv4dp));
1003 dp.ipv4dp.dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1004 dp.ipv4dp.dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_IPV4;
1005 dp.ipv4dp.dp.length = sizeof(dp.ipv4dp);
1006 dp.ipv4dp.protocol = 6;
1007 if (ip)
1008 memcpy(&dp.ipv4dp.local_ip_address, ip, sizeof(*ip));
1009 if (mask)
1010 memcpy(&dp.ipv4dp.subnet_mask, mask, sizeof(*mask));
1011 if (srv)
1012 memcpy(&dp.ipv4dp.remote_ip_address, srv, sizeof(*srv));
1013 pos = &dp.end;
1014 memcpy(pos, &END, sizeof(END));
1015
Adriano Cordova1d370312025-03-03 11:13:14 -03001016 dp1 = efi_dp_from_eth(dev);
Adriano Cordovae9be2da2024-12-04 00:05:18 -03001017 if (!dp1)
1018 return NULL;
1019
1020 dp2 = efi_dp_concat(dp1, (const struct efi_device_path *)&dp, 0);
1021
1022 efi_free_pool(dp1);
1023
1024 return dp2;
1025}
1026
Adriano Cordova4bd5eca2024-12-04 00:05:22 -03001027/**
1028 * efi_dp_from_http() - set device path from http
1029 *
1030 * Set the device path to an IPv4 path as provided by efi_dp_from_ipv4
1031 * concatenated with a device path of subtype DEVICE_PATH_SUB_TYPE_MSG_URI,
1032 * and an END node.
1033 *
1034 * @server: URI of remote server
Adriano Cordova1d370312025-03-03 11:13:14 -03001035 * @dev: net udevice
Adriano Cordova4bd5eca2024-12-04 00:05:22 -03001036 * Return: pointer to HTTP device path, NULL on error
1037 */
Adriano Cordova1d370312025-03-03 11:13:14 -03001038struct efi_device_path *efi_dp_from_http(const char *server, struct udevice *dev)
Adriano Cordova4bd5eca2024-12-04 00:05:22 -03001039{
1040 struct efi_device_path *dp1, *dp2;
1041 struct efi_device_path_uri *uridp;
1042 efi_uintn_t uridp_len;
1043 char *pos;
1044 char tmp[128];
1045 struct efi_ipv4_address ip;
1046 struct efi_ipv4_address mask;
1047
1048 if ((server && strlen("http://") + strlen(server) + 1 > sizeof(tmp)) ||
1049 (!server && IS_ENABLED(CONFIG_NET_LWIP)))
1050 return NULL;
1051
Adriano Cordova54674692025-03-03 11:13:15 -03001052 efi_net_get_addr(&ip, &mask, NULL, dev);
Adriano Cordova4bd5eca2024-12-04 00:05:22 -03001053
Adriano Cordova1d370312025-03-03 11:13:14 -03001054 dp1 = efi_dp_from_ipv4(&ip, &mask, NULL, dev);
Adriano Cordova4bd5eca2024-12-04 00:05:22 -03001055 if (!dp1)
1056 return NULL;
1057
Adriano Cordova1d370312025-03-03 11:13:14 -03001058
Adriano Cordova4bd5eca2024-12-04 00:05:22 -03001059 strcpy(tmp, "http://");
1060
1061 if (server) {
1062 strlcat(tmp, server, sizeof(tmp));
1063#if !IS_ENABLED(CONFIG_NET_LWIP)
Tom Rini19687542024-12-04 09:32:28 -06001064 } else {
Adriano Cordova4bd5eca2024-12-04 00:05:22 -03001065 ip_to_string(net_server_ip, tmp + strlen("http://"));
1066#endif
1067 }
1068
1069 uridp_len = sizeof(struct efi_device_path) + strlen(tmp) + 1;
1070 uridp = efi_alloc(uridp_len + sizeof(END));
1071 if (!uridp) {
1072 log_err("Out of memory\n");
1073 return NULL;
1074 }
1075 uridp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1076 uridp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_URI;
1077 uridp->dp.length = uridp_len;
1078 debug("device path: setting uri device path to %s\n", tmp);
1079 memcpy(uridp->uri, tmp, strlen(tmp) + 1);
1080
1081 pos = (char *)uridp + uridp_len;
1082 memcpy(pos, &END, sizeof(END));
1083
1084 dp2 = efi_dp_concat(dp1, (const struct efi_device_path *)uridp, 0);
1085
1086 efi_free_pool(uridp);
1087 efi_free_pool(dp1);
1088
1089 return dp2;
1090}
1091
Rob Clark18ceba72017-10-10 08:23:06 -04001092/* Construct a device-path for memory-mapped image */
1093struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1094 uint64_t start_address,
Moritz Fischerfe61a8b2024-11-04 01:49:34 +00001095 size_t size)
Rob Clark18ceba72017-10-10 08:23:06 -04001096{
1097 struct efi_device_path_memory *mdp;
1098 void *buf, *start;
1099
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001100 start = buf = efi_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001101 if (!buf)
1102 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -04001103
1104 mdp = buf;
1105 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1106 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1107 mdp->dp.length = sizeof(*mdp);
1108 mdp->memory_type = memory_type;
1109 mdp->start_address = start_address;
Moritz Fischerfe61a8b2024-11-04 01:49:34 +00001110 mdp->end_address = start_address + size;
Rob Clark18ceba72017-10-10 08:23:06 -04001111 buf = &mdp[1];
1112
1113 *((struct efi_device_path *)buf) = END;
1114
1115 return start;
1116}
1117
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001118/**
1119 * efi_dp_split_file_path() - split of relative file path from device path
1120 *
1121 * Given a device path indicating a file on a device, separate the device
1122 * path in two: the device path of the actual device and the file path
1123 * relative to this device.
1124 *
1125 * @full_path: device path including device and file path
1126 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001127 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001128 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001129 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001130efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1131 struct efi_device_path **device_path,
1132 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001133{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001134 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001135
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001136 *device_path = NULL;
1137 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001138 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001139 if (!dp)
1140 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001141 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001142 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001143 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001144 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001145 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001146 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001147 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001148 if (!fp)
1149 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001150 p->type = DEVICE_PATH_TYPE_END;
1151 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1152 p->length = sizeof(*p);
1153
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001154out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001155 *device_path = dp;
1156 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001157 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001158}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001159
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001160/**
1161 * efi_dp_from_name() - convert U-Boot device and file path to device path
1162 *
1163 * @dev: U-Boot device, e.g. 'mmc'
1164 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1165 * @path: file path relative to U-Boot device, may be NULL
1166 * @device: pointer to receive device path of the device
1167 * @file: pointer to receive device path for the file
1168 * Return: status code
1169 */
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001170efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1171 const char *path,
1172 struct efi_device_path **device,
1173 struct efi_device_path **file)
1174{
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001175 struct blk_desc *desc = NULL;
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001176 struct efi_device_path *dp;
Simon Glassc1c4a8f2020-05-10 11:39:57 -06001177 struct disk_partition fs_partition;
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001178 size_t image_size;
1179 void *image_addr;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001180 int part = 0;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001181
AKASHI Takahiro39844412018-11-05 18:06:40 +09001182 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001183 return EFI_INVALID_PARAMETER;
1184
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +09001185 if (IS_ENABLED(CONFIG_EFI_BINARY_EXEC) &&
1186 (!strcmp(dev, "Mem") || !strcmp(dev, "hostfs"))) {
Heinrich Schuchardtaecb9e22023-05-12 20:18:10 +02001187 /* loadm command and semihosting */
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001188 efi_get_image_parameters(&image_addr, &image_size);
1189
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001190 dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
Moritz Fischerfe61a8b2024-11-04 01:49:34 +00001191 (uintptr_t)image_addr, image_size);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001192 } else if (IS_ENABLED(CONFIG_NETDEVICES) &&
Adriano Cordova54674692025-03-03 11:13:15 -03001193 (!strcmp(dev, "Net") || !strcmp(dev, "Http"))) {
1194 efi_net_dp_from_dev(&dp, eth_get_dev(), false);
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001195 } else if (!strcmp(dev, "Uart")) {
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001196 dp = efi_dp_from_uart();
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001197 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001198 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1199 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001200 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001201 return EFI_INVALID_PARAMETER;
1202
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001203 dp = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001204 }
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001205 if (device)
1206 *device = dp;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001207
1208 if (!path)
1209 return EFI_SUCCESS;
1210
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +02001211 *file = efi_dp_from_file(dp, path);
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001212 if (!*file)
Heinrich Schuchardtbf0b3a12023-05-13 10:22:21 +02001213 return EFI_OUT_OF_RESOURCES;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001214
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001215 return EFI_SUCCESS;
1216}
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001217
1218/**
1219 * efi_dp_check_length() - check length of a device path
1220 *
1221 * @dp: pointer to device path
1222 * @maxlen: maximum length of the device path
1223 * Return:
1224 * * length of the device path if it is less or equal @maxlen
1225 * * -1 if the device path is longer then @maxlen
1226 * * -1 if a device path node has a length of less than 4
1227 * * -EINVAL if maxlen exceeds SSIZE_MAX
1228 */
1229ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1230 const size_t maxlen)
1231{
1232 ssize_t ret = 0;
1233 u16 len;
1234
1235 if (maxlen > SSIZE_MAX)
1236 return -EINVAL;
1237 for (;;) {
1238 len = dp->length;
1239 if (len < 4)
1240 return -1;
1241 ret += len;
1242 if (ret > maxlen)
1243 return -1;
1244 if (dp->type == DEVICE_PATH_TYPE_END &&
1245 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1246 return ret;
1247 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1248 }
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001249}
1250
1251/**
Heinrich Schuchardt2ca26802024-04-26 16:13:19 +02001252 * efi_dp_from_lo() - get device-path from load option
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001253 *
Heinrich Schuchardt2ca26802024-04-26 16:13:19 +02001254 * The load options in U-Boot may contain multiple concatenated device-paths.
1255 * The first device-path indicates the EFI binary to execute. Subsequent
1256 * device-paths start with a VenMedia node where the GUID identifies the
1257 * function (initrd or fdt).
1258 *
1259 * @lo: EFI load option containing a valid device path
1260 * @guid: GUID identifying device-path or NULL for the EFI binary
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001261 *
1262 * Return:
Heinrich Schuchardt2ca26802024-04-26 16:13:19 +02001263 * device path excluding the matched VenMedia node or NULL.
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001264 * Caller must free the returned value.
1265 */
1266struct
1267efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001268 const efi_guid_t *guid)
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001269{
1270 struct efi_device_path *fp = lo->file_path;
1271 struct efi_device_path_vendor *vendor;
1272 int lo_len = lo->file_path_length;
1273
Heinrich Schuchardt2ca26802024-04-26 16:13:19 +02001274 if (!guid)
1275 return efi_dp_dup(fp);
1276
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001277 for (; lo_len >= sizeof(struct efi_device_path);
1278 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1279 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1280 break;
1281 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1282 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1283 continue;
1284
1285 vendor = (struct efi_device_path_vendor *)fp;
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001286 if (!guidcmp(&vendor->guid, guid))
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +02001287 return efi_dp_dup(efi_dp_next(fp));
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001288 }
1289 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1290
1291 return NULL;
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001292}
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001293
1294/**
1295 * search_gpt_dp_node() - search gpt device path node
1296 *
1297 * @device_path: device path
1298 *
1299 * Return: pointer to the gpt device path node
1300 */
1301struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1302{
1303 struct efi_device_path *dp = device_path;
1304
1305 while (dp) {
1306 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1307 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1308 struct efi_device_path_hard_drive_path *hd_dp =
1309 (struct efi_device_path_hard_drive_path *)dp;
1310
1311 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1312 hd_dp->signature_type == SIG_TYPE_GUID)
1313 return dp;
1314 }
1315 dp = efi_dp_next(dp);
1316 }
1317
1318 return NULL;
1319}