blob: fba76bcb71d7d4e2ac1d6c214a6ab2ec47c86354 [file] [log] [blame]
Angel Ponsb706ab32020-04-02 23:48:09 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Angel Ponsb706ab32020-04-02 23:48:09 +02002
Damien Roth4e7e9872016-01-16 18:59:51 -07003/*
Damien Roth4e7e9872016-01-16 18:59:51 -07004 * vtxprintf.c, originally from linux/lib/vsprintf.c
Stefan Reinauer34e3a142004-05-28 15:07:03 +00005 */
6
Stefan Reinauer52fc6b12009-10-24 13:06:04 +00007#include <console/vtxprintf.h>
Joel Kitching393c71c2019-06-16 16:09:42 +08008#include <ctype.h>
Edward O'Callaghan0ddb8262014-06-17 18:37:08 +10009#include <string.h>
Elyes HAOUAS826459d2021-02-11 20:28:22 +010010#include <types.h>
Stefan Reinauer34e3a142004-05-28 15:07:03 +000011
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020012#define call_tx(x) tx_byte(x, data)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010013
Stefan Reinauer34e3a142004-05-28 15:07:03 +000014#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' */
21
Elyes Haouasd0033e32022-05-29 15:34:41 +020022static 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)
Stefan Reinauer34e3a142004-05-28 15:07:03 +000024{
Elyes HAOUASa4184142018-05-15 20:57:01 +020025 char c, sign, tmp[66];
Jacob Garberb19946c2019-07-03 14:40:11 -060026 const char *digits = "0123456789abcdef";
Stefan Reinauer34e3a142004-05-28 15:07:03 +000027 int i;
28 int count = 0;
Vadim Bendebury9b0584672014-11-29 22:05:09 -080029 unsigned long long num = inum;
Julius Werner7c712bb2019-05-01 16:51:20 -070030 long long snum = num;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000031
32 if (type & LARGE)
Jacob Garberb19946c2019-07-03 14:40:11 -060033 digits = "0123456789ABCDEF";
Stefan Reinauer34e3a142004-05-28 15:07:03 +000034 if (type & LEFT)
35 type &= ~ZEROPAD;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000036 c = (type & ZEROPAD) ? '0' : ' ';
37 sign = 0;
38 if (type & SIGN) {
Julius Werner7c712bb2019-05-01 16:51:20 -070039 if (snum < 0) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +000040 sign = '-';
Julius Werner7c712bb2019-05-01 16:51:20 -070041 num = -snum;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000042 size--;
43 } else if (type & PLUS) {
44 sign = '+';
45 size--;
46 } else if (type & SPACE) {
47 sign = ' ';
48 size--;
49 }
50 }
51 if (type & SPECIAL) {
52 if (base == 16)
53 size -= 2;
54 else if (base == 8)
55 size--;
56 }
57 i = 0;
Elyes HAOUASa4184142018-05-15 20:57:01 +020058 if (num == 0) {
59 tmp[i++] = '0';
60 } else {
61 while (num != 0) {
62 tmp[i++] = digits[num % base];
63 num /= base;
64 }
Ronald G. Minnich79e36d92013-01-30 14:29:34 -080065 }
Elyes HAOUASa4184142018-05-15 20:57:01 +020066 if (i > precision) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +000067 precision = i;
Elyes HAOUASa4184142018-05-15 20:57:01 +020068 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +000069 size -= precision;
Elyes Haouas23f27292022-09-13 08:54:49 +020070 if (!(type & (ZEROPAD | LEFT))) {
Elyes HAOUAS2e0f3d72018-05-03 09:36:28 +020071 while (size-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010072 call_tx(' '), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +020073 }
74 if (sign) {
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010075 call_tx(sign), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +020076 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +000077 if (type & SPECIAL) {
Elyes HAOUAS20767ba2018-04-26 10:00:35 +020078 if (base == 8)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010079 call_tx('0'), count++;
Elyes HAOUAS20767ba2018-04-26 10:00:35 +020080 else if (base == 16) {
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010081 call_tx('0'), count++;
Jacob Garber5b994812019-07-10 11:44:45 -060082 if (type & LARGE)
83 call_tx('X'), count++;
84 else
85 call_tx('x'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000086 }
87 }
Elyes HAOUASa4184142018-05-15 20:57:01 +020088 if (!(type & LEFT)) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +000089 while (size-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010090 call_tx(c), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +020091 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +000092 while (i < precision--)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010093 call_tx('0'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000094 while (i-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010095 call_tx(tmp[i]), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000096 while (size-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010097 call_tx(' '), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000098 return count;
99}
100
Elyes Haouasd0033e32022-05-29 15:34:41 +0200101int vtxprintf(void (*tx_byte)(unsigned char byte, void *data), const char *fmt, va_list args,
102 void *data)
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000103{
104 int len;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000105 unsigned long long num;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000106 int i, base;
107 const char *s;
108
109 int flags; /* flags to number() */
110
111 int field_width; /* width of output field */
112 int precision; /* min. # of digits for integers; max
113 number of chars for from string */
Jacob Garberc764fb22019-07-03 12:38:46 -0600114 int qualifier; /* 'h', 'H', 'l', 'L', 'z', or 'j' for integer fields */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000115
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000116 int count;
117
Elyes Haouasd0033e32022-05-29 15:34:41 +0200118 for (count = 0; *fmt; ++fmt) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000119 if (*fmt != '%') {
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100120 call_tx(*fmt), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000121 continue;
122 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000123
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000124 /* process flags */
125 flags = 0;
Patrick Georgic5fc7db2012-03-07 15:55:47 +0100126repeat:
Elyes Haouasd0033e32022-05-29 15:34:41 +0200127 ++fmt; /* this also skips first '%' */
128 switch (*fmt) {
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;
134 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000135
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000136 /* get field width */
137 field_width = -1;
Julius Werner66c77c22019-05-13 17:45:27 -0700138 if (isdigit(*fmt)) {
139 field_width = skip_atoi((char **)&fmt);
Elyes HAOUASa4184142018-05-15 20:57:01 +0200140 } else if (*fmt == '*') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000141 ++fmt;
142 /* it's the next argument */
143 field_width = va_arg(args, int);
144 if (field_width < 0) {
145 field_width = -field_width;
146 flags |= LEFT;
147 }
148 }
149
150 /* get the precision */
151 precision = -1;
152 if (*fmt == '.') {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000153 ++fmt;
Julius Werner66c77c22019-05-13 17:45:27 -0700154 if (isdigit(*fmt)) {
155 precision = skip_atoi((char **)&fmt);
Elyes HAOUASa4184142018-05-15 20:57:01 +0200156 } else if (*fmt == '*') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000157 ++fmt;
158 /* it's the next argument */
159 precision = va_arg(args, int);
160 }
Elyes HAOUASa4184142018-05-15 20:57:01 +0200161 if (precision < 0) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000162 precision = 0;
Elyes HAOUASa4184142018-05-15 20:57:01 +0200163 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000164 }
165
166 /* get the conversion qualifier */
167 qualifier = -1;
Jacob Garberc764fb22019-07-03 12:38:46 -0600168 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'z' || *fmt == 'j') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000169 qualifier = *fmt;
170 ++fmt;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000171 if (*fmt == 'l') {
172 qualifier = 'L';
173 ++fmt;
174 }
Patrick Georgid01ed752014-01-18 16:56:36 +0100175 if (*fmt == 'h') {
176 qualifier = 'H';
177 ++fmt;
178 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000179 }
180
181 /* default base */
182 base = 10;
183
184 switch (*fmt) {
185 case 'c':
186 if (!(flags & LEFT))
187 while (--field_width > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100188 call_tx(' '), count++;
Elyes Haouasd0033e32022-05-29 15:34:41 +0200189 call_tx((unsigned char)va_arg(args, int)), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000190 while (--field_width > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100191 call_tx(' '), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000192 continue;
193
194 case 's':
195 s = va_arg(args, char *);
196 if (!s)
197 s = "<NULL>";
198
Martin Rothad6c8852016-11-18 11:35:01 -0700199 len = strnlen(s, (size_t)precision);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000200
Elyes HAOUASa4184142018-05-15 20:57:01 +0200201 if (!(flags & LEFT)) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000202 while (len < field_width--)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100203 call_tx(' '), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +0200204 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000205 for (i = 0; i < len; ++i)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100206 call_tx(*s++), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000207 while (len < field_width--)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100208 call_tx(' '), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000209 continue;
210
211 case 'p':
Julius Werner540a9802019-12-09 13:03:29 -0800212 /* 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)
Elyes Haouasd0033e32022-05-29 15:34:41 +0200215 precision = 2 * sizeof(uint32_t);
Julius Werner540a9802019-12-09 13:03:29 -0800216 flags |= SPECIAL;
Elyes Haouasd0033e32022-05-29 15:34:41 +0200217 count += number(tx_byte, (unsigned long)va_arg(args, void *), 16,
218 field_width, precision, flags, data);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000219 continue;
220
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000221 case 'n':
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000222 if (qualifier == 'L') {
223 long long *ip = va_arg(args, long long *);
224 *ip = count;
225 } else if (qualifier == 'l') {
Elyes HAOUASa4184142018-05-15 20:57:01 +0200226 long *ip = va_arg(args, long *);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000227 *ip = count;
228 } else {
Elyes HAOUASa4184142018-05-15 20:57:01 +0200229 int *ip = va_arg(args, int *);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000230 *ip = count;
231 }
232 continue;
233
234 case '%':
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100235 call_tx('%'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000236 continue;
237
238 /* integer number formats - set up the flags and "break" */
239 case 'o':
240 base = 8;
241 break;
242
243 case 'X':
244 flags |= LARGE;
Arthur Heymansfff20212021-03-15 14:56:16 +0100245 __fallthrough;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000246 case 'x':
247 base = 16;
248 break;
249
250 case 'd':
251 case 'i':
252 flags |= SIGN;
Arthur Heymansfff20212021-03-15 14:56:16 +0100253 __fallthrough;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000254 case 'u':
255 break;
256
257 default:
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100258 call_tx('%'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000259 if (*fmt)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100260 call_tx(*fmt), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000261 else
262 --fmt;
263 continue;
264 }
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000265 if (qualifier == 'L') {
266 num = va_arg(args, unsigned long long);
267 } else if (qualifier == 'l') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000268 num = va_arg(args, unsigned long);
Stefan Reinauerbfff6de2012-05-15 13:28:07 -0700269 } else if (qualifier == 'z') {
270 num = va_arg(args, size_t);
Jacob Garberc764fb22019-07-03 12:38:46 -0600271 } else if (qualifier == 'j') {
272 num = va_arg(args, uintmax_t);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000273 } else if (qualifier == 'h') {
Elyes Haouasd0033e32022-05-29 15:34:41 +0200274 num = (unsigned short)va_arg(args, int);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000275 if (flags & SIGN)
Elyes Haouasd0033e32022-05-29 15:34:41 +0200276 num = (short)num;
Patrick Georgid01ed752014-01-18 16:56:36 +0100277 } else if (qualifier == 'H') {
Elyes Haouasd0033e32022-05-29 15:34:41 +0200278 num = (unsigned char)va_arg(args, int);
Patrick Georgid01ed752014-01-18 16:56:36 +0100279 if (flags & SIGN)
Elyes Haouasd0033e32022-05-29 15:34:41 +0200280 num = (signed char)num;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000281 } else if (flags & SIGN) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000282 num = va_arg(args, int);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000283 } else {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000284 num = va_arg(args, unsigned int);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000285 }
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +0200286 count += number(tx_byte, num, base, field_width, precision, flags, data);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000287 }
288 return count;
289}