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