Skip to content

Commit 3375120

Browse files
nvlsianpubjarki-andreasen
authored andcommitted
[nrf noup] loader: introduced cleanup of unusable secondary slot
Added procedure which clean-up content of all the secondary slot which contains valid header but couldn't be assigned to any of supported primary images. This behavior is needed when configuration allows to use one secondary slot for collecting image for multiple primary slots. Signed-off-by: Andrzej Puzdrowski <[email protected]> (cherry picked from commit 8f4b472)
1 parent 5e5529c commit 3375120

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

boot/bootutil/src/loader.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,87 @@ boot_update_security_counter(uint8_t image_index, int slot,
12401240
}
12411241
#endif /* MCUBOOT_HW_ROLLBACK_PROT */
12421242

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+
12431324
#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
12441325
/**
12451326
* 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,
12781359
if (rc != 0) {
12791360
return BOOT_SWAP_TYPE_FAIL;
12801361
}
1362+
1363+
sec_slot_touch(state);
1364+
12811365
#ifdef PM_S1_ADDRESS
12821366
#ifdef PM_CPUNET_B0N_ADDRESS
12831367
if(reset_addr < PM_CPUNET_B0N_ADDRESS)
@@ -1312,6 +1396,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
13121396
}
13131397
#else
13141398
return BOOT_SWAP_TYPE_NONE;
1399+
13151400
#endif
13161401

13171402
} 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,
13201405
}
13211406
}
13221407
#endif /* PM_S1_ADDRESS */
1408+
sec_slot_mark_assigned(state);
13231409
}
1410+
13241411
#endif /* PM_S1_ADDRESS || CONFIG_SOC_NRF5340_CPUAPP */
13251412

13261413
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)
24502537
}
24512538
}
24522539

2540+
/* cleanup secondary slots which were recognized unusable*/
2541+
sec_slot_cleanup_if_unusable();
2542+
24532543
#if (BOOT_IMAGE_NUMBER > 1)
24542544
if (has_upgrade) {
24552545
/* Iterate over all the images and verify whether the image dependencies

0 commit comments

Comments
 (0)