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;
diff --git a/boot/zephyr/include/flash_map_backend/flash_map_backend.h b/boot/zephyr/include/flash_map_backend/flash_map_backend.h
index c082dd6..33c34f0 100644
--- a/boot/zephyr/include/flash_map_backend/flash_map_backend.h
+++ b/boot/zephyr/include/flash_map_backend/flash_map_backend.h
@@ -51,6 +51,7 @@
 int flash_device_base(uint8_t fd_id, uintptr_t *ret);
 
 int flash_area_id_from_image_slot(int slot);
+int flash_area_id_from_multi_image_slot(int image_index, int slot);
 
 /**
  * Converts the specified flash area ID to an image slot index.
@@ -60,6 +61,15 @@
  */
 int flash_area_id_to_image_slot(int area_id);
 
+/**
+ * Converts the specified flash area ID and image index (in multi-image setup)
+ * to an image slot index.
+ *
+ * Returns image slot index (0 or 1), or -1 if ID doesn't correspond to an image
+ * slot.
+ */
+int flash_area_id_to_multi_image_slot(int image_index, int area_id);
+
 /* Retrieve the flash sector a given offset belongs to.
  *
  * Returns 0 on success, or an error code on failure.
diff --git a/boot/zephyr/include/sysflash/sysflash.h b/boot/zephyr/include/sysflash/sysflash.h
index d4617bb..c422efd 100644
--- a/boot/zephyr/include/sysflash/sysflash.h
+++ b/boot/zephyr/include/sysflash/sysflash.h
@@ -6,26 +6,33 @@
 #include <generated_dts_board.h>
 #include <mcuboot_config/mcuboot_config.h>
 
-extern uint8_t current_image;
-
 #if (MCUBOOT_IMAGE_NUMBER == 1)
-#define FLASH_AREA_IMAGE_PRIMARY    DT_FLASH_AREA_IMAGE_0_ID
-#define FLASH_AREA_IMAGE_SECONDARY  DT_FLASH_AREA_IMAGE_1_ID
+/*
+ * NOTE: the definition below returns the same values for true/false on
+ * purpose, to avoid having to mark x as non-used by all callers when
+ * running in single image mode.
+ */
+#define FLASH_AREA_IMAGE_PRIMARY(x)    (((x) == 0) ?                \
+                                         DT_FLASH_AREA_IMAGE_0_ID : \
+                                         DT_FLASH_AREA_IMAGE_0_ID)
+#define FLASH_AREA_IMAGE_SECONDARY(x)  (((x) == 0) ?                \
+                                         DT_FLASH_AREA_IMAGE_1_ID : \
+                                         DT_FLASH_AREA_IMAGE_1_ID)
 #elif (MCUBOOT_IMAGE_NUMBER == 2)
 /* MCUBoot currently supports only up to 2 updateable firmware images.
  * If the number of the current image is greater than MCUBOOT_IMAGE_NUMBER - 1
  * then a dummy value will be assigned to the flash area macros.
  */
-#define FLASH_AREA_IMAGE_PRIMARY    ((current_image == 0) ?         \
+#define FLASH_AREA_IMAGE_PRIMARY(x)    (((x) == 0) ?                \
                                          DT_FLASH_AREA_IMAGE_0_ID : \
-                                     (current_image == 1) ?         \
+                                        ((x) == 1) ?                \
                                          DT_FLASH_AREA_IMAGE_2_ID : \
-                                         255 )
-#define FLASH_AREA_IMAGE_SECONDARY  ((current_image == 0) ?         \
+                                         255)
+#define FLASH_AREA_IMAGE_SECONDARY(x)  (((x) == 0) ?                \
                                          DT_FLASH_AREA_IMAGE_1_ID : \
-                                     (current_image == 1) ?         \
+                                        ((x) == 1) ?                \
                                          DT_FLASH_AREA_IMAGE_3_ID : \
-                                         255 )
+                                         255)
 #else
 #error "Image slot and flash area mapping is not defined"
 #endif