blob: d5cc49583041f388930299bffa33250af9dfe347 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clarkf90cb9f2017-09-13 18:05:28 -04002/*
3 * EFI device path from u-boot device-model mapping
4 *
5 * (C) Copyright 2017 Rob Clark
Rob Clarkf90cb9f2017-09-13 18:05:28 -04006 */
7
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +02008#define LOG_CATEGORY LOGC_EFI
9
Rob Clarkf90cb9f2017-09-13 18:05:28 -040010#include <common.h>
11#include <blk.h>
12#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <net.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040015#include <usb.h>
16#include <mmc.h>
Patrick Wildta3ca37e2019-10-03 16:24:17 +020017#include <nvme.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040018#include <efi_loader.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040019#include <part.h>
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
AKASHI Takahiro659a6262019-09-12 13:52:35 +090024#ifdef CONFIG_SANDBOX
25const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
26#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +020027#ifdef CONFIG_VIRTIO_BLK
28const efi_guid_t efi_guid_virtio_dev = U_BOOT_VIRTIO_DEV_GUID;
29#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +090030
Rob Clarkf90cb9f2017-09-13 18:05:28 -040031/* template END node: */
Masahisa Kojima97bd9da2022-06-19 13:55:59 +090032const struct efi_device_path END = {
Rob Clarkf90cb9f2017-09-13 18:05:28 -040033 .type = DEVICE_PATH_TYPE_END,
34 .sub_type = DEVICE_PATH_SUB_TYPE_END,
35 .length = sizeof(END),
36};
37
Rob Clarkf90cb9f2017-09-13 18:05:28 -040038/* template ROOT node: */
39static const struct efi_device_path_vendor ROOT = {
40 .dp = {
41 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
42 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
43 .length = sizeof(ROOT),
44 },
45 .guid = U_BOOT_GUID,
46};
47
Simon Glass222f3cb2021-09-24 18:30:17 -060048#if defined(CONFIG_MMC)
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010049/*
50 * Determine if an MMC device is an SD card.
51 *
52 * @desc block device descriptor
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010053 * Return: true if the device is an SD card
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010054 */
55static bool is_sd(struct blk_desc *desc)
56{
57 struct mmc *mmc = find_mmc_device(desc->devnum);
58
59 if (!mmc)
60 return false;
61
62 return IS_SD(mmc) != 0U;
63}
64#endif
65
Rob Clarkf90cb9f2017-09-13 18:05:28 -040066/*
67 * Iterate to next block in device-path, terminating (returning NULL)
68 * at /End* node.
69 */
70struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
71{
72 if (dp == NULL)
73 return NULL;
74 if (dp->type == DEVICE_PATH_TYPE_END)
75 return NULL;
76 dp = ((void *)dp) + dp->length;
77 if (dp->type == DEVICE_PATH_TYPE_END)
78 return NULL;
79 return (struct efi_device_path *)dp;
80}
81
82/*
83 * Compare two device-paths, stopping when the shorter of the two hits
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +020084 * an End* node. This is useful to, for example, compare a device-path
Rob Clarkf90cb9f2017-09-13 18:05:28 -040085 * representing a device with one representing a file on the device, or
86 * a device with a parent device.
87 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +020088int efi_dp_match(const struct efi_device_path *a,
89 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -040090{
91 while (1) {
92 int ret;
93
94 ret = memcmp(&a->length, &b->length, sizeof(a->length));
95 if (ret)
96 return ret;
97
98 ret = memcmp(a, b, a->length);
99 if (ret)
100 return ret;
101
102 a = efi_dp_next(a);
103 b = efi_dp_next(b);
104
105 if (!a || !b)
106 return 0;
107 }
108}
109
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100110/**
111 * efi_dp_shorten() - shorten device-path
112 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200113 * We can have device paths that start with a USB WWID or a USB Class node,
114 * and a few other cases which don't encode the full device path with bus
115 * hierarchy:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400116 *
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100117 * * MESSAGING:USB_WWID
118 * * MESSAGING:USB_CLASS
119 * * MEDIA:FILE_PATH
120 * * MEDIA:HARD_DRIVE
121 * * MESSAGING:URI
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200122 *
123 * See UEFI spec (section 3.1.2, about short-form device-paths)
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100124 *
Heinrich Schuchardtdb17dbc2022-03-21 08:26:48 +0100125 * @dp: original device-path
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100126 * @Return: shortened device-path or NULL
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400127 */
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100128struct efi_device_path *efi_dp_shorten(struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400129{
130 while (dp) {
131 /*
132 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
133 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
134 * so not sure when we would see these other cases.
135 */
Heinrich Schuchardtfc607ab2023-03-19 16:18:09 +0100136 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB) ||
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400137 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
138 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
139 return dp;
140
141 dp = efi_dp_next(dp);
142 }
143
144 return dp;
145}
146
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100147/**
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100148 * find_handle() - find handle by device path and installed protocol
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100149 *
150 * If @rem is provided, the handle with the longest partial match is returned.
151 *
152 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100153 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100154 * @short_path: use short form device path for matching
155 * @rem: pointer to receive remaining device path
156 * Return: matching handle
157 */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100158static efi_handle_t find_handle(struct efi_device_path *dp,
159 const efi_guid_t *guid, bool short_path,
160 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400161{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100162 efi_handle_t handle, best_handle = NULL;
163 efi_uintn_t len, best_len = 0;
164
165 len = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400166
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100167 list_for_each_entry(handle, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100168 struct efi_handler *handler;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100169 struct efi_device_path *dp_current;
170 efi_uintn_t len_current;
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100171 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400172
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100173 if (guid) {
174 ret = efi_search_protocol(handle, guid, &handler);
175 if (ret != EFI_SUCCESS)
176 continue;
177 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100178 ret = efi_search_protocol(handle, &efi_guid_device_path,
179 &handler);
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100180 if (ret != EFI_SUCCESS)
181 continue;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100182 dp_current = handler->protocol_interface;
183 if (short_path) {
184 dp_current = efi_dp_shorten(dp_current);
185 if (!dp_current)
186 continue;
187 }
188 len_current = efi_dp_instance_size(dp_current);
189 if (rem) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100190 if (len_current > len)
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100191 continue;
192 } else {
193 if (len_current != len)
194 continue;
195 }
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100196 if (memcmp(dp_current, dp, len_current))
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100197 continue;
198 if (!rem)
199 return handle;
200 if (len_current > best_len) {
201 best_len = len_current;
202 best_handle = handle;
203 *rem = (void*)((u8 *)dp + len_current);
204 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400205 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100206 return best_handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400207}
208
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100209/**
210 * efi_dp_find_obj() - find handle by device path
211 *
212 * If @rem is provided, the handle with the longest partial match is returned.
213 *
214 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100215 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100216 * @rem: pointer to receive remaining device path
217 * Return: matching handle
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400218 */
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100219efi_handle_t efi_dp_find_obj(struct efi_device_path *dp,
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100220 const efi_guid_t *guid,
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100221 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400222{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100223 efi_handle_t handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400224
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100225 handle = find_handle(dp, guid, false, rem);
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100226 if (!handle)
227 /* Match short form device path */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100228 handle = find_handle(dp, guid, true, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400229
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100230 return handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400231}
232
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100233/*
234 * Determine the last device path node that is not the end node.
235 *
236 * @dp device path
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100237 * Return: last node before the end node if it exists
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100238 * otherwise NULL
239 */
240const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
241{
242 struct efi_device_path *ret;
243
244 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
245 return NULL;
246 while (dp) {
247 ret = (struct efi_device_path *)dp;
248 dp = efi_dp_next(dp);
249 }
250 return ret;
251}
252
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200253/* get size of the first device path instance excluding end node */
254efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400255{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200256 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400257
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200258 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
259 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400260 while (dp) {
261 sz += dp->length;
262 dp = efi_dp_next(dp);
263 }
264
265 return sz;
266}
267
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200268/* get size of multi-instance device path excluding end node */
269efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
270{
271 const struct efi_device_path *p = dp;
272
273 if (!p)
274 return 0;
275 while (p->type != DEVICE_PATH_TYPE_END ||
276 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
277 p = (void *)p + p->length;
278
279 return (void *)p - (void *)dp;
280}
281
282/* copy multi-instance device path */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400283struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
284{
285 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200286 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400287
288 if (!dp)
289 return NULL;
290
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100291 ndp = efi_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100292 if (!ndp)
293 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400294 memcpy(ndp, dp, sz);
295
296 return ndp;
297}
298
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200299/**
300 * efi_dp_append_or_concatenate() - Append or concatenate two device paths.
301 * Concatenated device path will be separated
302 * by a sub-type 0xff end node
303 *
304 * @dp1: First device path
305 * @dp2: Second device path
306 * @concat: If true the two device paths will be concatenated and separated
307 * by an end of entrire device path sub-type 0xff end node.
308 * If true the second device path will be appended to the first and
309 * terminated by an end node
310 *
311 * Return:
312 * concatenated device path or NULL. Caller must free the returned value
313 */
314static struct
315efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
316 const struct efi_device_path *dp2,
317 bool concat)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400318{
319 struct efi_device_path *ret;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200320 size_t end_size = sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400321
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200322 if (concat)
323 end_size = 2 * sizeof(END);
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200324 if (!dp1 && !dp2) {
325 /* return an end node */
326 ret = efi_dp_dup(&END);
327 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400328 ret = efi_dp_dup(dp2);
329 } else if (!dp2) {
330 ret = efi_dp_dup(dp1);
331 } else {
332 /* both dp1 and dp2 are non-null */
333 unsigned sz1 = efi_dp_size(dp1);
334 unsigned sz2 = efi_dp_size(dp2);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100335 void *p = efi_alloc(sz1 + sz2 + end_size);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100336 if (!p)
337 return NULL;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200338 ret = p;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400339 memcpy(p, dp1, sz1);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200340 p += sz1;
341
342 if (concat) {
343 memcpy(p, &END, sizeof(END));
344 p += sizeof(END);
345 }
346
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200347 /* the end node of the second device path has to be retained */
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200348 memcpy(p, dp2, sz2);
349 p += sz2;
350 memcpy(p, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400351 }
352
353 return ret;
354}
355
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200356/**
357 * efi_dp_append() - Append a device to an existing device path.
358 *
359 * @dp1: First device path
360 * @dp2: Second device path
361 *
362 * Return:
363 * concatenated device path or NULL. Caller must free the returned value
364 */
365struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
366 const struct efi_device_path *dp2)
367{
368 return efi_dp_append_or_concatenate(dp1, dp2, false);
369}
370
371/**
372 * efi_dp_concat() - Concatenate 2 device paths. The final device path will
373 * contain two device paths separated by and end node (0xff).
374 *
375 * @dp1: First device path
376 * @dp2: Second device path
377 *
378 * Return:
379 * concatenated device path or NULL. Caller must free the returned value
380 */
381struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
382 const struct efi_device_path *dp2)
383{
384 return efi_dp_append_or_concatenate(dp1, dp2, true);
385}
386
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400387struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
388 const struct efi_device_path *node)
389{
390 struct efi_device_path *ret;
391
392 if (!node && !dp) {
393 ret = efi_dp_dup(&END);
394 } else if (!node) {
395 ret = efi_dp_dup(dp);
396 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200397 size_t sz = node->length;
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100398 void *p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100399 if (!p)
400 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400401 memcpy(p, node, sz);
402 memcpy(p + sz, &END, sizeof(END));
403 ret = p;
404 } else {
405 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200406 size_t sz = efi_dp_size(dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100407 void *p = efi_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100408 if (!p)
409 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400410 memcpy(p, dp, sz);
411 memcpy(p + sz, node, node->length);
412 memcpy(p + sz + node->length, &END, sizeof(END));
413 ret = p;
414 }
415
416 return ret;
417}
418
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200419struct efi_device_path *efi_dp_create_device_node(const u8 type,
420 const u8 sub_type,
421 const u16 length)
422{
423 struct efi_device_path *ret;
424
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200425 if (length < sizeof(struct efi_device_path))
426 return NULL;
427
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100428 ret = efi_alloc(length);
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200429 if (!ret)
430 return ret;
431 ret->type = type;
432 ret->sub_type = sub_type;
433 ret->length = length;
434 return ret;
435}
436
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200437struct efi_device_path *efi_dp_append_instance(
438 const struct efi_device_path *dp,
439 const struct efi_device_path *dpi)
440{
441 size_t sz, szi;
442 struct efi_device_path *p, *ret;
443
444 if (!dpi)
445 return NULL;
446 if (!dp)
447 return efi_dp_dup(dpi);
448 sz = efi_dp_size(dp);
449 szi = efi_dp_instance_size(dpi);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100450 p = efi_alloc(sz + szi + 2 * sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200451 if (!p)
452 return NULL;
453 ret = p;
454 memcpy(p, dp, sz + sizeof(END));
455 p = (void *)p + sz;
456 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
457 p = (void *)p + sizeof(END);
458 memcpy(p, dpi, szi);
459 p = (void *)p + szi;
460 memcpy(p, &END, sizeof(END));
461 return ret;
462}
463
464struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
465 efi_uintn_t *size)
466{
467 size_t sz;
468 struct efi_device_path *p;
469
470 if (size)
471 *size = 0;
472 if (!dp || !*dp)
473 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200474 sz = efi_dp_instance_size(*dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100475 p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200476 if (!p)
477 return NULL;
478 memcpy(p, *dp, sz + sizeof(END));
479 *dp = (void *)*dp + sz;
480 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
481 *dp = (void *)*dp + sizeof(END);
482 else
483 *dp = NULL;
484 if (size)
485 *size = sz + sizeof(END);
486 return p;
487}
488
489bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
490{
491 const struct efi_device_path *p = dp;
492
493 if (!p)
494 return false;
495 while (p->type != DEVICE_PATH_TYPE_END)
496 p = (void *)p + p->length;
497 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
498}
499
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400500/* size of device-path not including END node for device and all parents
501 * up to the root device.
502 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100503__maybe_unused static unsigned int dp_size(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400504{
505 if (!dev || !dev->driver)
506 return sizeof(ROOT);
507
Simon Glass56ada7b2022-01-29 14:58:38 -0700508 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400509 case UCLASS_ROOT:
510 case UCLASS_SIMPLE_BUS:
511 /* stop traversing parents at this point: */
512 return sizeof(ROOT);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100513 case UCLASS_ETH:
514 return dp_size(dev->parent) +
515 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100516 case UCLASS_BLK:
517 switch (dev->parent->uclass->uc_drv->id) {
518#ifdef CONFIG_IDE
519 case UCLASS_IDE:
520 return dp_size(dev->parent) +
521 sizeof(struct efi_device_path_atapi);
522#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600523#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100524 case UCLASS_SCSI:
525 return dp_size(dev->parent) +
526 sizeof(struct efi_device_path_scsi);
527#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600528#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100529 case UCLASS_MMC:
530 return dp_size(dev->parent) +
531 sizeof(struct efi_device_path_sd_mmc_path);
532#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200533#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
534 case UCLASS_AHCI:
535 return dp_size(dev->parent) +
536 sizeof(struct efi_device_path_sata);
537#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200538#if defined(CONFIG_NVME)
539 case UCLASS_NVME:
540 return dp_size(dev->parent) +
541 sizeof(struct efi_device_path_nvme);
542#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900543#ifdef CONFIG_SANDBOX
Simon Glasse57f8d42022-10-29 19:47:17 -0600544 case UCLASS_HOST:
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900545 /*
546 * Sandbox's host device will be represented
547 * as vendor device with extra one byte for
548 * device number
549 */
550 return dp_size(dev->parent)
551 + sizeof(struct efi_device_path_vendor) + 1;
552#endif
Heinrich Schuchardtfc607ab2023-03-19 16:18:09 +0100553#ifdef CONFIG_USB
554 case UCLASS_MASS_STORAGE:
555 return dp_size(dev->parent)
556 + sizeof(struct efi_device_path_controller);
557#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200558#ifdef CONFIG_VIRTIO_BLK
559 case UCLASS_VIRTIO:
560 /*
561 * Virtio devices will be represented as a vendor
562 * device node with an extra byte for the device
563 * number.
564 */
565 return dp_size(dev->parent)
566 + sizeof(struct efi_device_path_vendor) + 1;
567#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100568 default:
569 return dp_size(dev->parent);
570 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600571#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400572 case UCLASS_MMC:
573 return dp_size(dev->parent) +
574 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100575#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400576 case UCLASS_MASS_STORAGE:
577 case UCLASS_USB_HUB:
578 return dp_size(dev->parent) +
Heinrich Schuchardtfc607ab2023-03-19 16:18:09 +0100579 sizeof(struct efi_device_path_usb);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400580 default:
581 /* just skip over unknown classes: */
582 return dp_size(dev->parent);
583 }
584}
585
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100586/*
587 * Recursively build a device path.
588 *
589 * @buf pointer to the end of the device path
590 * @dev device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100591 * Return: pointer to the end of the device path
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100592 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100593__maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400594{
595 if (!dev || !dev->driver)
596 return buf;
597
Simon Glass56ada7b2022-01-29 14:58:38 -0700598 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400599 case UCLASS_ROOT:
600 case UCLASS_SIMPLE_BUS: {
601 /* stop traversing parents at this point: */
602 struct efi_device_path_vendor *vdp = buf;
603 *vdp = ROOT;
604 return &vdp[1];
605 }
Jan Kiszkaf1389822022-10-14 18:10:06 +0200606#ifdef CONFIG_NETDEVICES
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100607 case UCLASS_ETH: {
608 struct efi_device_path_mac_addr *dp =
609 dp_fill(buf, dev->parent);
Simon Glass95588622020-12-22 19:30:28 -0700610 struct eth_pdata *pdata = dev_get_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100611
612 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
613 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
614 dp->dp.length = sizeof(*dp);
615 memset(&dp->mac, 0, sizeof(dp->mac));
616 /* We only support IPv4 */
617 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
618 /* Ethernet */
619 dp->if_type = 1;
620 return &dp[1];
621 }
622#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100623 case UCLASS_BLK:
624 switch (dev->parent->uclass->uc_drv->id) {
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900625#ifdef CONFIG_SANDBOX
Simon Glasse57f8d42022-10-29 19:47:17 -0600626 case UCLASS_HOST: {
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900627 /* stop traversing parents at this point: */
Heinrich Schuchardt1e3beaf2020-05-06 01:28:08 +0200628 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700629 struct blk_desc *desc = dev_get_uclass_plat(dev);
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900630
631 dp_fill(buf, dev->parent);
632 dp = buf;
633 ++dp;
634 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
635 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
636 dp->dp.length = sizeof(*dp) + 1;
637 memcpy(&dp->guid, &efi_guid_host_dev,
638 sizeof(efi_guid_t));
639 dp->vendor_data[0] = desc->devnum;
640 return &dp->vendor_data[1];
641 }
642#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200643#ifdef CONFIG_VIRTIO_BLK
644 case UCLASS_VIRTIO: {
645 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700646 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200647
648 dp_fill(buf, dev->parent);
649 dp = buf;
650 ++dp;
651 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
652 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
653 dp->dp.length = sizeof(*dp) + 1;
654 memcpy(&dp->guid, &efi_guid_virtio_dev,
655 sizeof(efi_guid_t));
656 dp->vendor_data[0] = desc->devnum;
657 return &dp->vendor_data[1];
658 }
659#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100660#ifdef CONFIG_IDE
661 case UCLASS_IDE: {
662 struct efi_device_path_atapi *dp =
663 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700664 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100665
666 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
667 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
668 dp->dp.length = sizeof(*dp);
669 dp->logical_unit_number = desc->devnum;
670 dp->primary_secondary = IDE_BUS(desc->devnum);
671 dp->slave_master = desc->devnum %
672 (CONFIG_SYS_IDE_MAXDEVICE /
673 CONFIG_SYS_IDE_MAXBUS);
674 return &dp[1];
675 }
676#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600677#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100678 case UCLASS_SCSI: {
679 struct efi_device_path_scsi *dp =
680 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700681 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100682
683 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
684 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
685 dp->dp.length = sizeof(*dp);
686 dp->logical_unit_number = desc->lun;
687 dp->target_id = desc->target;
688 return &dp[1];
689 }
690#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600691#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100692 case UCLASS_MMC: {
693 struct efi_device_path_sd_mmc_path *sddp =
694 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700695 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100696
697 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
698 sddp->dp.sub_type = is_sd(desc) ?
699 DEVICE_PATH_SUB_TYPE_MSG_SD :
700 DEVICE_PATH_SUB_TYPE_MSG_MMC;
701 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700702 sddp->slot_number = dev_seq(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100703 return &sddp[1];
704 }
705#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200706#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
707 case UCLASS_AHCI: {
708 struct efi_device_path_sata *dp =
709 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700710 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200711
712 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
713 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
714 dp->dp.length = sizeof(*dp);
715 dp->hba_port = desc->devnum;
716 /* default 0xffff implies no port multiplier */
717 dp->port_multiplier_port = 0xffff;
718 dp->logical_unit_number = desc->lun;
719 return &dp[1];
720 }
721#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200722#if defined(CONFIG_NVME)
723 case UCLASS_NVME: {
724 struct efi_device_path_nvme *dp =
725 dp_fill(buf, dev->parent);
726 u32 ns_id;
727
728 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
729 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
730 dp->dp.length = sizeof(*dp);
731 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
732 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
733 return &dp[1];
734 }
735#endif
Heinrich Schuchardtfc607ab2023-03-19 16:18:09 +0100736#if defined(CONFIG_USB)
737 case UCLASS_MASS_STORAGE: {
738 struct blk_desc *desc = desc = dev_get_uclass_plat(dev);
739 struct efi_device_path_controller *dp =
740 dp_fill(buf, dev->parent);
741
742 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
743 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CONTROLLER;
744 dp->dp.length = sizeof(*dp);
745 dp->controller_number = desc->lun;
746 return &dp[1];
747 }
748#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100749 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100750 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
751 __FILE__, __LINE__, __func__,
752 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100753 return dp_fill(buf, dev->parent);
754 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600755#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400756 case UCLASS_MMC: {
757 struct efi_device_path_sd_mmc_path *sddp =
758 dp_fill(buf, dev->parent);
759 struct mmc *mmc = mmc_get_mmc_dev(dev);
760 struct blk_desc *desc = mmc_get_blk_desc(mmc);
761
762 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100763 sddp->dp.sub_type = is_sd(desc) ?
764 DEVICE_PATH_SUB_TYPE_MSG_SD :
765 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400766 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700767 sddp->slot_number = dev_seq(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400768
769 return &sddp[1];
770 }
771#endif
772 case UCLASS_MASS_STORAGE:
773 case UCLASS_USB_HUB: {
Heinrich Schuchardtfc607ab2023-03-19 16:18:09 +0100774 struct efi_device_path_usb *udp = dp_fill(buf, dev->parent);
775
776 switch (device_get_uclass_id(dev->parent)) {
777 case UCLASS_USB_HUB: {
778 struct usb_device *udev = dev_get_parent_priv(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400779
Heinrich Schuchardtfc607ab2023-03-19 16:18:09 +0100780 udp->parent_port_number = udev->portnr;
781 break;
782 }
783 default:
784 udp->parent_port_number = 0;
785 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400786 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardtfc607ab2023-03-19 16:18:09 +0100787 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400788 udp->dp.length = sizeof(*udp);
Heinrich Schuchardtfc607ab2023-03-19 16:18:09 +0100789 udp->usb_interface = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400790
791 return &udp[1];
792 }
793 default:
Simon Glass56ada7b2022-01-29 14:58:38 -0700794 /* If the uclass driver is missing, this will show NULL */
795 log_debug("unhandled device class: %s (%s)\n", dev->name,
796 dev_get_uclass_name(dev));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400797 return dp_fill(buf, dev->parent);
798 }
799}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400800
801static unsigned dp_part_size(struct blk_desc *desc, int part)
802{
803 unsigned dpsize;
Simon Glassec209a72022-01-29 14:58:39 -0700804 struct udevice *dev = desc->bdev;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400805
Simon Glass222f3cb2021-09-24 18:30:17 -0600806 dpsize = dp_size(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400807
808 if (part == 0) /* the actual disk, not a partition */
809 return dpsize;
810
811 if (desc->part_type == PART_TYPE_ISO)
812 dpsize += sizeof(struct efi_device_path_cdrom_path);
813 else
814 dpsize += sizeof(struct efi_device_path_hard_drive_path);
815
816 return dpsize;
817}
818
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100819/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100820 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100821 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200822 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100823 * @desc block device descriptor
824 * @part partition number, 0 identifies a block device
825 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100826static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400827{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600828 struct disk_partition info;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400829
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400830 part_get_info(desc, part, &info);
831
832 if (desc->part_type == PART_TYPE_ISO) {
833 struct efi_device_path_cdrom_path *cddp = buf;
834
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100835 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400836 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
837 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
838 cddp->dp.length = sizeof(*cddp);
839 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200840 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400841
842 buf = &cddp[1];
843 } else {
844 struct efi_device_path_hard_drive_path *hddp = buf;
845
846 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
847 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
848 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100849 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400850 hddp->partition_start = info.start;
851 hddp->partition_end = info.size;
852 if (desc->part_type == PART_TYPE_EFI)
853 hddp->partmap_type = 2;
854 else
855 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100856
857 switch (desc->sig_type) {
858 case SIG_TYPE_NONE:
859 default:
860 hddp->signature_type = 0;
861 memset(hddp->partition_signature, 0,
862 sizeof(hddp->partition_signature));
863 break;
864 case SIG_TYPE_MBR:
865 hddp->signature_type = 1;
866 memset(hddp->partition_signature, 0,
867 sizeof(hddp->partition_signature));
868 memcpy(hddp->partition_signature, &desc->mbr_sig,
869 sizeof(desc->mbr_sig));
870 break;
871 case SIG_TYPE_GUID:
872 hddp->signature_type = 2;
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900873#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
874 /* info.uuid exists only with PARTITION_UUIDS */
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200875 if (uuid_str_to_bin(info.uuid,
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900876 hddp->partition_signature,
877 UUID_STR_FORMAT_GUID)) {
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200878 log_warning(
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900879 "Partition %d: invalid GUID %s\n",
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200880 part, info.uuid);
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900881 }
882#endif
Jonathan Gray84b4d702017-11-22 14:18:59 +1100883 break;
884 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400885
886 buf = &hddp[1];
887 }
888
889 return buf;
890}
891
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100892/*
893 * Create a device path for a block device or one of its partitions.
894 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200895 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100896 * @desc block device descriptor
897 * @part partition number, 0 identifies a block device
898 */
899static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
900{
Simon Glassec209a72022-01-29 14:58:39 -0700901 struct udevice *dev = desc->bdev;
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100902
Simon Glass222f3cb2021-09-24 18:30:17 -0600903 buf = dp_fill(buf, dev);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100904
905 if (part == 0) /* the actual disk, not a partition */
906 return buf;
907
908 return dp_part_node(buf, desc, part);
909}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400910
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200911/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400912struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
913{
914 void *buf, *start;
915
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100916 start = buf = efi_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100917 if (!buf)
918 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400919
920 buf = dp_part_fill(buf, desc, part);
921
922 *((struct efi_device_path *)buf) = END;
923
924 return start;
925}
926
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100927/*
928 * Create a device node for a block device partition.
929 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200930 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100931 * @desc block device descriptor
932 * @part partition number, 0 identifies a block device
933 */
934struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
935{
936 efi_uintn_t dpsize;
937 void *buf;
938
939 if (desc->part_type == PART_TYPE_ISO)
940 dpsize = sizeof(struct efi_device_path_cdrom_path);
941 else
942 dpsize = sizeof(struct efi_device_path_hard_drive_path);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100943 buf = efi_alloc(dpsize);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100944
Heinrich Schuchardte29fbb32022-10-06 13:36:02 +0200945 if (buf)
946 dp_part_node(buf, desc, part);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100947
948 return buf;
949}
950
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200951/**
952 * path_to_uefi() - convert UTF-8 path to an UEFI style path
953 *
954 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
955 * separators and UTF-16).
956 *
957 * @src: source buffer
958 * @uefi: target buffer, possibly unaligned
959 */
960static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400961{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200962 u16 *pos = uefi;
963
964 /*
965 * efi_set_bootdev() calls this routine indirectly before the UEFI
966 * subsystem is initialized. So we cannot assume unaligned access to be
967 * enabled.
968 */
969 allow_unaligned();
970
971 while (*src) {
972 s32 code = utf8_get(&src);
973
974 if (code < 0)
975 code = '?';
976 else if (code == '/')
977 code = '\\';
978 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400979 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200980 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400981}
982
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000983/**
984 * efi_dp_from_file() - create device path for file
985 *
986 * The function creates a device path from the block descriptor @desc and the
987 * partition number @part and appends a device path node created describing the
988 * file path @path.
989 *
990 * If @desc is NULL, the device path will not contain nodes describing the
991 * partition.
992 * If @path is an empty string "", the device path will not contain a node
993 * for the file path.
994 *
995 * @desc: block device descriptor or NULL
996 * @part: partition number
997 * @path: file path on partition or ""
998 * Return: device path or NULL in case of an error
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400999 */
1000struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
1001 const char *path)
1002{
1003 struct efi_device_path_file_path *fp;
1004 void *buf, *start;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001005 size_t dpsize = 0, fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001006
1007 if (desc)
1008 dpsize = dp_part_size(desc, part);
1009
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +02001010 fpsize = sizeof(struct efi_device_path) +
1011 2 * (utf8_utf16_strlen(path) + 1);
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001012 if (fpsize > U16_MAX)
1013 return NULL;
1014
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001015 dpsize += fpsize;
1016
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001017 start = buf = efi_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001018 if (!buf)
1019 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001020
1021 if (desc)
1022 buf = dp_part_fill(buf, desc, part);
1023
1024 /* add file-path: */
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +00001025 if (*path) {
1026 fp = buf;
1027 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
1028 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
1029 fp->dp.length = (u16)fpsize;
1030 path_to_uefi(fp->str, path);
1031 buf += fpsize;
1032 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001033
1034 *((struct efi_device_path *)buf) = END;
1035
1036 return start;
1037}
1038
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001039struct efi_device_path *efi_dp_from_uart(void)
1040{
1041 void *buf, *pos;
1042 struct efi_device_path_uart *uart;
1043 size_t dpsize = sizeof(ROOT) + sizeof(*uart) + sizeof(END);
1044
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001045 buf = efi_alloc(dpsize);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001046 if (!buf)
1047 return NULL;
1048 pos = buf;
1049 memcpy(pos, &ROOT, sizeof(ROOT));
1050 pos += sizeof(ROOT);
1051 uart = pos;
1052 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1053 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
1054 uart->dp.length = sizeof(*uart);
1055 pos += sizeof(*uart);
1056 memcpy(pos, &END, sizeof(END));
1057
1058 return buf;
1059}
1060
Jan Kiszkaf1389822022-10-14 18:10:06 +02001061#ifdef CONFIG_NETDEVICES
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001062struct efi_device_path *efi_dp_from_eth(void)
1063{
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001064 void *buf, *start;
1065 unsigned dpsize = 0;
1066
1067 assert(eth_get_dev());
1068
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001069 dpsize += dp_size(eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001070
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001071 start = buf = efi_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001072 if (!buf)
1073 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001074
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001075 buf = dp_fill(buf, eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001076
1077 *((struct efi_device_path *)buf) = END;
1078
1079 return start;
1080}
1081#endif
1082
Rob Clark18ceba72017-10-10 08:23:06 -04001083/* Construct a device-path for memory-mapped image */
1084struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1085 uint64_t start_address,
1086 uint64_t end_address)
1087{
1088 struct efi_device_path_memory *mdp;
1089 void *buf, *start;
1090
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001091 start = buf = efi_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001092 if (!buf)
1093 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -04001094
1095 mdp = buf;
1096 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1097 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1098 mdp->dp.length = sizeof(*mdp);
1099 mdp->memory_type = memory_type;
1100 mdp->start_address = start_address;
1101 mdp->end_address = end_address;
1102 buf = &mdp[1];
1103
1104 *((struct efi_device_path *)buf) = END;
1105
1106 return start;
1107}
1108
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001109/**
1110 * efi_dp_split_file_path() - split of relative file path from device path
1111 *
1112 * Given a device path indicating a file on a device, separate the device
1113 * path in two: the device path of the actual device and the file path
1114 * relative to this device.
1115 *
1116 * @full_path: device path including device and file path
1117 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001118 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001119 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001120 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001121efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1122 struct efi_device_path **device_path,
1123 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001124{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001125 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001126
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001127 *device_path = NULL;
1128 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001129 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001130 if (!dp)
1131 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001132 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001133 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001134 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001135 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001136 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001137 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001138 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001139 if (!fp)
1140 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001141 p->type = DEVICE_PATH_TYPE_END;
1142 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1143 p->length = sizeof(*p);
1144
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001145out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001146 *device_path = dp;
1147 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001148 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001149}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001150
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001151/**
1152 * efi_dp_from_name() - convert U-Boot device and file path to device path
1153 *
1154 * @dev: U-Boot device, e.g. 'mmc'
1155 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1156 * @path: file path relative to U-Boot device, may be NULL
1157 * @device: pointer to receive device path of the device
1158 * @file: pointer to receive device path for the file
1159 * Return: status code
1160 */
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001161efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1162 const char *path,
1163 struct efi_device_path **device,
1164 struct efi_device_path **file)
1165{
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001166 struct blk_desc *desc = NULL;
Simon Glassc1c4a8f2020-05-10 11:39:57 -06001167 struct disk_partition fs_partition;
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001168 size_t image_size;
1169 void *image_addr;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001170 int part = 0;
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001171 char *filename;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001172 char *s;
1173
AKASHI Takahiro39844412018-11-05 18:06:40 +09001174 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001175 return EFI_INVALID_PARAMETER;
1176
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001177 if (!strcmp(dev, "Net")) {
Jan Kiszkaf1389822022-10-14 18:10:06 +02001178#ifdef CONFIG_NETDEVICES
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001179 if (device)
1180 *device = efi_dp_from_eth();
1181#endif
1182 } else if (!strcmp(dev, "Uart")) {
1183 if (device)
1184 *device = efi_dp_from_uart();
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001185 } else if (!strcmp(dev, "Mem")) {
1186 efi_get_image_parameters(&image_addr, &image_size);
1187
1188 if (device)
1189 *device = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
1190 (uintptr_t)image_addr,
1191 image_size);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001192 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001193 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1194 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001195 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001196 return EFI_INVALID_PARAMETER;
1197
AKASHI Takahiro39844412018-11-05 18:06:40 +09001198 if (device)
1199 *device = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001200 }
1201
1202 if (!path)
1203 return EFI_SUCCESS;
1204
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001205 filename = calloc(1, strlen(path) + 1);
1206 if (!filename)
1207 return EFI_OUT_OF_RESOURCES;
1208
1209 sprintf(filename, "%s", path);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001210 /* DOS style file path: */
1211 s = filename;
1212 while ((s = strchr(s, '/')))
1213 *s++ = '\\';
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001214 *file = efi_dp_from_file(desc, part, filename);
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001215 free(filename);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001216
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001217 if (!*file)
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001218 return EFI_INVALID_PARAMETER;
1219
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001220 return EFI_SUCCESS;
1221}
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001222
1223/**
1224 * efi_dp_check_length() - check length of a device path
1225 *
1226 * @dp: pointer to device path
1227 * @maxlen: maximum length of the device path
1228 * Return:
1229 * * length of the device path if it is less or equal @maxlen
1230 * * -1 if the device path is longer then @maxlen
1231 * * -1 if a device path node has a length of less than 4
1232 * * -EINVAL if maxlen exceeds SSIZE_MAX
1233 */
1234ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1235 const size_t maxlen)
1236{
1237 ssize_t ret = 0;
1238 u16 len;
1239
1240 if (maxlen > SSIZE_MAX)
1241 return -EINVAL;
1242 for (;;) {
1243 len = dp->length;
1244 if (len < 4)
1245 return -1;
1246 ret += len;
1247 if (ret > maxlen)
1248 return -1;
1249 if (dp->type == DEVICE_PATH_TYPE_END &&
1250 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1251 return ret;
1252 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1253 }
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001254}
1255
1256/**
1257 * efi_dp_from_lo() - Get the instance of a VenMedia node in a
1258 * multi-instance device path that matches
1259 * a specific GUID. This kind of device paths
1260 * is found in Boot#### options describing an
1261 * initrd location
1262 *
1263 * @lo: EFI_LOAD_OPTION containing a valid device path
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001264 * @guid: guid to search for
1265 *
1266 * Return:
1267 * device path including the VenMedia node or NULL.
1268 * Caller must free the returned value.
1269 */
1270struct
1271efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001272 const efi_guid_t *guid)
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001273{
1274 struct efi_device_path *fp = lo->file_path;
1275 struct efi_device_path_vendor *vendor;
1276 int lo_len = lo->file_path_length;
1277
1278 for (; lo_len >= sizeof(struct efi_device_path);
1279 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1280 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1281 break;
1282 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1283 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1284 continue;
1285
1286 vendor = (struct efi_device_path_vendor *)fp;
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001287 if (!guidcmp(&vendor->guid, guid))
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +02001288 return efi_dp_dup(efi_dp_next(fp));
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001289 }
1290 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1291
1292 return NULL;
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001293}
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001294
1295/**
1296 * search_gpt_dp_node() - search gpt device path node
1297 *
1298 * @device_path: device path
1299 *
1300 * Return: pointer to the gpt device path node
1301 */
1302struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1303{
1304 struct efi_device_path *dp = device_path;
1305
1306 while (dp) {
1307 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1308 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1309 struct efi_device_path_hard_drive_path *hd_dp =
1310 (struct efi_device_path_hard_drive_path *)dp;
1311
1312 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1313 hd_dp->signature_type == SIG_TYPE_GUID)
1314 return dp;
1315 }
1316 dp = efi_dp_next(dp);
1317 }
1318
1319 return NULL;
1320}