Documentation: Fix sphinx configuration
[coreboot.git] / src / console / vtxprintf.c
blob273bc7ed729c34a5b7984ee341ee60cdcaf351a3
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 <string.h>
10 #include <stdint.h>
12 #define call_tx(x) tx_byte(x, data)
14 #define ZEROPAD 1 /* pad with zero */
15 #define SIGN 2 /* unsigned/signed long */
16 #define PLUS 4 /* show plus */
17 #define SPACE 8 /* space if plus */
18 #define LEFT 16 /* left justified */
19 #define SPECIAL 32 /* 0x */
20 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
22 static int number(void (*tx_byte)(unsigned char byte, void *data),
23 unsigned long long inum, int base, int size, int precision, int type,
24 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;
103 int vtxprintf(void (*tx_byte)(unsigned char byte, void *data),
104 const char *fmt, va_list args, void *data)
106 int len;
107 unsigned long long num;
108 int i, base;
109 const char *s;
111 int flags; /* flags to number() */
113 int field_width; /* width of output field */
114 int precision; /* min. # of digits for integers; max
115 number of chars for from string */
116 int qualifier; /* 'h', 'H', 'l', 'L', 'z', or 'j' for integer fields */
118 int count;
120 for (count = 0; *fmt ; ++fmt) {
121 if (*fmt != '%') {
122 call_tx(*fmt), count++;
123 continue;
126 /* process flags */
127 flags = 0;
128 repeat:
129 ++fmt; /* this also skips first '%' */
130 switch (*fmt) {
131 case '-': flags |= LEFT; goto repeat;
132 case '+': flags |= PLUS; goto repeat;
133 case ' ': flags |= SPACE; goto repeat;
134 case '#': flags |= SPECIAL; goto repeat;
135 case '0': flags |= ZEROPAD; goto repeat;
138 /* get field width */
139 field_width = -1;
140 if (isdigit(*fmt)) {
141 field_width = skip_atoi((char **)&fmt);
142 } else if (*fmt == '*') {
143 ++fmt;
144 /* it's the next argument */
145 field_width = va_arg(args, int);
146 if (field_width < 0) {
147 field_width = -field_width;
148 flags |= LEFT;
152 /* get the precision */
153 precision = -1;
154 if (*fmt == '.') {
155 ++fmt;
156 if (isdigit(*fmt)) {
157 precision = skip_atoi((char **)&fmt);
158 } else if (*fmt == '*') {
159 ++fmt;
160 /* it's the next argument */
161 precision = va_arg(args, int);
163 if (precision < 0) {
164 precision = 0;
168 /* get the conversion qualifier */
169 qualifier = -1;
170 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'z' || *fmt == 'j') {
171 qualifier = *fmt;
172 ++fmt;
173 if (*fmt == 'l') {
174 qualifier = 'L';
175 ++fmt;
177 if (*fmt == 'h') {
178 qualifier = 'H';
179 ++fmt;
183 /* default base */
184 base = 10;
186 switch (*fmt) {
187 case 'c':
188 if (!(flags & LEFT))
189 while (--field_width > 0)
190 call_tx(' '), count++;
191 call_tx((unsigned char) va_arg(args, int)), count++;
192 while (--field_width > 0)
193 call_tx(' '), count++;
194 continue;
196 case 's':
197 s = va_arg(args, char *);
198 if (!s)
199 s = "<NULL>";
201 len = strnlen(s, (size_t)precision);
203 if (!(flags & LEFT)) {
204 while (len < field_width--)
205 call_tx(' '), count++;
207 for (i = 0; i < len; ++i)
208 call_tx(*s++), count++;
209 while (len < field_width--)
210 call_tx(' '), count++;
211 continue;
213 case 'p':
214 /* even on 64-bit systems, coreboot only resides in the
215 low 4GB so pad pointers to 32-bit for readability. */
216 if (field_width == -1 && precision == -1)
217 precision = 2*sizeof(uint32_t);
218 flags |= SPECIAL;
219 count += number(tx_byte,
220 (unsigned long) va_arg(args, void *), 16,
221 field_width, precision, flags, data);
222 continue;
224 case 'n':
225 if (qualifier == 'L') {
226 long long *ip = va_arg(args, long long *);
227 *ip = count;
228 } else if (qualifier == 'l') {
229 long *ip = va_arg(args, long *);
230 *ip = count;
231 } else {
232 int *ip = va_arg(args, int *);
233 *ip = count;
235 continue;
237 case '%':
238 call_tx('%'), count++;
239 continue;
241 /* integer number formats - set up the flags and "break" */
242 case 'o':
243 base = 8;
244 break;
246 case 'X':
247 flags |= LARGE;
248 /* fall through */
249 case 'x':
250 base = 16;
251 break;
253 case 'd':
254 case 'i':
255 flags |= SIGN;
256 case 'u':
257 break;
259 default:
260 call_tx('%'), count++;
261 if (*fmt)
262 call_tx(*fmt), count++;
263 else
264 --fmt;
265 continue;
267 if (qualifier == 'L') {
268 num = va_arg(args, unsigned long long);
269 } else if (qualifier == 'l') {
270 num = va_arg(args, unsigned long);
271 } else if (qualifier == 'z') {
272 num = va_arg(args, size_t);
273 } else if (qualifier == 'j') {
274 num = va_arg(args, uintmax_t);
275 } else if (qualifier == 'h') {
276 num = (unsigned short) va_arg(args, int);
277 if (flags & SIGN)
278 num = (short) num;
279 } else if (qualifier == 'H') {
280 num = (unsigned char) va_arg(args, int);
281 if (flags & SIGN)
282 num = (signed char) num;
283 } else if (flags & SIGN) {
284 num = va_arg(args, int);
285 } else {
286 num = va_arg(args, unsigned int);
288 count += number(tx_byte, num, base, field_width, precision, flags, data);
290 return count;