part.c 15 KB

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