part.c 18 KB

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