blob: cfba6db420a06a3507b4b6df113f5a1982120da6 [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>
Elyes Haouas419c5782022-10-02 12:33:27 +02009#include <stdarg.h>
Edward O'Callaghan0ddb8262014-06-17 18:37:08 +100010#include <string.h>
Elyes HAOUAS826459d2021-02-11 20:28:22 +010011#include <types.h>
Stefan Reinauer34e3a142004-05-28 15:07:03 +000012
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020013#define call_tx(x) tx_byte(x, data)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010014
Stefan Reinauer34e3a142004-05-28 15:07:03 +000015#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' */
22
Elyes Haouasd0033e32022-05-29 15:34:41 +020023static 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)
Stefan Reinauer34e3a142004-05-28 15:07:03 +000025{
Elyes HAOUASa4184142018-05-15 20:57:01 +020026 char c, sign, tmp[66];
Jacob Garberb19946c2019-07-03 14:40:11 -060027 const char *digits = "0123456789abcdef";
Stefan Reinauer34e3a142004-05-28 15:07:03 +000028 int i;
29 int count = 0;
Vadim Bendebury9b0584672014-11-29 22:05:09 -080030 unsigned long long num = inum;
Julius Werner7c712bb2019-05-01 16:51:20 -070031 long long snum = num;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000032
33 if (type & LARGE)
Jacob Garberb19946c2019-07-03 14:40:11 -060034 digits = "0123456789ABCDEF";
Stefan Reinauer34e3a142004-05-28 15:07:03 +000035 if (type & LEFT)
36 type &= ~ZEROPAD;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000037 c = (type & ZEROPAD) ? '0' : ' ';
38 sign = 0;
39 if (type & SIGN) {
Julius Werner7c712bb2019-05-01 16:51:20 -070040 if (snum < 0) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +000041 sign = '-';
Julius Werner7c712bb2019-05-01 16:51:20 -070042 num = -snum;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000043 size--;
44 } else if (type & PLUS) {
45 sign = '+';
46 size--;
47 } else if (type & SPACE) {
48 sign = ' ';
49 size--;
50 }
51 }
52 if (type & SPECIAL) {
53 if (base == 16)
54 size -= 2;
55 else if (base == 8)
56 size--;
57 }
58 i = 0;
Elyes HAOUASa4184142018-05-15 20:57:01 +020059 if (num == 0) {
60 tmp[i++] = '0';
61 } else {
62 while (num != 0) {
63 tmp[i++] = digits[num % base];
64 num /= base;
65 }
Ronald G. Minnich79e36d92013-01-30 14:29:34 -080066 }
Elyes HAOUASa4184142018-05-15 20:57:01 +020067 if (i > precision) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +000068 precision = i;
Elyes HAOUASa4184142018-05-15 20:57:01 +020069 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +000070 size -= precision;
Elyes Haouas23f27292022-09-13 08:54:49 +020071 if (!(type & (ZEROPAD | LEFT))) {
Elyes HAOUAS2e0f3d72018-05-03 09:36:28 +020072 while (size-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010073 call_tx(' '), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +020074 }
75 if (sign) {
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010076 call_tx(sign), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +020077 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +000078 if (type & SPECIAL) {
Elyes HAOUAS20767ba2018-04-26 10:00:35 +020079 if (base == 8)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010080 call_tx('0'), count++;
Elyes HAOUAS20767ba2018-04-26 10:00:35 +020081 else if (base == 16) {
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010082 call_tx('0'), count++;
Jacob Garber5b994812019-07-10 11:44:45 -060083 if (type & LARGE)
84 call_tx('X'), count++;
85 else
86 call_tx('x'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000087 }
88 }
Elyes HAOUASa4184142018-05-15 20:57:01 +020089 if (!(type & LEFT)) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +000090 while (size-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010091 call_tx(c), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +020092 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +000093 while (i < precision--)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010094 call_tx('0'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000095 while (i-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010096 call_tx(tmp[i]), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000097 while (size-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010098 call_tx(' '), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000099 return count;
100}
101
Elyes Haouasd0033e32022-05-29 15:34:41 +0200102int vtxprintf(void (*tx_byte)(unsigned char byte, void *data), const char *fmt, va_list args,
103 void *data)
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000104{
105 int len;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000106 unsigned long long num;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000107 int i, base;
108 const char *s;
109
110 int flags; /* flags to number() */
111
112 int field_width; /* width of output field */
113 int precision; /* min. # of digits for integers; max
114 number of chars for from string */
Jacob Garberc764fb22019-07-03 12:38:46 -0600115 int qualifier; /* 'h', 'H', 'l', 'L', 'z', or 'j' for integer fields */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000116
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000117 int count;
118
Elyes Haouasd0033e32022-05-29 15:34:41 +0200119 for (count = 0; *fmt; ++fmt) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000120 if (*fmt != '%') {
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100121 call_tx(*fmt), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000122 continue;
123 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000124
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000125 /* process flags */
126 flags = 0;
Patrick Georgic5fc7db2012-03-07 15:55:47 +0100127repeat:
Elyes Haouasd0033e32022-05-29 15:34:41 +0200128 ++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;
135 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000136
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000137 /* get field width */
138 field_width = -1;
Julius Werner66c77c22019-05-13 17:45:27 -0700139 if (isdigit(*fmt)) {
140 field_width = skip_atoi((char **)&fmt);
Elyes HAOUASa4184142018-05-15 20:57:01 +0200141 } else if (*fmt == '*') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000142 ++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;
148 }
149 }
150
151 /* get the precision */
152 precision = -1;
153 if (*fmt == '.') {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000154 ++fmt;
Julius Werner66c77c22019-05-13 17:45:27 -0700155 if (isdigit(*fmt)) {
156 precision = skip_atoi((char **)&fmt);
Elyes HAOUASa4184142018-05-15 20:57:01 +0200157 } else if (*fmt == '*') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000158 ++fmt;
159 /* it's the next argument */
160 precision = va_arg(args, int);
161 }
Elyes HAOUASa4184142018-05-15 20:57:01 +0200162 if (precision < 0) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000163 precision = 0;
Elyes HAOUASa4184142018-05-15 20:57:01 +0200164 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000165 }
166
167 /* get the conversion qualifier */
168 qualifier = -1;
Jacob Garberc764fb22019-07-03 12:38:46 -0600169 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'z' || *fmt == 'j') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000170 qualifier = *fmt;
171 ++fmt;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000172 if (*fmt == 'l') {
173 qualifier = 'L';
174 ++fmt;
175 }
Patrick Georgid01ed752014-01-18 16:56:36 +0100176 if (*fmt == 'h') {
177 qualifier = 'H';
178 ++fmt;
179 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000180 }
181
182 /* default base */
183 base = 10;
184
185 switch (*fmt) {
186 case 'c':
187 if (!(flags & LEFT))
188 while (--field_width > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100189 call_tx(' '), count++;
Elyes Haouasd0033e32022-05-29 15:34:41 +0200190 call_tx((unsigned char)va_arg(args, int)), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000191 while (--field_width > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100192 call_tx(' '), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000193 continue;
194
195 case 's':
196 s = va_arg(args, char *);
197 if (!s)
198 s = "<NULL>";
199
Martin Rothad6c8852016-11-18 11:35:01 -0700200 len = strnlen(s, (size_t)precision);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000201
Elyes HAOUASa4184142018-05-15 20:57:01 +0200202 if (!(flags & LEFT)) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000203 while (len < field_width--)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100204 call_tx(' '), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +0200205 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000206 for (i = 0; i < len; ++i)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100207 call_tx(*s++), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000208 while (len < field_width--)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100209 call_tx(' '), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000210 continue;
211
212 case 'p':
Julius Werner540a9802019-12-09 13:03:29 -0800213 /* 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)
Elyes Haouasd0033e32022-05-29 15:34:41 +0200216 precision = 2 * sizeof(uint32_t);
Julius Werner540a9802019-12-09 13:03:29 -0800217 flags |= SPECIAL;
Elyes Haouasd0033e32022-05-29 15:34:41 +0200218 count += number(tx_byte, (unsigned long)va_arg(args, void *), 16,
219 field_width, precision, flags, data);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000220 continue;
221
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000222 case 'n':
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000223 if (qualifier == 'L') {
224 long long *ip = va_arg(args, long long *);
225 *ip = count;
226 } else if (qualifier == 'l') {
Elyes HAOUASa4184142018-05-15 20:57:01 +0200227 long *ip = va_arg(args, long *);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000228 *ip = count;
229 } else {
Elyes HAOUASa4184142018-05-15 20:57:01 +0200230 int *ip = va_arg(args, int *);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000231 *ip = count;
232 }
233 continue;
234
235 case '%':
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100236 call_tx('%'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000237 continue;
238
239 /* integer number formats - set up the flags and "break" */
240 case 'o':
241 base = 8;
242 break;
243
244 case 'X':
245 flags |= LARGE;
Arthur Heymansfff20212021-03-15 14:56:16 +0100246 __fallthrough;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000247 case 'x':
248 base = 16;
249 break;
250
251 case 'd':
252 case 'i':
253 flags |= SIGN;
Arthur Heymansfff20212021-03-15 14:56:16 +0100254 __fallthrough;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000255 case 'u':
256 break;
257
258 default:
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100259 call_tx('%'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000260 if (*fmt)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100261 call_tx(*fmt), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000262 else
263 --fmt;
264 continue;
265 }
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000266 if (qualifier == 'L') {
267 num = va_arg(args, unsigned long long);
268 } else if (qualifier == 'l') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000269 num = va_arg(args, unsigned long);
Stefan Reinauerbfff6de2012-05-15 13:28:07 -0700270 } else if (qualifier == 'z') {
271 num = va_arg(args, size_t);
Jacob Garberc764fb22019-07-03 12:38:46 -0600272 } else if (qualifier == 'j') {
273 num = va_arg(args, uintmax_t);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000274 } else if (qualifier == 'h') {
Elyes Haouasd0033e32022-05-29 15:34:41 +0200275 num = (unsigned short)va_arg(args, int);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000276 if (flags & SIGN)
Elyes Haouasd0033e32022-05-29 15:34:41 +0200277 num = (short)num;
Patrick Georgid01ed752014-01-18 16:56:36 +0100278 } else if (qualifier == 'H') {
Elyes Haouasd0033e32022-05-29 15:34:41 +0200279 num = (unsigned char)va_arg(args, int);
Patrick Georgid01ed752014-01-18 16:56:36 +0100280 if (flags & SIGN)
Elyes Haouasd0033e32022-05-29 15:34:41 +0200281 num = (signed char)num;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000282 } else if (flags & SIGN) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000283 num = va_arg(args, int);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000284 } else {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000285 num = va_arg(args, unsigned int);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000286 }
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +0200287 count += number(tx_byte, num, base, field_width, precision, flags, data);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000288 }
289 return count;
290}