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