blob: 2b537c1e2eff2c5bf53101f3058fc2ba91fe33c1 [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
8#include <common.h>
9#include <blk.h>
10#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <net.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040013#include <usb.h>
14#include <mmc.h>
Patrick Wildta3ca37e2019-10-03 16:24:17 +020015#include <nvme.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040016#include <efi_loader.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040017#include <part.h>
AKASHI Takahiro659a6262019-09-12 13:52:35 +090018#include <sandboxblockdev.h>
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +020019#include <asm-generic/unaligned.h>
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +090020#include <linux/compat.h> /* U16_MAX */
Rob Clarkf90cb9f2017-09-13 18:05:28 -040021
AKASHI Takahiro659a6262019-09-12 13:52:35 +090022#ifdef CONFIG_SANDBOX
23const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
24#endif
25
Rob Clarkf90cb9f2017-09-13 18:05:28 -040026/* template END node: */
27static const struct efi_device_path END = {
28 .type = DEVICE_PATH_TYPE_END,
29 .sub_type = DEVICE_PATH_SUB_TYPE_END,
30 .length = sizeof(END),
31};
32
Rob Clarkf90cb9f2017-09-13 18:05:28 -040033/* template ROOT node: */
34static const struct efi_device_path_vendor ROOT = {
35 .dp = {
36 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
37 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
38 .length = sizeof(ROOT),
39 },
40 .guid = U_BOOT_GUID,
41};
42
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010043#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
44/*
45 * Determine if an MMC device is an SD card.
46 *
47 * @desc block device descriptor
48 * @return true if the device is an SD card
49 */
50static bool is_sd(struct blk_desc *desc)
51{
52 struct mmc *mmc = find_mmc_device(desc->devnum);
53
54 if (!mmc)
55 return false;
56
57 return IS_SD(mmc) != 0U;
58}
59#endif
60
Rob Clarkf90cb9f2017-09-13 18:05:28 -040061static void *dp_alloc(size_t sz)
62{
63 void *buf;
64
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010065 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
66 EFI_SUCCESS) {
67 debug("EFI: ERROR: out of memory in %s\n", __func__);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040068 return NULL;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010069 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -040070
Patrick Wildtacf7bee2018-03-25 19:54:03 +020071 memset(buf, 0, sz);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040072 return buf;
73}
74
75/*
76 * Iterate to next block in device-path, terminating (returning NULL)
77 * at /End* node.
78 */
79struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
80{
81 if (dp == NULL)
82 return NULL;
83 if (dp->type == DEVICE_PATH_TYPE_END)
84 return NULL;
85 dp = ((void *)dp) + dp->length;
86 if (dp->type == DEVICE_PATH_TYPE_END)
87 return NULL;
88 return (struct efi_device_path *)dp;
89}
90
91/*
92 * Compare two device-paths, stopping when the shorter of the two hits
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +020093 * an End* node. This is useful to, for example, compare a device-path
Rob Clarkf90cb9f2017-09-13 18:05:28 -040094 * representing a device with one representing a file on the device, or
95 * a device with a parent device.
96 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +020097int efi_dp_match(const struct efi_device_path *a,
98 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -040099{
100 while (1) {
101 int ret;
102
103 ret = memcmp(&a->length, &b->length, sizeof(a->length));
104 if (ret)
105 return ret;
106
107 ret = memcmp(a, b, a->length);
108 if (ret)
109 return ret;
110
111 a = efi_dp_next(a);
112 b = efi_dp_next(b);
113
114 if (!a || !b)
115 return 0;
116 }
117}
118
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400119/*
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200120 * We can have device paths that start with a USB WWID or a USB Class node,
121 * and a few other cases which don't encode the full device path with bus
122 * hierarchy:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400123 *
124 * - MESSAGING:USB_WWID
125 * - MESSAGING:USB_CLASS
126 * - MEDIA:FILE_PATH
127 * - MEDIA:HARD_DRIVE
128 * - MESSAGING:URI
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200129 *
130 * See UEFI spec (section 3.1.2, about short-form device-paths)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400131 */
132static struct efi_device_path *shorten_path(struct efi_device_path *dp)
133{
134 while (dp) {
135 /*
136 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
137 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
138 * so not sure when we would see these other cases.
139 */
140 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
141 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
142 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
143 return dp;
144
145 dp = efi_dp_next(dp);
146 }
147
148 return dp;
149}
150
151static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
152 struct efi_device_path **rem)
153{
154 struct efi_object *efiobj;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200155 efi_uintn_t dp_size = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400156
157 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100158 struct efi_handler *handler;
159 struct efi_device_path *obj_dp;
160 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400161
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200162 ret = efi_search_protocol(efiobj,
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100163 &efi_guid_device_path, &handler);
164 if (ret != EFI_SUCCESS)
165 continue;
166 obj_dp = handler->protocol_interface;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400167
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100168 do {
169 if (efi_dp_match(dp, obj_dp) == 0) {
170 if (rem) {
Alexander Graff9360fb2017-12-11 14:29:46 +0100171 /*
172 * Allow partial matches, but inform
173 * the caller.
174 */
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100175 *rem = ((void *)dp) +
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200176 efi_dp_instance_size(obj_dp);
Alexander Graff9360fb2017-12-11 14:29:46 +0100177 return efiobj;
178 } else {
179 /* Only return on exact matches */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200180 if (efi_dp_instance_size(obj_dp) ==
181 dp_size)
Alexander Graff9360fb2017-12-11 14:29:46 +0100182 return efiobj;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400183 }
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100184 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400185
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100186 obj_dp = shorten_path(efi_dp_next(obj_dp));
187 } while (short_path && obj_dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400188 }
189
190 return NULL;
191}
192
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400193/*
194 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
195 * remaining part of the device path after the matched object.
196 */
197struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
198 struct efi_device_path **rem)
199{
200 struct efi_object *efiobj;
201
Alexander Graff9360fb2017-12-11 14:29:46 +0100202 /* Search for an exact match first */
203 efiobj = find_obj(dp, false, NULL);
204
205 /* Then for a fuzzy match */
206 if (!efiobj)
207 efiobj = find_obj(dp, false, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400208
Alexander Graff9360fb2017-12-11 14:29:46 +0100209 /* And now for a fuzzy short match */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400210 if (!efiobj)
211 efiobj = find_obj(dp, true, rem);
212
213 return efiobj;
214}
215
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100216/*
217 * Determine the last device path node that is not the end node.
218 *
219 * @dp device path
220 * @return last node before the end node if it exists
221 * otherwise NULL
222 */
223const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
224{
225 struct efi_device_path *ret;
226
227 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
228 return NULL;
229 while (dp) {
230 ret = (struct efi_device_path *)dp;
231 dp = efi_dp_next(dp);
232 }
233 return ret;
234}
235
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200236/* get size of the first device path instance excluding end node */
237efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400238{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200239 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400240
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200241 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
242 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400243 while (dp) {
244 sz += dp->length;
245 dp = efi_dp_next(dp);
246 }
247
248 return sz;
249}
250
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200251/* get size of multi-instance device path excluding end node */
252efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
253{
254 const struct efi_device_path *p = dp;
255
256 if (!p)
257 return 0;
258 while (p->type != DEVICE_PATH_TYPE_END ||
259 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
260 p = (void *)p + p->length;
261
262 return (void *)p - (void *)dp;
263}
264
265/* copy multi-instance device path */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400266struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
267{
268 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200269 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400270
271 if (!dp)
272 return NULL;
273
274 ndp = dp_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100275 if (!ndp)
276 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400277 memcpy(ndp, dp, sz);
278
279 return ndp;
280}
281
282struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
283 const struct efi_device_path *dp2)
284{
285 struct efi_device_path *ret;
286
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200287 if (!dp1 && !dp2) {
288 /* return an end node */
289 ret = efi_dp_dup(&END);
290 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400291 ret = efi_dp_dup(dp2);
292 } else if (!dp2) {
293 ret = efi_dp_dup(dp1);
294 } else {
295 /* both dp1 and dp2 are non-null */
296 unsigned sz1 = efi_dp_size(dp1);
297 unsigned sz2 = efi_dp_size(dp2);
298 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100299 if (!p)
300 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400301 memcpy(p, dp1, sz1);
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200302 /* the end node of the second device path has to be retained */
303 memcpy(p + sz1, dp2, sz2 + sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400304 ret = p;
305 }
306
307 return ret;
308}
309
310struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
311 const struct efi_device_path *node)
312{
313 struct efi_device_path *ret;
314
315 if (!node && !dp) {
316 ret = efi_dp_dup(&END);
317 } else if (!node) {
318 ret = efi_dp_dup(dp);
319 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200320 size_t sz = node->length;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400321 void *p = dp_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100322 if (!p)
323 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400324 memcpy(p, node, sz);
325 memcpy(p + sz, &END, sizeof(END));
326 ret = p;
327 } else {
328 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200329 size_t sz = efi_dp_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400330 void *p = dp_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100331 if (!p)
332 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400333 memcpy(p, dp, sz);
334 memcpy(p + sz, node, node->length);
335 memcpy(p + sz + node->length, &END, sizeof(END));
336 ret = p;
337 }
338
339 return ret;
340}
341
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200342struct efi_device_path *efi_dp_create_device_node(const u8 type,
343 const u8 sub_type,
344 const u16 length)
345{
346 struct efi_device_path *ret;
347
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200348 if (length < sizeof(struct efi_device_path))
349 return NULL;
350
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200351 ret = dp_alloc(length);
352 if (!ret)
353 return ret;
354 ret->type = type;
355 ret->sub_type = sub_type;
356 ret->length = length;
357 return ret;
358}
359
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200360struct efi_device_path *efi_dp_append_instance(
361 const struct efi_device_path *dp,
362 const struct efi_device_path *dpi)
363{
364 size_t sz, szi;
365 struct efi_device_path *p, *ret;
366
367 if (!dpi)
368 return NULL;
369 if (!dp)
370 return efi_dp_dup(dpi);
371 sz = efi_dp_size(dp);
372 szi = efi_dp_instance_size(dpi);
373 p = dp_alloc(sz + szi + 2 * sizeof(END));
374 if (!p)
375 return NULL;
376 ret = p;
377 memcpy(p, dp, sz + sizeof(END));
378 p = (void *)p + sz;
379 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
380 p = (void *)p + sizeof(END);
381 memcpy(p, dpi, szi);
382 p = (void *)p + szi;
383 memcpy(p, &END, sizeof(END));
384 return ret;
385}
386
387struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
388 efi_uintn_t *size)
389{
390 size_t sz;
391 struct efi_device_path *p;
392
393 if (size)
394 *size = 0;
395 if (!dp || !*dp)
396 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200397 sz = efi_dp_instance_size(*dp);
398 p = dp_alloc(sz + sizeof(END));
399 if (!p)
400 return NULL;
401 memcpy(p, *dp, sz + sizeof(END));
402 *dp = (void *)*dp + sz;
403 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
404 *dp = (void *)*dp + sizeof(END);
405 else
406 *dp = NULL;
407 if (size)
408 *size = sz + sizeof(END);
409 return p;
410}
411
412bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
413{
414 const struct efi_device_path *p = dp;
415
416 if (!p)
417 return false;
418 while (p->type != DEVICE_PATH_TYPE_END)
419 p = (void *)p + p->length;
420 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
421}
422
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400423#ifdef CONFIG_DM
424/* size of device-path not including END node for device and all parents
425 * up to the root device.
426 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100427__maybe_unused static unsigned int dp_size(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400428{
429 if (!dev || !dev->driver)
430 return sizeof(ROOT);
431
432 switch (dev->driver->id) {
433 case UCLASS_ROOT:
434 case UCLASS_SIMPLE_BUS:
435 /* stop traversing parents at this point: */
436 return sizeof(ROOT);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100437 case UCLASS_ETH:
438 return dp_size(dev->parent) +
439 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100440#ifdef CONFIG_BLK
441 case UCLASS_BLK:
442 switch (dev->parent->uclass->uc_drv->id) {
443#ifdef CONFIG_IDE
444 case UCLASS_IDE:
445 return dp_size(dev->parent) +
446 sizeof(struct efi_device_path_atapi);
447#endif
448#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
449 case UCLASS_SCSI:
450 return dp_size(dev->parent) +
451 sizeof(struct efi_device_path_scsi);
452#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100453#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
454 case UCLASS_MMC:
455 return dp_size(dev->parent) +
456 sizeof(struct efi_device_path_sd_mmc_path);
457#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200458#if defined(CONFIG_NVME)
459 case UCLASS_NVME:
460 return dp_size(dev->parent) +
461 sizeof(struct efi_device_path_nvme);
462#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900463#ifdef CONFIG_SANDBOX
464 case UCLASS_ROOT:
465 /*
466 * Sandbox's host device will be represented
467 * as vendor device with extra one byte for
468 * device number
469 */
470 return dp_size(dev->parent)
471 + sizeof(struct efi_device_path_vendor) + 1;
472#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100473 default:
474 return dp_size(dev->parent);
475 }
476#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100477#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400478 case UCLASS_MMC:
479 return dp_size(dev->parent) +
480 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100481#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400482 case UCLASS_MASS_STORAGE:
483 case UCLASS_USB_HUB:
484 return dp_size(dev->parent) +
485 sizeof(struct efi_device_path_usb_class);
486 default:
487 /* just skip over unknown classes: */
488 return dp_size(dev->parent);
489 }
490}
491
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100492/*
493 * Recursively build a device path.
494 *
495 * @buf pointer to the end of the device path
496 * @dev device
497 * @return pointer to the end of the device path
498 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100499__maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400500{
501 if (!dev || !dev->driver)
502 return buf;
503
504 switch (dev->driver->id) {
505 case UCLASS_ROOT:
506 case UCLASS_SIMPLE_BUS: {
507 /* stop traversing parents at this point: */
508 struct efi_device_path_vendor *vdp = buf;
509 *vdp = ROOT;
510 return &vdp[1];
511 }
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100512#ifdef CONFIG_DM_ETH
513 case UCLASS_ETH: {
514 struct efi_device_path_mac_addr *dp =
515 dp_fill(buf, dev->parent);
516 struct eth_pdata *pdata = dev->platdata;
517
518 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
519 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
520 dp->dp.length = sizeof(*dp);
521 memset(&dp->mac, 0, sizeof(dp->mac));
522 /* We only support IPv4 */
523 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
524 /* Ethernet */
525 dp->if_type = 1;
526 return &dp[1];
527 }
528#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100529#ifdef CONFIG_BLK
530 case UCLASS_BLK:
531 switch (dev->parent->uclass->uc_drv->id) {
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900532#ifdef CONFIG_SANDBOX
533 case UCLASS_ROOT: {
534 /* stop traversing parents at this point: */
Heinrich Schuchardt1e3beaf2020-05-06 01:28:08 +0200535 struct efi_device_path_vendor *dp;
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900536 struct blk_desc *desc = dev_get_uclass_platdata(dev);
537
538 dp_fill(buf, dev->parent);
539 dp = buf;
540 ++dp;
541 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
542 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
543 dp->dp.length = sizeof(*dp) + 1;
544 memcpy(&dp->guid, &efi_guid_host_dev,
545 sizeof(efi_guid_t));
546 dp->vendor_data[0] = desc->devnum;
547 return &dp->vendor_data[1];
548 }
549#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100550#ifdef CONFIG_IDE
551 case UCLASS_IDE: {
552 struct efi_device_path_atapi *dp =
553 dp_fill(buf, dev->parent);
554 struct blk_desc *desc = dev_get_uclass_platdata(dev);
555
556 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
557 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
558 dp->dp.length = sizeof(*dp);
559 dp->logical_unit_number = desc->devnum;
560 dp->primary_secondary = IDE_BUS(desc->devnum);
561 dp->slave_master = desc->devnum %
562 (CONFIG_SYS_IDE_MAXDEVICE /
563 CONFIG_SYS_IDE_MAXBUS);
564 return &dp[1];
565 }
566#endif
567#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
568 case UCLASS_SCSI: {
569 struct efi_device_path_scsi *dp =
570 dp_fill(buf, dev->parent);
571 struct blk_desc *desc = dev_get_uclass_platdata(dev);
572
573 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
574 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
575 dp->dp.length = sizeof(*dp);
576 dp->logical_unit_number = desc->lun;
577 dp->target_id = desc->target;
578 return &dp[1];
579 }
580#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100581#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
582 case UCLASS_MMC: {
583 struct efi_device_path_sd_mmc_path *sddp =
584 dp_fill(buf, dev->parent);
585 struct blk_desc *desc = dev_get_uclass_platdata(dev);
586
587 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
588 sddp->dp.sub_type = is_sd(desc) ?
589 DEVICE_PATH_SUB_TYPE_MSG_SD :
590 DEVICE_PATH_SUB_TYPE_MSG_MMC;
591 sddp->dp.length = sizeof(*sddp);
592 sddp->slot_number = dev->seq;
593 return &sddp[1];
594 }
595#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200596#if defined(CONFIG_NVME)
597 case UCLASS_NVME: {
598 struct efi_device_path_nvme *dp =
599 dp_fill(buf, dev->parent);
600 u32 ns_id;
601
602 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
603 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
604 dp->dp.length = sizeof(*dp);
605 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
606 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
607 return &dp[1];
608 }
609#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100610 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100611 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
612 __FILE__, __LINE__, __func__,
613 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100614 return dp_fill(buf, dev->parent);
615 }
616#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400617#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
618 case UCLASS_MMC: {
619 struct efi_device_path_sd_mmc_path *sddp =
620 dp_fill(buf, dev->parent);
621 struct mmc *mmc = mmc_get_mmc_dev(dev);
622 struct blk_desc *desc = mmc_get_blk_desc(mmc);
623
624 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100625 sddp->dp.sub_type = is_sd(desc) ?
626 DEVICE_PATH_SUB_TYPE_MSG_SD :
627 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400628 sddp->dp.length = sizeof(*sddp);
629 sddp->slot_number = dev->seq;
630
631 return &sddp[1];
632 }
633#endif
634 case UCLASS_MASS_STORAGE:
635 case UCLASS_USB_HUB: {
636 struct efi_device_path_usb_class *udp =
637 dp_fill(buf, dev->parent);
638 struct usb_device *udev = dev_get_parent_priv(dev);
639 struct usb_device_descriptor *desc = &udev->descriptor;
640
641 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
642 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
643 udp->dp.length = sizeof(*udp);
644 udp->vendor_id = desc->idVendor;
645 udp->product_id = desc->idProduct;
646 udp->device_class = desc->bDeviceClass;
647 udp->device_subclass = desc->bDeviceSubClass;
648 udp->device_protocol = desc->bDeviceProtocol;
649
650 return &udp[1];
651 }
652 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100653 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
654 __FILE__, __LINE__, __func__,
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400655 dev->name, dev->driver->id);
656 return dp_fill(buf, dev->parent);
657 }
658}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400659#endif
660
661static unsigned dp_part_size(struct blk_desc *desc, int part)
662{
663 unsigned dpsize;
664
665#ifdef CONFIG_BLK
Heinrich Schuchardt3a615c82017-12-11 12:56:43 +0100666 {
667 struct udevice *dev;
668 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
669
670 if (ret)
671 dev = desc->bdev->parent;
672 dpsize = dp_size(dev);
673 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400674#else
675 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
676#endif
677
678 if (part == 0) /* the actual disk, not a partition */
679 return dpsize;
680
681 if (desc->part_type == PART_TYPE_ISO)
682 dpsize += sizeof(struct efi_device_path_cdrom_path);
683 else
684 dpsize += sizeof(struct efi_device_path_hard_drive_path);
685
686 return dpsize;
687}
688
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100689/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100690 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100691 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200692 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100693 * @desc block device descriptor
694 * @part partition number, 0 identifies a block device
695 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100696static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400697{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600698 struct disk_partition info;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400699
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400700 part_get_info(desc, part, &info);
701
702 if (desc->part_type == PART_TYPE_ISO) {
703 struct efi_device_path_cdrom_path *cddp = buf;
704
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100705 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400706 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
707 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
708 cddp->dp.length = sizeof(*cddp);
709 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200710 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400711
712 buf = &cddp[1];
713 } else {
714 struct efi_device_path_hard_drive_path *hddp = buf;
715
716 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
717 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
718 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100719 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400720 hddp->partition_start = info.start;
721 hddp->partition_end = info.size;
722 if (desc->part_type == PART_TYPE_EFI)
723 hddp->partmap_type = 2;
724 else
725 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100726
727 switch (desc->sig_type) {
728 case SIG_TYPE_NONE:
729 default:
730 hddp->signature_type = 0;
731 memset(hddp->partition_signature, 0,
732 sizeof(hddp->partition_signature));
733 break;
734 case SIG_TYPE_MBR:
735 hddp->signature_type = 1;
736 memset(hddp->partition_signature, 0,
737 sizeof(hddp->partition_signature));
738 memcpy(hddp->partition_signature, &desc->mbr_sig,
739 sizeof(desc->mbr_sig));
740 break;
741 case SIG_TYPE_GUID:
742 hddp->signature_type = 2;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400743 memcpy(hddp->partition_signature, &desc->guid_sig,
744 sizeof(hddp->partition_signature));
Jonathan Gray84b4d702017-11-22 14:18:59 +1100745 break;
746 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400747
748 buf = &hddp[1];
749 }
750
751 return buf;
752}
753
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100754/*
755 * Create a device path for a block device or one of its partitions.
756 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200757 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100758 * @desc block device descriptor
759 * @part partition number, 0 identifies a block device
760 */
761static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
762{
763#ifdef CONFIG_BLK
764 {
765 struct udevice *dev;
766 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
767
768 if (ret)
769 dev = desc->bdev->parent;
770 buf = dp_fill(buf, dev);
771 }
772#else
773 /*
774 * We *could* make a more accurate path, by looking at if_type
775 * and handling all the different cases like we do for non-
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200776 * legacy (i.e. CONFIG_BLK=y) case. But most important thing
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100777 * is just to have a unique device-path for if_type+devnum.
778 * So map things to a fictitious USB device.
779 */
780 struct efi_device_path_usb *udp;
781
782 memcpy(buf, &ROOT, sizeof(ROOT));
783 buf += sizeof(ROOT);
784
785 udp = buf;
786 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
787 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
788 udp->dp.length = sizeof(*udp);
789 udp->parent_port_number = desc->if_type;
790 udp->usb_interface = desc->devnum;
791 buf = &udp[1];
792#endif
793
794 if (part == 0) /* the actual disk, not a partition */
795 return buf;
796
797 return dp_part_node(buf, desc, part);
798}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400799
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200800/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400801struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
802{
803 void *buf, *start;
804
805 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100806 if (!buf)
807 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400808
809 buf = dp_part_fill(buf, desc, part);
810
811 *((struct efi_device_path *)buf) = END;
812
813 return start;
814}
815
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100816/*
817 * Create a device node for a block device partition.
818 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200819 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100820 * @desc block device descriptor
821 * @part partition number, 0 identifies a block device
822 */
823struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
824{
825 efi_uintn_t dpsize;
826 void *buf;
827
828 if (desc->part_type == PART_TYPE_ISO)
829 dpsize = sizeof(struct efi_device_path_cdrom_path);
830 else
831 dpsize = sizeof(struct efi_device_path_hard_drive_path);
832 buf = dp_alloc(dpsize);
833
834 dp_part_node(buf, desc, part);
835
836 return buf;
837}
838
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200839/**
840 * path_to_uefi() - convert UTF-8 path to an UEFI style path
841 *
842 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
843 * separators and UTF-16).
844 *
845 * @src: source buffer
846 * @uefi: target buffer, possibly unaligned
847 */
848static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400849{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200850 u16 *pos = uefi;
851
852 /*
853 * efi_set_bootdev() calls this routine indirectly before the UEFI
854 * subsystem is initialized. So we cannot assume unaligned access to be
855 * enabled.
856 */
857 allow_unaligned();
858
859 while (*src) {
860 s32 code = utf8_get(&src);
861
862 if (code < 0)
863 code = '?';
864 else if (code == '/')
865 code = '\\';
866 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400867 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200868 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400869}
870
871/*
872 * If desc is NULL, this creates a path with only the file component,
873 * otherwise it creates a full path with both device and file components
874 */
875struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
876 const char *path)
877{
878 struct efi_device_path_file_path *fp;
879 void *buf, *start;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900880 size_t dpsize = 0, fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400881
882 if (desc)
883 dpsize = dp_part_size(desc, part);
884
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200885 fpsize = sizeof(struct efi_device_path) +
886 2 * (utf8_utf16_strlen(path) + 1);
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900887 if (fpsize > U16_MAX)
888 return NULL;
889
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400890 dpsize += fpsize;
891
892 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100893 if (!buf)
894 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400895
896 if (desc)
897 buf = dp_part_fill(buf, desc, part);
898
899 /* add file-path: */
900 fp = buf;
901 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
902 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +0900903 fp->dp.length = (u16)fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400904 path_to_uefi(fp->str, path);
905 buf += fpsize;
906
907 *((struct efi_device_path *)buf) = END;
908
909 return start;
910}
911
Joe Hershberger5277a972018-04-13 15:26:39 -0500912#ifdef CONFIG_NET
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400913struct efi_device_path *efi_dp_from_eth(void)
914{
Alexander Grafc4e983f2018-03-15 17:33:38 +0100915#ifndef CONFIG_DM_ETH
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400916 struct efi_device_path_mac_addr *ndp;
Alexander Grafc4e983f2018-03-15 17:33:38 +0100917#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400918 void *buf, *start;
919 unsigned dpsize = 0;
920
921 assert(eth_get_dev());
922
923#ifdef CONFIG_DM_ETH
924 dpsize += dp_size(eth_get_dev());
925#else
926 dpsize += sizeof(ROOT);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400927 dpsize += sizeof(*ndp);
Alexander Grafc4e983f2018-03-15 17:33:38 +0100928#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400929
930 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100931 if (!buf)
932 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400933
934#ifdef CONFIG_DM_ETH
935 buf = dp_fill(buf, eth_get_dev());
936#else
937 memcpy(buf, &ROOT, sizeof(ROOT));
938 buf += sizeof(ROOT);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400939
940 ndp = buf;
941 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
942 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
943 ndp->dp.length = sizeof(*ndp);
Alexander Grafc4e983f2018-03-15 17:33:38 +0100944 ndp->if_type = 1; /* Ethernet */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400945 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
946 buf = &ndp[1];
Alexander Grafc4e983f2018-03-15 17:33:38 +0100947#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400948
949 *((struct efi_device_path *)buf) = END;
950
951 return start;
952}
953#endif
954
Rob Clark18ceba72017-10-10 08:23:06 -0400955/* Construct a device-path for memory-mapped image */
956struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
957 uint64_t start_address,
958 uint64_t end_address)
959{
960 struct efi_device_path_memory *mdp;
961 void *buf, *start;
962
963 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100964 if (!buf)
965 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -0400966
967 mdp = buf;
968 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
969 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
970 mdp->dp.length = sizeof(*mdp);
971 mdp->memory_type = memory_type;
972 mdp->start_address = start_address;
973 mdp->end_address = end_address;
974 buf = &mdp[1];
975
976 *((struct efi_device_path *)buf) = END;
977
978 return start;
979}
980
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +0100981/**
982 * efi_dp_split_file_path() - split of relative file path from device path
983 *
984 * Given a device path indicating a file on a device, separate the device
985 * path in two: the device path of the actual device and the file path
986 * relative to this device.
987 *
988 * @full_path: device path including device and file path
989 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +0200990 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +0100991 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400992 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100993efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
994 struct efi_device_path **device_path,
995 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400996{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +0200997 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400998
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100999 *device_path = NULL;
1000 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001001 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001002 if (!dp)
1003 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001004 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001005 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001006 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001007 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001008 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001009 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001010 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001011 if (!fp)
1012 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001013 p->type = DEVICE_PATH_TYPE_END;
1014 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1015 p->length = sizeof(*p);
1016
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001017out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001018 *device_path = dp;
1019 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001020 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001021}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001022
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001023/**
1024 * efi_dp_from_name() - convert U-Boot device and file path to device path
1025 *
1026 * @dev: U-Boot device, e.g. 'mmc'
1027 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1028 * @path: file path relative to U-Boot device, may be NULL
1029 * @device: pointer to receive device path of the device
1030 * @file: pointer to receive device path for the file
1031 * Return: status code
1032 */
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001033efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1034 const char *path,
1035 struct efi_device_path **device,
1036 struct efi_device_path **file)
1037{
1038 int is_net;
1039 struct blk_desc *desc = NULL;
Simon Glassc1c4a8f2020-05-10 11:39:57 -06001040 struct disk_partition fs_partition;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001041 int part = 0;
1042 char filename[32] = { 0 }; /* dp->str is u16[32] long */
1043 char *s;
1044
AKASHI Takahiro39844412018-11-05 18:06:40 +09001045 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001046 return EFI_INVALID_PARAMETER;
1047
1048 is_net = !strcmp(dev, "Net");
1049 if (!is_net) {
1050 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1051 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001052 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001053 return EFI_INVALID_PARAMETER;
1054
AKASHI Takahiro39844412018-11-05 18:06:40 +09001055 if (device)
1056 *device = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001057 } else {
1058#ifdef CONFIG_NET
AKASHI Takahiro39844412018-11-05 18:06:40 +09001059 if (device)
1060 *device = efi_dp_from_eth();
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001061#endif
1062 }
1063
1064 if (!path)
1065 return EFI_SUCCESS;
1066
Heinrich Schuchardt7d698072019-02-23 11:20:23 +01001067 snprintf(filename, sizeof(filename), "%s", path);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001068 /* DOS style file path: */
1069 s = filename;
1070 while ((s = strchr(s, '/')))
1071 *s++ = '\\';
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001072 *file = efi_dp_from_file(is_net ? NULL : desc, part, filename);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001073
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001074 if (!*file)
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001075 return EFI_INVALID_PARAMETER;
1076
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001077 return EFI_SUCCESS;
1078}