blob: dc0e995f00fbd08b4786728dcf7bec8091cf296a [file] [log] [blame]
Shawn Shan17d4e232020-06-12 16:45:00 +08001/*
2 * Copyright (c) 2020, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "tfm_spm_log.h"
9
10#define MAX_DIGIT_BITS 10 /* Max bits of uint32_t value 0xFFFFFFFF add '0x' */
11const static char HEX_TABLE[] = {'0', '1', '2', '3', '4', '5', '6', '7',
12 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
13
14/**
15 * \brief Convert digit number into HEX format string, the string have '0x'
16 * prefix and leading zeros are not stripped.
17 *
18 * \param[in] value A value need to be converted.
19 * \param[in] msg A string message that the value converted to.
20 *
21 */
22
23static void to_hex(uint32_t value, char msg[])
24{
25 int i;
26
27 msg[0] = '0';
28 msg[1] = 'x';
29 for (i = MAX_DIGIT_BITS - 1; i >= 2; i--, value >>= 4) {
30 msg[i] = HEX_TABLE[value & 0xF];
31 }
32}
33
34int32_t spm_log_msgval(const char *msg, size_t len, uint32_t value)
35{
36 int32_t result_msg = 0, result_val;
37 char value_str[MAX_DIGIT_BITS];
38
39 if (msg && len) {
40 result_msg = tfm_hal_output_spm_log(msg, len);
41 if (result_msg < TFM_HAL_SUCCESS) {
42 return result_msg;
43 }
44 }
45
46 to_hex(value, value_str);
47
48 result_val = tfm_hal_output_spm_log(value_str,
49 MAX_DIGIT_BITS);
50 if (result_val < TFM_HAL_SUCCESS) {
51 return result_val;
52 }
53 return (result_msg + result_val);
54}