bootutil: add abstraction for area initialization

Add an abstraction for initializing bootloader state for a particular
flash area.

For now, we preserve some existing hacky behavior related to the
scratch area. This will get cleaned up more later in the series.

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 ddfdebd..4c12916 100644
--- a/boot/bootutil/src/bootutil_priv.h
+++ b/boot/bootutil/src/bootutil_priv.h
@@ -20,6 +20,7 @@
 #ifndef H_BOOTUTIL_PRIV_
 #define H_BOOTUTIL_PRIV_
 
+#include "sysflash/sysflash.h"
 #include "flash_map/flash_map.h"
 #include "bootutil/image.h"
 
@@ -98,6 +99,9 @@
 /** Number of image slots in flash; currently limited to two. */
 #define BOOT_NUM_SLOTS              2
 
+/** Maximum number of image sectors supported by the bootloader. */
+#define BOOT_MAX_IMG_SECTORS        120
+
 /** Private state maintained during boot. */
 struct boot_loader_state {
     struct {
@@ -155,13 +159,6 @@
     return state->imgs[slot].num_sectors;
 }
 
-static inline void
-boot_img_set_num_sectors(struct boot_loader_state *state, size_t slot,
-                         size_t num_sectors)
-{
-    state->imgs[slot].num_sectors = num_sectors;
-}
-
 static inline size_t
 boot_img_sector_size(struct boot_loader_state *state,
                      size_t slot, size_t sector)
@@ -174,6 +171,45 @@
     return state->scratch_sector.fa_size;
 }
 
+static inline int
+boot_initialize_area(struct boot_loader_state *state, int flash_area)
+{
+    const struct flash_area *scratch;
+    int num_sectors = BOOT_MAX_IMG_SECTORS;
+    size_t slot;
+    int rc;
+
+    switch (flash_area) {
+    case FLASH_AREA_IMAGE_0:
+        slot = 0;
+        break;
+    case FLASH_AREA_IMAGE_1:
+        slot = 1;
+        break;
+    case FLASH_AREA_IMAGE_SCRATCH:
+        /*
+         * Special case, handle hackishly by getting area pointer and
+         * leaving the area open.
+         */
+        rc = flash_area_open(flash_area, &scratch);
+        if (rc != 0) {
+            return rc;
+        }
+        state->scratch_sector = *scratch;
+        return 0;
+    default:
+        return BOOT_EFLASH;
+    }
+
+    rc = flash_area_to_sectors(flash_area, &num_sectors,
+                               state->imgs[slot].sectors);
+    if (rc != 0) {
+        return rc;
+    }
+    state->imgs[slot].num_sectors = num_sectors;
+    return 0;
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c
index c0ecda2..e6d3b5c 100644
--- a/boot/bootutil/src/loader.c
+++ b/boot/bootutil/src/loader.c
@@ -27,7 +27,6 @@
 #include <inttypes.h>
 #include <stdlib.h>
 #include <string.h>
-#include "sysflash/sysflash.h"
 #include <hal/hal_flash.h>
 #include <os/os_malloc.h>
 #include "bootutil/bootutil.h"
@@ -41,8 +40,6 @@
 #include "mynewt/config.h"
 #endif
 
-#define BOOT_MAX_IMG_SECTORS        120
-
 static struct boot_loader_state boot_data;
 
 struct boot_status_table {
@@ -326,32 +323,22 @@
 static int
 boot_read_sectors(void)
 {
-    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_to_sectors(FLASH_AREA_IMAGE_0, &num_sectors_slot0,
-                               boot_data.imgs[0].sectors);
+    rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
     if (rc != 0) {
         return BOOT_EFLASH;
     }
-    boot_img_set_num_sectors(&boot_data, 0, num_sectors_slot0);
 
-    num_sectors_slot1 = BOOT_MAX_IMG_SECTORS;
-    rc = flash_area_to_sectors(FLASH_AREA_IMAGE_1, &num_sectors_slot1,
-                               boot_data.imgs[1].sectors);
+    rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
     if (rc != 0) {
         return BOOT_EFLASH;
     }
-    boot_img_set_num_sectors(&boot_data, 1, num_sectors_slot1);
 
-    rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &scratch);
+    rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
     if (rc != 0) {
         return BOOT_EFLASH;
     }
-    boot_data.scratch_sector = *scratch;
 
     boot_data.write_sz = boot_write_sz();