part.c 14 KB

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