@@ -153,15 +153,15 @@ boot_read_image_headers(struct boot_loader_state *state, bool require_all,
153
153
*
154
154
* Failure to read any headers is a fatal error.
155
155
*/
156
- #ifdef PM_S1_ADDRESS
156
+ #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
157
157
/* Patch needed for NCS. The primary slot of the second image
158
158
* (image 1) will not contain a valid image header until an upgrade
159
159
* of mcuboot has happened (filling S1 with the new version).
160
160
*/
161
- if (BOOT_CURR_IMG (state ) == 1 && i == 0 ) {
161
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER && i == 0 ) {
162
162
continue ;
163
163
}
164
- #endif /* PM_S1_ADDRESS */
164
+ #endif /* CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1 */
165
165
if (i > 0 && !require_all ) {
166
166
return 0 ;
167
167
} else {
@@ -1160,7 +1160,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
1160
1160
1161
1161
#if defined(CONFIG_SOC_NRF5340_CPUAPP ) && defined(CONFIG_NRF53_MULTI_IMAGE_UPDATE ) \
1162
1162
&& defined(CONFIG_PCD_APP ) && defined(CONFIG_PCD_READ_NETCORE_APP_VERSION )
1163
- if (BOOT_CURR_IMG (state ) == 1 ) {
1163
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER ) {
1164
1164
rc = pcd_version_cmp_net (fap , boot_img_hdr (state , BOOT_SECONDARY_SLOT ));
1165
1165
} else {
1166
1166
rc = boot_version_cmp (
@@ -1229,36 +1229,55 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
1229
1229
struct image_header * secondary_hdr = boot_img_hdr (state , slot );
1230
1230
uint32_t reset_value = 0 ;
1231
1231
uint32_t reset_addr = secondary_hdr -> ih_hdr_size + sizeof (reset_value );
1232
+ uint32_t min_addr , max_addr ;
1233
+ bool check_addresses = false;
1232
1234
1233
1235
rc = flash_area_read (fap , reset_addr , & reset_value , sizeof (reset_value ));
1234
1236
if (rc != 0 ) {
1235
1237
fih_rc = FIH_NO_BOOTABLE_IMAGE ;
1236
1238
goto out ;
1237
1239
}
1238
1240
1239
- uint32_t min_addr , max_addr ;
1240
-
1241
1241
#ifdef PM_CPUNET_APP_ADDRESS
1242
1242
/* The primary slot for the network core is emulated in RAM.
1243
1243
* Its flash_area hasn't got relevant boundaries.
1244
1244
* Therfore need to override its boundaries for the check.
1245
1245
*/
1246
- if (BOOT_CURR_IMG (state ) == 1 ) {
1246
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER ) {
1247
1247
min_addr = PM_CPUNET_APP_ADDRESS ;
1248
1248
max_addr = PM_CPUNET_APP_ADDRESS + PM_CPUNET_APP_SIZE ;
1249
- #ifdef PM_S1_ADDRESS
1250
- } else if (BOOT_CURR_IMG (state ) == 0 ) {
1249
+ check_addresses = true;
1250
+ } else
1251
+ #endif
1252
+ #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1253
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ) {
1254
+ #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1251
1255
min_addr = PM_S0_ADDRESS ;
1252
- max_addr = pri_fa -> fa_off + pri_fa -> fa_size ;
1256
+ max_addr = (PM_S0_ADDRESS + PM_S0_SIZE );
1257
+ #else
1258
+ min_addr = PM_S1_ADDRESS ;
1259
+ max_addr = (PM_S1_ADDRESS + PM_S1_SIZE );
1253
1260
#endif
1261
+ check_addresses = true;
1254
1262
} else
1255
1263
#endif
1256
- {
1264
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
1265
+ #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1266
+ #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1267
+ min_addr = MIN (pri_fa -> fa_off , PM_S0_ADDRESS );
1268
+ max_addr = MAX ((pri_fa -> fa_off + pri_fa -> fa_size ), (PM_S0_ADDRESS + PM_S0_SIZE ));
1269
+ #else
1270
+ min_addr = MIN (pri_fa -> fa_off , PM_S1_ADDRESS );
1271
+ max_addr = MAX ((pri_fa -> fa_off + pri_fa -> fa_size ), (PM_S1_ADDRESS + PM_S1_SIZE ));
1272
+ #endif
1273
+ #else
1257
1274
min_addr = pri_fa -> fa_off ;
1258
1275
max_addr = pri_fa -> fa_off + pri_fa -> fa_size ;
1276
+ #endif
1277
+ check_addresses = true;
1259
1278
}
1260
1279
1261
- if (reset_value < min_addr || reset_value > ( max_addr )) {
1280
+ if (check_addresses == true && ( reset_value < min_addr || reset_value > max_addr )) {
1262
1281
BOOT_LOG_ERR ("Reset address of image in secondary slot is not in the primary slot" );
1263
1282
BOOT_LOG_ERR ("Erasing image from secondary slot" );
1264
1283
@@ -1335,36 +1354,42 @@ boot_update_security_counter(uint8_t image_index, int slot,
1335
1354
#define SEC_SLOT_TOUCHED 1
1336
1355
#define SEC_SLOT_ASSIGNED 2
1337
1356
1338
- #if (MCUBOOT_IMAGE_NUMBER == 2 ) && defined(PM_B0_ADDRESS ) && \
1339
- !defined(CONFIG_NRF53_MULTI_IMAGE_UPDATE )
1340
- /* This configuration is peculiar - the one physical secondary slot is
1341
- * mocking two logical secondary
1342
- */
1343
- #define SEC_SLOT_PHYSICAL_CNT 1
1344
- #else
1345
- #define SEC_SLOT_PHYSICAL_CNT MCUBOOT_IMAGE_NUMBER
1346
- #endif
1347
-
1348
- static uint8_t sec_slot_assignmnet [SEC_SLOT_PHYSICAL_CNT ] = {0 };
1357
+ static uint8_t sec_slot_assignmnet [MCUBOOT_IMAGE_NUMBER ] = {0 };
1349
1358
1350
1359
static inline void sec_slot_touch (struct boot_loader_state * state )
1351
1360
{
1352
- uint8_t idx = (SEC_SLOT_PHYSICAL_CNT == 1 ) ? 0 : BOOT_CURR_IMG (state );
1361
+ #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1362
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ) {
1363
+ if (sec_slot_assignmnet [CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ] == SEC_SLOT_VIRGIN ) {
1364
+ sec_slot_assignmnet [CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ] = SEC_SLOT_TOUCHED ;
1365
+ }
1366
+ } else if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
1367
+ if (sec_slot_assignmnet [CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ] == SEC_SLOT_VIRGIN ) {
1368
+ sec_slot_assignmnet [CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ] = SEC_SLOT_TOUCHED ;
1369
+ }
1370
+ }
1371
+ #endif
1353
1372
1354
- if (SEC_SLOT_VIRGIN == sec_slot_assignmnet [ idx ] ) {
1355
- sec_slot_assignmnet [idx ] = SEC_SLOT_TOUCHED ;
1373
+ if (sec_slot_assignmnet [ BOOT_CURR_IMG ( state )] == SEC_SLOT_VIRGIN ) {
1374
+ sec_slot_assignmnet [BOOT_CURR_IMG ( state ) ] = SEC_SLOT_TOUCHED ;
1356
1375
}
1357
1376
}
1358
1377
1359
1378
static inline void sec_slot_mark_assigned (struct boot_loader_state * state )
1360
1379
{
1361
- uint8_t idx = (SEC_SLOT_PHYSICAL_CNT == 1 ) ? 0 : BOOT_CURR_IMG (state );
1380
+ #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1381
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ) {
1382
+ sec_slot_assignmnet [CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ] = SEC_SLOT_ASSIGNED ;
1383
+ } else if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
1384
+ sec_slot_assignmnet [CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ] = SEC_SLOT_ASSIGNED ;
1385
+ }
1386
+ #endif
1362
1387
1363
- sec_slot_assignmnet [idx ] = SEC_SLOT_ASSIGNED ;
1388
+ sec_slot_assignmnet [BOOT_CURR_IMG ( state ) ] = SEC_SLOT_ASSIGNED ;
1364
1389
}
1365
1390
1366
1391
/**
1367
- * Cleanu up all secondary slot which couldn't be assigned to any primary slot.
1392
+ * Cleanup up all secondary slot which couldn't be assigned to any primary slot.
1368
1393
*
1369
1394
* This function erases content of each secondary slot which contains valid
1370
1395
* header but couldn't be assigned to any of supported primary images.
@@ -1376,7 +1401,7 @@ static void sec_slot_cleanup_if_unusable(void)
1376
1401
{
1377
1402
uint8_t idx ;
1378
1403
1379
- for (idx = 0 ; idx < SEC_SLOT_PHYSICAL_CNT ; idx ++ ) {
1404
+ for (idx = 0 ; idx < MCUBOOT_IMAGE_NUMBER ; idx ++ ) {
1380
1405
if (SEC_SLOT_TOUCHED == sec_slot_assignmnet [idx ]) {
1381
1406
const struct flash_area * secondary_fa ;
1382
1407
int rc ;
@@ -1386,12 +1411,12 @@ static void sec_slot_cleanup_if_unusable(void)
1386
1411
if (!rc ) {
1387
1412
rc = flash_area_erase (secondary_fa , 0 , secondary_fa -> fa_size );
1388
1413
if (!rc ) {
1389
- BOOT_LOG_ERR ("Cleaned-up secondary slot of %d. image. " , idx );
1414
+ BOOT_LOG_ERR ("Cleaned-up secondary slot of image %d " , idx );
1390
1415
}
1391
1416
}
1392
1417
1393
1418
if (rc ) {
1394
- BOOT_LOG_ERR ("Can not cleanup secondary slot of %d. image. " , idx );
1419
+ BOOT_LOG_ERR ("Failed to clean-up secondary slot of image %d: %d " , idx , rc );
1395
1420
}
1396
1421
}
1397
1422
}
@@ -1428,7 +1453,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
1428
1453
owner_nsib [BOOT_CURR_IMG (state )] = false;
1429
1454
#endif
1430
1455
1431
- #if defined(PM_S1_ADDRESS ) || defined(CONFIG_SOC_NRF5340_CPUAPP )
1456
+ #if defined(PM_S1_ADDRESS ) || defined(PM_CPUNET_B0N_ADDRESS )
1432
1457
const struct flash_area * secondary_fa =
1433
1458
BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT );
1434
1459
struct image_header * hdr = boot_img_hdr (state , BOOT_SECONDARY_SLOT );
@@ -1466,31 +1491,29 @@ boot_validated_swap_type(struct boot_loader_state *state,
1466
1491
}
1467
1492
1468
1493
/* Check start and end of primary slot for current image */
1469
- if (reset_addr < primary_fa -> fa_off ) {
1470
- #if defined(CONFIG_SOC_NRF5340_CPUAPP ) && defined(CONFIG_NRF53_MULTI_IMAGE_UPDATE )
1471
- const struct flash_area * nsib_fa ;
1472
-
1473
- /* NSIB upgrade slot */
1474
- rc = flash_area_open ((uint32_t )_image_1_primary_slot_id ,
1475
- & nsib_fa );
1476
-
1477
- if (rc != 0 ) {
1478
- return BOOT_SWAP_TYPE_FAIL ;
1479
- }
1480
-
1481
- /* Image is placed before Primary and within the NSIB slot */
1482
- if (reset_addr > nsib_fa -> fa_off
1483
- && reset_addr < (nsib_fa -> fa_off + nsib_fa -> fa_size )) {
1484
- /* Set primary to be NSIB upgrade slot */
1485
- BOOT_IMG_AREA (state , 0 ) = nsib_fa ;
1486
- owner_nsib [BOOT_CURR_IMG (state )] = true;
1487
- }
1494
+ #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1495
+ if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE )) {
1488
1496
#else
1489
- return BOOT_SWAP_TYPE_NONE ;
1490
-
1497
+ if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE )) {
1491
1498
#endif
1499
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
1500
+ /* This is not the s0/s1 upgrade image but the application image, pretend
1501
+ * there is no image so the NSIB update can be loaded
1502
+ */
1503
+ return BOOT_SWAP_TYPE_NONE ;
1504
+ }
1492
1505
1493
- } else if (reset_addr > (primary_fa -> fa_off + primary_fa -> fa_size )) {
1506
+ owner_nsib [BOOT_CURR_IMG (state )] = true;
1507
+ #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1508
+ } else if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE )) {
1509
+ #else
1510
+ } else if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE )) {
1511
+ #endif
1512
+ /* NSIB upgrade but for the wrong slot, must be erased */
1513
+ BOOT_LOG_ERR ("Image in slot is for wrong s0/s1 image" );
1514
+ flash_area_erase (secondary_fa , 0 , secondary_fa -> fa_size );
1515
+ return BOOT_SWAP_TYPE_FAIL ;
1516
+ } else if (reset_addr < primary_fa -> fa_off || reset_addr > (primary_fa -> fa_off + primary_fa -> fa_size )) {
1494
1517
/* The image in the secondary slot is not intended for any */
1495
1518
return BOOT_SWAP_TYPE_NONE ;
1496
1519
}
@@ -1503,7 +1526,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
1503
1526
sec_slot_mark_assigned (state );
1504
1527
}
1505
1528
1506
- #endif /* PM_S1_ADDRESS || CONFIG_SOC_NRF5340_CPUAPP */
1529
+ #endif /* PM_S1_ADDRESS || PM_CPUNET_B0N_ADDRESS */
1507
1530
1508
1531
swap_type = boot_swap_type_multi (BOOT_CURR_IMG (state ));
1509
1532
if (BOOT_IS_UPGRADE (swap_type )) {
@@ -2035,7 +2058,22 @@ boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
2035
2058
flash_area_close (fap );
2036
2059
}
2037
2060
2038
- swap_run (state , bs , copy_size );
2061
+ #if defined(PM_S1_ADDRESS ) && CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
2062
+ if (owner_nsib [BOOT_CURR_IMG (state )]) {
2063
+ if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ) {
2064
+ /* For NSIB, move the image instead of swapping it */
2065
+ nsib_swap_run (state , bs );
2066
+
2067
+ #if defined(CONFIG_REBOOT )
2068
+ /* Should also reboot at this point so the new S0/S1 update is applied */
2069
+ sys_reboot (SYS_REBOOT_COLD );
2070
+ #endif
2071
+ }
2072
+ } else
2073
+ #endif
2074
+ {
2075
+ swap_run (state , bs , copy_size );
2076
+ }
2039
2077
2040
2078
#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
2041
2079
extern int boot_status_fails ;
@@ -2701,12 +2739,6 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2701
2739
rc = boot_perform_update (state , & bs );
2702
2740
}
2703
2741
assert (rc == 0 );
2704
- #if defined(PM_S1_ADDRESS ) && defined(CONFIG_REBOOT )
2705
- if (owner_nsib [BOOT_CURR_IMG (state )]) {
2706
- sys_reboot (SYS_REBOOT_COLD );
2707
-
2708
- }
2709
- #endif
2710
2742
break ;
2711
2743
2712
2744
case BOOT_SWAP_TYPE_FAIL :
@@ -2780,7 +2812,8 @@ context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2780
2812
* executing MCUBoot image, and is therefore already validated by NSIB and
2781
2813
* does not need to also be validated by MCUBoot.
2782
2814
*/
2783
- bool image_validated_by_nsib = BOOT_CURR_IMG (state ) == 1 ;
2815
+ bool image_validated_by_nsib = BOOT_CURR_IMG (state ) ==
2816
+ CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ;
2784
2817
if (!image_validated_by_nsib )
2785
2818
#endif
2786
2819
{
0 commit comments