blob: b15f87d764737cedfa348dd182d473ef4079fb82 [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
Ken Liu8d973da2020-11-03 15:42:46 +080010#define MAX_DIGIT_BITS 12 /* 8 char for number, 2 for '0x' and 2 for '\r\n' */
Shawn Shan17d4e232020-06-12 16:45:00 +080011const 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{
Ken Liu8d973da2020-11-03 15:42:46 +080025 int i = MAX_DIGIT_BITS - 1;
Shawn Shan17d4e232020-06-12 16:45:00 +080026
Ken Liu8d973da2020-11-03 15:42:46 +080027 msg[i--] = '\n';
28 msg[i--] = '\r';
29 for (; i > 1; i--, value >>= 4) {
Shawn Shan17d4e232020-06-12 16:45:00 +080030 msg[i] = HEX_TABLE[value & 0xF];
31 }
Ken Liu8d973da2020-11-03 15:42:46 +080032 msg[i--] = 'x';
33 msg[i--] = '0';
Shawn Shan17d4e232020-06-12 16:45:00 +080034}
35
36int32_t spm_log_msgval(const char *msg, size_t len, uint32_t value)
37{
38 int32_t result_msg = 0, result_val;
39 char value_str[MAX_DIGIT_BITS];
40
41 if (msg && len) {
42 result_msg = tfm_hal_output_spm_log(msg, len);
43 if (result_msg < TFM_HAL_SUCCESS) {
44 return result_msg;
45 }
46 }
47
48 to_hex(value, value_str);
49
50 result_val = tfm_hal_output_spm_log(value_str,
51 MAX_DIGIT_BITS);
52 if (result_val < TFM_HAL_SUCCESS) {
53 return result_val;
54 }
55 return (result_msg + result_val);
56}