mmc.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2003
  4. * Kyle Harris, kharris@nexus-tech.net
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <console.h>
  9. #include <mmc.h>
  10. #include <sparse_format.h>
  11. #include <image-sparse.h>
  12. static int curr_device = -1;
  13. static void print_mmcinfo(struct mmc *mmc)
  14. {
  15. int i;
  16. printf("Device: %s\n", mmc->cfg->name);
  17. printf("Manufacturer ID: %x\n", mmc->cid[0] >> 24);
  18. printf("OEM: %x\n", (mmc->cid[0] >> 8) & 0xffff);
  19. printf("Name: %c%c%c%c%c \n", mmc->cid[0] & 0xff,
  20. (mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
  21. (mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
  22. printf("Bus Speed: %d\n", mmc->clock);
  23. #if CONFIG_IS_ENABLED(MMC_VERBOSE)
  24. printf("Mode: %s\n", mmc_mode_name(mmc->selected_mode));
  25. mmc_dump_capabilities("card capabilities", mmc->card_caps);
  26. mmc_dump_capabilities("host capabilities", mmc->host_caps);
  27. #endif
  28. printf("Rd Block Len: %d\n", mmc->read_bl_len);
  29. printf("%s version %d.%d", IS_SD(mmc) ? "SD" : "MMC",
  30. EXTRACT_SDMMC_MAJOR_VERSION(mmc->version),
  31. EXTRACT_SDMMC_MINOR_VERSION(mmc->version));
  32. if (EXTRACT_SDMMC_CHANGE_VERSION(mmc->version) != 0)
  33. printf(".%d", EXTRACT_SDMMC_CHANGE_VERSION(mmc->version));
  34. printf("\n");
  35. printf("High Capacity: %s\n", mmc->high_capacity ? "Yes" : "No");
  36. puts("Capacity: ");
  37. print_size(mmc->capacity, "\n");
  38. printf("Bus Width: %d-bit%s\n", mmc->bus_width,
  39. mmc->ddr_mode ? " DDR" : "");
  40. #if CONFIG_IS_ENABLED(MMC_WRITE)
  41. puts("Erase Group Size: ");
  42. print_size(((u64)mmc->erase_grp_size) << 9, "\n");
  43. #endif
  44. if (!IS_SD(mmc) && mmc->version >= MMC_VERSION_4_41) {
  45. bool has_enh = (mmc->part_support & ENHNCD_SUPPORT) != 0;
  46. bool usr_enh = has_enh && (mmc->part_attr & EXT_CSD_ENH_USR);
  47. u8 wp, ext_csd[MMC_MAX_BLOCK_LEN];
  48. int ret;
  49. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  50. puts("HC WP Group Size: ");
  51. print_size(((u64)mmc->hc_wp_grp_size) << 9, "\n");
  52. #endif
  53. puts("User Capacity: ");
  54. print_size(mmc->capacity_user, usr_enh ? " ENH" : "");
  55. if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_USR)
  56. puts(" WRREL\n");
  57. else
  58. putc('\n');
  59. if (usr_enh) {
  60. puts("User Enhanced Start: ");
  61. print_size(mmc->enh_user_start, "\n");
  62. puts("User Enhanced Size: ");
  63. print_size(mmc->enh_user_size, "\n");
  64. }
  65. puts("Boot Capacity: ");
  66. print_size(mmc->capacity_boot, has_enh ? " ENH\n" : "\n");
  67. puts("RPMB Capacity: ");
  68. print_size(mmc->capacity_rpmb, has_enh ? " ENH\n" : "\n");
  69. for (i = 0; i < ARRAY_SIZE(mmc->capacity_gp); i++) {
  70. bool is_enh = has_enh &&
  71. (mmc->part_attr & EXT_CSD_ENH_GP(i));
  72. if (mmc->capacity_gp[i]) {
  73. printf("GP%i Capacity: ", i+1);
  74. print_size(mmc->capacity_gp[i],
  75. is_enh ? " ENH" : "");
  76. if (mmc->wr_rel_set & EXT_CSD_WR_DATA_REL_GP(i))
  77. puts(" WRREL\n");
  78. else
  79. putc('\n');
  80. }
  81. }
  82. ret = mmc_send_ext_csd(mmc, ext_csd);
  83. if (ret)
  84. return;
  85. wp = ext_csd[EXT_CSD_BOOT_WP_STATUS];
  86. for (i = 0; i < 2; ++i) {
  87. printf("Boot area %d is ", i);
  88. switch (wp & 3) {
  89. case 0:
  90. printf("not write protected\n");
  91. break;
  92. case 1:
  93. printf("power on protected\n");
  94. break;
  95. case 2:
  96. printf("permanently protected\n");
  97. break;
  98. default:
  99. printf("in reserved protection state\n");
  100. break;
  101. }
  102. wp >>= 2;
  103. }
  104. }
  105. }
  106. static struct mmc *init_mmc_device(int dev, bool force_init)
  107. {
  108. struct mmc *mmc;
  109. mmc = find_mmc_device(dev);
  110. if (!mmc) {
  111. printf("no mmc device at slot %x\n", dev);
  112. return NULL;
  113. }
  114. if (!mmc_getcd(mmc))
  115. force_init = true;
  116. if (force_init)
  117. mmc->has_init = 0;
  118. if (mmc_init(mmc))
  119. return NULL;
  120. #ifdef CONFIG_BLOCK_CACHE
  121. struct blk_desc *bd = mmc_get_blk_desc(mmc);
  122. blkcache_invalidate(bd->if_type, bd->devnum);
  123. #endif
  124. return mmc;
  125. }
  126. static int do_mmcinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  127. {
  128. struct mmc *mmc;
  129. if (curr_device < 0) {
  130. if (get_mmc_num() > 0)
  131. curr_device = 0;
  132. else {
  133. puts("No MMC device available\n");
  134. return 1;
  135. }
  136. }
  137. mmc = init_mmc_device(curr_device, false);
  138. if (!mmc)
  139. return CMD_RET_FAILURE;
  140. print_mmcinfo(mmc);
  141. return CMD_RET_SUCCESS;
  142. }
  143. #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
  144. static int confirm_key_prog(void)
  145. {
  146. puts("Warning: Programming authentication key can be done only once !\n"
  147. " Use this command only if you are sure of what you are doing,\n"
  148. "Really perform the key programming? <y/N> ");
  149. if (confirm_yesno())
  150. return 1;
  151. puts("Authentication key programming aborted\n");
  152. return 0;
  153. }
  154. static int do_mmcrpmb_key(cmd_tbl_t *cmdtp, int flag,
  155. int argc, char * const argv[])
  156. {
  157. void *key_addr;
  158. struct mmc *mmc = find_mmc_device(curr_device);
  159. if (argc != 2)
  160. return CMD_RET_USAGE;
  161. key_addr = (void *)simple_strtoul(argv[1], NULL, 16);
  162. if (!confirm_key_prog())
  163. return CMD_RET_FAILURE;
  164. if (mmc_rpmb_set_key(mmc, key_addr)) {
  165. printf("ERROR - Key already programmed ?\n");
  166. return CMD_RET_FAILURE;
  167. }
  168. return CMD_RET_SUCCESS;
  169. }
  170. static int do_mmcrpmb_read(cmd_tbl_t *cmdtp, int flag,
  171. int argc, char * const argv[])
  172. {
  173. u16 blk, cnt;
  174. void *addr;
  175. int n;
  176. void *key_addr = NULL;
  177. struct mmc *mmc = find_mmc_device(curr_device);
  178. if (argc < 4)
  179. return CMD_RET_USAGE;
  180. addr = (void *)simple_strtoul(argv[1], NULL, 16);
  181. blk = simple_strtoul(argv[2], NULL, 16);
  182. cnt = simple_strtoul(argv[3], NULL, 16);
  183. if (argc == 5)
  184. key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
  185. printf("\nMMC RPMB read: dev # %d, block # %d, count %d ... ",
  186. curr_device, blk, cnt);
  187. n = mmc_rpmb_read(mmc, addr, blk, cnt, key_addr);
  188. printf("%d RPMB blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
  189. if (n != cnt)
  190. return CMD_RET_FAILURE;
  191. return CMD_RET_SUCCESS;
  192. }
  193. static int do_mmcrpmb_write(cmd_tbl_t *cmdtp, int flag,
  194. int argc, char * const argv[])
  195. {
  196. u16 blk, cnt;
  197. void *addr;
  198. int n;
  199. void *key_addr;
  200. struct mmc *mmc = find_mmc_device(curr_device);
  201. if (argc != 5)
  202. return CMD_RET_USAGE;
  203. addr = (void *)simple_strtoul(argv[1], NULL, 16);
  204. blk = simple_strtoul(argv[2], NULL, 16);
  205. cnt = simple_strtoul(argv[3], NULL, 16);
  206. key_addr = (void *)simple_strtoul(argv[4], NULL, 16);
  207. printf("\nMMC RPMB write: dev # %d, block # %d, count %d ... ",
  208. curr_device, blk, cnt);
  209. n = mmc_rpmb_write(mmc, addr, blk, cnt, key_addr);
  210. printf("%d RPMB blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
  211. if (n != cnt)
  212. return CMD_RET_FAILURE;
  213. return CMD_RET_SUCCESS;
  214. }
  215. static int do_mmcrpmb_counter(cmd_tbl_t *cmdtp, int flag,
  216. int argc, char * const argv[])
  217. {
  218. unsigned long counter;
  219. struct mmc *mmc = find_mmc_device(curr_device);
  220. if (mmc_rpmb_get_counter(mmc, &counter))
  221. return CMD_RET_FAILURE;
  222. printf("RPMB Write counter= %lx\n", counter);
  223. return CMD_RET_SUCCESS;
  224. }
  225. static cmd_tbl_t cmd_rpmb[] = {
  226. U_BOOT_CMD_MKENT(key, 2, 0, do_mmcrpmb_key, "", ""),
  227. U_BOOT_CMD_MKENT(read, 5, 1, do_mmcrpmb_read, "", ""),
  228. U_BOOT_CMD_MKENT(write, 5, 0, do_mmcrpmb_write, "", ""),
  229. U_BOOT_CMD_MKENT(counter, 1, 1, do_mmcrpmb_counter, "", ""),
  230. };
  231. static int do_mmcrpmb(cmd_tbl_t *cmdtp, int flag,
  232. int argc, char * const argv[])
  233. {
  234. cmd_tbl_t *cp;
  235. struct mmc *mmc;
  236. char original_part;
  237. int ret;
  238. cp = find_cmd_tbl(argv[1], cmd_rpmb, ARRAY_SIZE(cmd_rpmb));
  239. /* Drop the rpmb subcommand */
  240. argc--;
  241. argv++;
  242. if (cp == NULL || argc > cp->maxargs)
  243. return CMD_RET_USAGE;
  244. if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
  245. return CMD_RET_SUCCESS;
  246. mmc = init_mmc_device(curr_device, false);
  247. if (!mmc)
  248. return CMD_RET_FAILURE;
  249. if (!(mmc->version & MMC_VERSION_MMC)) {
  250. printf("It is not an eMMC device\n");
  251. return CMD_RET_FAILURE;
  252. }
  253. if (mmc->version < MMC_VERSION_4_41) {
  254. printf("RPMB not supported before version 4.41\n");
  255. return CMD_RET_FAILURE;
  256. }
  257. /* Switch to the RPMB partition */
  258. #ifndef CONFIG_BLK
  259. original_part = mmc->block_dev.hwpart;
  260. #else
  261. original_part = mmc_get_blk_desc(mmc)->hwpart;
  262. #endif
  263. if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, MMC_PART_RPMB) !=
  264. 0)
  265. return CMD_RET_FAILURE;
  266. ret = cp->cmd(cmdtp, flag, argc, argv);
  267. /* Return to original partition */
  268. if (blk_select_hwpart_devnum(IF_TYPE_MMC, curr_device, original_part) !=
  269. 0)
  270. return CMD_RET_FAILURE;
  271. return ret;
  272. }
  273. #endif
  274. static int do_mmc_read(cmd_tbl_t *cmdtp, int flag,
  275. int argc, char * const argv[])
  276. {
  277. struct mmc *mmc;
  278. u32 blk, cnt, n;
  279. void *addr;
  280. if (argc != 4)
  281. return CMD_RET_USAGE;
  282. addr = (void *)simple_strtoul(argv[1], NULL, 16);
  283. blk = simple_strtoul(argv[2], NULL, 16);
  284. cnt = simple_strtoul(argv[3], NULL, 16);
  285. mmc = init_mmc_device(curr_device, false);
  286. if (!mmc)
  287. return CMD_RET_FAILURE;
  288. printf("\nMMC read: dev # %d, block # %d, count %d ... ",
  289. curr_device, blk, cnt);
  290. n = blk_dread(mmc_get_blk_desc(mmc), blk, cnt, addr);
  291. printf("%d blocks read: %s\n", n, (n == cnt) ? "OK" : "ERROR");
  292. return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
  293. }
  294. #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
  295. static lbaint_t mmc_sparse_write(struct sparse_storage *info, lbaint_t blk,
  296. lbaint_t blkcnt, const void *buffer)
  297. {
  298. struct blk_desc *dev_desc = info->priv;
  299. return blk_dwrite(dev_desc, blk, blkcnt, buffer);
  300. }
  301. static lbaint_t mmc_sparse_reserve(struct sparse_storage *info,
  302. lbaint_t blk, lbaint_t blkcnt)
  303. {
  304. return blkcnt;
  305. }
  306. static int do_mmc_sparse_write(cmd_tbl_t *cmdtp, int flag,
  307. int argc, char * const argv[])
  308. {
  309. struct sparse_storage sparse;
  310. struct blk_desc *dev_desc;
  311. struct mmc *mmc;
  312. char dest[11];
  313. void *addr;
  314. u32 blk;
  315. if (argc != 3)
  316. return CMD_RET_USAGE;
  317. addr = (void *)simple_strtoul(argv[1], NULL, 16);
  318. blk = simple_strtoul(argv[2], NULL, 16);
  319. if (!is_sparse_image(addr)) {
  320. printf("Not a sparse image\n");
  321. return CMD_RET_FAILURE;
  322. }
  323. mmc = init_mmc_device(curr_device, false);
  324. if (!mmc)
  325. return CMD_RET_FAILURE;
  326. printf("\nMMC Sparse write: dev # %d, block # %d ... ",
  327. curr_device, blk);
  328. if (mmc_getwp(mmc) == 1) {
  329. printf("Error: card is write protected!\n");
  330. return CMD_RET_FAILURE;
  331. }
  332. dev_desc = mmc_get_blk_desc(mmc);
  333. sparse.priv = dev_desc;
  334. sparse.blksz = 512;
  335. sparse.start = blk;
  336. sparse.size = dev_desc->lba - blk;
  337. sparse.write = mmc_sparse_write;
  338. sparse.reserve = mmc_sparse_reserve;
  339. sparse.mssg = NULL;
  340. sprintf(dest, "0x" LBAF, sparse.start * sparse.blksz);
  341. if (write_sparse_image(&sparse, dest, addr, NULL))
  342. return CMD_RET_FAILURE;
  343. else
  344. return CMD_RET_SUCCESS;
  345. }
  346. #endif
  347. #if CONFIG_IS_ENABLED(MMC_WRITE)
  348. static int do_mmc_write(cmd_tbl_t *cmdtp, int flag,
  349. int argc, char * const argv[])
  350. {
  351. struct mmc *mmc;
  352. u32 blk, cnt, n;
  353. void *addr;
  354. if (argc != 4)
  355. return CMD_RET_USAGE;
  356. addr = (void *)simple_strtoul(argv[1], NULL, 16);
  357. blk = simple_strtoul(argv[2], NULL, 16);
  358. cnt = simple_strtoul(argv[3], NULL, 16);
  359. mmc = init_mmc_device(curr_device, false);
  360. if (!mmc)
  361. return CMD_RET_FAILURE;
  362. printf("\nMMC write: dev # %d, block # %d, count %d ... ",
  363. curr_device, blk, cnt);
  364. if (mmc_getwp(mmc) == 1) {
  365. printf("Error: card is write protected!\n");
  366. return CMD_RET_FAILURE;
  367. }
  368. n = blk_dwrite(mmc_get_blk_desc(mmc), blk, cnt, addr);
  369. printf("%d blocks written: %s\n", n, (n == cnt) ? "OK" : "ERROR");
  370. return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
  371. }
  372. static int do_mmc_erase(cmd_tbl_t *cmdtp, int flag,
  373. int argc, char * const argv[])
  374. {
  375. struct mmc *mmc;
  376. u32 blk, cnt, n;
  377. if (argc != 3)
  378. return CMD_RET_USAGE;
  379. blk = simple_strtoul(argv[1], NULL, 16);
  380. cnt = simple_strtoul(argv[2], NULL, 16);
  381. mmc = init_mmc_device(curr_device, false);
  382. if (!mmc)
  383. return CMD_RET_FAILURE;
  384. printf("\nMMC erase: dev # %d, block # %d, count %d ... ",
  385. curr_device, blk, cnt);
  386. if (mmc_getwp(mmc) == 1) {
  387. printf("Error: card is write protected!\n");
  388. return CMD_RET_FAILURE;
  389. }
  390. n = blk_derase(mmc_get_blk_desc(mmc), blk, cnt);
  391. printf("%d blocks erased: %s\n", n, (n == cnt) ? "OK" : "ERROR");
  392. return (n == cnt) ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
  393. }
  394. #endif
  395. static int do_mmc_rescan(cmd_tbl_t *cmdtp, int flag,
  396. int argc, char * const argv[])
  397. {
  398. struct mmc *mmc;
  399. mmc = init_mmc_device(curr_device, true);
  400. if (!mmc)
  401. return CMD_RET_FAILURE;
  402. return CMD_RET_SUCCESS;
  403. }
  404. static int do_mmc_part(cmd_tbl_t *cmdtp, int flag,
  405. int argc, char * const argv[])
  406. {
  407. struct blk_desc *mmc_dev;
  408. struct mmc *mmc;
  409. mmc = init_mmc_device(curr_device, false);
  410. if (!mmc)
  411. return CMD_RET_FAILURE;
  412. mmc_dev = blk_get_devnum_by_type(IF_TYPE_MMC, curr_device);
  413. if (mmc_dev != NULL && mmc_dev->type != DEV_TYPE_UNKNOWN) {
  414. part_print(mmc_dev);
  415. return CMD_RET_SUCCESS;
  416. }
  417. puts("get mmc type error!\n");
  418. return CMD_RET_FAILURE;
  419. }
  420. static int do_mmc_dev(cmd_tbl_t *cmdtp, int flag,
  421. int argc, char * const argv[])
  422. {
  423. int dev, part = 0, ret;
  424. struct mmc *mmc;
  425. if (argc == 1) {
  426. dev = curr_device;
  427. } else if (argc == 2) {
  428. dev = simple_strtoul(argv[1], NULL, 10);
  429. } else if (argc == 3) {
  430. dev = (int)simple_strtoul(argv[1], NULL, 10);
  431. part = (int)simple_strtoul(argv[2], NULL, 10);
  432. if (part > PART_ACCESS_MASK) {
  433. printf("#part_num shouldn't be larger than %d\n",
  434. PART_ACCESS_MASK);
  435. return CMD_RET_FAILURE;
  436. }
  437. } else {
  438. return CMD_RET_USAGE;
  439. }
  440. mmc = init_mmc_device(dev, true);
  441. if (!mmc)
  442. return CMD_RET_FAILURE;
  443. ret = blk_select_hwpart_devnum(IF_TYPE_MMC, dev, part);
  444. printf("switch to partitions #%d, %s\n",
  445. part, (!ret) ? "OK" : "ERROR");
  446. if (ret)
  447. return 1;
  448. curr_device = dev;
  449. if (mmc->part_config == MMCPART_NOAVAILABLE)
  450. printf("mmc%d is current device\n", curr_device);
  451. else
  452. printf("mmc%d(part %d) is current device\n",
  453. curr_device, mmc_get_blk_desc(mmc)->hwpart);
  454. return CMD_RET_SUCCESS;
  455. }
  456. static int do_mmc_list(cmd_tbl_t *cmdtp, int flag,
  457. int argc, char * const argv[])
  458. {
  459. print_mmc_devices('\n');
  460. return CMD_RET_SUCCESS;
  461. }
  462. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  463. static int parse_hwpart_user(struct mmc_hwpart_conf *pconf,
  464. int argc, char * const argv[])
  465. {
  466. int i = 0;
  467. memset(&pconf->user, 0, sizeof(pconf->user));
  468. while (i < argc) {
  469. if (!strcmp(argv[i], "enh")) {
  470. if (i + 2 >= argc)
  471. return -1;
  472. pconf->user.enh_start =
  473. simple_strtoul(argv[i+1], NULL, 10);
  474. pconf->user.enh_size =
  475. simple_strtoul(argv[i+2], NULL, 10);
  476. i += 3;
  477. } else if (!strcmp(argv[i], "wrrel")) {
  478. if (i + 1 >= argc)
  479. return -1;
  480. pconf->user.wr_rel_change = 1;
  481. if (!strcmp(argv[i+1], "on"))
  482. pconf->user.wr_rel_set = 1;
  483. else if (!strcmp(argv[i+1], "off"))
  484. pconf->user.wr_rel_set = 0;
  485. else
  486. return -1;
  487. i += 2;
  488. } else {
  489. break;
  490. }
  491. }
  492. return i;
  493. }
  494. static int parse_hwpart_gp(struct mmc_hwpart_conf *pconf, int pidx,
  495. int argc, char * const argv[])
  496. {
  497. int i;
  498. memset(&pconf->gp_part[pidx], 0, sizeof(pconf->gp_part[pidx]));
  499. if (1 >= argc)
  500. return -1;
  501. pconf->gp_part[pidx].size = simple_strtoul(argv[0], NULL, 10);
  502. i = 1;
  503. while (i < argc) {
  504. if (!strcmp(argv[i], "enh")) {
  505. pconf->gp_part[pidx].enhanced = 1;
  506. i += 1;
  507. } else if (!strcmp(argv[i], "wrrel")) {
  508. if (i + 1 >= argc)
  509. return -1;
  510. pconf->gp_part[pidx].wr_rel_change = 1;
  511. if (!strcmp(argv[i+1], "on"))
  512. pconf->gp_part[pidx].wr_rel_set = 1;
  513. else if (!strcmp(argv[i+1], "off"))
  514. pconf->gp_part[pidx].wr_rel_set = 0;
  515. else
  516. return -1;
  517. i += 2;
  518. } else {
  519. break;
  520. }
  521. }
  522. return i;
  523. }
  524. static int do_mmc_hwpartition(cmd_tbl_t *cmdtp, int flag,
  525. int argc, char * const argv[])
  526. {
  527. struct mmc *mmc;
  528. struct mmc_hwpart_conf pconf = { };
  529. enum mmc_hwpart_conf_mode mode = MMC_HWPART_CONF_CHECK;
  530. int i, r, pidx;
  531. mmc = init_mmc_device(curr_device, false);
  532. if (!mmc)
  533. return CMD_RET_FAILURE;
  534. if (argc < 1)
  535. return CMD_RET_USAGE;
  536. i = 1;
  537. while (i < argc) {
  538. if (!strcmp(argv[i], "user")) {
  539. i++;
  540. r = parse_hwpart_user(&pconf, argc-i, &argv[i]);
  541. if (r < 0)
  542. return CMD_RET_USAGE;
  543. i += r;
  544. } else if (!strncmp(argv[i], "gp", 2) &&
  545. strlen(argv[i]) == 3 &&
  546. argv[i][2] >= '1' && argv[i][2] <= '4') {
  547. pidx = argv[i][2] - '1';
  548. i++;
  549. r = parse_hwpart_gp(&pconf, pidx, argc-i, &argv[i]);
  550. if (r < 0)
  551. return CMD_RET_USAGE;
  552. i += r;
  553. } else if (!strcmp(argv[i], "check")) {
  554. mode = MMC_HWPART_CONF_CHECK;
  555. i++;
  556. } else if (!strcmp(argv[i], "set")) {
  557. mode = MMC_HWPART_CONF_SET;
  558. i++;
  559. } else if (!strcmp(argv[i], "complete")) {
  560. mode = MMC_HWPART_CONF_COMPLETE;
  561. i++;
  562. } else {
  563. return CMD_RET_USAGE;
  564. }
  565. }
  566. puts("Partition configuration:\n");
  567. if (pconf.user.enh_size) {
  568. puts("\tUser Enhanced Start: ");
  569. print_size(((u64)pconf.user.enh_start) << 9, "\n");
  570. puts("\tUser Enhanced Size: ");
  571. print_size(((u64)pconf.user.enh_size) << 9, "\n");
  572. } else {
  573. puts("\tNo enhanced user data area\n");
  574. }
  575. if (pconf.user.wr_rel_change)
  576. printf("\tUser partition write reliability: %s\n",
  577. pconf.user.wr_rel_set ? "on" : "off");
  578. for (pidx = 0; pidx < 4; pidx++) {
  579. if (pconf.gp_part[pidx].size) {
  580. printf("\tGP%i Capacity: ", pidx+1);
  581. print_size(((u64)pconf.gp_part[pidx].size) << 9,
  582. pconf.gp_part[pidx].enhanced ?
  583. " ENH\n" : "\n");
  584. } else {
  585. printf("\tNo GP%i partition\n", pidx+1);
  586. }
  587. if (pconf.gp_part[pidx].wr_rel_change)
  588. printf("\tGP%i write reliability: %s\n", pidx+1,
  589. pconf.gp_part[pidx].wr_rel_set ? "on" : "off");
  590. }
  591. if (!mmc_hwpart_config(mmc, &pconf, mode)) {
  592. if (mode == MMC_HWPART_CONF_COMPLETE)
  593. puts("Partitioning successful, "
  594. "power-cycle to make effective\n");
  595. return CMD_RET_SUCCESS;
  596. } else {
  597. puts("Failed!\n");
  598. return CMD_RET_FAILURE;
  599. }
  600. }
  601. #endif
  602. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  603. static int do_mmc_bootbus(cmd_tbl_t *cmdtp, int flag,
  604. int argc, char * const argv[])
  605. {
  606. int dev;
  607. struct mmc *mmc;
  608. u8 width, reset, mode;
  609. if (argc != 5)
  610. return CMD_RET_USAGE;
  611. dev = simple_strtoul(argv[1], NULL, 10);
  612. width = simple_strtoul(argv[2], NULL, 10);
  613. reset = simple_strtoul(argv[3], NULL, 10);
  614. mode = simple_strtoul(argv[4], NULL, 10);
  615. mmc = init_mmc_device(dev, false);
  616. if (!mmc)
  617. return CMD_RET_FAILURE;
  618. if (IS_SD(mmc)) {
  619. puts("BOOT_BUS_WIDTH only exists on eMMC\n");
  620. return CMD_RET_FAILURE;
  621. }
  622. /* acknowledge to be sent during boot operation */
  623. return mmc_set_boot_bus_width(mmc, width, reset, mode);
  624. }
  625. static int do_mmc_boot_resize(cmd_tbl_t *cmdtp, int flag,
  626. int argc, char * const argv[])
  627. {
  628. int dev;
  629. struct mmc *mmc;
  630. u32 bootsize, rpmbsize;
  631. if (argc != 4)
  632. return CMD_RET_USAGE;
  633. dev = simple_strtoul(argv[1], NULL, 10);
  634. bootsize = simple_strtoul(argv[2], NULL, 10);
  635. rpmbsize = simple_strtoul(argv[3], NULL, 10);
  636. mmc = init_mmc_device(dev, false);
  637. if (!mmc)
  638. return CMD_RET_FAILURE;
  639. if (IS_SD(mmc)) {
  640. printf("It is not an eMMC device\n");
  641. return CMD_RET_FAILURE;
  642. }
  643. if (mmc_boot_partition_size_change(mmc, bootsize, rpmbsize)) {
  644. printf("EMMC boot partition Size change Failed.\n");
  645. return CMD_RET_FAILURE;
  646. }
  647. printf("EMMC boot partition Size %d MB\n", bootsize);
  648. printf("EMMC RPMB partition Size %d MB\n", rpmbsize);
  649. return CMD_RET_SUCCESS;
  650. }
  651. static int mmc_partconf_print(struct mmc *mmc)
  652. {
  653. u8 ack, access, part;
  654. if (mmc->part_config == MMCPART_NOAVAILABLE) {
  655. printf("No part_config info for ver. 0x%x\n", mmc->version);
  656. return CMD_RET_FAILURE;
  657. }
  658. access = EXT_CSD_EXTRACT_PARTITION_ACCESS(mmc->part_config);
  659. ack = EXT_CSD_EXTRACT_BOOT_ACK(mmc->part_config);
  660. part = EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config);
  661. printf("EXT_CSD[179], PARTITION_CONFIG:\n"
  662. "BOOT_ACK: 0x%x\n"
  663. "BOOT_PARTITION_ENABLE: 0x%x\n"
  664. "PARTITION_ACCESS: 0x%x\n", ack, part, access);
  665. return CMD_RET_SUCCESS;
  666. }
  667. static int do_mmc_partconf(cmd_tbl_t *cmdtp, int flag,
  668. int argc, char * const argv[])
  669. {
  670. int dev;
  671. struct mmc *mmc;
  672. u8 ack, part_num, access;
  673. if (argc != 2 && argc != 5)
  674. return CMD_RET_USAGE;
  675. dev = simple_strtoul(argv[1], NULL, 10);
  676. mmc = init_mmc_device(dev, false);
  677. if (!mmc)
  678. return CMD_RET_FAILURE;
  679. if (IS_SD(mmc)) {
  680. puts("PARTITION_CONFIG only exists on eMMC\n");
  681. return CMD_RET_FAILURE;
  682. }
  683. if (argc == 2)
  684. return mmc_partconf_print(mmc);
  685. ack = simple_strtoul(argv[2], NULL, 10);
  686. part_num = simple_strtoul(argv[3], NULL, 10);
  687. access = simple_strtoul(argv[4], NULL, 10);
  688. /* acknowledge to be sent during boot operation */
  689. return mmc_set_part_conf(mmc, ack, part_num, access);
  690. }
  691. static int do_mmc_rst_func(cmd_tbl_t *cmdtp, int flag,
  692. int argc, char * const argv[])
  693. {
  694. int dev;
  695. struct mmc *mmc;
  696. u8 enable;
  697. /*
  698. * Set the RST_n_ENABLE bit of RST_n_FUNCTION
  699. * The only valid values are 0x0, 0x1 and 0x2 and writing
  700. * a value of 0x1 or 0x2 sets the value permanently.
  701. */
  702. if (argc != 3)
  703. return CMD_RET_USAGE;
  704. dev = simple_strtoul(argv[1], NULL, 10);
  705. enable = simple_strtoul(argv[2], NULL, 10);
  706. if (enable > 2) {
  707. puts("Invalid RST_n_ENABLE value\n");
  708. return CMD_RET_USAGE;
  709. }
  710. mmc = init_mmc_device(dev, false);
  711. if (!mmc)
  712. return CMD_RET_FAILURE;
  713. if (IS_SD(mmc)) {
  714. puts("RST_n_FUNCTION only exists on eMMC\n");
  715. return CMD_RET_FAILURE;
  716. }
  717. return mmc_set_rst_n_function(mmc, enable);
  718. }
  719. #endif
  720. static int do_mmc_setdsr(cmd_tbl_t *cmdtp, int flag,
  721. int argc, char * const argv[])
  722. {
  723. struct mmc *mmc;
  724. u32 val;
  725. int ret;
  726. if (argc != 2)
  727. return CMD_RET_USAGE;
  728. val = simple_strtoul(argv[1], NULL, 16);
  729. mmc = find_mmc_device(curr_device);
  730. if (!mmc) {
  731. printf("no mmc device at slot %x\n", curr_device);
  732. return CMD_RET_FAILURE;
  733. }
  734. ret = mmc_set_dsr(mmc, val);
  735. printf("set dsr %s\n", (!ret) ? "OK, force rescan" : "ERROR");
  736. if (!ret) {
  737. mmc->has_init = 0;
  738. if (mmc_init(mmc))
  739. return CMD_RET_FAILURE;
  740. else
  741. return CMD_RET_SUCCESS;
  742. }
  743. return ret;
  744. }
  745. #ifdef CONFIG_CMD_BKOPS_ENABLE
  746. static int do_mmc_bkops_enable(cmd_tbl_t *cmdtp, int flag,
  747. int argc, char * const argv[])
  748. {
  749. int dev;
  750. struct mmc *mmc;
  751. if (argc != 2)
  752. return CMD_RET_USAGE;
  753. dev = simple_strtoul(argv[1], NULL, 10);
  754. mmc = init_mmc_device(dev, false);
  755. if (!mmc)
  756. return CMD_RET_FAILURE;
  757. if (IS_SD(mmc)) {
  758. puts("BKOPS_EN only exists on eMMC\n");
  759. return CMD_RET_FAILURE;
  760. }
  761. return mmc_set_bkops_enable(mmc);
  762. }
  763. #endif
  764. static int do_mmc_boot_wp(cmd_tbl_t *cmdtp, int flag,
  765. int argc, char * const argv[])
  766. {
  767. int err;
  768. struct mmc *mmc;
  769. mmc = init_mmc_device(curr_device, false);
  770. if (!mmc)
  771. return CMD_RET_FAILURE;
  772. if (IS_SD(mmc)) {
  773. printf("It is not an eMMC device\n");
  774. return CMD_RET_FAILURE;
  775. }
  776. err = mmc_boot_wp(mmc);
  777. if (err)
  778. return CMD_RET_FAILURE;
  779. printf("boot areas protected\n");
  780. return CMD_RET_SUCCESS;
  781. }
  782. static cmd_tbl_t cmd_mmc[] = {
  783. U_BOOT_CMD_MKENT(info, 1, 0, do_mmcinfo, "", ""),
  784. U_BOOT_CMD_MKENT(read, 4, 1, do_mmc_read, "", ""),
  785. U_BOOT_CMD_MKENT(wp, 1, 0, do_mmc_boot_wp, "", ""),
  786. #if CONFIG_IS_ENABLED(MMC_WRITE)
  787. U_BOOT_CMD_MKENT(write, 4, 0, do_mmc_write, "", ""),
  788. U_BOOT_CMD_MKENT(erase, 3, 0, do_mmc_erase, "", ""),
  789. #endif
  790. #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
  791. U_BOOT_CMD_MKENT(swrite, 3, 0, do_mmc_sparse_write, "", ""),
  792. #endif
  793. U_BOOT_CMD_MKENT(rescan, 1, 1, do_mmc_rescan, "", ""),
  794. U_BOOT_CMD_MKENT(part, 1, 1, do_mmc_part, "", ""),
  795. U_BOOT_CMD_MKENT(dev, 3, 0, do_mmc_dev, "", ""),
  796. U_BOOT_CMD_MKENT(list, 1, 1, do_mmc_list, "", ""),
  797. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  798. U_BOOT_CMD_MKENT(hwpartition, 28, 0, do_mmc_hwpartition, "", ""),
  799. #endif
  800. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  801. U_BOOT_CMD_MKENT(bootbus, 5, 0, do_mmc_bootbus, "", ""),
  802. U_BOOT_CMD_MKENT(bootpart-resize, 4, 0, do_mmc_boot_resize, "", ""),
  803. U_BOOT_CMD_MKENT(partconf, 5, 0, do_mmc_partconf, "", ""),
  804. U_BOOT_CMD_MKENT(rst-function, 3, 0, do_mmc_rst_func, "", ""),
  805. #endif
  806. #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
  807. U_BOOT_CMD_MKENT(rpmb, CONFIG_SYS_MAXARGS, 1, do_mmcrpmb, "", ""),
  808. #endif
  809. U_BOOT_CMD_MKENT(setdsr, 2, 0, do_mmc_setdsr, "", ""),
  810. #ifdef CONFIG_CMD_BKOPS_ENABLE
  811. U_BOOT_CMD_MKENT(bkops-enable, 2, 0, do_mmc_bkops_enable, "", ""),
  812. #endif
  813. };
  814. static int do_mmcops(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  815. {
  816. cmd_tbl_t *cp;
  817. cp = find_cmd_tbl(argv[1], cmd_mmc, ARRAY_SIZE(cmd_mmc));
  818. /* Drop the mmc command */
  819. argc--;
  820. argv++;
  821. if (cp == NULL || argc > cp->maxargs)
  822. return CMD_RET_USAGE;
  823. if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp))
  824. return CMD_RET_SUCCESS;
  825. if (curr_device < 0) {
  826. if (get_mmc_num() > 0) {
  827. curr_device = 0;
  828. } else {
  829. puts("No MMC device available\n");
  830. return CMD_RET_FAILURE;
  831. }
  832. }
  833. return cp->cmd(cmdtp, flag, argc, argv);
  834. }
  835. U_BOOT_CMD(
  836. mmc, 29, 1, do_mmcops,
  837. "MMC sub system",
  838. "info - display info of the current MMC device\n"
  839. "mmc read addr blk# cnt\n"
  840. "mmc write addr blk# cnt\n"
  841. #if CONFIG_IS_ENABLED(CMD_MMC_SWRITE)
  842. "mmc swrite addr blk#\n"
  843. #endif
  844. "mmc erase blk# cnt\n"
  845. "mmc rescan\n"
  846. "mmc part - lists available partition on current mmc device\n"
  847. "mmc dev [dev] [part] - show or set current mmc device [partition]\n"
  848. "mmc list - lists available devices\n"
  849. "mmc wp - power on write protect booot partitions\n"
  850. #if CONFIG_IS_ENABLED(MMC_HW_PARTITIONING)
  851. "mmc hwpartition [args...] - does hardware partitioning\n"
  852. " arguments (sizes in 512-byte blocks):\n"
  853. " [user [enh start cnt] [wrrel {on|off}]] - sets user data area attributes\n"
  854. " [gp1|gp2|gp3|gp4 cnt [enh] [wrrel {on|off}]] - general purpose partition\n"
  855. " [check|set|complete] - mode, complete set partitioning completed\n"
  856. " WARNING: Partitioning is a write-once setting once it is set to complete.\n"
  857. " Power cycling is required to initialize partitions after set to complete.\n"
  858. #endif
  859. #ifdef CONFIG_SUPPORT_EMMC_BOOT
  860. "mmc bootbus dev boot_bus_width reset_boot_bus_width boot_mode\n"
  861. " - Set the BOOT_BUS_WIDTH field of the specified device\n"
  862. "mmc bootpart-resize <dev> <boot part size MB> <RPMB part size MB>\n"
  863. " - Change sizes of boot and RPMB partitions of specified device\n"
  864. "mmc partconf dev [boot_ack boot_partition partition_access]\n"
  865. " - Show or change the bits of the PARTITION_CONFIG field of the specified device\n"
  866. "mmc rst-function dev value\n"
  867. " - Change the RST_n_FUNCTION field of the specified device\n"
  868. " WARNING: This is a write-once field and 0 / 1 / 2 are the only valid values.\n"
  869. #endif
  870. #if CONFIG_IS_ENABLED(CMD_MMC_RPMB)
  871. "mmc rpmb read addr blk# cnt [address of auth-key] - block size is 256 bytes\n"
  872. "mmc rpmb write addr blk# cnt <address of auth-key> - block size is 256 bytes\n"
  873. "mmc rpmb key <address of auth-key> - program the RPMB authentication key.\n"
  874. "mmc rpmb counter - read the value of the write counter\n"
  875. #endif
  876. "mmc setdsr <value> - set DSR register value\n"
  877. #ifdef CONFIG_CMD_BKOPS_ENABLE
  878. "mmc bkops-enable <dev> - enable background operations handshake on device\n"
  879. " WARNING: This is a write-once setting.\n"
  880. #endif
  881. );
  882. /* Old command kept for compatibility. Same as 'mmc info' */
  883. U_BOOT_CMD(
  884. mmcinfo, 1, 0, do_mmcinfo,
  885. "display MMC info",
  886. "- display info of the current MMC device"
  887. );