Replace mcuboot flash_map by zephyr flash_map

The patch introduce usage of zephyr flas_map module instead
of mcuboot zephyr-only implementation. Unused flash_area_to_sectors
API of former flash_map was removed as well.
Size of sector-status-update-map entry is now defined thanks to the
minimum write size supported by the flash driver.

For avoid ambiguity former zephyr-only files flash_map.c
were renamed to flash_map_extended.c (its code now implements
only addition to this what zephyr flash_map implements).

flash_map.h header include is now warped by flash_map_backedn.h headre
because implementations and include pathes are diferent in Zephyr and Mynewt.

Usage of hal_flash_align() were replaced by usage flash_area_align().
This provide consistency between MyNewt and Zephyr implementation as
this API is available in both RTOSes.

flash_map.h was moved to the simulator c-support files as now missing in
the boot/zephyr subdirectories.

f. boot_scratch_fa_device_id was removed as unused.
f. boot_img_fa_device_id was and expanded the only use of it
(on loader.c).

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
diff --git a/boot/boot_serial/pkg.yml b/boot/boot_serial/pkg.yml
index 9fa5acf..dd92cf4 100644
--- a/boot/boot_serial/pkg.yml
+++ b/boot/boot_serial/pkg.yml
@@ -31,7 +31,7 @@
     - "@apache-mynewt-core/encoding/tinycbor"
     - "@apache-mynewt-core/encoding/cborattr"
     - "@apache-mynewt-core/encoding/base64"
-    - "@apache-mynewt-core/sys/flash_map"
+    - "@mcuboot/boot/mynewt/flash_map_backend"
     - "@apache-mynewt-core/util/crc"
 
 pkg.req_apis:
diff --git a/boot/boot_serial/src/boot_serial.c b/boot/boot_serial/src/boot_serial.c
index f4c6087..5e6fff1 100644
--- a/boot/boot_serial/src/boot_serial.c
+++ b/boot/boot_serial/src/boot_serial.c
@@ -49,8 +49,7 @@
 #endif /* __ZEPHYR__ */
 
 #include <cborattr/cborattr.h>
-
-#include <flash_map/flash_map.h>
+#include <flash_map_backend/flash_map_backend.h>
 #include <hal/hal_flash.h>
 #include <os/os.h>
 #include <os/os_malloc.h>
diff --git a/boot/boot_serial/test/src/boot_test.c b/boot/boot_serial/test/src/boot_test.c
index 94efbd6..88a2706 100644
--- a/boot/boot_serial/test/src/boot_test.c
+++ b/boot/boot_serial/test/src/boot_test.c
@@ -29,7 +29,7 @@
 #include "crc/crc16.h"
 #include "testutil/testutil.h"
 #include "hal/hal_flash.h"
-#include "flash_map/flash_map.h"
+#include "flash_map_backend/flash_map_backend.h"
 
 #include "boot_serial_priv.h"
 
diff --git a/boot/boot_serial/test/src/boot_test.h b/boot/boot_serial/test/src/boot_test.h
index b517a04..402d5b1 100644
--- a/boot/boot_serial/test/src/boot_test.h
+++ b/boot/boot_serial/test/src/boot_test.h
@@ -32,7 +32,7 @@
 #include "crc/crc16.h"
 #include "testutil/testutil.h"
 #include "hal/hal_flash.h"
-#include "flash_map/flash_map.h"
+#include "flash_map_backend/flash_map_backend.h"
 
 #include "boot_serial_priv.h"
 
diff --git a/boot/bootutil/pkg.yml b/boot/bootutil/pkg.yml
index e5b2098..9a1ff71 100644
--- a/boot/bootutil/pkg.yml
+++ b/boot/bootutil/pkg.yml
@@ -36,7 +36,7 @@
     - "@apache-mynewt-core/hw/hal"
     - "@apache-mynewt-core/kernel/os"
     - "@apache-mynewt-core/sys/defs"
-    - "@apache-mynewt-core/sys/flash_map"
+    - "@mcuboot/boot/mynewt/flash_map_backend"
 
 pkg.deps.BOOTUTIL_USE_MBED_TLS:
     - "@apache-mynewt-core/crypto/mbedtls"
diff --git a/boot/bootutil/src/bootutil_misc.c b/boot/bootutil/src/bootutil_misc.c
index 0ec8605..0b8b671 100644
--- a/boot/bootutil/src/bootutil_misc.c
+++ b/boot/bootutil/src/bootutil_misc.c
@@ -25,7 +25,9 @@
 #include "sysflash/sysflash.h"
 #include "hal/hal_bsp.h"
 #include "hal/hal_flash.h"
