Fix corruption of last boot sector with encryption#592
Merged
rizlik merged 4 commits intowolfSSL:masterfrom Sep 12, 2025
Merged
Fix corruption of last boot sector with encryption#592rizlik merged 4 commits intowolfSSL:masterfrom
rizlik merged 4 commits intowolfSSL:masterfrom
Conversation
As reported by @reza-hdd in wolfSSL#589: > When Firmware is large enough to consume all the allocated boot partition sectors, in the the final swap and erase operations (_wolfBoot_swap_and_final_erase_) the last sector of FW in Boot partition, which is used as a temporary sector for a copy operation, gets corrupted after being copied in external Swap partition and copied back. It looks like the contents are copied to the external Swap partition without being encrypted, but get decrypted when it is read back. This was due to the function wolfBoot_copy_sector() assuming that all copies having an external SWAP as destination would not need any encryption, as the "normal" case during update is to copy already encrypted sectors from UPDATE->SWAP. In the final state, after the update is successfully applied, the mechanism saves a copy of the last sector from BOOT->SWAP. In this case, if the application is big enough, an extra function is needed to encrypt the content of this last sector before writing it to SWAP. Restoring the backup is OK (normal case SWAP->BOOT, decryption was already there as also noted by the reporter).
rizlik
requested changes
Sep 11, 2025
rizlik
requested changes
Sep 11, 2025
rizlik
approved these changes
Sep 12, 2025
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
As reported by @reza-hdd in #589:
This was due to the function wolfBoot_copy_sector() assuming that all copies having an external SWAP as destination would not need any encryption, as the "normal" case during update is to copy already encrypted sectors from UPDATE->SWAP.
In the final state, after the update is successfully applied, the mechanism saves a copy of the last sector from BOOT->SWAP. In this case, if the application is big enough, an extra function is needed to encrypt the content of this last sector before writing it to SWAP.
Restoring the backup is OK (normal case SWAP->BOOT, decryption was already there as also noted by the reporter).
Testing
I was able to reproduce the issue in the simulator by applying the following patch to the test application:
FILLER_SIZE is arbitrary, and adjusted on my setup to produce a test image.bin as big to fill up to the third-last sector (sector size in sim= 0x1000, two sectors reserved for redundancy of
NVM_FLASH_WRITEONCEworkaround, 0x200 or 0x400 for manifest header).I used the following configuration:
and built the test case for the update:
Running the update test:
Would fail on current master branch, as reported. With this fix the test is successful, and the last sector is encrypted during the backup step.