1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <stdio.h> 30 #include <stdarg.h> 31 #include <string.h> 32 #include <string_ext.h> 33 #include <trace.h> 34 #include <util.h> 35 #include <utee_syscalls.h> 36 37 #if TRACE_LEVEL > 0 38 39 void trace_ext_puts(const char *str) 40 { 41 utee_log(str, strlen(str)); 42 } 43 44 int trace_ext_get_thread_id(void) 45 { 46 return -1; 47 } 48 49 /* 50 * printf and puts - stdio printf support 51 * 52 * 'printf()' and 'puts()' traces have the 'info' trace level. 53 */ 54 int printf(const char *fmt, ...) 55 { 56 char to_format[MAX_PRINT_SIZE]; 57 va_list ap; 58 int s; 59 60 if (trace_get_level() < TRACE_PRINTF_LEVEL) 61 return 0; 62 63 va_start(ap, fmt); 64 s = vsnprintf(to_format, sizeof(to_format), fmt, ap); 65 va_end(ap); 66 67 if (s < 0) 68 return s; 69 70 trace_ext_puts(to_format); 71 72 return s; 73 } 74 75 int puts(const char *str) 76 { 77 if (trace_get_level() >= TRACE_PRINTF_LEVEL) { 78 trace_ext_puts(str); 79 trace_ext_puts("\n"); 80 } 81 return 1; 82 } 83 84 int putchar(int c) 85 { 86 char str[2] = { (char)c, '\0' }; 87 88 if (trace_get_level() >= TRACE_PRINTF_LEVEL) 89 trace_ext_puts(str); 90 /* 91 * From the putchar() man page: 92 * "fputc(), putc() and putchar() return the character written as an 93 * unsigned char cast to an int or EOF on error." 94 */ 95 return (int)(unsigned char)c; 96 } 97 98 #else 99 100 int printf(const char *fmt __unused, ...) 101 { 102 return 0; 103 } 104 105 int puts(const char *str __unused) 106 { 107 return 0; 108 } 109 110 int putchar(int c) 111 { 112 return (int)(unsigned char)c; 113 } 114 115 #endif 116