blob: ddd5f132ec50a2c7b585fcbdda33f2c8a9880f77 [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>
AKASHI Takahiro659a6262019-09-12 13:52:35 +090020#include <sandboxblockdev.h>
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +020021#include <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
AKASHI Takahiro659a6262019-09-12 13:52:35 +090025#ifdef CONFIG_SANDBOX
26const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
27#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +020028#ifdef CONFIG_VIRTIO_BLK
29const efi_guid_t efi_guid_virtio_dev = U_BOOT_VIRTIO_DEV_GUID;
30#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +090031
Rob Clarkf90cb9f2017-09-13 18:05:28 -040032/* template END node: */
33static const struct efi_device_path END = {
34 .type = DEVICE_PATH_TYPE_END,
35 .sub_type = DEVICE_PATH_SUB_TYPE_END,
36 .length = sizeof(END),
37};
38
Rob Clarkf90cb9f2017-09-13 18:05:28 -040039/* template ROOT node: */
40static const struct efi_device_path_vendor ROOT = {
41 .dp = {
42 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
43 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
44 .length = sizeof(ROOT),
45 },
46 .guid = U_BOOT_GUID,
47};
48
Simon Glass222f3cb2021-09-24 18:30:17 -060049#if defined(CONFIG_MMC)
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010050/*
51 * Determine if an MMC device is an SD card.
52 *
53 * @desc block device descriptor
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010054 * Return: true if the device is an SD card
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010055 */
56static bool is_sd(struct blk_desc *desc)
57{
58 struct mmc *mmc = find_mmc_device(desc->devnum);
59
60 if (!mmc)
61 return false;
62
63 return IS_SD(mmc) != 0U;
64}
65#endif
66
Rob Clarkf90cb9f2017-09-13 18:05:28 -040067static void *dp_alloc(size_t sz)
68{
69 void *buf;
70
Heinrich Schuchardt16546012021-08-17 15:15:23 +020071 if (efi_allocate_pool(EFI_BOOT_SERVICES_DATA, sz, &buf) !=
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010072 EFI_SUCCESS) {
73 debug("EFI: ERROR: out of memory in %s\n", __func__);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040074 return NULL;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010075 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -040076
Patrick Wildtacf7bee2018-03-25 19:54:03 +020077 memset(buf, 0, sz);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040078 return buf;
79}
80
81/*
82 * Iterate to next block in device-path, terminating (returning NULL)
83 * at /End* node.
84 */
85struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
86{
87 if (dp == NULL)
88 return NULL;
89 if (dp->type == DEVICE_PATH_TYPE_END)
90 return NULL;
91 dp = ((void *)dp) + dp->length;
92 if (dp->type == DEVICE_PATH_TYPE_END)
93 return NULL;
94 return (struct efi_device_path *)dp;
95}
96
97/*
98 * Compare two device-paths, stopping when the shorter of the two hits
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +020099 * an End* node. This is useful to, for example, compare a device-path
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400100 * representing a device with one representing a file on the device, or
101 * a device with a parent device.
102 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +0200103int efi_dp_match(const struct efi_device_path *a,
104 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400105{
106 while (1) {
107 int ret;
108
109 ret = memcmp(&a->length, &b->length, sizeof(a->length));
110 if (ret)
111 return ret;
112
113 ret = memcmp(a, b, a->length);
114 if (ret)
115 return ret;
116
117 a = efi_dp_next(a);
118 b = efi_dp_next(b);
119
120 if (!a || !b)
121 return 0;
122 }
123}
124
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100125/**
126 * efi_dp_shorten() - shorten device-path
127 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200128 * We can have device paths that start with a USB WWID or a USB Class node,
129 * and a few other cases which don't encode the full device path with bus
130 * hierarchy:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400131 *
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100132 * * MESSAGING:USB_WWID
133 * * MESSAGING:USB_CLASS
134 * * MEDIA:FILE_PATH
135 * * MEDIA:HARD_DRIVE
136 * * MESSAGING:URI
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200137 *
138 * See UEFI spec (section 3.1.2, about short-form device-paths)
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100139 *
140 * @dp: original devie-path
141 * @Return: shortened device-path or NULL
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400142 */
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100143struct efi_device_path *efi_dp_shorten(struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400144{
145 while (dp) {
146 /*
147 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
148 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
149 * so not sure when we would see these other cases.
150 */
151 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
152 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
153 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
154 return dp;
155
156 dp = efi_dp_next(dp);
157 }
158
159 return dp;
160}
161
162static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
163 struct efi_device_path **rem)
164{
165 struct efi_object *efiobj;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200166 efi_uintn_t dp_size = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400167
168 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100169 struct efi_handler *handler;
170 struct efi_device_path *obj_dp;
171 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400172
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200173 ret = efi_search_protocol(efiobj,
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100174 &efi_guid_device_path, &handler);
175 if (ret != EFI_SUCCESS)
176 continue;
177 obj_dp = handler->protocol_interface;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400178
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100179 do {
180 if (efi_dp_match(dp, obj_dp) == 0) {
181 if (rem) {
Alexander Graff9360fb2017-12-11 14:29:46 +0100182 /*
183 * Allow partial matches, but inform
184 * the caller.
185 */
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100186 *rem = ((void *)dp) +
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200187 efi_dp_instance_size(obj_dp);
Alexander Graff9360fb2017-12-11 14:29:46 +0100188 return efiobj;
189 } else {
190 /* Only return on exact matches */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200191 if (efi_dp_instance_size(obj_dp) ==
192 dp_size)
Alexander Graff9360fb2017-12-11 14:29:46 +0100193 return efiobj;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400194 }
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100195 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400196
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100197 obj_dp = efi_dp_shorten(efi_dp_next(obj_dp));
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100198 } while (short_path && obj_dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400199 }
200
201 return NULL;
202}
203
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400204/*
205 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
206 * remaining part of the device path after the matched object.
207 */
208struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
209 struct efi_device_path **rem)
210{
211 struct efi_object *efiobj;
212
Alexander Graff9360fb2017-12-11 14:29:46 +0100213 /* Search for an exact match first */
214 efiobj = find_obj(dp, false, NULL);
215
216 /* Then for a fuzzy match */
217 if (!efiobj)
218 efiobj = find_obj(dp, false, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400219
Alexander Graff9360fb2017-12-11 14:29:46 +0100220 /* And now for a fuzzy short match */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400221 if (!efiobj)
222 efiobj = find_obj(dp, true, rem);
223
224 return efiobj;
225}
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
285 ndp = dp_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);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200329 void *p = dp_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;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400392 void *p = dp_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);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400401 void *p = dp_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 Schuchardtb41c8e22018-04-16 07:59:05 +0200422 ret = dp_alloc(length);
423 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);
444 p = dp_alloc(sz + szi + 2 * sizeof(END));
445 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);
469 p = dp_alloc(sz + sizeof(END));
470 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
538 case UCLASS_ROOT:
539 /*
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 Schuchardtc770aaa2020-05-20 22:39:35 +0200547#ifdef CONFIG_VIRTIO_BLK
548 case UCLASS_VIRTIO:
549 /*
550 * Virtio devices will be represented as a vendor
551 * device node with an extra byte for the device
552 * number.
553 */
554 return dp_size(dev->parent)
555 + sizeof(struct efi_device_path_vendor) + 1;
556#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100557 default:
558 return dp_size(dev->parent);
559 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600560#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400561 case UCLASS_MMC:
562 return dp_size(dev->parent) +
563 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100564#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400565 case UCLASS_MASS_STORAGE:
566 case UCLASS_USB_HUB:
567 return dp_size(dev->parent) +
568 sizeof(struct efi_device_path_usb_class);
569 default:
570 /* just skip over unknown classes: */
571 return dp_size(dev->parent);
572 }
573}
574
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100575/*
576 * Recursively build a device path.
577 *
578 * @buf pointer to the end of the device path
579 * @dev device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100580 * Return: pointer to the end of the device path
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100581 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100582__maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400583{
584 if (!dev || !dev->driver)
585 return buf;
586
Simon Glass56ada7b2022-01-29 14:58:38 -0700587 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400588 case UCLASS_ROOT:
589 case UCLASS_SIMPLE_BUS: {
590 /* stop traversing parents at this point: */
591 struct efi_device_path_vendor *vdp = buf;
592 *vdp = ROOT;
593 return &vdp[1];
594 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600595#ifdef CONFIG_NET
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100596 case UCLASS_ETH: {
597 struct efi_device_path_mac_addr *dp =
598 dp_fill(buf, dev->parent);
Simon Glass95588622020-12-22 19:30:28 -0700599 struct eth_pdata *pdata = dev_get_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100600
601 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
602 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
603 dp->dp.length = sizeof(*dp);
604 memset(&dp->mac, 0, sizeof(dp->mac));
605 /* We only support IPv4 */
606 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
607 /* Ethernet */
608 dp->if_type = 1;
609 return &dp[1];
610 }
611#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100612 case UCLASS_BLK:
613 switch (dev->parent->uclass->uc_drv->id) {
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900614#ifdef CONFIG_SANDBOX
615 case UCLASS_ROOT: {
616 /* stop traversing parents at this point: */
Heinrich Schuchardt1e3beaf2020-05-06 01:28:08 +0200617 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700618 struct blk_desc *desc = dev_get_uclass_plat(dev);
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900619
620 dp_fill(buf, dev->parent);
621 dp = buf;
622 ++dp;
623 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
624 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
625 dp->dp.length = sizeof(*dp) + 1;
626 memcpy(&dp->guid, &efi_guid_host_dev,
627 sizeof(efi_guid_t));
628 dp->vendor_data[0] = desc->devnum;
629 return &dp->vendor_data[1];
630 }
631#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200632#ifdef CONFIG_VIRTIO_BLK
633 case UCLASS_VIRTIO: {
634 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700635 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200636
637 dp_fill(buf, dev->parent);
638 dp = buf;
639 ++dp;
640 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
641 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
642 dp->dp.length = sizeof(*dp) + 1;
643 memcpy(&dp->guid, &efi_guid_virtio_dev,
644 sizeof(efi_guid_t));
645 dp->vendor_data[0] = desc->devnum;
646 return &dp->vendor_data[1];
647 }
648#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100649#ifdef CONFIG_IDE
650 case UCLASS_IDE: {
651 struct efi_device_path_atapi *dp =
652 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700653 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100654
655 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
656 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
657 dp->dp.length = sizeof(*dp);
658 dp->logical_unit_number = desc->devnum;
659 dp->primary_secondary = IDE_BUS(desc->devnum);
660 dp->slave_master = desc->devnum %
661 (CONFIG_SYS_IDE_MAXDEVICE /
662 CONFIG_SYS_IDE_MAXBUS);
663 return &dp[1];
664 }
665#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600666#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100667 case UCLASS_SCSI: {
668 struct efi_device_path_scsi *dp =
669 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700670 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100671
672 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
673 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
674 dp->dp.length = sizeof(*dp);
675 dp->logical_unit_number = desc->lun;
676 dp->target_id = desc->target;
677 return &dp[1];
678 }
679#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600680#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100681 case UCLASS_MMC: {
682 struct efi_device_path_sd_mmc_path *sddp =
683 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700684 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100685
686 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
687 sddp->dp.sub_type = is_sd(desc) ?
688 DEVICE_PATH_SUB_TYPE_MSG_SD :
689 DEVICE_PATH_SUB_TYPE_MSG_MMC;
690 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700691 sddp->slot_number = dev_seq(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100692 return &sddp[1];
693 }
694#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200695#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
696 case UCLASS_AHCI: {
697 struct efi_device_path_sata *dp =
698 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700699 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200700
701 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
702 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
703 dp->dp.length = sizeof(*dp);
704 dp->hba_port = desc->devnum;
705 /* default 0xffff implies no port multiplier */
706 dp->port_multiplier_port = 0xffff;
707 dp->logical_unit_number = desc->lun;
708 return &dp[1];
709 }
710#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200711#if defined(CONFIG_NVME)
712 case UCLASS_NVME: {
713 struct efi_device_path_nvme *dp =
714 dp_fill(buf, dev->parent);
715 u32 ns_id;
716
717 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
718 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
719 dp->dp.length = sizeof(*dp);
720 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
721 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
722 return &dp[1];
723 }
724#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100725 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100726 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
727 __FILE__, __LINE__, __func__,
728 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100729 return dp_fill(buf, dev->parent);
730 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600731#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400732 case UCLASS_MMC: {
733 struct efi_device_path_sd_mmc_path *sddp =
734 dp_fill(buf, dev->parent);
735 struct mmc *mmc = mmc_get_mmc_dev(dev);
736 struct blk_desc *desc = mmc_get_blk_desc(mmc);
737
738 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100739 sddp->dp.sub_type = is_sd(desc) ?
740 DEVICE_PATH_SUB_TYPE_MSG_SD :
741 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400742 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700743 sddp->slot_number = dev_seq(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400744
745 return &sddp[1];
746 }
747#endif
748 case UCLASS_MASS_STORAGE:
749 case UCLASS_USB_HUB: {
750 struct efi_device_path_usb_class *udp =
751 dp_fill(buf, dev->parent);
752 struct usb_device *udev = dev_get_parent_priv(dev);
753 struct usb_device_descriptor *desc = &udev->descriptor;
754
755 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
756 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
757 udp->dp.length = sizeof(*udp);
758 udp->vendor_id = desc->idVendor;
759 udp->product_id = desc->idProduct;
760 udp->device_class = desc->bDeviceClass;
761 udp->device_subclass = desc->bDeviceSubClass;
762 udp->device_protocol = desc->bDeviceProtocol;
763
764 return &udp[1];
765 }
766 default:
Simon Glass56ada7b2022-01-29 14:58:38 -0700767 /* If the uclass driver is missing, this will show NULL */
768 log_debug("unhandled device class: %s (%s)\n", dev->name,
769 dev_get_uclass_name(dev));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400770 return dp_fill(buf, dev->parent);
771 }
772}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400773
774static unsigned dp_part_size(struct blk_desc *desc, int part)
775{
776 unsigned dpsize;
Simon Glassec209a72022-01-29 14:58:39 -0700777 struct udevice *dev = desc->bdev;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400778
Simon Glass222f3cb2021-09-24 18:30:17 -0600779 dpsize = dp_size(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400780
781 if (part == 0) /* the actual disk, not a partition */
782 return dpsize;
783
784 if (desc->part_type == PART_TYPE_ISO)
785 dpsize += sizeof(struct efi_device_path_cdrom_path);
786 else
787 dpsize += sizeof(struct efi_device_path_hard_drive_path);
788
789 return dpsize;
790}
791
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100792/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100793 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100794 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200795 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100796 * @desc block device descriptor
797 * @part partition number, 0 identifies a block device
798 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100799static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400800{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600801 struct disk_partition info;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400802
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400803 part_get_info(desc, part, &info);
804
805 if (desc->part_type == PART_TYPE_ISO) {
806 struct efi_device_path_cdrom_path *cddp = buf;
807
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100808 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400809 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
810 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
811 cddp->dp.length = sizeof(*cddp);
812 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200813 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400814
815 buf = &cddp[1];
816 } else {
817 struct efi_device_path_hard_drive_path *hddp = buf;
818
819 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
820 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
821 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100822 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400823 hddp->partition_start = info.start;
824 hddp->partition_end = info.size;
825 if (desc->part_type == PART_TYPE_EFI)
826 hddp->partmap_type = 2;
827 else
828 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100829
830 switch (desc->sig_type) {
831 case SIG_TYPE_NONE:
832 default:
833 hddp->signature_type = 0;
834 memset(hddp->partition_signature, 0,
835 sizeof(hddp->partition_signature));
836 break;
837 case SIG_TYPE_MBR:
838 hddp->signature_type = 1;
839 memset(hddp->partition_signature, 0,
840 sizeof(hddp->partition_signature));
841 memcpy(hddp->partition_signature, &desc->mbr_sig,
842 sizeof(desc->mbr_sig));
843 break;
844 case SIG_TYPE_GUID:
845 hddp->signature_type = 2;
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200846 if (uuid_str_to_bin(info.uuid,
847 hddp->partition_signature, 1))
848 log_warning(
849 "Partition no. %d: invalid guid: %s\n",
850 part, info.uuid);
Jonathan Gray84b4d702017-11-22 14:18:59 +1100851 break;
852 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400853
854 buf = &hddp[1];
855 }
856
857 return buf;
858}
859
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100860/*
861 * Create a device path for a block device or one of its partitions.
862 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200863 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100864 * @desc block device descriptor
865 * @part partition number, 0 identifies a block device
866 */
867static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
868{
Simon Glassec209a72022-01-29 14:58:39 -0700869 struct udevice *dev = desc->bdev;
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100870
Simon Glass222f3cb2021-09-24 18:30:17 -0600871 buf = dp_fill(buf, dev);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100872
873 if (part == 0) /* the actual disk, not a partition */
874 return buf;
875
876 return dp_part_node(buf, desc, part);
877}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400878
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200879/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400880struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
881{
882 void *buf, *start;
883
884 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100885 if (!buf)
886 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400887
888 buf = dp_part_fill(buf, desc, part);
889
890 *((struct efi_device_path *)buf) = END;
891
892 return start;
893}
894
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100895/*
896 * Create a device node for a block device partition.
897 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200898 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100899 * @desc block device descriptor
900 * @part partition number, 0 identifies a block device
901 */
902struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
903{
904 efi_uintn_t dpsize;
905 void *buf;
906
907 if (desc->part_type == PART_TYPE_ISO)
908 dpsize = sizeof(struct efi_device_path_cdrom_path);
909 else
910 dpsize = sizeof(struct efi_device_path_hard_drive_path);
911 buf = dp_alloc(dpsize);
912
913 dp_part_node(buf, desc, part);
914
915 return buf;
916}
917
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200918/**
919 * path_to_uefi() - convert UTF-8 path to an UEFI style path
920 *
921 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
922 * separators and UTF-16).
923 *
924 * @src: source buffer
925 * @uefi: target buffer, possibly unaligned
926 */
927static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400928{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200929 u16 *pos = uefi;
930
931 /*
932 * efi_set_bootdev() calls this routine indirectly before the UEFI
933 * subsystem is initialized. So we cannot assume unaligned access to be
934 * enabled.
935 */
936 allow_unaligned();
937
938 while (*src) {
939 s32 code = utf8_get(&src);
940
941 if (code < 0)
942 code = '?';
943 else if (code == '/')
944 code = '\\';
945 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400946 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200947 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400948}
949
950/*
951 * If desc is NULL, this creates a path with only the file component,
952 * otherwise it creates a full path with both device and file components
953 */
954struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
955 const char *path)
956{
957 struct efi_device_path_file_path *fp;
958 void *buf, *start;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900959 size_t dpsize = 0, fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400960
961 if (desc)
962 dpsize = dp_part_size(desc, part);
963
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200964 fpsize = sizeof(struct efi_device_path) +
965 2 * (utf8_utf16_strlen(path) + 1);
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900966 if (fpsize > U16_MAX)
967 return NULL;
968
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400969 dpsize += fpsize;
970
971 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100972 if (!buf)
973 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400974
975 if (desc)
976 buf = dp_part_fill(buf, desc, part);
977
978 /* add file-path: */
979 fp = buf;
980 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
981 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900982 fp->dp.length = (u16)fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400983 path_to_uefi(fp->str, path);
984 buf += fpsize;
985
986 *((struct efi_device_path *)buf) = END;
987
988 return start;
989}
990
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +0100991struct efi_device_path *efi_dp_from_uart(void)
992{
993 void *buf, *pos;
994 struct efi_device_path_uart *uart;
995 size_t dpsize = sizeof(ROOT) + sizeof(*uart) + sizeof(END);
996
997 buf = dp_alloc(dpsize);
998 if (!buf)
999 return NULL;
1000 pos = buf;
1001 memcpy(pos, &ROOT, sizeof(ROOT));
1002 pos += sizeof(ROOT);
1003 uart = pos;
1004 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1005 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
1006 uart->dp.length = sizeof(*uart);
1007 pos += sizeof(*uart);
1008 memcpy(pos, &END, sizeof(END));
1009
1010 return buf;
1011}
1012
Joe Hershberger5277a972018-04-13 15:26:39 -05001013#ifdef CONFIG_NET
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001014struct efi_device_path *efi_dp_from_eth(void)
1015{
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001016 void *buf, *start;
1017 unsigned dpsize = 0;
1018
1019 assert(eth_get_dev());
1020
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001021 dpsize += dp_size(eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001022
1023 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001024 if (!buf)
1025 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001026
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001027 buf = dp_fill(buf, eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001028
1029 *((struct efi_device_path *)buf) = END;
1030
1031 return start;
1032}
1033#endif
1034
Rob Clark18ceba72017-10-10 08:23:06 -04001035/* Construct a device-path for memory-mapped image */
1036struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1037 uint64_t start_address,
1038 uint64_t end_address)
1039{
1040 struct efi_device_path_memory *mdp;
1041 void *buf, *start;
1042
1043 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001044 if (!buf)
1045 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -04001046
1047 mdp = buf;
1048 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1049 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1050 mdp->dp.length = sizeof(*mdp);
1051 mdp->memory_type = memory_type;
1052 mdp->start_address = start_address;
1053 mdp->end_address = end_address;
1054 buf = &mdp[1];
1055
1056 *((struct efi_device_path *)buf) = END;
1057
1058 return start;
1059}
1060
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001061/**
1062 * efi_dp_split_file_path() - split of relative file path from device path
1063 *
1064 * Given a device path indicating a file on a device, separate the device
1065 * path in two: the device path of the actual device and the file path
1066 * relative to this device.
1067 *
1068 * @full_path: device path including device and file path
1069 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001070 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001071 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001072 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001073efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1074 struct efi_device_path **device_path,
1075 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001076{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001077 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001078
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001079 *device_path = NULL;
1080 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001081 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001082 if (!dp)
1083 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001084 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001085 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001086 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001087 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001088 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001089 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001090 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001091 if (!fp)
1092 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001093 p->type = DEVICE_PATH_TYPE_END;
1094 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1095 p->length = sizeof(*p);
1096
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001097out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001098 *device_path = dp;
1099 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001100 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001101}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001102
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001103/**
1104 * efi_dp_from_name() - convert U-Boot device and file path to device path
1105 *
1106 * @dev: U-Boot device, e.g. 'mmc'
1107 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1108 * @path: file path relative to U-Boot device, may be NULL
1109 * @device: pointer to receive device path of the device
1110 * @file: pointer to receive device path for the file
1111 * Return: status code
1112 */
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001113efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1114 const char *path,
1115 struct efi_device_path **device,
1116 struct efi_device_path **file)
1117{
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001118 struct blk_desc *desc = NULL;
Simon Glassc1c4a8f2020-05-10 11:39:57 -06001119 struct disk_partition fs_partition;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001120 int part = 0;
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001121 char *filename;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001122 char *s;
1123
AKASHI Takahiro39844412018-11-05 18:06:40 +09001124 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001125 return EFI_INVALID_PARAMETER;
1126
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001127 if (!strcmp(dev, "Net")) {
1128#ifdef CONFIG_NET
1129 if (device)
1130 *device = efi_dp_from_eth();
1131#endif
1132 } else if (!strcmp(dev, "Uart")) {
1133 if (device)
1134 *device = efi_dp_from_uart();
1135 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001136 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1137 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001138 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001139 return EFI_INVALID_PARAMETER;
1140
AKASHI Takahiro39844412018-11-05 18:06:40 +09001141 if (device)
1142 *device = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001143 }
1144
1145 if (!path)
1146 return EFI_SUCCESS;
1147
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001148 filename = calloc(1, strlen(path) + 1);
1149 if (!filename)
1150 return EFI_OUT_OF_RESOURCES;
1151
1152 sprintf(filename, "%s", path);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001153 /* DOS style file path: */
1154 s = filename;
1155 while ((s = strchr(s, '/')))
1156 *s++ = '\\';
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001157 *file = efi_dp_from_file(desc, part, filename);
Heinrich Schuchardt158c0d72021-05-25 12:07:30 +02001158 free(filename);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001159
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001160 if (!*file)
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001161 return EFI_INVALID_PARAMETER;
1162
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001163 return EFI_SUCCESS;
1164}
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001165
1166/**
1167 * efi_dp_check_length() - check length of a device path
1168 *
1169 * @dp: pointer to device path
1170 * @maxlen: maximum length of the device path
1171 * Return:
1172 * * length of the device path if it is less or equal @maxlen
1173 * * -1 if the device path is longer then @maxlen
1174 * * -1 if a device path node has a length of less than 4
1175 * * -EINVAL if maxlen exceeds SSIZE_MAX
1176 */
1177ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1178 const size_t maxlen)
1179{
1180 ssize_t ret = 0;
1181 u16 len;
1182
1183 if (maxlen > SSIZE_MAX)
1184 return -EINVAL;
1185 for (;;) {
1186 len = dp->length;
1187 if (len < 4)
1188 return -1;
1189 ret += len;
1190 if (ret > maxlen)
1191 return -1;
1192 if (dp->type == DEVICE_PATH_TYPE_END &&
1193 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1194 return ret;
1195 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1196 }
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001197}
1198
1199/**
1200 * efi_dp_from_lo() - Get the instance of a VenMedia node in a
1201 * multi-instance device path that matches
1202 * a specific GUID. This kind of device paths
1203 * is found in Boot#### options describing an
1204 * initrd location
1205 *
1206 * @lo: EFI_LOAD_OPTION containing a valid device path
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001207 * @guid: guid to search for
1208 *
1209 * Return:
1210 * device path including the VenMedia node or NULL.
1211 * Caller must free the returned value.
1212 */
1213struct
1214efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001215 const efi_guid_t *guid)
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001216{
1217 struct efi_device_path *fp = lo->file_path;
1218 struct efi_device_path_vendor *vendor;
1219 int lo_len = lo->file_path_length;
1220
1221 for (; lo_len >= sizeof(struct efi_device_path);
1222 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1223 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1224 break;
1225 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1226 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1227 continue;
1228
1229 vendor = (struct efi_device_path_vendor *)fp;
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001230 if (!guidcmp(&vendor->guid, guid))
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +02001231 return efi_dp_dup(efi_dp_next(fp));
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001232 }
1233 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1234
1235 return NULL;
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001236}
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001237
1238/**
1239 * search_gpt_dp_node() - search gpt device path node
1240 *
1241 * @device_path: device path
1242 *
1243 * Return: pointer to the gpt device path node
1244 */
1245struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1246{
1247 struct efi_device_path *dp = device_path;
1248
1249 while (dp) {
1250 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1251 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1252 struct efi_device_path_hard_drive_path *hd_dp =
1253 (struct efi_device_path_hard_drive_path *)dp;
1254
1255 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1256 hd_dp->signature_type == SIG_TYPE_GUID)
1257 return dp;
1258 }
1259 dp = efi_dp_next(dp);
1260 }
1261
1262 return NULL;
1263}