AuditLog: Additional API primitives

-- Add two additional API's to compute the log size
   and remove log items.
-- Amend the log retrieval API to allow specifying
   the index from where to start reading items from
   the log.

Change-Id: Ifdc267806f8b73d4548418f6f632df4a4a376038
Signed-off-by: Antonio de Angelis <antonio.deangelis@arm.com>
diff --git a/interface/include/tfm_log_api.h b/interface/include/tfm_log_api.h
index 24b73b4..6b7d0e6 100644
--- a/interface/include/tfm_log_api.h
+++ b/interface/include/tfm_log_api.h
@@ -17,16 +17,56 @@
 /**
  * \brief Retrieves the audit log
  *
+ * \details The function reads the audit log into the buffer provided.
+ *          If provided buffer size is too small to fit the full log,
+ *          the function will read the maximum number of items in the
+ *          log that fit the available space in the buffer
+ *
  * \param[in]  size     Maximum number of bytes to retrieve from the log
+ * \param[in]  start    Index of element from where to start retrieval
  * \param[out] buffer   Pointer to the buffer that will hold the log
- * \param[out] log_size Pointer to the actual size of the log retrieved
+ * \param[out] info     Pointer to the \ref tfm_log_info structure
+ *                      contained information related to the retrieved
+ *                      portion of the log (size and number of items)
  *
  * \return Returns TFM_LOG_ERR_SUCCESS if retrieval has been completed,
  *         otherwise error as specified in \ref tfm_log_err
+ *
+ * \note If start is equal to TFM_ALG_READ_RECENT, the function will
+ *       retrieve the most recent elements that fit the provided size
  */
 enum tfm_log_err tfm_log_retrieve(uint32_t size,
+                                  int32_t start,
                                   uint8_t *buffer,
-                                  uint32_t *log_size);
+                                  struct tfm_log_info *info);
+
+/**
+ * \brief Gets the log information
+ *
+ * \param[out] info Pointer to the \ref tfm_log_info structure that
+ *                  holds the current log size (both in bytes and items)
+ *
+ * \return Returns TFM_LOG_ERR_SUCCESS if reading has been completed,
+ *         otherwise error as specified in \ref tfm_log_err
+ */
+enum tfm_log_err tfm_log_get_info(struct tfm_log_info *info);
+
+/**
+ * \brief Deletes one or more elements from the head of the log
+ *
+ * \param[in]  num_items Number of elements to be deleted
+ * \param[out] rem_items Pointer to the number of elements removed. This
+ *                       value indicates the number of elements actually
+ *                       removed from the log. In case the number of items
+ *                       stored is less than the number of items requested
+ *                       to remove, this value will reflect the number of
+ *                       items effectively removed.
+ *
+ * \return Returns TFM_LOG_ERR_SUCCESS if removal has been completed,
+ *         otherwise error as specified in \ref tfm_log_err
+ */
+enum tfm_log_err tfm_log_delete_items(uint32_t num_items,
+                                      uint32_t *rem_items);
 
 #ifdef __cplusplus
 }
diff --git a/interface/include/tfm_log_defs.h b/interface/include/tfm_log_defs.h
index ccd779e..5153891 100644
--- a/interface/include/tfm_log_defs.h
+++ b/interface/include/tfm_log_defs.h
@@ -16,8 +16,27 @@
 #include "tfm_api.h"
 #include "limits.h"
 
-/* The return value is shared with the TFM partition status value. The LOG return
- * codes shouldn't overlap with predefined TFM status values.
+/*!
+ * \def TFM_LOG_READ_RECENT
+ *
+ * \brief Special value used in the log retrieval API to indicate
+ *        that data should be read up to the most recent entry
+ */
+#define TFM_LOG_READ_RECENT (-1)
+
+/*!
+ * \struct tfm_log_info
+ *
+ * \brief Structure containing information related to the size in bytes
+ *        and number of items retrieved or available in the audit log
+ */
+struct tfm_log_info {
+    uint32_t size; /*!< Size in bytes of items retrieved or available */
+    uint32_t num_items; /*!< Number of items retrieved or available */
+};
+
+/* The return value is shared with the TFM partition status value. The LOG
+ * return codes shouldn't overlap with predefined TFM status values.
  */
 #define TFM_LOG_ERR_OFFSET (TFM_PARTITION_SPECIFIC_ERROR_MIN)
 
