payloads/edk2: Disable the CPU Timer Lib unless supported
[coreboot.git] / src / console / vtxprintf.c
blobcfba6db420a06a3507b4b6df113f5a1982120da6
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 /*
4 * vtxprintf.c, originally from linux/lib/vsprintf.c
5 */
7 #include <console/vtxprintf.h>
8 #include <ctype.h>
9 #include <stdarg.h>
10 #include <string.h>
11 #include <types.h>
13 #define call_tx(x) tx_byte(x, data)
15 #define ZEROPAD 1 /* pad with zero */
16 #define SIGN 2 /* unsigned/signed long */
17 #define PLUS 4 /* show plus */
18 #define SPACE 8 /* space if plus */
19 #define LEFT 16 /* left justified */
20 #define SPECIAL 32 /* 0x */
21 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
23 static int number(void (*tx_byte)(unsigned char byte, void *data), unsigned long long inum,
24 int base, int size, int precision, int type, void *data)
26 char c, sign, tmp[66];
27 const char *digits = "0123456789abcdef";
28 int i;
29 int count = 0;
30 unsigned long long num = inum;
31 long long snum = num;
33 if (type & LARGE)
34 digits = "0123456789ABCDEF";
35 if (type & LEFT)
36 type &= ~ZEROPAD;
37 c = (type & ZEROPAD) ? '0' : ' ';
38 sign = 0;
39 if (type & SIGN) {
40 if (snum < 0) {
41 sign = '-';
42 num = -snum;
43 size--;
44 } else if (type & PLUS) {
45 sign = '+';
46 size--;
47 } else if (type & SPACE) {
48 sign = ' ';
49 size--;
52 if (type & SPECIAL) {
53 if (base == 16)
54 size -= 2;
55 else if (base == 8)
56 size--;
58 i = 0;
59 if (num == 0) {
60 tmp[i++] = '0';
61 } else {
62 while (num != 0) {
63 tmp[i++] = digits[num % base];
64 num /= base;
67 if (i > precision) {
68 precision = i;
70 size -= precision;
71 if (!(type & (ZEROPAD | LEFT))) {
72 while (size-- > 0)
73 call_tx(' '), count++;
75 if (sign) {
76 call_tx(sign), count++;
78 if (type & SPECIAL) {
79 if (base == 8)
80 call_tx('0'), count++;
81 else if (base == 16) {
82 call_tx('0'), count++;
83 if (type & LARGE)
84 call_tx('X'), count++;
85 else
86 call_tx('x'), count++;
89 if (!(type & LEFT)) {
90 while (size-- > 0)
91 call_tx(c), count++;
93 while (i < precision--)
94 call_tx('0'), count++;
95 while (i-- > 0)
96 call_tx(tmp[i]), count++;
97 while (size-- > 0)
98 call_tx(' '), count++;
99 return count;
102 int vtxprintf(void (*tx_byte)(unsigned char byte, void *data), const char *fmt, va_list args,
103 void *data)
105 int len;
106 unsigned long long num;
107 int i, base;
108 const char *s;
110 int flags; /* flags to number() */
112 int field_width; /* width of output field */
113 int precision; /* min. # of digits for integers; max
114 number of chars for from string */
115 int qualifier; /* 'h', 'H', 'l', 'L', 'z', or 'j' for integer fields */
117 int count;
119 for (count = 0; *fmt; ++fmt) {
120 if (*fmt != '%') {
121 call_tx(*fmt), count++;
122 continue;
125 /* process flags */
126 flags = 0;
127 repeat:
128 ++fmt; /* this also skips first '%' */
129 switch (*fmt) {
130 case '-': flags |= LEFT; goto repeat;
131 case '+': flags |= PLUS; goto repeat;
132 case ' ': flags |= SPACE; goto repeat;
133 case '#': flags |= SPECIAL; goto repeat;
134 case '0': flags |= ZEROPAD; goto repeat;
137 /* get field width */
138 field_width = -1;
139 if (isdigit(*fmt)) {
140 field_width = skip_atoi((char **)&fmt);
141 } else if (*fmt == '*') {
142 ++fmt;
143 /* it's the next argument */
144 field_width = va_arg(args, int);
145 if (field_width < 0) {
146 field_width = -field_width;
147 flags |= LEFT;
151 /* get the precision */
152 precision = -1;
153 if (*fmt == '.') {
154 ++fmt;
155 if (isdigit(*fmt)) {
156 precision = skip_atoi((char **)&fmt);
157 } else if (*fmt == '*') {
158 ++fmt;
159 /* it's the next argument */
160 precision = va_arg(args, int);
162 if (precision < 0) {
163 precision = 0;
167 /* get the conversion qualifier */
168 qualifier = -1;
169 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'z' || *fmt == 'j') {
170 qualifier = *fmt;
171 ++fmt;
172 if (*fmt == 'l') {
173 qualifier = 'L';
174 ++fmt;
176 if (*fmt == 'h') {
177 qualifier = 'H';
178 ++fmt;
182 /* default base */
183 base = 10;
185 switch (*fmt) {
186 case 'c':
187 if (!(flags & LEFT))
188 while (--field_width > 0)
189 call_tx(' '), count++;
190 call_tx((unsigned char)va_arg(args, int)), count++;
191 while (--field_width > 0)
192 call_tx(' '), count++;
193 continue;
195 case 's':
196 s = va_arg(args, char *);
197 if (!s)
198 s = "<NULL>";
200 len = strnlen(s, (size_t)precision);
202 if (!(flags & LEFT)) {
203 while (len < field_width--)
204 call_tx(' '), count++;
206 for (i = 0; i < len; ++i)
207 call_tx(*s++), count++;
208 while (len < field_width--)
209 call_tx(' '), count++;
210 continue;
212 case 'p':
213 /* even on 64-bit systems, coreboot only resides in the
214 low 4GB so pad pointers to 32-bit for readability. */
215 if (field_width == -1 && precision == -1)
216 precision = 2 * sizeof(uint32_t);
217 flags |= SPECIAL;
218 count += number(tx_byte, (unsigned long)va_arg(args, void *), 16,
219 field_width, precision, flags, data);
220 continue;
222 case 'n':
223 if (qualifier == 'L') {
224 long long *ip = va_arg(args, long long *);
225 *ip = count;
226 } else if (qualifier == 'l') {
227 long *ip = va_arg(args, long *);
228 *ip = count;
229 } else {
230 int *ip = va_arg(args, int *);
231 *ip = count;
233 continue;
235 case '%':
236 call_tx('%'), count++;
237 continue;
239 /* integer number formats - set up the flags and "break" */
240 case 'o':
241 base = 8;
242 break;
244 case 'X':
245 flags |= LARGE;
246 __fallthrough;
247 case 'x':
248 base = 16;
249 break;
251 case 'd':
252 case 'i':
253 flags |= SIGN;
254 __fallthrough;
255 case 'u':
256 break;
258 default:
259 call_tx('%'), count++;
260 if (*fmt)
261 call_tx(*fmt), count++;
262 else
263 --fmt;
264 continue;
266 if (qualifier == 'L') {
267 num = va_arg(args, unsigned long long);
268 } else if (qualifier == 'l') {
269 num = va_arg(args, unsigned long);
270 } else if (qualifier == 'z') {
271 num = va_arg(args, size_t);
272 } else if (qualifier == 'j') {
273 num = va_arg(args, uintmax_t);
274 } else if (qualifier == 'h') {
275 num = (unsigned short)va_arg(args, int);
276 if (flags & SIGN)
277 num = (short)num;
278 } else if (qualifier == 'H') {
279 num = (unsigned char)va_arg(args, int);
280 if (flags & SIGN)
281 num = (signed char)num;
282 } else if (flags & SIGN) {
283 num = va_arg(args, int);
284 } else {
285 num = va_arg(args, unsigned int);
287 count += number(tx_byte, num, base, field_width, precision, flags, data);
289 return count;