Revert "Merge pull request #49 from mbolivar/flash-map-updates"

This reverts commit a52d7a21f01bd2859be9e62e20cbe029dc5e846c, reversing
changes made to d21abaaa35c31d88f9b4a18f82bce848adffb569.

Premature merge.  Will bring this back in after #41 merges.
diff --git a/apps/boot/src/boot.c b/apps/boot/src/boot.c
index 0a26edb..fbc4b7d 100755
--- a/apps/boot/src/boot.c
+++ b/apps/boot/src/boot.c
@@ -47,7 +47,6 @@
 main(void)
 {
     struct boot_rsp rsp;
-    uintptr_t flash_base;
     int rc;
 
 #if MYNEWT_VAL(BOOT_SERIAL)
@@ -71,11 +70,7 @@
     rc = boot_go(&rsp);
     assert(rc == 0);
 
-    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));
+    hal_system_start((void *)(rsp.br_image_addr + rsp.br_hdr->ih_hdr_size));
 
     return 0;
 }
diff --git a/boot/bootutil/include/bootutil/bootutil.h b/boot/bootutil/include/bootutil/bootutil.h
index c97fc4c..0947f50 100644
--- a/boot/bootutil/include/bootutil/bootutil.h
+++ b/boot/bootutil/include/bootutil/bootutil.h
@@ -51,10 +51,10 @@
 
     /**
      * The flash offset of the image to execute.  Indicates the position of
-     * the image header within its flash device.
+     * the image header.
      */
-    uint8_t br_flash_dev_id;
-    uint32_t br_image_off;
+    uint8_t br_flash_id;
+    uint32_t br_image_addr;
 };
 
 /* you must have pre-allocated all the entries within this structure */
diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c
index 3d84a05..e0867d6 100644
--- a/boot/bootutil/src/loader.c
+++ b/boot/bootutil/src/loader.c
@@ -39,7 +39,6 @@
 #include "bootutil/bootutil_log.h"
 
 #define BOOT_MAX_IMG_SECTORS        120
-#define BOOT_MAX_SCRATCH_SECTORS    35
 
 /** Number of image slots in flash; currently limited to two. */
 #define BOOT_NUM_SLOTS              2
@@ -47,14 +46,11 @@
 static struct {
     struct {
         struct image_header hdr;
-        const struct flash_area *area;
-        struct flash_sector *sectors;
-        uint32_t num_sectors;
+        struct flash_area *sectors;
+        int num_sectors;
     } imgs[BOOT_NUM_SLOTS];
 
-    const struct flash_area *scratch_area;
-    struct flash_sector *scratch_sectors;
-    uint32_t scratch_num_sectors;
+    struct flash_area scratch_sector;
 
     uint8_t write_sz;
 } boot_data;
@@ -236,7 +232,7 @@
             return boot_swap_trans_table[i][0];
         }
     }
-
+    
     /* XXX: Temporary assert. */
     assert(0);
 
@@ -304,8 +300,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_data.imgs[0].area->fa_device_id);
-    align = hal_flash_align(boot_data.scratch_area->fa_device_id);
+    elem_sz = hal_flash_align(boot_data.imgs[0].sectors[0].fa_device_id);
+    align = hal_flash_align(boot_data.scratch_sector.fa_device_id);
     if (align > elem_sz) {
         elem_sz = align;
     }
