1 /* 2 * This program is distributed in the hope that it will be useful, 3 * but WITHOUT ANY WARRANTY; without even the implied warranty of 4 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 5 * GNU General Public License for more details. 6 * 7 * You should have received a copy of the GNU General Public License 8 * along with this program; if not, write to the Free Software 9 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 10 */ 11 #include <stdlib.h> 12 #include <common.h> 13 #include <console.h> 14 #include <bootretry.h> 15 #include <cli.h> 16 #include <command.h> 17 #include <console.h> 18 #include <malloc.h> 19 #include <inttypes.h> 20 #include <mapmem.h> 21 #include <asm/io.h> 22 #include <linux/compiler.h> 23 #include <u-boot/sha256.h> 24 #include "otp_info.h" 25 26 DECLARE_GLOBAL_DATA_PTR; 27 28 #define OTP_VER "1.0.1" 29 30 #define OTP_PASSWD 0x349fe38a 31 #define RETRY 20 32 #define OTP_REGION_STRAP BIT(0) 33 #define OTP_REGION_CONF BIT(1) 34 #define OTP_REGION_DATA BIT(2) 35 36 #define OTP_USAGE -1 37 #define OTP_FAILURE -2 38 #define OTP_SUCCESS 0 39 40 #define OTP_PROG_SKIP 1 41 42 #define OTP_KEY_TYPE_RSA 1 43 #define OTP_KEY_TYPE_AES 2 44 #define OTP_KEY_TYPE_VAULT 3 45 #define OTP_KEY_TYPE_HMAC 4 46 47 #define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||" 48 #define PBWIDTH 60 49 50 #define OTP_BASE 0x1e6f2000 51 #define OTP_PROTECT_KEY OTP_BASE 52 #define OTP_COMMAND OTP_BASE + 0x4 53 #define OTP_TIMING OTP_BASE + 0x8 54 #define OTP_ADDR OTP_BASE + 0x10 55 #define OTP_STATUS OTP_BASE + 0x14 56 #define OTP_COMPARE_1 OTP_BASE + 0x20 57 #define OTP_COMPARE_2 OTP_BASE + 0x24 58 #define OTP_COMPARE_3 OTP_BASE + 0x28 59 #define OTP_COMPARE_4 OTP_BASE + 0x2c 60 61 #define OTP_MAGIC "SOCOTP" 62 #define CHECKSUM_LEN 32 63 #define OTP_INC_DATA (1 << 31) 64 #define OTP_INC_CONFIG (1 << 30) 65 #define OTP_INC_STRAP (1 << 29) 66 #define OTP_ECC_EN (1 << 28) 67 #define OTP_REGION_SIZE(info) ((info >> 16) & 0xffff) 68 #define OTP_REGION_OFFSET(info) (info & 0xffff) 69 #define OTP_IMAGE_SIZE(info) (info & 0xffff) 70 71 #define OTP_AST2600A0 0 72 #define OTP_AST2600A1 1 73 #define OTP_AST2600A2 2 74 75 struct otp_header { 76 u8 otp_magic[8]; 77 u8 otp_version[8]; 78 u32 image_info; 79 u32 data_info; 80 u32 config_info; 81 u32 strap_info; 82 u32 checksum_offset; 83 } __attribute__((packed)); 84 85 struct otpstrap_status { 86 int value; 87 int option_array[7]; 88 int remain_times; 89 int writeable_option; 90 int reg_protected; 91 int protected; 92 }; 93 94 struct otpconf_parse { 95 int dw_offset; 96 int bit; 97 int length; 98 int value; 99 int ignore; 100 char status[80]; 101 }; 102 103 struct otpkey_type { 104 int value; 105 int key_type; 106 int need_id; 107 char information[110]; 108 }; 109 110 struct otp_info_cb { 111 int version; 112 const struct otpstrap_info *strap_info; 113 int strap_info_len; 114 const struct otpconf_info *conf_info; 115 int conf_info_len; 116 const struct otpkey_type *key_info; 117 int key_info_len; 118 119 }; 120 121 struct otp_image_layout { 122 int data_length; 123 int conf_length; 124 int strap_length; 125 uint8_t *data; 126 uint8_t *data_ignore; 127 uint8_t *conf; 128 uint8_t *conf_ignore; 129 uint8_t *strap; 130 uint8_t *strap_reg_pro; 131 uint8_t *strap_pro; 132 uint8_t *strap_ignore; 133 }; 134 135 static struct otp_info_cb info_cb; 136 137 static const struct otpkey_type a0_key_type[] = { 138 {0, OTP_KEY_TYPE_AES, 0, "AES-256 as OEM platform key for image encryption/decryption"}, 139 {1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"}, 140 {4, OTP_KEY_TYPE_HMAC, 1, "HMAC as encrypted OEM HMAC keys in Mode 1"}, 141 {8, OTP_KEY_TYPE_RSA, 1, "RSA-public as OEM DSS public keys in Mode 2"}, 142 {9, OTP_KEY_TYPE_RSA, 0, "RSA-public as SOC public key"}, 143 {10, OTP_KEY_TYPE_RSA, 0, "RSA-public as AES key decryption key"}, 144 {13, OTP_KEY_TYPE_RSA, 0, "RSA-private as SOC private key"}, 145 {14, OTP_KEY_TYPE_RSA, 0, "RSA-private as AES key decryption key"}, 146 }; 147 148 static const struct otpkey_type a1_key_type[] = { 149 {1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"}, 150 {2, OTP_KEY_TYPE_AES, 1, "AES-256 as OEM platform key for image encryption/decryption in Mode 2 or AES-256 as OEM DSS keys for Mode GCM"}, 151 {8, OTP_KEY_TYPE_RSA, 1, "RSA-public as OEM DSS public keys in Mode 2"}, 152 {10, OTP_KEY_TYPE_RSA, 0, "RSA-public as AES key decryption key"}, 153 {14, OTP_KEY_TYPE_RSA, 0, "RSA-private as AES key decryption key"}, 154 }; 155 156 static const struct otpkey_type a2_key_type[] = { 157 {1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"}, 158 {2, OTP_KEY_TYPE_AES, 1, "AES-256 as OEM platform key for image encryption/decryption in Mode 2 or AES-256 as OEM DSS keys for Mode GCM"}, 159 {8, OTP_KEY_TYPE_RSA, 1, "RSA-public as OEM DSS public keys in Mode 2"}, 160 {10, OTP_KEY_TYPE_RSA, 0, "RSA-public as AES key decryption key"}, 161 {14, OTP_KEY_TYPE_RSA, 0, "RSA-private as AES key decryption key"}, 162 }; 163 164 static uint32_t chip_version(void) 165 { 166 uint32_t rev_id[2]; 167 168 rev_id[0] = readl(0x1e6e2004); 169 rev_id[1] = readl(0x1e6e2014); 170 171 if (rev_id[0] == 0x05000303 && rev_id[1] == 0x05000303) { 172 return OTP_AST2600A0; 173 } else if (rev_id[0] == 0x05010303 && rev_id[1] == 0x05010303) { 174 return OTP_AST2600A1; 175 } else if (rev_id[0] == 0x05010303 && rev_id[1] == 0x05020303) { 176 return OTP_AST2600A2; 177 } else if (rev_id[0] == 0x05010203 && rev_id[1] == 0x05010203) { 178 return OTP_AST2600A1; 179 } else if (rev_id[0] == 0x05010203 && rev_id[1] == 0x05020203) { 180 return OTP_AST2600A2; 181 } 182 183 return -1; 184 } 185 186 static void wait_complete(void) 187 { 188 int reg; 189 190 do { 191 reg = readl(OTP_STATUS); 192 } while ((reg & 0x6) != 0x6); 193 } 194 195 static void otp_write(uint32_t otp_addr, uint32_t data) 196 { 197 writel(otp_addr, OTP_ADDR); //write address 198 writel(data, OTP_COMPARE_1); //write data 199 writel(0x23b1e362, OTP_COMMAND); //write command 200 wait_complete(); 201 } 202 203 static void otp_soak(int soak) 204 { 205 if (info_cb.version == OTP_AST2600A2) { 206 switch (soak) { 207 case 0: //default 208 otp_write(0x3000, 0x0210); // Write MRA 209 otp_write(0x5000, 0x2000); // Write MRB 210 otp_write(0x1000, 0x0); // Write MR 211 break; 212 case 1: //normal program 213 otp_write(0x3000, 0x1200); // Write MRA 214 otp_write(0x5000, 0x107F); // Write MRB 215 otp_write(0x1000, 0x1024); // Write MR 216 writel(0x04191388, OTP_TIMING); // 200us 217 break; 218 case 2: //soak program 219 otp_write(0x3000, 0x1220); // Write MRA 220 otp_write(0x5000, 0x2074); // Write MRB 221 otp_write(0x1000, 0x08a4); // Write MR 222 writel(0x04193a98, OTP_TIMING); // 600us 223 break; 224 } 225 } else { 226 switch (soak) { 227 case 0: //default 228 otp_write(0x3000, 0x0); // Write MRA 229 otp_write(0x5000, 0x0); // Write MRB 230 otp_write(0x1000, 0x0); // Write MR 231 break; 232 case 1: //normal program 233 otp_write(0x3000, 0x4021); // Write MRA 234 otp_write(0x5000, 0x302f); // Write MRB 235 otp_write(0x1000, 0x4020); // Write MR 236 writel(0x04190760, OTP_TIMING); // 75us 237 break; 238 case 2: //soak program 239 otp_write(0x3000, 0x4021); // Write MRA 240 otp_write(0x5000, 0x1027); // Write MRB 241 otp_write(0x1000, 0x4820); // Write MR 242 writel(0x041930d4, OTP_TIMING); // 500us 243 break; 244 } 245 } 246 247 wait_complete(); 248 } 249 250 static void otp_read_data(uint32_t offset, uint32_t *data) 251 { 252 writel(offset, OTP_ADDR); //Read address 253 writel(0x23b1e361, OTP_COMMAND); //trigger read 254 wait_complete(); 255 data[0] = readl(OTP_COMPARE_1); 256 data[1] = readl(OTP_COMPARE_2); 257 } 258 259 static void otp_read_config(uint32_t offset, uint32_t *data) 260 { 261 int config_offset; 262 263 config_offset = 0x800; 264 config_offset |= (offset / 8) * 0x200; 265 config_offset |= (offset % 8) * 0x2; 266 267 writel(config_offset, OTP_ADDR); //Read address 268 writel(0x23b1e361, OTP_COMMAND); //trigger read 269 wait_complete(); 270 data[0] = readl(OTP_COMPARE_1); 271 } 272 273 static int otp_print_config(uint32_t offset, int dw_count) 274 { 275 int i; 276 uint32_t ret[1]; 277 278 if (offset + dw_count > 32) 279 return OTP_USAGE; 280 otp_soak(0); 281 for (i = offset; i < offset + dw_count; i ++) { 282 otp_read_config(i, ret); 283 printf("OTPCFG%X: %08X\n", i, ret[0]); 284 } 285 printf("\n"); 286 return OTP_SUCCESS; 287 } 288 289 static int otp_print_data(uint32_t offset, int dw_count) 290 { 291 int i; 292 uint32_t ret[2]; 293 294 if (offset + dw_count > 2048 || offset % 4 != 0) 295 return OTP_USAGE; 296 otp_soak(0); 297 for (i = offset; i < offset + dw_count; i += 2) { 298 otp_read_data(i, ret); 299 if (i % 4 == 0) 300 printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]); 301 else 302 printf("%08X %08X\n", ret[0], ret[1]); 303 304 } 305 printf("\n"); 306 return OTP_SUCCESS; 307 } 308 309 static int otp_compare(uint32_t otp_addr, uint32_t addr) 310 { 311 uint32_t ret; 312 uint32_t *buf; 313 314 buf = map_physmem(addr, 16, MAP_WRBACK); 315 printf("%08X\n", buf[0]); 316 printf("%08X\n", buf[1]); 317 printf("%08X\n", buf[2]); 318 printf("%08X\n", buf[3]); 319 writel(otp_addr, OTP_ADDR); //Compare address 320 writel(buf[0], OTP_COMPARE_1); //Compare data 1 321 writel(buf[1], OTP_COMPARE_2); //Compare data 2 322 writel(buf[2], OTP_COMPARE_3); //Compare data 3 323 writel(buf[3], OTP_COMPARE_4); //Compare data 4 324 writel(0x23b1e363, OTP_COMMAND); //Compare command 325 wait_complete(); 326 ret = readl(OTP_STATUS); //Compare command 327 if (ret & 0x1) 328 return 0; 329 else 330 return -1; 331 } 332 333 static int verify_bit(uint32_t otp_addr, int bit_offset, int value) 334 { 335 uint32_t ret[2]; 336 337 if (otp_addr % 2 == 0) 338 writel(otp_addr, OTP_ADDR); //Read address 339 else 340 writel(otp_addr - 1, OTP_ADDR); //Read address 341 342 writel(0x23b1e361, OTP_COMMAND); //trigger read 343 wait_complete(); 344 ret[0] = readl(OTP_COMPARE_1); 345 ret[1] = readl(OTP_COMPARE_2); 346 347 if (otp_addr % 2 == 0) { 348 if (((ret[0] >> bit_offset) & 1) == value) 349 return 0; 350 else 351 return -1; 352 } else { 353 if (((ret[1] >> bit_offset) & 1) == value) 354 return 0; 355 else 356 return -1; 357 } 358 359 } 360 361 static uint32_t verify_dw(uint32_t otp_addr, uint32_t *value, uint32_t *ignore, uint32_t *compare, int size) 362 { 363 uint32_t ret[2]; 364 365 otp_addr &= ~(1 << 15); 366 367 if (otp_addr % 2 == 0) 368 writel(otp_addr, OTP_ADDR); //Read address 369 else 370 writel(otp_addr - 1, OTP_ADDR); //Read address 371 writel(0x23b1e361, OTP_COMMAND); //trigger read 372 wait_complete(); 373 ret[0] = readl(OTP_COMPARE_1); 374 ret[1] = readl(OTP_COMPARE_2); 375 if (size == 1) { 376 if (otp_addr % 2 == 0) { 377 // printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]); 378 if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0])) { 379 compare[0] = 0; 380 return 0; 381 } else { 382 compare[0] = value[0] ^ ret[0]; 383 return -1; 384 } 385 386 } else { 387 // printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]); 388 if ((value[0] & ~ignore[0]) == (ret[1] & ~ignore[0])) { 389 compare[0] = ~0; 390 return 0; 391 } else { 392 compare[0] = ~(value[0] ^ ret[1]); 393 return -1; 394 } 395 } 396 } else if (size == 2) { 397 // otp_addr should be even 398 if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0]) && (value[1] & ~ignore[1]) == (ret[1] & ~ignore[1])) { 399 // printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]); 400 // printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]); 401 compare[0] = 0; 402 compare[1] = ~0; 403 return 0; 404 } else { 405 // printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]); 406 // printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]); 407 compare[0] = value[0] ^ ret[0]; 408 compare[1] = ~(value[1] ^ ret[1]); 409 return -1; 410 } 411 } else { 412 return -1; 413 } 414 } 415 416 static void otp_prog(uint32_t otp_addr, uint32_t prog_bit) 417 { 418 writel(otp_addr, OTP_ADDR); //write address 419 writel(prog_bit, OTP_COMPARE_1); //write data 420 writel(0x23b1e364, OTP_COMMAND); //write command 421 wait_complete(); 422 } 423 424 static void _otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset) 425 { 426 int prog_bit; 427 428 if (prog_address % 2 == 0) { 429 if (value) 430 prog_bit = ~(0x1 << bit_offset); 431 else 432 return; 433 } else { 434 prog_address |= 1 << 15; 435 if (!value) 436 prog_bit = 0x1 << bit_offset; 437 else 438 return; 439 } 440 otp_prog(prog_address, prog_bit); 441 } 442 443 static int otp_prog_bit(uint32_t value, uint32_t prog_address, uint32_t bit_offset) 444 { 445 int pass; 446 int i; 447 448 otp_soak(1); 449 _otp_prog_bit(value, prog_address, bit_offset); 450 pass = 0; 451 452 for (i = 0; i < RETRY; i++) { 453 if (verify_bit(prog_address, bit_offset, value) != 0) { 454 otp_soak(2); 455 _otp_prog_bit(value, prog_address, bit_offset); 456 if (verify_bit(prog_address, bit_offset, value) != 0) { 457 otp_soak(1); 458 } else { 459 pass = 1; 460 break; 461 } 462 } else { 463 pass = 1; 464 break; 465 } 466 } 467 468 return pass; 469 } 470 471 static void otp_prog_dw(uint32_t value, uint32_t ignore, uint32_t prog_address) 472 { 473 int j, bit_value, prog_bit; 474 475 for (j = 0; j < 32; j++) { 476 if ((ignore >> j) & 0x1) 477 continue; 478 bit_value = (value >> j) & 0x1; 479 if (prog_address % 2 == 0) { 480 if (bit_value) 481 prog_bit = ~(0x1 << j); 482 else 483 continue; 484 } else { 485 prog_address |= 1 << 15; 486 if (bit_value) 487 continue; 488 else 489 prog_bit = 0x1 << j; 490 } 491 otp_prog(prog_address, prog_bit); 492 } 493 } 494 495 static int otp_prog_verify_2dw(uint32_t *data, uint32_t *buf, uint32_t *ignore_mask, uint32_t prog_address) 496 { 497 int pass; 498 int i; 499 uint32_t data0_masked; 500 uint32_t data1_masked; 501 uint32_t buf0_masked; 502 uint32_t buf1_masked; 503 uint32_t compare[2]; 504 505 data0_masked = data[0] & ~ignore_mask[0]; 506 buf0_masked = buf[0] & ~ignore_mask[0]; 507 data1_masked = data[1] & ~ignore_mask[1]; 508 buf1_masked = buf[1] & ~ignore_mask[1]; 509 if ((data0_masked == buf0_masked) && (data1_masked == buf1_masked)) 510 return 0; 511 512 otp_soak(1); 513 if (data0_masked != buf0_masked) 514 otp_prog_dw(buf[0], ignore_mask[0], prog_address); 515 if (data1_masked != buf1_masked) 516 otp_prog_dw(buf[1], ignore_mask[1], prog_address + 1); 517 518 pass = 0; 519 for (i = 0; i < RETRY; i++) { 520 if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) { 521 otp_soak(2); 522 if (compare[0] != 0) { 523 otp_prog_dw(compare[0], ignore_mask[0], prog_address); 524 } 525 if (compare[1] != ~0) { 526 otp_prog_dw(compare[1], ignore_mask[1], prog_address + 1); 527 } 528 if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) { 529 otp_soak(1); 530 } else { 531 pass = 1; 532 break; 533 } 534 } else { 535 pass = 1; 536 break; 537 } 538 } 539 540 if (!pass) { 541 otp_soak(0); 542 return OTP_FAILURE; 543 } 544 return OTP_SUCCESS; 545 } 546 547 static void otp_strap_status(struct otpstrap_status *otpstrap) 548 { 549 uint32_t OTPSTRAP_RAW[2]; 550 int strap_end; 551 int i, j; 552 553 if (info_cb.version == OTP_AST2600A0) { 554 for (j = 0; j < 64; j++) { 555 otpstrap[j].value = 0; 556 otpstrap[j].remain_times = 7; 557 otpstrap[j].writeable_option = -1; 558 otpstrap[j].protected = 0; 559 } 560 strap_end = 30; 561 } else { 562 for (j = 0; j < 64; j++) { 563 otpstrap[j].value = 0; 564 otpstrap[j].remain_times = 6; 565 otpstrap[j].writeable_option = -1; 566 otpstrap[j].reg_protected = 0; 567 otpstrap[j].protected = 0; 568 } 569 strap_end = 28; 570 } 571 572 otp_soak(0); 573 for (i = 16; i < strap_end; i += 2) { 574 int option = (i - 16) / 2; 575 otp_read_config(i, &OTPSTRAP_RAW[0]); 576 otp_read_config(i + 1, &OTPSTRAP_RAW[1]); 577 for (j = 0; j < 32; j++) { 578 char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1); 579 if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) { 580 otpstrap[j].writeable_option = option; 581 } 582 if (bit_value == 1) 583 otpstrap[j].remain_times --; 584 otpstrap[j].value ^= bit_value; 585 otpstrap[j].option_array[option] = bit_value; 586 } 587 for (j = 32; j < 64; j++) { 588 char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1); 589 if ((bit_value == 0) && (otpstrap[j].writeable_option == -1)) { 590 otpstrap[j].writeable_option = option; 591 } 592 if (bit_value == 1) 593 otpstrap[j].remain_times --; 594 otpstrap[j].value ^= bit_value; 595 otpstrap[j].option_array[option] = bit_value; 596 } 597 } 598 599 if (info_cb.version != OTP_AST2600A0) { 600 otp_read_config(28, &OTPSTRAP_RAW[0]); 601 otp_read_config(29, &OTPSTRAP_RAW[1]); 602 for (j = 0; j < 32; j++) { 603 if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1) 604 otpstrap[j].reg_protected = 1; 605 } 606 for (j = 32; j < 64; j++) { 607 if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1) 608 otpstrap[j].reg_protected = 1; 609 } 610 611 } 612 613 otp_read_config(30, &OTPSTRAP_RAW[0]); 614 otp_read_config(31, &OTPSTRAP_RAW[1]); 615 for (j = 0; j < 32; j++) { 616 if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1) 617 otpstrap[j].protected = 1; 618 } 619 for (j = 32; j < 64; j++) { 620 if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1) 621 otpstrap[j].protected = 1; 622 } 623 } 624 625 static int otp_print_conf_image(struct otp_image_layout *image_layout) 626 { 627 const struct otpconf_info *conf_info = info_cb.conf_info; 628 uint32_t *OTPCFG = (uint32_t *)image_layout->conf; 629 uint32_t *OTPCFG_IGNORE = (uint32_t *)image_layout->conf_ignore; 630 uint32_t mask; 631 uint32_t dw_offset; 632 uint32_t bit_offset; 633 uint32_t otp_value; 634 uint32_t otp_ignore; 635 int fail = 0; 636 char valid_bit[20]; 637 int i; 638 int j; 639 640 printf("DW BIT Value Description\n"); 641 printf("__________________________________________________________________________\n"); 642 for (i = 0; i < info_cb.conf_info_len; i++) { 643 dw_offset = conf_info[i].dw_offset; 644 bit_offset = conf_info[i].bit_offset; 645 mask = BIT(conf_info[i].length) - 1; 646 otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask; 647 otp_ignore = (OTPCFG_IGNORE[dw_offset] >> bit_offset) & mask; 648 649 if (otp_ignore == mask) { 650 continue; 651 } else if (otp_ignore != 0) { 652 fail = 1; 653 } 654 655 if ((otp_value != conf_info[i].value) && 656 conf_info[i].value != OTP_REG_RESERVED && 657 conf_info[i].value != OTP_REG_VALUE && 658 conf_info[i].value != OTP_REG_VALID_BIT) 659 continue; 660 printf("0x%-4X", dw_offset); 661 662 if (conf_info[i].length == 1) { 663 printf("0x%-9X", conf_info[i].bit_offset); 664 } else { 665 printf("0x%-2X:0x%-4X", 666 conf_info[i].bit_offset + conf_info[i].length - 1, 667 conf_info[i].bit_offset); 668 } 669 printf("0x%-10x", otp_value); 670 671 if (fail) { 672 printf("Ignore mask error\n"); 673 } else { 674 if (conf_info[i].value == OTP_REG_RESERVED) { 675 printf("Reserved\n"); 676 } else if (conf_info[i].value == OTP_REG_VALUE) { 677 printf(conf_info[i].information, otp_value); 678 printf("\n"); 679 } else if (conf_info[i].value == OTP_REG_VALID_BIT) { 680 if (otp_value != 0) { 681 for (j = 0; j < 7; j++) { 682 if (otp_value == (1 << j)) { 683 valid_bit[j * 2] = '1'; 684 } else { 685 valid_bit[j * 2] = '0'; 686 } 687 valid_bit[j * 2 + 1] = ' '; 688 } 689 valid_bit[15] = 0; 690 } else { 691 strcpy(valid_bit, "0 0 0 0 0 0 0 0\0"); 692 } 693 printf(conf_info[i].information, valid_bit); 694 printf("\n"); 695 } else { 696 printf("%s\n", conf_info[i].information); 697 } 698 } 699 } 700 701 if (fail) 702 return OTP_FAILURE; 703 704 return OTP_SUCCESS; 705 } 706 707 static int otp_print_conf_info(int input_offset) 708 { 709 const struct otpconf_info *conf_info = info_cb.conf_info; 710 uint32_t OTPCFG[16]; 711 uint32_t mask; 712 uint32_t dw_offset; 713 uint32_t bit_offset; 714 uint32_t otp_value; 715 char valid_bit[20]; 716 int i; 717 int j; 718 719 otp_soak(0); 720 for (i = 0; i < 16; i++) 721 otp_read_config(i, &OTPCFG[i]); 722 723 724 printf("DW BIT Value Description\n"); 725 printf("__________________________________________________________________________\n"); 726 for (i = 0; i < info_cb.conf_info_len; i++) { 727 if (input_offset != -1 && input_offset != conf_info[i].dw_offset) 728 continue; 729 dw_offset = conf_info[i].dw_offset; 730 bit_offset = conf_info[i].bit_offset; 731 mask = BIT(conf_info[i].length) - 1; 732 otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask; 733 734 if ((otp_value != conf_info[i].value) && 735 conf_info[i].value != OTP_REG_RESERVED && 736 conf_info[i].value != OTP_REG_VALUE && 737 conf_info[i].value != OTP_REG_VALID_BIT) 738 continue; 739 printf("0x%-4X", dw_offset); 740 741 if (conf_info[i].length == 1) { 742 printf("0x%-9X", conf_info[i].bit_offset); 743 } else { 744 printf("0x%-2X:0x%-4X", 745 conf_info[i].bit_offset + conf_info[i].length - 1, 746 conf_info[i].bit_offset); 747 } 748 printf("0x%-10x", otp_value); 749 750 if (conf_info[i].value == OTP_REG_RESERVED) { 751 printf("Reserved\n"); 752 } else if (conf_info[i].value == OTP_REG_VALUE) { 753 printf(conf_info[i].information, otp_value); 754 printf("\n"); 755 } else if (conf_info[i].value == OTP_REG_VALID_BIT) { 756 if (otp_value != 0) { 757 for (j = 0; j < 7; j++) { 758 if (otp_value == (1 << j)) { 759 valid_bit[j * 2] = '1'; 760 } else { 761 valid_bit[j * 2] = '0'; 762 } 763 valid_bit[j * 2 + 1] = ' '; 764 } 765 valid_bit[15] = 0; 766 } else { 767 strcpy(valid_bit, "0 0 0 0 0 0 0 0\0"); 768 } 769 printf(conf_info[i].information, valid_bit); 770 printf("\n"); 771 } else { 772 printf("%s\n", conf_info[i].information); 773 } 774 } 775 return OTP_SUCCESS; 776 } 777 778 static int otp_print_strap_image(struct otp_image_layout *image_layout) 779 { 780 const struct otpstrap_info *strap_info = info_cb.strap_info; 781 uint32_t *OTPSTRAP; 782 uint32_t *OTPSTRAP_REG_PRO; 783 uint32_t *OTPSTRAP_PRO; 784 uint32_t *OTPSTRAP_IGNORE; 785 int i; 786 int fail = 0; 787 uint32_t bit_offset; 788 uint32_t dw_offset; 789 uint32_t mask; 790 uint32_t otp_value; 791 uint32_t otp_reg_protect; 792 uint32_t otp_protect; 793 uint32_t otp_ignore; 794 795 OTPSTRAP = (uint32_t *)image_layout->strap; 796 OTPSTRAP_PRO = (uint32_t *)image_layout->strap_pro; 797 OTPSTRAP_IGNORE = (uint32_t *)image_layout->strap_ignore; 798 if (info_cb.version == OTP_AST2600A0) { 799 OTPSTRAP_REG_PRO = NULL; 800 printf("BIT(hex) Value Protect Description\n"); 801 } else { 802 OTPSTRAP_REG_PRO = (uint32_t *)image_layout->strap_reg_pro; 803 printf("BIT(hex) Value Reg_Protect Protect Description\n"); 804 } 805 printf("__________________________________________________________________________________________\n"); 806 807 for (i = 0; i < info_cb.strap_info_len; i++) { 808 if (strap_info[i].bit_offset > 31) { 809 dw_offset = 1; 810 bit_offset = strap_info[i].bit_offset - 32; 811 } else { 812 dw_offset = 0; 813 bit_offset = strap_info[i].bit_offset; 814 } 815 816 mask = BIT(strap_info[i].length) - 1; 817 otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask; 818 otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask; 819 otp_ignore = (OTPSTRAP_IGNORE[dw_offset] >> bit_offset) & mask; 820 821 if (info_cb.version != OTP_AST2600A0) 822 otp_reg_protect = (OTPSTRAP_REG_PRO[dw_offset] >> bit_offset) & mask; 823 else 824 otp_reg_protect = 0; 825 826 if (otp_ignore == mask) { 827 continue; 828 } else if (otp_ignore != 0) { 829 fail = 1; 830 } 831 832 if ((otp_value != strap_info[i].value) && 833 strap_info[i].value != OTP_REG_RESERVED) 834 continue; 835 836 if (strap_info[i].length == 1) { 837 printf("0x%-9X", strap_info[i].bit_offset); 838 } else { 839 printf("0x%-2X:0x%-4X", 840 strap_info[i].bit_offset + strap_info[i].length - 1, 841 strap_info[i].bit_offset); 842 } 843 printf("0x%-10x", otp_value); 844 if (info_cb.version != OTP_AST2600A0) 845 printf("0x%-10x", otp_reg_protect); 846 printf("0x%-10x", otp_protect); 847 848 if (fail) { 849 printf("Ignore mask error\n"); 850 } else { 851 if (strap_info[i].value != OTP_REG_RESERVED) 852 printf("%s\n", strap_info[i].information); 853 else 854 printf("Reserved\n"); 855 } 856 } 857 858 if (fail) 859 return OTP_FAILURE; 860 861 return OTP_SUCCESS; 862 } 863 864 static int otp_print_strap_info(int view) 865 { 866 const struct otpstrap_info *strap_info = info_cb.strap_info; 867 struct otpstrap_status strap_status[64]; 868 int i, j; 869 int fail = 0; 870 uint32_t bit_offset; 871 uint32_t length; 872 uint32_t otp_value; 873 uint32_t otp_protect; 874 875 otp_strap_status(strap_status); 876 877 if (view) { 878 if (info_cb.version == OTP_AST2600A0) 879 printf("BIT(hex) Value Remains Protect Description\n"); 880 else 881 printf("BIT(hex) Value Remains Reg_Protect Protect Description\n"); 882 printf("___________________________________________________________________________________________________\n"); 883 } else { 884 printf("BIT(hex) Value Description\n"); 885 printf("________________________________________________________________________________\n"); 886 } 887 for (i = 0; i < info_cb.strap_info_len; i++) { 888 otp_value = 0; 889 bit_offset = strap_info[i].bit_offset; 890 length = strap_info[i].length; 891 for (j = 0; j < length; j++) { 892 otp_value |= strap_status[bit_offset + j].value << j; 893 otp_protect |= strap_status[bit_offset + j].protected << j; 894 } 895 if ((otp_value != strap_info[i].value) && 896 strap_info[i].value != OTP_REG_RESERVED) 897 continue; 898 if (view) { 899 for (j = 0; j < length; j++) { 900 printf("0x%-7X", strap_info[i].bit_offset + j); 901 printf("0x%-5X", strap_status[bit_offset + j].value); 902 printf("%-9d", strap_status[bit_offset + j].remain_times); 903 if (info_cb.version != OTP_AST2600A0) 904 printf("0x%-10X", strap_status[bit_offset + j].reg_protected); 905 printf("0x%-7X", strap_status[bit_offset + j].protected); 906 if (strap_info[i].value == OTP_REG_RESERVED) { 907 printf(" Reserved\n"); 908 continue; 909 } 910 if (length == 1) { 911 printf(" %s\n", strap_info[i].information); 912 continue; 913 } 914 915 if (j == 0) 916 printf("/%s\n", strap_info[i].information); 917 else if (j == length - 1) 918 printf("\\ \"\n"); 919 else 920 printf("| \"\n"); 921 } 922 } else { 923 if (length == 1) { 924 printf("0x%-9X", strap_info[i].bit_offset); 925 } else { 926 printf("0x%-2X:0x%-4X", 927 bit_offset + length - 1, bit_offset); 928 } 929 930 printf("0x%-10X", otp_value); 931 932 if (strap_info[i].value != OTP_REG_RESERVED) 933 printf("%s\n", strap_info[i].information); 934 else 935 printf("Reserved\n"); 936 } 937 } 938 939 if (fail) 940 return OTP_FAILURE; 941 942 return OTP_SUCCESS; 943 } 944 945 static void buf_print(uint8_t *buf, int len) 946 { 947 int i; 948 printf(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n"); 949 for (i = 0; i < len; i++) { 950 if (i % 16 == 0) { 951 printf("%04X: ", i); 952 } 953 printf("%02X ", buf[i]); 954 if ((i + 1) % 16 == 0) { 955 printf("\n"); 956 } 957 } 958 } 959 960 static int otp_print_data_info(struct otp_image_layout *image_layout) 961 { 962 int key_id, key_offset, last, key_type, key_length, exp_length; 963 const struct otpkey_type *key_info_array = info_cb.key_info; 964 struct otpkey_type key_info; 965 uint32_t *buf; 966 uint8_t *byte_buf; 967 char empty = 1; 968 int i = 0, len = 0; 969 int j; 970 971 byte_buf = image_layout->data; 972 buf = (uint32_t *)byte_buf; 973 974 for (i = 0; i < 16; i++) { 975 if (buf[i] != 0) { 976 empty = 0; 977 } 978 } 979 if (empty) 980 return 0; 981 982 i = 0; 983 while (1) { 984 key_id = buf[i] & 0x7; 985 key_offset = buf[i] & 0x1ff8; 986 last = (buf[i] >> 13) & 1; 987 key_type = (buf[i] >> 14) & 0xf; 988 key_length = (buf[i] >> 18) & 0x3; 989 exp_length = (buf[i] >> 20) & 0xfff; 990 991 for (j = 0; j < info_cb.key_info_len; j++) { 992 if (key_type == key_info_array[j].value) { 993 key_info = key_info_array[j]; 994 break; 995 } 996 } 997 998 printf("\nKey[%d]:\n", i); 999 printf("Key Type: "); 1000 printf("%s\n", key_info.information); 1001 1002 if (key_info.key_type == OTP_KEY_TYPE_HMAC) { 1003 printf("HMAC SHA Type: "); 1004 switch (key_length) { 1005 case 0: 1006 printf("HMAC(SHA224)\n"); 1007 break; 1008 case 1: 1009 printf("HMAC(SHA256)\n"); 1010 break; 1011 case 2: 1012 printf("HMAC(SHA384)\n"); 1013 break; 1014 case 3: 1015 printf("HMAC(SHA512)\n"); 1016 break; 1017 } 1018 } else if (key_info.key_type == OTP_KEY_TYPE_RSA) { 1019 printf("RSA SHA Type: "); 1020 switch (key_length) { 1021 case 0: 1022 printf("RSA1024\n"); 1023 len = 0x100; 1024 break; 1025 case 1: 1026 printf("RSA2048\n"); 1027 len = 0x200; 1028 break; 1029 case 2: 1030 printf("RSA3072\n"); 1031 len = 0x300; 1032 break; 1033 case 3: 1034 printf("RSA4096\n"); 1035 len = 0x400; 1036 break; 1037 } 1038 printf("RSA exponent bit length: %d\n", exp_length); 1039 } 1040 if (key_info.need_id) 1041 printf("Key Number ID: %d\n", key_id); 1042 printf("Key Value:\n"); 1043 if (key_info.key_type == OTP_KEY_TYPE_HMAC) { 1044 buf_print(&byte_buf[key_offset], 0x40); 1045 } else if (key_info.key_type == OTP_KEY_TYPE_AES) { 1046 printf("AES Key:\n"); 1047 buf_print(&byte_buf[key_offset], 0x20); 1048 if (info_cb.version == OTP_AST2600A0) { 1049 printf("AES IV:\n"); 1050 buf_print(&byte_buf[key_offset + 0x20], 0x10); 1051 } 1052 1053 } else if (key_info.key_type == OTP_KEY_TYPE_VAULT) { 1054 if (info_cb.version == OTP_AST2600A0) { 1055 printf("AES Key:\n"); 1056 buf_print(&byte_buf[key_offset], 0x20); 1057 printf("AES IV:\n"); 1058 buf_print(&byte_buf[key_offset + 0x20], 0x10); 1059 } else { 1060 printf("AES Key 1:\n"); 1061 buf_print(&byte_buf[key_offset], 0x20); 1062 printf("AES Key 2:\n"); 1063 buf_print(&byte_buf[key_offset + 0x20], 0x20); 1064 } 1065 1066 } else if (key_info.key_type == OTP_KEY_TYPE_RSA) { 1067 printf("RSA mod:\n"); 1068 buf_print(&byte_buf[key_offset], len / 2); 1069 printf("RSA exp:\n"); 1070 buf_print(&byte_buf[key_offset + (len / 2)], len / 2); 1071 } 1072 if (last) 1073 break; 1074 i++; 1075 } 1076 return 0; 1077 } 1078 1079 static int otp_prog_conf(struct otp_image_layout *image_layout) 1080 { 1081 int i, k; 1082 int pass = 0; 1083 uint32_t prog_address; 1084 uint32_t data[16]; 1085 uint32_t compare[2]; 1086 uint32_t *conf = (uint32_t *)image_layout->conf; 1087 uint32_t *conf_ignore = (uint32_t *)image_layout->conf_ignore; 1088 uint32_t data_masked; 1089 uint32_t buf_masked; 1090 1091 printf("Read OTP Config Region:\n"); 1092 1093 for (i = 0; i < 16 ; i ++) { 1094 prog_address = 0x800; 1095 prog_address |= (i / 8) * 0x200; 1096 prog_address |= (i % 8) * 0x2; 1097 otp_read_data(prog_address, &data[i]); 1098 } 1099 1100 printf("Check writable...\n"); 1101 for (i = 0; i < 16; i++) { 1102 data_masked = data[i] & ~conf_ignore[i]; 1103 buf_masked = conf[i] & ~conf_ignore[i]; 1104 if (data_masked == buf_masked) 1105 continue; 1106 if ((data_masked | buf_masked) == buf_masked) { 1107 continue; 1108 } else { 1109 printf("Input image can't program into OTP, please check.\n"); 1110 printf("OTPCFG[%X] = %x\n", i, data[i]); 1111 printf("Input [%X] = %x\n", i, conf[i]); 1112 printf("Mask [%X] = %x\n", i, ~conf_ignore[i]); 1113 return OTP_FAILURE; 1114 } 1115 } 1116 1117 printf("Start Programing...\n"); 1118 otp_soak(0); 1119 for (i = 0; i < 16; i++) { 1120 data_masked = data[i] & ~conf_ignore[i]; 1121 buf_masked = conf[i] & ~conf_ignore[i]; 1122 prog_address = 0x800; 1123 prog_address |= (i / 8) * 0x200; 1124 prog_address |= (i % 8) * 0x2; 1125 if (data_masked == buf_masked) { 1126 pass = 1; 1127 continue; 1128 } 1129 1130 1131 otp_soak(1); 1132 otp_prog_dw(conf[i], conf_ignore[i], prog_address); 1133 1134 pass = 0; 1135 for (k = 0; k < RETRY; k++) { 1136 if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) { 1137 otp_soak(2); 1138 otp_prog_dw(compare[0], conf_ignore[i], prog_address); 1139 if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) { 1140 otp_soak(1); 1141 } else { 1142 pass = 1; 1143 break; 1144 } 1145 } else { 1146 pass = 1; 1147 break; 1148 } 1149 } 1150 if (pass == 0) { 1151 printf("address: %08x, data: %08x, buffer: %08x, mask: %08x\n", 1152 i, data[i], conf[i], conf_ignore[i]); 1153 break; 1154 } 1155 } 1156 1157 otp_soak(0); 1158 if (!pass) 1159 return OTP_FAILURE; 1160 1161 return OTP_SUCCESS; 1162 1163 } 1164 1165 static int otp_strap_bit_confirm(struct otpstrap_status *otpstrap, int offset, int ibit, int bit, int pbit, int rpbit) 1166 { 1167 if (ibit == 1) { 1168 return OTP_SUCCESS; 1169 } else { 1170 printf("OTPSTRAP[%X]:\n", offset); 1171 } 1172 if (bit == otpstrap->value) { 1173 printf(" The value is same as before, skip it.\n"); 1174 return OTP_PROG_SKIP; 1175 } 1176 if (otpstrap->protected == 1) { 1177 printf(" This bit is protected and is not writable\n"); 1178 return OTP_FAILURE; 1179 } 1180 if (otpstrap->remain_times == 0) { 1181 printf(" This bit is no remaining times to write.\n"); 1182 return OTP_FAILURE; 1183 } 1184 if (pbit == 1) { 1185 printf(" This bit will be protected and become non-writable.\n"); 1186 } 1187 if (rpbit == 1 && info_cb.version != OTP_AST2600A0) { 1188 printf(" The relative register will be protected.\n"); 1189 } 1190 printf(" Write 1 to OTPSTRAP[%X] OPTION[%X], that value becomes from %d to %d.\n", offset, otpstrap->writeable_option + 1, otpstrap->value, otpstrap->value ^ 1); 1191 return OTP_SUCCESS; 1192 } 1193 1194 static int otp_strap_image_confirm(struct otp_image_layout *image_layout) 1195 { 1196 int i; 1197 uint32_t *strap; 1198 uint32_t *strap_ignore; 1199 uint32_t *strap_reg_protect; 1200 uint32_t *strap_pro; 1201 int bit, pbit, ibit, rpbit; 1202 int fail = 0; 1203 int skip = -1; 1204 int ret; 1205 struct otpstrap_status otpstrap[64]; 1206 1207 strap = (uint32_t *)image_layout->strap; 1208 strap_pro = (uint32_t *)image_layout->strap_pro; 1209 strap_ignore = (uint32_t *)image_layout->strap_ignore; 1210 strap_reg_protect = (uint32_t *)image_layout->strap_reg_pro; 1211 1212 otp_strap_status(otpstrap); 1213 for (i = 0; i < 64; i++) { 1214 if (i < 32) { 1215 bit = (strap[0] >> i) & 0x1; 1216 ibit = (strap_ignore[0] >> i) & 0x1; 1217 pbit = (strap_pro[0] >> i) & 0x1; 1218 } else { 1219 bit = (strap[1] >> (i - 32)) & 0x1; 1220 ibit = (strap_ignore[1] >> (i - 32)) & 0x1; 1221 pbit = (strap_pro[1] >> (i - 32)) & 0x1; 1222 } 1223 1224 if (info_cb.version != OTP_AST2600A0) { 1225 if (i < 32) { 1226 rpbit = (strap_reg_protect[0] >> i) & 0x1; 1227 } else { 1228 rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1; 1229 } 1230 } else { 1231 rpbit = 0; 1232 } 1233 ret = otp_strap_bit_confirm(&otpstrap[i], i, ibit, bit, pbit, rpbit); 1234 if (ret == OTP_PROG_SKIP) { 1235 if (skip == -1) 1236 skip = 1; 1237 continue; 1238 } else { 1239 skip = 1; 1240 } 1241 1242 if (ret == OTP_FAILURE) 1243 fail = 1; 1244 } 1245 if (fail == 1) 1246 return OTP_FAILURE; 1247 else if (skip == 1) 1248 return OTP_PROG_SKIP; 1249 1250 return OTP_SUCCESS; 1251 } 1252 1253 static int otp_print_strap(int start, int count) 1254 { 1255 int i, j; 1256 int remains; 1257 struct otpstrap_status otpstrap[64]; 1258 1259 if (start < 0 || start > 64) 1260 return OTP_USAGE; 1261 1262 if ((start + count) < 0 || (start + count) > 64) 1263 return OTP_USAGE; 1264 1265 otp_strap_status(otpstrap); 1266 1267 if (info_cb.version == OTP_AST2600A0) { 1268 remains = 7; 1269 printf("BIT(hex) Value Option Status\n"); 1270 } else { 1271 remains = 6; 1272 printf("BIT(hex) Value Option Reg_Protect Status\n"); 1273 } 1274 printf("______________________________________________________________________________\n"); 1275 1276 for (i = start; i < start + count; i++) { 1277 printf("0x%-8X", i); 1278 printf("%-7d", otpstrap[i].value); 1279 for (j = 0; j < remains; j++) 1280 printf("%d ", otpstrap[i].option_array[j]); 1281 printf(" "); 1282 if (info_cb.version != OTP_AST2600A0) { 1283 printf("%d ", otpstrap[i].reg_protected); 1284 } 1285 if (otpstrap[i].protected == 1) { 1286 printf("protected and not writable"); 1287 } else { 1288 printf("not protected "); 1289 if (otpstrap[i].remain_times == 0) { 1290 printf("and no remaining times to write."); 1291 } else { 1292 printf("and still can write %d times", otpstrap[i].remain_times); 1293 } 1294 } 1295 printf("\n"); 1296 } 1297 1298 return OTP_SUCCESS; 1299 } 1300 1301 static int otp_prog_strap_bit(int bit_offset, int value) 1302 { 1303 struct otpstrap_status otpstrap[64]; 1304 uint32_t prog_address; 1305 int offset; 1306 int ret; 1307 1308 1309 otp_strap_status(otpstrap); 1310 1311 ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0); 1312 1313 if (ret != OTP_SUCCESS) { 1314 return ret; 1315 } 1316 1317 prog_address = 0x800; 1318 if (bit_offset < 32) { 1319 offset = bit_offset; 1320 prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) / 8) * 0x200; 1321 prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) % 8) * 0x2; 1322 1323 } else { 1324 offset = (bit_offset - 32); 1325 prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) / 8) * 0x200; 1326 prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) % 8) * 0x2; 1327 } 1328 1329 1330 return otp_prog_bit(1, prog_address, offset); 1331 } 1332 1333 static int otp_prog_strap(struct otp_image_layout *image_layout) 1334 { 1335 uint32_t *strap; 1336 uint32_t *strap_ignore; 1337 uint32_t *strap_pro; 1338 uint32_t *strap_reg_protect; 1339 uint32_t prog_address; 1340 int i; 1341 int bit, pbit, ibit, offset, rpbit; 1342 int fail = 0; 1343 int ret; 1344 struct otpstrap_status otpstrap[64]; 1345 1346 strap = (uint32_t *)image_layout->strap; 1347 strap_pro = (uint32_t *)image_layout->strap_pro; 1348 strap_ignore = (uint32_t *)image_layout->strap_ignore; 1349 strap_reg_protect = (uint32_t *)image_layout->strap_reg_pro; 1350 1351 printf("Read OTP Strap Region:\n"); 1352 otp_strap_status(otpstrap); 1353 1354 printf("Check writable...\n"); 1355 if (otp_strap_image_confirm(image_layout) == OTP_FAILURE) { 1356 printf("Input image can't program into OTP, please check.\n"); 1357 return OTP_FAILURE; 1358 } 1359 1360 for (i = 0; i < 64; i++) { 1361 prog_address = 0x800; 1362 if (i < 32) { 1363 offset = i; 1364 bit = (strap[0] >> offset) & 0x1; 1365 ibit = (strap_ignore[0] >> offset) & 0x1; 1366 pbit = (strap_pro[0] >> offset) & 0x1; 1367 prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200; 1368 prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2; 1369 1370 } else { 1371 offset = (i - 32); 1372 bit = (strap[1] >> offset) & 0x1; 1373 ibit = (strap_ignore[1] >> offset) & 0x1; 1374 pbit = (strap_pro[1] >> offset) & 0x1; 1375 prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200; 1376 prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2; 1377 } 1378 if (info_cb.version != OTP_AST2600A0) { 1379 if (i < 32) { 1380 rpbit = (strap_reg_protect[0] >> i) & 0x1; 1381 } else { 1382 rpbit = (strap_reg_protect[1] >> (i - 32)) & 0x1; 1383 } 1384 } else { 1385 rpbit = 0; 1386 } 1387 1388 if (ibit == 1) { 1389 continue; 1390 } 1391 if (bit == otpstrap[i].value) { 1392 continue; 1393 } 1394 if (otpstrap[i].protected == 1) { 1395 fail = 1; 1396 continue; 1397 } 1398 if (otpstrap[i].remain_times == 0) { 1399 fail = 1; 1400 continue; 1401 } 1402 1403 ret = otp_prog_bit(1, prog_address, offset); 1404 if (!ret) 1405 return OTP_FAILURE; 1406 1407 if (rpbit == 1 && info_cb.version != OTP_AST2600A0) { 1408 prog_address = 0x800; 1409 if (i < 32) 1410 prog_address |= 0x608; 1411 else 1412 prog_address |= 0x60a; 1413 1414 ret = otp_prog_bit(1, prog_address, offset); 1415 if (!ret) 1416 return OTP_FAILURE; 1417 } 1418 1419 if (pbit != 0) { 1420 prog_address = 0x800; 1421 if (i < 32) 1422 prog_address |= 0x60c; 1423 else 1424 prog_address |= 0x60e; 1425 1426 ret = otp_prog_bit(1, prog_address, offset); 1427 if (!ret) 1428 return OTP_FAILURE; 1429 } 1430 1431 } 1432 otp_soak(0); 1433 if (fail == 1) 1434 return OTP_FAILURE; 1435 else 1436 return OTP_SUCCESS; 1437 1438 } 1439 1440 static int otp_prog_data(struct otp_image_layout *image_layout) 1441 { 1442 int i; 1443 int ret; 1444 int data_dw; 1445 uint32_t data[2048]; 1446 uint32_t *buf; 1447 uint32_t *buf_ignore; 1448 1449 uint32_t data_masked; 1450 uint32_t buf_masked; 1451 1452 buf = (uint32_t *)image_layout->data; 1453 buf_ignore = (uint32_t *)image_layout->data_ignore; 1454 1455 data_dw = image_layout->data_length / 4; 1456 1457 printf("Read OTP Data:\n"); 1458 1459 for (i = 0; i < data_dw - 2 ; i += 2) { 1460 otp_read_data(i, &data[i]); 1461 } 1462 1463 printf("Check writable...\n"); 1464 // ignore last two dw, the last two dw is used for slt otp write check. 1465 for (i = 0; i < data_dw - 2; i++) { 1466 data_masked = data[i] & ~buf_ignore[i]; 1467 buf_masked = buf[i] & ~buf_ignore[i]; 1468 if (data_masked == buf_masked) 1469 continue; 1470 if (i % 2 == 0) { 1471 if ((data_masked | buf_masked) == buf_masked) { 1472 continue; 1473 } else { 1474 printf("Input image can't program into OTP, please check.\n"); 1475 printf("OTP_ADDR[%x] = %x\n", i, data[i]); 1476 printf("Input [%x] = %x\n", i, buf[i]); 1477 printf("Mask [%x] = %x\n", i, ~buf_ignore[i]); 1478 return OTP_FAILURE; 1479 } 1480 } else { 1481 if ((data_masked & buf_masked) == buf_masked) { 1482 continue; 1483 } else { 1484 printf("Input image can't program into OTP, please check.\n"); 1485 printf("OTP_ADDR[%x] = %x\n", i, data[i]); 1486 printf("Input [%x] = %x\n", i, buf[i]); 1487 printf("Mask [%x] = %x\n", i, ~buf_ignore[i]); 1488 return OTP_FAILURE; 1489 } 1490 } 1491 } 1492 1493 printf("Start Programing...\n"); 1494 1495 // programing ecc region first 1496 for (i = 1792; i < 2046; i += 2) { 1497 ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i); 1498 if (ret != OTP_SUCCESS) { 1499 printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n", 1500 i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]); 1501 return ret; 1502 } 1503 } 1504 1505 for (i = 0; i < 1792; i += 2) { 1506 ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i); 1507 if (ret != OTP_SUCCESS) { 1508 printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n", 1509 i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]); 1510 return ret; 1511 } 1512 } 1513 otp_soak(0); 1514 return OTP_SUCCESS; 1515 1516 } 1517 1518 static int otp_image_verify(uint8_t *src_buf, uint32_t length, uint8_t *digest_buf) 1519 { 1520 sha256_context ctx; 1521 u8 digest_ret[CHECKSUM_LEN]; 1522 1523 sha256_starts(&ctx); 1524 sha256_update(&ctx, src_buf, length); 1525 sha256_finish(&ctx, digest_ret); 1526 1527 if (!memcmp(digest_buf, digest_ret, CHECKSUM_LEN)) 1528 return 0; 1529 else 1530 return -1; 1531 1532 } 1533 1534 static int do_otp_prog(int addr, int nconfirm) 1535 { 1536 int ret; 1537 int image_version = 0; 1538 struct otp_header *otp_header; 1539 struct otp_image_layout image_layout; 1540 int image_size; 1541 uint8_t *buf; 1542 uint8_t *checksum; 1543 1544 otp_header = map_physmem(addr, sizeof(struct otp_header), MAP_WRBACK); 1545 if (!otp_header) { 1546 puts("Failed to map physical memory\n"); 1547 return OTP_FAILURE; 1548 } 1549 1550 image_size = OTP_IMAGE_SIZE(otp_header->image_info); 1551 unmap_physmem(otp_header, MAP_WRBACK); 1552 1553 buf = map_physmem(addr, image_size + CHECKSUM_LEN, MAP_WRBACK); 1554 1555 if (!buf) { 1556 puts("Failed to map physical memory\n"); 1557 return OTP_FAILURE; 1558 } 1559 otp_header = (struct otp_header *) buf; 1560 checksum = buf + otp_header->checksum_offset; 1561 1562 if (strcmp(OTP_MAGIC, (char *)otp_header->otp_magic) != 0) { 1563 puts("Image is invalid\n"); 1564 return OTP_FAILURE; 1565 } 1566 1567 1568 image_layout.data_length = (int)(OTP_REGION_SIZE(otp_header->data_info) / 2); 1569 image_layout.data = buf + OTP_REGION_OFFSET(otp_header->data_info); 1570 image_layout.data_ignore = image_layout.data + image_layout.data_length; 1571 1572 image_layout.conf_length = (int)(OTP_REGION_SIZE(otp_header->config_info) / 2); 1573 image_layout.conf = buf + OTP_REGION_OFFSET(otp_header->config_info); 1574 image_layout.conf_ignore = image_layout.conf + image_layout.conf_length; 1575 1576 image_layout.strap = buf + OTP_REGION_OFFSET(otp_header->strap_info); 1577 1578 if (!strcmp("A0", (char *)otp_header->otp_version)) { 1579 image_version = OTP_AST2600A0; 1580 image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 3); 1581 image_layout.strap_pro = image_layout.strap + image_layout.strap_length; 1582 image_layout.strap_ignore = image_layout.strap + 2 * image_layout.strap_length; 1583 } else if (!strcmp("A1", (char *)otp_header->otp_version)) { 1584 image_version = OTP_AST2600A1; 1585 image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4); 1586 image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length; 1587 image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length; 1588 image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length; 1589 } else if (!strcmp("A2", (char *)otp_header->otp_version)) { 1590 image_version = OTP_AST2600A2; 1591 image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 4); 1592 image_layout.strap_reg_pro = image_layout.strap + image_layout.strap_length; 1593 image_layout.strap_pro = image_layout.strap + 2 * image_layout.strap_length; 1594 image_layout.strap_ignore = image_layout.strap + 3 * image_layout.strap_length; 1595 } else { 1596 puts("Version is not supported\n"); 1597 return OTP_FAILURE; 1598 } 1599 1600 if (image_version != info_cb.version) { 1601 puts("Version is not match\n"); 1602 return OTP_FAILURE; 1603 } 1604 1605 if (otp_image_verify(buf, image_size, checksum)) { 1606 puts("checksum is invalid\n"); 1607 return OTP_FAILURE; 1608 } 1609 1610 if (!nconfirm) { 1611 if (otp_header->image_info & OTP_INC_DATA) { 1612 printf("\nOTP data region :\n"); 1613 if (otp_print_data_info(&image_layout) < 0) { 1614 printf("OTP data error, please check.\n"); 1615 return OTP_FAILURE; 1616 } 1617 } 1618 if (otp_header->image_info & OTP_INC_STRAP) { 1619 printf("\nOTP strap region :\n"); 1620 if (otp_print_strap_image(&image_layout) < 0) { 1621 printf("OTP strap error, please check.\n"); 1622 return OTP_FAILURE; 1623 } 1624 } 1625 if (otp_header->image_info & OTP_INC_CONFIG) { 1626 printf("\nOTP configuration region :\n"); 1627 if (otp_print_conf_image(&image_layout) < 0) { 1628 printf("OTP config error, please check.\n"); 1629 return OTP_FAILURE; 1630 } 1631 } 1632 1633 printf("type \"YES\" (no quotes) to continue:\n"); 1634 if (!confirm_yesno()) { 1635 printf(" Aborting\n"); 1636 return OTP_FAILURE; 1637 } 1638 } 1639 1640 if (otp_header->image_info & OTP_INC_DATA) { 1641 printf("programing data region ...\n"); 1642 ret = otp_prog_data(&image_layout); 1643 if (ret != 0) { 1644 printf("Error\n"); 1645 return ret; 1646 } else { 1647 printf("Done\n"); 1648 } 1649 } 1650 if (otp_header->image_info & OTP_INC_STRAP) { 1651 printf("programing strap region ...\n"); 1652 ret = otp_prog_strap(&image_layout); 1653 if (ret != 0) { 1654 printf("Error\n"); 1655 return ret; 1656 } else { 1657 printf("Done\n"); 1658 } 1659 } 1660 if (otp_header->image_info & OTP_INC_CONFIG) { 1661 printf("programing configuration region ...\n"); 1662 ret = otp_prog_conf(&image_layout); 1663 if (ret != 0) { 1664 printf("Error\n"); 1665 return ret; 1666 } 1667 printf("Done\n"); 1668 } 1669 1670 return OTP_SUCCESS; 1671 } 1672 1673 static int do_otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm) 1674 { 1675 uint32_t read[2]; 1676 uint32_t prog_address = 0; 1677 struct otpstrap_status otpstrap[64]; 1678 int otp_bit; 1679 int ret = 0; 1680 1681 otp_soak(0); 1682 switch (mode) { 1683 case OTP_REGION_CONF: 1684 otp_read_config(otp_dw_offset, read); 1685 prog_address = 0x800; 1686 prog_address |= (otp_dw_offset / 8) * 0x200; 1687 prog_address |= (otp_dw_offset % 8) * 0x2; 1688 otp_bit = (read[0] >> bit_offset) & 0x1; 1689 if (otp_bit == value) { 1690 printf("OTPCFG%X[%X] = %d\n", otp_dw_offset, bit_offset, value); 1691 printf("No need to program\n"); 1692 return OTP_SUCCESS; 1693 } 1694 if (otp_bit == 1 && value == 0) { 1695 printf("OTPCFG%X[%X] = 1\n", otp_dw_offset, bit_offset); 1696 printf("OTP is programed, which can't be clean\n"); 1697 return OTP_FAILURE; 1698 } 1699 printf("Program OTPCFG%X[%X] to 1\n", otp_dw_offset, bit_offset); 1700 break; 1701 case OTP_REGION_DATA: 1702 prog_address = otp_dw_offset; 1703 1704 if (otp_dw_offset % 2 == 0) { 1705 otp_read_data(otp_dw_offset, read); 1706 otp_bit = (read[0] >> bit_offset) & 0x1; 1707 1708 if (otp_bit == 1 && value == 0) { 1709 printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset); 1710 printf("OTP is programed, which can't be cleaned\n"); 1711 return OTP_FAILURE; 1712 } 1713 } else { 1714 otp_read_data(otp_dw_offset - 1, read); 1715 otp_bit = (read[1] >> bit_offset) & 0x1; 1716 1717 if (otp_bit == 0 && value == 1) { 1718 printf("OTPDATA%X[%X] = 1\n", otp_dw_offset, bit_offset); 1719 printf("OTP is programed, which can't be writen\n"); 1720 return OTP_FAILURE; 1721 } 1722 } 1723 if (otp_bit == value) { 1724 printf("OTPDATA%X[%X] = %d\n", otp_dw_offset, bit_offset, value); 1725 printf("No need to program\n"); 1726 return OTP_SUCCESS; 1727 } 1728 1729 printf("Program OTPDATA%X[%X] to 1\n", otp_dw_offset, bit_offset); 1730 break; 1731 case OTP_REGION_STRAP: 1732 otp_strap_status(otpstrap); 1733 otp_print_strap(bit_offset, 1); 1734 ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0, 0); 1735 if (ret == OTP_FAILURE) 1736 return OTP_FAILURE; 1737 else if (ret == OTP_PROG_SKIP) 1738 return OTP_SUCCESS; 1739 1740 break; 1741 } 1742 1743 if (!nconfirm) { 1744 printf("type \"YES\" (no quotes) to continue:\n"); 1745 if (!confirm_yesno()) { 1746 printf(" Aborting\n"); 1747 return OTP_FAILURE; 1748 } 1749 } 1750 1751 switch (mode) { 1752 case OTP_REGION_STRAP: 1753 ret = otp_prog_strap_bit(bit_offset, value); 1754 break; 1755 case OTP_REGION_CONF: 1756 case OTP_REGION_DATA: 1757 ret = otp_prog_bit(value, prog_address, bit_offset); 1758 break; 1759 } 1760 otp_soak(0); 1761 if (ret) { 1762 printf("SUCCESS\n"); 1763 return OTP_SUCCESS; 1764 } else { 1765 printf("OTP cannot be programed\n"); 1766 printf("FAILED\n"); 1767 return OTP_FAILURE; 1768 } 1769 1770 return OTP_USAGE; 1771 } 1772 1773 static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 1774 { 1775 uint32_t offset, count; 1776 int ret; 1777 1778 if (argc == 4) { 1779 offset = simple_strtoul(argv[2], NULL, 16); 1780 count = simple_strtoul(argv[3], NULL, 16); 1781 } else if (argc == 3) { 1782 offset = simple_strtoul(argv[2], NULL, 16); 1783 count = 1; 1784 } else { 1785 return CMD_RET_USAGE; 1786 } 1787 1788 1789 if (!strcmp(argv[1], "conf")) { 1790 writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 1791 ret = otp_print_config(offset, count); 1792 } else if (!strcmp(argv[1], "data")) { 1793 writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 1794 ret = otp_print_data(offset, count); 1795 } else if (!strcmp(argv[1], "strap")) { 1796 writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 1797 ret = otp_print_strap(offset, count); 1798 } else { 1799 return CMD_RET_USAGE; 1800 } 1801 1802 if (ret == OTP_SUCCESS) 1803 return CMD_RET_SUCCESS; 1804 else 1805 return CMD_RET_USAGE; 1806 1807 } 1808 1809 static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 1810 { 1811 phys_addr_t addr; 1812 int ret; 1813 1814 if (argc == 3) { 1815 if (strcmp(argv[1], "o")) 1816 return CMD_RET_USAGE; 1817 addr = simple_strtoul(argv[2], NULL, 16); 1818 writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 1819 ret = do_otp_prog(addr, 1); 1820 } else if (argc == 2) { 1821 addr = simple_strtoul(argv[1], NULL, 16); 1822 writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 1823 ret = do_otp_prog(addr, 0); 1824 } else { 1825 return CMD_RET_USAGE; 1826 } 1827 1828 if (ret == OTP_SUCCESS) 1829 return CMD_RET_SUCCESS; 1830 else if (ret == OTP_FAILURE) 1831 return CMD_RET_FAILURE; 1832 else 1833 return CMD_RET_USAGE; 1834 } 1835 1836 static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 1837 { 1838 int mode = 0; 1839 int nconfirm = 0; 1840 int otp_addr = 0; 1841 int bit_offset; 1842 int value; 1843 int ret; 1844 1845 if (argc != 4 && argc != 5 && argc != 6) 1846 return CMD_RET_USAGE; 1847 1848 /* Drop the pb cmd */ 1849 argc--; 1850 argv++; 1851 1852 if (!strcmp(argv[0], "conf")) 1853 mode = OTP_REGION_CONF; 1854 else if (!strcmp(argv[0], "strap")) 1855 mode = OTP_REGION_STRAP; 1856 else if (!strcmp(argv[0], "data")) 1857 mode = OTP_REGION_DATA; 1858 else 1859 return CMD_RET_USAGE; 1860 1861 /* Drop the region cmd */ 1862 argc--; 1863 argv++; 1864 1865 if (!strcmp(argv[0], "o")) { 1866 nconfirm = 1; 1867 /* Drop the force option */ 1868 argc--; 1869 argv++; 1870 } 1871 1872 if (mode == OTP_REGION_STRAP) { 1873 bit_offset = simple_strtoul(argv[0], NULL, 16); 1874 value = simple_strtoul(argv[1], NULL, 16); 1875 if (bit_offset >= 64 || (value != 0 && value != 1)) 1876 return CMD_RET_USAGE; 1877 } else { 1878 otp_addr = simple_strtoul(argv[0], NULL, 16); 1879 bit_offset = simple_strtoul(argv[1], NULL, 16); 1880 value = simple_strtoul(argv[2], NULL, 16); 1881 if (bit_offset >= 32 || (value != 0 && value != 1)) 1882 return CMD_RET_USAGE; 1883 if (mode == OTP_REGION_DATA) { 1884 if (otp_addr >= 0x800) 1885 return CMD_RET_USAGE; 1886 } else { 1887 if (otp_addr >= 0x20) 1888 return CMD_RET_USAGE; 1889 } 1890 } 1891 if (value != 0 && value != 1) 1892 return CMD_RET_USAGE; 1893 1894 writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 1895 ret = do_otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm); 1896 1897 if (ret == OTP_SUCCESS) 1898 return CMD_RET_SUCCESS; 1899 else if (ret == OTP_FAILURE) 1900 return CMD_RET_FAILURE; 1901 else 1902 return CMD_RET_USAGE; 1903 } 1904 1905 static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 1906 { 1907 phys_addr_t addr; 1908 int otp_addr = 0; 1909 1910 if (argc != 3) 1911 return CMD_RET_USAGE; 1912 1913 writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 1914 addr = simple_strtoul(argv[1], NULL, 16); 1915 otp_addr = simple_strtoul(argv[2], NULL, 16); 1916 if (otp_compare(otp_addr, addr) == 0) { 1917 printf("Compare pass\n"); 1918 return CMD_RET_SUCCESS; 1919 } else { 1920 printf("Compare fail\n"); 1921 return CMD_RET_FAILURE; 1922 } 1923 } 1924 1925 static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 1926 { 1927 int view = 0; 1928 int input; 1929 1930 if (argc != 2 && argc != 3) 1931 return CMD_RET_USAGE; 1932 1933 if (!strcmp(argv[1], "conf")) { 1934 1935 writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 1936 if (argc == 3) { 1937 input = simple_strtoul(argv[2], NULL, 16); 1938 otp_print_conf_info(input); 1939 } else { 1940 otp_print_conf_info(-1); 1941 } 1942 } else if (!strcmp(argv[1], "strap")) { 1943 if (!strcmp(argv[2], "v")) { 1944 view = 1; 1945 /* Drop the view option */ 1946 argc--; 1947 argv++; 1948 } 1949 writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 1950 otp_print_strap_info(view); 1951 } else { 1952 return CMD_RET_USAGE; 1953 } 1954 1955 return CMD_RET_SUCCESS; 1956 } 1957 1958 static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 1959 { 1960 int input; 1961 int bit_offset; 1962 int prog_address; 1963 int ret; 1964 if (argc != 3 && argc != 2) 1965 return CMD_RET_USAGE; 1966 1967 if (!strcmp(argv[0], "o")) { 1968 input = simple_strtoul(argv[2], NULL, 16); 1969 } else { 1970 input = simple_strtoul(argv[1], NULL, 16); 1971 printf("OTPSTRAP[%d] will be protected\n", input); 1972 printf("type \"YES\" (no quotes) to continue:\n"); 1973 if (!confirm_yesno()) { 1974 printf(" Aborting\n"); 1975 return CMD_RET_FAILURE; 1976 } 1977 } 1978 1979 prog_address = 0x800; 1980 if (input < 32) { 1981 bit_offset = input; 1982 prog_address |= 0x60c; 1983 } else if (input < 64) { 1984 bit_offset = input - 32; 1985 prog_address |= 0x60e; 1986 } else { 1987 return CMD_RET_USAGE; 1988 } 1989 1990 if (verify_bit(prog_address, bit_offset, 1) == 0) { 1991 printf("OTPSTRAP[%d] already protected\n", input); 1992 } 1993 1994 ret = otp_prog_bit(1, prog_address, bit_offset); 1995 otp_soak(0); 1996 1997 if (ret) { 1998 printf("OTPSTRAP[%d] is protected\n", input); 1999 return CMD_RET_SUCCESS; 2000 } 2001 2002 printf("Protect OTPSTRAP[%d] fail\n", input); 2003 return CMD_RET_FAILURE; 2004 2005 } 2006 2007 static int do_otpver(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2008 { 2009 printf("OTP tool version: %s\n", OTP_VER); 2010 printf("OTP info version: %s\n", OTP_INFO_VER); 2011 2012 return CMD_RET_SUCCESS; 2013 } 2014 2015 static cmd_tbl_t cmd_otp[] = { 2016 U_BOOT_CMD_MKENT(version, 1, 0, do_otpver, "", ""), 2017 U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""), 2018 U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""), 2019 U_BOOT_CMD_MKENT(prog, 3, 0, do_otpprog, "", ""), 2020 U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""), 2021 U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""), 2022 U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""), 2023 }; 2024 2025 static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2026 { 2027 cmd_tbl_t *cp; 2028 uint32_t ver; 2029 2030 cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp)); 2031 2032 /* Drop the otp command */ 2033 argc--; 2034 argv++; 2035 2036 if (cp == NULL || argc > cp->maxargs) 2037 return CMD_RET_USAGE; 2038 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp)) 2039 return CMD_RET_SUCCESS; 2040 2041 ver = chip_version(); 2042 switch (ver) { 2043 case OTP_AST2600A0: 2044 info_cb.version = OTP_AST2600A0; 2045 info_cb.conf_info = a0_conf_info; 2046 info_cb.conf_info_len = ARRAY_SIZE(a0_conf_info); 2047 info_cb.strap_info = a0_strap_info; 2048 info_cb.strap_info_len = ARRAY_SIZE(a0_strap_info); 2049 info_cb.key_info = a0_key_type; 2050 info_cb.key_info_len = ARRAY_SIZE(a0_key_type); 2051 break; 2052 case OTP_AST2600A1: 2053 info_cb.version = OTP_AST2600A1; 2054 info_cb.conf_info = a1_conf_info; 2055 info_cb.conf_info_len = ARRAY_SIZE(a1_conf_info); 2056 info_cb.strap_info = a1_strap_info; 2057 info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info); 2058 info_cb.key_info = a1_key_type; 2059 info_cb.key_info_len = ARRAY_SIZE(a1_key_type); 2060 break; 2061 case OTP_AST2600A2: 2062 info_cb.version = OTP_AST2600A2; 2063 info_cb.conf_info = a2_conf_info; 2064 info_cb.conf_info_len = ARRAY_SIZE(a2_conf_info); 2065 info_cb.strap_info = a2_strap_info; 2066 info_cb.strap_info_len = ARRAY_SIZE(a2_strap_info); 2067 info_cb.key_info = a2_key_type; 2068 info_cb.key_info_len = ARRAY_SIZE(a2_key_type); 2069 break; 2070 default: 2071 return CMD_RET_FAILURE; 2072 } 2073 2074 return cp->cmd(cmdtp, flag, argc, argv); 2075 } 2076 2077 U_BOOT_CMD( 2078 otp, 7, 0, do_ast_otp, 2079 "ASPEED One-Time-Programmable sub-system", 2080 "version\n" 2081 "otp read conf|data <otp_dw_offset> <dw_count>\n" 2082 "otp read strap <strap_bit_offset> <bit_count>\n" 2083 "otp info strap [v]\n" 2084 "otp info conf [otp_dw_offset]\n" 2085 "otp prog [o] <addr>\n" 2086 "otp pb conf|data [o] <otp_dw_offset> <bit_offset> <value>\n" 2087 "otp pb strap [o] <bit_offset> <value>\n" 2088 "otp protect [o] <bit_offset>\n" 2089 "otp cmp <addr> <otp_dw_offset>\n" 2090 ); 2091