part.c 14 KB

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