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