@@ -88,6 +88,29 @@ int pcd_version_cmp_net(const struct flash_area *fap, struct image_header *hdr);
8888void s2ram_designate_slot (uint8_t slot );
8989#endif
9090
91+ #ifdef CONFIG_PARTITION_MANAGER_ENABLED
92+ #define THREE_CAT_ (a , b , c ) (a##b##c)
93+ #define THREE_CAT (a , b , c ) THREE_CAT_(a, b, c)
94+
95+ #ifdef CONFIG_NCS_IS_VARIANT_IMAGE
96+ /* Running from slot 1, checking slot 0 */
97+ #define NCS_VARIANT_SLOT_ID 0
98+ #else /* CONFIG_NCS_IS_VARIANT_IMAGE */
99+ /* Running from slot 0, checking slot 1 */
100+ #define NCS_VARIANT_SLOT_ID 1
101+ #endif /* CONFIG_NCS_IS_VARIANT_IMAGE */
102+
103+ #define NCS_VARIANT_SLOT_MIN_ADDR THREE_CAT(PM_S, NCS_VARIANT_SLOT_ID, _ADDRESS)
104+ #define NCS_VARIANT_SLOT_MAX_ADDR ((NCS_VARIANT_SLOT_MIN_ADDR) + THREE_CAT(PM_S, NCS_VARIANT_SLOT_ID, _SIZE))
105+
106+ #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER == -1
107+ #define CHECK_MCUBOOT_IMAGE 0
108+ #else
109+ #define CHECK_MCUBOOT_IMAGE 1
110+ #endif
111+
112+ #endif /* CONFIG_PARTITION_MANAGER_ENABLED */
113+
91114BOOT_LOG_MODULE_DECLARE (mcuboot );
92115
93116static struct boot_loader_state boot_data ;
@@ -1154,12 +1177,15 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
11541177 goto out ;
11551178 }
11561179
1157- #if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES ) && defined(MCUBOOT_VERIFY_IMG_ADDRESS )
1180+ #if MCUBOOT_IMAGE_NUMBER > 1 && \
1181+ (!defined(MCUBOOT_ENC_IMAGES ) || defined(CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDRESS )) && \
1182+ defined(MCUBOOT_VERIFY_IMG_ADDRESS )
11581183 /* Verify that the image in the secondary slot has a reset address
11591184 * located in the primary slot. This is done to avoid users incorrectly
11601185 * overwriting an application written to the incorrect slot.
11611186 * This feature is only supported by ARM platforms.
11621187 */
1188+
11631189#if MCUBOOT_IMAGE_NUMBER >= 3
11641190 /* Currently the MCUboot can be configured for up to 3 image, where image number 2 is
11651191 * designated for XIP, where it is the second part of image stored in slots of image
@@ -1171,58 +1197,72 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
11711197 }
11721198#endif
11731199 if (fap == BOOT_IMG_AREA (state , BOOT_SLOT_SECONDARY )) {
1174- const struct flash_area * pri_fa = BOOT_IMG_AREA (state , BOOT_SLOT_PRIMARY );
11751200 struct image_header * secondary_hdr = boot_img_hdr (state , slot );
1176- uint32_t reset_value = 0 ;
1177- uint32_t reset_addr = secondary_hdr -> ih_hdr_size + sizeof (reset_value );
1178- uint32_t min_addr , max_addr ;
1179- bool check_addresses = false;
1201+ uint32_t internal_img_addr = 0 ; /* either the reset handler addres or the image beginning addres */
1202+ uint32_t min_addr ;
1203+ uint32_t max_addr ;
11801204
1181- if (flash_area_read (fap , reset_addr , & reset_value , sizeof (reset_value )) != 0 ) {
1205+ min_addr = flash_area_get_off (BOOT_IMG_AREA (state , BOOT_SLOT_PRIMARY ));
1206+ max_addr = flash_area_get_size (BOOT_IMG_AREA (state , BOOT_SLOT_PRIMARY )) + min_addr ;
1207+
1208+ #ifdef CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDRESS
1209+ internal_img_addr = secondary_hdr -> ih_load_addr ;
1210+ #else
1211+ const uint32_t offset = secondary_hdr -> ih_hdr_size + sizeof (internal_img_addr );
1212+ BOOT_LOG_DBG ("Getting image %d internal addr from offset %d" ,
1213+ BOOT_CURR_IMG (state ), offset );
1214+ /* Read reset vector from ARM vector table */
1215+ if (flash_area_read (fap , offset , & internal_img_addr , sizeof (internal_img_addr )) != 0 ) {
1216+ BOOT_LOG_ERR ("Failed to read image %d load address" , BOOT_CURR_IMG (state ));
11821217 fih_rc = FIH_NO_BOOTABLE_IMAGE ;
11831218 goto out ;
11841219 }
1220+ #endif
1221+ BOOT_LOG_DBG ("Image %d expected load address 0x%x" , BOOT_CURR_IMG (state ), internal_img_addr );
11851222
1186- #ifdef PM_CPUNET_APP_ADDRESS
1223+ #ifdef CONFIG_PARTITION_MANAGER_ENABLED
11871224 /* The primary slot for the network core is emulated in RAM.
11881225 * Its flash_area hasn't got relevant boundaries.
11891226 * Therfore need to override its boundaries for the check.
11901227 */
1228+ #ifdef PM_CPUNET_APP_ADDRESS
11911229 if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER ) {
1230+
1231+ BOOT_LOG_DBG ("Image %d is NETCORE image" , BOOT_CURR_IMG (state ));
1232+
11921233 min_addr = PM_CPUNET_APP_ADDRESS ;
11931234 max_addr = PM_CPUNET_APP_ADDRESS + PM_CPUNET_APP_SIZE ;
1194- check_addresses = true;
1195- } else
1196- #endif
1197- #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != - 1
1235+ }
1236+ #endif /* PM_CPUNET_APP_SIZE */
1237+
1238+ #if CHECK_MCUBOOT_IMAGE == 1
11981239 if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER ) {
1199- #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1200- min_addr = PM_S0_ADDRESS ;
1201- max_addr = (PM_S0_ADDRESS + PM_S0_SIZE );
1202- #else
1203- min_addr = PM_S1_ADDRESS ;
1204- max_addr = (PM_S1_ADDRESS + PM_S1_SIZE );
1205- #endif
1206- check_addresses = true;
1207- } else
1208- #endif
1240+
1241+ BOOT_LOG_DBG ("Image %d is another stage MCBoot image" , BOOT_CURR_IMG (state ));
1242+
1243+ min_addr = NCS_VARIANT_SLOT_MIN_ADDR ;
1244+ max_addr = NCS_VARIANT_SLOT_MAX_ADDR ;
1245+ }
1246+ #endif /* CHECK_MCUBOOT_IMAGE */
1247+
1248+ #if CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER != -1
12091249 if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
1210- #if CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
1211- #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1212- min_addr = MIN (pri_fa -> fa_off , PM_S0_ADDRESS );
1213- max_addr = MAX ((pri_fa -> fa_off + pri_fa -> fa_size ), (PM_S0_ADDRESS + PM_S0_SIZE ));
1214- #else
1215- min_addr = MIN (pri_fa -> fa_off , PM_S1_ADDRESS );
1216- max_addr = MAX ((pri_fa -> fa_off + pri_fa -> fa_size ), (PM_S1_ADDRESS + PM_S1_SIZE ));
1217- #endif
1218- #else
1219- min_addr = pri_fa -> fa_off ;
1220- max_addr = pri_fa -> fa_off + pri_fa -> fa_size ;
1250+
1251+ BOOT_LOG_DBG ("Image %d is application MCBoot image" , BOOT_CURR_IMG (state ));
1252+
1253+ /* If not checking the MCUboot image, then we leave the original
1254+ * min/max assignments from the primary slot read. */
1255+ #if CHECK_MCUBOOT_IMAGE == 1
1256+ min_addr = MIN (min_addr , NCS_VARIANT_SLOT_MIN_ADDR );
1257+ max_addr = MAX (max_addr , NCS_VARIANT_SLOT_MAX_ADDR );
12211258#endif
1222- check_addresses = true;
12231259 }
1260+ #endif
1261+ #endif /* CONFIG_PARTITION_MANAGER_ENABLED */
12241262
1225- if (check_addresses == true && (reset_value < min_addr || reset_value > max_addr )) {
1263+ BOOT_LOG_DBG ("Check 0x%x is within [min_addr, max_addr] = [0x%x, 0x%x)" ,
1264+ internal_img_addr , min_addr , max_addr );
1265+ if (internal_img_addr < min_addr || internal_img_addr >= max_addr ) {
12261266 BOOT_LOG_ERR ("Reset address of image in secondary slot is not in the primary slot" );
12271267 BOOT_LOG_ERR ("Erasing image from secondary slot" );
12281268
@@ -1236,6 +1276,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
12361276 fih_rc = FIH_NO_BOOTABLE_IMAGE ;
12371277 goto out ;
12381278 }
1279+ BOOT_LOG_DBG ("Image %d validation OK" , BOOT_CURR_IMG (state ));
12391280 }
12401281#endif
12411282
@@ -1439,6 +1480,18 @@ static inline void sec_slot_cleanup_if_unusable(void)
14391480#endif /* defined(CONFIG_MCUBOOT_CLEANUP_UNUSABLE_SECONDARY) &&\
14401481 defined(PM_S1_ADDRESS) || defined(CONFIG_SOC_NRF5340_CPUAPP) */
14411482
1483+ #define IS_IN_RANGE_CPUNET_APP_ADDR (_addr ) (((_addr) >= PM_CPUNET_APP_ADDRESS) && ((_addr) < PM_CPUNET_APP_END_ADDRESS))
1484+ #define _IS_IN_RANGE_S_VARIANT_ADDR (_addr , x ) (((_addr) >= PM_S##x##_ADDRESS) && ((_addr) <= (PM_S##x##_ADDRESS + PM_S##x##_SIZE)))
1485+ #if defined(CONFIG_NCS_IS_VARIANT_IMAGE )
1486+ #define IS_IN_RANGE_S_ALTERNATE_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 0)
1487+ #define IS_IN_RANGE_S_CURRENT_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 1)
1488+ #else
1489+ #define IS_IN_RANGE_S_ALTERNATE_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 1)
1490+ #define IS_IN_RANGE_S_CURRENT_ADDR (_addr ) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 0)
1491+ #endif
1492+ #define IS_IN_RANGE_IMAGE_ADDR (_addr , _fa ) \
1493+ (((_addr) >= flash_area_get_off(_fa)) && (_addr) < (flash_area_get_off(_fa) + flash_area_get_size(_fa)))
1494+
14421495/**
14431496 * Determines which swap operation to perform, if any. If it is determined
14441497 * that a swap operation is required, the image in the secondary slot is checked
@@ -1462,8 +1515,9 @@ boot_validated_swap_type(struct boot_loader_state *state,
14621515 const struct flash_area * secondary_fa =
14631516 BOOT_IMG_AREA (state , BOOT_SLOT_SECONDARY );
14641517 struct image_header * hdr = boot_img_hdr (state , BOOT_SLOT_SECONDARY );
1465- uint32_t reset_addr = 0 ;
1518+ uint32_t internal_img_addr = 0 ; /* either the reset handler addres or the image beginning addres */
14661519 int rc = 0 ;
1520+
14671521 /* Patch needed for NCS. Since image 0 (the app) and image 1 (the other
14681522 * B1 slot S0 or S1) share the same secondary slot, we need to check
14691523 * whether the update candidate in the secondary slot is intended for
@@ -1473,18 +1527,22 @@ boot_validated_swap_type(struct boot_loader_state *state,
14731527 */
14741528
14751529 if (hdr -> ih_magic == IMAGE_MAGIC ) {
1530+ #ifdef CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDRESS
1531+ internal_img_addr = hdr -> ih_load_addr ;
1532+ #else
14761533 rc = flash_area_read (secondary_fa , hdr -> ih_hdr_size +
1477- sizeof (uint32_t ), & reset_addr ,
1478- sizeof (reset_addr ));
1534+ sizeof (uint32_t ), & internal_img_addr ,
1535+ sizeof (internal_img_addr ));
14791536 if (rc != 0 ) {
14801537 return BOOT_SWAP_TYPE_FAIL ;
14811538 }
1539+ #endif /* CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDRESS */
14821540
14831541 sec_slot_touch (state );
14841542
14851543#ifdef PM_S1_ADDRESS
14861544#ifdef PM_CPUNET_B0N_ADDRESS
1487- if (!( reset_addr >= PM_CPUNET_APP_ADDRESS && reset_addr < PM_CPUNET_APP_END_ADDRESS ))
1545+ if (!IS_IN_RANGE_CPUNET_APP_ADDR ( internal_img_addr ))
14881546#endif
14891547 {
14901548 const struct flash_area * primary_fa ;
@@ -1496,11 +1554,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
14961554 }
14971555
14981556 /* Check start and end of primary slot for current image */
1499- #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1500- if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE )) {
1501- #else
1502- if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE )) {
1503- #endif
1557+ if (IS_IN_RANGE_S_ALTERNATE_ADDR (internal_img_addr )) {
15041558 if (BOOT_CURR_IMG (state ) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER ) {
15051559 /* This is not the s0/s1 upgrade image but the application image, pretend
15061560 * there is no image so the NSIB update can be loaded
@@ -1509,18 +1563,14 @@ boot_validated_swap_type(struct boot_loader_state *state,
15091563 }
15101564
15111565 owner_nsib [BOOT_CURR_IMG (state )] = true;
1512- #if (CONFIG_NCS_IS_VARIANT_IMAGE )
1513- } else if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE )) {
1514- #else
1515- } else if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE )) {
1516- #endif
1566+ } else if (IS_IN_RANGE_S_CURRENT_ADDR (internal_img_addr )) {
15171567 /* NSIB upgrade but for the wrong slot, must be erased */
15181568 BOOT_LOG_ERR ("Image in slot is for wrong s0/s1 image" );
15191569 flash_area_erase (secondary_fa , 0 , secondary_fa -> fa_size );
15201570 sec_slot_untouch (state );
15211571 BOOT_LOG_ERR ("Cleaned-up secondary slot of image %d" , BOOT_CURR_IMG (state ));
15221572 return BOOT_SWAP_TYPE_FAIL ;
1523- } else if (reset_addr < primary_fa -> fa_off || reset_addr > ( primary_fa -> fa_off + primary_fa -> fa_size )) {
1573+ } else if (! IS_IN_RANGE_IMAGE_ADDR ( internal_img_addr , primary_fa )) {
15241574 /* The image in the secondary slot is not intended for any */
15251575 return BOOT_SWAP_TYPE_NONE ;
15261576 }
@@ -1557,8 +1607,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
15571607 * update and indicate to the caller of this function that no update is
15581608 * available
15591609 */
1560- if (upgrade_valid && reset_addr >= PM_CPUNET_APP_ADDRESS &&
1561- reset_addr < PM_CPUNET_APP_END_ADDRESS ) {
1610+ if (upgrade_valid && IS_IN_RANGE_CPUNET_APP_ADDR (internal_img_addr )) {
15621611 struct image_header * hdr = (struct image_header * )secondary_fa -> fa_off ;
15631612 uint32_t vtable_addr = (uint32_t )hdr + hdr -> ih_hdr_size ;
15641613 uint32_t * net_core_fw_addr = (uint32_t * )(vtable_addr );
0 commit comments