Skip to content

boot: bootutil: Fix non-swap modes using swap overhead - #3001

Open
nordicjm wants to merge 3 commits into
mcu-tools:mainfrom
nordicjm:fixwrongswapoverhead
Open

nordicjm wants to merge 3 commits into
mcu-tools:mainfrom
nordicjm:fixwrongswapoverhead

Conversation

@nordicjm

@nordicjm nordicjm commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Note: Not for 2.5.1, for 2.6/3.0

Fixes an oversight whereby non-swapping modes that are not single
image modes were wrongly including the size of the swap area into
calculations that limit the size of the image and put image status
flags earlier than they should be placed

and

boot: zephyr: Add CONFIG_BOOT_LEGACY_SWAP_OVERHEAD Kconfig

Adds a Kconfig which can be enabled to continue using the legacy
MCUboot 2.5 and earlier swap overhead calculations (if
compatibility is required)

@d3zd3z was wondering about renaming --overwrite-only in imgtool since it's not just for upgrade/overwrite only mode, but any non-swap mode, e.g. --no-swap?

Fixes #2999

@de-nordic de-nordic added the area: core Affects core functionality label Nov 19, 2026
Comment thread boot/zephyr/Kconfig
Comment on lines +757 to +758
config MCUBOOT_LEGACY_SWAP_OVERHEAD
depends on BOOT_LEGACY_SWAP_OVERHEAD

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This symbol has no type, so Kconfig emits "MCUBOOT_LEGACY_SWAP_OVERHEAD defined without a type". That warning is what fails the build_zephyr_with_twister job. Please add bool and follow the sibling MCUBOOT_* symbols, which are hidden internal symbols. Something like:

Suggested change
config MCUBOOT_LEGACY_SWAP_OVERHEAD
depends on BOOT_LEGACY_SWAP_OVERHEAD
config MCUBOOT_LEGACY_SWAP_OVERHEAD
bool
default y if BOOT_LEGACY_SWAP_OVERHEAD

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

correct, this is a zephyr symbol which is declared here: https://github.com/zephyrproject-rtos/zephyr/pull/126265/files as it is common between both application image and MCUboot, so it would not be defined in MCUboot

Comment thread boot/zephyr/CMakeLists.txt Outdated
Comment on lines +791 to +795
if((CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_BOOT_FIRMWARE_LOADER)
set(required_upgrade_size "0")
elseif(CONFIG_BOOT_DIRECT_XIP OR CONFIG_BOOT_RAM_LOAD) AND NOT CONFIG_BOOT_DIRECT_XIP_REVERT
AND NOT CONFIG_BOOT_RAM_LOAD_REVERT
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The parentheses in this if/elseif chain are wrong. if((CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_BOOT_FIRMWARE_LOADER) is unbalanced. In the elseif, the AND NOT ... clauses sit outside the closing paren of elseif(...). Either CMake fails to parse this on the sysbuild path, or the condition does not mean what you intend. I think you want if(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_BOOT_FIRMWARE_LOADER) followed by elseif((CONFIG_BOOT_DIRECT_XIP OR CONFIG_BOOT_RAM_LOAD) AND NOT CONFIG_BOOT_DIRECT_XIP_REVERT AND NOT CONFIG_BOOT_RAM_LOAD_REVERT). Could you also confirm you have built a sysbuild configuration with this change?

@nordicjm nordicjm Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yep seems I missed moving the bracket. Builds were tested and working before splitting into various commits which is where the error was introduced. CI job of using this PR with the needed zephyr commit: https://github.com/mcu-tools/mcuboot/actions/runs/45357825720/job/138320341576

Comment on lines +719 to +720
math(EXPR boot_trailer_size "16 + ${max_align_size} * 4")
align_up(${boot_trailer_size} ${write_size} boot_trailer_size)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The estimate does not look at CONFIG_BOOT_LEGACY_SWAP_OVERHEAD. With that option enabled, the C side reserves BOOT_STATUS_STATE_COUNT * min_write_sz for the swap status in the trailer. Here boot_status_data_size is 0 for the non-swap modes, so the estimate would come out too small. Should the legacy case add that space? Also, does 16 + max_align_size * 4, aligned to write_size, match boot_trailer_sz() in bootutil_area.c for each mode?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I thought about it, it's never been taken into consideration though with older modes so left it alone to replicate existing behaviour, think it should be added in? Yes it matches, the variable part is if encryption is enabled and that is handled elsewhere in CMake

/* #define MCUBOOT_RAM_LOAD */

#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_DIRECT_XIP) || \
defined(MCUBOOT_DIRECT_XIP_REVERT) || defined(MCUBOOT_RAM_LOAD_REVERT) || \\

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: there is a stray second backslash at the end of this line (\\). It should be a single \ continuation. Also, MCUBOOT_RAM_LOAD_REVERT is listed here, but MCUBOOT_OVERWRITE_ONLY-style modes are the only ones the Kconfig lets you select the legacy option for. Please keep the two lists in sync.

Suggested change
defined(MCUBOOT_DIRECT_XIP_REVERT) || defined(MCUBOOT_RAM_LOAD_REVERT) || \\
defined(MCUBOOT_DIRECT_XIP_REVERT) || defined(MCUBOOT_RAM_LOAD_REVERT) || \

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

it's all modes that are not swap modes, which includes overwrite and RAM load/direct XIP (including reverts) since it was adding the size of the swap area overhead in these modes, the revert modes use the status flags at the end of the image but do not use any swap areas, so the list (when corrected for double backslash) is correct

Fixes an oversight whereby non-swapping modes that are not single
image modes were wrongly including the size of the swap area into
calculations that limit the size of the image and put image status
flags earlier than they should be placed

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
Adds a Kconfig which can be enabled to continue using the legacy
MCUboot 2.5 and earlier swap overhead calculations (if
compatibility is required)

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
The image estimated overhead size missed some things, this has
now been fixed

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
@nordicjm
nordicjm force-pushed the fixwrongswapoverhead branch from 3fa58fc to 1561be0 Compare November 24, 2026 08:20
@nordicjm
nordicjm requested a review from d3zd3z November 24, 2026 08:21

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: core Affects core functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

direct-xip (zephyr?) mismatch of image overhead and flag location

3 participants