part.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. case IF_TYPE_EFI_MEDIA:
  269. puts("EFI");
  270. break;
  271. default:
  272. puts("UNKNOWN");
  273. break;
  274. }
  275. printf (" device %d -- Partition Type: %s\n\n",
  276. dev_desc->devnum, type);
  277. #endif /* any CONFIG_..._PARTITION */
  278. }
  279. void part_print(struct blk_desc *dev_desc)
  280. {
  281. struct part_driver *drv;
  282. drv = part_driver_lookup_type(dev_desc);
  283. if (!drv) {
  284. printf("## Unknown partition table type %x\n",
  285. dev_desc->part_type);
  286. return;
  287. }
  288. PRINTF("## Testing for valid %s partition ##\n", drv->name);
  289. print_part_header(drv->name, dev_desc);
  290. if (drv->print)
  291. drv->print(dev_desc);
  292. }
  293. #endif /* CONFIG_HAVE_BLOCK_DEVICE */
  294. int part_get_info(struct blk_desc *dev_desc, int part,
  295. struct disk_partition *info)
  296. {
  297. #ifdef CONFIG_HAVE_BLOCK_DEVICE
  298. struct part_driver *drv;
  299. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  300. /* The common case is no UUID support */
  301. info->uuid[0] = 0;
  302. #endif
  303. #ifdef CONFIG_PARTITION_TYPE_GUID
  304. info->type_guid[0] = 0;
  305. #endif
  306. drv = part_driver_lookup_type(dev_desc);
  307. if (!drv) {
  308. debug("## Unknown partition table type %x\n",
  309. dev_desc->part_type);
  310. return -EPROTONOSUPPORT;
  311. }
  312. if (!drv->get_info) {
  313. PRINTF("## Driver %s does not have the get_info() method\n",
  314. drv->name);
  315. return -ENOSYS;
  316. }
  317. if (drv->get_info(dev_desc, part, info) == 0) {
  318. PRINTF("## Valid %s partition found ##\n", drv->name);
  319. return 0;
  320. }
  321. #endif /* CONFIG_HAVE_BLOCK_DEVICE */
  322. return -ENOENT;
  323. }
  324. int part_get_info_whole_disk(struct blk_desc *dev_desc,
  325. struct disk_partition *info)
  326. {
  327. info->start = 0;
  328. info->size = dev_desc->lba;
  329. info->blksz = dev_desc->blksz;
  330. info->bootable = 0;
  331. strcpy((char *)info->type, BOOT_PART_TYPE);
  332. strcpy((char *)info->name, "Whole Disk");
  333. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  334. info->uuid[0] = 0;
  335. #endif
  336. #ifdef CONFIG_PARTITION_TYPE_GUID
  337. info->type_guid[0] = 0;
  338. #endif
  339. return 0;
  340. }
  341. int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
  342. struct blk_desc **dev_desc)
  343. {
  344. char *ep;
  345. char *dup_str = NULL;
  346. const char *dev_str, *hwpart_str;
  347. int dev, hwpart;
  348. hwpart_str = strchr(dev_hwpart_str, '.');
  349. if (hwpart_str) {
  350. dup_str = strdup(dev_hwpart_str);
  351. dup_str[hwpart_str - dev_hwpart_str] = 0;
  352. dev_str = dup_str;
  353. hwpart_str++;
  354. } else {
  355. dev_str = dev_hwpart_str;
  356. hwpart = 0;
  357. }
  358. dev = hextoul(dev_str, &ep);
  359. if (*ep) {
  360. printf("** Bad device specification %s %s **\n",
  361. ifname, dev_str);
  362. dev = -EINVAL;
  363. goto cleanup;
  364. }
  365. if (hwpart_str) {
  366. hwpart = hextoul(hwpart_str, &ep);
  367. if (*ep) {
  368. printf("** Bad HW partition specification %s %s **\n",
  369. ifname, hwpart_str);
  370. dev = -EINVAL;
  371. goto cleanup;
  372. }
  373. }
  374. *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
  375. if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
  376. debug("** Bad device %s %s **\n", ifname, dev_hwpart_str);
  377. dev = -ENODEV;
  378. goto cleanup;
  379. }
  380. #ifdef CONFIG_HAVE_BLOCK_DEVICE
  381. /*
  382. * Updates the partition table for the specified hw partition.
  383. * Always should be done, otherwise hw partition 0 will return stale
  384. * data after displaying a non-zero hw partition.
  385. */
  386. if ((*dev_desc)->if_type == IF_TYPE_MMC)
  387. part_init(*dev_desc);
  388. #endif
  389. cleanup:
  390. free(dup_str);
  391. return dev;
  392. }
  393. #define PART_UNSPECIFIED -2
  394. #define PART_AUTO -1
  395. int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
  396. struct blk_desc **dev_desc,
  397. struct disk_partition *info, int allow_whole_dev)
  398. {
  399. int ret;
  400. const char *part_str;
  401. char *dup_str = NULL;
  402. const char *dev_str;
  403. int dev;
  404. char *ep;
  405. int p;
  406. int part;
  407. struct disk_partition tmpinfo;
  408. #ifdef CONFIG_SANDBOX
  409. /*
  410. * Special-case a pseudo block device "hostfs", to allow access to the
  411. * host's own filesystem.
  412. */
  413. if (0 == strcmp(ifname, "hostfs")) {
  414. *dev_desc = NULL;
  415. info->start = 0;
  416. info->size = 0;
  417. info->blksz = 0;
  418. info->bootable = 0;
  419. strcpy((char *)info->type, BOOT_PART_TYPE);
  420. strcpy((char *)info->name, "Sandbox host");
  421. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  422. info->uuid[0] = 0;
  423. #endif
  424. #ifdef CONFIG_PARTITION_TYPE_GUID
  425. info->type_guid[0] = 0;
  426. #endif
  427. return 0;
  428. }
  429. #endif
  430. #ifdef CONFIG_CMD_UBIFS
  431. /*
  432. * Special-case ubi, ubi goes through a mtd, rather than through
  433. * a regular block device.
  434. */
  435. if (0 == strcmp(ifname, "ubi")) {
  436. if (!ubifs_is_mounted()) {
  437. printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
  438. return -EINVAL;
  439. }
  440. *dev_desc = NULL;
  441. memset(info, 0, sizeof(*info));
  442. strcpy((char *)info->type, BOOT_PART_TYPE);
  443. strcpy((char *)info->name, "UBI");
  444. #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
  445. info->uuid[0] = 0;
  446. #endif
  447. return 0;
  448. }
  449. #endif
  450. /* If no dev_part_str, use bootdevice environment variable */
  451. if (!dev_part_str || !strlen(dev_part_str) ||
  452. !strcmp(dev_part_str, "-"))
  453. dev_part_str = env_get("bootdevice");
  454. /* If still no dev_part_str, it's an error */
  455. if (!dev_part_str) {
  456. printf("** No device specified **\n");
  457. ret = -ENODEV;
  458. goto cleanup;
  459. }
  460. /* Separate device and partition ID specification */
  461. part_str = strchr(dev_part_str, ':');
  462. if (part_str) {
  463. dup_str = strdup(dev_part_str);
  464. dup_str[part_str - dev_part_str] = 0;
  465. dev_str = dup_str;
  466. part_str++;
  467. } else {
  468. dev_str = dev_part_str;
  469. }
  470. /* Look up the device */
  471. dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
  472. if (dev < 0) {
  473. ret = dev;
  474. goto cleanup;
  475. }
  476. /* Convert partition ID string to number */
  477. if (!part_str || !*part_str) {
  478. part = PART_UNSPECIFIED;
  479. } else if (!strcmp(part_str, "auto")) {
  480. part = PART_AUTO;
  481. } else {
  482. /* Something specified -> use exactly that */
  483. part = (int)hextoul(part_str, &ep);
  484. /*
  485. * Less than whole string converted,
  486. * or request for whole device, but caller requires partition.
  487. */
  488. if (*ep || (part == 0 && !allow_whole_dev)) {
  489. printf("** Bad partition specification %s %s **\n",
  490. ifname, dev_part_str);
  491. ret = -ENOENT;
  492. goto cleanup;
  493. }
  494. }
  495. /*
  496. * No partition table on device,
  497. * or user requested partition 0 (entire device).
  498. */
  499. if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
  500. (part == 0)) {
  501. if (!(*dev_desc)->lba) {
  502. printf("** Bad device size - %s %s **\n", ifname,
  503. dev_str);
  504. ret = -EINVAL;
  505. goto cleanup;
  506. }
  507. /*
  508. * If user specified a partition ID other than 0,
  509. * or the calling command only accepts partitions,
  510. * it's an error.
  511. */
  512. if ((part > 0) || (!allow_whole_dev)) {
  513. printf("** No partition table - %s %s **\n", ifname,
  514. dev_str);
  515. ret = -EPROTONOSUPPORT;
  516. goto cleanup;
  517. }
  518. (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
  519. part_get_info_whole_disk(*dev_desc, info);
  520. ret = 0;
  521. goto cleanup;
  522. }
  523. /*
  524. * Now there's known to be a partition table,
  525. * not specifying a partition means to pick partition 1.
  526. */
  527. if (part == PART_UNSPECIFIED)
  528. part = 1;
  529. /*
  530. * If user didn't specify a partition number, or did specify something
  531. * other than "auto", use that partition number directly.
  532. */
  533. if (part != PART_AUTO) {
  534. ret = part_get_info(*dev_desc, part, info);
  535. if (ret) {
  536. printf("** Invalid partition %d **\n", part);
  537. goto cleanup;
  538. }
  539. } else {
  540. /*
  541. * Find the first bootable partition.
  542. * If none are bootable, fall back to the first valid partition.
  543. */
  544. part = 0;
  545. for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
  546. ret = part_get_info(*dev_desc, p, info);
  547. if (ret)
  548. continue;
  549. /*
  550. * First valid partition, or new better partition?
  551. * If so, save partition ID.
  552. */
  553. if (!part || info->bootable)
  554. part = p;
  555. /* Best possible partition? Stop searching. */
  556. if (info->bootable)
  557. break;
  558. /*
  559. * We now need to search further for best possible.
  560. * If we what we just queried was the best so far,
  561. * save the info since we over-write it next loop.
  562. */
  563. if (part == p)
  564. tmpinfo = *info;
  565. }
  566. /* If we found any acceptable partition */
  567. if (part) {
  568. /*
  569. * If we searched all possible partition IDs,
  570. * return the first valid partition we found.
  571. */
  572. if (p == MAX_SEARCH_PARTITIONS + 1)
  573. *info = tmpinfo;
  574. } else {
  575. printf("** No valid partitions found **\n");
  576. goto cleanup;
  577. }
  578. }
  579. if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
  580. printf("** Invalid partition type \"%.32s\""
  581. " (expect \"" BOOT_PART_TYPE "\")\n",
  582. info->type);
  583. ret = -EINVAL;
  584. goto cleanup;
  585. }
  586. (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
  587. ret = part;
  588. goto cleanup;
  589. cleanup:
  590. free(dup_str);
  591. return ret;
  592. }
  593. int part_get_info_by_name_type(struct blk_desc *dev_desc, const char *name,
  594. struct disk_partition *info, int part_type)
  595. {
  596. struct part_driver *part_drv;
  597. int ret;
  598. int i;
  599. part_drv = part_driver_lookup_type(dev_desc);
  600. if (!part_drv)
  601. return -1;
  602. if (!part_drv->get_info) {
  603. log_debug("## Driver %s does not have the get_info() method\n",
  604. part_drv->name);
  605. return -ENOSYS;
  606. }
  607. for (i = 1; i < part_drv->max_entries; i++) {
  608. ret = part_drv->get_info(dev_desc, i, info);
  609. if (ret != 0) {
  610. /* no more entries in table */
  611. break;
  612. }
  613. if (strcmp(name, (const char *)info->name) == 0) {
  614. /* matched */
  615. return i;
  616. }
  617. }
  618. return -ENOENT;
  619. }
  620. int part_get_info_by_name(struct blk_desc *dev_desc, const char *name,
  621. struct disk_partition *info)
  622. {
  623. return part_get_info_by_name_type(dev_desc, name, info, PART_TYPE_ALL);
  624. }
  625. /**
  626. * Get partition info from device number and partition name.
  627. *
  628. * Parse a device number and partition name string in the form of
  629. * "devicenum.hwpartnum#partition_name", for example "0.1#misc". devicenum and
  630. * hwpartnum are both optional, defaulting to 0. If the partition is found,
  631. * sets dev_desc and part_info accordingly with the information of the
  632. * partition with the given partition_name.
  633. *
  634. * @param[in] dev_iface Device interface
  635. * @param[in] dev_part_str Input string argument, like "0.1#misc"
  636. * @param[out] dev_desc Place to store the device description pointer
  637. * @param[out] part_info Place to store the partition information
  638. * Return: 0 on success, or a negative on error
  639. */
  640. static int part_get_info_by_dev_and_name(const char *dev_iface,
  641. const char *dev_part_str,
  642. struct blk_desc **dev_desc,
  643. struct disk_partition *part_info)
  644. {
  645. char *dup_str = NULL;
  646. const char *dev_str, *part_str;
  647. int ret;
  648. /* Separate device and partition name specification */
  649. if (dev_part_str)
  650. part_str = strchr(dev_part_str, '#');
  651. else
  652. part_str = NULL;
  653. if (part_str) {
  654. dup_str = strdup(dev_part_str);
  655. dup_str[part_str - dev_part_str] = 0;
  656. dev_str = dup_str;
  657. part_str++;
  658. } else {
  659. return -EINVAL;
  660. }
  661. ret = blk_get_device_by_str(dev_iface, dev_str, dev_desc);
  662. if (ret < 0)
  663. goto cleanup;
  664. ret = part_get_info_by_name(*dev_desc, part_str, part_info);
  665. if (ret < 0)
  666. printf("Could not find \"%s\" partition\n", part_str);
  667. cleanup:
  668. free(dup_str);
  669. return ret;
  670. }
  671. int part_get_info_by_dev_and_name_or_num(const char *dev_iface,
  672. const char *dev_part_str,
  673. struct blk_desc **dev_desc,
  674. struct disk_partition *part_info,
  675. int allow_whole_dev)
  676. {
  677. int ret;
  678. /* Split the part_name if passed as "$dev_num#part_name". */
  679. ret = part_get_info_by_dev_and_name(dev_iface, dev_part_str,
  680. dev_desc, part_info);
  681. if (ret >= 0)
  682. return ret;
  683. /*
  684. * Couldn't lookup by name, try looking up the partition description
  685. * directly.
  686. */
  687. ret = blk_get_device_part_str(dev_iface, dev_part_str,
  688. dev_desc, part_info, allow_whole_dev);
  689. if (ret < 0)
  690. printf("Couldn't find partition %s %s\n",
  691. dev_iface, dev_part_str);
  692. return ret;
  693. }
  694. void part_set_generic_name(const struct blk_desc *dev_desc,
  695. int part_num, char *name)
  696. {
  697. char *devtype;
  698. switch (dev_desc->if_type) {
  699. case IF_TYPE_IDE:
  700. case IF_TYPE_SATA:
  701. case IF_TYPE_ATAPI:
  702. devtype = "hd";
  703. break;
  704. case IF_TYPE_SCSI:
  705. devtype = "sd";
  706. break;
  707. case IF_TYPE_USB:
  708. devtype = "usbd";
  709. break;
  710. case IF_TYPE_DOC:
  711. devtype = "docd";
  712. break;
  713. case IF_TYPE_MMC:
  714. case IF_TYPE_SD:
  715. devtype = "mmcsd";
  716. break;
  717. default:
  718. devtype = "xx";
  719. break;
  720. }
  721. sprintf(name, "%s%c%d", devtype, 'a' + dev_desc->devnum, part_num);
  722. }