part.c 14 KB

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