diff --git a/interface/include/tfm_log_svc_handler.h b/interface/include/tfm_log_svc_handler.h
index 3e3b087..5c90c25 100644
--- a/interface/include/tfm_log_svc_handler.h
+++ b/interface/include/tfm_log_svc_handler.h
@@ -17,16 +17,56 @@
 /**
  * \brief Retrieves the audit log (SVC function)
  *
+ * \details The function reads the audit log into the buffer provided.
+ *          If provided buffer size is too small to fit the full log,
+ *          the function will read the maximum number of items in the
+ *          log that fit the available space in the buffer
+ *
  * \param[in]  size     Maximum number of bytes to retrieve from the log
+ * \param[in]  start    Index of element from where to start retrieval
  * \param[out] buffer   Pointer to the buffer that will hold the log
- * \param[out] log_size Pointer to the actual size of the log retrieved
+ * \param[out] info     Pointer to the \ref tfm_log_info structure
+ *                      contained information related to the retrieved
+ *                      portion of the log (size and number of items)
  *
  * \return Returns TFM_LOG_ERR_SUCCESS if retrieval has been completed,
  *         otherwise error as specified in \ref tfm_log_err
+ *
+ * \note If start is equal to TFM_ALG_READ_RECENT, the function will
+ *       retrieve the most recent elements that fit the provided size
  */
 enum tfm_log_err tfm_log_svc_retrieve(uint32_t size,
+                                      int32_t start,
                                       uint8_t *buffer,
-                                      uint32_t *log_size);
+                                      struct tfm_log_info *info);
+
+/**
+ * \brief Gets the log information (SVC function)
+ *
+ * \param[out] info Pointer to the \ref tfm_log_info structure that
+ *                  holds the current log size (both in bytes and items)
+ *
+ * \return Returns TFM_LOG_ERR_SUCCESS if reading has been completed,
+ *         otherwise error as specified in \ref tfm_log_err
+ */
+enum tfm_log_err tfm_log_svc_get_info(struct tfm_log_info *info);
+
+/**
+ * \brief Deletes one or more elements from the head of the log (SVC function)
+ *
+ * \param[in]  num_items Number of elements to be deleted
+ * \param[out] rem_items Pointer to the number of elements removed. This
+ *                       value indicates the number of elements actually
+ *                       removed from the log. In case the number of items
+ *                       stored is less than the number of items requested
+ *                       to remove, this value will reflect the number of
+ *                       items effectively removed.
+ *
+ * \return Returns TFM_LOG_ERR_SUCCESS if removal has been completed,
+ *         otherwise error as specified in \ref tfm_log_err
+ */
+enum tfm_log_err tfm_log_svc_delete_items(uint32_t num_items,
+                                          uint32_t *rem_items);
 
 #ifdef __cplusplus
 }
diff --git a/interface/include/tfm_log_veneers.h b/interface/include/tfm_log_veneers.h
index f86f987..043f611 100644
--- a/interface/include/tfm_log_veneers.h
+++ b/interface/include/tfm_log_veneers.h
@@ -18,16 +18,28 @@
 /**
  * \brief Retrieves the audit log
  *
+ * \details The function reads the audit log into the buffer provided.
+ *          If provided buffer size is too small to fit the full log,
+ *          the function will read the maximum number of items in the
+ *          log that fit the available space in the buffer
+ *
  * \param[in]  size     Maximum number of bytes to retrieve from the log
+ * \param[in]  start    Index of element from where to start retrieval
  * \param[out] buffer   Pointer to the buffer that will hold the log
- * \param[out] log_size Pointer to the actual size of the log retrieved
+ * \param[out] info     Pointer to the \ref tfm_log_info structure
+ *                      contained information related to the retrieved
+ *                      portion of the log (size and number of items)
  *
  * \return Returns TFM_LOG_ERR_SUCCESS if retrieval has been completed,
  *         otherwise error as specified in \ref tfm_log_err
+ *
+ * \note If start is equal to TFM_ALG_READ_RECENT, the function will
+ *       retrieve the most recent elements that fit the provided size
  */
 enum tfm_log_err tfm_log_veneer_retrieve(uint32_t size,
+                                         int32_t start,
                                          uint8_t *buffer,
-                                         uint32_t *log_size);
+                                         struct tfm_log_info *info);
 /**
  * \brief Adds a log entry
  *
@@ -42,6 +54,33 @@
  */
 enum tfm_log_err tfm_log_veneer_add_line(struct tfm_log_line *line);
 
