Skip to content

Commit db3fe90

Browse files
committed
[nrf noup] boot/bootutil/loader: image discovery by ih_load_address
Jakis Opis musi byc ref.: NCSIDB-1173 Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
1 parent f123819 commit db3fe90

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

boot/bootutil/src/loader.c

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,15 +1249,19 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
12491249
if (fap == BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT)) {
12501250
const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
12511251
struct image_header *secondary_hdr = boot_img_hdr(state, slot);
1252-
uint32_t reset_value = 0;
1253-
uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value);
1252+
uint32_t internal_img_addr = 0; /* either the reset handler addres or the image beginning addres */
12541253
uint32_t min_addr, max_addr;
12551254
bool check_addresses = false;
12561255

1257-
if (flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value)) != 0) {
1256+
#ifdef CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDR
1257+
internal_img_addr = secondary_hdr->ih_load_addr;
1258+
#endif
1259+
if (flash_area_read(fap, secondary_hdr->ih_hdr_size + sizeof(internal_img_addr),
1260+
&internal_img_addr, sizeof(internal_img_addr)) != 0) {
12581261
fih_rc = FIH_NO_BOOTABLE_IMAGE;
12591262
goto out;
12601263
}
1264+
#else /* BOOT_USE_CHECK_LOAD_ADDR */
12611265

12621266
#ifdef PM_CPUNET_APP_ADDRESS
12631267
/* The primary slot for the network core is emulated in RAM.
@@ -1298,7 +1302,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
12981302
check_addresses = true;
12991303
}
13001304

1301-
if (check_addresses == true && (reset_value < min_addr || reset_value > max_addr)) {
1305+
if (check_addresses == true && (internal_img_addr < min_addr || internal_img_addr > max_addr)) {
13021306
BOOT_LOG_ERR("Reset address of image in secondary slot is not in the primary slot");
13031307
BOOT_LOG_ERR("Erasing image from secondary slot");
13041308

@@ -1515,6 +1519,17 @@ static inline void sec_slot_cleanup_if_unusable(void)
15151519
#endif /* defined(CONFIG_MCUBOOT_CLEANUP_UNUSABLE_SECONDARY) &&\
15161520
defined(PM_S1_ADDRESS) || defined(CONFIG_SOC_NRF5340_CPUAPP) */
15171521

1522+
#define IS_IN_RANGE_CPUNET_APP_ADDR(_addr) ((_addr) >= PM_CPUNET_APP_ADDRESS && (_addr) < PM_CPUNET_APP_END_ADDRESS))
1523+
#define _IS_IN_RANGE_S_VARIANT_ADDR(_addr, x) ((_addr) >= PM_S##x_ADDRESS && (_addr) <= (PM_S##x_ADDRESS + PM_S##x_SIZE))
1524+
#if (CONFIG_NCS_IS_VARIANT_IMAGE)
1525+
#define IS_IN_RANGE_S_ALTERNATE_ADDR(_addr) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 0)
1526+
#define IS_IN_RANGE_S_CURRENT_ADDR(_addr) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 1)
1527+
#else
1528+
#define IS_IN_RANGE_S_ALTERNATE_ADDR(_addr) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 1)
1529+
#define IS_IN_RANGE_S_CURRENT_ADDR(_addr) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 0)
1530+
#endif
1531+
#define IS_IN_RANGE_IMAGE_ADDR(_addr, _fa) ((_addr) >= _fa->fa_off && (_addr) < (_fa->fa_off + _fa->fa_size))
1532+
15181533
/**
15191534
* Determines which swap operation to perform, if any. If it is determined
15201535
* that a swap operation is required, the image in the secondary slot is checked
@@ -1538,8 +1553,9 @@ boot_validated_swap_type(struct boot_loader_state *state,
15381553
const struct flash_area *secondary_fa =
15391554
BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
15401555
struct image_header *hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
1541-
uint32_t reset_addr = 0;
1556+
uint32_t internal_img_addr = 0; /* either the reset handler addres or the image beginning addres */
15421557
int rc = 0;
1558+
15431559
/* Patch needed for NCS. Since image 0 (the app) and image 1 (the other
15441560
* B1 slot S0 or S1) share the same secondary slot, we need to check
15451561
* whether the update candidate in the secondary slot is intended for
@@ -1549,18 +1565,22 @@ boot_validated_swap_type(struct boot_loader_state *state,
15491565
*/
15501566

15511567
if (hdr->ih_magic == IMAGE_MAGIC) {
1568+
#ifdef CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDR
1569+
internal_img_addr = hdr->ih_load_addr;
1570+
#else
15521571
rc = flash_area_read(secondary_fa, hdr->ih_hdr_size +
1553-
sizeof(uint32_t), &reset_addr,
1554-
sizeof(reset_addr));
1572+
sizeof(uint32_t), &internal_img_addr,
1573+
sizeof(internal_img_addr));
15551574
if (rc != 0) {
15561575
return BOOT_SWAP_TYPE_FAIL;
15571576
}
1577+
#endif /* CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDR */
15581578

15591579
sec_slot_touch(state);
15601580

15611581
#ifdef PM_S1_ADDRESS
15621582
#ifdef PM_CPUNET_B0N_ADDRESS
1563-
if(!(reset_addr >= PM_CPUNET_APP_ADDRESS && reset_addr < PM_CPUNET_APP_END_ADDRESS))
1583+
if(!IS_IN_RANGE_CPUNET_APP_ADDR(internal_img_addr))
15641584
#endif
15651585
{
15661586
const struct flash_area *primary_fa;
@@ -1572,11 +1592,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
15721592
}
15731593

15741594
/* Check start and end of primary slot for current image */
1575-
#if (CONFIG_NCS_IS_VARIANT_IMAGE)
1576-
if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE)) {
1577-
#else
1578-
if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE)) {
1579-
#endif
1595+
if (IS_IN_RANGE_S_VARIANT_ADDR(internal_img_addr)) {
15801596
if (BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER) {
15811597
/* This is not the s0/s1 upgrade image but the application image, pretend
15821598
* there is no image so the NSIB update can be loaded
@@ -1585,18 +1601,14 @@ boot_validated_swap_type(struct boot_loader_state *state,
15851601
}
15861602

15871603
owner_nsib[BOOT_CURR_IMG(state)] = true;
1588-
#if (CONFIG_NCS_IS_VARIANT_IMAGE)
1589-
} else if (reset_addr >= PM_S1_ADDRESS && reset_addr <= (PM_S1_ADDRESS + PM_S1_SIZE)) {
1590-
#else
1591-
} else if (reset_addr >= PM_S0_ADDRESS && reset_addr <= (PM_S0_ADDRESS + PM_S0_SIZE)) {
1592-
#endif
1604+
} else if (IS_IN_RANGE_S_CURRENT_ADDR(internal_img_addr)) {
15931605
/* NSIB upgrade but for the wrong slot, must be erased */
15941606
BOOT_LOG_ERR("Image in slot is for wrong s0/s1 image");
15951607
flash_area_erase(secondary_fa, 0, secondary_fa->fa_size);
15961608
sec_slot_untouch(state);
15971609
BOOT_LOG_ERR("Cleaned-up secondary slot of image %d", BOOT_CURR_IMG(state));
15981610
return BOOT_SWAP_TYPE_FAIL;
1599-
} else if (reset_addr < primary_fa->fa_off || reset_addr > (primary_fa->fa_off + primary_fa->fa_size)) {
1611+
} else if (!IS_IN_RANGE_IMAGE_ADDR(internal_img_addr, primary_fa)) {
16001612
/* The image in the secondary slot is not intended for any */
16011613
return BOOT_SWAP_TYPE_NONE;
16021614
}
@@ -1633,8 +1645,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
16331645
* update and indicate to the caller of this function that no update is
16341646
* available
16351647
*/
1636-
if (upgrade_valid && reset_addr >= PM_CPUNET_APP_ADDRESS &&
1637-
reset_addr < PM_CPUNET_APP_END_ADDRESS) {
1648+
if (upgrade_valid && IS_IN_RANGE_CPUNET_APP_ADDR(internal_img_addr)) {
16381649
struct image_header *hdr = (struct image_header *)secondary_fa->fa_off;
16391650
uint32_t vtable_addr = (uint32_t)hdr + hdr->ih_hdr_size;
16401651
uint32_t *net_core_fw_addr = (uint32_t *)(vtable_addr);

boot/zephyr/Kconfig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,10 +1194,17 @@ config USB_DEVICE_PRODUCT
11941194
config MCUBOOT_BOOTUTIL_LIB_OWN_LOG
11951195
bool
11961196

1197+
config MCUBOOT_USE_CHECK_LOAD_ADDR
1198+
bool "use check of load address"
1199+
help
1200+
If y, the bootloader will use the load address form image header
1201+
for checking to which slot image belongs instead of usage of reset
1202+
handler addres reading form the image.
1203+
11971204
config MCUBOOT_VERIFY_IMG_ADDRESS
11981205
bool "Verify reset address of image in secondary slot"
11991206
depends on UPDATEABLE_IMAGE_NUMBER > 1
1200-
depends on !BOOT_ENCRYPT_IMAGE
1207+
depends on !BOOT_ENCRYPT_IMAGE || MCUBOOT_USE_CHECK_LOAD_ADDR
12011208
depends on ARM
12021209
default y if BOOT_UPGRADE_ONLY
12031210
help

0 commit comments

Comments
 (0)