part.c 18 KB

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