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