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
#include <debug.h>
#include <stdarg.h>
9
#include <stdbool.h>
10
11
#include <stdint.h>

12
13
14
15
#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)))
16

17
18
19
20
#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)))
21

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

26
	assert(str != NULL);
27

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

	return count;
34
35
}

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

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

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

62
	while (--i >= 0) {
63
		(void)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 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
	char padc = '\0'; /* Padding character */
97
	int padn; /* Number of characters to pad */
98
	int count = 0; /* Number of printed characters */
99

100
	while (*fmt != '\0') {
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
				if (num < 0) {
113
					(void)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 += string_print(str);
125
				break;
126
			case 'p':
127
				unum = (uintptr_t)va_arg(args, void *);
128
				if (unum > 0U) {
129
					count += 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
			case 'z':
142
				if (sizeof(size_t) == 8U)
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
			case '0':
				padc = '0';
				padn = 0;
				fmt++;

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

	return count;
182
183
}

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

	va_start(va, fmt);
190
	count = vprintf(fmt, va);
191
	va_end(va);
192
193

	return count;
194
}