tf_printf.c 4.13 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2017, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
 */
Soby Mathew's avatar
Soby Mathew committed
6
7
8
#include <arch.h>
#include <arch_helpers.h>
#include <assert.h>
9
#include <debug.h>
Soby Mathew's avatar
Soby Mathew committed
10
#include <limits.h>
11
12
13
14
15
16
#include <stdarg.h>
#include <stdint.h>

/***********************************************************
 * The tf_printf implementation for all BL stages
 ***********************************************************/
17

Daniel Boulby's avatar
Daniel Boulby committed
18
19
20
#define get_num_va_args(_args, _lcount) \
	(((_lcount) > 1) ? va_arg(_args, long long int) :	\
	((_lcount) ? va_arg(_args, long int) : va_arg(_args, int)))
21

Daniel Boulby's avatar
Daniel Boulby committed
22
23
24
#define get_unum_va_args(_args, _lcount) \
	(((_lcount) > 1) ? va_arg(_args, unsigned long long int) :	\
	((_lcount) ? va_arg(_args, unsigned long int) : va_arg(_args, unsigned int)))
25

26
int tf_string_print(const char *str)
27
{
28
29
	int count = 0;

30
31
	assert(str);

32
	while (*str) {
33
		putchar(*str++);
34
35
36
37
		count++;
	}

	return count;
38
39
}

40
41
static int unsigned_num_print(unsigned long long int unum, unsigned int radix,
			      char padc, int padn)
42
43
44
{
	/* Just need enough space to store 64 bit decimal integer */
	unsigned char num_buf[20];
45
	int i = 0, rem, count = 0;
46
47
48
49
50
51
52
53
54

	do {
		rem = unum % radix;
		if (rem < 0xa)
			num_buf[i++] = '0' + rem;
		else
			num_buf[i++] = 'a' + (rem - 0xa);
	} while (unum /= radix);

55
56
57
	if (padn > 0) {
		while (i < padn--) {
			putchar(padc);
58
			count++;
59
60
61
		}
	}

62
	while (--i >= 0) {
63
		putchar(num_buf[i]);
64
65
66
67
		count++;
	}

	return count;
68
69
70
71
}

/*******************************************************************
 * Reduced format print for Trusted firmware.
72
73
 * The following type specifiers are supported by this print
 * %x - hexadecimal format
74
 * %s - string format
75
76
 * %d or %i - signed decimal format
 * %u - unsigned decimal format
77
 * %p - pointer format
78
79
80
81
82
83
 *
 * The following length specifiers are supported by this print
 * %l - long int (64-bit on AArch64)
 * %ll - long long int (64-bit on AArch64)
 * %z - size_t sized integer formats (64 bit on AArch64)
 *
84
85
86
 * The following padding specifiers are supported by this print
 * %0NN - Left-pad the number with 0s (NN is a decimal number)
 *
87
88
 * The print exits on all other formats specifiers other than valid
 * combinations of the above specifiers.
89
 *******************************************************************/
90
int tf_vprintf(const char *fmt, va_list args)
91
{
92
93
94
	int l_count;
	long long int num;
	unsigned long long int unum;
95
	char *str;
96
97
	char padc = 0; /* Padding character */
	int padn; /* Number of characters to pad */
98
	int count = 0; /* Number of printed characters */
99
100

	while (*fmt) {
101
		l_count = 0;
102
		padn = 0;
103
104
105
106
107
108
109
110

		if (*fmt == '%') {
			fmt++;
			/* Check the format specifier */
loop:
			switch (*fmt) {
			case 'i': /* Fall through to next one */
			case 'd':
111
				num = get_num_va_args(args, l_count);
112
113
				if (num < 0) {
					putchar('-');
114
					unum = (unsigned long long int)-num;
115
					padn--;
116
				} else
117
					unum = (unsigned long long int)num;
118

119
120
				count += unsigned_num_print(unum, 10,
							    padc, padn);
121
122
123
				break;
			case 's':
				str = va_arg(args, char *);
124
				count += tf_string_print(str);
125
				break;
126
			case 'p':
127
				unum = (uintptr_t)va_arg(args, void *);
128
				if (unum) {
129
					count += tf_string_print("0x");
130
131
					padn -= 2;
				}
132

133
134
				count += unsigned_num_print(unum, 16,
							    padc, padn);
135
				break;
136
			case 'x':
137
				unum = get_unum_va_args(args, l_count);
138
139
				count += unsigned_num_print(unum, 16,
							    padc, padn);
140
				break;
141
142
			case 'z':
				if (sizeof(size_t) == 8)
143
144
					l_count = 2;

145
146
				fmt++;
				goto loop;
147
			case 'l':
148
				l_count++;
149
150
151
				fmt++;
				goto loop;
			case 'u':
152
				unum = get_unum_va_args(args, l_count);
153
154
				count += unsigned_num_print(unum, 10,
							    padc, padn);
155
				break;
156
157
158
159
160
161
162
163
164
165
166
167
168
			case '0':
				padc = '0';
				padn = 0;
				fmt++;

				while (1) {
					char ch = *fmt;
					if (ch < '0' || ch > '9') {
						goto loop;
					}
					padn = (padn * 10) + (ch - '0');
					fmt++;
				}
169
170
			default:
				/* Exit on any other format specifier */
171
				return -1;
172
173
174
175
176
			}
			fmt++;
			continue;
		}
		putchar(*fmt++);
177
		count++;
178
	}
179
180

	return count;
181
182
}

183
int tf_printf(const char *fmt, ...)
184
{
185
	int count;
186
187
188
	va_list va;

	va_start(va, fmt);
189
	count = tf_vprintf(fmt, va);
190
	va_end(va);
191
192

	return count;
193
}