1 /* 2 * (C) Copyright 2001 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 #include <command.h> 10 #include <errno.h> 11 #include <ide.h> 12 #include <malloc.h> 13 #include <part.h> 14 #include <ubifs_uboot.h> 15 16 #undef PART_DEBUG 17 18 #ifdef PART_DEBUG 19 #define PRINTF(fmt,args...) printf (fmt ,##args) 20 #else 21 #define PRINTF(fmt,args...) 22 #endif 23 24 DECLARE_GLOBAL_DATA_PTR; 25 26 #ifdef HAVE_BLOCK_DEVICE 27 static struct part_driver *part_driver_lookup_type(int part_type) 28 { 29 struct part_driver *drv = 30 ll_entry_start(struct part_driver, part_driver); 31 const int n_ents = ll_entry_count(struct part_driver, part_driver); 32 struct part_driver *entry; 33 34 for (entry = drv; entry != drv + n_ents; entry++) { 35 if (part_type == entry->part_type) 36 return entry; 37 } 38 39 /* Not found */ 40 return NULL; 41 } 42 43 static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart) 44 { 45 struct blk_desc *dev_desc; 46 int ret; 47 48 dev_desc = blk_get_devnum_by_typename(ifname, dev); 49 if (!dev_desc) { 50 debug("%s: No device for iface '%s', dev %d\n", __func__, 51 ifname, dev); 52 return NULL; 53 } 54 ret = blk_dselect_hwpart(dev_desc, hwpart); 55 if (ret) { 56 debug("%s: Failed to select h/w partition: err-%d\n", __func__, 57 ret); 58 return NULL; 59 } 60 61 return dev_desc; 62 } 63 64 struct blk_desc *blk_get_dev(const char *ifname, int dev) 65 { 66 return get_dev_hwpart(ifname, dev, 0); 67 } 68 #else 69 struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart) 70 { 71 return NULL; 72 } 73 74 struct blk_desc *blk_get_dev(const char *ifname, int dev) 75 { 76 return NULL; 77 } 78 #endif 79 80 #ifdef HAVE_BLOCK_DEVICE 81 82 /* ------------------------------------------------------------------------- */ 83 /* 84 * reports device info to the user 85 */ 86 87 #ifdef CONFIG_LBA48 88 typedef uint64_t lba512_t; 89 #else 90 typedef lbaint_t lba512_t; 91 #endif 92 93 /* 94 * Overflowless variant of (block_count * mul_by / div_by) 95 * when div_by > mul_by 96 */ 97 static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by) 98 { 99 lba512_t bc_quot, bc_rem; 100 101 /* x * m / d == x / d * m + (x % d) * m / d */ 102 bc_quot = block_count / div_by; 103 bc_rem = block_count - div_by * bc_quot; 104 return bc_quot * mul_by + (bc_rem * mul_by) / div_by; 105 } 106 107 void dev_print (struct blk_desc *dev_desc) 108 { 109 lba512_t lba512; /* number of blocks if 512bytes block size */ 110 111 if (dev_desc->type == DEV_TYPE_UNKNOWN) { 112 puts ("not available\n"); 113 return; 114 } 115 116 switch (dev_desc->if_type) { 117 case IF_TYPE_SCSI: 118 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n", 119 dev_desc->target,dev_desc->lun, 120 dev_desc->vendor, 121 dev_desc->product, 122 dev_desc->revision); 123 break; 124 case IF_TYPE_ATAPI: 125 case IF_TYPE_IDE: 126 case IF_TYPE_SATA: 127 printf ("Model: %s Firm: %s Ser#: %s\n", 128 dev_desc->vendor, 129 dev_desc->revision, 130 dev_desc->product); 131 break; 132 case IF_TYPE_SD: 133 case IF_TYPE_MMC: 134 case IF_TYPE_USB: 135 case IF_TYPE_NVME: 136 case IF_TYPE_RKNAND: 137 printf ("Vendor: %s Rev: %s Prod: %s\n", 138 dev_desc->vendor, 139 dev_desc->revision, 140 dev_desc->product); 141 break; 142 case IF_TYPE_DOC: 143 puts("device type DOC\n"); 144 return; 145 case IF_TYPE_UNKNOWN: 146 puts("device type unknown\n"); 147 return; 148 default: 149 printf("Unhandled device type: %i\n", dev_desc->if_type); 150 return; 151 } 152 puts (" Type: "); 153 if (dev_desc->removable) 154 puts ("Removable "); 155 switch (dev_desc->type & 0x1F) { 156 case DEV_TYPE_HARDDISK: 157 puts ("Hard Disk"); 158 break; 159 case DEV_TYPE_CDROM: 160 puts ("CD ROM"); 161 break; 162 case DEV_TYPE_OPDISK: 163 puts ("Optical Device"); 164 break; 165 case DEV_TYPE_TAPE: 166 puts ("Tape"); 167 break; 168 default: 169 printf ("# %02X #", dev_desc->type & 0x1F); 170 break; 171 } 172 puts ("\n"); 173 if (dev_desc->lba > 0L && dev_desc->blksz > 0L) { 174 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem; 175 lbaint_t lba; 176 177 lba = dev_desc->lba; 178 179 lba512 = (lba * (dev_desc->blksz/512)); 180 /* round to 1 digit */ 181 /* 2048 = (1024 * 1024) / 512 MB */ 182 mb = lba512_muldiv(lba512, 10, 2048); 183 184 mb_quot = mb / 10; 185 mb_rem = mb - (10 * mb_quot); 186 187 gb = mb / 1024; 188 gb_quot = gb / 10; 189 gb_rem = gb - (10 * gb_quot); 190 #ifdef CONFIG_LBA48 191 if (dev_desc->lba48) 192 printf (" Supports 48-bit addressing\n"); 193 #endif 194 #if defined(CONFIG_SYS_64BIT_LBA) 195 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n", 196 mb_quot, mb_rem, 197 gb_quot, gb_rem, 198 lba, 199 dev_desc->blksz); 200 #else 201 printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%lu x %lu)\n", 202 mb_quot, mb_rem, 203 gb_quot, gb_rem, 204 (ulong)lba, 205 dev_desc->blksz); 206 #endif 207 } else { 208 puts (" Capacity: not available\n"); 209 } 210 } 211 #endif 212 213 #ifdef HAVE_BLOCK_DEVICE 214 215 void part_init(struct blk_desc *dev_desc) 216 { 217 struct part_driver *drv = 218 ll_entry_start(struct part_driver, part_driver); 219 const int n_ents = ll_entry_count(struct part_driver, part_driver); 220 struct part_driver *entry; 221 222 blkcache_invalidate(dev_desc->if_type, dev_desc->devnum); 223 224 dev_desc->part_type = PART_TYPE_UNKNOWN; 225 for (entry = drv; entry != drv + n_ents; entry++) { 226 int ret; 227 228 ret = entry->test(dev_desc); 229 debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret); 230 if (!ret) { 231 dev_desc->part_type = entry->part_type; 232 break; 233 } 234 } 235 } 236 237 static void print_part_header(const char *type, struct blk_desc *dev_desc) 238 { 239 #if CONFIG_IS_ENABLED(MAC_PARTITION) || \ 240 CONFIG_IS_ENABLED(DOS_PARTITION) || \ 241 CONFIG_IS_ENABLED(ISO_PARTITION) || \ 242 CONFIG_IS_ENABLED(AMIGA_PARTITION) || \ 243 CONFIG_IS_ENABLED(EFI_PARTITION) 244 puts ("\nPartition Map for "); 245 switch (dev_desc->if_type) { 246 case IF_TYPE_IDE: 247 puts ("IDE"); 248 break; 249 case IF_TYPE_SATA: 250 puts ("SATA"); 251 break; 252 case IF_TYPE_SCSI: 253 puts ("SCSI"); 254 break; 255 case IF_TYPE_ATAPI: 256 puts ("ATAPI"); 257 break; 258 case IF_TYPE_USB: 259 puts ("USB"); 260 break; 261 case IF_TYPE_DOC: 262 puts ("DOC"); 263 break; 264 case IF_TYPE_MMC: 265 puts ("MMC"); 266 break; 267 case IF_TYPE_HOST: 268 puts ("HOST"); 269 break; 270 case IF_TYPE_NVME: 271 puts ("NVMe"); 272 break; 273 case IF_TYPE_RKNAND: 274 puts("RKNAND"); 275 break; 276 default: 277 puts ("UNKNOWN"); 278 break; 279 } 280 printf (" device %d -- Partition Type: %s\n\n", 281 dev_desc->devnum, type); 282 #endif /* any CONFIG_..._PARTITION */ 283 } 284 285 void part_print(struct blk_desc *dev_desc) 286 { 287 struct part_driver *drv; 288 289 drv = part_driver_lookup_type(dev_desc->part_type); 290 if (!drv) { 291 printf("## Unknown partition table type %x\n", 292 dev_desc->part_type); 293 return; 294 } 295 296 PRINTF("## Testing for valid %s partition ##\n", drv->name); 297 print_part_header(drv->name, dev_desc); 298 if (drv->print) 299 drv->print(dev_desc); 300 } 301 302 #endif /* HAVE_BLOCK_DEVICE */ 303 304 int part_get_info(struct blk_desc *dev_desc, int part, 305 disk_partition_t *info) 306 { 307 #ifdef HAVE_BLOCK_DEVICE 308 struct part_driver *drv; 309 310 #if CONFIG_IS_ENABLED(PARTITION_UUIDS) 311 /* The common case is no UUID support */ 312 info->uuid[0] = 0; 313 #endif 314 #ifdef CONFIG_PARTITION_TYPE_GUID 315 info->type_guid[0] = 0; 316 #endif 317 318 drv = part_driver_lookup_type(dev_desc->part_type); 319 if (!drv) { 320 debug("## Unknown partition table type %x\n", 321 dev_desc->part_type); 322 return -EPROTONOSUPPORT; 323 } 324 if (!drv->get_info) { 325 PRINTF("## Driver %s does not have the get_info() method\n", 326 drv->name); 327 return -ENOSYS; 328 } 329 if (drv->get_info(dev_desc, part, info) == 0) { 330 PRINTF("## Valid %s partition found ##\n", drv->name); 331 return 0; 332 } 333 #endif /* HAVE_BLOCK_DEVICE */ 334 335 return -1; 336 } 337 338 int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str, 339 struct blk_desc **dev_desc) 340 { 341 char *ep; 342 char *dup_str = NULL; 343 const char *dev_str, *hwpart_str; 344 int dev, hwpart; 345 346 hwpart_str = strchr(dev_hwpart_str, '.'); 347 if (hwpart_str) { 348 dup_str = strdup(dev_hwpart_str); 349 dup_str[hwpart_str - dev_hwpart_str] = 0; 350 dev_str = dup_str; 351 hwpart_str++; 352 } else { 353 dev_str = dev_hwpart_str; 354 hwpart = 0; 355 } 356 357 dev = simple_strtoul(dev_str, &ep, 16); 358 if (*ep) { 359 printf("** Bad device specification %s %s **\n", 360 ifname, dev_str); 361 dev = -EINVAL; 362 goto cleanup; 363 } 364 365 if (hwpart_str) { 366 hwpart = simple_strtoul(hwpart_str, &ep, 16); 367 if (*ep) { 368 printf("** Bad HW partition specification %s %s **\n", 369 ifname, hwpart_str); 370 dev = -EINVAL; 371 goto cleanup; 372 } 373 } 374 375 *dev_desc = get_dev_hwpart(ifname, dev, hwpart); 376 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) { 377 printf("** Bad device %s %s **\n", ifname, dev_hwpart_str); 378 dev = -ENOENT; 379 goto cleanup; 380 } 381 382 #ifdef HAVE_BLOCK_DEVICE 383 /* 384 * Updates the partition table for the specified hw partition. 385 * Does not need to be done for hwpart 0 since it is default and 386 * already loaded. 387 */ 388 if(hwpart != 0) 389 part_init(*dev_desc); 390 #endif 391 392 cleanup: 393 free(dup_str); 394 return dev; 395 } 396 397 #define PART_UNSPECIFIED -2 398 #define PART_AUTO -1 399 int blk_get_device_part_str(const char *ifname, const char *dev_part_str, 400 struct blk_desc **dev_desc, 401 disk_partition_t *info, int allow_whole_dev) 402 { 403 int ret = -1; 404 const char *part_str; 405 char *dup_str = NULL; 406 const char *dev_str; 407 int dev; 408 char *ep; 409 int p; 410 int part; 411 disk_partition_t tmpinfo; 412 413 #ifdef CONFIG_SANDBOX 414 /* 415 * Special-case a pseudo block device "hostfs", to allow access to the 416 * host's own filesystem. 417 */ 418 if (0 == strcmp(ifname, "hostfs")) { 419 *dev_desc = NULL; 420 info->start = 0; 421 info->size = 0; 422 info->blksz = 0; 423 info->bootable = 0; 424 strcpy((char *)info->type, BOOT_PART_TYPE); 425 strcpy((char *)info->name, "Sandbox host"); 426 #if CONFIG_IS_ENABLED(PARTITION_UUIDS) 427 info->uuid[0] = 0; 428 #endif 429 #ifdef CONFIG_PARTITION_TYPE_GUID 430 info->type_guid[0] = 0; 431 #endif 432 433 return 0; 434 } 435 #endif 436 437 #ifdef CONFIG_CMD_UBIFS 438 /* 439 * Special-case ubi, ubi goes through a mtd, rathen then through 440 * a regular block device. 441 */ 442 if (0 == strcmp(ifname, "ubi")) { 443 if (!ubifs_is_mounted()) { 444 printf("UBIFS not mounted, use ubifsmount to mount volume first!\n"); 445 return -1; 446 } 447 448 *dev_desc = NULL; 449 memset(info, 0, sizeof(*info)); 450 strcpy((char *)info->type, BOOT_PART_TYPE); 451 strcpy((char *)info->name, "UBI"); 452 #if CONFIG_IS_ENABLED(PARTITION_UUIDS) 453 info->uuid[0] = 0; 454 #endif 455 return 0; 456 } 457 #endif 458 459 /* If no dev_part_str, use bootdevice environment variable */ 460 if (!dev_part_str || !strlen(dev_part_str) || 461 !strcmp(dev_part_str, "-")) 462 dev_part_str = env_get("bootdevice"); 463 464 /* If still no dev_part_str, it's an error */ 465 if (!dev_part_str) { 466 printf("** No device specified **\n"); 467 goto cleanup; 468 } 469 470 /* Separate device and partition ID specification */ 471 part_str = strchr(dev_part_str, ':'); 472 if (part_str) { 473 dup_str = strdup(dev_part_str); 474 dup_str[part_str - dev_part_str] = 0; 475 dev_str = dup_str; 476 part_str++; 477 } else { 478 dev_str = dev_part_str; 479 } 480 481 /* Look up the device */ 482 dev = blk_get_device_by_str(ifname, dev_str, dev_desc); 483 if (dev < 0) 484 goto cleanup; 485 486 /* Convert partition ID string to number */ 487 if (!part_str || !*part_str) { 488 part = PART_UNSPECIFIED; 489 } else if (!strcmp(part_str, "auto")) { 490 part = PART_AUTO; 491 } else { 492 /* Something specified -> use exactly that */ 493 part = (int)simple_strtoul(part_str, &ep, 16); 494 /* 495 * Less than whole string converted, 496 * or request for whole device, but caller requires partition. 497 */ 498 if (*ep || (part == 0 && !allow_whole_dev)) { 499 printf("** Bad partition specification %s %s **\n", 500 ifname, dev_part_str); 501 goto cleanup; 502 } 503 } 504 505 /* 506 * No partition table on device, 507 * or user requested partition 0 (entire device). 508 */ 509 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) || 510 (part == 0)) { 511 if (!(*dev_desc)->lba) { 512 printf("** Bad device size - %s %s **\n", ifname, 513 dev_str); 514 goto cleanup; 515 } 516 517 /* 518 * If user specified a partition ID other than 0, 519 * or the calling command only accepts partitions, 520 * it's an error. 521 */ 522 if ((part > 0) || (!allow_whole_dev)) { 523 printf("** No partition table - %s %s **\n", ifname, 524 dev_str); 525 goto cleanup; 526 } 527 528 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz); 529 530 info->start = 0; 531 info->size = (*dev_desc)->lba; 532 info->blksz = (*dev_desc)->blksz; 533 info->bootable = 0; 534 strcpy((char *)info->type, BOOT_PART_TYPE); 535 strcpy((char *)info->name, "Whole Disk"); 536 #if CONFIG_IS_ENABLED(PARTITION_UUIDS) 537 info->uuid[0] = 0; 538 #endif 539 #ifdef CONFIG_PARTITION_TYPE_GUID 540 info->type_guid[0] = 0; 541 #endif 542 543 ret = 0; 544 goto cleanup; 545 } 546 547 /* 548 * Now there's known to be a partition table, 549 * not specifying a partition means to pick partition 1. 550 */ 551 if (part == PART_UNSPECIFIED) 552 part = 1; 553 554 /* 555 * If user didn't specify a partition number, or did specify something 556 * other than "auto", use that partition number directly. 557 */ 558 if (part != PART_AUTO) { 559 ret = part_get_info(*dev_desc, part, info); 560 if (ret) { 561 printf("** Invalid partition %d **\n", part); 562 goto cleanup; 563 } 564 } else { 565 /* 566 * Find the first bootable partition. 567 * If none are bootable, fall back to the first valid partition. 568 */ 569 part = 0; 570 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { 571 ret = part_get_info(*dev_desc, p, info); 572 if (ret) 573 continue; 574 575 /* 576 * First valid partition, or new better partition? 577 * If so, save partition ID. 578 */ 579 if (!part || info->bootable) 580 part = p; 581 582 /* Best possible partition? Stop searching. */ 583 if (info->bootable) 584 break; 585 586 /* 587 * We now need to search further for best possible. 588 * If we what we just queried was the best so far, 589 * save the info since we over-write it next loop. 590 */ 591 if (part == p) 592 tmpinfo = *info; 593 } 594 /* If we found any acceptable partition */ 595 if (part) { 596 /* 597 * If we searched all possible partition IDs, 598 * return the first valid partition we found. 599 */ 600 if (p == MAX_SEARCH_PARTITIONS + 1) 601 *info = tmpinfo; 602 } else { 603 printf("** No valid partitions found **\n"); 604 ret = -1; 605 goto cleanup; 606 } 607 } 608 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) { 609 printf("** Invalid partition type \"%.32s\"" 610 " (expect \"" BOOT_PART_TYPE "\")\n", 611 info->type); 612 ret = -1; 613 goto cleanup; 614 } 615 616 (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz); 617 618 ret = part; 619 goto cleanup; 620 621 cleanup: 622 free(dup_str); 623 return ret; 624 } 625 626 int part_get_info_by_name(struct blk_desc *dev_desc, const char *name, 627 disk_partition_t *info) 628 { 629 struct part_driver *first_drv = 630 ll_entry_start(struct part_driver, part_driver); 631 const int n_drvs = ll_entry_count(struct part_driver, part_driver); 632 struct part_driver *part_drv; 633 634 for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) { 635 int ret; 636 int i; 637 for (i = 1; i < part_drv->max_entries; i++) { 638 ret = part_drv->get_info(dev_desc, i, info); 639 if (ret != 0) { 640 /* no more entries in table */ 641 break; 642 } 643 if (strcmp(name, (const char *)info->name) == 0) { 644 /* matched */ 645 return i; 646 } 647 } 648 } 649 return -1; 650 } 651 652 void part_set_generic_name(const struct blk_desc *dev_desc, 653 int part_num, char *name) 654 { 655 char *devtype; 656 657 switch (dev_desc->if_type) { 658 case IF_TYPE_IDE: 659 case IF_TYPE_SATA: 660 case IF_TYPE_ATAPI: 661 devtype = "hd"; 662 break; 663 case IF_TYPE_SCSI: 664 devtype = "sd"; 665 break; 666 case IF_TYPE_USB: 667 devtype = "usbd"; 668 break; 669 case IF_TYPE_DOC: 670 devtype = "docd"; 671 break; 672 case IF_TYPE_MMC: 673 case IF_TYPE_SD: 674 devtype = "mmcsd"; 675 break; 676 default: 677 devtype = "xx"; 678 break; 679 } 680 681 sprintf(name, "%s%c%d", devtype, 'a' + dev_desc->devnum, part_num); 682 } 683