T398: Source cleanup for tool chain integration

This is a code cleanup to improve portability.

Specific issues addressed:
- Added type casts to (void *) here and there
- Changed non-standard \e escapes to \033
- Added cmake function to handle preinclude
- Changed a few Image$$ references to make use of the REGION_DECLARE
  macro
- Reordered code slightly to avoid the need for a "void *rangeptr"
  variable
- Changed compile time check typedef "err_msg" to avoid declaring
  zero sized array, which is not standards compliant. It will now
  either be -1 (error) or 1 (ok), not -1 and 0
- Reordered the *nfsptr_t typedef to make the cmse_nonsecure_call
  standards compliant
- Added null tests to both secure and non_secure suites to avoid
  defining zero length array. Also use this to find end of list
- Only define __stdout for ARMCLANG builds and conditionalize ns printf
  output for ARMCLANG/GCC/IAR
- Cleaned up some enum type mismatches
- Changed non standard EINVAL error return to -1. The value was only
  checked against 0 anyway
- Added type cast for conversion from float to int

Have tested with IAR, which starts and runs the idle thread. Changes
related to this is not included in this commit.

       Author: Thomas Tornblom <thomas.tornblom@iar.com>
       Signed-off-by: Thomas Tornblom <thomas.tornblom@iar.com>

       Note: Sign off authority needs to adhere to the [DCO](./dco.txt)
       rules.

Change-Id: I3e5229c0777623b128474af0311020ccacc1b797
diff --git a/secure_fw/core/tfm_core.c b/secure_fw/core/tfm_core.c
index fc060f3..dd65cd5 100644
--- a/secure_fw/core/tfm_core.c
+++ b/secure_fw/core/tfm_core.c
@@ -195,8 +195,8 @@
 #ifndef TFM_PSA_API
     tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_RUNNING);
 
-    extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
-    uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base;
+    REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base)[];
+    uint32_t psp_stack_bottom = (uint32_t)REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
 
     __set_PSPLIM(psp_stack_bottom);
 
diff --git a/secure_fw/core/tfm_func_api.c b/secure_fw/core/tfm_func_api.c
index aeaf5f2..2edb3b6 100644
--- a/secure_fw/core/tfm_func_api.c
+++ b/secure_fw/core/tfm_func_api.c
@@ -497,8 +497,8 @@
             (struct tfm_exc_stack_t *)ret_part_data->stack_ptr);
         *excReturn = ret_part_data->lr;
         __set_PSP(ret_part_data->stack_ptr);
-        extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
-        uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base;
+        REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base)[];
+        uint32_t psp_stack_bottom = (uint32_t)REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
        __set_PSPLIM(psp_stack_bottom);
 
         /* FIXME: The condition should be removed once all the secure service
@@ -829,7 +829,6 @@
     uint32_t running_partition_flags =
             tfm_spm_partition_get_flags(running_partition_idx);
     int32_t flags = 0;
-    void *rangeptr;
 
     if (!(running_partition_flags & SPM_PART_FLAG_APP_ROT) || (size == 0)) {
         /* This handler should only be called from a secure partition. */
@@ -848,16 +847,14 @@
     }
 
     /* Check if partition access to address would fail */
-    rangeptr = cmse_check_address_range((void *)ptr, size, flags);
-
-    /* Get regions associated with address */
-    cmse_address_info_t addr_info = cmse_TT((void *)ptr);
-
-    if (rangeptr == NULL) {
+    if (cmse_check_address_range((void *)ptr, size, flags) == NULL) {
         svc_args[0] = TFM_ERROR_INVALID_PARAMETER;
         return;
     }
 
+    /* Get regions associated with address */
+    cmse_address_info_t addr_info = cmse_TT((void *)ptr);
+
     if (addr_info.flags.secure) {
 #if TFM_LVL == 1
         /* For privileged partition execution, all secure data memory is
diff --git a/secure_fw/core/tfm_handler.c b/secure_fw/core/tfm_handler.c
index 10a91a56..3195634 100644
--- a/secure_fw/core/tfm_handler.c
+++ b/secure_fw/core/tfm_handler.c
@@ -203,7 +203,7 @@
         break;
 #endif
     case TFM_SVC_PRINT:
-        printf("\e[1;34m[Sec Thread] %s\e[0m\r\n", (char *)svc_args[0]);
+        printf("\033[1;34m[Sec Thread] %s\033[0m\r\n", (char *)svc_args[0]);
         break;
     case TFM_SVC_GET_BOOT_DATA:
         tfm_core_get_boot_data_handler(svc_args);
diff --git a/secure_fw/core/tfm_internal.h b/secure_fw/core/tfm_internal.h
index 6273478..dad36ad 100644
--- a/secure_fw/core/tfm_internal.h
+++ b/secure_fw/core/tfm_internal.h
@@ -17,7 +17,7 @@
  * Registers will be cleared before branching so that no information leaks
  * from secure to non-secure world.
  */
-typedef void __attribute__((cmse_nonsecure_call)) (*nsfptr_t) (void);
+typedef void (*nsfptr_t) (void) __attribute__((cmse_nonsecure_call));
 
 extern nsfptr_t ns_entry;
 
diff --git a/secure_fw/ns_callable/tfm_audit_veneers.c b/secure_fw/ns_callable/tfm_audit_veneers.c
index a48ec87..7256482 100644
--- a/secure_fw/ns_callable/tfm_audit_veneers.c
+++ b/secure_fw/ns_callable/tfm_audit_veneers.c
@@ -17,7 +17,7 @@
                                     struct audit_core_retrieve_output *output_s)
 {
     TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID,
-                         audit_core_retrieve_record_wrapper,
+                         (void *) audit_core_retrieve_record_wrapper,
                          input_s, output_s, 0, 0);
 }
 
