From 453b0dd790770ebcf062b3a0606a108cf80749c5 Mon Sep 17 00:00:00 2001 From: Andrzej Puzdrowski Date: Wed, 16 Feb 2022 13:55:10 +0100 Subject: [PATCH] sim: added flash_area_sector_from_off() API implementation Added implementation of the above API function. Signed-off-by: Andrzej Puzdrowski --- sim/mcuboot-sys/csupport/run.c | 33 ++++++++++++++++++++ sim/mcuboot-sys/csupport/storage/flash_map.h | 7 +++++ 2 files changed, 40 insertions(+) diff --git a/sim/mcuboot-sys/csupport/run.c b/sim/mcuboot-sys/csupport/run.c index 82d3e11e..df62cb4d 100644 --- a/sim/mcuboot-sys/csupport/run.c +++ b/sim/mcuboot-sys/csupport/run.c @@ -448,6 +448,39 @@ int flash_area_id_from_image_slot(int slot) { return flash_area_id_from_multi_image_slot(0, slot); } +int flash_area_sector_from_off(uint32_t off, struct flash_sector *sector) +{ + uint32_t i, sec_off, sec_size; + struct area *slot; + struct area_desc *flash_areas; + + flash_areas = sim_get_flash_areas(); + for (i = 0; i < flash_areas->num_slots; i++) { + if (flash_areas->slots[i].id == FLASH_AREA_ID(image_0)) + break; + } + + if (i == flash_areas->num_slots) { + printf("Unsupported area\n"); + abort(); + } + + slot = &flash_areas->slots[i]; + + for (i = 0; i < slot->num_areas; i++) { + sec_off = slot->areas[i].fa_off - slot->whole.fa_off; + sec_size = slot->areas[i].fa_size; + + if (off >= sec_off && off < (sec_off + sec_size)) { + sector->fs_off = sec_off; + sector->fs_size = sec_size; + break; + } + } + + return (i < slot->num_areas) ? 0 : -1; +} + void sim_assert(int x, const char *assertion, const char *file, unsigned int line, const char *function) { if (!(x)) { diff --git a/sim/mcuboot-sys/csupport/storage/flash_map.h b/sim/mcuboot-sys/csupport/storage/flash_map.h index f8388f5b..92649d9d 100644 --- a/sim/mcuboot-sys/csupport/storage/flash_map.h +++ b/sim/mcuboot-sys/csupport/storage/flash_map.h @@ -136,6 +136,13 @@ uint8_t flash_area_erased_val(const struct flash_area *); int flash_area_get_sectors(int fa_id, uint32_t *count, struct flash_sector *sectors); + +/* Retrieve the flash sector a given offset belongs to. + * + * Returns 0 on success, or an error code on failure. + */ +int flash_area_sector_from_off(uint32_t off, struct flash_sector *sector); + /* * Similar to flash_area_get_sectors(), but return the values in an * array of struct flash_area instead.