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 <linux/iopoll.h> 19 #include <u-boot/sha256.h> 20 #include "otp_info.h" 21 22 DECLARE_GLOBAL_DATA_PTR; 23 24 #define OTP_VER "1.2.1" 25 26 #define OTP_PASSWD 0x349fe38a 27 #define RETRY 20 28 #define OTP_REGION_STRAP BIT(0) 29 #define OTP_REGION_CONF BIT(1) 30 #define OTP_REGION_DATA BIT(2) 31 32 #define OTP_USAGE -1 33 #define OTP_FAILURE -2 34 #define OTP_SUCCESS 0 35 36 #define OTP_PROG_SKIP 1 37 38 #define OTP_KEY_TYPE_RSA_PUB 1 39 #define OTP_KEY_TYPE_RSA_PRIV 2 40 #define OTP_KEY_TYPE_AES 3 41 #define OTP_KEY_TYPE_VAULT 4 42 #define OTP_KEY_TYPE_HMAC 5 43 44 #define OTP_BASE 0x1e6f2000 45 #define OTP_PROTECT_KEY OTP_BASE 46 #define OTP_COMMAND OTP_BASE + 0x4 47 #define OTP_TIMING OTP_BASE + 0x8 48 #define OTP_ADDR OTP_BASE + 0x10 49 #define OTP_STATUS OTP_BASE + 0x14 50 #define OTP_COMPARE_1 OTP_BASE + 0x20 51 #define OTP_COMPARE_2 OTP_BASE + 0x24 52 #define OTP_COMPARE_3 OTP_BASE + 0x28 53 #define OTP_COMPARE_4 OTP_BASE + 0x2c 54 #define SW_REV_ID0 OTP_BASE + 0x68 55 #define SW_REV_ID1 OTP_BASE + 0x6c 56 #define SEC_KEY_NUM OTP_BASE + 0x78 57 58 #define OTP_MAGIC "SOCOTP" 59 #define CHECKSUM_LEN 32 60 #define OTP_INC_DATA BIT(31) 61 #define OTP_INC_CONFIG BIT(30) 62 #define OTP_INC_STRAP BIT(29) 63 #define OTP_ECC_EN BIT(28) 64 #define OTP_INC_SCU_PRO BIT(25) 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 0x05010303 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 #define SOC_AST2600A0 0 98 #define SOC_AST2600A1 1 99 #define SOC_AST2600A2 2 100 #define SOC_AST2600A3 3 101 102 #define OTPTOOL_VERSION(a, b, c) (((a) << 24) + ((b) << 12) + (c)) 103 #define OTPTOOL_VERSION_MAJOR(x) (((x) >> 24) & 0xff) 104 #define OTPTOOL_VERSION_PATCHLEVEL(x) (((x) >> 12) & 0xfff) 105 #define OTPTOOL_VERSION_SUBLEVEL(x) ((x) & 0xfff) 106 #define OTPTOOL_COMPT_VERSION 1 107 108 struct otp_header { 109 u8 otp_magic[8]; 110 u32 soc_ver; 111 u32 otptool_ver; 112 u32 image_info; 113 u32 data_info; 114 u32 config_info; 115 u32 strap_info; 116 u32 scu_protect_info; 117 u32 checksum_offset; 118 } __packed; 119 120 struct otpstrap_status { 121 int value; 122 int option_array[7]; 123 int remain_times; 124 int writeable_option; 125 int protected; 126 }; 127 128 struct otpkey_type { 129 int value; 130 int key_type; 131 int need_id; 132 char information[110]; 133 }; 134 135 struct otp_pro_sts { 136 char mem_lock; 137 char pro_key_ret; 138 char pro_strap; 139 char pro_conf; 140 char pro_data; 141 char pro_sec; 142 u32 sec_size; 143 }; 144 145 struct otp_info_cb { 146 int version; 147 char ver_name[3]; 148 const struct otpstrap_info *strap_info; 149 int strap_info_len; 150 const struct otpconf_info *conf_info; 151 int conf_info_len; 152 const struct otpkey_type *key_info; 153 int key_info_len; 154 const struct scu_info *scu_info; 155 int scu_info_len; 156 struct otp_pro_sts pro_sts; 157 }; 158 159 struct otp_image_layout { 160 int data_length; 161 int conf_length; 162 int strap_length; 163 int scu_pro_length; 164 u8 *data; 165 u8 *data_ignore; 166 u8 *conf; 167 u8 *conf_ignore; 168 u8 *strap; 169 u8 *strap_pro; 170 u8 *strap_ignore; 171 u8 *scu_pro; 172 u8 *scu_pro_ignore; 173 }; 174 175 static struct otp_info_cb info_cb; 176 177 static const struct otpkey_type a0_key_type[] = { 178 {0, OTP_KEY_TYPE_AES, 0, "AES-256 as OEM platform key for image encryption/decryption"}, 179 {1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"}, 180 {4, OTP_KEY_TYPE_HMAC, 1, "HMAC as encrypted OEM HMAC keys in Mode 1"}, 181 {8, OTP_KEY_TYPE_RSA_PUB, 1, "RSA-public as OEM DSS public keys in Mode 2"}, 182 {9, OTP_KEY_TYPE_RSA_PUB, 0, "RSA-public as SOC public key"}, 183 {10, OTP_KEY_TYPE_RSA_PUB, 0, "RSA-public as AES key decryption key"}, 184 {13, OTP_KEY_TYPE_RSA_PRIV, 0, "RSA-private as SOC private key"}, 185 {14, OTP_KEY_TYPE_RSA_PRIV, 0, "RSA-private as AES key decryption key"}, 186 }; 187 188 static const struct otpkey_type a1_key_type[] = { 189 {1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"}, 190 {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"}, 191 {8, OTP_KEY_TYPE_RSA_PUB, 1, "RSA-public as OEM DSS public keys in Mode 2"}, 192 {10, OTP_KEY_TYPE_RSA_PUB, 0, "RSA-public as AES key decryption key"}, 193 {14, OTP_KEY_TYPE_RSA_PRIV, 0, "RSA-private as AES key decryption key"}, 194 }; 195 196 static const struct otpkey_type a2_key_type[] = { 197 {1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"}, 198 {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"}, 199 {8, OTP_KEY_TYPE_RSA_PUB, 1, "RSA-public as OEM DSS public keys in Mode 2"}, 200 {10, OTP_KEY_TYPE_RSA_PUB, 0, "RSA-public as AES key decryption key"}, 201 {14, OTP_KEY_TYPE_RSA_PRIV, 0, "RSA-private as AES key decryption key"}, 202 }; 203 204 static const struct otpkey_type a3_key_type[] = { 205 {1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"}, 206 {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"}, 207 {8, OTP_KEY_TYPE_RSA_PUB, 1, "RSA-public as OEM DSS public keys in Mode 2"}, 208 {9, OTP_KEY_TYPE_RSA_PUB, 1, "RSA-public as OEM DSS public keys in Mode 2(big endian)"}, 209 {10, OTP_KEY_TYPE_RSA_PUB, 0, "RSA-public as AES key decryption key"}, 210 {11, OTP_KEY_TYPE_RSA_PUB, 0, "RSA-public as AES key decryption key(big endian)"}, 211 {12, OTP_KEY_TYPE_RSA_PRIV, 0, "RSA-private as AES key decryption key"}, 212 {13, OTP_KEY_TYPE_RSA_PRIV, 0, "RSA-private as AES key decryption key(big endian)"}, 213 }; 214 215 static void buf_print(u8 *buf, int len) 216 { 217 int i; 218 219 printf(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n"); 220 for (i = 0; i < len; i++) { 221 if (i % 16 == 0) 222 printf("%04X: ", i); 223 printf("%02X ", buf[i]); 224 if ((i + 1) % 16 == 0) 225 printf("\n"); 226 } 227 printf("\n"); 228 } 229 230 static int get_dw_bit(u32 *rid, int offset) 231 { 232 int bit_offset; 233 int i; 234 235 if (offset < 32) { 236 i = 0; 237 bit_offset = offset; 238 } else { 239 i = 1; 240 bit_offset = offset - 32; 241 } 242 if ((rid[i] >> bit_offset) & 0x1) 243 return 1; 244 else 245 return 0; 246 } 247 248 static int get_rid_num(u32 *rid) 249 { 250 int i; 251 int fz = 0; 252 int rid_num = 0; 253 int ret = 0; 254 255 for (i = 0; i < 64; i++) { 256 if (get_dw_bit(rid, i) == 0) { 257 if (!fz) 258 fz = 1; 259 260 } else { 261 rid_num++; 262 if (fz) 263 ret = OTP_FAILURE; 264 } 265 } 266 if (ret) 267 return ret; 268 269 return rid_num; 270 } 271 272 static u32 chip_version(void) 273 { 274 u32 revid0, revid1; 275 276 revid0 = readl(ASPEED_REVISION_ID0); 277 revid1 = readl(ASPEED_REVISION_ID1); 278 279 if (revid0 == ID0_AST2600A0 && revid1 == ID1_AST2600A0) { 280 /* AST2600-A0 */ 281 return OTP_A0; 282 } else if (revid0 == ID0_AST2600A1 && revid1 == ID1_AST2600A1) { 283 /* AST2600-A1 */ 284 return OTP_A1; 285 } else if (revid0 == ID0_AST2600A2 && revid1 == ID1_AST2600A2) { 286 /* AST2600-A2 */ 287 return OTP_A2; 288 } else if (revid0 == ID0_AST2600A3 && revid1 == ID1_AST2600A3) { 289 /* AST2600-A3 */ 290 return OTP_A3; 291 } else if (revid0 == ID0_AST2620A1 && revid1 == ID1_AST2620A1) { 292 /* AST2620-A1 */ 293 return OTP_A1; 294 } else if (revid0 == ID0_AST2620A2 && revid1 == ID1_AST2620A2) { 295 /* AST2620-A2 */ 296 return OTP_A2; 297 } else if (revid0 == ID0_AST2620A3 && revid1 == ID1_AST2620A3) { 298 /* AST2620-A3 */ 299 return OTP_A3; 300 } else if (revid0 == ID0_AST2605A2 && revid1 == ID1_AST2605A2) { 301 /* AST2605-A2 */ 302 return OTP_A2; 303 } else if (revid0 == ID0_AST2605A3 && revid1 == ID1_AST2605A3) { 304 /* AST2605-A3 */ 305 return OTP_A3; 306 } else if (revid0 == ID0_AST2625A3 && revid1 == ID1_AST2625A3) { 307 /* AST2605-A3 */ 308 return OTP_A3; 309 } 310 return OTP_FAILURE; 311 } 312 313 static int wait_complete(void) 314 { 315 u32 val; 316 int ret; 317 318 udelay(1); 319 ret = readl_poll_timeout(OTP_STATUS, val, (val & 0x6) == 0x6, 100000); 320 if (ret) 321 printf("%s: timeout, SEC14 = 0x%x\n", __func__, val); 322 323 return ret; 324 } 325 326 static void otp_write(u32 otp_addr, u32 data) 327 { 328 writel(otp_addr, OTP_ADDR); //write address 329 writel(data, OTP_COMPARE_1); //write data 330 writel(0x23b1e362, OTP_COMMAND); //write command 331 wait_complete(); 332 } 333 334 static void otp_soak(int soak) 335 { 336 if (info_cb.version == OTP_A2 || info_cb.version == OTP_A3) { 337 switch (soak) { 338 case 0: //default 339 otp_write(0x3000, 0x0); // Write MRA 340 otp_write(0x5000, 0x0); // Write MRB 341 otp_write(0x1000, 0x0); // Write MR 342 break; 343 case 1: //normal program 344 otp_write(0x3000, 0x1320); // Write MRA 345 otp_write(0x5000, 0x1008); // Write MRB 346 otp_write(0x1000, 0x0024); // Write MR 347 writel(0x04191388, OTP_TIMING); // 200us 348 break; 349 case 2: //soak program 350 otp_write(0x3000, 0x1320); // Write MRA 351 otp_write(0x5000, 0x0007); // Write MRB 352 otp_write(0x1000, 0x0100); // Write MR 353 writel(0x04193a98, OTP_TIMING); // 600us 354 break; 355 } 356 } else { 357 switch (soak) { 358 case 0: //default 359 otp_write(0x3000, 0x0); // Write MRA 360 otp_write(0x5000, 0x0); // Write MRB 361 otp_write(0x1000, 0x0); // Write MR 362 break; 363 case 1: //normal program 364 otp_write(0x3000, 0x4021); // Write MRA 365 otp_write(0x5000, 0x302f); // Write MRB 366 otp_write(0x1000, 0x4020); // Write MR 367 writel(0x04190760, OTP_TIMING); // 75us 368 break; 369 case 2: //soak program 370 otp_write(0x3000, 0x4021); // Write MRA 371 otp_write(0x5000, 0x1027); // Write MRB 372 otp_write(0x1000, 0x4820); // Write MR 373 writel(0x041930d4, OTP_TIMING); // 500us 374 break; 375 } 376 } 377 378 wait_complete(); 379 } 380 381 static void otp_read_data(u32 offset, u32 *data) 382 { 383 writel(offset, OTP_ADDR); //Read address 384 writel(0x23b1e361, OTP_COMMAND); //trigger read 385 wait_complete(); 386 data[0] = readl(OTP_COMPARE_1); 387 data[1] = readl(OTP_COMPARE_2); 388 } 389 390 static void otp_read_conf(u32 offset, u32 *data) 391 { 392 int config_offset; 393 394 config_offset = 0x800; 395 config_offset |= (offset / 8) * 0x200; 396 config_offset |= (offset % 8) * 0x2; 397 398 writel(config_offset, OTP_ADDR); //Read address 399 writel(0x23b1e361, OTP_COMMAND); //trigger read 400 wait_complete(); 401 data[0] = readl(OTP_COMPARE_1); 402 } 403 404 static int otp_compare(u32 otp_addr, u32 addr) 405 { 406 u32 ret; 407 u32 *buf; 408 409 buf = map_physmem(addr, 16, MAP_WRBACK); 410 printf("%08X\n", buf[0]); 411 printf("%08X\n", buf[1]); 412 printf("%08X\n", buf[2]); 413 printf("%08X\n", buf[3]); 414 writel(otp_addr, OTP_ADDR); //Compare address 415 writel(buf[0], OTP_COMPARE_1); //Compare data 1 416 writel(buf[1], OTP_COMPARE_2); //Compare data 2 417 writel(buf[2], OTP_COMPARE_3); //Compare data 3 418 writel(buf[3], OTP_COMPARE_4); //Compare data 4 419 writel(0x23b1e363, OTP_COMMAND); //Compare command 420 wait_complete(); 421 ret = readl(OTP_STATUS); //Compare command 422 if (ret & 0x1) 423 return OTP_SUCCESS; 424 else 425 return OTP_FAILURE; 426 } 427 428 static int verify_bit(u32 otp_addr, int bit_offset, int value) 429 { 430 u32 ret[2]; 431 432 if (otp_addr % 2 == 0) 433 writel(otp_addr, OTP_ADDR); //Read address 434 else 435 writel(otp_addr - 1, OTP_ADDR); //Read address 436 437 writel(0x23b1e361, OTP_COMMAND); //trigger read 438 wait_complete(); 439 ret[0] = readl(OTP_COMPARE_1); 440 ret[1] = readl(OTP_COMPARE_2); 441 442 if (otp_addr % 2 == 0) { 443 if (((ret[0] >> bit_offset) & 1) == value) 444 return OTP_SUCCESS; 445 else 446 return OTP_FAILURE; 447 } else { 448 if (((ret[1] >> bit_offset) & 1) == value) 449 return OTP_SUCCESS; 450 else 451 return OTP_FAILURE; 452 } 453 } 454 455 static u32 verify_dw(u32 otp_addr, u32 *value, u32 *ignore, u32 *compare, int size) 456 { 457 u32 ret[2]; 458 459 otp_addr &= ~(1 << 15); 460 461 if (otp_addr % 2 == 0) 462 writel(otp_addr, OTP_ADDR); //Read address 463 else 464 writel(otp_addr - 1, OTP_ADDR); //Read address 465 writel(0x23b1e361, OTP_COMMAND); //trigger read 466 wait_complete(); 467 ret[0] = readl(OTP_COMPARE_1); 468 ret[1] = readl(OTP_COMPARE_2); 469 if (size == 1) { 470 if (otp_addr % 2 == 0) { 471 // printf("check %x : %x = %x\n", otp_addr, ret[0], value[0]); 472 if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0])) { 473 compare[0] = 0; 474 return OTP_SUCCESS; 475 } 476 compare[0] = value[0] ^ ret[0]; 477 return OTP_FAILURE; 478 479 } else { 480 // printf("check %x : %x = %x\n", otp_addr, ret[1], value[0]); 481 if ((value[0] & ~ignore[0]) == (ret[1] & ~ignore[0])) { 482 compare[0] = ~0; 483 return OTP_SUCCESS; 484 } 485 compare[0] = ~(value[0] ^ ret[1]); 486 return OTP_FAILURE; 487 } 488 } else if (size == 2) { 489 // otp_addr should be even 490 if ((value[0] & ~ignore[0]) == (ret[0] & ~ignore[0]) && (value[1] & ~ignore[1]) == (ret[1] & ~ignore[1])) { 491 // printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]); 492 // printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]); 493 compare[0] = 0; 494 compare[1] = ~0; 495 return OTP_SUCCESS; 496 } 497 // printf("check[0] %x : %x = %x\n", otp_addr, ret[0], value[0]); 498 // printf("check[1] %x : %x = %x\n", otp_addr, ret[1], value[1]); 499 compare[0] = value[0] ^ ret[0]; 500 compare[1] = ~(value[1] ^ ret[1]); 501 return OTP_FAILURE; 502 } else { 503 return OTP_FAILURE; 504 } 505 } 506 507 static int otp_prog(u32 otp_addr, u32 prog_bit) 508 { 509 otp_write(0x0, prog_bit); 510 writel(otp_addr, OTP_ADDR); //write address 511 writel(prog_bit, OTP_COMPARE_1); //write data 512 writel(0x23b1e364, OTP_COMMAND); //write command 513 514 return wait_complete(); 515 } 516 517 static int _otp_prog_bit(u32 value, u32 prog_address, u32 bit_offset) 518 { 519 int prog_bit; 520 521 if (prog_address % 2 == 0) { 522 if (value) 523 prog_bit = ~(0x1 << bit_offset); 524 else 525 return 0; 526 } else { 527 if (info_cb.version != OTP_A3) 528 prog_address |= 1 << 15; 529 if (!value) 530 prog_bit = 0x1 << bit_offset; 531 else 532 return 0; 533 } 534 return otp_prog(prog_address, prog_bit); 535 } 536 537 static int otp_prog_dc_b(u32 value, u32 prog_address, u32 bit_offset) 538 { 539 int pass; 540 int i; 541 int ret; 542 543 otp_soak(1); 544 ret = _otp_prog_bit(value, prog_address, bit_offset); 545 if (ret) 546 return OTP_FAILURE; 547 pass = 0; 548 549 for (i = 0; i < RETRY; i++) { 550 if (verify_bit(prog_address, bit_offset, value) != 0) { 551 otp_soak(2); 552 ret = _otp_prog_bit(value, prog_address, bit_offset); 553 if (ret) 554 return OTP_FAILURE; 555 if (verify_bit(prog_address, bit_offset, value) != 0) { 556 otp_soak(1); 557 } else { 558 pass = 1; 559 break; 560 } 561 } else { 562 pass = 1; 563 break; 564 } 565 } 566 if (pass) 567 return OTP_SUCCESS; 568 569 return OTP_FAILURE; 570 } 571 572 static int otp_prog_dw(u32 value, u32 ignore, u32 prog_address) 573 { 574 int j, bit_value, prog_bit; 575 int ret; 576 577 for (j = 0; j < 32; j++) { 578 if ((ignore >> j) & 0x1) 579 continue; 580 bit_value = (value >> j) & 0x1; 581 if (prog_address % 2 == 0) { 582 if (bit_value) 583 prog_bit = ~(0x1 << j); 584 else 585 continue; 586 } else { 587 if (info_cb.version != OTP_A3) 588 prog_address |= 1 << 15; 589 if (bit_value) 590 continue; 591 else 592 prog_bit = 0x1 << j; 593 } 594 ret = otp_prog(prog_address, prog_bit); 595 if (ret) 596 return ret; 597 } 598 return 0; 599 } 600 601 static int otp_prog_verify_2dw(u32 *data, u32 *buf, u32 *ignore_mask, u32 prog_address) 602 { 603 int pass; 604 int i; 605 u32 data0_masked; 606 u32 data1_masked; 607 u32 buf0_masked; 608 u32 buf1_masked; 609 u32 compare[2]; 610 int ret; 611 612 data0_masked = data[0] & ~ignore_mask[0]; 613 buf0_masked = buf[0] & ~ignore_mask[0]; 614 data1_masked = data[1] & ~ignore_mask[1]; 615 buf1_masked = buf[1] & ~ignore_mask[1]; 616 if (data0_masked == buf0_masked && data1_masked == buf1_masked) 617 return OTP_SUCCESS; 618 619 for (i = 0; i < 32; i++) { 620 if (((data0_masked >> i) & 0x1) == 1 && ((buf0_masked >> i) & 0x1) == 0) 621 return OTP_FAILURE; 622 if (((data1_masked >> i) & 0x1) == 0 && ((buf1_masked >> i) & 0x1) == 1) 623 return OTP_FAILURE; 624 } 625 626 otp_soak(1); 627 if (data0_masked != buf0_masked) { 628 ret = otp_prog_dw(buf[0], ignore_mask[0], prog_address); 629 if (ret) 630 return OTP_FAILURE; 631 } 632 633 if (data1_masked != buf1_masked) { 634 ret = otp_prog_dw(buf[1], ignore_mask[1], prog_address + 1); 635 if (ret) 636 return OTP_FAILURE; 637 } 638 639 pass = 0; 640 for (i = 0; i < RETRY; i++) { 641 if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) { 642 otp_soak(2); 643 if (compare[0] != 0) { 644 ret = otp_prog_dw(compare[0], ignore_mask[0], prog_address); 645 if (ret) 646 return OTP_FAILURE; 647 } 648 if (compare[1] != ~0) { 649 ret = otp_prog_dw(compare[1], ignore_mask[1], prog_address + 1); 650 if (ret) 651 return OTP_FAILURE; 652 } 653 if (verify_dw(prog_address, buf, ignore_mask, compare, 2) != 0) { 654 otp_soak(1); 655 } else { 656 pass = 1; 657 break; 658 } 659 } else { 660 pass = 1; 661 break; 662 } 663 } 664 665 if (!pass) { 666 otp_soak(0); 667 return OTP_FAILURE; 668 } 669 return OTP_SUCCESS; 670 } 671 672 static void otp_strap_status(struct otpstrap_status *otpstrap) 673 { 674 u32 OTPSTRAP_RAW[2]; 675 int strap_end; 676 int i, j; 677 678 if (info_cb.version == OTP_A0) { 679 for (j = 0; j < 64; j++) { 680 otpstrap[j].value = 0; 681 otpstrap[j].remain_times = 7; 682 otpstrap[j].writeable_option = -1; 683 otpstrap[j].protected = 0; 684 } 685 strap_end = 30; 686 } else { 687 for (j = 0; j < 64; j++) { 688 otpstrap[j].value = 0; 689 otpstrap[j].remain_times = 6; 690 otpstrap[j].writeable_option = -1; 691 otpstrap[j].protected = 0; 692 } 693 strap_end = 28; 694 } 695 696 otp_soak(0); 697 for (i = 16; i < strap_end; i += 2) { 698 int option = (i - 16) / 2; 699 700 otp_read_conf(i, &OTPSTRAP_RAW[0]); 701 otp_read_conf(i + 1, &OTPSTRAP_RAW[1]); 702 for (j = 0; j < 32; j++) { 703 char bit_value = ((OTPSTRAP_RAW[0] >> j) & 0x1); 704 705 if (bit_value == 0 && otpstrap[j].writeable_option == -1) 706 otpstrap[j].writeable_option = option; 707 if (bit_value == 1) 708 otpstrap[j].remain_times--; 709 otpstrap[j].value ^= bit_value; 710 otpstrap[j].option_array[option] = bit_value; 711 } 712 for (j = 32; j < 64; j++) { 713 char bit_value = ((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1); 714 715 if (bit_value == 0 && otpstrap[j].writeable_option == -1) 716 otpstrap[j].writeable_option = option; 717 if (bit_value == 1) 718 otpstrap[j].remain_times--; 719 otpstrap[j].value ^= bit_value; 720 otpstrap[j].option_array[option] = bit_value; 721 } 722 } 723 724 otp_read_conf(30, &OTPSTRAP_RAW[0]); 725 otp_read_conf(31, &OTPSTRAP_RAW[1]); 726 for (j = 0; j < 32; j++) { 727 if (((OTPSTRAP_RAW[0] >> j) & 0x1) == 1) 728 otpstrap[j].protected = 1; 729 } 730 for (j = 32; j < 64; j++) { 731 if (((OTPSTRAP_RAW[1] >> (j - 32)) & 0x1) == 1) 732 otpstrap[j].protected = 1; 733 } 734 } 735 736 static int otp_strap_bit_confirm(struct otpstrap_status *otpstrap, int offset, int ibit, int bit, int pbit) 737 { 738 int prog_flag = 0; 739 740 // ignore this bit 741 if (ibit == 1) 742 return OTP_SUCCESS; 743 printf("OTPSTRAP[0x%X]:\n", offset); 744 745 if (bit == otpstrap->value) { 746 if (!pbit) { 747 printf(" The value is same as before, skip it.\n"); 748 return OTP_PROG_SKIP; 749 } 750 printf(" The value is same as before.\n"); 751 } else { 752 prog_flag = 1; 753 } 754 if (otpstrap->protected == 1 && prog_flag) { 755 printf(" This bit is protected and is not writable\n"); 756 return OTP_FAILURE; 757 } 758 if (otpstrap->remain_times == 0 && prog_flag) { 759 printf(" This bit has no remaining chance to write.\n"); 760 return OTP_FAILURE; 761 } 762 if (pbit == 1) 763 printf(" This bit will be protected and become non-writable.\n"); 764 if (prog_flag) 765 printf(" Write 1 to OTPSTRAP[0x%X] OPTION[0x%X], that value becomes from 0x%X to 0x%X.\n", offset, otpstrap->writeable_option + 1, otpstrap->value, otpstrap->value ^ 1); 766 767 return OTP_SUCCESS; 768 } 769 770 static int otp_prog_strap_b(int bit_offset, int value) 771 { 772 struct otpstrap_status otpstrap[64]; 773 u32 prog_address; 774 int offset; 775 int ret; 776 777 otp_strap_status(otpstrap); 778 779 ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0); 780 781 if (ret != OTP_SUCCESS) 782 return ret; 783 784 prog_address = 0x800; 785 if (bit_offset < 32) { 786 offset = bit_offset; 787 prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) / 8) * 0x200; 788 prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 16) % 8) * 0x2; 789 790 } else { 791 offset = (bit_offset - 32); 792 prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) / 8) * 0x200; 793 prog_address |= ((otpstrap[bit_offset].writeable_option * 2 + 17) % 8) * 0x2; 794 } 795 796 return otp_prog_dc_b(1, prog_address, offset); 797 } 798 799 static int otp_print_conf(u32 offset, int dw_count) 800 { 801 int i; 802 u32 ret[1]; 803 804 if (offset + dw_count > 32) 805 return OTP_USAGE; 806 otp_soak(0); 807 for (i = offset; i < offset + dw_count; i++) { 808 otp_read_conf(i, ret); 809 printf("OTPCFG0x%X: 0x%08X\n", i, ret[0]); 810 } 811 printf("\n"); 812 return OTP_SUCCESS; 813 } 814 815 static int otp_print_data(u32 offset, int dw_count) 816 { 817 int i; 818 u32 ret[2]; 819 820 if (offset + dw_count > 2048 || offset % 4 != 0) 821 return OTP_USAGE; 822 otp_soak(0); 823 for (i = offset; i < offset + dw_count; i += 2) { 824 otp_read_data(i, ret); 825 if (i % 4 == 0) 826 printf("%03X: %08X %08X ", i * 4, ret[0], ret[1]); 827 else 828 printf("%08X %08X\n", ret[0], ret[1]); 829 } 830 printf("\n"); 831 return OTP_SUCCESS; 832 } 833 834 static int otp_print_strap(int start, int count) 835 { 836 int i, j; 837 int remains; 838 struct otpstrap_status otpstrap[64]; 839 840 if (start < 0 || start > 64) 841 return OTP_USAGE; 842 843 if ((start + count) < 0 || (start + count) > 64) 844 return OTP_USAGE; 845 846 otp_strap_status(otpstrap); 847 848 if (info_cb.version == OTP_A0) 849 remains = 7; 850 else 851 remains = 6; 852 printf("BIT(hex) Value Option Status\n"); 853 printf("______________________________________________________________________________\n"); 854 855 for (i = start; i < start + count; i++) { 856 printf("0x%-8X", i); 857 printf("%-7d", otpstrap[i].value); 858 for (j = 0; j < remains; j++) 859 printf("%d ", otpstrap[i].option_array[j]); 860 printf(" "); 861 if (otpstrap[i].protected == 1) { 862 printf("protected and not writable"); 863 } else { 864 printf("not protected "); 865 if (otpstrap[i].remain_times == 0) 866 printf("and no remaining times to write."); 867 else 868 printf("and still can write %d times", otpstrap[i].remain_times); 869 } 870 printf("\n"); 871 } 872 873 return OTP_SUCCESS; 874 } 875 876 static void otp_print_revid(u32 *rid) 877 { 878 int bit_offset; 879 int i, j; 880 881 printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); 882 printf("___________________________________________________\n"); 883 for (i = 0; i < 64; i++) { 884 if (i < 32) { 885 j = 0; 886 bit_offset = i; 887 } else { 888 j = 1; 889 bit_offset = i - 32; 890 } 891 if (i % 16 == 0) 892 printf("%2x | ", i); 893 printf("%d ", (rid[j] >> bit_offset) & 0x1); 894 if ((i + 1) % 16 == 0) 895 printf("\n"); 896 } 897 } 898 899 static int otp_print_scu_image(struct otp_image_layout *image_layout) 900 { 901 const struct scu_info *scu_info = info_cb.scu_info; 902 u32 *OTPSCU = (u32 *)image_layout->scu_pro; 903 u32 *OTPSCU_IGNORE = (u32 *)image_layout->scu_pro_ignore; 904 int i; 905 u32 scu_offset; 906 u32 dw_offset; 907 u32 bit_offset; 908 u32 mask; 909 u32 otp_value; 910 u32 otp_ignore; 911 912 printf("SCU BIT reg_protect Description\n"); 913 printf("____________________________________________________________________\n"); 914 for (i = 0; i < info_cb.scu_info_len; i++) { 915 mask = BIT(scu_info[i].length) - 1; 916 917 if (scu_info[i].bit_offset > 31) { 918 scu_offset = 0x510; 919 dw_offset = 1; 920 bit_offset = scu_info[i].bit_offset - 32; 921 } else { 922 scu_offset = 0x500; 923 dw_offset = 0; 924 bit_offset = scu_info[i].bit_offset; 925 } 926 927 otp_value = (OTPSCU[dw_offset] >> bit_offset) & mask; 928 otp_ignore = (OTPSCU_IGNORE[dw_offset] >> bit_offset) & mask; 929 930 if (otp_ignore == mask) 931 continue; 932 else if (otp_ignore != 0) 933 return OTP_FAILURE; 934 935 if (otp_value != 0 && otp_value != mask) 936 return OTP_FAILURE; 937 938 printf("0x%-6X", scu_offset); 939 if (scu_info[i].length == 1) 940 printf("0x%-11X", bit_offset); 941 else 942 printf("0x%-2X:0x%-6x", bit_offset, bit_offset + scu_info[i].length - 1); 943 printf("0x%-14X", otp_value); 944 printf("%s\n", scu_info[i].information); 945 } 946 return OTP_SUCCESS; 947 } 948 949 static void otp_print_scu_info(void) 950 { 951 const struct scu_info *scu_info = info_cb.scu_info; 952 u32 OTPCFG[2]; 953 u32 scu_offset; 954 u32 bit_offset; 955 u32 reg_p; 956 u32 length; 957 int i, j; 958 959 otp_soak(0); 960 otp_read_conf(28, &OTPCFG[0]); 961 otp_read_conf(29, &OTPCFG[1]); 962 printf("SCU BIT reg_protect Description\n"); 963 printf("____________________________________________________________________\n"); 964 for (i = 0; i < info_cb.scu_info_len; i++) { 965 length = scu_info[i].length; 966 for (j = 0; j < length; j++) { 967 if (scu_info[i].bit_offset + j < 32) { 968 scu_offset = 0x500; 969 bit_offset = scu_info[i].bit_offset + j; 970 reg_p = (OTPCFG[0] >> bit_offset) & 0x1; 971 } else { 972 scu_offset = 0x510; 973 bit_offset = scu_info[i].bit_offset + j - 32; 974 reg_p = (OTPCFG[1] >> bit_offset) & 0x1; 975 } 976 printf("0x%-6X", scu_offset); 977 printf("0x%-4X", bit_offset); 978 printf("0x%-13X", reg_p); 979 if (length == 1) { 980 printf(" %s\n", scu_info[i].information); 981 continue; 982 } 983 984 if (j == 0) 985 printf("/%s\n", scu_info[i].information); 986 else if (j == length - 1) 987 printf("\\ \"\n"); 988 else 989 printf("| \"\n"); 990 } 991 } 992 } 993 994 static int otp_print_conf_image(struct otp_image_layout *image_layout) 995 { 996 const struct otpconf_info *conf_info = info_cb.conf_info; 997 u32 *OTPCFG = (u32 *)image_layout->conf; 998 u32 *OTPCFG_IGNORE = (u32 *)image_layout->conf_ignore; 999 u32 mask; 1000 u32 dw_offset; 1001 u32 bit_offset; 1002 u32 otp_value; 1003 u32 otp_ignore; 1004 int fail = 0; 1005 int mask_err; 1006 int rid_num = 0; 1007 char valid_bit[20]; 1008 int fz; 1009 int i; 1010 int j; 1011 1012 printf("DW BIT Value Description\n"); 1013 printf("__________________________________________________________________________\n"); 1014 for (i = 0; i < info_cb.conf_info_len; i++) { 1015 mask_err = 0; 1016 dw_offset = conf_info[i].dw_offset; 1017 bit_offset = conf_info[i].bit_offset; 1018 mask = BIT(conf_info[i].length) - 1; 1019 otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask; 1020 otp_ignore = (OTPCFG_IGNORE[dw_offset] >> bit_offset) & mask; 1021 1022 if (conf_info[i].value == OTP_REG_VALID_BIT) { 1023 if (((otp_value + otp_ignore) & mask) != mask) { 1024 fail = 1; 1025 mask_err = 1; 1026 } 1027 } else { 1028 if (otp_ignore == mask) { 1029 continue; 1030 } else if (otp_ignore != 0) { 1031 fail = 1; 1032 mask_err = 1; 1033 } 1034 } 1035 1036 if (otp_value != conf_info[i].value && 1037 conf_info[i].value != OTP_REG_RESERVED && 1038 conf_info[i].value != OTP_REG_VALUE && 1039 conf_info[i].value != OTP_REG_VALID_BIT) 1040 continue; 1041 printf("0x%-4X", dw_offset); 1042 1043 if (conf_info[i].length == 1) { 1044 printf("0x%-9X", conf_info[i].bit_offset); 1045 } else { 1046 printf("0x%-2X:0x%-4X", 1047 conf_info[i].bit_offset + conf_info[i].length - 1, 1048 conf_info[i].bit_offset); 1049 } 1050 printf("0x%-10x", otp_value); 1051 1052 if (mask_err) { 1053 printf("Ignore, mask error\n"); 1054 continue; 1055 } 1056 if (conf_info[i].value == OTP_REG_RESERVED) { 1057 printf("Reserved\n"); 1058 } else if (conf_info[i].value == OTP_REG_VALUE) { 1059 printf(conf_info[i].information, otp_value); 1060 printf("\n"); 1061 } else if (conf_info[i].value == OTP_REG_VALID_BIT) { 1062 if (otp_value != 0) { 1063 for (j = 0; j < 7; j++) { 1064 if (otp_value & (1 << j)) 1065 valid_bit[j * 2] = '1'; 1066 else 1067 valid_bit[j * 2] = '0'; 1068 valid_bit[j * 2 + 1] = ' '; 1069 } 1070 valid_bit[15] = 0; 1071 } else { 1072 strcpy(valid_bit, "0 0 0 0 0 0 0 0\0"); 1073 } 1074 printf(conf_info[i].information, valid_bit); 1075 printf("\n"); 1076 } else { 1077 printf("%s\n", conf_info[i].information); 1078 } 1079 } 1080 1081 if (OTPCFG[0xa] != 0 || OTPCFG[0xb] != 0) { 1082 if (OTPCFG_IGNORE[0xa] != 0 && OTPCFG_IGNORE[0xb] != 0) { 1083 printf("OTP revision ID is invalid.\n"); 1084 fail = 1; 1085 } else { 1086 fz = 0; 1087 for (i = 0; i < 64; i++) { 1088 if (get_dw_bit(&OTPCFG[0xa], i) == 0) { 1089 if (!fz) 1090 fz = 1; 1091 } else { 1092 rid_num++; 1093 if (fz) { 1094 printf("OTP revision ID is invalid.\n"); 1095 fail = 1; 1096 break; 1097 } 1098 } 1099 } 1100 } 1101 if (fail) 1102 printf("OTP revision ID\n"); 1103 else 1104 printf("OTP revision ID: 0x%x\n", rid_num); 1105 otp_print_revid(&OTPCFG[0xa]); 1106 } 1107 1108 if (fail) 1109 return OTP_FAILURE; 1110 1111 return OTP_SUCCESS; 1112 } 1113 1114 static int otp_print_conf_info(int input_offset) 1115 { 1116 const struct otpconf_info *conf_info = info_cb.conf_info; 1117 u32 OTPCFG[16]; 1118 u32 mask; 1119 u32 dw_offset; 1120 u32 bit_offset; 1121 u32 otp_value; 1122 char valid_bit[20]; 1123 int i; 1124 int j; 1125 1126 otp_soak(0); 1127 for (i = 0; i < 16; i++) 1128 otp_read_conf(i, &OTPCFG[i]); 1129 1130 printf("DW BIT Value Description\n"); 1131 printf("__________________________________________________________________________\n"); 1132 for (i = 0; i < info_cb.conf_info_len; i++) { 1133 if (input_offset != -1 && input_offset != conf_info[i].dw_offset) 1134 continue; 1135 dw_offset = conf_info[i].dw_offset; 1136 bit_offset = conf_info[i].bit_offset; 1137 mask = BIT(conf_info[i].length) - 1; 1138 otp_value = (OTPCFG[dw_offset] >> bit_offset) & mask; 1139 1140 if (otp_value != conf_info[i].value && 1141 conf_info[i].value != OTP_REG_RESERVED && 1142 conf_info[i].value != OTP_REG_VALUE && 1143 conf_info[i].value != OTP_REG_VALID_BIT) 1144 continue; 1145 printf("0x%-4X", dw_offset); 1146 1147 if (conf_info[i].length == 1) { 1148 printf("0x%-9X", conf_info[i].bit_offset); 1149 } else { 1150 printf("0x%-2X:0x%-4X", 1151 conf_info[i].bit_offset + conf_info[i].length - 1, 1152 conf_info[i].bit_offset); 1153 } 1154 printf("0x%-10x", otp_value); 1155 1156 if (conf_info[i].value == OTP_REG_RESERVED) { 1157 printf("Reserved\n"); 1158 } else if (conf_info[i].value == OTP_REG_VALUE) { 1159 printf(conf_info[i].information, otp_value); 1160 printf("\n"); 1161 } else if (conf_info[i].value == OTP_REG_VALID_BIT) { 1162 if (otp_value != 0) { 1163 for (j = 0; j < 7; j++) { 1164 if (otp_value & (1 << j)) 1165 valid_bit[j * 2] = '1'; 1166 else 1167 valid_bit[j * 2] = '0'; 1168 valid_bit[j * 2 + 1] = ' '; 1169 } 1170 valid_bit[15] = 0; 1171 } else { 1172 strcpy(valid_bit, "0 0 0 0 0 0 0 0\0"); 1173 } 1174 printf(conf_info[i].information, valid_bit); 1175 printf("\n"); 1176 } else { 1177 printf("%s\n", conf_info[i].information); 1178 } 1179 } 1180 return OTP_SUCCESS; 1181 } 1182 1183 static int otp_print_strap_image(struct otp_image_layout *image_layout) 1184 { 1185 const struct otpstrap_info *strap_info = info_cb.strap_info; 1186 u32 *OTPSTRAP; 1187 u32 *OTPSTRAP_PRO; 1188 u32 *OTPSTRAP_IGNORE; 1189 int i; 1190 int fail = 0; 1191 u32 bit_offset; 1192 u32 dw_offset; 1193 u32 mask; 1194 u32 otp_value; 1195 u32 otp_protect; 1196 u32 otp_ignore; 1197 1198 OTPSTRAP = (u32 *)image_layout->strap; 1199 OTPSTRAP_PRO = (u32 *)image_layout->strap_pro; 1200 OTPSTRAP_IGNORE = (u32 *)image_layout->strap_ignore; 1201 1202 printf("BIT(hex) Value Protect Description\n"); 1203 printf("__________________________________________________________________________________________\n"); 1204 1205 for (i = 0; i < info_cb.strap_info_len; i++) { 1206 fail = 0; 1207 if (strap_info[i].bit_offset > 31) { 1208 dw_offset = 1; 1209 bit_offset = strap_info[i].bit_offset - 32; 1210 } else { 1211 dw_offset = 0; 1212 bit_offset = strap_info[i].bit_offset; 1213 } 1214 1215 mask = BIT(strap_info[i].length) - 1; 1216 otp_value = (OTPSTRAP[dw_offset] >> bit_offset) & mask; 1217 otp_protect = (OTPSTRAP_PRO[dw_offset] >> bit_offset) & mask; 1218 otp_ignore = (OTPSTRAP_IGNORE[dw_offset] >> bit_offset) & mask; 1219 1220 if (otp_ignore == mask) 1221 continue; 1222 else if (otp_ignore != 0) 1223 fail = 1; 1224 1225 if (otp_value != strap_info[i].value && 1226 strap_info[i].value != OTP_REG_RESERVED) 1227 continue; 1228 1229 if (strap_info[i].length == 1) { 1230 printf("0x%-9X", strap_info[i].bit_offset); 1231 } else { 1232 printf("0x%-2X:0x%-4X", 1233 strap_info[i].bit_offset + strap_info[i].length - 1, 1234 strap_info[i].bit_offset); 1235 } 1236 printf("0x%-10x", otp_value); 1237 printf("0x%-10x", otp_protect); 1238 1239 if (fail) { 1240 printf("Ignore mask error\n"); 1241 } else { 1242 if (strap_info[i].value != OTP_REG_RESERVED) 1243 printf("%s\n", strap_info[i].information); 1244 else 1245 printf("Reserved\n"); 1246 } 1247 } 1248 1249 if (fail) 1250 return OTP_FAILURE; 1251 1252 return OTP_SUCCESS; 1253 } 1254 1255 static int otp_print_strap_info(int view) 1256 { 1257 const struct otpstrap_info *strap_info = info_cb.strap_info; 1258 struct otpstrap_status strap_status[64]; 1259 int i, j; 1260 int fail = 0; 1261 u32 bit_offset; 1262 u32 length; 1263 u32 otp_value; 1264 u32 otp_protect; 1265 1266 otp_strap_status(strap_status); 1267 1268 if (view) { 1269 printf("BIT(hex) Value Remains Protect Description\n"); 1270 printf("___________________________________________________________________________________________________\n"); 1271 } else { 1272 printf("BIT(hex) Value Description\n"); 1273 printf("________________________________________________________________________________\n"); 1274 } 1275 for (i = 0; i < info_cb.strap_info_len; i++) { 1276 otp_value = 0; 1277 bit_offset = strap_info[i].bit_offset; 1278 length = strap_info[i].length; 1279 for (j = 0; j < length; j++) { 1280 otp_value |= strap_status[bit_offset + j].value << j; 1281 otp_protect |= strap_status[bit_offset + j].protected << j; 1282 } 1283 if (otp_value != strap_info[i].value && 1284 strap_info[i].value != OTP_REG_RESERVED) 1285 continue; 1286 if (view) { 1287 for (j = 0; j < length; j++) { 1288 printf("0x%-7X", strap_info[i].bit_offset + j); 1289 printf("0x%-5X", strap_status[bit_offset + j].value); 1290 printf("%-9d", strap_status[bit_offset + j].remain_times); 1291 printf("0x%-7X", strap_status[bit_offset + j].protected); 1292 if (strap_info[i].value == OTP_REG_RESERVED) { 1293 printf(" Reserved\n"); 1294 continue; 1295 } 1296 if (length == 1) { 1297 printf(" %s\n", strap_info[i].information); 1298 continue; 1299 } 1300 1301 if (j == 0) 1302 printf("/%s\n", strap_info[i].information); 1303 else if (j == length - 1) 1304 printf("\\ \"\n"); 1305 else 1306 printf("| \"\n"); 1307 } 1308 } else { 1309 if (length == 1) { 1310 printf("0x%-9X", strap_info[i].bit_offset); 1311 } else { 1312 printf("0x%-2X:0x%-4X", 1313 bit_offset + length - 1, bit_offset); 1314 } 1315 1316 printf("0x%-10X", otp_value); 1317 1318 if (strap_info[i].value != OTP_REG_RESERVED) 1319 printf("%s\n", strap_info[i].information); 1320 else 1321 printf("Reserved\n"); 1322 } 1323 } 1324 1325 if (fail) 1326 return OTP_FAILURE; 1327 1328 return OTP_SUCCESS; 1329 } 1330 1331 static void _otp_print_key(u32 *data) 1332 { 1333 int i, j; 1334 int key_id, key_offset, last, key_type, key_length, exp_length; 1335 struct otpkey_type key_info; 1336 const struct otpkey_type *key_info_array = info_cb.key_info; 1337 int len = 0; 1338 u8 *byte_buf; 1339 int empty; 1340 1341 byte_buf = (u8 *)data; 1342 1343 empty = 1; 1344 for (i = 0; i < 16; i++) { 1345 if (i % 2) { 1346 if (data[i] != 0xffffffff) 1347 empty = 0; 1348 } else { 1349 if (data[i] != 0) 1350 empty = 0; 1351 } 1352 } 1353 if (empty) { 1354 printf("OTP data header is empty\n"); 1355 return; 1356 } 1357 1358 for (i = 0; i < 16; i++) { 1359 key_id = data[i] & 0x7; 1360 key_offset = data[i] & 0x1ff8; 1361 last = (data[i] >> 13) & 1; 1362 key_type = (data[i] >> 14) & 0xf; 1363 key_length = (data[i] >> 18) & 0x3; 1364 exp_length = (data[i] >> 20) & 0xfff; 1365 1366 key_info.value = -1; 1367 for (j = 0; j < info_cb.key_info_len; j++) { 1368 if (key_type == key_info_array[j].value) { 1369 key_info = key_info_array[j]; 1370 break; 1371 } 1372 } 1373 if (key_info.value == -1) 1374 break; 1375 1376 printf("\nKey[%d]:\n", i); 1377 printf("Key Type: "); 1378 printf("%s\n", key_info.information); 1379 1380 if (key_info.key_type == OTP_KEY_TYPE_HMAC) { 1381 printf("HMAC SHA Type: "); 1382 switch (key_length) { 1383 case 0: 1384 printf("HMAC(SHA224)\n"); 1385 break; 1386 case 1: 1387 printf("HMAC(SHA256)\n"); 1388 break; 1389 case 2: 1390 printf("HMAC(SHA384)\n"); 1391 break; 1392 case 3: 1393 printf("HMAC(SHA512)\n"); 1394 break; 1395 } 1396 } else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV || 1397 key_info.key_type == OTP_KEY_TYPE_RSA_PUB) { 1398 printf("RSA SHA Type: "); 1399 switch (key_length) { 1400 case 0: 1401 printf("RSA1024\n"); 1402 len = 0x100; 1403 break; 1404 case 1: 1405 printf("RSA2048\n"); 1406 len = 0x200; 1407 break; 1408 case 2: 1409 printf("RSA3072\n"); 1410 len = 0x300; 1411 break; 1412 case 3: 1413 printf("RSA4096\n"); 1414 len = 0x400; 1415 break; 1416 } 1417 printf("RSA exponent bit length: %d\n", exp_length); 1418 } 1419 if (key_info.need_id) 1420 printf("Key Number ID: %d\n", key_id); 1421 printf("Key Value:\n"); 1422 if (key_info.key_type == OTP_KEY_TYPE_HMAC) { 1423 buf_print(&byte_buf[key_offset], 0x40); 1424 } else if (key_info.key_type == OTP_KEY_TYPE_AES) { 1425 printf("AES Key:\n"); 1426 buf_print(&byte_buf[key_offset], 0x20); 1427 if (info_cb.version == OTP_A0) { 1428 printf("AES IV:\n"); 1429 buf_print(&byte_buf[key_offset + 0x20], 0x10); 1430 } 1431 1432 } else if (key_info.key_type == OTP_KEY_TYPE_VAULT) { 1433 if (info_cb.version == OTP_A0) { 1434 printf("AES Key:\n"); 1435 buf_print(&byte_buf[key_offset], 0x20); 1436 printf("AES IV:\n"); 1437 buf_print(&byte_buf[key_offset + 0x20], 0x10); 1438 } else { 1439 printf("AES Key 1:\n"); 1440 buf_print(&byte_buf[key_offset], 0x20); 1441 printf("AES Key 2:\n"); 1442 buf_print(&byte_buf[key_offset + 0x20], 0x20); 1443 } 1444 } else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV) { 1445 printf("RSA mod:\n"); 1446 buf_print(&byte_buf[key_offset], len / 2); 1447 printf("RSA exp:\n"); 1448 buf_print(&byte_buf[key_offset + (len / 2)], len / 2); 1449 } else if (key_info.key_type == OTP_KEY_TYPE_RSA_PUB) { 1450 printf("RSA mod:\n"); 1451 buf_print(&byte_buf[key_offset], len / 2); 1452 printf("RSA exp:\n"); 1453 buf_print((u8 *)"\x01\x00\x01", 3); 1454 } 1455 if (last) 1456 break; 1457 } 1458 } 1459 1460 static int otp_print_data_image(struct otp_image_layout *image_layout) 1461 { 1462 u32 *buf; 1463 1464 buf = (u32 *)image_layout->data; 1465 _otp_print_key(buf); 1466 1467 return OTP_SUCCESS; 1468 } 1469 1470 static void otp_print_key_info(void) 1471 { 1472 u32 data[2048]; 1473 int i; 1474 1475 for (i = 0; i < 2048 ; i += 2) 1476 otp_read_data(i, &data[i]); 1477 1478 _otp_print_key(data); 1479 } 1480 1481 static int otp_prog_data(struct otp_image_layout *image_layout, u32 *data) 1482 { 1483 int i; 1484 int ret; 1485 u32 *buf; 1486 u32 *buf_ignore; 1487 1488 buf = (u32 *)image_layout->data; 1489 buf_ignore = (u32 *)image_layout->data_ignore; 1490 printf("Start Programing...\n"); 1491 1492 // programing ecc region first 1493 for (i = 1792; i < 2046; i += 2) { 1494 ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i); 1495 if (ret != OTP_SUCCESS) { 1496 printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n", 1497 i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]); 1498 return ret; 1499 } 1500 } 1501 1502 for (i = 0; i < 1792; i += 2) { 1503 ret = otp_prog_verify_2dw(&data[i], &buf[i], &buf_ignore[i], i); 1504 if (ret != OTP_SUCCESS) { 1505 printf("address: %08x, data: %08x %08x, buffer: %08x %08x, mask: %08x %08x\n", 1506 i, data[i], data[i + 1], buf[i], buf[i + 1], buf_ignore[i], buf_ignore[i + 1]); 1507 return ret; 1508 } 1509 } 1510 otp_soak(0); 1511 return OTP_SUCCESS; 1512 } 1513 1514 static int otp_prog_strap(struct otp_image_layout *image_layout, struct otpstrap_status *otpstrap) 1515 { 1516 u32 *strap; 1517 u32 *strap_ignore; 1518 u32 *strap_pro; 1519 u32 prog_address; 1520 int i; 1521 int bit, pbit, ibit, offset; 1522 int fail = 0; 1523 int ret; 1524 int prog_flag = 0; 1525 1526 strap = (u32 *)image_layout->strap; 1527 strap_pro = (u32 *)image_layout->strap_pro; 1528 strap_ignore = (u32 *)image_layout->strap_ignore; 1529 1530 for (i = 0; i < 64; i++) { 1531 prog_address = 0x800; 1532 if (i < 32) { 1533 offset = i; 1534 bit = (strap[0] >> offset) & 0x1; 1535 ibit = (strap_ignore[0] >> offset) & 0x1; 1536 pbit = (strap_pro[0] >> offset) & 0x1; 1537 prog_address |= ((otpstrap[i].writeable_option * 2 + 16) / 8) * 0x200; 1538 prog_address |= ((otpstrap[i].writeable_option * 2 + 16) % 8) * 0x2; 1539 1540 } else { 1541 offset = (i - 32); 1542 bit = (strap[1] >> offset) & 0x1; 1543 ibit = (strap_ignore[1] >> offset) & 0x1; 1544 pbit = (strap_pro[1] >> offset) & 0x1; 1545 prog_address |= ((otpstrap[i].writeable_option * 2 + 17) / 8) * 0x200; 1546 prog_address |= ((otpstrap[i].writeable_option * 2 + 17) % 8) * 0x2; 1547 } 1548 1549 if (ibit == 1) 1550 continue; 1551 if (bit == otpstrap[i].value) 1552 prog_flag = 0; 1553 else 1554 prog_flag = 1; 1555 1556 if (otpstrap[i].protected == 1 && prog_flag) { 1557 fail = 1; 1558 continue; 1559 } 1560 if (otpstrap[i].remain_times == 0 && prog_flag) { 1561 fail = 1; 1562 continue; 1563 } 1564 1565 if (prog_flag) { 1566 ret = otp_prog_dc_b(1, prog_address, offset); 1567 if (ret) 1568 return OTP_FAILURE; 1569 } 1570 1571 if (pbit != 0) { 1572 prog_address = 0x800; 1573 if (i < 32) 1574 prog_address |= 0x60c; 1575 else 1576 prog_address |= 0x60e; 1577 1578 ret = otp_prog_dc_b(1, prog_address, offset); 1579 if (ret) 1580 return OTP_FAILURE; 1581 } 1582 } 1583 otp_soak(0); 1584 if (fail == 1) 1585 return OTP_FAILURE; 1586 return OTP_SUCCESS; 1587 } 1588 1589 static int otp_prog_conf(struct otp_image_layout *image_layout, u32 *otp_conf) 1590 { 1591 int i, k; 1592 int pass = 0; 1593 u32 prog_address; 1594 u32 compare[2]; 1595 u32 *conf = (u32 *)image_layout->conf; 1596 u32 *conf_ignore = (u32 *)image_layout->conf_ignore; 1597 u32 data_masked; 1598 u32 buf_masked; 1599 int ret; 1600 1601 printf("Start Programing...\n"); 1602 otp_soak(0); 1603 for (i = 0; i < 16; i++) { 1604 data_masked = otp_conf[i] & ~conf_ignore[i]; 1605 buf_masked = conf[i] & ~conf_ignore[i]; 1606 prog_address = 0x800; 1607 prog_address |= (i / 8) * 0x200; 1608 prog_address |= (i % 8) * 0x2; 1609 if (data_masked == buf_masked) { 1610 pass = 1; 1611 continue; 1612 } 1613 1614 otp_soak(1); 1615 ret = otp_prog_dw(conf[i], conf_ignore[i], prog_address); 1616 if (ret) 1617 return OTP_FAILURE; 1618 1619 pass = 0; 1620 for (k = 0; k < RETRY; k++) { 1621 if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) { 1622 otp_soak(2); 1623 ret = otp_prog_dw(compare[0], conf_ignore[i], prog_address); 1624 if (ret) 1625 return OTP_FAILURE; 1626 if (verify_dw(prog_address, &conf[i], &conf_ignore[i], compare, 1) != 0) { 1627 otp_soak(1); 1628 } else { 1629 pass = 1; 1630 break; 1631 } 1632 } else { 1633 pass = 1; 1634 break; 1635 } 1636 } 1637 if (pass == 0) { 1638 printf("address: %08x, otp_conf: %08x, input_conf: %08x, mask: %08x\n", 1639 i, otp_conf[i], conf[i], conf_ignore[i]); 1640 break; 1641 } 1642 } 1643 1644 otp_soak(0); 1645 if (!pass) 1646 return OTP_FAILURE; 1647 1648 return OTP_SUCCESS; 1649 } 1650 1651 static int otp_prog_scu_protect(struct otp_image_layout *image_layout, u32 *scu_pro) 1652 { 1653 int i, k; 1654 int pass = 0; 1655 u32 prog_address; 1656 u32 compare[2]; 1657 u32 *OTPSCU = (u32 *)image_layout->scu_pro; 1658 u32 *OTPSCU_IGNORE = (u32 *)image_layout->scu_pro_ignore; 1659 u32 data_masked; 1660 u32 buf_masked; 1661 int ret; 1662 1663 printf("Start Programing...\n"); 1664 otp_soak(0); 1665 for (i = 0; i < 2; i++) { 1666 data_masked = scu_pro[i] & ~OTPSCU_IGNORE[i]; 1667 buf_masked = OTPSCU[i] & ~OTPSCU_IGNORE[i]; 1668 prog_address = 0xe08 + i * 2; 1669 if (data_masked == buf_masked) { 1670 pass = 1; 1671 continue; 1672 } 1673 1674 otp_soak(1); 1675 ret = otp_prog_dw(OTPSCU[i], OTPSCU_IGNORE[i], prog_address); 1676 if (ret) 1677 return OTP_FAILURE; 1678 pass = 0; 1679 for (k = 0; k < RETRY; k++) { 1680 if (verify_dw(prog_address, &OTPSCU[i], &OTPSCU_IGNORE[i], compare, 1) != 0) { 1681 otp_soak(2); 1682 ret = otp_prog_dw(compare[0], OTPSCU_IGNORE[i], prog_address); 1683 if (ret) 1684 return OTP_FAILURE; 1685 if (verify_dw(prog_address, &OTPSCU[i], &OTPSCU_IGNORE[i], compare, 1) != 0) { 1686 otp_soak(1); 1687 } else { 1688 pass = 1; 1689 break; 1690 } 1691 } else { 1692 pass = 1; 1693 break; 1694 } 1695 } 1696 if (pass == 0) { 1697 printf("OTPCFG0x%x: 0x%08x, input: 0x%08x, mask: 0x%08x\n", 1698 i + 28, scu_pro[i], OTPSCU[i], OTPSCU_IGNORE[i]); 1699 break; 1700 } 1701 } 1702 1703 otp_soak(0); 1704 if (!pass) 1705 return OTP_FAILURE; 1706 1707 return OTP_SUCCESS; 1708 } 1709 1710 static int otp_check_data_image(struct otp_image_layout *image_layout, u32 *data) 1711 { 1712 int data_dw; 1713 u32 data_masked; 1714 u32 buf_masked; 1715 u32 *buf = (u32 *)image_layout->data; 1716 u32 *buf_ignore = (u32 *)image_layout->data_ignore; 1717 int i; 1718 1719 data_dw = image_layout->data_length / 4; 1720 // ignore last two dw, the last two dw is used for slt otp write check. 1721 for (i = 0; i < data_dw - 2; i++) { 1722 data_masked = data[i] & ~buf_ignore[i]; 1723 buf_masked = buf[i] & ~buf_ignore[i]; 1724 if (data_masked == buf_masked) 1725 continue; 1726 if (i % 2 == 0) { 1727 if ((data_masked | buf_masked) == buf_masked) { 1728 continue; 1729 } else { 1730 printf("Input image can't program into OTP, please check.\n"); 1731 printf("OTP_ADDR[0x%x] = 0x%x\n", i, data[i]); 1732 printf("Input [0x%x] = 0x%x\n", i, buf[i]); 1733 printf("Mask [0x%x] = 0x%x\n", i, ~buf_ignore[i]); 1734 return OTP_FAILURE; 1735 } 1736 } else { 1737 if ((data_masked & buf_masked) == buf_masked) { 1738 continue; 1739 } else { 1740 printf("Input image can't program into OTP, please check.\n"); 1741 printf("OTP_ADDR[0x%x] = 0x%x\n", i, data[i]); 1742 printf("Input [0x%x] = 0x%x\n", i, buf[i]); 1743 printf("Mask [0x%x] = 0x%x\n", i, ~buf_ignore[i]); 1744 return OTP_FAILURE; 1745 } 1746 } 1747 } 1748 return OTP_SUCCESS; 1749 } 1750 1751 static int otp_check_strap_image(struct otp_image_layout *image_layout, struct otpstrap_status *otpstrap) 1752 { 1753 int i; 1754 u32 *strap; 1755 u32 *strap_ignore; 1756 u32 *strap_pro; 1757 int bit, pbit, ibit; 1758 int fail = 0; 1759 int ret; 1760 1761 strap = (u32 *)image_layout->strap; 1762 strap_pro = (u32 *)image_layout->strap_pro; 1763 strap_ignore = (u32 *)image_layout->strap_ignore; 1764 1765 for (i = 0; i < 64; i++) { 1766 if (i < 32) { 1767 bit = (strap[0] >> i) & 0x1; 1768 ibit = (strap_ignore[0] >> i) & 0x1; 1769 pbit = (strap_pro[0] >> i) & 0x1; 1770 } else { 1771 bit = (strap[1] >> (i - 32)) & 0x1; 1772 ibit = (strap_ignore[1] >> (i - 32)) & 0x1; 1773 pbit = (strap_pro[1] >> (i - 32)) & 0x1; 1774 } 1775 1776 ret = otp_strap_bit_confirm(&otpstrap[i], i, ibit, bit, pbit); 1777 1778 if (ret == OTP_FAILURE) 1779 fail = 1; 1780 } 1781 if (fail == 1) { 1782 printf("Input image can't program into OTP, please check.\n"); 1783 return OTP_FAILURE; 1784 } 1785 return OTP_SUCCESS; 1786 } 1787 1788 static int otp_check_conf_image(struct otp_image_layout *image_layout, u32 *otp_conf) 1789 { 1790 u32 *conf = (u32 *)image_layout->conf; 1791 u32 *conf_ignore = (u32 *)image_layout->conf_ignore; 1792 u32 data_masked; 1793 u32 buf_masked; 1794 int i; 1795 1796 for (i = 0; i < 16; i++) { 1797 data_masked = otp_conf[i] & ~conf_ignore[i]; 1798 buf_masked = conf[i] & ~conf_ignore[i]; 1799 if (data_masked == buf_masked) 1800 continue; 1801 if ((data_masked | buf_masked) == buf_masked) { 1802 continue; 1803 } else { 1804 printf("Input image can't program into OTP, please check.\n"); 1805 printf("OTPCFG[%X] = %x\n", i, otp_conf[i]); 1806 printf("Input [%X] = %x\n", i, conf[i]); 1807 printf("Mask [%X] = %x\n", i, ~conf_ignore[i]); 1808 return OTP_FAILURE; 1809 } 1810 } 1811 return OTP_SUCCESS; 1812 } 1813 1814 static int otp_check_scu_image(struct otp_image_layout *image_layout, u32 *scu_pro) 1815 { 1816 u32 *OTPSCU = (u32 *)image_layout->scu_pro; 1817 u32 *OTPSCU_IGNORE = (u32 *)image_layout->scu_pro_ignore; 1818 u32 data_masked; 1819 u32 buf_masked; 1820 int i; 1821 1822 for (i = 0; i < 2; i++) { 1823 data_masked = scu_pro[i] & ~OTPSCU_IGNORE[i]; 1824 buf_masked = OTPSCU[i] & ~OTPSCU_IGNORE[i]; 1825 if (data_masked == buf_masked) 1826 continue; 1827 if ((data_masked | buf_masked) == buf_masked) { 1828 continue; 1829 } else { 1830 printf("Input image can't program into OTP, please check.\n"); 1831 printf("OTPCFG[0x%X] = 0x%X\n", 28 + i, scu_pro[i]); 1832 printf("Input [0x%X] = 0x%X\n", 28 + i, OTPSCU[i]); 1833 printf("Mask [0x%X] = 0x%X\n", 28 + i, ~OTPSCU_IGNORE[i]); 1834 return OTP_FAILURE; 1835 } 1836 } 1837 return OTP_SUCCESS; 1838 } 1839 1840 static int otp_verify_image(u8 *src_buf, u32 length, u8 *digest_buf) 1841 { 1842 sha256_context ctx; 1843 u8 digest_ret[CHECKSUM_LEN]; 1844 1845 sha256_starts(&ctx); 1846 sha256_update(&ctx, src_buf, length); 1847 sha256_finish(&ctx, digest_ret); 1848 1849 if (!memcmp(digest_buf, digest_ret, CHECKSUM_LEN)) 1850 return OTP_SUCCESS; 1851 return OTP_FAILURE; 1852 } 1853 1854 static int otp_prog_image(int addr, int nconfirm) 1855 { 1856 int ret; 1857 int image_soc_ver = 0; 1858 struct otp_header *otp_header; 1859 struct otp_image_layout image_layout; 1860 int image_size; 1861 u8 *buf; 1862 u8 *checksum; 1863 int i; 1864 u32 data[2048]; 1865 u32 conf[16]; 1866 u32 scu_pro[2]; 1867 struct otpstrap_status otpstrap[64]; 1868 1869 otp_header = map_physmem(addr, sizeof(struct otp_header), MAP_WRBACK); 1870 if (!otp_header) { 1871 printf("Failed to map physical memory\n"); 1872 return OTP_FAILURE; 1873 } 1874 1875 image_size = OTP_IMAGE_SIZE(otp_header->image_info); 1876 unmap_physmem(otp_header, MAP_WRBACK); 1877 1878 buf = map_physmem(addr, image_size + CHECKSUM_LEN, MAP_WRBACK); 1879 1880 if (!buf) { 1881 printf("Failed to map physical memory\n"); 1882 return OTP_FAILURE; 1883 } 1884 otp_header = (struct otp_header *)buf; 1885 checksum = buf + otp_header->checksum_offset; 1886 1887 if (strcmp(OTP_MAGIC, (char *)otp_header->otp_magic) != 0) { 1888 printf("Image is invalid\n"); 1889 return OTP_FAILURE; 1890 } 1891 1892 image_layout.data_length = (int)(OTP_REGION_SIZE(otp_header->data_info) / 2); 1893 image_layout.data = buf + OTP_REGION_OFFSET(otp_header->data_info); 1894 image_layout.data_ignore = image_layout.data + image_layout.data_length; 1895 1896 image_layout.conf_length = (int)(OTP_REGION_SIZE(otp_header->config_info) / 2); 1897 image_layout.conf = buf + OTP_REGION_OFFSET(otp_header->config_info); 1898 image_layout.conf_ignore = image_layout.conf + image_layout.conf_length; 1899 1900 image_layout.strap = buf + OTP_REGION_OFFSET(otp_header->strap_info); 1901 image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 3); 1902 image_layout.strap_pro = image_layout.strap + image_layout.strap_length; 1903 image_layout.strap_ignore = image_layout.strap + 2 * image_layout.strap_length; 1904 1905 image_layout.scu_pro = buf + OTP_REGION_OFFSET(otp_header->scu_protect_info); 1906 image_layout.scu_pro_length = (int)(OTP_REGION_SIZE(otp_header->scu_protect_info) / 2); 1907 image_layout.scu_pro_ignore = image_layout.scu_pro + image_layout.scu_pro_length; 1908 1909 if (otp_header->soc_ver == SOC_AST2600A0) { 1910 image_soc_ver = OTP_A0; 1911 } else if (otp_header->soc_ver == SOC_AST2600A1) { 1912 image_soc_ver = OTP_A1; 1913 } else if (otp_header->soc_ver == SOC_AST2600A2) { 1914 image_soc_ver = OTP_A2; 1915 } else if (otp_header->soc_ver == SOC_AST2600A3) { 1916 image_soc_ver = OTP_A3; 1917 } else { 1918 printf("Image SOC Version is not supported\n"); 1919 return OTP_FAILURE; 1920 } 1921 1922 if (image_soc_ver != info_cb.version) { 1923 printf("Image SOC version is not match to HW SOC version\n"); 1924 return OTP_FAILURE; 1925 } 1926 1927 if (OTPTOOL_VERSION_MAJOR(otp_header->otptool_ver) != OTPTOOL_COMPT_VERSION) { 1928 printf("OTP image is not generated by otptool v1.x.x\n"); 1929 return OTP_FAILURE; 1930 } 1931 1932 if (otp_verify_image(buf, image_size, checksum)) { 1933 printf("checksum is invalid\n"); 1934 return OTP_FAILURE; 1935 } 1936 1937 if (info_cb.pro_sts.mem_lock) { 1938 printf("OTP memory is locked\n"); 1939 return OTP_FAILURE; 1940 } 1941 ret = 0; 1942 if (otp_header->image_info & OTP_INC_DATA) { 1943 if (info_cb.pro_sts.pro_data) { 1944 printf("OTP data region is protected\n"); 1945 ret = -1; 1946 } 1947 if (info_cb.pro_sts.pro_sec) { 1948 printf("OTP secure region is protected\n"); 1949 ret = -1; 1950 } 1951 printf("Read OTP Data Region:\n"); 1952 for (i = 0; i < 2048 ; i += 2) 1953 otp_read_data(i, &data[i]); 1954 1955 printf("Check writable...\n"); 1956 if (otp_check_data_image(&image_layout, data) == OTP_FAILURE) 1957 ret = -1; 1958 } 1959 if (otp_header->image_info & OTP_INC_CONFIG) { 1960 if (info_cb.pro_sts.pro_conf) { 1961 printf("OTP config region is protected\n"); 1962 ret = -1; 1963 } 1964 printf("Read OTP Config Region:\n"); 1965 for (i = 0; i < 16 ; i++) 1966 otp_read_conf(i, &conf[i]); 1967 1968 printf("Check writable...\n"); 1969 if (otp_check_conf_image(&image_layout, conf) == OTP_FAILURE) 1970 ret = -1; 1971 } 1972 if (otp_header->image_info & OTP_INC_STRAP) { 1973 if (info_cb.pro_sts.pro_strap) { 1974 printf("OTP strap region is protected\n"); 1975 ret = -1; 1976 } 1977 printf("Read OTP Strap Region:\n"); 1978 otp_strap_status(otpstrap); 1979 1980 printf("Check writable...\n"); 1981 if (otp_check_strap_image(&image_layout, otpstrap) == OTP_FAILURE) 1982 ret = -1; 1983 } 1984 if (otp_header->image_info & OTP_INC_SCU_PRO) { 1985 if (info_cb.pro_sts.pro_strap) { 1986 printf("OTP strap region is protected\n"); 1987 ret = -1; 1988 } 1989 printf("Read SCU Protect Region:\n"); 1990 otp_read_conf(28, &scu_pro[0]); 1991 otp_read_conf(29, &scu_pro[1]); 1992 1993 printf("Check writable...\n"); 1994 if (otp_check_scu_image(&image_layout, scu_pro) == OTP_FAILURE) 1995 ret = -1; 1996 } 1997 if (ret == -1) 1998 return OTP_FAILURE; 1999 2000 if (!nconfirm) { 2001 if (otp_header->image_info & OTP_INC_DATA) { 2002 printf("\nOTP data region :\n"); 2003 if (otp_print_data_image(&image_layout) < 0) { 2004 printf("OTP data error, please check.\n"); 2005 return OTP_FAILURE; 2006 } 2007 } 2008 if (otp_header->image_info & OTP_INC_CONFIG) { 2009 printf("\nOTP configuration region :\n"); 2010 if (otp_print_conf_image(&image_layout) < 0) { 2011 printf("OTP config error, please check.\n"); 2012 return OTP_FAILURE; 2013 } 2014 } 2015 if (otp_header->image_info & OTP_INC_STRAP) { 2016 printf("\nOTP strap region :\n"); 2017 if (otp_print_strap_image(&image_layout) < 0) { 2018 printf("OTP strap error, please check.\n"); 2019 return OTP_FAILURE; 2020 } 2021 } 2022 if (otp_header->image_info & OTP_INC_SCU_PRO) { 2023 printf("\nOTP scu protect region :\n"); 2024 if (otp_print_scu_image(&image_layout) < 0) { 2025 printf("OTP scu protect error, please check.\n"); 2026 return OTP_FAILURE; 2027 } 2028 } 2029 2030 printf("type \"YES\" (no quotes) to continue:\n"); 2031 if (!confirm_yesno()) { 2032 printf(" Aborting\n"); 2033 return OTP_FAILURE; 2034 } 2035 } 2036 2037 if (otp_header->image_info & OTP_INC_DATA) { 2038 printf("programing data region ...\n"); 2039 ret = otp_prog_data(&image_layout, data); 2040 if (ret != 0) { 2041 printf("Error\n"); 2042 return ret; 2043 } 2044 printf("Done\n"); 2045 } 2046 if (otp_header->image_info & OTP_INC_STRAP) { 2047 printf("programing strap region ...\n"); 2048 ret = otp_prog_strap(&image_layout, otpstrap); 2049 if (ret != 0) { 2050 printf("Error\n"); 2051 return ret; 2052 } 2053 printf("Done\n"); 2054 } 2055 if (otp_header->image_info & OTP_INC_SCU_PRO) { 2056 printf("programing scu protect region ...\n"); 2057 ret = otp_prog_scu_protect(&image_layout, scu_pro); 2058 if (ret != 0) { 2059 printf("Error\n"); 2060 return ret; 2061 } 2062 printf("Done\n"); 2063 } 2064 if (otp_header->image_info & OTP_INC_CONFIG) { 2065 printf("programing configuration region ...\n"); 2066 ret = otp_prog_conf(&image_layout, conf); 2067 if (ret != 0) { 2068 printf("Error\n"); 2069 return ret; 2070 } 2071 printf("Done\n"); 2072 } 2073 2074 return OTP_SUCCESS; 2075 } 2076 2077 static int otp_prog_bit(int mode, int otp_dw_offset, int bit_offset, int value, int nconfirm) 2078 { 2079 u32 read[2]; 2080 u32 prog_address = 0; 2081 struct otpstrap_status otpstrap[64]; 2082 int otp_bit; 2083 int ret = 0; 2084 2085 otp_soak(0); 2086 switch (mode) { 2087 case OTP_REGION_CONF: 2088 otp_read_conf(otp_dw_offset, read); 2089 prog_address = 0x800; 2090 prog_address |= (otp_dw_offset / 8) * 0x200; 2091 prog_address |= (otp_dw_offset % 8) * 0x2; 2092 otp_bit = (read[0] >> bit_offset) & 0x1; 2093 if (otp_bit == value) { 2094 printf("OTPCFG0x%X[0x%X] = %d\n", otp_dw_offset, bit_offset, value); 2095 printf("No need to program\n"); 2096 return OTP_SUCCESS; 2097 } 2098 if (otp_bit == 1 && value == 0) { 2099 printf("OTPCFG0x%X[0x%X] = 1\n", otp_dw_offset, bit_offset); 2100 printf("OTP is programmed, which can't be clean\n"); 2101 return OTP_FAILURE; 2102 } 2103 printf("Program OTPCFG0x%X[0x%X] to 1\n", otp_dw_offset, bit_offset); 2104 break; 2105 case OTP_REGION_DATA: 2106 prog_address = otp_dw_offset; 2107 2108 if (otp_dw_offset % 2 == 0) { 2109 otp_read_data(otp_dw_offset, read); 2110 otp_bit = (read[0] >> bit_offset) & 0x1; 2111 2112 if (otp_bit == 1 && value == 0) { 2113 printf("OTPDATA0x%X[0x%X] = 1\n", otp_dw_offset, bit_offset); 2114 printf("OTP is programmed, which can't be cleared\n"); 2115 return OTP_FAILURE; 2116 } 2117 } else { 2118 otp_read_data(otp_dw_offset - 1, read); 2119 otp_bit = (read[1] >> bit_offset) & 0x1; 2120 2121 if (otp_bit == 0 && value == 1) { 2122 printf("OTPDATA0x%X[0x%X] = 1\n", otp_dw_offset, bit_offset); 2123 printf("OTP is programmed, which can't be written\n"); 2124 return OTP_FAILURE; 2125 } 2126 } 2127 if (otp_bit == value) { 2128 printf("OTPDATA0x%X[0x%X] = %d\n", otp_dw_offset, bit_offset, value); 2129 printf("No need to program\n"); 2130 return OTP_SUCCESS; 2131 } 2132 2133 printf("Program OTPDATA0x%X[0x%X] to 1\n", otp_dw_offset, bit_offset); 2134 break; 2135 case OTP_REGION_STRAP: 2136 otp_strap_status(otpstrap); 2137 otp_print_strap(bit_offset, 1); 2138 ret = otp_strap_bit_confirm(&otpstrap[bit_offset], bit_offset, 0, value, 0); 2139 if (ret == OTP_FAILURE) 2140 return OTP_FAILURE; 2141 else if (ret == OTP_PROG_SKIP) 2142 return OTP_SUCCESS; 2143 2144 break; 2145 } 2146 2147 if (!nconfirm) { 2148 printf("type \"YES\" (no quotes) to continue:\n"); 2149 if (!confirm_yesno()) { 2150 printf(" Aborting\n"); 2151 return OTP_FAILURE; 2152 } 2153 } 2154 2155 switch (mode) { 2156 case OTP_REGION_STRAP: 2157 ret = otp_prog_strap_b(bit_offset, value); 2158 break; 2159 case OTP_REGION_CONF: 2160 case OTP_REGION_DATA: 2161 ret = otp_prog_dc_b(value, prog_address, bit_offset); 2162 break; 2163 } 2164 otp_soak(0); 2165 if (ret) { 2166 printf("OTP cannot be programmed\n"); 2167 printf("FAILURE\n"); 2168 return OTP_FAILURE; 2169 } 2170 2171 printf("SUCCESS\n"); 2172 return OTP_SUCCESS; 2173 } 2174 2175 static int otp_update_rid(u32 update_num, int force) 2176 { 2177 u32 otp_rid[2]; 2178 u32 sw_rid[2]; 2179 int rid_num = 0; 2180 int sw_rid_num = 0; 2181 int bit_offset; 2182 int dw_offset; 2183 int i; 2184 int ret; 2185 2186 otp_read_conf(10, &otp_rid[0]); 2187 otp_read_conf(11, &otp_rid[1]); 2188 2189 sw_rid[0] = readl(SW_REV_ID0); 2190 sw_rid[1] = readl(SW_REV_ID1); 2191 2192 rid_num = get_rid_num(otp_rid); 2193 sw_rid_num = get_rid_num(sw_rid); 2194 2195 if (sw_rid_num < 0) { 2196 printf("SW revision id is invalid, please check.\n"); 2197 return OTP_FAILURE; 2198 } 2199 2200 if (update_num > sw_rid_num) { 2201 printf("current SW revision ID: 0x%x\n", sw_rid_num); 2202 printf("update number could not bigger than current SW revision id\n"); 2203 return OTP_FAILURE; 2204 } 2205 2206 if (rid_num < 0) { 2207 printf("Current OTP revision ID cannot handle by this command,\n" 2208 "please use 'otp pb' command to update it manually\n"); 2209 otp_print_revid(otp_rid); 2210 return OTP_FAILURE; 2211 } 2212 2213 printf("current OTP revision ID: 0x%x\n", rid_num); 2214 otp_print_revid(otp_rid); 2215 printf("input update number: 0x%x\n", update_num); 2216 2217 if (rid_num > update_num) { 2218 printf("OTP rev_id is bigger than 0x%X\n", update_num); 2219 printf("Skip\n"); 2220 return OTP_FAILURE; 2221 } else if (rid_num == update_num) { 2222 printf("OTP rev_id is same as input\n"); 2223 printf("Skip\n"); 2224 return OTP_FAILURE; 2225 } 2226 2227 for (i = rid_num; i < update_num; i++) { 2228 if (i < 32) { 2229 dw_offset = 0xa; 2230 bit_offset = i; 2231 } else { 2232 dw_offset = 0xb; 2233 bit_offset = i - 32; 2234 } 2235 printf("OTPCFG0x%X[0x%X]", dw_offset, bit_offset); 2236 if (i + 1 != update_num) 2237 printf(", "); 2238 } 2239 2240 printf(" will be programmed\n"); 2241 if (force == 0) { 2242 printf("type \"YES\" (no quotes) to continue:\n"); 2243 if (!confirm_yesno()) { 2244 printf(" Aborting\n"); 2245 return OTP_FAILURE; 2246 } 2247 } 2248 2249 ret = 0; 2250 for (i = rid_num; i < update_num; i++) { 2251 if (i < 32) { 2252 dw_offset = 0xa04; 2253 bit_offset = i; 2254 } else { 2255 dw_offset = 0xa06; 2256 bit_offset = i - 32; 2257 } 2258 if (otp_prog_dc_b(1, dw_offset, bit_offset)) { 2259 printf("OTPCFG0x%X[0x%X] programming failed\n", dw_offset, bit_offset); 2260 ret = OTP_FAILURE; 2261 break; 2262 } 2263 } 2264 otp_soak(0); 2265 otp_read_conf(10, &otp_rid[0]); 2266 otp_read_conf(11, &otp_rid[1]); 2267 rid_num = get_rid_num(otp_rid); 2268 if (rid_num >= 0) 2269 printf("OTP revision ID: 0x%x\n", rid_num); 2270 else 2271 printf("OTP revision ID\n"); 2272 otp_print_revid(otp_rid); 2273 if (!ret) 2274 printf("SUCCESS\n"); 2275 else 2276 printf("FAILED\n"); 2277 return ret; 2278 } 2279 2280 static int otp_retire_key(u32 retire_id, int force) 2281 { 2282 u32 otpcfg4; 2283 u32 krb; 2284 u32 krb_b; 2285 u32 krb_or; 2286 u32 current_id; 2287 2288 otp_read_conf(4, &otpcfg4); 2289 current_id = readl(SEC_KEY_NUM) & 7; 2290 krb = otpcfg4 & 0xff; 2291 krb_b = (otpcfg4 >> 16) & 0xff; 2292 krb_or = krb | krb_b; 2293 2294 printf("current Key ID: 0x%x\n", current_id); 2295 printf("input retire ID: 0x%x\n", retire_id); 2296 printf("OTPCFG0x4 = 0x%X\n", otpcfg4); 2297 2298 if (info_cb.pro_sts.pro_key_ret) { 2299 printf("OTPCFG4 is protected\n"); 2300 return OTP_FAILURE; 2301 } 2302 2303 if (retire_id >= current_id) { 2304 printf("Retire key id is equal or bigger than current boot key\n"); 2305 return OTP_FAILURE; 2306 } 2307 2308 if (krb_or & (1 << retire_id)) { 2309 printf("Key 0x%X already retired\n", retire_id); 2310 return OTP_SUCCESS; 2311 } 2312 2313 printf("OTPCFG0x4[0x%X] will be programmed\n", retire_id); 2314 if (force == 0) { 2315 printf("type \"YES\" (no quotes) to continue:\n"); 2316 if (!confirm_yesno()) { 2317 printf(" Aborting\n"); 2318 return OTP_FAILURE; 2319 } 2320 } 2321 2322 if (otp_prog_dc_b(1, 0x808, retire_id) == OTP_FAILURE) { 2323 printf("OTPCFG0x4[0x%X] programming failed\n", retire_id); 2324 printf("try to program backup OTPCFG0x4[0x%X]\n", retire_id + 16); 2325 if (otp_prog_dc_b(1, 0x808, retire_id + 16) == OTP_FAILURE) 2326 printf("OTPCFG0x4[0x%X] programming failed", retire_id + 16); 2327 } 2328 2329 otp_soak(0); 2330 otp_read_conf(4, &otpcfg4); 2331 krb = otpcfg4 & 0xff; 2332 krb_b = (otpcfg4 >> 16) & 0xff; 2333 krb_or = krb | krb_b; 2334 if (krb_or & (1 << retire_id)) { 2335 printf("SUCCESS\n"); 2336 return OTP_SUCCESS; 2337 } 2338 printf("FAILED\n"); 2339 return OTP_FAILURE; 2340 } 2341 2342 static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2343 { 2344 u32 offset, count; 2345 int ret; 2346 2347 if (argc == 4) { 2348 offset = simple_strtoul(argv[2], NULL, 16); 2349 count = simple_strtoul(argv[3], NULL, 16); 2350 } else if (argc == 3) { 2351 offset = simple_strtoul(argv[2], NULL, 16); 2352 count = 1; 2353 } else { 2354 return CMD_RET_USAGE; 2355 } 2356 2357 if (!strcmp(argv[1], "conf")) 2358 ret = otp_print_conf(offset, count); 2359 else if (!strcmp(argv[1], "data")) 2360 ret = otp_print_data(offset, count); 2361 else if (!strcmp(argv[1], "strap")) 2362 ret = otp_print_strap(offset, count); 2363 else 2364 return CMD_RET_USAGE; 2365 2366 if (ret == OTP_SUCCESS) 2367 return CMD_RET_SUCCESS; 2368 return CMD_RET_USAGE; 2369 } 2370 2371 static int do_otpprog(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2372 { 2373 phys_addr_t addr; 2374 int ret; 2375 2376 if (argc == 3) { 2377 if (strcmp(argv[1], "o")) 2378 return CMD_RET_USAGE; 2379 addr = simple_strtoul(argv[2], NULL, 16); 2380 ret = otp_prog_image(addr, 1); 2381 } else if (argc == 2) { 2382 addr = simple_strtoul(argv[1], NULL, 16); 2383 ret = otp_prog_image(addr, 0); 2384 } else { 2385 return CMD_RET_USAGE; 2386 } 2387 2388 if (ret == OTP_SUCCESS) 2389 return CMD_RET_SUCCESS; 2390 else if (ret == OTP_FAILURE) 2391 return CMD_RET_FAILURE; 2392 else 2393 return CMD_RET_USAGE; 2394 } 2395 2396 static int do_otppb(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2397 { 2398 int mode = 0; 2399 int nconfirm = 0; 2400 int otp_addr = 0; 2401 int bit_offset; 2402 int value; 2403 int ret; 2404 u32 otp_strap_pro; 2405 2406 if (argc != 4 && argc != 5 && argc != 6) 2407 return CMD_RET_USAGE; 2408 2409 /* Drop the pb cmd */ 2410 argc--; 2411 argv++; 2412 2413 if (!strcmp(argv[0], "conf")) 2414 mode = OTP_REGION_CONF; 2415 else if (!strcmp(argv[0], "strap")) 2416 mode = OTP_REGION_STRAP; 2417 else if (!strcmp(argv[0], "data")) 2418 mode = OTP_REGION_DATA; 2419 else 2420 return CMD_RET_USAGE; 2421 2422 /* Drop the region cmd */ 2423 argc--; 2424 argv++; 2425 2426 if (!strcmp(argv[0], "o")) { 2427 nconfirm = 1; 2428 /* Drop the force option */ 2429 argc--; 2430 argv++; 2431 } 2432 2433 if (mode == OTP_REGION_STRAP) { 2434 bit_offset = simple_strtoul(argv[0], NULL, 16); 2435 value = simple_strtoul(argv[1], NULL, 16); 2436 if (bit_offset >= 64 || (value != 0 && value != 1)) 2437 return CMD_RET_USAGE; 2438 } else { 2439 otp_addr = simple_strtoul(argv[0], NULL, 16); 2440 bit_offset = simple_strtoul(argv[1], NULL, 16); 2441 value = simple_strtoul(argv[2], NULL, 16); 2442 if (bit_offset >= 32 || (value != 0 && value != 1)) 2443 return CMD_RET_USAGE; 2444 if (mode == OTP_REGION_DATA) { 2445 if (otp_addr >= 0x800) 2446 return CMD_RET_USAGE; 2447 } else { 2448 if (otp_addr >= 0x20) 2449 return CMD_RET_USAGE; 2450 } 2451 } 2452 if (value != 0 && value != 1) 2453 return CMD_RET_USAGE; 2454 2455 ret = 0; 2456 if (info_cb.pro_sts.mem_lock) { 2457 printf("OTP memory is locked\n"); 2458 return CMD_RET_FAILURE; 2459 } 2460 if (mode == OTP_REGION_DATA) { 2461 if (info_cb.pro_sts.sec_size == 0) { 2462 if (info_cb.pro_sts.pro_data) { 2463 printf("OTP data region is protected\n"); 2464 ret = -1; 2465 } 2466 } else if (otp_addr < info_cb.pro_sts.sec_size && otp_addr >= 16) { 2467 printf("OTP secure region is not readable, skip it to prevent unpredictable result\n"); 2468 ret = -1; 2469 } else if (otp_addr < info_cb.pro_sts.sec_size) { 2470 // header region(0x0~0x40) is still readable even secure region is set. 2471 if (info_cb.pro_sts.pro_sec) { 2472 printf("OTP secure region is protected\n"); 2473 ret = -1; 2474 } 2475 } else if (info_cb.pro_sts.pro_data) { 2476 printf("OTP data region is protected\n"); 2477 ret = -1; 2478 } 2479 } else if (mode == OTP_REGION_CONF) { 2480 if (otp_addr != 4 && otp_addr != 10 && otp_addr != 11 && otp_addr < 16) { 2481 if (info_cb.pro_sts.pro_conf) { 2482 printf("OTP config region is protected\n"); 2483 ret = -1; 2484 } 2485 } else if (otp_addr == 10 || otp_addr == 11) { 2486 u32 otp_rid[2]; 2487 u32 sw_rid[2]; 2488 u64 *otp_rid64 = (u64 *)otp_rid; 2489 u64 *sw_rid64 = (u64 *)sw_rid; 2490 2491 otp_read_conf(10, &otp_rid[0]); 2492 otp_read_conf(11, &otp_rid[1]); 2493 sw_rid[0] = readl(SW_REV_ID0); 2494 sw_rid[1] = readl(SW_REV_ID1); 2495 2496 if (otp_addr == 10) 2497 otp_rid[0] |= 1 << bit_offset; 2498 else 2499 otp_rid[1] |= 1 << bit_offset; 2500 2501 if (*otp_rid64 > *sw_rid64) { 2502 printf("update number could not bigger than current SW revision id\n"); 2503 ret = -1; 2504 } 2505 } else if (otp_addr == 4) { 2506 if (info_cb.pro_sts.pro_key_ret) { 2507 printf("OTPCFG4 is protected\n"); 2508 ret = -1; 2509 } else { 2510 if ((bit_offset >= 0 && bit_offset <= 7) || 2511 (bit_offset >= 16 && bit_offset <= 23)) { 2512 u32 key_num; 2513 u32 retire; 2514 2515 key_num = readl(SEC_KEY_NUM) & 3; 2516 if (bit_offset >= 16) 2517 retire = bit_offset - 16; 2518 else 2519 retire = bit_offset; 2520 if (retire >= key_num) { 2521 printf("Retire key id is equal or bigger than current boot key\n"); 2522 ret = -1; 2523 } 2524 } 2525 } 2526 } else if (otp_addr >= 16 && otp_addr <= 31) { 2527 if (info_cb.pro_sts.pro_strap) { 2528 printf("OTP strap region is protected\n"); 2529 ret = -1; 2530 } else if ((otp_addr < 30 && info_cb.version == OTP_A0) || 2531 (otp_addr < 28 && info_cb.version != OTP_A0)) { 2532 if (otp_addr % 2 == 0) 2533 otp_read_conf(30, &otp_strap_pro); 2534 else 2535 otp_read_conf(31, &otp_strap_pro); 2536 if (otp_strap_pro >> bit_offset & 0x1) { 2537 printf("OTPCFG0x%X[0x%X] is protected\n", otp_addr, bit_offset); 2538 ret = -1; 2539 } 2540 } 2541 } 2542 } else if (mode == OTP_REGION_STRAP) { 2543 // per bit protection will check in otp_strap_bit_confirm 2544 if (info_cb.pro_sts.pro_strap) { 2545 printf("OTP strap region is protected\n"); 2546 ret = -1; 2547 } 2548 } 2549 2550 if (ret == -1) 2551 return CMD_RET_FAILURE; 2552 2553 ret = otp_prog_bit(mode, otp_addr, bit_offset, value, nconfirm); 2554 2555 if (ret == OTP_SUCCESS) 2556 return CMD_RET_SUCCESS; 2557 else if (ret == OTP_FAILURE) 2558 return CMD_RET_FAILURE; 2559 else 2560 return CMD_RET_USAGE; 2561 } 2562 2563 static int do_otpcmp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2564 { 2565 phys_addr_t addr; 2566 int otp_addr = 0; 2567 int ret; 2568 2569 if (argc != 3) 2570 return CMD_RET_USAGE; 2571 2572 addr = simple_strtoul(argv[1], NULL, 16); 2573 otp_addr = simple_strtoul(argv[2], NULL, 16); 2574 ret = otp_compare(otp_addr, addr); 2575 if (ret == 0) { 2576 printf("Compare pass\n"); 2577 return CMD_RET_SUCCESS; 2578 } 2579 printf("Compare fail\n"); 2580 return CMD_RET_FAILURE; 2581 } 2582 2583 static int do_otpinfo(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2584 { 2585 int view = 0; 2586 int input; 2587 2588 if (argc != 2 && argc != 3) 2589 return CMD_RET_USAGE; 2590 2591 if (!strcmp(argv[1], "conf")) { 2592 if (argc == 3) { 2593 input = simple_strtoul(argv[2], NULL, 16); 2594 otp_print_conf_info(input); 2595 } else { 2596 otp_print_conf_info(-1); 2597 } 2598 } else if (!strcmp(argv[1], "strap")) { 2599 if (!strcmp(argv[2], "v")) { 2600 view = 1; 2601 /* Drop the view option */ 2602 argc--; 2603 argv++; 2604 } 2605 otp_print_strap_info(view); 2606 } else if (!strcmp(argv[1], "scu")) { 2607 otp_print_scu_info(); 2608 } else if (!strcmp(argv[1], "key")) { 2609 otp_print_key_info(); 2610 } else { 2611 return CMD_RET_USAGE; 2612 } 2613 2614 return CMD_RET_SUCCESS; 2615 } 2616 2617 static int do_otpprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2618 { 2619 u32 input; 2620 u32 bit_offset; 2621 u32 prog_address; 2622 char force; 2623 int ret; 2624 2625 if (argc != 3 && argc != 2) 2626 return CMD_RET_USAGE; 2627 2628 if (!strcmp(argv[1], "o")) { 2629 input = simple_strtoul(argv[2], NULL, 16); 2630 force = 1; 2631 } else { 2632 input = simple_strtoul(argv[1], NULL, 16); 2633 force = 0; 2634 } 2635 2636 if (input < 32) { 2637 bit_offset = input; 2638 prog_address = 0xe0c; 2639 } else if (input < 64) { 2640 bit_offset = input - 32; 2641 prog_address = 0xe0e; 2642 } else { 2643 return CMD_RET_USAGE; 2644 } 2645 2646 if (info_cb.pro_sts.pro_strap) { 2647 printf("OTP strap region is protected\n"); 2648 return CMD_RET_FAILURE; 2649 } 2650 2651 if (!force) { 2652 printf("OTPSTRAP[0x%X] will be protected\n", input); 2653 printf("type \"YES\" (no quotes) to continue:\n"); 2654 if (!confirm_yesno()) { 2655 printf(" Aborting\n"); 2656 return CMD_RET_FAILURE; 2657 } 2658 } 2659 2660 if (verify_bit(prog_address, bit_offset, 1) == 0) { 2661 printf("OTPSTRAP[0x%X] already protected\n", input); 2662 return CMD_RET_SUCCESS; 2663 } 2664 2665 ret = otp_prog_dc_b(1, prog_address, bit_offset); 2666 otp_soak(0); 2667 2668 if (ret) { 2669 printf("Protect OTPSTRAP[0x%X] fail\n", input); 2670 return CMD_RET_FAILURE; 2671 } 2672 2673 printf("OTPSTRAP[0x%X] is protected\n", input); 2674 return CMD_RET_SUCCESS; 2675 } 2676 2677 static int do_otp_scuprotect(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2678 { 2679 u32 scu_offset; 2680 u32 bit_offset; 2681 u32 conf_offset; 2682 u32 prog_address; 2683 char force; 2684 int ret; 2685 2686 if (argc != 4 && argc != 3) 2687 return CMD_RET_USAGE; 2688 2689 if (!strcmp(argv[1], "o")) { 2690 scu_offset = simple_strtoul(argv[2], NULL, 16); 2691 bit_offset = simple_strtoul(argv[3], NULL, 16); 2692 force = 1; 2693 } else { 2694 scu_offset = simple_strtoul(argv[1], NULL, 16); 2695 bit_offset = simple_strtoul(argv[2], NULL, 16); 2696 force = 0; 2697 } 2698 if (scu_offset == 0x500) { 2699 prog_address = 0xe08; 2700 conf_offset = 28; 2701 } else if (scu_offset == 0x510) { 2702 prog_address = 0xe0a; 2703 conf_offset = 29; 2704 } else { 2705 return CMD_RET_USAGE; 2706 } 2707 if (bit_offset < 0 || bit_offset > 31) 2708 return CMD_RET_USAGE; 2709 if (info_cb.pro_sts.pro_strap) { 2710 printf("OTP strap region is protected\n"); 2711 return CMD_RET_FAILURE; 2712 } 2713 if (!force) { 2714 printf("OTPCONF0x%X[0x%X] will be programmed\n", conf_offset, bit_offset); 2715 printf("SCU0x%X[0x%X] will be protected\n", scu_offset, bit_offset); 2716 printf("type \"YES\" (no quotes) to continue:\n"); 2717 if (!confirm_yesno()) { 2718 printf(" Aborting\n"); 2719 return CMD_RET_FAILURE; 2720 } 2721 } 2722 2723 if (verify_bit(prog_address, bit_offset, 1) == 0) { 2724 printf("OTPCONF0x%X[0x%X] already programmed\n", conf_offset, bit_offset); 2725 return CMD_RET_SUCCESS; 2726 } 2727 2728 ret = otp_prog_dc_b(1, prog_address, bit_offset); 2729 otp_soak(0); 2730 2731 if (ret) { 2732 printf("Program OTPCONF0x%X[0x%X] fail\n", conf_offset, bit_offset); 2733 return CMD_RET_FAILURE; 2734 } 2735 2736 printf("OTPCONF0x%X[0x%X] programmed success\n", conf_offset, bit_offset); 2737 return CMD_RET_SUCCESS; 2738 } 2739 2740 static int do_otpver(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2741 { 2742 printf("SOC OTP version: %s\n", info_cb.ver_name); 2743 printf("OTP tool version: %s\n", OTP_VER); 2744 printf("OTP info version: %s\n", OTP_INFO_VER); 2745 2746 return CMD_RET_SUCCESS; 2747 } 2748 2749 static int do_otpupdate(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2750 { 2751 u32 update_num; 2752 int force = 0; 2753 int ret; 2754 2755 if (argc == 3) { 2756 if (strcmp(argv[1], "o")) 2757 return CMD_RET_USAGE; 2758 force = 1; 2759 update_num = simple_strtoul(argv[2], NULL, 16); 2760 } else if (argc == 2) { 2761 update_num = simple_strtoul(argv[1], NULL, 16); 2762 } else { 2763 return CMD_RET_USAGE; 2764 } 2765 2766 if (update_num > 64) 2767 return CMD_RET_USAGE; 2768 ret = otp_update_rid(update_num, force); 2769 2770 if (ret) 2771 return CMD_RET_FAILURE; 2772 return CMD_RET_SUCCESS; 2773 } 2774 2775 static int do_otprid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2776 { 2777 u32 otp_rid[2]; 2778 u32 sw_rid[2]; 2779 int rid_num = 0; 2780 int sw_rid_num = 0; 2781 int ret; 2782 2783 if (argc != 1) 2784 return CMD_RET_USAGE; 2785 2786 otp_read_conf(10, &otp_rid[0]); 2787 otp_read_conf(11, &otp_rid[1]); 2788 2789 sw_rid[0] = readl(SW_REV_ID0); 2790 sw_rid[1] = readl(SW_REV_ID1); 2791 2792 rid_num = get_rid_num(otp_rid); 2793 sw_rid_num = get_rid_num(sw_rid); 2794 2795 if (sw_rid_num < 0) { 2796 printf("SW revision id is invalid, please check.\n"); 2797 printf("SEC68:0x%x\n", sw_rid[0]); 2798 printf("SEC6C:0x%x\n", sw_rid[1]); 2799 } else { 2800 printf("current SW revision ID: 0x%x\n", sw_rid_num); 2801 } 2802 if (rid_num >= 0) { 2803 printf("current OTP revision ID: 0x%x\n", rid_num); 2804 ret = CMD_RET_SUCCESS; 2805 } else { 2806 printf("Current OTP revision ID cannot handle by 'otp update',\n" 2807 "please use 'otp pb' command to update it manually\n" 2808 "current OTP revision ID\n"); 2809 ret = CMD_RET_FAILURE; 2810 } 2811 otp_print_revid(otp_rid); 2812 2813 return ret; 2814 } 2815 2816 static int do_otpretire(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2817 { 2818 u32 retire_id; 2819 int force = 0; 2820 int ret; 2821 2822 if (argc == 3) { 2823 if (strcmp(argv[1], "o")) 2824 return CMD_RET_USAGE; 2825 force = 1; 2826 retire_id = simple_strtoul(argv[2], NULL, 16); 2827 } else if (argc == 2) { 2828 retire_id = simple_strtoul(argv[1], NULL, 16); 2829 } else { 2830 return CMD_RET_USAGE; 2831 } 2832 2833 if (retire_id > 7) 2834 return CMD_RET_USAGE; 2835 ret = otp_retire_key(retire_id, force); 2836 2837 if (ret) 2838 return CMD_RET_FAILURE; 2839 return CMD_RET_SUCCESS; 2840 } 2841 2842 static cmd_tbl_t cmd_otp[] = { 2843 U_BOOT_CMD_MKENT(version, 1, 0, do_otpver, "", ""), 2844 U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""), 2845 U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""), 2846 U_BOOT_CMD_MKENT(prog, 3, 0, do_otpprog, "", ""), 2847 U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""), 2848 U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""), 2849 U_BOOT_CMD_MKENT(scuprotect, 4, 0, do_otp_scuprotect, "", ""), 2850 U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""), 2851 U_BOOT_CMD_MKENT(update, 3, 0, do_otpupdate, "", ""), 2852 U_BOOT_CMD_MKENT(rid, 1, 0, do_otprid, "", ""), 2853 U_BOOT_CMD_MKENT(retire, 3, 0, do_otpretire, "", ""), 2854 }; 2855 2856 static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) 2857 { 2858 struct otp_pro_sts *pro_sts; 2859 cmd_tbl_t *cp; 2860 u32 ver; 2861 int ret; 2862 u32 otp_conf0; 2863 2864 cp = find_cmd_tbl(argv[1], cmd_otp, ARRAY_SIZE(cmd_otp)); 2865 2866 /* Drop the otp command */ 2867 argc--; 2868 argv++; 2869 2870 if (!cp || argc > cp->maxargs) 2871 return CMD_RET_USAGE; 2872 if (flag == CMD_FLAG_REPEAT && !cmd_is_repeatable(cp)) 2873 return CMD_RET_SUCCESS; 2874 2875 ver = chip_version(); 2876 switch (ver) { 2877 case OTP_A0: 2878 info_cb.version = OTP_A0; 2879 info_cb.conf_info = a0_conf_info; 2880 info_cb.conf_info_len = ARRAY_SIZE(a0_conf_info); 2881 info_cb.strap_info = a0_strap_info; 2882 info_cb.strap_info_len = ARRAY_SIZE(a0_strap_info); 2883 info_cb.key_info = a0_key_type; 2884 info_cb.key_info_len = ARRAY_SIZE(a0_key_type); 2885 sprintf(info_cb.ver_name, "A0"); 2886 break; 2887 case OTP_A1: 2888 info_cb.version = OTP_A1; 2889 info_cb.conf_info = a1_conf_info; 2890 info_cb.conf_info_len = ARRAY_SIZE(a1_conf_info); 2891 info_cb.strap_info = a1_strap_info; 2892 info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info); 2893 info_cb.key_info = a1_key_type; 2894 info_cb.key_info_len = ARRAY_SIZE(a1_key_type); 2895 info_cb.scu_info = a1_scu_info; 2896 info_cb.scu_info_len = ARRAY_SIZE(a1_scu_info); 2897 sprintf(info_cb.ver_name, "A1"); 2898 break; 2899 case OTP_A2: 2900 info_cb.version = OTP_A2; 2901 info_cb.conf_info = a2_conf_info; 2902 info_cb.conf_info_len = ARRAY_SIZE(a2_conf_info); 2903 info_cb.strap_info = a1_strap_info; 2904 info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info); 2905 info_cb.key_info = a2_key_type; 2906 info_cb.key_info_len = ARRAY_SIZE(a2_key_type); 2907 info_cb.scu_info = a1_scu_info; 2908 info_cb.scu_info_len = ARRAY_SIZE(a1_scu_info); 2909 sprintf(info_cb.ver_name, "A2"); 2910 break; 2911 case OTP_A3: 2912 info_cb.version = OTP_A3; 2913 info_cb.conf_info = a3_conf_info; 2914 info_cb.conf_info_len = ARRAY_SIZE(a3_conf_info); 2915 info_cb.strap_info = a1_strap_info; 2916 info_cb.strap_info_len = ARRAY_SIZE(a1_strap_info); 2917 info_cb.key_info = a3_key_type; 2918 info_cb.key_info_len = ARRAY_SIZE(a3_key_type); 2919 info_cb.scu_info = a1_scu_info; 2920 info_cb.scu_info_len = ARRAY_SIZE(a1_scu_info); 2921 sprintf(info_cb.ver_name, "A3"); 2922 break; 2923 default: 2924 printf("SOC is not supported\n"); 2925 return CMD_RET_FAILURE; 2926 } 2927 2928 writel(OTP_PASSWD, OTP_PROTECT_KEY); //password 2929 otp_read_conf(0, &otp_conf0); 2930 pro_sts = &info_cb.pro_sts; 2931 2932 pro_sts->mem_lock = (otp_conf0 >> 31) & 0x1; 2933 pro_sts->pro_key_ret = (otp_conf0 >> 29) & 0x1; 2934 pro_sts->pro_strap = (otp_conf0 >> 25) & 0x1; 2935 pro_sts->pro_conf = (otp_conf0 >> 24) & 0x1; 2936 pro_sts->pro_data = (otp_conf0 >> 23) & 0x1; 2937 pro_sts->pro_sec = (otp_conf0 >> 22) & 0x1; 2938 pro_sts->sec_size = ((otp_conf0 >> 16) & 0x3f) << 5; 2939 2940 ret = cp->cmd(cmdtp, flag, argc, argv); 2941 writel(1, OTP_PROTECT_KEY); //protect otp controller 2942 2943 return ret; 2944 } 2945 2946 U_BOOT_CMD(otp, 7, 0, do_ast_otp, 2947 "ASPEED One-Time-Programmable sub-system", 2948 "version\n" 2949 "otp read conf|data <otp_dw_offset> <dw_count>\n" 2950 "otp read strap <strap_bit_offset> <bit_count>\n" 2951 "otp info strap [v]\n" 2952 "otp info conf [otp_dw_offset]\n" 2953 "otp info scu\n" 2954 "otp info key\n" 2955 "otp prog [o] <addr>\n" 2956 "otp pb conf|data [o] <otp_dw_offset> <bit_offset> <value>\n" 2957 "otp pb strap [o] <bit_offset> <value>\n" 2958 "otp protect [o] <bit_offset>\n" 2959 "otp scuprotect [o] <scu_offset> <bit_offset>\n" 2960 "otp update [o] <revision_id>\n" 2961 "otp rid\n" 2962 "otp retire [o] <key_id>\n" 2963 ); 2964