part.c 15 KB

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