Build: Revert hotfix and fix BL2 test issues

- Revert all hotfix changes
- Move PSA api test cmake from secure_fw into app cmakelists
- Change log from using DOMAIN_NS to USE_SP_LOG and invert ifdefs
- Update BL2 tests to avoid corrupting images in various scenarios
- Standardise test skip colour and fix colour name typo

Change-Id: I10a1d7ee7123f452cf56eb29bc255a4f44b7b795
Signed-off-by: Raef Coles <raef.coles@arm.com>
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 8709d92..e498c75 100755
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -21,6 +21,11 @@
         framework
 )
 
+target_link_libraries(tfm_test_framework_common
+    INTERFACE
+        tfm_log_interface
+)
+
 if(TEST_FRAMEWORK_S OR TEST_FRAMEWORK_NS)
     add_subdirectory(secure_fw)
 endif()
diff --git a/test/bl2/mcuboot/CMakeLists.txt b/test/bl2/mcuboot/CMakeLists.txt
index 3ba5b85..f4472ee 100644
--- a/test/bl2/mcuboot/CMakeLists.txt
+++ b/test/bl2/mcuboot/CMakeLists.txt
@@ -22,13 +22,7 @@
 target_link_libraries(mcuboot_tests
     PUBLIC
         tfm_test_framework_common
-        tfm_log
     PRIVATE
         platform_bl2
         mcuboot_test_suite_integration
 )
-
-target_compile_definitions(mcuboot_tests
-    PUBLIC
-        TEST_BL2
-)
diff --git a/test/bl2/mcuboot/suites/integration/CMakeLists.txt b/test/bl2/mcuboot/suites/integration/CMakeLists.txt
index d62b752..6505bd4 100644
--- a/test/bl2/mcuboot/suites/integration/CMakeLists.txt
+++ b/test/bl2/mcuboot/suites/integration/CMakeLists.txt
@@ -27,8 +27,3 @@
         tfm_log
         platform_bl2
 )
-
-target_compile_definitions(mcuboot_test_suite_integration
-    PUBLIC
-        TEST_BL2
-)
diff --git a/test/bl2/mcuboot/suites/integration/mcuboot_integration_tests.c b/test/bl2/mcuboot/suites/integration/mcuboot_integration_tests.c
index decbbf4..baf7394 100644
--- a/test/bl2/mcuboot/suites/integration/mcuboot_integration_tests.c
+++ b/test/bl2/mcuboot/suites/integration/mcuboot_integration_tests.c
@@ -125,23 +125,51 @@
 static int test_setup(int *original_image_idx)
 {
     int rc;
-    struct image_header hdr;
+    struct image_header hdr_0;
+    struct image_header hdr_1;
 
-    rc = read_image_header(0, &hdr);
+    rc = read_image_header(0, &hdr_0);
     if (rc) {
-        return rc;
+        return 1;
     }
 
-    /* If there's a valid image in slot 0 copy that to slot 1 */
-    if (hdr.ih_magic == IMAGE_MAGIC) {
+    rc = read_image_header(1, &hdr_1);
+    if (rc) {
+        return 1;
+    }
+
+    if (hdr_0.ih_magic == IMAGE_MAGIC && hdr_1.ih_magic == IMAGE_MAGIC) {
+        /* If there are images in both slots, it's not reasonable to run the
+         * test as we cannot restore the original state
+         */
+        printf_set_color(MAGENTA);
+        TEST_LOG("%s %s", "Cannot execute test without overwriting one slot.",
+                 "The test execution was SKIPPED.\r\n");
+        printf_set_color(DEFAULT);
+        return 2;
+    } else if (hdr_0.ih_magic == IMAGE_MAGIC) {
+        /* If there's a valid image in slot 0 copy that to slot 1 */
         *original_image_idx = 0;
-        return copy_image_to_slot(0, 1);
-    } else {
+        rc = copy_image_to_slot(0, 1);
+        if (rc) {
+            return 1;
+        } else {
+            return 0;
+        }
+    } else if (hdr_1.ih_magic == IMAGE_MAGIC) {
         /* Else copy slot 1 to slot 0. We assume that one of the slots has a
          * valid image.
          */
         *original_image_idx = 1;
-        return copy_image_to_slot(1, 0);
+        rc = copy_image_to_slot(1, 0);
+        if (rc) {
+            return 1;
+        } else {
+            return 0;
+        }
+    } else {
+        /* No valid images are loaded, error */
+        return 1;
     }
 }
 
@@ -156,14 +184,30 @@
     header->tlv_tot_len = SHARED_DATA_HEADER_SIZE;
 }
 
