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