part.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. DECLARE_GLOBAL_DATA_PTR;
  21. #ifdef HAVE_BLOCK_DEVICE
  22. static struct part_driver *part_driver_lookup_type(int part_type)
  23. {
  24. struct part_driver *drv =
  25. ll_entry_start(struct part_driver, part_driver);
  26. const int n_ents = ll_entry_count(struct part_driver, part_driver);
  27. struct part_driver *entry;
  28. for (entry = drv; entry != drv + n_ents; entry++) {
  29. if (part_type == entry->part_type)
  30. return entry;
  31. }
  32. /* Not found */
  33. return NULL;
  34. }
  35. static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
  36. {
  37. struct blk_desc *dev_desc;
  38. int ret;
  39. dev_desc = blk_get_devnum_by_typename(ifname, dev);
  40. if (!dev_desc) {
  41. debug("%s: No device for iface '%s', dev %d\n", __func__,
  42. ifname, dev);
  43. return NULL;
  44. }
  45. ret = blk_dselect_hwpart(dev_desc, hwpart);
  46. if (ret) {
  47. debug("%s: Failed to select h/w partition: err-%d\n", __func__,
  48. ret);
  49. return NULL;
  50. }
  51. return dev_desc;
  52. }
  53. struct blk_desc *blk_get_dev(const char *ifname, int dev)
  54. {
  55. return get_dev_hwpart(ifname, dev, 0);
  56. }
  57. #else
  58. struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
  59. {
  60. return NULL;
  61. }
  62. struct blk_desc *blk_get_dev(const char *ifname, int dev)
  63. {
  64. return NULL;
  65. }
  66. #endif
  67. #ifdef HAVE_BLOCK_DEVICE
  68. /* ------------------------------------------------------------------------- */
  69. /*
  70. * reports device info to the user
  71. */
  72. #ifdef CONFIG_LBA48
  73. typedef uint64_t lba512_t;
  74. #else
  75. typedef lbaint_t lba512_t;
  76. #endif
  77. /*
  78. * Overflowless variant of (block_count * mul_by / div_by)
  79. * when div_by > mul_by
  80. */
  81. static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by)
  82. {
  83. lba512_t bc_quot, bc_rem;
  84. /* x * m / d == x / d * m + (x % d) * m / d */
  85. bc_quot = block_count / div_by;
  86. bc_rem = block_count - div_by * bc_quot;
  87. return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
  88. }
  89. void dev_print (struct blk_desc *dev_desc)
  90. {
  91. lba512_t lba512; /* number of blocks if 512bytes block size */
  92. if (dev_desc->type == DEV_TYPE_UNKNOWN) {
  93. puts ("not available\n");
  94. return;
  95. }
  96. switch (dev_desc->if_type) {
  97. case IF_TYPE_SCSI:
  98. printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
  99. dev_desc->target,dev_desc->lun,
  100. dev_desc->vendor,
  101. dev_desc->product,
  102. dev_desc->revision);
  103. break;
  104. case IF_TYPE_ATAPI:
  105. case IF_TYPE_IDE:
  106. case IF_TYPE_SATA:
  107. printf ("Model: %s Firm: %s Ser#: %s\n",
  108. dev_desc->vendor,
  109. dev_desc->revision,
  110. dev_desc->product);
  111. break;
  112. case IF_TYPE_SD:
  113. case IF_TYPE_MMC:
  114. case IF_TYPE_USB:
  115. printf ("Vendor: %s Rev: %s Prod: %s\n",
  116. dev_desc->vendor,
  117. dev_desc->revision,
  118. dev_desc->product);
  119. break;
  120. case IF_TYPE_DOC:
  121. puts("device type DOC\n");
  122. return;
  123. case IF_TYPE_UNKNOWN:
  124. puts("device type unknown\n");
  125. return;
  126. default:
  127. printf("Unhandled device type: %i\n", dev_desc->if_type);
  128. return;
  129. }
  130. puts (" Type: ");
  131. if (dev_desc->removable)
  132. puts ("Removable ");
  133. switch (dev_desc->type & 0x1F) {
  134. case DEV_TYPE_HARDDISK:
  135. puts ("Hard Disk");
  136. break;
  137. case DEV_TYPE_CDROM:
  138. puts ("CD ROM");
  139. break;
  140. case DEV_TYPE_OPDISK:
  141. puts ("Optical Device");
  142. break;
  143. case DEV_TYPE_TAPE:
  144. puts ("Tape");
  145. break;
  146. default:
  147. printf ("# %02X #", dev_desc->type & 0x1F);
  148. break;
  149. }
  150. puts ("\n");
  151. if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
  152. ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
  153. lbaint_t lba;
  154. lba = dev_desc->lba;
  155. lba512 = (lba * (dev_desc->blksz/512));
  156. /* round to 1 digit */
  157. /* 2048 = (1024 * 1024) / 512 MB */
  158. mb = lba512_muldiv(lba512, 10, 2048);
  159. mb_quot = mb / 10;
  160. mb_rem = mb - (10 * mb_quot);
  161. gb = mb / 1024;
  162. gb_quot = gb / 10;
  163. gb_rem = gb - (10 * gb_quot);
  164. #ifdef CONFIG_LBA48
  165. if (dev_desc->lba48)
  166. printf (" Supports 48-bit addressing\n");
  167. #endif
  168. #if defined(CONFIG_SYS_64BIT_LBA)
  169. printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
  170. mb_quot, mb_rem,
  171. gb_quot, gb_rem,
  172. lba,
  173. dev_desc->blksz);
  174. #else
  175. printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
  176. mb_quot, mb_rem,
  177. gb_quot, gb_rem,
  178. (ulong)lba,
  179. dev_desc->blksz);
  180. #endif
  181. } else {
  182. puts (" Capacity: not available\n");
  183. }
  184. }
  185. #endif
  186. #ifdef HAVE_BLOCK_DEVICE
  187. void part_init(struct blk_desc *dev_desc)
  188. {
  189. struct part_driver *drv =
  190. ll_entry_start(struct part_driver, part_driver);
  191. const int n_ents = ll_entry_count(struct part_driver, part_driver);
  192. struct part_driver *entry;
  193. blkcache_invalidate(dev_desc->if_type, dev_desc->devnum);
  194. dev_desc->part_type = PART_TYPE_UNKNOWN;
  195. for (entry = drv; entry != drv + n_ents; entry++) {
  196. int ret;
  197. ret = entry->test(dev_desc);
  198. debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
  199. if (!ret) {
  200. dev_desc->part_type = entry->part_type;
  201. break;
  202. }
  203. }
  204. }
  205. static void print_part_header(const char *type, struct blk_desc *dev_desc)
  206. {
  207. #if defined(CONFIG_MAC_PARTITION) || \
  208. defined(CONFIG_DOS_PARTITION) || \
  209. defined(CONFIG_ISO_PARTITION) || \
  210. defined(CONFIG_AMIGA_PARTITION) || \
  211. defined(CONFIG_EFI_PARTITION)
  212. puts ("\nPartition Map for ");
  213. switch (dev_desc->if_type) {
  214. case IF_TYPE_IDE:
  215. puts ("IDE");
  216. break;
  217. case IF_TYPE_SATA:
  218. puts ("SATA");
  219. break;
  220. case IF_TYPE_SCSI:
  221. puts ("SCSI");
  222. break;
  223. case IF_TYPE_ATAPI:
  224. puts ("ATAPI");
  225. break;
  226. case IF_TYPE_USB:
  227. puts ("USB");
  228. break;
  229. case IF_TYPE_DOC:
  230. puts ("DOC");
  231. break;
  232. case IF_TYPE_MMC:
  233. puts ("MMC");
  234. break;
  235. case IF_TYPE_HOST:
  236. puts("HOST");
  237. break;
  238. default:
  239. puts ("UNKNOWN");
  240. break;
  241. }
  242. printf (" device %d -- Partition Type: %s\n\n",
  243. dev_desc->devnum, type);
  244. #endif /* any CONFIG_..._PARTITION */
  245. }
  246. void part_print(struct blk_desc *dev_desc)
  247. {
  248. struct part_driver *drv;
  249. drv = part_driver_lookup_type(dev_desc->part_type);
  250. if (!drv) {
  251. printf("## Unknown partition table type %x\n",
  252. dev_desc->part_type);
  253. return;
  254. }
  255. PRINTF("## Testing for valid %s partition ##\n", drv->name);
  256. print_part_header(drv->name, dev_desc);
  257. if (drv->print)
  258. drv->print(dev_desc);
  259. }
  260. #endif /* HAVE_BLOCK_DEVICE */
  261. int part_get_info(struct blk_desc *dev_desc, int part,
  262. disk_partition_t *info)
  263. {
  264. #ifdef HAVE_BLOCK_DEVICE
  265. struct part_driver *drv;
  266. #ifdef CONFIG_PARTITION_UUIDS
  267. /* The common case is no UUID support */
  268. info->uuid[0] = 0;
  269. #endif
  270. #ifdef CONFIG_PARTITION_TYPE_GUID
  271. info->type_guid[0] = 0;
  272. #endif
  273. drv = part_driver_lookup_type(dev_desc->part_type);
  274. if (!drv) {
  275. debug("## Unknown partition table type %x\n",
  276. dev_desc->part_type);
  277. return -EPROTONOSUPPORT;
  278. }
  279. if (!drv->get_info) {
  280. PRINTF("## Driver %s does not have the get_info() method\n",
  281. drv->name);
  282. return -ENOSYS;
  283. }
  284. if (drv->get_info(dev_desc, part, info) == 0) {
  285. PRINTF("## Valid %s partition found ##\n", drv->name);
  286. return 0;
  287. }
  288. #endif /* HAVE_BLOCK_DEVICE */
  289. return -1;
  290. }
  291. int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
  292. struct blk_desc **dev_desc)
  293. {
  294. char *ep;
  295. char *dup_str = NULL;
  296. const char *dev_str, *hwpart_str;
  297. int dev, hwpart;
  298. hwpart_str = strchr(dev_hwpart_str, '.');
  299. if (hwpart_str) {
  300. dup_str = strdup(dev_hwpart_str);
  301. dup_str[hwpart_str - dev_hwpart_str] = 0;
  302. dev_str = dup_str;
  303. hwpart_str++;
  304. } else {
  305. dev_str = dev_hwpart_str;
  306. hwpart = 0;
  307. }
  308. dev = simple_strtoul(dev_str, &ep, 16);
  309. if (*ep) {
  310. printf("** Bad device specification %s %s **\n",
  311. ifname, dev_str);
  312. dev = -EINVAL;
  313. goto cleanup;
  314. }
  315. if (hwpart_str) {
  316. hwpart = simple_strtoul(hwpart_str, &ep, 16);
  317. if (*ep) {
  318. printf("** Bad HW partition specification %s %s **\n",
  319. ifname, hwpart_str);
  320. dev = -EINVAL;
  321. goto cleanup;
  322. }
  323. }
  324. *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
  325. if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
  326. printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
  327. dev = -ENOENT;
  328. goto cleanup;
  329. }
  330. #ifdef HAVE_BLOCK_DEVICE
  331. /*
  332. * Updates the partition table for the specified hw partition.
  333. * Does not need to be done for hwpart 0 since it is default and
  334. * already loaded.
  335. */
  336. if(hwpart != 0)
  337. part_init(*dev_desc);
  338. #endif
  339. cleanup:
  340. free(dup_str);
  341. return dev;
  342. }
  343. #define PART_UNSPECIFIED -2
  344. #define PART_AUTO -1
  345. #define MAX_SEARCH_PARTITIONS 16
  346. int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
  347. struct blk_desc **dev_desc,
  348. disk_partition_t *info, int allow_whole_dev)
  349. {
  350. int ret = -1;
  351. const char *part_str;
  352. char *dup_str = NULL;
  353. const char *dev_str;
  354. int dev;
  355. char *ep;
  356. int p;
  357. int part;
  358. disk_partition_t tmpinfo;
  359. #ifdef CONFIG_SANDBOX
  360. /*
  361. * Special-case a pseudo block device "hostfs", to allow access to the
  362. * host's own filesystem.
  363. */
  364. if (0 == strcmp(ifname, "hostfs")) {
  365. *dev_desc = NULL;
  366. info->start = 0;
  367. info->size = 0;
  368. info->blksz = 0;
  369. info->bootable = 0;
  370. strcpy((char *)info->type, BOOT_PART_TYPE);
  371. strcpy((char *)info->name, "Sandbox host");
  372. #ifdef CONFIG_PARTITION_UUIDS
  373. info->uuid[0] = 0;
  374. #endif
  375. #ifdef CONFIG_PARTITION_TYPE_GUID
  376. info->type_guid[0] = 0;
  377. #endif
  378. return 0;
  379. }
  380. #endif
  381. #ifdef CONFIG_CMD_UBIFS
  382. /*
  383. * Special-case ubi, ubi goes through a mtd, rathen then through
  384. * a regular block device.
  385. */
  386. if (0 == strcmp(ifname, "ubi")) {
  387. if (!ubifs_is_mounted()) {
  388. printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
  389. return -1;
  390. }
  391. *dev_desc = NULL;
  392. memset(info, 0, sizeof(*info));
  393. strcpy((char *)info->type, BOOT_PART_TYPE);
  394. strcpy((char *)info->name, "UBI");
  395. #ifdef CONFIG_PARTITION_UUIDS
  396. info->uuid[0] = 0;
  397. #endif
  398. return 0;
  399. }
  400. #endif
  401. /* If no dev_part_str, use bootdevice environment variable */
  402. if (!dev_part_str || !strlen(dev_part_str) ||
  403. !strcmp(dev_part_str, "-"))
  404. dev_part_str = getenv("bootdevice");
  405. /* If still no dev_part_str, it's an error */
  406. if (!dev_part_str) {
  407. printf("** No device specified **\n");
  408. goto cleanup;
  409. }
  410. /* Separate device and partition ID specification */
  411. part_str = strchr(dev_part_str, ':');
  412. if (part_str) {
  413. dup_str = strdup(dev_part_str);
  414. dup_str[part_str - dev_part_str] = 0;
  415. dev_str = dup_str;
  416. part_str++;
  417. } else {
  418. dev_str = dev_part_str;
  419. }
  420. /* Look up the device */
  421. dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
  422. if (dev < 0)
  423. goto cleanup;
  424. /* Convert partition ID string to number */
  425. if (!part_str || !*part_str) {
  426. part = PART_UNSPECIFIED;
  427. } else if (!strcmp(part_str, "auto")) {
  428. part = PART_AUTO;
  429. } else {
  430. /* Something specified -> use exactly that */
  431. part = (int)simple_strtoul(part_str, &ep, 16);
  432. /*
  433. * Less than whole string converted,
  434. * or request for whole device, but caller requires partition.
  435. */
  436. if (*ep || (part == 0 && !allow_whole_dev)) {
  437. printf("** Bad partition specification %s %s **\n",
  438. ifname, dev_part_str);
  439. goto cleanup;
  440. }
  441. }
  442. /*
  443. * No partition table on device,
  444. * or user requested partition 0 (entire device).
  445. */
  446. if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
  447. (part == 0)) {
  448. if (!(*dev_desc)->lba) {
  449. printf("** Bad device size - %s %s **\n", ifname,
  450. dev_str);
  451. goto cleanup;
  452. }
  453. /*
  454. * If user specified a partition ID other than 0,
  455. * or the calling command only accepts partitions,
  456. * it's an error.
  457. */
  458. if ((part > 0) || (!allow_whole_dev)) {
  459. printf("** No partition table - %s %s **\n", ifname,
  460. dev_str);
  461. goto cleanup;
  462. }
  463. (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
  464. info->start = 0;
  465. info->size = (*dev_desc)->lba;
  466. info->blksz = (*dev_desc)->blksz;
  467. info->bootable = 0;
  468. strcpy((char *)info->type, BOOT_PART_TYPE);
  469. strcpy((char *)info->name, "Whole Disk");
  470. #ifdef CONFIG_PARTITION_UUIDS
  471. info->uuid[0] = 0;
  472. #endif
  473. #ifdef CONFIG_PARTITION_TYPE_GUID
  474. info->type_guid[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 = part_get_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 = part_get_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. }
  550. int part_get_info_by_name(struct blk_desc *dev_desc, const char *name,
  551. disk_partition_t *info)
  552. {
  553. struct part_driver *first_drv =
  554. ll_entry_start(struct part_driver, part_driver);
  555. const int n_drvs = ll_entry_count(struct part_driver, part_driver);
  556. struct part_driver *part_drv;
  557. for (part_drv = first_drv; part_drv != first_drv + n_drvs; part_drv++) {
  558. int ret;
  559. int i;
  560. for (i = 1; i < part_drv->max_entries; i++) {
  561. ret = part_drv->get_info(dev_desc, i, info);
  562. if (ret != 0) {
  563. /* no more entries in table */
  564. break;
  565. }
  566. if (strcmp(name, (const char *)info->name) == 0) {
  567. /* matched */
  568. return 0;
  569. }
  570. }
  571. }
  572. return -1;
  573. }