Skip to content

Commit

Permalink
boot: espressif: add support for upgrade modes w/o scratch
Browse files Browse the repository at this point in the history
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 <martin@libre.solar>
  • Loading branch information
martinjaeger committed Jan 8, 2025
1 parent 95773ed commit 6b07b98
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 9 deletions.
34 changes: 27 additions & 7 deletions boot/espressif/hal/include/mcuboot_config/mcuboot_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
3 changes: 3 additions & 0 deletions boot/espressif/hal/src/flash_encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions boot/espressif/port/esp32/bootloader-multi.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions boot/espressif/port/esp32/bootloader.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions boot/espressif/port/esp32c2/bootloader.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions boot/espressif/port/esp32c3/bootloader.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions boot/espressif/port/esp32c6/bootloader.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions boot/espressif/port/esp32h2/bootloader.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions boot/espressif/port/esp32s2/bootloader.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions boot/espressif/port/esp32s3/bootloader-multi.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions boot/espressif/port/esp32s3/bootloader.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions boot/espressif/port/esp_mcuboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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, ...);

Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down

0 comments on commit 6b07b98

Please # to comment.