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