Remove current_image global and macro updates
Currently to determine which image is being operated on, there is a global
variable called current_image which is used by most macros and APIs to
correctly access the flash areas required by the bootloader. This moves
this variable to the already existing state struct and refactors all
macros and APIs to receive the current image by parameters. To maintain
compatibility some of the macros were not updated and use image 0 when
called.
The definitions of FLASH_AREA_IMAGE_PRIMARY and FLASH_AREA_IMAGE_SECONDARY
for Mynewt compatibility were moved out of bootutil sources to a Mynewt
specific include file.
Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/boot/zephyr/flash_map_extended.c b/boot/zephyr/flash_map_extended.c
index 535e69a..47da3b7 100644
--- a/boot/zephyr/flash_map_extended.c
+++ b/boot/zephyr/flash_map_extended.c
@@ -53,28 +53,28 @@
* MCUBoot uses continuous numbering for the primary slot, the secondary slot,
* and the scratch while zephyr might number it differently.
*/
-int flash_area_id_from_image_slot(int slot)
+int flash_area_id_from_multi_image_slot(int image_index, int slot)
{
-#if (MCUBOOT_IMAGE_NUMBER == 1)
- static
-#endif
- const int area_id_tab[] = {FLASH_AREA_IMAGE_PRIMARY,
- FLASH_AREA_IMAGE_SECONDARY,
- FLASH_AREA_IMAGE_SCRATCH};
-
- if (slot >= 0 && slot < ARRAY_SIZE(area_id_tab)) {
- return area_id_tab[slot];
+ switch (slot) {
+ case 0: return FLASH_AREA_IMAGE_PRIMARY(image_index);
+ case 1: return FLASH_AREA_IMAGE_SECONDARY(image_index);
+ case 2: return FLASH_AREA_IMAGE_SCRATCH;
}
return -EINVAL; /* flash_area_open will fail on that */
}
-int flash_area_id_to_image_slot(int area_id)
+int flash_area_id_from_image_slot(int slot)
{
- if (area_id == FLASH_AREA_IMAGE_PRIMARY) {
+ return flash_area_id_from_multi_image_slot(0, slot);
+}
+
+int flash_area_id_to_multi_image_slot(int image_index, int area_id)
+{
+ if (area_id == FLASH_AREA_IMAGE_PRIMARY(image_index)) {
return 0;
}
- if (area_id == FLASH_AREA_IMAGE_SECONDARY) {
+ if (area_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
return 1;
}
@@ -82,6 +82,11 @@
return -1;
}
+int flash_area_id_to_image_slot(int area_id)
+{
+ return flash_area_id_to_multi_image_slot(0, area_id);
+}
+
int flash_area_sector_from_off(off_t off, struct flash_sector *sector)
{
int rc;