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