blob: 8dbd8105ae26dd3dec5e7b5bffc6cd8ac61f326d [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/**
274 * efi_dp_append_or_concatenate() - Append or concatenate two device paths.
275 * Concatenated device path will be separated
276 * by a sub-type 0xff end node
277 *
278 * @dp1: First device path
279 * @dp2: Second device path
280 * @concat: If true the two device paths will be concatenated and separated
281 * by an end of entrire device path sub-type 0xff end node.
282 * If true the second device path will be appended to the first and
283 * terminated by an end node
284 *
285 * Return:
286 * concatenated device path or NULL. Caller must free the returned value
287 */
288static struct
289efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
290 const struct efi_device_path *dp2,
291 bool concat)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400292{
293 struct efi_device_path *ret;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200294 size_t end_size = sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400295
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200296 if (concat)
297 end_size = 2 * sizeof(END);
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200298 if (!dp1 && !dp2) {
299 /* return an end node */
300 ret = efi_dp_dup(&END);
301 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400302 ret = efi_dp_dup(dp2);
303 } else if (!dp2) {
304 ret = efi_dp_dup(dp1);
305 } else {
306 /* both dp1 and dp2 are non-null */
307 unsigned sz1 = efi_dp_size(dp1);
308 unsigned sz2 = efi_dp_size(dp2);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100309 void *p = efi_alloc(sz1 + sz2 + end_size);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100310 if (!p)
311 return NULL;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200312 ret = p;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400313 memcpy(p, dp1, sz1);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200314 p += sz1;
315
316 if (concat) {
317 memcpy(p, &END, sizeof(END));
318 p += sizeof(END);
319 }
320
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200321 /* the end node of the second device path has to be retained */
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200322 memcpy(p, dp2, sz2);
323 p += sz2;
324 memcpy(p, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400325 }
326
327 return ret;
328}
329
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200330/**
331 * efi_dp_append() - Append a device to an existing device path.
332 *
333 * @dp1: First device path
334 * @dp2: Second device path
335 *
336 * Return:
337 * concatenated device path or NULL. Caller must free the returned value
338 */
339struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
340 const struct efi_device_path *dp2)
341{
342 return efi_dp_append_or_concatenate(dp1, dp2, false);
343}
344
345/**
346 * efi_dp_concat() - Concatenate 2 device paths. The final device path will
347 * contain two device paths separated by and end node (0xff).
348 *
349 * @dp1: First device path
350 * @dp2: Second device path
351 *
352 * Return:
353 * concatenated device path or NULL. Caller must free the returned value
354 */
355struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
356 const struct efi_device_path *dp2)
357{
358 return efi_dp_append_or_concatenate(dp1, dp2, true);
359}
360
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400361struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
362 const struct efi_device_path *node)
363{
364 struct efi_device_path *ret;
365
366 if (!node && !dp) {
367 ret = efi_dp_dup(&END);
368 } else if (!node) {
369 ret = efi_dp_dup(dp);
370 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200371 size_t sz = node->length;
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100372 void *p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100373 if (!p)
374 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400375 memcpy(p, node, sz);
376 memcpy(p + sz, &END, sizeof(END));
377 ret = p;
378 } else {
379 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200380 size_t sz = efi_dp_size(dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100381 void *p = efi_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100382 if (!p)
383 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400384 memcpy(p, dp, sz);
385 memcpy(p + sz, node, node->length);
386 memcpy(p + sz + node->length, &END, sizeof(END));
387 ret = p;
388 }
389
390 return ret;
391}
392
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200393struct efi_device_path *efi_dp_create_device_node(const u8 type,
394 const u8 sub_type,
395 const u16 length)
396{
397 struct efi_device_path *ret;
398
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200399 if (length < sizeof(struct efi_device_path))
400 return NULL;
401
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100402 ret = efi_alloc(length);
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200403 if (!ret)
404 return ret;
405 ret->type = type;
406 ret->sub_type = sub_type;
407 ret->length = length;
408 return ret;
409}
410
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200411struct efi_device_path *efi_dp_append_instance(
412 const struct efi_device_path *dp,
413 const struct efi_device_path *dpi)
414{
415 size_t sz, szi;
416 struct efi_device_path *p, *ret;
417
418 if (!dpi)
419 return NULL;
420 if (!dp)
421 return efi_dp_dup(dpi);
422 sz = efi_dp_size(dp);
423 szi = efi_dp_instance_size(dpi);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100424 p = efi_alloc(sz + szi + 2 * sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200425 if (!p)
426 return NULL;
427 ret = p;
428 memcpy(p, dp, sz + sizeof(END));
429 p = (void *)p + sz;
430 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
431 p = (void *)p + sizeof(END);
432 memcpy(p, dpi, szi);
433 p = (void *)p + szi;
434 memcpy(p, &END, sizeof(END));
435 return ret;
436}
437
438struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
439 efi_uintn_t *size)
440{
441 size_t sz;
442 struct efi_device_path *p;
443
444 if (size)
445 *size = 0;
446 if (!dp || !*dp)
447 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200448 sz = efi_dp_instance_size(*dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100449 p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200450 if (!p)
451 return NULL;
452 memcpy(p, *dp, sz + sizeof(END));
453 *dp = (void *)*dp + sz;
454 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
455 *dp = (void *)*dp + sizeof(END);
456 else
457 *dp = NULL;
458 if (size)
459 *size = sz + sizeof(END);
460 return p;
461}
462
463bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
464{
465 const struct efi_device_path *p = dp;
466
467 if (!p)
468 return false;
469 while (p->type != DEVICE_PATH_TYPE_END)
470 p = (void *)p + p->length;
471 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
472}
473
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400474/* size of device-path not including END node for device and all parents
475 * up to the root device.
476 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100477__maybe_unused static unsigned int dp_size(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400478{
479 if (!dev || !dev->driver)
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200480 return sizeof(struct efi_device_path_udevice);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400481
Simon Glass56ada7b2022-01-29 14:58:38 -0700482 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400483 case UCLASS_ROOT:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400484 /* stop traversing parents at this point: */
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200485 return sizeof(struct efi_device_path_udevice);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100486 case UCLASS_ETH:
487 return dp_size(dev->parent) +
488 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100489 case UCLASS_BLK:
490 switch (dev->parent->uclass->uc_drv->id) {
491#ifdef CONFIG_IDE
492 case UCLASS_IDE:
493 return dp_size(dev->parent) +
494 sizeof(struct efi_device_path_atapi);
495#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600496#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100497 case UCLASS_SCSI:
498 return dp_size(dev->parent) +
499 sizeof(struct efi_device_path_scsi);
500#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600501#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100502 case UCLASS_MMC:
503 return dp_size(dev->parent) +
504 sizeof(struct efi_device_path_sd_mmc_path);
505#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200506#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
507 case UCLASS_AHCI:
508 return dp_size(dev->parent) +
509 sizeof(struct efi_device_path_sata);
510#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200511#if defined(CONFIG_NVME)
512 case UCLASS_NVME:
513 return dp_size(dev->parent) +
514 sizeof(struct efi_device_path_nvme);
515#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100516#ifdef CONFIG_USB
517 case UCLASS_MASS_STORAGE:
518 return dp_size(dev->parent)
519 + sizeof(struct efi_device_path_controller);
520#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100521 default:
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200522 /* UCLASS_BLKMAP, UCLASS_HOST, UCLASS_VIRTIO */
523 return dp_size(dev->parent) +
524 sizeof(struct efi_device_path_udevice);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100525 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600526#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400527 case UCLASS_MMC:
528 return dp_size(dev->parent) +
529 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100530#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400531 case UCLASS_MASS_STORAGE:
532 case UCLASS_USB_HUB:
533 return dp_size(dev->parent) +
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100534 sizeof(struct efi_device_path_usb);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400535 default:
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200536 return dp_size(dev->parent) +
537 sizeof(struct efi_device_path_udevice);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400538 }
539}
540
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100541/*
542 * Recursively build a device path.
543 *
544 * @buf pointer to the end of the device path
545 * @dev device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100546 * Return: pointer to the end of the device path
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100547 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100548__maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400549{
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200550 enum uclass_id uclass_id;
551
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400552 if (!dev || !dev->driver)
553 return buf;
554
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200555 uclass_id = device_get_uclass_id(dev);
556 if (uclass_id != UCLASS_ROOT)
557 buf = dp_fill(buf, dev->parent);
558
559 switch (uclass_id) {
Jan Kiszkaf1389822022-10-14 18:10:06 +0200560#ifdef CONFIG_NETDEVICES
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100561 case UCLASS_ETH: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200562 struct efi_device_path_mac_addr *dp = buf;
Simon Glass95588622020-12-22 19:30:28 -0700563 struct eth_pdata *pdata = dev_get_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100564
565 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
566 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
567 dp->dp.length = sizeof(*dp);
568 memset(&dp->mac, 0, sizeof(dp->mac));
569 /* We only support IPv4 */
570 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
571 /* Ethernet */
572 dp->if_type = 1;
573 return &dp[1];
574 }
575#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100576 case UCLASS_BLK:
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200577 switch (device_get_uclass_id(dev->parent)) {
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100578#ifdef CONFIG_IDE
579 case UCLASS_IDE: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200580 struct efi_device_path_atapi *dp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700581 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100582
583 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
584 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
585 dp->dp.length = sizeof(*dp);
586 dp->logical_unit_number = desc->devnum;
587 dp->primary_secondary = IDE_BUS(desc->devnum);
588 dp->slave_master = desc->devnum %
589 (CONFIG_SYS_IDE_MAXDEVICE /
590 CONFIG_SYS_IDE_MAXBUS);
591 return &dp[1];
592 }
593#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600594#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100595 case UCLASS_SCSI: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200596 struct efi_device_path_scsi *dp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700597 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100598
599 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
600 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
601 dp->dp.length = sizeof(*dp);
602 dp->logical_unit_number = desc->lun;
603 dp->target_id = desc->target;
604 return &dp[1];
605 }
606#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600607#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100608 case UCLASS_MMC: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200609 struct efi_device_path_sd_mmc_path *sddp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700610 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100611
612 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
613 sddp->dp.sub_type = is_sd(desc) ?
614 DEVICE_PATH_SUB_TYPE_MSG_SD :
615 DEVICE_PATH_SUB_TYPE_MSG_MMC;
616 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700617 sddp->slot_number = dev_seq(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100618 return &sddp[1];
619 }
620#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200621#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
622 case UCLASS_AHCI: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200623 struct efi_device_path_sata *dp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700624 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200625
626 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
627 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
628 dp->dp.length = sizeof(*dp);
629 dp->hba_port = desc->devnum;
630 /* default 0xffff implies no port multiplier */
631 dp->port_multiplier_port = 0xffff;
632 dp->logical_unit_number = desc->lun;
633 return &dp[1];
634 }
635#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200636#if defined(CONFIG_NVME)
637 case UCLASS_NVME: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200638 struct efi_device_path_nvme *dp = buf;
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200639 u32 ns_id;
640
641 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
642 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
643 dp->dp.length = sizeof(*dp);
644 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
645 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
646 return &dp[1];
647 }
648#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100649#if defined(CONFIG_USB)
650 case UCLASS_MASS_STORAGE: {
Heinrich Schuchardt232c9db2023-04-01 07:21:55 +0200651 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200652 struct efi_device_path_controller *dp = buf;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100653
654 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
655 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CONTROLLER;
656 dp->dp.length = sizeof(*dp);
657 dp->controller_number = desc->lun;
658 return &dp[1];
659 }
660#endif
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200661 default: {
662 /* UCLASS_BLKMAP, UCLASS_HOST, UCLASS_VIRTIO */
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200663 struct efi_device_path_udevice *dp = buf;
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200664 struct blk_desc *desc = dev_get_uclass_plat(dev);
665
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200666 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
667 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
668 dp->dp.length = sizeof(*dp);
669 memcpy(&dp->guid, &efi_u_boot_guid,
670 sizeof(efi_guid_t));
671 dp->uclass_id = (UCLASS_BLK & 0xffff) |
672 (desc->uclass_id << 16);
673 dp->dev_number = desc->devnum;
674
675 return &dp[1];
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100676 }
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200677 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600678#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400679 case UCLASS_MMC: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200680 struct efi_device_path_sd_mmc_path *sddp = buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400681 struct mmc *mmc = mmc_get_mmc_dev(dev);
682 struct blk_desc *desc = mmc_get_blk_desc(mmc);
683
684 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100685 sddp->dp.sub_type = is_sd(desc) ?
686 DEVICE_PATH_SUB_TYPE_MSG_SD :
687 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400688 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700689 sddp->slot_number = dev_seq(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400690
691 return &sddp[1];
692 }
693#endif
694 case UCLASS_MASS_STORAGE:
695 case UCLASS_USB_HUB: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200696 struct efi_device_path_usb *udp = buf;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100697
698 switch (device_get_uclass_id(dev->parent)) {
699 case UCLASS_USB_HUB: {
700 struct usb_device *udev = dev_get_parent_priv(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400701
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100702 udp->parent_port_number = udev->portnr;
703 break;
704 }
705 default:
706 udp->parent_port_number = 0;
707 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400708 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100709 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400710 udp->dp.length = sizeof(*udp);
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100711 udp->usb_interface = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400712
713 return &udp[1];
714 }
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200715 default: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200716 struct efi_device_path_udevice *vdp = buf;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200717
718 vdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
719 vdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
720 vdp->dp.length = sizeof(*vdp);
721 memcpy(&vdp->guid, &efi_u_boot_guid, sizeof(efi_guid_t));
722 vdp->uclass_id = uclass_id;
723 vdp->dev_number = dev->seq_;
724
725 return &vdp[1];
726 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400727 }
728}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400729
730static unsigned dp_part_size(struct blk_desc *desc, int part)
731{
732 unsigned dpsize;
Simon Glassec209a72022-01-29 14:58:39 -0700733 struct udevice *dev = desc->bdev;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400734
Simon Glass222f3cb2021-09-24 18:30:17 -0600735 dpsize = dp_size(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400736
737 if (part == 0) /* the actual disk, not a partition */
738 return dpsize;
739
740 if (desc->part_type == PART_TYPE_ISO)
741 dpsize += sizeof(struct efi_device_path_cdrom_path);
742 else
743 dpsize += sizeof(struct efi_device_path_hard_drive_path);
744
745 return dpsize;
746}
747
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100748/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100749 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100750 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200751 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100752 * @desc block device descriptor
753 * @part partition number, 0 identifies a block device
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200754 *
755 * Return: pointer to position after the node
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100756 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100757static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400758{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600759 struct disk_partition info;
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200760 int ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400761
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200762 ret = part_get_info(desc, part, &info);
763 if (ret < 0)
764 return buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400765
766 if (desc->part_type == PART_TYPE_ISO) {
767 struct efi_device_path_cdrom_path *cddp = buf;
768
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100769 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400770 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
771 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
772 cddp->dp.length = sizeof(*cddp);
773 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200774 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400775
776 buf = &cddp[1];
777 } else {
778 struct efi_device_path_hard_drive_path *hddp = buf;
779
780 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
781 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
782 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100783 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400784 hddp->partition_start = info.start;
785 hddp->partition_end = info.size;
786 if (desc->part_type == PART_TYPE_EFI)
787 hddp->partmap_type = 2;
788 else
789 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100790
791 switch (desc->sig_type) {
792 case SIG_TYPE_NONE:
793 default:
794 hddp->signature_type = 0;
795 memset(hddp->partition_signature, 0,
796 sizeof(hddp->partition_signature));
797 break;
798 case SIG_TYPE_MBR:
799 hddp->signature_type = 1;
800 memset(hddp->partition_signature, 0,
801 sizeof(hddp->partition_signature));
802 memcpy(hddp->partition_signature, &desc->mbr_sig,
803 sizeof(desc->mbr_sig));
804 break;
805 case SIG_TYPE_GUID:
806 hddp->signature_type = 2;
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900807#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
808 /* info.uuid exists only with PARTITION_UUIDS */
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200809 if (uuid_str_to_bin(info.uuid,
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900810 hddp->partition_signature,
811 UUID_STR_FORMAT_GUID)) {
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200812 log_warning(
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900813 "Partition %d: invalid GUID %s\n",
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200814 part, info.uuid);
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900815 }
816#endif
Jonathan Gray84b4d702017-11-22 14:18:59 +1100817 break;
818 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400819
820 buf = &hddp[1];
821 }
822
823 return buf;
824}
825
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100826/*
827 * Create a device path for a block device or one of its partitions.
828 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200829 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100830 * @desc block device descriptor
831 * @part partition number, 0 identifies a block device
832 */
833static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
834{
Simon Glassec209a72022-01-29 14:58:39 -0700835 struct udevice *dev = desc->bdev;
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100836
Simon Glass222f3cb2021-09-24 18:30:17 -0600837 buf = dp_fill(buf, dev);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100838
839 if (part == 0) /* the actual disk, not a partition */
840 return buf;
841
842 return dp_part_node(buf, desc, part);
843}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400844
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200845/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400846struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
847{
848 void *buf, *start;
849
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100850 start = buf = efi_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100851 if (!buf)
852 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400853
854 buf = dp_part_fill(buf, desc, part);
855
856 *((struct efi_device_path *)buf) = END;
857
858 return start;
859}
860
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100861/*
862 * Create a device node for a block device partition.
863 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200864 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100865 * @desc block device descriptor
866 * @part partition number, 0 identifies a block device
867 */
868struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
869{
870 efi_uintn_t dpsize;
871 void *buf;
872
873 if (desc->part_type == PART_TYPE_ISO)
874 dpsize = sizeof(struct efi_device_path_cdrom_path);
875 else
876 dpsize = sizeof(struct efi_device_path_hard_drive_path);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100877 buf = efi_alloc(dpsize);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100878
Heinrich Schuchardte29fbb32022-10-06 13:36:02 +0200879 if (buf)
880 dp_part_node(buf, desc, part);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100881
882 return buf;
883}
884
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200885/**
886 * path_to_uefi() - convert UTF-8 path to an UEFI style path
887 *
888 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
889 * separators and UTF-16).
890 *
891 * @src: source buffer
892 * @uefi: target buffer, possibly unaligned
893 */
894static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400895{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200896 u16 *pos = uefi;
897
898 /*
899 * efi_set_bootdev() calls this routine indirectly before the UEFI
900 * subsystem is initialized. So we cannot assume unaligned access to be
901 * enabled.
902 */
903 allow_unaligned();
904
905 while (*src) {
906 s32 code = utf8_get(&src);
907
908 if (code < 0)
909 code = '?';
910 else if (code == '/')
911 code = '\\';
912 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400913 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200914 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400915}
916
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000917/**
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200918 * efi_dp_from_file() - append file path node to device path.
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000919 *
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200920 * @dp: device path or NULL
921 * @path: file path or NULL
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000922 * Return: device path or NULL in case of an error
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400923 */
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200924struct efi_device_path *efi_dp_from_file(const struct efi_device_path *dp,
925 const char *path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400926{
927 struct efi_device_path_file_path *fp;
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200928 void *buf, *pos;
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200929 size_t dpsize, fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400930
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200931 dpsize = efi_dp_size(dp);
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200932 fpsize = sizeof(struct efi_device_path) +
933 2 * (utf8_utf16_strlen(path) + 1);
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900934 if (fpsize > U16_MAX)
935 return NULL;
936
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200937 buf = efi_alloc(dpsize + fpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100938 if (!buf)
939 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400940
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200941 memcpy(buf, dp, dpsize);
942 pos = buf + dpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400943
944 /* add file-path: */
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000945 if (*path) {
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200946 fp = pos;
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000947 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
948 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
949 fp->dp.length = (u16)fpsize;
950 path_to_uefi(fp->str, path);
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200951 pos += fpsize;
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000952 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400953
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200954 memcpy(pos, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400955
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200956 return buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400957}
958
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100959struct efi_device_path *efi_dp_from_uart(void)
960{
961 void *buf, *pos;
962 struct efi_device_path_uart *uart;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200963 size_t dpsize = dp_size(dm_root()) + sizeof(*uart) + sizeof(END);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100964
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100965 buf = efi_alloc(dpsize);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100966 if (!buf)
967 return NULL;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200968 pos = dp_fill(buf, dm_root());
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100969 uart = pos;
970 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
971 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
972 uart->dp.length = sizeof(*uart);
973 pos += sizeof(*uart);
974 memcpy(pos, &END, sizeof(END));
975
976 return buf;
977}
978
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +0200979struct efi_device_path __maybe_unused *efi_dp_from_eth(void)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400980{
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400981 void *buf, *start;
982 unsigned dpsize = 0;
983
984 assert(eth_get_dev());
985
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400986 dpsize += dp_size(eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400987
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100988 start = buf = efi_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100989 if (!buf)
990 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400991
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400992 buf = dp_fill(buf, eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400993
994 *((struct efi_device_path *)buf) = END;
995
996 return start;
997}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400998
Rob Clark18ceba72017-10-10 08:23:06 -0400999/* Construct a device-path for memory-mapped image */
1000struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1001 uint64_t start_address,
1002 uint64_t end_address)
1003{
1004 struct efi_device_path_memory *mdp;
1005 void *buf, *start;
1006
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001007 start = buf = efi_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001008 if (!buf)
1009 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -04001010
1011 mdp = buf;
1012 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1013 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1014 mdp->dp.length = sizeof(*mdp);
1015 mdp->memory_type = memory_type;
1016 mdp->start_address = start_address;
1017 mdp->end_address = end_address;
1018 buf = &mdp[1];
1019
1020 *((struct efi_device_path *)buf) = END;
1021
1022 return start;
1023}
1024
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001025/**
1026 * efi_dp_split_file_path() - split of relative file path from device path
1027 *
1028 * Given a device path indicating a file on a device, separate the device
1029 * path in two: the device path of the actual device and the file path
1030 * relative to this device.
1031 *
1032 * @full_path: device path including device and file path
1033 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001034 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001035 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001036 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001037efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1038 struct efi_device_path **device_path,
1039 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001040{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001041 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001042
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001043 *device_path = NULL;
1044 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001045 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001046 if (!dp)
1047 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001048 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001049 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001050 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001051 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001052 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001053 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001054 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001055 if (!fp)
1056 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001057 p->type = DEVICE_PATH_TYPE_END;
1058 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1059 p->length = sizeof(*p);
1060
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001061out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001062 *device_path = dp;
1063 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001064 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001065}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001066
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001067/**
1068 * efi_dp_from_name() - convert U-Boot device and file path to device path
1069 *
1070 * @dev: U-Boot device, e.g. 'mmc'
1071 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1072 * @path: file path relative to U-Boot device, may be NULL
1073 * @device: pointer to receive device path of the device
1074 * @file: pointer to receive device path for the file
1075 * Return: status code
1076 */
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001077efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1078 const char *path,
1079 struct efi_device_path **device,
1080 struct efi_device_path **file)
1081{
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001082 struct blk_desc *desc = NULL;
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001083 struct efi_device_path *dp;
Simon Glassc1c4a8f2020-05-10 11:39:57 -06001084 struct disk_partition fs_partition;
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001085 size_t image_size;
1086 void *image_addr;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001087 int part = 0;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001088
AKASHI Takahiro39844412018-11-05 18:06:40 +09001089 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001090 return EFI_INVALID_PARAMETER;
1091
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001092 if (!strcmp(dev, "Mem") || !strcmp(dev, "hostfs")) {
Heinrich Schuchardtaecb9e22023-05-12 20:18:10 +02001093 /* loadm command and semihosting */
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001094 efi_get_image_parameters(&image_addr, &image_size);
1095
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001096 dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
1097 (uintptr_t)image_addr, image_size);
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001098 } else if (IS_ENABLED(CONFIG_NETDEVICES) && !strcmp(dev, "Net")) {
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001099 dp = efi_dp_from_eth();
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001100 } else if (!strcmp(dev, "Uart")) {
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001101 dp = efi_dp_from_uart();
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001102 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001103 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1104 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001105 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001106 return EFI_INVALID_PARAMETER;
1107
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001108 dp = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001109 }
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001110 if (device)
1111 *device = dp;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001112
1113 if (!path)
1114 return EFI_SUCCESS;
1115
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +02001116 *file = efi_dp_from_file(dp, path);
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001117 if (!*file)
Heinrich Schuchardtbf0b3a12023-05-13 10:22:21 +02001118 return EFI_OUT_OF_RESOURCES;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001119
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001120 return EFI_SUCCESS;
1121}
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001122
1123/**
1124 * efi_dp_check_length() - check length of a device path
1125 *
1126 * @dp: pointer to device path
1127 * @maxlen: maximum length of the device path
1128 * Return:
1129 * * length of the device path if it is less or equal @maxlen
1130 * * -1 if the device path is longer then @maxlen
1131 * * -1 if a device path node has a length of less than 4
1132 * * -EINVAL if maxlen exceeds SSIZE_MAX
1133 */
1134ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1135 const size_t maxlen)
1136{
1137 ssize_t ret = 0;
1138 u16 len;
1139
1140 if (maxlen > SSIZE_MAX)
1141 return -EINVAL;
1142 for (;;) {
1143 len = dp->length;
1144 if (len < 4)
1145 return -1;
1146 ret += len;
1147 if (ret > maxlen)
1148 return -1;
1149 if (dp->type == DEVICE_PATH_TYPE_END &&
1150 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1151 return ret;
1152 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1153 }
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001154}
1155
1156/**
1157 * efi_dp_from_lo() - Get the instance of a VenMedia node in a
1158 * multi-instance device path that matches
1159 * a specific GUID. This kind of device paths
1160 * is found in Boot#### options describing an
1161 * initrd location
1162 *
1163 * @lo: EFI_LOAD_OPTION containing a valid device path
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001164 * @guid: guid to search for
1165 *
1166 * Return:
1167 * device path including the VenMedia node or NULL.
1168 * Caller must free the returned value.
1169 */
1170struct
1171efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001172 const efi_guid_t *guid)
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001173{
1174 struct efi_device_path *fp = lo->file_path;
1175 struct efi_device_path_vendor *vendor;
1176 int lo_len = lo->file_path_length;
1177
1178 for (; lo_len >= sizeof(struct efi_device_path);
1179 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1180 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1181 break;
1182 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1183 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1184 continue;
1185
1186 vendor = (struct efi_device_path_vendor *)fp;
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001187 if (!guidcmp(&vendor->guid, guid))
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +02001188 return efi_dp_dup(efi_dp_next(fp));
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001189 }
1190 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1191
1192 return NULL;
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001193}
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001194
1195/**
1196 * search_gpt_dp_node() - search gpt device path node
1197 *
1198 * @device_path: device path
1199 *
1200 * Return: pointer to the gpt device path node
1201 */
1202struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1203{
1204 struct efi_device_path *dp = device_path;
1205
1206 while (dp) {
1207 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1208 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1209 struct efi_device_path_hard_drive_path *hd_dp =
1210 (struct efi_device_path_hard_drive_path *)dp;
1211
1212 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1213 hd_dp->signature_type == SIG_TYPE_GUID)
1214 return dp;
1215 }
1216 dp = efi_dp_next(dp);
1217 }
1218
1219 return NULL;
1220}