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