-#include "flash_map/flash_map.h"
+
+#include "flash_map_backend/flash_map_backend.h"
+
 #include "os/os.h"
 #include "bootutil/image.h"
 #include "bootutil/bootutil.h"
@@ -356,7 +358,7 @@
         return BOOT_EBADARGS;
     }
 
-    align = hal_flash_align(fap->fa_device_id);
+    align = flash_area_align(fap);
     assert(align <= BOOT_MAX_ALIGN);
     memset(buf, 0xFF, BOOT_MAX_ALIGN);
     buf[0] = BOOT_FLAG_SET;
@@ -390,7 +392,7 @@
     uint8_t align;
 
     off = boot_swap_size_off(fap);
-    align = hal_flash_align(fap->fa_device_id);
+    align = flash_area_align(fap);
     assert(align <= BOOT_MAX_ALIGN);
     if (align < sizeof swap_size) {
         align = sizeof swap_size;
diff --git a/boot/bootutil/src/bootutil_priv.h b/boot/bootutil/src/bootutil_priv.h
index d2ee6fc..c5c23aa 100644
--- a/boot/bootutil/src/bootutil_priv.h
+++ b/boot/bootutil/src/bootutil_priv.h
@@ -21,7 +21,9 @@
 #define H_BOOTUTIL_PRIV_
 
 #include "sysflash/sysflash.h"
-#include "flash_map/flash_map.h"
+
+#include <flash_map_backend/flash_map_backend.h>
+
 #include "bootutil/image.h"
 #include "mcuboot_config/mcuboot_config.h"
 
@@ -181,18 +183,6 @@
     return &state->imgs[slot].hdr;
 }
 
-static inline uint8_t
-boot_img_fa_device_id(struct boot_loader_state *state, size_t slot)
-{
-    return state->imgs[slot].area->fa_device_id;
-}
-
-static inline uint8_t
-boot_scratch_fa_device_id(struct boot_loader_state *state)
-{
-    return state->scratch_area->fa_device_id;
-}
-
 static inline size_t
 boot_img_num_sectors(struct boot_loader_state *state, size_t slot)
 {
diff --git a/boot/bootutil/src/image_validate.c b/boot/bootutil/src/image_validate.c
index 496ed27..f9dcde3 100644
--- a/boot/bootutil/src/image_validate.c
+++ b/boot/bootutil/src/image_validate.c
@@ -23,7 +23,9 @@
 #include <string.h>
 
 #include "hal/hal_flash.h"
-#include "flash_map/flash_map.h"
+
+#include <flash_map_backend/flash_map_backend.h>
+
 #include "bootutil/image.h"
 #include "bootutil/sha256.h"
 #include "bootutil/sign_key.h"
diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c
index a76d056..a08bda2 100644
--- a/boot/bootutil/src/loader.c
+++ b/boot/bootutil/src/loader.c
@@ -318,8 +318,8 @@
      * on what the minimum write size is for scratch area, active image slot.
      * We need to use the bigger of those 2 values.
      */
-    elem_sz = hal_flash_align(boot_img_fa_device_id(&boot_data, 0));
-    align = hal_flash_align(boot_scratch_fa_device_id(&boot_data));
+    elem_sz = flash_area_align(boot_data.imgs[0].area);
+    align = flash_area_align(boot_data.scratch_area);
     if (align > elem_sz) {
         elem_sz = align;
     }
@@ -547,8 +547,7 @@
     off = boot_status_off(fap) +
           boot_status_internal_off(bs->idx, bs->state,
                                    BOOT_WRITE_SZ(&boot_data));
-
-    align = hal_flash_align(fap->fa_device_id);
+    align = flash_area_align(fap);
     memset(buf, 0xFF, BOOT_MAX_ALIGN);
     buf[0] = bs->state;
 
@@ -1451,7 +1450,7 @@
 #endif
 
     /* Always boot from the primary slot. */
-    rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, 0);
+    rsp->br_flash_dev_id = boot_data.imgs[0].area->fa_device_id;
     rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
     rsp->br_hdr = boot_img_hdr(&boot_data, slot);
 
diff --git a/boot/bootutil/test/src/boot_test.c b/boot/bootutil/test/src/boot_test.c
index 98972f3..d80727e 100644
--- a/boot/bootutil/test/src/boot_test.c
+++ b/boot/bootutil/test/src/boot_test.c
@@ -27,7 +27,8 @@
 #include "sysflash/sysflash.h"
 #include "testutil/testutil.h"
 #include "hal/hal_flash.h"
-#include "flash_map/flash_map.h"
+#include <flash_map_backend/flash_map_backend.h>
+
 #include "bootutil/image.h"
 #include "bootutil/bootutil.h"
 #include "bootutil_priv.h"
