part.c 19 KB

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