21 #ifndef AUDIT_UART_REDIRECTION
22 #define AUDIT_UART_REDIRECTION (0U)
25 #if (AUDIT_UART_REDIRECTION == 1U)
27 #include "Driver_USART.h"
31 #define LOG_UART_NAME Driver_USART1
33 extern ARM_DRIVER_USART LOG_UART_NAME;
41 #ifndef LOG_UART_BAUD_RATE
42 #define LOG_UART_BAUD_RATE (DEFAULT_UART_BAUDRATE)
51 static uint8_t log_uart_init_success = 0U;
59 static const char hex_values[] =
"0123456789ABCDEF";
67 #define MEMBER_SIZE(type,member) sizeof(((type *)0)->member)
76 #define LOG_FIXED_FIELD_SIZE (MEMBER_SIZE(struct log_hdr, timestamp) + \
77 MEMBER_SIZE(struct log_hdr, iv_counter) + \
78 MEMBER_SIZE(struct log_hdr, partition_id) + \
79 MEMBER_SIZE(struct log_hdr, size))
87 #define LOG_SIZE (1024)
96 __attribute__ ((aligned(4)))
97 static uint8_t log_buffer[
LOG_SIZE] = {0};
105 static uint64_t scratch_buffer[(
LOG_SIZE)/8] = {0};
130 static struct log_vars log_state = {0};
142 static uint64_t global_timestamp = 0;
151 __attribute__ ((always_inline)) __STATIC_INLINE
154 return (
struct log_hdr *)( &log_buffer[idx] );
164 __attribute__ ((always_inline)) __STATIC_INLINE
180 __attribute__ ((always_inline)) __STATIC_INLINE
194 __attribute__ ((always_inline)) __STATIC_INLINE
211 static void audit_update_state(
const uint32_t
first_el_idx,
237 static void audit_replace_record(
const uint32_t size,
243 uint32_t start_pos = 0, stop_pos = 0;
255 if (num_items == 0) {
271 if (num_items == 0) {
295 static psa_status_t audit_buffer_copy(
const uint8_t *src,
300 uint32_t dest_idx = (uint32_t)dest - (uint32_t)&log_buffer[0];
311 for (idx = 0; idx < size; idx++) {
312 log_buffer[(dest_idx + idx) %
LOG_SIZE] = src[idx];
332 for (idx = 0; idx < size; idx++) {
333 dest[idx] = src[idx];
349 const int32_t partition_id,
362 hdr = (
struct log_hdr *) buffer;
377 status = audit_memcpy((
const uint8_t *) record,
379 (uint8_t *) &(hdr->
size));
405 static void audit_uart_redirection(
const uint32_t start_idx)
407 #if (AUDIT_UART_REDIRECTION == 1U)
409 uint8_t end_of_line[] = {
'\r',
'\n'};
413 if (log_uart_init_success == 1U) {
415 read_byte = log_buffer[(start_idx+idx) %
LOG_SIZE];
416 (void)LOG_UART_NAME.Send(&hex_values[(read_byte >> 4) & 0xF],1);
417 (void)LOG_UART_NAME.Send(&hex_values[read_byte & 0xF], 1);
418 (void)LOG_UART_NAME.Send(
" ", 1);
420 (void)LOG_UART_NAME.Send(&end_of_line, 2);
425 static psa_status_t _audit_core_get_info(uint32_t *num_records, uint32_t *size)
436 static psa_status_t _audit_core_get_record_info(
const uint32_t record_index,
439 uint32_t start_idx, idx;
449 for (idx = 0; idx < record_index; idx++) {
467 #if (AUDIT_UART_REDIRECTION == 1U)
468 int32_t ret = ARM_DRIVER_OK;
470 ret = LOG_UART_NAME.Initialize(NULL);
471 if (ret != ARM_DRIVER_OK) {
475 ret = LOG_UART_NAME.Control(ARM_USART_MODE_ASYNCHRONOUS,
477 if (ret != ARM_DRIVER_OK) {
482 log_uart_init_success = 1U;
486 audit_update_state(0,0,0,0);
496 uint32_t first_el_idx, size_removed;
498 if ((in_len != 2) || (out_len != 0)) {
502 if (in_vec[0].len !=
sizeof(uint32_t)) {
506 const uint32_t record_index = *((uint32_t *)in_vec[0].base);
507 const uint8_t *token = in_vec[1].
base;
508 const uint32_t token_size = in_vec[1].
len;
514 if (record_index > 0) {
522 if ((token != NULL) || (token_size != 0)) {
535 audit_update_state(0,0,0,0);
565 if ((in_len != 0) || (out_len != 2)) {
569 if ((out_vec[0].len !=
sizeof(uint32_t)) ||
570 (out_vec[1].len !=
sizeof(uint32_t))) {
574 uint32_t *num_records = out_vec[0].
base;
575 uint32_t *size = out_vec[1].
base;
591 uint32_t start_idx, idx;
593 if ((in_len != 1) || (out_len != 1)) {
597 if ((in_vec[0].len !=
sizeof(uint32_t)) ||
598 (out_vec[0].len !=
sizeof(uint32_t))) {
602 const uint32_t record_index = *((uint32_t *)in_vec[0].base);
603 uint32_t *size = out_vec[0].
base;
613 for (idx = 0; idx < record_index; idx++) {
628 uint32_t start_pos = 0, stop_pos = 0;
629 uint32_t first_el_idx = 0, last_el_idx = 0, size = 0;
630 uint32_t num_items = 0, stored_size = 0;
631 int32_t partition_id;
634 if ((in_len != 1) || (out_len != 0)) {
670 status = _audit_core_get_info(&num_items, &stored_size);
675 if (num_items == 0) {
690 status = audit_format_buffer(record, partition_id, &scratch_buffer[0]);
698 status = audit_buffer_copy((
const uint8_t *) &scratch_buffer[0],
700 (uint8_t *) &log_buffer[start_pos]);
711 last_el_idx = start_pos;
718 audit_update_state(first_el_idx, last_el_idx, stored_size, num_items);
725 audit_uart_redirection(last_el_idx);
735 uint32_t idx, start_idx, record_size_tmp;
738 if ((in_len != 2) || (out_len != 1)) {
742 if (in_vec[0].len !=
sizeof(uint32_t)) {
745 const uint32_t record_index = *((uint32_t *)in_vec[0].base);
746 const uint8_t *token = in_vec[1].
base;
747 const uint32_t token_size = in_vec[1].
len;
748 uint8_t *buffer = out_vec[0].
base;
749 uint32_t buffer_size = out_vec[0].
len;
755 if ((token != NULL) || (token_size != 0)) {
761 status = _audit_core_get_record_info(record_index, &record_size_tmp);
770 if (buffer_size < record_size_tmp) {
779 for (idx=0; idx<record_index; idx++) {
784 for (idx=0; idx<record_size_tmp; idx++) {
785 buffer[idx] = log_buffer[(start_idx + idx) %
LOG_SIZE];
789 out_vec[0].
len = record_size_tmp;
__STATIC_INLINE uint32_t COMPUTE_LOG_ENTRY_SIZE(const uint32_t size)
Static inline function to compute the full log entry size starting from the value of the size field...
#define PSA_ERROR_BUFFER_TOO_SMALL
psa_status_t audit_core_delete_record(psa_invec in_vec[], size_t in_len, psa_outvec out_vec[], size_t out_len)
psa_status_t audit_core_add_record(psa_invec in_vec[], size_t in_len, psa_outvec out_vec[], size_t out_len)
Fixed size header for a log record.
#define LOG_MAC_SIZE
Size in bytes of the MAC for each entry.
#define PSA_ERROR_INSUFFICIENT_MEMORY
#define PSA_ERROR_GENERIC_ERROR
This structure contains the record that is added to the audit log by the requesting secure service...
#define PSA_ERROR_NOT_PERMITTED
Contains the state variables associated to the current state of the audit log.
__STATIC_INLINE uint32_t GET_NEXT_LOG_INDEX(const uint32_t idx)
Static inline function to get the index to the base of the log buffer for the next item with respect ...
#define PSA_ERROR_PROGRAMMER_ERROR
int32_t tfm_core_get_caller_client_id(int32_t *caller_client_id)
#define PSA_ERROR_NOT_SUPPORTED
#define LOG_SIZE
Size of the allocated space for the log, in bytes.
psa_status_t audit_core_init(void)
Initializes the Audit logging service during the TFM boot up process.
#define PSA_ERROR_CONNECTION_REFUSED
Fixed size logging entry trailer.
#define LOG_FIXED_FIELD_SIZE
Size of the mandatory header fields that are before the info received from the client partition...
psa_status_t audit_core_get_info(psa_invec in_vec[], size_t in_len, psa_outvec out_vec[], size_t out_len)
#define TFM_CLIENT_ID_IS_NS(client_id)
Checks if the provided client ID is a non-secure client ID.
psa_status_t audit_core_retrieve_record(psa_invec in_vec[], size_t in_len, psa_outvec out_vec[], size_t out_len)
int32_t psa_status_t
Function return status.
__STATIC_INLINE uint32_t * GET_SIZE_FIELD_POINTER(const uint32_t idx)
Static inline function to get the pointer to the SIZE field.
psa_status_t audit_core_get_record_info(psa_invec in_vec[], size_t in_len, psa_outvec out_vec[], size_t out_len)
__STATIC_INLINE struct log_hdr * GET_LOG_POINTER(const uint32_t idx)
Static inline function to get the log buffer ptr from index.