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