1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * vtxprintf.c, originally from linux/lib/vsprintf.c
7 #include <console/vtxprintf.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
), unsigned long long inum
,
23 int base
, int size
, int precision
, int type
, void *data
)
25 char c
, sign
, tmp
[66];
26 const char *digits
= "0123456789abcdef";
29 unsigned long long num
= inum
;
33 digits
= "0123456789ABCDEF";
36 c
= (type
& ZEROPAD
) ? '0' : ' ';
43 } else if (type
& PLUS
) {
46 } else if (type
& SPACE
) {
62 tmp
[i
++] = digits
[num
% base
];
70 if (!(type
& (ZEROPAD
| LEFT
))) {
72 call_tx(' '), count
++;
75 call_tx(sign
), count
++;
79 call_tx('0'), count
++;
80 else if (base
== 16) {
81 call_tx('0'), count
++;
83 call_tx('X'), count
++;
85 call_tx('x'), count
++;
92 while (i
< precision
--)
93 call_tx('0'), count
++;
95 call_tx(tmp
[i
]), count
++;
97 call_tx(' '), count
++;
101 int vtxprintf(void (*tx_byte
)(unsigned char byte
, void *data
), const char *fmt
, va_list args
,
105 unsigned long long num
;
109 int flags
; /* flags to number() */
111 int field_width
; /* width of output field */
112 int precision
; /* min. # of digits for integers; max
113 number of chars for from string */
114 int qualifier
; /* 'h', 'H', 'l', 'L', 'z', or 'j' for integer fields */
118 for (count
= 0; *fmt
; ++fmt
) {
120 call_tx(*fmt
), count
++;
127 ++fmt
; /* this also skips first '%' */
129 case '-': flags
|= LEFT
; goto repeat
;
130 case '+': flags
|= PLUS
; goto repeat
;
131 case ' ': flags
|= SPACE
; goto repeat
;
132 case '#': flags
|= SPECIAL
; goto repeat
;
133 case '0': flags
|= ZEROPAD
; goto repeat
;
136 /* get field width */
139 field_width
= skip_atoi((char **)&fmt
);
140 } else if (*fmt
== '*') {
142 /* it's the next argument */
143 field_width
= va_arg(args
, int);
144 if (field_width
< 0) {
145 field_width
= -field_width
;
150 /* get the precision */
155 precision
= skip_atoi((char **)&fmt
);
156 } else if (*fmt
== '*') {
158 /* it's the next argument */
159 precision
= va_arg(args
, int);
166 /* get the conversion qualifier */
168 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' || *fmt
== 'z' || *fmt
== 'j') {
187 while (--field_width
> 0)
188 call_tx(' '), count
++;
189 call_tx((unsigned char)va_arg(args
, int)), count
++;
190 while (--field_width
> 0)
191 call_tx(' '), count
++;
195 s
= va_arg(args
, char *);
199 len
= strnlen(s
, (size_t)precision
);
201 if (!(flags
& LEFT
)) {
202 while (len
< field_width
--)
203 call_tx(' '), count
++;
205 for (i
= 0; i
< len
; ++i
)
206 call_tx(*s
++), count
++;
207 while (len
< field_width
--)
208 call_tx(' '), count
++;
212 /* even on 64-bit systems, coreboot only resides in the
213 low 4GB so pad pointers to 32-bit for readability. */
214 if (field_width
== -1 && precision
== -1)
215 precision
= 2 * sizeof(uint32_t);
217 count
+= number(tx_byte
, (unsigned long)va_arg(args
, void *), 16,
218 field_width
, precision
, flags
, data
);
222 if (qualifier
== 'L') {
223 long long *ip
= va_arg(args
, long long *);
225 } else if (qualifier
== 'l') {
226 long *ip
= va_arg(args
, long *);
229 int *ip
= va_arg(args
, int *);
235 call_tx('%'), count
++;
238 /* integer number formats - set up the flags and "break" */
258 call_tx('%'), count
++;
260 call_tx(*fmt
), count
++;
265 if (qualifier
== 'L') {
266 num
= va_arg(args
, unsigned long long);
267 } else if (qualifier
== 'l') {
268 num
= va_arg(args
, unsigned long);
269 } else if (qualifier
== 'z') {
270 num
= va_arg(args
, size_t);
271 } else if (qualifier
== 'j') {
272 num
= va_arg(args
, uintmax_t);
273 } else if (qualifier
== 'h') {
274 num
= (unsigned short)va_arg(args
, int);
277 } else if (qualifier
== 'H') {
278 num
= (unsigned char)va_arg(args
, int);
280 num
= (signed char)num
;
281 } else if (flags
& SIGN
) {
282 num
= va_arg(args
, int);
284 num
= va_arg(args
, unsigned int);
286 count
+= number(tx_byte
, num
, base
, field_width
, precision
, flags
, data
);