bootutil: add image and area offset abstractions

Add abstractions for calculating the starting offset of a sector from
the beginning of its image, and the starting offset of an image slot
from the beginning of its flash device.

Using this tweaks a check in boot_swap_sectors(), but doesn't change
its outcome.

Signed-off-by: Marti Bolivar <marti.bolivar@linaro.org>
diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c
index e6d3b5c..18db4af 100644
--- a/boot/bootutil/src/loader.c
+++ b/boot/bootutil/src/loader.c
@@ -784,7 +784,6 @@
 {
     uint8_t slot;
     uint32_t last_sector;
-    struct flash_area *sectors;
     int rc;
 
     switch (flash_area_id) {
@@ -799,9 +798,8 @@
     }
 
     last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
-    sectors = boot_data.imgs[slot].sectors;
     rc = boot_erase_sector(flash_area_id,
-            sectors[last_sector].fa_off - sectors[0].fa_off,
+            boot_img_sector_off(&boot_data, slot, last_sector),
             boot_img_sector_size(&boot_data, slot, last_sector));
     assert(rc == 0);
 
@@ -833,14 +831,13 @@
     int rc;
 
     /* 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;
+    img_off = boot_img_sector_off(&boot_data, 0, idx);
 
     copy_sz = sz;
     trailer_sz = boot_slots_trailer_sz(boot_data.write_sz);
 
     /* sz in this function is always is always sized on a multiple of the
-     * sector size. The check against the first address of the last sector
+     * sector size. The check against the start offset of the last sector
      * is to determine if we're swapping the last sector. The last sector
      * needs special handling because it's where the trailer lives. If we're
      * copying it, we need to use scratch to write the trailer temporarily.
@@ -849,8 +846,7 @@
      * controls if special handling is needed (swapping last sector).
      */
     last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
-    if (boot_data.imgs[0].sectors[idx].fa_off + sz >
-        boot_data.imgs[0].sectors[last_sector].fa_off) {
+    if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
         copy_sz -= trailer_sz;
     }
 
@@ -1283,7 +1279,7 @@
 
     /* Always boot from the primary slot. */
     rsp->br_flash_id = boot_img_fa_device_id(&boot_data, 0);
-    rsp->br_image_addr = boot_data.imgs[0].sectors[0].fa_off;
+    rsp->br_image_addr = boot_img_slot_off(&boot_data, 0);
     rsp->br_hdr = boot_img_hdr(&boot_data, slot);
 
     return 0;
@@ -1350,7 +1346,7 @@
         goto done;
     }
 
-    entry_val = boot_data.imgs[split_slot].sectors[0].fa_off +
+    entry_val = boot_img_slot_off(&boot_data, split_slot) +
                 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
     *entry = (void *) entry_val;
     rc = SPLIT_GO_OK;