Skip to content

Commit d5f373a

Browse files
nordicjmrlubos
authored andcommitted
[nrf noup] boot: bootutil: loader: Add s0/s1 checking of MCUboot image
Adds a check that will also check the s0/s1 package version of the currently running MCUboot against a MCUboot update image to ensure that an older version of MCUboot isn't loaded to the opposite slot Signed-off-by: Jamie McCrae <[email protected]> (cherry picked from commit 9c83462)
1 parent 352b2a4 commit d5f373a

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

boot/bootutil/src/loader.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ static struct sector_buffer_t sector_buffers;
101101
#endif
102102
#endif
103103

104+
#if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1 && defined(MCUBOOT_OVERWRITE_ONLY) && \
105+
defined(MCUBOOT_DOWNGRADE_PREVENTION)
106+
/* s0/s1 package version of the current MCUboot image */
107+
static const struct image_version mcuboot_s0_s1_image_version = {
108+
.iv_major = CONFIG_MCUBOOT_MCUBOOT_S0_S1_VERSION_MAJOR,
109+
.iv_minor = CONFIG_MCUBOOT_MCUBOOT_S0_S1_VERSION_MINOR,
110+
.iv_revision = CONFIG_MCUBOOT_MCUBOOT_S0_S1_VERSION_REVISION,
111+
.iv_build_num = CONFIG_MCUBOOT_MCUBOOT_S0_S1_VERSION_BUILD_NUMBER,
112+
};
113+
#endif
114+
104115
#if (BOOT_IMAGE_NUMBER > 1)
105116
#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
106117
#else
@@ -1182,11 +1193,45 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
11821193
rc = boot_version_cmp(
11831194
&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
11841195
&boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1196+
1197+
#if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1198+
if (rc >= 0 && BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER) {
1199+
/* Also check the new version of MCUboot against that of the current s0/s1 MCUboot
1200+
* trailer version to prevent downgrades
1201+
*/
1202+
int version_check;
1203+
1204+
version_check = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1205+
&mcuboot_s0_s1_image_version);
1206+
1207+
/* Only update rc if the currently running version is newer */
1208+
if (version_check < rc) {
1209+
rc = version_check;
1210+
}
1211+
}
1212+
#endif
11851213
}
11861214
#else
11871215
rc = boot_version_cmp(
11881216
&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
11891217
&boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1218+
1219+
#if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1220+
if (rc >= 0 && BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER) {
1221+
/* Also check the new version of MCUboot against that of the current s0/s1 MCUboot
1222+
* trailer version to prevent downgrades
1223+
*/
1224+
int version_check;
1225+
1226+
version_check = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1227+
&mcuboot_s0_s1_image_version);
1228+
1229+
/* Only update rc if the currently running version is newer */
1230+
if (version_check < rc) {
1231+
rc = version_check;
1232+
}
1233+
}
1234+
#endif
11901235
#endif
11911236
if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
11921237
BOOT_LOG_ERR("insufficient version in secondary slot");

0 commit comments

Comments
 (0)