@@ -316,8 +312,8 @@
 static int
 boot_slots_compatible(void)
 {
-    const struct flash_sector *sector0;
-    const struct flash_sector *sector1;
+    const struct flash_area *sector0;
+    const struct flash_area *sector1;
     int i;
 
     /* Ensure both image slots have identical sector layouts. */
@@ -327,7 +323,7 @@
     for (i = 0; i < boot_data.imgs[0].num_sectors; i++) {
         sector0 = boot_data.imgs[0].sectors + i;
         sector1 = boot_data.imgs[1].sectors + i;
-        if (sector0->fs_size != sector1->fs_size) {
+        if (sector0->fa_size != sector1->fa_size) {
             return 0;
         }
     }
@@ -344,34 +340,32 @@
 static int
 boot_read_sectors(void)
 {
-    uint32_t num_sectors_slot0;
-    uint32_t num_sectors_slot1;
-    uint32_t num_sectors_scratch;
+    const struct flash_area *scratch;
+    int num_sectors_slot0;
+    int num_sectors_slot1;
     int rc;
 
     num_sectors_slot0 = BOOT_MAX_IMG_SECTORS;
-    rc = flash_area_get_sectors(FLASH_AREA_IMAGE_0, &num_sectors_slot0,
-                                boot_data.imgs[0].sectors);
+    rc = flash_area_to_sectors(FLASH_AREA_IMAGE_0, &num_sectors_slot0,
+                               boot_data.imgs[0].sectors);
     if (rc != 0) {
         return BOOT_EFLASH;
     }
     boot_data.imgs[0].num_sectors = num_sectors_slot0;
 
     num_sectors_slot1 = BOOT_MAX_IMG_SECTORS;
-    rc = flash_area_get_sectors(FLASH_AREA_IMAGE_1, &num_sectors_slot1,
-                                boot_data.imgs[1].sectors);
+    rc = flash_area_to_sectors(FLASH_AREA_IMAGE_1, &num_sectors_slot1,
+                               boot_data.imgs[1].sectors);
     if (rc != 0) {
         return BOOT_EFLASH;
     }
     boot_data.imgs[1].num_sectors = num_sectors_slot1;
 
-    num_sectors_scratch = BOOT_MAX_SCRATCH_SECTORS;
-    rc = flash_area_get_sectors(FLASH_AREA_IMAGE_SCRATCH, &num_sectors_scratch,
-                                boot_data.scratch_sectors);
+    rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &scratch);
     if (rc != 0) {
         return BOOT_EFLASH;
     }
-    boot_data.scratch_num_sectors = num_sectors_scratch;
+    boot_data.scratch_sector = *scratch;
 
     boot_data.write_sz = boot_write_sz();
 
@@ -577,7 +571,7 @@
 {
     const struct flash_area *fap;
     int rc;
-
+    
     if (boot_data.imgs[slot].hdr.ih_magic == 0xffffffff ||
         boot_data.imgs[slot].hdr.ih_flags & IMAGE_F_NON_BOOTABLE) {
 
@@ -661,8 +655,8 @@
     sz = 0;
 
     for (i = last_sector_idx; i >= 0; i--) {
-        new_sz = sz + boot_data.imgs[0].sectors[i].fs_size;
-        if (new_sz > boot_data.scratch_area->fa_size) {
+        new_sz = sz + boot_data.imgs[0].sectors[i].fa_size;
+        if (new_sz > boot_data.scratch_sector.fa_size) {
             break;
         }
         sz = new_sz;
@@ -801,12 +795,11 @@
 {
     uint32_t copy_sz;
     uint32_t img_off;
-    uint32_t max_off;
     int rc;
 
-    /* Sector offsets are from start of image area. */
-    img_off = boot_data.imgs[0].sectors[idx].fs_off;
-    max_off = boot_data.imgs[0].area->fa_size;
+    /* Calculate offset from start of image area. */
+    img_off = boot_data.imgs[0].sectors[idx].fa_off -
+              boot_data.imgs[0].sectors[0].fa_off;
 
     if (bs->state == 0) {
         rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
@@ -825,14 +818,13 @@
         assert(rc == 0);
 
         copy_sz = sz;
-        if (img_off >= max_off - sz) {
-            /*
-             * This is the end of the area.  Don't copy the image state into
+        if (boot_data.imgs[0].sectors[idx].fa_off + sz >=
+            boot_data.imgs[1].sectors[0].fa_off) {
+
+            /* This is the end of the area.  Don't copy the image state into
              * slot 1.
              */
-            BOOT_LOG_WRN("long write: idx=%d (off=0x%x), size=%u, max_off=%u",
-                         idx, img_off, sz, max_off);
-            copy_sz = max_off - img_off - boot_trailer_sz(boot_data.write_sz);
+            copy_sz -= boot_trailer_sz(boot_data.write_sz);
         }
 
         rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
@@ -1063,40 +1055,27 @@
     int swap_type;
     int slot;
     int rc;
-    int fa_id;
 
     /* The array of slot sectors are defined here (as opposed to file scope) so
      * that they don't get allocated for non-boot-loader apps.  This is
      * necessary because the gcc option "-fdata-sections" doesn't seem to have
      * any effect in older gcc versions (e.g., 4.8.4).
      */
-    static struct flash_sector slot0_sectors[BOOT_MAX_IMG_SECTORS];
-    static struct flash_sector slot1_sectors[BOOT_MAX_IMG_SECTORS];
-    static struct flash_sector scratch_sectors[BOOT_MAX_SCRATCH_SECTORS];
-
+    static struct flash_area slot0_sectors[BOOT_MAX_IMG_SECTORS];
+    static struct flash_area slot1_sectors[BOOT_MAX_IMG_SECTORS];
     boot_data.imgs[0].sectors = slot0_sectors;
     boot_data.imgs[1].sectors = slot1_sectors;
-    boot_data.scratch_sectors = scratch_sectors;
-
-    /* Open boot_data's flash areas for use in this file. */
-    for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
-        fa_id = flash_area_id_from_image_slot(slot);
-        rc = flash_area_open(fa_id, &boot_data.imgs[slot].area);
-        assert(rc == 0);
-    }
-    rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &boot_data.scratch_area);
-    assert(rc == 0);
 
     /* Determine the sector layout of the image slots and scratch area. */
     rc = boot_read_sectors();
     if (rc != 0) {
-        goto out;
+        return rc;
     }
 
     /* Attempt to read an image header from each slot. */
     rc = boot_read_image_headers();
     if (rc != 0) {
-        goto out;
+        return rc;
     }
 
     /* If the image slots aren't compatible, no swap is possible.  Just boot
@@ -1105,7 +1084,7 @@
     if (boot_slots_compatible()) {
         rc = boot_swap_if_needed(&swap_type);
         if (rc != 0) {
-            goto out;
+            return rc;
         }
     } else {
         swap_type = BOOT_SWAP_TYPE_NONE;
@@ -1116,8 +1095,7 @@
 #ifdef BOOTUTIL_VALIDATE_SLOT0
         rc = boot_validate_slot(0);
         if (rc != 0) {
-            rc = BOOT_EBADIMAGE;
-            goto out;
+            return BOOT_EBADIMAGE;
         }
 #endif
         slot = 0;
@@ -1150,104 +1128,82 @@
     }
 
     /* Always boot from the primary slot. */
-    rsp->br_flash_dev_id = boot_data.imgs[0].area->fa_device_id;
-    rsp->br_image_off = boot_data.imgs[0].area->fa_off;
+    rsp->br_flash_id = boot_data.imgs[0].sectors[0].fa_device_id;
+    rsp->br_image_addr = boot_data.imgs[0].sectors[0].fa_off;
     rsp->br_hdr = &boot_data.imgs[slot].hdr;
 
- out:
-    /*
-     * We're done using the flash areas now; the boot response
-     * contains the information the caller needs.
-     */
-    for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
-        flash_area_close(boot_data.imgs[slot].area);
-    }
-    flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &boot_data.scratch_area);
-
     return 0;
 }
 
 int
 split_go(int loader_slot, int split_slot, void **entry)
 {
-    struct flash_sector *sectors;
+    const struct flash_area *loader_fap;
+    const struct flash_area *app_fap;
+    struct flash_area *sectors;
     uintptr_t entry_val;
     int loader_flash_id;
-    int split_flash_id;
-    uint32_t loader_num_sectors;
-    uint32_t split_num_sectors;
+    int app_flash_id;
     int rc;
 
+    app_fap = NULL;
+    loader_fap = NULL;
+
     sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
     if (sectors == NULL) {
         rc = SPLIT_GO_ERR;
         goto done;
     }
-    boot_data.imgs[loader_slot].sectors = sectors + 0;
-    boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
+    boot_data.imgs[0].sectors = sectors + 0;
+    boot_data.imgs[1].sectors = sectors + BOOT_MAX_IMG_SECTORS;
 
-    loader_flash_id = flash_area_id_from_image_slot(loader_slot);
-    rc = flash_area_open(loader_flash_id, &boot_data.imgs[loader_slot].area);
-    if (rc != 0) {
-        rc = BOOT_EFLASH;
-        goto done;
-    }
-
-    split_flash_id = flash_area_id_from_image_slot(split_slot);
-    rc = flash_area_open(split_flash_id, &boot_data.imgs[split_slot].area);
-    if (rc != 0) {
-        rc = BOOT_EFLASH;
-        goto done;
-    }
-
-    /* Determine the sector layout of the image slots.
-     *
-     * A scratch area is not meaningful for split booting, so don't
-     * try to initialize it.
-     */
-    loader_num_sectors = BOOT_MAX_IMG_SECTORS;
-    rc = flash_area_get_sectors(loader_flash_id, &loader_num_sectors,
-                                boot_data.imgs[loader_slot].sectors);
+    /* Determine the sector layout of the image slots and scratch area. */
+    rc = boot_read_sectors();
     if (rc != 0) {
         rc = SPLIT_GO_ERR;
         goto done;
     }
-    boot_data.imgs[loader_slot].num_sectors = loader_num_sectors;
-    split_num_sectors = BOOT_MAX_IMG_SECTORS;
-    rc = flash_area_get_sectors(split_flash_id, &split_num_sectors,
-                                boot_data.imgs[split_slot].sectors);
-    if (rc != 0) {
-        rc = SPLIT_GO_ERR;
-        goto done;
-    }
-    boot_data.imgs[split_slot].num_sectors = split_num_sectors;
 
     rc = boot_read_image_headers();
     if (rc != 0) {
         goto done;
     }
 
+    app_flash_id = flash_area_id_from_image_slot(split_slot);
+    rc = flash_area_open(app_flash_id, &app_fap);
+    if (rc != 0) {
+        rc = BOOT_EFLASH;
+        goto done;
+    }
+
+    loader_flash_id = flash_area_id_from_image_slot(loader_slot);
+    rc = flash_area_open(loader_flash_id, &loader_fap);
+    if (rc != 0) {
+        rc = BOOT_EFLASH;
+        goto done;
+    }
+
     /* Don't check the bootable image flag because we could really call a
      * bootable or non-bootable image.  Just validate that the image check
      * passes which is distinct from the normal check.
      */
     rc = split_image_check(&boot_data.imgs[split_slot].hdr,
-                           boot_data.imgs[split_slot].area,
+                           app_fap,
                            &boot_data.imgs[loader_slot].hdr,
-                           boot_data.imgs[loader_slot].area);
+                           loader_fap);
     if (rc != 0) {
         rc = SPLIT_GO_NON_MATCHING;
         goto done;
     }
 
-    entry_val = boot_data.imgs[split_slot].area->fa_off +
+    entry_val = boot_data.imgs[split_slot].sectors[0].fa_off +
                 boot_data.imgs[split_slot].hdr.ih_hdr_size;
     *entry = (void *) entry_val;
     rc = SPLIT_GO_OK;
 
 done:
     free(sectors);
-    flash_area_close(boot_data.imgs[loader_slot].area);
-    flash_area_close(boot_data.imgs[split_slot].area);
+    flash_area_close(app_fap);
+    flash_area_close(loader_fap);
     return rc;
 }
diff --git a/boot/bootutil/test/src/boot_test_utils.c b/boot/bootutil/test/src/boot_test_utils.c
index bf66d24..30297c8 100644
--- a/boot/bootutil/test/src/boot_test_utils.c
+++ b/boot/bootutil/test/src/boot_test_utils.c
@@ -464,7 +464,6 @@
     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;
@@ -504,13 +503,9 @@
             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(flash_base + rsp.br_image_off ==
-                    boot_test_img_addrs[0].address);
+        TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+        TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
 
         boot_test_util_verify_flash(slot0hdr, orig_slot_0,
                                     slot1hdr, orig_slot_1);
diff --git a/boot/zephyr/flash_map.c b/boot/zephyr/flash_map.c
index 2f2b160..fca3724 100644
--- a/boot/zephyr/flash_map.c
+++ b/boot/zephyr/flash_map.c
@@ -32,67 +32,27 @@
 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[] = {
+static const struct flash_area 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,
-        },
+        .fa_id = FLASH_AREA_IMAGE_0,
+        .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,
-        },
+        .fa_id = FLASH_AREA_IMAGE_1,
+        .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,
-        },
-    }
+        .fa_id = FLASH_AREA_IMAGE_SCRATCH,
+        .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.
@@ -104,7 +64,7 @@
     BOOT_LOG_DBG("area %d", id);
 
     for (i = 0; i < ARRAY_SIZE(part_map); i++) {
-        if (id == part_map[i].area.fa_id) {
+        if (id == part_map[i].fa_id) {
             break;
         }
     }
@@ -112,8 +72,7 @@
         return -1;
     }
 
