otp.c (b64ca3961d0f6d3b4840918f89584590659dc196) otp.c (b25f02d2a37ccdfd491e399c41d140ba83305b17)
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>

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

35#define OTP_PROG_SKIP 1
36
37#define OTP_KEY_TYPE_RSA_PUB 1
38#define OTP_KEY_TYPE_RSA_PRIV 2
39#define OTP_KEY_TYPE_AES 3
40#define OTP_KEY_TYPE_VAULT 4
41#define OTP_KEY_TYPE_HMAC 5
42
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>

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

35#define OTP_PROG_SKIP 1
36
37#define OTP_KEY_TYPE_RSA_PUB 1
38#define OTP_KEY_TYPE_RSA_PRIV 2
39#define OTP_KEY_TYPE_AES 3
40#define OTP_KEY_TYPE_VAULT 4
41#define OTP_KEY_TYPE_HMAC 5
42
43#define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
44#define PBWIDTH 60
45
46#define OTP_BASE 0x1e6f2000
47#define OTP_PROTECT_KEY OTP_BASE
48#define OTP_COMMAND OTP_BASE + 0x4
49#define OTP_TIMING OTP_BASE + 0x8
50#define OTP_ADDR OTP_BASE + 0x10
51#define OTP_STATUS OTP_BASE + 0x14
52#define OTP_COMPARE_1 OTP_BASE + 0x20
53#define OTP_COMPARE_2 OTP_BASE + 0x24

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

58#define SEC_KEY_NUM OTP_BASE + 0x78
59
60#define OTP_MAGIC "SOCOTP"
61#define CHECKSUM_LEN 32
62#define OTP_INC_DATA BIT(31)
63#define OTP_INC_CONFIG BIT(30)
64#define OTP_INC_STRAP BIT(29)
65#define OTP_ECC_EN BIT(28)
43#define OTP_BASE 0x1e6f2000
44#define OTP_PROTECT_KEY OTP_BASE
45#define OTP_COMMAND OTP_BASE + 0x4
46#define OTP_TIMING OTP_BASE + 0x8
47#define OTP_ADDR OTP_BASE + 0x10
48#define OTP_STATUS OTP_BASE + 0x14
49#define OTP_COMPARE_1 OTP_BASE + 0x20
50#define OTP_COMPARE_2 OTP_BASE + 0x24

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

55#define SEC_KEY_NUM OTP_BASE + 0x78
56
57#define OTP_MAGIC "SOCOTP"
58#define CHECKSUM_LEN 32
59#define OTP_INC_DATA BIT(31)
60#define OTP_INC_CONFIG BIT(30)
61#define OTP_INC_STRAP BIT(29)
62#define OTP_ECC_EN BIT(28)
63#define OTP_INC_SCU_PRO BIT(25)
66#define OTP_REGION_SIZE(info) ((info >> 16) & 0xffff)
67#define OTP_REGION_OFFSET(info) (info & 0xffff)
68#define OTP_IMAGE_SIZE(info) (info & 0xffff)
69
70#define OTP_A0 0
71#define OTP_A1 1
72#define OTP_A2 2
73#define OTP_A3 3

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

