part.c 14 KB

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