diff --git a/boot/bootutil/test/src/boot_test.h b/boot/bootutil/test/src/boot_test.h
index d0c1319..eec85b1 100644
--- a/boot/bootutil/test/src/boot_test.h
+++ b/boot/bootutil/test/src/boot_test.h
@@ -29,7 +29,7 @@
 #include "sysflash/sysflash.h"
 #include "testutil/testutil.h"
 #include "hal/hal_flash.h"
-#include "flash_map/flash_map.h"
+#include "flash_map_backend/flash_map_backend.h"
 #include "bootutil/image.h"
 #include "bootutil/bootutil.h"
 #include "bootutil_priv.h"
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
new file mode 100644
index 0000000..5de2800
--- /dev/null
+++ b/boot/mynewt/flash_map_backend/include/flash_map_backend/flash_map_backend.h
@@ -0,0 +1,12 @@
+/*
+ * Copyright (c) 2018 Nordic Semiconductor ASA
+ * Copyright (c) 2015 Runtime Inc
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+#ifndef __FLASH_MAP_BACKEND_H__
+#define __FLASH_MAP_BACKEND_H__
+
+#include <flash_map/flash_map.h>
+
+#endif /* __FLASH_MAP_BACKEND_H__ */
diff --git a/boot/mynewt/flash_map_backend/pkg.yml b/boot/mynewt/flash_map_backend/pkg.yml
new file mode 100644
index 0000000..5f04e29
--- /dev/null
+++ b/boot/mynewt/flash_map_backend/pkg.yml
@@ -0,0 +1,13 @@
+#
+# Copyright (c) 2018 Nordic Semiconductor ASA
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+
+pkg.name: boot/mynewt/flash_map_backend
+pkg.description: Flash_map API helper reference.
+pkg.author: "Nordic Semiconductor ASA"
+pkg.homepage: "http://mcuboot.com"
+
+pkg.deps:
+    - "@apache-mynewt-core/sys/flash_map"
diff --git a/boot/mynewt/pkg.yml b/boot/mynewt/pkg.yml
index a005522..396df49 100644
--- a/boot/mynewt/pkg.yml
+++ b/boot/mynewt/pkg.yml
@@ -31,6 +31,7 @@
 pkg.deps:
     - "@mcuboot/boot/mynewt/mcuboot_config"
     - "@mcuboot/boot/bootutil"
+    - "@mcuboot/boot/mynewt/flash_map_backend"
     - "@apache-mynewt-core/kernel/os"
     - "@apache-mynewt-core/sys/console/minimal"
 
diff --git a/boot/mynewt/src/main.c b/boot/mynewt/src/main.c
index 8cad67c..1f2bb5e 100755
--- a/boot/mynewt/src/main.c
+++ b/boot/mynewt/src/main.c
@@ -24,7 +24,7 @@
 #include <inttypes.h>
 #include <stdio.h>
 #include "syscfg/syscfg.h"
-#include <flash_map/flash_map.h>
+#include <flash_map_backend/flash_map_backend.h>
 #include <os/os.h>
 #include <bsp/bsp.h>
 #include <hal/hal_bsp.h>
diff --git a/boot/zephyr/CMakeLists.txt b/boot/zephyr/CMakeLists.txt
index a76bcf2..3b8ad92 100644
--- a/boot/zephyr/CMakeLists.txt
+++ b/boot/zephyr/CMakeLists.txt
@@ -77,8 +77,7 @@
 
 # Zephyr port-specific sources.
 target_sources(app PRIVATE main.c)
-target_sources(app PRIVATE flash_map.c)
-target_sources(app PRIVATE hal_flash.c)
+target_sources(app PRIVATE flash_map_extended.c)
 target_sources(app PRIVATE os.c)
 target_sources(app PRIVATE keys.c)
 if(NOT DEFINED CONFIG_FLASH_PAGE_LAYOUT)
diff --git a/boot/zephyr/Kconfig b/boot/zephyr/Kconfig
index 14eba57..bc3c234 100644
--- a/boot/zephyr/Kconfig
+++ b/boot/zephyr/Kconfig
@@ -129,6 +129,8 @@
 	select MCUBOOT_DEVICE_CPU_CORTEX_M0 if CPU_CORTEX_M0
         # Enable flash page layout if available
 	select FLASH_PAGE_LAYOUT if FLASH_HAS_PAGE_LAYOUT
+	# Enable flash_map module as flash I/O back-end
+	select FLASH_MAP
 
 config MCUBOOT_DEVICE_CPU_CORTEX_M0
 	# Hidden selector for Cortex-M0 settings
