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