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