Skip to content

Commit 04481ec

Browse files
committed
[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]>
1 parent 87841fb commit 04481ec

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
@@ -105,6 +105,17 @@ static struct sector_buffer_t sector_buffers;
105105
#endif
106106
#endif
107107

108+
#if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1 && defined(MCUBOOT_OVERWRITE_ONLY) && \
109+
defined(MCUBOOT_DOWNGRADE_PREVENTION)
110+
/* s0/s1 package version of the current MCUboot image */
111+
static const struct image_version mcuboot_s0_s1_image_version = {
112+
.iv_major = CONFIG_MCUBOOT_MCUBOOT_S0_S1_VERSION_MAJOR,
113+
.iv_minor = CONFIG_MCUBOOT_MCUBOOT_S0_S1_VERSION_MINOR,
114+
.iv_revision = CONFIG_MCUBOOT_MCUBOOT_S0_S1_VERSION_REVISION,
115+
.iv_build_num = CONFIG_MCUBOOT_MCUBOOT_S0_S1_VERSION_BUILD_NUMBER,
116+
};
117+
#endif
118+
108119
#if (BOOT_IMAGE_NUMBER > 1)
109120
#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
110121
#else
@@ -1166,11 +1177,45 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
11661177
rc = boot_version_cmp(
11671178
&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
11681179
&boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1180+
1181+
#if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1182+
if (rc >= 0 && BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER) {
1183+
/* Also check the new version of MCUboot against that of the current s0/s1 MCUboot
1184+
* trailer version to prevent downgrades
1185+
*/
1186+
int version_check;
1187+
1188+
version_check = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1189+
&mcuboot_s0_s1_image_version);
1190+
1191+
/* Only update rc if the currently running version is newer */
1192+
if (version_check < rc) {
1193+
rc = version_check;
1194+
}
1195+
}
1196+
#endif
11691197
}
11701198
#else
11711199
rc = boot_version_cmp(
11721200
&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
11731201
&boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1202+
1203+
#if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1204+
if (rc >= 0 && BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER) {
1205+
/* Also check the new version of MCUboot against that of the current s0/s1 MCUboot
1206+
* trailer version to prevent downgrades
1207+
*/
1208+
int version_check;
1209+
1210+
version_check = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1211+
&mcuboot_s0_s1_image_version);
1212+
1213+
/* Only update rc if the currently running version is newer */
1214+
if (version_check < rc) {
1215+
rc = version_check;
1216+
}
1217+
}
1218+
#endif
11741219
#endif
11751220
if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
11761221
BOOT_LOG_ERR("insufficient version in secondary slot");

0 commit comments

Comments
 (0)