Skip to content

Commit ac3f262

Browse files
committed
sysbuild: Allow building secondary MCUboot without header
The commit adds Kconfig sysbuild option SECURE_BOOT_MCUBOOT_DISCARDS_MCUBOOT_IMAGE_HEADER that allows to build NSIB and MCUboot to discard MCUboot image header on MCUboot image, to s0/s1 slot, update. Warning: Images built with the option will not be compatbile with existing builds did not have the option or did not have it selected. Enabling the options allows to slightly increase space available for MCUboot image in s0/s1 slot and reduce size of MCUboot image in DFU. Signed-off-by: Dominik Ermel <dominik.ermel@nordicsemi.no>
1 parent 0a28ebf commit ac3f262

4 files changed

Lines changed: 48 additions & 3 deletions

File tree

cmake/sysbuild/b0_mcuboot_signing.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function(ncs_secure_boot_mcuboot_sign application bin_files signed_targets prefi
6969
# image makes no sense, so the --header-size=auto will be used.
7070

7171
# Do not bother picking header size from image that is for CPUNET
72-
if("${prefix}" STREQUAL "CPUNET_")
72+
if("${prefix}" STREQUAL "CPUNET_" OR SB_CONFIG_SECURE_BOOT_MCUBOOT_DISCARDS_MCUBOOT_IMAGE_HEADER)
7373
set(header_size auto)
7474
else()
7575
sysbuild_get(header_size IMAGE ${application} VAR CONFIG_ROM_START_OFFSET KCONFIG)
@@ -92,7 +92,7 @@ function(ncs_secure_boot_mcuboot_sign application bin_files signed_targets prefi
9292
# partition start address.
9393
if(SB_CONFIG_PARTITION_MANAGER)
9494
set(pad_header --pad-header)
95-
elseif("${prefix}" STREQUAL "CPUNET_")
95+
elseif("${prefix}" STREQUAL "CPUNET_" OR SB_CONFIG_SECURE_BOOT_MCUBOOT_DISCARDS_MCUBOOT_IMAGE_HEADER)
9696
set(pad_header --pad-header)
9797
else()
9898
set(pad_header)

cmake/sysbuild/sign.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function(b0_sign_image slot cpunet_target)
9898
set(skip_size 0)
9999
elseif(NOT SB_CONFIG_BOOTLOADER_MCUBOOT)
100100
set(skip_size 0)
101-
elseif(cpunet_target)
101+
elseif(cpunet_target OR SB_CONFIG_SECURE_BOOT_MCUBOOT_DISCARDS_MCUBOOT_IMAGE_HEADER)
102102
set(skip_size 0)
103103
else()
104104
sysbuild_get(build_with_tfm IMAGE ${DEFAULT_IMAGE} VAR CONFIG_BUILD_WITH_TFM KCONFIG)

sysbuild/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,28 @@ function(${SYSBUILD_CURRENT_MODULE_NAME}_pre_cmake)
675675
set_config_bool(mcuboot CONFIG_SECURE_BOOT y)
676676
set_config_bool(mcuboot CONFIG_FW_INFO y)
677677

678+
# Discardable header is glued in front of image, not part of firmware image,
679+
# so there is no offset to jump to, as executable image starts at the partition
680+
# starting address.
681+
if(SB_CONFIG_SECURE_BOOT_MCUBOOT_DISCARDS_MCUBOOT_IMAGE_HEADER)
682+
set_config_int(b0 CONFIG_SB_IMAGE_BOOT_OFFSET 0)
683+
set_config_bool(mcuboot CONFIG_NCS_MCUBOOT_DISCARDS_HEADER_IN_SECONDARY_MCUBOOT y)
684+
# No nice way to do this without changes to Zephyr ROM_START_OFFSET definitions.
685+
# This is special case, as we have MCUboot as bootloader and at the same time
686+
# we do not want the shifter binary, as we usually do with Zephyr builds for
687+
# MCUboot, with space left for MCUboot image header.
688+
set_config_int(mcuboot CONFIG_ROM_START_OFFSET 0)
689+
else()
690+
if(SB_CONFIG_SOC_SERIES_NRF53 OR SB_CONFIG_SOC_SERIES_NRF91 OR SB_CONFIG_SOC_SERIES_NRF52)
691+
set_config_int(b0 CONFIG_SB_IMAGE_BOOT_OFFSET 0x200)
692+
elseif(SB_CONFIG_SOC_SERIES_NRF54L)
693+
set_config_int(b0 CONFIG_SB_IMAGE_BOOT_OFFSET 0x800)
694+
else()
695+
message(WARNING "Missing CONFIG_SB_IMAGE_BOOT_OFFSET setting for selected SoC")
696+
endif()
697+
set_config_bool(mcuboot CONFIG_NCS_MCUBOOT_DISCARDS_HEADER_IN_SECONDARY_MCUBOOT n)
698+
endif()
699+
678700
if(SB_CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256)
679701
if(SB_CONFIG_BOOT_SHARED_CRYPTO_ECDSA_P256)
680702
add_overlay_config(

sysbuild/Kconfig.mcuboot

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,29 @@ config SECURE_BOOT_MCUBOOT_VERSION
174174
help
175175
The version of the MCUboot S0/S1 upgrade package
176176

177+
config SECURE_BOOT_MCUBOOT_DISCARDS_MCUBOOT_IMAGE_HEADER
178+
bool "Use enough space in front of image to just fit header there"
179+
depends on SECURE_BOOT
180+
depends on BOOTLOADER_MCUBOOT
181+
help
182+
INCOMPATIBILITY WARNING: MCUboot as secondary bootloader that have been
183+
built without the option enabled will not be able to update itself
184+
with a binary built with the option enabled. NSIB will not be able to
185+
boot such MCUboot either. The entire bootchain has to be built with the
186+
option enabled. This will happen because pre-existing MCUboot, built
187+
without the option, will try to skip non-existing header while trying
188+
to verify the image with NSIB, failing the process of verification.
189+
When this option is enabled, the MCUboot for secondary bootloader is
190+
built with ROM_START_OFFSET set to 0 and NSIB has nothing to skip over.
191+
Instead the header is glued in front of the image during signing, post
192+
build, and is left out when MCUboot does update.
193+
NSIB will now have to verify, and boot MCUBOOT image, exactly at
194+
the beginning of S0/S1 partition, which means
195+
that SB_IMAGE_BOOT_OFFSET, for Secure Bootloader, has to be set to 0 too;
196+
RWX protections no longer need to skip the leftover MCUboot image header,
197+
because it no longer occupies the beginning of partition, so
198+
SB_DISABLE_SELF_RWX_SKIP_SIZE should also be set to 0.
199+
177200
if BOOTLOADER_MCUBOOT
178201

179202
choice BOOT_SIGNATURE_TYPE

0 commit comments

Comments
 (0)