diff --git a/include/zephyr/dfu/flash_img.h b/include/zephyr/dfu/flash_img.h index 00446852609..8ae5fe52e47 100644 --- a/include/zephyr/dfu/flash_img.h +++ b/include/zephyr/dfu/flash_img.h @@ -111,6 +111,13 @@ int flash_img_check(struct flash_img_context *ctx, const struct flash_img_check *fic, uint8_t area_id); +/** + * @brief Get the flash area id for the image upload slot. + * + * @return flash area id for the image upload slot + */ +uint8_t flash_img_get_upload_slot(void); + #ifdef __cplusplus } #endif diff --git a/include/zephyr/dfu/mcuboot.h b/include/zephyr/dfu/mcuboot.h index fe15f769f98..8788ec4657e 100644 --- a/include/zephyr/dfu/mcuboot.h +++ b/include/zephyr/dfu/mcuboot.h @@ -159,6 +159,13 @@ int boot_read_bank_header(uint8_t area_id, struct mcuboot_img_header *header, size_t header_size); +/** + * @brief Get the flash area id for the active image slot. + * + * @return flash area id for the active image slot + */ +uint8_t boot_fetch_active_slot(void); + /** * @brief Check if the currently running image is confirmed as OK. * diff --git a/subsys/dfu/boot/mcuboot.c b/subsys/dfu/boot/mcuboot.c index 93d58cb5b6c..a23c82dce75 100644 --- a/subsys/dfu/boot/mcuboot.c +++ b/subsys/dfu/boot/mcuboot.c @@ -44,7 +44,6 @@ LOG_MODULE_REGISTER(mcuboot_dfu, LOG_LEVEL_DBG); #if defined(CONFIG_MCUBOOT_BOOTLOADER_MODE_RAM_LOAD) /* For RAM LOAD mode, the active image must be fetched from the bootloader */ -static uint8_t boot_fetch_active_slot(void); #define ACTIVE_SLOT_FLASH_AREA_ID boot_fetch_active_slot() #define INVALID_SLOT_ID 255 #else @@ -76,7 +75,7 @@ struct mcuboot_v1_raw_header { */ #if defined(CONFIG_MCUBOOT_BOOTLOADER_MODE_RAM_LOAD) -static uint8_t boot_fetch_active_slot(void) +uint8_t boot_fetch_active_slot(void) { int rc; uint8_t slot; @@ -93,7 +92,12 @@ static uint8_t boot_fetch_active_slot(void) return slot; } -#endif +#else /* CONFIG_MCUBOOT_BOOTLOADER_MODE_RAM_LOAD */ +uint8_t boot_fetch_active_slot(void) +{ + return ACTIVE_SLOT_FLASH_AREA_ID; +} +#endif /* CONFIG_MCUBOOT_BOOTLOADER_MODE_RAM_LOAD */ static int boot_read_v1_header(uint8_t area_id, struct mcuboot_v1_raw_header *v1_raw) diff --git a/subsys/dfu/img_util/flash_img.c b/subsys/dfu/img_util/flash_img.c index 69fa559f0e3..31e65c4e30c 100644 --- a/subsys/dfu/img_util/flash_img.c +++ b/subsys/dfu/img_util/flash_img.c @@ -10,12 +10,12 @@ #include #include #include +#include #include #include #ifdef CONFIG_IMG_ERASE_PROGRESSIVELY #include -#include #endif #include @@ -29,8 +29,13 @@ #endif #endif +#ifdef CONFIG_MCUBOOT_BOOTLOADER_MODE_RAM_LOAD +/* For RAM LOAD mode, the active image must be fetched from the bootloader */ +#define UPLOAD_FLASH_AREA_ID flash_img_get_upload_slot() +#else /* FIXED_PARTITION_ID() values used below are auto-generated by DT */ #define UPLOAD_FLASH_AREA_ID FIXED_PARTITION_ID(UPLOAD_FLASH_AREA_LABEL) +#endif /* CONFIG_MCUBOOT_BOOTLOADER_MODE_RAM_LOAD */ #define UPLOAD_FLASH_AREA_CONTROLLER \ DT_GPARENT(DT_NODELABEL(UPLOAD_FLASH_AREA_LABEL)) @@ -145,6 +150,25 @@ int flash_img_init_id(struct flash_img_context *ctx, uint8_t area_id) ctx->flash_area->fa_size, NULL); } +#ifdef CONFIG_MCUBOOT_BOOTLOADER_MODE_RAM_LOAD +uint8_t flash_img_get_upload_slot(void) +{ + uint8_t slot; + + slot = boot_fetch_active_slot(); + + if (slot == FIXED_PARTITION_ID(slot0_partition)) { + return FIXED_PARTITION_ID(slot1_partition); + } + return FIXED_PARTITION_ID(slot0_partition); +} +#else /* CONFIG_MCUBOOT_BOOTLOADER_MODE_RAM_LOAD */ +uint8_t flash_img_get_upload_slot(void) +{ + return UPLOAD_FLASH_AREA_ID; +} +#endif /* CONFIG_MCUBOOT_BOOTLOADER_MODE_RAM_LOAD */ + int flash_img_init(struct flash_img_context *ctx) { return flash_img_init_id(ctx, UPLOAD_FLASH_AREA_ID);