Refactor trailer writing routines

This adds a single trailer writing routine that can write any amount of
data (upto BOOT_MAX_ALIGN); the functions that were writing to the
trailer now just call this one passing the offset and data/len.

Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/boot/bootutil/src/bootutil_misc.c b/boot/bootutil/src/bootutil_misc.c
index 37618e5..84e77e1 100644
--- a/boot/bootutil/src/bootutil_misc.c
+++ b/boot/bootutil/src/bootutil_misc.c
@@ -417,9 +417,14 @@
     return 0;
 }
 
+/**
+ * Write trailer data; status bytes, swap_size, etc
+ *
+ * @returns 0 on success, != 0 on error.
+ */
 static int
-boot_write_trailer_byte(const struct flash_area *fap, uint32_t off,
-                        uint8_t val)
+boot_write_trailer(const struct flash_area *fap, uint32_t off,
+        const uint8_t *inbuf, uint8_t inlen)
 {
     uint8_t buf[BOOT_MAX_ALIGN];
     uint8_t align;
@@ -427,10 +432,15 @@
     int rc;
 
     align = flash_area_align(fap);
-    assert(align <= BOOT_MAX_ALIGN);
+    if (inlen > BOOT_MAX_ALIGN || align > BOOT_MAX_ALIGN) {
+        return -1;
+    }
     erased_val = flash_area_erased_val(fap);
-    memset(buf, erased_val, BOOT_MAX_ALIGN);
-    buf[0] = val;
+    if (align < inlen) {
+        align = inlen;
+    }
+    memcpy(buf, inbuf, inlen);
+    memset(&buf[inlen], erased_val, align - inlen);
 
     rc = flash_area_write(fap, off, buf, align);
     if (rc != 0) {
@@ -440,6 +450,14 @@
     return 0;
 }
 
+static int
+boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
+        uint8_t flag_val)
+{
+    const uint8_t buf[1] = { flag_val };
+    return boot_write_trailer(fap, off, buf, 1);
+}
+
 int
 boot_write_copy_done(const struct flash_area *fap)
 {
@@ -448,7 +466,7 @@
     off = boot_copy_done_off(fap);
     BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%x (0x%x)",
                  fap->fa_id, off, fap->fa_off + off);
-    return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
+    return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
 }
 
 int
@@ -459,7 +477,7 @@
     off = boot_image_ok_off(fap);
     BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%x (0x%x)",
                  fap->fa_id, off, fap->fa_off + off);
-    return boot_write_trailer_byte(fap, off, BOOT_FLAG_SET);
+    return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET);
 }
 
 /**
@@ -479,37 +497,18 @@
     BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%x (0x%x), swap_type=0x%x"
                  " image_num=0x%x",
                  fap->fa_id, off, fap->fa_off + off, swap_type, image_num);
-    return boot_write_trailer_byte(fap, off, swap_info);
+    return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1);
 }
 
 int
 boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size)
 {
     uint32_t off;
-    int rc;
-    uint8_t buf[BOOT_MAX_ALIGN];
-    uint8_t align;
-    uint8_t erased_val;
 
     off = boot_swap_size_off(fap);
-    align = flash_area_align(fap);
-    assert(align <= BOOT_MAX_ALIGN);
-    if (align < sizeof swap_size) {
-        align = sizeof swap_size;
-    }
-    erased_val = flash_area_erased_val(fap);
-    memset(buf, erased_val, BOOT_MAX_ALIGN);
-    memcpy(buf, (uint8_t *)&swap_size, sizeof swap_size);
-
     BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%x (0x%x)",
                  fap->fa_id, off, fap->fa_off + off);
-
-    rc = flash_area_write(fap, off, buf, align);
-    if (rc != 0) {
-        return BOOT_EFLASH;
-    }
-
-    return 0;
+    return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4);
 }
 
 #ifdef MCUBOOT_ENC_IMAGES