@@ -84,6 +84,29 @@ int pcd_version_cmp_net(const struct flash_area *fap, struct image_header *hdr);
8484#include "bootutil/key_revocation.h"
8585#endif
8686
87+ #ifdef CONFIG_PARTITION_MANAGER_ENABLED
88+ #define THREE_CAT_ (a , b , c ) (a##b##c)
89+ #define THREE_CAT (a , b , c ) THREE_CAT_(a, b, c)
90+
91+ #ifdef CONFIG_NCS_IS_VARIANT_IMAGE
92+ /* Running from slot 1, checking slot 0 */
93+ #define NCS_VARIANT_SLOT_ID 0
94+ #else /* CONFIG_NCS_IS_VARIANT_IMAGE */
95+ /* Running from slot 0, checking slot 1 */
96+ #define NCS_VARIANT_SLOT_ID 1
97+ #endif /* CONFIG_NCS_IS_VARIANT_IMAGE */
98+
99+ #define NCS_VARIANT_SLOT_MIN_ADDR THREE_CAT(PM_S, NCS_VARIANT_SLOT_ID, _ADDRESS)
100+ #define NCS_VARIANT_SLOT_MAX_ADDR ((NCS_VARIANT_SLOT_MIN_ADDR) + THREE_CAT(PM_S, NCS_VARIANT_SLOT_ID, _SIZE))
101+
102+ #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER == -1
103+ #define CHECK_MCUBOOT_IMAGE 0
104+ #else
105+ #define CHECK_MCUBOOT_IMAGE 1
106+ #endif
107+
108+ #endif /* CONFIG_PARTITION_MANAGER_ENABLED */
109+
87110BOOT_LOG_MODULE_DECLARE (mcuboot );
88111
89112static struct boot_loader_state boot_data ;
@@ -1150,12 +1173,15 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
11501173 goto out ;
11511174 }
11521175
1153- #if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES ) && defined(MCUBOOT_VERIFY_IMG_ADDRESS )
1176+ #if MCUBOOT_IMAGE_NUMBER > 1 && \
1177+ (!defined(MCUBOOT_ENC_IMAGES ) || defined(CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDRESS )) && \
1178+ defined(MCUBOOT_VERIFY_IMG_ADDRESS )
11541179 /* Verify that the image in the secondary slot has a reset address
11551180 * located in the primary slot. This is done to avoid users incorrectly
11561181 * overwriting an application written to the incorrect slot.
11571182 * This feature is only supported by ARM platforms.
11581183 */
1184+
11591185#if MCUBOOT_IMAGE_NUMBER >= 3
11601186 /* Currently the MCUboot can be configured for up to 3 image, where image number 2 is
11611187 * designated for XIP, where it is the second part of image stored in slots of image
@@ -1167,58 +1193,72 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
11671193 }
11681194#endif
11691195 if (fap == BOOT_IMG_AREA (state , BOOT_SLOT_SECONDARY )) {
1170- const struct flash_area * pri_fa = BOOT_IMG_AREA (state , BOOT_SLOT_PRIMARY );
11711196 struct image_header * secondary_hdr = boot_img_hdr (state , slot );
1172- uint32_t reset_value = 0 ;
1173- uint32_t reset_addr = secondary_hdr -> ih_hdr_size + sizeof (reset_value );
1174- uint32_t min_addr , max_addr ;
1175- bool check_addresses = false;
1197+ uint32_t internal_img_addr = 0 ; /* either the reset handler addres or the image beginning addres */
1198+ uint32_t min_addr ;
1199+ uint32_t max_addr ;
11761200
1177- if (flash_area_read (fap , reset_addr , & reset_value , sizeof (reset_value )) != 0 ) {
1201+ min_addr = flash_area_get_off (BOOT_IMG_AREA (state , BOOT_SLOT_PRIMARY ));
1202+ max_addr = flash_area_get_size (BOOT_IMG_AREA (state , BOOT_SLOT_PRIMARY )) + min_addr ;
1203+
1204+ #ifdef CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDRESS
1205+ internal_img_addr = secondary_hdr -> ih_load_addr ;
1206+ #else
1207+ const uint32_t offset = secondary_hdr -> ih_hdr_size + sizeof (internal_img_addr );
1208+ BOOT_LOG_DBG ("Getting image %d internal addr from offset %d" ,
1209+ BOOT_CURR_IMG (state ), offset );
1210+ /* Read reset vector from ARM vector table */
1211+ if (flash_area_read (fap , offset , & internal_img_addr , sizeof (internal_img_addr )) != 0 ) {
1212+ BOOT_LOG_ERR ("Failed to read image load address" , BOOT_CURR_IMG (state ));
11781213 fih_rc = FIH_NO_BOOTABLE_IMAGE ;
11791214 goto out ;
11801215 }
1216+ #endif
1217+ BOOT_LOG_DBG ("Image %d expected load address 0x%x" , BOOT_CURR_IMG (state ), internal_img_addr );
11811218
1182- #ifdef PM_CPUNET_APP_ADDRESS
1219+ #ifdef CONFIG_PARTITION_MANAGER_ENABLED
11831220 /* The primary slot for the network core is emulated in RAM.
11841221 * Its flash_area hasn't got relevant boundaries.
11851222 * Therfore need to override its boundaries for the check.
11861223 */
1224+ #ifdef PM_CPUNET_APP_ADDRESS
11871225 if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER ) {
1226+
1227+ BOOT_LOG_DBG ("Image %d is NETCORE image" , BOOT_CURR_IMG (state ));
1228+
11881229 min_addr = PM_CPUNET_APP_ADDRESS ;
11891230 max_addr = PM_CPUNET_APP_ADDRESS + PM_CPUNET_APP_SIZE ;
1190- check_addresses = true;
1191- } else
1192- #endif
1193- #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != - 1
1231+ }
1232+ #endif /* PM_CPUNET_APP_SIZE */
1233+
1234+ #if CHECK_MCUBOOT_IMAGE == 1
11941235 if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ) {
1195- #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1196- min_addr = PM_S0_ADDRESS ;
1197- max_addr = (PM_S0_ADDRESS + PM_S0_SIZE );
1198- #else
1199- min_addr = PM_S1_ADDRESS ;
1200- max_addr = (PM_S1_ADDRESS + PM_S1_SIZE );
1201- #endif
1202- check_addresses = true;
1203- } else
1204- #endif
1236+
1237+ BOOT_LOG_DBG ("Image %d is another stage MCBoot image" , BOOT_CURR_IMG (state ));
1238+
1239+ min_addr = NCS_VARIANT_SLOT_MIN_ADDR ;
1240+ max_addr = NCS_VARIANT_SLOT_MAX_ADDR ;
1241+ }
1242+ #endif /* CHECK_MCUBOOT_IMAGE */
1243+
1244+ #if CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER != -1
12051245 if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
1206- #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1207- #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1208- min_addr = MIN (pri_fa -> fa_off , PM_S0_ADDRESS );
1209- max_addr = MAX ((pri_fa -> fa_off + pri_fa -> fa_size ), (PM_S0_ADDRESS + PM_S0_SIZE ));
1210- #else
1211- min_addr = MIN (pri_fa -> fa_off , PM_S1_ADDRESS );
1212- max_addr = MAX ((pri_fa -> fa_off + pri_fa -> fa_size ), (PM_S1_ADDRESS + PM_S1_SIZE ));
1213- #endif
1214- #else
1215- min_addr = pri_fa -> fa_off ;
1216- max_addr = pri_fa -> fa_off + pri_fa -> fa_size ;
1246+
1247+ BOOT_LOG_DBG ("Image %d is application MCBoot image" , BOOT_CURR_IMG (state ));
1248+
1249+ /* If not checking the MCUboot image, then we leave the original
1250+ * min/max assignments from the primary slot read. */
1251+ #if CHECK_MCUBOOT_IMAGE == 1
1252+ min_addr = MIN (min_addr , NCS_VARIANT_SLOT_MIN_ADDR );
1253+ max_addr = MAX (max_addr , NCS_VARIANT_SLOT_MAX_ADDR );
12171254#endif
1218- check_addresses = true;
12191255 }
1256+ #endif
1257+ #endif /* CONFIG_PARTITION_MANAGER_ENABLED */
12201258
1221- if (check_addresses == true && (reset_value < min_addr || reset_value > max_addr )) {
1259+ LOG_DBG ("Check 0x%x is within [min_addr, max_addr] = [0x%x, 0x%x)" ,
1260+ internal_img_addr , min_addr , max_addr );
1261+ if (internal_img_addr < min_addr || internal_img_addr >= max_addr ) {
12221262 BOOT_LOG_ERR ("Reset address of image in secondary slot is not in the primary slot" );
12231263 BOOT_LOG_ERR ("Erasing image from secondary slot" );
12241264
@@ -1232,6 +1272,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
12321272 fih_rc = FIH_NO_BOOTABLE_IMAGE ;
12331273 goto out ;
12341274 }
1275+ LOG_DBG ("Image %d validation OK" , BOOT_CURR_IMG (state ));
12351276 }
12361277#endif
12371278
@@ -1435,6 +1476,18 @@ static inline void sec_slot_cleanup_if_unusable(void)
14351476#endif /* defined(CONFIG_MCUBOOT_CLEANUP_UNUSABLE_SECONDARY) &&\
14361477 defined(PM_S1_ADDRESS) || defined(CONFIG_SOC_NRF5340_CPUAPP) */
14371478
1479+ #define IS_IN_RANGE_CPUNET_APP_ADDR (_addr ) (((_addr) >= PM_CPUNET_APP_ADDRESS) && ((_addr) < PM_CPUNET_APP_END_ADDRESS))
1480+ #define _IS_IN_RANGE_S_VARIANT_ADDR (_addr , x ) (((_addr) >= PM_S##x##_ADDRESS) && ((_addr) <= (PM_S##x##_ADDRESS + PM_S##x##_SIZE)))
1481+ #if defined(CONFIG_NCS_IS_VARIANT_IMAGE )
1482+ #define IS_IN_RANGE_S_ALTERNATE_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 0)
1483+ #define IS_IN_RANGE_S_CURRENT_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 1)
1484+ #else
1485+ #define IS_IN_RANGE_S_ALTERNATE_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 1)
1486+ #define IS_IN_RANGE_S_CURRENT_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 0)
1487+ #endif
1488+ #define IS_IN_RANGE_IMAGE_ADDR (_addr , _fa ) \
1489+ (((_addr) >= flash_area_get_off(_fa)) && (_addr) < (flash_area_get_off(_fa) + flash_area_get_size(_fa)))
1490+
14381491/**
14391492 * Determines which swap operation to perform, if any. If it is determined
14401493 * that a swap operation is required, the image in the secondary slot is checked
@@ -1458,8 +1511,9 @@ boot_validated_swap_type(struct boot_loader_state *state,
14581511 const struct flash_area * secondary_fa =
14591512 BOOT_IMG_AREA (state , BOOT_SLOT_SECONDARY );
14601513 struct image_header * hdr = boot_img_hdr (state , BOOT_SLOT_SECONDARY );
1461- uint32_t reset_addr = 0 ;
1514+ uint32_t internal_img_addr = 0 ; /* either the reset handler addres or the image beginning addres */
14621515 int rc = 0 ;
1516+
14631517 /* Patch needed for NCS. Since image 0 (the app) and image 1 (the other
14641518 * B1 slot S0 or S1) share the same secondary slot, we need to check
14651519 * whether the update candidate in the secondary slot is intended for
@@ -1469,18 +1523,22 @@ boot_validated_swap_type(struct boot_loader_state *state,
14691523 */
14701524
14711525 if (hdr -> ih_magic == IMAGE_MAGIC ) {
1526+ #ifdef CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDRESS
1527+ internal_img_addr = hdr -> ih_load_addr ;
1528+ #else
14721529 rc = flash_area_read (secondary_fa , hdr -> ih_hdr_size +
1473- sizeof (uint32_t ), & reset_addr ,
1474- sizeof (reset_addr ));
1530+ sizeof (uint32_t ), & internal_img_addr ,
1531+ sizeof (internal_img_addr ));
14751532 if (rc != 0 ) {
14761533 return BOOT_SWAP_TYPE_FAIL ;
14771534 }
1535+ #endif /* CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDRESS */
14781536
14791537 sec_slot_touch (state );
14801538
14811539#ifdef PM_S1_ADDRESS
14821540#ifdef PM_CPUNET_B0N_ADDRESS
1483- if (!( reset_addr >= PM_CPUNET_APP_ADDRESS && reset_addr < PM_CPUNET_APP_END_ADDRESS ))
1541+ if (!IS_IN_RANGE_CPUNET_APP_ADDR ( internal_img_addr ))
14841542#endif
14851543 {
14861544 const struct flash_area * primary_fa ;
@@ -1492,11 +1550,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
14921550 }
14931551
14941552 /* Check start and end of primary slot for current image */
1495- #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1496- if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE )) {
1497- #else
1498- if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE )) {
1499- #endif
1553+ if (IS_IN_RANGE_S_ALTERNATE_ADDR (internal_img_addr )) {
15001554 if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
15011555 /* This is not the s0/s1 upgrade image but the application image, pretend
15021556 * there is no image so the NSIB update can be loaded
@@ -1505,18 +1559,14 @@ boot_validated_swap_type(struct boot_loader_state *state,
15051559 }
15061560
15071561 owner_nsib [BOOT_CURR_IMG (state )] = true;
1508- #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1509- } else if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE )) {
1510- #else
1511- } else if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE )) {
1512- #endif
1562+ } else if (IS_IN_RANGE_S_CURRENT_ADDR (internal_img_addr )) {
15131563 /* NSIB upgrade but for the wrong slot, must be erased */
15141564 BOOT_LOG_ERR ("Image in slot is for wrong s0/s1 image" );
15151565 flash_area_erase (secondary_fa , 0 , secondary_fa -> fa_size );
15161566 sec_slot_untouch (state );
15171567 BOOT_LOG_ERR ("Cleaned-up secondary slot of image %d" , BOOT_CURR_IMG (state ));
15181568 return BOOT_SWAP_TYPE_FAIL ;
1519- } else if (reset_addr < primary_fa -> fa_off || reset_addr > ( primary_fa -> fa_off + primary_fa -> fa_size )) {
1569+ } else if (! IS_IN_RANGE_IMAGE_ADDR ( internal_img_addr , primary_fa )) {
15201570 /* The image in the secondary slot is not intended for any */
15211571 return BOOT_SWAP_TYPE_NONE ;
15221572 }
@@ -1553,8 +1603,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
15531603 * update and indicate to the caller of this function that no update is
15541604 * available
15551605 */
1556- if (upgrade_valid && reset_addr >= PM_CPUNET_APP_ADDRESS &&
1557- reset_addr < PM_CPUNET_APP_END_ADDRESS ) {
1606+ if (upgrade_valid && IS_IN_RANGE_CPUNET_APP_ADDR (internal_img_addr )) {
15581607 struct image_header * hdr = (struct image_header * )secondary_fa -> fa_off ;
15591608 uint32_t vtable_addr = (uint32_t )hdr + hdr -> ih_hdr_size ;
15601609 uint32_t * net_core_fw_addr = (uint32_t * )(vtable_addr );
0 commit comments