Core: Improve code quality

This patch fixes the following things:
* avoid implicit casting by using matching types or casting
* avoid implicit casting between enum tfm_buffer_share_region_e and
uint32_t types by replacing the enumerated type with uint32_t and its
constants with preprocessor constants
* make explicit that the return value of a function is not used
* remove function return type if not needed
* replace preprocessor if condition by ifdef if only definition is
  checked
* replace enumerated types not used by preprocessor constants

Change-Id: Ib718937f89631cae2212de1fc748c48461b683d0
Signed-off-by: Hugues de Valon <hugues.devalon@arm.com>
diff --git a/secure_fw/core/ipc/tfm_spm.c b/secure_fw/core/ipc/tfm_spm.c
index 3aa43ca..2f6598a 100644
--- a/secure_fw/core/ipc/tfm_spm.c
+++ b/secure_fw/core/ipc/tfm_spm.c
@@ -450,7 +450,7 @@
                          enum tfm_memory_access_e access,
                          uint32_t privileged)
 {
-    int32_t err;
+    enum tfm_status_e err;
 
     /* If len is zero, this indicates an empty buffer and base is ignored */
     if (len == 0) {
diff --git a/secure_fw/core/tfm_boot_data.c b/secure_fw/core/tfm_boot_data.c
index c4f5882..bd3ec35 100644
--- a/secure_fw/core/tfm_boot_data.c
+++ b/secure_fw/core/tfm_boot_data.c
@@ -91,7 +91,7 @@
                                        2); /* Check 4 bytes alignment */
     if (!res) {
         /* Not in accessible range, return error */
-        args[0] = TFM_ERROR_INVALID_PARAMETER;
+        args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
         return;
     }
 #else
@@ -104,7 +104,7 @@
     if (tfm_memory_check(buf_start, buf_size, false, TFM_MEMORY_ACCESS_RW,
         privileged) != IPC_SUCCESS) {
         /* Not in accessible range, return error */
-        args[0] = TFM_ERROR_INVALID_PARAMETER;
+        args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
         return;
     }
 #endif
@@ -112,7 +112,7 @@
     /* FixMe: Check whether caller has access right to given tlv_major_type */
 
     if (is_boot_data_valid != BOOT_DATA_VALID) {
-        args[0] = TFM_ERROR_INVALID_PARAMETER;
+        args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
         return;
     }
 
@@ -123,7 +123,7 @@
 
     /* Add header to output buffer as well */
     if (buf_size < SHARED_DATA_HEADER_SIZE) {
-        args[0] = TFM_ERROR_INVALID_PARAMETER;
+        args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
         return;
     } else {
         boot_data = (struct tfm_boot_data *)buf_start;
@@ -137,22 +137,22 @@
      */
     for (; offset < tlv_end; offset += tlv_entry.tlv_len) {
         /* Create local copy to avoid unaligned access */
-        tfm_memcpy(&tlv_entry,
-                   (const void *)offset,
-                   SHARED_DATA_ENTRY_HEADER_SIZE);
+        (void)tfm_memcpy(&tlv_entry,
+                         (const void *)offset,
+                         SHARED_DATA_ENTRY_HEADER_SIZE);
         if (GET_MAJOR(tlv_entry.tlv_type) == tlv_major) {
             /* Check buffer overflow */
             if (((ptr - buf_start) + tlv_entry.tlv_len) > buf_size) {
-                args[0] = TFM_ERROR_INVALID_PARAMETER;
+                args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
                 return;
             }
 
-            tfm_memcpy(ptr, (const void *)offset, tlv_entry.tlv_len);
+            (void)tfm_memcpy(ptr, (const void *)offset, tlv_entry.tlv_len);
 
             ptr += tlv_entry.tlv_len;
             boot_data->header.tlv_tot_len += tlv_entry.tlv_len;
         }
     }
-    args[0] = TFM_SUCCESS;
+    args[0] = (uint32_t)TFM_SUCCESS;
     return;
 }
diff --git a/secure_fw/core/tfm_core.c b/secure_fw/core/tfm_core.c
index bb0c978..a133b40 100644
--- a/secure_fw/core/tfm_core.c
+++ b/secure_fw/core/tfm_core.c
@@ -143,7 +143,7 @@
     return 0;
 }
 
