From a4149a37d9da8fd9b15eca1ab9824158deacec3c Mon Sep 17 00:00:00 2001 From: Fabio Utzig Date: Fri, 22 Nov 2019 15:57:06 -0300 Subject: [PATCH] boot: zephyr: add config for swap without scratch Add Zephyr option to enable building a bootloader that uses an alternative swap algorithm, that first moves up all sectors in slot1 and then directly swaps between slot0 and slot1. Signed-off-by: Fabio Utzig --- boot/zephyr/CMakeLists.txt | 2 ++ boot/zephyr/Kconfig | 13 +++++++++++++ boot/zephyr/flash_map_extended.c | 2 ++ boot/zephyr/include/mcuboot_config/mcuboot_config.h | 4 ++++ boot/zephyr/include/sysflash/sysflash.h | 2 ++ boot/zephyr/include/target.h | 4 ++-- boot/zephyr/prj.conf | 2 ++ 7 files changed, 27 insertions(+), 2 deletions(-) diff --git a/boot/zephyr/CMakeLists.txt b/boot/zephyr/CMakeLists.txt index de7754abf8..05c25e0d3e 100644 --- a/boot/zephyr/CMakeLists.txt +++ b/boot/zephyr/CMakeLists.txt @@ -103,6 +103,8 @@ endif() zephyr_library_include_directories(${BOOT_DIR}/bootutil/include) zephyr_library_sources( ${BOOT_DIR}/bootutil/src/loader.c + ${BOOT_DIR}/bootutil/src/swap_scratch.c + ${BOOT_DIR}/bootutil/src/swap_move.c ${BOOT_DIR}/bootutil/src/bootutil_misc.c ${BOOT_DIR}/bootutil/src/image_validate.c ${BOOT_DIR}/bootutil/src/encrypted.c diff --git a/boot/zephyr/Kconfig b/boot/zephyr/Kconfig index 56b5b19137..e304a09672 100644 --- a/boot/zephyr/Kconfig +++ b/boot/zephyr/Kconfig @@ -126,6 +126,19 @@ config BOOT_UPGRADE_ONLY of swapping them. This prevents the fallback recovery, but uses a much simpler code path. +config BOOT_SWAP_MOVE + bool "Swap mode that can run without a scratch partition" + default n + help + If y, the swap upgrade is done in two steps, where first every + sector of the secondary slot is moved up one sector, then for + each sector X in the primary slot, it is moved to index X in + the secondary slot, then the sector at X+1 in the secondary is + moved to index X in the primary. + This allows a swap upgrade without using a scratch partition, + but is currently limited to all sectors in both slots being of + the same size. + config BOOT_BOOTSTRAP bool "Bootstrap erased the primary slot from the secondary slot" default n diff --git a/boot/zephyr/flash_map_extended.c b/boot/zephyr/flash_map_extended.c index 47da3b7ea7..cec3be55d6 100644 --- a/boot/zephyr/flash_map_extended.c +++ b/boot/zephyr/flash_map_extended.c @@ -58,7 +58,9 @@ int flash_area_id_from_multi_image_slot(int image_index, int slot) switch (slot) { case 0: return FLASH_AREA_IMAGE_PRIMARY(image_index); case 1: return FLASH_AREA_IMAGE_SECONDARY(image_index); +#if !defined(CONFIG_BOOT_SWAP_MOVE) case 2: return FLASH_AREA_IMAGE_SCRATCH; +#endif } return -EINVAL; /* flash_area_open will fail on that */ diff --git a/boot/zephyr/include/mcuboot_config/mcuboot_config.h b/boot/zephyr/include/mcuboot_config/mcuboot_config.h index f517b7ea15..bafe521d2d 100644 --- a/boot/zephyr/include/mcuboot_config/mcuboot_config.h +++ b/boot/zephyr/include/mcuboot_config/mcuboot_config.h @@ -53,6 +53,10 @@ #define MCUBOOT_OVERWRITE_ONLY_FAST #endif +#ifdef CONFIG_BOOT_SWAP_MOVE +#define MCUBOOT_SWAP_MOVE 1 +#endif + #ifdef CONFIG_LOG #define MCUBOOT_HAVE_LOGGING 1 #endif diff --git a/boot/zephyr/include/sysflash/sysflash.h b/boot/zephyr/include/sysflash/sysflash.h index c422efd649..c3981e611c 100644 --- a/boot/zephyr/include/sysflash/sysflash.h +++ b/boot/zephyr/include/sysflash/sysflash.h @@ -37,6 +37,8 @@ #error "Image slot and flash area mapping is not defined" #endif +#if !defined(CONFIG_BOOT_SWAP_MOVE) #define FLASH_AREA_IMAGE_SCRATCH DT_FLASH_AREA_IMAGE_SCRATCH_ID +#endif #endif /* __SYSFLASH_H__ */ diff --git a/boot/zephyr/include/target.h b/boot/zephyr/include/target.h index a11616fc07..06b29e4f3e 100644 --- a/boot/zephyr/include/target.h +++ b/boot/zephyr/include/target.h @@ -42,8 +42,8 @@ !defined(FLASH_AREA_IMAGE_0_SIZE) || \ !defined(FLASH_AREA_IMAGE_1_OFFSET) || \ !defined(FLASH_AREA_IMAGE_1_SIZE) || \ - !defined(FLASH_AREA_IMAGE_SCRATCH_OFFSET) || \ - !defined(FLASH_AREA_IMAGE_SCRATCH_SIZE) + (!defined(CONFIG_BOOT_SWAP_MOVE) && !defined(FLASH_AREA_IMAGE_SCRATCH_OFFSET)) || \ + (!defined(CONFIG_BOOT_SWAP_MOVE) && !defined(FLASH_AREA_IMAGE_SCRATCH_SIZE)) #error "Target support is incomplete; cannot build mcuboot." #endif diff --git a/boot/zephyr/prj.conf b/boot/zephyr/prj.conf index 89e55f5de4..43c7f139d5 100644 --- a/boot/zephyr/prj.conf +++ b/boot/zephyr/prj.conf @@ -9,6 +9,8 @@ CONFIG_MBEDTLS_CFG_FILE="mcuboot-mbedtls-cfg.h" CONFIG_BOOT_ENCRYPT_RSA=n CONFIG_BOOT_ENCRYPT_EC256=n +CONFIG_BOOT_UPGRADE_ONLY=n +CONFIG_BOOT_SWAP_MOVE=n CONFIG_BOOT_BOOTSTRAP=n ### Default to RSA