blob: 3b267b713e98750748aa6497ae26008b9bdf3882 [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 -040066static void *dp_alloc(size_t sz)
67{
68 void *buf;
69
Heinrich Schuchardt16546012021-08-17 15:15:23 +020070 if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, sz, &buf) !=
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010071 EFI_SUCCESS) {
72 debug("EFI: ERROR: out of memory in %s\n", __func__);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040073 return NULL;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010074 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -040075
Patrick Wildtacf7bee2018-03-25 19:54:03 +020076 memset(buf, 0, sz);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040077 return buf;
78}
79
80/*
81 * Iterate to next block in device-path, terminating (returning NULL)
82 * at /End* node.
83 */
84struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
85{
86 if (dp == NULL)
87 return NULL;
88 if (dp->type == DEVICE_PATH_TYPE_END)
89 return NULL;
90 dp = ((void *)dp) + dp->length;
91 if (dp->type == DEVICE_PATH_TYPE_END)
92 return NULL;
93 return (struct efi_device_path *)dp;
94}
95
96/*
97 * Compare two device-paths, stopping when the shorter of the two hits
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +020098 * an End* node. This is useful to, for example, compare a device-path
Rob Clarkf90cb9f2017-09-13 18:05:28 -040099 * representing a device with one representing a file on the device, or
100 * a device with a parent device.
101 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +0200102int efi_dp_match(const struct efi_device_path *a,
103 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400104{
105 while (1) {
106 int ret;
107
108 ret = memcmp(&a->length, &b->length, sizeof(a->length));
109 if (ret)
110 return ret;
111
112 ret = memcmp(a, b, a->length);
113 if (ret)
114 return ret;
115
116 a = efi_dp_next(a);
117 b = efi_dp_next(b);
118
119 if (!a || !b)
120 return 0;
121 }
122}
123
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100124/**
125 * efi_dp_shorten() - shorten device-path
126 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200127 * We can have device paths that start with a USB WWID or a USB Class node,
128 * and a few other cases which don't encode the full device path with bus
129 * hierarchy:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400130 *
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100131 * * MESSAGING:USB_WWID
132 * * MESSAGING:USB_CLASS
133 * * MEDIA:FILE_PATH
134 * * MEDIA:HARD_DRIVE
135 * * MESSAGING:URI
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200136 *
137 * See UEFI spec (section 3.1.2, about short-form device-paths)
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100138 *
Heinrich Schuchardtdb17dbc2022-03-21 08:26:48 +0100139 * @dp: original device-path
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100140 * @Return: shortened device-path or NULL
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400141 */
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100142struct efi_device_path *efi_dp_shorten(struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400143{
144 while (dp) {
145 /*
146 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
147 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
148 * so not sure when we would see these other cases.
149 */
150 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
151 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
152 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
153 return dp;
154
155 dp = efi_dp_next(dp);
156 }
157
158 return dp;
159}
160
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100161/**
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100162 * find_handle() - find handle by device path and installed protocol
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100163 *
164 * If @rem is provided, the handle with the longest partial match is returned.
165 *
166 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100167 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100168 * @short_path: use short form device path for matching
169 * @rem: pointer to receive remaining device path
170 * Return: matching handle
171 */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100172static efi_handle_t find_handle(struct efi_device_path *dp,
173 const efi_guid_t *guid, bool short_path,
174 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400175{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100176 efi_handle_t handle, best_handle = NULL;
177 efi_uintn_t len, best_len = 0;
178
179 len = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400180
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100181 list_for_each_entry(handle, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100182 struct efi_handler *handler;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100183 struct efi_device_path *dp_current;
184 efi_uintn_t len_current;
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100185 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400186
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100187 if (guid) {
188 ret = efi_search_protocol(handle, guid, &handler);
189 if (ret != EFI_SUCCESS)
190 continue;
191 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100192 ret = efi_search_protocol(handle, &efi_guid_device_path,
193 &handler);
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100194 if (ret != EFI_SUCCESS)
195 continue;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100196 dp_current = handler->protocol_interface;
197 if (short_path) {
198 dp_current = efi_dp_shorten(dp_current);
199 if (!dp_current)
200 continue;
201 }
202 len_current = efi_dp_instance_size(dp_current);
203 if (rem) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100204 if (len_current > len)
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100205 continue;
206 } else {
207 if (len_current != len)
208 continue;
209 }
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100210 if (memcmp(dp_current, dp, len_current))
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100211 continue;
212 if (!rem)
213 return handle;
214 if (len_current > best_len) {
215 best_len = len_current;
216 best_handle = handle;
217 *rem = (void*)((u8 *)dp + len_current);
218 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400219 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100220 return best_handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400221}
222
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100223/**
224 * efi_dp_find_obj() - find handle by device path
225 *
226 * If @rem is provided, the handle with the longest partial match is returned.
227 *
228 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100229 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100230 * @rem: pointer to receive remaining device path
231 * Return: matching handle
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400232 */
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100233efi_handle_t efi_dp_find_obj(struct efi_device_path *dp,
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100234 const efi_guid_t *guid,
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100235 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400236{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100237 efi_handle_t handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400238
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100239 handle = find_handle(dp, guid, false, rem);
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100240 if (!handle)
241 /* Match short form device path */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100242 handle = find_handle(dp, guid, true, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400243
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100244 return handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400245}
246
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100247/*
248 * Determine the last device path node that is not the end node.
249 *
250 * @dp device path
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100251 * Return: last node before the end node if it exists
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100252 * otherwise NULL
253 */
254const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
255{
256 struct efi_device_path *ret;
257
258 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
259 return NULL;
260 while (dp) {
261 ret = (struct efi_device_path *)dp;
262 dp = efi_dp_next(dp);
263 }
264 return ret;
265}
266
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200267/* get size of the first device path instance excluding end node */
268efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400269{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200270 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400271
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200272 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
273 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400274 while (dp) {
275 sz += dp->length;
276 dp = efi_dp_next(dp);
277 }
278
279 return sz;
280}
281
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200282/* get size of multi-instance device path excluding end node */
283efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
284{
285 const struct efi_device_path *p = dp;
286
287 if (!p)
288 return 0;
289 while (p->type != DEVICE_PATH_TYPE_END ||
290 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
291 p = (void *)p + p->length;
292
293 return (void *)p - (void *)dp;
294}
295
296/* copy multi-instance device path */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400297struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
298{
299 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200300 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400301
302 if (!dp)
303 return NULL;
304
305 ndp = dp_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100306 if (!ndp)
307 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400308 memcpy(ndp, dp, sz);
309
310 return ndp;
311}
312
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200313/**
314 * efi_dp_append_or_concatenate() - Append or concatenate two device paths.
315 * Concatenated device path will be separated
316 * by a sub-type 0xff end node
317 *
318 * @dp1: First device path
319 * @dp2: Second device path
320 * @concat: If true the two device paths will be concatenated and separated
321 * by an end of entrire device path sub-type 0xff end node.
322 * If true the second device path will be appended to the first and
323 * terminated by an end node
324 *
325 * Return:
326 * concatenated device path or NULL. Caller must free the returned value
327 */
328static struct
329efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
330 const struct efi_device_path *dp2,
331 bool concat)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400332{
333 struct efi_device_path *ret;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200334 size_t end_size = sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400335
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200336 if (concat)
337 end_size = 2 * sizeof(END);
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200338 if (!dp1 && !dp2) {
339 /* return an end node */
340 ret = efi_dp_dup(&END);
341 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400342 ret = efi_dp_dup(dp2);
343 } else if (!dp2) {
344 ret = efi_dp_dup(dp1);
345 } else {
346 /* both dp1 and dp2 are non-null */
347 unsigned sz1 = efi_dp_size(dp1);
348 unsigned sz2 = efi_dp_size(dp2);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200349 void *p = dp_alloc(sz1 + sz2 + end_size);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100350 if (!p)
351 return NULL;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200352 ret = p;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400353 memcpy(p, dp1, sz1);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200354 p += sz1;
355
356 if (concat) {
357 memcpy(p, &END, sizeof(END));
358 p += sizeof(END);
359 }
360
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200361 /* the end node of the second device path has to be retained */
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200362 memcpy(p, dp2, sz2);
363 p += sz2;
364 memcpy(p, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400365 }
366
367 return ret;
368}
369
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200370/**
371 * efi_dp_append() - Append a device to an existing device path.
372 *
373 * @dp1: First device path
374 * @dp2: Second device path
375 *
376 * Return:
377 * concatenated device path or NULL. Caller must free the returned value
378 */
379struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
380 const struct efi_device_path *dp2)
381{
382 return efi_dp_append_or_concatenate(dp1, dp2, false);
383}
384
385/**
386 * efi_dp_concat() - Concatenate 2 device paths. The final device path will
387 * contain two device paths separated by and end node (0xff).
388 *
389 * @dp1: First device path
390 * @dp2: Second device path
391 *
392 * Return:
393 * concatenated device path or NULL. Caller must free the returned value
394 */
395struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
396 const struct efi_device_path *dp2)
397{
398 return efi_dp_append_or_concatenate(dp1, dp2, true);
399}
400
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400401struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
402 const struct efi_device_path *node)
403{
404 struct efi_device_path *ret;
405
406 if (!node && !dp) {
407 ret = efi_dp_dup(&END);
408 } else if (!node) {
409 ret = efi_dp_dup(dp);
410 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200411 size_t sz = node->length;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400412 void *p = dp_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100413 if (!p)
414 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400415 memcpy(p, node, sz);
416 memcpy(p + sz, &END, sizeof(END));
417 ret = p;
418 } else {
419 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200420 size_t sz = efi_dp_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400421 void *p = dp_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100422 if (!p)
423 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400424 memcpy(p, dp, sz);
425 memcpy(p + sz, node, node->length);
426 memcpy(p + sz + node->length, &END, sizeof(END));
427 ret = p;
428 }
429
430 return ret;
431}
432
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200433struct efi_device_path *efi_dp_create_device_node(const u8 type,
434 const u8 sub_type,
435 const u16 length)
436{
437 struct efi_device_path *ret;
438
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200439 if (length < sizeof(struct efi_device_path))
440 return NULL;
441
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200442 ret = dp_alloc(length);
443 if (!ret)
444 return ret;
445 ret->type = type;
446 ret->sub_type = sub_type;
447 ret->length = length;
448 return ret;
449}
450
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200451struct efi_device_path *efi_dp_append_instance(
452 const struct efi_device_path *dp,
453 const struct efi_device_path *dpi)
454{
455 size_t sz, szi;
456 struct efi_device_path *p, *ret;
457
458 if (!dpi)
459 return NULL;
460 if (!dp)
461 return efi_dp_dup(dpi);
462 sz = efi_dp_size(dp);
463 szi = efi_dp_instance_size(dpi);
464 p = dp_alloc(sz + szi + 2 * sizeof(END));
465 if (!p)
466 return NULL;
467 ret = p;
468 memcpy(p, dp, sz + sizeof(END));
469 p = (void *)p + sz;
470 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
471 p = (void *)p + sizeof(END);
472 memcpy(p, dpi, szi);
473 p = (void *)p + szi;
474 memcpy(p, &END, sizeof(END));
475 return ret;
476}
477
478struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
479 efi_uintn_t *size)
480{
481 size_t sz;
482 struct efi_device_path *p;
483
484 if (size)
485 *size = 0;
486 if (!dp || !*dp)
487 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200488 sz = efi_dp_instance_size(*dp);
489 p = dp_alloc(sz + sizeof(END));
490 if (!p)
491 return NULL;
492 memcpy(p, *dp, sz + sizeof(END));
493 *dp = (void *)*dp + sz;
494 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
495 *dp = (void *)*dp + sizeof(END);
496 else
497 *dp = NULL;
498 if (size)
499 *size = sz + sizeof(END);
500 return p;
501}
502
503bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
504{
505 const struct efi_device_path *p = dp;
506
507 if (!p)
508 return false;
509 while (p->type != DEVICE_PATH_TYPE_END)
510 p = (void *)p + p->length;
511 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
512}
513
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400514/* size of device-path not including END node for device and all parents
515 * up to the root device.
516 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100517__maybe_unused static unsigned int dp_size(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400518{
519 if (!dev || !dev->driver)
520 return sizeof(ROOT);
521
Simon Glass56ada7b2022-01-29 14:58:38 -0700522 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400523 case UCLASS_ROOT:
524 case UCLASS_SIMPLE_BUS:
525 /* stop traversing parents at this point: */
526 return sizeof(ROOT);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100527 case UCLASS_ETH:
528 return dp_size(dev->parent) +
529 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100530 case UCLASS_BLK:
531 switch (dev->parent->uclass->uc_drv->id) {
532#ifdef CONFIG_IDE
533 case UCLASS_IDE:
534 return dp_size(dev->parent) +
535 sizeof(struct efi_device_path_atapi);
536#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600537#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100538 case UCLASS_SCSI:
539 return dp_size(dev->parent) +
540 sizeof(struct efi_device_path_scsi);
541#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600542#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100543 case UCLASS_MMC:
544 return dp_size(dev->parent) +
545 sizeof(struct efi_device_path_sd_mmc_path);
546#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200547#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
548 case UCLASS_AHCI:
549 return dp_size(dev->parent) +
550 sizeof(struct efi_device_path_sata);
551#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200552#if defined(CONFIG_NVME)
553 case UCLASS_NVME:
554 return dp_size(dev->parent) +
555 sizeof(struct efi_device_path_nvme);
556#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900557#ifdef CONFIG_SANDBOX
Simon Glasse57f8d42022-10-29 19:47:17 -0600558 case UCLASS_HOST:
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900559 /*
560 * Sandbox's host device will be represented
561 * as vendor device with extra one byte for
562 * device number
563 */
564 return dp_size(dev->parent)
565 + sizeof(struct efi_device_path_vendor) + 1;
566#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200567#ifdef CONFIG_VIRTIO_BLK
568 case UCLASS_VIRTIO:
569 /*
570 * Virtio devices will be represented as a vendor
571 * device node with an extra byte for the device
572 * number.
573 */
574 return dp_size(dev->parent)
575 + sizeof(struct efi_device_path_vendor) + 1;
576#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100577 default:
578 return dp_size(dev->parent);
579 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600580#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400581 case UCLASS_MMC:
582 return dp_size(dev->parent) +
583 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100584#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400585 case UCLASS_MASS_STORAGE:
586 case UCLASS_USB_HUB:
587 return dp_size(dev->parent) +
588 sizeof(struct efi_device_path_usb_class);
589 default:
590 /* just skip over unknown classes: */
591 return dp_size(dev->parent);
592 }
593}
594
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100595/*
596 * Recursively build a device path.
597 *
598 * @buf pointer to the end of the device path
599 * @dev device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100600 * Return: pointer to the end of the device path
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100601 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100602__maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400603{
604 if (!dev || !dev->driver)
605 return buf;
606
Simon Glass56ada7b2022-01-29 14:58:38 -0700607 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400608 case UCLASS_ROOT:
609 case UCLASS_SIMPLE_BUS: {
610 /* stop traversing parents at this point: */
611 struct efi_device_path_vendor *vdp = buf;
612 *vdp = ROOT;
613 return &vdp[1];
614 }
Jan Kiszkaf1389822022-10-14 18:10:06 +0200615#ifdef CONFIG_NETDEVICES
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100616 case UCLASS_ETH: {
617 struct efi_device_path_mac_addr *dp =
618 dp_fill(buf, dev->parent);
Simon Glass95588622020-12-22 19:30:28 -0700619 struct eth_pdata *pdata = dev_get_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100620
621 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
622 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
623 dp->dp.length = sizeof(*dp);
624 memset(&dp->mac, 0, sizeof(dp->mac));
625 /* We only support IPv4 */
626 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
627 /* Ethernet */
628 dp->if_type = 1;
629 return &dp[1];
630 }
631#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100632 case UCLASS_BLK:
633 switch (dev->parent->uclass->uc_drv->id) {
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900634#ifdef CONFIG_SANDBOX
Simon Glasse57f8d42022-10-29 19:47:17 -0600635 case UCLASS_HOST: {
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900636 /* stop traversing parents at this point: */
Heinrich Schuchardt1e3beaf2020-05-06 01:28:08 +0200637 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700638 struct blk_desc *desc = dev_get_uclass_plat(dev);
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900639
640 dp_fill(buf, dev->parent);
641 dp = buf;
642 ++dp;
643 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
644 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
645 dp->dp.length = sizeof(*dp) + 1;
646 memcpy(&dp->guid, &efi_guid_host_dev,
647 sizeof(efi_guid_t));
648 dp->vendor_data[0] = desc->devnum;
649 return &dp->vendor_data[1];
650 }
651#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200652#ifdef CONFIG_VIRTIO_BLK
653 case UCLASS_VIRTIO: {
654 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700655 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200656
657 dp_fill(buf, dev->parent);
658 dp = buf;
659 ++dp;
660 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
661 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
662 dp->dp.length = sizeof(*dp) + 1;
663 memcpy(&dp->guid, &efi_guid_virtio_dev,
664 sizeof(efi_guid_t));
665 dp->vendor_data[0] = desc->devnum;
666 return &dp->vendor_data[1];
667 }
668#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100669#ifdef CONFIG_IDE
670 case UCLASS_IDE: {
671 struct efi_device_path_atapi *dp =
672 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700673 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100674
675 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
676 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
677 dp->dp.length = sizeof(*dp);
678 dp->logical_unit_number = desc->devnum;
679 dp->primary_secondary = IDE_BUS(desc->devnum);
680 dp->slave_master = desc->devnum %
681 (CONFIG_SYS_IDE_MAXDEVICE /
682 CONFIG_SYS_IDE_MAXBUS);
683 return &dp[1];
684 }
685#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600686#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100687 case UCLASS_SCSI: {
688 struct efi_device_path_scsi *dp =
689 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700690 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100691
692 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
693 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
694 dp->dp.length = sizeof(*dp);
695 dp->logical_unit_number = desc->lun;
696 dp->target_id = desc->target;
697 return &dp[1];
698 }
699#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600700#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100701 case UCLASS_MMC: {
702 struct efi_device_path_sd_mmc_path *sddp =
703 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700704 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100705
706 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
707 sddp->dp.sub_type = is_sd(desc) ?
708 DEVICE_PATH_SUB_TYPE_MSG_SD :
709 DEVICE_PATH_SUB_TYPE_MSG_MMC;
710 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700711 sddp->slot_number = dev_seq(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100712 return &sddp[1];
713 }
714#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200715#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
716 case UCLASS_AHCI: {
717 struct efi_device_path_sata *dp =
718 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700719 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200720
721 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
722 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
723 dp->dp.length = sizeof(*dp);
724 dp->hba_port = desc->devnum;
725 /* default 0xffff implies no port multiplier */
726 dp->port_multiplier_port = 0xffff;
727 dp->logical_unit_number = desc->lun;
728 return &dp[1];
729 }
730#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200731#if defined(CONFIG_NVME)
732 case UCLASS_NVME: {
733 struct efi_device_path_nvme *dp =
734 dp_fill(buf, dev->parent);
735 u32 ns_id;
736
737 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
738 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
739 dp->dp.length = sizeof(*dp);
740 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
741 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
742 return &dp[1];
743 }
744#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100745 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100746 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
747 __FILE__, __LINE__, __func__,
748 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100749 return dp_fill(buf, dev->parent);
750 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600751#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400752 case UCLASS_MMC: {
753 struct efi_device_path_sd_mmc_path *sddp =
754 dp_fill(buf, dev->parent);
755 struct mmc *mmc = mmc_get_mmc_dev(dev);
756 struct blk_desc *desc = mmc_get_blk_desc(mmc);
757
758 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100759 sddp->dp.sub_type = is_sd(desc) ?
760 DEVICE_PATH_SUB_TYPE_MSG_SD :
761 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400762 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700763 sddp->slot_number = dev_seq(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400764
765 return &sddp[1];
766 }
767#endif
768 case UCLASS_MASS_STORAGE:
769 case UCLASS_USB_HUB: {
770 struct efi_device_path_usb_class *udp =
771 dp_fill(buf, dev->parent);
772 struct usb_device *udev = dev_get_parent_priv(dev);
773 struct usb_device_descriptor *desc = &udev->descriptor;
774
775 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
776 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
777 udp->dp.length = sizeof(*udp);
778 udp->vendor_id = desc->idVendor;
779 udp->product_id = desc->idProduct;
780 udp->device_class = desc->bDeviceClass;
781 udp->device_subclass = desc->bDeviceSubClass;
782 udp->device_protocol = desc->bDeviceProtocol;
783
784 return &udp[1];
785 }
786 default:
Simon Glass56ada7b2022-01-29 14:58:38 -0700787 /* If the uclass driver is missing, this will show NULL */
788 log_debug("unhandled device class: %s (%s)\n", dev->name,
789 dev_get_uclass_name(dev));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400790 return dp_fill(buf, dev->parent);
791 }
792}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400793
794static unsigned dp_part_size(struct blk_desc *desc, int part)
795{
796 unsigned dpsize;
Simon Glassec209a72022-01-29 14:58:39 -0700797 struct udevice *dev = desc->bdev;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400798
Simon Glass222f3cb2021-09-24 18:30:17 -0600799 dpsize = dp_size(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400800
801 if (part == 0) /* the actual disk, not a partition */
802 return dpsize;
803
804 if (desc->part_type == PART_TYPE_ISO)
805 dpsize += sizeof(struct efi_device_path_cdrom_path);
806 else
807 dpsize += sizeof(struct efi_device_path_hard_drive_path);
808
809 return dpsize;
810}
811
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100812/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100813 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100814 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200815 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100816 * @desc block device descriptor
817 * @part partition number, 0 identifies a block device
818 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100819static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400820{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600821 struct disk_partition info;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400822
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400823 part_get_info(desc, part, &info);
824
825 if (desc->part_type == PART_TYPE_ISO) {
826 struct efi_device_path_cdrom_path *cddp = buf;
827
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100828 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400829 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
830 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
831 cddp->dp.length = sizeof(*cddp);
832 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200833 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400834
835 buf = &cddp[1];
836 } else {
837 struct efi_device_path_hard_drive_path *hddp = buf;
838
839 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
840 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
841 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100842 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400843 hddp->partition_start = info.start;
844 hddp->partition_end = info.size;
845 if (desc->part_type == PART_TYPE_EFI)
846 hddp->partmap_type = 2;
847 else
848 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100849
850 switch (desc->sig_type) {
851 case SIG_TYPE_NONE:
852 default:
853 hddp->signature_type = 0;
854 memset(hddp->partition_signature, 0,
855 sizeof(hddp->partition_signature));
856 break;
857 case SIG_TYPE_MBR:
858 hddp->signature_type = 1;
859 memset(hddp->partition_signature, 0,
860 sizeof(hddp->partition_signature));
861 memcpy(hddp->partition_signature, &desc->mbr_sig,
862 sizeof(desc->mbr_sig));
863 break;
864 case SIG_TYPE_GUID:
865 hddp->signature_type = 2;
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900866#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
867 /* info.uuid exists only with PARTITION_UUIDS */
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200868 if (uuid_str_to_bin(info.uuid,
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900869 hddp->partition_signature,
870 UUID_STR_FORMAT_GUID)) {
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200871 log_warning(
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900872 "Partition %d: invalid GUID %s\n",
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200873 part, info.uuid);
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900874 }
875#endif
Jonathan Gray84b4d702017-11-22 14:18:59 +1100876 break;
877 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400878
879 buf = &hddp[1];
880 }
881
882 return buf;
883}
884
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100885/*
886 * Create a device path for a block device or one of its partitions.
887 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200888 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100889 * @desc block device descriptor
890 * @part partition number, 0 identifies a block device
891 */
892static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
893{
Simon Glassec209a72022-01-29 14:58:39 -0700894 struct udevice *dev = desc->bdev;
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100895
Simon Glass222f3cb2021-09-24 18:30:17 -0600896 buf = dp_fill(buf, dev);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100897
898 if (part == 0) /* the actual disk, not a partition */
899 return buf;
900
901 return dp_part_node(buf, desc, part);
902}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400903
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200904/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400905struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
906{
907 void *buf, *start;
908
909 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100910 if (!buf)
911 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400912
913 buf = dp_part_fill(buf, desc, part);
914
915 *((struct efi_device_path *)buf) = END;
916
917 return start;
918}
919
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100920/*
921 * Create a device node for a block device partition.
922 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200923 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100924 * @desc block device descriptor
925 * @part partition number, 0 identifies a block device
926 */
927struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
928{
929 efi_uintn_t dpsize;
930 void *buf;
931
932 if (desc->part_type == PART_TYPE_ISO)
933 dpsize = sizeof(struct efi_device_path_cdrom_path);
934 else
935 dpsize = sizeof(struct efi_device_path_hard_drive_path);
936 buf = dp_alloc(dpsize);
937
Heinrich Schuchardte29fbb32022-10-06 13:36:02 +0200938 if (buf)
939 dp_part_node(buf, desc, part);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100940
941 return buf;
942}
943
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200944/**
945 * path_to_uefi() - convert UTF-8 path to an UEFI style path
946 *
947 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
948 * separators and UTF-16).
949 *
950 * @src: source buffer
951 * @uefi: target buffer, possibly unaligned
952 */
953static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400954{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200955 u16 *pos = uefi;
956
957 /*
958 * efi_set_bootdev() calls this routine indirectly before the UEFI
959 * subsystem is initialized. So we cannot assume unaligned access to be
960 * enabled.
961 */
962 allow_unaligned();
963
964 while (*src) {
965 s32 code = utf8_get(&src);
966
967 if (code < 0)
968 code = '?';
969 else if (code == '/')
970 code = '\\';
971 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400972 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200973 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400974}
975
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000976/**
977 * efi_dp_from_file() - create device path for file
978 *
979 * The function creates a device path from the block descriptor @desc and the
980 * partition number @part and appends a device path node created describing the
981 * file path @path.
982 *
983 * If @desc is NULL, the device path will not contain nodes describing the
984 * partition.
985 * If @path is an empty string "", the device path will not contain a node
986 * for the file path.
987 *
988 * @desc: block device descriptor or NULL
989 * @part: partition number
990 * @path: file path on partition or ""
991 * Return: device path or NULL in case of an error
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400992 */
993struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
994 const char *path)
995{
996 struct efi_device_path_file_path *fp;
997 void *buf, *start;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900998 size_t dpsize = 0, fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400999
1000 if (desc)
1001 dpsize = dp_part_size(desc, part);
1002
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +02001003 fpsize = sizeof(struct efi_device_path) +
1004 2 * (utf8_utf16_strlen(path) + 1);
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001005 if (fpsize > U16_MAX)
1006 return NULL;
1007
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001008 dpsize += fpsize;
1009
1010 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001011 if (!buf)
1012 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001013
1014 if (desc)
1015 buf = dp_part_fill(buf, desc, part);
1016
1017 /* add file-path: */
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +00001018 if (*path) {
1019 fp = buf;
1020 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
1021 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
1022 fp->dp.length = (u16)fpsize;
1023 path_to_uefi(fp->str, path);
1024 buf += fpsize;
1025 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001026
1027 *((struct efi_device_path *)buf) = END;
1028
1029 return start;
1030}
1031
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001032struct efi_device_path *efi_dp_from_uart(void)
1033{
1034 void *buf, *pos;
1035 struct efi_device_path_uart *uart;
1036 size_t dpsize = sizeof(ROOT) + sizeof(*uart) + sizeof(END);
1037
1038 buf = dp_alloc(dpsize);
1039 if (!buf)
1040 return NULL;
1041 pos = buf;
1042 memcpy(pos, &ROOT, sizeof(ROOT));
1043 pos += sizeof(ROOT);
1044 uart = pos;
1045 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1046 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
1047 uart->dp.length = sizeof(*uart);
1048 pos += sizeof(*uart);
1049 memcpy(pos, &END, sizeof(END));
1050
1051 return buf;
1052}
1053
Jan Kiszkaf1389822022-10-14 18:10:06 +02001054#ifdef CONFIG_NETDEVICES
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001055struct efi_device_path *efi_dp_from_eth(void)
1056{
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001057 void *buf, *start;
1058 unsigned dpsize = 0;
1059
1060 assert(eth_get_dev());
1061
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001062 dpsize += dp_size(eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001063
1064 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001065 if (!buf)
1066 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001067
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001068 buf = dp_fill(buf, eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001069
1070 *((struct efi_device_path *)buf) = END;
1071
1072 return start;
1073}
1074#endif
1075
Rob Clark18ceba72017-10-10 08:23:06 -04001076/* Construct a device-path for memory-mapped image */
1077struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1078 uint64_t start_address,
1079 uint64_t end_address)
1080{
1081 struct efi_device_path_memory *mdp;
1082 void *buf, *start;
1083
1084 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001085 if (!buf)
1086 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -04001087
1088 mdp = buf;
1089 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1090 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1091 mdp->dp.length = sizeof(*mdp);
1092 mdp->memory_type = memory_type;
1093 mdp->start_address = start_address;
1094 mdp->end_address = end_address;
1095 buf = &mdp[1];
1096
1097 *((struct efi_device_path *)buf) = END;
1098
1099 return start;
1100}
1101
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001102/**
1103 * efi_dp_split_file_path() - split of relative file path from device path
1104 *
1105 * Given a device path indicating a file on a device, separate the device
1106 * path in two: the device path of the actual device and the file path
1107 * relative to this device.
1108 *
1109 * @full_path: device path including device and file path
1110 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001111 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001112 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001113 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001114efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1115 struct efi_device_path **device_path,
1116 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001117{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001118 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001119
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001120 *device_path = NULL;
1121 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001122 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001123 if (!dp)
1124 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001125 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001126 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001127 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001128 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001129 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001130 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001131 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001132 if (!fp)
1133 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001134 p->type = DEVICE_PATH_TYPE_END;
1135 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1136 p->length = sizeof(*p);
1137
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001138out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001139 *device_path = dp;
1140 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001141 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001142}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001143
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001144/**
1145 * efi_dp_from_name() - convert U-Boot device and file path to device path
1146 *
1147 * @dev: U-Boot device, e.g. 'mmc'
1148 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1149 * @path: file path relative to U-Boot device, may be NULL
1150 * @device: pointer to receive device path of the device
1151 * @file: pointer to receive device path for the file
1152 * Return: status code
1153 */
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001154efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1155 const char *path,
1156 struct efi_device_path **device,
1157 struct efi_device_path **file)
1158{
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001159 struct blk_desc *desc = NULL;
Simon Glassc1c4a8f2020-05-10 11:39:57 -06001160 struct disk_partition fs_partition;
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001161 size_t image_size;
1162 void *image_addr;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001163 int part = 0;
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001164 char *filename;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001165 char *s;
1166
AKASHI Takahiro39844412018-11-05 18:06:40 +09001167 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001168 return EFI_INVALID_PARAMETER;
1169
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001170 if (!strcmp(dev, "Net")) {
Jan Kiszkaf1389822022-10-14 18:10:06 +02001171#ifdef CONFIG_NETDEVICES
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001172 if (device)
1173 *device = efi_dp_from_eth();
1174#endif
1175 } else if (!strcmp(dev, "Uart")) {
1176 if (device)
1177 *device = efi_dp_from_uart();
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001178 } else if (!strcmp(dev, "Mem")) {
1179 efi_get_image_parameters(&image_addr, &image_size);
1180
1181 if (device)
1182 *device = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
1183 (uintptr_t)image_addr,
1184 image_size);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001185 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001186 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1187 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001188 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001189 return EFI_INVALID_PARAMETER;
1190
AKASHI Takahiro39844412018-11-05 18:06:40 +09001191 if (device)
1192 *device = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001193 }
1194
1195 if (!path)
1196 return EFI_SUCCESS;
1197
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001198 filename = calloc(1, strlen(path) + 1);
1199 if (!filename)
1200 return EFI_OUT_OF_RESOURCES;
1201
1202 sprintf(filename, "%s", path);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001203 /* DOS style file path: */
1204 s = filename;
1205 while ((s = strchr(s, '/')))
1206 *s++ = '\\';
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001207 *file = efi_dp_from_file(desc, part, filename);
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001208 free(filename);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001209
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001210 if (!*file)
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001211 return EFI_INVALID_PARAMETER;
1212
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001213 return EFI_SUCCESS;
1214}
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001215
1216/**
1217 * efi_dp_check_length() - check length of a device path
1218 *
1219 * @dp: pointer to device path
1220 * @maxlen: maximum length of the device path
1221 * Return:
1222 * * length of the device path if it is less or equal @maxlen
1223 * * -1 if the device path is longer then @maxlen
1224 * * -1 if a device path node has a length of less than 4
1225 * * -EINVAL if maxlen exceeds SSIZE_MAX
1226 */
1227ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1228 const size_t maxlen)
1229{
1230 ssize_t ret = 0;
1231 u16 len;
1232
1233 if (maxlen > SSIZE_MAX)
1234 return -EINVAL;
1235 for (;;) {
1236 len = dp->length;
1237 if (len < 4)
1238 return -1;
1239 ret += len;
1240 if (ret > maxlen)
1241 return -1;
1242 if (dp->type == DEVICE_PATH_TYPE_END &&
1243 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1244 return ret;
1245 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1246 }
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001247}
1248
1249/**
1250 * efi_dp_from_lo() - Get the instance of a VenMedia node in a
1251 * multi-instance device path that matches
1252 * a specific GUID. This kind of device paths
1253 * is found in Boot#### options describing an
1254 * initrd location
1255 *
1256 * @lo: EFI_LOAD_OPTION containing a valid device path
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001257 * @guid: guid to search for
1258 *
1259 * Return:
1260 * device path including the VenMedia node or NULL.
1261 * Caller must free the returned value.
1262 */
1263struct
1264efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001265 const efi_guid_t *guid)
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001266{
1267 struct efi_device_path *fp = lo->file_path;
1268 struct efi_device_path_vendor *vendor;
1269 int lo_len = lo->file_path_length;
1270
1271 for (; lo_len >= sizeof(struct efi_device_path);
1272 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1273 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1274 break;
1275 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1276 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1277 continue;
1278
1279 vendor = (struct efi_device_path_vendor *)fp;
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001280 if (!guidcmp(&vendor->guid, guid))
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +02001281 return efi_dp_dup(efi_dp_next(fp));
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001282 }
1283 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1284
1285 return NULL;
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001286}
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001287
1288/**
1289 * search_gpt_dp_node() - search gpt device path node
1290 *
1291 * @device_path: device path
1292 *
1293 * Return: pointer to the gpt device path node
1294 */
1295struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1296{
1297 struct efi_device_path *dp = device_path;
1298
1299 while (dp) {
1300 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1301 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1302 struct efi_device_path_hard_drive_path *hd_dp =
1303 (struct efi_device_path_hard_drive_path *)dp;
1304
1305 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1306 hd_dp->signature_type == SIG_TYPE_GUID)
1307 return dp;
1308 }
1309 dp = efi_dp_next(dp);
1310 }
1311
1312 return NULL;
1313}