Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 2 | /* |
| 3 | * EFI device path from u-boot device-model mapping |
| 4 | * |
| 5 | * (C) Copyright 2017 Rob Clark |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Alfonso Sánchez-Beato | f007a37 | 2021-07-15 15:31:42 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 10 | #include <blk.h> |
| 11 | #include <dm.h> |
Heinrich Schuchardt | 39568fd | 2023-07-19 06:43:08 +0200 | [diff] [blame] | 12 | #include <dm/root.h> |
Tom Rini | efa387d | 2025-05-21 16:51:19 -0600 | [diff] [blame^] | 13 | #include <ide.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 15 | #include <net.h> |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 16 | #include <usb.h> |
| 17 | #include <mmc.h> |
Patrick Wildt | a3ca37e | 2019-10-03 16:24:17 +0200 | [diff] [blame] | 18 | #include <nvme.h> |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 19 | #include <efi_loader.h> |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 20 | #include <part.h> |
Caleb Connolly | 29cab7c | 2024-08-30 13:34:37 +0100 | [diff] [blame] | 21 | #include <u-boot/uuid.h> |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 22 | #include <asm-generic/unaligned.h> |
AKASHI Takahiro | f1dbbae | 2019-10-09 16:19:52 +0900 | [diff] [blame] | 23 | #include <linux/compat.h> /* U16_MAX */ |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 24 | |
| 25 | /* template END node: */ |
Masahisa Kojima | 97bd9da | 2022-06-19 13:55:59 +0900 | [diff] [blame] | 26 | const struct efi_device_path END = { |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 27 | .type = DEVICE_PATH_TYPE_END, |
| 28 | .sub_type = DEVICE_PATH_SUB_TYPE_END, |
| 29 | .length = sizeof(END), |
| 30 | }; |
| 31 | |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 32 | #if defined(CONFIG_MMC) |
Heinrich Schuchardt | 7d569db | 2017-12-11 12:56:39 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Determine if an MMC device is an SD card. |
| 35 | * |
| 36 | * @desc block device descriptor |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 37 | * Return: true if the device is an SD card |
Heinrich Schuchardt | 7d569db | 2017-12-11 12:56:39 +0100 | [diff] [blame] | 38 | */ |
| 39 | static bool is_sd(struct blk_desc *desc) |
| 40 | { |
| 41 | struct mmc *mmc = find_mmc_device(desc->devnum); |
| 42 | |
| 43 | if (!mmc) |
| 44 | return false; |
| 45 | |
| 46 | return IS_SD(mmc) != 0U; |
| 47 | } |
| 48 | #endif |
| 49 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 50 | /* |
| 51 | * Iterate to next block in device-path, terminating (returning NULL) |
| 52 | * at /End* node. |
| 53 | */ |
| 54 | struct efi_device_path *efi_dp_next(const struct efi_device_path *dp) |
| 55 | { |
| 56 | if (dp == NULL) |
| 57 | return NULL; |
| 58 | if (dp->type == DEVICE_PATH_TYPE_END) |
| 59 | return NULL; |
| 60 | dp = ((void *)dp) + dp->length; |
| 61 | if (dp->type == DEVICE_PATH_TYPE_END) |
| 62 | return NULL; |
| 63 | return (struct efi_device_path *)dp; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Compare two device-paths, stopping when the shorter of the two hits |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 68 | * an End* node. This is useful to, for example, compare a device-path |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 69 | * representing a device with one representing a file on the device, or |
| 70 | * a device with a parent device. |
| 71 | */ |
Heinrich Schuchardt | 753e248 | 2017-10-26 19:25:48 +0200 | [diff] [blame] | 72 | int efi_dp_match(const struct efi_device_path *a, |
| 73 | const struct efi_device_path *b) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 74 | { |
| 75 | while (1) { |
| 76 | int ret; |
| 77 | |
| 78 | ret = memcmp(&a->length, &b->length, sizeof(a->length)); |
| 79 | if (ret) |
| 80 | return ret; |
| 81 | |
| 82 | ret = memcmp(a, b, a->length); |
| 83 | if (ret) |
| 84 | return ret; |
| 85 | |
| 86 | a = efi_dp_next(a); |
| 87 | b = efi_dp_next(b); |
| 88 | |
| 89 | if (!a || !b) |
| 90 | return 0; |
| 91 | } |
| 92 | } |
| 93 | |
Heinrich Schuchardt | 24f0e6a | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 94 | /** |
| 95 | * efi_dp_shorten() - shorten device-path |
| 96 | * |
Heinrich Schuchardt | a7ffd90 | 2023-03-26 12:22:40 +0200 | [diff] [blame] | 97 | * When creating a short boot option we want to use a device-path that is |
| 98 | * independent of the location where the block device is plugged in. |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 99 | * |
Heinrich Schuchardt | a7ffd90 | 2023-03-26 12:22:40 +0200 | [diff] [blame] | 100 | * UsbWwi() nodes contain a serial number, hard drive paths a partition |
| 101 | * UUID. Both should be unique. |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 102 | * |
Heinrich Schuchardt | a7ffd90 | 2023-03-26 12:22:40 +0200 | [diff] [blame] | 103 | * See UEFI spec, section 3.1.2 for "short-form device path". |
Heinrich Schuchardt | 24f0e6a | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 104 | * |
Heinrich Schuchardt | db17dbc | 2022-03-21 08:26:48 +0100 | [diff] [blame] | 105 | * @dp: original device-path |
Heinrich Schuchardt | 2e67085 | 2024-04-26 12:09:38 +0200 | [diff] [blame] | 106 | * Return: shortened device-path or NULL |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 107 | */ |
Heinrich Schuchardt | 24f0e6a | 2022-02-26 12:10:10 +0100 | [diff] [blame] | 108 | struct efi_device_path *efi_dp_shorten(struct efi_device_path *dp) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 109 | { |
| 110 | while (dp) { |
Heinrich Schuchardt | a7ffd90 | 2023-03-26 12:22:40 +0200 | [diff] [blame] | 111 | if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_WWI) || |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 112 | EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) || |
| 113 | EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH)) |
| 114 | return dp; |
| 115 | |
| 116 | dp = efi_dp_next(dp); |
| 117 | } |
| 118 | |
| 119 | return dp; |
| 120 | } |
| 121 | |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 122 | /** |
Heinrich Schuchardt | 0a04a41 | 2022-03-19 06:35:43 +0100 | [diff] [blame] | 123 | * find_handle() - find handle by device path and installed protocol |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 124 | * |
| 125 | * If @rem is provided, the handle with the longest partial match is returned. |
| 126 | * |
| 127 | * @dp: device path to search |
Heinrich Schuchardt | 0a04a41 | 2022-03-19 06:35:43 +0100 | [diff] [blame] | 128 | * @guid: GUID of protocol that must be installed on path or NULL |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 129 | * @short_path: use short form device path for matching |
| 130 | * @rem: pointer to receive remaining device path |
| 131 | * Return: matching handle |
| 132 | */ |
Heinrich Schuchardt | 0a04a41 | 2022-03-19 06:35:43 +0100 | [diff] [blame] | 133 | static efi_handle_t find_handle(struct efi_device_path *dp, |
| 134 | const efi_guid_t *guid, bool short_path, |
| 135 | struct efi_device_path **rem) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 136 | { |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 137 | efi_handle_t handle, best_handle = NULL; |
| 138 | efi_uintn_t len, best_len = 0; |
| 139 | |
| 140 | len = efi_dp_instance_size(dp); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 141 | |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 142 | list_for_each_entry(handle, &efi_obj_list, link) { |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 143 | struct efi_handler *handler; |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 144 | struct efi_device_path *dp_current; |
| 145 | efi_uintn_t len_current; |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 146 | efi_status_t ret; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 147 | |
Heinrich Schuchardt | 0a04a41 | 2022-03-19 06:35:43 +0100 | [diff] [blame] | 148 | if (guid) { |
| 149 | ret = efi_search_protocol(handle, guid, &handler); |
| 150 | if (ret != EFI_SUCCESS) |
| 151 | continue; |
| 152 | } |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 153 | ret = efi_search_protocol(handle, &efi_guid_device_path, |
| 154 | &handler); |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 155 | if (ret != EFI_SUCCESS) |
| 156 | continue; |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 157 | dp_current = handler->protocol_interface; |
| 158 | if (short_path) { |
| 159 | dp_current = efi_dp_shorten(dp_current); |
| 160 | if (!dp_current) |
| 161 | continue; |
| 162 | } |
| 163 | len_current = efi_dp_instance_size(dp_current); |
| 164 | if (rem) { |
Heinrich Schuchardt | 0a04a41 | 2022-03-19 06:35:43 +0100 | [diff] [blame] | 165 | if (len_current > len) |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 166 | continue; |
| 167 | } else { |
| 168 | if (len_current != len) |
| 169 | continue; |
| 170 | } |
Heinrich Schuchardt | 0a04a41 | 2022-03-19 06:35:43 +0100 | [diff] [blame] | 171 | if (memcmp(dp_current, dp, len_current)) |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 172 | continue; |
| 173 | if (!rem) |
| 174 | return handle; |
| 175 | if (len_current > best_len) { |
| 176 | best_len = len_current; |
| 177 | best_handle = handle; |
| 178 | *rem = (void*)((u8 *)dp + len_current); |
| 179 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 180 | } |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 181 | return best_handle; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 182 | } |
| 183 | |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 184 | /** |
| 185 | * efi_dp_find_obj() - find handle by device path |
| 186 | * |
| 187 | * If @rem is provided, the handle with the longest partial match is returned. |
| 188 | * |
| 189 | * @dp: device path to search |
Heinrich Schuchardt | 0a04a41 | 2022-03-19 06:35:43 +0100 | [diff] [blame] | 190 | * @guid: GUID of protocol that must be installed on path or NULL |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 191 | * @rem: pointer to receive remaining device path |
| 192 | * Return: matching handle |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 193 | */ |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 194 | efi_handle_t efi_dp_find_obj(struct efi_device_path *dp, |
Heinrich Schuchardt | 0a04a41 | 2022-03-19 06:35:43 +0100 | [diff] [blame] | 195 | const efi_guid_t *guid, |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 196 | struct efi_device_path **rem) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 197 | { |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 198 | efi_handle_t handle; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 199 | |
Heinrich Schuchardt | 0a04a41 | 2022-03-19 06:35:43 +0100 | [diff] [blame] | 200 | handle = find_handle(dp, guid, false, rem); |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 201 | if (!handle) |
| 202 | /* Match short form device path */ |
Heinrich Schuchardt | 0a04a41 | 2022-03-19 06:35:43 +0100 | [diff] [blame] | 203 | handle = find_handle(dp, guid, true, rem); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 204 | |
Heinrich Schuchardt | ad6d5a4 | 2022-03-04 08:20:00 +0100 | [diff] [blame] | 205 | return handle; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 206 | } |
| 207 | |
Heinrich Schuchardt | 0976f8b | 2018-01-19 20:24:49 +0100 | [diff] [blame] | 208 | /* |
| 209 | * Determine the last device path node that is not the end node. |
| 210 | * |
| 211 | * @dp device path |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 212 | * Return: last node before the end node if it exists |
Heinrich Schuchardt | 0976f8b | 2018-01-19 20:24:49 +0100 | [diff] [blame] | 213 | * otherwise NULL |
| 214 | */ |
| 215 | const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp) |
| 216 | { |
| 217 | struct efi_device_path *ret; |
| 218 | |
| 219 | if (!dp || dp->type == DEVICE_PATH_TYPE_END) |
| 220 | return NULL; |
| 221 | while (dp) { |
| 222 | ret = (struct efi_device_path *)dp; |
| 223 | dp = efi_dp_next(dp); |
| 224 | } |
| 225 | return ret; |
| 226 | } |
| 227 | |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 228 | /* get size of the first device path instance excluding end node */ |
| 229 | efi_uintn_t efi_dp_instance_size(const struct efi_device_path *dp) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 230 | { |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 231 | efi_uintn_t sz = 0; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 232 | |
Heinrich Schuchardt | 01d48ed | 2018-04-16 07:59:07 +0200 | [diff] [blame] | 233 | if (!dp || dp->type == DEVICE_PATH_TYPE_END) |
| 234 | return 0; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 235 | while (dp) { |
| 236 | sz += dp->length; |
| 237 | dp = efi_dp_next(dp); |
| 238 | } |
| 239 | |
| 240 | return sz; |
| 241 | } |
| 242 | |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 243 | /* get size of multi-instance device path excluding end node */ |
| 244 | efi_uintn_t efi_dp_size(const struct efi_device_path *dp) |
| 245 | { |
| 246 | const struct efi_device_path *p = dp; |
| 247 | |
| 248 | if (!p) |
| 249 | return 0; |
| 250 | while (p->type != DEVICE_PATH_TYPE_END || |
| 251 | p->sub_type != DEVICE_PATH_SUB_TYPE_END) |
| 252 | p = (void *)p + p->length; |
| 253 | |
| 254 | return (void *)p - (void *)dp; |
| 255 | } |
| 256 | |
| 257 | /* copy multi-instance device path */ |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 258 | struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp) |
| 259 | { |
| 260 | struct efi_device_path *ndp; |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 261 | size_t sz = efi_dp_size(dp) + sizeof(END); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 262 | |
| 263 | if (!dp) |
| 264 | return NULL; |
| 265 | |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 266 | ndp = efi_alloc(sz); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 267 | if (!ndp) |
| 268 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 269 | memcpy(ndp, dp, sz); |
| 270 | |
| 271 | return ndp; |
| 272 | } |
| 273 | |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 274 | /** |
Ilias Apalodimas | 5dcefa7 | 2024-01-08 10:55:33 +0200 | [diff] [blame] | 275 | * efi_dp_concat() - Concatenate two device paths and add and terminate them |
| 276 | * with an end node. |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 277 | * |
Ilias Apalodimas | 5dcefa7 | 2024-01-08 10:55:33 +0200 | [diff] [blame] | 278 | * @dp1: First device path |
| 279 | * @dp2: Second device path |
Heinrich Schuchardt | f8de009 | 2024-05-24 14:54:26 +0200 | [diff] [blame] | 280 | * @split_end_node: |
| 281 | * * 0 to concatenate |
| 282 | * * 1 to concatenate with end node added as separator |
| 283 | * * size of dp1 excluding last end node to concatenate with end node as |
| 284 | * separator in case dp1 contains an end node |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 285 | * |
| 286 | * Return: |
| 287 | * concatenated device path or NULL. Caller must free the returned value |
| 288 | */ |
Ilias Apalodimas | 5dcefa7 | 2024-01-08 10:55:33 +0200 | [diff] [blame] | 289 | struct |
| 290 | efi_device_path *efi_dp_concat(const struct efi_device_path *dp1, |
| 291 | const struct efi_device_path *dp2, |
Heinrich Schuchardt | f8de009 | 2024-05-24 14:54:26 +0200 | [diff] [blame] | 292 | size_t split_end_node) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 293 | { |
| 294 | struct efi_device_path *ret; |
Ilias Apalodimas | 5dcefa7 | 2024-01-08 10:55:33 +0200 | [diff] [blame] | 295 | size_t end_size; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 296 | |
Heinrich Schuchardt | 60b5ab2 | 2018-04-16 07:59:06 +0200 | [diff] [blame] | 297 | if (!dp1 && !dp2) { |
| 298 | /* return an end node */ |
| 299 | ret = efi_dp_dup(&END); |
| 300 | } else if (!dp1) { |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 301 | ret = efi_dp_dup(dp2); |
| 302 | } else if (!dp2) { |
| 303 | ret = efi_dp_dup(dp1); |
| 304 | } else { |
| 305 | /* both dp1 and dp2 are non-null */ |
Heinrich Schuchardt | f8de009 | 2024-05-24 14:54:26 +0200 | [diff] [blame] | 306 | size_t sz1; |
| 307 | size_t sz2 = efi_dp_size(dp2); |
Ilias Apalodimas | 5dcefa7 | 2024-01-08 10:55:33 +0200 | [diff] [blame] | 308 | void *p; |
| 309 | |
Heinrich Schuchardt | f8de009 | 2024-05-24 14:54:26 +0200 | [diff] [blame] | 310 | if (split_end_node < sizeof(struct efi_device_path)) |
| 311 | sz1 = efi_dp_size(dp1); |
| 312 | else |
| 313 | sz1 = split_end_node; |
| 314 | |
Ilias Apalodimas | 5dcefa7 | 2024-01-08 10:55:33 +0200 | [diff] [blame] | 315 | if (split_end_node) |
| 316 | end_size = 2 * sizeof(END); |
| 317 | else |
| 318 | end_size = sizeof(END); |
| 319 | p = efi_alloc(sz1 + sz2 + end_size); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 320 | if (!p) |
| 321 | return NULL; |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 322 | ret = p; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 323 | memcpy(p, dp1, sz1); |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 324 | p += sz1; |
| 325 | |
Ilias Apalodimas | 5dcefa7 | 2024-01-08 10:55:33 +0200 | [diff] [blame] | 326 | if (split_end_node) { |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 327 | memcpy(p, &END, sizeof(END)); |
| 328 | p += sizeof(END); |
| 329 | } |
| 330 | |
Heinrich Schuchardt | 60b5ab2 | 2018-04-16 07:59:06 +0200 | [diff] [blame] | 331 | /* the end node of the second device path has to be retained */ |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 332 | memcpy(p, dp2, sz2); |
| 333 | p += sz2; |
| 334 | memcpy(p, &END, sizeof(END)); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp, |
| 341 | const struct efi_device_path *node) |
| 342 | { |
| 343 | struct efi_device_path *ret; |
| 344 | |
| 345 | if (!node && !dp) { |
| 346 | ret = efi_dp_dup(&END); |
| 347 | } else if (!node) { |
| 348 | ret = efi_dp_dup(dp); |
| 349 | } else if (!dp) { |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 350 | size_t sz = node->length; |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 351 | void *p = efi_alloc(sz + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 352 | if (!p) |
| 353 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 354 | memcpy(p, node, sz); |
| 355 | memcpy(p + sz, &END, sizeof(END)); |
| 356 | ret = p; |
| 357 | } else { |
| 358 | /* both dp and node are non-null */ |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 359 | size_t sz = efi_dp_size(dp); |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 360 | void *p = efi_alloc(sz + node->length + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 361 | if (!p) |
| 362 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 363 | memcpy(p, dp, sz); |
| 364 | memcpy(p + sz, node, node->length); |
| 365 | memcpy(p + sz + node->length, &END, sizeof(END)); |
| 366 | ret = p; |
| 367 | } |
| 368 | |
| 369 | return ret; |
| 370 | } |
| 371 | |
Heinrich Schuchardt | b41c8e2 | 2018-04-16 07:59:05 +0200 | [diff] [blame] | 372 | struct efi_device_path *efi_dp_create_device_node(const u8 type, |
| 373 | const u8 sub_type, |
| 374 | const u16 length) |
| 375 | { |
| 376 | struct efi_device_path *ret; |
| 377 | |
Heinrich Schuchardt | c96c594 | 2019-04-23 00:51:01 +0200 | [diff] [blame] | 378 | if (length < sizeof(struct efi_device_path)) |
| 379 | return NULL; |
| 380 | |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 381 | ret = efi_alloc(length); |
Heinrich Schuchardt | b41c8e2 | 2018-04-16 07:59:05 +0200 | [diff] [blame] | 382 | if (!ret) |
| 383 | return ret; |
| 384 | ret->type = type; |
| 385 | ret->sub_type = sub_type; |
| 386 | ret->length = length; |
| 387 | return ret; |
| 388 | } |
| 389 | |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 390 | struct efi_device_path *efi_dp_append_instance( |
| 391 | const struct efi_device_path *dp, |
| 392 | const struct efi_device_path *dpi) |
| 393 | { |
| 394 | size_t sz, szi; |
| 395 | struct efi_device_path *p, *ret; |
| 396 | |
| 397 | if (!dpi) |
| 398 | return NULL; |
| 399 | if (!dp) |
| 400 | return efi_dp_dup(dpi); |
| 401 | sz = efi_dp_size(dp); |
| 402 | szi = efi_dp_instance_size(dpi); |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 403 | p = efi_alloc(sz + szi + 2 * sizeof(END)); |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 404 | if (!p) |
| 405 | return NULL; |
| 406 | ret = p; |
| 407 | memcpy(p, dp, sz + sizeof(END)); |
| 408 | p = (void *)p + sz; |
| 409 | p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END; |
| 410 | p = (void *)p + sizeof(END); |
| 411 | memcpy(p, dpi, szi); |
| 412 | p = (void *)p + szi; |
| 413 | memcpy(p, &END, sizeof(END)); |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp, |
| 418 | efi_uintn_t *size) |
| 419 | { |
| 420 | size_t sz; |
| 421 | struct efi_device_path *p; |
| 422 | |
| 423 | if (size) |
| 424 | *size = 0; |
| 425 | if (!dp || !*dp) |
| 426 | return NULL; |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 427 | sz = efi_dp_instance_size(*dp); |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 428 | p = efi_alloc(sz + sizeof(END)); |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 429 | if (!p) |
| 430 | return NULL; |
| 431 | memcpy(p, *dp, sz + sizeof(END)); |
| 432 | *dp = (void *)*dp + sz; |
| 433 | if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END) |
| 434 | *dp = (void *)*dp + sizeof(END); |
| 435 | else |
| 436 | *dp = NULL; |
| 437 | if (size) |
| 438 | *size = sz + sizeof(END); |
| 439 | return p; |
| 440 | } |
| 441 | |
| 442 | bool efi_dp_is_multi_instance(const struct efi_device_path *dp) |
| 443 | { |
| 444 | const struct efi_device_path *p = dp; |
| 445 | |
| 446 | if (!p) |
| 447 | return false; |
| 448 | while (p->type != DEVICE_PATH_TYPE_END) |
| 449 | p = (void *)p + p->length; |
| 450 | return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END; |
| 451 | } |
| 452 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 453 | /* size of device-path not including END node for device and all parents |
| 454 | * up to the root device. |
| 455 | */ |
Heinrich Schuchardt | c22d9e3 | 2019-11-10 02:16:33 +0100 | [diff] [blame] | 456 | __maybe_unused static unsigned int dp_size(struct udevice *dev) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 457 | { |
| 458 | if (!dev || !dev->driver) |
Heinrich Schuchardt | 39568fd | 2023-07-19 06:43:08 +0200 | [diff] [blame] | 459 | return sizeof(struct efi_device_path_udevice); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 460 | |
Simon Glass | 56ada7b | 2022-01-29 14:58:38 -0700 | [diff] [blame] | 461 | switch (device_get_uclass_id(dev)) { |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 462 | case UCLASS_ROOT: |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 463 | /* stop traversing parents at this point: */ |
Heinrich Schuchardt | 39568fd | 2023-07-19 06:43:08 +0200 | [diff] [blame] | 464 | return sizeof(struct efi_device_path_udevice); |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 465 | case UCLASS_ETH: |
| 466 | return dp_size(dev->parent) + |
| 467 | sizeof(struct efi_device_path_mac_addr); |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 468 | case UCLASS_BLK: |
| 469 | switch (dev->parent->uclass->uc_drv->id) { |
| 470 | #ifdef CONFIG_IDE |
| 471 | case UCLASS_IDE: |
| 472 | return dp_size(dev->parent) + |
| 473 | sizeof(struct efi_device_path_atapi); |
| 474 | #endif |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 475 | #if defined(CONFIG_SCSI) |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 476 | case UCLASS_SCSI: |
| 477 | return dp_size(dev->parent) + |
| 478 | sizeof(struct efi_device_path_scsi); |
| 479 | #endif |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 480 | #if defined(CONFIG_MMC) |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 481 | case UCLASS_MMC: |
| 482 | return dp_size(dev->parent) + |
| 483 | sizeof(struct efi_device_path_sd_mmc_path); |
| 484 | #endif |
Heinrich Schuchardt | 7bdb602 | 2020-05-20 23:12:02 +0200 | [diff] [blame] | 485 | #if defined(CONFIG_AHCI) || defined(CONFIG_SATA) |
| 486 | case UCLASS_AHCI: |
| 487 | return dp_size(dev->parent) + |
| 488 | sizeof(struct efi_device_path_sata); |
| 489 | #endif |
Patrick Wildt | a3ca37e | 2019-10-03 16:24:17 +0200 | [diff] [blame] | 490 | #if defined(CONFIG_NVME) |
| 491 | case UCLASS_NVME: |
| 492 | return dp_size(dev->parent) + |
| 493 | sizeof(struct efi_device_path_nvme); |
| 494 | #endif |
Heinrich Schuchardt | 25b18d6 | 2023-03-19 16:18:09 +0100 | [diff] [blame] | 495 | #ifdef CONFIG_USB |
| 496 | case UCLASS_MASS_STORAGE: |
| 497 | return dp_size(dev->parent) |
| 498 | + sizeof(struct efi_device_path_controller); |
| 499 | #endif |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 500 | default: |
Heinrich Schuchardt | 8433a5d | 2023-07-21 00:03:46 +0200 | [diff] [blame] | 501 | /* UCLASS_BLKMAP, UCLASS_HOST, UCLASS_VIRTIO */ |
| 502 | return dp_size(dev->parent) + |
| 503 | sizeof(struct efi_device_path_udevice); |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 504 | } |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 505 | #if defined(CONFIG_MMC) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 506 | case UCLASS_MMC: |
| 507 | return dp_size(dev->parent) + |
| 508 | sizeof(struct efi_device_path_sd_mmc_path); |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 509 | #endif |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 510 | case UCLASS_MASS_STORAGE: |
| 511 | case UCLASS_USB_HUB: |
| 512 | return dp_size(dev->parent) + |
Heinrich Schuchardt | 25b18d6 | 2023-03-19 16:18:09 +0100 | [diff] [blame] | 513 | sizeof(struct efi_device_path_usb); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 514 | default: |
Heinrich Schuchardt | 39568fd | 2023-07-19 06:43:08 +0200 | [diff] [blame] | 515 | return dp_size(dev->parent) + |
| 516 | sizeof(struct efi_device_path_udevice); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 520 | /* |
| 521 | * Recursively build a device path. |
| 522 | * |
| 523 | * @buf pointer to the end of the device path |
| 524 | * @dev device |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 525 | * Return: pointer to the end of the device path |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 526 | */ |
Heinrich Schuchardt | c22d9e3 | 2019-11-10 02:16:33 +0100 | [diff] [blame] | 527 | __maybe_unused static void *dp_fill(void *buf, struct udevice *dev) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 528 | { |
Heinrich Schuchardt | 3d34763 | 2023-07-21 08:34:18 +0200 | [diff] [blame] | 529 | enum uclass_id uclass_id; |
| 530 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 531 | if (!dev || !dev->driver) |
| 532 | return buf; |
| 533 | |
Heinrich Schuchardt | 3d34763 | 2023-07-21 08:34:18 +0200 | [diff] [blame] | 534 | uclass_id = device_get_uclass_id(dev); |
| 535 | if (uclass_id != UCLASS_ROOT) |
| 536 | buf = dp_fill(buf, dev->parent); |
| 537 | |
| 538 | switch (uclass_id) { |
Jan Kiszka | f138982 | 2022-10-14 18:10:06 +0200 | [diff] [blame] | 539 | #ifdef CONFIG_NETDEVICES |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 540 | case UCLASS_ETH: { |
Heinrich Schuchardt | 3d34763 | 2023-07-21 08:34:18 +0200 | [diff] [blame] | 541 | struct efi_device_path_mac_addr *dp = buf; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 542 | struct eth_pdata *pdata = dev_get_plat(dev); |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 543 | |
| 544 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 545 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR; |
| 546 | dp->dp.length = sizeof(*dp); |
| 547 | memset(&dp->mac, 0, sizeof(dp->mac)); |
| 548 | /* We only support IPv4 */ |
| 549 | memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN); |
| 550 | /* Ethernet */ |
| 551 | dp->if_type = 1; |
| 552 | return &dp[1]; |
| 553 | } |
| 554 | #endif |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 555 | case UCLASS_BLK: |
Heinrich Schuchardt | 8433a5d | 2023-07-21 00:03:46 +0200 | [diff] [blame] | 556 | switch (device_get_uclass_id(dev->parent)) { |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 557 | #ifdef CONFIG_IDE |
| 558 | case UCLASS_IDE: { |
Heinrich Schuchardt | 3d34763 | 2023-07-21 08:34:18 +0200 | [diff] [blame] | 559 | struct efi_device_path_atapi *dp = buf; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 560 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 561 | |
| 562 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 563 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI; |
| 564 | dp->dp.length = sizeof(*dp); |
| 565 | dp->logical_unit_number = desc->devnum; |
| 566 | dp->primary_secondary = IDE_BUS(desc->devnum); |
| 567 | dp->slave_master = desc->devnum % |
| 568 | (CONFIG_SYS_IDE_MAXDEVICE / |
| 569 | CONFIG_SYS_IDE_MAXBUS); |
| 570 | return &dp[1]; |
| 571 | } |
| 572 | #endif |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 573 | #if defined(CONFIG_SCSI) |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 574 | case UCLASS_SCSI: { |
Heinrich Schuchardt | 3d34763 | 2023-07-21 08:34:18 +0200 | [diff] [blame] | 575 | struct efi_device_path_scsi *dp = buf; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 576 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 577 | |
| 578 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 579 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI; |
| 580 | dp->dp.length = sizeof(*dp); |
| 581 | dp->logical_unit_number = desc->lun; |
| 582 | dp->target_id = desc->target; |
| 583 | return &dp[1]; |
| 584 | } |
| 585 | #endif |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 586 | #if defined(CONFIG_MMC) |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 587 | case UCLASS_MMC: { |
Heinrich Schuchardt | 3d34763 | 2023-07-21 08:34:18 +0200 | [diff] [blame] | 588 | struct efi_device_path_sd_mmc_path *sddp = buf; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 589 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 590 | |
| 591 | sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 592 | sddp->dp.sub_type = is_sd(desc) ? |
| 593 | DEVICE_PATH_SUB_TYPE_MSG_SD : |
| 594 | DEVICE_PATH_SUB_TYPE_MSG_MMC; |
| 595 | sddp->dp.length = sizeof(*sddp); |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 596 | sddp->slot_number = dev_seq(dev); |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 597 | return &sddp[1]; |
| 598 | } |
| 599 | #endif |
Heinrich Schuchardt | 7bdb602 | 2020-05-20 23:12:02 +0200 | [diff] [blame] | 600 | #if defined(CONFIG_AHCI) || defined(CONFIG_SATA) |
| 601 | case UCLASS_AHCI: { |
Heinrich Schuchardt | 3d34763 | 2023-07-21 08:34:18 +0200 | [diff] [blame] | 602 | struct efi_device_path_sata *dp = buf; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 603 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Heinrich Schuchardt | 7bdb602 | 2020-05-20 23:12:02 +0200 | [diff] [blame] | 604 | |
| 605 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 606 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA; |
| 607 | dp->dp.length = sizeof(*dp); |
| 608 | dp->hba_port = desc->devnum; |
| 609 | /* default 0xffff implies no port multiplier */ |
| 610 | dp->port_multiplier_port = 0xffff; |
| 611 | dp->logical_unit_number = desc->lun; |
| 612 | return &dp[1]; |
| 613 | } |
| 614 | #endif |
Patrick Wildt | a3ca37e | 2019-10-03 16:24:17 +0200 | [diff] [blame] | 615 | #if defined(CONFIG_NVME) |
| 616 | case UCLASS_NVME: { |
Heinrich Schuchardt | 3d34763 | 2023-07-21 08:34:18 +0200 | [diff] [blame] | 617 | struct efi_device_path_nvme *dp = buf; |
Patrick Wildt | a3ca37e | 2019-10-03 16:24:17 +0200 | [diff] [blame] | 618 | u32 ns_id; |
| 619 | |
| 620 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 621 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME; |
| 622 | dp->dp.length = sizeof(*dp); |
| 623 | nvme_get_namespace_id(dev, &ns_id, dp->eui64); |
| 624 | memcpy(&dp->ns_id, &ns_id, sizeof(ns_id)); |
| 625 | return &dp[1]; |
| 626 | } |
| 627 | #endif |
Heinrich Schuchardt | 25b18d6 | 2023-03-19 16:18:09 +0100 | [diff] [blame] | 628 | #if defined(CONFIG_USB) |
| 629 | case UCLASS_MASS_STORAGE: { |
Heinrich Schuchardt | 232c9db | 2023-04-01 07:21:55 +0200 | [diff] [blame] | 630 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Heinrich Schuchardt | 3d34763 | 2023-07-21 08:34:18 +0200 | [diff] [blame] | 631 | struct efi_device_path_controller *dp = buf; |
Heinrich Schuchardt | 25b18d6 | 2023-03-19 16:18:09 +0100 | [diff] [blame] | 632 | |
| 633 | dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE; |
| 634 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CONTROLLER; |
| 635 | dp->dp.length = sizeof(*dp); |
| 636 | dp->controller_number = desc->lun; |
| 637 | return &dp[1]; |
| 638 | } |
| 639 | #endif |
Heinrich Schuchardt | 8433a5d | 2023-07-21 00:03:46 +0200 | [diff] [blame] | 640 | default: { |
| 641 | /* UCLASS_BLKMAP, UCLASS_HOST, UCLASS_VIRTIO */ |
Heinrich Schuchardt | 3d34763 | 2023-07-21 08:34:18 +0200 | [diff] [blame] | 642 | struct efi_device_path_udevice *dp = buf; |
Heinrich Schuchardt | 8433a5d | 2023-07-21 00:03:46 +0200 | [diff] [blame] | 643 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
| 644 | |
Heinrich Schuchardt | 8433a5d | 2023-07-21 00:03:46 +0200 | [diff] [blame] | 645 | dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE; |
| 646 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR; |
| 647 | dp->dp.length = sizeof(*dp); |
| 648 | memcpy(&dp->guid, &efi_u_boot_guid, |
| 649 | sizeof(efi_guid_t)); |
| 650 | dp->uclass_id = (UCLASS_BLK & 0xffff) | |
| 651 | (desc->uclass_id << 16); |
| 652 | dp->dev_number = desc->devnum; |
| 653 | |
| 654 | return &dp[1]; |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 655 | } |
Heinrich Schuchardt | 8433a5d | 2023-07-21 00:03:46 +0200 | [diff] [blame] | 656 | } |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 657 | #if defined(CONFIG_MMC) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 658 | case UCLASS_MMC: { |
Heinrich Schuchardt | 3d34763 | 2023-07-21 08:34:18 +0200 | [diff] [blame] | 659 | struct efi_device_path_sd_mmc_path *sddp = buf; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 660 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 661 | struct blk_desc *desc = mmc_get_blk_desc(mmc); |
| 662 | |
| 663 | sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
Heinrich Schuchardt | 7d569db | 2017-12-11 12:56:39 +0100 | [diff] [blame] | 664 | sddp->dp.sub_type = is_sd(desc) ? |
| 665 | DEVICE_PATH_SUB_TYPE_MSG_SD : |
| 666 | DEVICE_PATH_SUB_TYPE_MSG_MMC; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 667 | sddp->dp.length = sizeof(*sddp); |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 668 | sddp->slot_number = dev_seq(dev); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 669 | |
| 670 | return &sddp[1]; |
| 671 | } |
| 672 | #endif |
| 673 | case UCLASS_MASS_STORAGE: |
| 674 | case UCLASS_USB_HUB: { |
Heinrich Schuchardt | 3d34763 | 2023-07-21 08:34:18 +0200 | [diff] [blame] | 675 | struct efi_device_path_usb *udp = buf; |
Heinrich Schuchardt | 25b18d6 | 2023-03-19 16:18:09 +0100 | [diff] [blame] | 676 | |
| 677 | switch (device_get_uclass_id(dev->parent)) { |
| 678 | case UCLASS_USB_HUB: { |
| 679 | struct usb_device *udev = dev_get_parent_priv(dev); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 680 | |
Heinrich Schuchardt | 25b18d6 | 2023-03-19 16:18:09 +0100 | [diff] [blame] | 681 | udp->parent_port_number = udev->portnr; |
| 682 | break; |
| 683 | } |
| 684 | default: |
| 685 | udp->parent_port_number = 0; |
| 686 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 687 | udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
Heinrich Schuchardt | 25b18d6 | 2023-03-19 16:18:09 +0100 | [diff] [blame] | 688 | udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 689 | udp->dp.length = sizeof(*udp); |
Heinrich Schuchardt | 25b18d6 | 2023-03-19 16:18:09 +0100 | [diff] [blame] | 690 | udp->usb_interface = 0; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 691 | |
| 692 | return &udp[1]; |
| 693 | } |
Heinrich Schuchardt | 39568fd | 2023-07-19 06:43:08 +0200 | [diff] [blame] | 694 | default: { |
Heinrich Schuchardt | 3d34763 | 2023-07-21 08:34:18 +0200 | [diff] [blame] | 695 | struct efi_device_path_udevice *vdp = buf; |
Heinrich Schuchardt | 39568fd | 2023-07-19 06:43:08 +0200 | [diff] [blame] | 696 | |
| 697 | vdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE; |
| 698 | vdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR; |
| 699 | vdp->dp.length = sizeof(*vdp); |
| 700 | memcpy(&vdp->guid, &efi_u_boot_guid, sizeof(efi_guid_t)); |
| 701 | vdp->uclass_id = uclass_id; |
| 702 | vdp->dev_number = dev->seq_; |
| 703 | |
| 704 | return &vdp[1]; |
| 705 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 706 | } |
| 707 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 708 | |
| 709 | static unsigned dp_part_size(struct blk_desc *desc, int part) |
| 710 | { |
| 711 | unsigned dpsize; |
Simon Glass | ec209a7 | 2022-01-29 14:58:39 -0700 | [diff] [blame] | 712 | struct udevice *dev = desc->bdev; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 713 | |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 714 | dpsize = dp_size(dev); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 715 | |
| 716 | if (part == 0) /* the actual disk, not a partition */ |
| 717 | return dpsize; |
| 718 | |
| 719 | if (desc->part_type == PART_TYPE_ISO) |
| 720 | dpsize += sizeof(struct efi_device_path_cdrom_path); |
| 721 | else |
| 722 | dpsize += sizeof(struct efi_device_path_hard_drive_path); |
| 723 | |
| 724 | return dpsize; |
| 725 | } |
| 726 | |
Heinrich Schuchardt | 8983ffd | 2017-12-11 12:56:42 +0100 | [diff] [blame] | 727 | /* |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 728 | * Create a device node for a block device partition. |
Heinrich Schuchardt | 8983ffd | 2017-12-11 12:56:42 +0100 | [diff] [blame] | 729 | * |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 730 | * @buf buffer to which the device path is written |
Heinrich Schuchardt | 8983ffd | 2017-12-11 12:56:42 +0100 | [diff] [blame] | 731 | * @desc block device descriptor |
| 732 | * @part partition number, 0 identifies a block device |
Heinrich Schuchardt | ee69ae1 | 2023-05-27 08:18:28 +0200 | [diff] [blame] | 733 | * |
| 734 | * Return: pointer to position after the node |
Heinrich Schuchardt | 8983ffd | 2017-12-11 12:56:42 +0100 | [diff] [blame] | 735 | */ |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 736 | static void *dp_part_node(void *buf, struct blk_desc *desc, int part) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 737 | { |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 738 | struct disk_partition info; |
Heinrich Schuchardt | ee69ae1 | 2023-05-27 08:18:28 +0200 | [diff] [blame] | 739 | int ret; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 740 | |
Heinrich Schuchardt | ee69ae1 | 2023-05-27 08:18:28 +0200 | [diff] [blame] | 741 | ret = part_get_info(desc, part, &info); |
| 742 | if (ret < 0) |
| 743 | return buf; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 744 | |
| 745 | if (desc->part_type == PART_TYPE_ISO) { |
| 746 | struct efi_device_path_cdrom_path *cddp = buf; |
| 747 | |
Heinrich Schuchardt | 2aae6db | 2017-12-11 12:56:40 +0100 | [diff] [blame] | 748 | cddp->boot_entry = part; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 749 | cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; |
| 750 | cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH; |
| 751 | cddp->dp.length = sizeof(*cddp); |
| 752 | cddp->partition_start = info.start; |
Heinrich Schuchardt | 28dfd1a | 2019-09-04 13:56:01 +0200 | [diff] [blame] | 753 | cddp->partition_size = info.size; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 754 | |
| 755 | buf = &cddp[1]; |
| 756 | } else { |
| 757 | struct efi_device_path_hard_drive_path *hddp = buf; |
| 758 | |
| 759 | hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; |
| 760 | hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH; |
| 761 | hddp->dp.length = sizeof(*hddp); |
Heinrich Schuchardt | 2aae6db | 2017-12-11 12:56:40 +0100 | [diff] [blame] | 762 | hddp->partition_number = part; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 763 | hddp->partition_start = info.start; |
| 764 | hddp->partition_end = info.size; |
| 765 | if (desc->part_type == PART_TYPE_EFI) |
| 766 | hddp->partmap_type = 2; |
| 767 | else |
| 768 | hddp->partmap_type = 1; |
Jonathan Gray | 84b4d70 | 2017-11-22 14:18:59 +1100 | [diff] [blame] | 769 | |
| 770 | switch (desc->sig_type) { |
| 771 | case SIG_TYPE_NONE: |
| 772 | default: |
| 773 | hddp->signature_type = 0; |
| 774 | memset(hddp->partition_signature, 0, |
| 775 | sizeof(hddp->partition_signature)); |
| 776 | break; |
| 777 | case SIG_TYPE_MBR: |
| 778 | hddp->signature_type = 1; |
| 779 | memset(hddp->partition_signature, 0, |
| 780 | sizeof(hddp->partition_signature)); |
| 781 | memcpy(hddp->partition_signature, &desc->mbr_sig, |
| 782 | sizeof(desc->mbr_sig)); |
| 783 | break; |
| 784 | case SIG_TYPE_GUID: |
| 785 | hddp->signature_type = 2; |
AKASHI Takahiro | ae18a67 | 2022-04-19 10:01:56 +0900 | [diff] [blame] | 786 | #if CONFIG_IS_ENABLED(PARTITION_UUIDS) |
| 787 | /* info.uuid exists only with PARTITION_UUIDS */ |
Alfonso Sánchez-Beato | f007a37 | 2021-07-15 15:31:42 +0200 | [diff] [blame] | 788 | if (uuid_str_to_bin(info.uuid, |
AKASHI Takahiro | ae18a67 | 2022-04-19 10:01:56 +0900 | [diff] [blame] | 789 | hddp->partition_signature, |
| 790 | UUID_STR_FORMAT_GUID)) { |
Alfonso Sánchez-Beato | f007a37 | 2021-07-15 15:31:42 +0200 | [diff] [blame] | 791 | log_warning( |
AKASHI Takahiro | ae18a67 | 2022-04-19 10:01:56 +0900 | [diff] [blame] | 792 | "Partition %d: invalid GUID %s\n", |
Alfonso Sánchez-Beato | f007a37 | 2021-07-15 15:31:42 +0200 | [diff] [blame] | 793 | part, info.uuid); |
AKASHI Takahiro | ae18a67 | 2022-04-19 10:01:56 +0900 | [diff] [blame] | 794 | } |
| 795 | #endif |
Jonathan Gray | 84b4d70 | 2017-11-22 14:18:59 +1100 | [diff] [blame] | 796 | break; |
| 797 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 798 | |
| 799 | buf = &hddp[1]; |
| 800 | } |
| 801 | |
| 802 | return buf; |
| 803 | } |
| 804 | |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 805 | /* |
| 806 | * Create a device path for a block device or one of its partitions. |
| 807 | * |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 808 | * @buf buffer to which the device path is written |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 809 | * @desc block device descriptor |
| 810 | * @part partition number, 0 identifies a block device |
| 811 | */ |
| 812 | static void *dp_part_fill(void *buf, struct blk_desc *desc, int part) |
| 813 | { |
Simon Glass | ec209a7 | 2022-01-29 14:58:39 -0700 | [diff] [blame] | 814 | struct udevice *dev = desc->bdev; |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 815 | |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 816 | buf = dp_fill(buf, dev); |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 817 | |
| 818 | if (part == 0) /* the actual disk, not a partition */ |
| 819 | return buf; |
| 820 | |
| 821 | return dp_part_node(buf, desc, part); |
| 822 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 823 | |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 824 | /* Construct a device-path from a partition on a block device: */ |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 825 | struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part) |
| 826 | { |
| 827 | void *buf, *start; |
| 828 | |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 829 | start = buf = efi_alloc(dp_part_size(desc, part) + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 830 | if (!buf) |
| 831 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 832 | |
| 833 | buf = dp_part_fill(buf, desc, part); |
| 834 | |
| 835 | *((struct efi_device_path *)buf) = END; |
| 836 | |
| 837 | return start; |
| 838 | } |
| 839 | |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 840 | /* |
| 841 | * Create a device node for a block device partition. |
| 842 | * |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 843 | * @buf buffer to which the device path is written |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 844 | * @desc block device descriptor |
| 845 | * @part partition number, 0 identifies a block device |
| 846 | */ |
| 847 | struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part) |
| 848 | { |
| 849 | efi_uintn_t dpsize; |
| 850 | void *buf; |
| 851 | |
| 852 | if (desc->part_type == PART_TYPE_ISO) |
| 853 | dpsize = sizeof(struct efi_device_path_cdrom_path); |
| 854 | else |
| 855 | dpsize = sizeof(struct efi_device_path_hard_drive_path); |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 856 | buf = efi_alloc(dpsize); |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 857 | |
Heinrich Schuchardt | e29fbb3 | 2022-10-06 13:36:02 +0200 | [diff] [blame] | 858 | if (buf) |
| 859 | dp_part_node(buf, desc, part); |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 860 | |
| 861 | return buf; |
| 862 | } |
| 863 | |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 864 | /** |
| 865 | * path_to_uefi() - convert UTF-8 path to an UEFI style path |
| 866 | * |
| 867 | * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path |
| 868 | * separators and UTF-16). |
| 869 | * |
| 870 | * @src: source buffer |
| 871 | * @uefi: target buffer, possibly unaligned |
| 872 | */ |
| 873 | static void path_to_uefi(void *uefi, const char *src) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 874 | { |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 875 | u16 *pos = uefi; |
| 876 | |
| 877 | /* |
| 878 | * efi_set_bootdev() calls this routine indirectly before the UEFI |
| 879 | * subsystem is initialized. So we cannot assume unaligned access to be |
| 880 | * enabled. |
| 881 | */ |
| 882 | allow_unaligned(); |
| 883 | |
| 884 | while (*src) { |
| 885 | s32 code = utf8_get(&src); |
| 886 | |
| 887 | if (code < 0) |
| 888 | code = '?'; |
| 889 | else if (code == '/') |
| 890 | code = '\\'; |
| 891 | utf16_put(code, &pos); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 892 | } |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 893 | *pos = 0; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 894 | } |
| 895 | |
Heinrich Schuchardt | f57eacb | 2022-06-11 05:22:08 +0000 | [diff] [blame] | 896 | /** |
Heinrich Schuchardt | ff08eac | 2023-05-13 10:36:21 +0200 | [diff] [blame] | 897 | * efi_dp_from_file() - append file path node to device path. |
Heinrich Schuchardt | f57eacb | 2022-06-11 05:22:08 +0000 | [diff] [blame] | 898 | * |
Heinrich Schuchardt | ff08eac | 2023-05-13 10:36:21 +0200 | [diff] [blame] | 899 | * @dp: device path or NULL |
| 900 | * @path: file path or NULL |
Heinrich Schuchardt | f57eacb | 2022-06-11 05:22:08 +0000 | [diff] [blame] | 901 | * Return: device path or NULL in case of an error |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 902 | */ |
Heinrich Schuchardt | ff08eac | 2023-05-13 10:36:21 +0200 | [diff] [blame] | 903 | struct efi_device_path *efi_dp_from_file(const struct efi_device_path *dp, |
| 904 | const char *path) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 905 | { |
| 906 | struct efi_device_path_file_path *fp; |
Heinrich Schuchardt | 16eff69 | 2023-05-13 10:18:24 +0200 | [diff] [blame] | 907 | void *buf, *pos; |
Heinrich Schuchardt | ff08eac | 2023-05-13 10:36:21 +0200 | [diff] [blame] | 908 | size_t dpsize, fpsize; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 909 | |
Heinrich Schuchardt | ff08eac | 2023-05-13 10:36:21 +0200 | [diff] [blame] | 910 | dpsize = efi_dp_size(dp); |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 911 | fpsize = sizeof(struct efi_device_path) + |
| 912 | 2 * (utf8_utf16_strlen(path) + 1); |
AKASHI Takahiro | f1dbbae | 2019-10-09 16:19:52 +0900 | [diff] [blame] | 913 | if (fpsize > U16_MAX) |
| 914 | return NULL; |
| 915 | |
Heinrich Schuchardt | ff08eac | 2023-05-13 10:36:21 +0200 | [diff] [blame] | 916 | buf = efi_alloc(dpsize + fpsize + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 917 | if (!buf) |
| 918 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 919 | |
Heinrich Schuchardt | ff08eac | 2023-05-13 10:36:21 +0200 | [diff] [blame] | 920 | memcpy(buf, dp, dpsize); |
| 921 | pos = buf + dpsize; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 922 | |
| 923 | /* add file-path: */ |
Heinrich Schuchardt | f57eacb | 2022-06-11 05:22:08 +0000 | [diff] [blame] | 924 | if (*path) { |
Heinrich Schuchardt | 16eff69 | 2023-05-13 10:18:24 +0200 | [diff] [blame] | 925 | fp = pos; |
Heinrich Schuchardt | f57eacb | 2022-06-11 05:22:08 +0000 | [diff] [blame] | 926 | fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; |
| 927 | fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH; |
| 928 | fp->dp.length = (u16)fpsize; |
| 929 | path_to_uefi(fp->str, path); |
Heinrich Schuchardt | 16eff69 | 2023-05-13 10:18:24 +0200 | [diff] [blame] | 930 | pos += fpsize; |
Heinrich Schuchardt | f57eacb | 2022-06-11 05:22:08 +0000 | [diff] [blame] | 931 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 932 | |
Heinrich Schuchardt | 16eff69 | 2023-05-13 10:18:24 +0200 | [diff] [blame] | 933 | memcpy(pos, &END, sizeof(END)); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 934 | |
Heinrich Schuchardt | 16eff69 | 2023-05-13 10:18:24 +0200 | [diff] [blame] | 935 | return buf; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 936 | } |
| 937 | |
Heinrich Schuchardt | 77c0da8 | 2021-03-19 02:49:54 +0100 | [diff] [blame] | 938 | struct efi_device_path *efi_dp_from_uart(void) |
| 939 | { |
| 940 | void *buf, *pos; |
| 941 | struct efi_device_path_uart *uart; |
Heinrich Schuchardt | 39568fd | 2023-07-19 06:43:08 +0200 | [diff] [blame] | 942 | size_t dpsize = dp_size(dm_root()) + sizeof(*uart) + sizeof(END); |
Heinrich Schuchardt | 77c0da8 | 2021-03-19 02:49:54 +0100 | [diff] [blame] | 943 | |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 944 | buf = efi_alloc(dpsize); |
Heinrich Schuchardt | 77c0da8 | 2021-03-19 02:49:54 +0100 | [diff] [blame] | 945 | if (!buf) |
| 946 | return NULL; |
Heinrich Schuchardt | 39568fd | 2023-07-19 06:43:08 +0200 | [diff] [blame] | 947 | pos = dp_fill(buf, dm_root()); |
Heinrich Schuchardt | 77c0da8 | 2021-03-19 02:49:54 +0100 | [diff] [blame] | 948 | uart = pos; |
| 949 | uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 950 | uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART; |
| 951 | uart->dp.length = sizeof(*uart); |
| 952 | pos += sizeof(*uart); |
| 953 | memcpy(pos, &END, sizeof(END)); |
| 954 | |
| 955 | return buf; |
| 956 | } |
| 957 | |
Adriano Cordova | 1d37031 | 2025-03-03 11:13:14 -0300 | [diff] [blame] | 958 | struct efi_device_path __maybe_unused *efi_dp_from_eth(struct udevice *dev) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 959 | { |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 960 | void *buf, *start; |
| 961 | unsigned dpsize = 0; |
| 962 | |
Adriano Cordova | 1d37031 | 2025-03-03 11:13:14 -0300 | [diff] [blame] | 963 | assert(dev); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 964 | |
Adriano Cordova | 1d37031 | 2025-03-03 11:13:14 -0300 | [diff] [blame] | 965 | dpsize += dp_size(dev); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 966 | |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 967 | start = buf = efi_alloc(dpsize + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 968 | if (!buf) |
| 969 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 970 | |
Adriano Cordova | 1d37031 | 2025-03-03 11:13:14 -0300 | [diff] [blame] | 971 | buf = dp_fill(buf, dev); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 972 | |
| 973 | *((struct efi_device_path *)buf) = END; |
| 974 | |
| 975 | return start; |
| 976 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 977 | |
Adriano Cordova | e9be2da | 2024-12-04 00:05:18 -0300 | [diff] [blame] | 978 | /** |
| 979 | * efi_dp_from_ipv4() - set device path from IPv4 address |
| 980 | * |
| 981 | * Set the device path to an ethernet device path as provided by |
| 982 | * efi_dp_from_eth() concatenated with a device path of subtype |
| 983 | * DEVICE_PATH_SUB_TYPE_MSG_IPV4, and an END node. |
| 984 | * |
| 985 | * @ip: IPv4 local address |
| 986 | * @mask: network mask |
| 987 | * @srv: IPv4 remote/server address |
Adriano Cordova | 1d37031 | 2025-03-03 11:13:14 -0300 | [diff] [blame] | 988 | * @dev: net udevice |
Adriano Cordova | e9be2da | 2024-12-04 00:05:18 -0300 | [diff] [blame] | 989 | * Return: pointer to device path, NULL on error |
| 990 | */ |
| 991 | static struct efi_device_path *efi_dp_from_ipv4(struct efi_ipv4_address *ip, |
| 992 | struct efi_ipv4_address *mask, |
Adriano Cordova | 1d37031 | 2025-03-03 11:13:14 -0300 | [diff] [blame] | 993 | struct efi_ipv4_address *srv, |
| 994 | struct udevice *dev) |
Adriano Cordova | e9be2da | 2024-12-04 00:05:18 -0300 | [diff] [blame] | 995 | { |
| 996 | struct efi_device_path *dp1, *dp2, *pos; |
| 997 | struct { |
| 998 | struct efi_device_path_ipv4 ipv4dp; |
| 999 | struct efi_device_path end; |
| 1000 | } dp; |
| 1001 | |
| 1002 | memset(&dp.ipv4dp, 0, sizeof(dp.ipv4dp)); |
| 1003 | dp.ipv4dp.dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 1004 | dp.ipv4dp.dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_IPV4; |
| 1005 | dp.ipv4dp.dp.length = sizeof(dp.ipv4dp); |
| 1006 | dp.ipv4dp.protocol = 6; |
| 1007 | if (ip) |
| 1008 | memcpy(&dp.ipv4dp.local_ip_address, ip, sizeof(*ip)); |
| 1009 | if (mask) |
| 1010 | memcpy(&dp.ipv4dp.subnet_mask, mask, sizeof(*mask)); |
| 1011 | if (srv) |
| 1012 | memcpy(&dp.ipv4dp.remote_ip_address, srv, sizeof(*srv)); |
| 1013 | pos = &dp.end; |
| 1014 | memcpy(pos, &END, sizeof(END)); |
| 1015 | |
Adriano Cordova | 1d37031 | 2025-03-03 11:13:14 -0300 | [diff] [blame] | 1016 | dp1 = efi_dp_from_eth(dev); |
Adriano Cordova | e9be2da | 2024-12-04 00:05:18 -0300 | [diff] [blame] | 1017 | if (!dp1) |
| 1018 | return NULL; |
| 1019 | |
| 1020 | dp2 = efi_dp_concat(dp1, (const struct efi_device_path *)&dp, 0); |
| 1021 | |
| 1022 | efi_free_pool(dp1); |
| 1023 | |
| 1024 | return dp2; |
| 1025 | } |
| 1026 | |
Adriano Cordova | 4bd5eca | 2024-12-04 00:05:22 -0300 | [diff] [blame] | 1027 | /** |
| 1028 | * efi_dp_from_http() - set device path from http |
| 1029 | * |
| 1030 | * Set the device path to an IPv4 path as provided by efi_dp_from_ipv4 |
| 1031 | * concatenated with a device path of subtype DEVICE_PATH_SUB_TYPE_MSG_URI, |
| 1032 | * and an END node. |
| 1033 | * |
| 1034 | * @server: URI of remote server |
Adriano Cordova | 1d37031 | 2025-03-03 11:13:14 -0300 | [diff] [blame] | 1035 | * @dev: net udevice |
Adriano Cordova | 4bd5eca | 2024-12-04 00:05:22 -0300 | [diff] [blame] | 1036 | * Return: pointer to HTTP device path, NULL on error |
| 1037 | */ |
Adriano Cordova | 1d37031 | 2025-03-03 11:13:14 -0300 | [diff] [blame] | 1038 | struct efi_device_path *efi_dp_from_http(const char *server, struct udevice *dev) |
Adriano Cordova | 4bd5eca | 2024-12-04 00:05:22 -0300 | [diff] [blame] | 1039 | { |
| 1040 | struct efi_device_path *dp1, *dp2; |
| 1041 | struct efi_device_path_uri *uridp; |
| 1042 | efi_uintn_t uridp_len; |
| 1043 | char *pos; |
| 1044 | char tmp[128]; |
| 1045 | struct efi_ipv4_address ip; |
| 1046 | struct efi_ipv4_address mask; |
| 1047 | |
| 1048 | if ((server && strlen("http://") + strlen(server) + 1 > sizeof(tmp)) || |
| 1049 | (!server && IS_ENABLED(CONFIG_NET_LWIP))) |
| 1050 | return NULL; |
| 1051 | |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1052 | efi_net_get_addr(&ip, &mask, NULL, dev); |
Adriano Cordova | 4bd5eca | 2024-12-04 00:05:22 -0300 | [diff] [blame] | 1053 | |
Adriano Cordova | 1d37031 | 2025-03-03 11:13:14 -0300 | [diff] [blame] | 1054 | dp1 = efi_dp_from_ipv4(&ip, &mask, NULL, dev); |
Adriano Cordova | 4bd5eca | 2024-12-04 00:05:22 -0300 | [diff] [blame] | 1055 | if (!dp1) |
| 1056 | return NULL; |
| 1057 | |
Adriano Cordova | 1d37031 | 2025-03-03 11:13:14 -0300 | [diff] [blame] | 1058 | |
Adriano Cordova | 4bd5eca | 2024-12-04 00:05:22 -0300 | [diff] [blame] | 1059 | strcpy(tmp, "http://"); |
| 1060 | |
| 1061 | if (server) { |
| 1062 | strlcat(tmp, server, sizeof(tmp)); |
| 1063 | #if !IS_ENABLED(CONFIG_NET_LWIP) |
Tom Rini | 1968754 | 2024-12-04 09:32:28 -0600 | [diff] [blame] | 1064 | } else { |
Adriano Cordova | 4bd5eca | 2024-12-04 00:05:22 -0300 | [diff] [blame] | 1065 | ip_to_string(net_server_ip, tmp + strlen("http://")); |
| 1066 | #endif |
| 1067 | } |
| 1068 | |
| 1069 | uridp_len = sizeof(struct efi_device_path) + strlen(tmp) + 1; |
| 1070 | uridp = efi_alloc(uridp_len + sizeof(END)); |
| 1071 | if (!uridp) { |
| 1072 | log_err("Out of memory\n"); |
| 1073 | return NULL; |
| 1074 | } |
| 1075 | uridp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 1076 | uridp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_URI; |
| 1077 | uridp->dp.length = uridp_len; |
| 1078 | debug("device path: setting uri device path to %s\n", tmp); |
| 1079 | memcpy(uridp->uri, tmp, strlen(tmp) + 1); |
| 1080 | |
| 1081 | pos = (char *)uridp + uridp_len; |
| 1082 | memcpy(pos, &END, sizeof(END)); |
| 1083 | |
| 1084 | dp2 = efi_dp_concat(dp1, (const struct efi_device_path *)uridp, 0); |
| 1085 | |
| 1086 | efi_free_pool(uridp); |
| 1087 | efi_free_pool(dp1); |
| 1088 | |
| 1089 | return dp2; |
| 1090 | } |
| 1091 | |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 1092 | /* Construct a device-path for memory-mapped image */ |
| 1093 | struct efi_device_path *efi_dp_from_mem(uint32_t memory_type, |
| 1094 | uint64_t start_address, |
Moritz Fischer | fe61a8b | 2024-11-04 01:49:34 +0000 | [diff] [blame] | 1095 | size_t size) |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 1096 | { |
| 1097 | struct efi_device_path_memory *mdp; |
| 1098 | void *buf, *start; |
| 1099 | |
Heinrich Schuchardt | 4564025 | 2023-03-19 09:20:22 +0100 | [diff] [blame] | 1100 | start = buf = efi_alloc(sizeof(*mdp) + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1101 | if (!buf) |
| 1102 | return NULL; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 1103 | |
| 1104 | mdp = buf; |
| 1105 | mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE; |
| 1106 | mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY; |
| 1107 | mdp->dp.length = sizeof(*mdp); |
| 1108 | mdp->memory_type = memory_type; |
| 1109 | mdp->start_address = start_address; |
Moritz Fischer | fe61a8b | 2024-11-04 01:49:34 +0000 | [diff] [blame] | 1110 | mdp->end_address = start_address + size; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 1111 | buf = &mdp[1]; |
| 1112 | |
| 1113 | *((struct efi_device_path *)buf) = END; |
| 1114 | |
| 1115 | return start; |
| 1116 | } |
| 1117 | |
Heinrich Schuchardt | 0d36adc | 2019-02-04 12:49:43 +0100 | [diff] [blame] | 1118 | /** |
| 1119 | * efi_dp_split_file_path() - split of relative file path from device path |
| 1120 | * |
| 1121 | * Given a device path indicating a file on a device, separate the device |
| 1122 | * path in two: the device path of the actual device and the file path |
| 1123 | * relative to this device. |
| 1124 | * |
| 1125 | * @full_path: device path including device and file path |
| 1126 | * @device_path: path of the device |
AKASHI Takahiro | 9c6531f | 2019-04-16 17:39:26 +0200 | [diff] [blame] | 1127 | * @file_path: relative path of the file or NULL if there is none |
Heinrich Schuchardt | 0d36adc | 2019-02-04 12:49:43 +0100 | [diff] [blame] | 1128 | * Return: status code |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1129 | */ |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1130 | efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path, |
| 1131 | struct efi_device_path **device_path, |
| 1132 | struct efi_device_path **file_path) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1133 | { |
AKASHI Takahiro | 9c6531f | 2019-04-16 17:39:26 +0200 | [diff] [blame] | 1134 | struct efi_device_path *p, *dp, *fp = NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1135 | |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1136 | *device_path = NULL; |
| 1137 | *file_path = NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1138 | dp = efi_dp_dup(full_path); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1139 | if (!dp) |
| 1140 | return EFI_OUT_OF_RESOURCES; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1141 | p = dp; |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1142 | while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) { |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1143 | p = efi_dp_next(p); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1144 | if (!p) |
AKASHI Takahiro | 9c6531f | 2019-04-16 17:39:26 +0200 | [diff] [blame] | 1145 | goto out; |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1146 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1147 | fp = efi_dp_dup(p); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1148 | if (!fp) |
| 1149 | return EFI_OUT_OF_RESOURCES; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1150 | p->type = DEVICE_PATH_TYPE_END; |
| 1151 | p->sub_type = DEVICE_PATH_SUB_TYPE_END; |
| 1152 | p->length = sizeof(*p); |
| 1153 | |
AKASHI Takahiro | 9c6531f | 2019-04-16 17:39:26 +0200 | [diff] [blame] | 1154 | out: |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1155 | *device_path = dp; |
| 1156 | *file_path = fp; |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1157 | return EFI_SUCCESS; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1158 | } |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1159 | |
Heinrich Schuchardt | 6e0a1b4 | 2019-10-30 20:13:24 +0100 | [diff] [blame] | 1160 | /** |
| 1161 | * efi_dp_from_name() - convert U-Boot device and file path to device path |
| 1162 | * |
| 1163 | * @dev: U-Boot device, e.g. 'mmc' |
| 1164 | * @devnr: U-Boot device number, e.g. 1 for 'mmc:1' |
| 1165 | * @path: file path relative to U-Boot device, may be NULL |
| 1166 | * @device: pointer to receive device path of the device |
| 1167 | * @file: pointer to receive device path for the file |
| 1168 | * Return: status code |
| 1169 | */ |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1170 | efi_status_t efi_dp_from_name(const char *dev, const char *devnr, |
| 1171 | const char *path, |
| 1172 | struct efi_device_path **device, |
| 1173 | struct efi_device_path **file) |
| 1174 | { |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1175 | struct blk_desc *desc = NULL; |
Heinrich Schuchardt | 7e269a3 | 2023-05-13 10:30:43 +0200 | [diff] [blame] | 1176 | struct efi_device_path *dp; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 1177 | struct disk_partition fs_partition; |
Rui Miguel Silva | 433f15a | 2022-05-11 10:55:40 +0100 | [diff] [blame] | 1178 | size_t image_size; |
| 1179 | void *image_addr; |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1180 | int part = 0; |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1181 | |
AKASHI Takahiro | 3984441 | 2018-11-05 18:06:40 +0900 | [diff] [blame] | 1182 | if (path && !file) |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1183 | return EFI_INVALID_PARAMETER; |
| 1184 | |
AKASHI Takahiro | 9b08b9a | 2024-01-17 13:39:41 +0900 | [diff] [blame] | 1185 | if (IS_ENABLED(CONFIG_EFI_BINARY_EXEC) && |
| 1186 | (!strcmp(dev, "Mem") || !strcmp(dev, "hostfs"))) { |
Heinrich Schuchardt | aecb9e2 | 2023-05-12 20:18:10 +0200 | [diff] [blame] | 1187 | /* loadm command and semihosting */ |
Rui Miguel Silva | 433f15a | 2022-05-11 10:55:40 +0100 | [diff] [blame] | 1188 | efi_get_image_parameters(&image_addr, &image_size); |
| 1189 | |
Heinrich Schuchardt | 7e269a3 | 2023-05-13 10:30:43 +0200 | [diff] [blame] | 1190 | dp = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, |
Moritz Fischer | fe61a8b | 2024-11-04 01:49:34 +0000 | [diff] [blame] | 1191 | (uintptr_t)image_addr, image_size); |
Adriano Cordova | 93cba0f | 2024-12-04 00:05:23 -0300 | [diff] [blame] | 1192 | } else if (IS_ENABLED(CONFIG_NETDEVICES) && |
Adriano Cordova | 5467469 | 2025-03-03 11:13:15 -0300 | [diff] [blame] | 1193 | (!strcmp(dev, "Net") || !strcmp(dev, "Http"))) { |
| 1194 | efi_net_dp_from_dev(&dp, eth_get_dev(), false); |
Heinrich Schuchardt | 87b8a71 | 2023-05-13 09:55:26 +0200 | [diff] [blame] | 1195 | } else if (!strcmp(dev, "Uart")) { |
Heinrich Schuchardt | 7e269a3 | 2023-05-13 10:30:43 +0200 | [diff] [blame] | 1196 | dp = efi_dp_from_uart(); |
Heinrich Schuchardt | 77c0da8 | 2021-03-19 02:49:54 +0100 | [diff] [blame] | 1197 | } else { |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1198 | part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition, |
| 1199 | 1); |
Patrick Delaunay | ba7a950 | 2019-04-10 11:02:58 +0200 | [diff] [blame] | 1200 | if (part < 0 || !desc) |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1201 | return EFI_INVALID_PARAMETER; |
| 1202 | |
Heinrich Schuchardt | 7e269a3 | 2023-05-13 10:30:43 +0200 | [diff] [blame] | 1203 | dp = efi_dp_from_part(desc, part); |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1204 | } |
Heinrich Schuchardt | 7e269a3 | 2023-05-13 10:30:43 +0200 | [diff] [blame] | 1205 | if (device) |
| 1206 | *device = dp; |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1207 | |
| 1208 | if (!path) |
| 1209 | return EFI_SUCCESS; |
| 1210 | |
Heinrich Schuchardt | ff08eac | 2023-05-13 10:36:21 +0200 | [diff] [blame] | 1211 | *file = efi_dp_from_file(dp, path); |
Heinrich Schuchardt | 6e0a1b4 | 2019-10-30 20:13:24 +0100 | [diff] [blame] | 1212 | if (!*file) |
Heinrich Schuchardt | bf0b3a1 | 2023-05-13 10:22:21 +0200 | [diff] [blame] | 1213 | return EFI_OUT_OF_RESOURCES; |
AKASHI Takahiro | f1dbbae | 2019-10-09 16:19:52 +0900 | [diff] [blame] | 1214 | |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1215 | return EFI_SUCCESS; |
| 1216 | } |
Heinrich Schuchardt | 22c8e08 | 2020-08-23 10:49:46 +0200 | [diff] [blame] | 1217 | |
| 1218 | /** |
| 1219 | * efi_dp_check_length() - check length of a device path |
| 1220 | * |
| 1221 | * @dp: pointer to device path |
| 1222 | * @maxlen: maximum length of the device path |
| 1223 | * Return: |
| 1224 | * * length of the device path if it is less or equal @maxlen |
| 1225 | * * -1 if the device path is longer then @maxlen |
| 1226 | * * -1 if a device path node has a length of less than 4 |
| 1227 | * * -EINVAL if maxlen exceeds SSIZE_MAX |
| 1228 | */ |
| 1229 | ssize_t efi_dp_check_length(const struct efi_device_path *dp, |
| 1230 | const size_t maxlen) |
| 1231 | { |
| 1232 | ssize_t ret = 0; |
| 1233 | u16 len; |
| 1234 | |
| 1235 | if (maxlen > SSIZE_MAX) |
| 1236 | return -EINVAL; |
| 1237 | for (;;) { |
| 1238 | len = dp->length; |
| 1239 | if (len < 4) |
| 1240 | return -1; |
| 1241 | ret += len; |
| 1242 | if (ret > maxlen) |
| 1243 | return -1; |
| 1244 | if (dp->type == DEVICE_PATH_TYPE_END && |
| 1245 | dp->sub_type == DEVICE_PATH_SUB_TYPE_END) |
| 1246 | return ret; |
| 1247 | dp = (const struct efi_device_path *)((const u8 *)dp + len); |
| 1248 | } |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | /** |
Heinrich Schuchardt | 2ca2680 | 2024-04-26 16:13:19 +0200 | [diff] [blame] | 1252 | * efi_dp_from_lo() - get device-path from load option |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 1253 | * |
Heinrich Schuchardt | 2ca2680 | 2024-04-26 16:13:19 +0200 | [diff] [blame] | 1254 | * The load options in U-Boot may contain multiple concatenated device-paths. |
| 1255 | * The first device-path indicates the EFI binary to execute. Subsequent |
| 1256 | * device-paths start with a VenMedia node where the GUID identifies the |
| 1257 | * function (initrd or fdt). |
| 1258 | * |
| 1259 | * @lo: EFI load option containing a valid device path |
| 1260 | * @guid: GUID identifying device-path or NULL for the EFI binary |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 1261 | * |
| 1262 | * Return: |
Heinrich Schuchardt | 2ca2680 | 2024-04-26 16:13:19 +0200 | [diff] [blame] | 1263 | * device path excluding the matched VenMedia node or NULL. |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 1264 | * Caller must free the returned value. |
| 1265 | */ |
| 1266 | struct |
| 1267 | efi_device_path *efi_dp_from_lo(struct efi_load_option *lo, |
Heinrich Schuchardt | 9979cff | 2021-10-15 01:31:02 +0200 | [diff] [blame] | 1268 | const efi_guid_t *guid) |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 1269 | { |
| 1270 | struct efi_device_path *fp = lo->file_path; |
| 1271 | struct efi_device_path_vendor *vendor; |
| 1272 | int lo_len = lo->file_path_length; |
| 1273 | |
Heinrich Schuchardt | 2ca2680 | 2024-04-26 16:13:19 +0200 | [diff] [blame] | 1274 | if (!guid) |
| 1275 | return efi_dp_dup(fp); |
| 1276 | |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 1277 | for (; lo_len >= sizeof(struct efi_device_path); |
| 1278 | lo_len -= fp->length, fp = (void *)fp + fp->length) { |
| 1279 | if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0) |
| 1280 | break; |
| 1281 | if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE || |
| 1282 | fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH) |
| 1283 | continue; |
| 1284 | |
| 1285 | vendor = (struct efi_device_path_vendor *)fp; |
Heinrich Schuchardt | 9979cff | 2021-10-15 01:31:02 +0200 | [diff] [blame] | 1286 | if (!guidcmp(&vendor->guid, guid)) |
Heinrich Schuchardt | 35dd322 | 2021-10-15 02:59:15 +0200 | [diff] [blame] | 1287 | return efi_dp_dup(efi_dp_next(fp)); |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 1288 | } |
| 1289 | log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label); |
| 1290 | |
| 1291 | return NULL; |
Heinrich Schuchardt | 22c8e08 | 2020-08-23 10:49:46 +0200 | [diff] [blame] | 1292 | } |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1293 | |
| 1294 | /** |
| 1295 | * search_gpt_dp_node() - search gpt device path node |
| 1296 | * |
| 1297 | * @device_path: device path |
| 1298 | * |
| 1299 | * Return: pointer to the gpt device path node |
| 1300 | */ |
| 1301 | struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path) |
| 1302 | { |
| 1303 | struct efi_device_path *dp = device_path; |
| 1304 | |
| 1305 | while (dp) { |
| 1306 | if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE && |
| 1307 | dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) { |
| 1308 | struct efi_device_path_hard_drive_path *hd_dp = |
| 1309 | (struct efi_device_path_hard_drive_path *)dp; |
| 1310 | |
| 1311 | if (hd_dp->partmap_type == PART_FORMAT_GPT && |
| 1312 | hd_dp->signature_type == SIG_TYPE_GUID) |
| 1313 | return dp; |
| 1314 | } |
| 1315 | dp = efi_dp_next(dp); |
| 1316 | } |
| 1317 | |
| 1318 | return NULL; |
| 1319 | } |