dfu: flash_img: add flash_img_get_upload_slot()

add flash_img_get_upload_slot() to get current
upload slot.
when CONFIG_MCUBOOT_BOOTLOADER_MODE_RAM_LOAD
is enabled, it is not based on the DT.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
This commit is contained in:
Fin Maaß 2024-11-06 13:22:19 +01:00 committed by Benjamin Cabé
parent cd0a20d1a9
commit beffba6d87
4 changed files with 46 additions and 4 deletions

View file

@ -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

View file

@ -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.
*

View file

@ -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)

View file

@ -10,12 +10,12 @@
#include <stdio.h>
#include <string.h>
#include <zephyr/dfu/flash_img.h>
#include <zephyr/dfu/mcuboot.h>
#include <zephyr/storage/flash_map.h>
#include <zephyr/storage/stream_flash.h>
#ifdef CONFIG_IMG_ERASE_PROGRESSIVELY
#include <bootutil/bootutil_public.h>
#include <zephyr/dfu/mcuboot.h>
#endif
#include <zephyr/devicetree.h>
@ -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);