+/**
+ * \brief Gets the log information
+ *
+ * \param[out] info Pointer to the \ref tfm_log_info structure that
+ *                  holds the current log size (both in bytes and items)
+ *
+ * \return Returns TFM_LOG_ERR_SUCCESS if reading has been completed,
+ *         otherwise error as specified in \ref tfm_log_err
+ */
+enum tfm_log_err tfm_log_veneer_get_info(struct tfm_log_info *info);
+
+/**
+ * \brief Deletes one or more elements from the head of the log
+ *
+ * \param[in]  num_items Number of elements to be deleted
+ * \param[out] rem_items Pointer to the number of elements removed. This
+ *                       value indicates the number of elements actually
+ *                       removed from the log. In case the number of items
+ *                       stored is less than the number of items requested
+ *                       to remove, this value will reflect the number of
+ *                       items effectively removed.
+ *
+ * \return Returns TFM_LOG_ERR_SUCCESS if removal has been completed,
+ *         otherwise error as specified in \ref tfm_log_err
+ */
+enum tfm_log_err tfm_log_veneer_delete_items(uint32_t num_items,
+                                             uint32_t *rem_items);
 #ifdef __cplusplus
 }
 #endif
diff --git a/interface/include/tfm_ns_svc.h b/interface/include/tfm_ns_svc.h
index 2102060..2788759 100644
--- a/interface/include/tfm_ns_svc.h
+++ b/interface/include/tfm_ns_svc.h
@@ -47,7 +47,9 @@
     X(SVC_TFM_SST_READ, tfm_sst_svc_read) \
     X(SVC_TFM_SST_WRITE, tfm_sst_svc_write) \
     X(SVC_TFM_SST_DELETE, tfm_sst_svc_delete) \
-    X(SVC_TFM_LOG_RETRIEVE, tfm_log_svc_retrieve)
+    X(SVC_TFM_LOG_RETRIEVE, tfm_log_svc_retrieve) \
+    X(SVC_TFM_LOG_GET_INFO, tfm_log_svc_get_info) \
+    X(SVC_TFM_LOG_DELETE_ITEMS, tfm_log_svc_delete_items)
 
 /**
  * \def LIST_SVC_CORE_TEST_INTERACTIVE
diff --git a/interface/src/tfm_log_api.c b/interface/src/tfm_log_api.c
index de7a31b..ae918da 100644
--- a/interface/src/tfm_log_api.c
+++ b/interface/src/tfm_log_api.c
@@ -9,12 +9,32 @@
 #include "tfm_ns_lock.h"
 
 enum tfm_log_err tfm_log_retrieve(uint32_t size,
+                                  int32_t start,
                                   uint8_t *buffer,
-                                  uint32_t *log_size)
+                                  struct tfm_log_info *info)
 {
     return tfm_ns_lock_svc_dispatch(SVC_TFM_LOG_RETRIEVE,
-                                    (uint32_t)size,
+                                    size,
+                                    (uint32_t)start,
                                     (uint32_t)buffer,
-                                    (uint32_t)log_size,
+                                    (uint32_t)info);
+}
+
+enum tfm_log_err tfm_log_get_info(struct tfm_log_info *info)
+{
+    return tfm_ns_lock_svc_dispatch(SVC_TFM_LOG_GET_INFO,
+                                    (uint32_t)info,
+                                    0,
+                                    0,
+                                    0);
+}
+
+enum tfm_log_err tfm_log_delete_items(uint32_t num_items,
+                                      uint32_t *rem_items)
+{
+    return tfm_ns_lock_svc_dispatch(SVC_TFM_LOG_DELETE_ITEMS,
+                                    num_items,
+                                    (uint32_t)rem_items,
+                                    0,
                                     0);
 }
diff --git a/interface/src/tfm_log_svc_handler.c b/interface/src/tfm_log_svc_handler.c
index 0a67a78..325d6de 100644
--- a/interface/src/tfm_log_svc_handler.c
+++ b/interface/src/tfm_log_svc_handler.c
@@ -10,8 +10,20 @@
 
 /* SVC function implementations */
 enum tfm_log_err tfm_log_svc_retrieve(uint32_t size,
+                                      int32_t start,
                                       uint8_t* buffer,
-                                      uint32_t *log_size)
+                                      struct tfm_log_info *info)
 {
-    return tfm_log_veneer_retrieve(size, buffer, log_size);
+    return tfm_log_veneer_retrieve(size, start, buffer, info);
+}
+
+enum tfm_log_err tfm_log_svc_get_info(struct tfm_log_info *info)
+{
+    return tfm_log_veneer_get_info(info);
+}
+
+enum tfm_log_err tfm_log_svc_delete_items(uint32_t num_items,
+                                          uint32_t *rem_items)
+{
+    return tfm_log_veneer_delete_items(num_items, rem_items);
 }