Skip to content

Commit 4752c56

Browse files
nvlsianpude-nordic
authored andcommitted
[nrf noup] boot/bootutil/loader: image discovery by ih_load_address
nrf-squash! [nrf noup] treewide: Add support for sysbuild assigned images Introduce alternative procedure for detecting to which partition image candidate belongs. 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. This new procedure can be enabled using CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDR=y. Firmware need to be signed with imgtool.py sign --rom-fixed <partition_address> parameter. ref.: NCSIDB-1173 Signed-off-by: Andrzej Puzdrowski <[email protected]>
1 parent 754f958 commit 4752c56

File tree

2 files changed

+108
-53
lines changed

2 files changed

+108
-53
lines changed

boot/bootutil/src/loader.c

Lines changed: 99 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,29 @@ int pcd_version_cmp_net(const struct flash_area *fap, struct image_header *hdr);
8888
void 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+
91114
BOOT_LOG_MODULE_DECLARE(mcuboot);
92115

93116
static struct boot_loader_state boot_data;
@@ -1154,7 +1177,8 @@ 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 defined(MCUBOOT_VERIFY_IMG_ADDRESS) && !defined(MCUBOOT_ENC_IMAGES) || \
1181+
defined(CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDRESS)
11581182
/* Verify that the image in the secondary slot has a reset address
11591183
* located in the primary slot. This is done to avoid users incorrectly
11601184
* overwriting an application written to the incorrect slot.
@@ -1171,58 +1195,72 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
11711195
}
11721196
#endif
11731197
if (fap == BOOT_IMG_AREA(state, BOOT_SLOT_SECONDARY)) {
1174-
const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY);
11751198
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;
1199+
uint32_t internal_img_addr = 0; /* either the reset handler addres or the image beginning addres */
1200+
uint32_t min_addr;
1201+
uint32_t max_addr;
1202+
1203+
min_addr = flash_area_get_off(BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY));
1204+
max_addr = flash_area_get_size(BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY)) + min_addr;
11801205

1181-
if (flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value)) != 0) {
1206+
#ifdef CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDRESS
1207+
internal_img_addr = secondary_hdr->ih_load_addr;
1208+
#else
1209+
const uint32_t offset = secondary_hdr->ih_hdr_size + sizeof(internal_img_addr);
1210+
BOOT_LOG_DBG("Getting image %d internal addr from offset %u",
1211+
BOOT_CURR_IMG(state), offset);
1212+
/* Read reset vector from ARM vector table */
1213+
if (flash_area_read(fap, offset, &internal_img_addr, sizeof(internal_img_addr)) != 0) {
1214+
BOOT_LOG_ERR("Failed to read image %d load address", BOOT_CURR_IMG(state));
11821215
fih_rc = FIH_NO_BOOTABLE_IMAGE;
11831216
goto out;
11841217
}
1218+
#endif
1219+
BOOT_LOG_DBG("Image %d expected load address 0x%x", BOOT_CURR_IMG(state), internal_img_addr);
11851220

1186-
#ifdef PM_CPUNET_APP_ADDRESS
1221+
#ifdef CONFIG_PARTITION_MANAGER_ENABLED
11871222
/* The primary slot for the network core is emulated in RAM.
11881223
* Its flash_area hasn't got relevant boundaries.
11891224
* Therfore need to override its boundaries for the check.
11901225
*/
1226+
#ifdef PM_CPUNET_APP_ADDRESS
11911227
if (BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_NETWORK_CORE_IMAGE_NUMBER) {
1228+
1229+
BOOT_LOG_DBG("Image %d is NETCORE image", BOOT_CURR_IMG(state));
1230+
11921231
min_addr = PM_CPUNET_APP_ADDRESS;
11931232
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
1233+
}
1234+
#endif /* PM_CPUNET_APP_SIZE */
1235+
1236+
#if CHECK_MCUBOOT_IMAGE == 1
11981237
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
1238+
1239+
BOOT_LOG_DBG("Image %d is another stage MCBoot image", BOOT_CURR_IMG(state));
1240+
1241+
min_addr = NCS_VARIANT_SLOT_MIN_ADDR;
1242+
max_addr = NCS_VARIANT_SLOT_MAX_ADDR;
1243+
}
1244+
#endif /* CHECK_MCUBOOT_IMAGE */
1245+
1246+
#if CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER != -1
12091247
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;
1248+
1249+
BOOT_LOG_DBG("Image %d is application MCBoot image", BOOT_CURR_IMG(state));
1250+
1251+
/* If not checking the MCUboot image, then we leave the original
1252+
* min/max assignments from the primary slot read. */
1253+
#if CHECK_MCUBOOT_IMAGE == 1
1254+
min_addr = MIN(min_addr, NCS_VARIANT_SLOT_MIN_ADDR);
1255+
max_addr = MAX(max_addr, NCS_VARIANT_SLOT_MAX_ADDR);
12211256
#endif
1222-
check_addresses = true;
12231257
}
1258+
#endif
1259+
#endif /* CONFIG_PARTITION_MANAGER_ENABLED */
12241260

