sim: Support non-continuous area ID mapping
Support non-continuous image flash area ID mapping. It was assumed
that the flash area IDs are subsequent and increasing numbers which
might not be true in all cases.
Change-Id: I0d1285d6fcf1e83a64611c9ad4f65abd002c25d3
Signed-off-by: David Vincze <david.vincze@arm.com>
diff --git a/sim/mcuboot-sys/csupport/run.c b/sim/mcuboot-sys/csupport/run.c
index fdee14c..8f2a01c 100644
--- a/sim/mcuboot-sys/csupport/run.c
+++ b/sim/mcuboot-sys/csupport/run.c
@@ -25,6 +25,8 @@
#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_ERROR
#include <bootutil/bootutil_log.h>
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+
extern int sim_flash_erase(uint8_t flash_id, uint32_t offset, uint32_t size);
extern int sim_flash_read(uint8_t flash_id, uint32_t offset, uint8_t *dest,
uint32_t size);
@@ -242,7 +244,16 @@
int flash_area_id_from_image_slot(int slot)
{
- return slot + 1;
+ const int area_id_tab[] = {FLASH_AREA_IMAGE_PRIMARY,
+ FLASH_AREA_IMAGE_SECONDARY,
+ FLASH_AREA_IMAGE_SCRATCH};
+
+ if (slot >= 0 && (unsigned)slot < ARRAY_SIZE(area_id_tab)) {
+ return area_id_tab[slot];
+ }
+
+ printf("Image flash area ID not found\n");
+ return -1; /* flash_area_open will fail on that */
}
int flash_area_open(uint8_t id, const struct flash_area **area)