@@ -101,6 +101,17 @@ static struct sector_buffer_t sector_buffers;
101
101
#endif
102
102
#endif
103
103
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
+
104
115
#if (BOOT_IMAGE_NUMBER > 1 )
105
116
#define IMAGES_ITER (x ) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
106
117
#else
@@ -1182,11 +1193,45 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
1182
1193
rc = boot_version_cmp (
1183
1194
& boot_img_hdr (state , BOOT_SECONDARY_SLOT )-> ih_ver ,
1184
1195
& 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
1185
1213
}
1186
1214
#else
1187
1215
rc = boot_version_cmp (
1188
1216
& boot_img_hdr (state , BOOT_SECONDARY_SLOT )-> ih_ver ,
1189
1217
& 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
1190
1235
#endif
1191
1236
if (rc < 0 && boot_check_header_erased (state , BOOT_PRIMARY_SLOT )) {
1192
1237
BOOT_LOG_ERR ("insufficient version in secondary slot" );
0 commit comments