blob: 8c1857e58fcb087add7ada03bcb4117238081591 [file] [log] [blame]
Soby Mathewafe7e2f2014-06-12 17:23:58 +01001/*
Soby Mathewf62d5462016-03-22 17:38:00 +00002 * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved.
Soby Mathewafe7e2f2014-06-12 17:23:58 +01003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
Soby Mathew0691d972016-05-05 12:34:41 +010030#include <arch.h>
31#include <arch_helpers.h>
32#include <assert.h>
Soby Mathewafe7e2f2014-06-12 17:23:58 +010033#include <debug.h>
Soby Mathew0691d972016-05-05 12:34:41 +010034#include <limits.h>
Soby Mathewafe7e2f2014-06-12 17:23:58 +010035#include <stdarg.h>
36#include <stdint.h>
37
38/***********************************************************
39 * The tf_printf implementation for all BL stages
40 ***********************************************************/
Soby Mathewf62d5462016-03-22 17:38:00 +000041
42#define get_num_va_args(args, lcount) \
43 (((lcount) > 1) ? va_arg(args, long long int) : \
44 ((lcount) ? va_arg(args, long int) : va_arg(args, int)))
45
46#define get_unum_va_args(args, lcount) \
47 (((lcount) > 1) ? va_arg(args, unsigned long long int) : \
48 ((lcount) ? va_arg(args, unsigned long int) : va_arg(args, unsigned int)))
49
50static void string_print(const char *str)
51{
52 while (*str)
53 putchar(*str++);
54}
55
Soby Mathew0691d972016-05-05 12:34:41 +010056#ifdef AARCH32
57#define unsigned_num_print(unum, radix) \
58 do { \
59 if ((radix) == 16) \
60 unsigned_hex_print(unum); \
61 else if ((radix) == 10) \
62 unsigned_dec_print(unum); \
63 else \
64 string_print("tf_printf : Unsupported radix");\
65 } while (0);
66
67/*
68 * Utility function to print an unsigned number in decimal format for AArch32.
69 * The function doesn't support printing decimal integers higher than 32 bits
70 * to avoid having to implement 64-bit integer compiler library functions.
71 */
72static void unsigned_dec_print(unsigned long long int unum)
73{
74 unsigned int local_num;
75 /* Just need enough space to store 32 bit decimal integer */
76 unsigned char num_buf[10];
77 int i = 0, rem;
78
79 if (unum > UINT_MAX) {
80 string_print("tf_printf : decimal numbers higher than 32 bits"
81 " not supported\n");
82 return;
83 }
84
85 local_num = (unsigned int)unum;
86
87 do {
88 rem = local_num % 10;
89 num_buf[i++] = '0' + rem;
90 } while (local_num /= 10);
91
92 while (--i >= 0)
93 putchar(num_buf[i]);
94}
95
96/*
97 * Utility function to print an unsigned number in hexadecimal format for
98 * AArch32. The function doesn't use 64-bit integer arithmetic to avoid
99 * having to implement 64-bit compiler library functions. It splits the
100 * 64 bit number into two 32 bit numbers and converts them into equivalent
101 * ASCII characters.
102 */
103static void unsigned_hex_print(unsigned long long int unum)
104{
105 /* Just need enough space to store 16 characters */
106 unsigned char num_buf[16];
107 int i = 0, rem;
108 uint32_t num_local = 0, num_msb = 0;
109
110 /* Get the LSB of 64 bit unum */
111 num_local = (uint32_t)unum;
112 /* Get the MSB of 64 bit unum. This works only on Little Endian */
113 assert((read_sctlr() & SCTLR_EE_BIT) == 0);
114 num_msb = *(((uint32_t *) &unum) + 1);
115
116 do {
117 do {
118 rem = (num_local & 0xf);
119 if (rem < 0xa)
120 num_buf[i++] = '0' + rem;
121 else
122 num_buf[i++] = 'a' + (rem - 0xa);
123 } while (num_local >>= 4);
124
125 num_local = num_msb;
126 num_msb = 0;
127 } while (num_local);
128
129 while (--i >= 0)
130 putchar(num_buf[i]);
131}
132
133#else
134
Soby Mathewf62d5462016-03-22 17:38:00 +0000135static void unsigned_num_print(unsigned long long int unum, unsigned int radix)
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100136{
137 /* Just need enough space to store 64 bit decimal integer */
138 unsigned char num_buf[20];
Sandrine Bailleuxa64a8542015-03-05 10:54:34 +0000139 int i = 0, rem;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100140
141 do {
142 rem = unum % radix;
143 if (rem < 0xa)
144 num_buf[i++] = '0' + rem;
145 else
146 num_buf[i++] = 'a' + (rem - 0xa);
147 } while (unum /= radix);
148
149 while (--i >= 0)
150 putchar(num_buf[i]);
151}
Soby Mathew0691d972016-05-05 12:34:41 +0100152#endif /* AARCH32 */
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100153
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100154/*******************************************************************
155 * Reduced format print for Trusted firmware.
Soby Mathewf62d5462016-03-22 17:38:00 +0000156 * The following type specifiers are supported by this print
157 * %x - hexadecimal format
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100158 * %s - string format
Soby Mathewf62d5462016-03-22 17:38:00 +0000159 * %d or %i - signed decimal format
160 * %u - unsigned decimal format
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000161 * %p - pointer format
Soby Mathewf62d5462016-03-22 17:38:00 +0000162 *
163 * The following length specifiers are supported by this print
164 * %l - long int (64-bit on AArch64)
165 * %ll - long long int (64-bit on AArch64)
166 * %z - size_t sized integer formats (64 bit on AArch64)
167 *
168 * The print exits on all other formats specifiers other than valid
169 * combinations of the above specifiers.
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100170 *******************************************************************/
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100171void tf_printf(const char *fmt, ...)
172{
173 va_list args;
Soby Mathewf62d5462016-03-22 17:38:00 +0000174 int l_count;
175 long long int num;
176 unsigned long long int unum;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100177 char *str;
178
179 va_start(args, fmt);
180 while (*fmt) {
Soby Mathewf62d5462016-03-22 17:38:00 +0000181 l_count = 0;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100182
183 if (*fmt == '%') {
184 fmt++;
185 /* Check the format specifier */
186loop:
187 switch (*fmt) {
188 case 'i': /* Fall through to next one */
189 case 'd':
Soby Mathewf62d5462016-03-22 17:38:00 +0000190 num = get_num_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100191 if (num < 0) {
192 putchar('-');
Soby Mathewf62d5462016-03-22 17:38:00 +0000193 unum = (unsigned long long int)-num;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100194 } else
Soby Mathewf62d5462016-03-22 17:38:00 +0000195 unum = (unsigned long long int)num;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100196
197 unsigned_num_print(unum, 10);
198 break;
199 case 's':
200 str = va_arg(args, char *);
201 string_print(str);
202 break;
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000203 case 'p':
Soby Mathewf62d5462016-03-22 17:38:00 +0000204 unum = (uintptr_t)va_arg(args, void *);
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000205 if (unum)
206 string_print("0x");
207
208 unsigned_num_print(unum, 16);
209 break;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100210 case 'x':
Soby Mathewf62d5462016-03-22 17:38:00 +0000211 unum = get_unum_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100212 unsigned_num_print(unum, 16);
213 break;
Scott Branden57bd75c2016-03-23 13:37:33 -0700214 case 'z':
215 if (sizeof(size_t) == 8)
Soby Mathewf62d5462016-03-22 17:38:00 +0000216 l_count = 2;
217
Scott Branden57bd75c2016-03-23 13:37:33 -0700218 fmt++;
219 goto loop;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100220 case 'l':
Soby Mathewf62d5462016-03-22 17:38:00 +0000221 l_count++;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100222 fmt++;
223 goto loop;
224 case 'u':
Soby Mathewf62d5462016-03-22 17:38:00 +0000225 unum = get_unum_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100226 unsigned_num_print(unum, 10);
227 break;
228 default:
229 /* Exit on any other format specifier */
230 goto exit;
231 }
232 fmt++;
233 continue;
234 }
235 putchar(*fmt++);
236 }
237exit:
238 va_end(args);
239}