Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add swap using offset mode #2162

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

Conversation

nordicjm
Copy link
Collaborator

@nordicjm nordicjm commented Jan 2, 2025

Fixes #2134

165KiB update using swap using move on nrf5340dk: 18 seconds
182KiB update using swap using offset on nrf5340dk: 13 seconds

@nordicjm nordicjm requested a review from de-nordic January 2, 2025 12:29
@nordicjm nordicjm force-pushed the swaplesserase branch 5 times, most recently from 1f86386 to 484d0f5 Compare January 3, 2025 12:53
@Laczen
Copy link

Laczen commented Jan 4, 2025

@nordicjm this is a very nice improvement. There are some minor documentation nits.

A further possible improvement could be to place the swap state (image trailer) in the update image slot (this will not be erased during the swap, and thus the image could go up to the slot size - trailer size).

@nordicjm
Copy link
Collaborator Author

nordicjm commented Jan 6, 2025

@nordicjm this is a very nice improvement. There are some minor documentation nits.

A further possible improvement could be to place the swap state (image trailer) in the update image slot (this will not be erased during the swap, and thus the image could go up to the slot size - trailer size).

I left it in the first as with the "move" slot being moved to the second image, the overhead in both slots is now about equal (swap status in primary, move slot in secondary). However I'm not sure if this mode will actually be finished, the simulator is a giant fight and lots of edge cases need handling in application code to account for the secondary slot image being at either offset, and as of right now the simulator failures make no sense to me

@nordicjm nordicjm force-pushed the swaplesserase branch 5 times, most recently from f61a84d to 546b5b5 Compare January 10, 2025 13:38
@Laczen
Copy link

Laczen commented Jan 11, 2025

Some food for further thoughts: I have created a similar approach outside of mcuboot, by using a different terminology:
A "backup slot" is defined that starts one sector before the "update slot" and has the same size of the "update slot" and the "image slot", so the backup slot covers a part of the "update slot" (ends one sector sooner).

The image update moves one sector from image to backup followed by a move from update to image, when the update is finished the old image is in the backup slot and the new image is in the image slot. A restore is a move from the backup slot to the image slot.

The method also allows removing the "swap with scratch" as the "scratch" sector can be defined as the first sector of the backup image.

For devices that have plenty of flash the backup slot can also be defined separate from the update slot to enhance the life and keep flash wear equal for all slots (update erases all flash only once).

@nordicjm
Copy link
Collaborator Author

The method also allows removing the "swap with scratch" as the "scratch" sector can be defined as the first sector of the backup image.

The plan is that swap using scratch will be removed in the coming future, but as it stands right now this would not work, e.g. on an stm32 where the sector sizes are 32KiB, 32KiB, 64KiB, 128KiB, 128KiB - making it the first sector give a scratch size of 32KiB which means you can't complete a single swap. The idea is that MCUboot's "sector size" will not be the flash sector size in future, it will be able to be configured so in the case of the stm32 will be 128KiB, in the case of a device with internal and external flash whereby internal flash has 2KiB sectors and external flash has 8KiB sectors, both will be 8KiB - once this change is made it means that swap using scratch can be removed entirely because despite the device have different sector sizes, the move is done in multiples of the largest sector size. The added benefit of this is that the code is smaller and swap status area is smaller too

@nordicjm nordicjm marked this pull request as ready for review January 13, 2025 08:11
@nordicjm nordicjm force-pushed the swaplesserase branch 3 times, most recently from 769bca3 to 9e28dea Compare January 13, 2025 10:18
@butok
Copy link
Contributor

butok commented Jan 14, 2025

Hi @nordicjm
SWAP_USING_OFFSET is better than SWAP_USING_MOVE.
Are there any cases where SWAP_USING_MOVE is preferable over OFFSET?

@nordicjm
Copy link
Collaborator Author

Hi @nordicjm SWAP_USING_OFFSET is better than SWAP_USING_MOVE. Are there any cases where SWAP_USING_MOVE is preferable over OFFSET?

When building for existing products where the bootloader is configured in swap using move mode, other than that swap using offset should become the new default

@nordicjm nordicjm force-pushed the swaplesserase branch 2 times, most recently from d12d10d to 8cf39b8 Compare January 15, 2025 08:16
Refactors some functions so that the state variable is present in it

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Adds a new variation of the swap using move mode, named swap using
offset, whereby instead of moving all the sectors in the primary
image, the sectors in the secondary image are offset instead. This
fastens image swapping time both for updates and reverts as each
sector in both slots is erased only once, which also reduces flash
wear, and uses less swap status bits to represent

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Adds support for using this mode to zephyr

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Adds support for getting the sector size of less sectors than are
in an image, which mirrors support in zephyr and allows getting
just the size of the first sector in an image

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Enables testing this new mode

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Fixes not using a pointer as a pointer

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Adds details on how this new mode works

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Adds a note about this new algorithm being added

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Fixes some comments which had typos or were not formatted correctly

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Adds support for serial recovery of images when MCUboot is set to
swap using offset mode

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Copy link
Member

@d3zd3z d3zd3z left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I think this will make a good default.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RFC: Swap using move but using extra sector in slot 1
4 participants