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/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);