Skip to content

Commit a7e4e03

Browse files
nasahlpasiemen11
authored andcommitted
[pentest] Add CharSramWriteReadAlt IbexFi test
This commit adds the CharSramWriteReadAlt test to the IbexFi test framework. Signed-off-by: Pascal Nasahl <[email protected]> Co-authored-by: Siemen Dhooghe <[email protected]>
1 parent 96296fa commit a7e4e03

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ enum {
3838
* Number of reference values that are available in the ref_values array.
3939
*/
4040
kNumRefValues = 32,
41+
/**
42+
* Number of values used for the sram_write_read_alt test.
43+
*/
44+
kSramWriteReadAltNumValues = 16,
4145
};
4246

4347
// A function which takes an uint32_t as its only argument.
@@ -3318,6 +3322,96 @@ status_t handle_ibex_fi_char_sram_write_read(ujson_t *uj)
33183322
return OK_STATUS();
33193323
}
33203324

3325+
status_t handle_ibex_fi_char_sram_write_read_alt(ujson_t *uj)
3326+
__attribute__((optnone)) {
3327+
// Clear registered alerts in alert handler.
3328+
pentest_registered_alerts_t reg_alerts = pentest_get_triggered_alerts();
3329+
// Clear the AST recoverable alerts.
3330+
pentest_clear_sensor_recov_alerts();
3331+
3332+
// Initialize the SRAM with a counter
3333+
for (size_t i = 0; i < kSramWriteReadAltNumValues; i++) {
3334+
sram_main_buffer[i] = i;
3335+
}
3336+
3337+
// Initialize the register file before the FI trigger window.
3338+
INIT_REGISTER_FILE
3339+
3340+
// FI code target.
3341+
PENTEST_ASM_TRIGGER_HIGH
3342+
asm volatile("sw x5, (%0)" : : "r"((uint32_t *)&sram_main_buffer[0]));
3343+
asm volatile("lw x5, (%0)" : : "r"((uint32_t *)&sram_main_buffer[0]));
3344+
asm volatile("sw x6, (%0)" : : "r"((uint32_t *)&sram_main_buffer[1]));
3345+
asm volatile("lw x6, (%0)" : : "r"((uint32_t *)&sram_main_buffer[1]));
3346+
asm volatile("sw x7, (%0)" : : "r"((uint32_t *)&sram_main_buffer[2]));
3347+
asm volatile("lw x7, (%0)" : : "r"((uint32_t *)&sram_main_buffer[2]));
3348+
asm volatile("sw x5, (%0)" : : "r"((uint32_t *)&sram_main_buffer[3]));
3349+
asm volatile("lw x5, (%0)" : : "r"((uint32_t *)&sram_main_buffer[3]));
3350+
asm volatile("sw x6, (%0)" : : "r"((uint32_t *)&sram_main_buffer[4]));
3351+
asm volatile("lw x6, (%0)" : : "r"((uint32_t *)&sram_main_buffer[4]));
3352+
asm volatile("sw x7, (%0)" : : "r"((uint32_t *)&sram_main_buffer[5]));
3353+
asm volatile("lw x7, (%0)" : : "r"((uint32_t *)&sram_main_buffer[5]));
3354+
asm volatile("sw x5, (%0)" : : "r"((uint32_t *)&sram_main_buffer[6]));
3355+
asm volatile("lw x5, (%0)" : : "r"((uint32_t *)&sram_main_buffer[6]));
3356+
asm volatile("sw x6, (%0)" : : "r"((uint32_t *)&sram_main_buffer[7]));
3357+
asm volatile("lw x6, (%0)" : : "r"((uint32_t *)&sram_main_buffer[7]));
3358+
asm volatile("sw x7, (%0)" : : "r"((uint32_t *)&sram_main_buffer[8]));
3359+
asm volatile("lw x7, (%0)" : : "r"((uint32_t *)&sram_main_buffer[8]));
3360+
asm volatile("sw x5, (%0)" : : "r"((uint32_t *)&sram_main_buffer[9]));
3361+
asm volatile("lw x5, (%0)" : : "r"((uint32_t *)&sram_main_buffer[9]));
3362+
asm volatile("sw x6, (%0)" : : "r"((uint32_t *)&sram_main_buffer[10]));
3363+
asm volatile("lw x6, (%0)" : : "r"((uint32_t *)&sram_main_buffer[10]));
3364+
asm volatile("sw x7, (%0)" : : "r"((uint32_t *)&sram_main_buffer[11]));
3365+
asm volatile("lw x7, (%0)" : : "r"((uint32_t *)&sram_main_buffer[11]));
3366+
asm volatile("sw x5, (%0)" : : "r"((uint32_t *)&sram_main_buffer[12]));
3367+
asm volatile("lw x5, (%0)" : : "r"((uint32_t *)&sram_main_buffer[12]));
3368+
asm volatile("sw x6, (%0)" : : "r"((uint32_t *)&sram_main_buffer[13]));
3369+
asm volatile("lw x6, (%0)" : : "r"((uint32_t *)&sram_main_buffer[13]));
3370+
asm volatile("sw x7, (%0)" : : "r"((uint32_t *)&sram_main_buffer[14]));
3371+
asm volatile("lw x7, (%0)" : : "r"((uint32_t *)&sram_main_buffer[14]));
3372+
asm volatile("sw x5, (%0)" : : "r"((uint32_t *)&sram_main_buffer[15]));
3373+
asm volatile("lw x5, (%0)" : : "r"((uint32_t *)&sram_main_buffer[15]));
3374+
PENTEST_ASM_TRIGGER_LOW
3375+
3376+
// Dump the register file after the FI trigger window.
3377+
DUMP_REGISTER_FILE
3378+
3379+
// Load the values stored in the sram buffer.
3380+
uint32_t sram_values[kSramWriteReadAltNumValues] = {0};
3381+
for (size_t i = 0; i < kSramWriteReadAltNumValues; i++) {
3382+
sram_values[i] = sram_main_buffer[i];
3383+
}
3384+
3385+
// Get registered alerts from alert handler.
3386+
reg_alerts = pentest_get_triggered_alerts();
3387+
// Get fatal and recoverable AST alerts from sensor controller.
3388+
pentest_sensor_alerts_t sensor_alerts = pentest_get_sensor_alerts();
3389+
3390+
// Read ERR_STATUS register.
3391+
dif_rv_core_ibex_error_status_t codes;
3392+
TRY(dif_rv_core_ibex_get_error_status(&rv_core_ibex, &codes));
3393+
3394+
ibex_fi_test_result_sram_t uj_output;
3395+
// Preset buffers to 0.
3396+
memset(uj_output.memory, 0, sizeof(uj_output.memory));
3397+
memset(uj_output.registers, 0, sizeof(uj_output.registers));
3398+
3399+
// Return the 16 values from SRAM.
3400+
for (size_t it = 0; it < kSramWriteReadAltNumValues; it++) {
3401+
uj_output.memory[it] = sram_values[it];
3402+
}
3403+
3404+
// Return register file dump back to host.
3405+
memcpy(uj_output.registers, registers_dumped, sizeof(registers_dumped));
3406+
// Send result & ERR_STATUS to host.
3407+
uj_output.err_status = codes;
3408+
memcpy(uj_output.alerts, reg_alerts.alerts, sizeof(reg_alerts.alerts));
3409+
memcpy(uj_output.ast_alerts, sensor_alerts.alerts,
3410+
sizeof(sensor_alerts.alerts));
3411+
RESP_OK(ujson_serialize_ibex_fi_test_result_sram_t, uj, &uj_output);
3412+
return OK_STATUS();
3413+
}
3414+
33213415
status_t handle_ibex_fi_char_sram_write_static_unrolled(ujson_t *uj)
33223416
__attribute__((optnone)) {
33233417
// Clear registered alerts in alert handler.
@@ -4029,6 +4123,8 @@ status_t handle_ibex_fi(ujson_t *uj) {
40294123
return handle_ibex_fi_char_sram_write(uj);
40304124
case kIbexFiSubcommandCharSramWriteRead:
40314125
return handle_ibex_fi_char_sram_write_read(uj);
4126+
case kIbexFiSubcommandCharSramWriteReadAlt:
4127+
return handle_ibex_fi_char_sram_write_read_alt(uj);
40324128
case kIbexFiSubcommandCharSramWriteStaticUnrolled:
40334129
return handle_ibex_fi_char_sram_write_static_unrolled(uj);
40344130
case kIbexFiSubcommandCharUncondBranch:

sw/device/tests/penetrationtests/firmware/fi/ibex_fi.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,26 @@ status_t handle_ibex_fi_char_sram_write(ujson_t *uj);
667667
*/
668668
status_t handle_ibex_fi_char_sram_write_read(ujson_t *uj);
669669

670+
/**
671+
* ibex.fi.char.sram_write_read_alt command handler.
672+
*
673+
* This FI penetration tests executes the following instructions:
674+
* - Set the trigger.
675+
* - Add 10 NOPs to delay the trigger
676+
* - Store a reference value in SRAM, load the value back in a different
677+
* register and repeat for a different SRAM address repeated 16 times.
678+
* - Unset the trigger.
679+
* - Read back values from the registers and the SRAM and compare.
680+
* - Return the values over UART.
681+
*
682+
* Faults are injected during the trigger_high & trigger_low.
683+
* It needs to be ensured that the compiler does not optimize this code.
684+
*
685+
* @param uj An initialized uJSON context.
686+
* @return OK or error.
687+
*/
688+
status_t handle_ibex_fi_char_sram_write_read_alt(ujson_t *uj);
689+
670690
/**
671691
* ibex.fi.char.sram_write_static_unrolled command handler.
672692
*

sw/device/tests/penetrationtests/json/ibex_fi_commands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ extern "C" {
5252
value(_, CharSramStatic) \
5353
value(_, CharSramWrite) \
5454
value(_, CharSramWriteRead) \
55+
value(_, CharSramWriteReadAlt) \
5556
value(_, CharSramWriteStaticUnrolled) \
5657
value(_, CharUncondBranch) \
5758
value(_, CharUncondBranchNop) \

sw/host/penetrationtests/testvectors/data/fi_ibex.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,10 @@
172172
"{\"result\":13,\"err_status\":0,\"alerts\":[0,0,0],\"ast_alerts\":[0,0]}",
173173
"{\"result\":15,\"err_status\":0,\"alerts\":[0,0,0],\"ast_alerts\":[0,0]}"
174174
]
175+
},
176+
{
177+
"test_case_id": 33,
178+
"command": "CharSramWriteReadAlt",
179+
"expected_output": ["{\"memory\":[2880293630,3131746989,3134333474,2880293630,3131746989,3134333474,2880293630,3131746989,3134333474,2880293630,3131746989,3134333474,2880293630,3131746989,3134333474,2880293630,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\"err_status\":0,\"alerts\":[0,0,0],\"ast_alerts\":[0,0]}"]
175180
}
176181
]

0 commit comments

Comments
 (0)