Skip to content

Commit ba9d3ad

Browse files
committed
[nrf fromlist] bootutil: Add API to lock HW counter
Add a new API to lock further updates of the HW-based security counter. This API may prevent the non-bootloader application from accidental invalidation of the bootable firmware. Upstream PR #: 2543 Signed-off-by: Tomasz Chyrowicz <[email protected]>
1 parent 0d263fa commit ba9d3ad

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

boot/bootutil/include/bootutil/security_cnt.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ int32_t boot_nv_security_counter_update(uint32_t image_id,
8585
fih_ret boot_nv_security_counter_is_update_possible(uint32_t image_id,
8686
uint32_t img_security_cnt);
8787

88+
/**
89+
* Lock updates of the stored value of a given image's security counter.
90+
*
91+
* @param image_id Index of the image (from 0).
92+
*
93+
* @return 0 on success; nonzero on failure.
94+
*/
95+
int32_t boot_nv_security_counter_lock(uint32_t image_id);
96+
8897
#ifdef __cplusplus
8998
}
9099
#endif

boot/bootutil/src/loader.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,8 +2581,15 @@ boot_update_hw_rollback_protection(struct boot_loader_state *state)
25812581
if (swap_state.magic != BOOT_MAGIC_GOOD || swap_state.image_ok == BOOT_FLAG_SET) {
25822582
rc = boot_update_security_counter(state, BOOT_SLOT_PRIMARY, BOOT_SLOT_PRIMARY);
25832583
if (rc != 0) {
2584-
BOOT_LOG_ERR("Security counter update failed after image "
2585-
"validation.");
2584+
BOOT_LOG_ERR("Security counter update failed after image %d validation.",
2585+
BOOT_CURR_IMG(state));
2586+
return rc;
2587+
}
2588+
2589+
rc = boot_nv_security_counter_lock(BOOT_CURR_IMG(state));
2590+
if (rc != 0) {
2591+
BOOT_LOG_ERR("Security counter lock failed after image %d validation.",
2592+
BOOT_CURR_IMG(state));
25862593
return rc;
25872594
}
25882595
}
@@ -3361,7 +3368,15 @@ boot_update_hw_rollback_protection(struct boot_loader_state *state)
33613368
state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
33623369
state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
33633370
if (rc != 0) {
3364-
BOOT_LOG_ERR("Security counter update failed after image %d validation.", BOOT_CURR_IMG(state));
3371+
BOOT_LOG_ERR("Security counter update failed after image %d validation.",
3372+
BOOT_CURR_IMG(state));
3373+
return rc;
3374+
}
3375+
3376+
rc = boot_nv_security_counter_lock(BOOT_CURR_IMG(state));
3377+
if (rc != 0) {
3378+
BOOT_LOG_ERR("Security counter lock failed after image %d validation.",
3379+
BOOT_CURR_IMG(state));
33653380
return rc;
33663381
}
33673382
#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)

0 commit comments

Comments
 (0)