@@ -160,15 +160,15 @@ boot_read_image_headers(struct boot_loader_state *state, bool require_all,
160
160
*
161
161
* Failure to read any headers is a fatal error.
162
162
*/
163
- #ifdef PM_S1_ADDRESS
163
+ #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
164
164
/* Patch needed for NCS. The primary slot of the second image
165
165
* (image 1) will not contain a valid image header until an upgrade
166
166
* of mcuboot has happened (filling S1 with the new version).
167
167
*/
168
- if (BOOT_CURR_IMG (state ) == 1 && i == 0 ) {
168
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER && i == 0 ) {
169
169
continue ;
170
170
}
171
- #endif /* PM_S1_ADDRESS */
171
+ #endif /* CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1 */
172
172
if (i > 0 && !require_all ) {
173
173
return 0 ;
174
174
} else {
@@ -1176,7 +1176,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
1176
1176
1177
1177
#if defined(CONFIG_SOC_NRF5340_CPUAPP ) && defined(CONFIG_NRF53_MULTI_IMAGE_UPDATE ) \
1178
1178
&& defined(CONFIG_PCD_APP ) && defined(CONFIG_PCD_READ_NETCORE_APP_VERSION )
1179
- if (BOOT_CURR_IMG (state ) == 1 ) {
1179
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER ) {
1180
1180
rc = pcd_version_cmp_net (fap , boot_img_hdr (state , BOOT_SECONDARY_SLOT ));
1181
1181
} else {
1182
1182
rc = boot_version_cmp (
@@ -1247,35 +1247,54 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
1247
1247
struct image_header * secondary_hdr = boot_img_hdr (state , slot );
1248
1248
uint32_t reset_value = 0 ;
1249
1249
uint32_t reset_addr = secondary_hdr -> ih_hdr_size + sizeof (reset_value );
1250
+ uint32_t min_addr , max_addr ;
1251
+ bool check_addresses = false;
1250
1252
1251
1253
if (flash_area_read (fap , reset_addr , & reset_value , sizeof (reset_value )) != 0 ) {
1252
1254
fih_rc = FIH_NO_BOOTABLE_IMAGE ;
1253
1255
goto out ;
1254
1256
}
1255
1257
1256
- uint32_t min_addr , max_addr ;
1257
-
1258
1258
#ifdef PM_CPUNET_APP_ADDRESS
1259
1259
/* The primary slot for the network core is emulated in RAM.
1260
1260
* Its flash_area hasn't got relevant boundaries.
1261
1261
* Therfore need to override its boundaries for the check.
1262
1262
*/
1263
- if (BOOT_CURR_IMG (state ) == 1 ) {
1263
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER ) {
1264
1264
min_addr = PM_CPUNET_APP_ADDRESS ;
1265
1265
max_addr = PM_CPUNET_APP_ADDRESS + PM_CPUNET_APP_SIZE ;
1266
- #ifdef PM_S1_ADDRESS
1267
- } else if (BOOT_CURR_IMG (state ) == 0 ) {
1266
+ check_addresses = true;
1267
+ } else
1268
+ #endif
1269
+ #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1270
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ) {
1271
+ #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1268
1272
min_addr = PM_S0_ADDRESS ;
1269
- max_addr = pri_fa -> fa_off + pri_fa -> fa_size ;
1273
+ max_addr = (PM_S0_ADDRESS + PM_S0_SIZE );
1274
+ #else
1275
+ min_addr = PM_S1_ADDRESS ;
1276
+ max_addr = (PM_S1_ADDRESS + PM_S1_SIZE );
1270
1277
#endif
1278
+ check_addresses = true;
1271
1279
} else
1272
1280
#endif
1273
- {
1281
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
1282
+ #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1283
+ #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1284
+ min_addr = MIN (pri_fa -> fa_off , PM_S0_ADDRESS );
1285
+ max_addr = MAX ((pri_fa -> fa_off + pri_fa -> fa_size ), (PM_S0_ADDRESS + PM_S0_SIZE ));
1286
+ #else
1287
+ min_addr = MIN (pri_fa -> fa_off , PM_S1_ADDRESS );
1288
+ max_addr = MAX ((pri_fa -> fa_off + pri_fa -> fa_size ), (PM_S1_ADDRESS + PM_S1_SIZE ));
1289
+ #endif
1290
+ #else
1274
1291
min_addr = pri_fa -> fa_off ;
1275
1292
max_addr = pri_fa -> fa_off + pri_fa -> fa_size ;
1293
+ #endif
1294
+ check_addresses = true;
1276
1295
}
1277
1296
1278
- if (reset_value < min_addr || reset_value > ( max_addr )) {
1297
+ if (check_addresses == true && ( reset_value < min_addr || reset_value > max_addr )) {
1279
1298
BOOT_LOG_ERR ("Reset address of image in secondary slot is not in the primary slot" );
1280
1299
BOOT_LOG_ERR ("Erasing image from secondary slot" );
1281
1300
@@ -1397,36 +1416,54 @@ boot_update_security_counter(struct boot_loader_state *state, int slot, int hdr_
1397
1416
#define SEC_SLOT_TOUCHED 1
1398
1417
#define SEC_SLOT_ASSIGNED 2
1399
1418
1400
- #if (MCUBOOT_IMAGE_NUMBER == 2 ) && defined(PM_B0_ADDRESS ) && \
1401
- !defined(CONFIG_NRF53_MULTI_IMAGE_UPDATE )
1402
- /* This configuration is peculiar - the one physical secondary slot is
1403
- * mocking two logical secondary
1404
- */
1405
- #define SEC_SLOT_PHYSICAL_CNT 1
1419
+ static uint8_t sec_slot_assignment [MCUBOOT_IMAGE_NUMBER ] = {0 };
1420
+
1421
+ #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1422
+ static inline void sec_slot_untouch (struct boot_loader_state * state )
1423
+ {
1424
+ sec_slot_assignment [CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ] = SEC_SLOT_VIRGIN ;
1425
+ sec_slot_assignment [CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ] = SEC_SLOT_VIRGIN ;
1426
+ }
1406
1427
#else
1407
- #define SEC_SLOT_PHYSICAL_CNT MCUBOOT_IMAGE_NUMBER
1428
+ static inline void sec_slot_untouch (struct boot_loader_state * state )
1429
+ {
1430
+ }
1408
1431
#endif
1409
1432
1410
- static uint8_t sec_slot_assignmnet [SEC_SLOT_PHYSICAL_CNT ] = {0 };
1411
-
1412
1433
static inline void sec_slot_touch (struct boot_loader_state * state )
1413
1434
{
1414
- uint8_t idx = (SEC_SLOT_PHYSICAL_CNT == 1 ) ? 0 : BOOT_CURR_IMG (state );
1435
+ #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1436
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ) {
1437
+ if (sec_slot_assignment [CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ] == SEC_SLOT_VIRGIN ) {
1438
+ sec_slot_assignment [CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ] = SEC_SLOT_TOUCHED ;
1439
+ }
1440
+ } else if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
1441
+ if (sec_slot_assignment [CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ] == SEC_SLOT_VIRGIN ) {
1442
+ sec_slot_assignment [CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ] = SEC_SLOT_TOUCHED ;
1443
+ }
1444
+ }
1445
+ #endif
1415
1446
1416
- if (SEC_SLOT_VIRGIN == sec_slot_assignmnet [ idx ] ) {
1417
- sec_slot_assignmnet [ idx ] = SEC_SLOT_TOUCHED ;
1447
+ if (sec_slot_assignment [ BOOT_CURR_IMG ( state )] == SEC_SLOT_VIRGIN ) {
1448
+ sec_slot_assignment [ BOOT_CURR_IMG ( state ) ] = SEC_SLOT_TOUCHED ;
1418
1449
}
1419
1450
}
1420
1451
1421
1452
static inline void sec_slot_mark_assigned (struct boot_loader_state * state )
1422
1453
{
1423
- uint8_t idx = (SEC_SLOT_PHYSICAL_CNT == 1 ) ? 0 : BOOT_CURR_IMG (state );
1454
+ #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1455
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ) {
1456
+ sec_slot_assignment [CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ] = SEC_SLOT_ASSIGNED ;
1457
+ } else if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
1458
+ sec_slot_assignment [CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ] = SEC_SLOT_ASSIGNED ;
1459
+ }
1460
+ #endif
1424
1461
1425
- sec_slot_assignmnet [ idx ] = SEC_SLOT_ASSIGNED ;
1462
+ sec_slot_assignment [ BOOT_CURR_IMG ( state ) ] = SEC_SLOT_ASSIGNED ;
1426
1463
}
1427
1464
1428
1465
/**
1429
- * Cleanu up all secondary slot which couldn't be assigned to any primary slot.
1466
+ * Cleanup up all secondary slot which couldn't be assigned to any primary slot.
1430
1467
*
1431
1468
* This function erases content of each secondary slot which contains valid
1432
1469
* header but couldn't be assigned to any of supported primary images.
@@ -1438,8 +1475,8 @@ static void sec_slot_cleanup_if_unusable(void)
1438
1475
{
1439
1476
uint8_t idx ;
1440
1477
1441
- for (idx = 0 ; idx < SEC_SLOT_PHYSICAL_CNT ; idx ++ ) {
1442
- if (SEC_SLOT_TOUCHED == sec_slot_assignmnet [idx ]) {
1478
+ for (idx = 0 ; idx < MCUBOOT_IMAGE_NUMBER ; idx ++ ) {
1479
+ if (SEC_SLOT_TOUCHED == sec_slot_assignment [idx ]) {
1443
1480
const struct flash_area * secondary_fa ;
1444
1481
int rc ;
1445
1482
@@ -1448,17 +1485,20 @@ static void sec_slot_cleanup_if_unusable(void)
1448
1485
if (!rc ) {
1449
1486
rc = flash_area_erase (secondary_fa , 0 , secondary_fa -> fa_size );
1450
1487
if (!rc ) {
1451
- BOOT_LOG_ERR ("Cleaned-up secondary slot of %d. image. " , idx );
1488
+ BOOT_LOG_ERR ("Cleaned-up secondary slot of image %d " , idx );
1452
1489
}
1453
1490
}
1454
1491
1455
1492
if (rc ) {
1456
- BOOT_LOG_ERR ("Can not cleanup secondary slot of %d. image. " , idx );
1493
+ BOOT_LOG_ERR ("Failed to clean-up secondary slot of image %d: %d " , idx , rc );
1457
1494
}
1458
1495
}
1459
1496
}
1460
1497
}
1461
1498
#else
1499
+ static inline void sec_slot_untouch (struct boot_loader_state * state )
1500
+ {
1501
+ }
1462
1502
static inline void sec_slot_touch (struct boot_loader_state * state )
1463
1503
{
1464
1504
}
@@ -1490,7 +1530,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
1490
1530
owner_nsib [BOOT_CURR_IMG (state )] = false;
1491
1531
#endif
1492
1532
1493
- #if defined(PM_S1_ADDRESS ) || defined(CONFIG_SOC_NRF5340_CPUAPP )
1533
+ #if defined(PM_S1_ADDRESS ) || defined(PM_CPUNET_B0N_ADDRESS )
1494
1534
const struct flash_area * secondary_fa =
1495
1535
BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT );
1496
1536
struct image_header * hdr = boot_img_hdr (state , BOOT_SECONDARY_SLOT );
@@ -1528,31 +1568,31 @@ boot_validated_swap_type(struct boot_loader_state *state,
1528
1568
}
1529
1569
1530
1570
/* Check start and end of primary slot for current image */
1531
- if (reset_addr < primary_fa -> fa_off ) {
1532
- #if defined(CONFIG_SOC_NRF5340_CPUAPP ) && defined(CONFIG_NRF53_MULTI_IMAGE_UPDATE )
1533
- const struct flash_area * nsib_fa ;
1534
-
1535
- /* NSIB upgrade slot */
1536
- rc = flash_area_open ((uint32_t )_image_1_primary_slot_id ,
1537
- & nsib_fa );
1538
-
1539
- if (rc != 0 ) {
1540
- return BOOT_SWAP_TYPE_FAIL ;
1541
- }
1542
-
1543
- /* Image is placed before Primary and within the NSIB slot */
1544
- if (reset_addr > nsib_fa -> fa_off
1545
- && reset_addr < (nsib_fa -> fa_off + nsib_fa -> fa_size )) {
1546
- /* Set primary to be NSIB upgrade slot */
1547
- BOOT_IMG_AREA (state , 0 ) = nsib_fa ;
1548
- owner_nsib [BOOT_CURR_IMG (state )] = true;
1549
- }
1571
+ #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1572
+ if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE )) {
1550
1573
#else
1551
- return BOOT_SWAP_TYPE_NONE ;
1552
-
1574
+ if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE )) {
1553
1575
#endif
1576
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
1577
+ /* This is not the s0/s1 upgrade image but the application image, pretend
1578
+ * there is no image so the NSIB update can be loaded
1579
+ */
1580
+ return BOOT_SWAP_TYPE_NONE ;
1581
+ }
1554
1582
1555
- } else if (reset_addr > (primary_fa -> fa_off + primary_fa -> fa_size )) {
1583
+ owner_nsib [BOOT_CURR_IMG (state )] = true;
1584
+ #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1585
+ } else if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE )) {
1586
+ #else
1587
+ } else if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE )) {
1588
+ #endif
1589
+ /* NSIB upgrade but for the wrong slot, must be erased */
1590
+ BOOT_LOG_ERR ("Image in slot is for wrong s0/s1 image" );
1591
+ flash_area_erase (secondary_fa , 0 , secondary_fa -> fa_size );
1592
+ sec_slot_untouch (state );
1593
+ BOOT_LOG_ERR ("Cleaned-up secondary slot of image %d" , BOOT_CURR_IMG (state ));
1594
+ return BOOT_SWAP_TYPE_FAIL ;
1595
+ } else if (reset_addr < primary_fa -> fa_off || reset_addr > (primary_fa -> fa_off + primary_fa -> fa_size )) {
1556
1596
/* The image in the secondary slot is not intended for any */
1557
1597
return BOOT_SWAP_TYPE_NONE ;
1558
1598
}
@@ -1565,7 +1605,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
1565
1605
sec_slot_mark_assigned (state );
1566
1606
}
1567
1607
1568
- #endif /* PM_S1_ADDRESS || CONFIG_SOC_NRF5340_CPUAPP */
1608
+ #endif /* PM_S1_ADDRESS || PM_CPUNET_B0N_ADDRESS */
1569
1609
1570
1610
swap_type = boot_swap_type_multi (BOOT_CURR_IMG (state ));
1571
1611
if (BOOT_IS_UPGRADE (swap_type )) {
@@ -2237,7 +2277,22 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
2237
2277
flash_area_close (fap );
2238
2278
}
2239
2279
2240
- swap_run (state , bs , copy_size );
2280
+ #if defined(PM_S1_ADDRESS ) && CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
2281
+ if (owner_nsib [BOOT_CURR_IMG (state )]) {
2282
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ) {
2283
+ /* For NSIB, move the image instead of swapping it */
2284
+ nsib_swap_run (state , bs );
2285
+
2286
+ #if defined(CONFIG_REBOOT )
2287
+ /* Should also reboot at this point so the new S0/S1 update is applied */
2288
+ sys_reboot (SYS_REBOOT_COLD );
2289
+ #endif
2290
+ }
2291
+ } else
2292
+ #endif
2293
+ {
2294
+ swap_run (state , bs , copy_size );
2295
+ }
2241
2296
2242
2297
#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
2243
2298
extern int boot_status_fails ;
@@ -2931,12 +2986,6 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2931
2986
rc = boot_perform_update (state , & bs );
2932
2987
}
2933
2988
assert (rc == 0 );
2934
- #if defined(PM_S1_ADDRESS ) && defined(CONFIG_REBOOT )
2935
- if (owner_nsib [BOOT_CURR_IMG (state )]) {
2936
- sys_reboot (SYS_REBOOT_COLD );
2937
-
2938
- }
2939
- #endif
2940
2989
break ;
2941
2990
2942
2991
case BOOT_SWAP_TYPE_FAIL :
@@ -3004,13 +3053,17 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3004
3053
*/
3005
3054
}
3006
3055
3007
- #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
3008
3056
#ifdef PM_S1_ADDRESS
3009
3057
/* Patch needed for NCS. Image 1 primary is the currently
3010
3058
* executing MCUBoot image, and is therefore already validated by NSIB and
3011
3059
* does not need to also be validated by MCUBoot.
3012
3060
*/
3013
- bool image_validated_by_nsib = BOOT_CURR_IMG (state ) == 1 ;
3061
+ bool image_validated_by_nsib = BOOT_CURR_IMG (state ) ==
3062
+ CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ;
3063
+ #endif
3064
+
3065
+ #ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
3066
+ #ifdef PM_S1_ADDRESS
3014
3067
if (!image_validated_by_nsib )
3015
3068
#endif
3016
3069
{
0 commit comments