diff --git a/boot/zephyr/flash_map.c b/boot/zephyr/flash_map.c
deleted file mode 100644
index 899c5ad..0000000
--- a/boot/zephyr/flash_map.c
+++ /dev/null
@@ -1,360 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include <zephyr.h>
-#include <flash.h>
-
-#include "target.h"
-
-#include <flash_map/flash_map.h>
-#include <hal/hal_flash.h>
-#include <sysflash/sysflash.h>
-
-#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
-#include "bootutil/bootutil_log.h"
-
-extern struct device *boot_flash_device;
-
-/*
- * For now, we only support one flash device.
- *
- * Pick a random device ID for it that's unlikely to collide with
- * anything "real".
- */
-#define FLASH_DEVICE_ID 100
-#define FLASH_DEVICE_BASE CONFIG_FLASH_BASE_ADDRESS
-
-#define FLASH_MAP_ENTRY_MAGIC 0xd00dbeef
-
-struct flash_map_entry {
-    const uint32_t magic;
-    const struct flash_area area;
-    unsigned int ref_count;
-};
-
-/*
- * The flash area describes essentially the partition table of the
- * flash.  In this case, it starts with FLASH_AREA_IMAGE_0.
- */
-static struct flash_map_entry part_map[] = {
-    {
-        .magic = FLASH_MAP_ENTRY_MAGIC,
-        .area = {
-            .fa_id = FLASH_AREA_IMAGE_0,
-            .fa_device_id = FLASH_DEVICE_ID,
-            .fa_off = FLASH_AREA_IMAGE_0_OFFSET,
-            .fa_size = FLASH_AREA_IMAGE_0_SIZE,
-        },
-    },
-    {
-        .magic = FLASH_MAP_ENTRY_MAGIC,
-        .area = {
-            .fa_id = FLASH_AREA_IMAGE_1,
-            .fa_device_id = FLASH_DEVICE_ID,
-            .fa_off = FLASH_AREA_IMAGE_1_OFFSET,
-            .fa_size = FLASH_AREA_IMAGE_1_SIZE,
-        },
-    },
-    {
-        .magic = FLASH_MAP_ENTRY_MAGIC,
-        .area = {
-            .fa_id = FLASH_AREA_IMAGE_SCRATCH,
-            .fa_device_id = FLASH_DEVICE_ID,
-            .fa_off = FLASH_AREA_IMAGE_SCRATCH_OFFSET,
-            .fa_size = FLASH_AREA_IMAGE_SCRATCH_SIZE,
-        },
-    }
-};
-
-int flash_device_base(uint8_t fd_id, uintptr_t *ret)
-{
-    if (fd_id != FLASH_DEVICE_ID) {
-        BOOT_LOG_ERR("invalid flash ID %d; expected %d",
-                     fd_id, FLASH_DEVICE_ID);
-        return -EINVAL;
-    }
-    *ret = FLASH_DEVICE_BASE;
-    return 0;
-}
-
-/*
- * `open` a flash area.  The `area` in this case is not the individual
- * sectors, but describes the particular flash area in question.
- */
-int flash_area_open(uint8_t id, const struct flash_area **area)
-{
-    int i;
-
-    BOOT_LOG_DBG("area %d", id);
-
-    for (i = 0; i < ARRAY_SIZE(part_map); i++) {
-        if (id == part_map[i].area.fa_id) {
-            break;
-        }
-    }
-    if (i == ARRAY_SIZE(part_map)) {
-        return -1;
-    }
-
-    *area = &part_map[i].area;
-    part_map[i].ref_count++;
-    return 0;
-}
-
-/*
- * Nothing to do on close.
- */
-void flash_area_close(const struct flash_area *area)
-{
-    struct flash_map_entry *entry;
-
-    if (!area) {
-        return;
-    }
-
-    entry = CONTAINER_OF(area, struct flash_map_entry, area);
-    if (entry->magic != FLASH_MAP_ENTRY_MAGIC) {
-        BOOT_LOG_ERR("invalid area %p (id %u)", area, area->fa_id);
-        return;
-    }
-    if (entry->ref_count == 0) {
-        BOOT_LOG_ERR("area %u use count underflow", area->fa_id);
-        return;
-    }
-    entry->ref_count--;
-}
-
-void zephyr_flash_area_warn_on_open(void)
-{
-    int i;
-
-    for (i = 0; i < ARRAY_SIZE(part_map); i++) {
-        struct flash_map_entry *entry = &part_map[i];
-        if (entry->ref_count) {
-            BOOT_LOG_WRN("area %u has %u users",
-                         entry->area.fa_id, entry->ref_count);
-        }
-    }
-}
-
-int flash_area_read(const struct flash_area *area, uint32_t off, void *dst,
-            uint32_t len)
-{
-    BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
-    return flash_read(boot_flash_device, area->fa_off + off, dst, len);
-}
-
-int flash_area_write(const struct flash_area *area, uint32_t off, const void *src,
-             uint32_t len)
-{
-    int rc = 0;
-
-    BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
-    flash_write_protection_set(boot_flash_device, false);
-    rc = flash_write(boot_flash_device, area->fa_off + off, src, len);
-    flash_write_protection_set(boot_flash_device, true);
-    return rc;
-}
-
-int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len)
-{
-    int rc;
-
-    BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
-    flash_write_protection_set(boot_flash_device, false);
-    rc = flash_erase(boot_flash_device, area->fa_off + off, len);
-    flash_write_protection_set(boot_flash_device, true);
-    return rc;
-}
-
-uint8_t flash_area_align(const struct flash_area *area)
-{
-    return hal_flash_align(area->fa_id);
-}
-
-/*
- * This depends on the mappings defined in sysflash.h, and assumes
- * that slot 0, slot 1, and the scratch area area contiguous.
- */
-int flash_area_id_from_image_slot(int slot)
-{
-    return slot + FLASH_AREA_IMAGE_0;
-}
-
-/*
- * This is used by the legacy file as well; don't mark it static until
- * that file is removed.
- */
-int flash_area_get_bounds(int idx, uint32_t *off, uint32_t *len)
-{
-    /*
-     * This simple layout has uniform slots, so just fill in the
-     * right one.
-     */
-    if (idx < FLASH_AREA_IMAGE_0 || idx > FLASH_AREA_IMAGE_SCRATCH) {
-        return -1;
-    }
-
-    switch (idx) {
-    case FLASH_AREA_IMAGE_0:
-        *off = FLASH_AREA_IMAGE_0_OFFSET;
-        *len = FLASH_AREA_IMAGE_0_SIZE;
-        break;
-    case FLASH_AREA_IMAGE_1:
-        *off = FLASH_AREA_IMAGE_1_OFFSET;
-        *len = FLASH_AREA_IMAGE_1_SIZE;
-        break;
-    case FLASH_AREA_IMAGE_SCRATCH:
-        *off = FLASH_AREA_IMAGE_SCRATCH_OFFSET;
-        *len = FLASH_AREA_IMAGE_SCRATCH_SIZE;
-        break;
-    default:
-        BOOT_LOG_ERR("unknown flash area %d", idx);
-        return -1;
-    }
-
-    BOOT_LOG_DBG("area %d: offset=0x%x, length=0x%x", idx, *off, *len);
-    return 0;
-}
-
-/*
- * The legacy fallbacks are used instead if the flash driver doesn't
- * provide page layout support.
- */
-#if defined(CONFIG_FLASH_PAGE_LAYOUT)
-struct layout_data {
-    uint32_t area_idx;
-    uint32_t area_off;
-    uint32_t area_len;
-    void *ret;        /* struct flash_area* or struct flash_sector* */
-    uint32_t ret_idx;
-    uint32_t ret_len;
-    int status;
-};
-
-/*
- * Generic page layout discovery routine. This is kept separate to
- * support both the deprecated flash_area_to_sectors() and the current
- * flash_area_get_sectors(). A lot of this can be inlined once
- * flash_area_to_sectors() is removed.
- */
-static int flash_area_layout(int idx, int *cnt, void *ret,
-                             flash_page_cb cb, struct layout_data *cb_data)
-{
-    cb_data->area_idx = idx;
-    if (flash_area_get_bounds(idx, &cb_data->area_off, &cb_data->area_len)) {
-        return -1;
-    }
-    cb_data->ret = ret;
-    cb_data->ret_idx = 0;
-    cb_data->ret_len = *cnt;
-    cb_data->status = 0;
-
-    flash_page_foreach(boot_flash_device, cb, cb_data);
-
-    if (cb_data->status == 0) {
-        *cnt = cb_data->ret_idx;
-    }
-
-    return cb_data->status;
-}
-
-/*
- * Check if a flash_page_foreach() callback should exit early, due to
- * one of the following conditions:
- *
- * - The flash page described by "info" is before the area of interest
- *   described in "data"
- * - The flash page is after the end of the area
- * - There are too many flash pages on the device to fit in the array
- *   held in data->ret. In this case, data->status is set to -ENOMEM.
- *
- * The value to return to flash_page_foreach() is stored in
- * "bail_value" if the callback should exit early.
- */
-static bool should_bail(const struct flash_pages_info *info,
-                        struct layout_data *data,
-                        bool *bail_value)
-{
-    if (info->start_offset < data->area_off) {
-        *bail_value = true;
-        return true;
-    } else if (info->start_offset >= data->area_off + data->area_len) {
-        *bail_value = false;
-        return true;
-    } else if (data->ret_idx >= data->ret_len) {
-        data->status = -ENOMEM;
-        *bail_value = false;
-        return true;
-    }
-
-    return false;
-}
-
-static bool to_sectors_cb(const struct flash_pages_info *info, void *datav)
-{
-    struct layout_data *data = datav;
-    struct flash_area *ret = data->ret;
-    bool bail;
-
-    if (should_bail(info, data, &bail)) {
-        return bail;
-    }
-
-    ret[data->ret_idx].fa_id = data->area_idx;
-    ret[data->ret_idx].fa_device_id = 0;
-    ret[data->ret_idx].pad16 = 0;
-    ret[data->ret_idx].fa_off = info->start_offset;
-    ret[data->ret_idx].fa_size = info->size;
-    data->ret_idx++;
-
-    return true;
-}
-
-int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
-{
-    struct layout_data data;
-
-    return flash_area_layout(idx, cnt, ret, to_sectors_cb, &data);
-}
-
-static bool get_sectors_cb(const struct flash_pages_info *info, void *datav)
-{
-    struct layout_data *data = datav;
-    struct flash_sector *ret = data->ret;
-    bool bail;
-
-    if (should_bail(info, data, &bail)) {
-        return bail;
-    }
-
-    ret[data->ret_idx].fs_off = info->start_offset - data->area_off;
-    ret[data->ret_idx].fs_size = info->size;
-    data->ret_idx++;
-
-    return true;
-}
-
-int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
-{
-    struct layout_data data;
-
-    return flash_area_layout(idx, cnt, ret, get_sectors_cb, &data);
-}
-#endif /* defined(CONFIG_FLASH_PAGE_LAYOUT) */
diff --git a/boot/zephyr/flash_map_extended.c b/boot/zephyr/flash_map_extended.c
new file mode 100644
index 0000000..ee2320c
--- /dev/null
+++ b/boot/zephyr/flash_map_extended.c
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2018 Nordic Semiconductor ASA
+ * Copyright (c) 2015 Runtime Inc
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <zephyr.h>
+#include <flash.h>
+
+#include "target.h"
+
+#include <flash_map_backend/flash_map_backend.h>
+#include <hal/hal_flash.h>
+#include <sysflash/sysflash.h>
+
+#include "bootutil/bootutil_log.h"
+
+/*
+ * For now, we only support one flash device.
+ *
+ * Pick the SoC Flash driver ID.
+ */
+#define FLASH_DEVICE_ID SOC_FLASH_0_ID
+#define FLASH_DEVICE_BASE CONFIG_FLASH_BASE_ADDRESS
+
+int flash_device_base(uint8_t fd_id, uintptr_t *ret)
+{
+    if (fd_id != FLASH_DEVICE_ID) {
+        BOOT_LOG_ERR("invalid flash ID %d; expected %d",
+                     fd_id, FLASH_DEVICE_ID);
+        return -EINVAL;
+    }
+    *ret = FLASH_DEVICE_BASE;
+    return 0;
+}
+
+/*
+ * This depends on the mappings defined in sysflash.h, and assumes
+ * that slot 0, slot 1, and the scratch areas are contiguous.
+ */
+int flash_area_id_from_image_slot(int slot)
+{
+    return slot + FLASH_AREA_IMAGE_0;
+}
diff --git a/boot/zephyr/flash_map_legacy.c b/boot/zephyr/flash_map_legacy.c
index 2bfa182..e7daa41 100644
--- a/boot/zephyr/flash_map_legacy.c
+++ b/boot/zephyr/flash_map_legacy.c
@@ -40,7 +40,7 @@
 #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
 #include "bootutil/bootutil_log.h"
 
