-
Notifications
You must be signed in to change notification settings - Fork 824
Add MCUBOOT_CHECK_HEADER_LOAD_ADDRESS #2481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
de-nordic
wants to merge
2
commits into
mcu-tools:main
Choose a base branch
from
de-nordic:461
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,6 +81,13 @@ static struct image_max_size image_max_sizes[BOOT_IMAGE_NUMBER] = {0}; | |
#define TARGET_STATIC | ||
#endif | ||
|
||
#if defined(MCUBOOT_VERIFY_IMG_ADDRESS) && defined(MCUBOOT_CHECK_HEADER_LOAD_ADDRESS) | ||
#warning MCUBOOT_CHECK_HEADER_LOAD_ADDRESS takes precedence over MCUBOOT_VERIFY_IMG_ADDRESS | ||
#endif | ||
|
||
/* Valid only for ARM Cortext M */ | ||
#define RESET_OFFSET (2 * sizeof(uin32_t)) | ||
|
||
#if BOOT_MAX_ALIGN > 1024 | ||
#define BUF_SZ BOOT_MAX_ALIGN | ||
#else | ||
|
@@ -1002,24 +1009,41 @@ boot_validate_slot(struct boot_loader_state *state, int slot, | |
goto out; | ||
} | ||
|
||
#if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_VERIFY_IMG_ADDRESS) | ||
#if defined(MCUBOOT_VERIFY_IMG_ADDRESS) && !defined(MCUBOOT_ENC_IMAGES) || \ | ||
defined(MCUBOOT_CHECK_HEADER_LOAD_ADDRESS) | ||
/* Verify that the image in the secondary slot has a reset address | ||
* located in the primary slot. This is done to avoid users incorrectly | ||
* overwriting an application written to the incorrect slot. | ||
* This feature is only supported by ARM platforms. | ||
*/ | ||
if (fap == BOOT_IMG_AREA(state, BOOT_SLOT_SECONDARY)) { | ||
const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY); | ||
struct image_header *secondary_hdr = boot_img_hdr(state, slot); | ||
uint32_t reset_value = 0; | ||
uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value); | ||
uint32_t internal_img_addr = 0; /* either the reset handler addres or the image beginning addres */ | ||
uint32_t min_addr; | ||
uint32_t max_addr; | ||
|
||
if (flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value)) != 0) { | ||
min_addr = flash_area_get_off(BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY)); | ||
max_addr = flash_area_get_size(BOOT_IMG_AREA(state, BOOT_SLOT_PRIMARY)) + min_addr; | ||
|
||
/* MCUBOOT_CHECK_HEADER_LOAD_ADDRESS takes priority over MCUBOOT_VERIFY_IMG_ADDRESS */ | ||
#ifdef MCUBOOT_CHECK_HEADER_LOAD_ADDRESS | ||
internal_img_addr = secondary_hdr->ih_load_addr; | ||
#else | ||
/* This is platform specific code that should not be here */ | ||
const uint32_t offset = secondary_hdr->ih_hdr_size + RESET_OFFSET; | ||
BOOT_LOG_DBG("Getting image %d internal addr from offset %u", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would mark this as deprecated then remove it in 2 releases |
||
BOOT_CURR_IMG(state), offset); | ||
if (flash_area_read(fap, offset, &internal_img_addr, sizeof(internal_img_addr)) != 0) | ||
BOOT_LOG_ERR("Failed to read image %d load address", BOOT_CURR_IMG(state)); | ||
fih_rc = FIH_NO_BOOTABLE_IMAGE; | ||
goto out; | ||
} | ||
#endif | ||
|
||
if (reset_value < pri_fa->fa_off || reset_value> (pri_fa->fa_off + pri_fa->fa_size)) { | ||
BOOT_LOG_DBG("Image %d expected load address 0x%x", BOOT_CURR_IMG(state), internal_img_addr); | ||
BOOT_LOG_DBG("Check 0x%x is within [min_addr, max_addr] = [0x%x, 0x%x)", | ||
internal_img_addr, min_addr, max_addr); | ||
if (internal_img_addr < min_addr || internal_img_addr >= max_addr) { | ||
BOOT_LOG_ERR("Reset address of image in secondary slot is not in the primary slot"); | ||
BOOT_LOG_ERR("Erasing image from secondary slot"); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing bracket here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is. I am still testing this locally and trying to make it run with sim.