Skip to content

Commit ab75982

Browse files
committed
[ot] hw/opentitan: ot_lc_ctrl: Fix HW_CFG load memcpy size calculation
These `memcpy`s were accidentally using the size of the dereferenced byte array (i.e., always just 1 byte), and so were only copying the first byte of the HW_CFG device ID and manufacturing state that were being loaded from the OTP. Also add some local calculations and static assertions to check that the HW_CFG value sizes that are given do not become out of sync with the size of the registers. Signed-off-by: Alex Jones <[email protected]>
1 parent 1ede166 commit ab75982

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

hw/opentitan/ot_lc_ctrl.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ REG32(MANUF_STATE_7, 0x88u)
146146
#define LC_TOKEN_WIDTH 16u /* 128 bits */
147147
#define LC_TOKEN_DWORDS (LC_TOKEN_WIDTH / sizeof(uint64_t))
148148

149+
#define LC_DEV_ID_WIDTH OT_REG_SPAN(DEVICE_ID, 7)
150+
#define LC_MANUF_STATE_WIDTH OT_REG_SPAN(MANUF_STATE, 7)
151+
149152
#define REG_NAME_ENTRY(_reg_) [R_##_reg_] = stringify(_reg_)
150153
static const char *REG_NAMES[REGS_COUNT] = {
151154
REG_NAME_ENTRY(ALERT_TEST),
@@ -435,7 +438,11 @@ typedef struct {
435438
} OtLcCtrlTransitionDesc;
436439

437440
static_assert(sizeof(OtOTPTokenValue) == LC_TOKEN_WIDTH,
438-
"Unexpected LC TOLEN WIDTH");
441+
"Unexpected LC Token width");
442+
static_assert(OT_OTP_HWCFG_DEVICE_ID_BYTES == LC_DEV_ID_WIDTH,
443+
"Unexpected LC Device ID width");
444+
static_assert(OT_OTP_HWCFG_MANUF_STATE_BYTES == LC_MANUF_STATE_WIDTH,
445+
"Unexpected LC Manufacturing State width");
439446

440447
#define KECCAK_STATE_BITS 1600u
441448
#define KECCAK_STATE_BYTES (KECCAK_STATE_BITS / 8u)
@@ -1211,10 +1218,14 @@ static void ot_lc_ctrl_load_otp_hw_cfg(OtLcCtrlState *s)
12111218
OtOTPClass *oc = OBJECT_GET_CLASS(OtOTPClass, s->otp_ctrl, TYPE_OT_OTP);
12121219
const OtOTPHWCfg *hw_cfg = oc->get_hw_cfg(s->otp_ctrl);
12131220

1214-
memcpy(&s->regs[R_DEVICE_ID_0], &hw_cfg->device_id[0],
1215-
sizeof(*hw_cfg->device_id));
1221+
static_assert(sizeof(hw_cfg->device_id) == LC_DEV_ID_WIDTH,
1222+
"HW_CFG Device ID size does not match size in registers");
1223+
memcpy(&s->regs[R_DEVICE_ID_0], &hw_cfg->device_id[0], LC_DEV_ID_WIDTH);
1224+
1225+
static_assert(sizeof(hw_cfg->manuf_state) == LC_MANUF_STATE_WIDTH,
1226+
"HW_CFG Manuf State size does not match size in registers");
12161227
memcpy(&s->regs[R_MANUF_STATE_0], &hw_cfg->manuf_state[0],
1217-
sizeof(*hw_cfg->manuf_state));
1228+
LC_MANUF_STATE_WIDTH);
12181229

12191230
if (!s->socdbg) {
12201231
return;

0 commit comments

Comments
 (0)