1 /* 2 * (C) Copyright 2001 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4 * 5 * See file CREDITS for list of people who contributed to this 6 * project. 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License as 10 * published by the Free Software Foundation; either version 2 of 11 * the License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 21 * MA 02111-1307 USA 22 */ 23 24 #include <common.h> 25 #include <command.h> 26 #include <ide.h> 27 #include <malloc.h> 28 #include <part.h> 29 30 #undef PART_DEBUG 31 32 #ifdef PART_DEBUG 33 #define PRINTF(fmt,args...) printf (fmt ,##args) 34 #else 35 #define PRINTF(fmt,args...) 36 #endif 37 38 #if (defined(CONFIG_CMD_IDE) || \ 39 defined(CONFIG_CMD_SATA) || \ 40 defined(CONFIG_CMD_SCSI) || \ 41 defined(CONFIG_CMD_USB) || \ 42 defined(CONFIG_MMC) || \ 43 defined(CONFIG_SYSTEMACE) ) 44 45 struct block_drvr { 46 char *name; 47 block_dev_desc_t* (*get_dev)(int dev); 48 }; 49 50 static const struct block_drvr block_drvr[] = { 51 #if defined(CONFIG_CMD_IDE) 52 { .name = "ide", .get_dev = ide_get_dev, }, 53 #endif 54 #if defined(CONFIG_CMD_SATA) 55 {.name = "sata", .get_dev = sata_get_dev, }, 56 #endif 57 #if defined(CONFIG_CMD_SCSI) 58 { .name = "scsi", .get_dev = scsi_get_dev, }, 59 #endif 60 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE) 61 { .name = "usb", .get_dev = usb_stor_get_dev, }, 62 #endif 63 #if defined(CONFIG_MMC) 64 { .name = "mmc", .get_dev = mmc_get_dev, }, 65 #endif 66 #if defined(CONFIG_SYSTEMACE) 67 { .name = "ace", .get_dev = systemace_get_dev, }, 68 #endif 69 { }, 70 }; 71 72 DECLARE_GLOBAL_DATA_PTR; 73 74 block_dev_desc_t *get_dev(const char *ifname, int dev) 75 { 76 const struct block_drvr *drvr = block_drvr; 77 block_dev_desc_t* (*reloc_get_dev)(int dev); 78 char *name; 79 80 if (!ifname) 81 return NULL; 82 83 name = drvr->name; 84 #ifdef CONFIG_NEEDS_MANUAL_RELOC 85 name += gd->reloc_off; 86 #endif 87 while (drvr->name) { 88 name = drvr->name; 89 reloc_get_dev = drvr->get_dev; 90 #ifdef CONFIG_NEEDS_MANUAL_RELOC 91 name += gd->reloc_off; 92 reloc_get_dev += gd->reloc_off; 93 #endif 94 if (strncmp(ifname, name, strlen(name)) == 0) 95 return reloc_get_dev(dev); 96 drvr++; 97 } 98 return NULL; 99 } 100 #else 101 block_dev_desc_t *get_dev(const char *ifname, int dev) 102 { 103 return NULL; 104 } 105 #endif 106 107 #if (defined(CONFIG_CMD_IDE) || \ 108 defined(CONFIG_CMD_SATA) || \ 109 defined(CONFIG_CMD_SCSI) || \ 110 defined(CONFIG_CMD_USB) || \ 111 defined(CONFIG_MMC) || \ 112 defined(CONFIG_SYSTEMACE) ) 113 114 /* ------------------------------------------------------------------------- */ 115 /* 116 * reports device info to the user 117 */ 118 119 #ifdef CONFIG_LBA48 120 typedef uint64_t lba512_t; 121 #else 122 typedef lbaint_t lba512_t; 123 #endif 124 125 /* 126 * Overflowless variant of (block_count * mul_by / div_by) 127 * when div_by > mul_by 128 */ 129 static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by) 130 { 131 lba512_t bc_quot, bc_rem; 132 133 /* x * m / d == x / d * m + (x % d) * m / d */ 134 bc_quot = block_count / div_by; 135 bc_rem = block_count - div_by * bc_quot; 136 return bc_quot * mul_by + (bc_rem * mul_by) / div_by; 137 } 138 139 void dev_print (block_dev_desc_t *dev_desc) 140 { 141 lba512_t lba512; /* number of blocks if 512bytes block size */ 142 143 if (dev_desc->type == DEV_TYPE_UNKNOWN) { 144 puts ("not available\n"); 145 return; 146 } 147 148 switch (dev_desc->if_type) { 149 case IF_TYPE_SCSI: 150 printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n", 151 dev_desc->target,dev_desc->lun, 152 dev_desc->vendor, 153 dev_desc->product, 154 dev_desc->revision); 155 break; 156 case IF_TYPE_ATAPI: 157 case IF_TYPE_IDE: 158 case IF_TYPE_SATA: 159 printf ("Model: %s Firm: %s Ser#: %s\n", 160 dev_desc->vendor, 161 dev_desc->revision, 162 dev_desc->product); 163 break; 164 case IF_TYPE_SD: 165 case IF_TYPE_MMC: 166 case IF_TYPE_USB: 167 printf ("Vendor: %s Rev: %s Prod: %s\n", 168 dev_desc->vendor, 169 dev_desc->revision, 170 dev_desc->product); 171 break; 172 case IF_TYPE_DOC: 173 puts("device type DOC\n"); 174 return; 175 case IF_TYPE_UNKNOWN: 176 puts("device type unknown\n"); 177 return; 178 default: 179 printf("Unhandled device type: %i\n", dev_desc->if_type); 180 return; 181 } 182 puts (" Type: "); 183 if (dev_desc->removable) 184 puts ("Removable "); 185 switch (dev_desc->type & 0x1F) { 186 case DEV_TYPE_HARDDISK: 187 puts ("Hard Disk"); 188 break; 189 case DEV_TYPE_CDROM: 190 puts ("CD ROM"); 191 break; 192 case DEV_TYPE_OPDISK: 193 puts ("Optical Device"); 194 break; 195 case DEV_TYPE_TAPE: 196 puts ("Tape"); 197 break; 198 default: 199 printf ("# %02X #", dev_desc->type & 0x1F); 200 break; 201 } 202 puts ("\n"); 203 if ((dev_desc->lba * dev_desc->blksz)>0L) { 204 ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem; 205 lbaint_t lba; 206 207 lba = dev_desc->lba; 208 209 lba512 = (lba * (dev_desc->blksz/512)); 210 /* round to 1 digit */ 211 mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */ 212 213 mb_quot = mb / 10; 214 mb_rem = mb - (10 * mb_quot); 215 216 gb = mb / 1024; 217 gb_quot = gb / 10; 218 gb_rem = gb - (10 * gb_quot); 219 #ifdef CONFIG_LBA48 220 if (dev_desc->lba48) 221 printf (" Supports 48-bit addressing\n"); 222 #endif 223 #if defined(CONFIG_SYS_64BIT_LBA) 224 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n", 225 mb_quot, mb_rem, 226 gb_quot, gb_rem, 227 lba, 228 dev_desc->blksz); 229 #else 230 printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n", 231 mb_quot, mb_rem, 232 gb_quot, gb_rem, 233 (ulong)lba, 234 dev_desc->blksz); 235 #endif 236 } else { 237 puts (" Capacity: not available\n"); 238 } 239 } 240 #endif 241 242 #if (defined(CONFIG_CMD_IDE) || \ 243 defined(CONFIG_CMD_SATA) || \ 244 defined(CONFIG_CMD_SCSI) || \ 245 defined(CONFIG_CMD_USB) || \ 246 defined(CONFIG_MMC) || \ 247 defined(CONFIG_SYSTEMACE) ) 248 249 #if defined(CONFIG_MAC_PARTITION) || \ 250 defined(CONFIG_DOS_PARTITION) || \ 251 defined(CONFIG_ISO_PARTITION) || \ 252 defined(CONFIG_AMIGA_PARTITION) || \ 253 defined(CONFIG_EFI_PARTITION) 254 255 void init_part (block_dev_desc_t * dev_desc) 256 { 257 #ifdef CONFIG_ISO_PARTITION 258 if (test_part_iso(dev_desc) == 0) { 259 dev_desc->part_type = PART_TYPE_ISO; 260 return; 261 } 262 #endif 263 264 #ifdef CONFIG_MAC_PARTITION 265 if (test_part_mac(dev_desc) == 0) { 266 dev_desc->part_type = PART_TYPE_MAC; 267 return; 268 } 269 #endif 270 271 /* must be placed before DOS partition detection */ 272 #ifdef CONFIG_EFI_PARTITION 273 if (test_part_efi(dev_desc) == 0) { 274 dev_desc->part_type = PART_TYPE_EFI; 275 return; 276 } 277 #endif 278 279 #ifdef CONFIG_DOS_PARTITION 280 if (test_part_dos(dev_desc) == 0) { 281 dev_desc->part_type = PART_TYPE_DOS; 282 return; 283 } 284 #endif 285 286 #ifdef CONFIG_AMIGA_PARTITION 287 if (test_part_amiga(dev_desc) == 0) { 288 dev_desc->part_type = PART_TYPE_AMIGA; 289 return; 290 } 291 #endif 292 dev_desc->part_type = PART_TYPE_UNKNOWN; 293 } 294 295 296 static void print_part_header (const char *type, block_dev_desc_t * dev_desc) 297 { 298 puts ("\nPartition Map for "); 299 switch (dev_desc->if_type) { 300 case IF_TYPE_IDE: 301 puts ("IDE"); 302 break; 303 case IF_TYPE_SATA: 304 puts ("SATA"); 305 break; 306 case IF_TYPE_SCSI: 307 puts ("SCSI"); 308 break; 309 case IF_TYPE_ATAPI: 310 puts ("ATAPI"); 311 break; 312 case IF_TYPE_USB: 313 puts ("USB"); 314 break; 315 case IF_TYPE_DOC: 316 puts ("DOC"); 317 break; 318 case IF_TYPE_MMC: 319 puts ("MMC"); 320 break; 321 default: 322 puts ("UNKNOWN"); 323 break; 324 } 325 printf (" device %d -- Partition Type: %s\n\n", 326 dev_desc->dev, type); 327 } 328 329 void print_part (block_dev_desc_t * dev_desc) 330 { 331 332 switch (dev_desc->part_type) { 333 #ifdef CONFIG_MAC_PARTITION 334 case PART_TYPE_MAC: 335 PRINTF ("## Testing for valid MAC partition ##\n"); 336 print_part_header ("MAC", dev_desc); 337 print_part_mac (dev_desc); 338 return; 339 #endif 340 #ifdef CONFIG_DOS_PARTITION 341 case PART_TYPE_DOS: 342 PRINTF ("## Testing for valid DOS partition ##\n"); 343 print_part_header ("DOS", dev_desc); 344 print_part_dos (dev_desc); 345 return; 346 #endif 347 348 #ifdef CONFIG_ISO_PARTITION 349 case PART_TYPE_ISO: 350 PRINTF ("## Testing for valid ISO Boot partition ##\n"); 351 print_part_header ("ISO", dev_desc); 352 print_part_iso (dev_desc); 353 return; 354 #endif 355 356 #ifdef CONFIG_AMIGA_PARTITION 357 case PART_TYPE_AMIGA: 358 PRINTF ("## Testing for a valid Amiga partition ##\n"); 359 print_part_header ("AMIGA", dev_desc); 360 print_part_amiga (dev_desc); 361 return; 362 #endif 363 364 #ifdef CONFIG_EFI_PARTITION 365 case PART_TYPE_EFI: 366 PRINTF ("## Testing for valid EFI partition ##\n"); 367 print_part_header ("EFI", dev_desc); 368 print_part_efi (dev_desc); 369 return; 370 #endif 371 } 372 puts ("## Unknown partition table\n"); 373 } 374 375 376 #else /* neither MAC nor DOS nor ISO nor AMIGA nor EFI partition configured */ 377 # error neither CONFIG_MAC_PARTITION nor CONFIG_DOS_PARTITION 378 # error nor CONFIG_ISO_PARTITION nor CONFIG_AMIGA_PARTITION 379 # error nor CONFIG_EFI_PARTITION configured! 380 #endif 381 382 #endif 383 384 int get_partition_info(block_dev_desc_t *dev_desc, int part 385 , disk_partition_t *info) 386 { 387 #if defined(CONFIG_CMD_IDE) || \ 388 defined(CONFIG_CMD_SATA) || \ 389 defined(CONFIG_CMD_SCSI) || \ 390 defined(CONFIG_CMD_USB) || \ 391 defined(CONFIG_MMC) || \ 392 defined(CONFIG_SYSTEMACE) 393 394 switch (dev_desc->part_type) { 395 #ifdef CONFIG_MAC_PARTITION 396 case PART_TYPE_MAC: 397 if (get_partition_info_mac(dev_desc, part, info) == 0) { 398 PRINTF("## Valid MAC partition found ##\n"); 399 return 0; 400 } 401 break; 402 #endif 403 404 #ifdef CONFIG_DOS_PARTITION 405 case PART_TYPE_DOS: 406 if (get_partition_info_dos(dev_desc, part, info) == 0) { 407 PRINTF("## Valid DOS partition found ##\n"); 408 return 0; 409 } 410 break; 411 #endif 412 413 #ifdef CONFIG_ISO_PARTITION 414 case PART_TYPE_ISO: 415 if (get_partition_info_iso(dev_desc, part, info) == 0) { 416 PRINTF("## Valid ISO boot partition found ##\n"); 417 return 0; 418 } 419 break; 420 #endif 421 422 #ifdef CONFIG_AMIGA_PARTITION 423 case PART_TYPE_AMIGA: 424 if (get_partition_info_amiga(dev_desc, part, info) == 0) { 425 PRINTF("## Valid Amiga partition found ##\n"); 426 return 0; 427 } 428 break; 429 #endif 430 431 #ifdef CONFIG_EFI_PARTITION 432 case PART_TYPE_EFI: 433 if (get_partition_info_efi(dev_desc, part, info) == 0) { 434 PRINTF("## Valid EFI partition found ##\n"); 435 return 0; 436 } 437 break; 438 #endif 439 default: 440 break; 441 } 442 #endif 443 444 return -1; 445 } 446 447 int get_device(const char *ifname, const char *dev_str, 448 block_dev_desc_t **dev_desc) 449 { 450 char *ep; 451 int dev; 452 453 dev = simple_strtoul(dev_str, &ep, 16); 454 if (*ep) { 455 printf("** Bad device specification %s %s **\n", 456 ifname, dev_str); 457 return -1; 458 } 459 460 *dev_desc = get_dev(ifname, dev); 461 if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) { 462 printf("** Bad device %s %s **\n", ifname, dev_str); 463 return -1; 464 } 465 466 return dev; 467 } 468 469 #define PART_UNSPECIFIED -2 470 #define PART_AUTO -1 471 #define MAX_SEARCH_PARTITIONS 16 472 int get_device_and_partition(const char *ifname, const char *dev_part_str, 473 block_dev_desc_t **dev_desc, 474 disk_partition_t *info, int allow_whole_dev) 475 { 476 int ret = -1; 477 const char *part_str; 478 char *dup_str = NULL; 479 const char *dev_str; 480 int dev; 481 char *ep; 482 int p; 483 int part; 484 disk_partition_t tmpinfo; 485 486 /* If no dev_part_str, use bootdevice environment variable */ 487 if (!dev_part_str) 488 dev_part_str = getenv("bootdevice"); 489 490 /* If still no dev_part_str, it's an error */ 491 if (!dev_part_str) { 492 printf("** No device specified **\n"); 493 goto cleanup; 494 } 495 496 /* Separate device and partition ID specification */ 497 part_str = strchr(dev_part_str, ':'); 498 if (part_str) { 499 dup_str = strdup(dev_part_str); 500 dup_str[part_str - dev_part_str] = 0; 501 dev_str = dup_str; 502 part_str++; 503 } else { 504 dev_str = dev_part_str; 505 } 506 507 /* Look up the device */ 508 dev = get_device(ifname, dev_str, dev_desc); 509 if (dev < 0) 510 goto cleanup; 511 512 /* Convert partition ID string to number */ 513 if (!part_str || !*part_str) { 514 part = PART_UNSPECIFIED; 515 } else if (!strcmp(part_str, "auto")) { 516 part = PART_AUTO; 517 } else { 518 /* Something specified -> use exactly that */ 519 part = (int)simple_strtoul(part_str, &ep, 16); 520 /* 521 * Less than whole string converted, 522 * or request for whole device, but caller requires partition. 523 */ 524 if (*ep || (part == 0 && !allow_whole_dev)) { 525 printf("** Bad partition specification %s %s **\n", 526 ifname, dev_part_str); 527 goto cleanup; 528 } 529 } 530 531 /* 532 * No partition table on device, 533 * or user requested partition 0 (entire device). 534 */ 535 if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) || 536 (part == 0)) { 537 if (!(*dev_desc)->lba) { 538 printf("** Bad device size - %s %s **\n", ifname, 539 dev_str); 540 goto cleanup; 541 } 542 543 /* 544 * If user specified a partition ID other than 0, 545 * or the calling command only accepts partitions, 546 * it's an error. 547 */ 548 if ((part > 0) || (!allow_whole_dev)) { 549 printf("** No partition table - %s %s **\n", ifname, 550 dev_str); 551 goto cleanup; 552 } 553 554 info->start = 0; 555 info->size = (*dev_desc)->lba; 556 info->blksz = (*dev_desc)->blksz; 557 info->bootable = 0; 558 #ifdef CONFIG_PARTITION_UUIDS 559 info->uuid[0] = 0; 560 #endif 561 562 ret = 0; 563 goto cleanup; 564 } 565 566 /* 567 * Now there's known to be a partition table, 568 * not specifying a partition means to pick partition 1. 569 */ 570 if (part == PART_UNSPECIFIED) 571 part = 1; 572 573 /* 574 * If user didn't specify a partition number, or did specify something 575 * other than "auto", use that partition number directly. 576 */ 577 if (part != PART_AUTO) { 578 ret = get_partition_info(*dev_desc, part, info); 579 if (ret) { 580 printf("** Invalid partition %d **\n", part); 581 goto cleanup; 582 } 583 } else { 584 /* 585 * Find the first bootable partition. 586 * If none are bootable, fall back to the first valid partition. 587 */ 588 part = 0; 589 for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { 590 ret = get_partition_info(*dev_desc, p, info); 591 if (ret) 592 continue; 593 594 /* 595 * First valid partition, or new better partition? 596 * If so, save partition ID. 597 */ 598 if (!part || info->bootable) 599 part = p; 600 601 /* Best possible partition? Stop searching. */ 602 if (info->bootable) 603 break; 604 605 /* 606 * We now need to search further for best possible. 607 * If we what we just queried was the best so far, 608 * save the info since we over-write it next loop. 609 */ 610 if (part == p) 611 tmpinfo = *info; 612 } 613 /* If we found any acceptable partition */ 614 if (part) { 615 /* 616 * If we searched all possible partition IDs, 617 * return the first valid partition we found. 618 */ 619 if (p == MAX_SEARCH_PARTITIONS + 1) 620 *info = tmpinfo; 621 ret = 0; 622 } else { 623 printf("** No valid partitions found **\n"); 624 goto cleanup; 625 } 626 } 627 if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) { 628 printf("** Invalid partition type \"%.32s\"" 629 " (expect \"" BOOT_PART_TYPE "\")\n", 630 info->type); 631 ret = -1; 632 goto cleanup; 633 } 634 635 ret = part; 636 goto cleanup; 637 638 cleanup: 639 free(dup_str); 640 return ret; 641 } 642