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