@@ -1240,6 +1240,87 @@ boot_update_security_counter(uint8_t image_index, int slot,
1240
1240
}
1241
1241
#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1242
1242
1243
+ #if defined(CONFIG_MCUBOOT_CLEANUP_UNUSABLE_SECONDARY ) && \
1244
+ (defined(PM_S1_ADDRESS ) || defined(CONFIG_SOC_NRF5340_CPUAPP ))
1245
+
1246
+ #define SEC_SLOT_VIRGIN 0
1247
+ #define SEC_SLOT_TOUCHED 1
1248
+ #define SEC_SLOT_ASSIGNED 2
1249
+
1250
+ #if (MCUBOOT_IMAGE_NUMBER == 2 ) && defined(PM_B0_ADDRESS ) && \
1251
+ !defined(CONFIG_NRF53_MULTI_IMAGE_UPDATE )
1252
+ /* This configuration is peculiar - the one physical secondary slot is
1253
+ * mocking two logical secondary
1254
+ */
1255
+ #define SEC_SLOT_PHYSICAL_CNT 1
1256
+ #else
1257
+ #define SEC_SLOT_PHYSICAL_CNT MCUBOOT_IMAGE_NUMBER
1258
+ #endif
1259
+
1260
+ static uint8_t sec_slot_assignmnet [SEC_SLOT_PHYSICAL_CNT ] = {0 };
1261
+
1262
+ static inline void sec_slot_touch (struct boot_loader_state * state )
1263
+ {
1264
+ uint8_t idx = (SEC_SLOT_PHYSICAL_CNT == 1 ) ? 0 : BOOT_CURR_IMG (state );
1265
+
1266
+ if (SEC_SLOT_VIRGIN == sec_slot_assignmnet [idx ]) {
1267
+ sec_slot_assignmnet [idx ] = SEC_SLOT_TOUCHED ;
1268
+ }
1269
+ }
1270
+
1271
+ static inline void sec_slot_mark_assigned (struct boot_loader_state * state )
1272
+ {
1273
+ uint8_t idx = (SEC_SLOT_PHYSICAL_CNT == 1 ) ? 0 : BOOT_CURR_IMG (state );
1274
+
1275
+ sec_slot_assignmnet [idx ] = SEC_SLOT_ASSIGNED ;
1276
+ }
1277
+
1278
+ /**
1279
+ * Cleanu up all secondary slot which couldn't be assigned to any primary slot.
1280
+ *
1281
+ * This function erases content of each secondary slot which contains valid
1282
+ * header but couldn't be assigned to any of supported primary images.
1283
+ *
1284
+ * This function is supposed to be called after boot_validated_swap_type()
1285
+ * iterates over all the images in context_boot_go().
1286
+ */
1287
+ static void sec_slot_cleanup_if_unusable (void )
1288
+ {
1289
+ uint8_t idx ;
1290
+
1291
+ for (idx = 0 ; idx < SEC_SLOT_PHYSICAL_CNT ; idx ++ ) {
1292
+ if (SEC_SLOT_TOUCHED == sec_slot_assignmnet [idx ]) {
1293
+ const struct flash_area * secondary_fa ;
1294
+ int rc ;
1295
+
1296
+ rc = flash_area_open (flash_area_id_from_multi_image_slot (idx , BOOT_SECONDARY_SLOT ),
1297
+ & secondary_fa );
1298
+ if (!rc ) {
1299
+ rc = flash_area_erase (secondary_fa , 0 , secondary_fa -> fa_size );
1300
+ if (!rc ) {
1301
+ BOOT_LOG_ERR ("Cleaned-up secondary slot of %d. image." , idx );
1302
+ }
1303
+ }
1304
+
1305
+ if (rc ) {
1306
+ BOOT_LOG_ERR ("Can not cleanup secondary slot of %d. image." , idx );
1307
+ }
1308
+ }
1309
+ }
1310
+ }
1311
+ #else
1312
+ static inline void sec_slot_touch (struct boot_loader_state * state )
1313
+ {
1314
+ }
1315
+ static inline void sec_slot_mark_assigned (struct boot_loader_state * state )
1316
+ {
1317
+ }
1318
+ static inline void sec_slot_cleanup_if_unusable (void )
1319
+ {
1320
+ }
1321
+ #endif /* defined(CONFIG_MCUBOOT_CLEANUP_UNUSABLE_SECONDARY) &&\
1322
+ defined(PM_S1_ADDRESS) || defined(CONFIG_SOC_NRF5340_CPUAPP) */
1323
+
1243
1324
#if !defined(MCUBOOT_DIRECT_XIP ) && !defined(MCUBOOT_RAM_LOAD )
1244
1325
/**
1245
1326
* Determines which swap operation to perform, if any. If it is determined
@@ -1278,6 +1359,9 @@ boot_validated_swap_type(struct boot_loader_state *state,
1278
1359
if (rc != 0 ) {
1279
1360
return BOOT_SWAP_TYPE_FAIL ;
1280
1361
}
1362
+
1363
+ sec_slot_touch (state );
1364
+
1281
1365
#ifdef PM_S1_ADDRESS
1282
1366
#ifdef PM_CPUNET_B0N_ADDRESS
1283
1367
if (reset_addr < PM_CPUNET_B0N_ADDRESS )
@@ -1312,6 +1396,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
1312
1396
}
1313
1397
#else
1314
1398
return BOOT_SWAP_TYPE_NONE ;
1399
+
1315
1400
#endif
1316
1401
1317
1402
} else if (reset_addr > (primary_fa -> fa_off + primary_fa -> fa_size )) {
@@ -1320,7 +1405,9 @@ boot_validated_swap_type(struct boot_loader_state *state,
1320
1405
}
1321
1406
}
1322
1407
#endif /* PM_S1_ADDRESS */
1408
+ sec_slot_mark_assigned (state );
1323
1409
}
1410
+
1324
1411
#endif /* PM_S1_ADDRESS || CONFIG_SOC_NRF5340_CPUAPP */
1325
1412
1326
1413
swap_type = boot_swap_type_multi (BOOT_CURR_IMG (state ));
@@ -2450,6 +2537,9 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2450
2537
}
2451
2538
}
2452
2539
2540
+ /* cleanup secondary slots which were recognized unusable*/
2541
+ sec_slot_cleanup_if_unusable ();
2542
+
2453
2543
#if (BOOT_IMAGE_NUMBER > 1 )
2454
2544
if (has_upgrade ) {
2455
2545
/* Iterate over all the images and verify whether the image dependencies
0 commit comments