blob: ad0b90aa03b3d83d727748813a48b1725b09cfff [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 */
30#include <debug.h>
31#include <stdarg.h>
32#include <stdint.h>
33
34/***********************************************************
35 * The tf_printf implementation for all BL stages
36 ***********************************************************/
Soby Mathewf62d5462016-03-22 17:38:00 +000037
38#define get_num_va_args(args, lcount) \
39 (((lcount) > 1) ? va_arg(args, long long int) : \
40 ((lcount) ? va_arg(args, long int) : va_arg(args, int)))
41
42#define get_unum_va_args(args, lcount) \
43 (((lcount) > 1) ? va_arg(args, unsigned long long int) : \
44 ((lcount) ? va_arg(args, unsigned long int) : va_arg(args, unsigned int)))
45
46static void string_print(const char *str)
47{
48 while (*str)
49 putchar(*str++);
50}
51
52static void unsigned_num_print(unsigned long long int unum, unsigned int radix)
Soby Mathewafe7e2f2014-06-12 17:23:58 +010053{
54 /* Just need enough space to store 64 bit decimal integer */
55 unsigned char num_buf[20];
Sandrine Bailleuxa64a8542015-03-05 10:54:34 +000056 int i = 0, rem;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010057
58 do {
59 rem = unum % radix;
60 if (rem < 0xa)
61 num_buf[i++] = '0' + rem;
62 else
63 num_buf[i++] = 'a' + (rem - 0xa);
64 } while (unum /= radix);
65
66 while (--i >= 0)
67 putchar(num_buf[i]);
68}
69
Soby Mathewafe7e2f2014-06-12 17:23:58 +010070/*******************************************************************
71 * Reduced format print for Trusted firmware.
Soby Mathewf62d5462016-03-22 17:38:00 +000072 * The following type specifiers are supported by this print
73 * %x - hexadecimal format
Soby Mathewafe7e2f2014-06-12 17:23:58 +010074 * %s - string format
Soby Mathewf62d5462016-03-22 17:38:00 +000075 * %d or %i - signed decimal format
76 * %u - unsigned decimal format
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +000077 * %p - pointer format
Soby Mathewf62d5462016-03-22 17:38:00 +000078 *
79 * The following length specifiers are supported by this print
80 * %l - long int (64-bit on AArch64)
81 * %ll - long long int (64-bit on AArch64)
82 * %z - size_t sized integer formats (64 bit on AArch64)
83 *
84 * The print exits on all other formats specifiers other than valid
85 * combinations of the above specifiers.
Soby Mathewafe7e2f2014-06-12 17:23:58 +010086 *******************************************************************/
Soby Mathewafe7e2f2014-06-12 17:23:58 +010087void tf_printf(const char *fmt, ...)
88{
89 va_list args;
Soby Mathewf62d5462016-03-22 17:38:00 +000090 int l_count;
91 long long int num;
92 unsigned long long int unum;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010093 char *str;
94
95 va_start(args, fmt);
96 while (*fmt) {
Soby Mathewf62d5462016-03-22 17:38:00 +000097 l_count = 0;
Soby Mathewafe7e2f2014-06-12 17:23:58 +010098
99 if (*fmt == '%') {
100 fmt++;
101 /* Check the format specifier */
102loop:
103 switch (*fmt) {
104 case 'i': /* Fall through to next one */
105 case 'd':
Soby Mathewf62d5462016-03-22 17:38:00 +0000106 num = get_num_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100107 if (num < 0) {
108 putchar('-');
Soby Mathewf62d5462016-03-22 17:38:00 +0000109 unum = (unsigned long long int)-num;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100110 } else
Soby Mathewf62d5462016-03-22 17:38:00 +0000111 unum = (unsigned long long int)num;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100112
113 unsigned_num_print(unum, 10);
114 break;
115 case 's':
116 str = va_arg(args, char *);
117 string_print(str);
118 break;
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000119 case 'p':
Soby Mathewf62d5462016-03-22 17:38:00 +0000120 unum = (uintptr_t)va_arg(args, void *);
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000121 if (unum)
122 string_print("0x");
123
124 unsigned_num_print(unum, 16);
125 break;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100126 case 'x':
Soby Mathewf62d5462016-03-22 17:38:00 +0000127 unum = get_unum_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100128 unsigned_num_print(unum, 16);
129 break;
Scott Branden57bd75c2016-03-23 13:37:33 -0700130 case 'z':
131 if (sizeof(size_t) == 8)
Soby Mathewf62d5462016-03-22 17:38:00 +0000132 l_count = 2;
133
Scott Branden57bd75c2016-03-23 13:37:33 -0700134 fmt++;
135 goto loop;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100136 case 'l':
Soby Mathewf62d5462016-03-22 17:38:00 +0000137 l_count++;
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100138 fmt++;
139 goto loop;
140 case 'u':
Soby Mathewf62d5462016-03-22 17:38:00 +0000141 unum = get_unum_va_args(args, l_count);
Soby Mathewafe7e2f2014-06-12 17:23:58 +0100142 unsigned_num_print(unum, 10);
143 break;
144 default:
145 /* Exit on any other format specifier */
146 goto exit;
147 }
148 fmt++;
149 continue;
150 }
151 putchar(*fmt++);
152 }
153exit:
154 va_end(args);
155}