part.c 15 KB

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