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