blob: a6a6ef0d6cabcab09b4cefbe517ca593141257d5 [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
Tobias Waldekranz32152682023-02-16 16:33:55 +010024#ifdef CONFIG_BLKMAP
25const efi_guid_t efi_guid_blkmap_dev = U_BOOT_BLKMAP_DEV_GUID;
26#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +090027#ifdef CONFIG_SANDBOX
28const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
29#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +020030#ifdef CONFIG_VIRTIO_BLK
31const efi_guid_t efi_guid_virtio_dev = U_BOOT_VIRTIO_DEV_GUID;
32#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +090033
Rob Clarkf90cb9f2017-09-13 18:05:28 -040034/* template END node: */
Masahisa Kojima97bd9da2022-06-19 13:55:59 +090035const struct efi_device_path END = {
Rob Clarkf90cb9f2017-09-13 18:05:28 -040036 .type = DEVICE_PATH_TYPE_END,
37 .sub_type = DEVICE_PATH_SUB_TYPE_END,
38 .length = sizeof(END),
39};
40
Rob Clarkf90cb9f2017-09-13 18:05:28 -040041/* template ROOT node: */
42static const struct efi_device_path_vendor ROOT = {
43 .dp = {
44 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
45 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
46 .length = sizeof(ROOT),
47 },
48 .guid = U_BOOT_GUID,
49};
50
Simon Glass222f3cb2021-09-24 18:30:17 -060051#if defined(CONFIG_MMC)
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010052/*
53 * Determine if an MMC device is an SD card.
54 *
55 * @desc block device descriptor
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010056 * Return: true if the device is an SD card
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010057 */
58static bool is_sd(struct blk_desc *desc)
59{
60 struct mmc *mmc = find_mmc_device(desc->devnum);
61
62 if (!mmc)
63 return false;
64
65 return IS_SD(mmc) != 0U;
66}
67#endif
68
Rob Clarkf90cb9f2017-09-13 18:05:28 -040069/*
70 * Iterate to next block in device-path, terminating (returning NULL)
71 * at /End* node.
72 */
73struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
74{
75 if (dp == NULL)
76 return NULL;
77 if (dp->type == DEVICE_PATH_TYPE_END)
78 return NULL;
79 dp = ((void *)dp) + dp->length;
80 if (dp->type == DEVICE_PATH_TYPE_END)
81 return NULL;
82 return (struct efi_device_path *)dp;
83}
84
85/*
86 * Compare two device-paths, stopping when the shorter of the two hits
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +020087 * an End* node. This is useful to, for example, compare a device-path
Rob Clarkf90cb9f2017-09-13 18:05:28 -040088 * representing a device with one representing a file on the device, or
89 * a device with a parent device.
90 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +020091int efi_dp_match(const struct efi_device_path *a,
92 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -040093{
94 while (1) {
95 int ret;
96
97 ret = memcmp(&a->length, &b->length, sizeof(a->length));
98 if (ret)
99 return ret;
100
101 ret = memcmp(a, b, a->length);
102 if (ret)
103 return ret;
104
105 a = efi_dp_next(a);
106 b = efi_dp_next(b);
107
108 if (!a || !b)
109 return 0;
110 }
111}
112
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100113/**
114 * efi_dp_shorten() - shorten device-path
115 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200116 * When creating a short boot option we want to use a device-path that is
117 * independent of the location where the block device is plugged in.
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400118 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200119 * UsbWwi() nodes contain a serial number, hard drive paths a partition
120 * UUID. Both should be unique.
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200121 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200122 * See UEFI spec, section 3.1.2 for "short-form device path".
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100123 *
Heinrich Schuchardtdb17dbc2022-03-21 08:26:48 +0100124 * @dp: original device-path
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100125 * @Return: shortened device-path or NULL
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400126 */
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100127struct efi_device_path *efi_dp_shorten(struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400128{
129 while (dp) {
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200130 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_WWI) ||
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400131 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
132 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
133 return dp;
134
135 dp = efi_dp_next(dp);
136 }
137
138 return dp;
139}
140
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100141/**
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100142 * find_handle() - find handle by device path and installed protocol
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100143 *
144 * If @rem is provided, the handle with the longest partial match is returned.
145 *
146 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100147 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100148 * @short_path: use short form device path for matching
149 * @rem: pointer to receive remaining device path
150 * Return: matching handle
151 */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100152static efi_handle_t find_handle(struct efi_device_path *dp,
153 const efi_guid_t *guid, bool short_path,
154 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400155{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100156 efi_handle_t handle, best_handle = NULL;
157 efi_uintn_t len, best_len = 0;
158
159 len = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400160
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100161 list_for_each_entry(handle, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100162 struct efi_handler *handler;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100163 struct efi_device_path *dp_current;
164 efi_uintn_t len_current;
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100165 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400166
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100167 if (guid) {
168 ret = efi_search_protocol(handle, guid, &handler);
169 if (ret != EFI_SUCCESS)
170 continue;
171 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100172 ret = efi_search_protocol(handle, &efi_guid_device_path,
173 &handler);
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100174 if (ret != EFI_SUCCESS)
175 continue;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100176 dp_current = handler->protocol_interface;
177 if (short_path) {
178 dp_current = efi_dp_shorten(dp_current);
179 if (!dp_current)
180 continue;
181 }
182 len_current = efi_dp_instance_size(dp_current);
183 if (rem) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100184 if (len_current > len)
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100185 continue;
186 } else {
187 if (len_current != len)
188 continue;
189 }
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100190 if (memcmp(dp_current, dp, len_current))
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100191 continue;
192 if (!rem)
193 return handle;
194 if (len_current > best_len) {
195 best_len = len_current;
196 best_handle = handle;
197 *rem = (void*)((u8 *)dp + len_current);
198 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400199 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100200 return best_handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400201}
202
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100203/**
204 * efi_dp_find_obj() - find handle by device path
205 *
206 * If @rem is provided, the handle with the longest partial match is returned.
207 *
208 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100209 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100210 * @rem: pointer to receive remaining device path
211 * Return: matching handle
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400212 */
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100213efi_handle_t efi_dp_find_obj(struct efi_device_path *dp,
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100214 const efi_guid_t *guid,
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100215 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400216{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100217 efi_handle_t handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400218
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100219 handle = find_handle(dp, guid, false, rem);
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100220 if (!handle)
221 /* Match short form device path */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100222 handle = find_handle(dp, guid, true, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400223
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100224 return handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400225}
226
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100227/*
228 * Determine the last device path node that is not the end node.
229 *
230 * @dp device path
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100231 * Return: last node before the end node if it exists
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100232 * otherwise NULL
233 */
234const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
235{
236 struct efi_device_path *ret;
237
238 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
239 return NULL;
240 while (dp) {
241 ret = (struct efi_device_path *)dp;
242 dp = efi_dp_next(dp);
243 }
244 return ret;
245}
246
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200247/* get size of the first device path instance excluding end node */
248efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400249{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200250 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400251
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200252 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
253 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400254 while (dp) {
255 sz += dp->length;
256 dp = efi_dp_next(dp);
257 }
258
259 return sz;
260}
261
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200262/* get size of multi-instance device path excluding end node */
263efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
264{
265 const struct efi_device_path *p = dp;
266
267 if (!p)
268 return 0;
269 while (p->type != DEVICE_PATH_TYPE_END ||
270 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
271 p = (void *)p + p->length;
272
273 return (void *)p - (void *)dp;
274}
275
276/* copy multi-instance device path */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400277struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
278{
279 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200280 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400281
282 if (!dp)
283 return NULL;
284
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100285 ndp = efi_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100286 if (!ndp)
287 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400288 memcpy(ndp, dp, sz);
289
290 return ndp;
291}
292
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200293/**
294 * efi_dp_append_or_concatenate() - Append or concatenate two device paths.
295 * Concatenated device path will be separated
296 * by a sub-type 0xff end node
297 *
298 * @dp1: First device path
299 * @dp2: Second device path
300 * @concat: If true the two device paths will be concatenated and separated
301 * by an end of entrire device path sub-type 0xff end node.
302 * If true the second device path will be appended to the first and
303 * terminated by an end node
304 *
305 * Return:
306 * concatenated device path or NULL. Caller must free the returned value
307 */
308static struct
309efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
310 const struct efi_device_path *dp2,
311 bool concat)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400312{
313 struct efi_device_path *ret;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200314 size_t end_size = sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400315
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200316 if (concat)
317 end_size = 2 * sizeof(END);
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200318 if (!dp1 && !dp2) {
319 /* return an end node */
320 ret = efi_dp_dup(&END);
321 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400322 ret = efi_dp_dup(dp2);
323 } else if (!dp2) {
324 ret = efi_dp_dup(dp1);
325 } else {
326 /* both dp1 and dp2 are non-null */
327 unsigned sz1 = efi_dp_size(dp1);
328 unsigned sz2 = efi_dp_size(dp2);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100329 void *p = efi_alloc(sz1 + sz2 + end_size);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100330 if (!p)
331 return NULL;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200332 ret = p;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400333 memcpy(p, dp1, sz1);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200334 p += sz1;
335
336 if (concat) {
337 memcpy(p, &END, sizeof(END));
338 p += sizeof(END);
339 }
340
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200341 /* the end node of the second device path has to be retained */
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200342 memcpy(p, dp2, sz2);
343 p += sz2;
344 memcpy(p, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400345 }
346
347 return ret;
348}
349
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200350/**
351 * efi_dp_append() - Append a device to an existing device path.
352 *
353 * @dp1: First device path
354 * @dp2: Second device path
355 *
356 * Return:
357 * concatenated device path or NULL. Caller must free the returned value
358 */
359struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
360 const struct efi_device_path *dp2)
361{
362 return efi_dp_append_or_concatenate(dp1, dp2, false);
363}
364
365/**
366 * efi_dp_concat() - Concatenate 2 device paths. The final device path will
367 * contain two device paths separated by and end node (0xff).
368 *
369 * @dp1: First device path
370 * @dp2: Second device path
371 *
372 * Return:
373 * concatenated device path or NULL. Caller must free the returned value
374 */
375struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
376 const struct efi_device_path *dp2)
377{
378 return efi_dp_append_or_concatenate(dp1, dp2, true);
379}
380
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400381struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
382 const struct efi_device_path *node)
383{
384 struct efi_device_path *ret;
385
386 if (!node && !dp) {
387 ret = efi_dp_dup(&END);
388 } else if (!node) {
389 ret = efi_dp_dup(dp);
390 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200391 size_t sz = node->length;
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100392 void *p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100393 if (!p)
394 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400395 memcpy(p, node, sz);
396 memcpy(p + sz, &END, sizeof(END));
397 ret = p;
398 } else {
399 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200400 size_t sz = efi_dp_size(dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100401 void *p = efi_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100402 if (!p)
403 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400404 memcpy(p, dp, sz);
405 memcpy(p + sz, node, node->length);
406 memcpy(p + sz + node->length, &END, sizeof(END));
407 ret = p;
408 }
409
410 return ret;
411}
412
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200413struct efi_device_path *efi_dp_create_device_node(const u8 type,
414 const u8 sub_type,
415 const u16 length)
416{
417 struct efi_device_path *ret;
418
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200419 if (length < sizeof(struct efi_device_path))
420 return NULL;
421
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100422 ret = efi_alloc(length);
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200423 if (!ret)
424 return ret;
425 ret->type = type;
426 ret->sub_type = sub_type;
427 ret->length = length;
428 return ret;
429}
430
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200431struct efi_device_path *efi_dp_append_instance(
432 const struct efi_device_path *dp,
433 const struct efi_device_path *dpi)
434{
435 size_t sz, szi;
436 struct efi_device_path *p, *ret;
437
438 if (!dpi)
439 return NULL;
440 if (!dp)
441 return efi_dp_dup(dpi);
442 sz = efi_dp_size(dp);
443 szi = efi_dp_instance_size(dpi);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100444 p = efi_alloc(sz + szi + 2 * sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200445 if (!p)
446 return NULL;
447 ret = p;
448 memcpy(p, dp, sz + sizeof(END));
449 p = (void *)p + sz;
450 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
451 p = (void *)p + sizeof(END);
452 memcpy(p, dpi, szi);
453 p = (void *)p + szi;
454 memcpy(p, &END, sizeof(END));
455 return ret;
456}
457
458struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
459 efi_uintn_t *size)
460{
461 size_t sz;
462 struct efi_device_path *p;
463
464 if (size)
465 *size = 0;
466 if (!dp || !*dp)
467 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200468 sz = efi_dp_instance_size(*dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100469 p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200470 if (!p)
471 return NULL;
472 memcpy(p, *dp, sz + sizeof(END));
473 *dp = (void *)*dp + sz;
474 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
475 *dp = (void *)*dp + sizeof(END);
476 else
477 *dp = NULL;
478 if (size)
479 *size = sz + sizeof(END);
480 return p;
481}
482
483bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
484{
485 const struct efi_device_path *p = dp;
486
487 if (!p)
488 return false;
489 while (p->type != DEVICE_PATH_TYPE_END)
490 p = (void *)p + p->length;
491 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
492}
493
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400494/* size of device-path not including END node for device and all parents
495 * up to the root device.
496 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100497__maybe_unused static unsigned int dp_size(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400498{
499 if (!dev || !dev->driver)
500 return sizeof(ROOT);
501
Simon Glass56ada7b2022-01-29 14:58:38 -0700502 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400503 case UCLASS_ROOT:
504 case UCLASS_SIMPLE_BUS:
505 /* stop traversing parents at this point: */
506 return sizeof(ROOT);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100507 case UCLASS_ETH:
508 return dp_size(dev->parent) +
509 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100510 case UCLASS_BLK:
511 switch (dev->parent->uclass->uc_drv->id) {
512#ifdef CONFIG_IDE
513 case UCLASS_IDE:
514 return dp_size(dev->parent) +
515 sizeof(struct efi_device_path_atapi);
516#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600517#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100518 case UCLASS_SCSI:
519 return dp_size(dev->parent) +
520 sizeof(struct efi_device_path_scsi);
521#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600522#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100523 case UCLASS_MMC:
524 return dp_size(dev->parent) +
525 sizeof(struct efi_device_path_sd_mmc_path);
526#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200527#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
528 case UCLASS_AHCI:
529 return dp_size(dev->parent) +
530 sizeof(struct efi_device_path_sata);
531#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200532#if defined(CONFIG_NVME)
533 case UCLASS_NVME:
534 return dp_size(dev->parent) +
535 sizeof(struct efi_device_path_nvme);
536#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900537#ifdef CONFIG_SANDBOX
Simon Glasse57f8d42022-10-29 19:47:17 -0600538 case UCLASS_HOST:
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900539 /*
540 * Sandbox's host device will be represented
541 * as vendor device with extra one byte for
542 * device number
543 */
544 return dp_size(dev->parent)
545 + sizeof(struct efi_device_path_vendor) + 1;
546#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100547#ifdef CONFIG_USB
548 case UCLASS_MASS_STORAGE:
549 return dp_size(dev->parent)
550 + sizeof(struct efi_device_path_controller);
551#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200552#ifdef CONFIG_VIRTIO_BLK
553 case UCLASS_VIRTIO:
554 /*
555 * Virtio devices will be represented as a vendor
556 * device node with an extra byte for the device
557 * number.
558 */
559 return dp_size(dev->parent)
560 + sizeof(struct efi_device_path_vendor) + 1;
561#endif
Tobias Waldekranz32152682023-02-16 16:33:55 +0100562#ifdef CONFIG_BLKMAP
563 case UCLASS_BLKMAP:
564 /*
565 * blkmap devices will be represented as a vendor
566 * device node with an extra byte for the device
567 * number.
568 */
569 return dp_size(dev->parent)
570 + sizeof(struct efi_device_path_vendor) + 1;
571#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100572 default:
573 return dp_size(dev->parent);
574 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600575#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400576 case UCLASS_MMC:
577 return dp_size(dev->parent) +
578 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100579#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400580 case UCLASS_MASS_STORAGE:
581 case UCLASS_USB_HUB:
582 return dp_size(dev->parent) +
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100583 sizeof(struct efi_device_path_usb);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400584 default:
585 /* just skip over unknown classes: */
586 return dp_size(dev->parent);
587 }
588}
589
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100590/*
591 * Recursively build a device path.
592 *
593 * @buf pointer to the end of the device path
594 * @dev device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100595 * Return: pointer to the end of the device path
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100596 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100597__maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400598{
599 if (!dev || !dev->driver)
600 return buf;
601
Simon Glass56ada7b2022-01-29 14:58:38 -0700602 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400603 case UCLASS_ROOT:
604 case UCLASS_SIMPLE_BUS: {
605 /* stop traversing parents at this point: */
606 struct efi_device_path_vendor *vdp = buf;
607 *vdp = ROOT;
608 return &vdp[1];
609 }
Jan Kiszkaf1389822022-10-14 18:10:06 +0200610#ifdef CONFIG_NETDEVICES
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100611 case UCLASS_ETH: {
612 struct efi_device_path_mac_addr *dp =
613 dp_fill(buf, dev->parent);
Simon Glass95588622020-12-22 19:30:28 -0700614 struct eth_pdata *pdata = dev_get_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100615
616 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
617 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
618 dp->dp.length = sizeof(*dp);
619 memset(&dp->mac, 0, sizeof(dp->mac));
620 /* We only support IPv4 */
621 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
622 /* Ethernet */
623 dp->if_type = 1;
624 return &dp[1];
625 }
626#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100627 case UCLASS_BLK:
628 switch (dev->parent->uclass->uc_drv->id) {
Tobias Waldekranz32152682023-02-16 16:33:55 +0100629#ifdef CONFIG_BLKMAP
630 case UCLASS_BLKMAP: {
631 struct efi_device_path_vendor *dp;
632 struct blk_desc *desc = dev_get_uclass_plat(dev);
633
634 dp_fill(buf, dev->parent);
635 dp = buf;
636 ++dp;
637 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
638 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
639 dp->dp.length = sizeof(*dp) + 1;
640 memcpy(&dp->guid, &efi_guid_blkmap_dev,
641 sizeof(efi_guid_t));
642 dp->vendor_data[0] = desc->devnum;
643 return &dp->vendor_data[1];
644 }
645#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900646#ifdef CONFIG_SANDBOX
Simon Glasse57f8d42022-10-29 19:47:17 -0600647 case UCLASS_HOST: {
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900648 /* stop traversing parents at this point: */
Heinrich Schuchardt1e3beaf2020-05-06 01:28:08 +0200649 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700650 struct blk_desc *desc = dev_get_uclass_plat(dev);
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900651
652 dp_fill(buf, dev->parent);
653 dp = buf;
654 ++dp;
655 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
656 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
657 dp->dp.length = sizeof(*dp) + 1;
658 memcpy(&dp->guid, &efi_guid_host_dev,
659 sizeof(efi_guid_t));
660 dp->vendor_data[0] = desc->devnum;
661 return &dp->vendor_data[1];
662 }
663#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200664#ifdef CONFIG_VIRTIO_BLK
665 case UCLASS_VIRTIO: {
666 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700667 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200668
669 dp_fill(buf, dev->parent);
670 dp = buf;
671 ++dp;
672 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
673 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
674 dp->dp.length = sizeof(*dp) + 1;
675 memcpy(&dp->guid, &efi_guid_virtio_dev,
676 sizeof(efi_guid_t));
677 dp->vendor_data[0] = desc->devnum;
678 return &dp->vendor_data[1];
679 }
680#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100681#ifdef CONFIG_IDE
682 case UCLASS_IDE: {
683 struct efi_device_path_atapi *dp =
684 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700685 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100686
687 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
688 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
689 dp->dp.length = sizeof(*dp);
690 dp->logical_unit_number = desc->devnum;
691 dp->primary_secondary = IDE_BUS(desc->devnum);
692 dp->slave_master = desc->devnum %
693 (CONFIG_SYS_IDE_MAXDEVICE /
694 CONFIG_SYS_IDE_MAXBUS);
695 return &dp[1];
696 }
697#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600698#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100699 case UCLASS_SCSI: {
700 struct efi_device_path_scsi *dp =
701 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700702 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100703
704 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
705 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
706 dp->dp.length = sizeof(*dp);
707 dp->logical_unit_number = desc->lun;
708 dp->target_id = desc->target;
709 return &dp[1];
710 }
711#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600712#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100713 case UCLASS_MMC: {
714 struct efi_device_path_sd_mmc_path *sddp =
715 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700716 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100717
718 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
719 sddp->dp.sub_type = is_sd(desc) ?
720 DEVICE_PATH_SUB_TYPE_MSG_SD :
721 DEVICE_PATH_SUB_TYPE_MSG_MMC;
722 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700723 sddp->slot_number = dev_seq(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100724 return &sddp[1];
725 }
726#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200727#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
728 case UCLASS_AHCI: {
729 struct efi_device_path_sata *dp =
730 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700731 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200732
733 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
734 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
735 dp->dp.length = sizeof(*dp);
736 dp->hba_port = desc->devnum;
737 /* default 0xffff implies no port multiplier */
738 dp->port_multiplier_port = 0xffff;
739 dp->logical_unit_number = desc->lun;
740 return &dp[1];
741 }
742#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200743#if defined(CONFIG_NVME)
744 case UCLASS_NVME: {
745 struct efi_device_path_nvme *dp =
746 dp_fill(buf, dev->parent);
747 u32 ns_id;
748
749 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
750 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
751 dp->dp.length = sizeof(*dp);
752 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
753 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
754 return &dp[1];
755 }
756#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100757#if defined(CONFIG_USB)
758 case UCLASS_MASS_STORAGE: {
Heinrich Schuchardt232c9db2023-04-01 07:21:55 +0200759 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100760 struct efi_device_path_controller *dp =
761 dp_fill(buf, dev->parent);
762
763 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
764 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CONTROLLER;
765 dp->dp.length = sizeof(*dp);
766 dp->controller_number = desc->lun;
767 return &dp[1];
768 }
769#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100770 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100771 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
772 __FILE__, __LINE__, __func__,
773 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100774 return dp_fill(buf, dev->parent);
775 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600776#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400777 case UCLASS_MMC: {
778 struct efi_device_path_sd_mmc_path *sddp =
779 dp_fill(buf, dev->parent);
780 struct mmc *mmc = mmc_get_mmc_dev(dev);
781 struct blk_desc *desc = mmc_get_blk_desc(mmc);
782
783 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100784 sddp->dp.sub_type = is_sd(desc) ?
785 DEVICE_PATH_SUB_TYPE_MSG_SD :
786 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400787 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700788 sddp->slot_number = dev_seq(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400789
790 return &sddp[1];
791 }
792#endif
793 case UCLASS_MASS_STORAGE:
794 case UCLASS_USB_HUB: {
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100795 struct efi_device_path_usb *udp = dp_fill(buf, dev->parent);
796
797 switch (device_get_uclass_id(dev->parent)) {
798 case UCLASS_USB_HUB: {
799 struct usb_device *udev = dev_get_parent_priv(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400800
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100801 udp->parent_port_number = udev->portnr;
802 break;
803 }
804 default:
805 udp->parent_port_number = 0;
806 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400807 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100808 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400809 udp->dp.length = sizeof(*udp);
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100810 udp->usb_interface = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400811
812 return &udp[1];
813 }
814 default:
Simon Glass56ada7b2022-01-29 14:58:38 -0700815 /* If the uclass driver is missing, this will show NULL */
816 log_debug("unhandled device class: %s (%s)\n", dev->name,
817 dev_get_uclass_name(dev));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400818 return dp_fill(buf, dev->parent);
819 }
820}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400821
822static unsigned dp_part_size(struct blk_desc *desc, int part)
823{
824 unsigned dpsize;
Simon Glassec209a72022-01-29 14:58:39 -0700825 struct udevice *dev = desc->bdev;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400826
Simon Glass222f3cb2021-09-24 18:30:17 -0600827 dpsize = dp_size(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400828
829 if (part == 0) /* the actual disk, not a partition */
830 return dpsize;
831
832 if (desc->part_type == PART_TYPE_ISO)
833 dpsize += sizeof(struct efi_device_path_cdrom_path);
834 else
835 dpsize += sizeof(struct efi_device_path_hard_drive_path);
836
837 return dpsize;
838}
839
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100840/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100841 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100842 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200843 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100844 * @desc block device descriptor
845 * @part partition number, 0 identifies a block device
846 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100847static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400848{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600849 struct disk_partition info;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400850
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400851 part_get_info(desc, part, &info);
852
853 if (desc->part_type == PART_TYPE_ISO) {
854 struct efi_device_path_cdrom_path *cddp = buf;
855
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100856 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400857 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
858 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
859 cddp->dp.length = sizeof(*cddp);
860 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200861 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400862
863 buf = &cddp[1];
864 } else {
865 struct efi_device_path_hard_drive_path *hddp = buf;
866
867 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
868 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
869 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100870 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400871 hddp->partition_start = info.start;
872 hddp->partition_end = info.size;
873 if (desc->part_type == PART_TYPE_EFI)
874 hddp->partmap_type = 2;
875 else
876 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100877
878 switch (desc->sig_type) {
879 case SIG_TYPE_NONE:
880 default:
881 hddp->signature_type = 0;
882 memset(hddp->partition_signature, 0,
883 sizeof(hddp->partition_signature));
884 break;
885 case SIG_TYPE_MBR:
886 hddp->signature_type = 1;
887 memset(hddp->partition_signature, 0,
888 sizeof(hddp->partition_signature));
889 memcpy(hddp->partition_signature, &desc->mbr_sig,
890 sizeof(desc->mbr_sig));
891 break;
892 case SIG_TYPE_GUID:
893 hddp->signature_type = 2;
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900894#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
895 /* info.uuid exists only with PARTITION_UUIDS */
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200896 if (uuid_str_to_bin(info.uuid,
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900897 hddp->partition_signature,
898 UUID_STR_FORMAT_GUID)) {
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200899 log_warning(
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900900 "Partition %d: invalid GUID %s\n",
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200901 part, info.uuid);
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900902 }
903#endif
Jonathan Gray84b4d702017-11-22 14:18:59 +1100904 break;
905 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400906
907 buf = &hddp[1];
908 }
909
910 return buf;
911}
912
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100913/*
914 * Create a device path for a block device or one of its partitions.
915 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200916 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100917 * @desc block device descriptor
918 * @part partition number, 0 identifies a block device
919 */
920static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
921{
Simon Glassec209a72022-01-29 14:58:39 -0700922 struct udevice *dev = desc->bdev;
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100923
Simon Glass222f3cb2021-09-24 18:30:17 -0600924 buf = dp_fill(buf, dev);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100925
926 if (part == 0) /* the actual disk, not a partition */
927 return buf;
928
929 return dp_part_node(buf, desc, part);
930}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400931
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200932/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400933struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
934{
935 void *buf, *start;
936
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100937 start = buf = efi_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100938 if (!buf)
939 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400940
941 buf = dp_part_fill(buf, desc, part);
942
943 *((struct efi_device_path *)buf) = END;
944
945 return start;
946}
947
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100948/*
949 * Create a device node for a block device partition.
950 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200951 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100952 * @desc block device descriptor
953 * @part partition number, 0 identifies a block device
954 */
955struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
956{
957 efi_uintn_t dpsize;
958 void *buf;
959
960 if (desc->part_type == PART_TYPE_ISO)
961 dpsize = sizeof(struct efi_device_path_cdrom_path);
962 else
963 dpsize = sizeof(struct efi_device_path_hard_drive_path);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100964 buf = efi_alloc(dpsize);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100965
Heinrich Schuchardte29fbb32022-10-06 13:36:02 +0200966 if (buf)
967 dp_part_node(buf, desc, part);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100968
969 return buf;
970}
971
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200972/**
973 * path_to_uefi() - convert UTF-8 path to an UEFI style path
974 *
975 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
976 * separators and UTF-16).
977 *
978 * @src: source buffer
979 * @uefi: target buffer, possibly unaligned
980 */
981static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400982{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200983 u16 *pos = uefi;
984
985 /*
986 * efi_set_bootdev() calls this routine indirectly before the UEFI
987 * subsystem is initialized. So we cannot assume unaligned access to be
988 * enabled.
989 */
990 allow_unaligned();
991
992 while (*src) {
993 s32 code = utf8_get(&src);
994
995 if (code < 0)
996 code = '?';
997 else if (code == '/')
998 code = '\\';
999 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001000 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +02001001 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001002}
1003
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +00001004/**
1005 * efi_dp_from_file() - create device path for file
1006 *
1007 * The function creates a device path from the block descriptor @desc and the
1008 * partition number @part and appends a device path node created describing the
1009 * file path @path.
1010 *
1011 * If @desc is NULL, the device path will not contain nodes describing the
1012 * partition.
1013 * If @path is an empty string "", the device path will not contain a node
1014 * for the file path.
1015 *
1016 * @desc: block device descriptor or NULL
1017 * @part: partition number
1018 * @path: file path on partition or ""
1019 * Return: device path or NULL in case of an error
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001020 */
1021struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
1022 const char *path)
1023{
1024 struct efi_device_path_file_path *fp;
1025 void *buf, *start;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001026 size_t dpsize = 0, fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001027
1028 if (desc)
1029 dpsize = dp_part_size(desc, part);
1030
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +02001031 fpsize = sizeof(struct efi_device_path) +
1032 2 * (utf8_utf16_strlen(path) + 1);
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001033 if (fpsize > U16_MAX)
1034 return NULL;
1035
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001036 dpsize += fpsize;
1037
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001038 start = buf = efi_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001039 if (!buf)
1040 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001041
1042 if (desc)
1043 buf = dp_part_fill(buf, desc, part);
1044
1045 /* add file-path: */
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +00001046 if (*path) {
1047 fp = buf;
1048 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
1049 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
1050 fp->dp.length = (u16)fpsize;
1051 path_to_uefi(fp->str, path);
1052 buf += fpsize;
1053 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001054
1055 *((struct efi_device_path *)buf) = END;
1056
1057 return start;
1058}
1059
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001060struct efi_device_path *efi_dp_from_uart(void)
1061{
1062 void *buf, *pos;
1063 struct efi_device_path_uart *uart;
1064 size_t dpsize = sizeof(ROOT) + sizeof(*uart) + sizeof(END);
1065
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001066 buf = efi_alloc(dpsize);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001067 if (!buf)
1068 return NULL;
1069 pos = buf;
1070 memcpy(pos, &ROOT, sizeof(ROOT));
1071 pos += sizeof(ROOT);
1072 uart = pos;
1073 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1074 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
1075 uart->dp.length = sizeof(*uart);
1076 pos += sizeof(*uart);
1077 memcpy(pos, &END, sizeof(END));
1078
1079 return buf;
1080}
1081
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001082struct efi_device_path __maybe_unused *efi_dp_from_eth(void)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001083{
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001084 void *buf, *start;
1085 unsigned dpsize = 0;
1086
1087 assert(eth_get_dev());
1088
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001089 dpsize += dp_size(eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001090
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001091 start = buf = efi_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001092 if (!buf)
1093 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001094
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001095 buf = dp_fill(buf, eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001096
1097 *((struct efi_device_path *)buf) = END;
1098
1099 return start;
1100}
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001101
Rob Clark18ceba72017-10-10 08:23:06 -04001102/* Construct a device-path for memory-mapped image */
1103struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1104 uint64_t start_address,
1105 uint64_t end_address)
1106{
1107 struct efi_device_path_memory *mdp;
1108 void *buf, *start;
1109
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001110 start = buf = efi_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001111 if (!buf)
1112 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -04001113
1114 mdp = buf;
1115 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1116 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1117 mdp->dp.length = sizeof(*mdp);
1118 mdp->memory_type = memory_type;
1119 mdp->start_address = start_address;
1120 mdp->end_address = end_address;
1121 buf = &mdp[1];
1122
1123 *((struct efi_device_path *)buf) = END;
1124
1125 return start;
1126}
1127
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001128/**
1129 * efi_dp_split_file_path() - split of relative file path from device path
1130 *
1131 * Given a device path indicating a file on a device, separate the device
1132 * path in two: the device path of the actual device and the file path
1133 * relative to this device.
1134 *
1135 * @full_path: device path including device and file path
1136 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001137 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001138 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001139 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001140efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1141 struct efi_device_path **device_path,
1142 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001143{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001144 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001145
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001146 *device_path = NULL;
1147 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001148 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001149 if (!dp)
1150 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001151 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001152 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001153 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001154 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001155 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001156 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001157 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001158 if (!fp)
1159 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001160 p->type = DEVICE_PATH_TYPE_END;
1161 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1162 p->length = sizeof(*p);
1163
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001164out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001165 *device_path = dp;
1166 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001167 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001168}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001169
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001170/**
1171 * efi_dp_from_name() - convert U-Boot device and file path to device path
1172 *
1173 * @dev: U-Boot device, e.g. 'mmc'
1174 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1175 * @path: file path relative to U-Boot device, may be NULL
1176 * @device: pointer to receive device path of the device
1177 * @file: pointer to receive device path for the file
1178 * Return: status code
1179 */
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001180efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1181 const char *path,
1182 struct efi_device_path **device,
1183 struct efi_device_path **file)
1184{
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001185 struct blk_desc *desc = NULL;
Simon Glassc1c4a8f2020-05-10 11:39:57 -06001186 struct disk_partition fs_partition;
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001187 size_t image_size;
1188 void *image_addr;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001189 int part = 0;
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001190 char *filename;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001191 char *s;
1192
AKASHI Takahiro39844412018-11-05 18:06:40 +09001193 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001194 return EFI_INVALID_PARAMETER;
1195
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001196 if (!strcmp(dev, "Mem") || !strcmp(dev, "hostfs")) {
Heinrich Schuchardtaecb9e22023-05-12 20:18:10 +02001197 /* loadm command and semihosting */
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001198 efi_get_image_parameters(&image_addr, &image_size);
1199
1200 if (device)
1201 *device = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
1202 (uintptr_t)image_addr,
1203 image_size);
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001204 } else if (IS_ENABLED(CONFIG_NETDEVICES) && !strcmp(dev, "Net")) {
1205 if (device)
1206 *device = efi_dp_from_eth();
1207 } else if (!strcmp(dev, "Uart")) {
1208 if (device)
1209 *device = efi_dp_from_uart();
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001210 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001211 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1212 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001213 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001214 return EFI_INVALID_PARAMETER;
1215
AKASHI Takahiro39844412018-11-05 18:06:40 +09001216 if (device)
1217 *device = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001218 }
1219
1220 if (!path)
1221 return EFI_SUCCESS;
1222
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001223 filename = calloc(1, strlen(path) + 1);
1224 if (!filename)
1225 return EFI_OUT_OF_RESOURCES;
1226
1227 sprintf(filename, "%s", path);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001228 /* DOS style file path: */
1229 s = filename;
1230 while ((s = strchr(s, '/')))
1231 *s++ = '\\';
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001232 *file = efi_dp_from_file(desc, part, filename);
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001233 free(filename);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001234
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001235 if (!*file)
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001236 return EFI_INVALID_PARAMETER;
1237
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001238 return EFI_SUCCESS;
1239}
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001240
1241/**
1242 * efi_dp_check_length() - check length of a device path
1243 *
1244 * @dp: pointer to device path
1245 * @maxlen: maximum length of the device path
1246 * Return:
1247 * * length of the device path if it is less or equal @maxlen
1248 * * -1 if the device path is longer then @maxlen
1249 * * -1 if a device path node has a length of less than 4
1250 * * -EINVAL if maxlen exceeds SSIZE_MAX
1251 */
1252ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1253 const size_t maxlen)
1254{
1255 ssize_t ret = 0;
1256 u16 len;
1257
1258 if (maxlen > SSIZE_MAX)
1259 return -EINVAL;
1260 for (;;) {
1261 len = dp->length;
1262 if (len < 4)
1263 return -1;
1264 ret += len;
1265 if (ret > maxlen)
1266 return -1;
1267 if (dp->type == DEVICE_PATH_TYPE_END &&
1268 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1269 return ret;
1270 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1271 }
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001272}
1273
1274/**
1275 * efi_dp_from_lo() - Get the instance of a VenMedia node in a
1276 * multi-instance device path that matches
1277 * a specific GUID. This kind of device paths
1278 * is found in Boot#### options describing an
1279 * initrd location
1280 *
1281 * @lo: EFI_LOAD_OPTION containing a valid device path
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001282 * @guid: guid to search for
1283 *
1284 * Return:
1285 * device path including the VenMedia node or NULL.
1286 * Caller must free the returned value.
1287 */
1288struct
1289efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001290 const efi_guid_t *guid)
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001291{
1292 struct efi_device_path *fp = lo->file_path;
1293 struct efi_device_path_vendor *vendor;
1294 int lo_len = lo->file_path_length;
1295
1296 for (; lo_len >= sizeof(struct efi_device_path);
1297 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1298 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1299 break;
1300 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1301 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1302 continue;
1303
1304 vendor = (struct efi_device_path_vendor *)fp;
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001305 if (!guidcmp(&vendor->guid, guid))
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +02001306 return efi_dp_dup(efi_dp_next(fp));
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001307 }
1308 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1309
1310 return NULL;
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001311}
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001312
1313/**
1314 * search_gpt_dp_node() - search gpt device path node
1315 *
1316 * @device_path: device path
1317 *
1318 * Return: pointer to the gpt device path node
1319 */
1320struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1321{
1322 struct efi_device_path *dp = device_path;
1323
1324 while (dp) {
1325 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1326 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1327 struct efi_device_path_hard_drive_path *hd_dp =
1328 (struct efi_device_path_hard_drive_path *)dp;
1329
1330 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1331 hd_dp->signature_type == SIG_TYPE_GUID)
1332 return dp;
1333 }
1334 dp = efi_dp_next(dp);
1335 }
1336
1337 return NULL;
1338}