-static int32_t tfm_core_set_secure_exception_priorities(void)
+static void tfm_core_set_secure_exception_priorities(void)
 {
     tfm_arch_prioritize_secure_exception();
 
@@ -166,8 +166,6 @@
      * priority level configurable on the platform, just below 0x80.
      */
     NVIC_SetPriority(PendSV_IRQn, (1 << (__NVIC_PRIO_BITS - 1)) - 1);
-
-    return TFM_SUCCESS;
 }
 
 void tfm_core_spm_request_handler(const struct tfm_exc_stack_t *svc_ctx)
@@ -181,10 +179,10 @@
         /* FixMe: this is a placeholder for checks to be performed before
          * allowing execution of reset
          */
-        *res_ptr = TFM_SUCCESS;
+        *res_ptr = (uint32_t)TFM_SUCCESS;
         break;
     default:
-        *res_ptr = TFM_ERROR_INVALID_PARAMETER;
+        *res_ptr = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
     }
 }
 
diff --git a/secure_fw/core/tfm_func_api.c b/secure_fw/core/tfm_func_api.c
index f526b25..1c39820 100644
--- a/secure_fw/core/tfm_func_api.c
+++ b/secure_fw/core/tfm_func_api.c
@@ -162,7 +162,7 @@
  * \return Return /ref TFM_SUCCESS if the iovec parameters are valid, error code
  *         otherwise as in /ref tfm_status_e
  */
