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