Skip to content

Commit 2534681

Browse files
adsz-nordicnordicjm
authored andcommitted
[nrf noup] boot: Add retry for image verification
Intended mainly for direct-xip mode. Allows to control: - number of image validation attempts performed before considering the image invalid - time before next attempt is made Signed-off-by: Adam Szczygieł <[email protected]>
1 parent 9016396 commit 2534681

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

boot/bootutil/src/loader.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757

5858
#ifdef __ZEPHYR__
5959
#include <zephyr/sys/reboot.h>
60+
#if defined(CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_WAIT_MS)
61+
#include <zephyr/kernel.h>
62+
#endif /* CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_WAIT_MS */
6063
#endif
6164

6265
#if defined(CONFIG_SOC_NRF5340_CPUAPP) && defined(PM_CPUNET_B0N_ADDRESS)
@@ -862,14 +865,42 @@ boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
862865
}
863866
#endif
864867

868+
for (int i = 1; i <= CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT; i++ ) {
869+
#if CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT > 1
870+
BOOT_LOG_DBG("Image validation attempt %d/%d", i, CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT);
871+
#endif /* CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT > 1 */
872+
865873
#if defined(MCUBOOT_SWAP_USING_OFFSET) && defined(MCUBOOT_SERIAL_RECOVERY)
866-
FIH_CALL(bootutil_img_validate, fih_rc, state, hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
867-
NULL, 0, NULL, 0);
874+
FIH_CALL(bootutil_img_validate, fih_rc, state, hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
875+
NULL, 0, NULL, 0);
868876
#else
869-
FIH_CALL(bootutil_img_validate, fih_rc, state, hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
870-
NULL, 0, NULL);
877+
FIH_CALL(bootutil_img_validate, fih_rc, state, hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
878+
NULL, 0, NULL);
871879
#endif
872880

881+
if (FIH_EQ(fih_rc, FIH_SUCCESS)) {
882+
#if CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT > 1
883+
BOOT_LOG_DBG("Image validation attempt %d/%d success", i, CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT);
884+
#endif /* CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT > 1 */
885+
break;
886+
} else {
887+
#if CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT > 1
888+
BOOT_LOG_WRN("Image validation attempt %d/%d failure: %d",
889+
i,
890+
CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT, fih_rc);
891+
#endif /* CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT > 1 */
892+
893+
if (i < CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT) {
894+
#if defined(CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_WAIT_MS)
895+
#if CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT > 1
896+
BOOT_LOG_DBG("Waiting %d ms before next attempt",
897+
CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_WAIT_MS);
898+
#endif /* CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT > 1 */
899+
k_busy_wait(CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_WAIT_MS * 1000);
900+
#endif /* CONFIG_NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_WAIT_MS */
901+
}
902+
}
903+
}
873904
FIH_RET(fih_rc);
874905
}
875906

boot/zephyr/Kconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,4 +1213,21 @@ config MCUBOOT_VERIFY_IMG_ADDRESS
12131213
also be useful when BOOT_DIRECT_XIP is enabled, to ensure that the image
12141214
linked at the correct address is loaded.
12151215

1216+
config NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT
1217+
int "Number of image validation attempts"
1218+
default 1
1219+
help
1220+
Number of image validation attempts performed before an image is considered invalid.
1221+
A wait is done between each attempt to allow for recovery from a temporary disruption.
1222+
This can prevent erasing an image when initial validation fails.
1223+
Wait time is controlled by MCUBOOT_IMG_VALIDATE_ATTEMPT_WAIT_MS.
1224+
1225+
config NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_WAIT_MS
1226+
int "Time between image validation attempts"
1227+
depends on NRF_MCUBOOT_IMG_VALIDATE_ATTEMPT_COUNT > 1
1228+
default 5000
1229+
help
1230+
Time between image validation attempts, in milliseconds.
1231+
Allows for recovery from transient bit flips or similar situations.
1232+
12161233
source "Kconfig.zephyr"

0 commit comments

Comments
 (0)