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