part.c 14 KB

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