Skip to content

Commit d535766

Browse files
committed
bootloader: mcuboot: Fixed security counter overflow detected to late
This commit fixes the issue, occuring when the maximum amount of security counter updates has been reached. This fact was only detected after a permament update already happened - the updated firmware was unable to boot, as it failed when trying to update the security counter after the permament swap. This commit adds the check if the security counter can be updated (i. e. free security counter slots are still available) before the swap is performed, fixing the issue. Signed-off-by: Artur Hadasz <[email protected]>
1 parent 8c2b246 commit d535766

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

include/bl_storage.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,19 @@ int get_monotonic_counter(uint16_t counter_desc, counter_t *counter_value);
275275
*/
276276
int set_monotonic_counter(uint16_t counter_desc, counter_t new_counter);
277277

278+
/**
279+
* @brief Checks whether it is possible to update the monotonic counter
280+
* to a new value.
281+
*
282+
* @param[in] counter_desc Counter description.
283+
*
284+
* @retval 0 The counter was updated successfully.
285+
* @retval -EINVAL @p counter_desc is invalid.
286+
* @retval -ENOMEM There are no more free counter slots (see
287+
* @kconfig{CONFIG_SB_NUM_VER_COUNTER_SLOTS}).
288+
*/
289+
int is_monotonic_counter_update_possible(uint16_t counter_desc);
290+
278291
/**
279292
* @brief The PSA life cycle states a device can be in.
280293
*

subsys/bootloader/bl_storage/bl_storage.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,27 @@ int set_monotonic_counter(uint16_t counter_desc, counter_t new_counter)
390390
return 0;
391391
}
392392

393+
int is_monotonic_counter_update_possible(uint16_t counter_desc)
394+
{
395+
int err;
396+
397+
const counter_t *next_counter_addr;
398+
counter_t current_cnt_value;
399+
400+
err = get_counter(counter_desc, &current_cnt_value, &next_counter_addr);
401+
(void) current_cnt_value;
402+
403+
if (err != 0) {
404+
return err;
405+
}
406+
407+
if (next_counter_addr == NULL) {
408+
err = -ENOMEM;
409+
}
410+
411+
return err;
412+
}
413+
393414
static lcs_data_t bl_storage_lcs_get(uint32_t address)
394415
{
395416
#if defined(CONFIG_NRFX_NVMC)

subsys/bootloader/bl_storage/nrf_nv_counters.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,16 @@ int32_t boot_nv_security_counter_update(uint32_t image_id, uint32_t img_security
7474

7575
return err == 0 ? 0 : -BOOT_EBADSTATUS;
7676
}
77+
78+
fih_int boot_nv_security_counter_is_update_possible(void)
79+
{
80+
int err;
81+
82+
err = is_monotonic_counter_update_possible(BL_MONOTONIC_COUNTERS_DESC_MCUBOOT_ID0);
83+
84+
if (err != 0) {
85+
FIH_RET(FIH_FAILURE);
86+
}
87+
88+
FIH_RET(FIH_SUCCESS);
89+
}

west.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ manifest:
128128
compare-by-default: true
129129
- name: mcuboot
130130
repo-path: sdk-mcuboot
131-
revision: 4bfb139af4fadbc651ea61e27d21331834ce2307
131+
revision: pull/493/head
132132
path: bootloader/mcuboot
133133
- name: qcbor
134134
url: https://github.com/laurencelundblade/QCBOR

0 commit comments

Comments
 (0)