Skip to content

Commit b3b7b93

Browse files
nvlsianpurlubos
authored andcommitted
[nrf noup] booutil/src/loader: ensure ABS image address comparison
Address of image shall be compared to absolut address. flash_area API provides relative address. So if flash_base address if not 0, check might fail. Patch introduce get_image_perspective_flash_area_bounds() function which gets absolute bonduaries for most cases. These bounduaries are used in comparasion where needed. Excuse is a direct-xip mode native check which still uses relative bonduaries due to backward compatibility. Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no> (cherry picked from commit dcfcd37)
1 parent 7ec4f70 commit b3b7b93

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

boot/bootutil/src/loader.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,27 @@ boot_rom_address_check(struct boot_loader_state *state)
605605
}
606606
#endif
607607

608+
#if (((defined(MCUBOOT_VERIFY_IMG_ADDRESS) && !defined(MCUBOOT_ENC_IMAGES)) \
609+
|| defined(MCUBOOT_CHECK_HEADER_LOAD_ADDRESS)) ||\
610+
defined(MCUBOOT_IS_SECOND_STAGE))
611+
static void
612+
get_image_perspective_flash_area_bounds(const struct flash_area *fa, uint32_t *start, uint32_t *end)
613+
{
614+
*start = flash_area_get_off(fa);
615+
*end = *start + flash_area_get_size(fa);
616+
617+
/* For nRF54H20 in a direct-xip mode use relative bounds due to backwards compatibility
618+
* For other platforms and modes use absolute bounds.
619+
*/
620+
621+
uintptr_t flash_base = 0;
622+
623+
if (flash_device_base(flash_area_get_device_id(fa), &flash_base) == 0) {
624+
*start += (uint32_t)flash_base;
625+
*end += (uint32_t)flash_base;
626+
}
627+
}
628+
#endif
608629
/*
609630
* Check that there is a valid image in a slot
610631
*
@@ -836,8 +857,8 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
836857
} else
837858
#endif
838859
if (BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER) {
839-
min_addr = flash_area_get_off(BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY));
840-
max_addr = flash_area_get_size(BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY)) + min_addr;
860+
get_image_perspective_flash_area_bounds(BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY),
861+
&min_addr, &max_addr);
841862

842863
#ifdef MCUBOOT_IS_SECOND_STAGE
843864
min_addr = MIN(min_addr, SECOND_STAGE_INACTIVE_MCUBOOT_OFFSET);
@@ -1095,8 +1116,11 @@ boot_validated_swap_type(struct boot_loader_state *state,
10951116
return BOOT_SWAP_TYPE_FAIL;
10961117
}
10971118

1098-
const uint32_t pri_off = flash_area_get_off(primary_fa);
1099-
const uint32_t pri_end = pri_off + flash_area_get_size(primary_fa);
1119+
const uint32_t pri_fa_off = flash_area_get_off(primary_fa);
1120+
uint32_t pri_off;
1121+
uint32_t pri_end;
1122+
1123+
get_image_perspective_flash_area_bounds(primary_fa, &pri_off, &pri_end);
11001124

11011125
/* Check start and end of primary slot for current image */
11021126
if (internal_img_addr >= SECOND_STAGE_INACTIVE_MCUBOOT_OFFSET &&
@@ -1124,8 +1148,9 @@ boot_validated_swap_type(struct boot_loader_state *state,
11241148
return BOOT_SWAP_TYPE_NONE;
11251149
}
11261150

1127-
if ((pri_off == SECOND_STAGE_ACTIVE_MCUBOOT_OFFSET) ||
1128-
(pri_off == SECOND_STAGE_INACTIVE_MCUBOOT_OFFSET)) {
1151+
/* SECOND_STAGE_*_OFFSET is PARTITION_OFFSET (fa_off space), not absolute. */
1152+
if ((pri_fa_off == SECOND_STAGE_ACTIVE_MCUBOOT_OFFSET) ||
1153+
(pri_fa_off == SECOND_STAGE_INACTIVE_MCUBOOT_OFFSET)) {
11291154
NSIB_OWNED_SET(BOOT_CURR_IMG(state));
11301155
}
11311156
}

0 commit comments

Comments
 (0)