@@ -1331,15 +1331,19 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
1331
1331
if (fap == BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT )) {
1332
1332
const struct flash_area * pri_fa = BOOT_IMG_AREA (state , BOOT_PRIMARY_SLOT );
1333
1333
struct image_header * secondary_hdr = boot_img_hdr (state , slot );
1334
- uint32_t reset_value = 0 ;
1335
- uint32_t reset_addr = secondary_hdr -> ih_hdr_size + sizeof (reset_value );
1334
+ uint32_t internal_img_addr = 0 ; /* either the reset handler addres or the image beginning addres */
1336
1335
uint32_t min_addr , max_addr ;
1337
1336
bool check_addresses = false;
1338
1337
1339
- if (flash_area_read (fap , reset_addr , & reset_value , sizeof (reset_value )) != 0 ) {
1338
+ #ifdef CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDR
1339
+ internal_img_addr = secondary_hdr -> ih_load_addr ;
1340
+ #else
1341
+ if (flash_area_read (fap , secondary_hdr -> ih_hdr_size + sizeof (internal_img_addr ),
1342
+ & internal_img_addr , sizeof (internal_img_addr )) != 0 ) {
1340
1343
fih_rc = FIH_NO_BOOTABLE_IMAGE ;
1341
1344
goto out ;
1342
1345
}
1346
+ #endif
1343
1347
1344
1348
#ifdef PM_CPUNET_APP_ADDRESS
1345
1349
/* The primary slot for the network core is emulated in RAM.
@@ -1380,7 +1384,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
1380
1384
check_addresses = true;
1381
1385
}
1382
1386
1383
- if (check_addresses == true && (reset_value < min_addr || reset_value > max_addr )) {
1387
+ if (check_addresses == true && (internal_img_addr < min_addr || internal_img_addr > max_addr )) {
1384
1388
BOOT_LOG_ERR ("Reset address of image in secondary slot is not in the primary slot" );
1385
1389
BOOT_LOG_ERR ("Erasing image from secondary slot" );
1386
1390
@@ -1597,6 +1601,17 @@ static inline void sec_slot_cleanup_if_unusable(void)
1597
1601
#endif /* defined(CONFIG_MCUBOOT_CLEANUP_UNUSABLE_SECONDARY) &&\
1598
1602
defined(PM_S1_ADDRESS) || defined(CONFIG_SOC_NRF5340_CPUAPP) */
1599
1603
1604
+ #define IS_IN_RANGE_CPUNET_APP_ADDR (_addr ) ((_addr) >= PM_CPUNET_APP_ADDRESS && (_addr) < PM_CPUNET_APP_END_ADDRESS)
1605
+ #define _IS_IN_RANGE_S_VARIANT_ADDR (_addr , x ) ((_addr) >= PM_S##x_ADDRESS && (_addr) <= (PM_S##x_ADDRESS + PM_S##x_SIZE))
1606
+ #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1607
+ #define IS_IN_RANGE_S_ALTERNATE_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 0)
1608
+ #define IS_IN_RANGE_S_CURRENT_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 1)
1609
+ #else
1610
+ #define IS_IN_RANGE_S_ALTERNATE_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 1)
1611
+ #define IS_IN_RANGE_S_CURRENT_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 0)
1612
+ #endif
1613
+ #define IS_IN_RANGE_IMAGE_ADDR (_addr , _fa ) ((_addr) >= _fa->fa_off && (_addr) < (_fa->fa_off + _fa->fa_size))
1614
+
1600
1615
/**
1601
1616
* Determines which swap operation to perform, if any. If it is determined
1602
1617
* that a swap operation is required, the image in the secondary slot is checked
@@ -1620,8 +1635,9 @@ boot_validated_swap_type(struct boot_loader_state *state,
1620
1635
const struct flash_area * secondary_fa =
1621
1636
BOOT_IMG_AREA (state , BOOT_SECONDARY_SLOT );
1622
1637
struct image_header * hdr = boot_img_hdr (state , BOOT_SECONDARY_SLOT );
1623
- uint32_t reset_addr = 0 ;
1638
+ uint32_t internal_img_addr = 0 ; /* either the reset handler addres or the image beginning addres */
1624
1639
int rc = 0 ;
1640
+
1625
1641
/* Patch needed for NCS. Since image 0 (the app) and image 1 (the other
1626
1642
* B1 slot S0 or S1) share the same secondary slot, we need to check
1627
1643
* whether the update candidate in the secondary slot is intended for
@@ -1631,18 +1647,22 @@ boot_validated_swap_type(struct boot_loader_state *state,
1631
1647
*/
1632
1648
1633
1649
if (hdr -> ih_magic == IMAGE_MAGIC ) {
1650
+ #ifdef CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDR
1651
+ internal_img_addr = hdr -> ih_load_addr ;
1652
+ #else
1634
1653
rc = flash_area_read (secondary_fa , hdr -> ih_hdr_size +
1635
- sizeof (uint32_t ), & reset_addr ,
1636
- sizeof (reset_addr ));
1654
+ sizeof (uint32_t ), & internal_img_addr ,
1655
+ sizeof (internal_img_addr ));
1637
1656
if (rc != 0 ) {
1638
1657
return BOOT_SWAP_TYPE_FAIL ;
1639
1658
}
1659
+ #endif /* CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDR */
1640
1660
1641
1661
sec_slot_touch (state );
1642
1662
1643
1663
#ifdef PM_S1_ADDRESS
1644
1664
#ifdef PM_CPUNET_B0N_ADDRESS
1645
- if (!( reset_addr >= PM_CPUNET_APP_ADDRESS && reset_addr < PM_CPUNET_APP_END_ADDRESS ))
1665
+ if (!IS_IN_RANGE_CPUNET_APP_ADDR ( internal_img_addr ))
1646
1666
#endif
1647
1667
{
1648
1668
const struct flash_area * primary_fa ;
@@ -1654,11 +1674,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
1654
1674
}
1655
1675
1656
1676
/* Check start and end of primary slot for current image */
1657
- #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1658
- if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE )) {
1659
- #else
1660
- if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE )) {
1661
- #endif
1677
+ if (IS_IN_RANGE_S_ALTERNATE_ADDR (internal_img_addr )) {
1662
1678
if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
1663
1679
/* This is not the s0/s1 upgrade image but the application image, pretend
1664
1680
* there is no image so the NSIB update can be loaded
@@ -1667,18 +1683,14 @@ boot_validated_swap_type(struct boot_loader_state *state,
1667
1683
}
1668
1684
1669
1685
owner_nsib [BOOT_CURR_IMG (state )] = true;
1670
- #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1671
- } else if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE )) {
1672
- #else
1673
- } else if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE )) {
1674
- #endif
1686
+ } else if (IS_IN_RANGE_S_CURRENT_ADDR (internal_img_addr )) {
1675
1687
/* NSIB upgrade but for the wrong slot, must be erased */
1676
1688
BOOT_LOG_ERR ("Image in slot is for wrong s0/s1 image" );
1677
1689
flash_area_erase (secondary_fa , 0 , secondary_fa -> fa_size );
1678
1690
sec_slot_untouch (state );
1679
1691
BOOT_LOG_ERR ("Cleaned-up secondary slot of image %d" , BOOT_CURR_IMG (state ));
1680
1692
return BOOT_SWAP_TYPE_FAIL ;
1681
- } else if (reset_addr < primary_fa -> fa_off || reset_addr > ( primary_fa -> fa_off + primary_fa -> fa_size )) {
1693
+ } else if (! IS_IN_RANGE_IMAGE_ADDR ( internal_img_addr , primary_fa )) {
1682
1694
/* The image in the secondary slot is not intended for any */
1683
1695
return BOOT_SWAP_TYPE_NONE ;
1684
1696
}
@@ -1715,8 +1727,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
1715
1727
* update and indicate to the caller of this function that no update is
1716
1728
* available
1717
1729
*/
1718
- if (upgrade_valid && reset_addr >= PM_CPUNET_APP_ADDRESS &&
1719
- reset_addr < PM_CPUNET_APP_END_ADDRESS ) {
1730
+ if (upgrade_valid && IS_IN_RANGE_CPUNET_APP_ADDR (internal_img_addr )) {
1720
1731
struct image_header * hdr = (struct image_header * )secondary_fa -> fa_off ;
1721
1732
uint32_t vtable_addr = (uint32_t )hdr + hdr -> ih_hdr_size ;
1722
1733
uint32_t * net_core_fw_addr = (uint32_t * )(vtable_addr );
0 commit comments