-static int test_teardown(int original_image_idx)
+static int test_teardown(int test_image_idx)
 {
     int rc;
-    int test_image_idx;
     const struct flash_area *fap;
+#if !(defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOADING))
+    struct image_header hdr;
+#endif /* !(defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOADING)) */
+
+#if !(defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOADING))
+    /* Copy modes will have now moved the good image into slot 0. Copy the image
+     * back to restore starting state.
+     */
+    if (test_image_idx == 0) {
+        rc = copy_image_to_slot(0, 1);
+        if (rc) {
+            return rc;
+        }
+
+        rc = read_image_header(0, &hdr);
+        rc = write_image_header(1, &hdr);
+    }
+#endif /* !(defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOADING)) */
 
     /* Erase the test image */
-    test_image_idx = original_image_idx == 0 ? 1 : 0;
     rc = flash_area_open(flash_area_id_from_image_slot(test_image_idx), &fap);
     if (rc) {
         return rc;
@@ -181,27 +225,34 @@
 {
     int rc;
     int original_image_idx = 0;
+    int test_image_idx;
     struct image_header hdr;
     struct boot_rsp rsp;
 
     rc = test_setup(&original_image_idx);
-    if (rc) {
+    if (rc == 2) {
+        /* Skipped tests are treated as a success */
+        ret->val = TEST_PASSED;
+        return;
+    } else if (rc != 0) {
         TEST_FAIL("Failed to setup test");
-        goto out;
+        return;
     }
 
-    rc = read_image_header(0, &hdr);
+    rc = read_image_header(original_image_idx, &hdr);
     if (rc) {
         TEST_FAIL("Failed to read image header");
         goto out;
     }
 
+    test_image_idx = (original_image_idx + 1) % 2;
+
     /* Increasing the version both causes the image to boot preferentially and
      * also invalidates the signature
      */
     hdr.ih_ver.iv_major += 1;
 
-    rc = write_image_header(1, &hdr);
+    rc = write_image_header(test_image_idx, &hdr);
     if (rc) {
         TEST_FAIL("Failed to write image header");
         goto out;
@@ -216,7 +267,7 @@
     ret->val = TEST_PASSED;
 
 out:
-    rc = test_teardown(original_image_idx);
+    rc = test_teardown(test_image_idx);
     if (rc) {
         TEST_FAIL("Failed to teardown test");
     }
diff --git a/test/framework/test_framework_helpers.h b/test/framework/test_framework_helpers.h
index 1cec7e0..beb6197 100644
--- a/test/framework/test_framework_helpers.h
+++ b/test/framework/test_framework_helpers.h
@@ -22,7 +22,7 @@
     GREEN   = 32,
     YELLOW  = 33,
     BLUE    = 34,
-    MAGENDA = 35,
+    MAGENTA = 35,
     CYAN    = 36,
     WHITE   = 37,
 };
diff --git a/test/framework/test_log.h b/test/framework/test_log.h
index 801d538..1a80741 100644
--- a/test/framework/test_log.h
+++ b/test/framework/test_log.h
@@ -8,21 +8,21 @@
 #ifndef __TEST_LOG_H__
 #define __TEST_LOG_H__
 
-#if (DOMAIN_NS == 1) || defined(TEST_BL2)
-#include "tfm_log_raw.h"
-#else
+#ifdef USE_SP_LOG
 #include "tfm_sp_log.h"
-#endif
+#else
+#include "tfm_log_raw.h"
+#endif /* USE_SP_LOG */
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-#if (DOMAIN_NS == 1) || defined(TEST_BL2)
-#define TEST_LOG(...) tfm_log_printf(__VA_ARGS__)
-#else
+#ifdef USE_SP_LOG
 #define TEST_LOG(...) tfm_sp_log_printf(__VA_ARGS__)
-#endif
+#else
+#define TEST_LOG(...) tfm_log_printf(__VA_ARGS__)
+#endif /* USE_SP_LOG */
 
 #ifdef __cplusplus
 }
diff --git a/test/secure_fw/secure_tests.cmake b/test/secure_fw/secure_tests.cmake
index ffb7b6b..26debd1 100644
--- a/test/secure_fw/secure_tests.cmake
+++ b/test/secure_fw/secure_tests.cmake
@@ -45,6 +45,11 @@
         tfm_sp_log_raw
 )
 
+target_compile_definitions(tfm_test_framework_s
+    INTERFACE
+        USE_SP_LOG
+)
+
 target_sources(tfm_s_tests
     INTERFACE
         ${CMAKE_CURRENT_SOURCE_DIR}/secure_suites.c
diff --git a/test/secure_fw/suites/crypto/crypto_tests_common.c b/test/secure_fw/suites/crypto/crypto_tests_common.c
index 47503df..4e2c2bc 100644
--- a/test/secure_fw/suites/crypto/crypto_tests_common.c
+++ b/test/secure_fw/suites/crypto/crypto_tests_common.c
@@ -431,8 +431,10 @@
      */
 #ifdef CRYPTO_HW_ACCELERATOR_CC312
     if (alg == PSA_ALG_CFB) {
+        printf_set_color(MAGENTA);
         TEST_LOG("%s %s", "The CC312 does not support CFB mode.",
                  "The test execution was SKIPPED.\r\n");
+        printf_set_color(DEFAULT);
         return;
     }
 #endif /* CRYPTO_HW_ACCELERATOR_CC312 */