boot_serial: added hooks for mcuboot image access operations

Added hook for: read image header, validate the image and hook
which is called after image was uploaded completely.

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
diff --git a/boot/boot_serial/src/boot_serial.c b/boot/boot_serial/src/boot_serial.c
index 979e98d..e138615 100644
--- a/boot/boot_serial/src/boot_serial.c
+++ b/boot/boot_serial/src/boot_serial.c
@@ -59,6 +59,7 @@
 #endif
 
 #include "serial_recovery_cbor.h"
+#include "bootutil/boot_hooks.h"
 
 BOOT_LOG_MODULE_DECLARE(mcuboot);
 
@@ -187,15 +188,32 @@
                 continue;
             }
 
-            flash_area_read(fap, 0, &hdr, sizeof(hdr));
+            int rc = BOOT_HOOK_CALL(boot_read_image_header_hook,
+                                    BOOT_HOOK_REGULAR, image_index, slot, &hdr);
+            if (rc == BOOT_HOOK_REGULAR)
+            {
+                flash_area_read(fap, 0, &hdr, sizeof(hdr));
+            }
 
-            if (hdr.ih_magic != IMAGE_MAGIC ||
-              bootutil_img_validate(NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf),
-                                    NULL, 0, NULL)) {
-                flash_area_close(fap);
+            fih_int fih_rc = FIH_FAILURE;
+
+            if (hdr.ih_magic == IMAGE_MAGIC)
+            {
+                BOOT_HOOK_CALL_FIH(boot_image_check_hook,
+                                   fih_int_encode(BOOT_HOOK_REGULAR),
+                                   fih_rc, image_index, slot);
+                if (fih_eq(fih_rc, BOOT_HOOK_REGULAR))
+                {
+                    FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, &hdr, fap, tmpbuf, sizeof(tmpbuf),
+                                    NULL, 0, NULL);
+                }
+            }
+
+            flash_area_close(fap);
+
+            if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
                 continue;
             }
-            flash_area_close(fap);
 
             map_start_encode(&cbor_state, 20);
 
@@ -376,8 +394,8 @@
 
     if (rc == 0) {
         curr_off += img_blen;
-#ifdef MCUBOOT_ERASE_PROGRESSIVELY
         if (curr_off == img_size) {
+#ifdef MCUBOOT_ERASE_PROGRESSIVELY
             /* get the last sector offset */
             rc = flash_area_sector_from_off(boot_status_off(fap), &sector);
             if (rc) {
@@ -397,8 +415,14 @@
                     goto out;
                 }
             }
-        }
 #endif
+            rc = BOOT_HOOK_CALL(boot_serial_uploaded_hook, 0, img_num, fap,
+                                img_size);
+            if (rc) {
+                BOOT_LOG_ERR("Error %d post upload hook", rc);
+                goto out;
+            }
+        }
     } else {
     out_invalid_data:
         rc = MGMT_ERR_EINVAL;
diff --git a/boot/bootutil/include/bootutil/boot_hooks.h b/boot/bootutil/include/bootutil/boot_hooks.h
index a94c5de..bb5406b 100644
--- a/boot/bootutil/include/bootutil/boot_hooks.h
+++ b/boot/bootutil/include/bootutil/boot_hooks.h
@@ -119,4 +119,22 @@
 int boot_copy_region_post_hook(int img_index, const struct flash_area *area,
                                size_t size);
 
+/** Hook for implement image's post recovery upload action
+ *
+ * This hook is for implement action which might be done right after image was
+ * copied to the primary slot. This hook is called in serial recovery upload
+ * operation.
+ *
+ * @param img_index the index of the image pair
+ * @param area the flash area of the primary image.
+ * @param size size of copied image.
+ *
+ * @retval 0: success, mcuboot will follow normal code execution flow after
+ *            execution of this call.
+ *         non-zero: an error, will be transferred as part of comand response
+ *            as "rc" entry.
+ */
+int boot_serial_uploaded_hook(int img_index, const struct flash_area *area,
+                              size_t size);
+
 #endif /*H_BOOTUTIL_HOOKS*/