bootutil: optionally use flash_area_get_sectors()

Add a typedef which lets us use flash_sector or flash_area to contain
the sectors within the boot_data global. When
MCUBOOT_USE_FLASH_AREA_GET_SECTORS is defined, this is struct
flash_sector.

Also add struct boot_loader_state accessors to handle this case, and
make the appropriate changes to where the sectors are allocated to use
the new typedef.

Finally, ensure MCUBOOT_USE_FLASH_AREA_GET_SECTORS is defined in the
Zephyr Makefile, since flash_area_get_sectors() is already provided
there.

This lets mcuboot users convert to the new flash API gradually.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
diff --git a/boot/bootutil/src/bootutil_priv.h b/boot/bootutil/src/bootutil_priv.h
index 7603bd0..3f0a68d 100644
--- a/boot/bootutil/src/bootutil_priv.h
+++ b/boot/bootutil/src/bootutil_priv.h
@@ -102,12 +102,23 @@
 /** Maximum number of image sectors supported by the bootloader. */
 #define BOOT_MAX_IMG_SECTORS        120
 
+/**
+ * Compatibility shim for flash sector type.
+ *
+ * This can be deleted when flash_area_to_sectors() is removed.
+ */
+#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
+typedef struct flash_sector boot_sector_t;
+#else
+typedef struct flash_area boot_sector_t;
+#endif
+
 /** Private state maintained during boot. */
 struct boot_loader_state {
     struct {
         struct image_header hdr;
         const struct flash_area *area;
-        struct flash_area *sectors;
+        boot_sector_t *sectors;
         size_t num_sectors;
     } imgs[BOOT_NUM_SLOTS];
 
@@ -179,6 +190,8 @@
     return state->scratch_area->fa_size;
 }
 
+#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
+
 static inline size_t
 boot_img_sector_size(struct boot_loader_state *state,
                      size_t slot, size_t sector)
@@ -225,6 +238,56 @@
     return 0;
 }
 
+#else  /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
+
+static inline size_t
+boot_img_sector_size(struct boot_loader_state *state,
+                     size_t slot, size_t sector)
+{
+    return state->imgs[slot].sectors[sector].fs_size;
+}
+
+static inline uint32_t
+boot_img_sector_off(struct boot_loader_state *state, size_t slot,
+                    size_t sector)
+{
+    return state->imgs[slot].sectors[sector].fs_off -
+           state->imgs[slot].sectors[0].fs_off;
+}
+
+static inline int
+boot_initialize_area(struct boot_loader_state *state, int flash_area)
+{
+    uint32_t num_sectors;
+    struct flash_sector *out_sectors;
+    size_t *out_num_sectors;
+    int rc;
+
+    switch (flash_area) {
+    case FLASH_AREA_IMAGE_0:
+        num_sectors = BOOT_MAX_IMG_SECTORS;
+        out_sectors = state->imgs[0].sectors;
+        out_num_sectors = &state->imgs[0].num_sectors;
+        break;
+    case FLASH_AREA_IMAGE_1:
+        num_sectors = BOOT_MAX_IMG_SECTORS;
+        out_sectors = state->imgs[1].sectors;
+        out_num_sectors = &state->imgs[1].num_sectors;
+        break;
+    default:
+        return -1;
+    }
+
+    rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
+    if (rc != 0) {
+        return rc;
+    }
+    *out_num_sectors = num_sectors;
+    return 0;
+}
+
+#endif  /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
+
 #ifdef __cplusplus
 }
 #endif