-#include <flash_map/flash_map.h>
+#include <flash_map_backend/flash_map_backend.h>
 #include <inttypes.h>
 #include <target.h>
 
@@ -50,49 +50,6 @@
 #define FLASH_AREA_IMAGE_SECTOR_SIZE FLASH_AREA_IMAGE_SCRATCH_SIZE
 #endif
 
-extern int flash_area_get_bounds(int idx, uint32_t *off, uint32_t *len);
-
-int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
-{
-    uint32_t off;
-    uint32_t len;
-    uint32_t max_cnt = *cnt;
-    uint32_t rem_len;
-
-    if (flash_area_get_bounds(idx, &off, &len)) {
-        return -1;
-    }
-
-    if (*cnt < 1) {
-        return -1;
-    }
-
-    rem_len = len;
-    *cnt = 0;
-    while (rem_len > 0 && *cnt < max_cnt) {
-        if (rem_len < FLASH_AREA_IMAGE_SECTOR_SIZE) {
-            BOOT_LOG_ERR("area %d size 0x%x not divisible by sector size 0x%x",
-                     idx, len, FLASH_AREA_IMAGE_SECTOR_SIZE);
-            return -1;
-        }
-
-        ret[*cnt].fa_id = idx;
-        ret[*cnt].fa_device_id = 0;
-        ret[*cnt].pad16 = 0;
-        ret[*cnt].fa_off = off + (FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt));
-        ret[*cnt].fa_size = FLASH_AREA_IMAGE_SECTOR_SIZE;
-        *cnt = *cnt + 1;
-        rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE;
-    }
-
-    if (*cnt >= max_cnt) {
-        BOOT_LOG_ERR("flash area %d sector count overflow", idx);
-        return -1;
-    }
-
-    return 0;
-}
-
 /*
  * Lookup the sector map for a given flash area.  This should fill in
  * `ret` with all of the sectors in the area.  `*cnt` will be set to
@@ -101,26 +58,29 @@
  */
 int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
 {
-    uint32_t off;
-    uint32_t len;
+    const struct flash_area *fa;
     uint32_t max_cnt = *cnt;
     uint32_t rem_len;
+    int rc = -1;
 
-    if (flash_area_get_bounds(idx, &off, &len)) {
-        return -1;
+    if (flash_area_open(idx, &fa))  {
+        goto out;
     }
 
+    BOOT_LOG_DBG("area %d: offset=0x%x, length=0x%x", idx, fa->fa_off,
+                 fa->fa_size);
+
     if (*cnt < 1) {
-        return -1;
+        goto fa_close_out;
     }
 
-    rem_len = len;
+    rem_len = fa->fa_size;
     *cnt = 0;
     while (rem_len > 0 && *cnt < max_cnt) {
         if (rem_len < FLASH_AREA_IMAGE_SECTOR_SIZE) {
             BOOT_LOG_ERR("area %d size 0x%x not divisible by sector size 0x%x",
-                         idx, len, FLASH_AREA_IMAGE_SECTOR_SIZE);
-            return -1;
+                         idx, fa->fa_size, FLASH_AREA_IMAGE_SECTOR_SIZE);
+            goto fa_close_out;
         }
 
         ret[*cnt].fs_off = FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt);
@@ -131,8 +91,13 @@
 
     if (*cnt >= max_cnt) {
         BOOT_LOG_ERR("flash area %d sector count overflow", idx);
-        return -1;
+        goto fa_close_out;
     }
 
-    return 0;
+    rc = 0;
+
+fa_close_out:
+    flash_area_close(fa);
+out:
+    return rc;
 }
