blob: 19e8861ef492a2aaf7223beb0dbddd1ff0b3cacb [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>
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +020013#include <dm/root.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <net.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040016#include <usb.h>
17#include <mmc.h>
Patrick Wildta3ca37e2019-10-03 16:24:17 +020018#include <nvme.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040019#include <efi_loader.h>
Rob Clarkf90cb9f2017-09-13 18:05:28 -040020#include <part.h>
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
Tobias Waldekranz32152682023-02-16 16:33:55 +010025#ifdef CONFIG_BLKMAP
26const efi_guid_t efi_guid_blkmap_dev = U_BOOT_BLKMAP_DEV_GUID;
27#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +090028#ifdef CONFIG_SANDBOX
29const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID;
30#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +020031#ifdef CONFIG_VIRTIO_BLK
32const efi_guid_t efi_guid_virtio_dev = U_BOOT_VIRTIO_DEV_GUID;
33#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +090034
Rob Clarkf90cb9f2017-09-13 18:05:28 -040035/* template END node: */
Masahisa Kojima97bd9da2022-06-19 13:55:59 +090036const struct efi_device_path END = {
Rob Clarkf90cb9f2017-09-13 18:05:28 -040037 .type = DEVICE_PATH_TYPE_END,
38 .sub_type = DEVICE_PATH_SUB_TYPE_END,
39 .length = sizeof(END),
40};
41
Simon Glass222f3cb2021-09-24 18:30:17 -060042#if defined(CONFIG_MMC)
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010043/*
44 * Determine if an MMC device is an SD card.
45 *
46 * @desc block device descriptor
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010047 * Return: true if the device is an SD card
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +010048 */
49static bool is_sd(struct blk_desc *desc)
50{
51 struct mmc *mmc = find_mmc_device(desc->devnum);
52
53 if (!mmc)
54 return false;
55
56 return IS_SD(mmc) != 0U;
57}
58#endif
59
Rob Clarkf90cb9f2017-09-13 18:05:28 -040060/*
61 * Iterate to next block in device-path, terminating (returning NULL)
62 * at /End* node.
63 */
64struct efi_device_path *efi_dp_next(const struct efi_device_path *dp)
65{
66 if (dp == NULL)
67 return NULL;
68 if (dp->type == DEVICE_PATH_TYPE_END)
69 return NULL;
70 dp = ((void *)dp) + dp->length;
71 if (dp->type == DEVICE_PATH_TYPE_END)
72 return NULL;
73 return (struct efi_device_path *)dp;
74}
75
76/*
77 * Compare two device-paths, stopping when the shorter of the two hits
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +020078 * an End* node. This is useful to, for example, compare a device-path
Rob Clarkf90cb9f2017-09-13 18:05:28 -040079 * representing a device with one representing a file on the device, or
80 * a device with a parent device.
81 */
Heinrich Schuchardt753e2482017-10-26 19:25:48 +020082int efi_dp_match(const struct efi_device_path *a,
83 const struct efi_device_path *b)
Rob Clarkf90cb9f2017-09-13 18:05:28 -040084{
85 while (1) {
86 int ret;
87
88 ret = memcmp(&a->length, &b->length, sizeof(a->length));
89 if (ret)
90 return ret;
91
92 ret = memcmp(a, b, a->length);
93 if (ret)
94 return ret;
95
96 a = efi_dp_next(a);
97 b = efi_dp_next(b);
98
99 if (!a || !b)
100 return 0;
101 }
102}
103
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100104/**
105 * efi_dp_shorten() - shorten device-path
106 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200107 * When creating a short boot option we want to use a device-path that is
108 * independent of the location where the block device is plugged in.
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400109 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200110 * UsbWwi() nodes contain a serial number, hard drive paths a partition
111 * UUID. Both should be unique.
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200112 *
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200113 * See UEFI spec, section 3.1.2 for "short-form device path".
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100114 *
Heinrich Schuchardtdb17dbc2022-03-21 08:26:48 +0100115 * @dp: original device-path
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100116 * @Return: shortened device-path or NULL
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400117 */
Heinrich Schuchardt24f0e6a2022-02-26 12:10:10 +0100118struct efi_device_path *efi_dp_shorten(struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400119{
120 while (dp) {
Heinrich Schuchardta7ffd902023-03-26 12:22:40 +0200121 if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_WWI) ||
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400122 EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) ||
123 EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH))
124 return dp;
125
126 dp = efi_dp_next(dp);
127 }
128
129 return dp;
130}
131
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100132/**
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100133 * find_handle() - find handle by device path and installed protocol
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100134 *
135 * If @rem is provided, the handle with the longest partial match is returned.
136 *
137 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100138 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100139 * @short_path: use short form device path for matching
140 * @rem: pointer to receive remaining device path
141 * Return: matching handle
142 */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100143static efi_handle_t find_handle(struct efi_device_path *dp,
144 const efi_guid_t *guid, bool short_path,
145 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400146{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100147 efi_handle_t handle, best_handle = NULL;
148 efi_uintn_t len, best_len = 0;
149
150 len = efi_dp_instance_size(dp);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400151
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100152 list_for_each_entry(handle, &efi_obj_list, link) {
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100153 struct efi_handler *handler;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100154 struct efi_device_path *dp_current;
155 efi_uintn_t len_current;
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100156 efi_status_t ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400157
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100158 if (guid) {
159 ret = efi_search_protocol(handle, guid, &handler);
160 if (ret != EFI_SUCCESS)
161 continue;
162 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100163 ret = efi_search_protocol(handle, &efi_guid_device_path,
164 &handler);
Heinrich Schuchardt6953c102017-11-26 14:05:16 +0100165 if (ret != EFI_SUCCESS)
166 continue;
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100167 dp_current = handler->protocol_interface;
168 if (short_path) {
169 dp_current = efi_dp_shorten(dp_current);
170 if (!dp_current)
171 continue;
172 }
173 len_current = efi_dp_instance_size(dp_current);
174 if (rem) {
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100175 if (len_current > len)
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100176 continue;
177 } else {
178 if (len_current != len)
179 continue;
180 }
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100181 if (memcmp(dp_current, dp, len_current))
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100182 continue;
183 if (!rem)
184 return handle;
185 if (len_current > best_len) {
186 best_len = len_current;
187 best_handle = handle;
188 *rem = (void*)((u8 *)dp + len_current);
189 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400190 }
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100191 return best_handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400192}
193
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100194/**
195 * efi_dp_find_obj() - find handle by device path
196 *
197 * If @rem is provided, the handle with the longest partial match is returned.
198 *
199 * @dp: device path to search
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100200 * @guid: GUID of protocol that must be installed on path or NULL
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100201 * @rem: pointer to receive remaining device path
202 * Return: matching handle
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400203 */
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100204efi_handle_t efi_dp_find_obj(struct efi_device_path *dp,
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100205 const efi_guid_t *guid,
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100206 struct efi_device_path **rem)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400207{
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100208 efi_handle_t handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400209
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100210 handle = find_handle(dp, guid, false, rem);
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100211 if (!handle)
212 /* Match short form device path */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100213 handle = find_handle(dp, guid, true, rem);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400214
Heinrich Schuchardtad6d5a42022-03-04 08:20:00 +0100215 return handle;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400216}
217
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100218/*
219 * Determine the last device path node that is not the end node.
220 *
221 * @dp device path
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100222 * Return: last node before the end node if it exists
Heinrich Schuchardt0976f8b2018-01-19 20:24:49 +0100223 * otherwise NULL
224 */
225const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp)
226{
227 struct efi_device_path *ret;
228
229 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
230 return NULL;
231 while (dp) {
232 ret = (struct efi_device_path *)dp;
233 dp = efi_dp_next(dp);
234 }
235 return ret;
236}
237
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200238/* get size of the first device path instance excluding end node */
239efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400240{
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200241 efi_uintn_t sz = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400242
Heinrich Schuchardt01d48ed2018-04-16 07:59:07 +0200243 if (!dp || dp->type == DEVICE_PATH_TYPE_END)
244 return 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400245 while (dp) {
246 sz += dp->length;
247 dp = efi_dp_next(dp);
248 }
249
250 return sz;
251}
252
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200253/* get size of multi-instance device path excluding end node */
254efi_uintn_t efi_dp_size(const struct efi_device_path *dp)
255{
256 const struct efi_device_path *p = dp;
257
258 if (!p)
259 return 0;
260 while (p->type != DEVICE_PATH_TYPE_END ||
261 p->sub_type != DEVICE_PATH_SUB_TYPE_END)
262 p = (void *)p + p->length;
263
264 return (void *)p - (void *)dp;
265}
266
267/* copy multi-instance device path */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400268struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp)
269{
270 struct efi_device_path *ndp;
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200271 size_t sz = efi_dp_size(dp) + sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400272
273 if (!dp)
274 return NULL;
275
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100276 ndp = efi_alloc(sz);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100277 if (!ndp)
278 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400279 memcpy(ndp, dp, sz);
280
281 return ndp;
282}
283
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200284/**
285 * efi_dp_append_or_concatenate() - Append or concatenate two device paths.
286 * Concatenated device path will be separated
287 * by a sub-type 0xff end node
288 *
289 * @dp1: First device path
290 * @dp2: Second device path
291 * @concat: If true the two device paths will be concatenated and separated
292 * by an end of entrire device path sub-type 0xff end node.
293 * If true the second device path will be appended to the first and
294 * terminated by an end node
295 *
296 * Return:
297 * concatenated device path or NULL. Caller must free the returned value
298 */
299static struct
300efi_device_path *efi_dp_append_or_concatenate(const struct efi_device_path *dp1,
301 const struct efi_device_path *dp2,
302 bool concat)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400303{
304 struct efi_device_path *ret;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200305 size_t end_size = sizeof(END);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400306
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200307 if (concat)
308 end_size = 2 * sizeof(END);
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200309 if (!dp1 && !dp2) {
310 /* return an end node */
311 ret = efi_dp_dup(&END);
312 } else if (!dp1) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400313 ret = efi_dp_dup(dp2);
314 } else if (!dp2) {
315 ret = efi_dp_dup(dp1);
316 } else {
317 /* both dp1 and dp2 are non-null */
318 unsigned sz1 = efi_dp_size(dp1);
319 unsigned sz2 = efi_dp_size(dp2);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100320 void *p = efi_alloc(sz1 + sz2 + end_size);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100321 if (!p)
322 return NULL;
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200323 ret = p;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400324 memcpy(p, dp1, sz1);
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200325 p += sz1;
326
327 if (concat) {
328 memcpy(p, &END, sizeof(END));
329 p += sizeof(END);
330 }
331
Heinrich Schuchardt60b5ab22018-04-16 07:59:06 +0200332 /* the end node of the second device path has to be retained */
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200333 memcpy(p, dp2, sz2);
334 p += sz2;
335 memcpy(p, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400336 }
337
338 return ret;
339}
340
Ilias Apalodimas483d28e2021-03-17 21:54:58 +0200341/**
342 * efi_dp_append() - Append a device to an existing device path.
343 *
344 * @dp1: First device path
345 * @dp2: Second device path
346 *
347 * Return:
348 * concatenated device path or NULL. Caller must free the returned value
349 */
350struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1,
351 const struct efi_device_path *dp2)
352{
353 return efi_dp_append_or_concatenate(dp1, dp2, false);
354}
355
356/**
357 * efi_dp_concat() - Concatenate 2 device paths. The final device path will
358 * contain two device paths separated by and end node (0xff).
359 *
360 * @dp1: First device path
361 * @dp2: Second device path
362 *
363 * Return:
364 * concatenated device path or NULL. Caller must free the returned value
365 */
366struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
367 const struct efi_device_path *dp2)
368{
369 return efi_dp_append_or_concatenate(dp1, dp2, true);
370}
371
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400372struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp,
373 const struct efi_device_path *node)
374{
375 struct efi_device_path *ret;
376
377 if (!node && !dp) {
378 ret = efi_dp_dup(&END);
379 } else if (!node) {
380 ret = efi_dp_dup(dp);
381 } else if (!dp) {
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200382 size_t sz = node->length;
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100383 void *p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100384 if (!p)
385 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400386 memcpy(p, node, sz);
387 memcpy(p + sz, &END, sizeof(END));
388 ret = p;
389 } else {
390 /* both dp and node are non-null */
Heinrich Schuchardt51ca4f52018-04-16 07:59:08 +0200391 size_t sz = efi_dp_size(dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100392 void *p = efi_alloc(sz + node->length + 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, dp, sz);
396 memcpy(p + sz, node, node->length);
397 memcpy(p + sz + node->length, &END, sizeof(END));
398 ret = p;
399 }
400
401 return ret;
402}
403
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200404struct efi_device_path *efi_dp_create_device_node(const u8 type,
405 const u8 sub_type,
406 const u16 length)
407{
408 struct efi_device_path *ret;
409
Heinrich Schuchardtc96c5942019-04-23 00:51:01 +0200410 if (length < sizeof(struct efi_device_path))
411 return NULL;
412
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100413 ret = efi_alloc(length);
Heinrich Schuchardtb41c8e22018-04-16 07:59:05 +0200414 if (!ret)
415 return ret;
416 ret->type = type;
417 ret->sub_type = sub_type;
418 ret->length = length;
419 return ret;
420}
421
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200422struct efi_device_path *efi_dp_append_instance(
423 const struct efi_device_path *dp,
424 const struct efi_device_path *dpi)
425{
426 size_t sz, szi;
427 struct efi_device_path *p, *ret;
428
429 if (!dpi)
430 return NULL;
431 if (!dp)
432 return efi_dp_dup(dpi);
433 sz = efi_dp_size(dp);
434 szi = efi_dp_instance_size(dpi);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100435 p = efi_alloc(sz + szi + 2 * sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200436 if (!p)
437 return NULL;
438 ret = p;
439 memcpy(p, dp, sz + sizeof(END));
440 p = (void *)p + sz;
441 p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END;
442 p = (void *)p + sizeof(END);
443 memcpy(p, dpi, szi);
444 p = (void *)p + szi;
445 memcpy(p, &END, sizeof(END));
446 return ret;
447}
448
449struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp,
450 efi_uintn_t *size)
451{
452 size_t sz;
453 struct efi_device_path *p;
454
455 if (size)
456 *size = 0;
457 if (!dp || !*dp)
458 return NULL;
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200459 sz = efi_dp_instance_size(*dp);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100460 p = efi_alloc(sz + sizeof(END));
Heinrich Schuchardtcb0f7ce2018-04-16 07:59:09 +0200461 if (!p)
462 return NULL;
463 memcpy(p, *dp, sz + sizeof(END));
464 *dp = (void *)*dp + sz;
465 if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END)
466 *dp = (void *)*dp + sizeof(END);
467 else
468 *dp = NULL;
469 if (size)
470 *size = sz + sizeof(END);
471 return p;
472}
473
474bool efi_dp_is_multi_instance(const struct efi_device_path *dp)
475{
476 const struct efi_device_path *p = dp;
477
478 if (!p)
479 return false;
480 while (p->type != DEVICE_PATH_TYPE_END)
481 p = (void *)p + p->length;
482 return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END;
483}
484
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400485/* size of device-path not including END node for device and all parents
486 * up to the root device.
487 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100488__maybe_unused static unsigned int dp_size(struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400489{
490 if (!dev || !dev->driver)
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200491 return sizeof(struct efi_device_path_udevice);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400492
Simon Glass56ada7b2022-01-29 14:58:38 -0700493 switch (device_get_uclass_id(dev)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400494 case UCLASS_ROOT:
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400495 /* stop traversing parents at this point: */
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200496 return sizeof(struct efi_device_path_udevice);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100497 case UCLASS_ETH:
498 return dp_size(dev->parent) +
499 sizeof(struct efi_device_path_mac_addr);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100500 case UCLASS_BLK:
501 switch (dev->parent->uclass->uc_drv->id) {
502#ifdef CONFIG_IDE
503 case UCLASS_IDE:
504 return dp_size(dev->parent) +
505 sizeof(struct efi_device_path_atapi);
506#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600507#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100508 case UCLASS_SCSI:
509 return dp_size(dev->parent) +
510 sizeof(struct efi_device_path_scsi);
511#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600512#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100513 case UCLASS_MMC:
514 return dp_size(dev->parent) +
515 sizeof(struct efi_device_path_sd_mmc_path);
516#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200517#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
518 case UCLASS_AHCI:
519 return dp_size(dev->parent) +
520 sizeof(struct efi_device_path_sata);
521#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200522#if defined(CONFIG_NVME)
523 case UCLASS_NVME:
524 return dp_size(dev->parent) +
525 sizeof(struct efi_device_path_nvme);
526#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900527#ifdef CONFIG_SANDBOX
Simon Glasse57f8d42022-10-29 19:47:17 -0600528 case UCLASS_HOST:
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900529 /*
530 * Sandbox's host device will be represented
531 * as vendor device with extra one byte for
532 * device number
533 */
534 return dp_size(dev->parent)
535 + sizeof(struct efi_device_path_vendor) + 1;
536#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100537#ifdef CONFIG_USB
538 case UCLASS_MASS_STORAGE:
539 return dp_size(dev->parent)
540 + sizeof(struct efi_device_path_controller);
541#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200542#ifdef CONFIG_VIRTIO_BLK
543 case UCLASS_VIRTIO:
544 /*
545 * Virtio devices will be represented as a vendor
546 * device node with an extra byte for the device
547 * number.
548 */
549 return dp_size(dev->parent)
550 + sizeof(struct efi_device_path_vendor) + 1;
551#endif
Tobias Waldekranz32152682023-02-16 16:33:55 +0100552#ifdef CONFIG_BLKMAP
553 case UCLASS_BLKMAP:
554 /*
555 * blkmap devices will be represented as a vendor
556 * device node with an extra byte for the device
557 * number.
558 */
559 return dp_size(dev->parent)
560 + sizeof(struct efi_device_path_vendor) + 1;
561#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100562 default:
563 return dp_size(dev->parent);
564 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600565#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400566 case UCLASS_MMC:
567 return dp_size(dev->parent) +
568 sizeof(struct efi_device_path_sd_mmc_path);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100569#endif
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400570 case UCLASS_MASS_STORAGE:
571 case UCLASS_USB_HUB:
572 return dp_size(dev->parent) +
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100573 sizeof(struct efi_device_path_usb);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400574 default:
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200575 return dp_size(dev->parent) +
576 sizeof(struct efi_device_path_udevice);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400577 }
578}
579
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100580/*
581 * Recursively build a device path.
582 *
583 * @buf pointer to the end of the device path
584 * @dev device
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100585 * Return: pointer to the end of the device path
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100586 */
Heinrich Schuchardtc22d9e32019-11-10 02:16:33 +0100587__maybe_unused static void *dp_fill(void *buf, struct udevice *dev)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400588{
589 if (!dev || !dev->driver)
590 return buf;
591
Simon Glass56ada7b2022-01-29 14:58:38 -0700592 switch (device_get_uclass_id(dev)) {
Jan Kiszkaf1389822022-10-14 18:10:06 +0200593#ifdef CONFIG_NETDEVICES
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100594 case UCLASS_ETH: {
595 struct efi_device_path_mac_addr *dp =
596 dp_fill(buf, dev->parent);
Simon Glass95588622020-12-22 19:30:28 -0700597 struct eth_pdata *pdata = dev_get_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100598
599 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
600 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR;
601 dp->dp.length = sizeof(*dp);
602 memset(&dp->mac, 0, sizeof(dp->mac));
603 /* We only support IPv4 */
604 memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN);
605 /* Ethernet */
606 dp->if_type = 1;
607 return &dp[1];
608 }
609#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100610 case UCLASS_BLK:
611 switch (dev->parent->uclass->uc_drv->id) {
Tobias Waldekranz32152682023-02-16 16:33:55 +0100612#ifdef CONFIG_BLKMAP
613 case UCLASS_BLKMAP: {
614 struct efi_device_path_vendor *dp;
615 struct blk_desc *desc = dev_get_uclass_plat(dev);
616
Heinrich Schuchardtb8ea6ff2023-07-19 16:49:46 +0200617 dp = dp_fill(buf, dev->parent);
Tobias Waldekranz32152682023-02-16 16:33:55 +0100618 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
619 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
620 dp->dp.length = sizeof(*dp) + 1;
621 memcpy(&dp->guid, &efi_guid_blkmap_dev,
622 sizeof(efi_guid_t));
623 dp->vendor_data[0] = desc->devnum;
624 return &dp->vendor_data[1];
625 }
626#endif
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900627#ifdef CONFIG_SANDBOX
Simon Glasse57f8d42022-10-29 19:47:17 -0600628 case UCLASS_HOST: {
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900629 /* stop traversing parents at this point: */
Heinrich Schuchardt1e3beaf2020-05-06 01:28:08 +0200630 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700631 struct blk_desc *desc = dev_get_uclass_plat(dev);
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900632
Heinrich Schuchardtb8ea6ff2023-07-19 16:49:46 +0200633 dp = dp_fill(buf, dev->parent);
AKASHI Takahiro659a6262019-09-12 13:52:35 +0900634 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
635 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
636 dp->dp.length = sizeof(*dp) + 1;
637 memcpy(&dp->guid, &efi_guid_host_dev,
638 sizeof(efi_guid_t));
639 dp->vendor_data[0] = desc->devnum;
640 return &dp->vendor_data[1];
641 }
642#endif
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200643#ifdef CONFIG_VIRTIO_BLK
644 case UCLASS_VIRTIO: {
645 struct efi_device_path_vendor *dp;
Simon Glass71fa5b42020-12-03 16:55:18 -0700646 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200647
Heinrich Schuchardtb8ea6ff2023-07-19 16:49:46 +0200648 dp = dp_fill(buf, dev->parent);
Heinrich Schuchardtc770aaa2020-05-20 22:39:35 +0200649 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
650 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
651 dp->dp.length = sizeof(*dp) + 1;
652 memcpy(&dp->guid, &efi_guid_virtio_dev,
653 sizeof(efi_guid_t));
654 dp->vendor_data[0] = desc->devnum;
655 return &dp->vendor_data[1];
656 }
657#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100658#ifdef CONFIG_IDE
659 case UCLASS_IDE: {
660 struct efi_device_path_atapi *dp =
661 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700662 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100663
664 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
665 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI;
666 dp->dp.length = sizeof(*dp);
667 dp->logical_unit_number = desc->devnum;
668 dp->primary_secondary = IDE_BUS(desc->devnum);
669 dp->slave_master = desc->devnum %
670 (CONFIG_SYS_IDE_MAXDEVICE /
671 CONFIG_SYS_IDE_MAXBUS);
672 return &dp[1];
673 }
674#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600675#if defined(CONFIG_SCSI)
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100676 case UCLASS_SCSI: {
677 struct efi_device_path_scsi *dp =
678 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700679 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100680
681 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
682 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI;
683 dp->dp.length = sizeof(*dp);
684 dp->logical_unit_number = desc->lun;
685 dp->target_id = desc->target;
686 return &dp[1];
687 }
688#endif
Simon Glass222f3cb2021-09-24 18:30:17 -0600689#if defined(CONFIG_MMC)
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100690 case UCLASS_MMC: {
691 struct efi_device_path_sd_mmc_path *sddp =
692 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700693 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100694
695 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
696 sddp->dp.sub_type = is_sd(desc) ?
697 DEVICE_PATH_SUB_TYPE_MSG_SD :
698 DEVICE_PATH_SUB_TYPE_MSG_MMC;
699 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700700 sddp->slot_number = dev_seq(dev);
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100701 return &sddp[1];
702 }
703#endif
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200704#if defined(CONFIG_AHCI) || defined(CONFIG_SATA)
705 case UCLASS_AHCI: {
706 struct efi_device_path_sata *dp =
707 dp_fill(buf, dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700708 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt7bdb6022020-05-20 23:12:02 +0200709
710 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
711 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA;
712 dp->dp.length = sizeof(*dp);
713 dp->hba_port = desc->devnum;
714 /* default 0xffff implies no port multiplier */
715 dp->port_multiplier_port = 0xffff;
716 dp->logical_unit_number = desc->lun;
717 return &dp[1];
718 }
719#endif
Patrick Wildta3ca37e2019-10-03 16:24:17 +0200720#if defined(CONFIG_NVME)
721 case UCLASS_NVME: {
722 struct efi_device_path_nvme *dp =
723 dp_fill(buf, dev->parent);
724 u32 ns_id;
725
726 dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
727 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME;
728 dp->dp.length = sizeof(*dp);
729 nvme_get_namespace_id(dev, &ns_id, dp->eui64);
730 memcpy(&dp->ns_id, &ns_id, sizeof(ns_id));
731 return &dp[1];
732 }
733#endif
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100734#if defined(CONFIG_USB)
735 case UCLASS_MASS_STORAGE: {
Heinrich Schuchardt232c9db2023-04-01 07:21:55 +0200736 struct blk_desc *desc = dev_get_uclass_plat(dev);
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100737 struct efi_device_path_controller *dp =
738 dp_fill(buf, dev->parent);
739
740 dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
741 dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CONTROLLER;
742 dp->dp.length = sizeof(*dp);
743 dp->controller_number = desc->lun;
744 return &dp[1];
745 }
746#endif
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100747 default:
Heinrich Schuchardt1f1d49d2018-01-20 21:02:18 +0100748 debug("%s(%u) %s: unhandled parent class: %s (%u)\n",
749 __FILE__, __LINE__, __func__,
750 dev->name, dev->parent->uclass->uc_drv->id);
Heinrich Schuchardte2dcb9a2017-12-11 12:56:44 +0100751 return dp_fill(buf, dev->parent);
752 }
Simon Glass222f3cb2021-09-24 18:30:17 -0600753#if defined(CONFIG_MMC)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400754 case UCLASS_MMC: {
755 struct efi_device_path_sd_mmc_path *sddp =
756 dp_fill(buf, dev->parent);
757 struct mmc *mmc = mmc_get_mmc_dev(dev);
758 struct blk_desc *desc = mmc_get_blk_desc(mmc);
759
760 sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt7d569db2017-12-11 12:56:39 +0100761 sddp->dp.sub_type = is_sd(desc) ?
762 DEVICE_PATH_SUB_TYPE_MSG_SD :
763 DEVICE_PATH_SUB_TYPE_MSG_MMC;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400764 sddp->dp.length = sizeof(*sddp);
Simon Glass75e534b2020-12-16 21:20:07 -0700765 sddp->slot_number = dev_seq(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400766
767 return &sddp[1];
768 }
769#endif
770 case UCLASS_MASS_STORAGE:
771 case UCLASS_USB_HUB: {
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100772 struct efi_device_path_usb *udp = dp_fill(buf, dev->parent);
773
774 switch (device_get_uclass_id(dev->parent)) {
775 case UCLASS_USB_HUB: {
776 struct usb_device *udev = dev_get_parent_priv(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400777
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100778 udp->parent_port_number = udev->portnr;
779 break;
780 }
781 default:
782 udp->parent_port_number = 0;
783 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400784 udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100785 udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400786 udp->dp.length = sizeof(*udp);
Heinrich Schuchardt25b18d62023-03-19 16:18:09 +0100787 udp->usb_interface = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400788
789 return &udp[1];
790 }
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +0200791 default: {
792 struct efi_device_path_udevice *vdp;
793 enum uclass_id uclass_id = device_get_uclass_id(dev);
794
795 if (uclass_id == UCLASS_ROOT)
796 vdp = buf;
797 else
798 vdp = dp_fill(buf, dev->parent);
799
800 vdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
801 vdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR;
802 vdp->dp.length = sizeof(*vdp);
803 memcpy(&vdp->guid, &efi_u_boot_guid, sizeof(efi_guid_t));
804 vdp->uclass_id = uclass_id;
805 vdp->dev_number = dev->seq_;
806
807 return &vdp[1];
808 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400809 }
810}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400811
812static unsigned dp_part_size(struct blk_desc *desc, int part)
813{
814 unsigned dpsize;
Simon Glassec209a72022-01-29 14:58:39 -0700815 struct udevice *dev = desc->bdev;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400816
Simon Glass222f3cb2021-09-24 18:30:17 -0600817 dpsize = dp_size(dev);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400818
819 if (part == 0) /* the actual disk, not a partition */
820 return dpsize;
821
822 if (desc->part_type == PART_TYPE_ISO)
823 dpsize += sizeof(struct efi_device_path_cdrom_path);
824 else
825 dpsize += sizeof(struct efi_device_path_hard_drive_path);
826
827 return dpsize;
828}
829
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100830/*
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100831 * Create a device node for a block device partition.
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100832 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200833 * @buf buffer to which the device path is written
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100834 * @desc block device descriptor
835 * @part partition number, 0 identifies a block device
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200836 *
837 * Return: pointer to position after the node
Heinrich Schuchardt8983ffd2017-12-11 12:56:42 +0100838 */
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100839static void *dp_part_node(void *buf, struct blk_desc *desc, int part)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400840{
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600841 struct disk_partition info;
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200842 int ret;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400843
Heinrich Schuchardtee69ae12023-05-27 08:18:28 +0200844 ret = part_get_info(desc, part, &info);
845 if (ret < 0)
846 return buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400847
848 if (desc->part_type == PART_TYPE_ISO) {
849 struct efi_device_path_cdrom_path *cddp = buf;
850
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100851 cddp->boot_entry = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400852 cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
853 cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH;
854 cddp->dp.length = sizeof(*cddp);
855 cddp->partition_start = info.start;
Heinrich Schuchardt28dfd1a2019-09-04 13:56:01 +0200856 cddp->partition_size = info.size;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400857
858 buf = &cddp[1];
859 } else {
860 struct efi_device_path_hard_drive_path *hddp = buf;
861
862 hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
863 hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH;
864 hddp->dp.length = sizeof(*hddp);
Heinrich Schuchardt2aae6db2017-12-11 12:56:40 +0100865 hddp->partition_number = part;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400866 hddp->partition_start = info.start;
867 hddp->partition_end = info.size;
868 if (desc->part_type == PART_TYPE_EFI)
869 hddp->partmap_type = 2;
870 else
871 hddp->partmap_type = 1;
Jonathan Gray84b4d702017-11-22 14:18:59 +1100872
873 switch (desc->sig_type) {
874 case SIG_TYPE_NONE:
875 default:
876 hddp->signature_type = 0;
877 memset(hddp->partition_signature, 0,
878 sizeof(hddp->partition_signature));
879 break;
880 case SIG_TYPE_MBR:
881 hddp->signature_type = 1;
882 memset(hddp->partition_signature, 0,
883 sizeof(hddp->partition_signature));
884 memcpy(hddp->partition_signature, &desc->mbr_sig,
885 sizeof(desc->mbr_sig));
886 break;
887 case SIG_TYPE_GUID:
888 hddp->signature_type = 2;
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900889#if CONFIG_IS_ENABLED(PARTITION_UUIDS)
890 /* info.uuid exists only with PARTITION_UUIDS */
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200891 if (uuid_str_to_bin(info.uuid,
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900892 hddp->partition_signature,
893 UUID_STR_FORMAT_GUID)) {
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200894 log_warning(
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900895 "Partition %d: invalid GUID %s\n",
Alfonso Sánchez-Beatof007a372021-07-15 15:31:42 +0200896 part, info.uuid);
AKASHI Takahiroae18a672022-04-19 10:01:56 +0900897 }
898#endif
Jonathan Gray84b4d702017-11-22 14:18:59 +1100899 break;
900 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400901
902 buf = &hddp[1];
903 }
904
905 return buf;
906}
907
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100908/*
909 * Create a device path for a block device or one of its partitions.
910 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200911 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100912 * @desc block device descriptor
913 * @part partition number, 0 identifies a block device
914 */
915static void *dp_part_fill(void *buf, struct blk_desc *desc, int part)
916{
Simon Glassec209a72022-01-29 14:58:39 -0700917 struct udevice *dev = desc->bdev;
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100918
Simon Glass222f3cb2021-09-24 18:30:17 -0600919 buf = dp_fill(buf, dev);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100920
921 if (part == 0) /* the actual disk, not a partition */
922 return buf;
923
924 return dp_part_node(buf, desc, part);
925}
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400926
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200927/* Construct a device-path from a partition on a block device: */
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400928struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part)
929{
930 void *buf, *start;
931
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100932 start = buf = efi_alloc(dp_part_size(desc, part) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +0100933 if (!buf)
934 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400935
936 buf = dp_part_fill(buf, desc, part);
937
938 *((struct efi_device_path *)buf) = END;
939
940 return start;
941}
942
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100943/*
944 * Create a device node for a block device partition.
945 *
Heinrich Schuchardtb21f8392018-10-17 21:55:24 +0200946 * @buf buffer to which the device path is written
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100947 * @desc block device descriptor
948 * @part partition number, 0 identifies a block device
949 */
950struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part)
951{
952 efi_uintn_t dpsize;
953 void *buf;
954
955 if (desc->part_type == PART_TYPE_ISO)
956 dpsize = sizeof(struct efi_device_path_cdrom_path);
957 else
958 dpsize = sizeof(struct efi_device_path_hard_drive_path);
Heinrich Schuchardt45640252023-03-19 09:20:22 +0100959 buf = efi_alloc(dpsize);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100960
Heinrich Schuchardte29fbb32022-10-06 13:36:02 +0200961 if (buf)
962 dp_part_node(buf, desc, part);
Heinrich Schuchardt6c6307c2018-01-19 20:24:46 +0100963
964 return buf;
965}
966
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200967/**
968 * path_to_uefi() - convert UTF-8 path to an UEFI style path
969 *
970 * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path
971 * separators and UTF-16).
972 *
973 * @src: source buffer
974 * @uefi: target buffer, possibly unaligned
975 */
976static void path_to_uefi(void *uefi, const char *src)
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400977{
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200978 u16 *pos = uefi;
979
980 /*
981 * efi_set_bootdev() calls this routine indirectly before the UEFI
982 * subsystem is initialized. So we cannot assume unaligned access to be
983 * enabled.
984 */
985 allow_unaligned();
986
987 while (*src) {
988 s32 code = utf8_get(&src);
989
990 if (code < 0)
991 code = '?';
992 else if (code == '/')
993 code = '\\';
994 utf16_put(code, &pos);
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400995 }
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +0200996 *pos = 0;
Rob Clarkf90cb9f2017-09-13 18:05:28 -0400997}
998
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +0000999/**
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +02001000 * efi_dp_from_file() - append file path node to device path.
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +00001001 *
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +02001002 * @dp: device path or NULL
1003 * @path: file path or NULL
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +00001004 * Return: device path or NULL in case of an error
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001005 */
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +02001006struct efi_device_path *efi_dp_from_file(const struct efi_device_path *dp,
1007 const char *path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001008{
1009 struct efi_device_path_file_path *fp;
Heinrich Schuchardt16eff692023-05-13 10:18:24 +02001010 void *buf, *pos;
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +02001011 size_t dpsize, fpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001012
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +02001013 dpsize = efi_dp_size(dp);
Heinrich Schuchardt8d80af82019-07-14 19:26:47 +02001014 fpsize = sizeof(struct efi_device_path) +
1015 2 * (utf8_utf16_strlen(path) + 1);
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001016 if (fpsize > U16_MAX)
1017 return NULL;
1018
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +02001019 buf = efi_alloc(dpsize + fpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001020 if (!buf)
1021 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001022
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +02001023 memcpy(buf, dp, dpsize);
1024 pos = buf + dpsize;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001025
1026 /* add file-path: */
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +00001027 if (*path) {
Heinrich Schuchardt16eff692023-05-13 10:18:24 +02001028 fp = pos;
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +00001029 fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE;
1030 fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH;
1031 fp->dp.length = (u16)fpsize;
1032 path_to_uefi(fp->str, path);
Heinrich Schuchardt16eff692023-05-13 10:18:24 +02001033 pos += fpsize;
Heinrich Schuchardtf57eacb2022-06-11 05:22:08 +00001034 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001035
Heinrich Schuchardt16eff692023-05-13 10:18:24 +02001036 memcpy(pos, &END, sizeof(END));
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001037
Heinrich Schuchardt16eff692023-05-13 10:18:24 +02001038 return buf;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001039}
1040
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001041struct efi_device_path *efi_dp_from_uart(void)
1042{
1043 void *buf, *pos;
1044 struct efi_device_path_uart *uart;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +02001045 size_t dpsize = dp_size(dm_root()) + sizeof(*uart) + sizeof(END);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001046
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001047 buf = efi_alloc(dpsize);
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001048 if (!buf)
1049 return NULL;
Heinrich Schuchardt39568fd2023-07-19 06:43:08 +02001050 pos = dp_fill(buf, dm_root());
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001051 uart = pos;
1052 uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
1053 uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART;
1054 uart->dp.length = sizeof(*uart);
1055 pos += sizeof(*uart);
1056 memcpy(pos, &END, sizeof(END));
1057
1058 return buf;
1059}
1060
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001061struct efi_device_path __maybe_unused *efi_dp_from_eth(void)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001062{
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001063 void *buf, *start;
1064 unsigned dpsize = 0;
1065
1066 assert(eth_get_dev());
1067
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001068 dpsize += dp_size(eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001069
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001070 start = buf = efi_alloc(dpsize + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001071 if (!buf)
1072 return NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001073
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001074 buf = dp_fill(buf, eth_get_dev());
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001075
1076 *((struct efi_device_path *)buf) = END;
1077
1078 return start;
1079}
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001080
Rob Clark18ceba72017-10-10 08:23:06 -04001081/* Construct a device-path for memory-mapped image */
1082struct efi_device_path *efi_dp_from_mem(uint32_t memory_type,
1083 uint64_t start_address,
1084 uint64_t end_address)
1085{
1086 struct efi_device_path_memory *mdp;
1087 void *buf, *start;
1088
Heinrich Schuchardt45640252023-03-19 09:20:22 +01001089 start = buf = efi_alloc(sizeof(*mdp) + sizeof(END));
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001090 if (!buf)
1091 return NULL;
Rob Clark18ceba72017-10-10 08:23:06 -04001092
1093 mdp = buf;
1094 mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE;
1095 mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY;
1096 mdp->dp.length = sizeof(*mdp);
1097 mdp->memory_type = memory_type;
1098 mdp->start_address = start_address;
1099 mdp->end_address = end_address;
1100 buf = &mdp[1];
1101
1102 *((struct efi_device_path *)buf) = END;
1103
1104 return start;
1105}
1106
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001107/**
1108 * efi_dp_split_file_path() - split of relative file path from device path
1109 *
1110 * Given a device path indicating a file on a device, separate the device
1111 * path in two: the device path of the actual device and the file path
1112 * relative to this device.
1113 *
1114 * @full_path: device path including device and file path
1115 * @device_path: path of the device
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001116 * @file_path: relative path of the file or NULL if there is none
Heinrich Schuchardt0d36adc2019-02-04 12:49:43 +01001117 * Return: status code
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001118 */
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001119efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path,
1120 struct efi_device_path **device_path,
1121 struct efi_device_path **file_path)
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001122{
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001123 struct efi_device_path *p, *dp, *fp = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001124
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001125 *device_path = NULL;
1126 *file_path = NULL;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001127 dp = efi_dp_dup(full_path);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001128 if (!dp)
1129 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001130 p = dp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001131 while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) {
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001132 p = efi_dp_next(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001133 if (!p)
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001134 goto out;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001135 }
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001136 fp = efi_dp_dup(p);
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001137 if (!fp)
1138 return EFI_OUT_OF_RESOURCES;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001139 p->type = DEVICE_PATH_TYPE_END;
1140 p->sub_type = DEVICE_PATH_SUB_TYPE_END;
1141 p->length = sizeof(*p);
1142
AKASHI Takahiro9c6531f2019-04-16 17:39:26 +02001143out:
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001144 *device_path = dp;
1145 *file_path = fp;
Heinrich Schuchardt468bf972018-01-19 20:24:37 +01001146 return EFI_SUCCESS;
Rob Clarkf90cb9f2017-09-13 18:05:28 -04001147}
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001148
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001149/**
1150 * efi_dp_from_name() - convert U-Boot device and file path to device path
1151 *
1152 * @dev: U-Boot device, e.g. 'mmc'
1153 * @devnr: U-Boot device number, e.g. 1 for 'mmc:1'
1154 * @path: file path relative to U-Boot device, may be NULL
1155 * @device: pointer to receive device path of the device
1156 * @file: pointer to receive device path for the file
1157 * Return: status code
1158 */
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001159efi_status_t efi_dp_from_name(const char *dev, const char *devnr,
1160 const char *path,
1161 struct efi_device_path **device,
1162 struct efi_device_path **file)
1163{
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001164 struct blk_desc *desc = NULL;
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001165 struct efi_device_path *dp;
Simon Glassc1c4a8f2020-05-10 11:39:57 -06001166 struct disk_partition fs_partition;
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001167 size_t image_size;
1168 void *image_addr;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001169 int part = 0;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001170
AKASHI Takahiro39844412018-11-05 18:06:40 +09001171 if (path && !file)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001172 return EFI_INVALID_PARAMETER;
1173
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001174 if (!strcmp(dev, "Mem") || !strcmp(dev, "hostfs")) {
Heinrich Schuchardtaecb9e22023-05-12 20:18:10 +02001175 /* loadm command and semihosting */
Rui Miguel Silva433f15a2022-05-11 10:55:40 +01001176 efi_get_image_parameters(&image_addr, &image_size);
1177
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001178 dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE,
1179 (uintptr_t)image_addr, image_size);
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001180 } else if (IS_ENABLED(CONFIG_NETDEVICES) && !strcmp(dev, "Net")) {
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001181 dp = efi_dp_from_eth();
Heinrich Schuchardt87b8a712023-05-13 09:55:26 +02001182 } else if (!strcmp(dev, "Uart")) {
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001183 dp = efi_dp_from_uart();
Heinrich Schuchardt77c0da82021-03-19 02:49:54 +01001184 } else {
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001185 part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition,
1186 1);
Patrick Delaunayba7a9502019-04-10 11:02:58 +02001187 if (part < 0 || !desc)
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001188 return EFI_INVALID_PARAMETER;
1189
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001190 dp = efi_dp_from_part(desc, part);
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001191 }
Heinrich Schuchardt7e269a32023-05-13 10:30:43 +02001192 if (device)
1193 *device = dp;
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001194
1195 if (!path)
1196 return EFI_SUCCESS;
1197
Heinrich Schuchardtff08eac2023-05-13 10:36:21 +02001198 *file = efi_dp_from_file(dp, path);
Heinrich Schuchardt6e0a1b42019-10-30 20:13:24 +01001199 if (!*file)
Heinrich Schuchardtbf0b3a12023-05-13 10:22:21 +02001200 return EFI_OUT_OF_RESOURCES;
AKASHI Takahirof1dbbae2019-10-09 16:19:52 +09001201
AKASHI Takahiro035fb012018-10-17 16:32:03 +09001202 return EFI_SUCCESS;
1203}
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001204
1205/**
1206 * efi_dp_check_length() - check length of a device path
1207 *
1208 * @dp: pointer to device path
1209 * @maxlen: maximum length of the device path
1210 * Return:
1211 * * length of the device path if it is less or equal @maxlen
1212 * * -1 if the device path is longer then @maxlen
1213 * * -1 if a device path node has a length of less than 4
1214 * * -EINVAL if maxlen exceeds SSIZE_MAX
1215 */
1216ssize_t efi_dp_check_length(const struct efi_device_path *dp,
1217 const size_t maxlen)
1218{
1219 ssize_t ret = 0;
1220 u16 len;
1221
1222 if (maxlen > SSIZE_MAX)
1223 return -EINVAL;
1224 for (;;) {
1225 len = dp->length;
1226 if (len < 4)
1227 return -1;
1228 ret += len;
1229 if (ret > maxlen)
1230 return -1;
1231 if (dp->type == DEVICE_PATH_TYPE_END &&
1232 dp->sub_type == DEVICE_PATH_SUB_TYPE_END)
1233 return ret;
1234 dp = (const struct efi_device_path *)((const u8 *)dp + len);
1235 }
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001236}
1237
1238/**
1239 * efi_dp_from_lo() - Get the instance of a VenMedia node in a
1240 * multi-instance device path that matches
1241 * a specific GUID. This kind of device paths
1242 * is found in Boot#### options describing an
1243 * initrd location
1244 *
1245 * @lo: EFI_LOAD_OPTION containing a valid device path
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001246 * @guid: guid to search for
1247 *
1248 * Return:
1249 * device path including the VenMedia node or NULL.
1250 * Caller must free the returned value.
1251 */
1252struct
1253efi_device_path *efi_dp_from_lo(struct efi_load_option *lo,
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001254 const efi_guid_t *guid)
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001255{
1256 struct efi_device_path *fp = lo->file_path;
1257 struct efi_device_path_vendor *vendor;
1258 int lo_len = lo->file_path_length;
1259
1260 for (; lo_len >= sizeof(struct efi_device_path);
1261 lo_len -= fp->length, fp = (void *)fp + fp->length) {
1262 if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
1263 break;
1264 if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE ||
1265 fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH)
1266 continue;
1267
1268 vendor = (struct efi_device_path_vendor *)fp;
Heinrich Schuchardt9979cff2021-10-15 01:31:02 +02001269 if (!guidcmp(&vendor->guid, guid))
Heinrich Schuchardt35dd3222021-10-15 02:59:15 +02001270 return efi_dp_dup(efi_dp_next(fp));
Ilias Apalodimas483d28e2021-03-17 21:54:58 +02001271 }
1272 log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label);
1273
1274 return NULL;
Heinrich Schuchardt22c8e082020-08-23 10:49:46 +02001275}
Masahisa Kojima6460c3e2021-10-26 17:27:25 +09001276
1277/**
1278 * search_gpt_dp_node() - search gpt device path node
1279 *
1280 * @device_path: device path
1281 *
1282 * Return: pointer to the gpt device path node
1283 */
1284struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path)
1285{
1286 struct efi_device_path *dp = device_path;
1287
1288 while (dp) {
1289 if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE &&
1290 dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) {
1291 struct efi_device_path_hard_drive_path *hd_dp =
1292 (struct efi_device_path_hard_drive_path *)dp;
1293
1294 if (hd_dp->partmap_type == PART_FORMAT_GPT &&
1295 hd_dp->signature_type == SIG_TYPE_GUID)
1296 return dp;
1297 }
1298 dp = efi_dp_next(dp);
1299 }
1300
1301 return NULL;
1302}