part.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. drv->name);
  332. return -ENOSYS;
  333. }
  334. if (drv->get_info(dev_desc, part, info) == 0) {
  335. PRINTF("## Valid %s partition found ##\n", drv->name);
  336. return 0;
  337. }
  338. #endif /* HAVE_BLOCK_DEVICE */
  339. return -1;
  340. }
  341. int blk_get_device_by_str(const char *ifname, const char *dev_hwpart_str,
  342. struct blk_desc **dev_desc)
  343. {
  344. char *ep;
  345. char *dup_str = NULL;
  346. const char *dev_str, *hwpart_str;
  347. int dev, hwpart;
  348. hwpart_str = strchr(dev_hwpart_str, '.');
  349. if (hwpart_str) {
  350. dup_str = strdup(dev_hwpart_str);
  351. dup_str[hwpart_str - dev_hwpart_str] = 0;
  352. dev_str = dup_str;
  353. hwpart_str++;
  354. } else {
  355. dev_str = dev_hwpart_str;
  356. hwpart = 0;
  357. }
  358. dev = simple_strtoul(dev_str, &ep, 16);
  359. if (*ep) {
  360. printf("** Bad device specification %s %s **\n",
  361. ifname, dev_str);
  362. dev = -1;
  363. goto cleanup;
  364. }
  365. if (hwpart_str) {
  366. hwpart = simple_strtoul(hwpart_str, &ep, 16);
  367. if (*ep) {
  368. printf("** Bad HW partition specification %s %s **\n",
  369. ifname, hwpart_str);
  370. dev = -1;
  371. goto cleanup;
  372. }
  373. }
  374. *dev_desc = get_dev_hwpart(ifname, dev, hwpart);
  375. if (!(*dev_desc) || ((*dev_desc)->type == DEV_TYPE_UNKNOWN)) {
  376. printf("** Bad device %s %s **\n", ifname, dev_hwpart_str);
  377. dev = -1;
  378. goto cleanup;
  379. }
  380. #ifdef HAVE_BLOCK_DEVICE
  381. /*
  382. * Updates the partition table for the specified hw partition.
  383. * Does not need to be done for hwpart 0 since it is default and
  384. * already loaded.
  385. */
  386. if(hwpart != 0)
  387. part_init(*dev_desc);
  388. #endif
  389. cleanup:
  390. free(dup_str);
  391. return dev;
  392. }
  393. #define PART_UNSPECIFIED -2
  394. #define PART_AUTO -1
  395. #define MAX_SEARCH_PARTITIONS 16
  396. int blk_get_device_part_str(const char *ifname, const char *dev_part_str,
  397. struct blk_desc **dev_desc,
  398. disk_partition_t *info, int allow_whole_dev)
  399. {
  400. int ret = -1;
  401. const char *part_str;
  402. char *dup_str = NULL;
  403. const char *dev_str;
  404. int dev;
  405. char *ep;
  406. int p;
  407. int part;
  408. disk_partition_t tmpinfo;
  409. #ifdef CONFIG_SANDBOX
  410. /*
  411. * Special-case a pseudo block device "hostfs", to allow access to the
  412. * host's own filesystem.
  413. */
  414. if (0 == strcmp(ifname, "hostfs")) {
  415. *dev_desc = NULL;
  416. info->start = 0;
  417. info->size = 0;
  418. info->blksz = 0;
  419. info->bootable = 0;
  420. strcpy((char *)info->type, BOOT_PART_TYPE);
  421. strcpy((char *)info->name, "Sandbox host");
  422. #ifdef CONFIG_PARTITION_UUIDS
  423. info->uuid[0] = 0;
  424. #endif
  425. #ifdef CONFIG_PARTITION_TYPE_GUID
  426. info->type_guid[0] = 0;
  427. #endif
  428. return 0;
  429. }
  430. #endif
  431. #ifdef CONFIG_CMD_UBIFS
  432. /*
  433. * Special-case ubi, ubi goes through a mtd, rathen then through
  434. * a regular block device.
  435. */
  436. if (0 == strcmp(ifname, "ubi")) {
  437. if (!ubifs_is_mounted()) {
  438. printf("UBIFS not mounted, use ubifsmount to mount volume first!\n");
  439. return -1;
  440. }
  441. *dev_desc = NULL;
  442. memset(info, 0, sizeof(*info));
  443. strcpy((char *)info->type, BOOT_PART_TYPE);
  444. strcpy((char *)info->name, "UBI");
  445. #ifdef CONFIG_PARTITION_UUIDS
  446. info->uuid[0] = 0;
  447. #endif
  448. return 0;
  449. }
  450. #endif
  451. /* If no dev_part_str, use bootdevice environment variable */
  452. if (!dev_part_str || !strlen(dev_part_str) ||
  453. !strcmp(dev_part_str, "-"))
  454. dev_part_str = getenv("bootdevice");
  455. /* If still no dev_part_str, it's an error */
  456. if (!dev_part_str) {
  457. printf("** No device specified **\n");
  458. goto cleanup;
  459. }
  460. /* Separate device and partition ID specification */
  461. part_str = strchr(dev_part_str, ':');
  462. if (part_str) {
  463. dup_str = strdup(dev_part_str);
  464. dup_str[part_str - dev_part_str] = 0;
  465. dev_str = dup_str;
  466. part_str++;
  467. } else {
  468. dev_str = dev_part_str;
  469. }
  470. /* Look up the device */
  471. dev = blk_get_device_by_str(ifname, dev_str, dev_desc);
  472. if (dev < 0)
  473. goto cleanup;
  474. /* Convert partition ID string to number */
  475. if (!part_str || !*part_str) {
  476. part = PART_UNSPECIFIED;
  477. } else if (!strcmp(part_str, "auto")) {
  478. part = PART_AUTO;
  479. } else {
  480. /* Something specified -> use exactly that */
  481. part = (int)simple_strtoul(part_str, &ep, 16);
  482. /*
  483. * Less than whole string converted,
  484. * or request for whole device, but caller requires partition.
  485. */
  486. if (*ep || (part == 0 && !allow_whole_dev)) {
  487. printf("** Bad partition specification %s %s **\n",
  488. ifname, dev_part_str);
  489. goto cleanup;
  490. }
  491. }
  492. /*
  493. * No partition table on device,
  494. * or user requested partition 0 (entire device).
  495. */
  496. if (((*dev_desc)->part_type == PART_TYPE_UNKNOWN) ||
  497. (part == 0)) {
  498. if (!(*dev_desc)->lba) {
  499. printf("** Bad device size - %s %s **\n", ifname,
  500. dev_str);
  501. goto cleanup;
  502. }
  503. /*
  504. * If user specified a partition ID other than 0,
  505. * or the calling command only accepts partitions,
  506. * it's an error.
  507. */
  508. if ((part > 0) || (!allow_whole_dev)) {
  509. printf("** No partition table - %s %s **\n", ifname,
  510. dev_str);
  511. goto cleanup;
  512. }
  513. (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
  514. info->start = 0;
  515. info->size = (*dev_desc)->lba;
  516. info->blksz = (*dev_desc)->blksz;
  517. info->bootable = 0;
  518. strcpy((char *)info->type, BOOT_PART_TYPE);
  519. strcpy((char *)info->name, "Whole Disk");
  520. #ifdef CONFIG_PARTITION_UUIDS
  521. info->uuid[0] = 0;
  522. #endif
  523. #ifdef CONFIG_PARTITION_TYPE_GUID
  524. info->type_guid[0] = 0;
  525. #endif
  526. ret = 0;
  527. goto cleanup;
  528. }
  529. /*
  530. * Now there's known to be a partition table,
  531. * not specifying a partition means to pick partition 1.
  532. */
  533. if (part == PART_UNSPECIFIED)
  534. part = 1;
  535. /*
  536. * If user didn't specify a partition number, or did specify something
  537. * other than "auto", use that partition number directly.
  538. */
  539. if (part != PART_AUTO) {
  540. ret = part_get_info(*dev_desc, part, info);
  541. if (ret) {
  542. printf("** Invalid partition %d **\n", part);
  543. goto cleanup;
  544. }
  545. } else {
  546. /*
  547. * Find the first bootable partition.
  548. * If none are bootable, fall back to the first valid partition.
  549. */
  550. part = 0;
  551. for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) {
  552. ret = part_get_info(*dev_desc, p, info);
  553. if (ret)
  554. continue;
  555. /*
  556. * First valid partition, or new better partition?
  557. * If so, save partition ID.
  558. */
  559. if (!part || info->bootable)
  560. part = p;
  561. /* Best possible partition? Stop searching. */
  562. if (info->bootable)
  563. break;
  564. /*
  565. * We now need to search further for best possible.
  566. * If we what we just queried was the best so far,
  567. * save the info since we over-write it next loop.
  568. */
  569. if (part == p)
  570. tmpinfo = *info;
  571. }
  572. /* If we found any acceptable partition */
  573. if (part) {
  574. /*
  575. * If we searched all possible partition IDs,
  576. * return the first valid partition we found.
  577. */
  578. if (p == MAX_SEARCH_PARTITIONS + 1)
  579. *info = tmpinfo;
  580. } else {
  581. printf("** No valid partitions found **\n");
  582. ret = -1;
  583. goto cleanup;
  584. }
  585. }
  586. if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
  587. printf("** Invalid partition type \"%.32s\""
  588. " (expect \"" BOOT_PART_TYPE "\")\n",
  589. info->type);
  590. ret = -1;
  591. goto cleanup;
  592. }
  593. (*dev_desc)->log2blksz = LOG2((*dev_desc)->blksz);
  594. ret = part;
  595. goto cleanup;
  596. cleanup:
  597. free(dup_str);
  598. return ret;
  599. }