@@ -25,7 +25,7 @@
 enum psa_audit_err tfm_audit_veneer_add_record(
                                           const struct psa_audit_record *record)
 {
-    TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, audit_core_add_record,
+    TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, (void *) audit_core_add_record,
                          record, 0, 0, 0);
 }
 
@@ -33,7 +33,7 @@
 enum psa_audit_err tfm_audit_veneer_get_info(uint32_t *num_records,
                                              uint32_t *size)
 {
-    TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, audit_core_get_info,
+    TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, (void *) audit_core_get_info,
                          num_records, size, 0, 0);
 }
 
@@ -41,7 +41,7 @@
 enum psa_audit_err tfm_audit_veneer_get_record_info(const uint32_t record_index,
                                                     uint32_t *size)
 {
-    TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, audit_core_get_record_info,
+    TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, (void *) audit_core_get_record_info,
                          record_index, size, 0, 0);
 }
 
@@ -50,6 +50,6 @@
                                                   const uint8_t *token,
                                                   const uint32_t token_size)
 {
-    TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, audit_core_delete_record,
+    TFM_CORE_SFN_REQUEST(TFM_SP_AUDIT_LOG_ID, (void *) audit_core_delete_record,
                          record_index, token, token_size, 0);
 }
diff --git a/secure_fw/ns_callable/tfm_veneers.c b/secure_fw/ns_callable/tfm_veneers.c
index 3effa95..2ba8a2c 100644
--- a/secure_fw/ns_callable/tfm_veneers.c
+++ b/secure_fw/ns_callable/tfm_veneers.c
@@ -118,7 +118,7 @@
                                          size_t out_len) \
     { \
         TFM_CORE_IOVEC_SFN_REQUEST(partition_name##_ID, \
-                                   sfn_name, \
+                                   (void *) sfn_name, \
                                    in_vec, in_len, out_vec, out_len); \
     }
 
diff --git a/secure_fw/ns_callable/tfm_veneers.c.template b/secure_fw/ns_callable/tfm_veneers.c.template
index 828a8c4..b58b2f3 100644
--- a/secure_fw/ns_callable/tfm_veneers.c.template
+++ b/secure_fw/ns_callable/tfm_veneers.c.template
@@ -32,7 +32,7 @@
                                          size_t out_len) \
     { \
         TFM_CORE_IOVEC_SFN_REQUEST(partition_name##_ID, \
-                                   sfn_name, \
+                                   (void *) sfn_name, \
                                    in_vec, in_len, out_vec, out_len); \
     }
 
diff --git a/secure_fw/services/secure_storage/sst_utils.h b/secure_fw/services/secure_storage/sst_utils.h
index 6a3662f..b03b6c7 100644
--- a/secure_fw/services/secure_storage/sst_utils.h
+++ b/secure_fw/services/secure_storage/sst_utils.h
@@ -39,7 +39,7 @@
  *          "... error: 'err_msg' declared as an array with a negative size"
  */
 #define SST_UTILS_BOUND_CHECK(err_msg, data_size, data_buf_size) \
-typedef char err_msg[(data_size <= data_buf_size) - 1]
+typedef char err_msg[(data_size <= data_buf_size)*2 - 1]
 
 /**
  * \brief Macro to get the number of bytes aligned with the