part.c 19 KB

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