1 /*
2  * SPDX-FileCopyrightText: Copyright The TrustedFirmware-M Contributors
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <stddef.h>
9 #include <inttypes.h>
10 #include "tfm_log.h"
11 #include "fatal_error.h"
12 #include "tfm_hal_device_header.h"
13 #include "uart_stdout.h"
14 
log_error_permissions_check(uint32_t err,bool is_fatal)15 __WEAK bool log_error_permissions_check(uint32_t err, bool is_fatal)
16 {
17     return true;
18 }
19 
20 #define LOG_FATAL_NON_FATAL_ERR(_is_fatal, ...) \
21     do {                                        \
22         if (_is_fatal) {                        \
23             ERROR_RAW(__VA_ARGS__);             \
24         } else {                                \
25             WARN_RAW(__VA_ARGS__);              \
26         }                                       \
27     } while (0);
28 
log_error(char * file,uint32_t line,uint32_t err,void * sp,bool is_fatal)29 __WEAK void log_error(char *file, uint32_t line, uint32_t err, void *sp, bool is_fatal)
30 {
31     if (stdio_is_initialized()) {
32         if (is_fatal) {
33             ERROR("Fatal error ");
34         } else {
35             WARN("Non-fatal error ");
36         }
37 
38         if (err != 0) {
39             LOG_FATAL_NON_FATAL_ERR(is_fatal, "%08"PRIx32" ", err);
40         }
41 
42         if (file != NULL) {
43             LOG_FATAL_NON_FATAL_ERR(is_fatal, "in file %s ", file);
44         }
45 
46         if (line != 0) {
47             LOG_FATAL_NON_FATAL_ERR(is_fatal, "at line %"PRIu32" ", line);
48         }
49 
50         if (sp != NULL) {
51             LOG_FATAL_NON_FATAL_ERR(is_fatal, "with SP=0x%"PRIx32" ", (uint32_t)sp);
52         }
53 
54         LOG_FATAL_NON_FATAL_ERR(is_fatal, "\n");
55     }
56 }
57