Shawn Shan | 17d4e23 | 2020-06-12 16:45:00 +0800 | [diff] [blame] | 1 | /* |
| 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 Liu | 8d973da | 2020-11-03 15:42:46 +0800 | [diff] [blame^] | 10 | #define MAX_DIGIT_BITS 12 /* 8 char for number, 2 for '0x' and 2 for '\r\n' */ |
Shawn Shan | 17d4e23 | 2020-06-12 16:45:00 +0800 | [diff] [blame] | 11 | const 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 | |
| 23 | static void to_hex(uint32_t value, char msg[]) |
| 24 | { |
Ken Liu | 8d973da | 2020-11-03 15:42:46 +0800 | [diff] [blame^] | 25 | int i = MAX_DIGIT_BITS - 1; |
Shawn Shan | 17d4e23 | 2020-06-12 16:45:00 +0800 | [diff] [blame] | 26 | |
Ken Liu | 8d973da | 2020-11-03 15:42:46 +0800 | [diff] [blame^] | 27 | msg[i--] = '\n'; |
| 28 | msg[i--] = '\r'; |
| 29 | for (; i > 1; i--, value >>= 4) { |
Shawn Shan | 17d4e23 | 2020-06-12 16:45:00 +0800 | [diff] [blame] | 30 | msg[i] = HEX_TABLE[value & 0xF]; |
| 31 | } |
Ken Liu | 8d973da | 2020-11-03 15:42:46 +0800 | [diff] [blame^] | 32 | msg[i--] = 'x'; |
| 33 | msg[i--] = '0'; |
Shawn Shan | 17d4e23 | 2020-06-12 16:45:00 +0800 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | int32_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 | } |