Skip to content

Commit e0629bc

Browse files
siemen11nasahlpa
authored andcommitted
[pentest] Fix fw override rng
Fix the fi rng test of using fw override. The test would only work more than once in case the device is reset in between. Instead, we re-initialize the fw override every call (we would like consecutive bits so we stop the fifo after every call). There was also a bug causing stack overflow by asking for bits in a for loop whereas the full buffer was filled in a single loop. Signed-off-by: Siemen Dhooghe <[email protected]> (cherry picked from commit 7f48129)
1 parent 5e16bb5 commit e0629bc

File tree

2 files changed

+24
-31
lines changed

2 files changed

+24
-31
lines changed

sw/device/tests/penetrationtests/firmware/fi/rng_fi.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ static dif_entropy_src_t entropy_src;
4141
static dif_csrng_t csrng;
4242
static dif_edn_t edn0;
4343
static dif_edn_t edn1;
44-
static bool disable_health_check;
45-
46-
static bool firmware_override_init;
4744

4845
static const uint32_t kInputMsg[kCsrngBiasFWFifoBufferSize] = {
4946
0xa52a0da9, 0xcae141b2, 0x6d5bab9d, 0x2c3e5cc0, 0x225afc93, 0x5d31a610,
@@ -219,42 +216,42 @@ status_t handle_rng_fi_firmware_override(ujson_t *uj) {
219216
// Clear the AST recoverable alerts.
220217
pentest_clear_sensor_recov_alerts();
221218

222-
if (!firmware_override_init) {
223-
// Check if we keep heal tests enabled.
224-
rng_fi_fw_overwrite_health_t uj_data;
225-
TRY(ujson_deserialize_rng_fi_fw_overwrite_health_t(uj, &uj_data));
226-
disable_health_check = uj_data.disable_health_check;
227-
228-
firmware_override_init = true;
229-
}
219+
rng_fi_fw_overwrite_health_t uj_data;
220+
TRY(ujson_deserialize_rng_fi_fw_overwrite_health_t(uj, &uj_data));
230221

222+
// Stop the entropy complex.
231223
TRY(entropy_testutils_stop_all());
232224

233-
if (disable_health_check) {
225+
if (uj_data.disable_health_check) {
234226
// Disable all health tests.
235227
TRY(entropy_testutils_disable_health_tests(&entropy_src));
236228
}
237229

230+
// Enable firmware override mode
238231
TRY(entropy_testutils_fw_override_enable(&entropy_src, kEntropyFifoBufferSize,
239232
/*route_to_firmware=*/true,
240233
/*bypass_conditioner=*/true));
241234

242-
entropy_data_flush(&entropy_src);
235+
// Flush any residual data in the observation FIFO.
236+
size_t len;
237+
do {
238+
len = kEntropyFifoBufferSize;
239+
TRY(dif_entropy_src_observe_fifo_nonblocking_read(&entropy_src, NULL,
240+
&len));
241+
} while (len > 0);
243242

244243
uint32_t buf[kEntropyFifoBufferSize] = {0};
245244

246245
pentest_set_trigger_high();
247246
asm volatile(NOP30);
248-
for (size_t it = 0; it < kEntropyFifoBufferSize; it++) {
249-
while (buf[it] == 0) {
250-
TRY(dif_entropy_src_observe_fifo_blocking_read(&entropy_src, &buf[it],
251-
kEntropyFifoBufferSize));
252-
}
253-
}
247+
TRY(dif_entropy_src_observe_fifo_blocking_read(&entropy_src, buf,
248+
kEntropyFifoBufferSize));
254249

255250
asm volatile(NOP30);
256251
pentest_set_trigger_low();
257252

253+
TRY(entropy_testutils_stop_all());
254+
258255
// Get registered alerts from alert handler.
259256
reg_alerts = pentest_get_triggered_alerts();
260257
// Get registered local alerts from alert handler.
@@ -434,9 +431,6 @@ status_t handle_rng_fi_edn_init(ujson_t *uj) {
434431
TRY(dif_edn_init(mmio_region_from_addr(TOP_EARLGREY_EDN0_BASE_ADDR), &edn0));
435432
TRY(dif_edn_init(mmio_region_from_addr(TOP_EARLGREY_EDN1_BASE_ADDR), &edn1));
436433

437-
firmware_override_init = false;
438-
439-
// Read different SKU config fields and return to host.
440434
TRY(pentest_send_sku_config(uj));
441435

442436
return OK_STATUS();

sw/host/penetrationtests/python/fi/host_scripts/fi_rng_functions.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,18 @@ def char_entropy_bias(target, iterations, reset = False):
7474
return response
7575

7676

77-
def char_fw_overwrite(target, iterations, disable_health_check):
77+
def char_fw_overwrite(target, iterations, disable_health_check, reset = False):
7878
rngfi = OTFIRng(target)
79-
80-
for _ in range(iterations):
79+
if reset:
8180
target.reset_target()
8281
# Clear the output from the reset
8382
target.dump_all()
84-
85-
# Initialize our chip and catch its output
86-
device_id, sensors, alerts, owner_page, boot_log, boot_measurements, version = (
87-
rngfi.init("char_fw_overwrite",
88-
alert_config=common_library.default_fpga_friendly_alert_config)
89-
)
83+
# Initialize our chip and catch its output
84+
device_id, sensors, alerts, owner_page, boot_log, boot_measurements, version = (
85+
rngfi.init("char_fw_overwrite",
86+
alert_config=common_library.default_fpga_friendly_alert_config)
87+
)
88+
for _ in range(iterations):
9089
rngfi.rng_fw_overwrite(disable_health_check)
9190
response = target.read_response()
9291
return response

0 commit comments

Comments
 (0)