part.c 17 KB

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