Skip to content

Commit 48acefe

Browse files
committed
[test] Prevent creator secret being re-written
`keymgr` testutils tries to write the creator secret to flash even if it's locked, triggering a recoverable flash_ctrl alert which is ignored. Signed-off-by: James Wainwright <[email protected]> (cherry picked from commit cbaf5e3)
1 parent 347aa22 commit 48acefe

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

sw/device/lib/testing/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ cc_library(
232232
":kmac_testutils",
233233
":otp_ctrl_testutils",
234234
":rstmgr_testutils",
235+
"//hw/top:dt",
235236
"//hw/top:keymgr_c_regs",
236237
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
237238
"//sw/device/lib/arch:boot_stage",

sw/device/lib/testing/keymgr_testutils.c

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
#include "sw/device/lib/testing/keymgr_testutils.h"
66

7+
#include "hw/top/dt/dt_otp_ctrl.h"
78
#include "sw/device/lib/arch/boot_stage.h"
89
#include "sw/device/lib/dif/dif_flash_ctrl.h"
910
#include "sw/device/lib/dif/dif_keymgr.h"
10-
#include "sw/device/lib/dif/dif_kmac.h"
1111
#include "sw/device/lib/dif/dif_otp_ctrl.h"
1212
#include "sw/device/lib/dif/dif_rstmgr.h"
13-
#include "sw/device/lib/runtime/ibex.h"
1413
#include "sw/device/lib/runtime/log.h"
1514
#include "sw/device/lib/testing/entropy_testutils.h"
1615
#include "sw/device/lib/testing/flash_ctrl_testutils.h"
@@ -78,17 +77,18 @@ status_t keymgr_testutils_flash_init(
7877
const keymgr_testutils_secret_t *creator_secret,
7978
const keymgr_testutils_secret_t *owner_secret) {
8079
// Initialize flash secrets.
81-
write_info_page(flash, kFlashInfoPageIdCreatorSecret, creator_secret,
82-
/*scramble=*/true);
80+
if (creator_secret) {
81+
write_info_page(flash, kFlashInfoPageIdCreatorSecret, creator_secret,
82+
/*scramble=*/true);
83+
}
8384
write_info_page(flash, kFlashInfoPageIdOwnerSecret, owner_secret,
8485
/*scramble=*/true);
8586
return OK_STATUS();
8687
}
8788

8889
static status_t check_lock_otp_partition(void) {
8990
dif_otp_ctrl_t otp;
90-
TRY(dif_otp_ctrl_init(
91-
mmio_region_from_addr(TOP_EARLGREY_OTP_CTRL_CORE_BASE_ADDR), &otp));
91+
TRY(dif_otp_ctrl_init_from_dt(kDtOtpCtrl, &otp));
9292

9393
bool is_computed;
9494
TRY(dif_otp_ctrl_is_digest_computed(&otp, kDifOtpCtrlPartitionSecret2,
@@ -189,6 +189,7 @@ status_t keymgr_testutils_try_startup(dif_keymgr_t *keymgr, dif_kmac_t *kmac,
189189
status_t keymgr_testutils_init_nvm_then_reset(void) {
190190
dif_flash_ctrl_state_t flash;
191191
dif_rstmgr_t rstmgr;
192+
dif_otp_ctrl_t otp_ctrl;
192193

193194
TRY(dif_rstmgr_init(mmio_region_from_addr(TOP_EARLGREY_RSTMGR_AON_BASE_ADDR),
194195
&rstmgr));
@@ -201,8 +202,22 @@ status_t keymgr_testutils_init_nvm_then_reset(void) {
201202

202203
TRY(dif_flash_ctrl_init_state(
203204
&flash, mmio_region_from_addr(TOP_EARLGREY_FLASH_CTRL_CORE_BASE_ADDR)));
204-
205-
TRY(keymgr_testutils_flash_init(&flash, &kCreatorSecret, &kOwnerSecret));
205+
TRY(dif_otp_ctrl_init(
206+
mmio_region_from_addr(TOP_EARLGREY_OTP_CTRL_CORE_BASE_ADDR),
207+
&otp_ctrl));
208+
209+
bool secret2_computed = false;
210+
TRY(dif_otp_ctrl_is_digest_computed(&otp_ctrl, kDifOtpCtrlPartitionSecret2,
211+
&secret2_computed));
212+
213+
// Only initialise the creator secret if `SECRET2` digest has not been
214+
// computed. `flash_ctrl` will throw a recoverable error if we try to write
215+
// this afterwards.
216+
const keymgr_testutils_secret_t *creator_secret = NULL;
217+
if (!secret2_computed) {
218+
creator_secret = &kCreatorSecret;
219+
}
220+
TRY(keymgr_testutils_flash_init(&flash, creator_secret, &kOwnerSecret));
206221

207222
TRY(check_lock_otp_partition());
208223

0 commit comments

Comments
 (0)