mmc.c 26 KB

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