diff --git a/subsys/mgmt/mcumgr/grp/img_mgmt/Kconfig b/subsys/mgmt/mcumgr/grp/img_mgmt/Kconfig index b75b92dddbc..641e16bd703 100644 --- a/subsys/mgmt/mcumgr/grp/img_mgmt/Kconfig +++ b/subsys/mgmt/mcumgr/grp/img_mgmt/Kconfig @@ -82,6 +82,15 @@ config MCUMGR_GRP_IMG_ALLOW_CONFIRM_NON_ACTIVE_IMAGE_ANY broken and may not boot in other slot; instead application should have means to test and confirm the image. +if !MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP +config MCUMGR_GRP_IMG_ALLOW_ERASE_PENDING + bool "Allow to erase pending slot" + help + Allows erasing secondary slot which is marked for test or confirmed; this allows + erasing slots that have been set for next boot but the device has not + reset yet, so has not yet been swapped. +endif + config MCUMGR_GRP_IMG_DIRECT_UPLOAD bool "Allow direct image upload" help 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 dce0c8c1d5c..0d733ce7abc 100644 --- a/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c +++ b/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt.c @@ -322,24 +322,6 @@ static void img_mgmt_reset_upload(void) img_mgmt_release_lock(); } -static int -img_mgmt_get_other_slot(void) -{ - int slot = img_mgmt_active_slot(img_mgmt_active_image()); - - switch (slot) { - case 1: - return 0; -#if CONFIG_MCUMGR_GRP_IMG_UPDATABLE_IMAGE_NUMBER > 2 - case 2: - return 3; - case 3: - return 2; -#endif - } - return 1; -} - /** * Command handler: image erase */ @@ -351,7 +333,7 @@ img_mgmt_erase(struct smp_streamer *ctxt) zcbor_state_t *zse = ctxt->writer->zs; zcbor_state_t *zsd = ctxt->reader->zs; bool ok; - uint32_t slot = img_mgmt_get_other_slot(); + uint32_t slot = img_mgmt_get_opposite_slot(img_mgmt_active_slot(img_mgmt_active_image())); size_t decoded = 0; struct zcbor_map_decode_key_val image_erase_decode[] = { diff --git a/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt_state.c b/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt_state.c index 0480f59fb5c..a782acb92ee 100644 --- a/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt_state.c +++ b/subsys/mgmt/mcumgr/grp/img_mgmt/src/img_mgmt_state.c @@ -233,9 +233,24 @@ img_mgmt_slot_in_use(int slot) int active_slot = img_mgmt_active_slot(image); #if !defined(CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP) - if (slot == img_mgmt_get_next_boot_slot(image, NULL)) { + enum img_mgmt_next_boot_type type = NEXT_BOOT_TYPE_NORMAL; + int nbs = img_mgmt_get_next_boot_slot(image, &type); + + if (slot == nbs && type == NEXT_BOOT_TYPE_REVERT) { + LOG_DBG("(%d) Refused erase revert", slot); return 1; } + + if ((slot == nbs && type == NEXT_BOOT_TYPE_TEST) || + (active_slot != nbs && type == NEXT_BOOT_TYPE_NORMAL)) { +#if defined(CONFIG_MCUMGR_GRP_IMG_ALLOW_ERASE_PENDING) + LOG_DBG("(%d) Allowed erase pending", slot); + /* Pass through to return (active_slot == slot) */ +#else + LOG_DBG("(%d) Refused erase pending", slot); + return 1; +#endif + } #endif return (active_slot == slot);