152 int scu_info_len;
153 struct otp_pro_sts pro_sts;
154};
155
156struct otp_image_layout {
157 int data_length;
158 int conf_length;
159 int strap_length;
64#define OTP_REGION_SIZE(info) ((info >> 16) & 0xffff)
65#define OTP_REGION_OFFSET(info) (info & 0xffff)
66#define OTP_IMAGE_SIZE(info) (info & 0xffff)
67
68#define OTP_A0 0
69#define OTP_A1 1
70#define OTP_A2 2
71#define OTP_A3 3

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

150 int scu_info_len;
151 struct otp_pro_sts pro_sts;
152};
153
154struct otp_image_layout {
155 int data_length;
156 int conf_length;
157 int strap_length;
158 int scu_pro_length;
160 u8 *data;
161 u8 *data_ignore;
162 u8 *conf;
163 u8 *conf_ignore;
164 u8 *strap;
165 u8 *strap_pro;
166 u8 *strap_ignore;
159 u8 *data;
160 u8 *data_ignore;
161 u8 *conf;
162 u8 *conf_ignore;
163 u8 *strap;
164 u8 *strap_pro;
165 u8 *strap_ignore;
166 u8 *scu_pro;
167 u8 *scu_pro_ignore;
167};
168
169static struct otp_info_cb info_cb;
170
171static const struct otpkey_type a0_key_type[] = {
172 {0, OTP_KEY_TYPE_AES, 0, "AES-256 as OEM platform key for image encryption/decryption"},
173 {1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
174 {4, OTP_KEY_TYPE_HMAC, 1, "HMAC as encrypted OEM HMAC keys in Mode 1"},

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

856 if (i % 16 == 0)
857 printf("%2x | ", i);
858 printf("%d ", (rid[j] >> bit_offset) & 0x1);
859 if ((i + 1) % 16 == 0)
860 printf("\n");
861 }
862}
863
168};
169
170static struct otp_info_cb info_cb;
171
172static const struct otpkey_type a0_key_type[] = {
173 {0, OTP_KEY_TYPE_AES, 0, "AES-256 as OEM platform key for image encryption/decryption"},
174 {1, OTP_KEY_TYPE_VAULT, 0, "AES-256 as secret vault key"},
175 {4, OTP_KEY_TYPE_HMAC, 1, "HMAC as encrypted OEM HMAC keys in Mode 1"},

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

857 if (i % 16 == 0)
858 printf("%2x | ", i);
859 printf("%d ", (rid[j] >> bit_offset) & 0x1);
860 if ((i + 1) % 16 == 0)
861 printf("\n");
862 }
863}
864
865static int otp_print_scu_image(struct otp_image_layout *image_layout)
866{
867 const struct scu_info *scu_info = info_cb.scu_info;
868 u32 *OTPSCU = (u32 *)image_layout->scu_pro;
869 u32 *OTPSCU_IGNORE = (u32 *)image_layout->scu_pro_ignore;
870 int i;
871 u32 scu_offset;
872 u32 dw_offset;
873 u32 bit_offset;
874 u32 mask;
875 u32 otp_value;
876 u32 otp_ignore;
877
878 printf("SCU BIT reg_protect Description\n");
879 printf("____________________________________________________________________\n");
880 for (i = 0; i < info_cb.scu_info_len; i++) {
881 mask = BIT(scu_info[i].length) - 1;
882
883 if (scu_info[i].bit_offset > 31) {
884 scu_offset = 0x510;
885 dw_offset = 1;
886 bit_offset = scu_info[i].bit_offset - 32;
887 } else {
888 scu_offset = 0x500;
889 dw_offset = 0;
890 bit_offset = scu_info[i].bit_offset;
891 }
892
893 otp_value = (OTPSCU[dw_offset] >> bit_offset) & mask;
894 otp_ignore = (OTPSCU_IGNORE[dw_offset] >> bit_offset) & mask;
895
896 if (otp_ignore == mask)
897 continue;
898 else if (otp_ignore != 0)
899 return OTP_FAILURE;
900
901 if (otp_value != 0 && otp_value != mask)
902 return OTP_FAILURE;
903
904 printf("0x%-6X", scu_offset);
905 if (scu_info[i].length == 1)
906 printf("0x%-11X", bit_offset);
907 else
908 printf("0x%-2X:0x%-4x", bit_offset, bit_offset + scu_info[i].length - 1);
909 printf("0x%-14X", otp_value);
910 printf("%s\n", scu_info[i].information);
911 }
912 return OTP_SUCCESS;
913}
914
864static void otp_print_scu_info(void)
865{
866 const struct scu_info *scu_info = info_cb.scu_info;
867 u32 OTPCFG[2];
868 u32 scu_offset;
869 u32 bit_offset;
870 u32 reg_p;
871 u32 length;

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

1526
1527 otp_soak(0);
1528 if (!pass)
1529 return OTP_FAILURE;
1530
1531 return OTP_SUCCESS;
1532}
1533
915static void otp_print_scu_info(void)
916{
917 const struct scu_info *scu_info = info_cb.scu_info;
918 u32 OTPCFG[2];
919 u32 scu_offset;
920 u32 bit_offset;
921 u32 reg_p;
922 u32 length;

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

1577
1578 otp_soak(0);
1579 if (!pass)
1580 return OTP_FAILURE;
1581
1582 return OTP_SUCCESS;
1583}
1584
1585static int otp_prog_scu_protect(struct otp_image_layout *image_layout, u32 *scu_pro)
1586{
1587 int i, k;
1588 int pass = 0;
1589 u32 prog_address;
1590 u32 compare[2];
1591 u32 *OTPSCU = (u32 *)image_layout->scu_pro;
1592 u32 *OTPSCU_IGNORE = (u32 *)image_layout->scu_pro_ignore;
1593 u32 data_masked;
1594 u32 buf_masked;
1595
1596 printf("Start Programing...\n");
1597 otp_soak(0);
1598 for (i = 0; i < 2; i++) {
1599 data_masked = scu_pro[i] & ~OTPSCU_IGNORE[i];
1600 buf_masked = OTPSCU[i] & ~OTPSCU_IGNORE[i];
1601 prog_address = 0xe08 + i * 2;
1602 if (data_masked == buf_masked) {
1603 pass = 1;
1604 continue;
1605 }
1606
1607 otp_soak(1);
1608 otp_prog_dw(OTPSCU[i], OTPSCU_IGNORE[i], prog_address);
1609
1610 pass = 0;
1611 for (k = 0; k < RETRY; k++) {
1612 if (verify_dw(prog_address, &OTPSCU[i], &OTPSCU_IGNORE[i], compare, 1) != 0) {
1613 otp_soak(2);
1614 otp_prog_dw(compare[0], OTPSCU_IGNORE[i], prog_address);
1615 if (verify_dw(prog_address, &OTPSCU[i], &OTPSCU_IGNORE[i], compare, 1) != 0) {
1616 otp_soak(1);
1617 } else {
1618 pass = 1;
1619 break;
1620 }
1621 } else {
1622 pass = 1;
1623 break;
1624 }
1625 }
1626 if (pass == 0) {
1627 printf("OTPCFG0x%x: %08x, input: %08x, mask: %08x\n",
1628 i + 28, scu_pro[i], OTPSCU[i], OTPSCU_IGNORE[i]);
1629 break;
1630 }
1631 }
1632
1633 otp_soak(0);
1634 if (!pass)
1635 return OTP_FAILURE;
1636
1637 return OTP_SUCCESS;
1638}
1639
1534static int otp_check_data_image(struct otp_image_layout *image_layout, u32 *data)
1535{
1536 int data_dw;
1537 u32 data_masked;
1538 u32 buf_masked;
1539 u32 *buf = (u32 *)image_layout->data;
1540 u32 *buf_ignore = (u32 *)image_layout->data_ignore;
1541 int i;

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

1630 printf("Input [%X] = %x\n", i, conf[i]);
1631 printf("Mask [%X] = %x\n", i, ~conf_ignore[i]);
1632 return OTP_FAILURE;
1633 }
1634 }
1635 return OTP_SUCCESS;
1636}
1637
1640static int otp_check_data_image(struct otp_image_layout *image_layout, u32 *data)
1641{
1642 int data_dw;
1643 u32 data_masked;
1644 u32 buf_masked;
1645 u32 *buf = (u32 *)image_layout->data;
1646 u32 *buf_ignore = (u32 *)image_layout->data_ignore;
1647 int i;

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

1736 printf("Input [%X] = %x\n", i, conf[i]);
1737 printf("Mask [%X] = %x\n", i, ~conf_ignore[i]);
1738 return OTP_FAILURE;
1739 }
1740 }
1741 return OTP_SUCCESS;
1742}
1743
1744static int otp_check_scu_image(struct otp_image_layout *image_layout, u32 *scu_pro)
1745{
1746 u32 *OTPSCU = (u32 *)image_layout->scu_pro;
1747 u32 *OTPSCU_IGNORE = (u32 *)image_layout->scu_pro_ignore;
1748 u32 data_masked;
1749 u32 buf_masked;
1750 int i;
1751
1752 for (i = 0; i < 2; i++) {
1753 data_masked = scu_pro[i] & ~OTPSCU_IGNORE[i];
1754 buf_masked = OTPSCU[i] & ~OTPSCU_IGNORE[i];
1755 if (data_masked == buf_masked)
1756 continue;
1757 if ((data_masked | buf_masked) == buf_masked) {
1758 continue;
1759 } else {
1760 printf("Input image can't program into OTP, please check.\n");
1761 printf("OTPCFG[%X] = %x\n", 28 + i, scu_pro[i]);
1762 printf("Input [%X] = %x\n", 28 + i, OTPSCU[i]);
1763 printf("Mask [%X] = %x\n", 28 + i, ~OTPSCU_IGNORE[i]);
1764 return OTP_FAILURE;
1765 }
1766 }
1767 return OTP_SUCCESS;
1768}
1769
1638static int otp_verify_image(u8 *src_buf, u32 length, u8 *digest_buf)
1639{
1640 sha256_context ctx;
1641 u8 digest_ret[CHECKSUM_LEN];
1642
1643 sha256_starts(&ctx);
1644 sha256_update(&ctx, src_buf, length);
1645 sha256_finish(&ctx, digest_ret);

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

1656 struct otp_header *otp_header;
1657 struct otp_image_layout image_layout;
1658 int image_size;
1659 u8 *buf;
1660 u8 *checksum;
1661 int i;
1662 u32 data[2048];
1663 u32 conf[16];
1770static int otp_verify_image(u8 *src_buf, u32 length, u8 *digest_buf)
1771{
1772 sha256_context ctx;
1773 u8 digest_ret[CHECKSUM_LEN];
1774
1775 sha256_starts(&ctx);
1776 sha256_update(&ctx, src_buf, length);
1777 sha256_finish(&ctx, digest_ret);

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

1788 struct otp_header *otp_header;
1789 struct otp_image_layout image_layout;
1790 int image_size;
1791 u8 *buf;
1792 u8 *checksum;
1793 int i;
1794 u32 data[2048];
1795 u32 conf[16];
1796 u32 scu_pro[2];
1664 struct otpstrap_status otpstrap[64];
1665
1666 otp_header = map_physmem(addr, sizeof(struct otp_header), MAP_WRBACK);
1667 if (!otp_header) {
1668 printf("Failed to map physical memory\n");
1669 return OTP_FAILURE;
1670 }
1671

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

1694 image_layout.conf = buf + OTP_REGION_OFFSET(otp_header->config_info);
1695 image_layout.conf_ignore = image_layout.conf + image_layout.conf_length;
1696
1697 image_layout.strap = buf + OTP_REGION_OFFSET(otp_header->strap_info);
1698 image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 3);
1699 image_layout.strap_pro = image_layout.strap + image_layout.strap_length;
1700 image_layout.strap_ignore = image_layout.strap + 2 * image_layout.strap_length;
1701
1797 struct otpstrap_status otpstrap[64];
1798
1799 otp_header = map_physmem(addr, sizeof(struct otp_header), MAP_WRBACK);
1800 if (!otp_header) {
1801 printf("Failed to map physical memory\n");
1802 return OTP_FAILURE;
1803 }
1804

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

1827 image_layout.conf = buf + OTP_REGION_OFFSET(otp_header->config_info);
1828 image_layout.conf_ignore = image_layout.conf + image_layout.conf_length;
1829
1830 image_layout.strap = buf + OTP_REGION_OFFSET(otp_header->strap_info);
1831 image_layout.strap_length = (int)(OTP_REGION_SIZE(otp_header->strap_info) / 3);
1832 image_layout.strap_pro = image_layout.strap + image_layout.strap_length;
1833 image_layout.strap_ignore = image_layout.strap + 2 * image_layout.strap_length;
1834
1835 image_layout.scu_pro = buf + OTP_REGION_OFFSET(otp_header->scu_protect_info);
1836 image_layout.scu_pro_length = (int)(OTP_REGION_SIZE(otp_header->scu_protect_info) / 2);
1837 image_layout.scu_pro_ignore = image_layout.scu_pro + image_layout.scu_pro_length;
1838
1702 if (otp_header->soc_ver == SOC_AST2600A0) {
1703 image_soc_ver = OTP_A0;
1704 } else if (otp_header->soc_ver == SOC_AST2600A1) {
1705 image_soc_ver = OTP_A1;
1706 } else if (otp_header->soc_ver == SOC_AST2600A2) {
1707 image_soc_ver = OTP_A2;
1708 } else if (otp_header->soc_ver == SOC_AST2600A3) {
1709 image_soc_ver = OTP_A3;

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

1769 }
1770 printf("Read OTP Strap Region:\n");
1771 otp_strap_status(otpstrap);
1772
1773 printf("Check writable...\n");
1774 if (otp_check_strap_image(&image_layout, otpstrap) == OTP_FAILURE)
1775 ret = -1;
1776 }
1839 if (otp_header->soc_ver == SOC_AST2600A0) {
1840 image_soc_ver = OTP_A0;
1841 } else if (otp_header->soc_ver == SOC_AST2600A1) {
1842 image_soc_ver = OTP_A1;
1843 } else if (otp_header->soc_ver == SOC_AST2600A2) {
1844 image_soc_ver = OTP_A2;
1845 } else if (otp_header->soc_ver == SOC_AST2600A3) {
1846 image_soc_ver = OTP_A3;

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

1906 }
1907 printf("Read OTP Strap Region:\n");
1908 otp_strap_status(otpstrap);
1909
1910 printf("Check writable...\n");
1911 if (otp_check_strap_image(&image_layout, otpstrap) == OTP_FAILURE)
1912 ret = -1;
1913 }
1914 if (otp_header->image_info & OTP_INC_SCU_PRO) {
1915 if (info_cb.pro_sts.pro_strap) {
1916 printf("OTP strap region is protected\n");
1917 ret = -1;
1918 }
1919 printf("Read SCU Protect Region:\n");
1920 otp_read_conf(28, &scu_pro[0]);
1921 otp_read_conf(29, &scu_pro[1]);
1922
1923 printf("Check writable...\n");
1924 if (otp_check_scu_image(&image_layout, scu_pro) == OTP_FAILURE)
1925 ret = -1;
1926 }
1777 if (ret == -1)
1778 return OTP_FAILURE;
1779
1780 if (!nconfirm) {
1781 if (otp_header->image_info & OTP_INC_DATA) {
1782 printf("\nOTP data region :\n");
1783 if (otp_print_data_image(&image_layout) < 0) {
1784 printf("OTP data error, please check.\n");

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

1794 }
1795 if (otp_header->image_info & OTP_INC_STRAP) {
1796 printf("\nOTP strap region :\n");
1797 if (otp_print_strap_image(&image_layout) < 0) {
1798 printf("OTP strap error, please check.\n");
1799 return OTP_FAILURE;
1800 }
1801 }
1927 if (ret == -1)
1928 return OTP_FAILURE;
1929
1930 if (!nconfirm) {
1931 if (otp_header->image_info & OTP_INC_DATA) {
1932 printf("\nOTP data region :\n");
1933 if (otp_print_data_image(&image_layout) < 0) {
1934 printf("OTP data error, please check.\n");

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

1944 }
1945 if (otp_header->image_info & OTP_INC_STRAP) {
1946 printf("\nOTP strap region :\n");
1947 if (otp_print_strap_image(&image_layout) < 0) {
1948 printf("OTP strap error, please check.\n");
1949 return OTP_FAILURE;
1950 }
1951 }
1952 if (otp_header->image_info & OTP_INC_SCU_PRO) {
1953 printf("\nOTP scu protect region :\n");
1954 if (otp_print_scu_image(&image_layout) < 0) {
1955 printf("OTP scu protect error, please check.\n");
1956 return OTP_FAILURE;
1957 }
1958 }
1802
1803 printf("type \"YES\" (no quotes) to continue:\n");
1804 if (!confirm_yesno()) {
1805 printf(" Aborting\n");
1806 return OTP_FAILURE;
1807 }
1808 }
1809

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

1820 printf("programing strap region ...\n");
1821 ret = otp_prog_strap(&image_layout, otpstrap);
1822 if (ret != 0) {
1823 printf("Error\n");
1824 return ret;
1825 }
1826 printf("Done\n");
1827 }
1959
1960 printf("type \"YES\" (no quotes) to continue:\n");
1961 if (!confirm_yesno()) {
1962 printf(" Aborting\n");
1963 return OTP_FAILURE;
1964 }
1965 }
1966

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

1977 printf("programing strap region ...\n");
1978 ret = otp_prog_strap(&image_layout, otpstrap);
1979 if (ret != 0) {
1980 printf("Error\n");
1981 return ret;
1982 }
1983 printf("Done\n");
1984 }
1985 if (otp_header->image_info & OTP_INC_SCU_PRO) {
1986 printf("programing scu protect region ...\n");
1987 ret = otp_prog_scu_protect(&image_layout, scu_pro);
1988 if (ret != 0) {
1989 printf("Error\n");
1990 return ret;
1991 }
1992 printf("Done\n");
1993 }
1828 if (otp_header->image_info & OTP_INC_CONFIG) {
1829 printf("programing configuration region ...\n");
1830 ret = otp_prog_conf(&image_layout, conf);
1831 if (ret != 0) {
1832 printf("Error\n");
1833 return ret;
1834 }
1835 printf("Done\n");

--- 799 unchanged lines hidden ---
1994 if (otp_header->image_info & OTP_INC_CONFIG) {
1995 printf("programing configuration region ...\n");
1996 ret = otp_prog_conf(&image_layout, conf);
1997 if (ret != 0) {
1998 printf("Error\n");
1999 return ret;
2000 }
2001 printf("Done\n");

--- 799 unchanged lines hidden ---