diff --git a/boot/zephyr/hal_flash.c b/boot/zephyr/hal_flash.c
deleted file mode 100644
index 17b6124..0000000
--- a/boot/zephyr/hal_flash.c
+++ /dev/null
@@ -1,29 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include <zephyr.h>
-
-#include "target.h"
-
-#include "hal/hal_flash.h"
-
-uint8_t hal_flash_align(uint8_t flash_id)
-{
-    return FLASH_ALIGN;
-}
diff --git a/boot/zephyr/include/flash_map/flash_map.h b/boot/zephyr/include/flash_map/flash_map.h
deleted file mode 100644
index 506f266..0000000
--- a/boot/zephyr/include/flash_map/flash_map.h
+++ /dev/null
@@ -1,148 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#ifndef H_UTIL_FLASH_MAP_
-#define H_UTIL_FLASH_MAP_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/**
- *
- * Provides abstraction of flash regions for type of use.
- * I.e. dude where's my image?
- *
- * System will contain a map which contains flash areas. Every
- * region will contain flash identifier, offset within flash and length.
- *
- * 1. This system map could be in a file within filesystem (Initializer
- * must know/figure out where the filesystem is at).
- * 2. Map could be at fixed location for project (compiled to code)
- * 3. Map could be at specific place in flash (put in place at mfg time).
- *
- * Note that the map you use must be valid for BSP it's for,
- * match the linker scripts when platform executes from flash,
- * and match the target offset specified in download script.
- */
-#include <inttypes.h>
-
-/**
- * @brief Structure describing an area on a flash device.
- *
- * Multiple flash devices may be available in the system, each of
- * which may have its own areas. For this reason, flash areas track
- * which flash device they are part of.
- */
-struct flash_area {
-    /**
-     * This flash area's ID; unique in the system.
-     */
-    uint8_t fa_id;
-
-    /**
-     * ID of the flash device this area is a part of.
-     */
-    uint8_t fa_device_id;
-
-    uint16_t pad16;
-
-    /**
-     * This area's offset, relative to the beginning of its flash
-     * device's storage.
-     */
-    uint32_t fa_off;
-
-    /**
-     * This area's size, in bytes.
-     */
-    uint32_t fa_size;
-};
-
-/**
- * @brief Structure describing a sector within a flash area.
- *
- * Each sector has an offset relative to the start of its flash area
- * (NOT relative to the start of its flash device), and a size. A
- * flash area may contain sectors with different sizes.
- */
-struct flash_sector {
-    /**
-     * Offset of this sector, from the start of its flash area (not device).
-     */
-    uint32_t fs_off;
-
-    /**
-     * Size of this sector, in bytes.
-     */
-    uint32_t fs_size;
-};
-
-/*
- * Retrieve a memory-mapped flash device's base address.
- *
- * On success, the address will be stored in the value pointed to by
- * ret.
- *
- * Returns 0 on success, or an error code on failure.
- */
-int flash_device_base(uint8_t fd_id, uintptr_t *ret);
-
-/*
- * Start using flash area.
- */
-int flash_area_open(uint8_t id, const struct flash_area **);
-
-void flash_area_close(const struct flash_area *);
-
-/*
- * Read/write/erase. Offset is relative from beginning of flash area.
- */
-int flash_area_read(const struct flash_area *, uint32_t off, void *dst,
-  uint32_t len);
-int flash_area_write(const struct flash_area *, uint32_t off, const void *src,
-  uint32_t len);
-int flash_area_erase(const struct flash_area *, uint32_t off, uint32_t len);
-
-/*
- * Alignment restriction for flash writes.
- */
-uint8_t flash_area_align(const struct flash_area *);
-
-/*
- * Given flash area ID, return info about sectors within the area.
- */
-int flash_area_get_sectors(int fa_id, uint32_t *count,
-  struct flash_sector *sectors);
-
-/*
- * Similar to flash_area_get_sectors(), but return the values in an
- * array of struct flash_area instead.
- */
-__attribute__((deprecated))
-int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret);
-
-int flash_area_id_from_image_slot(int slot);
-int flash_area_id_to_image_slot(int area_id);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* H_UTIL_FLASH_MAP_ */
diff --git a/boot/zephyr/include/flash_map_backend/flash_map_backend.h b/boot/zephyr/include/flash_map_backend/flash_map_backend.h
new file mode 100644
index 0000000..18009c5
--- /dev/null
+++ b/boot/zephyr/include/flash_map_backend/flash_map_backend.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2018 Nordic Semiconductor ASA
+ * Copyright (c) 2015 Runtime Inc
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#ifndef __FLASH_MAP_BACKEND_H__
+#define __FLASH_MAP_BACKEND_H__
+
+#include <flash_map.h> // the zephyr falsh_map
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ *
+ * Provides abstraction of flash regions for type of use.
+ * I.e. dude where's my image?
+ *
+ * System will contain a map which contains flash areas. Every
+ * region will contain flash identifier, offset within flash and length.
+ *
+ * 1. This system map could be in a file within filesystem (Initializer
+ * must know/figure out where the filesystem is at).
+ * 2. Map could be at fixed location for project (compiled to code)
+ * 3. Map could be at specific place in flash (put in place at mfg time).
+ *
+ * Note that the map you use must be valid for BSP it's for,
+ * match the linker scripts when platform executes from flash,
+ * and match the target offset specified in download script.
+ */
+#include <inttypes.h>
+
+
+/*
+ * Retrieve a memory-mapped flash device's base address.
+ *
+ * On success, the address will be stored in the value pointed to by
+ * ret.
+ *
+ * Returns 0 on success, or an error code on failure.
+ */
+int flash_device_base(uint8_t fd_id, uintptr_t *ret);
+
+int flash_area_id_from_image_slot(int slot);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __FLASH_MAP_BACKEND_H__ */
diff --git a/boot/zephyr/main.c b/boot/zephyr/main.c
index 6dda3bd..94963b1 100644
--- a/boot/zephyr/main.c
+++ b/boot/zephyr/main.c
@@ -27,7 +27,7 @@
 #include "bootutil/bootutil_log.h"
 #include "bootutil/image.h"
 #include "bootutil/bootutil.h"
-#include "flash_map/flash_map.h"
+#include "flash_map_backend/flash_map_backend.h"
 
 #ifdef CONFIG_MCUBOOT_SERIAL
 #include <boot_serial/boot_serial.h>
@@ -37,8 +37,6 @@
 
 void os_heap_init(void);
 
-extern void zephyr_flash_area_warn_on_open(void);
-
 #if defined(CONFIG_ARM)
 struct arm_vector_table {
     uint32_t msp;
@@ -138,7 +136,7 @@
 
     BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
                  rsp.br_image_off);
-    zephyr_flash_area_warn_on_open();
+
     BOOT_LOG_INF("Jumping to the first image slot");
     do_boot(&rsp);