part.c 13 KB

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