Skip to content
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

boot: bootutil: Fix invalid last sector computation for swap-scratch #2205

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

taltenbach
Copy link
Contributor

@taltenbach taltenbach commented Feb 15, 2025

At the beginning of a swap-scratch upgrade, the index of the last sector in the primary slot that needs to be swapped is computed using the find_last_sector_idx routine. However, if the primary slot is composed of larger sectors than the secondary slot, this routine could return a wrong sector index for the primary slot. The index might even be outside the primary slot, which would lead to at best a simple failure of the upgrade and at worst a corruption of the flash memory bricking the device.

The issue hasn't be detected in the simulator until now since among the tested configurations, none used a primary slot composed of larger sectors than the secondary slot. This MR adds such a configuration, which as expected lead to the tests to fail:

$ cargo test --features "validate-primary-slot" -- norevert
[...]
core-e6747e257778d78c: ../../boot/bootutil/src/swap_scratch.c:647: boot_swap_sectors: Assertion `rc == 0' failed.
error: test failed, to rerun pass `-p bootsim --test core`

The problem is that, with such a configuration, the find_last_sector_idx returns an index of a sector in the secondary slot. This MR fixes that by ensuring find_last_sector_idx always returns a valid sector index for the primary slot.

The simulator was not performing any test with a configuration where the
primary slot is composed of larger sectors than the secondary slot.
This can be typically case when using a STM32 with an external flash
memory since most STM32 have large internal sectors. This configuration
was causing issues when using the swap-scratch upgrade strategy.

Signed-off-by: Thomas Altenbach <[email protected]>
At the beginning of a swap-scratch upgrade, the index of the last sector
in the primary slot that need to be swapped is computed using the
'find_last_sector_idx' routine. However, if the primary slot is composed
of larger sectors than the secondary slots, this routine could return a
wrong sector index for the primary slot. The index might even be outside
the primary slot, which would lead to (at best) a simple failure of the
upgrade and at worst a corruption of the flash memory bricking the
device.

This commit fixes the issue by ensuring 'find_last_sector_idx' always
returns a valid sector index for the primary slot.

Signed-off-by: Thomas Altenbach <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant