@@ -81,6 +81,13 @@ static struct image_max_size image_max_sizes[BOOT_IMAGE_NUMBER] = {0};
81
81
#define TARGET_STATIC
82
82
#endif
83
83
84
+ #if defined(MCUBOOT_VERIFY_IMG_ADDRESS ) && defined(MCUBOOT_CHECK_HEADER_LOAD_ADDRESS )
85
+ #warning MCUBOOT_CHECK_HEADER_LOAD_ADDRESS takes precedense over MCUBOOT_VERIFY_IMG_ADDRESS
86
+ #endif
87
+
88
+ /* Valid only for ARM Cortext M */
89
+ #define RESET_OFFSET (2 * sizeof(uin32_t))
90
+
84
91
#if BOOT_MAX_ALIGN > 1024
85
92
#define BUF_SZ BOOT_MAX_ALIGN
86
93
#else
@@ -1002,24 +1009,42 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
1002
1009
goto out ;
1003
1010
}
1004
1011
1005
- #if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES ) && defined(MCUBOOT_VERIFY_IMG_ADDRESS )
1012
+ #if defined(MCUBOOT_VERIFY_IMG_ADDRESS && !defined(MCUBOOT_ENC_IMAGES ) || \
1013
+ defined(MCUBOOT_CHECK_HEADER_LOAD_ADDRESS )
1006
1014
/* Verify that the image in the secondary slot has a reset address
1007
1015
* located in the primary slot. This is done to avoid users incorrectly
1008
1016
* overwriting an application written to the incorrect slot.
1009
1017
* This feature is only supported by ARM platforms.
1010
1018
*/
1011
1019
if (fap == BOOT_IMG_AREA (state , BOOT_SLOT_SECONDARY )) {
1012
- const struct flash_area * pri_fa = BOOT_IMG_AREA (state , BOOT_SLOT_PRIMARY );
1013
1020
struct image_header * secondary_hdr = boot_img_hdr (state , slot );
1014
1021
uint32_t reset_value = 0 ;
1015
- uint32_t reset_addr = secondary_hdr -> ih_hdr_size + sizeof (reset_value );
1022
+ uint32_t internal_img_addr = 0 ; /* either the reset handler addres or the image beginning addres */
1023
+ uint32_t min_addr ;
1024
+ uint32_t max_addr ;
1016
1025
1017
- if (flash_area_read (fap , reset_addr , & reset_value , sizeof (reset_value )) != 0 ) {
1026
+ min_addr = flash_area_get_off (BOOT_IMG_AREA (state , BOOT_SLOT_PRIMARY ));
1027
+ max_addr = flash_area_get_size (BOOT_IMG_AREA (state , BOOT_SLOT_PRIMARY )) + min_addr ;
1028
+
1029
+ /* MCUBOOT_CHECK_HEADER_LOAD_ADDRESS takes priority over MCUBOOT_VERIFY_IMG_ADDRESS */
1030
+ #ifdef MCUBOOT_CHECK_HEADER_LOAD_ADDRESS
1031
+ internal_img_addr = secondary_hdr -> ih_load_addr ;
1032
+ #else
1033
+ /* This is platform specific code that should not be here */
1034
+ const uint32_t offset = secondary_hdr -> ih_hdr_size + RESET_OFFSET ;
1035
+ BOOT_LOG_DBG ("Getting image %d internal addr from offset %u" ,
1036
+ BOOT_CURR_IMG (state ), offset );
1037
+ if (flash_area_read (fap , offset , & internal_img_addr , sizeof (internal_img_addr )) != 0 )
1038
+ BOOT_LOG_ERR ("Failed to read image %d load address" , BOOT_CURR_IMG (state ));
1018
1039
fih_rc = FIH_NO_BOOTABLE_IMAGE ;
1019
1040
goto out ;
1020
1041
}
1042
+ #endif
1021
1043
1022
- if (reset_value < pri_fa -> fa_off || reset_value > (pri_fa -> fa_off + pri_fa -> fa_size )) {
1044
+ BOOT_LOG_DBG ("Image %d expected load address 0x%x" , BOOT_CURR_IMG (state ), internal_img_addr );
1045
+ BOOT_LOG_DBG ("Check 0x%x is within [min_addr, max_addr] = [0x%x, 0x%x)" ,
1046
+ internal_img_addr , min_addr , max_addr );
1047
+ if (internal_img_addr < min_addr || internal_img_addr >= max_addr ) {
1023
1048
BOOT_LOG_ERR ("Reset address of image in secondary slot is not in the primary slot" );
1024
1049
BOOT_LOG_ERR ("Erasing image from secondary slot" );
1025
1050
0 commit comments