1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * Copyright (c) 2020, Arm Limited
5  */
6 
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <string.h>
10 #include <string_ext.h>
11 #include <trace.h>
12 #include <util.h>
13 #include <utee_syscalls.h>
14 
15 #ifdef __LDELF__
16 #include <ldelf_syscalls.h>
17 #endif
18 
19 #if TRACE_LEVEL > 0
20 
trace_ext_puts(const char * str)21 void trace_ext_puts(const char *str)
22 {
23 #ifdef __LDELF__
24 	_ldelf_log(str, strlen(str));
25 #else
26 	_utee_log(str, strlen(str));
27 #endif
28 }
29 
trace_ext_get_thread_id(void)30 int trace_ext_get_thread_id(void)
31 {
32 	return -1;
33 }
34 
35 /*
36  * printf and puts - stdio printf support
37  *
38  * 'printf()' and 'puts()' traces have the 'info' trace level.
39  */
printf(const char * fmt,...)40 int printf(const char *fmt, ...)
41 {
42 	char to_format[MAX_PRINT_SIZE];
43 	va_list ap;
44 	int s;
45 
46 	if (trace_get_level() < TRACE_PRINTF_LEVEL)
47 		return 0;
48 
49 	va_start(ap, fmt);
50 	s = vsnprintf(to_format, sizeof(to_format), fmt, ap);
51 	va_end(ap);
52 
53 	if (s < 0)
54 		return s;
55 
56 	trace_ext_puts(to_format);
57 
58 	return s;
59 }
60 
puts(const char * str)61 int puts(const char *str)
62 {
63 	if (trace_get_level() >= TRACE_PRINTF_LEVEL) {
64 		trace_ext_puts(str);
65 		trace_ext_puts("\n");
66 	}
67 	return 1;
68 }
69 
putchar(int c)70 int putchar(int c)
71 {
72 	char str[2] = { (char)c, '\0' };
73 
74 	if (trace_get_level() >= TRACE_PRINTF_LEVEL)
75 		trace_ext_puts(str);
76 	/*
77 	 * From the putchar() man page:
78 	 * "fputc(), putc() and putchar() return the character written as an
79 	 * unsigned char cast to an int or EOF on error."
80 	 */
81 	return (int)(unsigned char)c;
82 }
83 
84 #else
85 
trace_ext_puts(const char * str __unused)86 void trace_ext_puts(const char *str __unused)
87 {
88 }
89 
printf(const char * fmt __unused,...)90 int printf(const char *fmt __unused, ...)
91 {
92 	return 0;
93 }
94 
puts(const char * str __unused)95 int puts(const char *str __unused)
96 {
97 	return 0;
98 }
99 
putchar(int c)100 int putchar(int c)
101 {
102 	return (int)(unsigned char)c;
103 }
104 
105 #endif
106