boot: bootutil: Fix security counter updated before revert after resuming upgrade #2282
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.
This PR fixes an issue that was preventing an interrupted then resumed upgrade to be reverted when the hardware rollback protection is enabled and the swap-scratch, swap-move or swap-offset strategy is used.
After a "test" upgrade has been performed, the security counter of every image must not be updated immediately to be able to roll back in case the upgrade is not confirmed. To that end, the
boot_update_hw_rollback_protection
routine, called just before exiting MCUboot, was checking the swap type of the image and updating the security counter only if the swap type was none, with the assumption that it implies no upgrade has been performed by MCUboot. This was working properly in the nominal case, when the upgrades are not interrupted, however this assumption is false if MCUboot has just resumed an upgrade after a power cycle because in that case the swap type is set to none after the end of the upgrade process:mcuboot/boot/bootutil/src/loader.c
Lines 2251 to 2252 in 8131548
So, let's suppose an image A with a security counter N is upgraded, in "test" mode, to an image B with a security counter M > N:
This was not detected by the tests since in the simulator the upgrades were performed with images having the same security counter value. In addition to fixing the issue, this MR also updates the simulator to use images with different security counter values, enabling to catch such kind of bugs.