blob: 818f993f7cc02b8b8fc8281d7f3a0f141e3ba0f2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Clarkf90cb9f2017-09-13 18:05:28 -04002/*
3 * EFI device path from u-boot device-model mapping
4 *
5 * (C) Copyright 2017 Rob Clark
Rob Clarkf90cb9f2017-09-13 18:05:28 -04006 */
7
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +02008#define LOG_CATEGORY LOGC_EFI
9
Rob Clarkf90cb9f2017-09-13 18:05:28 -040010#include <blk.h>
11#include <dm.h>
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +020012#include <dm/root.h>
Simon Glass37972f42025-05-24 11:28:21 -060013#include <efi_device_path.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <net.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040016#include <usb.h>
17#include <mmc.h>
Patrick Wildta3ca37e2019-10-03 16:24:17 +020018#include <nvme.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040019#include <efi_loader.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040020#include <part.h>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010021#include <u-boot/uuid.h>
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +020022#include <asm-generic/unaligned.h>
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +090023#include <linux/compat.h> /* U16_MAX */
Rob Clarkf90cb9f2017-09-13 18:05:28 -040024
25/* template END node: */
Masahisa Kojima97bd9da2022-06-19 13:55:59 +090026const struct efi_device_path END = {
Rob Clarkf90cb9f2017-09-13 18:05:28 -040027 .type = DEVICE_PATH_TYPE_END,
28 .sub_type = DEVICE_PATH_SUB_TYPE_END,
29 .length = sizeof(END),
30};
31
Simon Glass222f3cb2021-09-24 18:30:17 -060032#if defined(CONFIG_MMC)
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010033/*
34 * Determine if an MMC device is an SD card.
35 *
36 * @desc block device descriptor
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010037 * Return: true if the device is an SD card
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010038 */
39static bool is_sd(struct blk_desc *desc)
40{
41 struct mmc *mmc = find_mmc_device(desc->devnum);
42
43 if (!mmc)
44 return false;
45
46 return IS_SD(mmc) != 0U;
47}
48#endif
49
Rob Clarkf90cb9f2017-09-13 18:05:28 -040050struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
51{
52 if (dp == NULL)
53 return NULL;
54 if (dp->type == DEVICE_PATH_TYPE_END)
55 return NULL;
56 dp = ((void *)dp) + dp->length;
57 if (dp->type == DEVICE_PATH_TYPE_END)
58 return NULL;
59 return (struct efi_device_path *)dp;
60}
61
Heinrich Schuchardt753e2482017-10-26 19:25:48 +020062int efi_dp_match(const struct efi_device_path *a,
63 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -040064{
65 while (1) {
66 int ret;
67
68 ret = memcmp(&a->length, &b->length, sizeof(a->length));
69 if (ret)
70 return ret;
71
72 ret = memcmp(a, b, a->length);
73 if (ret)
74 return ret;
75
76 a = efi_dp_next(a);
77 b = efi_dp_next(b);
78
79 if (!a || !b)
80 return 0;
81 }
82}
83
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +010084struct efi_device_path *efi_dp_shorten(struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -040085{
86 while (dp) {
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +020087 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_WWI) ||
Rob Clarkf90cb9f2017-09-13 18:05:28 -040088 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
89 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
90 return dp;
91
92 dp = efi_dp_next(dp);
93 }
94
95 return dp;
96}
97
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +010098/**
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +010099 * find_handle() - find handle by device path and installed protocol
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100100 *
101 * If @rem is provided, the handle with the longest partial match is returned.
102 *
103 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100104 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100105 * @short_path: use short form device path for matching
106 * @rem: pointer to receive remaining device path
107 * Return: matching handle
108 */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100109static efi_handle_t find_handle(struct efi_device_path *dp,
110 const efi_guid_t *guid, bool short_path,
111 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400112{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100113 efi_handle_t handle, best_handle = NULL;
114 efi_uintn_t len, best_len = 0;
115
116 len = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400117
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100118 list_for_each_entry(handle, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100119 struct efi_handler *handler;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100120 struct efi_device_path *dp_current;
121 efi_uintn_t len_current;
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100122 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400123
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100124 if (guid) {
125 ret = efi_search_protocol(handle, guid, &handler);
126 if (ret != EFI_SUCCESS)
127 continue;
128 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100129 ret = efi_search_protocol(handle, &efi_guid_device_path,
130 &handler);
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100131 if (ret != EFI_SUCCESS)
132 continue;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100133 dp_current = handler->protocol_interface;
134 if (short_path) {
135 dp_current = efi_dp_shorten(dp_current);
136 if (!dp_current)
137 continue;
138 }
139 len_current = efi_dp_instance_size(dp_current);
140 if (rem) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100141 if (len_current > len)
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100142 continue;
143 } else {
144 if (len_current != len)
145 continue;
146 }
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100147 if (memcmp(dp_current, dp, len_current))
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100148 continue;
149 if (!rem)
150 return handle;
151 if (len_current > best_len) {
152 best_len = len_current;
153 best_handle = handle;
154 *rem = (void*)((u8 *)dp + len_current);
155 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400156 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100157 return best_handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400158}
159
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100160efi_handle_t efi_dp_find_obj(struct efi_device_path *dp,
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100161 const efi_guid_t *guid,
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100162 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400163{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100164 efi_handle_t handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400165
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100166 handle = find_handle(dp, guid, false, rem);
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100167 if (!handle)
168 /* Match short form device path */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100169 handle = find_handle(dp, guid, true, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400170
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100171 return handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400172}
173
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100174const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
175{
176 struct efi_device_path *ret;
177
178 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
179 return NULL;
180 while (dp) {
181 ret = (struct efi_device_path *)dp;
182 dp = efi_dp_next(dp);
183 }
184 return ret;
185}
186
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200187efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400188{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200189 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400190
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200191 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
192 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400193 while (dp) {
194 sz += dp->length;
195 dp = efi_dp_next(dp);
196 }
197
198 return sz;
199}
200
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200201efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
202{
203 const struct efi_device_path *p = dp;
204
205 if (!p)
206 return 0;
207 while (p->type != DEVICE_PATH_TYPE_END ||
208 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
209 p = (void *)p + p->length;
210
211 return (void *)p - (void *)dp;
212}
213
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400214struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
215{
216 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200217 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400218
219 if (!dp)
220 return NULL;
221
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100222 ndp = efi_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100223 if (!ndp)
224 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400225 memcpy(ndp, dp, sz);
226
227 return ndp;
228}
229
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200230struct
231efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
232 const struct efi_device_path *dp2,
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +0200233 size_t split_end_node)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400234{
235 struct efi_device_path *ret;
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200236 size_t end_size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400237
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200238 if (!dp1 && !dp2) {
239 /* return an end node */
240 ret = efi_dp_dup(&END);
241 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400242 ret = efi_dp_dup(dp2);
243 } else if (!dp2) {
244 ret = efi_dp_dup(dp1);
245 } else {
246 /* both dp1 and dp2 are non-null */
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +0200247 size_t sz1;
248 size_t sz2 = efi_dp_size(dp2);
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200249 void *p;
250
Heinrich Schuchardtf8de0092024-05-24 14:54:26 +0200251 if (split_end_node < sizeof(struct efi_device_path))
252 sz1 = efi_dp_size(dp1);
253 else
254 sz1 = split_end_node;
255
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200256 if (split_end_node)
257 end_size = 2 * sizeof(END);
258 else
259 end_size = sizeof(END);
260 p = efi_alloc(sz1 + sz2 + end_size);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100261 if (!p)
262 return NULL;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200263 ret = p;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400264 memcpy(p, dp1, sz1);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200265 p += sz1;
266
Ilias Apalodimas5dcefa72024-01-08 10:55:33 +0200267 if (split_end_node) {
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200268 memcpy(p, &END, sizeof(END));
269 p += sizeof(END);
270 }
271
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200272 /* the end node of the second device path has to be retained */
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200273 memcpy(p, dp2, sz2);
274 p += sz2;
275 memcpy(p, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400276 }
277
278 return ret;
279}
280
281struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
282 const struct efi_device_path *node)
283{
284 struct efi_device_path *ret;
285
286 if (!node && !dp) {
287 ret = efi_dp_dup(&END);
288 } else if (!node) {
289 ret = efi_dp_dup(dp);
290 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200291 size_t sz = node->length;
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100292 void *p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100293 if (!p)
294 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400295 memcpy(p, node, sz);
296 memcpy(p + sz, &END, sizeof(END));
297 ret = p;
298 } else {
299 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200300 size_t sz = efi_dp_size(dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100301 void *p = efi_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100302 if (!p)
303 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400304 memcpy(p, dp, sz);
305 memcpy(p + sz, node, node->length);
306 memcpy(p + sz + node->length, &END, sizeof(END));
307 ret = p;
308 }
309
310 return ret;
311}
312
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200313struct efi_device_path *efi_dp_create_device_node(const u8 type,
314 const u8 sub_type,
315 const u16 length)
316{
317 struct efi_device_path *ret;
318
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200319 if (length < sizeof(struct efi_device_path))
320 return NULL;
321
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100322 ret = efi_alloc(length);
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200323 if (!ret)
324 return ret;
325 ret->type = type;
326 ret->sub_type = sub_type;
327 ret->length = length;
328 return ret;
329}
330
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200331struct efi_device_path *efi_dp_append_instance(
332 const struct efi_device_path *dp,
333 const struct efi_device_path *dpi)
334{
335 size_t sz, szi;
336 struct efi_device_path *p, *ret;
337
338 if (!dpi)
339 return NULL;
340 if (!dp)
341 return efi_dp_dup(dpi);
342 sz = efi_dp_size(dp);
343 szi = efi_dp_instance_size(dpi);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100344 p = efi_alloc(sz + szi + 2 * sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200345 if (!p)
346 return NULL;
347 ret = p;
348 memcpy(p, dp, sz + sizeof(END));
349 p = (void *)p + sz;
350 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
351 p = (void *)p + sizeof(END);
352 memcpy(p, dpi, szi);
353 p = (void *)p + szi;
354 memcpy(p, &END, sizeof(END));
355 return ret;
356}
357
358struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
359 efi_uintn_t *size)
360{
361 size_t sz;
362 struct efi_device_path *p;
363
364 if (size)
365 *size = 0;
366 if (!dp || !*dp)
367 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200368 sz = efi_dp_instance_size(*dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100369 p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200370 if (!p)
371 return NULL;
372 memcpy(p, *dp, sz + sizeof(END));
373 *dp = (void *)*dp + sz;
374 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
375 *dp = (void *)*dp + sizeof(END);
376 else
377 *dp = NULL;
378 if (size)
379 *size = sz + sizeof(END);
380 return p;
381}
382
383bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
384{
385 const struct efi_device_path *p = dp;
386
387 if (!p)
388 return false;
389 while (p->type != DEVICE_PATH_TYPE_END)
390 p = (void *)p + p->length;
391 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
392}
393
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100394__maybe_unused static unsigned int dp_size(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400395{
396 if (!dev || !dev->driver)
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200397 return sizeof(struct efi_device_path_udevice);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400398
Simon Glass56ada7b2022-01-29 14:58:38 -0700399 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400400 case UCLASS_ROOT:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400401 /* stop traversing parents at this point: */
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200402 return sizeof(struct efi_device_path_udevice);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100403 case UCLASS_ETH:
404 return dp_size(dev->parent) +
405 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100406 case UCLASS_BLK:
407 switch (dev->parent->uclass->uc_drv->id) {
408#ifdef CONFIG_IDE
409 case UCLASS_IDE:
410 return dp_size(dev->parent) +
411 sizeof(struct efi_device_path_atapi);
412#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600413#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100414 case UCLASS_SCSI:
415 return dp_size(dev->parent) +
416 sizeof(struct efi_device_path_scsi);
417#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600418#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100419 case UCLASS_MMC:
420 return dp_size(dev->parent) +
421 sizeof(struct efi_device_path_sd_mmc_path);
422#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200423#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
424 case UCLASS_AHCI:
425 return dp_size(dev->parent) +
426 sizeof(struct efi_device_path_sata);
427#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200428#if defined(CONFIG_NVME)
429 case UCLASS_NVME:
430 return dp_size(dev->parent) +
431 sizeof(struct efi_device_path_nvme);
432#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100433#ifdef CONFIG_USB
434 case UCLASS_MASS_STORAGE:
435 return dp_size(dev->parent)
436 + sizeof(struct efi_device_path_controller);
437#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100438 default:
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200439 /* UCLASS_BLKMAP, UCLASS_HOST, UCLASS_VIRTIO */
440 return dp_size(dev->parent) +
441 sizeof(struct efi_device_path_udevice);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100442 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600443#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400444 case UCLASS_MMC:
445 return dp_size(dev->parent) +
446 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100447#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400448 case UCLASS_MASS_STORAGE:
449 case UCLASS_USB_HUB:
450 return dp_size(dev->parent) +
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100451 sizeof(struct efi_device_path_usb);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400452 default:
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200453 return dp_size(dev->parent) +
454 sizeof(struct efi_device_path_udevice);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400455 }
456}
457
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100458/*
459 * Recursively build a device path.
460 *
461 * @buf pointer to the end of the device path
462 * @dev device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100463 * Return: pointer to the end of the device path
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100464 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100465__maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400466{
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200467 enum uclass_id uclass_id;
468
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400469 if (!dev || !dev->driver)
470 return buf;
471
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200472 uclass_id = device_get_uclass_id(dev);
473 if (uclass_id != UCLASS_ROOT)
474 buf = dp_fill(buf, dev->parent);
475
476 switch (uclass_id) {
Jan Kiszkaf1389822022-10-14 18:10:06 +0200477#ifdef CONFIG_NETDEVICES
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100478 case UCLASS_ETH: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200479 struct efi_device_path_mac_addr *dp = buf;
Simon Glass95588622020-12-22 19:30:28 -0700480 struct eth_pdata *pdata = dev_get_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100481
482 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
483 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
484 dp->dp.length = sizeof(*dp);
485 memset(&dp->mac, 0, sizeof(dp->mac));
486 /* We only support IPv4 */
487 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
488 /* Ethernet */
489 dp->if_type = 1;
490 return &dp[1];
491 }
492#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100493 case UCLASS_BLK:
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200494 switch (device_get_uclass_id(dev->parent)) {
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100495#ifdef CONFIG_IDE
496 case UCLASS_IDE: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200497 struct efi_device_path_atapi *dp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700498 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100499
500 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
501 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
502 dp->dp.length = sizeof(*dp);
503 dp->logical_unit_number = desc->devnum;
504 dp->primary_secondary = IDE_BUS(desc->devnum);
505 dp->slave_master = desc->devnum %
506 (CONFIG_SYS_IDE_MAXDEVICE /
507 CONFIG_SYS_IDE_MAXBUS);
508 return &dp[1];
509 }
510#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600511#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100512 case UCLASS_SCSI: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200513 struct efi_device_path_scsi *dp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700514 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100515
516 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
517 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
518 dp->dp.length = sizeof(*dp);
519 dp->logical_unit_number = desc->lun;
520 dp->target_id = desc->target;
521 return &dp[1];
522 }
523#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600524#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100525 case UCLASS_MMC: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200526 struct efi_device_path_sd_mmc_path *sddp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700527 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100528
529 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
530 sddp->dp.sub_type = is_sd(desc) ?
531 DEVICE_PATH_SUB_TYPE_MSG_SD :
532 DEVICE_PATH_SUB_TYPE_MSG_MMC;
533 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700534 sddp->slot_number = dev_seq(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100535 return &sddp[1];
536 }
537#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200538#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
539 case UCLASS_AHCI: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200540 struct efi_device_path_sata *dp = buf;
Simon Glass71fa5b42020-12-03 16:55:18 -0700541 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200542
543 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
544 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
545 dp->dp.length = sizeof(*dp);
546 dp->hba_port = desc->devnum;
547 /* default 0xffff implies no port multiplier */
548 dp->port_multiplier_port = 0xffff;
549 dp->logical_unit_number = desc->lun;
550 return &dp[1];
551 }
552#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200553#if defined(CONFIG_NVME)
554 case UCLASS_NVME: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200555 struct efi_device_path_nvme *dp = buf;
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200556 u32 ns_id;
557
558 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
559 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
560 dp->dp.length = sizeof(*dp);
561 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
562 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
563 return &dp[1];
564 }
565#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100566#if defined(CONFIG_USB)
567 case UCLASS_MASS_STORAGE: {
Heinrich Schuchardt232c9db2023-04-01 07:21:55 +0200568 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200569 struct efi_device_path_controller *dp = buf;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100570
571 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
572 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CONTROLLER;
573 dp->dp.length = sizeof(*dp);
574 dp->controller_number = desc->lun;
575 return &dp[1];
576 }
577#endif
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200578 default: {
579 /* UCLASS_BLKMAP, UCLASS_HOST, UCLASS_VIRTIO */
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200580 struct efi_device_path_udevice *dp = buf;
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200581 struct blk_desc *desc = dev_get_uclass_plat(dev);
582
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200583 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
584 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
585 dp->dp.length = sizeof(*dp);
586 memcpy(&dp->guid, &efi_u_boot_guid,
587 sizeof(efi_guid_t));
588 dp->uclass_id = (UCLASS_BLK & 0xffff) |
589 (desc->uclass_id << 16);
590 dp->dev_number = desc->devnum;
591
592 return &dp[1];
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100593 }
Heinrich Schuchardt8433a5d2023-07-21 00:03:46 +0200594 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600595#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400596 case UCLASS_MMC: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200597 struct efi_device_path_sd_mmc_path *sddp = buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400598 struct mmc *mmc = mmc_get_mmc_dev(dev);
599 struct blk_desc *desc = mmc_get_blk_desc(mmc);
600
601 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100602 sddp->dp.sub_type = is_sd(desc) ?
603 DEVICE_PATH_SUB_TYPE_MSG_SD :
604 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400605 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700606 sddp->slot_number = dev_seq(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400607
608 return &sddp[1];
609 }
610#endif
611 case UCLASS_MASS_STORAGE:
612 case UCLASS_USB_HUB: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200613 struct efi_device_path_usb *udp = buf;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100614
615 switch (device_get_uclass_id(dev->parent)) {
616 case UCLASS_USB_HUB: {
617 struct usb_device *udev = dev_get_parent_priv(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400618
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100619 udp->parent_port_number = udev->portnr;
620 break;
621 }
622 default:
623 udp->parent_port_number = 0;
624 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400625 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100626 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400627 udp->dp.length = sizeof(*udp);
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100628 udp->usb_interface = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400629
630 return &udp[1];
631 }
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200632 default: {
Heinrich Schuchardt3d347632023-07-21 08:34:18 +0200633 struct efi_device_path_udevice *vdp = buf;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200634
635 vdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
636 vdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
637 vdp->dp.length = sizeof(*vdp);
638 memcpy(&vdp->guid, &efi_u_boot_guid, sizeof(efi_guid_t));
639 vdp->uclass_id = uclass_id;
640 vdp->dev_number = dev->seq_;
641
642 return &vdp[1];
643 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400644 }
645}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400646
647static unsigned dp_part_size(struct blk_desc *desc, int part)
648{
649 unsigned dpsize;
Simon Glassec209a72022-01-29 14:58:39 -0700650 struct udevice *dev = desc->bdev;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400651
Simon Glass222f3cb2021-09-24 18:30:17 -0600652 dpsize = dp_size(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400653
654 if (part == 0) /* the actual disk, not a partition */
655 return dpsize;
656
657 if (desc->part_type == PART_TYPE_ISO)
658 dpsize += sizeof(struct efi_device_path_cdrom_path);
659 else
660 dpsize += sizeof(struct efi_device_path_hard_drive_path);
661
662 return dpsize;
663}
664
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100665/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100666 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100667 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200668 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100669 * @desc block device descriptor
670 * @part partition number, 0 identifies a block device
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200671 *
672 * Return: pointer to position after the node
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100673 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100674static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400675{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600676 struct disk_partition info;
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200677 int ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400678
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200679 ret = part_get_info(desc, part, &info);
680 if (ret < 0)
681 return buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400682
683 if (desc->part_type == PART_TYPE_ISO) {
684 struct efi_device_path_cdrom_path *cddp = buf;
685
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100686 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400687 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
688 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
689 cddp->dp.length = sizeof(*cddp);
690 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200691 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400692
693 buf = &cddp[1];
694 } else {
695 struct efi_device_path_hard_drive_path *hddp = buf;
696
697 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
698 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
699 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100700 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400701 hddp->partition_start = info.start;
702 hddp->partition_end = info.size;
703 if (desc->part_type == PART_TYPE_EFI)
704 hddp->partmap_type = 2;
705 else
706 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100707
708 switch (desc->sig_type) {
709 case SIG_TYPE_NONE:
710 default:
711 hddp->signature_type = 0;
712 memset(hddp->partition_signature, 0,
713 sizeof(hddp->partition_signature));
714 break;
715 case SIG_TYPE_MBR:
716 hddp->signature_type = 1;
717 memset(hddp->partition_signature, 0,
718 sizeof(hddp->partition_signature));
719 memcpy(hddp->partition_signature, &desc->mbr_sig,
720 sizeof(desc->mbr_sig));
721 break;
722 case SIG_TYPE_GUID:
723 hddp->signature_type = 2;
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900724#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
725 /* info.uuid exists only with PARTITION_UUIDS */
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200726 if (uuid_str_to_bin(info.uuid,
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900727 hddp->partition_signature,
728 UUID_STR_FORMAT_GUID)) {
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200729 log_warning(
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900730 "Partition %d: invalid GUID %s\n",
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200731 part, info.uuid);
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900732 }
733#endif
Jonathan Gray84b4d702017-11-22 14:18:59 +1100734 break;
735 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400736
737 buf = &hddp[1];
738 }
739
740 return buf;
741}
742
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100743/*
744 * Create a device path for a block device or one of its partitions.
745 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200746 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100747 * @desc block device descriptor
748 * @part partition number, 0 identifies a block device
749 */
750static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
751{
Simon Glassec209a72022-01-29 14:58:39 -0700752 struct udevice *dev = desc->bdev;
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100753
Simon Glass222f3cb2021-09-24 18:30:17 -0600754 buf = dp_fill(buf, dev);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100755
756 if (part == 0) /* the actual disk, not a partition */
757 return buf;
758
759 return dp_part_node(buf, desc, part);
760}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400761
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400762struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
763{
764 void *buf, *start;
765
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100766 start = buf = efi_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100767 if (!buf)
768 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400769
770 buf = dp_part_fill(buf, desc, part);
771
772 *((struct efi_device_path *)buf) = END;
773
774 return start;
775}
776
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100777struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
778{
779 efi_uintn_t dpsize;
780 void *buf;
781
782 if (desc->part_type == PART_TYPE_ISO)
783 dpsize = sizeof(struct efi_device_path_cdrom_path);
784 else
785 dpsize = sizeof(struct efi_device_path_hard_drive_path);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100786 buf = efi_alloc(dpsize);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100787
Heinrich Schuchardte29fbb32022-10-06 13:36:02 +0200788 if (buf)
789 dp_part_node(buf, desc, part);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100790
791 return buf;
792}
793
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200794/**
795 * path_to_uefi() - convert UTF-8 path to an UEFI style path
796 *
797 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
798 * separators and UTF-16).
799 *
800 * @src: source buffer
801 * @uefi: target buffer, possibly unaligned
802 */
803static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400804{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200805 u16 *pos = uefi;
806
807 /*
808 * efi_set_bootdev() calls this routine indirectly before the UEFI
809 * subsystem is initialized. So we cannot assume unaligned access to be
810 * enabled.
811 */
812 allow_unaligned();
813
814 while (*src) {
815 s32 code = utf8_get(&src);
816
817 if (code < 0)
818 code = '?';
819 else if (code == '/')
820 code = '\\';
821 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400822 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200823 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400824}
825
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200826struct efi_device_path *efi_dp_from_file(const struct efi_device_path *dp,
827 const char *path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400828{
829 struct efi_device_path_file_path *fp;
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200830 void *buf, *pos;
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200831 size_t dpsize, fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400832
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200833 dpsize = efi_dp_size(dp);
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200834 fpsize = sizeof(struct efi_device_path) +
835 2 * (utf8_utf16_strlen(path) + 1);
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900836 if (fpsize > U16_MAX)
837 return NULL;
838
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200839 buf = efi_alloc(dpsize + fpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100840 if (!buf)
841 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400842
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +0200843 memcpy(buf, dp, dpsize);
844 pos = buf + dpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400845
846 /* add file-path: */
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000847 if (*path) {
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200848 fp = pos;
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000849 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
850 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
851 fp->dp.length = (u16)fpsize;
852 path_to_uefi(fp->str, path);
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200853 pos += fpsize;
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000854 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400855
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200856 memcpy(pos, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400857
Heinrich Schuchardt16eff692023-05-13 10:18:24 +0200858 return buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400859}
860
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100861struct efi_device_path *efi_dp_from_uart(void)
862{
863 void *buf, *pos;
864 struct efi_device_path_uart *uart;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200865 size_t dpsize = dp_size(dm_root()) + sizeof(*uart) + sizeof(END);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100866
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100867 buf = efi_alloc(dpsize);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100868 if (!buf)
869 return NULL;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200870 pos = dp_fill(buf, dm_root());
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100871 uart = pos;
872 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
873 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
874 uart->dp.length = sizeof(*uart);
875 pos += sizeof(*uart);
876 memcpy(pos, &END, sizeof(END));
877
878 return buf;
879}
880
Adriano Cordova1d370312025-03-03 11:13:14 -0300881struct efi_device_path __maybe_unused *efi_dp_from_eth(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400882{
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400883 void *buf, *start;
884 unsigned dpsize = 0;
885
Adriano Cordova1d370312025-03-03 11:13:14 -0300886 assert(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400887
Adriano Cordova1d370312025-03-03 11:13:14 -0300888 dpsize += dp_size(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400889
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100890 start = buf = efi_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100891 if (!buf)
892 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400893
Adriano Cordova1d370312025-03-03 11:13:14 -0300894 buf = dp_fill(buf, dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400895
896 *((struct efi_device_path *)buf) = END;
897
898 return start;
899}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400900
Adriano Cordovae9be2da2024-12-04 00:05:18 -0300901/**
902 * efi_dp_from_ipv4() - set device path from IPv4 address
903 *
904 * Set the device path to an ethernet device path as provided by
905 * efi_dp_from_eth() concatenated with a device path of subtype
906 * DEVICE_PATH_SUB_TYPE_MSG_IPV4, and an END node.
907 *
908 * @ip: IPv4 local address
909 * @mask: network mask
910 * @srv: IPv4 remote/server address
Adriano Cordova1d370312025-03-03 11:13:14 -0300911 * @dev: net udevice
Adriano Cordovae9be2da2024-12-04 00:05:18 -0300912 * Return: pointer to device path, NULL on error
913 */
914static struct efi_device_path *efi_dp_from_ipv4(struct efi_ipv4_address *ip,
915 struct efi_ipv4_address *mask,
Adriano Cordova1d370312025-03-03 11:13:14 -0300916 struct efi_ipv4_address *srv,
917 struct udevice *dev)
Adriano Cordovae9be2da2024-12-04 00:05:18 -0300918{
919 struct efi_device_path *dp1, *dp2, *pos;
920 struct {
921 struct efi_device_path_ipv4 ipv4dp;
922 struct efi_device_path end;
923 } dp;
924
925 memset(&dp.ipv4dp, 0, sizeof(dp.ipv4dp));
926 dp.ipv4dp.dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
927 dp.ipv4dp.dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_IPV4;
928 dp.ipv4dp.dp.length = sizeof(dp.ipv4dp);
929 dp.ipv4dp.protocol = 6;
930 if (ip)
931 memcpy(&dp.ipv4dp.local_ip_address, ip, sizeof(*ip));
932 if (mask)
933 memcpy(&dp.ipv4dp.subnet_mask, mask, sizeof(*mask));
934 if (srv)
935 memcpy(&dp.ipv4dp.remote_ip_address, srv, sizeof(*srv));
936 pos = &dp.end;
937 memcpy(pos, &END, sizeof(END));
938
Adriano Cordova1d370312025-03-03 11:13:14 -0300939 dp1 = efi_dp_from_eth(dev);
Adriano Cordovae9be2da2024-12-04 00:05:18 -0300940 if (!dp1)
941 return NULL;
942
943 dp2 = efi_dp_concat(dp1, (const struct efi_device_path *)&dp, 0);
944
945 efi_free_pool(dp1);
946
947 return dp2;
948}
949
Adriano Cordova1d370312025-03-03 11:13:14 -0300950struct efi_device_path *efi_dp_from_http(const char *server, struct udevice *dev)
Adriano Cordova4bd5eca2024-12-04 00:05:22 -0300951{
952 struct efi_device_path *dp1, *dp2;
953 struct efi_device_path_uri *uridp;
954 efi_uintn_t uridp_len;
955 char *pos;
956 char tmp[128];
957 struct efi_ipv4_address ip;
958 struct efi_ipv4_address mask;
959
960 if ((server && strlen("http://") + strlen(server) + 1 > sizeof(tmp)) ||
961 (!server && IS_ENABLED(CONFIG_NET_LWIP)))
962 return NULL;
963
Adriano Cordova54674692025-03-03 11:13:15 -0300964 efi_net_get_addr(&ip, &mask, NULL, dev);
Adriano Cordova4bd5eca2024-12-04 00:05:22 -0300965
Adriano Cordova1d370312025-03-03 11:13:14 -0300966 dp1 = efi_dp_from_ipv4(&ip, &mask, NULL, dev);
Adriano Cordova4bd5eca2024-12-04 00:05:22 -0300967 if (!dp1)
968 return NULL;
969
Adriano Cordova1d370312025-03-03 11:13:14 -0300970
Adriano Cordova4bd5eca2024-12-04 00:05:22 -0300971 strcpy(tmp, "http://");
972
973 if (server) {
974 strlcat(tmp, server, sizeof(tmp));
975#if !IS_ENABLED(CONFIG_NET_LWIP)
Tom Rini19687542024-12-04 09:32:28 -0600976 } else {
Adriano Cordova4bd5eca2024-12-04 00:05:22 -0300977 ip_to_string(net_server_ip, tmp + strlen("http://"));
978#endif
979 }
980
981 uridp_len = sizeof(struct efi_device_path) + strlen(tmp) + 1;
982 uridp = efi_alloc(uridp_len + sizeof(END));
983 if (!uridp) {
984 log_err("Out of memory\n");
985 return NULL;
986 }
987 uridp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
988 uridp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_URI;
989 uridp->dp.length = uridp_len;
990 debug("device path: setting uri device path to %s\n", tmp);
991 memcpy(uridp->uri, tmp, strlen(tmp) + 1);
992
993 pos = (char *)uridp + uridp_len;
994 memcpy(pos, &END, sizeof(END));
995
996 dp2 = efi_dp_concat(dp1, (const struct efi_device_path *)uridp, 0);
997
998 efi_free_pool(uridp);
999 efi_free_pool(dp1);
1000
1001 return dp2;
1002}
1003
Rob Clark18ceba72017-10-10 08:23:06 -04001004/* Construct a device-path for memory-mapped image */
1005struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1006 uint64_t start_address,
Moritz Fischerfe61a8b2024-11-04 01:49:34 +00001007 size_t size)
Rob Clark18ceba72017-10-10 08:23:06 -04001008{
1009 struct efi_device_path_memory *mdp;
1010 void *buf, *start;
1011
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001012 start = buf = efi_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001013 if (!buf)
1014 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -04001015
1016 mdp = buf;
1017 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1018 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1019 mdp->dp.length = sizeof(*mdp);
1020 mdp->memory_type = memory_type;
1021 mdp->start_address = start_address;
Moritz Fischerfe61a8b2024-11-04 01:49:34 +00001022 mdp->end_address = start_address + size;
Rob Clark18ceba72017-10-10 08:23:06 -04001023 buf = &mdp[1];
1024
1025 *((struct efi_device_path *)buf) = END;
1026
1027 return start;
1028}
1029
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001030/**
1031 * efi_dp_split_file_path() - split of relative file path from device path
1032 *
1033 * Given a device path indicating a file on a device, separate the device
1034 * path in two: the device path of the actual device and the file path
1035 * relative to this device.
1036 *
1037 * @full_path: device path including device and file path
1038 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001039 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001040 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001041 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001042efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1043 struct efi_device_path **device_path,
1044 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001045{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001046 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001047
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001048 *device_path = NULL;
1049 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001050 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001051 if (!dp)
1052 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001053 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001054 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001055 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001056 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001057 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001058 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001059 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001060 if (!fp)
1061 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001062 p->type = DEVICE_PATH_TYPE_END;
1063 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1064 p->length = sizeof(*p);
1065
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001066out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001067 *device_path = dp;
1068 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001069 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001070}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001071
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001072/**
1073 * efi_dp_from_name() - convert U-Boot device and file path to device path
1074 *
1075 * @dev: U-Boot device, e.g. 'mmc'
1076 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1077 * @path: file path relative to U-Boot device, may be NULL
1078 * @device: pointer to receive device path of the device
1079 * @file: pointer to receive device path for the file
1080 * Return: status code
1081 */
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001082efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1083 const char *path,
1084 struct efi_device_path **device,
1085 struct efi_device_path **file)
1086{
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001087 struct blk_desc *desc = NULL;
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001088 struct efi_device_path *dp;
Simon Glassc1c4a8f2020-05-10 11:39:57 -06001089 struct disk_partition fs_partition;
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001090 size_t image_size;
1091 void *image_addr;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001092 int part = 0;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001093
AKASHI Takahiro39844412018-11-05 18:06:40 +09001094 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001095 return EFI_INVALID_PARAMETER;
1096
AKASHI Takahiro9b08b9a2024-01-17 13:39:41 +09001097 if (IS_ENABLED(CONFIG_EFI_BINARY_EXEC) &&
1098 (!strcmp(dev, "Mem") || !strcmp(dev, "hostfs"))) {
Heinrich Schuchardtaecb9e22023-05-12 20:18:10 +02001099 /* loadm command and semihosting */
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001100 efi_get_image_parameters(&image_addr, &image_size);
1101
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001102 dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
Moritz Fischerfe61a8b2024-11-04 01:49:34 +00001103 (uintptr_t)image_addr, image_size);
Adriano Cordova93cba0f2024-12-04 00:05:23 -03001104 } else if (IS_ENABLED(CONFIG_NETDEVICES) &&
Adriano Cordova54674692025-03-03 11:13:15 -03001105 (!strcmp(dev, "Net") || !strcmp(dev, "Http"))) {
1106 efi_net_dp_from_dev(&dp, eth_get_dev(), false);
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001107 } else if (!strcmp(dev, "Uart")) {
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001108 dp = efi_dp_from_uart();
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001109 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001110 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1111 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001112 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001113 return EFI_INVALID_PARAMETER;
1114
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001115 dp = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001116 }
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001117 if (device)
1118 *device = dp;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001119
1120 if (!path)
1121 return EFI_SUCCESS;
1122
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +02001123 *file = efi_dp_from_file(dp, path);
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001124 if (!*file)
Heinrich Schuchardtbf0b3a12023-05-13 10:22:21 +02001125 return EFI_OUT_OF_RESOURCES;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001126
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001127 return EFI_SUCCESS;
1128}
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001129
1130/**
1131 * efi_dp_check_length() - check length of a device path
1132 *
1133 * @dp: pointer to device path
1134 * @maxlen: maximum length of the device path
1135 * Return:
1136 * * length of the device path if it is less or equal @maxlen
1137 * * -1 if the device path is longer then @maxlen
1138 * * -1 if a device path node has a length of less than 4
1139 * * -EINVAL if maxlen exceeds SSIZE_MAX
1140 */
1141ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1142 const size_t maxlen)
1143{
1144 ssize_t ret = 0;
1145 u16 len;
1146
1147 if (maxlen > SSIZE_MAX)
1148 return -EINVAL;
1149 for (;;) {
1150 len = dp->length;
1151 if (len < 4)
1152 return -1;
1153 ret += len;
1154 if (ret > maxlen)
1155 return -1;
1156 if (dp->type == DEVICE_PATH_TYPE_END &&
1157 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1158 return ret;
1159 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1160 }
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001161}
1162
1163/**
Heinrich Schuchardt2ca26802024-04-26 16:13:19 +02001164 * efi_dp_from_lo() - get device-path from load option
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001165 *
Heinrich Schuchardt2ca26802024-04-26 16:13:19 +02001166 * The load options in U-Boot may contain multiple concatenated device-paths.
1167 * The first device-path indicates the EFI binary to execute. Subsequent
1168 * device-paths start with a VenMedia node where the GUID identifies the
1169 * function (initrd or fdt).
1170 *
1171 * @lo: EFI load option containing a valid device path
1172 * @guid: GUID identifying device-path or NULL for the EFI binary
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001173 *
1174 * Return:
Heinrich Schuchardt2ca26802024-04-26 16:13:19 +02001175 * device path excluding the matched VenMedia node or NULL.
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001176 * Caller must free the returned value.
1177 */
1178struct
1179efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001180 const efi_guid_t *guid)
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001181{
1182 struct efi_device_path *fp = lo->file_path;
1183 struct efi_device_path_vendor *vendor;
1184 int lo_len = lo->file_path_length;
1185
Heinrich Schuchardt2ca26802024-04-26 16:13:19 +02001186 if (!guid)
1187 return efi_dp_dup(fp);
1188
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001189 for (; lo_len >= sizeof(struct efi_device_path);
1190 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1191 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1192 break;
1193 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1194 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1195 continue;
1196
1197 vendor = (struct efi_device_path_vendor *)fp;
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001198 if (!guidcmp(&vendor->guid, guid))
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +02001199 return efi_dp_dup(efi_dp_next(fp));
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001200 }
1201 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1202
1203 return NULL;
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001204}
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001205
1206/**
1207 * search_gpt_dp_node() - search gpt device path node
1208 *
1209 * @device_path: device path
1210 *
1211 * Return: pointer to the gpt device path node
1212 */
1213struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1214{
1215 struct efi_device_path *dp = device_path;
1216
1217 while (dp) {
1218 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1219 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1220 struct efi_device_path_hard_drive_path *hd_dp =
1221 (struct efi_device_path_hard_drive_path *)dp;
1222
1223 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1224 hd_dp->signature_type == SIG_TYPE_GUID)
1225 return dp;
1226 }
1227 dp = efi_dp_next(dp);
1228 }
1229
1230 return NULL;
1231}