Fix boot_serial to follow flash write alignment

This fixes an issue found on stm32f3 where newtmgr after writing an odd
sized packet, would hardfault on the next write due to trying to write an
odd address which is not valid on stm32f3 flash.

Signed-off-by: Fabio Utzig <utzig@apache.org>
diff --git a/boot/boot_serial/src/boot_serial.c b/boot/boot_serial/src/boot_serial.c
index e893617..7467c9f 100644
--- a/boot/boot_serial/src/boot_serial.c
+++ b/boot/boot_serial/src/boot_serial.c
@@ -188,6 +188,7 @@
     uint8_t img_data[400];
     long long unsigned int off = UINT_MAX;
     size_t img_blen = 0;
+    uint8_t rem_bytes;
     long long unsigned int data_len = UINT_MAX;
     const struct cbor_attr_t attr[4] = {
         [0] = {
@@ -222,7 +223,6 @@
         goto out;
     }
 
-
     rc = flash_area_open(flash_area_id_from_image_slot(0), &fap);
     if (rc) {
         rc = MGMT_ERR_EINVAL;
@@ -246,6 +246,12 @@
         rc = 0;
         goto out;
     }
+    if (curr_off + img_blen < img_size) {
+        rem_bytes = img_blen % flash_area_align(fap);
+        if (rem_bytes) {
+            img_blen -= rem_bytes;
+        }
+    }
     rc = flash_area_write(fap, curr_off, img_data, img_blen);
     if (rc) {
         rc = MGMT_ERR_EINVAL;
@@ -322,7 +328,6 @@
     bs_writer.bytes_written = 0;
     cbor_encoder_init(&bs_root, &bs_writer, 0);
 
-
     /*
      * Limited support for commands.
      */