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