Skip to content

Commit 18fee81

Browse files
committed
zephyr: Add support for swap using offset mode
Adds support for using this mode to zephyr Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
1 parent 4af7316 commit 18fee81

File tree

6 files changed

+93
-29
lines changed

6 files changed

+93
-29
lines changed

boot/zephyr/CMakeLists.txt

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,30 +125,48 @@ zephyr_library_sources(
125125
)
126126
endif()
127127

128-
if(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_SINGLE_APPLICATION_SLOT_RAM_LOAD)
129-
zephyr_library_sources(
130-
${BOOT_DIR}/zephyr/single_loader.c
131-
)
132-
zephyr_library_include_directories(${BOOT_DIR}/bootutil/src)
128+
if(CONFIG_SINGLE_APPLICATION_SLOT)
129+
zephyr_library_sources(
130+
${BOOT_DIR}/zephyr/single_loader.c
131+
)
132+
zephyr_library_include_directories(${BOOT_DIR}/bootutil/src)
133+
elseif(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_SINGLE_APPLICATION_SLOT_RAM_LOAD)
134+
zephyr_library_sources(
135+
${BOOT_DIR}/zephyr/single_loader.c
136+
${BOOT_DIR}/bootutil/src/ram_load.c
137+
)
138+
zephyr_library_include_directories(${BOOT_DIR}/bootutil/src)
133139
elseif(CONFIG_BOOT_FIRMWARE_LOADER)
134-
zephyr_library_sources(
135-
${BOOT_DIR}/zephyr/firmware_loader.c
136-
)
137-
zephyr_library_include_directories(${BOOT_DIR}/bootutil/src)
140+
zephyr_library_sources(
141+
${BOOT_DIR}/zephyr/firmware_loader.c
142+
)
143+
zephyr_library_include_directories(${BOOT_DIR}/bootutil/src)
138144
else()
139-
zephyr_library_sources(
140-
${BOOT_DIR}/bootutil/src/loader.c
141-
${BOOT_DIR}/bootutil/src/swap_misc.c
142-
${BOOT_DIR}/bootutil/src/swap_scratch.c
143-
${BOOT_DIR}/bootutil/src/swap_move.c
144-
${BOOT_DIR}/bootutil/src/caps.c
145-
)
146-
endif()
147-
148-
if(CONFIG_BOOT_RAM_LOAD OR CONFIG_SINGLE_APPLICATION_SLOT_RAM_LOAD)
149145
zephyr_library_sources(
150-
${BOOT_DIR}/bootutil/src/ram_load.c
146+
${BOOT_DIR}/bootutil/src/loader.c
147+
${BOOT_DIR}/bootutil/src/swap_misc.c
148+
${BOOT_DIR}/bootutil/src/caps.c
151149
)
150+
151+
if(CONFIG_BOOT_SWAP_USING_MOVE)
152+
zephyr_library_sources(
153+
${BOOT_DIR}/bootutil/src/swap_move.c
154+
)
155+
elseif(CONFIG_BOOT_SWAP_USING_OFFSET)
156+
zephyr_library_sources(
157+
${BOOT_DIR}/bootutil/src/swap_offset.c
158+
)
159+
else()
160+
zephyr_library_sources(
161+
${BOOT_DIR}/bootutil/src/swap_scratch.c
162+
)
163+
164+
if(CONFIG_BOOT_RAM_LOAD OR CONFIG_SINGLE_APPLICATION_SLOT_RAM_LOAD)
165+
zephyr_library_sources(
166+
${BOOT_DIR}/bootutil/src/ram_load.c
167+
)
168+
endif()
169+
endif()
152170
endif()
153171

154172
if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256 OR CONFIG_BOOT_ENCRYPT_EC256)
@@ -427,7 +445,7 @@ dt_get_parent(slot0_flash)
427445
dt_prop(erase_size_slot0 PATH "${slot0_flash}" PROPERTY "erase-block-size")
428446
dt_prop(write_size_slot0 PATH "${slot0_flash}" PROPERTY "write-block-size")
429447

