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: */ |
| 33 | static const struct efi_device_path END = { |
| 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 | * |
| 140 | * @dp: original devie-path |
| 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 | |
| 162 | static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path, |
| 163 | struct efi_device_path **rem) |
| 164 | { |
| 165 | struct efi_object *efiobj; |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 166 | efi_uintn_t dp_size = efi_dp_instance_size(dp); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 167 | |
| 168 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 169 | struct efi_handler *handler; |
| 170 | struct efi_device_path *obj_dp; |
| 171 | efi_status_t ret; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 172 | |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 173 | ret = efi_search_protocol(efiobj, |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 174 | &efi_guid_device_path, &handler); |
| 175 | if (ret != EFI_SUCCESS) |
| 176 | continue; |
| 177 | obj_dp = handler->protocol_interface; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 178 | |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 179 | do { |
| 180 | if (efi_dp_match(dp, obj_dp) == 0) { |
| 181 | if (rem) { |
Alexander Graf | f9360fb | 2017-12-11 14:29:46 +0100 | [diff] [blame] | 182 | /* |
| 183 | * Allow partial matches, but inform |
| 184 | * the caller. |
| 185 | */ |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 186 | *rem = ((void *)dp) + |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 187 | efi_dp_instance_size(obj_dp); |
Alexander Graf | f9360fb | 2017-12-11 14:29:46 +0100 | [diff] [blame] | 188 | return efiobj; |
| 189 | } else { |
| 190 | /* Only return on exact matches */ |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 191 | if (efi_dp_instance_size(obj_dp) == |
| 192 | dp_size) |
Alexander Graf | f9360fb | 2017-12-11 14:29:46 +0100 | [diff] [blame] | 193 | return efiobj; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 194 | } |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 195 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 196 | |
Heinrich Schuchardt | 24f0e6a | 2022-02-26 12:10:10 +0100 | [diff] [blame^] | 197 | obj_dp = efi_dp_shorten(efi_dp_next(obj_dp)); |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 198 | } while (short_path && obj_dp); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | return NULL; |
| 202 | } |
| 203 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 204 | /* |
| 205 | * Find an efiobj from device-path, if 'rem' is not NULL, returns the |
| 206 | * remaining part of the device path after the matched object. |
| 207 | */ |
| 208 | struct efi_object *efi_dp_find_obj(struct efi_device_path *dp, |
| 209 | struct efi_device_path **rem) |
| 210 | { |
| 211 | struct efi_object *efiobj; |
| 212 | |
Alexander Graf | f9360fb | 2017-12-11 14:29:46 +0100 | [diff] [blame] | 213 | /* Search for an exact match first */ |
| 214 | efiobj = find_obj(dp, false, NULL); |
| 215 | |
| 216 | /* Then for a fuzzy match */ |
| 217 | if (!efiobj) |
| 218 | efiobj = find_obj(dp, false, rem); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 219 | |
Alexander Graf | f9360fb | 2017-12-11 14:29:46 +0100 | [diff] [blame] | 220 | /* And now for a fuzzy short match */ |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 221 | if (!efiobj) |
| 222 | efiobj = find_obj(dp, true, rem); |
| 223 | |
| 224 | return efiobj; |
| 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 | |
| 285 | ndp = dp_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); |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 329 | void *p = dp_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; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 392 | void *p = dp_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); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 401 | void *p = dp_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 | b41c8e2 | 2018-04-16 07:59:05 +0200 | [diff] [blame] | 422 | ret = dp_alloc(length); |
| 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); |
| 444 | p = dp_alloc(sz + szi + 2 * sizeof(END)); |
| 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); |
| 469 | p = dp_alloc(sz + sizeof(END)); |
| 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 |
| 538 | case UCLASS_ROOT: |
| 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 | c770aaa | 2020-05-20 22:39:35 +0200 | [diff] [blame] | 547 | #ifdef CONFIG_VIRTIO_BLK |
| 548 | case UCLASS_VIRTIO: |
| 549 | /* |
| 550 | * Virtio devices will be represented as a vendor |
| 551 | * device node with an extra byte for the device |
| 552 | * number. |
| 553 | */ |
| 554 | return dp_size(dev->parent) |
| 555 | + sizeof(struct efi_device_path_vendor) + 1; |
| 556 | #endif |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 557 | default: |
| 558 | return dp_size(dev->parent); |
| 559 | } |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 560 | #if defined(CONFIG_MMC) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 561 | case UCLASS_MMC: |
| 562 | return dp_size(dev->parent) + |
| 563 | sizeof(struct efi_device_path_sd_mmc_path); |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 564 | #endif |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 565 | case UCLASS_MASS_STORAGE: |
| 566 | case UCLASS_USB_HUB: |
| 567 | return dp_size(dev->parent) + |
| 568 | sizeof(struct efi_device_path_usb_class); |
| 569 | default: |
| 570 | /* just skip over unknown classes: */ |
| 571 | return dp_size(dev->parent); |
| 572 | } |
| 573 | } |
| 574 | |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 575 | /* |
| 576 | * Recursively build a device path. |
| 577 | * |
| 578 | * @buf pointer to the end of the device path |
| 579 | * @dev device |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 580 | * Return: pointer to the end of the device path |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 581 | */ |
Heinrich Schuchardt | c22d9e3 | 2019-11-10 02:16:33 +0100 | [diff] [blame] | 582 | __maybe_unused static void *dp_fill(void *buf, struct udevice *dev) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 583 | { |
| 584 | if (!dev || !dev->driver) |
| 585 | return buf; |
| 586 | |
Simon Glass | 56ada7b | 2022-01-29 14:58:38 -0700 | [diff] [blame] | 587 | switch (device_get_uclass_id(dev)) { |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 588 | case UCLASS_ROOT: |
| 589 | case UCLASS_SIMPLE_BUS: { |
| 590 | /* stop traversing parents at this point: */ |
| 591 | struct efi_device_path_vendor *vdp = buf; |
| 592 | *vdp = ROOT; |
| 593 | return &vdp[1]; |
| 594 | } |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 595 | #ifdef CONFIG_NET |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 596 | case UCLASS_ETH: { |
| 597 | struct efi_device_path_mac_addr *dp = |
| 598 | dp_fill(buf, dev->parent); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 599 | struct eth_pdata *pdata = dev_get_plat(dev); |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 600 | |
| 601 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 602 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR; |
| 603 | dp->dp.length = sizeof(*dp); |
| 604 | memset(&dp->mac, 0, sizeof(dp->mac)); |
| 605 | /* We only support IPv4 */ |
| 606 | memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN); |
| 607 | /* Ethernet */ |
| 608 | dp->if_type = 1; |
| 609 | return &dp[1]; |
| 610 | } |
| 611 | #endif |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 612 | case UCLASS_BLK: |
| 613 | switch (dev->parent->uclass->uc_drv->id) { |
AKASHI Takahiro | 659a626 | 2019-09-12 13:52:35 +0900 | [diff] [blame] | 614 | #ifdef CONFIG_SANDBOX |
| 615 | case UCLASS_ROOT: { |
| 616 | /* stop traversing parents at this point: */ |
Heinrich Schuchardt | 1e3beaf | 2020-05-06 01:28:08 +0200 | [diff] [blame] | 617 | struct efi_device_path_vendor *dp; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 618 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
AKASHI Takahiro | 659a626 | 2019-09-12 13:52:35 +0900 | [diff] [blame] | 619 | |
| 620 | dp_fill(buf, dev->parent); |
| 621 | dp = buf; |
| 622 | ++dp; |
| 623 | dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE; |
| 624 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR; |
| 625 | dp->dp.length = sizeof(*dp) + 1; |
| 626 | memcpy(&dp->guid, &efi_guid_host_dev, |
| 627 | sizeof(efi_guid_t)); |
| 628 | dp->vendor_data[0] = desc->devnum; |
| 629 | return &dp->vendor_data[1]; |
| 630 | } |
| 631 | #endif |
Heinrich Schuchardt | c770aaa | 2020-05-20 22:39:35 +0200 | [diff] [blame] | 632 | #ifdef CONFIG_VIRTIO_BLK |
| 633 | case UCLASS_VIRTIO: { |
| 634 | struct efi_device_path_vendor *dp; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 635 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Heinrich Schuchardt | c770aaa | 2020-05-20 22:39:35 +0200 | [diff] [blame] | 636 | |
| 637 | dp_fill(buf, dev->parent); |
| 638 | dp = buf; |
| 639 | ++dp; |
| 640 | dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE; |
| 641 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR; |
| 642 | dp->dp.length = sizeof(*dp) + 1; |
| 643 | memcpy(&dp->guid, &efi_guid_virtio_dev, |
| 644 | sizeof(efi_guid_t)); |
| 645 | dp->vendor_data[0] = desc->devnum; |
| 646 | return &dp->vendor_data[1]; |
| 647 | } |
| 648 | #endif |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 649 | #ifdef CONFIG_IDE |
| 650 | case UCLASS_IDE: { |
| 651 | struct efi_device_path_atapi *dp = |
| 652 | dp_fill(buf, dev->parent); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 653 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 654 | |
| 655 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 656 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI; |
| 657 | dp->dp.length = sizeof(*dp); |
| 658 | dp->logical_unit_number = desc->devnum; |
| 659 | dp->primary_secondary = IDE_BUS(desc->devnum); |
| 660 | dp->slave_master = desc->devnum % |
| 661 | (CONFIG_SYS_IDE_MAXDEVICE / |
| 662 | CONFIG_SYS_IDE_MAXBUS); |
| 663 | return &dp[1]; |
| 664 | } |
| 665 | #endif |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 666 | #if defined(CONFIG_SCSI) |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 667 | case UCLASS_SCSI: { |
| 668 | struct efi_device_path_scsi *dp = |
| 669 | dp_fill(buf, dev->parent); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 670 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 671 | |
| 672 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 673 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI; |
| 674 | dp->dp.length = sizeof(*dp); |
| 675 | dp->logical_unit_number = desc->lun; |
| 676 | dp->target_id = desc->target; |
| 677 | return &dp[1]; |
| 678 | } |
| 679 | #endif |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 680 | #if defined(CONFIG_MMC) |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 681 | case UCLASS_MMC: { |
| 682 | struct efi_device_path_sd_mmc_path *sddp = |
| 683 | dp_fill(buf, dev->parent); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 684 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 685 | |
| 686 | sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 687 | sddp->dp.sub_type = is_sd(desc) ? |
| 688 | DEVICE_PATH_SUB_TYPE_MSG_SD : |
| 689 | DEVICE_PATH_SUB_TYPE_MSG_MMC; |
| 690 | sddp->dp.length = sizeof(*sddp); |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 691 | sddp->slot_number = dev_seq(dev); |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 692 | return &sddp[1]; |
| 693 | } |
| 694 | #endif |
Heinrich Schuchardt | 7bdb602 | 2020-05-20 23:12:02 +0200 | [diff] [blame] | 695 | #if defined(CONFIG_AHCI) || defined(CONFIG_SATA) |
| 696 | case UCLASS_AHCI: { |
| 697 | struct efi_device_path_sata *dp = |
| 698 | dp_fill(buf, dev->parent); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 699 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Heinrich Schuchardt | 7bdb602 | 2020-05-20 23:12:02 +0200 | [diff] [blame] | 700 | |
| 701 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 702 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SATA; |
| 703 | dp->dp.length = sizeof(*dp); |
| 704 | dp->hba_port = desc->devnum; |
| 705 | /* default 0xffff implies no port multiplier */ |
| 706 | dp->port_multiplier_port = 0xffff; |
| 707 | dp->logical_unit_number = desc->lun; |
| 708 | return &dp[1]; |
| 709 | } |
| 710 | #endif |
Patrick Wildt | a3ca37e | 2019-10-03 16:24:17 +0200 | [diff] [blame] | 711 | #if defined(CONFIG_NVME) |
| 712 | case UCLASS_NVME: { |
| 713 | struct efi_device_path_nvme *dp = |
| 714 | dp_fill(buf, dev->parent); |
| 715 | u32 ns_id; |
| 716 | |
| 717 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 718 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME; |
| 719 | dp->dp.length = sizeof(*dp); |
| 720 | nvme_get_namespace_id(dev, &ns_id, dp->eui64); |
| 721 | memcpy(&dp->ns_id, &ns_id, sizeof(ns_id)); |
| 722 | return &dp[1]; |
| 723 | } |
| 724 | #endif |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 725 | default: |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 726 | debug("%s(%u) %s: unhandled parent class: %s (%u)\n", |
| 727 | __FILE__, __LINE__, __func__, |
| 728 | dev->name, dev->parent->uclass->uc_drv->id); |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 729 | return dp_fill(buf, dev->parent); |
| 730 | } |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 731 | #if defined(CONFIG_MMC) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 732 | case UCLASS_MMC: { |
| 733 | struct efi_device_path_sd_mmc_path *sddp = |
| 734 | dp_fill(buf, dev->parent); |
| 735 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 736 | struct blk_desc *desc = mmc_get_blk_desc(mmc); |
| 737 | |
| 738 | sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
Heinrich Schuchardt | 7d569db | 2017-12-11 12:56:39 +0100 | [diff] [blame] | 739 | sddp->dp.sub_type = is_sd(desc) ? |
| 740 | DEVICE_PATH_SUB_TYPE_MSG_SD : |
| 741 | DEVICE_PATH_SUB_TYPE_MSG_MMC; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 742 | sddp->dp.length = sizeof(*sddp); |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 743 | sddp->slot_number = dev_seq(dev); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 744 | |
| 745 | return &sddp[1]; |
| 746 | } |
| 747 | #endif |
| 748 | case UCLASS_MASS_STORAGE: |
| 749 | case UCLASS_USB_HUB: { |
| 750 | struct efi_device_path_usb_class *udp = |
| 751 | dp_fill(buf, dev->parent); |
| 752 | struct usb_device *udev = dev_get_parent_priv(dev); |
| 753 | struct usb_device_descriptor *desc = &udev->descriptor; |
| 754 | |
| 755 | udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 756 | udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS; |
| 757 | udp->dp.length = sizeof(*udp); |
| 758 | udp->vendor_id = desc->idVendor; |
| 759 | udp->product_id = desc->idProduct; |
| 760 | udp->device_class = desc->bDeviceClass; |
| 761 | udp->device_subclass = desc->bDeviceSubClass; |
| 762 | udp->device_protocol = desc->bDeviceProtocol; |
| 763 | |
| 764 | return &udp[1]; |
| 765 | } |
| 766 | default: |
Simon Glass | 56ada7b | 2022-01-29 14:58:38 -0700 | [diff] [blame] | 767 | /* If the uclass driver is missing, this will show NULL */ |
| 768 | log_debug("unhandled device class: %s (%s)\n", dev->name, |
| 769 | dev_get_uclass_name(dev)); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 770 | return dp_fill(buf, dev->parent); |
| 771 | } |
| 772 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 773 | |
| 774 | static unsigned dp_part_size(struct blk_desc *desc, int part) |
| 775 | { |
| 776 | unsigned dpsize; |
Simon Glass | ec209a7 | 2022-01-29 14:58:39 -0700 | [diff] [blame] | 777 | struct udevice *dev = desc->bdev; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 778 | |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 779 | dpsize = dp_size(dev); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 780 | |
| 781 | if (part == 0) /* the actual disk, not a partition */ |
| 782 | return dpsize; |
| 783 | |
| 784 | if (desc->part_type == PART_TYPE_ISO) |
| 785 | dpsize += sizeof(struct efi_device_path_cdrom_path); |
| 786 | else |
| 787 | dpsize += sizeof(struct efi_device_path_hard_drive_path); |
| 788 | |
| 789 | return dpsize; |
| 790 | } |
| 791 | |
Heinrich Schuchardt | 8983ffd | 2017-12-11 12:56:42 +0100 | [diff] [blame] | 792 | /* |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 793 | * Create a device node for a block device partition. |
Heinrich Schuchardt | 8983ffd | 2017-12-11 12:56:42 +0100 | [diff] [blame] | 794 | * |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 795 | * @buf buffer to which the device path is written |
Heinrich Schuchardt | 8983ffd | 2017-12-11 12:56:42 +0100 | [diff] [blame] | 796 | * @desc block device descriptor |
| 797 | * @part partition number, 0 identifies a block device |
| 798 | */ |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 799 | 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] | 800 | { |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 801 | struct disk_partition info; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 802 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 803 | part_get_info(desc, part, &info); |
| 804 | |
| 805 | if (desc->part_type == PART_TYPE_ISO) { |
| 806 | struct efi_device_path_cdrom_path *cddp = buf; |
| 807 | |
Heinrich Schuchardt | 2aae6db | 2017-12-11 12:56:40 +0100 | [diff] [blame] | 808 | cddp->boot_entry = part; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 809 | cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; |
| 810 | cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH; |
| 811 | cddp->dp.length = sizeof(*cddp); |
| 812 | cddp->partition_start = info.start; |
Heinrich Schuchardt | 28dfd1a | 2019-09-04 13:56:01 +0200 | [diff] [blame] | 813 | cddp->partition_size = info.size; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 814 | |
| 815 | buf = &cddp[1]; |
| 816 | } else { |
| 817 | struct efi_device_path_hard_drive_path *hddp = buf; |
| 818 | |
| 819 | hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; |
| 820 | hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH; |
| 821 | hddp->dp.length = sizeof(*hddp); |
Heinrich Schuchardt | 2aae6db | 2017-12-11 12:56:40 +0100 | [diff] [blame] | 822 | hddp->partition_number = part; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 823 | hddp->partition_start = info.start; |
| 824 | hddp->partition_end = info.size; |
| 825 | if (desc->part_type == PART_TYPE_EFI) |
| 826 | hddp->partmap_type = 2; |
| 827 | else |
| 828 | hddp->partmap_type = 1; |
Jonathan Gray | 84b4d70 | 2017-11-22 14:18:59 +1100 | [diff] [blame] | 829 | |
| 830 | switch (desc->sig_type) { |
| 831 | case SIG_TYPE_NONE: |
| 832 | default: |
| 833 | hddp->signature_type = 0; |
| 834 | memset(hddp->partition_signature, 0, |
| 835 | sizeof(hddp->partition_signature)); |
| 836 | break; |
| 837 | case SIG_TYPE_MBR: |
| 838 | hddp->signature_type = 1; |
| 839 | memset(hddp->partition_signature, 0, |
| 840 | sizeof(hddp->partition_signature)); |
| 841 | memcpy(hddp->partition_signature, &desc->mbr_sig, |
| 842 | sizeof(desc->mbr_sig)); |
| 843 | break; |
| 844 | case SIG_TYPE_GUID: |
| 845 | hddp->signature_type = 2; |
Alfonso Sánchez-Beato | f007a37 | 2021-07-15 15:31:42 +0200 | [diff] [blame] | 846 | if (uuid_str_to_bin(info.uuid, |
| 847 | hddp->partition_signature, 1)) |
| 848 | log_warning( |
| 849 | "Partition no. %d: invalid guid: %s\n", |
| 850 | part, info.uuid); |
Jonathan Gray | 84b4d70 | 2017-11-22 14:18:59 +1100 | [diff] [blame] | 851 | break; |
| 852 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 853 | |
| 854 | buf = &hddp[1]; |
| 855 | } |
| 856 | |
| 857 | return buf; |
| 858 | } |
| 859 | |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 860 | /* |
| 861 | * Create a device path for a block device or one of its partitions. |
| 862 | * |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 863 | * @buf buffer to which the device path is written |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 864 | * @desc block device descriptor |
| 865 | * @part partition number, 0 identifies a block device |
| 866 | */ |
| 867 | static void *dp_part_fill(void *buf, struct blk_desc *desc, int part) |
| 868 | { |
Simon Glass | ec209a7 | 2022-01-29 14:58:39 -0700 | [diff] [blame] | 869 | struct udevice *dev = desc->bdev; |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 870 | |
Simon Glass | 222f3cb | 2021-09-24 18:30:17 -0600 | [diff] [blame] | 871 | buf = dp_fill(buf, dev); |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 872 | |
| 873 | if (part == 0) /* the actual disk, not a partition */ |
| 874 | return buf; |
| 875 | |
| 876 | return dp_part_node(buf, desc, part); |
| 877 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 878 | |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 879 | /* Construct a device-path from a partition on a block device: */ |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 880 | struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part) |
| 881 | { |
| 882 | void *buf, *start; |
| 883 | |
| 884 | start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 885 | if (!buf) |
| 886 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 887 | |
| 888 | buf = dp_part_fill(buf, desc, part); |
| 889 | |
| 890 | *((struct efi_device_path *)buf) = END; |
| 891 | |
| 892 | return start; |
| 893 | } |
| 894 | |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 895 | /* |
| 896 | * Create a device node for a block device partition. |
| 897 | * |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 898 | * @buf buffer to which the device path is written |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 899 | * @desc block device descriptor |
| 900 | * @part partition number, 0 identifies a block device |
| 901 | */ |
| 902 | struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part) |
| 903 | { |
| 904 | efi_uintn_t dpsize; |
| 905 | void *buf; |
| 906 | |
| 907 | if (desc->part_type == PART_TYPE_ISO) |
| 908 | dpsize = sizeof(struct efi_device_path_cdrom_path); |
| 909 | else |
| 910 | dpsize = sizeof(struct efi_device_path_hard_drive_path); |
| 911 | buf = dp_alloc(dpsize); |
| 912 | |
| 913 | dp_part_node(buf, desc, part); |
| 914 | |
| 915 | return buf; |
| 916 | } |
| 917 | |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 918 | /** |
| 919 | * path_to_uefi() - convert UTF-8 path to an UEFI style path |
| 920 | * |
| 921 | * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path |
| 922 | * separators and UTF-16). |
| 923 | * |
| 924 | * @src: source buffer |
| 925 | * @uefi: target buffer, possibly unaligned |
| 926 | */ |
| 927 | static void path_to_uefi(void *uefi, const char *src) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 928 | { |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 929 | u16 *pos = uefi; |
| 930 | |
| 931 | /* |
| 932 | * efi_set_bootdev() calls this routine indirectly before the UEFI |
| 933 | * subsystem is initialized. So we cannot assume unaligned access to be |
| 934 | * enabled. |
| 935 | */ |
| 936 | allow_unaligned(); |
| 937 | |
| 938 | while (*src) { |
| 939 | s32 code = utf8_get(&src); |
| 940 | |
| 941 | if (code < 0) |
| 942 | code = '?'; |
| 943 | else if (code == '/') |
| 944 | code = '\\'; |
| 945 | utf16_put(code, &pos); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 946 | } |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 947 | *pos = 0; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /* |
| 951 | * If desc is NULL, this creates a path with only the file component, |
| 952 | * otherwise it creates a full path with both device and file components |
| 953 | */ |
| 954 | struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part, |
| 955 | const char *path) |
| 956 | { |
| 957 | struct efi_device_path_file_path *fp; |
| 958 | void *buf, *start; |
AKASHI Takahiro | f1dbbae | 2019-10-09 16:19:52 +0900 | [diff] [blame] | 959 | size_t dpsize = 0, fpsize; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 960 | |
| 961 | if (desc) |
| 962 | dpsize = dp_part_size(desc, part); |
| 963 | |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 964 | fpsize = sizeof(struct efi_device_path) + |
| 965 | 2 * (utf8_utf16_strlen(path) + 1); |
AKASHI Takahiro | f1dbbae | 2019-10-09 16:19:52 +0900 | [diff] [blame] | 966 | if (fpsize > U16_MAX) |
| 967 | return NULL; |
| 968 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 969 | dpsize += fpsize; |
| 970 | |
| 971 | start = buf = dp_alloc(dpsize + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 972 | if (!buf) |
| 973 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 974 | |
| 975 | if (desc) |
| 976 | buf = dp_part_fill(buf, desc, part); |
| 977 | |
| 978 | /* add file-path: */ |
| 979 | fp = buf; |
| 980 | fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; |
| 981 | fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH; |
AKASHI Takahiro | f1dbbae | 2019-10-09 16:19:52 +0900 | [diff] [blame] | 982 | fp->dp.length = (u16)fpsize; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 983 | path_to_uefi(fp->str, path); |
| 984 | buf += fpsize; |
| 985 | |
| 986 | *((struct efi_device_path *)buf) = END; |
| 987 | |
| 988 | return start; |
| 989 | } |
| 990 | |
Heinrich Schuchardt | 77c0da8 | 2021-03-19 02:49:54 +0100 | [diff] [blame] | 991 | struct efi_device_path *efi_dp_from_uart(void) |
| 992 | { |
| 993 | void *buf, *pos; |
| 994 | struct efi_device_path_uart *uart; |
| 995 | size_t dpsize = sizeof(ROOT) + sizeof(*uart) + sizeof(END); |
| 996 | |
| 997 | buf = dp_alloc(dpsize); |
| 998 | if (!buf) |
| 999 | return NULL; |
| 1000 | pos = buf; |
| 1001 | memcpy(pos, &ROOT, sizeof(ROOT)); |
| 1002 | pos += sizeof(ROOT); |
| 1003 | uart = pos; |
| 1004 | uart->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 1005 | uart->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_UART; |
| 1006 | uart->dp.length = sizeof(*uart); |
| 1007 | pos += sizeof(*uart); |
| 1008 | memcpy(pos, &END, sizeof(END)); |
| 1009 | |
| 1010 | return buf; |
| 1011 | } |
| 1012 | |
Joe Hershberger | 5277a97 | 2018-04-13 15:26:39 -0500 | [diff] [blame] | 1013 | #ifdef CONFIG_NET |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1014 | struct efi_device_path *efi_dp_from_eth(void) |
| 1015 | { |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1016 | void *buf, *start; |
| 1017 | unsigned dpsize = 0; |
| 1018 | |
| 1019 | assert(eth_get_dev()); |
| 1020 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1021 | dpsize += dp_size(eth_get_dev()); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1022 | |
| 1023 | start = buf = dp_alloc(dpsize + 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 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1027 | buf = dp_fill(buf, eth_get_dev()); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1028 | |
| 1029 | *((struct efi_device_path *)buf) = END; |
| 1030 | |
| 1031 | return start; |
| 1032 | } |
| 1033 | #endif |
| 1034 | |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 1035 | /* Construct a device-path for memory-mapped image */ |
| 1036 | struct efi_device_path *efi_dp_from_mem(uint32_t memory_type, |
| 1037 | uint64_t start_address, |
| 1038 | uint64_t end_address) |
| 1039 | { |
| 1040 | struct efi_device_path_memory *mdp; |
| 1041 | void *buf, *start; |
| 1042 | |
| 1043 | start = buf = dp_alloc(sizeof(*mdp) + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1044 | if (!buf) |
| 1045 | return NULL; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 1046 | |
| 1047 | mdp = buf; |
| 1048 | mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE; |
| 1049 | mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY; |
| 1050 | mdp->dp.length = sizeof(*mdp); |
| 1051 | mdp->memory_type = memory_type; |
| 1052 | mdp->start_address = start_address; |
| 1053 | mdp->end_address = end_address; |
| 1054 | buf = &mdp[1]; |
| 1055 | |
| 1056 | *((struct efi_device_path *)buf) = END; |
| 1057 | |
| 1058 | return start; |
| 1059 | } |
| 1060 | |
Heinrich Schuchardt | 0d36adc | 2019-02-04 12:49:43 +0100 | [diff] [blame] | 1061 | /** |
| 1062 | * efi_dp_split_file_path() - split of relative file path from device path |
| 1063 | * |
| 1064 | * Given a device path indicating a file on a device, separate the device |
| 1065 | * path in two: the device path of the actual device and the file path |
| 1066 | * relative to this device. |
| 1067 | * |
| 1068 | * @full_path: device path including device and file path |
| 1069 | * @device_path: path of the device |
AKASHI Takahiro | 9c6531f | 2019-04-16 17:39:26 +0200 | [diff] [blame] | 1070 | * @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] | 1071 | * Return: status code |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1072 | */ |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1073 | efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path, |
| 1074 | struct efi_device_path **device_path, |
| 1075 | struct efi_device_path **file_path) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1076 | { |
AKASHI Takahiro | 9c6531f | 2019-04-16 17:39:26 +0200 | [diff] [blame] | 1077 | struct efi_device_path *p, *dp, *fp = NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1078 | |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1079 | *device_path = NULL; |
| 1080 | *file_path = NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1081 | dp = efi_dp_dup(full_path); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1082 | if (!dp) |
| 1083 | return EFI_OUT_OF_RESOURCES; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1084 | p = dp; |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1085 | while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) { |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1086 | p = efi_dp_next(p); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1087 | if (!p) |
AKASHI Takahiro | 9c6531f | 2019-04-16 17:39:26 +0200 | [diff] [blame] | 1088 | goto out; |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1089 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1090 | fp = efi_dp_dup(p); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1091 | if (!fp) |
| 1092 | return EFI_OUT_OF_RESOURCES; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1093 | p->type = DEVICE_PATH_TYPE_END; |
| 1094 | p->sub_type = DEVICE_PATH_SUB_TYPE_END; |
| 1095 | p->length = sizeof(*p); |
| 1096 | |
AKASHI Takahiro | 9c6531f | 2019-04-16 17:39:26 +0200 | [diff] [blame] | 1097 | out: |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1098 | *device_path = dp; |
| 1099 | *file_path = fp; |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1100 | return EFI_SUCCESS; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1101 | } |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1102 | |
Heinrich Schuchardt | 6e0a1b4 | 2019-10-30 20:13:24 +0100 | [diff] [blame] | 1103 | /** |
| 1104 | * efi_dp_from_name() - convert U-Boot device and file path to device path |
| 1105 | * |
| 1106 | * @dev: U-Boot device, e.g. 'mmc' |
| 1107 | * @devnr: U-Boot device number, e.g. 1 for 'mmc:1' |
| 1108 | * @path: file path relative to U-Boot device, may be NULL |
| 1109 | * @device: pointer to receive device path of the device |
| 1110 | * @file: pointer to receive device path for the file |
| 1111 | * Return: status code |
| 1112 | */ |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1113 | efi_status_t efi_dp_from_name(const char *dev, const char *devnr, |
| 1114 | const char *path, |
| 1115 | struct efi_device_path **device, |
| 1116 | struct efi_device_path **file) |
| 1117 | { |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1118 | struct blk_desc *desc = NULL; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 1119 | struct disk_partition fs_partition; |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1120 | int part = 0; |
Heinrich Schuchardt | 158c0d7 | 2021-05-25 12:07:30 +0200 | [diff] [blame] | 1121 | char *filename; |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1122 | char *s; |
| 1123 | |
AKASHI Takahiro | 3984441 | 2018-11-05 18:06:40 +0900 | [diff] [blame] | 1124 | if (path && !file) |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1125 | return EFI_INVALID_PARAMETER; |
| 1126 | |
Heinrich Schuchardt | 77c0da8 | 2021-03-19 02:49:54 +0100 | [diff] [blame] | 1127 | if (!strcmp(dev, "Net")) { |
| 1128 | #ifdef CONFIG_NET |
| 1129 | if (device) |
| 1130 | *device = efi_dp_from_eth(); |
| 1131 | #endif |
| 1132 | } else if (!strcmp(dev, "Uart")) { |
| 1133 | if (device) |
| 1134 | *device = efi_dp_from_uart(); |
| 1135 | } else { |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1136 | part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition, |
| 1137 | 1); |
Patrick Delaunay | ba7a950 | 2019-04-10 11:02:58 +0200 | [diff] [blame] | 1138 | if (part < 0 || !desc) |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1139 | return EFI_INVALID_PARAMETER; |
| 1140 | |
AKASHI Takahiro | 3984441 | 2018-11-05 18:06:40 +0900 | [diff] [blame] | 1141 | if (device) |
| 1142 | *device = efi_dp_from_part(desc, part); |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | if (!path) |
| 1146 | return EFI_SUCCESS; |
| 1147 | |
Heinrich Schuchardt | 158c0d7 | 2021-05-25 12:07:30 +0200 | [diff] [blame] | 1148 | filename = calloc(1, strlen(path) + 1); |
| 1149 | if (!filename) |
| 1150 | return EFI_OUT_OF_RESOURCES; |
| 1151 | |
| 1152 | sprintf(filename, "%s", path); |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1153 | /* DOS style file path: */ |
| 1154 | s = filename; |
| 1155 | while ((s = strchr(s, '/'))) |
| 1156 | *s++ = '\\'; |
Heinrich Schuchardt | 77c0da8 | 2021-03-19 02:49:54 +0100 | [diff] [blame] | 1157 | *file = efi_dp_from_file(desc, part, filename); |
Heinrich Schuchardt | 158c0d7 | 2021-05-25 12:07:30 +0200 | [diff] [blame] | 1158 | free(filename); |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1159 | |
Heinrich Schuchardt | 6e0a1b4 | 2019-10-30 20:13:24 +0100 | [diff] [blame] | 1160 | if (!*file) |
AKASHI Takahiro | f1dbbae | 2019-10-09 16:19:52 +0900 | [diff] [blame] | 1161 | return EFI_INVALID_PARAMETER; |
| 1162 | |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1163 | return EFI_SUCCESS; |
| 1164 | } |
Heinrich Schuchardt | 22c8e08 | 2020-08-23 10:49:46 +0200 | [diff] [blame] | 1165 | |
| 1166 | /** |
| 1167 | * efi_dp_check_length() - check length of a device path |
| 1168 | * |
| 1169 | * @dp: pointer to device path |
| 1170 | * @maxlen: maximum length of the device path |
| 1171 | * Return: |
| 1172 | * * length of the device path if it is less or equal @maxlen |
| 1173 | * * -1 if the device path is longer then @maxlen |
| 1174 | * * -1 if a device path node has a length of less than 4 |
| 1175 | * * -EINVAL if maxlen exceeds SSIZE_MAX |
| 1176 | */ |
| 1177 | ssize_t efi_dp_check_length(const struct efi_device_path *dp, |
| 1178 | const size_t maxlen) |
| 1179 | { |
| 1180 | ssize_t ret = 0; |
| 1181 | u16 len; |
| 1182 | |
| 1183 | if (maxlen > SSIZE_MAX) |
| 1184 | return -EINVAL; |
| 1185 | for (;;) { |
| 1186 | len = dp->length; |
| 1187 | if (len < 4) |
| 1188 | return -1; |
| 1189 | ret += len; |
| 1190 | if (ret > maxlen) |
| 1191 | return -1; |
| 1192 | if (dp->type == DEVICE_PATH_TYPE_END && |
| 1193 | dp->sub_type == DEVICE_PATH_SUB_TYPE_END) |
| 1194 | return ret; |
| 1195 | dp = (const struct efi_device_path *)((const u8 *)dp + len); |
| 1196 | } |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /** |
| 1200 | * efi_dp_from_lo() - Get the instance of a VenMedia node in a |
| 1201 | * multi-instance device path that matches |
| 1202 | * a specific GUID. This kind of device paths |
| 1203 | * is found in Boot#### options describing an |
| 1204 | * initrd location |
| 1205 | * |
| 1206 | * @lo: EFI_LOAD_OPTION containing a valid device path |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 1207 | * @guid: guid to search for |
| 1208 | * |
| 1209 | * Return: |
| 1210 | * device path including the VenMedia node or NULL. |
| 1211 | * Caller must free the returned value. |
| 1212 | */ |
| 1213 | struct |
| 1214 | efi_device_path *efi_dp_from_lo(struct efi_load_option *lo, |
Heinrich Schuchardt | 9979cff | 2021-10-15 01:31:02 +0200 | [diff] [blame] | 1215 | const efi_guid_t *guid) |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 1216 | { |
| 1217 | struct efi_device_path *fp = lo->file_path; |
| 1218 | struct efi_device_path_vendor *vendor; |
| 1219 | int lo_len = lo->file_path_length; |
| 1220 | |
| 1221 | for (; lo_len >= sizeof(struct efi_device_path); |
| 1222 | lo_len -= fp->length, fp = (void *)fp + fp->length) { |
| 1223 | if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0) |
| 1224 | break; |
| 1225 | if (fp->type != DEVICE_PATH_TYPE_MEDIA_DEVICE || |
| 1226 | fp->sub_type != DEVICE_PATH_SUB_TYPE_VENDOR_PATH) |
| 1227 | continue; |
| 1228 | |
| 1229 | vendor = (struct efi_device_path_vendor *)fp; |
Heinrich Schuchardt | 9979cff | 2021-10-15 01:31:02 +0200 | [diff] [blame] | 1230 | if (!guidcmp(&vendor->guid, guid)) |
Heinrich Schuchardt | 35dd322 | 2021-10-15 02:59:15 +0200 | [diff] [blame] | 1231 | return efi_dp_dup(efi_dp_next(fp)); |
Ilias Apalodimas | 483d28e | 2021-03-17 21:54:58 +0200 | [diff] [blame] | 1232 | } |
| 1233 | log_debug("VenMedia(%pUl) not found in %ls\n", &guid, lo->label); |
| 1234 | |
| 1235 | return NULL; |
Heinrich Schuchardt | 22c8e08 | 2020-08-23 10:49:46 +0200 | [diff] [blame] | 1236 | } |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1237 | |
| 1238 | /** |
| 1239 | * search_gpt_dp_node() - search gpt device path node |
| 1240 | * |
| 1241 | * @device_path: device path |
| 1242 | * |
| 1243 | * Return: pointer to the gpt device path node |
| 1244 | */ |
| 1245 | struct efi_device_path *search_gpt_dp_node(struct efi_device_path *device_path) |
| 1246 | { |
| 1247 | struct efi_device_path *dp = device_path; |
| 1248 | |
| 1249 | while (dp) { |
| 1250 | if (dp->type == DEVICE_PATH_TYPE_MEDIA_DEVICE && |
| 1251 | dp->sub_type == DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH) { |
| 1252 | struct efi_device_path_hard_drive_path *hd_dp = |
| 1253 | (struct efi_device_path_hard_drive_path *)dp; |
| 1254 | |
| 1255 | if (hd_dp->partmap_type == PART_FORMAT_GPT && |
| 1256 | hd_dp->signature_type == SIG_TYPE_GUID) |
| 1257 | return dp; |
| 1258 | } |
| 1259 | dp = efi_dp_next(dp); |
| 1260 | } |
| 1261 | |
| 1262 | return NULL; |
| 1263 | } |