blob: ea097839dbe326b2fe681ec16ec5f96ec9756aac [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 Schuchardta7ffd902023-03-26 12:22:40 +0200113 * When creating a short boot option we want to use a device-path that is
114 * independent of the location where the block device is plugged in.
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400115 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200116 * UsbWwi() nodes contain a serial number, hard drive paths a partition
117 * UUID. Both should be unique.
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200118 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200119 * See UEFI spec, section 3.1.2 for "short-form device path".
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100120 *
Heinrich Schuchardtdb17dbc2022-03-21 08:26:48 +0100121 * @dp: original device-path
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100122 * @Return: shortened device-path or NULL
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400123 */
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100124struct efi_device_path *efi_dp_shorten(struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400125{
126 while (dp) {
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200127 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_WWI) ||
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400128 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
129 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
130 return dp;
131
132 dp = efi_dp_next(dp);
133 }
134
135 return dp;
136}
137
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100138/**
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100139 * find_handle() - find handle by device path and installed protocol
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100140 *
141 * If @rem is provided, the handle with the longest partial match is returned.
142 *
143 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100144 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100145 * @short_path: use short form device path for matching
146 * @rem: pointer to receive remaining device path
147 * Return: matching handle
148 */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100149static efi_handle_t find_handle(struct efi_device_path *dp,
150 const efi_guid_t *guid, bool short_path,
151 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400152{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100153 efi_handle_t handle, best_handle = NULL;
154 efi_uintn_t len, best_len = 0;
155
156 len = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400157
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100158 list_for_each_entry(handle, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100159 struct efi_handler *handler;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100160 struct efi_device_path *dp_current;
161 efi_uintn_t len_current;
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100162 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400163
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100164 if (guid) {
165 ret = efi_search_protocol(handle, guid, &handler);
166 if (ret != EFI_SUCCESS)
167 continue;
168 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100169 ret = efi_search_protocol(handle, &efi_guid_device_path,
170 &handler);
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100171 if (ret != EFI_SUCCESS)
172 continue;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100173 dp_current = handler->protocol_interface;
174 if (short_path) {
175 dp_current = efi_dp_shorten(dp_current);
176 if (!dp_current)
177 continue;
178 }
179 len_current = efi_dp_instance_size(dp_current);
180 if (rem) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100181 if (len_current > len)
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100182 continue;
183 } else {
184 if (len_current != len)
185 continue;
186 }
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100187 if (memcmp(dp_current, dp, len_current))
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100188 continue;
189 if (!rem)
190 return handle;
191 if (len_current > best_len) {
192 best_len = len_current;
193 best_handle = handle;
194 *rem = (void*)((u8 *)dp + len_current);
195 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400196 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100197 return best_handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400198}
199
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100200/**
201 * efi_dp_find_obj() - find handle by device path
202 *
203 * If @rem is provided, the handle with the longest partial match is returned.
204 *
205 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100206 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100207 * @rem: pointer to receive remaining device path
208 * Return: matching handle
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400209 */
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100210efi_handle_t efi_dp_find_obj(struct efi_device_path *dp,
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100211 const efi_guid_t *guid,
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100212 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400213{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100214 efi_handle_t handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400215
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100216 handle = find_handle(dp, guid, false, rem);
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100217 if (!handle)
218 /* Match short form device path */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100219 handle = find_handle(dp, guid, true, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400220
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100221 return handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400222}
223
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100224/*
225 * Determine the last device path node that is not the end node.
226 *
227 * @dp device path
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100228 * Return: last node before the end node if it exists
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100229 * otherwise NULL
230 */
231const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
232{
233 struct efi_device_path *ret;
234
235 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
236 return NULL;
237 while (dp) {
238 ret = (struct efi_device_path *)dp;
239 dp = efi_dp_next(dp);
240 }
241 return ret;
242}
243
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200244/* get size of the first device path instance excluding end node */
245efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400246{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200247 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400248
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200249 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
250 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400251 while (dp) {
252 sz += dp->length;
253 dp = efi_dp_next(dp);
254 }
255
256 return sz;
257}
258
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200259/* get size of multi-instance device path excluding end node */
260efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
261{
262 const struct efi_device_path *p = dp;
263
264 if (!p)
265 return 0;
266 while (p->type != DEVICE_PATH_TYPE_END ||
267 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
268 p = (void *)p + p->length;
269
270 return (void *)p - (void *)dp;
271}
272
273/* copy multi-instance device path */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400274struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
275{
276 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200277 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400278
279 if (!dp)
280 return NULL;
281
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100282 ndp = efi_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100283 if (!ndp)
284 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400285 memcpy(ndp, dp, sz);
286
287 return ndp;
288}
289
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200290/**
291 * efi_dp_append_or_concatenate() - Append or concatenate two device paths.
292 * Concatenated device path will be separated
293 * by a sub-type 0xff end node
294 *
295 * @dp1: First device path
296 * @dp2: Second device path
297 * @concat: If true the two device paths will be concatenated and separated
298 * by an end of entrire device path sub-type 0xff end node.
299 * If true the second device path will be appended to the first and
300 * terminated by an end node
301 *
302 * Return:
303 * concatenated device path or NULL. Caller must free the returned value
304 */
305static struct
306efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
307 const struct efi_device_path *dp2,
308 bool concat)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400309{
310 struct efi_device_path *ret;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200311 size_t end_size = sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400312
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200313 if (concat)
314 end_size = 2 * sizeof(END);
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200315 if (!dp1 && !dp2) {
316 /* return an end node */
317 ret = efi_dp_dup(&END);
318 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400319 ret = efi_dp_dup(dp2);
320 } else if (!dp2) {
321 ret = efi_dp_dup(dp1);
322 } else {
323 /* both dp1 and dp2 are non-null */
324 unsigned sz1 = efi_dp_size(dp1);
325 unsigned sz2 = efi_dp_size(dp2);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100326 void *p = efi_alloc(sz1 + sz2 + end_size);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100327 if (!p)
328 return NULL;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200329 ret = p;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400330 memcpy(p, dp1, sz1);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200331 p += sz1;
332
333 if (concat) {
334 memcpy(p, &END, sizeof(END));
335 p += sizeof(END);
336 }
337
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200338 /* the end node of the second device path has to be retained */
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200339 memcpy(p, dp2, sz2);
340 p += sz2;
341 memcpy(p, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400342 }
343
344 return ret;
345}
346
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200347/**
348 * efi_dp_append() - Append a device to an existing device path.
349 *
350 * @dp1: First device path
351 * @dp2: Second device path
352 *
353 * Return:
354 * concatenated device path or NULL. Caller must free the returned value
355 */
356struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
357 const struct efi_device_path *dp2)
358{
359 return efi_dp_append_or_concatenate(dp1, dp2, false);
360}
361
362/**
363 * efi_dp_concat() - Concatenate 2 device paths. The final device path will
364 * contain two device paths separated by and end node (0xff).
365 *
366 * @dp1: First device path
367 * @dp2: Second device path
368 *
369 * Return:
370 * concatenated device path or NULL. Caller must free the returned value
371 */
372struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
373 const struct efi_device_path *dp2)
374{
375 return efi_dp_append_or_concatenate(dp1, dp2, true);
376}
377
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400378struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
379 const struct efi_device_path *node)
380{
381 struct efi_device_path *ret;
382
383 if (!node && !dp) {
384 ret = efi_dp_dup(&END);
385 } else if (!node) {
386 ret = efi_dp_dup(dp);
387 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200388 size_t sz = node->length;
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100389 void *p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100390 if (!p)
391 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400392 memcpy(p, node, sz);
393 memcpy(p + sz, &END, sizeof(END));
394 ret = p;
395 } else {
396 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200397 size_t sz = efi_dp_size(dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100398 void *p = efi_alloc(sz + node->length + 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, dp, sz);
402 memcpy(p + sz, node, node->length);
403 memcpy(p + sz + node->length, &END, sizeof(END));
404 ret = p;
405 }
406
407 return ret;
408}
409
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200410struct efi_device_path *efi_dp_create_device_node(const u8 type,
411 const u8 sub_type,
412 const u16 length)
413{
414 struct efi_device_path *ret;
415
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200416 if (length < sizeof(struct efi_device_path))
417 return NULL;
418
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100419 ret = efi_alloc(length);
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200420 if (!ret)
421 return ret;
422 ret->type = type;
423 ret->sub_type = sub_type;
424 ret->length = length;
425 return ret;
426}
427
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200428struct efi_device_path *efi_dp_append_instance(
429 const struct efi_device_path *dp,
430 const struct efi_device_path *dpi)
431{
432 size_t sz, szi;
433 struct efi_device_path *p, *ret;
434
435 if (!dpi)
436 return NULL;
437 if (!dp)
438 return efi_dp_dup(dpi);
439 sz = efi_dp_size(dp);
440 szi = efi_dp_instance_size(dpi);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100441 p = efi_alloc(sz + szi + 2 * sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200442 if (!p)
443 return NULL;
444 ret = p;
445 memcpy(p, dp, sz + sizeof(END));
446 p = (void *)p + sz;
447 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
448 p = (void *)p + sizeof(END);
449 memcpy(p, dpi, szi);
450 p = (void *)p + szi;
451 memcpy(p, &END, sizeof(END));
452 return ret;
453}
454
455struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
456 efi_uintn_t *size)
457{
458 size_t sz;
459 struct efi_device_path *p;
460
461 if (size)
462 *size = 0;
463 if (!dp || !*dp)
464 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200465 sz = efi_dp_instance_size(*dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100466 p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200467 if (!p)
468 return NULL;
469 memcpy(p, *dp, sz + sizeof(END));
470 *dp = (void *)*dp + sz;
471 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
472 *dp = (void *)*dp + sizeof(END);
473 else
474 *dp = NULL;
475 if (size)
476 *size = sz + sizeof(END);
477 return p;
478}
479
480bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
481{
482 const struct efi_device_path *p = dp;
483
484 if (!p)
485 return false;
486 while (p->type != DEVICE_PATH_TYPE_END)
487 p = (void *)p + p->length;
488 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
489}
490
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400491/* size of device-path not including END node for device and all parents
492 * up to the root device.
493 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100494__maybe_unused static unsigned int dp_size(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400495{
496 if (!dev || !dev->driver)
497 return sizeof(ROOT);
498
Simon Glass56ada7b2022-01-29 14:58:38 -0700499 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400500 case UCLASS_ROOT:
501 case UCLASS_SIMPLE_BUS:
502 /* stop traversing parents at this point: */
503 return sizeof(ROOT);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100504 case UCLASS_ETH:
505 return dp_size(dev->parent) +
506 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100507 case UCLASS_BLK:
508 switch (dev->parent->uclass->uc_drv->id) {
509#ifdef CONFIG_IDE
510 case UCLASS_IDE:
511 return dp_size(dev->parent) +
512 sizeof(struct efi_device_path_atapi);
513#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600514#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100515 case UCLASS_SCSI:
516 return dp_size(dev->parent) +
517 sizeof(struct efi_device_path_scsi);
518#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600519#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100520 case UCLASS_MMC:
521 return dp_size(dev->parent) +
522 sizeof(struct efi_device_path_sd_mmc_path);
523#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200524#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
525 case UCLASS_AHCI:
526 return dp_size(dev->parent) +
527 sizeof(struct efi_device_path_sata);
528#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200529#if defined(CONFIG_NVME)
530 case UCLASS_NVME:
531 return dp_size(dev->parent) +
532 sizeof(struct efi_device_path_nvme);
533#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900534#ifdef CONFIG_SANDBOX
Simon Glasse57f8d42022-10-29 19:47:17 -0600535 case UCLASS_HOST:
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900536 /*
537 * Sandbox's host device will be represented
538 * as vendor device with extra one byte for
539 * device number
540 */
541 return dp_size(dev->parent)
542 + sizeof(struct efi_device_path_vendor) + 1;
543#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100544#ifdef CONFIG_USB
545 case UCLASS_MASS_STORAGE:
546 return dp_size(dev->parent)
547 + sizeof(struct efi_device_path_controller);
548#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200549#ifdef CONFIG_VIRTIO_BLK
550 case UCLASS_VIRTIO:
551 /*
552 * Virtio devices will be represented as a vendor
553 * device node with an extra byte for the device
554 * number.
555 */
556 return dp_size(dev->parent)
557 + sizeof(struct efi_device_path_vendor) + 1;
558#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100559 default:
560 return dp_size(dev->parent);
561 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600562#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400563 case UCLASS_MMC:
564 return dp_size(dev->parent) +
565 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100566#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400567 case UCLASS_MASS_STORAGE:
568 case UCLASS_USB_HUB:
569 return dp_size(dev->parent) +
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100570 sizeof(struct efi_device_path_usb);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400571 default:
572 /* just skip over unknown classes: */
573 return dp_size(dev->parent);
574 }
575}
576
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100577/*
578 * Recursively build a device path.
579 *
580 * @buf pointer to the end of the device path
581 * @dev device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100582 * Return: pointer to the end of the device path
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100583 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100584__maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400585{
586 if (!dev || !dev->driver)
587 return buf;
588
Simon Glass56ada7b2022-01-29 14:58:38 -0700589 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400590 case UCLASS_ROOT:
591 case UCLASS_SIMPLE_BUS: {
592 /* stop traversing parents at this point: */
593 struct efi_device_path_vendor *vdp = buf;
594 *vdp = ROOT;
595 return &vdp[1];
596 }
Jan Kiszkaf1389822022-10-14 18:10:06 +0200597#ifdef CONFIG_NETDEVICES
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100598 case UCLASS_ETH: {
599 struct efi_device_path_mac_addr *dp =
600 dp_fill(buf, dev->parent);
Simon Glass95588622020-12-22 19:30:28 -0700601 struct eth_pdata *pdata = dev_get_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100602
603 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
604 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
605 dp->dp.length = sizeof(*dp);
606 memset(&dp->mac, 0, sizeof(dp->mac));
607 /* We only support IPv4 */
608 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
609 /* Ethernet */
610 dp->if_type = 1;
611 return &dp[1];
612 }
613#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100614 case UCLASS_BLK:
615 switch (dev->parent->uclass->uc_drv->id) {
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900616#ifdef CONFIG_SANDBOX
Simon Glasse57f8d42022-10-29 19:47:17 -0600617 case UCLASS_HOST: {
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900618 /* stop traversing parents at this point: */
Heinrich Schuchardt1e3beaf2020-05-06 01:28:08 +0200619 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700620 struct blk_desc *desc = dev_get_uclass_plat(dev);
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900621
622 dp_fill(buf, dev->parent);
623 dp = buf;
624 ++dp;
625 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
626 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
627 dp->dp.length = sizeof(*dp) + 1;
628 memcpy(&dp->guid, &efi_guid_host_dev,
629 sizeof(efi_guid_t));
630 dp->vendor_data[0] = desc->devnum;
631 return &dp->vendor_data[1];
632 }
633#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200634#ifdef CONFIG_VIRTIO_BLK
635 case UCLASS_VIRTIO: {
636 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700637 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200638
639 dp_fill(buf, dev->parent);
640 dp = buf;
641 ++dp;
642 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
643 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
644 dp->dp.length = sizeof(*dp) + 1;
645 memcpy(&dp->guid, &efi_guid_virtio_dev,
646 sizeof(efi_guid_t));
647 dp->vendor_data[0] = desc->devnum;
648 return &dp->vendor_data[1];
649 }
650#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100651#ifdef CONFIG_IDE
652 case UCLASS_IDE: {
653 struct efi_device_path_atapi *dp =
654 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700655 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100656
657 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
658 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
659 dp->dp.length = sizeof(*dp);
660 dp->logical_unit_number = desc->devnum;
661 dp->primary_secondary = IDE_BUS(desc->devnum);
662 dp->slave_master = desc->devnum %
663 (CONFIG_SYS_IDE_MAXDEVICE /
664 CONFIG_SYS_IDE_MAXBUS);
665 return &dp[1];
666 }
667#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600668#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100669 case UCLASS_SCSI: {
670 struct efi_device_path_scsi *dp =
671 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700672 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100673
674 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
675 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
676 dp->dp.length = sizeof(*dp);
677 dp->logical_unit_number = desc->lun;
678 dp->target_id = desc->target;
679 return &dp[1];
680 }
681#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600682#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100683 case UCLASS_MMC: {
684 struct efi_device_path_sd_mmc_path *sddp =
685 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700686 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100687
688 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
689 sddp->dp.sub_type = is_sd(desc) ?
690 DEVICE_PATH_SUB_TYPE_MSG_SD :
691 DEVICE_PATH_SUB_TYPE_MSG_MMC;
692 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700693 sddp->slot_number = dev_seq(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100694 return &sddp[1];
695 }
696#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200697#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
698 case UCLASS_AHCI: {
699 struct efi_device_path_sata *dp =
700 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700701 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200702
703 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
704 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
705 dp->dp.length = sizeof(*dp);
706 dp->hba_port = desc->devnum;
707 /* default 0xffff implies no port multiplier */
708 dp->port_multiplier_port = 0xffff;
709 dp->logical_unit_number = desc->lun;
710 return &dp[1];
711 }
712#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200713#if defined(CONFIG_NVME)
714 case UCLASS_NVME: {
715 struct efi_device_path_nvme *dp =
716 dp_fill(buf, dev->parent);
717 u32 ns_id;
718
719 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
720 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
721 dp->dp.length = sizeof(*dp);
722 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
723 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
724 return &dp[1];
725 }
726#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100727#if defined(CONFIG_USB)
728 case UCLASS_MASS_STORAGE: {
Heinrich Schuchardt232c9db2023-04-01 07:21:55 +0200729 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100730 struct efi_device_path_controller *dp =
731 dp_fill(buf, dev->parent);
732
733 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
734 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CONTROLLER;
735 dp->dp.length = sizeof(*dp);
736 dp->controller_number = desc->lun;
737 return &dp[1];
738 }
739#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100740 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100741 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
742 __FILE__, __LINE__, __func__,
743 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100744 return dp_fill(buf, dev->parent);
745 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600746#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400747 case UCLASS_MMC: {
748 struct efi_device_path_sd_mmc_path *sddp =
749 dp_fill(buf, dev->parent);
750 struct mmc *mmc = mmc_get_mmc_dev(dev);
751 struct blk_desc *desc = mmc_get_blk_desc(mmc);
752
753 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100754 sddp->dp.sub_type = is_sd(desc) ?
755 DEVICE_PATH_SUB_TYPE_MSG_SD :
756 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400757 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700758 sddp->slot_number = dev_seq(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400759
760 return &sddp[1];
761 }
762#endif
763 case UCLASS_MASS_STORAGE:
764 case UCLASS_USB_HUB: {
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100765 struct efi_device_path_usb *udp = dp_fill(buf, dev->parent);
766
767 switch (device_get_uclass_id(dev->parent)) {
768 case UCLASS_USB_HUB: {
769 struct usb_device *udev = dev_get_parent_priv(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400770
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100771 udp->parent_port_number = udev->portnr;
772 break;
773 }
774 default:
775 udp->parent_port_number = 0;
776 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400777 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100778 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400779 udp->dp.length = sizeof(*udp);
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100780 udp->usb_interface = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400781
782 return &udp[1];
783 }
784 default:
Simon Glass56ada7b2022-01-29 14:58:38 -0700785 /* If the uclass driver is missing, this will show NULL */
786 log_debug("unhandled device class: %s (%s)\n", dev->name,
787 dev_get_uclass_name(dev));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400788 return dp_fill(buf, dev->parent);
789 }
790}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400791
792static unsigned dp_part_size(struct blk_desc *desc, int part)
793{
794 unsigned dpsize;
Simon Glassec209a72022-01-29 14:58:39 -0700795 struct udevice *dev = desc->bdev;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400796
Simon Glass222f3cb2021-09-24 18:30:17 -0600797 dpsize = dp_size(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400798
799 if (part == 0) /* the actual disk, not a partition */
800 return dpsize;
801
802 if (desc->part_type == PART_TYPE_ISO)
803 dpsize += sizeof(struct efi_device_path_cdrom_path);
804 else
805 dpsize += sizeof(struct efi_device_path_hard_drive_path);
806
807 return dpsize;
808}
809
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100810/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100811 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100812 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200813 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100814 * @desc block device descriptor
815 * @part partition number, 0 identifies a block device
816 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100817static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400818{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600819 struct disk_partition info;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400820
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400821 part_get_info(desc, part, &info);
822
823 if (desc->part_type == PART_TYPE_ISO) {
824 struct efi_device_path_cdrom_path *cddp = buf;
825
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100826 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400827 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
828 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
829 cddp->dp.length = sizeof(*cddp);
830 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200831 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400832
833 buf = &cddp[1];
834 } else {
835 struct efi_device_path_hard_drive_path *hddp = buf;
836
837 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
838 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
839 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100840 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400841 hddp->partition_start = info.start;
842 hddp->partition_end = info.size;
843 if (desc->part_type == PART_TYPE_EFI)
844 hddp->partmap_type = 2;
845 else
846 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100847
848 switch (desc->sig_type) {
849 case SIG_TYPE_NONE:
850 default:
851 hddp->signature_type = 0;
852 memset(hddp->partition_signature, 0,
853 sizeof(hddp->partition_signature));
854 break;
855 case SIG_TYPE_MBR:
856 hddp->signature_type = 1;
857 memset(hddp->partition_signature, 0,
858 sizeof(hddp->partition_signature));
859 memcpy(hddp->partition_signature, &desc->mbr_sig,
860 sizeof(desc->mbr_sig));
861 break;
862 case SIG_TYPE_GUID:
863 hddp->signature_type = 2;
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900864#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
865 /* info.uuid exists only with PARTITION_UUIDS */
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200866 if (uuid_str_to_bin(info.uuid,
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900867 hddp->partition_signature,
868 UUID_STR_FORMAT_GUID)) {
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200869 log_warning(
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900870 "Partition %d: invalid GUID %s\n",
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200871 part, info.uuid);
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900872 }
873#endif
Jonathan Gray84b4d702017-11-22 14:18:59 +1100874 break;
875 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400876
877 buf = &hddp[1];
878 }
879
880 return buf;
881}
882
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100883/*
884 * Create a device path for a block device or one of its partitions.
885 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200886 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100887 * @desc block device descriptor
888 * @part partition number, 0 identifies a block device
889 */
890static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
891{
Simon Glassec209a72022-01-29 14:58:39 -0700892 struct udevice *dev = desc->bdev;
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100893
Simon Glass222f3cb2021-09-24 18:30:17 -0600894 buf = dp_fill(buf, dev);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100895
896 if (part == 0) /* the actual disk, not a partition */
897 return buf;
898
899 return dp_part_node(buf, desc, part);
900}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400901
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200902/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400903struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
904{
905 void *buf, *start;
906
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100907 start = buf = efi_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100908 if (!buf)
909 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400910
911 buf = dp_part_fill(buf, desc, part);
912
913 *((struct efi_device_path *)buf) = END;
914
915 return start;
916}
917
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100918/*
919 * Create a device node for a block device partition.
920 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200921 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100922 * @desc block device descriptor
923 * @part partition number, 0 identifies a block device
924 */
925struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
926{
927 efi_uintn_t dpsize;
928 void *buf;
929
930 if (desc->part_type == PART_TYPE_ISO)
931 dpsize = sizeof(struct efi_device_path_cdrom_path);
932 else
933 dpsize = sizeof(struct efi_device_path_hard_drive_path);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100934 buf = efi_alloc(dpsize);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100935
Heinrich Schuchardte29fbb32022-10-06 13:36:02 +0200936 if (buf)
937 dp_part_node(buf, desc, part);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100938
939 return buf;
940}
941
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200942/**
943 * path_to_uefi() - convert UTF-8 path to an UEFI style path
944 *
945 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
946 * separators and UTF-16).
947 *
948 * @src: source buffer
949 * @uefi: target buffer, possibly unaligned
950 */
951static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400952{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200953 u16 *pos = uefi;
954
955 /*
956 * efi_set_bootdev() calls this routine indirectly before the UEFI
957 * subsystem is initialized. So we cannot assume unaligned access to be
958 * enabled.
959 */
960 allow_unaligned();
961
962 while (*src) {
963 s32 code = utf8_get(&src);
964
965 if (code < 0)
966 code = '?';
967 else if (code == '/')
968 code = '\\';
969 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400970 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200971 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400972}
973
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000974/**
975 * efi_dp_from_file() - create device path for file
976 *
977 * The function creates a device path from the block descriptor @desc and the
978 * partition number @part and appends a device path node created describing the
979 * file path @path.
980 *
981 * If @desc is NULL, the device path will not contain nodes describing the
982 * partition.
983 * If @path is an empty string "", the device path will not contain a node
984 * for the file path.
985 *
986 * @desc: block device descriptor or NULL
987 * @part: partition number
988 * @path: file path on partition or ""
989 * Return: device path or NULL in case of an error
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400990 */
991struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
992 const char *path)
993{
994 struct efi_device_path_file_path *fp;
995 void *buf, *start;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900996 size_t dpsize = 0, fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400997
998 if (desc)
999 dpsize = dp_part_size(desc, part);
1000
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +02001001 fpsize = sizeof(struct efi_device_path) +
1002 2 * (utf8_utf16_strlen(path) + 1);
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001003 if (fpsize > U16_MAX)
1004 return NULL;
1005
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001006 dpsize += fpsize;
1007
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001008 start = buf = efi_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001009 if (!buf)
1010 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001011
1012 if (desc)
1013 buf = dp_part_fill(buf, desc, part);
1014
1015 /* add file-path: */
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +00001016 if (*path) {
1017 fp = buf;
1018 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
1019 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
1020 fp->dp.length = (u16)fpsize;
1021 path_to_uefi(fp->str, path);
1022 buf += fpsize;
1023 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001024
1025 *((struct efi_device_path *)buf) = END;
1026
1027 return start;
1028}
1029
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001030struct efi_device_path *efi_dp_from_uart(void)
1031{
1032 void *buf, *pos;
1033 struct efi_device_path_uart *uart;
1034 size_t dpsize = sizeof(ROOT) + sizeof(*uart) + sizeof(END);
1035
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001036 buf = efi_alloc(dpsize);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001037 if (!buf)
1038 return NULL;
1039 pos = buf;
1040 memcpy(pos, &ROOT, sizeof(ROOT));
1041 pos += sizeof(ROOT);
1042 uart = pos;
1043 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1044 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
1045 uart->dp.length = sizeof(*uart);
1046 pos += sizeof(*uart);
1047 memcpy(pos, &END, sizeof(END));
1048
1049 return buf;
1050}
1051
Jan Kiszkaf1389822022-10-14 18:10:06 +02001052#ifdef CONFIG_NETDEVICES
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001053struct efi_device_path *efi_dp_from_eth(void)
1054{
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001055 void *buf, *start;
1056 unsigned dpsize = 0;
1057
1058 assert(eth_get_dev());
1059
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001060 dpsize += dp_size(eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001061
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001062 start = buf = efi_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001063 if (!buf)
1064 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001065
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001066 buf = dp_fill(buf, eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001067
1068 *((struct efi_device_path *)buf) = END;
1069
1070 return start;
1071}
1072#endif
1073
Rob Clark18ceba72017-10-10 08:23:06 -04001074/* Construct a device-path for memory-mapped image */
1075struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1076 uint64_t start_address,
1077 uint64_t end_address)
1078{
1079 struct efi_device_path_memory *mdp;
1080 void *buf, *start;
1081
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001082 start = buf = efi_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001083 if (!buf)
1084 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -04001085
1086 mdp = buf;
1087 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1088 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1089 mdp->dp.length = sizeof(*mdp);
1090 mdp->memory_type = memory_type;
1091 mdp->start_address = start_address;
1092 mdp->end_address = end_address;
1093 buf = &mdp[1];
1094
1095 *((struct efi_device_path *)buf) = END;
1096
1097 return start;
1098}
1099
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001100/**
1101 * efi_dp_split_file_path() - split of relative file path from device path
1102 *
1103 * Given a device path indicating a file on a device, separate the device
1104 * path in two: the device path of the actual device and the file path
1105 * relative to this device.
1106 *
1107 * @full_path: device path including device and file path
1108 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001109 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001110 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001111 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001112efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1113 struct efi_device_path **device_path,
1114 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001115{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001116 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001117
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001118 *device_path = NULL;
1119 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001120 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001121 if (!dp)
1122 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001123 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001124 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001125 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001126 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001127 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001128 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001129 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001130 if (!fp)
1131 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001132 p->type = DEVICE_PATH_TYPE_END;
1133 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1134 p->length = sizeof(*p);
1135
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001136out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001137 *device_path = dp;
1138 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001139 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001140}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001141
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001142/**
1143 * efi_dp_from_name() - convert U-Boot device and file path to device path
1144 *
1145 * @dev: U-Boot device, e.g. 'mmc'
1146 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1147 * @path: file path relative to U-Boot device, may be NULL
1148 * @device: pointer to receive device path of the device
1149 * @file: pointer to receive device path for the file
1150 * Return: status code
1151 */
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001152efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1153 const char *path,
1154 struct efi_device_path **device,
1155 struct efi_device_path **file)
1156{
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001157 struct blk_desc *desc = NULL;
Simon Glassc1c4a8f2020-05-10 11:39:57 -06001158 struct disk_partition fs_partition;
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001159 size_t image_size;
1160 void *image_addr;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001161 int part = 0;
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001162 char *filename;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001163 char *s;
1164
AKASHI Takahiro39844412018-11-05 18:06:40 +09001165 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001166 return EFI_INVALID_PARAMETER;
1167
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001168 if (!strcmp(dev, "Net")) {
Jan Kiszkaf1389822022-10-14 18:10:06 +02001169#ifdef CONFIG_NETDEVICES
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001170 if (device)
1171 *device = efi_dp_from_eth();
1172#endif
1173 } else if (!strcmp(dev, "Uart")) {
1174 if (device)
1175 *device = efi_dp_from_uart();
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001176 } else if (!strcmp(dev, "Mem")) {
1177 efi_get_image_parameters(&image_addr, &image_size);
1178
1179 if (device)
1180 *device = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
1181 (uintptr_t)image_addr,
1182 image_size);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001183 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001184 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1185 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001186 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001187 return EFI_INVALID_PARAMETER;
1188
AKASHI Takahiro39844412018-11-05 18:06:40 +09001189 if (device)
1190 *device = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001191 }
1192
1193 if (!path)
1194 return EFI_SUCCESS;
1195
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001196 filename = calloc(1, strlen(path) + 1);
1197 if (!filename)
1198 return EFI_OUT_OF_RESOURCES;
1199
1200 sprintf(filename, "%s", path);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001201 /* DOS style file path: */
1202 s = filename;
1203 while ((s = strchr(s, '/')))
1204 *s++ = '\\';
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001205 *file = efi_dp_from_file(desc, part, filename);
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001206 free(filename);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001207
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001208 if (!*file)
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001209 return EFI_INVALID_PARAMETER;
1210
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001211 return EFI_SUCCESS;
1212}
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001213
1214/**
1215 * efi_dp_check_length() - check length of a device path
1216 *
1217 * @dp: pointer to device path
1218 * @maxlen: maximum length of the device path
1219 * Return:
1220 * * length of the device path if it is less or equal @maxlen
1221 * * -1 if the device path is longer then @maxlen
1222 * * -1 if a device path node has a length of less than 4
1223 * * -EINVAL if maxlen exceeds SSIZE_MAX
1224 */
1225ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1226 const size_t maxlen)
1227{
1228 ssize_t ret = 0;
1229 u16 len;
1230
1231 if (maxlen > SSIZE_MAX)
1232 return -EINVAL;
1233 for (;;) {
1234 len = dp->length;
1235 if (len < 4)
1236 return -1;
1237 ret += len;
1238 if (ret > maxlen)
1239 return -1;
1240 if (dp->type == DEVICE_PATH_TYPE_END &&
1241 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1242 return ret;
1243 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1244 }
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001245}
1246
1247/**
1248 * efi_dp_from_lo() - Get the instance of a VenMedia node in a
1249 * multi-instance device path that matches
1250 * a specific GUID. This kind of device paths
1251 * is found in Boot#### options describing an
1252 * initrd location
1253 *
1254 * @lo: EFI_LOAD_OPTION containing a valid device path
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001255 * @guid: guid to search for
1256 *
1257 * Return:
1258 * device path including the VenMedia node or NULL.
1259 * Caller must free the returned value.
1260 */
1261struct
1262efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001263 const efi_guid_t *guid)
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001264{
1265 struct efi_device_path *fp = lo->file_path;
1266 struct efi_device_path_vendor *vendor;
1267 int lo_len = lo->file_path_length;
1268
1269 for (; lo_len >= sizeof(struct efi_device_path);
1270 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1271 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1272 break;
1273 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1274 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1275 continue;
1276
1277 vendor = (struct efi_device_path_vendor *)fp;
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001278 if (!guidcmp(&vendor->guid, guid))
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +02001279 return efi_dp_dup(efi_dp_next(fp));
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001280 }
1281 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1282
1283 return NULL;
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001284}
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001285
1286/**
1287 * search_gpt_dp_node() - search gpt device path node
1288 *
1289 * @device_path: device path
1290 *
1291 * Return: pointer to the gpt device path node
1292 */
1293struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1294{
1295 struct efi_device_path *dp = device_path;
1296
1297 while (dp) {
1298 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1299 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1300 struct efi_device_path_hard_drive_path *hd_dp =
1301 (struct efi_device_path_hard_drive_path *)dp;
1302
1303 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1304 hd_dp->signature_type == SIG_TYPE_GUID)
1305 return dp;
1306 }
1307 dp = efi_dp_next(dp);
1308 }
1309
1310 return NULL;
1311}