AuditLog: Improve code quality

This patch fix the following things:
* add const keyword when mutability is not required
* cast unused parameters to void to be more explicit
* add parenthesis for better readability of some expressions
* check the returning value of functions
* avoid implicit casting by using matching types

Change-Id: Ia3d8dfb34d6a0bfb36f0cfca1fe6db9a1820ca33
Signed-off-by: Hugues de Valon <hugues.devalon@arm.com>
diff --git a/interface/include/audit_wrappers.h b/interface/include/audit_wrappers.h
index ec98144..2e3f62e 100644
--- a/interface/include/audit_wrappers.h
+++ b/interface/include/audit_wrappers.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -53,8 +53,8 @@
  *
  */
 enum psa_audit_err audit_core_retrieve_record_wrapper(
-                                const struct audit_core_retrieve_input *input_s,
-                                   struct audit_core_retrieve_output *output_s);
+                             const struct audit_core_retrieve_input *input_s,
+                             const struct audit_core_retrieve_output *output_s);
 
 #ifdef __cplusplus
 }
diff --git a/interface/src/tfm_audit_api.c b/interface/src/tfm_audit_api.c
index 2a69582..0770bd0 100644
--- a/interface/src/tfm_audit_api.c
+++ b/interface/src/tfm_audit_api.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -70,5 +70,6 @@
      * of the Non-Secure interface, always directly return an error without
      * routing the call to TF-M in the Secure world
      */
+    (void)record;
     return PSA_AUDIT_ERR_NOT_SUPPORTED;
 }
diff --git a/secure_fw/services/audit_logging/audit_core.c b/secure_fw/services/audit_logging/audit_core.c
index 49797a3..76985ef 100644
--- a/secure_fw/services/audit_logging/audit_core.c
+++ b/secure_fw/services/audit_logging/audit_core.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -87,14 +87,6 @@
 #define LOG_SIZE (1024)
 
 /*!
- * \def MIN(a,b)
- *
- * \brief A standard MIN macro
- *
- */
-#define MIN(a,b) (((a) < (b)) ? (a) : (b))
-
-/*!
  * \var log_buffer
  *
  * \brief The private buffer containing the the log in memory
@@ -360,7 +352,8 @@
 {
     struct log_hdr *hdr = NULL;
     struct log_tlr *tlr = NULL;
-    uint32_t size, idx;
+    uint32_t size;
+    uint8_t idx;
 
     /* Get the size from the record */
     size = record->size;
@@ -381,9 +374,11 @@
     hdr->partition_id = partition_id;
 
     /* Copy the record into the scratch buffer */
-    audit_memcpy( (const uint8_t *) record,
+    if (audit_memcpy( (const uint8_t *) record,
                   size+4,
-                  (uint8_t *) &(hdr->size) );
+                  (uint8_t *) &(hdr->size) ) != PSA_AUDIT_ERR_SUCCESS) {
+        return PSA_AUDIT_ERR_FAILURE;
+    }
 
     /* FIXME: The MAC here is just a dummy value for prototyping. It will be
      *        filled by a call to the crypto interface directly when available.
@@ -582,12 +577,14 @@
     }
 
     /* Get the size in bytes and num of elements present in the log */
-    audit_core_get_info(&num_items, &stored_size);
+    if (audit_core_get_info(&num_items, &stored_size) !=
+                                                        PSA_AUDIT_ERR_SUCCESS) {
+        return PSA_AUDIT_ERR_FAILURE;
+    }
 
     if (num_items == 0) {
 
         start_pos = 0;
-        stop_pos = COMPUTE_LOG_ENTRY_SIZE(size) - 1;
 
     } else {
 
@@ -600,14 +597,20 @@
     }
 
     /* Format the scratch buffer with the complete log item */
-    audit_format_buffer(record, partition_id, &scratch_buffer[0]);
+    if (audit_format_buffer(record, partition_id, &scratch_buffer[0])
+                                                     != PSA_AUDIT_ERR_SUCCESS) {
+        return PSA_AUDIT_ERR_FAILURE;
+    }
 
     /* TODO: At this point, encryption should be called if supported */
 
     /* Do the copy of the log item to be added in the log */
-    audit_buffer_copy( (const uint8_t *) &scratch_buffer[0],
+    if (audit_buffer_copy( (const uint8_t *) &scratch_buffer[0],
                        COMPUTE_LOG_ENTRY_SIZE(size),
-                       (uint8_t *) &log_buffer[start_pos] );
+                       (uint8_t *) &log_buffer[start_pos] )
+                                                     != PSA_AUDIT_ERR_SUCCESS) {
+        return PSA_AUDIT_ERR_FAILURE;
+    }
 
     /* Retrieve current log state */
     first_el_idx = log_state.first_el_idx;
diff --git a/secure_fw/services/audit_logging/audit_wrappers.c b/secure_fw/services/audit_logging/audit_wrappers.c
index 5640e5e..ec89cde 100644
--- a/secure_fw/services/audit_logging/audit_wrappers.c
+++ b/secure_fw/services/audit_logging/audit_wrappers.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Arm Limited. All rights reserved.
+ * Copyright (c) 2018-2019, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -16,8 +16,8 @@
 
 /*!@{*/
 enum psa_audit_err audit_core_retrieve_record_wrapper(
-                                const struct audit_core_retrieve_input *input_s,
-                                    struct audit_core_retrieve_output *output_s)
+                              const struct audit_core_retrieve_input *input_s,
+                              const struct audit_core_retrieve_output *output_s)
 {
     return audit_core_retrieve_record(input_s->record_index,
                                       input_s->buffer_size,