From a0bb5991077282885b797d533f5670845f62ffb7 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Fri, 23 Jun 2023 16:19:25 +0000 Subject: [PATCH] [nrf fromlist] mgmt/MCUmgr/grp/img: Add support for three image configuration The commit adds support for uploading images to secondary slots of three images. Upstream PR: https://github.com/zephyrproject-rtos/zephyr/pull/59724 (cherry picked from commit a717dd20f3f54bf59d806974714e079f7fdb80bc) Signed-off-by: Dominik Ermel --- subsys/dfu/Kconfig | 2 +- subsys/mgmt/mcumgr/grp/img_mgmt/Kconfig | 2 +- .../mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c | 62 +++++++++---------- .../mcumgr/grp/img_mgmt/src/zephyr_img_mgmt.c | 36 +++++++++-- 4 files changed, 63 insertions(+), 39 deletions(-) diff --git a/subsys/dfu/Kconfig b/subsys/dfu/Kconfig index f998b2ef9f2..121fa3964f6 100644 --- a/subsys/dfu/Kconfig +++ b/subsys/dfu/Kconfig @@ -89,7 +89,7 @@ if !MCUBOOT config UPDATEABLE_IMAGE_NUMBER int "Number of updateable images" default 1 - range 1 2 + range 1 3 help If value is set to 2 or greater then, this enables support needed when application is combined with MCUboot multi-image boot. diff --git a/subsys/mgmt/mcumgr/grp/img_mgmt/Kconfig b/subsys/mgmt/mcumgr/grp/img_mgmt/Kconfig index 588a50ffd74..a12beaadca9 100644 --- a/subsys/mgmt/mcumgr/grp/img_mgmt/Kconfig +++ b/subsys/mgmt/mcumgr/grp/img_mgmt/Kconfig @@ -45,7 +45,7 @@ endif config MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER int "Number of supported images" default UPDATEABLE_IMAGE_NUMBER - range 1 2 + range 1 3 help Sets how many application images are supported (pairs of secondary and primary slots). Setting this to 2 requires MCUMGR_TRANSPORT_NETBUF_SIZE to be at least 512b. diff --git a/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c b/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c index 2f5ffe1b50e..6df14167ace 100644 --- a/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c +++ b/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c @@ -60,38 +60,36 @@ BUILD_ASSERT(PM_MCUBOOT_PAD_SIZE == PM_MCUBOOT_SECONDARY_PAD_SIZE); (FIXED_PARTITION_OFFSET(label) == CONFIG_FLASH_LOAD_OFFSET) #endif /* USE_PARTITION_MANAGER */ -#if FIXED_PARTITION_EXISTS(slot0_partition) -#if FIXED_PARTITION_IS_RUNNING_APP_PARTITION(slot0_partition) -#define NUMBER_OF_ACTIVE_IMAGE 0 -#endif -#endif - -#if !defined(NUMBER_OF_ACTIVE_IMAGE) && FIXED_PARTITION_EXISTS(slot0_ns_partition) -#if FIXED_PARTITION_IS_RUNNING_APP_PARTITION(slot0_ns_partition) -#define NUMBER_OF_ACTIVE_IMAGE 0 -#endif -#endif - -#if !defined(NUMBER_OF_ACTIVE_IMAGE) && FIXED_PARTITION_EXISTS(slot1_partition) -#if FIXED_PARTITION_IS_RUNNING_APP_PARTITION(slot1_partition) -#define NUMBER_OF_ACTIVE_IMAGE 0 -#endif -#endif - -#if !defined(NUMBER_OF_ACTIVE_IMAGE) && FIXED_PARTITION_EXISTS(slot2_partition) -#if FIXED_PARTITION_IS_RUNNING_APP_PARTITION(slot2_partition) -#define NUMBER_OF_ACTIVE_IMAGE 1 -#endif -#endif - -#if !defined(NUMBER_OF_ACTIVE_IMAGE) && FIXED_PARTITION_EXISTS(slot3_partition) -#if FIXED_PARTITION_IS_RUNNING_APP_PARTITION(slot3_partition) -#define NUMBER_OF_ACTIVE_IMAGE 1 -#endif +BUILD_ASSERT(sizeof(struct image_header) == IMAGE_HEADER_SIZE, + "struct image_header not required size"); + +#if CONFIG_MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER >= 2 +#if FIXED_PARTITION_EXISTS(slot0_ns_partition) && \ + FIXED_PARTITION_IS_RUNNING_APP_PARTITION(slot0_ns_partition) +#define ACTIVE_IMAGE_IS 0 +#elif FIXED_PARTITION_EXISTS(slot0_partition) && \ + FIXED_PARTITION_IS_RUNNING_APP_PARTITION(slot0_partition) +#define ACTIVE_IMAGE_IS 0 +#elif FIXED_PARTITION_EXISTS(slot1_partition) && \ + FIXED_PARTITION_IS_RUNNING_APP_PARTITION(slot1_partition) +#define ACTIVE_IMAGE_IS 0 +#elif FIXED_PARTITION_EXISTS(slot2_partition) && \ + FIXED_PARTITION_IS_RUNNING_APP_PARTITION(slot2_partition) +#define ACTIVE_IMAGE_IS 1 +#elif FIXED_PARTITION_EXISTS(slot3_partition) && \ + FIXED_PARTITION_IS_RUNNING_APP_PARTITION(slot3_partition) +#define ACTIVE_IMAGE_IS 1 +#elif FIXED_PARTITION_EXISTS(slot4_partition) && \ + FIXED_PARTITION_IS_RUNNING_APP_PARTITION(slot4_partition) +#define ACTIVE_IMAGE_IS 2 +#elif FIXED_PARTITION_EXISTS(slot5_partition) && \ + FIXED_PARTITION_IS_RUNNING_APP_PARTITION(slot5_partition) +#define ACTIVE_IMAGE_IS 2 +#else +#define ACTIVE_IMAGE_IS 0 #endif - -#ifndef NUMBER_OF_ACTIVE_IMAGE -#error "Unsupported code parition is set as active application partition" +#else +#define ACTIVE_IMAGE_IS 0 #endif LOG_MODULE_REGISTER(mcumgr_img_grp, CONFIG_MCUMGR_GRP_IMG_LOG_LEVEL); @@ -173,7 +171,7 @@ int img_mgmt_active_slot(int image) int img_mgmt_active_image(void) { - return NUMBER_OF_ACTIVE_IMAGE; + return ACTIVE_IMAGE_IS; } /* diff --git a/subsys/mgmt/mcumgr/grp/img_mgmt/src/zephyr_img_mgmt.c b/subsys/mgmt/mcumgr/grp/img_mgmt/src/zephyr_img_mgmt.c index a259a67acc2..bec796267cc 100644 --- a/subsys/mgmt/mcumgr/grp/img_mgmt/src/zephyr_img_mgmt.c +++ b/subsys/mgmt/mcumgr/grp/img_mgmt/src/zephyr_img_mgmt.c @@ -26,12 +26,26 @@ LOG_MODULE_DECLARE(mcumgr_img_grp, CONFIG_MCUMGR_GRP_IMG_LOG_LEVEL); #define SLOT1_PARTITION slot1_partition #define SLOT2_PARTITION slot2_partition #define SLOT3_PARTITION slot3_partition +#define SLOT4_PARTITION slot4_partition +#define SLOT5_PARTITION slot5_partition + +/* SLOT0_PARTITION and SLOT1_PARTITION are not checked because + * there is not conditional code that depends on them. If they do + * not exist compilation will fail, but in case if some of other + * partitions do not exist, code will compile and will not work + * properly. + */ +#if CONFIG_MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER >= 2 +BUILD_ASSERT(FIXED_PARTITION_EXISTS(SLOT2_PARTITION) && + FIXED_PARTITION_EXISTS(SLOT3_PARTITION), + "Missing partitions?"); +#endif -BUILD_ASSERT(CONFIG_MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER == 1 || - (CONFIG_MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER == 2 && - FIXED_PARTITION_EXISTS(SLOT2_PARTITION) && - FIXED_PARTITION_EXISTS(SLOT3_PARTITION)), +#if CONFIG_MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER == 3 +BUILD_ASSERT(FIXED_PARTITION_EXISTS(SLOT4_PARTITION) && + FIXED_PARTITION_EXISTS(SLOT5_PARTITION), "Missing partitions?"); +#endif /** * Determines if the specified area of flash is completely unwritten. @@ -138,6 +152,18 @@ img_mgmt_flash_area_id(int slot) break; #endif +#if FIXED_PARTITION_EXISTS(SLOT4_PARTITION) + case 4: + fa_id = FIXED_PARTITION_ID(SLOT4_PARTITION); + break; +#endif + +#if FIXED_PARTITION_EXISTS(SLOT5_PARTITION) + case 5: + fa_id = FIXED_PARTITION_ID(SLOT5_PARTITION); + break; +#endif + default: fa_id = -1; break; @@ -195,7 +221,7 @@ static int img_mgmt_get_unused_slot_area_id(int slot) return slot != -1 ? img_mgmt_flash_area_id(slot) : -1; #endif } -#elif CONFIG_MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER == 2 +#elif CONFIG_MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER >= 2 static int img_mgmt_get_unused_slot_area_id(int image) { int area_id = -1;