Add basic multi-image support for Mynewt

Allow Mynewt to be used in multi-image builds. Primary and secondary
slots of image 1 are hard-coded to FLASH_AREA_IMAGE_2 and FLASH_AREA_IMAGE_3.

Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/boot/mynewt/flash_map_backend/include/flash_map_backend/flash_map_backend.h b/boot/mynewt/flash_map_backend/include/flash_map_backend/flash_map_backend.h
index 593386c..ff628e1 100644
--- a/boot/mynewt/flash_map_backend/include/flash_map_backend/flash_map_backend.h
+++ b/boot/mynewt/flash_map_backend/include/flash_map_backend/flash_map_backend.h
@@ -7,7 +7,10 @@
 #ifndef __FLASH_MAP_BACKEND_H__
 #define __FLASH_MAP_BACKEND_H__
 
-#include <flash_map/flash_map.h>
+#include <sysflash/sysflash.h>
+#include <mcuboot_config/mcuboot_config.h>
+
+#if (MCUBOOT_IMAGE_NUMBER == 1)
 
 #define FLASH_AREA_IMAGE_PRIMARY(x)    (((x) == 0) ?          \
                                          FLASH_AREA_IMAGE_0 : \
@@ -16,6 +19,23 @@
                                          FLASH_AREA_IMAGE_1 : \
                                          FLASH_AREA_IMAGE_1)
 
+#elif (MCUBOOT_IMAGE_NUMBER == 2)
+
+#define FLASH_AREA_IMAGE_PRIMARY(x)    (((x) == 0) ?          \
+                                         FLASH_AREA_IMAGE_0 : \
+                                        ((x) == 1) ?          \
+                                         FLASH_AREA_IMAGE_2 : \
+                                         255)
+#define FLASH_AREA_IMAGE_SECONDARY(x)  (((x) == 0) ?          \
+                                         FLASH_AREA_IMAGE_1 : \
+                                        ((x) == 1) ?          \
+                                         FLASH_AREA_IMAGE_3 : \
+                                         255)
+
+#else
+#error "Image slot and flash area mapping is not defined"
+#endif
+
 int flash_area_id_from_multi_image_slot(int image_index, int slot);
 int flash_area_id_to_multi_image_slot(int image_index, int area_id);
 
diff --git a/boot/mynewt/flash_map_backend/src/flash_map_extended.c b/boot/mynewt/flash_map_backend/src/flash_map_extended.c
index 659da48..ee894dd 100644
--- a/boot/mynewt/flash_map_backend/src/flash_map_extended.c
+++ b/boot/mynewt/flash_map_backend/src/flash_map_extended.c
@@ -22,12 +22,21 @@
 
 int flash_area_id_from_multi_image_slot(int image_index, int slot)
 {
-    (void)image_index;
-    return flash_area_id_from_image_slot(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 255;
 }
 
 int flash_area_id_to_multi_image_slot(int image_index, int area_id)
 {
-    (void)image_index;
-    return flash_area_id_to_image_slot(area_id);
+    if (area_id == FLASH_AREA_IMAGE_PRIMARY(image_index)) {
+        return 0;
+    }
+    if (area_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
+        return 1;
+    }
+    return 255;
 }
diff --git a/boot/mynewt/mcuboot_config/include/mcuboot_config/mcuboot_config.h b/boot/mynewt/mcuboot_config/include/mcuboot_config/mcuboot_config.h
index 1db7fa8..13c31fd 100644
--- a/boot/mynewt/mcuboot_config/include/mcuboot_config/mcuboot_config.h
+++ b/boot/mynewt/mcuboot_config/include/mcuboot_config/mcuboot_config.h
@@ -21,6 +21,11 @@
 
 #include <syscfg/syscfg.h>
 
+#if MYNEWT_VAL(BOOTUTIL_IMAGE_NUMBER)
+#define MCUBOOT_IMAGE_NUMBER MYNEWT_VAL(BOOTUTIL_IMAGE_NUMBER)
+#else
+#define MCUBOOT_IMAGE_NUMBER 1
+#endif
 #if MYNEWT_VAL(BOOT_SERIAL)
 #define MCUBOOT_SERIAL 1
 #endif
diff --git a/boot/mynewt/mcuboot_config/syscfg.yml b/boot/mynewt/mcuboot_config/syscfg.yml
index 7759a95..ab3a47f 100644
--- a/boot/mynewt/mcuboot_config/syscfg.yml
+++ b/boot/mynewt/mcuboot_config/syscfg.yml
@@ -19,6 +19,9 @@
 # Package: boot/mynewt/mcuboot_config
 
 syscfg.defs:
+    BOOTUTIL_IMAGE_NUMBER:
+        description: 'Number of images for multi-image (0 and 1 mean single image).'
+        value: 0
     BOOTUTIL_VALIDATE_SLOT0:
         description: 'Validate image at slot 0 on each boot.'
         value: 0