zephyr: Added hooks for example

Example (very basic) hook implementation.
Added case description for test-build with these hooks
and multi-image feature.

Signed-off-by: Andrzej Puzdrowski <andrzej.puzdrowski@nordicsemi.no>
diff --git a/boot/zephyr/boards/nrf52840dk_hooks_sample_overlay.conf b/boot/zephyr/boards/nrf52840dk_hooks_sample_overlay.conf
new file mode 100644
index 0000000..5c13cd2
--- /dev/null
+++ b/boot/zephyr/boards/nrf52840dk_hooks_sample_overlay.conf
@@ -0,0 +1,7 @@
+CONFIG_UPDATEABLE_IMAGE_NUMBER=2
+
+CONFIG_FLASH_SIMULATOR=y
+CONFIG_FLASH_SIMULATOR_UNALIGNED_READ=y
+
+CONFIG_BOOT_IMAGE_ACCESS_HOOKS=y
+CONFIG_BOOT_IMAGE_ACCESS_HOOKS_FILE="hooks_sample.c"
diff --git a/boot/zephyr/boards/nrf52840dk_ram_multi.overlay b/boot/zephyr/boards/nrf52840dk_ram_multi.overlay
new file mode 100644
index 0000000..7befec9
--- /dev/null
+++ b/boot/zephyr/boards/nrf52840dk_ram_multi.overlay
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2021 Nordic Semiconductor ASA
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/delete-node/ &slot1_partition;
+/delete-node/ &slot0_partition;
+/delete-node/ &boot_partition;
+
+&flash0 {
+	partitions {
+		boot_partition: partition@0 {
+			label = "mcuboot";
+			reg = <0x000000000 0x00010000>;
+		};
+		slot0_partition: partition@10000 {
+			label = "image-0";
+			reg = <0x000010000 0x00000A000>;
+		};
+		slot1_partition: partition@1A000 {
+			label = "image-1";
+			reg = <0x00001A000 0x00000A000>;
+		};
+		slot3_partition: partition@24000 {
+			label = "image-3";
+			reg = <0x000024000 0x00000A000>;
+		};
+	};
+};
+
+/ {
+	soc {
+		flash_controller2: flash-controller@2 {
+			compatible = "zephyr,sim-flash";
+			reg = <0x00000000 DT_SIZE_K(40)>;
+
+			#address-cells = <1>;
+			#size-cells = <1>;
+			erase-value = <0xff>;
+
+			label = "flash_ctrl";
+
+			flash_sim0: flash_sim@0 {
+				status = "okay";
+				compatible = "soc-nv-flash";
+				label = "simulated_flash";
+				erase-block-size = <4096>;
+				write-block-size = <1>;
+				reg = <0x00000000 DT_SIZE_K(40)>;
+
+				partitions {
+					compatible = "fixed-partitions";
+					#address-cells = <1>;
+					#size-cells = <1>;
+
+					slot2_partition: partition@0 {
+						label = "image-2";
+						reg = <0x00000000 0x00000A000>;
+					};
+				};
+			};
+		};
+	};
+};
diff --git a/boot/zephyr/hooks_sample.c b/boot/zephyr/hooks_sample.c
new file mode 100644
index 0000000..a4dbfb5
--- /dev/null
+++ b/boot/zephyr/hooks_sample.c
@@ -0,0 +1,95 @@
+/*
+ * Copyright (c) 2021 Nordic Semiconductor ASA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <assert.h>
+#include <zephyr.h>
+#include "bootutil/image.h"
+#include "bootutil/bootutil.h"
+#include "bootutil/fault_injection_hardening.h"
+#include "flash_map_backend/flash_map_backend.h"
+
+/* @retval 0: header was read/populated
+ *         FIH_FAILURE: image is invalid,
+ *         BOOT_HOOK_REGULAR if hook not implemented for the image-slot,
+ *         othervise an error-code value.
+ */
+int boot_read_image_header_hook(int img_index, int slot,
+                                struct image_header *img_hed)
+{
+    if (img_index == 1 && slot == 0) {
+            img_hed->ih_magic = IMAGE_MAGIC;
+            return 0;
+    }
+
+    return BOOT_HOOK_REGULAR;
+}
+
+/* @retval FIH_SUCCESS: image is valid,
+ *         FIH_FAILURE: image is invalid,
+ *         fih encoded BOOT_HOOK_REGULAR if hook not implemented for
+ *         the image-slot.
+ */
+fih_int boot_image_check_hook(int img_index, int slot)
+{
+    if (img_index == 1 && slot == 0) {
+        FIH_RET(FIH_SUCCESS);
+    }
+
+    FIH_RET(fih_int_encode(BOOT_HOOK_REGULAR));
+}
+
+int boot_perform_update_hook(int img_index, struct image_header *img_head,
+                             const struct flash_area *area)
+{
+    if (img_index == 1) {
+        return 0;
+    }
+
+    return BOOT_HOOK_REGULAR;
+}
+
+int boot_read_swap_state_primary_slot_hook(int image_index,
+                                           struct boot_swap_state *state)
+{
+    if (image_index == 1) {
+        state->magic = BOOT_MAGIC_UNSET;
+        state->swap_type = BOOT_SWAP_TYPE_NONE;
+        state->image_num = image_index ; // ?
+        state->copy_done = BOOT_FLAG_UNSET;
+        state->image_ok = BOOT_FLAG_UNSET;
+
+        return 0;
+    }
+
+    return BOOT_HOOK_REGULAR;
+}
+
+int boot_copy_region_post_hook(int img_index, const struct flash_area *area,
+                               size_t size)
+{
+    return 0;
+}
+
+int boot_serial_uploaded_hook(int img_index, const struct flash_area *area,
+                               size_t size)
+{
+    return 0;
+}
+
+int boot_img_install_stat_hook(int image_index, int slot, int *img_install_stat)
+{
+    return BOOT_HOOK_REGULAR;
+}
diff --git a/boot/zephyr/sample.yaml b/boot/zephyr/sample.yaml
index 35b4f1e..4f8ee11 100644
--- a/boot/zephyr/sample.yaml
+++ b/boot/zephyr/sample.yaml
@@ -40,3 +40,10 @@
     integration_platforms:
       - nrf52840dk_nrf52840
     tags: bootloader_mcuboot
+  sample.bootloader.mcuboot.hooks_multi:
+    extra_args: DTC_OVERLAY_FILE=./boards/nrf52840dk_ram_multi.overlay
+      OVERLAY_CONFIG=./boards/nrf52840dk_hooks_sample_overlay.conf
+    platform_allow: nrf52840dk_nrf52840
+    integration_platforms:
+      - nrf52840dk_nrf52840
+    tags: bootloader_mcuboot