printf.c 4.05 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2018, 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
#include <assert.h>
7
8
9
10
11
#include <debug.h>
#include <stdarg.h>
#include <stdint.h>

/***********************************************************
12
 * The printf implementation for all BL stages
13
 ***********************************************************/
14

Daniel Boulby's avatar
Daniel Boulby committed
15
16
17
#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)))
18

Daniel Boulby's avatar
Daniel Boulby committed
19
20
21
#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)))
22

23
static int string_print(const char *str)
24
{
25
26
	int count = 0;

27
28
	assert(str);

29
	while (*str) {
30
		putchar(*str++);
31
32
33
34
		count++;
	}

	return count;
35
36
}

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

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

52
53
54
	if (padn > 0) {
		while (i < padn--) {
			putchar(padc);
55
			count++;
56
57
58
		}
	}

59
	while (--i >= 0) {
60
		putchar(num_buf[i]);
61
62
63
64
		count++;
	}

	return count;
65
66
67
68
}

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

	while (*fmt) {
98
		l_count = 0;
99
		padn = 0;
100
101
102
103
104
105
106
107

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

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

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

142
143
				fmt++;
				goto loop;
144
			case 'l':
145
				l_count++;
146
147
148
				fmt++;
				goto loop;
			case 'u':
149
				unum = get_unum_va_args(args, l_count);
150
151
				count += unsigned_num_print(unum, 10,
							    padc, padn);
152
				break;
153
154
155
156
157
158
159
160
161
162
163
164
165
			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++;
				}
166
167
			default:
				/* Exit on any other format specifier */
168
				return -1;
169
170
171
172
173
			}
			fmt++;
			continue;
		}
		putchar(*fmt++);
174
		count++;
175
	}
176
177

	return count;
178
179
}

180
int printf(const char *fmt, ...)
181
{
182
	int count;
183
184
185
	va_list va;

	va_start(va, fmt);
186
	count = vprintf(fmt, va);
187
	va_end(va);
188
189

	return count;
190
}