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