1225-
if (check_addresses == true && (reset_value < min_addr || reset_value > max_addr)) {
1261+
BOOT_LOG_DBG("Check 0x%x is within [min_addr, max_addr] = [0x%x, 0x%x)",
1262+
internal_img_addr, min_addr, max_addr);
1263+
if (internal_img_addr < min_addr || internal_img_addr >= max_addr) {
12261264
BOOT_LOG_ERR("Reset address of image in secondary slot is not in the primary slot");
12271265
BOOT_LOG_ERR("Erasing image from secondary slot");
12281266

@@ -1236,6 +1274,7 @@ boot_validate_slot(struct boot_loader_state *state, int slot,
12361274
fih_rc = FIH_NO_BOOTABLE_IMAGE;
12371275
goto out;
12381276
}
1277+
BOOT_LOG_DBG("Image %d validation OK", BOOT_CURR_IMG(state));
12391278
}
12401279
#endif
12411280

@@ -1439,6 +1478,18 @@ static inline void sec_slot_cleanup_if_unusable(void)
14391478
#endif /* defined(CONFIG_MCUBOOT_CLEANUP_UNUSABLE_SECONDARY) &&\
14401479
defined(PM_S1_ADDRESS) || defined(CONFIG_SOC_NRF5340_CPUAPP) */
14411480

1481+
#define IS_IN_RANGE_CPUNET_APP_ADDR(_addr) (((_addr) >= PM_CPUNET_APP_ADDRESS) && ((_addr) < PM_CPUNET_APP_END_ADDRESS))
1482+
#define _IS_IN_RANGE_S_VARIANT_ADDR(_addr, x) (((_addr) >= PM_S##x##_ADDRESS) && ((_addr) <= (PM_S##x##_ADDRESS + PM_S##x##_SIZE)))
1483+
#if defined(CONFIG_NCS_IS_VARIANT_IMAGE)
1484+
#define IS_IN_RANGE_S_ALTERNATE_ADDR(_addr) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 0)
1485+
#define IS_IN_RANGE_S_CURRENT_ADDR(_addr) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 1)
1486+
#else
1487+
#define IS_IN_RANGE_S_ALTERNATE_ADDR(_addr) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 1)
1488+
#define IS_IN_RANGE_S_CURRENT_ADDR(_addr) _IS_IN_RANGE_S_VARIANT_ADDR(_addr, 0)
1489+
#endif
1490+
#define IS_IN_RANGE_IMAGE_ADDR(_addr, _fa) \
1491+
(((_addr) >= flash_area_get_off(_fa)) && (_addr) < (flash_area_get_off(_fa) + flash_area_get_size(_fa)))
1492+
14421493
/**
14431494
* Determines which swap operation to perform, if any. If it is determined
14441495
* that a swap operation is required, the image in the secondary slot is checked
@@ -1462,8 +1513,9 @@ boot_validated_swap_type(struct boot_loader_state *state,
14621513
const struct flash_area *secondary_fa =
14631514
BOOT_IMG_AREA(state, BOOT_SLOT_SECONDARY);
14641515
struct image_header *hdr = boot_img_hdr(state, BOOT_SLOT_SECONDARY);
1465-
uint32_t reset_addr = 0;
1516+
uint32_t internal_img_addr = 0; /* either the reset handler addres or the image beginning addres */
14661517
int rc = 0;
1518+
14671519
/* Patch needed for NCS. Since image 0 (the app) and image 1 (the other
14681520
* B1 slot S0 or S1) share the same secondary slot, we need to check
14691521
* whether the update candidate in the secondary slot is intended for
@@ -1473,18 +1525,22 @@ boot_validated_swap_type(struct boot_loader_state *state,
14731525
*/
14741526

14751527
if (hdr->ih_magic == IMAGE_MAGIC) {
1528+
#ifdef CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDRESS
1529+
internal_img_addr = hdr->ih_load_addr;
1530+
#else
14761531
rc = flash_area_read(secondary_fa, hdr->ih_hdr_size +
1477-
sizeof(uint32_t), &reset_addr,
1478-
sizeof(reset_addr));
1532+
sizeof(uint32_t), &internal_img_addr,
1533+
sizeof(internal_img_addr));
14791534
if (rc != 0) {
14801535
return BOOT_SWAP_TYPE_FAIL;
14811536
}
1537+
#endif /* CONFIG_MCUBOOT_USE_CHECK_LOAD_ADDRESS */
14821538

14831539
sec_slot_touch(state);
14841540

14851541
#ifdef PM_S1_ADDRESS
14861542
#ifdef PM_CPUNET_B0N_ADDRESS
1487-
if(!(reset_addr >= PM_CPUNET_APP_ADDRESS && reset_addr < PM_CPUNET_APP_END_ADDRESS))
1543+
if(!IS_IN_RANGE_CPUNET_APP_ADDR(internal_img_addr))
14881544
#endif
14891545
{
14901546
const struct flash_area *primary_fa;
@@ -1496,11 +1552,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
14961552
}
14971553

14981554
/* 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
1555+
if (IS_IN_RANGE_S_ALTERNATE_ADDR(internal_img_addr)) {
15041556
if (BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_APPLICATION_IMAGE_NUMBER) {
15051557
/* This is not the s0/s1 upgrade image but the application image, pretend
15061558
* there is no image so the NSIB update can be loaded
@@ -1509,18 +1561,14 @@ boot_validated_swap_type(struct boot_loader_state *state,
15091561
}
15101562

15111563
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
1564+
} else if (IS_IN_RANGE_S_CURRENT_ADDR(internal_img_addr)) {
15171565
/* NSIB upgrade but for the wrong slot, must be erased */
15181566
BOOT_LOG_ERR("Image in slot is for wrong s0/s1 image");
15191567
flash_area_erase(secondary_fa, 0, secondary_fa->fa_size);
15201568
sec_slot_untouch(state);
15211569
BOOT_LOG_ERR("Cleaned-up secondary slot of image %d", BOOT_CURR_IMG(state));
15221570
return BOOT_SWAP_TYPE_FAIL;
1523-
} else if (reset_addr < primary_fa->fa_off || reset_addr > (primary_fa->fa_off + primary_fa->fa_size)) {
1571+
} else if (!IS_IN_RANGE_IMAGE_ADDR(internal_img_addr, primary_fa)) {
15241572
/* The image in the secondary slot is not intended for any */
15251573
return BOOT_SWAP_TYPE_NONE;
15261574
}
@@ -1557,8 +1605,7 @@ boot_validated_swap_type(struct boot_loader_state *state,
15571605
* update and indicate to the caller of this function that no update is
15581606
* available
15591607
*/
1560-
if (upgrade_valid && reset_addr >= PM_CPUNET_APP_ADDRESS &&
1561-
reset_addr < PM_CPUNET_APP_END_ADDRESS) {
1608+
if (upgrade_valid && IS_IN_RANGE_CPUNET_APP_ADDR(internal_img_addr)) {
15621609
struct image_header *hdr = (struct image_header *)secondary_fa->fa_off;
15631610
uint32_t vtable_addr = (uint32_t)hdr + hdr->ih_hdr_size;
15641611
uint32_t *net_core_fw_addr = (uint32_t *)(vtable_addr);

boot/zephyr/Kconfig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1409,10 +1409,18 @@ config NRF_MCUBOOT_BOOT_REQUEST
14091409
bool
14101410
imply FIND_NEXT_SLOT_HOOKS if BOOT_DIRECT_XIP || BOOT_RAM_LOAD
14111411

1412+
config MCUBOOT_USE_CHECK_LOAD_ADDRESS
1413+
bool "Use load address to verify application is in proper slot"
1414+
help
1415+
The bootloader will use the load address, from the image header,
1416+
to verify that binary is in slot designated for its execution.
1417+
When not selected reset vector, read from image, is used for
1418+
the same purpose.
1419+
14121420
config MCUBOOT_VERIFY_IMG_ADDRESS
14131421
bool "Verify reset address of image in secondary slot"
14141422
depends on UPDATEABLE_IMAGE_NUMBER > 1
1415-
depends on !BOOT_ENCRYPT_IMAGE
1423+
depends on !BOOT_ENCRYPT_IMAGE || MCUBOOT_USE_CHECK_LOAD_ADDRESS
14161424
depends on ARM
14171425
default y if BOOT_UPGRADE_ONLY
14181426
help

0 commit comments

Comments
 (0)