430-
if(CONFIG_BOOT_SWAP_USING_MOVE)
448+
if(CONFIG_BOOT_SWAP_USING_MOVE OR CONFIG_BOOT_SWAP_USING_OFFSET)
431449
if(DEFINED erase_size_slot0)
432450
zephyr_compile_definitions("MCUBOOT_SLOT0_EXPECTED_ERASE_SIZE=${erase_size_slot0}")
433451
endif()
@@ -445,7 +463,7 @@ if(NOT CONFIG_SINGLE_APPLICATION_SLOT AND NOT CONFIG_SINGLE_APPLICATION_SLOT_RAM
445463
dt_prop(erase_size_slot1 PATH "${slot1_flash}" PROPERTY "erase-block-size")
446464
dt_prop(write_size_slot1 PATH "${slot1_flash}" PROPERTY "write-block-size")
447465

448-
if(CONFIG_BOOT_SWAP_USING_MOVE)
466+
if(CONFIG_BOOT_SWAP_USING_MOVE OR CONFIG_BOOT_SWAP_USING_OFFSET)
449467
if(DEFINED erase_size_slot1)
450468
zephyr_compile_definitions("MCUBOOT_SLOT1_EXPECTED_ERASE_SIZE=${erase_size_slot1}")
451469
endif()
@@ -487,12 +505,12 @@ if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO)
487505
endif()
488506
endif()
489507

490-
if((CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE) AND (DEFINED write_size_slot0 OR DEFINED write_size_slot1))
508+
if((CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE OR CONFIG_BOOT_SWAP_USING_OFFSET) AND (DEFINED write_size_slot0 OR DEFINED write_size_slot1))
491509
zephyr_library_sources(flash_check.c)
492510
endif()
493511

494512
if(SYSBUILD)
495-
if(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_BOOT_FIRMWARE_LOADER OR CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE OR CONFIG_BOOT_UPGRADE_ONLY OR CONFIG_BOOT_DIRECT_XIP OR CONFIG_BOOT_RAM_LOAD)
513+
if(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_BOOT_FIRMWARE_LOADER OR CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE OR CONFIG_BOOT_SWAP_USING_OFFSET OR CONFIG_BOOT_UPGRADE_ONLY OR CONFIG_BOOT_DIRECT_XIP OR CONFIG_BOOT_RAM_LOAD)
496514
# TODO: RAM LOAD support
497515
dt_nodelabel(slot0_flash NODELABEL "slot0_partition")
498516
dt_get_parent(slot0_flash)
@@ -617,6 +635,17 @@ if(SYSBUILD)
617635
math(EXPR boot_status_data_size "128 * (3 * ${write_size})")
618636
endif()
619637
endif()
638+
elseif(CONFIG_BOOT_SWAP_USING_OFFSET)
639+
if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO AND DEFINED slot_min_sectors AND "${slot_min_sectors}" GREATER "0")
640+
math(EXPR boot_status_data_size "${slot_min_sectors} * (2 * ${write_size})")
641+
else()
642+
if(CONFIG_BOOT_MAX_IMG_SECTORS)
643+
math(EXPR boot_status_data_size "${CONFIG_BOOT_MAX_IMG_SECTORS} * (2 * ${write_size})")
644+
else()
645+
message(WARNING "CONFIG_BOOT_MAX_IMG_SECTORS is not defined, falling back to 128 sector default. Please set CONFIG_BOOT_MAX_IMG_SECTORS to the required value")
646+
math(EXPR boot_status_data_size "128 * (2 * ${write_size})")
647+
endif()
648+
endif()
620649
else()
621650
set(boot_status_data_size 0)
622651
endif()
@@ -634,6 +663,10 @@ if(SYSBUILD)
634663
if(CONFIG_BOOT_SWAP_USING_MOVE)
635664
math(EXPR required_size "${required_size} + ${erase_size}")
636665
math(EXPR required_upgrade_size "${required_upgrade_size} + ${erase_size}")
666+
elseif(CONFIG_BOOT_SWAP_USING_OFFSET)
667+
#todo: check how different slot sizes are...
668+
# math(EXPR required_size "${required_size} + ${erase_size}")
669+
# math(EXPR required_upgrade_size "${required_upgrade_size} + ${erase_size}")
637670
endif()
638671
else()
639672
set(required_size 0)

boot/zephyr/Kconfig

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ config BOOT_VALIDATE_SLOT0_ONCE
310310
low end devices with as a compromise lowering the security level.
311311
If unsure, leave at the default value.
312312

313+
config BOOT_PREFER_SWAP_OFFSET
314+
bool "Prefer the newer swap offset algorithm"
315+
help
316+
If y, the BOOT_IMAGE_UPGRADE_MODE will default to using "offset" instead of "scratch".
317+
This is a separate bool config option, because Kconfig doesn't allow defaults to be
318+
overridden in choice options. Most devices should be using swap using offset mode.
319+
313320
config BOOT_PREFER_SWAP_MOVE
314321
bool "Prefer the newer swap move algorithm"
315322
default y if SOC_FAMILY_NORDIC_NRF
@@ -318,12 +325,14 @@ config BOOT_PREFER_SWAP_MOVE
318325
If y, the BOOT_IMAGE_UPGRADE_MODE will default to using
319326
"move" instead of "scratch". This is a separate bool config
320327
option, because Kconfig doesn't allow defaults to be
321-
overridden in choice options. Most devices should be using
322-
swap move.
328+
overridden in choice options. This mode has been superceded
329+
by swap using offset, but is kept to allow existing projects
330+
to make use of it.
323331

