From 7648989328f647151c74aa7980d37ebc7db7f5e5 Mon Sep 17 00:00:00 2001 From: Michal Lenc Date: Tue, 20 Feb 2024 11:05:52 +0100 Subject: [PATCH] boot/nuttx: add configuration for tertiary slot This slot is used in newly introduced copy with revert algorithm. This commit allows NuttX to support this algorithm. Signed-off-by: Michal Lenc --- .../include/mcuboot_config/mcuboot_config.h | 10 +++++++ boot/nuttx/include/sysflash/sysflash.h | 6 +++- .../src/flash_map_backend/flash_map_backend.c | 28 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/boot/nuttx/include/mcuboot_config/mcuboot_config.h b/boot/nuttx/include/mcuboot_config/mcuboot_config.h index 41e7d6742..585bd0410 100644 --- a/boot/nuttx/include/mcuboot_config/mcuboot_config.h +++ b/boot/nuttx/include/mcuboot_config/mcuboot_config.h @@ -78,6 +78,16 @@ # define MCUBOOT_OVERWRITE_ONLY_FAST #endif +/* Enable copy with revert algorithm. This algorithm uses three slots and + * always keep the current image in both primary and secondary or tertiary + * partitions. This way update can be done without swapping while keeping + * the ability to revert. + */ + +#ifdef CONFIG_MCUBOOT_COPY_WITH_REVERT +# define MCUBOOT_COPY_WITH_REVERT +#endif + /* Enable the direct-xip code path. */ #ifdef CONFIG_MCUBOOT_DIRECT_XIP diff --git a/boot/nuttx/include/sysflash/sysflash.h b/boot/nuttx/include/sysflash/sysflash.h index 304ca0c1a..91ad07275 100644 --- a/boot/nuttx/include/sysflash/sysflash.h +++ b/boot/nuttx/include/sysflash/sysflash.h @@ -26,7 +26,8 @@ #define PRIMARY_ID 0 #define SECONDARY_ID 1 -#define SCRATCH_ID 2 +#define TERTIARY_ID 2 +#define SCRATCH_ID 3 #define FLASH_AREA_IMAGE_PRIMARY(x) (((x) == 0) ? \ PRIMARY_ID : \ @@ -34,6 +35,9 @@ #define FLASH_AREA_IMAGE_SECONDARY(x) (((x) == 0) ? \ SECONDARY_ID : \ SECONDARY_ID) +#define FLASH_AREA_IMAGE_TERTIARY(x) (((x) == 0) ? \ + TERTIARY_ID : \ + TERTIARY_ID) #define FLASH_AREA_IMAGE_SCRATCH SCRATCH_ID #endif /* __BOOT_NUTTX_INCLUDE_SYSFLASH_SYSFLASH_H */ diff --git a/boot/nuttx/src/flash_map_backend/flash_map_backend.c b/boot/nuttx/src/flash_map_backend/flash_map_backend.c index ae3ec6488..575d907d9 100644 --- a/boot/nuttx/src/flash_map_backend/flash_map_backend.c +++ b/boot/nuttx/src/flash_map_backend/flash_map_backend.c @@ -123,6 +123,31 @@ static struct flash_device_s g_secondary_priv = .erase_state = CONFIG_MCUBOOT_DEFAULT_FLASH_ERASE_STATE }; +static struct flash_area g_tertiary_img0 = +{ + .fa_id = FLASH_AREA_IMAGE_TERTIARY(0), + .fa_device_id = 0, + .fa_off = 0, + .fa_size = 0, + .fa_mtd_path = CONFIG_MCUBOOT_TERTIARY_SLOT_PATH +}; + +static struct flash_device_s g_tertiary_priv = +{ + .fa_cfg = &g_tertiary_img0, + .mtdgeo = + { + 0 + }, + .partinfo = + { + 0 + }, + .fd = -1, + .refs = 0, + .erase_state = CONFIG_MCUBOOT_DEFAULT_FLASH_ERASE_STATE +}; + static struct flash_area g_scratch_img0 = { .fa_id = FLASH_AREA_IMAGE_SCRATCH, @@ -152,6 +177,7 @@ static struct flash_device_s *g_flash_devices[] = { &g_primary_priv, &g_secondary_priv, + &g_tertiary_priv, &g_scratch_priv, }; @@ -713,6 +739,8 @@ int flash_area_id_from_multi_image_slot(int image_index, int slot) return FLASH_AREA_IMAGE_PRIMARY(image_index); case 1: return FLASH_AREA_IMAGE_SECONDARY(image_index); + case 2: + return FLASH_AREA_IMAGE_TERTIARY(image_index); } BOOT_LOG_ERR("Unexpected Request: image_index:%d, slot:%d",