Skip to content

Commit 2bc36d0

Browse files
tomchyde-nordic
authored andcommitted
bootutil: Expose hardware security counter
Add a possibility to read the current value of the hardware security counter from the application. Signed-off-by: Tomasz Chyrowicz <[email protected]>
1 parent 7a9f30f commit 2bc36d0

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

boot/bootutil/include/bootutil/boot_status.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ extern "C" {
119119
#define BLINFO_MAX_APPLICATION_SIZE_IMAGE_2 0x07
120120
#define BLINFO_MAX_APPLICATION_SIZE_IMAGE_3 0x08
121121
#define BLINFO_MAX_APPLICATION_SIZE_IMAGE_4 0x09
122+
#define BLINFO_SECURITY_COUNTER_IMAGE_0 0x10
123+
#define BLINFO_SECURITY_COUNTER_IMAGE_1 0x11
124+
#define BLINFO_SECURITY_COUNTER_IMAGE_2 0x12
125+
#define BLINFO_SECURITY_COUNTER_IMAGE_3 0x13
126+
#define BLINFO_SECURITY_COUNTER_IMAGE_4 0x14
122127

123128
enum mcuboot_mode {
124129
MCUBOOT_MODE_SINGLE_SLOT,

boot/bootutil/src/boot_record.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
#include "mcuboot_config/mcuboot_config.h"
2626
#include "bootutil/crypto/sha.h"
27+
#ifdef MCUBOOT_HW_ROLLBACK_PROT
28+
#include "bootutil/security_cnt.h"
29+
#endif
2730

2831
#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
2932
#include "bootutil/boot_record.h"
@@ -242,6 +245,10 @@ int boot_save_shared_data(const struct image_header *hdr, const struct flash_are
242245
int rc;
243246
#if !defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
244247
uint8_t image = 0;
248+
#ifdef MCUBOOT_HW_ROLLBACK_PROT
249+
uint32_t security_cnt;
250+
FIH_DECLARE(fih_rc, FIH_FAILURE);
251+
#endif
245252
#endif
246253

247254
#if defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
@@ -346,14 +353,37 @@ int boot_save_shared_data(const struct image_header *hdr, const struct flash_are
346353
}
347354
#endif
348355

356+
#if !defined(MCUBOOT_SINGLE_APPLICATION_SLOT) && defined(MCUBOOT_HW_ROLLBACK_PROT)
357+
image = 0;
358+
while (image < BOOT_IMAGE_NUMBER && !rc) {
359+
FIH_CALL(boot_nv_security_counter_get, fih_rc, image, &security_cnt);
360+
if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
361+
/* Some platforms support only a single security counter. */
362+
continue;
363+
}
364+
365+
rc = boot_add_data_to_shared_area(TLV_MAJOR_BLINFO,
366+
BLINFO_SECURITY_COUNTER_IMAGE_0 + image,
367+
sizeof(security_cnt),
368+
(void *)&security_cnt);
369+
if (!rc) {
370+
break;
371+
}
372+
++image;
373+
}
374+
#endif /* !defined(MCUBOOT_SINGLE_APPLICATION_SLOT) && defined(MCUBOOT_HW_ROLLBACK_PROT) */
375+
349376
#if !defined(MCUBOOT_SINGLE_APPLICATION_SLOT)
377+
image = 0;
350378
while (image < BOOT_IMAGE_NUMBER && !rc) {
351379
if (max_app_sizes[image].calculated == true) {
352380
rc = boot_add_data_to_shared_area(TLV_MAJOR_BLINFO,
353381
(BLINFO_MAX_APPLICATION_SIZE + image),
354382
sizeof(max_app_sizes[image].max_size),
355383
(void *)&max_app_sizes[image].max_size);
356-
384+
if (!rc) {
385+
break;
386+
}
357387
}
358388

359389
++image;

0 commit comments

Comments
 (0)