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 | |
| 8 | #include <common.h> |
| 9 | #include <blk.h> |
| 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 12 | #include <net.h> |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 13 | #include <usb.h> |
| 14 | #include <mmc.h> |
Patrick Wildt | a3ca37e | 2019-10-03 16:24:17 +0200 | [diff] [blame] | 15 | #include <nvme.h> |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 16 | #include <efi_loader.h> |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 17 | #include <part.h> |
AKASHI Takahiro | 659a626 | 2019-09-12 13:52:35 +0900 | [diff] [blame] | 18 | #include <sandboxblockdev.h> |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 19 | #include <asm-generic/unaligned.h> |
AKASHI Takahiro | f1dbbae | 2019-10-09 16:19:52 +0900 | [diff] [blame] | 20 | #include <linux/compat.h> /* U16_MAX */ |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 21 | |
AKASHI Takahiro | 659a626 | 2019-09-12 13:52:35 +0900 | [diff] [blame] | 22 | #ifdef CONFIG_SANDBOX |
| 23 | const efi_guid_t efi_guid_host_dev = U_BOOT_HOST_DEV_GUID; |
| 24 | #endif |
| 25 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 26 | /* template END node: */ |
| 27 | static const struct efi_device_path END = { |
| 28 | .type = DEVICE_PATH_TYPE_END, |
| 29 | .sub_type = DEVICE_PATH_SUB_TYPE_END, |
| 30 | .length = sizeof(END), |
| 31 | }; |
| 32 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 33 | /* template ROOT node: */ |
| 34 | static const struct efi_device_path_vendor ROOT = { |
| 35 | .dp = { |
| 36 | .type = DEVICE_PATH_TYPE_HARDWARE_DEVICE, |
| 37 | .sub_type = DEVICE_PATH_SUB_TYPE_VENDOR, |
| 38 | .length = sizeof(ROOT), |
| 39 | }, |
| 40 | .guid = U_BOOT_GUID, |
| 41 | }; |
| 42 | |
Heinrich Schuchardt | 7d569db | 2017-12-11 12:56:39 +0100 | [diff] [blame] | 43 | #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC) |
| 44 | /* |
| 45 | * Determine if an MMC device is an SD card. |
| 46 | * |
| 47 | * @desc block device descriptor |
| 48 | * @return true if the device is an SD card |
| 49 | */ |
| 50 | static bool is_sd(struct blk_desc *desc) |
| 51 | { |
| 52 | struct mmc *mmc = find_mmc_device(desc->devnum); |
| 53 | |
| 54 | if (!mmc) |
| 55 | return false; |
| 56 | |
| 57 | return IS_SD(mmc) != 0U; |
| 58 | } |
| 59 | #endif |
| 60 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 61 | static void *dp_alloc(size_t sz) |
| 62 | { |
| 63 | void *buf; |
| 64 | |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 65 | if (efi_allocate_pool(EFI_ALLOCATE_ANY_PAGES, sz, &buf) != |
| 66 | EFI_SUCCESS) { |
| 67 | debug("EFI: ERROR: out of memory in %s\n", __func__); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 68 | return NULL; |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 69 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 70 | |
Patrick Wildt | acf7bee | 2018-03-25 19:54:03 +0200 | [diff] [blame] | 71 | memset(buf, 0, sz); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 72 | return buf; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Iterate to next block in device-path, terminating (returning NULL) |
| 77 | * at /End* node. |
| 78 | */ |
| 79 | struct efi_device_path *efi_dp_next(const struct efi_device_path *dp) |
| 80 | { |
| 81 | if (dp == NULL) |
| 82 | return NULL; |
| 83 | if (dp->type == DEVICE_PATH_TYPE_END) |
| 84 | return NULL; |
| 85 | dp = ((void *)dp) + dp->length; |
| 86 | if (dp->type == DEVICE_PATH_TYPE_END) |
| 87 | return NULL; |
| 88 | return (struct efi_device_path *)dp; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Compare two device-paths, stopping when the shorter of the two hits |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 93 | * 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] | 94 | * representing a device with one representing a file on the device, or |
| 95 | * a device with a parent device. |
| 96 | */ |
Heinrich Schuchardt | 753e248 | 2017-10-26 19:25:48 +0200 | [diff] [blame] | 97 | int efi_dp_match(const struct efi_device_path *a, |
| 98 | const struct efi_device_path *b) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 99 | { |
| 100 | while (1) { |
| 101 | int ret; |
| 102 | |
| 103 | ret = memcmp(&a->length, &b->length, sizeof(a->length)); |
| 104 | if (ret) |
| 105 | return ret; |
| 106 | |
| 107 | ret = memcmp(a, b, a->length); |
| 108 | if (ret) |
| 109 | return ret; |
| 110 | |
| 111 | a = efi_dp_next(a); |
| 112 | b = efi_dp_next(b); |
| 113 | |
| 114 | if (!a || !b) |
| 115 | return 0; |
| 116 | } |
| 117 | } |
| 118 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 119 | /* |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 120 | * We can have device paths that start with a USB WWID or a USB Class node, |
| 121 | * and a few other cases which don't encode the full device path with bus |
| 122 | * hierarchy: |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 123 | * |
| 124 | * - MESSAGING:USB_WWID |
| 125 | * - MESSAGING:USB_CLASS |
| 126 | * - MEDIA:FILE_PATH |
| 127 | * - MEDIA:HARD_DRIVE |
| 128 | * - MESSAGING:URI |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 129 | * |
| 130 | * See UEFI spec (section 3.1.2, about short-form device-paths) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 131 | */ |
| 132 | static struct efi_device_path *shorten_path(struct efi_device_path *dp) |
| 133 | { |
| 134 | while (dp) { |
| 135 | /* |
| 136 | * TODO: Add MESSAGING:USB_WWID and MESSAGING:URI.. |
| 137 | * in practice fallback.efi just uses MEDIA:HARD_DRIVE |
| 138 | * so not sure when we would see these other cases. |
| 139 | */ |
| 140 | if (EFI_DP_TYPE(dp, MESSAGING_DEVICE, MSG_USB_CLASS) || |
| 141 | EFI_DP_TYPE(dp, MEDIA_DEVICE, HARD_DRIVE_PATH) || |
| 142 | EFI_DP_TYPE(dp, MEDIA_DEVICE, FILE_PATH)) |
| 143 | return dp; |
| 144 | |
| 145 | dp = efi_dp_next(dp); |
| 146 | } |
| 147 | |
| 148 | return dp; |
| 149 | } |
| 150 | |
| 151 | static struct efi_object *find_obj(struct efi_device_path *dp, bool short_path, |
| 152 | struct efi_device_path **rem) |
| 153 | { |
| 154 | struct efi_object *efiobj; |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 155 | efi_uintn_t dp_size = efi_dp_instance_size(dp); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 156 | |
| 157 | list_for_each_entry(efiobj, &efi_obj_list, link) { |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 158 | struct efi_handler *handler; |
| 159 | struct efi_device_path *obj_dp; |
| 160 | efi_status_t ret; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 161 | |
Heinrich Schuchardt | adb50d0 | 2018-09-26 05:27:55 +0200 | [diff] [blame] | 162 | ret = efi_search_protocol(efiobj, |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 163 | &efi_guid_device_path, &handler); |
| 164 | if (ret != EFI_SUCCESS) |
| 165 | continue; |
| 166 | obj_dp = handler->protocol_interface; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 167 | |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 168 | do { |
| 169 | if (efi_dp_match(dp, obj_dp) == 0) { |
| 170 | if (rem) { |
Alexander Graf | f9360fb | 2017-12-11 14:29:46 +0100 | [diff] [blame] | 171 | /* |
| 172 | * Allow partial matches, but inform |
| 173 | * the caller. |
| 174 | */ |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 175 | *rem = ((void *)dp) + |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 176 | efi_dp_instance_size(obj_dp); |
Alexander Graf | f9360fb | 2017-12-11 14:29:46 +0100 | [diff] [blame] | 177 | return efiobj; |
| 178 | } else { |
| 179 | /* Only return on exact matches */ |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 180 | if (efi_dp_instance_size(obj_dp) == |
| 181 | dp_size) |
Alexander Graf | f9360fb | 2017-12-11 14:29:46 +0100 | [diff] [blame] | 182 | return efiobj; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 183 | } |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 184 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 185 | |
Heinrich Schuchardt | 6953c10 | 2017-11-26 14:05:16 +0100 | [diff] [blame] | 186 | obj_dp = shorten_path(efi_dp_next(obj_dp)); |
| 187 | } while (short_path && obj_dp); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | return NULL; |
| 191 | } |
| 192 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 193 | /* |
| 194 | * Find an efiobj from device-path, if 'rem' is not NULL, returns the |
| 195 | * remaining part of the device path after the matched object. |
| 196 | */ |
| 197 | struct efi_object *efi_dp_find_obj(struct efi_device_path *dp, |
| 198 | struct efi_device_path **rem) |
| 199 | { |
| 200 | struct efi_object *efiobj; |
| 201 | |
Alexander Graf | f9360fb | 2017-12-11 14:29:46 +0100 | [diff] [blame] | 202 | /* Search for an exact match first */ |
| 203 | efiobj = find_obj(dp, false, NULL); |
| 204 | |
| 205 | /* Then for a fuzzy match */ |
| 206 | if (!efiobj) |
| 207 | efiobj = find_obj(dp, false, rem); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 208 | |
Alexander Graf | f9360fb | 2017-12-11 14:29:46 +0100 | [diff] [blame] | 209 | /* And now for a fuzzy short match */ |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 210 | if (!efiobj) |
| 211 | efiobj = find_obj(dp, true, rem); |
| 212 | |
| 213 | return efiobj; |
| 214 | } |
| 215 | |
Heinrich Schuchardt | 0976f8b | 2018-01-19 20:24:49 +0100 | [diff] [blame] | 216 | /* |
| 217 | * Determine the last device path node that is not the end node. |
| 218 | * |
| 219 | * @dp device path |
| 220 | * @return last node before the end node if it exists |
| 221 | * otherwise NULL |
| 222 | */ |
| 223 | const struct efi_device_path *efi_dp_last_node(const struct efi_device_path *dp) |
| 224 | { |
| 225 | struct efi_device_path *ret; |
| 226 | |
| 227 | if (!dp || dp->type == DEVICE_PATH_TYPE_END) |
| 228 | return NULL; |
| 229 | while (dp) { |
| 230 | ret = (struct efi_device_path *)dp; |
| 231 | dp = efi_dp_next(dp); |
| 232 | } |
| 233 | return ret; |
| 234 | } |
| 235 | |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 236 | /* get size of the first device path instance excluding end node */ |
| 237 | 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] | 238 | { |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 239 | efi_uintn_t sz = 0; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 240 | |
Heinrich Schuchardt | 01d48ed | 2018-04-16 07:59:07 +0200 | [diff] [blame] | 241 | if (!dp || dp->type == DEVICE_PATH_TYPE_END) |
| 242 | return 0; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 243 | while (dp) { |
| 244 | sz += dp->length; |
| 245 | dp = efi_dp_next(dp); |
| 246 | } |
| 247 | |
| 248 | return sz; |
| 249 | } |
| 250 | |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 251 | /* get size of multi-instance device path excluding end node */ |
| 252 | efi_uintn_t efi_dp_size(const struct efi_device_path *dp) |
| 253 | { |
| 254 | const struct efi_device_path *p = dp; |
| 255 | |
| 256 | if (!p) |
| 257 | return 0; |
| 258 | while (p->type != DEVICE_PATH_TYPE_END || |
| 259 | p->sub_type != DEVICE_PATH_SUB_TYPE_END) |
| 260 | p = (void *)p + p->length; |
| 261 | |
| 262 | return (void *)p - (void *)dp; |
| 263 | } |
| 264 | |
| 265 | /* copy multi-instance device path */ |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 266 | struct efi_device_path *efi_dp_dup(const struct efi_device_path *dp) |
| 267 | { |
| 268 | struct efi_device_path *ndp; |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 269 | size_t sz = efi_dp_size(dp) + sizeof(END); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 270 | |
| 271 | if (!dp) |
| 272 | return NULL; |
| 273 | |
| 274 | ndp = dp_alloc(sz); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 275 | if (!ndp) |
| 276 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 277 | memcpy(ndp, dp, sz); |
| 278 | |
| 279 | return ndp; |
| 280 | } |
| 281 | |
| 282 | struct efi_device_path *efi_dp_append(const struct efi_device_path *dp1, |
| 283 | const struct efi_device_path *dp2) |
| 284 | { |
| 285 | struct efi_device_path *ret; |
| 286 | |
Heinrich Schuchardt | 60b5ab2 | 2018-04-16 07:59:06 +0200 | [diff] [blame] | 287 | if (!dp1 && !dp2) { |
| 288 | /* return an end node */ |
| 289 | ret = efi_dp_dup(&END); |
| 290 | } else if (!dp1) { |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 291 | ret = efi_dp_dup(dp2); |
| 292 | } else if (!dp2) { |
| 293 | ret = efi_dp_dup(dp1); |
| 294 | } else { |
| 295 | /* both dp1 and dp2 are non-null */ |
| 296 | unsigned sz1 = efi_dp_size(dp1); |
| 297 | unsigned sz2 = efi_dp_size(dp2); |
| 298 | void *p = dp_alloc(sz1 + sz2 + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 299 | if (!p) |
| 300 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 301 | memcpy(p, dp1, sz1); |
Heinrich Schuchardt | 60b5ab2 | 2018-04-16 07:59:06 +0200 | [diff] [blame] | 302 | /* the end node of the second device path has to be retained */ |
| 303 | memcpy(p + sz1, dp2, sz2 + sizeof(END)); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 304 | ret = p; |
| 305 | } |
| 306 | |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | struct efi_device_path *efi_dp_append_node(const struct efi_device_path *dp, |
| 311 | const struct efi_device_path *node) |
| 312 | { |
| 313 | struct efi_device_path *ret; |
| 314 | |
| 315 | if (!node && !dp) { |
| 316 | ret = efi_dp_dup(&END); |
| 317 | } else if (!node) { |
| 318 | ret = efi_dp_dup(dp); |
| 319 | } else if (!dp) { |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 320 | size_t sz = node->length; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 321 | void *p = dp_alloc(sz + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 322 | if (!p) |
| 323 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 324 | memcpy(p, node, sz); |
| 325 | memcpy(p + sz, &END, sizeof(END)); |
| 326 | ret = p; |
| 327 | } else { |
| 328 | /* both dp and node are non-null */ |
Heinrich Schuchardt | 51ca4f5 | 2018-04-16 07:59:08 +0200 | [diff] [blame] | 329 | size_t sz = efi_dp_size(dp); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 330 | void *p = dp_alloc(sz + node->length + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 331 | if (!p) |
| 332 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 333 | memcpy(p, dp, sz); |
| 334 | memcpy(p + sz, node, node->length); |
| 335 | memcpy(p + sz + node->length, &END, sizeof(END)); |
| 336 | ret = p; |
| 337 | } |
| 338 | |
| 339 | return ret; |
| 340 | } |
| 341 | |
Heinrich Schuchardt | b41c8e2 | 2018-04-16 07:59:05 +0200 | [diff] [blame] | 342 | struct efi_device_path *efi_dp_create_device_node(const u8 type, |
| 343 | const u8 sub_type, |
| 344 | const u16 length) |
| 345 | { |
| 346 | struct efi_device_path *ret; |
| 347 | |
Heinrich Schuchardt | c96c594 | 2019-04-23 00:51:01 +0200 | [diff] [blame] | 348 | if (length < sizeof(struct efi_device_path)) |
| 349 | return NULL; |
| 350 | |
Heinrich Schuchardt | b41c8e2 | 2018-04-16 07:59:05 +0200 | [diff] [blame] | 351 | ret = dp_alloc(length); |
| 352 | if (!ret) |
| 353 | return ret; |
| 354 | ret->type = type; |
| 355 | ret->sub_type = sub_type; |
| 356 | ret->length = length; |
| 357 | return ret; |
| 358 | } |
| 359 | |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 360 | struct efi_device_path *efi_dp_append_instance( |
| 361 | const struct efi_device_path *dp, |
| 362 | const struct efi_device_path *dpi) |
| 363 | { |
| 364 | size_t sz, szi; |
| 365 | struct efi_device_path *p, *ret; |
| 366 | |
| 367 | if (!dpi) |
| 368 | return NULL; |
| 369 | if (!dp) |
| 370 | return efi_dp_dup(dpi); |
| 371 | sz = efi_dp_size(dp); |
| 372 | szi = efi_dp_instance_size(dpi); |
| 373 | p = dp_alloc(sz + szi + 2 * sizeof(END)); |
| 374 | if (!p) |
| 375 | return NULL; |
| 376 | ret = p; |
| 377 | memcpy(p, dp, sz + sizeof(END)); |
| 378 | p = (void *)p + sz; |
| 379 | p->sub_type = DEVICE_PATH_SUB_TYPE_INSTANCE_END; |
| 380 | p = (void *)p + sizeof(END); |
| 381 | memcpy(p, dpi, szi); |
| 382 | p = (void *)p + szi; |
| 383 | memcpy(p, &END, sizeof(END)); |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | struct efi_device_path *efi_dp_get_next_instance(struct efi_device_path **dp, |
| 388 | efi_uintn_t *size) |
| 389 | { |
| 390 | size_t sz; |
| 391 | struct efi_device_path *p; |
| 392 | |
| 393 | if (size) |
| 394 | *size = 0; |
| 395 | if (!dp || !*dp) |
| 396 | return NULL; |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 397 | sz = efi_dp_instance_size(*dp); |
| 398 | p = dp_alloc(sz + sizeof(END)); |
| 399 | if (!p) |
| 400 | return NULL; |
| 401 | memcpy(p, *dp, sz + sizeof(END)); |
| 402 | *dp = (void *)*dp + sz; |
| 403 | if ((*dp)->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END) |
| 404 | *dp = (void *)*dp + sizeof(END); |
| 405 | else |
| 406 | *dp = NULL; |
| 407 | if (size) |
| 408 | *size = sz + sizeof(END); |
| 409 | return p; |
| 410 | } |
| 411 | |
| 412 | bool efi_dp_is_multi_instance(const struct efi_device_path *dp) |
| 413 | { |
| 414 | const struct efi_device_path *p = dp; |
| 415 | |
| 416 | if (!p) |
| 417 | return false; |
| 418 | while (p->type != DEVICE_PATH_TYPE_END) |
| 419 | p = (void *)p + p->length; |
| 420 | return p->sub_type == DEVICE_PATH_SUB_TYPE_INSTANCE_END; |
| 421 | } |
| 422 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 423 | #ifdef CONFIG_DM |
| 424 | /* size of device-path not including END node for device and all parents |
| 425 | * up to the root device. |
| 426 | */ |
Heinrich Schuchardt | c22d9e3 | 2019-11-10 02:16:33 +0100 | [diff] [blame] | 427 | __maybe_unused static unsigned int dp_size(struct udevice *dev) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 428 | { |
| 429 | if (!dev || !dev->driver) |
| 430 | return sizeof(ROOT); |
| 431 | |
| 432 | switch (dev->driver->id) { |
| 433 | case UCLASS_ROOT: |
| 434 | case UCLASS_SIMPLE_BUS: |
| 435 | /* stop traversing parents at this point: */ |
| 436 | return sizeof(ROOT); |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 437 | case UCLASS_ETH: |
| 438 | return dp_size(dev->parent) + |
| 439 | sizeof(struct efi_device_path_mac_addr); |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 440 | #ifdef CONFIG_BLK |
| 441 | case UCLASS_BLK: |
| 442 | switch (dev->parent->uclass->uc_drv->id) { |
| 443 | #ifdef CONFIG_IDE |
| 444 | case UCLASS_IDE: |
| 445 | return dp_size(dev->parent) + |
| 446 | sizeof(struct efi_device_path_atapi); |
| 447 | #endif |
| 448 | #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI) |
| 449 | case UCLASS_SCSI: |
| 450 | return dp_size(dev->parent) + |
| 451 | sizeof(struct efi_device_path_scsi); |
| 452 | #endif |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 453 | #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC) |
| 454 | case UCLASS_MMC: |
| 455 | return dp_size(dev->parent) + |
| 456 | sizeof(struct efi_device_path_sd_mmc_path); |
| 457 | #endif |
Patrick Wildt | a3ca37e | 2019-10-03 16:24:17 +0200 | [diff] [blame] | 458 | #if defined(CONFIG_NVME) |
| 459 | case UCLASS_NVME: |
| 460 | return dp_size(dev->parent) + |
| 461 | sizeof(struct efi_device_path_nvme); |
| 462 | #endif |
AKASHI Takahiro | 659a626 | 2019-09-12 13:52:35 +0900 | [diff] [blame] | 463 | #ifdef CONFIG_SANDBOX |
| 464 | case UCLASS_ROOT: |
| 465 | /* |
| 466 | * Sandbox's host device will be represented |
| 467 | * as vendor device with extra one byte for |
| 468 | * device number |
| 469 | */ |
| 470 | return dp_size(dev->parent) |
| 471 | + sizeof(struct efi_device_path_vendor) + 1; |
| 472 | #endif |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 473 | default: |
| 474 | return dp_size(dev->parent); |
| 475 | } |
| 476 | #endif |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 477 | #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 478 | case UCLASS_MMC: |
| 479 | return dp_size(dev->parent) + |
| 480 | sizeof(struct efi_device_path_sd_mmc_path); |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 481 | #endif |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 482 | case UCLASS_MASS_STORAGE: |
| 483 | case UCLASS_USB_HUB: |
| 484 | return dp_size(dev->parent) + |
| 485 | sizeof(struct efi_device_path_usb_class); |
| 486 | default: |
| 487 | /* just skip over unknown classes: */ |
| 488 | return dp_size(dev->parent); |
| 489 | } |
| 490 | } |
| 491 | |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 492 | /* |
| 493 | * Recursively build a device path. |
| 494 | * |
| 495 | * @buf pointer to the end of the device path |
| 496 | * @dev device |
| 497 | * @return pointer to the end of the device path |
| 498 | */ |
Heinrich Schuchardt | c22d9e3 | 2019-11-10 02:16:33 +0100 | [diff] [blame] | 499 | __maybe_unused static void *dp_fill(void *buf, struct udevice *dev) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 500 | { |
| 501 | if (!dev || !dev->driver) |
| 502 | return buf; |
| 503 | |
| 504 | switch (dev->driver->id) { |
| 505 | case UCLASS_ROOT: |
| 506 | case UCLASS_SIMPLE_BUS: { |
| 507 | /* stop traversing parents at this point: */ |
| 508 | struct efi_device_path_vendor *vdp = buf; |
| 509 | *vdp = ROOT; |
| 510 | return &vdp[1]; |
| 511 | } |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 512 | #ifdef CONFIG_DM_ETH |
| 513 | case UCLASS_ETH: { |
| 514 | struct efi_device_path_mac_addr *dp = |
| 515 | dp_fill(buf, dev->parent); |
| 516 | struct eth_pdata *pdata = dev->platdata; |
| 517 | |
| 518 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 519 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR; |
| 520 | dp->dp.length = sizeof(*dp); |
| 521 | memset(&dp->mac, 0, sizeof(dp->mac)); |
| 522 | /* We only support IPv4 */ |
| 523 | memcpy(&dp->mac, &pdata->enetaddr, ARP_HLEN); |
| 524 | /* Ethernet */ |
| 525 | dp->if_type = 1; |
| 526 | return &dp[1]; |
| 527 | } |
| 528 | #endif |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 529 | #ifdef CONFIG_BLK |
| 530 | case UCLASS_BLK: |
| 531 | switch (dev->parent->uclass->uc_drv->id) { |
AKASHI Takahiro | 659a626 | 2019-09-12 13:52:35 +0900 | [diff] [blame] | 532 | #ifdef CONFIG_SANDBOX |
| 533 | case UCLASS_ROOT: { |
| 534 | /* stop traversing parents at this point: */ |
Heinrich Schuchardt | 1e3beaf | 2020-05-06 01:28:08 +0200 | [diff] [blame] | 535 | struct efi_device_path_vendor *dp; |
AKASHI Takahiro | 659a626 | 2019-09-12 13:52:35 +0900 | [diff] [blame] | 536 | struct blk_desc *desc = dev_get_uclass_platdata(dev); |
| 537 | |
| 538 | dp_fill(buf, dev->parent); |
| 539 | dp = buf; |
| 540 | ++dp; |
| 541 | dp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE; |
| 542 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_VENDOR; |
| 543 | dp->dp.length = sizeof(*dp) + 1; |
| 544 | memcpy(&dp->guid, &efi_guid_host_dev, |
| 545 | sizeof(efi_guid_t)); |
| 546 | dp->vendor_data[0] = desc->devnum; |
| 547 | return &dp->vendor_data[1]; |
| 548 | } |
| 549 | #endif |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 550 | #ifdef CONFIG_IDE |
| 551 | case UCLASS_IDE: { |
| 552 | struct efi_device_path_atapi *dp = |
| 553 | dp_fill(buf, dev->parent); |
| 554 | struct blk_desc *desc = dev_get_uclass_platdata(dev); |
| 555 | |
| 556 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 557 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_ATAPI; |
| 558 | dp->dp.length = sizeof(*dp); |
| 559 | dp->logical_unit_number = desc->devnum; |
| 560 | dp->primary_secondary = IDE_BUS(desc->devnum); |
| 561 | dp->slave_master = desc->devnum % |
| 562 | (CONFIG_SYS_IDE_MAXDEVICE / |
| 563 | CONFIG_SYS_IDE_MAXBUS); |
| 564 | return &dp[1]; |
| 565 | } |
| 566 | #endif |
| 567 | #if defined(CONFIG_SCSI) && defined(CONFIG_DM_SCSI) |
| 568 | case UCLASS_SCSI: { |
| 569 | struct efi_device_path_scsi *dp = |
| 570 | dp_fill(buf, dev->parent); |
| 571 | struct blk_desc *desc = dev_get_uclass_platdata(dev); |
| 572 | |
| 573 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 574 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SCSI; |
| 575 | dp->dp.length = sizeof(*dp); |
| 576 | dp->logical_unit_number = desc->lun; |
| 577 | dp->target_id = desc->target; |
| 578 | return &dp[1]; |
| 579 | } |
| 580 | #endif |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 581 | #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC) |
| 582 | case UCLASS_MMC: { |
| 583 | struct efi_device_path_sd_mmc_path *sddp = |
| 584 | dp_fill(buf, dev->parent); |
| 585 | struct blk_desc *desc = dev_get_uclass_platdata(dev); |
| 586 | |
| 587 | sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 588 | sddp->dp.sub_type = is_sd(desc) ? |
| 589 | DEVICE_PATH_SUB_TYPE_MSG_SD : |
| 590 | DEVICE_PATH_SUB_TYPE_MSG_MMC; |
| 591 | sddp->dp.length = sizeof(*sddp); |
| 592 | sddp->slot_number = dev->seq; |
| 593 | return &sddp[1]; |
| 594 | } |
| 595 | #endif |
Patrick Wildt | a3ca37e | 2019-10-03 16:24:17 +0200 | [diff] [blame] | 596 | #if defined(CONFIG_NVME) |
| 597 | case UCLASS_NVME: { |
| 598 | struct efi_device_path_nvme *dp = |
| 599 | dp_fill(buf, dev->parent); |
| 600 | u32 ns_id; |
| 601 | |
| 602 | dp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 603 | dp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_NVME; |
| 604 | dp->dp.length = sizeof(*dp); |
| 605 | nvme_get_namespace_id(dev, &ns_id, dp->eui64); |
| 606 | memcpy(&dp->ns_id, &ns_id, sizeof(ns_id)); |
| 607 | return &dp[1]; |
| 608 | } |
| 609 | #endif |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 610 | default: |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 611 | debug("%s(%u) %s: unhandled parent class: %s (%u)\n", |
| 612 | __FILE__, __LINE__, __func__, |
| 613 | dev->name, dev->parent->uclass->uc_drv->id); |
Heinrich Schuchardt | e2dcb9a | 2017-12-11 12:56:44 +0100 | [diff] [blame] | 614 | return dp_fill(buf, dev->parent); |
| 615 | } |
| 616 | #endif |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 617 | #if defined(CONFIG_DM_MMC) && defined(CONFIG_MMC) |
| 618 | case UCLASS_MMC: { |
| 619 | struct efi_device_path_sd_mmc_path *sddp = |
| 620 | dp_fill(buf, dev->parent); |
| 621 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 622 | struct blk_desc *desc = mmc_get_blk_desc(mmc); |
| 623 | |
| 624 | sddp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
Heinrich Schuchardt | 7d569db | 2017-12-11 12:56:39 +0100 | [diff] [blame] | 625 | sddp->dp.sub_type = is_sd(desc) ? |
| 626 | DEVICE_PATH_SUB_TYPE_MSG_SD : |
| 627 | DEVICE_PATH_SUB_TYPE_MSG_MMC; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 628 | sddp->dp.length = sizeof(*sddp); |
| 629 | sddp->slot_number = dev->seq; |
| 630 | |
| 631 | return &sddp[1]; |
| 632 | } |
| 633 | #endif |
| 634 | case UCLASS_MASS_STORAGE: |
| 635 | case UCLASS_USB_HUB: { |
| 636 | struct efi_device_path_usb_class *udp = |
| 637 | dp_fill(buf, dev->parent); |
| 638 | struct usb_device *udev = dev_get_parent_priv(dev); |
| 639 | struct usb_device_descriptor *desc = &udev->descriptor; |
| 640 | |
| 641 | udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 642 | udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB_CLASS; |
| 643 | udp->dp.length = sizeof(*udp); |
| 644 | udp->vendor_id = desc->idVendor; |
| 645 | udp->product_id = desc->idProduct; |
| 646 | udp->device_class = desc->bDeviceClass; |
| 647 | udp->device_subclass = desc->bDeviceSubClass; |
| 648 | udp->device_protocol = desc->bDeviceProtocol; |
| 649 | |
| 650 | return &udp[1]; |
| 651 | } |
| 652 | default: |
Heinrich Schuchardt | 1f1d49d | 2018-01-20 21:02:18 +0100 | [diff] [blame] | 653 | debug("%s(%u) %s: unhandled device class: %s (%u)\n", |
| 654 | __FILE__, __LINE__, __func__, |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 655 | dev->name, dev->driver->id); |
| 656 | return dp_fill(buf, dev->parent); |
| 657 | } |
| 658 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 659 | #endif |
| 660 | |
| 661 | static unsigned dp_part_size(struct blk_desc *desc, int part) |
| 662 | { |
| 663 | unsigned dpsize; |
| 664 | |
| 665 | #ifdef CONFIG_BLK |
Heinrich Schuchardt | 3a615c8 | 2017-12-11 12:56:43 +0100 | [diff] [blame] | 666 | { |
| 667 | struct udevice *dev; |
| 668 | int ret = blk_find_device(desc->if_type, desc->devnum, &dev); |
| 669 | |
| 670 | if (ret) |
| 671 | dev = desc->bdev->parent; |
| 672 | dpsize = dp_size(dev); |
| 673 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 674 | #else |
| 675 | dpsize = sizeof(ROOT) + sizeof(struct efi_device_path_usb); |
| 676 | #endif |
| 677 | |
| 678 | if (part == 0) /* the actual disk, not a partition */ |
| 679 | return dpsize; |
| 680 | |
| 681 | if (desc->part_type == PART_TYPE_ISO) |
| 682 | dpsize += sizeof(struct efi_device_path_cdrom_path); |
| 683 | else |
| 684 | dpsize += sizeof(struct efi_device_path_hard_drive_path); |
| 685 | |
| 686 | return dpsize; |
| 687 | } |
| 688 | |
Heinrich Schuchardt | 8983ffd | 2017-12-11 12:56:42 +0100 | [diff] [blame] | 689 | /* |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 690 | * Create a device node for a block device partition. |
Heinrich Schuchardt | 8983ffd | 2017-12-11 12:56:42 +0100 | [diff] [blame] | 691 | * |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 692 | * @buf buffer to which the device path is written |
Heinrich Schuchardt | 8983ffd | 2017-12-11 12:56:42 +0100 | [diff] [blame] | 693 | * @desc block device descriptor |
| 694 | * @part partition number, 0 identifies a block device |
| 695 | */ |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 696 | 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] | 697 | { |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 698 | struct disk_partition info; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 699 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 700 | part_get_info(desc, part, &info); |
| 701 | |
| 702 | if (desc->part_type == PART_TYPE_ISO) { |
| 703 | struct efi_device_path_cdrom_path *cddp = buf; |
| 704 | |
Heinrich Schuchardt | 2aae6db | 2017-12-11 12:56:40 +0100 | [diff] [blame] | 705 | cddp->boot_entry = part; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 706 | cddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; |
| 707 | cddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_CDROM_PATH; |
| 708 | cddp->dp.length = sizeof(*cddp); |
| 709 | cddp->partition_start = info.start; |
Heinrich Schuchardt | 28dfd1a | 2019-09-04 13:56:01 +0200 | [diff] [blame] | 710 | cddp->partition_size = info.size; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 711 | |
| 712 | buf = &cddp[1]; |
| 713 | } else { |
| 714 | struct efi_device_path_hard_drive_path *hddp = buf; |
| 715 | |
| 716 | hddp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; |
| 717 | hddp->dp.sub_type = DEVICE_PATH_SUB_TYPE_HARD_DRIVE_PATH; |
| 718 | hddp->dp.length = sizeof(*hddp); |
Heinrich Schuchardt | 2aae6db | 2017-12-11 12:56:40 +0100 | [diff] [blame] | 719 | hddp->partition_number = part; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 720 | hddp->partition_start = info.start; |
| 721 | hddp->partition_end = info.size; |
| 722 | if (desc->part_type == PART_TYPE_EFI) |
| 723 | hddp->partmap_type = 2; |
| 724 | else |
| 725 | hddp->partmap_type = 1; |
Jonathan Gray | 84b4d70 | 2017-11-22 14:18:59 +1100 | [diff] [blame] | 726 | |
| 727 | switch (desc->sig_type) { |
| 728 | case SIG_TYPE_NONE: |
| 729 | default: |
| 730 | hddp->signature_type = 0; |
| 731 | memset(hddp->partition_signature, 0, |
| 732 | sizeof(hddp->partition_signature)); |
| 733 | break; |
| 734 | case SIG_TYPE_MBR: |
| 735 | hddp->signature_type = 1; |
| 736 | memset(hddp->partition_signature, 0, |
| 737 | sizeof(hddp->partition_signature)); |
| 738 | memcpy(hddp->partition_signature, &desc->mbr_sig, |
| 739 | sizeof(desc->mbr_sig)); |
| 740 | break; |
| 741 | case SIG_TYPE_GUID: |
| 742 | hddp->signature_type = 2; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 743 | memcpy(hddp->partition_signature, &desc->guid_sig, |
| 744 | sizeof(hddp->partition_signature)); |
Jonathan Gray | 84b4d70 | 2017-11-22 14:18:59 +1100 | [diff] [blame] | 745 | break; |
| 746 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 747 | |
| 748 | buf = &hddp[1]; |
| 749 | } |
| 750 | |
| 751 | return buf; |
| 752 | } |
| 753 | |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 754 | /* |
| 755 | * Create a device path for a block device or one of its partitions. |
| 756 | * |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 757 | * @buf buffer to which the device path is written |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 758 | * @desc block device descriptor |
| 759 | * @part partition number, 0 identifies a block device |
| 760 | */ |
| 761 | static void *dp_part_fill(void *buf, struct blk_desc *desc, int part) |
| 762 | { |
| 763 | #ifdef CONFIG_BLK |
| 764 | { |
| 765 | struct udevice *dev; |
| 766 | int ret = blk_find_device(desc->if_type, desc->devnum, &dev); |
| 767 | |
| 768 | if (ret) |
| 769 | dev = desc->bdev->parent; |
| 770 | buf = dp_fill(buf, dev); |
| 771 | } |
| 772 | #else |
| 773 | /* |
| 774 | * We *could* make a more accurate path, by looking at if_type |
| 775 | * and handling all the different cases like we do for non- |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 776 | * legacy (i.e. CONFIG_BLK=y) case. But most important thing |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 777 | * is just to have a unique device-path for if_type+devnum. |
| 778 | * So map things to a fictitious USB device. |
| 779 | */ |
| 780 | struct efi_device_path_usb *udp; |
| 781 | |
| 782 | memcpy(buf, &ROOT, sizeof(ROOT)); |
| 783 | buf += sizeof(ROOT); |
| 784 | |
| 785 | udp = buf; |
| 786 | udp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 787 | udp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_USB; |
| 788 | udp->dp.length = sizeof(*udp); |
| 789 | udp->parent_port_number = desc->if_type; |
| 790 | udp->usb_interface = desc->devnum; |
| 791 | buf = &udp[1]; |
| 792 | #endif |
| 793 | |
| 794 | if (part == 0) /* the actual disk, not a partition */ |
| 795 | return buf; |
| 796 | |
| 797 | return dp_part_node(buf, desc, part); |
| 798 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 799 | |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 800 | /* Construct a device-path from a partition on a block device: */ |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 801 | struct efi_device_path *efi_dp_from_part(struct blk_desc *desc, int part) |
| 802 | { |
| 803 | void *buf, *start; |
| 804 | |
| 805 | start = buf = dp_alloc(dp_part_size(desc, part) + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 806 | if (!buf) |
| 807 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 808 | |
| 809 | buf = dp_part_fill(buf, desc, part); |
| 810 | |
| 811 | *((struct efi_device_path *)buf) = END; |
| 812 | |
| 813 | return start; |
| 814 | } |
| 815 | |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 816 | /* |
| 817 | * Create a device node for a block device partition. |
| 818 | * |
Heinrich Schuchardt | b21f839 | 2018-10-17 21:55:24 +0200 | [diff] [blame] | 819 | * @buf buffer to which the device path is written |
Heinrich Schuchardt | 6c6307c | 2018-01-19 20:24:46 +0100 | [diff] [blame] | 820 | * @desc block device descriptor |
| 821 | * @part partition number, 0 identifies a block device |
| 822 | */ |
| 823 | struct efi_device_path *efi_dp_part_node(struct blk_desc *desc, int part) |
| 824 | { |
| 825 | efi_uintn_t dpsize; |
| 826 | void *buf; |
| 827 | |
| 828 | if (desc->part_type == PART_TYPE_ISO) |
| 829 | dpsize = sizeof(struct efi_device_path_cdrom_path); |
| 830 | else |
| 831 | dpsize = sizeof(struct efi_device_path_hard_drive_path); |
| 832 | buf = dp_alloc(dpsize); |
| 833 | |
| 834 | dp_part_node(buf, desc, part); |
| 835 | |
| 836 | return buf; |
| 837 | } |
| 838 | |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 839 | /** |
| 840 | * path_to_uefi() - convert UTF-8 path to an UEFI style path |
| 841 | * |
| 842 | * Convert UTF-8 path to a UEFI style path (i.e. with backslashes as path |
| 843 | * separators and UTF-16). |
| 844 | * |
| 845 | * @src: source buffer |
| 846 | * @uefi: target buffer, possibly unaligned |
| 847 | */ |
| 848 | static void path_to_uefi(void *uefi, const char *src) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 849 | { |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 850 | u16 *pos = uefi; |
| 851 | |
| 852 | /* |
| 853 | * efi_set_bootdev() calls this routine indirectly before the UEFI |
| 854 | * subsystem is initialized. So we cannot assume unaligned access to be |
| 855 | * enabled. |
| 856 | */ |
| 857 | allow_unaligned(); |
| 858 | |
| 859 | while (*src) { |
| 860 | s32 code = utf8_get(&src); |
| 861 | |
| 862 | if (code < 0) |
| 863 | code = '?'; |
| 864 | else if (code == '/') |
| 865 | code = '\\'; |
| 866 | utf16_put(code, &pos); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 867 | } |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 868 | *pos = 0; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* |
| 872 | * If desc is NULL, this creates a path with only the file component, |
| 873 | * otherwise it creates a full path with both device and file components |
| 874 | */ |
| 875 | struct efi_device_path *efi_dp_from_file(struct blk_desc *desc, int part, |
| 876 | const char *path) |
| 877 | { |
| 878 | struct efi_device_path_file_path *fp; |
| 879 | void *buf, *start; |
AKASHI Takahiro | f1dbbae | 2019-10-09 16:19:52 +0900 | [diff] [blame] | 880 | size_t dpsize = 0, fpsize; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 881 | |
| 882 | if (desc) |
| 883 | dpsize = dp_part_size(desc, part); |
| 884 | |
Heinrich Schuchardt | 8d80af8 | 2019-07-14 19:26:47 +0200 | [diff] [blame] | 885 | fpsize = sizeof(struct efi_device_path) + |
| 886 | 2 * (utf8_utf16_strlen(path) + 1); |
AKASHI Takahiro | f1dbbae | 2019-10-09 16:19:52 +0900 | [diff] [blame] | 887 | if (fpsize > U16_MAX) |
| 888 | return NULL; |
| 889 | |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 890 | dpsize += fpsize; |
| 891 | |
| 892 | start = buf = dp_alloc(dpsize + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 893 | if (!buf) |
| 894 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 895 | |
| 896 | if (desc) |
| 897 | buf = dp_part_fill(buf, desc, part); |
| 898 | |
| 899 | /* add file-path: */ |
| 900 | fp = buf; |
| 901 | fp->dp.type = DEVICE_PATH_TYPE_MEDIA_DEVICE; |
| 902 | fp->dp.sub_type = DEVICE_PATH_SUB_TYPE_FILE_PATH; |
AKASHI Takahiro | f1dbbae | 2019-10-09 16:19:52 +0900 | [diff] [blame] | 903 | fp->dp.length = (u16)fpsize; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 904 | path_to_uefi(fp->str, path); |
| 905 | buf += fpsize; |
| 906 | |
| 907 | *((struct efi_device_path *)buf) = END; |
| 908 | |
| 909 | return start; |
| 910 | } |
| 911 | |
Joe Hershberger | 5277a97 | 2018-04-13 15:26:39 -0500 | [diff] [blame] | 912 | #ifdef CONFIG_NET |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 913 | struct efi_device_path *efi_dp_from_eth(void) |
| 914 | { |
Alexander Graf | c4e983f | 2018-03-15 17:33:38 +0100 | [diff] [blame] | 915 | #ifndef CONFIG_DM_ETH |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 916 | struct efi_device_path_mac_addr *ndp; |
Alexander Graf | c4e983f | 2018-03-15 17:33:38 +0100 | [diff] [blame] | 917 | #endif |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 918 | void *buf, *start; |
| 919 | unsigned dpsize = 0; |
| 920 | |
| 921 | assert(eth_get_dev()); |
| 922 | |
| 923 | #ifdef CONFIG_DM_ETH |
| 924 | dpsize += dp_size(eth_get_dev()); |
| 925 | #else |
| 926 | dpsize += sizeof(ROOT); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 927 | dpsize += sizeof(*ndp); |
Alexander Graf | c4e983f | 2018-03-15 17:33:38 +0100 | [diff] [blame] | 928 | #endif |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 929 | |
| 930 | start = buf = dp_alloc(dpsize + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 931 | if (!buf) |
| 932 | return NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 933 | |
| 934 | #ifdef CONFIG_DM_ETH |
| 935 | buf = dp_fill(buf, eth_get_dev()); |
| 936 | #else |
| 937 | memcpy(buf, &ROOT, sizeof(ROOT)); |
| 938 | buf += sizeof(ROOT); |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 939 | |
| 940 | ndp = buf; |
| 941 | ndp->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 942 | ndp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_MAC_ADDR; |
| 943 | ndp->dp.length = sizeof(*ndp); |
Alexander Graf | c4e983f | 2018-03-15 17:33:38 +0100 | [diff] [blame] | 944 | ndp->if_type = 1; /* Ethernet */ |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 945 | memcpy(ndp->mac.addr, eth_get_ethaddr(), ARP_HLEN); |
| 946 | buf = &ndp[1]; |
Alexander Graf | c4e983f | 2018-03-15 17:33:38 +0100 | [diff] [blame] | 947 | #endif |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 948 | |
| 949 | *((struct efi_device_path *)buf) = END; |
| 950 | |
| 951 | return start; |
| 952 | } |
| 953 | #endif |
| 954 | |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 955 | /* Construct a device-path for memory-mapped image */ |
| 956 | struct efi_device_path *efi_dp_from_mem(uint32_t memory_type, |
| 957 | uint64_t start_address, |
| 958 | uint64_t end_address) |
| 959 | { |
| 960 | struct efi_device_path_memory *mdp; |
| 961 | void *buf, *start; |
| 962 | |
| 963 | start = buf = dp_alloc(sizeof(*mdp) + sizeof(END)); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 964 | if (!buf) |
| 965 | return NULL; |
Rob Clark | 18ceba7 | 2017-10-10 08:23:06 -0400 | [diff] [blame] | 966 | |
| 967 | mdp = buf; |
| 968 | mdp->dp.type = DEVICE_PATH_TYPE_HARDWARE_DEVICE; |
| 969 | mdp->dp.sub_type = DEVICE_PATH_SUB_TYPE_MEMORY; |
| 970 | mdp->dp.length = sizeof(*mdp); |
| 971 | mdp->memory_type = memory_type; |
| 972 | mdp->start_address = start_address; |
| 973 | mdp->end_address = end_address; |
| 974 | buf = &mdp[1]; |
| 975 | |
| 976 | *((struct efi_device_path *)buf) = END; |
| 977 | |
| 978 | return start; |
| 979 | } |
| 980 | |
Heinrich Schuchardt | 0d36adc | 2019-02-04 12:49:43 +0100 | [diff] [blame] | 981 | /** |
| 982 | * efi_dp_split_file_path() - split of relative file path from device path |
| 983 | * |
| 984 | * Given a device path indicating a file on a device, separate the device |
| 985 | * path in two: the device path of the actual device and the file path |
| 986 | * relative to this device. |
| 987 | * |
| 988 | * @full_path: device path including device and file path |
| 989 | * @device_path: path of the device |
AKASHI Takahiro | 9c6531f | 2019-04-16 17:39:26 +0200 | [diff] [blame] | 990 | * @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] | 991 | * Return: status code |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 992 | */ |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 993 | efi_status_t efi_dp_split_file_path(struct efi_device_path *full_path, |
| 994 | struct efi_device_path **device_path, |
| 995 | struct efi_device_path **file_path) |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 996 | { |
AKASHI Takahiro | 9c6531f | 2019-04-16 17:39:26 +0200 | [diff] [blame] | 997 | struct efi_device_path *p, *dp, *fp = NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 998 | |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 999 | *device_path = NULL; |
| 1000 | *file_path = NULL; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1001 | dp = efi_dp_dup(full_path); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1002 | if (!dp) |
| 1003 | return EFI_OUT_OF_RESOURCES; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1004 | p = dp; |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1005 | while (!EFI_DP_TYPE(p, MEDIA_DEVICE, FILE_PATH)) { |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1006 | p = efi_dp_next(p); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1007 | if (!p) |
AKASHI Takahiro | 9c6531f | 2019-04-16 17:39:26 +0200 | [diff] [blame] | 1008 | goto out; |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1009 | } |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1010 | fp = efi_dp_dup(p); |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1011 | if (!fp) |
| 1012 | return EFI_OUT_OF_RESOURCES; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1013 | p->type = DEVICE_PATH_TYPE_END; |
| 1014 | p->sub_type = DEVICE_PATH_SUB_TYPE_END; |
| 1015 | p->length = sizeof(*p); |
| 1016 | |
AKASHI Takahiro | 9c6531f | 2019-04-16 17:39:26 +0200 | [diff] [blame] | 1017 | out: |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1018 | *device_path = dp; |
| 1019 | *file_path = fp; |
Heinrich Schuchardt | 468bf97 | 2018-01-19 20:24:37 +0100 | [diff] [blame] | 1020 | return EFI_SUCCESS; |
Rob Clark | f90cb9f | 2017-09-13 18:05:28 -0400 | [diff] [blame] | 1021 | } |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1022 | |
Heinrich Schuchardt | 6e0a1b4 | 2019-10-30 20:13:24 +0100 | [diff] [blame] | 1023 | /** |
| 1024 | * efi_dp_from_name() - convert U-Boot device and file path to device path |
| 1025 | * |
| 1026 | * @dev: U-Boot device, e.g. 'mmc' |
| 1027 | * @devnr: U-Boot device number, e.g. 1 for 'mmc:1' |
| 1028 | * @path: file path relative to U-Boot device, may be NULL |
| 1029 | * @device: pointer to receive device path of the device |
| 1030 | * @file: pointer to receive device path for the file |
| 1031 | * Return: status code |
| 1032 | */ |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1033 | efi_status_t efi_dp_from_name(const char *dev, const char *devnr, |
| 1034 | const char *path, |
| 1035 | struct efi_device_path **device, |
| 1036 | struct efi_device_path **file) |
| 1037 | { |
| 1038 | int is_net; |
| 1039 | struct blk_desc *desc = NULL; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 1040 | struct disk_partition fs_partition; |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1041 | int part = 0; |
| 1042 | char filename[32] = { 0 }; /* dp->str is u16[32] long */ |
| 1043 | char *s; |
| 1044 | |
AKASHI Takahiro | 3984441 | 2018-11-05 18:06:40 +0900 | [diff] [blame] | 1045 | if (path && !file) |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1046 | return EFI_INVALID_PARAMETER; |
| 1047 | |
| 1048 | is_net = !strcmp(dev, "Net"); |
| 1049 | if (!is_net) { |
| 1050 | part = blk_get_device_part_str(dev, devnr, &desc, &fs_partition, |
| 1051 | 1); |
Patrick Delaunay | ba7a950 | 2019-04-10 11:02:58 +0200 | [diff] [blame] | 1052 | if (part < 0 || !desc) |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1053 | return EFI_INVALID_PARAMETER; |
| 1054 | |
AKASHI Takahiro | 3984441 | 2018-11-05 18:06:40 +0900 | [diff] [blame] | 1055 | if (device) |
| 1056 | *device = efi_dp_from_part(desc, part); |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1057 | } else { |
| 1058 | #ifdef CONFIG_NET |
AKASHI Takahiro | 3984441 | 2018-11-05 18:06:40 +0900 | [diff] [blame] | 1059 | if (device) |
| 1060 | *device = efi_dp_from_eth(); |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1061 | #endif |
| 1062 | } |
| 1063 | |
| 1064 | if (!path) |
| 1065 | return EFI_SUCCESS; |
| 1066 | |
Heinrich Schuchardt | 7d69807 | 2019-02-23 11:20:23 +0100 | [diff] [blame] | 1067 | snprintf(filename, sizeof(filename), "%s", path); |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1068 | /* DOS style file path: */ |
| 1069 | s = filename; |
| 1070 | while ((s = strchr(s, '/'))) |
| 1071 | *s++ = '\\'; |
Heinrich Schuchardt | 6e0a1b4 | 2019-10-30 20:13:24 +0100 | [diff] [blame] | 1072 | *file = efi_dp_from_file(is_net ? NULL : desc, part, filename); |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1073 | |
Heinrich Schuchardt | 6e0a1b4 | 2019-10-30 20:13:24 +0100 | [diff] [blame] | 1074 | if (!*file) |
AKASHI Takahiro | f1dbbae | 2019-10-09 16:19:52 +0900 | [diff] [blame] | 1075 | return EFI_INVALID_PARAMETER; |
| 1076 | |
AKASHI Takahiro | 035fb01 | 2018-10-17 16:32:03 +0900 | [diff] [blame] | 1077 | return EFI_SUCCESS; |
| 1078 | } |