blob: 897fc1b2e8ac38f57819ab8157da4b968ea7428e [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>
11#include <usb.h>
12#include <mmc.h>
Patrick Wildta3ca37e2019-10-03 16:24:17 +020013#include <nvme.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040014#include <efi_loader.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040015#include <part.h>
AKASHI Takahiro659a6262019-09-12 13:52:35 +090016#include <sandboxblockdev.h>
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +020017#include <asm-generic/unaligned.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040018
AKASHI Takahiro659a6262019-09-12 13:52:35 +090019#ifdef CONFIG_SANDBOX
20const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
21#endif
22
Rob Clarkf90cb9f2017-09-13 18:05:28 -040023/* template END node: */
24static const struct efi_device_path END = {
25 .type = DEVICE_PATH_TYPE_END,
26 .sub_type = DEVICE_PATH_SUB_TYPE_END,
27 .length = sizeof(END),
28};
29
Rob Clarkf90cb9f2017-09-13 18:05:28 -040030/* template ROOT node: */
31static const struct efi_device_path_vendor ROOT = {
32 .dp = {
33 .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE,
34 .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR,
35 .length = sizeof(ROOT),
36 },
37 .guid = U_BOOT_GUID,
38};
39
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010040#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
41/*
42 * Determine if an MMC device is an SD card.
43 *
44 * @desc block device descriptor
45 * @return true if the device is an SD card
46 */
47static bool is_sd(struct blk_desc *desc)
48{
49 struct mmc *mmc = find_mmc_device(desc->devnum);
50
51 if (!mmc)
52 return false;
53
54 return IS_SD(mmc) != 0U;
55}
56#endif
57
Rob Clarkf90cb9f2017-09-13 18:05:28 -040058static void *dp_alloc(size_t sz)
59{
60 void *buf;
61
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010062 if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) !=
63 EFI_SUCCESS) {
64 debug("EFI: ERROR: out of memory in %s\n", __func__);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040065 return NULL;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +010066 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -040067
Patrick Wildtacf7bee2018-03-25 19:54:03 +020068 memset(buf, 0, sz);
Rob Clarkf90cb9f2017-09-13 18:05:28 -040069 return buf;
70}
71
72/*
73 * Iterate to next block in device-path, terminating (returning NULL)
74 * at /End* node.
75 */
76struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
77{
78 if (dp == NULL)
79 return NULL;
80 if (dp->type == DEVICE_PATH_TYPE_END)
81 return NULL;
82 dp = ((void *)dp) + dp->length;
83 if (dp->type == DEVICE_PATH_TYPE_END)
84 return NULL;
85 return (struct efi_device_path *)dp;
86}
87
88/*
89 * Compare two device-paths, stopping when the shorter of the two hits
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +020090 * an End* node. This is useful to, for example, compare a device-path
Rob Clarkf90cb9f2017-09-13 18:05:28 -040091 * representing a device with one representing a file on the device, or
92 * a device with a parent device.
93 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +020094int efi_dp_match(const struct efi_device_path *a,
95 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -040096{
97 while (1) {
98 int ret;
99
100 ret = memcmp(&a->length, &b->length, sizeof(a->length));
101 if (ret)
102 return ret;
103
104 ret = memcmp(a, b, a->length);
105 if (ret)
106 return ret;
107
108 a = efi_dp_next(a);
109 b = efi_dp_next(b);
110
111 if (!a || !b)
112 return 0;
113 }
114}
115
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400116/*
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200117 * We can have device paths that start with a USB WWID or a USB Class node,
118 * and a few other cases which don't encode the full device path with bus
119 * hierarchy:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400120 *
121 * - MESSAGING:USB_WWID
122 * - MESSAGING:USB_CLASS
123 * - MEDIA:FILE_PATH
124 * - MEDIA:HARD_DRIVE
125 * - MESSAGING:URI
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200126 *
127 * See UEFI spec (section 3.1.2, about short-form device-paths)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400128 */
129static struct efi_device_path *shorten_path(struct efi_device_path *dp)
130{
131 while (dp) {
132 /*
133 * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI..
134 * in practice fallback.efi just uses MEDIA:HARD_DRIVE
135 * so not sure when we would see these other cases.
136 */
137 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) ||
138 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
139 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
140 return dp;
141
142 dp = efi_dp_next(dp);
143 }
144
145 return dp;
146}
147
148static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path,
149 struct efi_device_path **rem)
150{
151 struct efi_object *efiobj;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200152 efi_uintn_t dp_size = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400153
154 list_for_each_entry(efiobj, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100155 struct efi_handler *handler;
156 struct efi_device_path *obj_dp;
157 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400158
Heinrich Schuchardtadb50d02018-09-26 05:27:55 +0200159 ret = efi_search_protocol(efiobj,
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100160 &efi_guid_device_path, &handler);
161 if (ret != EFI_SUCCESS)
162 continue;
163 obj_dp = handler->protocol_interface;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400164
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100165 do {
166 if (efi_dp_match(dp, obj_dp) == 0) {
167 if (rem) {
Alexander Graff9360fb2017-12-11 14:29:46 +0100168 /*
169 * Allow partial matches, but inform
170 * the caller.
171 */
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100172 *rem = ((void *)dp) +
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200173 efi_dp_instance_size(obj_dp);
Alexander Graff9360fb2017-12-11 14:29:46 +0100174 return efiobj;
175 } else {
176 /* Only return on exact matches */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200177 if (efi_dp_instance_size(obj_dp) ==
178 dp_size)
Alexander Graff9360fb2017-12-11 14:29:46 +0100179 return efiobj;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400180 }
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100181 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400182
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100183 obj_dp = shorten_path(efi_dp_next(obj_dp));
184 } while (short_path && obj_dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400185 }
186
187 return NULL;
188}
189
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400190/*
191 * Find an efiobj from device-path, if 'rem' is not NULL, returns the
192 * remaining part of the device path after the matched object.
193 */
194struct efi_object *efi_dp_find_obj(struct efi_device_path *dp,
195 struct efi_device_path **rem)
196{
197 struct efi_object *efiobj;
198
Alexander Graff9360fb2017-12-11 14:29:46 +0100199 /* Search for an exact match first */
200 efiobj = find_obj(dp, false, NULL);
201
202 /* Then for a fuzzy match */
203 if (!efiobj)
204 efiobj = find_obj(dp, false, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400205
Alexander Graff9360fb2017-12-11 14:29:46 +0100206 /* And now for a fuzzy short match */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400207 if (!efiobj)
208 efiobj = find_obj(dp, true, rem);
209
210 return efiobj;
211}
212
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100213/*
214 * Determine the last device path node that is not the end node.
215 *
216 * @dp device path
217 * @return last node before the end node if it exists
218 * otherwise NULL
219 */
220const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
221{
222 struct efi_device_path *ret;
223
224 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
225 return NULL;
226 while (dp) {
227 ret = (struct efi_device_path *)dp;
228 dp = efi_dp_next(dp);
229 }
230 return ret;
231}
232
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200233/* get size of the first device path instance excluding end node */
234efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400235{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200236 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400237
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200238 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
239 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400240 while (dp) {
241 sz += dp->length;
242 dp = efi_dp_next(dp);
243 }
244
245 return sz;
246}
247
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200248/* get size of multi-instance device path excluding end node */
249efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
250{
251 const struct efi_device_path *p = dp;
252
253 if (!p)
254 return 0;
255 while (p->type != DEVICE_PATH_TYPE_END ||
256 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
257 p = (void *)p + p->length;
258
259 return (void *)p - (void *)dp;
260}
261
262/* copy multi-instance device path */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400263struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
264{
265 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200266 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400267
268 if (!dp)
269 return NULL;
270
271 ndp = dp_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100272 if (!ndp)
273 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400274 memcpy(ndp, dp, sz);
275
276 return ndp;
277}
278
279struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
280 const struct efi_device_path *dp2)
281{
282 struct efi_device_path *ret;
283
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200284 if (!dp1 && !dp2) {
285 /* return an end node */
286 ret = efi_dp_dup(&END);
287 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400288 ret = efi_dp_dup(dp2);
289 } else if (!dp2) {
290 ret = efi_dp_dup(dp1);
291 } else {
292 /* both dp1 and dp2 are non-null */
293 unsigned sz1 = efi_dp_size(dp1);
294 unsigned sz2 = efi_dp_size(dp2);
295 void *p = dp_alloc(sz1 + sz2 + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100296 if (!p)
297 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400298 memcpy(p, dp1, sz1);
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200299 /* the end node of the second device path has to be retained */
300 memcpy(p + sz1, dp2, sz2 + sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400301 ret = p;
302 }
303
304 return ret;
305}
306
307struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
308 const struct efi_device_path *node)
309{
310 struct efi_device_path *ret;
311
312 if (!node && !dp) {
313 ret = efi_dp_dup(&END);
314 } else if (!node) {
315 ret = efi_dp_dup(dp);
316 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200317 size_t sz = node->length;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400318 void *p = dp_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100319 if (!p)
320 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400321 memcpy(p, node, sz);
322 memcpy(p + sz, &END, sizeof(END));
323 ret = p;
324 } else {
325 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200326 size_t sz = efi_dp_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400327 void *p = dp_alloc(sz + node->length + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100328 if (!p)
329 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400330 memcpy(p, dp, sz);
331 memcpy(p + sz, node, node->length);
332 memcpy(p + sz + node->length, &END, sizeof(END));
333 ret = p;
334 }
335
336 return ret;
337}
338
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200339struct efi_device_path *efi_dp_create_device_node(const u8 type,
340 const u8 sub_type,
341 const u16 length)
342{
343 struct efi_device_path *ret;
344
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200345 if (length < sizeof(struct efi_device_path))
346 return NULL;
347
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200348 ret = dp_alloc(length);
349 if (!ret)
350 return ret;
351 ret->type = type;
352 ret->sub_type = sub_type;
353 ret->length = length;
354 return ret;
355}
356
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200357struct efi_device_path *efi_dp_append_instance(
358 const struct efi_device_path *dp,
359 const struct efi_device_path *dpi)
360{
361 size_t sz, szi;
362 struct efi_device_path *p, *ret;
363
364 if (!dpi)
365 return NULL;
366 if (!dp)
367 return efi_dp_dup(dpi);
368 sz = efi_dp_size(dp);
369 szi = efi_dp_instance_size(dpi);
370 p = dp_alloc(sz + szi + 2 * sizeof(END));
371 if (!p)
372 return NULL;
373 ret = p;
374 memcpy(p, dp, sz + sizeof(END));
375 p = (void *)p + sz;
376 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
377 p = (void *)p + sizeof(END);
378 memcpy(p, dpi, szi);
379 p = (void *)p + szi;
380 memcpy(p, &END, sizeof(END));
381 return ret;
382}
383
384struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
385 efi_uintn_t *size)
386{
387 size_t sz;
388 struct efi_device_path *p;
389
390 if (size)
391 *size = 0;
392 if (!dp || !*dp)
393 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200394 sz = efi_dp_instance_size(*dp);
395 p = dp_alloc(sz + sizeof(END));
396 if (!p)
397 return NULL;
398 memcpy(p, *dp, sz + sizeof(END));
399 *dp = (void *)*dp + sz;
400 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
401 *dp = (void *)*dp + sizeof(END);
402 else
403 *dp = NULL;
404 if (size)
405 *size = sz + sizeof(END);
406 return p;
407}
408
409bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
410{
411 const struct efi_device_path *p = dp;
412
413 if (!p)
414 return false;
415 while (p->type != DEVICE_PATH_TYPE_END)
416 p = (void *)p + p->length;
417 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
418}
419
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400420#ifdef CONFIG_DM
421/* size of device-path not including END node for device and all parents
422 * up to the root device.
423 */
424static unsigned dp_size(struct udevice *dev)
425{
426 if (!dev || !dev->driver)
427 return sizeof(ROOT);
428
429 switch (dev->driver->id) {
430 case UCLASS_ROOT:
431 case UCLASS_SIMPLE_BUS:
432 /* stop traversing parents at this point: */
433 return sizeof(ROOT);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100434 case UCLASS_ETH:
435 return dp_size(dev->parent) +
436 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100437#ifdef CONFIG_BLK
438 case UCLASS_BLK:
439 switch (dev->parent->uclass->uc_drv->id) {
440#ifdef CONFIG_IDE
441 case UCLASS_IDE:
442 return dp_size(dev->parent) +
443 sizeof(struct efi_device_path_atapi);
444#endif
445#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
446 case UCLASS_SCSI:
447 return dp_size(dev->parent) +
448 sizeof(struct efi_device_path_scsi);
449#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100450#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
451 case UCLASS_MMC:
452 return dp_size(dev->parent) +
453 sizeof(struct efi_device_path_sd_mmc_path);
454#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200455#if defined(CONFIG_NVME)
456 case UCLASS_NVME:
457 return dp_size(dev->parent) +
458 sizeof(struct efi_device_path_nvme);
459#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900460#ifdef CONFIG_SANDBOX
461 case UCLASS_ROOT:
462 /*
463 * Sandbox's host device will be represented
464 * as vendor device with extra one byte for
465 * device number
466 */
467 return dp_size(dev->parent)
468 + sizeof(struct efi_device_path_vendor) + 1;
469#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100470 default:
471 return dp_size(dev->parent);
472 }
473#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100474#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400475 case UCLASS_MMC:
476 return dp_size(dev->parent) +
477 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100478#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400479 case UCLASS_MASS_STORAGE:
480 case UCLASS_USB_HUB:
481 return dp_size(dev->parent) +
482 sizeof(struct efi_device_path_usb_class);
483 default:
484 /* just skip over unknown classes: */
485 return dp_size(dev->parent);
486 }
487}
488
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100489/*
490 * Recursively build a device path.
491 *
492 * @buf pointer to the end of the device path
493 * @dev device
494 * @return pointer to the end of the device path
495 */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400496static void *dp_fill(void *buf, struct udevice *dev)
497{
498 if (!dev || !dev->driver)
499 return buf;
500
501 switch (dev->driver->id) {
502 case UCLASS_ROOT:
503 case UCLASS_SIMPLE_BUS: {
504 /* stop traversing parents at this point: */
505 struct efi_device_path_vendor *vdp = buf;
506 *vdp = ROOT;
507 return &vdp[1];
508 }
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100509#ifdef CONFIG_DM_ETH
510 case UCLASS_ETH: {
511 struct efi_device_path_mac_addr *dp =
512 dp_fill(buf, dev->parent);
513 struct eth_pdata *pdata = dev->platdata;
514
515 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
516 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
517 dp->dp.length = sizeof(*dp);
518 memset(&dp->mac, 0, sizeof(dp->mac));
519 /* We only support IPv4 */
520 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
521 /* Ethernet */
522 dp->if_type = 1;
523 return &dp[1];
524 }
525#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100526#ifdef CONFIG_BLK
527 case UCLASS_BLK:
528 switch (dev->parent->uclass->uc_drv->id) {
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900529#ifdef CONFIG_SANDBOX
530 case UCLASS_ROOT: {
531 /* stop traversing parents at this point: */
532 struct efi_device_path_vendor *dp = buf;
533 struct blk_desc *desc = dev_get_uclass_platdata(dev);
534
535 dp_fill(buf, dev->parent);
536 dp = buf;
537 ++dp;
538 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
539 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
540 dp->dp.length = sizeof(*dp) + 1;
541 memcpy(&dp->guid, &efi_guid_host_dev,
542 sizeof(efi_guid_t));
543 dp->vendor_data[0] = desc->devnum;
544 return &dp->vendor_data[1];
545 }
546#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100547#ifdef CONFIG_IDE
548 case UCLASS_IDE: {
549 struct efi_device_path_atapi *dp =
550 dp_fill(buf, dev->parent);
551 struct blk_desc *desc = dev_get_uclass_platdata(dev);
552
553 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
554 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
555 dp->dp.length = sizeof(*dp);
556 dp->logical_unit_number = desc->devnum;
557 dp->primary_secondary = IDE_BUS(desc->devnum);
558 dp->slave_master = desc->devnum %
559 (CONFIG_SYS_IDE_MAXDEVICE /
560 CONFIG_SYS_IDE_MAXBUS);
561 return &dp[1];
562 }
563#endif
564#if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI)
565 case UCLASS_SCSI: {
566 struct efi_device_path_scsi *dp =
567 dp_fill(buf, dev->parent);
568 struct blk_desc *desc = dev_get_uclass_platdata(dev);
569
570 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
571 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
572 dp->dp.length = sizeof(*dp);
573 dp->logical_unit_number = desc->lun;
574 dp->target_id = desc->target;
575 return &dp[1];
576 }
577#endif
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100578#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
579 case UCLASS_MMC: {
580 struct efi_device_path_sd_mmc_path *sddp =
581 dp_fill(buf, dev->parent);
582 struct blk_desc *desc = dev_get_uclass_platdata(dev);
583
584 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
585 sddp->dp.sub_type = is_sd(desc) ?
586 DEVICE_PATH_SUB_TYPE_MSG_SD :
587 DEVICE_PATH_SUB_TYPE_MSG_MMC;
588 sddp->dp.length = sizeof(*sddp);
589 sddp->slot_number = dev->seq;
590 return &sddp[1];
591 }
592#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200593#if defined(CONFIG_NVME)
594 case UCLASS_NVME: {
595 struct efi_device_path_nvme *dp =
596 dp_fill(buf, dev->parent);
597 u32 ns_id;
598
599 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
600 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
601 dp->dp.length = sizeof(*dp);
602 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
603 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
604 return &dp[1];
605 }
606#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100607 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100608 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
609 __FILE__, __LINE__, __func__,
610 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100611 return dp_fill(buf, dev->parent);
612 }
613#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400614#if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC)
615 case UCLASS_MMC: {
616 struct efi_device_path_sd_mmc_path *sddp =
617 dp_fill(buf, dev->parent);
618 struct mmc *mmc = mmc_get_mmc_dev(dev);
619 struct blk_desc *desc = mmc_get_blk_desc(mmc);
620
621 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100622 sddp->dp.sub_type = is_sd(desc) ?
623 DEVICE_PATH_SUB_TYPE_MSG_SD :
624 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400625 sddp->dp.length = sizeof(*sddp);
626 sddp->slot_number = dev->seq;
627
628 return &sddp[1];
629 }
630#endif
631 case UCLASS_MASS_STORAGE:
632 case UCLASS_USB_HUB: {
633 struct efi_device_path_usb_class *udp =
634 dp_fill(buf, dev->parent);
635 struct usb_device *udev = dev_get_parent_priv(dev);
636 struct usb_device_descriptor *desc = &udev->descriptor;
637
638 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
639 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS;
640 udp->dp.length = sizeof(*udp);
641 udp->vendor_id = desc->idVendor;
642 udp->product_id = desc->idProduct;
643 udp->device_class = desc->bDeviceClass;
644 udp->device_subclass = desc->bDeviceSubClass;
645 udp->device_protocol = desc->bDeviceProtocol;
646
647 return &udp[1];
648 }
649 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100650 debug("%s(%u) %s: unhandled device class: %s (%u)\n",
651 __FILE__, __LINE__, __func__,
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400652 dev->name, dev->driver->id);
653 return dp_fill(buf, dev->parent);
654 }
655}
656
657/* Construct a device-path from a device: */
658struct efi_device_path *efi_dp_from_dev(struct udevice *dev)
659{
660 void *buf, *start;
661
662 start = buf = dp_alloc(dp_size(dev) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100663 if (!buf)
664 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400665 buf = dp_fill(buf, dev);
666 *((struct efi_device_path *)buf) = END;
667
668 return start;
669}
670#endif
671
672static unsigned dp_part_size(struct blk_desc *desc, int part)
673{
674 unsigned dpsize;
675
676#ifdef CONFIG_BLK
Heinrich Schuchardt3a615c82017-12-11 12:56:43 +0100677 {
678 struct udevice *dev;
679 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
680
681 if (ret)
682 dev = desc->bdev->parent;
683 dpsize = dp_size(dev);
684 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400685#else
686 dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb);
687#endif
688
689 if (part == 0) /* the actual disk, not a partition */
690 return dpsize;
691
692 if (desc->part_type == PART_TYPE_ISO)
693 dpsize += sizeof(struct efi_device_path_cdrom_path);
694 else
695 dpsize += sizeof(struct efi_device_path_hard_drive_path);
696
697 return dpsize;
698}
699
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100700/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100701 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100702 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200703 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100704 * @desc block device descriptor
705 * @part partition number, 0 identifies a block device
706 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100707static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400708{
709 disk_partition_t info;
710
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400711 part_get_info(desc, part, &info);
712
713 if (desc->part_type == PART_TYPE_ISO) {
714 struct efi_device_path_cdrom_path *cddp = buf;
715
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100716 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400717 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
718 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
719 cddp->dp.length = sizeof(*cddp);
720 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200721 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400722
723 buf = &cddp[1];
724 } else {
725 struct efi_device_path_hard_drive_path *hddp = buf;
726
727 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
728 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
729 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100730 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400731 hddp->partition_start = info.start;
732 hddp->partition_end = info.size;
733 if (desc->part_type == PART_TYPE_EFI)
734 hddp->partmap_type = 2;
735 else
736 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100737
738 switch (desc->sig_type) {
739 case SIG_TYPE_NONE:
740 default:
741 hddp->signature_type = 0;
742 memset(hddp->partition_signature, 0,
743 sizeof(hddp->partition_signature));
744 break;
745 case SIG_TYPE_MBR:
746 hddp->signature_type = 1;
747 memset(hddp->partition_signature, 0,
748 sizeof(hddp->partition_signature));
749 memcpy(hddp->partition_signature, &desc->mbr_sig,
750 sizeof(desc->mbr_sig));
751 break;
752 case SIG_TYPE_GUID:
753 hddp->signature_type = 2;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400754 memcpy(hddp->partition_signature, &desc->guid_sig,
755 sizeof(hddp->partition_signature));
Jonathan Gray84b4d702017-11-22 14:18:59 +1100756 break;
757 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400758
759 buf = &hddp[1];
760 }
761
762 return buf;
763}
764
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100765/*
766 * Create a device path for a block device or one of its partitions.
767 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200768 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100769 * @desc block device descriptor
770 * @part partition number, 0 identifies a block device
771 */
772static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
773{
774#ifdef CONFIG_BLK
775 {
776 struct udevice *dev;
777 int ret = blk_find_device(desc->if_type, desc->devnum, &dev);
778
779 if (ret)
780 dev = desc->bdev->parent;
781 buf = dp_fill(buf, dev);
782 }
783#else
784 /*
785 * We *could* make a more accurate path, by looking at if_type
786 * and handling all the different cases like we do for non-
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200787 * legacy (i.e. CONFIG_BLK=y) case. But most important thing
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100788 * is just to have a unique device-path for if_type+devnum.
789 * So map things to a fictitious USB device.
790 */
791 struct efi_device_path_usb *udp;
792
793 memcpy(buf, &ROOT, sizeof(ROOT));
794 buf += sizeof(ROOT);
795
796 udp = buf;
797 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
798 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
799 udp->dp.length = sizeof(*udp);
800 udp->parent_port_number = desc->if_type;
801 udp->usb_interface = desc->devnum;
802 buf = &udp[1];
803#endif
804
805 if (part == 0) /* the actual disk, not a partition */
806 return buf;
807
808 return dp_part_node(buf, desc, part);
809}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400810
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200811/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400812struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
813{
814 void *buf, *start;
815
816 start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100817 if (!buf)
818 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400819
820 buf = dp_part_fill(buf, desc, part);
821
822 *((struct efi_device_path *)buf) = END;
823
824 return start;
825}
826
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100827/*
828 * Create a device node for a block device partition.
829 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200830 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100831 * @desc block device descriptor
832 * @part partition number, 0 identifies a block device
833 */
834struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
835{
836 efi_uintn_t dpsize;
837 void *buf;
838
839 if (desc->part_type == PART_TYPE_ISO)
840 dpsize = sizeof(struct efi_device_path_cdrom_path);
841 else
842 dpsize = sizeof(struct efi_device_path_hard_drive_path);
843 buf = dp_alloc(dpsize);
844
845 dp_part_node(buf, desc, part);
846
847 return buf;
848}
849
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200850/**
851 * path_to_uefi() - convert UTF-8 path to an UEFI style path
852 *
853 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
854 * separators and UTF-16).
855 *
856 * @src: source buffer
857 * @uefi: target buffer, possibly unaligned
858 */
859static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400860{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200861 u16 *pos = uefi;
862
863 /*
864 * efi_set_bootdev() calls this routine indirectly before the UEFI
865 * subsystem is initialized. So we cannot assume unaligned access to be
866 * enabled.
867 */
868 allow_unaligned();
869
870 while (*src) {
871 s32 code = utf8_get(&src);
872
873 if (code < 0)
874 code = '?';
875 else if (code == '/')
876 code = '\\';
877 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400878 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200879 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400880}
881
882/*
883 * If desc is NULL, this creates a path with only the file component,
884 * otherwise it creates a full path with both device and file components
885 */
886struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part,
887 const char *path)
888{
889 struct efi_device_path_file_path *fp;
890 void *buf, *start;
891 unsigned dpsize = 0, fpsize;
892
893 if (desc)
894 dpsize = dp_part_size(desc, part);
895
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200896 fpsize = sizeof(struct efi_device_path) +
897 2 * (utf8_utf16_strlen(path) + 1);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400898 dpsize += fpsize;
899
900 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100901 if (!buf)
902 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400903
904 if (desc)
905 buf = dp_part_fill(buf, desc, part);
906
907 /* add file-path: */
908 fp = buf;
909 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
910 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
911 fp->dp.length = fpsize;
912 path_to_uefi(fp->str, path);
913 buf += fpsize;
914
915 *((struct efi_device_path *)buf) = END;
916
917 return start;
918}
919
Joe Hershberger5277a972018-04-13 15:26:39 -0500920#ifdef CONFIG_NET
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400921struct efi_device_path *efi_dp_from_eth(void)
922{
Alexander Grafc4e983f2018-03-15 17:33:38 +0100923#ifndef CONFIG_DM_ETH
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400924 struct efi_device_path_mac_addr *ndp;
Alexander Grafc4e983f2018-03-15 17:33:38 +0100925#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400926 void *buf, *start;
927 unsigned dpsize = 0;
928
929 assert(eth_get_dev());
930
931#ifdef CONFIG_DM_ETH
932 dpsize += dp_size(eth_get_dev());
933#else
934 dpsize += sizeof(ROOT);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400935 dpsize += sizeof(*ndp);
Alexander Grafc4e983f2018-03-15 17:33:38 +0100936#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400937
938 start = buf = dp_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100939 if (!buf)
940 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400941
942#ifdef CONFIG_DM_ETH
943 buf = dp_fill(buf, eth_get_dev());
944#else
945 memcpy(buf, &ROOT, sizeof(ROOT));
946 buf += sizeof(ROOT);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400947
948 ndp = buf;
949 ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
950 ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
951 ndp->dp.length = sizeof(*ndp);
Alexander Grafc4e983f2018-03-15 17:33:38 +0100952 ndp->if_type = 1; /* Ethernet */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400953 memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN);
954 buf = &ndp[1];
Alexander Grafc4e983f2018-03-15 17:33:38 +0100955#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400956
957 *((struct efi_device_path *)buf) = END;
958
959 return start;
960}
961#endif
962
Rob Clark18ceba72017-10-10 08:23:06 -0400963/* Construct a device-path for memory-mapped image */
964struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
965 uint64_t start_address,
966 uint64_t end_address)
967{
968 struct efi_device_path_memory *mdp;
969 void *buf, *start;
970
971 start = buf = dp_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100972 if (!buf)
973 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -0400974
975 mdp = buf;
976 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
977 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
978 mdp->dp.length = sizeof(*mdp);
979 mdp->memory_type = memory_type;
980 mdp->start_address = start_address;
981 mdp->end_address = end_address;
982 buf = &mdp[1];
983
984 *((struct efi_device_path *)buf) = END;
985
986 return start;
987}
988
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +0100989/**
990 * efi_dp_split_file_path() - split of relative file path from device path
991 *
992 * Given a device path indicating a file on a device, separate the device
993 * path in two: the device path of the actual device and the file path
994 * relative to this device.
995 *
996 * @full_path: device path including device and file path
997 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +0200998 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +0100999 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001000 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001001efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1002 struct efi_device_path **device_path,
1003 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001004{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001005 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001006
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001007 *device_path = NULL;
1008 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001009 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001010 if (!dp)
1011 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001012 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001013 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001014 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001015 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001016 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001017 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001018 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001019 if (!fp)
1020 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001021 p->type = DEVICE_PATH_TYPE_END;
1022 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1023 p->length = sizeof(*p);
1024
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001025out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001026 *device_path = dp;
1027 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001028 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001029}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001030
1031efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1032 const char *path,
1033 struct efi_device_path **device,
1034 struct efi_device_path **file)
1035{
1036 int is_net;
1037 struct blk_desc *desc = NULL;
1038 disk_partition_t fs_partition;
1039 int part = 0;
1040 char filename[32] = { 0 }; /* dp->str is u16[32] long */
1041 char *s;
1042
AKASHI Takahiro39844412018-11-05 18:06:40 +09001043 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001044 return EFI_INVALID_PARAMETER;
1045
1046 is_net = !strcmp(dev, "Net");
1047 if (!is_net) {
1048 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1049 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001050 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001051 return EFI_INVALID_PARAMETER;
1052
AKASHI Takahiro39844412018-11-05 18:06:40 +09001053 if (device)
1054 *device = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001055 } else {
1056#ifdef CONFIG_NET
AKASHI Takahiro39844412018-11-05 18:06:40 +09001057 if (device)
1058 *device = efi_dp_from_eth();
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001059#endif
1060 }
1061
1062 if (!path)
1063 return EFI_SUCCESS;
1064
Heinrich Schuchardt7d698072019-02-23 11:20:23 +01001065 snprintf(filename, sizeof(filename), "%s", path);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001066 /* DOS style file path: */
1067 s = filename;
1068 while ((s = strchr(s, '/')))
1069 *s++ = '\\';
AKASHI Takahiro39844412018-11-05 18:06:40 +09001070 *file = efi_dp_from_file(((!is_net && device) ? desc : NULL),
1071 part, filename);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001072
1073 return EFI_SUCCESS;
1074}