From 6b07b98770ec6a62151f2e6812f02197f1db1812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20J=C3=A4ger?= Date: Sun, 5 Jan 2025 13:35:32 +0100 Subject: [PATCH] boot: espressif: add support for upgrade modes w/o scratch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Upgrade using move decreases flash wear and is now the default mode used by Zephyr. The implementation is done by MCUboot itself, so it is just a matter of setting the correct define to enable it. Swap using move was successfully tested with ESP32C6. Signed-off-by: Martin Jäger --- .../include/mcuboot_config/mcuboot_config.h | 34 +++++++++++++++---- boot/espressif/hal/src/flash_encrypt.c | 3 ++ .../port/esp32/bootloader-multi.conf | 4 +++ boot/espressif/port/esp32/bootloader.conf | 4 +++ boot/espressif/port/esp32c2/bootloader.conf | 4 +++ boot/espressif/port/esp32c3/bootloader.conf | 4 +++ boot/espressif/port/esp32c6/bootloader.conf | 4 +++ boot/espressif/port/esp32h2/bootloader.conf | 4 +++ boot/espressif/port/esp32s2/bootloader.conf | 4 +++ .../port/esp32s3/bootloader-multi.conf | 4 +++ boot/espressif/port/esp32s3/bootloader.conf | 4 +++ boot/espressif/port/esp_mcuboot.c | 12 +++++-- 12 files changed, 76 insertions(+), 9 deletions(-) diff --git a/boot/espressif/hal/include/mcuboot_config/mcuboot_config.h b/boot/espressif/hal/include/mcuboot_config/mcuboot_config.h index a299e3cfc8..345ca57b82 100644 --- a/boot/espressif/hal/include/mcuboot_config/mcuboot_config.h +++ b/boot/espressif/hal/include/mcuboot_config/mcuboot_config.h @@ -45,23 +45,43 @@ * the default upgrade mode. */ -/* Uncomment to enable the overwrite-only code path. */ -/* #define MCUBOOT_OVERWRITE_ONLY */ +/* Define to enable the swap-using-move code path. */ +#if defined(CONFIG_ESP_BOOT_SWAP_USING_MOVE) +#define MCUBOOT_SWAP_USING_MOVE 1 +#endif -#ifdef MCUBOOT_OVERWRITE_ONLY +/* Define to enable the overwrite-only code path. */ +#if defined(CONFIG_ESP_BOOT_UPGRADE_ONLY) +#define MCUBOOT_OVERWRITE_ONLY /* Uncomment to only erase and overwrite those primary slot sectors needed * to install the new image, rather than the entire image slot. */ /* #define MCUBOOT_OVERWRITE_ONLY_FAST */ #endif -/* Uncomment to enable the direct-xip code path. */ -/* #define MCUBOOT_DIRECT_XIP */ +/* Define to enable the direct-xip code path (CURRENTLY UNSUPPORTED!). */ +#if defined(CONFIG_ESP_BOOT_DIRECT_XIP) +#define MCUBOOT_DIRECT_XIP +#endif -/* Define to enable the ram-load code path. */ -#if defined(CONFIG_BOOT_RAM_LOAD) +/* Define to enable the ram-load code path (CURRENTLY UNSUPPORTED!). */ +#if defined(CONFIG_ESP_BOOT_RAM_LOAD) #define MCUBOOT_RAM_LOAD #endif +/* If none of the above paths is defined, define CONFIG_ESP_BOOT_SWAP_USING_SCRATCH. + * + * Note: MCUBOOT_SWAP_USING_SCRATCH does not have to be defined, as it will be defined + * by MCUboot in bootutil_priv.h. + */ +#if !defined(CONFIG_ESP_BOOT_SWAP_USING_SCRATCH) && \ + !defined(CONFIG_ESP_BOOT_SWAP_USING_MOVE) && \ + !defined(CONFIG_ESP_BOOT_UPGRADE_ONLY) && \ + !defined(CONFIG_ESP_BOOT_DIRECT_XIP) && \ + !defined(CONFIG_ESP_BOOT_RAM_LOAD) +#define CONFIG_ESP_BOOT_SWAP_USING_SCRATCH +#endif + + /* * Cryptographic settings * diff --git a/boot/espressif/hal/src/flash_encrypt.c b/boot/espressif/hal/src/flash_encrypt.c index d064d8b7b2..3996d0e7a5 100644 --- a/boot/espressif/hal/src/flash_encrypt.c +++ b/boot/espressif/hal/src/flash_encrypt.c @@ -307,12 +307,15 @@ esp_err_t esp_flash_encrypt_contents(void) if (err != ESP_OK) { return err; } + +#ifdef CONFIG_ESP_BOOT_SWAP_USING_SCRATCH region_addr = CONFIG_ESP_SCRATCH_OFFSET; region_size = CONFIG_ESP_SCRATCH_SIZE; err = esp_flash_encrypt_region(region_addr, region_size); if (err != ESP_OK) { return err; } +#endif #if defined(CONFIG_ESP_IMAGE_NUMBER) && (CONFIG_ESP_IMAGE_NUMBER == 2) region_addr = CONFIG_ESP_IMAGE1_PRIMARY_START_ADDRESS; diff --git a/boot/espressif/port/esp32/bootloader-multi.conf b/boot/espressif/port/esp32/bootloader-multi.conf index ad3355ec23..29e2073f68 100644 --- a/boot/espressif/port/esp32/bootloader-multi.conf +++ b/boot/espressif/port/esp32/bootloader-multi.conf @@ -2,6 +2,10 @@ # # SPDX-License-Identifier: Apache-2.0 +# Define upgrade mode (default is CONFIG_ESP_BOOT_SWAP_USING_SCRATCH) +# CONFIG_ESP_BOOT_SWAP_USING_MOVE=y +# CONFIG_ESP_BOOT_UPGRADE_ONLY=y + CONFIG_ESP_FLASH_SIZE=4MB CONFIG_ESP_BOOTLOADER_SIZE=0xF000 CONFIG_ESP_BOOTLOADER_OFFSET=0x1000 diff --git a/boot/espressif/port/esp32/bootloader.conf b/boot/espressif/port/esp32/bootloader.conf index 8f555ec574..5a59a422a4 100644 --- a/boot/espressif/port/esp32/bootloader.conf +++ b/boot/espressif/port/esp32/bootloader.conf @@ -2,6 +2,10 @@ # # SPDX-License-Identifier: Apache-2.0 +# Define upgrade mode (default is CONFIG_ESP_BOOT_SWAP_USING_SCRATCH) +# CONFIG_ESP_BOOT_SWAP_USING_MOVE=y +# CONFIG_ESP_BOOT_UPGRADE_ONLY=y + CONFIG_ESP_FLASH_SIZE=4MB CONFIG_ESP_BOOTLOADER_SIZE=0xF000 CONFIG_ESP_BOOTLOADER_OFFSET=0x1000 diff --git a/boot/espressif/port/esp32c2/bootloader.conf b/boot/espressif/port/esp32c2/bootloader.conf index 54f797e71e..8f6886023c 100644 --- a/boot/espressif/port/esp32c2/bootloader.conf +++ b/boot/espressif/port/esp32c2/bootloader.conf @@ -2,6 +2,10 @@ # # SPDX-License-Identifier: Apache-2.0 +# Define upgrade mode (default is CONFIG_ESP_BOOT_SWAP_USING_SCRATCH) +# CONFIG_ESP_BOOT_SWAP_USING_MOVE=y +# CONFIG_ESP_BOOT_UPGRADE_ONLY=y + CONFIG_ESP_FLASH_SIZE=4MB CONFIG_ESP_BOOTLOADER_SIZE=0xF000 CONFIG_ESP_BOOTLOADER_OFFSET=0x0000 diff --git a/boot/espressif/port/esp32c3/bootloader.conf b/boot/espressif/port/esp32c3/bootloader.conf index 88954eea0f..8ebd1b40ec 100644 --- a/boot/espressif/port/esp32c3/bootloader.conf +++ b/boot/espressif/port/esp32c3/bootloader.conf @@ -2,6 +2,10 @@ # # SPDX-License-Identifier: Apache-2.0 +# Define upgrade mode (default is CONFIG_ESP_BOOT_SWAP_USING_SCRATCH) +# CONFIG_ESP_BOOT_SWAP_USING_MOVE=y +# CONFIG_ESP_BOOT_UPGRADE_ONLY=y + CONFIG_ESP_FLASH_SIZE=4MB CONFIG_ESP_BOOTLOADER_SIZE=0xF000 CONFIG_ESP_BOOTLOADER_OFFSET=0x0000 diff --git a/boot/espressif/port/esp32c6/bootloader.conf b/boot/espressif/port/esp32c6/bootloader.conf index 5c5307c9ce..e92ddcc220 100644 --- a/boot/espressif/port/esp32c6/bootloader.conf +++ b/boot/espressif/port/esp32c6/bootloader.conf @@ -2,6 +2,10 @@ # # SPDX-License-Identifier: Apache-2.0 +# Define upgrade mode (default is CONFIG_ESP_BOOT_SWAP_USING_SCRATCH) +# CONFIG_ESP_BOOT_SWAP_USING_MOVE=y +# CONFIG_ESP_BOOT_UPGRADE_ONLY=y + CONFIG_ESP_FLASH_SIZE=4MB CONFIG_ESP_BOOTLOADER_SIZE=0xF000 CONFIG_ESP_BOOTLOADER_OFFSET=0x0000 diff --git a/boot/espressif/port/esp32h2/bootloader.conf b/boot/espressif/port/esp32h2/bootloader.conf index 5c5307c9ce..e92ddcc220 100644 --- a/boot/espressif/port/esp32h2/bootloader.conf +++ b/boot/espressif/port/esp32h2/bootloader.conf @@ -2,6 +2,10 @@ # # SPDX-License-Identifier: Apache-2.0 +# Define upgrade mode (default is CONFIG_ESP_BOOT_SWAP_USING_SCRATCH) +# CONFIG_ESP_BOOT_SWAP_USING_MOVE=y +# CONFIG_ESP_BOOT_UPGRADE_ONLY=y + CONFIG_ESP_FLASH_SIZE=4MB CONFIG_ESP_BOOTLOADER_SIZE=0xF000 CONFIG_ESP_BOOTLOADER_OFFSET=0x0000 diff --git a/boot/espressif/port/esp32s2/bootloader.conf b/boot/espressif/port/esp32s2/bootloader.conf index 485ba77e1a..981be59a10 100644 --- a/boot/espressif/port/esp32s2/bootloader.conf +++ b/boot/espressif/port/esp32s2/bootloader.conf @@ -2,6 +2,10 @@ # # SPDX-License-Identifier: Apache-2.0 +# Define upgrade mode (default is CONFIG_ESP_BOOT_SWAP_USING_SCRATCH) +# CONFIG_ESP_BOOT_SWAP_USING_MOVE=y +# CONFIG_ESP_BOOT_UPGRADE_ONLY=y + CONFIG_ESP_FLASH_SIZE=4MB CONFIG_ESP_BOOTLOADER_SIZE=0xF000 CONFIG_ESP_BOOTLOADER_OFFSET=0x1000 diff --git a/boot/espressif/port/esp32s3/bootloader-multi.conf b/boot/espressif/port/esp32s3/bootloader-multi.conf index 21c3457a4b..f82b5e235b 100644 --- a/boot/espressif/port/esp32s3/bootloader-multi.conf +++ b/boot/espressif/port/esp32s3/bootloader-multi.conf @@ -2,6 +2,10 @@ # # SPDX-License-Identifier: Apache-2.0 +# Define upgrade mode (default is CONFIG_ESP_BOOT_SWAP_USING_SCRATCH) +# CONFIG_ESP_BOOT_SWAP_USING_MOVE=y +# CONFIG_ESP_BOOT_UPGRADE_ONLY=y + CONFIG_ESP_FLASH_SIZE=4MB CONFIG_ESP_BOOTLOADER_SIZE=0xF000 CONFIG_ESP_BOOTLOADER_OFFSET=0x0000 diff --git a/boot/espressif/port/esp32s3/bootloader.conf b/boot/espressif/port/esp32s3/bootloader.conf index 138737dfb0..a45c0cd193 100644 --- a/boot/espressif/port/esp32s3/bootloader.conf +++ b/boot/espressif/port/esp32s3/bootloader.conf @@ -2,6 +2,10 @@ # # SPDX-License-Identifier: Apache-2.0 +# Define upgrade mode (default is CONFIG_ESP_BOOT_SWAP_USING_SCRATCH) +# CONFIG_ESP_BOOT_SWAP_USING_MOVE=y +# CONFIG_ESP_BOOT_UPGRADE_ONLY=y + CONFIG_ESP_FLASH_SIZE=4MB CONFIG_ESP_BOOTLOADER_SIZE=0xF000 CONFIG_ESP_BOOTLOADER_OFFSET=0x0000 diff --git a/boot/espressif/port/esp_mcuboot.c b/boot/espressif/port/esp_mcuboot.c index 0ee9e388b9..9bbd5a9f20 100644 --- a/boot/espressif/port/esp_mcuboot.c +++ b/boot/espressif/port/esp_mcuboot.c @@ -15,6 +15,7 @@ #include "esp_err.h" #include "bootloader_flash_priv.h" #include "esp_flash_encrypt.h" +#include "mcuboot_config/mcuboot_config.h" #include "flash_map_backend/flash_map_backend.h" #include "sysflash/sysflash.h" @@ -49,16 +50,19 @@ _Static_assert(IS_ALIGNED(FLASH_BUFFER_SIZE, 4), "Buffer size for SPI Flash oper #define BOOTLOADER_START_ADDRESS CONFIG_BOOTLOADER_OFFSET_IN_FLASH #define BOOTLOADER_SIZE CONFIG_ESP_BOOTLOADER_SIZE + #define IMAGE0_PRIMARY_START_ADDRESS CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS #define IMAGE0_SECONDARY_START_ADDRESS CONFIG_ESP_IMAGE0_SECONDARY_START_ADDRESS -#define SCRATCH_OFFSET CONFIG_ESP_SCRATCH_OFFSET #if (MCUBOOT_IMAGE_NUMBER == 2) #define IMAGE1_PRIMARY_START_ADDRESS CONFIG_ESP_IMAGE1_PRIMARY_START_ADDRESS #define IMAGE1_SECONDARY_START_ADDRESS CONFIG_ESP_IMAGE1_SECONDARY_START_ADDRESS #endif - #define APPLICATION_SIZE CONFIG_ESP_APPLICATION_SIZE + +#ifdef CONFIG_ESP_BOOT_SWAP_USING_SCRATCH +#define SCRATCH_OFFSET CONFIG_ESP_SCRATCH_OFFSET #define SCRATCH_SIZE CONFIG_ESP_SCRATCH_SIZE +#endif extern int ets_printf(const char *fmt, ...); @@ -99,12 +103,14 @@ static const struct flash_area secondary_img1 = { }; #endif +#ifdef CONFIG_ESP_BOOT_SWAP_USING_SCRATCH static const struct flash_area scratch_img0 = { .fa_id = FLASH_AREA_IMAGE_SCRATCH, .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH, .fa_off = SCRATCH_OFFSET, .fa_size = SCRATCH_SIZE, }; +#endif static const struct flash_area *s_flash_areas[] = { &bootloader, @@ -114,7 +120,9 @@ static const struct flash_area *s_flash_areas[] = { &primary_img1, &secondary_img1, #endif +#ifdef CONFIG_ESP_BOOT_SWAP_USING_SCRATCH &scratch_img0, +#endif }; static const struct flash_area *prv_lookup_flash_area(uint8_t id) {