otp.c (e7e21c4455937427b95934f61c48824737c20816) otp.c (418822f0e57e287e0a276be72738ad57d3f6ac28)
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>

--- 1377 unchanged lines hidden (view full) ---

1386 }
1387
1388 if (fail)
1389 return OTP_FAILURE;
1390
1391 return OTP_SUCCESS;
1392}
1393
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>

--- 1377 unchanged lines hidden (view full) ---

1386 }
1387
1388 if (fail)
1389 return OTP_FAILURE;
1390
1391 return OTP_SUCCESS;
1392}
1393
1394static void _otp_print_key(u32 *data)
1394static void _otp_print_key(u32 header, u32 offset, u8 *data)
1395{
1395{
1396 int i, j;
1397 int key_id, key_offset, last, key_type, key_length, exp_length;
1398 struct otpkey_type key_info;
1399 const struct otpkey_type *key_info_array = info_cb.key_info;
1396 const struct otpkey_type *key_info_array = info_cb.key_info;
1397 struct otpkey_type key_info;
1398 int key_id, key_offset, key_type, key_length, exp_length;
1400 int len = 0;
1399 int len = 0;
1400 int i;
1401
1402 key_id = header & 0x7;
1403 key_offset = header & 0x1ff8;
1404 key_type = (header >> 14) & 0xf;
1405 key_length = (header >> 18) & 0x3;
1406 exp_length = (header >> 20) & 0xfff;
1407
1408 printf("\nKey[%d]:\n", offset);
1409 printf("Header: %x\n", header);
1410
1411 key_info.value = -1;
1412 for (i = 0; i < info_cb.key_info_len; i++) {
1413 if (key_type == key_info_array[i].value) {
1414 key_info = key_info_array[i];
1415 break;
1416 }
1417 }
1418 if (key_info.value == -1)
1419 return;
1420
1421 printf("Key Type: ");
1422 printf("%s\n", key_info.information);
1423
1424 if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
1425 printf("HMAC SHA Type: ");
1426 switch (key_length) {
1427 case 0:
1428 printf("HMAC(SHA224)\n");
1429 break;
1430 case 1:
1431 printf("HMAC(SHA256)\n");
1432 break;
1433 case 2:
1434 printf("HMAC(SHA384)\n");
1435 break;
1436 case 3:
1437 printf("HMAC(SHA512)\n");
1438 break;
1439 }
1440 } else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV ||
1441 key_info.key_type == OTP_KEY_TYPE_RSA_PUB) {
1442 printf("RSA SHA Type: ");
1443 switch (key_length) {
1444 case 0:
1445 printf("RSA1024\n");
1446 len = 0x100;
1447 break;
1448 case 1:
1449 printf("RSA2048\n");
1450 len = 0x200;
1451 break;
1452 case 2:
1453 printf("RSA3072\n");
1454 len = 0x300;
1455 break;
1456 case 3:
1457 printf("RSA4096\n");
1458 len = 0x400;
1459 break;
1460 }
1461 printf("RSA exponent bit length: %d\n", exp_length);
1462 }
1463 if (key_info.need_id)
1464 printf("Key Number ID: %d\n", key_id);
1465 if (!data)
1466 return;
1467 printf("Key Value:\n");
1468 if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
1469 buf_print(&data[key_offset], 0x40);
1470 } else if (key_info.key_type == OTP_KEY_TYPE_AES) {
1471 printf("AES Key:\n");
1472 buf_print(&data[key_offset], 0x20);
1473 if (info_cb.version == OTP_A0) {
1474 printf("AES IV:\n");
1475 buf_print(&data[key_offset + 0x20], 0x10);
1476 }
1477
1478 } else if (key_info.key_type == OTP_KEY_TYPE_VAULT) {
1479 if (info_cb.version == OTP_A0) {
1480 printf("AES Key:\n");
1481 buf_print(&data[key_offset], 0x20);
1482 printf("AES IV:\n");
1483 buf_print(&data[key_offset + 0x20], 0x10);
1484 } else {
1485 printf("AES Key 1:\n");
1486 buf_print(&data[key_offset], 0x20);
1487 printf("AES Key 2:\n");
1488 buf_print(&data[key_offset + 0x20], 0x20);
1489 }
1490 } else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV) {
1491 printf("RSA mod:\n");
1492 buf_print(&data[key_offset], len / 2);
1493 printf("RSA exp:\n");
1494 buf_print(&data[key_offset + (len / 2)], len / 2);
1495 } else if (key_info.key_type == OTP_KEY_TYPE_RSA_PUB) {
1496 printf("RSA mod:\n");
1497 buf_print(&data[key_offset], len / 2);
1498 printf("RSA exp:\n");
1499 buf_print((u8 *)"\x01\x00\x01", 3);
1500 }
1501}
1502
1503static void otp_print_key(u32 *data)
1504{
1505 int i;
1506 int last;
1401 u8 *byte_buf;
1402 int empty;
1403
1404 byte_buf = (u8 *)data;
1405
1406 empty = 1;
1407 for (i = 0; i < 16; i++) {
1408 if (i % 2) {

--- 5 unchanged lines hidden (view full) ---

1414 }
1415 }
1416 if (empty) {
1417 printf("OTP data header is empty\n");
1418 return;
1419 }
1420
1421 for (i = 0; i < 16; i++) {
1507 u8 *byte_buf;
1508 int empty;
1509
1510 byte_buf = (u8 *)data;
1511
1512 empty = 1;
1513 for (i = 0; i < 16; i++) {
1514 if (i % 2) {

--- 5 unchanged lines hidden (view full) ---

1520 }
1521 }
1522 if (empty) {
1523 printf("OTP data header is empty\n");
1524 return;
1525 }
1526
1527 for (i = 0; i < 16; i++) {
1422 key_id = data[i] & 0x7;
1423 key_offset = data[i] & 0x1ff8;
1424 last = (data[i] >> 13) & 1;
1528 last = (data[i] >> 13) & 1;
1425 key_type = (data[i] >> 14) & 0xf;
1426 key_length = (data[i] >> 18) & 0x3;
1427 exp_length = (data[i] >> 20) & 0xfff;
1428
1429 key_info.value = -1;
1430 for (j = 0; j < info_cb.key_info_len; j++) {
1431 if (key_type == key_info_array[j].value) {
1432 key_info = key_info_array[j];
1433 break;
1434 }
1435 }
1436 if (key_info.value == -1)
1437 break;
1438
1439 printf("\nKey[%d]:\n", i);
1440 printf("Key Type: ");
1441 printf("%s\n", key_info.information);
1442
1443 if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
1444 printf("HMAC SHA Type: ");
1445 switch (key_length) {
1446 case 0:
1447 printf("HMAC(SHA224)\n");
1448 break;
1449 case 1:
1450 printf("HMAC(SHA256)\n");
1451 break;
1452 case 2:
1453 printf("HMAC(SHA384)\n");
1454 break;
1455 case 3:
1456 printf("HMAC(SHA512)\n");
1457 break;
1458 }
1459 } else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV ||
1460 key_info.key_type == OTP_KEY_TYPE_RSA_PUB) {
1461 printf("RSA SHA Type: ");
1462 switch (key_length) {
1463 case 0:
1464 printf("RSA1024\n");
1465 len = 0x100;
1466 break;
1467 case 1:
1468 printf("RSA2048\n");
1469 len = 0x200;
1470 break;
1471 case 2:
1472 printf("RSA3072\n");
1473 len = 0x300;
1474 break;
1475 case 3:
1476 printf("RSA4096\n");
1477 len = 0x400;
1478 break;
1479 }
1480 printf("RSA exponent bit length: %d\n", exp_length);
1481 }
1482 if (key_info.need_id)
1483 printf("Key Number ID: %d\n", key_id);
1484 printf("Key Value:\n");
1485 if (key_info.key_type == OTP_KEY_TYPE_HMAC) {
1486 buf_print(&byte_buf[key_offset], 0x40);
1487 } else if (key_info.key_type == OTP_KEY_TYPE_AES) {
1488 printf("AES Key:\n");
1489 buf_print(&byte_buf[key_offset], 0x20);
1490 if (info_cb.version == OTP_A0) {
1491 printf("AES IV:\n");
1492 buf_print(&byte_buf[key_offset + 0x20], 0x10);
1493 }
1494
1495 } else if (key_info.key_type == OTP_KEY_TYPE_VAULT) {
1496 if (info_cb.version == OTP_A0) {
1497 printf("AES Key:\n");
1498 buf_print(&byte_buf[key_offset], 0x20);
1499 printf("AES IV:\n");
1500 buf_print(&byte_buf[key_offset + 0x20], 0x10);
1501 } else {
1502 printf("AES Key 1:\n");
1503 buf_print(&byte_buf[key_offset], 0x20);
1504 printf("AES Key 2:\n");
1505 buf_print(&byte_buf[key_offset + 0x20], 0x20);
1506 }
1507 } else if (key_info.key_type == OTP_KEY_TYPE_RSA_PRIV) {
1508 printf("RSA mod:\n");
1509 buf_print(&byte_buf[key_offset], len / 2);
1510 printf("RSA exp:\n");
1511 buf_print(&byte_buf[key_offset + (len / 2)], len / 2);
1512 } else if (key_info.key_type == OTP_KEY_TYPE_RSA_PUB) {
1513 printf("RSA mod:\n");
1514 buf_print(&byte_buf[key_offset], len / 2);
1515 printf("RSA exp:\n");
1516 buf_print((u8 *)"\x01\x00\x01", 3);
1517 }
1529 _otp_print_key(data[i], i, byte_buf);
1518 if (last)
1519 break;
1520 }
1521}
1522
1523static int otp_print_data_image(struct otp_image_layout *image_layout)
1524{
1525 u32 *buf;
1526
1527 buf = (u32 *)image_layout->data;
1530 if (last)
1531 break;
1532 }
1533}
1534
1535static int otp_print_data_image(struct otp_image_layout *image_layout)
1536{
1537 u32 *buf;
1538
1539 buf = (u32 *)image_layout->data;
1528 _otp_print_key(buf);
1540 otp_print_key(buf);
1529
1530 return OTP_SUCCESS;
1531}
1532
1533static void otp_print_key_info(void)
1534{
1535 u32 data[2048];
1536 int i;
1537
1538 for (i = 0; i < 2048 ; i += 2)
1539 otp_read_data(i, &data[i]);
1540
1541
1542 return OTP_SUCCESS;
1543}
1544
1545static void otp_print_key_info(void)
1546{
1547 u32 data[2048];
1548 int i;
1549
1550 for (i = 0; i < 2048 ; i += 2)
1551 otp_read_data(i, &data[i]);
1552
1541 _otp_print_key(data);
1553 otp_print_key(data);
1542}
1543
1544static int otp_prog_data(struct otp_image_layout *image_layout, u32 *data)
1545{
1546 int i;
1547 int ret;
1548 u32 *buf;
1549 u32 *buf_ignore;

--- 1168 unchanged lines hidden (view full) ---

2718 printf(" Little endian\n");
2719 printf("Verify secure image: PASS\n");
2720 return OTP_SUCCESS;
2721 }
2722 printf("Verify secure image: FAIL\n");
2723 return OTP_FAILURE;
2724}
2725
1554}
1555
1556static int otp_prog_data(struct otp_image_layout *image_layout, u32 *data)
1557{
1558 int i;
1559 int ret;
1560 u32 *buf;
1561 u32 *buf_ignore;

--- 1168 unchanged lines hidden (view full) ---

2730 printf(" Little endian\n");
2731 printf("Verify secure image: PASS\n");
2732 return OTP_SUCCESS;
2733 }
2734 printf("Verify secure image: FAIL\n");
2735 return OTP_FAILURE;
2736}
2737
2738static int otp_invalid_key(u32 header_offset, int force)
2739{
2740 int i;
2741 int ret;
2742 u32 header_list[16];
2743 u32 header;
2744 u32 key_type;
2745 u32 prog_val;
2746
2747 for (i = 0; i < 16 ; i += 2)
2748 otp_read_data(i, &header_list[i]);
2749 header = header_list[header_offset];
2750 key_type = (header >> 14) & 0xf;
2751 _otp_print_key(header, header_offset, NULL);
2752 if (key_type == 0 || key_type == 0xf) {
2753 printf("Key[%d] already invalid\n", header_offset);
2754 return OTP_SUCCESS;
2755 }
2756
2757 printf("Key[%d] will be invalid\n", header_offset);
2758 if (force == 0) {
2759 printf("type \"YES\" (no quotes) to continue:\n");
2760 if (!confirm_yesno()) {
2761 printf(" Aborting\n");
2762 return OTP_FAILURE;
2763 }
2764 }
2765
2766 if (header_offset % 2)
2767 prog_val = 0;
2768 else
2769 prog_val = 1;
2770 for (i = 14; i <= 17; i++) {
2771 ret = otp_prog_dc_b(prog_val, header_offset, i);
2772 if (ret) {
2773 printf("OTPDATA0x%x[%d] programming failed\n", header_offset, i);
2774 return OTP_FAILURE;
2775 }
2776 }
2777
2778 printf("SUCCESS\n");
2779 return OTP_SUCCESS;
2780}
2781
2726static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2727{
2728 u32 offset, count;
2729 int ret;
2730
2731 if (argc == 4) {
2732 offset = simple_strtoul(argv[2], NULL, 16);
2733 count = simple_strtoul(argv[3], NULL, 16);

--- 504 unchanged lines hidden (view full) ---

3238 if (ret == OTP_SUCCESS)
3239 return CMD_RET_SUCCESS;
3240 else if (ret == OTP_FAILURE)
3241 return CMD_RET_FAILURE;
3242 else
3243 return CMD_RET_USAGE;
3244}
3245
2782static int do_otpread(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
2783{
2784 u32 offset, count;
2785 int ret;
2786
2787 if (argc == 4) {
2788 offset = simple_strtoul(argv[2], NULL, 16);
2789 count = simple_strtoul(argv[3], NULL, 16);

--- 504 unchanged lines hidden (view full) ---

3294 if (ret == OTP_SUCCESS)
3295 return CMD_RET_SUCCESS;
3296 else if (ret == OTP_FAILURE)
3297 return CMD_RET_FAILURE;
3298 else
3299 return CMD_RET_USAGE;
3300}
3301
3302static int do_otpinvalid(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
3303{
3304 u32 header_offset;
3305 int force = 0;
3306 int ret;
3307
3308 if (argc == 3) {
3309 if (strcmp(argv[1], "o"))
3310 return CMD_RET_USAGE;
3311 force = 1;
3312 header_offset = simple_strtoul(argv[2], NULL, 16);
3313 } else if (argc == 2) {
3314 header_offset = simple_strtoul(argv[1], NULL, 16);
3315 } else {
3316 return CMD_RET_USAGE;
3317 }
3318
3319 if (header_offset > 16)
3320 return CMD_RET_USAGE;
3321 ret = otp_invalid_key(header_offset, force);
3322
3323 if (ret)
3324 return CMD_RET_FAILURE;
3325 return CMD_RET_SUCCESS;
3326}
3327
3246static cmd_tbl_t cmd_otp[] = {
3247 U_BOOT_CMD_MKENT(version, 1, 0, do_otpver, "", ""),
3248 U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""),
3249 U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""),
3250 U_BOOT_CMD_MKENT(prog, 3, 0, do_otpprog, "", ""),
3251 U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""),
3252 U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""),
3253 U_BOOT_CMD_MKENT(scuprotect, 4, 0, do_otp_scuprotect, "", ""),
3254 U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""),
3255 U_BOOT_CMD_MKENT(update, 3, 0, do_otpupdate, "", ""),
3256 U_BOOT_CMD_MKENT(rid, 1, 0, do_otprid, "", ""),
3257 U_BOOT_CMD_MKENT(retire, 3, 0, do_otpretire, "", ""),
3258 U_BOOT_CMD_MKENT(verify, 2, 0, do_otpverify, "", ""),
3328static cmd_tbl_t cmd_otp[] = {
3329 U_BOOT_CMD_MKENT(version, 1, 0, do_otpver, "", ""),
3330 U_BOOT_CMD_MKENT(read, 4, 0, do_otpread, "", ""),
3331 U_BOOT_CMD_MKENT(info, 3, 0, do_otpinfo, "", ""),
3332 U_BOOT_CMD_MKENT(prog, 3, 0, do_otpprog, "", ""),
3333 U_BOOT_CMD_MKENT(pb, 6, 0, do_otppb, "", ""),
3334 U_BOOT_CMD_MKENT(protect, 3, 0, do_otpprotect, "", ""),
3335 U_BOOT_CMD_MKENT(scuprotect, 4, 0, do_otp_scuprotect, "", ""),
3336 U_BOOT_CMD_MKENT(cmp, 3, 0, do_otpcmp, "", ""),
3337 U_BOOT_CMD_MKENT(update, 3, 0, do_otpupdate, "", ""),
3338 U_BOOT_CMD_MKENT(rid, 1, 0, do_otprid, "", ""),
3339 U_BOOT_CMD_MKENT(retire, 3, 0, do_otpretire, "", ""),
3340 U_BOOT_CMD_MKENT(verify, 2, 0, do_otpverify, "", ""),
3341 U_BOOT_CMD_MKENT(invalid, 3, 0, do_otpinvalid, "", ""),
3259};
3260
3261static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
3262{
3263 struct otp_pro_sts *pro_sts;
3264 cmd_tbl_t *cp;
3265 u32 ver;
3266 int ret;

--- 94 unchanged lines hidden (view full) ---

3361 "otp pb conf|data [o] <otp_dw_offset> <bit_offset> <value>\n"
3362 "otp pb strap [o] <bit_offset> <value>\n"
3363 "otp protect [o] <bit_offset>\n"
3364 "otp scuprotect [o] <scu_offset> <bit_offset>\n"
3365 "otp update [o] <revision_id>\n"
3366 "otp rid\n"
3367 "otp retire [o] <key_id>\n"
3368 "otp verify <addr>\n"
3342};
3343
3344static int do_ast_otp(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
3345{
3346 struct otp_pro_sts *pro_sts;
3347 cmd_tbl_t *cp;
3348 u32 ver;
3349 int ret;

--- 94 unchanged lines hidden (view full) ---

3444 "otp pb conf|data [o] <otp_dw_offset> <bit_offset> <value>\n"
3445 "otp pb strap [o] <bit_offset> <value>\n"
3446 "otp protect [o] <bit_offset>\n"
3447 "otp scuprotect [o] <scu_offset> <bit_offset>\n"
3448 "otp update [o] <revision_id>\n"
3449 "otp rid\n"
3450 "otp retire [o] <key_id>\n"
3451 "otp verify <addr>\n"
3452 "otp invalid [o] <header_offset>\n"
3369 );
3453 );