Commit 4a748bfe authored by Fabio Utzig's avatar Fabio Utzig Committed by Fabio Utzig
Browse files

mynewt: add flash sector requirement for swap move



Add basic flash_sector struct and offset calculation routines. This
fixes the build using swap move, because this data is required to
calculate the maximum image size.

Fixes #1567

Signed-off-by: default avatarFabio Utzig <utzig@apache.org>
parent 5a013e32
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
#include <sysflash/sysflash.h>
#include <flash_map/flash_map.h>
#include <mcuboot_config/mcuboot_config.h>
#include <unistd.h>  /* off_t */

#if (MCUBOOT_IMAGE_NUMBER == 1)

@@ -40,6 +41,13 @@
int flash_area_id_from_multi_image_slot(int image_index, int slot);
int flash_area_id_to_multi_image_slot(int image_index, int area_id);

struct flash_sector {
    uint32_t fs_off;
    uint32_t fs_size;
};

int flash_area_sector_from_off(off_t off, struct flash_sector *sector);

static inline uint8_t flash_area_get_id(const struct flash_area *fa)
{
    return fa->fa_id;
@@ -60,4 +68,9 @@ static inline uint32_t flash_area_get_size(const struct flash_area *fa)
    return fa->fa_size;
}

static inline uint32_t flash_sector_get_off(const struct flash_sector *fs)
{
    return fs->fs_off;
}

#endif /* __FLASH_MAP_BACKEND_H__ */
+35 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@

#include <flash_map/flash_map.h>
#include <flash_map_backend/flash_map_backend.h>
#include <hal/hal_bsp.h>
#include <hal/hal_flash_int.h>

int flash_area_id_from_multi_image_slot(int image_index, int slot)
{
@@ -42,3 +44,36 @@ int flash_area_id_to_multi_image_slot(int image_index, int area_id)
    }
    return 255;
}

int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
{
    const struct flash_area *fa;
    const struct hal_flash *hf;
    uint32_t start;
    uint32_t size;
    int rc;
    int i;

    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fa);
    if (rc != 0) {
        return -1;
    }

    rc = -1;
    hf = hal_bsp_flash_dev(fa->fa_device_id);
    for (i = 0; i < hf->hf_sector_cnt; i++) {
        hf->hf_itf->hff_sector_info(hf, i, &start, &size);
        if (start < fa->fa_off) {
            continue;
        }
        if (off >= start - fa->fa_off && off <= (start - fa->fa_off) + size) {
            sector->fs_off = start - fa->fa_off;
            sector->fs_size = size;
            rc = 0;
            break;
        }
    }

    flash_area_close(fa);
    return rc;
}