bootutil: initial implementation of swap status partition

Signed-off-by: Bohdan Kovalchuk <bohd@cypress.com>
Signed-off-by: Roman Okhrimenko <roman.okhrimenko@cypress.com>
Signed-off-by: Taras Boretskyy <taras.boretskyy@cypress.com>
Signed-off-by: Kostiantyn Tkachov <kotk@cypress.com>
Signed-off-by: dmiv <dmiv@cypress.com>
diff --git a/boot/bootutil/src/bootutil_misc.c b/boot/bootutil/src/bootutil_misc.c
index 57ceff4..905bcf6 100644
--- a/boot/bootutil/src/bootutil_misc.c
+++ b/boot/bootutil/src/bootutil_misc.c
@@ -41,6 +41,12 @@
 #include "bootutil/enc_key.h"
 #endif
 
+#ifdef MCUBOOT_SWAP_USING_STATUS
+#include "swap_status.h"
+#endif
+
+#include "mcuboot_config/mcuboot_config.h"
+
 MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
 
 /* Currently only used by imgmgr */
@@ -150,6 +156,8 @@
 }
 #endif
 
+#ifndef MCUBOOT_SWAP_USING_STATUS
+
 static int
 boot_magic_decode(const uint32_t *magic)
 {
@@ -167,7 +175,7 @@
     }
     return BOOT_FLAG_SET;
 }
-
+#endif /* not defined MCUBOOT_SWAP_USING_STATUS */
 /**
  * Determines if a status source table is satisfied by the specified magic
  * code.
@@ -234,6 +242,7 @@
     return -1;
 }
 
+#ifndef MCUBOOT_SWAP_USING_STATUS
 uint32_t
 boot_status_off(const struct flash_area *fap)
 {
@@ -247,6 +256,7 @@
     assert(off_from_end <= fap->fa_size);
     return fap->fa_size - off_from_end;
 }
+#endif
 
 static inline uint32_t
 boot_magic_off(const struct flash_area *fap)
@@ -254,6 +264,8 @@
     return fap->fa_size - BOOT_MAGIC_SZ;
 }
 
+#ifndef MCUBOOT_SWAP_USING_STATUS
+
 static inline uint32_t
 boot_image_ok_off(const struct flash_area *fap)
 {
@@ -277,6 +289,7 @@
 {
     return boot_swap_info_off(fap) - BOOT_MAX_ALIGN;
 }
+#endif
 
 #ifdef MCUBOOT_ENC_IMAGES
 static inline uint32_t
@@ -312,6 +325,7 @@
     return true;
 }
 
+#ifndef MCUBOOT_SWAP_USING_STATUS
 int
 boot_read_swap_state(const struct flash_area *fap,
                      struct boot_swap_state *state)
@@ -374,6 +388,7 @@
 
     return 0;
 }
+#endif /* not MCUBOOT_SWAP_USING_STATUS */
 
 /**
  * Reads the image trailer from the scratch area.
@@ -394,6 +409,8 @@
     return rc;
 }
 
+#ifndef MCUBOOT_SWAP_USING_STATUS
+
 /**
  * This functions tries to locate the status area after an aborted swap,
  * by looking for the magic in the possible locations.
@@ -466,6 +483,8 @@
     return rc;
 }
 
+#endif /* not MCUBOOT_SWAP_USING_STATUS */
+
 #ifdef MCUBOOT_ENC_IMAGES
 int
 boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs)
@@ -503,6 +522,7 @@
 }
 #endif
 
+#ifndef MCUBOOT_SWAP_USING_STATUS
 int
 boot_write_magic(const struct flash_area *fap)
 {
@@ -555,6 +575,8 @@
     return 0;
 }
 
+#endif /* MCUBOOT_SWAP_USING_STATUS */
+
 static int
 boot_write_trailer_flag(const struct flash_area *fap, uint32_t off,
         uint8_t flag_val)
@@ -645,6 +667,17 @@
 }
 #endif
 
+#define BOOT_LOG_SWAP_STATE(area, state)                            \
+    BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, "   \
+                 "image_ok=0x%x",                                   \
+                 (area),                                            \
+                 ((state)->magic == BOOT_MAGIC_GOOD ? "good" :      \
+                  (state)->magic == BOOT_MAGIC_UNSET ? "unset" :    \
+                  "bad"),                                           \
+                 (state)->swap_type,                                \
+                 (state)->copy_done,                                \
+                 (state)->image_ok)
+
 int
 boot_swap_type_multi(int image_index)
 {
@@ -662,6 +695,9 @@
 
     rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
                                     &secondary_slot);
+
+    BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Primary image", &primary_slot);
+    BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Secondary image", &secondary_slot);
     if (rc) {
         return BOOT_SWAP_TYPE_PANIC;
     }