Add support for swapping only fw sectors in use

Before this patch, the swapping would process all sectors in a slot
not matter what the size of the binary firmware was. This changes the
swap process to swap only sectors that are in use by firmware.

Also, if the last slot sector, which stores the trailer, is actually not
in use by the binary firmware, now trailer is never written to scratch.
`use_scratch` temp variable was added to boot_status struct to control
this (this var is never written to disk).

Random other small refactorings were applied.
diff --git a/boot/bootutil/src/bootutil_misc.c b/boot/bootutil/src/bootutil_misc.c
index cfc9ba5..ba8fa7f 100644
--- a/boot/bootutil/src/bootutil_misc.c
+++ b/boot/bootutil/src/bootutil_misc.c
@@ -166,16 +166,18 @@
 boot_slots_trailer_sz(uint8_t min_write_sz)
 {
     return BOOT_MAGIC_SZ                                                    +
+           /* state for all sectors */
            BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz +
+           /* copy_done + image_ok */
            min_write_sz * 2;
 }
 
 static uint32_t
 boot_scratch_trailer_sz(uint8_t min_write_sz)
 {
-    return BOOT_MAGIC_SZ                           +
-           BOOT_STATUS_STATE_COUNT * min_write_sz  +
-           min_write_sz * 2;
+    return BOOT_MAGIC_SZ                          +  /* magic */
+           BOOT_STATUS_STATE_COUNT * min_write_sz +  /* state for one sector */
+           min_write_sz;                             /* image_ok */
 }
 
 static uint32_t
@@ -205,6 +207,7 @@
 static uint32_t
 boot_copy_done_off(const struct flash_area *fap)
 {
+    assert(fap->fa_id != FLASH_AREA_IMAGE_SCRATCH);
     return fap->fa_size - flash_area_align(fap) * 2;
 }
 
@@ -229,10 +232,12 @@
     }
     state->magic = boot_magic_code(magic);
 
-    off = boot_copy_done_off(fap);
-    rc = flash_area_read(fap, off, &state->copy_done, 1);
-    if (rc != 0) {
-        return BOOT_EFLASH;
+    if (fap->fa_id != FLASH_AREA_IMAGE_SCRATCH) {
+        off = boot_copy_done_off(fap);
+        rc = flash_area_read(fap, off, &state->copy_done, 1);
+        if (rc != 0) {
+            return BOOT_EFLASH;
+        }
     }
 
     off = boot_image_ok_off(fap);
@@ -248,56 +253,31 @@
  * Reads the image trailer from the scratch area.
  */
 int
-boot_read_swap_state_scratch(struct boot_swap_state *state)
+boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state)
 {
     const struct flash_area *fap;
     int rc;
 
-    rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
+    switch (flash_area_id) {
+    case FLASH_AREA_IMAGE_SCRATCH:
+    case FLASH_AREA_IMAGE_0:
+    case FLASH_AREA_IMAGE_1:
+        rc = flash_area_open(flash_area_id, &fap);
+        if (rc != 0) {
+            return BOOT_EFLASH;
+        }
+        break;
+    default:
+        return BOOT_EFLASH;
+    }
+
+    rc = boot_read_swap_state(fap, state);
     if (rc) {
-        rc = BOOT_EFLASH;
-        goto done;
+        flash_area_close(fap);
+        return rc;
     }
 
-    rc = boot_read_swap_state(fap, state);
-    if (rc != 0) {
-        goto done;
-    }
-
-    rc = 0;
-
-done:
-    flash_area_close(fap);
-    return rc;
-}
-
-/**
- * Reads the image trailer from a given image slot.
- */
-int
-boot_read_swap_state_img(int slot, struct boot_swap_state *state)
-{
-    const struct flash_area *fap;
-    int area_id;
-    int rc;
-
-    area_id = flash_area_id_from_image_slot(slot);
-    rc = flash_area_open(area_id, &fap);
-    if (rc != 0) {
-        rc = BOOT_EFLASH;
-        goto done;
-    }
-
-    rc = boot_read_swap_state(fap, state);
-    if (rc != 0) {
-        goto done;
-    }
-
-    rc = 0;
-
-done:
-    flash_area_close(fap);
-    return rc;
+    return 0;
 }
 
 int
@@ -316,15 +296,24 @@
     return 0;
 }
 
-int
-boot_write_copy_done(const struct flash_area *fap)
+static int
+boot_write_flag(int flag, const struct flash_area *fap)
 {
     uint32_t off;
     int rc;
     uint8_t buf[BOOT_MAX_ALIGN];
     uint8_t align;
 
-    off = boot_copy_done_off(fap);
+    switch (flag) {
+    case BOOT_FLAG_COPY_DONE:
+        off = boot_copy_done_off(fap);
+        break;
+    case BOOT_FLAG_IMAGE_OK:
+        off = boot_image_ok_off(fap);
+        break;
+    default:
+        return BOOT_EFLASH;
+    }
 
     align = hal_flash_align(fap->fa_device_id);
     assert(align <= BOOT_MAX_ALIGN);
@@ -340,26 +329,15 @@
 }
 
 int
+boot_write_copy_done(const struct flash_area *fap)
+{
+    return boot_write_flag(BOOT_FLAG_COPY_DONE, fap);
+}
+
+int
 boot_write_image_ok(const struct flash_area *fap)
 {
-    uint32_t off;
-    int rc;
-    uint8_t buf[BOOT_MAX_ALIGN];
-    uint8_t align;
-
-    off = boot_image_ok_off(fap);
-
-    align = hal_flash_align(fap->fa_device_id);
-    assert(align <= BOOT_MAX_ALIGN);
-    memset(buf, 0xFF, BOOT_MAX_ALIGN);
-    buf[0] = 1;
-
-    rc = flash_area_write(fap, off, buf, align);
-    if (rc != 0) {
-        return BOOT_EFLASH;
-    }
-
-    return 0;
+    return boot_write_flag(BOOT_FLAG_IMAGE_OK, fap);
 }
 
 int
@@ -371,10 +349,10 @@
     int rc;
     int i;
 
-    rc = boot_read_swap_state_img(0, &state_slot0);
+    rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
     assert(rc == 0);
 
-    rc = boot_read_swap_state_img(1, &state_slot1);
+    rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1);
     assert(rc == 0);
 
     for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) {
@@ -419,10 +397,9 @@
 {
     const struct flash_area *fap;
     struct boot_swap_state state_slot1;
-    int area_id;
     int rc;
 
-    rc = boot_read_swap_state_img(1, &state_slot1);
+    rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &state_slot1);
     if (rc != 0) {
         return rc;
     }
@@ -433,8 +410,7 @@
         return 0;
 
     case BOOT_MAGIC_UNSET:
-        area_id = flash_area_id_from_image_slot(1);
-        rc = flash_area_open(area_id, &fap);
+        rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap);
         if (rc != 0) {
             rc = BOOT_EFLASH;
         } else {
@@ -467,7 +443,7 @@
     struct boot_swap_state state_slot0;
     int rc;
 
-    rc = boot_read_swap_state_img(0, &state_slot0);
+    rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
     if (rc != 0) {
         return rc;
     }