part.c 17 KB

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