blob: 46aa59b9e40b5b4648592e1449f5216898fff91d [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>
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>
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +020020#include <uuid.h>
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +020021#include <asm-generic/unaligned.h>
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +090022#include <linux/compat.h> /* U16_MAX */
Rob Clarkf90cb9f2017-09-13 18:05:28 -040023
24/* template END node: */
Masahisa Kojima97bd9da2022-06-19 13:55:59 +090025const struct efi_device_path END = {
Rob Clarkf90cb9f2017-09-13 18:05:28 -040026 .type = DEVICE_PATH_TYPE_END,
27 .sub_type = DEVICE_PATH_SUB_TYPE_END,
28 .length = sizeof(END),
29};
30
Simon Glass222f3cb2021-09-24 18:30:17 -060031#if defined(CONFIG_MMC)
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010032/*
33 * Determine if an MMC device is an SD card.
34 *
35 * @desc block device descriptor
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010036 * Return: true if the device is an SD card
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010037 */
38static bool is_sd(struct blk_desc *desc)
39{
40 struct mmc *mmc = find_mmc_device(desc->devnum);
41
42 if (!mmc)
43 return false;
44
45 return IS_SD(mmc) != 0U;
46}
47#endif
48
Rob Clarkf90cb9f2017-09-13 18:05:28 -040049/*
50 * Iterate to next block in device-path, terminating (returning NULL)
51 * at /End* node.
52 */
53struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
54{
55 if (dp == NULL)
56 return NULL;
57 if (dp->type == DEVICE_PATH_TYPE_END)
58 return NULL;
59 dp = ((void *)dp) + dp->length;
60 if (dp->type == DEVICE_PATH_TYPE_END)
61 return NULL;
62 return (struct efi_device_path *)dp;
63}
64
65/*
66 * Compare two device-paths, stopping when the shorter of the two hits
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +020067 * an End* node. This is useful to, for example, compare a device-path
Rob Clarkf90cb9f2017-09-13 18:05:28 -040068 * representing a device with one representing a file on the device, or
69 * a device with a parent device.
70 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +020071int efi_dp_match(const struct efi_device_path *a,
72 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -040073{
74 while (1) {
75 int ret;
76
77 ret = memcmp(&a->length, &b->length, sizeof(a->length));
78 if (ret)
79 return ret;
80
81 ret = memcmp(a, b, a->length);
82 if (ret)
83 return ret;
84
85 a = efi_dp_next(a);
86 b = efi_dp_next(b);
87
88 if (!a || !b)
89 return 0;
90 }
91}
92
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +010093/**
94 * efi_dp_shorten() - shorten device-path
95 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +020096 * When creating a short boot option we want to use a device-path that is
97 * independent of the location where the block device is plugged in.
Rob Clarkf90cb9f2017-09-13 18:05:28 -040098 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +020099 * UsbWwi() nodes contain a serial number, hard drive paths a partition
100 * UUID. Both should be unique.
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200101 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200102 * See UEFI spec, section 3.1.2 for "short-form device path".
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100103 *
Heinrich Schuchardtdb17dbc2022-03-21 08:26:48 +0100104 * @dp: original device-path
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100105 * @Return: shortened device-path or NULL
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400106 */
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100107struct efi_device_path *efi_dp_shorten(struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400108{
109 while (dp) {
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200110 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_WWI) ||
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400111 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
112 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
113 return dp;
114
115 dp = efi_dp_next(dp);
116 }
117
118 return dp;
119}
120
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100121/**
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100122 * find_handle() - find handle by device path and installed protocol
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100123 *
124 * If @rem is provided, the handle with the longest partial match is returned.
125 *
126 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100127 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100128 * @short_path: use short form device path for matching
129 * @rem: pointer to receive remaining device path
130 * Return: matching handle
131 */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100132static efi_handle_t find_handle(struct efi_device_path *dp,
133 const efi_guid_t *guid, bool short_path,
134 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400135{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100136 efi_handle_t handle, best_handle = NULL;
137 efi_uintn_t len, best_len = 0;
138
139 len = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400140
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100141 list_for_each_entry(handle, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100142 struct efi_handler *handler;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100143 struct efi_device_path *dp_current;
144 efi_uintn_t len_current;
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100145 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400146
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100147 if (guid) {
148 ret = efi_search_protocol(handle, guid, &handler);
149 if (ret != EFI_SUCCESS)
150 continue;
151 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100152 ret = efi_search_protocol(handle, &efi_guid_device_path,
153 &handler);
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100154 if (ret != EFI_SUCCESS)
155 continue;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100156 dp_current = handler->protocol_interface;
157 if (short_path) {
158 dp_current = efi_dp_shorten(dp_current);
159 if (!dp_current)
160 continue;
161 }
162 len_current = efi_dp_instance_size(dp_current);
163 if (rem) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100164 if (len_current > len)
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100165 continue;
166 } else {
167 if (len_current != len)
168 continue;
169 }
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100170 if (memcmp(dp_current, dp, len_current))
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100171 continue;
172 if (!rem)
173 return handle;
174 if (len_current > best_len) {
175 best_len = len_current;
176 best_handle = handle;
177 *rem = (void*)((u8 *)dp + len_current);
178 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400179 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100180 return best_handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400181}
182
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100183/**
184 * efi_dp_find_obj() - find handle by device path
185 *
186 * If @rem is provided, the handle with the longest partial match is returned.
187 *
188 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100189 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100190 * @rem: pointer to receive remaining device path
191 * Return: matching handle
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400192 */
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100193efi_handle_t efi_dp_find_obj(struct efi_device_path *dp,
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100194 const efi_guid_t *guid,
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100195 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400196{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100197 efi_handle_t handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400198
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100199 handle = find_handle(dp, guid, false, rem);
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100200 if (!handle)
201 /* Match short form device path */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100202 handle = find_handle(dp, guid, true, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400203
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100204 return handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400205}
206
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100207/*
208 * Determine the last device path node that is not the end node.
209 *
210 * @dp device path
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100211 * Return: last node before the end node if it exists
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100212 * otherwise NULL
213 */
214const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
215{
216 struct efi_device_path *ret;
217
218 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
219 return NULL;
220 while (dp) {
221 ret = (struct efi_device_path *)dp;
222 dp = efi_dp_next(dp);
223 }
224 return ret;
225}
226
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200227/* get size of the first device path instance excluding end node */
228efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400229{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200230 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400231
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200232 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
233 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400234 while (dp) {
235 sz += dp->length;
236 dp = efi_dp_next(dp);
237 }
238
239 return sz;
240}
241
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200242/* get size of multi-instance device path excluding end node */
243efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
244{
245 const struct efi_device_path *p = dp;
246
247 if (!p)
248 return 0;
249 while (p->type != DEVICE_PATH_TYPE_END ||
250 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
251 p = (void *)p + p->length;
252
253 return (void *)p - (void *)dp;
254}
255
256/* copy multi-instance device path */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400257struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
258{
259 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200260 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400261
262 if (!dp)
263 return NULL;
264
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100265 ndp = efi_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100266 if (!ndp)
267 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400268 memcpy(ndp, dp, sz);
269
270 return ndp;
271}
272
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200273/**
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200274 * efi_dp_concat() - Concatenate two device paths and add and terminate them
275 * with an end node.
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200276 *
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200277 * @dp1: First device path
278 * @dp2: Second device path
279 * @split_end_node: If true the two device paths will be concatenated and
280 * separated by an end node (DEVICE_PATH_SUB_TYPE_END).
281 * If false the second device path will be concatenated to the
282 * first one as-is.
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200283 *
284 * Return:
285 * concatenated device path or NULL. Caller must free the returned value
286 */
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200287struct
288efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
289 const struct efi_device_path *dp2,
290 bool split_end_node)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400291{
292 struct efi_device_path *ret;
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200293 size_t end_size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400294
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200295 if (!dp1 && !dp2) {
296 /* return an end node */
297 ret = efi_dp_dup(&END);
298 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400299 ret = efi_dp_dup(dp2);
300 } else if (!dp2) {
301 ret = efi_dp_dup(dp1);
302 } else {
303 /* both dp1 and dp2 are non-null */
304 unsigned sz1 = efi_dp_size(dp1);
305 unsigned sz2 = efi_dp_size(dp2);
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200306 void *p;
307
308 if (split_end_node)
309 end_size = 2 * sizeof(END);
310 else
311 end_size = sizeof(END);
312 p = efi_alloc(sz1 + sz2 + end_size);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100313 if (!p)
314 return NULL;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200315 ret = p;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400316 memcpy(p, dp1, sz1);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200317 p += sz1;
318
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200319 if (split_end_node) {
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200320 memcpy(p, &END, sizeof(END));
321 p += sizeof(END);
322 }
323
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200324 /* the end node of the second device path has to be retained */
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200325 memcpy(p, dp2, sz2);
326 p += sz2;
327 memcpy(p, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400328 }
329
330 return ret;
331}
332
333struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
334 const struct efi_device_path *node)
335{
336 struct efi_device_path *ret;
337
338 if (!node && !dp) {
339 ret = efi_dp_dup(&END);
340 } else if (!node) {
341 ret = efi_dp_dup(dp);
342 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200343 size_t sz = node->length;
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100344 void *p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100345 if (!p)
346 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400347 memcpy(p, node, sz);
348 memcpy(p + sz, &END, sizeof(END));
349 ret = p;
350 } else {
351 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200352 size_t sz = efi_dp_size(dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100353 void *p = efi_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100354 if (!p)
355 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400356 memcpy(p, dp, sz);
357 memcpy(p + sz, node, node->length);
358 memcpy(p + sz + node->length, &END, sizeof(END));
359 ret = p;
360 }
361
362 return ret;
363}
364
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200365struct efi_device_path *efi_dp_create_device_node(const u8 type,
366 const u8 sub_type,
367 const u16 length)
368{
369 struct efi_device_path *ret;
370
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200371 if (length < sizeof(struct efi_device_path))
372 return NULL;
373
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100374 ret = efi_alloc(length);
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200375 if (!ret)
376 return ret;
377 ret->type = type;
378 ret->sub_type = sub_type;
379 ret->length = length;
380 return ret;
381}
382
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200383struct efi_device_path *efi_dp_append_instance(
384 const struct efi_device_path *dp,
385 const struct efi_device_path *dpi)
386{
387 size_t sz, szi;
388 struct efi_device_path *p, *ret;
389
390 if (!dpi)
391 return NULL;
392 if (!dp)
393 return efi_dp_dup(dpi);
394 sz = efi_dp_size(dp);
395 szi = efi_dp_instance_size(dpi);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100396 p = efi_alloc(sz + szi + 2 * sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200397 if (!p)
398 return NULL;
399 ret = p;
400 memcpy(p, dp, sz + sizeof(END));
401 p = (void *)p + sz;
402 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
403 p = (void *)p + sizeof(END);
404 memcpy(p, dpi, szi);
405 p = (void *)p + szi;
406 memcpy(p, &END, sizeof(END));
407 return ret;
408}
409
410struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
411 efi_uintn_t *size)
412{
413 size_t sz;
414 struct efi_device_path *p;
415
416 if (size)
417 *size = 0;
418 if (!dp || !*dp)
419 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200420 sz = efi_dp_instance_size(*dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100421 p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200422 if (!p)
423 return NULL;
424 memcpy(p, *dp, sz + sizeof(END));
425 *dp = (void *)*dp + sz;
426 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
427 *dp = (void *)*dp + sizeof(END);
428 else
429 *dp = NULL;
430 if (size)
431 *size = sz + sizeof(END);
432 return p;
433}
434
435bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
436{
437 const struct efi_device_path *p = dp;
438
439 if (!p)
440 return false;
441 while (p->type != DEVICE_PATH_TYPE_END)
442 p = (void *)p + p->length;
443 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
444}
445
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400446/* size of device-path not including END node for device and all parents
447 * up to the root device.
448 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100449__maybe_unused static unsigned int dp_size(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400450{
451 if (!dev || !dev->driver)
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200452 return sizeof(struct efi_device_path_udevice);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400453
Simon Glass56ada7b2022-01-29 14:58:38 -0700454 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400455 case UCLASS_ROOT:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400456 /* stop traversing parents at this point: */
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200457 return sizeof(struct efi_device_path_udevice);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100458 case UCLASS_ETH:
459 return dp_size(dev->parent) +
460 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100461 case UCLASS_BLK:
462 switch (dev->parent->uclass->uc_drv->id) {
463#ifdef CONFIG_IDE
464 case UCLASS_IDE:
465 return dp_size(dev->parent) +
466 sizeof(struct efi_device_path_atapi);
467#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600468#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100469 case UCLASS_SCSI:
470 return dp_size(dev->parent) +
471 sizeof(struct efi_device_path_scsi);
472#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600473#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100474 case UCLASS_MMC:
475 return dp_size(dev->parent) +
476 sizeof(struct efi_device_path_sd_mmc_path);
477#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200478#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
479 case UCLASS_AHCI:
480 return dp_size(dev->parent) +
481 sizeof(struct efi_device_path_sata);
482#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200483#if defined(CONFIG_NVME)
484 case UCLASS_NVME:
485 return dp_size(dev->parent) +
486 sizeof(struct efi_device_path_nvme);
487#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100488#ifdef CONFIG_USB
489 case UCLASS_MASS_STORAGE:
490 return dp_size(dev->parent)
491 + sizeof(struct efi_device_path_controller);
492#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100493 default:
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200494 /* UCLASS_BLKMAP, UCLASS_HOST, UCLASS_VIRTIO */
495 return dp_size(dev->parent) +
496 sizeof(struct efi_device_path_udevice);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100497 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600498#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400499 case UCLASS_MMC:
500 return dp_size(dev->parent) +
501 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100502#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400503 case UCLASS_MASS_STORAGE:
504 case UCLASS_USB_HUB:
505 return dp_size(dev->parent) +
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100506 sizeof(struct efi_device_path_usb);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400507 default:
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200508 return dp_size(dev->parent) +
509 sizeof(struct efi_device_path_udevice);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400510 }
511}
512
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100513/*
514 * Recursively build a device path.
515 *
516 * @buf pointer to the end of the device path
517 * @dev device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100518 * Return: pointer to the end of the device path
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100519 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100520__maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400521{
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200522 enum uclass_id uclass_id;
523
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400524 if (!dev || !dev->driver)
525 return buf;
526
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200527 uclass_id = device_get_uclass_id(dev);
528 if (uclass_id != UCLASS_ROOT)
529 buf = dp_fill(buf, dev->parent);
530
531 switch (uclass_id) {
Jan Kiszkaf1389822022-10-14 18:10:06 +0200532#ifdef CONFIG_NETDEVICES
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100533 case UCLASS_ETH: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200534 struct efi_device_path_mac_addr *dp = buf;
Simon Glass95588622020-12-22 19:30:28 -0700535 struct eth_pdata *pdata = dev_get_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100536
537 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
538 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
539 dp->dp.length = sizeof(*dp);
540 memset(&dp->mac, 0, sizeof(dp->mac));
541 /* We only support IPv4 */
542 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
543 /* Ethernet */
544 dp->if_type = 1;
545 return &dp[1];
546 }
547#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100548 case UCLASS_BLK:
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200549 switch (device_get_uclass_id(dev->parent)) {
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100550#ifdef CONFIG_IDE
551 case UCLASS_IDE: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200552 struct efi_device_path_atapi *dp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700553 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100554
555 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
556 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
557 dp->dp.length = sizeof(*dp);
558 dp->logical_unit_number = desc->devnum;
559 dp->primary_secondary = IDE_BUS(desc->devnum);
560 dp->slave_master = desc->devnum %
561 (CONFIG_SYS_IDE_MAXDEVICE /
562 CONFIG_SYS_IDE_MAXBUS);
563 return &dp[1];
564 }
565#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600566#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100567 case UCLASS_SCSI: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200568 struct efi_device_path_scsi *dp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700569 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100570
571 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
572 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
573 dp->dp.length = sizeof(*dp);
574 dp->logical_unit_number = desc->lun;
575 dp->target_id = desc->target;
576 return &dp[1];
577 }
578#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600579#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100580 case UCLASS_MMC: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200581 struct efi_device_path_sd_mmc_path *sddp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700582 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100583
584 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
585 sddp->dp.sub_type = is_sd(desc) ?
586 DEVICE_PATH_SUB_TYPE_MSG_SD :
587 DEVICE_PATH_SUB_TYPE_MSG_MMC;
588 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700589 sddp->slot_number = dev_seq(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100590 return &sddp[1];
591 }
592#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200593#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
594 case UCLASS_AHCI: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200595 struct efi_device_path_sata *dp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700596 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200597
598 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
599 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
600 dp->dp.length = sizeof(*dp);
601 dp->hba_port = desc->devnum;
602 /* default 0xffff implies no port multiplier */
603 dp->port_multiplier_port = 0xffff;
604 dp->logical_unit_number = desc->lun;
605 return &dp[1];
606 }
607#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200608#if defined(CONFIG_NVME)
609 case UCLASS_NVME: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200610 struct efi_device_path_nvme *dp = buf;
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200611 u32 ns_id;
612
613 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
614 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
615 dp->dp.length = sizeof(*dp);
616 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
617 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
618 return &dp[1];
619 }
620#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100621#if defined(CONFIG_USB)
622 case UCLASS_MASS_STORAGE: {
Heinrich Schuchardt232c9db2023-04-01 07:21:55 +0200623 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200624 struct efi_device_path_controller *dp = buf;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100625
626 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
627 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CONTROLLER;
628 dp->dp.length = sizeof(*dp);
629 dp->controller_number = desc->lun;
630 return &dp[1];
631 }
632#endif
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200633 default: {
634 /* UCLASS_BLKMAP, UCLASS_HOST, UCLASS_VIRTIO */
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200635 struct efi_device_path_udevice *dp = buf;
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200636 struct blk_desc *desc = dev_get_uclass_plat(dev);
637
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200638 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
639 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
640 dp->dp.length = sizeof(*dp);
641 memcpy(&dp->guid, &efi_u_boot_guid,
642 sizeof(efi_guid_t));
643 dp->uclass_id = (UCLASS_BLK & 0xffff) |
644 (desc->uclass_id << 16);
645 dp->dev_number = desc->devnum;
646
647 return &dp[1];
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100648 }
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200649 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600650#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400651 case UCLASS_MMC: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200652 struct efi_device_path_sd_mmc_path *sddp = buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400653 struct mmc *mmc = mmc_get_mmc_dev(dev);
654 struct blk_desc *desc = mmc_get_blk_desc(mmc);
655
656 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100657 sddp->dp.sub_type = is_sd(desc) ?
658 DEVICE_PATH_SUB_TYPE_MSG_SD :
659 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400660 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700661 sddp->slot_number = dev_seq(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400662
663 return &sddp[1];
664 }
665#endif
666 case UCLASS_MASS_STORAGE:
667 case UCLASS_USB_HUB: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200668 struct efi_device_path_usb *udp = buf;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100669
670 switch (device_get_uclass_id(dev->parent)) {
671 case UCLASS_USB_HUB: {
672 struct usb_device *udev = dev_get_parent_priv(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400673
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100674 udp->parent_port_number = udev->portnr;
675 break;
676 }
677 default:
678 udp->parent_port_number = 0;
679 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400680 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100681 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400682 udp->dp.length = sizeof(*udp);
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100683 udp->usb_interface = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400684
685 return &udp[1];
686 }
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200687 default: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200688 struct efi_device_path_udevice *vdp = buf;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200689
690 vdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
691 vdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
692 vdp->dp.length = sizeof(*vdp);
693 memcpy(&vdp->guid, &efi_u_boot_guid, sizeof(efi_guid_t));
694 vdp->uclass_id = uclass_id;
695 vdp->dev_number = dev->seq_;
696
697 return &vdp[1];
698 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400699 }
700}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400701
702static unsigned dp_part_size(struct blk_desc *desc, int part)
703{
704 unsigned dpsize;
Simon Glassec209a72022-01-29 14:58:39 -0700705 struct udevice *dev = desc->bdev;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400706
Simon Glass222f3cb2021-09-24 18:30:17 -0600707 dpsize = dp_size(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400708
709 if (part == 0) /* the actual disk, not a partition */
710 return dpsize;
711
712 if (desc->part_type == PART_TYPE_ISO)
713 dpsize += sizeof(struct efi_device_path_cdrom_path);
714 else
715 dpsize += sizeof(struct efi_device_path_hard_drive_path);
716
717 return dpsize;
718}
719
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100720/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100721 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100722 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200723 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100724 * @desc block device descriptor
725 * @part partition number, 0 identifies a block device
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200726 *
727 * Return: pointer to position after the node
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100728 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100729static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400730{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600731 struct disk_partition info;
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200732 int ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400733
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200734 ret = part_get_info(desc, part, &info);
735 if (ret < 0)
736 return buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400737
738 if (desc->part_type == PART_TYPE_ISO) {
739 struct efi_device_path_cdrom_path *cddp = buf;
740
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100741 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400742 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
743 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
744 cddp->dp.length = sizeof(*cddp);
745 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200746 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400747
748 buf = &cddp[1];
749 } else {
750 struct efi_device_path_hard_drive_path *hddp = buf;
751
752 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
753 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
754 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100755 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400756 hddp->partition_start = info.start;
757 hddp->partition_end = info.size;
758 if (desc->part_type == PART_TYPE_EFI)
759 hddp->partmap_type = 2;
760 else
761 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100762
763 switch (desc->sig_type) {
764 case SIG_TYPE_NONE:
765 default:
766 hddp->signature_type = 0;
767 memset(hddp->partition_signature, 0,
768 sizeof(hddp->partition_signature));
769 break;
770 case SIG_TYPE_MBR:
771 hddp->signature_type = 1;
772 memset(hddp->partition_signature, 0,
773 sizeof(hddp->partition_signature));
774 memcpy(hddp->partition_signature, &desc->mbr_sig,
775 sizeof(desc->mbr_sig));
776 break;
777 case SIG_TYPE_GUID:
778 hddp->signature_type = 2;
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900779#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
780 /* info.uuid exists only with PARTITION_UUIDS */
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200781 if (uuid_str_to_bin(info.uuid,
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900782 hddp->partition_signature,
783 UUID_STR_FORMAT_GUID)) {
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200784 log_warning(
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900785 "Partition %d: invalid GUID %s\n",
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200786 part, info.uuid);
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900787 }
788#endif
Jonathan Gray84b4d702017-11-22 14:18:59 +1100789 break;
790 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400791
792 buf = &hddp[1];
793 }
794
795 return buf;
796}
797
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100798/*
799 * Create a device path for a block device or one of its partitions.
800 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200801 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100802 * @desc block device descriptor
803 * @part partition number, 0 identifies a block device
804 */
805static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
806{
Simon Glassec209a72022-01-29 14:58:39 -0700807 struct udevice *dev = desc->bdev;
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100808
Simon Glass222f3cb2021-09-24 18:30:17 -0600809 buf = dp_fill(buf, dev);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100810
811 if (part == 0) /* the actual disk, not a partition */
812 return buf;
813
814 return dp_part_node(buf, desc, part);
815}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400816
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200817/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400818struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
819{
820 void *buf, *start;
821
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100822 start = buf = efi_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100823 if (!buf)
824 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400825
826 buf = dp_part_fill(buf, desc, part);
827
828 *((struct efi_device_path *)buf) = END;
829
830 return start;
831}
832
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100833/*
834 * Create a device node for a block device partition.
835 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200836 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100837 * @desc block device descriptor
838 * @part partition number, 0 identifies a block device
839 */
840struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
841{
842 efi_uintn_t dpsize;
843 void *buf;
844
845 if (desc->part_type == PART_TYPE_ISO)
846 dpsize = sizeof(struct efi_device_path_cdrom_path);
847 else
848 dpsize = sizeof(struct efi_device_path_hard_drive_path);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100849 buf = efi_alloc(dpsize);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100850
Heinrich Schuchardte29fbb32022-10-06 13:36:02 +0200851 if (buf)
852 dp_part_node(buf, desc, part);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100853
854 return buf;
855}
856
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200857/**
858 * path_to_uefi() - convert UTF-8 path to an UEFI style path
859 *
860 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
861 * separators and UTF-16).
862 *
863 * @src: source buffer
864 * @uefi: target buffer, possibly unaligned
865 */
866static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400867{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200868 u16 *pos = uefi;
869
870 /*
871 * efi_set_bootdev() calls this routine indirectly before the UEFI
872 * subsystem is initialized. So we cannot assume unaligned access to be
873 * enabled.
874 */
875 allow_unaligned();
876
877 while (*src) {
878 s32 code = utf8_get(&src);
879
880 if (code < 0)
881 code = '?';
882 else if (code == '/')
883 code = '\\';
884 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400885 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200886 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400887}
888
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000889/**
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200890 * efi_dp_from_file() - append file path node to device path.
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000891 *
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200892 * @dp: device path or NULL
893 * @path: file path or NULL
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000894 * Return: device path or NULL in case of an error
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400895 */
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200896struct efi_device_path *efi_dp_from_file(const struct efi_device_path *dp,
897 const char *path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400898{
899 struct efi_device_path_file_path *fp;
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200900 void *buf, *pos;
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200901 size_t dpsize, fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400902
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200903 dpsize = efi_dp_size(dp);
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200904 fpsize = sizeof(struct efi_device_path) +
905 2 * (utf8_utf16_strlen(path) + 1);
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900906 if (fpsize > U16_MAX)
907 return NULL;
908
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200909 buf = efi_alloc(dpsize + fpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100910 if (!buf)
911 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400912
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200913 memcpy(buf, dp, dpsize);
914 pos = buf + dpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400915
916 /* add file-path: */
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000917 if (*path) {
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200918 fp = pos;
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000919 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
920 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
921 fp->dp.length = (u16)fpsize;
922 path_to_uefi(fp->str, path);
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200923 pos += fpsize;
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000924 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400925
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200926 memcpy(pos, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400927
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200928 return buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400929}
930
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100931struct efi_device_path *efi_dp_from_uart(void)
932{
933 void *buf, *pos;
934 struct efi_device_path_uart *uart;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200935 size_t dpsize = dp_size(dm_root()) + sizeof(*uart) + sizeof(END);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100936
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100937 buf = efi_alloc(dpsize);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100938 if (!buf)
939 return NULL;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200940 pos = dp_fill(buf, dm_root());
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100941 uart = pos;
942 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
943 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
944 uart->dp.length = sizeof(*uart);
945 pos += sizeof(*uart);
946 memcpy(pos, &END, sizeof(END));
947
948 return buf;
949}
950
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +0200951struct efi_device_path __maybe_unused *efi_dp_from_eth(void)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400952{
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400953 void *buf, *start;
954 unsigned dpsize = 0;
955
956 assert(eth_get_dev());
957
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400958 dpsize += dp_size(eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400959
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100960 start = buf = efi_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100961 if (!buf)
962 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400963
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400964 buf = dp_fill(buf, eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400965
966 *((struct efi_device_path *)buf) = END;
967
968 return start;
969}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400970
Rob Clark18ceba72017-10-10 08:23:06 -0400971/* Construct a device-path for memory-mapped image */
972struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
973 uint64_t start_address,
974 uint64_t end_address)
975{
976 struct efi_device_path_memory *mdp;
977 void *buf, *start;
978
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100979 start = buf = efi_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100980 if (!buf)
981 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -0400982
983 mdp = buf;
984 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
985 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
986 mdp->dp.length = sizeof(*mdp);
987 mdp->memory_type = memory_type;
988 mdp->start_address = start_address;
989 mdp->end_address = end_address;
990 buf = &mdp[1];
991
992 *((struct efi_device_path *)buf) = END;
993
994 return start;
995}
996
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +0100997/**
998 * efi_dp_split_file_path() - split of relative file path from device path
999 *
1000 * Given a device path indicating a file on a device, separate the device
1001 * path in two: the device path of the actual device and the file path
1002 * relative to this device.
1003 *
1004 * @full_path: device path including device and file path
1005 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001006 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001007 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001008 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001009efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1010 struct efi_device_path **device_path,
1011 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001012{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001013 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001014
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001015 *device_path = NULL;
1016 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001017 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001018 if (!dp)
1019 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001020 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001021 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001022 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001023 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001024 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001025 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001026 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001027 if (!fp)
1028 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001029 p->type = DEVICE_PATH_TYPE_END;
1030 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1031 p->length = sizeof(*p);
1032
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001033out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001034 *device_path = dp;
1035 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001036 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001037}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001038
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001039/**
1040 * efi_dp_from_name() - convert U-Boot device and file path to device path
1041 *
1042 * @dev: U-Boot device, e.g. 'mmc'
1043 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1044 * @path: file path relative to U-Boot device, may be NULL
1045 * @device: pointer to receive device path of the device
1046 * @file: pointer to receive device path for the file
1047 * Return: status code
1048 */
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001049efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1050 const char *path,
1051 struct efi_device_path **device,
1052 struct efi_device_path **file)
1053{
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001054 struct blk_desc *desc = NULL;
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001055 struct efi_device_path *dp;
Simon Glassc1c4a8f2020-05-10 11:39:57 -06001056 struct disk_partition fs_partition;
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001057 size_t image_size;
1058 void *image_addr;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001059 int part = 0;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001060
AKASHI Takahiro39844412018-11-05 18:06:40 +09001061 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001062 return EFI_INVALID_PARAMETER;
1063
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +09001064 if (IS_ENABLED(CONFIG_EFI_BINARY_EXEC) &&
1065 (!strcmp(dev, "Mem") || !strcmp(dev, "hostfs"))) {
Heinrich Schuchardtaecb9e22023-05-12 20:18:10 +02001066 /* loadm command and semihosting */
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001067 efi_get_image_parameters(&image_addr, &image_size);
1068
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001069 dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
1070 (uintptr_t)image_addr, image_size);
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001071 } else if (IS_ENABLED(CONFIG_NETDEVICES) && !strcmp(dev, "Net")) {
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001072 dp = efi_dp_from_eth();
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001073 } else if (!strcmp(dev, "Uart")) {
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001074 dp = efi_dp_from_uart();
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001075 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001076 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1077 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001078 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001079 return EFI_INVALID_PARAMETER;
1080
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001081 dp = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001082 }
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001083 if (device)
1084 *device = dp;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001085
1086 if (!path)
1087 return EFI_SUCCESS;
1088
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +02001089 *file = efi_dp_from_file(dp, path);
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001090 if (!*file)
Heinrich Schuchardtbf0b3a12023-05-13 10:22:21 +02001091 return EFI_OUT_OF_RESOURCES;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001092
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001093 return EFI_SUCCESS;
1094}
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001095
1096/**
1097 * efi_dp_check_length() - check length of a device path
1098 *
1099 * @dp: pointer to device path
1100 * @maxlen: maximum length of the device path
1101 * Return:
1102 * * length of the device path if it is less or equal @maxlen
1103 * * -1 if the device path is longer then @maxlen
1104 * * -1 if a device path node has a length of less than 4
1105 * * -EINVAL if maxlen exceeds SSIZE_MAX
1106 */
1107ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1108 const size_t maxlen)
1109{
1110 ssize_t ret = 0;
1111 u16 len;
1112
1113 if (maxlen > SSIZE_MAX)
1114 return -EINVAL;
1115 for (;;) {
1116 len = dp->length;
1117 if (len < 4)
1118 return -1;
1119 ret += len;
1120 if (ret > maxlen)
1121 return -1;
1122 if (dp->type == DEVICE_PATH_TYPE_END &&
1123 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1124 return ret;
1125 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1126 }
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001127}
1128
1129/**
1130 * efi_dp_from_lo() - Get the instance of a VenMedia node in a
1131 * multi-instance device path that matches
1132 * a specific GUID. This kind of device paths
1133 * is found in Boot#### options describing an
1134 * initrd location
1135 *
1136 * @lo: EFI_LOAD_OPTION containing a valid device path
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001137 * @guid: guid to search for
1138 *
1139 * Return:
1140 * device path including the VenMedia node or NULL.
1141 * Caller must free the returned value.
1142 */
1143struct
1144efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001145 const efi_guid_t *guid)
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001146{
1147 struct efi_device_path *fp = lo->file_path;
1148 struct efi_device_path_vendor *vendor;
1149 int lo_len = lo->file_path_length;
1150
1151 for (; lo_len >= sizeof(struct efi_device_path);
1152 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1153 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1154 break;
1155 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1156 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1157 continue;
1158
1159 vendor = (struct efi_device_path_vendor *)fp;
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001160 if (!guidcmp(&vendor->guid, guid))
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +02001161 return efi_dp_dup(efi_dp_next(fp));
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001162 }
1163 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1164
1165 return NULL;
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001166}
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001167
1168/**
1169 * search_gpt_dp_node() - search gpt device path node
1170 *
1171 * @device_path: device path
1172 *
1173 * Return: pointer to the gpt device path node
1174 */
1175struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1176{
1177 struct efi_device_path *dp = device_path;
1178
1179 while (dp) {
1180 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1181 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1182 struct efi_device_path_hard_drive_path *hd_dp =
1183 (struct efi_device_path_hard_drive_path *)dp;
1184
1185 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1186 hd_dp->signature_type == SIG_TYPE_GUID)
1187 return dp;
1188 }
1189 dp = efi_dp_next(dp);
1190 }
1191
1192 return NULL;
1193}