part.c 15 KB

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