-    *area = &part_map[i].area;
-    part_map[i].ref_count++;
+    *area = &part_map[i];
     return 0;
 }
 
@@ -122,30 +81,6 @@
  */
 void flash_area_close(const struct flash_area *area)
 {
-    struct flash_map_entry *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,
@@ -197,8 +132,19 @@
 #define FLASH_AREA_IMAGE_SECTOR_SIZE FLASH_AREA_IMAGE_SCRATCH_SIZE
 #endif
 
-static int validate_idx(int idx, uint32_t *off, uint32_t *len)
+/*
+ * 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
+ * the storage at `ret` and should be set to the final number of
+ * sectors in this area.
+ */
+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;
+
     /*
      * This simple layout has uniform slots, so just fill in the
      * right one.
@@ -207,44 +153,30 @@
         return -1;
     }
 
+    if (*cnt < 1) {
+        return -1;
+    }
+
     switch (idx) {
     case FLASH_AREA_IMAGE_0:
-        *off = FLASH_AREA_IMAGE_0_OFFSET;
-        *len = FLASH_AREA_IMAGE_0_SIZE;
-        goto done;
+        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;
-        goto done;
+        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;
-        goto done;
+        off = FLASH_AREA_IMAGE_SCRATCH_OFFSET;
+        len = FLASH_AREA_IMAGE_SCRATCH_SIZE;
+        break;
     default:
         BOOT_LOG_ERR("unknown flash area %d", idx);
         return -1;
     }
 
- done:
     BOOT_LOG_DBG("area %d: offset=0x%x, length=0x%x, sector size=0x%x",
-                 idx, *off, *len, FLASH_AREA_IMAGE_SECTOR_SIZE);
-    return 0;
-}
-
-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 (validate_idx(idx, &off, &len)) {
-        return -1;
-    }
-
-    if (*cnt < 1) {
-        return -1;
-    }
+             idx, off, len, FLASH_AREA_IMAGE_SECTOR_SIZE);
 
     rem_len = len;
     *cnt = 0;
@@ -271,47 +203,3 @@
 
     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
- * the storage at `ret` and should be set to the final number of
- * sectors in this area.
- */
-int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
-{
-    uint32_t off;
-    uint32_t len;
-    uint32_t max_cnt = *cnt;
-    uint32_t rem_len;
-
-    if (validate_idx(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].fs_off = FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt);
-        ret[*cnt].fs_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;
-}
diff --git a/boot/zephyr/include/flash_map/flash_map.h b/boot/zephyr/include/flash_map/flash_map.h
index 506f266..271c50c 100644
--- a/boot/zephyr/include/flash_map/flash_map.h
+++ b/boot/zephyr/include/flash_map/flash_map.h
@@ -43,67 +43,14 @@
  */
 #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.
  */
@@ -126,16 +73,8 @@
 uint8_t flash_area_align(const struct flash_area *);
 
 /*
- * Given flash area ID, return info about sectors within the area.
+ * Given flash map index, 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);
diff --git a/boot/zephyr/main.c b/boot/zephyr/main.c
index e5a1eb8..9791f26 100644
--- a/boot/zephyr/main.c
+++ b/boot/zephyr/main.c
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 
-#include <assert.h>
 #include <zephyr.h>
 #include <flash.h>
 #include <asm_inline.h>
@@ -26,7 +25,6 @@
 #include "bootutil/bootutil_log.h"
 #include "bootutil/image.h"
 #include "bootutil/bootutil.h"
-#include "flash_map/flash_map.h"
 
 struct device *boot_flash_device;
 
@@ -38,24 +36,16 @@
     uint32_t reset;
 };
 
-extern void zephyr_flash_area_warn_on_open(void);
-
 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
      */
-    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 +
+    vt = (struct arm_vector_table *)(rsp->br_image_addr +
                                      rsp->br_hdr->ih_hdr_size);
     irq_lock();
     sys_clock_disable();
@@ -69,15 +59,9 @@
  */
 static void do_boot(struct boot_rsp *rsp)
 {
-    uintptr_t flash_base;
     void *start;
-    int rc;
 
-    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);
+    start = (void *)(rsp->br_image_addr + rsp->br_hdr->ih_hdr_size);
 
     /* Lock interrupts and dive into the entry point */
     irq_lock();
@@ -108,9 +92,7 @@
             ;
     }
 
-    BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
-                 rsp.br_image_off);
-    zephyr_flash_area_warn_on_open();
+    BOOT_LOG_INF("Bootloader chainload address: 0x%x", rsp.br_image_addr);
     BOOT_LOG_INF("Jumping to the first image slot");
     do_boot(&rsp);