part.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * (C) Copyright 2001
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <ide.h>
  10. #include <malloc.h>
  11. #include <part.h>
  12. #include <ubifs_uboot.h>
  13. #undef PART_DEBUG
  14. #ifdef PART_DEBUG
  15. #define PRINTF(fmt,args...) printf (fmt ,##args)
  16. #else
  17. #define PRINTF(fmt,args...)
  18. #endif
  19. struct block_drvr {
  20. char *name;
  21. block_dev_desc_t* (*get_dev)(int dev);
  22. int (*select_hwpart)(int dev_num, int hwpart);
  23. };
  24. static const struct block_drvr block_drvr[] = {
  25. #if defined(CONFIG_CMD_IDE)
  26. { .name = "ide", .get_dev = ide_get_dev, },
  27. #endif
  28. #if defined(CONFIG_CMD_SATA)
  29. {.name = "sata", .get_dev = sata_get_dev, },
  30. #endif
  31. #if defined(CONFIG_CMD_SCSI)
  32. { .name = "scsi", .get_dev = scsi_get_dev, },
  33. #endif
  34. #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
  35. { .name = "usb", .get_dev = usb_stor_get_dev, },
  36. #endif
  37. #if defined(CONFIG_MMC)
  38. {
  39. .name = "mmc",
  40. .get_dev = mmc_get_dev,
  41. .select_hwpart = mmc_select_hwpart,
  42. },
  43. #endif
  44. #if defined(CONFIG_SYSTEMACE)
  45. { .name = "ace", .get_dev = systemace_get_dev, },
  46. #endif
  47. #if defined(CONFIG_SANDBOX)
  48. { .name = "host", .get_dev = host_get_dev, },
  49. #endif
  50. { },
  51. };
  52. DECLARE_GLOBAL_DATA_PTR;
  53. #ifdef HAVE_BLOCK_DEVICE
  54. static block_dev_desc_t *get_dev_hwpart(const char *ifname, int dev, int hwpart)
  55. {
  56. const struct block_drvr *drvr = block_drvr;
  57. block_dev_desc_t* (*reloc_get_dev)(int dev);
  58. int (*select_hwpart)(int dev_num, int hwpart);
  59. char *name;
  60. int ret;
  61. if (!ifname)
  62. return NULL;
  63. name = drvr->name;
  64. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  65. name += gd->reloc_off;
  66. #endif
  67. while (drvr->name) {
  68. name = drvr->name;
  69. reloc_get_dev = drvr->get_dev;
  70. select_hwpart = drvr->select_hwpart;
  71. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  72. name += gd->reloc_off;
  73. reloc_get_dev += gd->reloc_off;
  74. if (select_hwpart)
  75. select_hwpart += gd->reloc_off;
  76. #endif
  77. if (strncmp(ifname, name, strlen(name)) == 0) {
  78. block_dev_desc_t *dev_desc = reloc_get_dev(dev);
  79. if (!dev_desc)
  80. return NULL;
  81. if (hwpart == 0 && !select_hwpart)
  82. return dev_desc;
  83. if (!select_hwpart)
  84. return NULL;
  85. ret = select_hwpart(dev_desc->dev, hwpart);
  86. if (ret < 0)
  87. return NULL;
  88. return dev_desc;
  89. }
  90. drvr++;
  91. }
  92. return NULL;
  93. }
  94. block_dev_desc_t *get_dev(const char *ifname, int dev)
  95. {
  96. return get_dev_hwpart(ifname, dev, 0);
  97. }
  98. #else
  99. block_dev_desc_t *get_dev_hwpart(const char *ifname, int dev, int hwpart)
  100. {
  101. return NULL;
  102. }
  103. block_dev_desc_t *get_dev(const char *ifname, int dev)
  104. {
  105. return NULL;
  106. }
  107. #endif
  108. #ifdef HAVE_BLOCK_DEVICE
  109. /* ------------------------------------------------------------------------- */
  110. /*
  111. * reports device info to the user
  112. */
  113. #ifdef CONFIG_LBA48
  114. typedef uint64_t lba512_t;
  115. #else
  116. typedef lbaint_t lba512_t;
  117. #endif
  118. /*
  119. * Overflowless variant of (block_count * mul_by / div_by)
  120. * when div_by > mul_by
  121. */
  122. static lba512_t lba512_muldiv(lba512_t block_count, lba512_t mul_by, lba512_t div_by)
  123. {
  124. lba512_t bc_quot, bc_rem;
  125. /* x * m / d == x / d * m + (x % d) * m / d */
  126. bc_quot = block_count / div_by;
  127. bc_rem = block_count - div_by * bc_quot;
  128. return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
  129. }
  130. void dev_print (block_dev_desc_t *dev_desc)
  131. {
  132. lba512_t lba512; /* number of blocks if 512bytes block size */
  133. if (dev_desc->type == DEV_TYPE_UNKNOWN) {
  134. puts ("not available\n");
  135. return;
  136. }
  137. switch (dev_desc->if_type) {
  138. case IF_TYPE_SCSI:
  139. printf ("(%d:%d) Vendor: %s Prod.: %s Rev: %s\n",
  140. dev_desc->target,dev_desc->lun,
  141. dev_desc->vendor,
  142. dev_desc->product,
  143. dev_desc->revision);
  144. break;
  145. case IF_TYPE_ATAPI:
  146. case IF_TYPE_IDE:
  147. case IF_TYPE_SATA:
  148. printf ("Model: %s Firm: %s Ser#: %s\n",
  149. dev_desc->vendor,
  150. dev_desc->revision,
  151. dev_desc->product);
  152. break;
  153. case IF_TYPE_SD:
  154. case IF_TYPE_MMC:
  155. case IF_TYPE_USB:
  156. printf ("Vendor: %s Rev: %s Prod: %s\n",
  157. dev_desc->vendor,
  158. dev_desc->revision,
  159. dev_desc->product);
  160. break;
  161. case IF_TYPE_DOC:
  162. puts("device type DOC\n");
  163. return;
  164. case IF_TYPE_UNKNOWN:
  165. puts("device type unknown\n");
  166. return;
  167. default:
  168. printf("Unhandled device type: %i\n", dev_desc->if_type);
  169. return;
  170. }
  171. puts (" Type: ");
  172. if (dev_desc->removable)
  173. puts ("Removable ");
  174. switch (dev_desc->type & 0x1F) {
  175. case DEV_TYPE_HARDDISK:
  176. puts ("Hard Disk");
  177. break;
  178. case DEV_TYPE_CDROM:
  179. puts ("CD ROM");
  180. break;
  181. case DEV_TYPE_OPDISK:
  182. puts ("Optical Device");
  183. break;
  184. case DEV_TYPE_TAPE:
  185. puts ("Tape");
  186. break;
  187. default:
  188. printf ("# %02X #", dev_desc->type & 0x1F);
  189. break;
  190. }
  191. puts ("\n");
  192. if (dev_desc->lba > 0L && dev_desc->blksz > 0L) {
  193. ulong mb, mb_quot, mb_rem, gb, gb_quot, gb_rem;
  194. lbaint_t lba;
  195. lba = dev_desc->lba;
  196. lba512 = (lba * (dev_desc->blksz/512));
  197. /* round to 1 digit */
  198. /* 2048 = (1024 * 1024) / 512 MB */
  199. mb = lba512_muldiv(lba512, 10, 2048);
  200. mb_quot = mb / 10;
  201. mb_rem = mb - (10 * mb_quot);
  202. gb = mb / 1024;
  203. gb_quot = gb / 10;
  204. gb_rem = gb - (10 * gb_quot);
  205. #ifdef CONFIG_LBA48
  206. if (dev_desc->lba48)
  207. printf (" Supports 48-bit addressing\n");
  208. #endif
  209. #if defined(CONFIG_SYS_64BIT_LBA)
  210. printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%Ld x %ld)\n",
  211. mb_quot, mb_rem,
  212. gb_quot, gb_rem,
  213. lba,
  214. dev_desc->blksz);
  215. #else
  216. printf (" Capacity: %ld.%ld MB = %ld.%ld GB (%ld x %ld)\n",
  217. mb_quot, mb_rem,
  218. gb_quot, gb_rem,
  219. (ulong)lba,
  220. dev_desc->blksz);
  221. #endif
  222. } else {
  223. puts (" Capacity: not available\n");
  224. }
  225. }
  226. #endif
  227. #ifdef HAVE_BLOCK_DEVICE
  228. void init_part(block_dev_desc_t *dev_desc)
  229. {
  230. #ifdef CONFIG_ISO_PARTITION
  231. if (test_part_iso(dev_desc) == 0) {
  232. dev_desc->part_type = PART_TYPE_ISO;
  233. return;
  234. }
  235. #endif
  236. #ifdef CONFIG_MAC_PARTITION
  237. if (test_part_mac(dev_desc) == 0) {
  238. dev_desc->part_type = PART_TYPE_MAC;
  239. return;
  240. }
  241. #endif
  242. /* must be placed before DOS partition detection */
  243. #ifdef CONFIG_EFI_PARTITION
  244. if (test_part_efi(dev_desc) == 0) {
  245. dev_desc->part_type = PART_TYPE_EFI;
  246. return;
  247. }
  248. #endif
  249. #ifdef CONFIG_DOS_PARTITION
  250. if (test_part_dos(dev_desc) == 0) {
  251. dev_desc->part_type = PART_TYPE_DOS;
  252. return;
  253. }
  254. #endif
  255. #ifdef CONFIG_AMIGA_PARTITION
  256. if (test_part_amiga(dev_desc) == 0) {
  257. dev_desc->part_type = PART_TYPE_AMIGA;
  258. return;
  259. }
  260. #endif
  261. dev_desc->part_type = PART_TYPE_UNKNOWN;
  262. }
  263. #if defined(CONFIG_MAC_PARTITION) || \
  264. defined(CONFIG_DOS_PARTITION) || \
  265. defined(CONFIG_ISO_PARTITION) || \
  266. defined(CONFIG_AMIGA_PARTITION) || \
  267. defined(CONFIG_EFI_PARTITION)
  268. static void print_part_header(const char *type, block_dev_desc_t *dev_desc)
  269. {
  270. puts ("\nPartition Map for ");
  271. switch (dev_desc->if_type) {
  272. case IF_TYPE_IDE:
  273. puts ("IDE");
  274. break;
  275. case IF_TYPE_SATA:
  276. puts ("SATA");
  277. break;
  278. case IF_TYPE_SCSI:
  279. puts ("SCSI");
  280. break;
  281. case IF_TYPE_ATAPI:
  282. puts ("ATAPI");
  283. break;
  284. case IF_TYPE_USB:
  285. puts ("USB");
  286. break;
  287. case IF_TYPE_DOC:
  288. puts ("DOC");
  289. break;
  290. case IF_TYPE_MMC:
  291. puts ("MMC");
  292. break;
  293. case IF_TYPE_HOST:
  294. puts("HOST");
  295. break;
  296. default:
  297. puts ("UNKNOWN");
  298. break;
  299. }
  300. printf (" device %d -- Partition Type: %s\n\n",
  301. dev_desc->dev, type);
  302. }
  303. #endif /* any CONFIG_..._PARTITION */
  304. void print_part(block_dev_desc_t * dev_desc)
  305. {
  306. switch (dev_desc->part_type) {
  307. #ifdef CONFIG_MAC_PARTITION
  308. case PART_TYPE_MAC:
  309. PRINTF ("## Testing for valid MAC partition ##\n");
  310. print_part_header ("MAC", dev_desc);
  311. print_part_mac (dev_desc);
  312. return;
  313. #endif
  314. #ifdef CONFIG_DOS_PARTITION
  315. case PART_TYPE_DOS:
  316. PRINTF ("## Testing for valid DOS partition ##\n");
  317. print_part_header ("DOS", dev_desc);
  318. print_part_dos (dev_desc);
  319. return;
  320. #endif
  321. #ifdef CONFIG_ISO_PARTITION
  322. case PART_TYPE_ISO:
  323. PRINTF ("## Testing for valid ISO Boot partition ##\n");
  324. print_part_header ("ISO", dev_desc);
  325. print_part_iso (dev_desc);
  326. return;
  327. #endif
  328. #ifdef CONFIG_AMIGA_PARTITION
  329. case PART_TYPE_AMIGA:
  330. PRINTF ("## Testing for a valid Amiga partition ##\n");
  331. print_part_header ("AMIGA", dev_desc);
  332. print_part_amiga (dev_desc);
  333. return;
  334. #endif
  335. #ifdef CONFIG_EFI_PARTITION
  336. case PART_TYPE_EFI:
  337. PRINTF ("## Testing for valid EFI partition ##\n");
  338. print_part_header ("EFI", dev_desc);
  339. print_part_efi (dev_desc);
  340. return;
  341. #endif
  342. }
  343. puts ("## Unknown partition table\n");
  344. }
  345. #endif /* HAVE_BLOCK_DEVICE */
  346. int get_partition_info(block_dev_desc_t *dev_desc, int part,
  347. disk_partition_t *info)
  348. {
  349. #ifdef HAVE_BLOCK_DEVICE
  350. #ifdef CONFIG_PARTITION_UUIDS
  351. /* The common case is no UUID support */
  352. info->uuid[0] = 0;
  353. #endif
  354. #ifdef CONFIG_PARTITION_TYPE_GUID
  355. info->type_guid[0] = 0;
  356. #endif
  357. switch (dev_desc->part_type) {
  358. #ifdef CONFIG_MAC_PARTITION
  359. case PART_TYPE_MAC:
  360. if (get_partition_info_mac(dev_desc, part, info) == 0) {
  361. PRINTF("## Valid MAC partition found ##\n");
  362. return 0;
  363. }
  364. break;
  365. #endif
  366. #ifdef CONFIG_DOS_PARTITION
  367. case PART_TYPE_DOS:
  368. if (get_partition_info_dos(dev_desc, part, info) == 0) {
  369. PRINTF("## Valid DOS partition found ##\n");
  370. return 0;
  371. }
  372. break;
  373. #endif
  374. #ifdef CONFIG_ISO_PARTITION
  375. case PART_TYPE_ISO:
  376. if (get_partition_info_iso(dev_desc, part, info) == 0) {
  377. PRINTF("## Valid ISO boot partition found ##\n");
  378. return 0;
  379. }
  380. break;
  381. #endif
  382. #ifdef CONFIG_AMIGA_PARTITION
  383. case PART_TYPE_AMIGA:
  384. if (get_partition_info_amiga(dev_desc, part, info) == 0) {
  385. PRINTF("## Valid Amiga partition found ##\n");
  386. return 0;
  387. }
  388. break;
  389. #endif
  390. #ifdef CONFIG_EFI_PARTITION
  391. case PART_TYPE_EFI:
  392. if (get_partition_info_efi(dev_desc, part, info) == 0) {
  393. PRINTF("## Valid EFI partition found ##\n");
  394. return 0;
  395. }
  396. break;
  397. #endif
  398. default:
  399. break;
  400. }
  401. #endif /* HAVE_BLOCK_DEVICE */
  402. return -1;
  403. }
  404. int get_device(const char *ifname, const char *dev_hwpart_str,
  405. block_dev_desc_t **dev_desc)
  406. {
  407. char *ep;
  408. char *dup_str = NULL;
  409. const char *dev_str, *hwpart_str;
  410. int dev, hwpart;
  411. hwpart_str = strchr(dev_hwpart_str, '.');
  412. if (hwpart_str) {
  413. dup_str = strdup(dev_hwpart_str);
  414. dup_str[hwpart_str - dev_hwpart_str] = 0;
  415. dev_str = dup_str;
  416. hwpart_str++;
  417. } else {
  418. dev_str = dev_hwpart_str;
  419. hwpart = 0;
  420. }
  421. dev = simple_strtoul(dev_str, &ep, 16);
  422. if (*ep) {
  423. printf("** Bad device specification %s %s **\n",
  424. ifname, dev_str);
  425. dev = -1;
  426. goto cleanup;
  427. }
  428. if (hwpart_str) {
  429. hwpart = simple_strtoul(hwpart_str, &ep, 16);
  430. if (*ep) {
  431. printf("** Bad HW partition specification %s %s **\n",
  432. ifname, hwpart_str);
  433. dev = -1;
  434. goto cleanup;
  435. }
  436. }
  437. *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
  438. if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
  439. printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
  440. dev = -1;
  441. goto cleanup;
  442. }
  443. cleanup:
  444. free(dup_str);
  445. return dev;
  446. }
  447. #define PART_UNSPECIFIED -2
  448. #define PART_AUTO -1
  449. #define MAX_SEARCH_PARTITIONS 16
  450. int get_device_and_partition(const char *ifname, const char *dev_part_str,
  451. block_dev_desc_t **dev_desc,
  452. disk_partition_t *info, int allow_whole_dev)
  453. {
  454. int ret = -1;
  455. const char *part_str;
  456. char *dup_str = NULL;
  457. const char *dev_str;
  458. int dev;
  459. char *ep;
  460. int p;
  461. int part;
  462. disk_partition_t tmpinfo;
  463. #if defined CONFIG_SANDBOX && defined CONFIG_CMD_UBIFS
  464. #error Only one of CONFIG_SANDBOX and CONFIG_CMD_UBIFS may be selected
  465. #endif
  466. #ifdef CONFIG_SANDBOX
  467. /*
  468. * Special-case a pseudo block device "hostfs", to allow access to the
  469. * host's own filesystem.
  470. */
  471. if (0 == strcmp(ifname, "hostfs")) {
  472. *dev_desc = NULL;
  473. info->start = 0;
  474. info->size = 0;
  475. info->blksz = 0;
  476. info->bootable = 0;
  477. strcpy((char *)info->type, BOOT_PART_TYPE);
  478. strcpy((char *)info->name, "Sandbox host");
  479. #ifdef CONFIG_PARTITION_UUIDS
  480. info->uuid[0] = 0;
  481. #endif
  482. #ifdef CONFIG_PARTITION_TYPE_GUID
  483. info->type_guid[0] = 0;
  484. #endif
  485. return 0;
  486. }
  487. #endif
  488. #ifdef CONFIG_CMD_UBIFS
  489. /*
  490. * Special-case ubi, ubi goes through a mtd, rathen then through
  491. * a regular block device.
  492. */
  493. if (0 == strcmp(ifname, "ubi")) {
  494. if (!ubifs_is_mounted()) {
  495. printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
  496. return -1;
  497. }
  498. *dev_desc = NULL;
  499. memset(info, 0, sizeof(*info));
  500. strcpy((char *)info->type, BOOT_PART_TYPE);
  501. strcpy((char *)info->name, "UBI");
  502. #ifdef CONFIG_PARTITION_UUIDS
  503. info->uuid[0] = 0;
  504. #endif
  505. return 0;
  506. }
  507. #endif
  508. /* If no dev_part_str, use bootdevice environment variable */
  509. if (!dev_part_str || !strlen(dev_part_str) ||
  510. !strcmp(dev_part_str, "-"))
  511. dev_part_str = getenv("bootdevice");
  512. /* If still no dev_part_str, it's an error */
  513. if (!dev_part_str) {
  514. printf("** No device specified **\n");
  515. goto cleanup;
  516. }
  517. /* Separate device and partition ID specification */
  518. part_str = strchr(dev_part_str, ':');
  519. if (part_str) {
  520. dup_str = strdup(dev_part_str);
  521. dup_str[part_str - dev_part_str] = 0;
  522. dev_str = dup_str;
  523. part_str++;
  524. } else {
  525. dev_str = dev_part_str;
  526. }
  527. /* Look up the device */
  528. dev = get_device(ifname, dev_str, dev_desc);
  529. if (dev < 0)
  530. goto cleanup;
  531. /* Convert partition ID string to number */
  532. if (!part_str || !*part_str) {
  533. part = PART_UNSPECIFIED;
  534. } else if (!strcmp(part_str, "auto")) {
  535. part = PART_AUTO;
  536. } else {
  537. /* Something specified -> use exactly that */
  538. part = (int)simple_strtoul(part_str, &ep, 16);
  539. /*
  540. * Less than whole string converted,
  541. * or request for whole device, but caller requires partition.
  542. */
  543. if (*ep || (part == 0 && !allow_whole_dev)) {
  544. printf("** Bad partition specification %s %s **\n",
  545. ifname, dev_part_str);
  546. goto cleanup;
  547. }
  548. }
  549. /*
  550. * No partition table on device,
  551. * or user requested partition 0 (entire device).
  552. */
  553. if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
  554. (part == 0)) {
  555. if (!(*dev_desc)->lba) {
  556. printf("** Bad device size - %s %s **\n", ifname,
  557. dev_str);
  558. goto cleanup;
  559. }
  560. /*
  561. * If user specified a partition ID other than 0,
  562. * or the calling command only accepts partitions,
  563. * it's an error.
  564. */
  565. if ((part > 0) || (!allow_whole_dev)) {
  566. printf("** No partition table - %s %s **\n", ifname,
  567. dev_str);
  568. goto cleanup;
  569. }
  570. (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
  571. info->start = 0;
  572. info->size = (*dev_desc)->lba;
  573. info->blksz = (*dev_desc)->blksz;
  574. info->bootable = 0;
  575. strcpy((char *)info->type, BOOT_PART_TYPE);
  576. strcpy((char *)info->name, "Whole Disk");
  577. #ifdef CONFIG_PARTITION_UUIDS
  578. info->uuid[0] = 0;
  579. #endif
  580. #ifdef CONFIG_PARTITION_TYPE_GUID
  581. info->type_guid[0] = 0;
  582. #endif
  583. ret = 0;
  584. goto cleanup;
  585. }
  586. /*
  587. * Now there's known to be a partition table,
  588. * not specifying a partition means to pick partition 1.
  589. */
  590. if (part == PART_UNSPECIFIED)
  591. part = 1;
  592. /*
  593. * If user didn't specify a partition number, or did specify something
  594. * other than "auto", use that partition number directly.
  595. */
  596. if (part != PART_AUTO) {
  597. ret = get_partition_info(*dev_desc, part, info);
  598. if (ret) {
  599. printf("** Invalid partition %d **\n", part);
  600. goto cleanup;
  601. }
  602. } else {
  603. /*
  604. * Find the first bootable partition.
  605. * If none are bootable, fall back to the first valid partition.
  606. */
  607. part = 0;
  608. for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
  609. ret = get_partition_info(*dev_desc, p, info);
  610. if (ret)
  611. continue;
  612. /*
  613. * First valid partition, or new better partition?
  614. * If so, save partition ID.
  615. */
  616. if (!part || info->bootable)
  617. part = p;
  618. /* Best possible partition? Stop searching. */
  619. if (info->bootable)
  620. break;
  621. /*
  622. * We now need to search further for best possible.
  623. * If we what we just queried was the best so far,
  624. * save the info since we over-write it next loop.
  625. */
  626. if (part == p)
  627. tmpinfo = *info;
  628. }
  629. /* If we found any acceptable partition */
  630. if (part) {
  631. /*
  632. * If we searched all possible partition IDs,
  633. * return the first valid partition we found.
  634. */
  635. if (p == MAX_SEARCH_PARTITIONS + 1)
  636. *info = tmpinfo;
  637. } else {
  638. printf("** No valid partitions found **\n");
  639. ret = -1;
  640. goto cleanup;
  641. }
  642. }
  643. if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
  644. printf("** Invalid partition type \"%.32s\""
  645. " (expect \"" BOOT_PART_TYPE "\")\n",
  646. info->type);
  647. ret = -1;
  648. goto cleanup;
  649. }
  650. (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
  651. ret = part;
  652. goto cleanup;
  653. cleanup:
  654. free(dup_str);
  655. return ret;
  656. }