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