1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * vtxprintf.c, originally from linux/lib/vsprintf.c
7 #include <console/vtxprintf.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";
30 unsigned long long num
= inum
;
34 digits
= "0123456789ABCDEF";
37 c
= (type
& ZEROPAD
) ? '0' : ' ';
44 } else if (type
& PLUS
) {
47 } else if (type
& SPACE
) {
63 tmp
[i
++] = digits
[num
% base
];
71 if (!(type
& (ZEROPAD
| LEFT
))) {
73 call_tx(' '), count
++;
76 call_tx(sign
), count
++;
80 call_tx('0'), count
++;
81 else if (base
== 16) {
82 call_tx('0'), count
++;
84 call_tx('X'), count
++;
86 call_tx('x'), count
++;
93 while (i
< precision
--)
94 call_tx('0'), count
++;
96 call_tx(tmp
[i
]), count
++;
98 call_tx(' '), count
++;
102 int vtxprintf(void (*tx_byte
)(unsigned char byte
, void *data
), const char *fmt
, va_list args
,
106 unsigned long long num
;
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 */
119 for (count
= 0; *fmt
; ++fmt
) {
121 call_tx(*fmt
), count
++;
128 ++fmt
; /* this also skips first '%' */
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 */
140 field_width
= skip_atoi((char **)&fmt
);
141 } else if (*fmt
== '*') {
143 /* it's the next argument */
144 field_width
= va_arg(args
, int);
145 if (field_width
< 0) {
146 field_width
= -field_width
;
151 /* get the precision */
156 precision
= skip_atoi((char **)&fmt
);
157 } else if (*fmt
== '*') {
159 /* it's the next argument */
160 precision
= va_arg(args
, int);
167 /* get the conversion qualifier */
169 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
== 'z' || *fmt
== 'j') {
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
++;
196 s
= va_arg(args
, char *);
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
++;
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);
218 count
+= number(tx_byte
, (unsigned long)va_arg(args
, void *), 16,
219 field_width
, precision
, flags
, data
);
223 if (qualifier
== 'L') {
224 long long *ip
= va_arg(args
, long long *);
226 } else if (qualifier
== 'l') {
227 long *ip
= va_arg(args
, long *);
230 int *ip
= va_arg(args
, int *);
236 call_tx('%'), count
++;
239 /* integer number formats - set up the flags and "break" */
259 call_tx('%'), count
++;
261 call_tx(*fmt
), count
++;
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);
278 } else if (qualifier
== 'H') {
279 num
= (unsigned char)va_arg(args
, int);
281 num
= (signed char)num
;
282 } else if (flags
& SIGN
) {
283 num
= va_arg(args
, int);
285 num
= va_arg(args
, unsigned int);
287 count
+= number(tx_byte
, num
, base
, field_width
, precision
, flags
, data
);