8 #include "sw/device/lib/base/crc32.h"
11 #include "sw/device/lib/testing/entropy_testutils.h"
12 #include "sw/device/lib/testing/rand_testutils.h"
13 #include "sw/device/lib/testing/test_framework/check.h"
15 #include "sw/device/silicon_creator/lib/drivers/otp.h"
16 #include "sw/device/silicon_creator/lib/drivers/rnd.h"
17 #include "sw/device/silicon_creator/lib/error.h"
20 #include "otp_ctrl_regs.h"
22 OTTF_DEFINE_TEST_CONFIG();
24 static dif_entropy_src_t entropy_src;
29 uint32_t rnd_threshold) {
40 result = rnd_threshold;
43 CHECK(
false,
"Unexpected dif_entropy_src_test_t %d", test_id);
52 static void configure_random_health_checks(
void) {
54 CHECK_STATUS_OK(entropy_testutils_auto_mode_init());
57 test_id < kDifEntropySrcTestNumVariants; ++test_id) {
60 uint32_t meaningless_threshold =
61 rand_testutils_gen32_range(0, 16);
64 .high_threshold = 0xFFFFFF00 ^ meaningless_threshold,
65 .low_threshold = random_low_threshold(test_id, meaningless_threshold),
70 CHECK_STATUS_OK(entropy_testutils_stop_all());
71 for (
size_t i = 0; i <
ARRAYSIZE(configs); ++i) {
75 CHECK_STATUS_OK(entropy_testutils_auto_mode_init());
80 static void configure_health_checks_from_otp(
void) {
81 CHECK_STATUS_OK(entropy_testutils_stop_all());
86 .high_threshold = otp_read32(
87 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_REPCNT_THRESHOLDS_OFFSET),
93 .high_threshold = otp_read32(
94 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_REPCNTS_THRESHOLDS_OFFSET),
100 .high_threshold = otp_read32(
101 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_ADAPTP_HI_THRESHOLDS_OFFSET),
102 .low_threshold = otp_read32(
103 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_ADAPTP_LO_THRESHOLDS_OFFSET),
108 .high_threshold = otp_read32(
109 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_BUCKET_THRESHOLDS_OFFSET),
115 .high_threshold = otp_read32(
116 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_MARKOV_HI_THRESHOLDS_OFFSET),
117 .low_threshold = otp_read32(
118 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_MARKOV_LO_THRESHOLDS_OFFSET),
123 .high_threshold = otp_read32(
124 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_EXTHT_HI_THRESHOLDS_OFFSET),
125 .low_threshold = otp_read32(
126 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_EXTHT_LO_THRESHOLDS_OFFSET),
129 for (
size_t i = 0; i <
ARRAYSIZE(configs); ++i) {
140 .route_to_firmware =
false,
141 .bypass_conditioner =
false,
143 .health_test_window_size = 0x0200,
144 .alert_threshold = (uint16_t)otp_read32(
145 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_ALERT_THRESHOLD_OFFSET),
153 rom_error_t test_otp_crc_check(
void) {
156 uint32_t otp_in_order_offsets[] = {
157 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_REPCNT_THRESHOLDS_OFFSET,
158 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_REPCNTS_THRESHOLDS_OFFSET,
159 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_ADAPTP_HI_THRESHOLDS_OFFSET,
160 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_ADAPTP_LO_THRESHOLDS_OFFSET,
161 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_BUCKET_THRESHOLDS_OFFSET,
162 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_MARKOV_HI_THRESHOLDS_OFFSET,
163 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_MARKOV_LO_THRESHOLDS_OFFSET,
164 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_EXTHT_HI_THRESHOLDS_OFFSET,
165 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_EXTHT_LO_THRESHOLDS_OFFSET,
166 OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_ALERT_THRESHOLD_OFFSET,
168 for (
size_t i = 0; i <
ARRAYSIZE(otp_in_order_offsets); ++i) {
169 crc32_add32(&ctx, otp_read32(otp_in_order_offsets[i]));
171 uint32_t crc = crc32_finish(&ctx);
172 uint32_t expected = crc ^ kErrorOk;
174 otp_read32(OTP_CTRL_PARAM_CREATOR_SW_CFG_RNG_HEALTH_CONFIG_DIGEST_OFFSET);
175 CHECK(expected == got,
"Unexpected OTP CRC value. expected: 0x%x, got: 0x%x",
182 rom_error_t test_rnd_health_config_check(
void) {
190 lifecycle_state_t lc_expect_check[] = {
196 configure_health_checks_from_otp();
197 for (
size_t i = 0; i <
ARRAYSIZE(lc_expect_check); ++i) {
198 rom_error_t res = rnd_health_config_check(lc_expect_check[i]);
199 CHECK(res == kErrorOk,
"Lifecycle: %d, error: %d", lc_expect_check[i], res);
204 lifecycle_state_t lc_expect_check_skip[] = {
207 configure_random_health_checks();
208 for (
size_t i = 0; i <
ARRAYSIZE(lc_expect_check_skip); ++i) {
209 CHECK(rnd_health_config_check(lc_expect_check_skip[i]) == kErrorOk);
214 CHECK_STATUS_OK(entropy_testutils_auto_mode_init());
218 rom_error_t test_rnd(
void) {
219 const size_t kTestNumIter = 5;
220 uint32_t prev = rnd_uint32();
221 uint32_t error_cnt = 0;
222 for (
size_t i = 0; i < kTestNumIter; ++i) {
223 uint32_t got = rnd_uint32();
225 LOG_ERROR(
"Unexpected duplicate random number: 0x%x.", got);
231 LOG_ERROR(
"rnd_test failed, expected: 0 errors, got: %d.", error_cnt);
232 return kErrorUnknown;
238 CHECK_DIF_OK(dif_entropy_src_init(
245 return status_ok(result);