@@ -105,6 +105,17 @@ static struct sector_buffer_t sector_buffers;
105
105
#endif
106
106
#endif
107
107
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
+
108
119
#if (BOOT_IMAGE_NUMBER > 1 )
109
120
#define IMAGES_ITER (x ) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
110
121
#else
@@ -1166,11 +1177,45 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
1166
1177
rc = boot_version_cmp (
1167
1178
& boot_img_hdr (state , BOOT_SECONDARY_SLOT )-> ih_ver ,
1168
1179
& 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
1169
1197
}
1170
1198
#else
1171
1199
rc = boot_version_cmp (
1172
1200
& boot_img_hdr (state , BOOT_SECONDARY_SLOT )-> ih_ver ,
1173
1201
& 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
1174
1219
#endif
1175
1220
if (rc < 0 && boot_check_header_erased (state , BOOT_PRIMARY_SLOT )) {
1176
1221
BOOT_LOG_ERR ("insufficient version in secondary slot" );
0 commit comments