-static int32_t tfm_core_check_sfn_parameters(
+static enum tfm_status_e tfm_core_check_sfn_parameters(
                                            const struct tfm_sfn_req_s *desc_ptr)
 {
     struct psa_invec *in_vec = (psa_invec *)desc_ptr->args[0];
@@ -288,9 +288,8 @@
  *
  * \return \ref TFM_SUCCESS if the check passes, error otherwise.
  */
-static int32_t check_partition_state(
-                                   enum spm_part_state_t curr_partition_state,
-                                   enum spm_part_state_t caller_partition_state)
+static enum tfm_status_e check_partition_state(uint32_t curr_partition_state,
+                                               uint32_t caller_partition_state)
 {
     if (caller_partition_state != SPM_PARTITION_STATE_RUNNING) {
         /* Calling partition from non-running state (e.g. during handling IRQ)
@@ -320,8 +319,8 @@
  *
  * \return \ref TFM_SUCCESS if the check passes, error otherwise.
  */
-static int32_t check_irq_partition_state(
-                                   enum spm_part_state_t called_partition_state)
+static enum tfm_status_e check_irq_partition_state(
+                                                uint32_t called_partition_state)
 {
     if (called_partition_state == SPM_PARTITION_STATE_IDLE ||
         called_partition_state == SPM_PARTITION_STATE_RUNNING ||
@@ -358,10 +357,11 @@
     return iovec_args;
 }
 
-static int32_t tfm_start_partition(const struct tfm_sfn_req_s *desc_ptr,
-                                                             uint32_t excReturn)
+static enum tfm_status_e tfm_start_partition(
+                                           const struct tfm_sfn_req_s *desc_ptr,
+                                           uint32_t excReturn)
 {
-    int32_t res;
+    enum tfm_status_e res;
     uint32_t caller_partition_idx = desc_ptr->caller_part_idx;
     const struct spm_partition_runtime_data_t *curr_part_data;
     const struct spm_partition_runtime_data_t *caller_part_data;
@@ -369,8 +369,8 @@
     register uint32_t partition_idx;
     uint32_t psp;
     uint32_t partition_psp, partition_psplim;
-    enum spm_part_state_t partition_state;
-    enum spm_part_state_t caller_partition_state;
+    uint32_t partition_state;
+    uint32_t caller_partition_state;
     uint32_t partition_flags;
     struct tfm_exc_stack_t *svc_ctx;
     uint32_t caller_partition_id;
@@ -461,8 +461,10 @@
      * this way partitions always get default access to input buffers
      */
     /* FixMe: return value/error handling TBD */
-    tfm_spm_partition_set_share(partition_idx, desc_ptr->ns_caller ?
-        TFM_BUFFER_SHARE_NS_CODE : TFM_BUFFER_SHARE_SCRATCH);
+    (void)tfm_spm_partition_set_share(
+               partition_idx,
+               desc_ptr->ns_caller ?
+                           TFM_BUFFER_SHARE_NS_CODE : TFM_BUFFER_SHARE_SCRATCH);
 
 #if TFM_LVL == 1
     /* In level one, only switch context and return from exception if in
@@ -501,7 +503,7 @@
     return TFM_SUCCESS;
 }
 
-static int32_t tfm_start_partition_for_irq_handling(
+static enum tfm_status_e tfm_start_partition_for_irq_handling(
                                                 uint32_t excReturn,
                                                 struct tfm_exc_stack_t *svc_ctx)
 {
@@ -509,13 +511,13 @@
     sfn_t unpriv_handler = (sfn_t)svc_ctx->R1;
     uint32_t irq_signal = svc_ctx->R2;
     uint32_t irq_line = svc_ctx->R3;
-    int32_t res;
+    enum tfm_status_e res;
     uint32_t psp = __get_PSP();
 #if (TFM_LVL != 1)
     uint32_t handler_partition_psplim;
 #endif
     uint32_t handler_partition_psp;
-    enum spm_part_state_t handler_partition_state;
+    uint32_t handler_partition_state;
     uint32_t interrupted_partition_idx =
             tfm_spm_partition_get_running_partition_idx();
     const struct spm_partition_runtime_data_t *handler_part_data;
@@ -584,7 +586,7 @@
     return TFM_SUCCESS;
 }
 
-static int32_t tfm_return_from_partition(uint32_t *excReturn)
+static enum tfm_status_e tfm_return_from_partition(uint32_t *excReturn)
 {
     uint32_t current_partition_idx =
             tfm_spm_partition_get_running_partition_idx();
@@ -711,7 +713,8 @@
     return TFM_SUCCESS;
 }
 
-static int32_t tfm_return_from_partition_irq_handling(uint32_t *excReturn)
+static enum tfm_status_e tfm_return_from_partition_irq_handling(
+                                                            uint32_t *excReturn)
 {
     uint32_t handler_partition_idx =
             tfm_spm_partition_get_running_partition_idx();
@@ -784,7 +787,8 @@
     return TFM_SUCCESS;
 }
 
-static int32_t tfm_check_sfn_req_integrity(const struct tfm_sfn_req_s *desc_ptr)
+static enum tfm_status_e tfm_check_sfn_req_integrity(
+                                           const struct tfm_sfn_req_s *desc_ptr)
 {
     if ((desc_ptr == NULL) ||
         (desc_ptr->sp_id == 0) ||
@@ -795,7 +799,7 @@
     return TFM_SUCCESS;
 }
 
-static int32_t tfm_core_check_sfn_req_rules(
+static enum tfm_status_e tfm_core_check_sfn_req_rules(
         const struct tfm_sfn_req_s *desc_ptr)
 {
     /* Check partition idx validity */
@@ -837,10 +841,10 @@
 #endif
 }
 
-int32_t tfm_core_sfn_request_handler(
+enum tfm_status_e tfm_core_sfn_request_handler(
                              struct tfm_sfn_req_s *desc_ptr, uint32_t excReturn)
 {
-    int32_t res;
+    enum tfm_status_e res;
 
     res = tfm_check_sfn_req_integrity(desc_ptr);
     if (res != TFM_SUCCESS) {
@@ -887,7 +891,7 @@
 #if TFM_LVL == 1
 int32_t tfm_core_sfn_request_thread_mode(struct tfm_sfn_req_s *desc_ptr)
 {
-    int32_t res;
+    enum tfm_status_e res;
     int32_t *args;
     int32_t retVal;
 
@@ -895,7 +899,7 @@
         res = tfm_core_check_sfn_parameters(desc_ptr);
         if (res != TFM_SUCCESS) {
             /* The sanity check of iovecs failed. */
-            return res;
+            return (int32_t)res;
         }
     }
 
@@ -914,7 +918,7 @@
     res = tfm_return_from_partition(NULL);
     if (res == TFM_SUCCESS) {
         /* If unlock successful, pass SS return value to caller */
-        res = retVal;
+        return retVal;
     } else {
         /* Unlock errors indicate ctx database corruption or unknown
          * anomalies. Halt execution
@@ -922,14 +926,14 @@
         ERROR_MSG("Secure API error during unlock!");
         tfm_secure_api_error_handler();
     }
-    return res;
+    return (int32_t)res;
 }
 #endif
 
 void tfm_core_validate_secure_caller_handler(uint32_t *svc_args)
 {
 
-    int32_t res = TFM_ERROR_GENERIC;
+    enum tfm_status_e res = TFM_ERROR_GENERIC;
     uint32_t running_partition_idx =
             tfm_spm_partition_get_running_partition_idx();
     const struct spm_partition_runtime_data_t *curr_part_data =
@@ -947,7 +951,7 @@
          * index might not be valid;
          * Partitions are only allowed to run while S domain is locked.
          */
-        svc_args[0] = TFM_ERROR_INVALID_PARAMETER;
+        svc_args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
         return;
     }
 
@@ -955,7 +959,7 @@
     if (caller_partition_flags & SPM_PART_FLAG_APP_ROT) {
         res = TFM_SUCCESS;
     }
-    svc_args[0] = res;
+    svc_args[0] = (uint32_t)res;
 }
 
 int32_t tfm_core_check_buffer_access(uint32_t  partition_idx,
@@ -1033,7 +1037,7 @@
          * index might not be valid;
          * Partitions are only allowed to run while S domain is locked.
          */
-        svc_args[0] = TFM_ERROR_INVALID_PARAMETER;
+        svc_args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
         return;
     }
 
@@ -1046,14 +1050,14 @@
                                        2);
     if (!res) {
         /* Not in accessible range, return error */
-        svc_args[0] = TFM_ERROR_INVALID_PARAMETER;
+        svc_args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
         return;
     }
 
     *((int32_t *)result_ptr_value) = curr_part_data->caller_client_id;
 
     /* Store return value in r0 */
-    svc_args[0] = TFM_SUCCESS;
+    svc_args[0] = (uint32_t)TFM_SUCCESS;
 }
 
 void tfm_core_memory_permission_check_handler(uint32_t *svc_args)
@@ -1063,7 +1067,7 @@
     int32_t access = svc_args[2];
 
     uint32_t max_buf_size, ptr_start, range_limit, range_check = false;
-    int32_t res;
+    enum tfm_status_e res;
     uint32_t running_partition_idx =
             tfm_spm_partition_get_running_partition_idx();
     const struct spm_partition_runtime_data_t *curr_part_data =
@@ -1074,7 +1078,7 @@
 
     if (!(running_partition_flags & SPM_PART_FLAG_APP_ROT) || (size == 0)) {
         /* This handler should only be called from a secure partition. */
-        svc_args[0] = TFM_ERROR_INVALID_PARAMETER;
+        svc_args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
         return;
     }
 
@@ -1082,7 +1086,7 @@
         flags |= CMSE_MPU_UNPRIV;
     }
 
-    if (access == TFM_MEMORY_ACCESS_RW) {
+    if (access == (int32_t)TFM_MEMORY_ACCESS_RW) {
         flags |= CMSE_MPU_READWRITE;
     } else {
         flags |= CMSE_MPU_READ;
@@ -1090,7 +1094,7 @@
 
     /* Check if partition access to address would fail */
     if (cmse_check_address_range((void *)ptr, size, flags) == NULL) {
-        svc_args[0] = TFM_ERROR_INVALID_PARAMETER;
+        svc_args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
         return;
     }
 
@@ -1116,12 +1120,12 @@
         if (!addr_info.flags.sau_region_valid) {
             /* If address is NS, TF-M expects SAU to be configured
              */
-            svc_args[0] = TFM_ERROR_INVALID_PARAMETER;
+            svc_args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER;
             return;
         }
         switch (addr_info.flags.sau_region) {
         case TFM_NS_REGION_CODE:
-            if (access == TFM_MEMORY_ACCESS_RW) {
+            if (access == (int32_t)TFM_MEMORY_ACCESS_RW) {
                 res = TFM_ERROR_INVALID_PARAMETER;
             } else {
                 /* Currently TF-M does not support checks for NS Memory
@@ -1153,7 +1157,7 @@
     }
 
     /* Store return value in r0 */
-    svc_args[0] = res;
+    svc_args[0] = (uint32_t)res;
 }
 
 /* This SVC handler is called if veneer is running in thread mode */
@@ -1188,7 +1192,7 @@
 {
     struct tfm_exc_stack_t *svc_ctx = (struct tfm_exc_stack_t *)svc_args;
 
-    int32_t res;
+    enum tfm_status_e res;
 
     if (excReturn & EXC_RETURN_STACK_PROCESS) {
         /* FixMe: error severity TBD */
@@ -1207,7 +1211,7 @@
 /* This SVC handler is called when sfn returns */
 uint32_t tfm_core_partition_return_handler(uint32_t lr)
 {
-    int32_t res;
+    enum tfm_status_e res;
 
     if (!(lr & EXC_RETURN_STACK_PROCESS)) {
         /* Partition return SVC called with MSP active.
@@ -1221,8 +1225,8 @@
     int32_t retVal = *(int32_t *)__get_PSP();
 
     if (!is_iovec_api_call()) {
-        if ((retVal > TFM_SUCCESS) &&
-            (retVal < TFM_PARTITION_SPECIFIC_ERROR_MIN)) {
+        if ((retVal > (int32_t)TFM_SUCCESS) &&
+            (retVal < (int32_t)TFM_PARTITION_SPECIFIC_ERROR_MIN)) {
             /* Secure function returned a reserved value */
 #ifdef TFM_CORE_DEBUG
             LOG_MSG("Invalid return value from secure partition!");
@@ -1263,7 +1267,7 @@
         tfm_secure_api_error_handler();
     }
 
-    int32_t res;
+    enum tfm_status_e res;
 
     res = tfm_return_from_partition_irq_handling(&lr);
     if (res != TFM_SUCCESS) {
@@ -1284,7 +1288,7 @@
     /* r0 is stored in args[0] in exception stack frame
      * Store input parameter before writing return value to that address
      */
-    enum tfm_buffer_share_region_e share;
+    uint32_t share;
     uint32_t running_partition_idx =
             tfm_spm_partition_get_running_partition_idx();
     const struct spm_partition_runtime_data_t *curr_part_data =
@@ -1305,29 +1309,29 @@
          * Also if the current partition is handling IRQ, the caller partition
          * index might not be valid;
          */
-        *res_ptr = TFM_ERROR_INVALID_PARAMETER;
+        *res_ptr = (int32_t)TFM_ERROR_INVALID_PARAMETER;
         return;
     }
 
     switch (args[0]) {
     case TFM_BUFFER_SHARE_DEFAULT:
         share = (!(caller_partition_flags & SPM_PART_FLAG_APP_ROT)) ?
-            (TFM_BUFFER_SHARE_NS_CODE) : (TFM_BUFFER_SHARE_SCRATCH);
+                            TFM_BUFFER_SHARE_NS_CODE : TFM_BUFFER_SHARE_SCRATCH;
         break;
     case TFM_BUFFER_SHARE_SCRATCH:
     case TFM_BUFFER_SHARE_NS_CODE:
         share = args[0];
         break;
     default:
-        *res_ptr = TFM_ERROR_INVALID_PARAMETER;
+        *res_ptr = (int32_t)TFM_ERROR_INVALID_PARAMETER;
         return;
     }
 
     if (tfm_spm_partition_set_share(running_partition_idx, share) ==
             SPM_ERR_OK) {
-        *res_ptr = TFM_SUCCESS;
+        *res_ptr = (int32_t)TFM_SUCCESS;
     } else {
-        *res_ptr = TFM_ERROR_INVALID_PARAMETER;
+        *res_ptr = (int32_t)TFM_ERROR_INVALID_PARAMETER;
     }
 
     return;
diff --git a/secure_fw/core/tfm_handler.c b/secure_fw/core/tfm_handler.c
index 563b499..4d2fdf2 100644
--- a/secure_fw/core/tfm_handler.c
+++ b/secure_fw/core/tfm_handler.c
@@ -28,7 +28,7 @@
 /* This SVC handler is called when a secure partition requests access to a
  * buffer area
  */
-extern int32_t tfm_core_set_buffer_area_handler(const uint32_t args[]);
+extern void tfm_core_set_buffer_area_handler(const uint32_t args[]);
 #ifdef TFM_PSA_API
 extern void tfm_psa_ipc_request_handler(const uint32_t svc_args[]);
 #endif
diff --git a/secure_fw/core/tfm_nspm.c b/secure_fw/core/tfm_nspm.c
index 6753dba..02be08b 100644
--- a/secure_fw/core/tfm_nspm.c
+++ b/secure_fw/core/tfm_nspm.c
@@ -9,7 +9,7 @@
 #include "secure_utilities.h"
 #include "tfm_arch.h"
 #include "tfm_api.h"
-#if TFM_PSA_API
+#ifdef TFM_PSA_API
 #include "tfm_utils.h"
 #include "tfm_internal.h"
 #endif
diff --git a/secure_fw/core/tfm_secure_api.c b/secure_fw/core/tfm_secure_api.c
index a27aa8a..57e94ef 100644
--- a/secure_fw/core/tfm_secure_api.c
+++ b/secure_fw/core/tfm_secure_api.c
@@ -67,8 +67,9 @@
  * \return TFM_SUCCESS if the region contains the range,
  *         TFM_ERROR_GENERIC otherwise.
  */
-static int32_t check_address_range(const void *p, size_t s,
-                                   uintptr_t region_start, uint32_t region_len)
+static enum tfm_status_e check_address_range(const void *p, size_t s,
+                                             uintptr_t region_start,
+                                             uint32_t region_len)
 {
     int32_t range_in_region;
 
@@ -105,7 +106,8 @@
  * \return TFM_SUCCESS if the partition has access to the memory range,
  *         TFM_ERROR_GENERIC otherwise.
  */
-static int32_t has_access_to_region(const void *p, size_t s, int flags)
+static enum tfm_status_e has_access_to_region(const void *p, size_t s,
+                                              int flags)
 {
     int32_t range_access_allowed_by_mpu;
 
@@ -149,9 +151,9 @@
     }
 }
 
-int32_t tfm_core_has_read_access_to_region(const void *p, size_t s,
-                                           uint32_t ns_caller,
-                                           uint32_t privileged)
+enum tfm_status_e tfm_core_has_read_access_to_region(const void *p, size_t s,
+                                                     uint32_t ns_caller,
+                                                     uint32_t privileged)
 {
     int flags = CMSE_MPU_READ;
 
@@ -166,9 +168,9 @@
     return has_access_to_region(p, s, flags);
 }
 
-int32_t tfm_core_has_write_access_to_region(void *p, size_t s,
-                                            uint32_t ns_caller,
-                                            uint32_t privileged)
+enum tfm_status_e tfm_core_has_write_access_to_region(void *p, size_t s,
+                                                      uint32_t ns_caller,
+                                                      uint32_t privileged)
 {
     int flags = CMSE_MPU_READWRITE;
 
diff --git a/secure_fw/core/tfm_secure_api.h b/secure_fw/core/tfm_secure_api.h
index 190ba27..e49924b 100644
--- a/secure_fw/core/tfm_secure_api.h
+++ b/secure_fw/core/tfm_secure_api.h
@@ -51,13 +51,12 @@
     uint32_t ns_caller;
 };
 
-enum tfm_buffer_share_region_e {
-    TFM_BUFFER_SHARE_DISABLE,
-    TFM_BUFFER_SHARE_NS_CODE,
-    TFM_BUFFER_SHARE_SCRATCH,
-    TFM_BUFFER_SHARE_PRIV, /* only for TCB in level 2, all in level 1 */
-    TFM_BUFFER_SHARE_DEFAULT,
-};
+#define TFM_BUFFER_SHARE_DISABLE 0U
+#define TFM_BUFFER_SHARE_NS_CODE 1U
+#define TFM_BUFFER_SHARE_SCRATCH 2U
+/* only for TCB in level 2, all in level 1 */
+#define TFM_BUFFER_SHARE_PRIV    3U
+#define TFM_BUFFER_SHARE_DEFAULT 4U
 
 enum tfm_ns_region_e {
     TFM_NS_REGION_CODE = 0,
@@ -73,7 +72,7 @@
     TFM_MEMORY_ACCESS_RW = 2,
 };
 
-extern int32_t tfm_core_set_buffer_area(enum tfm_buffer_share_region_e share);
+extern int32_t tfm_core_set_buffer_area(uint32_t share);
 
 extern int32_t tfm_core_validate_secure_caller(void);
 
@@ -107,9 +106,9 @@
  * \return TFM_SUCCESS if the partition has access to the memory range,
  *         TFM_ERROR_GENERIC otherwise.
  */
-int32_t tfm_core_has_read_access_to_region(const void *p, size_t s,
-                                           uint32_t ns_caller,
-                                           uint32_t privileged);
+enum tfm_status_e tfm_core_has_read_access_to_region(const void *p, size_t s,
+                                                     uint32_t ns_caller,
+                                                     uint32_t privileged);
 
 /**
  * \brief Check whether the current partition has write access to a memory range
@@ -127,9 +126,9 @@
  * \return TFM_SUCCESS if the partition has access to the memory range,
  *         TFM_ERROR_GENERIC otherwise.
  */
-int32_t tfm_core_has_write_access_to_region(void *p, size_t s,
-                                            uint32_t ns_caller,
-                                            uint32_t privileged);
+enum tfm_status_e tfm_core_has_write_access_to_region(void *p, size_t s,
+                                                      uint32_t ns_caller,
+                                                      uint32_t privileged);
 
 void tfm_enable_irq(psa_signal_t irq_signal);
 void tfm_disable_irq(psa_signal_t irq_signal);
@@ -199,7 +198,7 @@
     desc.iovec_api = iovec_api;
     if (__get_active_exc_num() != EXC_NUM_THREAD_MODE) {
         /* FixMe: Error severity TBD */
-        return TFM_ERROR_GENERIC;
+        return (int32_t)TFM_ERROR_GENERIC;
     } else {
 #if TFM_LVL == 1
         if (desc.ns_caller) {
diff --git a/secure_fw/core/tfm_spm_services.c b/secure_fw/core/tfm_spm_services.c
index 3b88301..bd428af 100644
--- a/secure_fw/core/tfm_spm_services.c
+++ b/secure_fw/core/tfm_spm_services.c
@@ -81,7 +81,7 @@
 }
 
 __attribute__((naked))
-int32_t tfm_core_set_buffer_area(enum tfm_buffer_share_region_e share)
+int32_t tfm_core_set_buffer_area(uint32_t share)
 {
     __ASM volatile(
         "SVC    %0\n"
diff --git a/secure_fw/spm/spm_api.c b/secure_fw/spm/spm_api.c
index e138190..9b3f3f2 100644
--- a/secure_fw/spm/spm_api.c
+++ b/secure_fw/spm/spm_api.c
@@ -134,7 +134,7 @@
     part_ptr = &(g_spm_partition_db.partitions[
             g_spm_partition_db.partition_count]);
     part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
-#if TFM_PSA_API
+#ifdef TFM_PSA_API
     part_ptr->static_data.partition_flags = SPM_PART_FLAG_APP_ROT |
                                             SPM_PART_FLAG_IPC;
     part_ptr->static_data.partition_priority = TFM_PRIORITY_LOW;
@@ -206,7 +206,7 @@
             desc.sfn = (sfn_t)part->static_data.partition_init;
             desc.sp_id = part->static_data.partition_id;
             res = tfm_core_sfn_request(&desc);
-            if (res == TFM_SUCCESS) {
+            if (res == (int32_t)TFM_SUCCESS) {
                 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
             } else {
                 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res);
diff --git a/secure_fw/spm/spm_api.h b/secure_fw/spm/spm_api.h
index d707346..565e708 100644
--- a/secure_fw/spm/spm_api.h
+++ b/secure_fw/spm/spm_api.h
@@ -28,21 +28,17 @@
     SPM_ERR_INVALID_CONFIG,
 };
 
-enum spm_part_state_t {
-    SPM_PARTITION_STATE_UNINIT = 0,
-    SPM_PARTITION_STATE_IDLE,
-    SPM_PARTITION_STATE_RUNNING,
-    SPM_PARTITION_STATE_HANDLING_IRQ,
-    SPM_PARTITION_STATE_SUSPENDED,
-    SPM_PARTITION_STATE_BLOCKED,
-    SPM_PARTITION_STATE_CLOSED
-};
+#define SPM_PARTITION_STATE_UNINIT       0
+#define SPM_PARTITION_STATE_IDLE         1
+#define SPM_PARTITION_STATE_RUNNING      2
+#define SPM_PARTITION_STATE_HANDLING_IRQ 3
+#define SPM_PARTITION_STATE_SUSPENDED    4
+#define SPM_PARTITION_STATE_BLOCKED      5
+#define SPM_PARTITION_STATE_CLOSED       6
 
-enum spm_part_flag_mask_t {
-    SPM_PART_FLAG_APP_ROT = 0x01,
-    SPM_PART_FLAG_PSA_ROT = 0x02,
-    SPM_PART_FLAG_IPC     = 0x04
-};
+#define SPM_PART_FLAG_APP_ROT 0x01
+#define SPM_PART_FLAG_PSA_ROT 0x02
+#define SPM_PART_FLAG_IPC     0x04
 
 /**
  * \brief Holds the iovec parameters that are passed to a service
@@ -339,7 +335,12 @@
  * \return Error code \ref spm_err_t
  *
  * \note This function doesn't check if partition_idx is valid.
- * \note share has to have the value set of \ref tfm_buffer_share_region_e
+ * \note share has to have one of the buffer share values:
+ *           - TFM_BUFFER_SHARE_DISABLE
+ *           - TFM_BUFFER_SHARE_NS_CODE
+ *           - TFM_BUFFER_SHARE_SCRATCH
+ *           - TFM_BUFFER_SHARE_PRIV
+ *           - TFM_BUFFER_SHARE_DEFAULT
  */
 enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
                                            uint32_t share);
diff --git a/secure_fw/spm/spm_db.h b/secure_fw/spm/spm_db.h
index 6e6e25c..a3af8be 100644
--- a/secure_fw/spm/spm_db.h
+++ b/secure_fw/spm/spm_db.h
@@ -22,17 +22,13 @@
 #define TFM_PARTITION_TYPE_PSA   "PSA-ROT"
 
 #ifdef TFM_PSA_API
-enum tfm_partition_priority {
-    TFM_PRIORITY_LOW = THRD_PRIOR_LOWEST,
-    TFM_PRIORITY_NORMAL = THRD_PRIOR_MEDIUM,
-    TFM_PRIORITY_HIGH = THRD_PRIOR_HIGHEST,
-};
+#define TFM_PRIORITY_LOW    THRD_PRIOR_LOWEST
+#define TFM_PRIORITY_NORMAL THRD_PRIOR_MEDIUM
+#define TFM_PRIORITY_HIGH   THRD_PRIOR_HIGHEST
 #else
-enum tfm_partition_priority {
-    TFM_PRIORITY_LOW = 0xFF,
-    TFM_PRIORITY_NORMAL = 0x7F,
-    TFM_PRIORITY_HIGH = 0,
-};
+#define TFM_PRIORITY_LOW    0xFF
+#define TFM_PRIORITY_NORMAL 0x7F
+#define TFM_PRIORITY_HIGH   0
 #endif
 
 #define TFM_PRIORITY(LEVEL)      TFM_PRIORITY_##LEVEL