324332
if !SINGLE_APPLICATION_SLOT
325333
choice BOOT_IMAGE_UPGRADE_MODE
326334
prompt "Image upgrade modes"
335+
default BOOT_SWAP_USING_OFFSET if BOOT_PREFER_SWAP_OFFSET
327336
default BOOT_SWAP_USING_MOVE if BOOT_PREFER_SWAP_MOVE
328337
default BOOT_SWAP_USING_SCRATCH
329338

@@ -340,8 +349,19 @@ config BOOT_UPGRADE_ONLY
340349
of swapping them. This prevents the fallback recovery, but
341350
uses a much simpler code path.
342351

352+
config BOOT_SWAP_USING_OFFSET
353+
bool "Swap using offset mode without scratch partition"
354+
help
355+
If y, the swap upgrade is done by each sector X+1 in the secondary slot moved index X in
356+
the primary slot, then the sector at X+1 in the primary is moved to index X in the
357+
secondary.
358+
This allows a swap upgrade without using a scratch partition, but is currently limited
359+
to all sectors in both slots being of the same size. This mode offers faster swap times
360+
with less flash endurance usage than swap using move, firmware updates must be placed at
361+
the second sector in the second slot instead of the first.
362+
343363
config BOOT_SWAP_USING_MOVE
344-
bool "Swap mode that can run without a scratch partition"
364+
bool "Swap using mode mode without scratch partition"
345365
help
346366
If y, the swap upgrade is done in two steps, where first every
347367
sector of the primary slot is moved up one sector, then for
@@ -779,7 +799,7 @@ config MCUBOOT_DOWNGRADE_PREVENTION
779799
config MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER
780800
bool "Use image security counter instead of version number"
781801
depends on MCUBOOT_DOWNGRADE_PREVENTION
782-
depends on (BOOT_SWAP_USING_MOVE || BOOT_SWAP_USING_SCRATCH)
802+
depends on (BOOT_SWAP_USING_MOVE || BOOT_SWAP_USING_SCRATCH || BOOT_SWAP_USING_OFFSET)
783803
help
784804
Security counter is used for version eligibility check instead of pure
785805
version. When this option is set, any upgrade must have greater or

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@
8484
#define MCUBOOT_SWAP_USING_MOVE 1
8585
#endif
8686

87+
#ifdef CONFIG_BOOT_SWAP_USING_OFFSET
88+
#define MCUBOOT_SWAP_USING_OFFSET 1
89+
#endif
90+
8791
#ifdef CONFIG_BOOT_DIRECT_XIP
8892
#define MCUBOOT_DIRECT_XIP
8993
#endif

boot/zephyr/include/sysflash/sysflash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static inline uint32_t __flash_area_ids_for_slot(int img, int slot)
5454
#define FLASH_AREA_IMAGE_PRIMARY(x) __flash_area_ids_for_slot(x, 0)
5555
#define FLASH_AREA_IMAGE_SECONDARY(x) __flash_area_ids_for_slot(x, 1)
5656

57-
#if !defined(CONFIG_BOOT_SWAP_USING_MOVE)
57+
#if !defined(CONFIG_BOOT_SWAP_USING_MOVE) && !defined(CONFIG_BOOT_SWAP_USING_OFFSET)
5858
#define FLASH_AREA_IMAGE_SCRATCH FIXED_PARTITION_ID(scratch_partition)
5959
#endif
6060

boot/zephyr/sample.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,9 @@ tests:
9090
platform_allow: mimxrt1020_evk
9191
integration_platforms:
9292
- mimxrt1020_evk
93+
sample.bootloader.mcuboot.swap_offset:
94+
extra_args: EXTRA_CONF_FILE=./swap_offset.conf
95+
platform_allow: nrf52840dk/nrf52840
96+
integration_platforms:
97+
- nrf52840dk/nrf52840
98+
tags: bootloader_mcuboot

boot/zephyr/swap_offset.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_BOOT_SWAP_USING_OFFSET=y

0 commit comments

Comments
 (0)