part.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2001
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #include <common.h>
  7. #include <blk.h>
  8. #include <command.h>
  9. #include <env.h>
  10. #include <errno.h>
  11. #include <ide.h>
  12. #include <log.h>
  13. #include <malloc.h>
  14. #include <part.h>
  15. #include <ubifs_uboot.h>
  16. #undef PART_DEBUG
  17. #ifdef PART_DEBUG
  18. #define PRINTF(fmt,args...) printf (fmt ,##args)
  19. #else
  20. #define PRINTF(fmt,args...)
  21. #endif
  22. /* Check all partition types */
  23. #define PART_TYPE_ALL -1
  24. static struct part_driver *part_driver_lookup_type(struct blk_desc *dev_desc)
  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. if (dev_desc->part_type == PART_TYPE_UNKNOWN) {
  31. for (entry = drv; entry != drv + n_ents; entry++) {
  32. int ret;
  33. ret = entry->test(dev_desc);
  34. if (!ret) {
  35. dev_desc->part_type = entry->part_type;
  36. return entry;
  37. }
  38. }
  39. } else {
  40. for (entry = drv; entry != drv + n_ents; entry++) {
  41. if (dev_desc->part_type == entry->part_type)
  42. return entry;
  43. }
  44. }
  45. /* Not found */
  46. return NULL;
  47. }
  48. #ifdef CONFIG_HAVE_BLOCK_DEVICE
  49. static struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
  50. {
  51. struct blk_desc *dev_desc;
  52. int ret;
  53. dev_desc = blk_get_devnum_by_typename(ifname, dev);
  54. if (!dev_desc) {
  55. debug("%s: No device for iface '%s', dev %d\n", __func__,
  56. ifname, dev);
  57. return NULL;
  58. }
  59. ret = blk_dselect_hwpart(dev_desc, hwpart);
  60. if (ret) {
  61. debug("%s: Failed to select h/w partition: err-%d\n", __func__,
  62. ret);
  63. return NULL;
  64. }
  65. return dev_desc;
  66. }
  67. struct blk_desc *blk_get_dev(const char *ifname, int dev)
  68. {
  69. return get_dev_hwpart(ifname, dev, 0);
  70. }
  71. #else
  72. struct blk_desc *get_dev_hwpart(const char *ifname, int dev, int hwpart)
  73. {
  74. return NULL;
  75. }
  76. struct blk_desc *blk_get_dev(const char *ifname, int dev)
  77. {
  78. return NULL;
  79. }
  80. #endif
  81. #ifdef CONFIG_HAVE_BLOCK_DEVICE
  82. /* ------------------------------------------------------------------------- */
  83. /*
  84. * reports device info to the user
  85. */
  86. #ifdef CONFIG_LBA48
  87. typedef uint64_t lba512_t;
  88. #else
  89. typedef lbaint_t lba512_t;
  90. #endif
  91. /*
  92. * Overflowless variant of (block_count * mul_by / 2**right_shift)
  93. * when 2**right_shift > mul_by
  94. */
  95. static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by,
  96. int right_shift)
  97. {
  98. lba512_t bc_quot, bc_rem;
  99. /* x * m / d == x / d * m + (x % d) * m / d */
  100. bc_quot = block_count >> right_shift;
  101. bc_rem = block_count - (bc_quot << right_shift);
  102. return bc_quot * mul_by + ((bc_rem * mul_by) >> right_shift);
  103. }
  104. void dev_print (struct blk_desc *dev_desc)
  105. {
  106. lba512_t lba512; /* number of blocks if 512bytes block size */
  107. if (dev_desc->type == DEV_TYPE_UNKNOWN) {
  108. puts ("not available\n");
  109. return;
  110. }
  111. switch (dev_desc->if_type) {
  112. case IF_TYPE_SCSI:
  113. printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
  114. dev_desc->target,dev_desc->lun,
  115. dev_desc->vendor,
  116. dev_desc->product,
  117. dev_desc->revision);
  118. break;
  119. case IF_TYPE_ATAPI:
  120. case IF_TYPE_IDE:
  121. case IF_TYPE_SATA:
  122. printf ("Model: %s Firm: %s Ser#: %s\n",
  123. dev_desc->vendor,
  124. dev_desc->revision,
  125. dev_desc->product);
  126. break;
  127. case IF_TYPE_SD:
  128. case IF_TYPE_MMC:
  129. case IF_TYPE_USB:
  130. case IF_TYPE_NVME:
  131. case IF_TYPE_PVBLOCK:
  132. case IF_TYPE_HOST:
  133. printf ("Vendor: %s Rev: %s Prod: %s\n",
  134. dev_desc->vendor,
  135. dev_desc->revision,
  136. dev_desc->product);
  137. break;
  138. case IF_TYPE_VIRTIO:
  139. printf("%s VirtIO Block Device\n", dev_desc->vendor);
  140. break;
  141. case IF_TYPE_DOC:
  142. puts("device type DOC\n");
  143. return;
  144. case IF_TYPE_UNKNOWN:
  145. puts("device type unknown\n");
  146. return;
  147. default:
  148. printf("Unhandled device type: %i\n", dev_desc->if_type);
  149. return;
  150. }
  151. puts (" Type: ");
  152. if (dev_desc->removable)
  153. puts ("Removable ");
  154. switch (dev_desc->type & 0x1F) {
  155. case DEV_TYPE_HARDDISK:
  156. puts ("Hard Disk");
  157. break;
  158. case DEV_TYPE_CDROM:
  159. puts ("CD ROM");
  160. break;
  161. case DEV_TYPE_OPDISK:
  162. puts ("Optical Device");
  163. break;
  164. case DEV_TYPE_TAPE:
  165. puts ("Tape");
  166. break;
  167. default:
  168. printf ("# %02X #", dev_desc->type & 0x1F);
  169. break;
  170. }
  171. puts ("\n");
  172. if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
  173. ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
  174. lbaint_t lba;
  175. lba = dev_desc->lba;
  176. lba512 = (lba * (dev_desc->blksz/512));
  177. /* round to 1 digit */
  178. /* 2048 = (1024 * 1024) / 512 MB */
  179. mb = lba512_muldiv(lba512, 10, 11);
  180. mb_quot = mb / 10;
  181. mb_rem = mb - (10 * mb_quot);
  182. gb = mb / 1024;
  183. gb_quot = gb / 10;
  184. gb_rem = gb - (10 * gb_quot);
  185. #ifdef CONFIG_LBA48
  186. if (dev_desc->lba48)
  187. printf (" Supports 48-bit addressing\n");
  188. #endif
  189. #if defined(CONFIG_SYS_64BIT_LBA)
  190. printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%llu x %lu)\n",
  191. mb_quot, mb_rem,
  192. gb_quot, gb_rem,
  193. lba,
  194. dev_desc->blksz);
  195. #else
  196. printf (" Capacity: %lu.%lu MB = %lu.%lu GB (%lu x %lu)\n",
  197. mb_quot, mb_rem,
  198. gb_quot, gb_rem,
  199. (ulong)lba,
  200. dev_desc->blksz);
  201. #endif
  202. } else {
  203. puts (" Capacity: not available\n");
  204. }
  205. }
  206. #endif
  207. #ifdef CONFIG_HAVE_BLOCK_DEVICE
  208. void part_init(struct blk_desc *dev_desc)
  209. {
  210. struct part_driver *drv =
  211. ll_entry_start(struct part_driver, part_driver);
  212. const int n_ents = ll_entry_count(struct part_driver, part_driver);
  213. struct part_driver *entry;
  214. blkcache_invalidate(dev_desc->if_type, dev_desc->devnum);
  215. dev_desc->part_type = PART_TYPE_UNKNOWN;
  216. for (entry = drv; entry != drv + n_ents; entry++) {
  217. int ret;
  218. ret = entry->test(dev_desc);
  219. debug("%s: try '%s': ret=%d\n", __func__, entry->name, ret);
  220. if (!ret) {
  221. dev_desc->part_type = entry->part_type;
  222. break;
  223. }
  224. }
  225. }
  226. static void print_part_header(const char *type, struct blk_desc *dev_desc)
  227. {
  228. #if CONFIG_IS_ENABLED(MAC_PARTITION) || \
  229. CONFIG_IS_ENABLED(DOS_PARTITION) || \
  230. CONFIG_IS_ENABLED(ISO_PARTITION) || \
  231. CONFIG_IS_ENABLED(AMIGA_PARTITION) || \
  232. CONFIG_IS_ENABLED(EFI_PARTITION)
  233. puts ("\nPartition Map for ");
  234. switch (dev_desc->if_type) {
  235. case IF_TYPE_IDE:
  236. puts ("IDE");
  237. break;
  238. case IF_TYPE_SATA:
  239. puts ("SATA");
  240. break;
  241. case IF_TYPE_SCSI:
  242. puts ("SCSI");
  243. break;
  244. case IF_TYPE_ATAPI:
  245. puts ("ATAPI");
  246. break;
  247. case IF_TYPE_USB:
  248. puts ("USB");
  249. break;
  250. case IF_TYPE_DOC:
  251. puts ("DOC");
  252. break;
  253. case IF_TYPE_MMC:
  254. puts ("MMC");
  255. break;
  256. case IF_TYPE_HOST:
  257. puts ("HOST");
  258. break;
  259. case IF_TYPE_NVME:
  260. puts ("NVMe");
  261. break;
  262. case IF_TYPE_PVBLOCK:
  263. puts("PV BLOCK");
  264. break;
  265. case IF_TYPE_VIRTIO:
  266. puts("VirtIO");
  267. break;
  268. default:
  269. puts ("UNKNOWN");
  270. break;
  271. }
  272. printf (" device %d -- Partition Type: %s\n\n",
  273. dev_desc->devnum, type);
  274. #endif /* any CONFIG_..._PARTITION */
  275. }
  276. void part_print(struct blk_desc *dev_desc)
  277. {
  278. struct part_driver *drv;
  279. drv = part_driver_lookup_type(dev_desc);
  280. if (!drv) {
  281. printf("## Unknown partition table type %x\n",
  282. dev_desc->part_type);
  283. return;
  284. }
  285. PRINTF("## Testing for valid %s partition ##\n", drv->name);
  286. print_part_header(drv->name, dev_desc);
  287. if (drv->print)
  288. drv->print(dev_desc);
  289. }
  290. #endif /* CONFIG_HAVE_BLOCK_DEVICE */
  291. int part_get_info(struct blk_desc *dev_desc, int part,
  292. struct disk_partition *info)
  293. {
  294. #ifdef CONFIG_HAVE_BLOCK_DEVICE
  295. struct part_driver *drv;
  296. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  297. /* The common case is no UUID support */
  298. info->uuid[0] = 0;
  299. #endif
  300. #ifdef CONFIG_PARTITION_TYPE_GUID
  301. info->type_guid[0] = 0;
  302. #endif
  303. drv = part_driver_lookup_type(dev_desc);
  304. if (!drv) {
  305. debug("## Unknown partition table type %x\n",
  306. dev_desc->part_type);
  307. return -EPROTONOSUPPORT;
  308. }
  309. if (!drv->get_info) {
  310. PRINTF("## Driver %s does not have the get_info() method\n",
  311. drv->name);
  312. return -ENOSYS;
  313. }
  314. if (drv->get_info(dev_desc, part, info) == 0) {
  315. PRINTF("## Valid %s partition found ##\n", drv->name);
  316. return 0;
  317. }
  318. #endif /* CONFIG_HAVE_BLOCK_DEVICE */
  319. return -ENOENT;
  320. }
  321. int part_get_info_whole_disk(struct blk_desc *dev_desc,
  322. struct disk_partition *info)
  323. {
  324. info->start = 0;
  325. info->size = dev_desc->lba;
  326. info->blksz = dev_desc->blksz;
  327. info->bootable = 0;
  328. strcpy((char *)info->type, BOOT_PART_TYPE);
  329. strcpy((char *)info->name, "Whole Disk");
  330. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  331. info->uuid[0] = 0;
  332. #endif
  333. #ifdef CONFIG_PARTITION_TYPE_GUID
  334. info->type_guid[0] = 0;
  335. #endif
  336. return 0;
  337. }
  338. int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
  339. struct blk_desc **dev_desc)
  340. {
  341. char *ep;
  342. char *dup_str = NULL;
  343. const char *dev_str, *hwpart_str;
  344. int dev, hwpart;
  345. hwpart_str = strchr(dev_hwpart_str, '.');
  346. if (hwpart_str) {
  347. dup_str = strdup(dev_hwpart_str);
  348. dup_str[hwpart_str - dev_hwpart_str] = 0;
  349. dev_str = dup_str;
  350. hwpart_str++;
  351. } else {
  352. dev_str = dev_hwpart_str;
  353. hwpart = 0;
  354. }
  355. dev = hextoul(dev_str, &ep);
  356. if (*ep) {
  357. printf("** Bad device specification %s %s **\n",
  358. ifname, dev_str);
  359. dev = -EINVAL;
  360. goto cleanup;
  361. }
  362. if (hwpart_str) {
  363. hwpart = hextoul(hwpart_str, &ep);
  364. if (*ep) {
  365. printf("** Bad HW partition specification %s %s **\n",
  366. ifname, hwpart_str);
  367. dev = -EINVAL;
  368. goto cleanup;
  369. }
  370. }
  371. *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
  372. if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
  373. debug("** Bad device %s %s **\n", ifname, dev_hwpart_str);
  374. dev = -ENODEV;
  375. goto cleanup;
  376. }
  377. #ifdef CONFIG_HAVE_BLOCK_DEVICE
  378. /*
  379. * Updates the partition table for the specified hw partition.
  380. * Always should be done, otherwise hw partition 0 will return stale
  381. * data after displaying a non-zero hw partition.
  382. */
  383. part_init(*dev_desc);
  384. #endif
  385. cleanup:
  386. free(dup_str);
  387. return dev;
  388. }
  389. #define PART_UNSPECIFIED -2
  390. #define PART_AUTO -1
  391. int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
  392. struct blk_desc **dev_desc,
  393. struct disk_partition *info, int allow_whole_dev)
  394. {
  395. int ret;
  396. const char *part_str;
  397. char *dup_str = NULL;
  398. const char *dev_str;
  399. int dev;
  400. char *ep;
  401. int p;
  402. int part;
  403. struct disk_partition tmpinfo;
  404. #ifdef CONFIG_SANDBOX
  405. /*
  406. * Special-case a pseudo block device "hostfs", to allow access to the
  407. * host's own filesystem.
  408. */
  409. if (0 == strcmp(ifname, "hostfs")) {
  410. *dev_desc = NULL;
  411. info->start = 0;
  412. info->size = 0;
  413. info->blksz = 0;
  414. info->bootable = 0;
  415. strcpy((char *)info->type, BOOT_PART_TYPE);
  416. strcpy((char *)info->name, "Sandbox host");
  417. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  418. info->uuid[0] = 0;
  419. #endif
  420. #ifdef CONFIG_PARTITION_TYPE_GUID
  421. info->type_guid[0] = 0;
  422. #endif
  423. return 0;
  424. }
  425. #endif
  426. #ifdef CONFIG_CMD_UBIFS
  427. /*
  428. * Special-case ubi, ubi goes through a mtd, rather than through
  429. * a regular block device.
  430. */
  431. if (0 == strcmp(ifname, "ubi")) {
  432. if (!ubifs_is_mounted()) {
  433. printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
  434. return -EINVAL;
  435. }
  436. *dev_desc = NULL;
  437. memset(info, 0, sizeof(*info));
  438. strcpy((char *)info->type, BOOT_PART_TYPE);
  439. strcpy((char *)info->name, "UBI");
  440. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  441. info->uuid[0] = 0;
  442. #endif
  443. return 0;
  444. }
  445. #endif
  446. /* If no dev_part_str, use bootdevice environment variable */
  447. if (!dev_part_str || !strlen(dev_part_str) ||
  448. !strcmp(dev_part_str, "-"))
  449. dev_part_str = env_get("bootdevice");
  450. /* If still no dev_part_str, it's an error */
  451. if (!dev_part_str) {
  452. printf("** No device specified **\n");
  453. ret = -ENODEV;
  454. goto cleanup;
  455. }
  456. /* Separate device and partition ID specification */
  457. part_str = strchr(dev_part_str, ':');
  458. if (part_str) {
  459. dup_str = strdup(dev_part_str);
  460. dup_str[part_str - dev_part_str] = 0;
  461. dev_str = dup_str;
  462. part_str++;
  463. } else {
  464. dev_str = dev_part_str;
  465. }
  466. /* Look up the device */
  467. dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
  468. if (dev < 0) {
  469. ret = dev;
  470. goto cleanup;
  471. }
  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)hextoul(part_str, &ep);
  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. ret = -ENOENT;
  488. goto cleanup;
  489. }
  490. }
  491. /*
  492. * No partition table on device,
  493. * or user requested partition 0 (entire device).
  494. */
  495. if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
  496. (part == 0)) {
  497. if (!(*dev_desc)->lba) {
  498. printf("** Bad device size - %s %s **\n", ifname,
  499. dev_str);
  500. ret = -EINVAL;
  501. goto cleanup;
  502. }
  503. /*
  504. * If user specified a partition ID other than 0,
  505. * or the calling command only accepts partitions,
  506. * it's an error.
  507. */
  508. if ((part > 0) || (!allow_whole_dev)) {
  509. printf("** No partition table - %s %s **\n", ifname,
  510. dev_str);
  511. ret = -EPROTONOSUPPORT;
  512. goto cleanup;
  513. }
  514. (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
  515. part_get_info_whole_disk(*dev_desc, info);
  516. ret = 0;
  517. goto cleanup;
  518. }
  519. /*
  520. * Now there's known to be a partition table,
  521. * not specifying a partition means to pick partition 1.
  522. */
  523. if (part == PART_UNSPECIFIED)
  524. part = 1;
  525. /*
  526. * If user didn't specify a partition number, or did specify something
  527. * other than "auto", use that partition number directly.
  528. */
  529. if (part != PART_AUTO) {
  530. ret = part_get_info(*dev_desc, part, info);
  531. if (ret) {
  532. printf("** Invalid partition %d **\n", part);
  533. goto cleanup;
  534. }
  535. } else {
  536. /*
  537. * Find the first bootable partition.
  538. * If none are bootable, fall back to the first valid partition.
  539. */
  540. part = 0;
  541. for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
  542. ret = part_get_info(*dev_desc, p, info);
  543. if (ret)
  544. continue;
  545. /*
  546. * First valid partition, or new better partition?
  547. * If so, save partition ID.
  548. */
  549. if (!part || info->bootable)
  550. part = p;
  551. /* Best possible partition? Stop searching. */
  552. if (info->bootable)
  553. break;
  554. /*
  555. * We now need to search further for best possible.
  556. * If we what we just queried was the best so far,
  557. * save the info since we over-write it next loop.
  558. */
  559. if (part == p)
  560. tmpinfo = *info;
  561. }
  562. /* If we found any acceptable partition */
  563. if (part) {
  564. /*
  565. * If we searched all possible partition IDs,
  566. * return the first valid partition we found.
  567. */
  568. if (p == MAX_SEARCH_PARTITIONS + 1)
  569. *info = tmpinfo;
  570. } else {
  571. printf("** No valid partitions found **\n");
  572. goto cleanup;
  573. }
  574. }
  575. if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
  576. printf("** Invalid partition type \"%.32s\""
  577. " (expect \"" BOOT_PART_TYPE "\")\n",
  578. info->type);
  579. ret = -EINVAL;
  580. goto cleanup;
  581. }
  582. (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
  583. ret = part;
  584. goto cleanup;
  585. cleanup:
  586. free(dup_str);
  587. return ret;
  588. }
  589. int part_get_info_by_name_type(struct blk_desc *dev_desc, const char *name,
  590. struct disk_partition *info, int part_type)
  591. {
  592. struct part_driver *part_drv;
  593. int ret;
  594. int i;
  595. part_drv = part_driver_lookup_type(dev_desc);
  596. if (!part_drv)
  597. return -1;
  598. for (i = 1; i < part_drv->max_entries; i++) {
  599. ret = part_drv->get_info(dev_desc, i, info);
  600. if (ret != 0) {
  601. /* no more entries in table */
  602. break;
  603. }
  604. if (strcmp(name, (const char *)info->name) == 0) {
  605. /* matched */
  606. return i;
  607. }
  608. }
  609. return -ENOENT;
  610. }
  611. int part_get_info_by_name(struct blk_desc *dev_desc, const char *name,
  612. struct disk_partition *info)
  613. {
  614. return part_get_info_by_name_type(dev_desc, name, info, PART_TYPE_ALL);
  615. }
  616. /**
  617. * Get partition info from device number and partition name.
  618. *
  619. * Parse a device number and partition name string in the form of
  620. * "devicenum.hwpartnum#partition_name", for example "0.1#misc". devicenum and
  621. * hwpartnum are both optional, defaulting to 0. If the partition is found,
  622. * sets dev_desc and part_info accordingly with the information of the
  623. * partition with the given partition_name.
  624. *
  625. * @param[in] dev_iface Device interface
  626. * @param[in] dev_part_str Input string argument, like "0.1#misc"
  627. * @param[out] dev_desc Place to store the device description pointer
  628. * @param[out] part_info Place to store the partition information
  629. * @return 0 on success, or a negative on error
  630. */
  631. static int part_get_info_by_dev_and_name(const char *dev_iface,
  632. const char *dev_part_str,
  633. struct blk_desc **dev_desc,
  634. struct disk_partition *part_info)
  635. {
  636. char *dup_str = NULL;
  637. const char *dev_str, *part_str;
  638. int ret;
  639. /* Separate device and partition name specification */
  640. if (dev_part_str)
  641. part_str = strchr(dev_part_str, '#');
  642. else
  643. part_str = NULL;
  644. if (part_str) {
  645. dup_str = strdup(dev_part_str);
  646. dup_str[part_str - dev_part_str] = 0;
  647. dev_str = dup_str;
  648. part_str++;
  649. } else {
  650. return -EINVAL;
  651. }
  652. ret = blk_get_device_by_str(dev_iface, dev_str, dev_desc);
  653. if (ret < 0)
  654. goto cleanup;
  655. ret = part_get_info_by_name(*dev_desc, part_str, part_info);
  656. if (ret < 0)
  657. printf("Could not find \"%s\" partition\n", part_str);
  658. cleanup:
  659. free(dup_str);
  660. return ret;
  661. }
  662. int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
  663. const char *dev_part_str,
  664. struct blk_desc **dev_desc,
  665. struct disk_partition *part_info,
  666. int allow_whole_dev)
  667. {
  668. int ret;
  669. /* Split the part_name if passed as "$dev_num#part_name". */
  670. ret = part_get_info_by_dev_and_name(dev_iface, dev_part_str,
  671. dev_desc, part_info);
  672. if (ret >= 0)
  673. return ret;
  674. /*
  675. * Couldn't lookup by name, try looking up the partition description
  676. * directly.
  677. */
  678. ret = blk_get_device_part_str(dev_iface, dev_part_str,
  679. dev_desc, part_info, allow_whole_dev);
  680. if (ret < 0)
  681. printf("Couldn't find partition %s %s\n",
  682. dev_iface, dev_part_str);
  683. return ret;
  684. }
  685. void part_set_generic_name(const struct blk_desc *dev_desc,
  686. int part_num, char *name)
  687. {
  688. char *devtype;
  689. switch (dev_desc->if_type) {
  690. case IF_TYPE_IDE:
  691. case IF_TYPE_SATA:
  692. case IF_TYPE_ATAPI:
  693. devtype = "hd";
  694. break;
  695. case IF_TYPE_SCSI:
  696. devtype = "sd";
  697. break;
  698. case IF_TYPE_USB:
  699. devtype = "usbd";
  700. break;
  701. case IF_TYPE_DOC:
  702. devtype = "docd";
  703. break;
  704. case IF_TYPE_MMC:
  705. case IF_TYPE_SD:
  706. devtype = "mmcsd";
  707. break;
  708. default:
  709. devtype = "xx";
  710. break;
  711. }
  712. sprintf(name, "%s%c%d", devtype, 'a' + dev_desc->devnum, part_num);
  713. }