Use flash_device_base() when booting.

Use flash_device_base() in the boot code to compute a real address,
given the offset returned by boot_go().

Provide an implementation on mynewt that preserves existing
behavior. If mynewt needs to support devices with nonzero flash base
addresses, this can be migrated to the core OS.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
diff --git a/boot/bootutil/test/src/boot_test_utils.c b/boot/bootutil/test/src/boot_test_utils.c
index 422220b..bf66d24 100644
--- a/boot/bootutil/test/src/boot_test_utils.c
+++ b/boot/bootutil/test/src/boot_test_utils.c
@@ -464,6 +464,7 @@
     const struct image_header *slot0hdr;
     const struct image_header *slot1hdr;
     struct boot_rsp rsp;
+    uintptr_t flash_base;
     int orig_slot_0;
     int orig_slot_1;
     int num_swaps;
@@ -503,9 +504,13 @@
             orig_slot_1 = 0;
         }
 
+        rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
+        TEST_ASSERT_FATAL(rc == 0);
+
         TEST_ASSERT(memcmp(rsp.br_hdr, slot0hdr, sizeof *slot0hdr) == 0);
         TEST_ASSERT(rsp.br_flash_dev_id == boot_test_img_addrs[0].flash_id);
-        TEST_ASSERT(rsp.br_image_off == boot_test_img_addrs[0].address);
+        TEST_ASSERT(flash_base + rsp.br_image_off ==
+                    boot_test_img_addrs[0].address);
 
         boot_test_util_verify_flash(slot0hdr, orig_slot_0,
                                     slot1hdr, orig_slot_1);
diff --git a/boot/mynewt/src/main.c b/boot/mynewt/src/main.c
index 2af6680..a0b888a 100755
--- a/boot/mynewt/src/main.c
+++ b/boot/mynewt/src/main.c
@@ -43,10 +43,23 @@
 #define BOOT_SER_CONS_INPUT         128
 #endif
 
+/*
+ * Temporary flash_device_base() implementation.
+ *
+ * TODO: remove this when mynewt needs to support flash_device_base()
+ * for devices with nonzero base addresses.
+ */
+int flash_device_base(uint8_t fd_id, uintptr_t *ret)
+{
+    *ret = 0;
+    return 0;
+}
+
 int
 main(void)
 {
     struct boot_rsp rsp;
+    uintptr_t flash_base;
     int rc;
 
 #ifdef MCUBOOT_SERIAL
@@ -70,7 +83,11 @@
     rc = boot_go(&rsp);
     assert(rc == 0);
 
-    hal_system_start((void *)(rsp.br_image_off + rsp.br_hdr->ih_hdr_size));
+    rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
+    assert(rc == 0);
+
+    hal_system_start((void *)(flash_base + rsp.br_image_off +
+                              rsp.br_hdr->ih_hdr_size));
 
     return 0;
 }
diff --git a/boot/zephyr/main.c b/boot/zephyr/main.c
index c6f4376..054c466 100644
--- a/boot/zephyr/main.c
+++ b/boot/zephyr/main.c
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <assert.h>
 #include <zephyr.h>
 #include <flash.h>
 #include <asm_inline.h>
@@ -25,6 +26,7 @@
 #include "bootutil/bootutil_log.h"
 #include "bootutil/image.h"
 #include "bootutil/bootutil.h"
+#include "flash_map/flash_map.h"
 
 struct device *boot_flash_device;
 
@@ -39,13 +41,19 @@
 static void do_boot(struct boot_rsp *rsp)
 {
     struct arm_vector_table *vt;
+    uintptr_t flash_base;
+    int rc;
 
     /* The beginning of the image is the ARM vector table, containing
      * the initial stack pointer address and the reset vector
      * consecutively. Manually set the stack pointer and jump into the
      * reset vector
      */
-    vt = (struct arm_vector_table *)(rsp->br_image_off +
+    rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
+    assert(rc == 0);
+
+    vt = (struct arm_vector_table *)(flash_base +
+                                     rsp->br_image_off +
                                      rsp->br_hdr->ih_hdr_size);
     irq_lock();
     sys_clock_disable();
@@ -59,9 +67,15 @@
  */
 static void do_boot(struct boot_rsp *rsp)
 {
+    uintptr_t flash_base;
     void *start;
+    int rc;
 
-    start = (void *)(rsp->br_image_off + rsp->br_hdr->ih_hdr_size);
+    rc = flash_device_base(rsp->br_flash_dev_id, &flash_base);
+    assert(rc == 0);
+
+    start = (void *)(flash_base + rsp->br_image_off +
+                     rsp->br_hdr->ih_hdr_size);
 
     /* Lock interrupts and dive into the entry point */
     irq_lock();