tf_printf.c 3.82 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
void tf_string_print(const char *str)
27
{
28
29
	assert(str);

30
31
32
33
	while (*str)
		putchar(*str++);
}

34
35
static void unsigned_num_print(unsigned long long int unum, unsigned int radix,
			       char padc, int padn)
36
37
38
{
	/* Just need enough space to store 64 bit decimal integer */
	unsigned char num_buf[20];
39
	int i = 0, rem;
40
41
42
43
44
45
46
47
48

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

49
50
51
52
53
54
	if (padn > 0) {
		while (i < padn--) {
			putchar(padc);
		}
	}

55
56
57
58
59
60
	while (--i >= 0)
		putchar(num_buf[i]);
}

/*******************************************************************
 * Reduced format print for Trusted firmware.
61
62
 * The following type specifiers are supported by this print
 * %x - hexadecimal format
63
 * %s - string format
64
65
 * %d or %i - signed decimal format
 * %u - unsigned decimal format
66
 * %p - pointer format
67
68
69
70
71
72
 *
 * 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)
 *
73
74
75
 * The following padding specifiers are supported by this print
 * %0NN - Left-pad the number with 0s (NN is a decimal number)
 *
76
77
 * The print exits on all other formats specifiers other than valid
 * combinations of the above specifiers.
78
 *******************************************************************/
79
void tf_vprintf(const char *fmt, va_list args)
80
{
81
82
83
	int l_count;
	long long int num;
	unsigned long long int unum;
84
	char *str;
85
86
	char padc = 0; /* Padding character */
	int padn; /* Number of characters to pad */
87
88

	while (*fmt) {
89
		l_count = 0;
90
		padn = 0;
91
92
93
94
95
96
97
98

		if (*fmt == '%') {
			fmt++;
			/* Check the format specifier */
loop:
			switch (*fmt) {
			case 'i': /* Fall through to next one */
			case 'd':
99
				num = get_num_va_args(args, l_count);
100
101
				if (num < 0) {
					putchar('-');
102
					unum = (unsigned long long int)-num;
103
					padn--;
104
				} else
105
					unum = (unsigned long long int)num;
106

107
				unsigned_num_print(unum, 10, padc, padn);
108
109
110
				break;
			case 's':
				str = va_arg(args, char *);
111
				tf_string_print(str);
112
				break;
113
			case 'p':
114
				unum = (uintptr_t)va_arg(args, void *);
115
				if (unum) {
116
					tf_string_print("0x");
117
118
					padn -= 2;
				}
119

120
				unsigned_num_print(unum, 16, padc, padn);
121
				break;
122
			case 'x':
123
				unum = get_unum_va_args(args, l_count);
124
				unsigned_num_print(unum, 16, padc, padn);
125
				break;
126
127
			case 'z':
				if (sizeof(size_t) == 8)
128
129
					l_count = 2;

130
131
				fmt++;
				goto loop;
132
			case 'l':
133
				l_count++;
134
135
136
				fmt++;
				goto loop;
			case 'u':
137
				unum = get_unum_va_args(args, l_count);
138
				unsigned_num_print(unum, 10, padc, padn);
139
				break;
140
141
142
143
144
145
146
147
148
149
150
151
152
			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++;
				}
153
154
			default:
				/* Exit on any other format specifier */
155
				return;
156
157
158
159
160
161
			}
			fmt++;
			continue;
		}
		putchar(*fmt++);
	}
162
163
164
165
166
167
168
169
170
}

void tf_printf(const char *fmt, ...)
{
	va_list va;

	va_start(va, fmt);
	tf_vprintf(fmt, va);
	va_end(va);
171
}