Skip to content

Commit 348bc33

Browse files
committed
[nrf noup] boot/bootutil/loader: image discovery by ih_load_address
Align all image's matching exercises to MCUBOOT_CHECK_HEADER_LOAD_ADDRES. This method uses ih_load_address field of the image header instead of reset vector address. This allows to match incoming image to the partition even when it is for instance encrypted, as the image header is always plain-text. Firmware need to be signed with imgtool.py sign --rom-fixed <partition_address> parameter in order to involve this feature. ref.: NCSIDB-1173 Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
1 parent cb3f8dc commit 348bc33

1 file changed

Lines changed: 31 additions & 17 deletions

File tree

boot/bootutil/src/loader.c

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,30 +1043,38 @@ boot_validated_swap_type(struct boot_loader_state *state,
10431043
#if defined(MCUBOOT_IS_SECOND_STAGE) || CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER != -1
10441044
const struct flash_area *secondary_fa = BOOT_IMG_AREA(state, BOOT_SLOT_SECONDARY);
10451045
struct image_header *hdr = boot_img_hdr(state, BOOT_SLOT_SECONDARY);
1046-
uint32_t reset_addr = 0;
1046+
uint32_t internal_img_addr = 0;
10471047
int rc = 0;
10481048
/* Patch needed for NCS. Since image 0 (the app) and image 1 (the other
10491049
* B1 slot S0 or S1) share the same secondary slot, we need to check
10501050
* whether the update candidate in the secondary slot is intended for
1051-
* image 0 or image 1 primary by looking at the address of the reset
1052-
* vector. Note that there are good reasons for not using img_num from
1053-
* the swap info.
1051+
* image 0 or image 1 primary. With MCUBOOT_CHECK_HEADER_LOAD_ADDRESS the
1052+
* image header load address is used; otherwise the reset vector is read
1053+
* from the image. Note that there are good reasons for not using img_num
1054+
* from the swap info.
10541055
*/
10551056
NSIB_OWNED_UNSET(BOOT_CURR_IMG(state));
10561057

10571058
if (hdr->ih_magic == IMAGE_MAGIC) {
1058-
rc = flash_area_read(secondary_fa, hdr->ih_hdr_size +
1059-
sizeof(uint32_t), &reset_addr,
1060-
sizeof(reset_addr));
1059+
#ifdef MCUBOOT_CHECK_HEADER_LOAD_ADDRESS
1060+
internal_img_addr = hdr->ih_load_addr;
1061+
#else
1062+
rc = flash_area_read(secondary_fa, hdr->ih_hdr_size + RESET_OFFSET,
1063+
&internal_img_addr, sizeof(internal_img_addr));
10611064
if (rc != 0) {
10621065
return BOOT_SWAP_TYPE_FAIL;
10631066
}
1067+
#endif
1068+
1069+
BOOT_LOG_DBG("boot_validated_swap_type: image %d addr 0x%x",
1070+
BOOT_CURR_IMG(state), internal_img_addr);
10641071

10651072
sec_slot_touch(state);
10661073

10671074
#ifdef MCUBOOT_IS_SECOND_STAGE
10681075
#if CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER != -1
1069-
if(!(reset_addr >= NETCPU_APP_SLOT_OFFSET && reset_addr < NETCPU_APP_SLOT_END))
1076+
if (!(internal_img_addr >= NETCPU_APP_SLOT_OFFSET &&
1077+
internal_img_addr < NETCPU_APP_SLOT_END))
10701078
#endif
10711079
{
10721080
const struct flash_area *primary_fa;
@@ -1077,9 +1085,14 @@ boot_validated_swap_type(struct boot_loader_state *state,
10771085
if (rc != 0) {
10781086
return BOOT_SWAP_TYPE_FAIL;
10791087
}
1088+
1089+
const uint32_t pri_off = flash_area_get_off(primary_fa);
1090+
const uint32_t pri_end = pri_off + flash_area_get_size(primary_fa);
1091+
10801092
/* Check start and end of primary slot for current image */
1081-
if (reset_addr >= SECOND_STAGE_INACTIVE_MCUBOOT_OFFSET &&
1082-
reset_addr <= (SECOND_STAGE_INACTIVE_MCUBOOT_OFFSET + SECOND_STAGE_INACTIVE_MCUBOOT_SIZE)) {
1093+
if (internal_img_addr >= SECOND_STAGE_INACTIVE_MCUBOOT_OFFSET &&
1094+
internal_img_addr < (SECOND_STAGE_INACTIVE_MCUBOOT_OFFSET +
1095+
SECOND_STAGE_INACTIVE_MCUBOOT_SIZE)) {
10831096
if (BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER) {
10841097
/* This is not the s0/s1 upgrade image but the application image, pretend
10851098
* there is no image so the NSIB update can be loaded
@@ -1088,21 +1101,22 @@ boot_validated_swap_type(struct boot_loader_state *state,
10881101
}
10891102

10901103
NSIB_OWNED_SET(BOOT_CURR_IMG(state));
1091-
} else if (reset_addr >= SECOND_STAGE_ACTIVE_MCUBOOT_OFFSET &&
1092-
reset_addr <= (SECOND_STAGE_ACTIVE_MCUBOOT_OFFSET + SECOND_STAGE_ACTIVE_MCUBOOT_SIZE)) {
1104+
} else if (internal_img_addr >= SECOND_STAGE_ACTIVE_MCUBOOT_OFFSET &&
1105+
internal_img_addr < (SECOND_STAGE_ACTIVE_MCUBOOT_OFFSET +
1106+
SECOND_STAGE_ACTIVE_MCUBOOT_SIZE)) {
10931107
/* NSIB upgrade but for the wrong slot, must be erased */
10941108
BOOT_LOG_ERR("Image in slot is for wrong s0/s1 image");
10951109
flash_area_erase(secondary_fa, 0, secondary_fa->fa_size);
10961110
sec_slot_untouch(state);
10971111
BOOT_LOG_ERR("Cleaned-up secondary slot of image %d", BOOT_CURR_IMG(state));
10981112
return BOOT_SWAP_TYPE_FAIL;
1099-
} else if (reset_addr < primary_fa->fa_off || reset_addr > (primary_fa->fa_off + primary_fa->fa_size)) {
1113+
} else if (internal_img_addr < pri_off || internal_img_addr >= pri_end) {
11001114
/* The image in the secondary slot is not intended for any */
11011115
return BOOT_SWAP_TYPE_NONE;
11021116
}
11031117

1104-
if ((primary_fa->fa_off == SECOND_STAGE_ACTIVE_MCUBOOT_OFFSET) ||
1105-
(primary_fa->fa_off == SECOND_STAGE_INACTIVE_MCUBOOT_OFFSET)) {
1118+
if ((pri_off == SECOND_STAGE_ACTIVE_MCUBOOT_OFFSET) ||
1119+
(pri_off == SECOND_STAGE_INACTIVE_MCUBOOT_OFFSET)) {
11061120
NSIB_OWNED_SET(BOOT_CURR_IMG(state));
11071121
}
11081122
}
@@ -1134,8 +1148,8 @@ boot_validated_swap_type(struct boot_loader_state *state,
11341148
* update and indicate to the caller of this function that no update is
11351149
* available
11361150
*/
1137-
if (upgrade_valid && reset_addr >= NETCPU_APP_SLOT_OFFSET &&
1138-
reset_addr < NETCPU_APP_SLOT_END) {
1151+
if (upgrade_valid && internal_img_addr >= NETCPU_APP_SLOT_OFFSET &&
1152+
internal_img_addr < NETCPU_APP_SLOT_END) {
11391153
struct image_header *hdr = (struct image_header *)secondary_fa->fa_off;
11401154
uint32_t vtable_addr = (uint32_t)hdr + hdr->ih_hdr_size;
11411155
uint32_t *net_core_fw_addr = (uint32_t *)(vtable_addr);

0 commit comments

Comments
 (0)