zephyr: allow dynamic numeration of flash_areas
Zephyr flash_map reworks caused that areas id exact number are
assigned dynamically.
This patch i counterpart to
https://github.com/zephyrproject-rtos/zephyr/pull/8837
Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
diff --git a/boot/zephyr/flash_map_extended.c b/boot/zephyr/flash_map_extended.c
index 5098012..0b56eeb 100644
--- a/boot/zephyr/flash_map_extended.c
+++ b/boot/zephyr/flash_map_extended.c
@@ -49,12 +49,20 @@
}
/*
- * This depends on the mappings defined in sysflash.h, and assumes
- * that slot 0, slot 1, and the scratch areas are contiguous.
+ * This depends on the mappings defined in sysflash.h.
+ * MCUBoot uses continuous numbering for slot 0, slot 1, and the scratch
+ * while zephyr might number it differently.
*/
int flash_area_id_from_image_slot(int slot)
{
- return slot + FLASH_AREA_IMAGE_0;
+ static const int area_id_tab[] = {FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
+ FLASH_AREA_IMAGE_SCRATCH};
+
+ if (slot >= 0 && slot < ARRAY_SIZE(area_id_tab)) {
+ return area_id_tab[slot];
+ }
+
+ return -EINVAL; /* flash_area_open will fail on that */
}
int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
diff --git a/boot/zephyr/include/sysflash/sysflash.h b/boot/zephyr/include/sysflash/sysflash.h
index e08f9eb..0fef026 100644
--- a/boot/zephyr/include/sysflash/sysflash.h
+++ b/boot/zephyr/include/sysflash/sysflash.h
@@ -3,8 +3,10 @@
#ifndef __SYSFLASH_H__
#define __SYSFLASH_H__
-#define FLASH_AREA_IMAGE_0 1
-#define FLASH_AREA_IMAGE_1 2
-#define FLASH_AREA_IMAGE_SCRATCH 3
+#include <generated_dts_board.h>
+
+#define FLASH_AREA_IMAGE_0 DT_FLASH_AREA_IMAGE_0_ID
+#define FLASH_AREA_IMAGE_1 DT_FLASH_AREA_IMAGE_1_ID
+#define FLASH_AREA_IMAGE_SCRATCH DT_FLASH_AREA_IMAGE_SCRATCH_ID
#endif /* __SYSFLASH_H__ */
diff --git a/sim/mcuboot-sys/csupport/generated_dts_board.h b/sim/mcuboot-sys/csupport/generated_dts_board.h
new file mode 100644
index 0000000..01c56ca
--- /dev/null
+++ b/sim/mcuboot-sys/csupport/generated_dts_board.h
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2019 Nordic Semiconductor ASA
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/* This file mocks zephyr's autogenerated DT output header file */
+
+#ifndef __GENERATED_DTS_BOARD_H__
+#define __GENERATED_DTS_BOARD_H__
+
+#define DT_FLASH_AREA_IMAGE_0_ID 1
+#define DT_FLASH_AREA_IMAGE_1_ID 2
+#define DT_FLASH_AREA_IMAGE_SCRATCH_ID 3
+
+#endif /*__GENERATED_DTS_BOARD_H__*/