part.c 15 KB

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