@@ -1249,15 +1249,19 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
1249
1249
if (fap == BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT )) {
1250
1250
const struct flash_area * pri_fa = BOOT_IMG_AREA (state , BOOT_PRIMARY_SLOT );
1251
1251
struct image_header * secondary_hdr = boot_img_hdr (state , slot );
1252
- uint32_t reset_value = 0 ;
1253
- uint32_t reset_addr = secondary_hdr -> ih_hdr_size + sizeof (reset_value );
1252
+ uint32_t internal_img_addr = 0 ; /* either the reset handler addres or the image beginning addres */
1254
1253
uint32_t min_addr , max_addr ;
1255
1254
bool check_addresses = false;
1256
1255
1257
- if (flash_area_read (fap , reset_addr , & reset_value , sizeof (reset_value )) != 0 ) {
1256
+ #ifdef CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDR
1257
+ internal_img_addr = secondary_hdr -> ih_load_addr ;
1258
+ #endif
1259
+ if (flash_area_read (fap , secondary_hdr -> ih_hdr_size + sizeof (internal_img_addr ),
1260
+ & internal_img_addr , sizeof (internal_img_addr )) != 0 ) {
1258
1261
fih_rc = FIH_NO_BOOTABLE_IMAGE ;
1259
1262
goto out ;
1260
1263
}
1264
+ #else /* BOOT_USE_CHECK_LOAD_ADDR */
1261
1265
1262
1266
#ifdef PM_CPUNET_APP_ADDRESS
1263
1267
/* The primary slot for the network core is emulated in RAM.
@@ -1298,7 +1302,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
1298
1302
check_addresses = true;
1299
1303
}
1300
1304
1301
- if (check_addresses == true && (reset_value < min_addr || reset_value > max_addr )) {
1305
+ if (check_addresses == true && (internal_img_addr < min_addr || internal_img_addr > max_addr )) {
1302
1306
BOOT_LOG_ERR ("Reset address of image in secondary slot is not in the primary slot" );
1303
1307
BOOT_LOG_ERR ("Erasing image from secondary slot" );
1304
1308
@@ -1515,6 +1519,17 @@ static inline void sec_slot_cleanup_if_unusable(void)
1515
1519
#endif /* defined(CONFIG_MCUBOOT_CLEANUP_UNUSABLE_SECONDARY) &&\
1516
1520
defined(PM_S1_ADDRESS) || defined(CONFIG_SOC_NRF5340_CPUAPP) */
1517
1521
1522
+ #define IS_IN_RANGE_CPUNET_APP_ADDR (_addr ) ((_addr) >= PM_CPUNET_APP_ADDRESS && (_addr) < PM_CPUNET_APP_END_ADDRESS))
1523
+ #define _IS_IN_RANGE_S_VARIANT_ADDR (_addr , x ) ((_addr) >= PM_S##x_ADDRESS && (_addr) <= (PM_S##x_ADDRESS + PM_S##x_SIZE))
1524
+ #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1525
+ #define IS_IN_RANGE_S_ALTERNATE_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 0)
1526
+ #define IS_IN_RANGE_S_CURRENT_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 1)
1527
+ #else
1528
+ #define IS_IN_RANGE_S_ALTERNATE_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 1)
1529
+ #define IS_IN_RANGE_S_CURRENT_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 0)
1530
+ #endif
1531
+ #define IS_IN_RANGE_IMAGE_ADDR (_addr , _fa ) ((_addr) >= _fa->fa_off && (_addr) < (_fa->fa_off + _fa->fa_size))
1532
+
1518
1533
/**
1519
1534
* Determines which swap operation to perform, if any. If it is determined
1520
1535
* that a swap operation is required, the image in the secondary slot is checked
@@ -1538,8 +1553,9 @@ boot_validated_swap_type(struct boot_loader_state *state,
1538
1553
const struct flash_area * secondary_fa =
1539
1554
BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT );
1540
1555
struct image_header * hdr = boot_img_hdr (state , BOOT_SECONDARY_SLOT );
1541
- uint32_t reset_addr = 0 ;
1556
+ uint32_t internal_img_addr = 0 ; /* either the reset handler addres or the image beginning addres */
1542
1557
int rc = 0 ;
1558
+
1543
1559
/* Patch needed for NCS. Since image 0 (the app) and image 1 (the other
1544
1560
* B1 slot S0 or S1) share the same secondary slot, we need to check
1545
1561
* whether the update candidate in the secondary slot is intended for
@@ -1549,18 +1565,22 @@ boot_validated_swap_type(struct boot_loader_state *state,
1549
1565
*/
1550
1566
1551
1567
if (hdr -> ih_magic == IMAGE_MAGIC ) {
1568
+ #ifdef CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDR
1569
+ internal_img_addr = hdr -> ih_load_addr ;
1570
+ #else
1552
1571
rc = flash_area_read (secondary_fa , hdr -> ih_hdr_size +
1553
- sizeof (uint32_t ), & reset_addr ,
1554
- sizeof (reset_addr ));
1572
+ sizeof (uint32_t ), & internal_img_addr ,
1573
+ sizeof (internal_img_addr ));
1555
1574
if (rc != 0 ) {
1556
1575
return BOOT_SWAP_TYPE_FAIL ;
1557
1576
}
1577
+ #endif /* CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDR */
1558
1578
1559
1579
sec_slot_touch (state );
1560
1580
1561
1581
#ifdef PM_S1_ADDRESS
1562
1582
#ifdef PM_CPUNET_B0N_ADDRESS
1563
- if (!( reset_addr >= PM_CPUNET_APP_ADDRESS && reset_addr < PM_CPUNET_APP_END_ADDRESS ))
1583
+ if (!IS_IN_RANGE_CPUNET_APP_ADDR ( internal_img_addr ))
1564
1584
#endif
1565
1585
{
1566
1586
const struct flash_area * primary_fa ;
@@ -1572,11 +1592,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
1572
1592
}
1573
1593
1574
1594
/* Check start and end of primary slot for current image */
1575
- #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1576
- if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE )) {
1577
- #else
1578
- if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE )) {
1579
- #endif
1595
+ if (IS_IN_RANGE_S_VARIANT_ADDR (internal_img_addr )) {
1580
1596
if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
1581
1597
/* This is not the s0/s1 upgrade image but the application image, pretend
1582
1598
* there is no image so the NSIB update can be loaded
@@ -1585,18 +1601,14 @@ boot_validated_swap_type(struct boot_loader_state *state,
1585
1601
}
1586
1602
1587
1603
owner_nsib [BOOT_CURR_IMG (state )] = true;
1588
- #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1589
- } else if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE )) {
1590
- #else
1591
- } else if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE )) {
1592
- #endif
1604
+ } else if (IS_IN_RANGE_S_CURRENT_ADDR (internal_img_addr )) {
1593
1605
/* NSIB upgrade but for the wrong slot, must be erased */
1594
1606
BOOT_LOG_ERR ("Image in slot is for wrong s0/s1 image" );
1595
1607
flash_area_erase (secondary_fa , 0 , secondary_fa -> fa_size );
1596
1608
sec_slot_untouch (state );
1597
1609
BOOT_LOG_ERR ("Cleaned-up secondary slot of image %d" , BOOT_CURR_IMG (state ));
1598
1610
return BOOT_SWAP_TYPE_FAIL ;
1599
- } else if (reset_addr < primary_fa -> fa_off || reset_addr > ( primary_fa -> fa_off + primary_fa -> fa_size )) {
1611
+ } else if (! IS_IN_RANGE_IMAGE_ADDR ( internal_img_addr , primary_fa )) {
1600
1612
/* The image in the secondary slot is not intended for any */
1601
1613
return BOOT_SWAP_TYPE_NONE ;
1602
1614
}
@@ -1633,8 +1645,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
1633
1645
* update and indicate to the caller of this function that no update is
1634
1646
* available
1635
1647
*/
1636
- if (upgrade_valid && reset_addr >= PM_CPUNET_APP_ADDRESS &&
1637
- reset_addr < PM_CPUNET_APP_END_ADDRESS ) {
1648
+ if (upgrade_valid && IS_IN_RANGE_CPUNET_APP_ADDR (internal_img_addr )) {
1638
1649
struct image_header * hdr = (struct image_header * )secondary_fa -> fa_off ;
1639
1650
uint32_t vtable_addr = (uint32_t )hdr + hdr -> ih_hdr_size ;
1640
1651
uint32_t * net_core_fw_addr = (uint32_t * )(vtable_addr );
0 commit comments