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