part.c 18 KB

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