printf.c 4.09 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
 */
6

Soby Mathew's avatar
Soby Mathew committed
7
#include <assert.h>
8
#include <stdarg.h>
9
#include <stdbool.h>
10
11
#include <stdint.h>

12
13
#include <common/debug.h>

14
15
16
17
#define get_num_va_args(_args, _lcount)				\
	(((_lcount) > 1)  ? va_arg(_args, long long int) :	\
	(((_lcount) == 1) ? va_arg(_args, long int) :		\
			    va_arg(_args, int)))
18

19
20
21
22
#define get_unum_va_args(_args, _lcount)				\
	(((_lcount) > 1)  ? va_arg(_args, unsigned long long int) :	\
	(((_lcount) == 1) ? va_arg(_args, unsigned long int) :		\
			    va_arg(_args, unsigned int)))
23

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

28
	assert(str != NULL);
29

30
31
	for ( ; *str != '\0'; str++) {
		(void)putchar(*str);
32
33
34
35
		count++;
	}

	return count;
36
37
}

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

	do {
		rem = unum % radix;
		if (rem < 0xa)
49
			num_buf[i] = '0' + rem;
50
		else
51
52
53
54
			num_buf[i] = 'a' + (rem - 0xa);
		i++;
		unum /= radix;
	} while (unum > 0U);
55

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

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

	return count;
70
71
72
73
}

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

102
	while (*fmt != '\0') {
103
		l_count = 0;
104
		padn = 0;
105
106
107
108
109
110
111
112

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

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

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

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

163
				for (;;) {
164
					char ch = *fmt;
165
					if ((ch < '0') || (ch > '9')) {
166
167
168
169
170
						goto loop;
					}
					padn = (padn * 10) + (ch - '0');
					fmt++;
				}
171
				assert(0); /* Unreachable */
172
173
			default:
				/* Exit on any other format specifier */
174
				return -1;
175
176
177
178
			}
			fmt++;
			continue;
		}
179
180
		(void)putchar(*fmt);
		fmt++;
181
		count++;
182
	}
183
184

	return count;
185
186
}

187
int printf(const char *fmt, ...)
188
{
189
	int count;
190
191
192
	va_list va;

	va_start(va, fmt);
193
	count = vprintf(fmt, va);
194
	va_end(va);
195
196

	return count;
197
}