blob: 40455438397f4cf66e6e938b4fe08095eee0503c [file] [log] [blame]
Damien Roth4e7e9872016-01-16 18:59:51 -07001/*
2 * This file is part of the coreboot project.
Stefan Reinauer34e3a142004-05-28 15:07:03 +00003 *
Damien Roth4e7e9872016-01-16 18:59:51 -07004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * vtxprintf.c, originally from linux/lib/vsprintf.c
Stefan Reinauer34e3a142004-05-28 15:07:03 +000014 */
15
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000016#include <console/vtxprintf.h>
Joel Kitching393c71c2019-06-16 16:09:42 +080017#include <ctype.h>
Edward O'Callaghan0ddb8262014-06-17 18:37:08 +100018#include <string.h>
Jacob Garberc764fb22019-07-03 12:38:46 -060019#include <stdint.h>
Stefan Reinauer34e3a142004-05-28 15:07:03 +000020
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020021#define call_tx(x) tx_byte(x, data)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010022
Stefan Reinauer34e3a142004-05-28 15:07:03 +000023#define ZEROPAD 1 /* pad with zero */
24#define SIGN 2 /* unsigned/signed long */
25#define PLUS 4 /* show plus */
26#define SPACE 8 /* space if plus */
27#define LEFT 16 /* left justified */
28#define SPECIAL 32 /* 0x */
29#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
30
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020031static int number(void (*tx_byte)(unsigned char byte, void *data),
Vadim Bendebury9b0584672014-11-29 22:05:09 -080032 unsigned long long inum, int base, int size, int precision, int type,
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020033 void *data)
Stefan Reinauer34e3a142004-05-28 15:07:03 +000034{
Elyes HAOUASa4184142018-05-15 20:57:01 +020035 char c, sign, tmp[66];
Jacob Garberb19946c2019-07-03 14:40:11 -060036 const char *digits = "0123456789abcdef";
Stefan Reinauer34e3a142004-05-28 15:07:03 +000037 int i;
38 int count = 0;
Vadim Bendebury9b0584672014-11-29 22:05:09 -080039 unsigned long long num = inum;
Julius Werner7c712bb2019-05-01 16:51:20 -070040 long long snum = num;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000041
42 if (type & LARGE)
Jacob Garberb19946c2019-07-03 14:40:11 -060043 digits = "0123456789ABCDEF";
Stefan Reinauer34e3a142004-05-28 15:07:03 +000044 if (type & LEFT)
45 type &= ~ZEROPAD;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000046 c = (type & ZEROPAD) ? '0' : ' ';
47 sign = 0;
48 if (type & SIGN) {
Julius Werner7c712bb2019-05-01 16:51:20 -070049 if (snum < 0) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +000050 sign = '-';
Julius Werner7c712bb2019-05-01 16:51:20 -070051 num = -snum;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000052 size--;
53 } else if (type & PLUS) {
54 sign = '+';
55 size--;
56 } else if (type & SPACE) {
57 sign = ' ';
58 size--;
59 }
60 }
61 if (type & SPECIAL) {
62 if (base == 16)
63 size -= 2;
64 else if (base == 8)
65 size--;
66 }
67 i = 0;
Elyes HAOUASa4184142018-05-15 20:57:01 +020068 if (num == 0) {
69 tmp[i++] = '0';
70 } else {
71 while (num != 0) {
72 tmp[i++] = digits[num % base];
73 num /= base;
74 }
Ronald G. Minnich79e36d92013-01-30 14:29:34 -080075 }
Elyes HAOUASa4184142018-05-15 20:57:01 +020076 if (i > precision) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +000077 precision = i;
Elyes HAOUASa4184142018-05-15 20:57:01 +020078 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +000079 size -= precision;
Elyes HAOUASa4184142018-05-15 20:57:01 +020080 if (!(type&(ZEROPAD+LEFT))) {
Elyes HAOUAS2e0f3d72018-05-03 09:36:28 +020081 while (size-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010082 call_tx(' '), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +020083 }
84 if (sign) {
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010085 call_tx(sign), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +020086 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +000087 if (type & SPECIAL) {
Elyes HAOUAS20767ba2018-04-26 10:00:35 +020088 if (base == 8)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010089 call_tx('0'), count++;
Elyes HAOUAS20767ba2018-04-26 10:00:35 +020090 else if (base == 16) {
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010091 call_tx('0'), count++;
Jacob Garber5b994812019-07-10 11:44:45 -060092 if (type & LARGE)
93 call_tx('X'), count++;
94 else
95 call_tx('x'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000096 }
97 }
Elyes HAOUASa4184142018-05-15 20:57:01 +020098 if (!(type & LEFT)) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +000099 while (size-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100100 call_tx(c), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +0200101 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000102 while (i < precision--)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100103 call_tx('0'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000104 while (i-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100105 call_tx(tmp[i]), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000106 while (size-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100107 call_tx(' '), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000108 return count;
109}
110
111
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +0200112int vtxprintf(void (*tx_byte)(unsigned char byte, void *data),
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100113 const char *fmt, va_list args, void *data)
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000114{
115 int len;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000116 unsigned long long num;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000117 int i, base;
118 const char *s;
119
120 int flags; /* flags to number() */
121
122 int field_width; /* width of output field */
123 int precision; /* min. # of digits for integers; max
124 number of chars for from string */
Jacob Garberc764fb22019-07-03 12:38:46 -0600125 int qualifier; /* 'h', 'H', 'l', 'L', 'z', or 'j' for integer fields */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000126
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000127 int count;
128
Elyes HAOUASa4184142018-05-15 20:57:01 +0200129 for (count = 0; *fmt ; ++fmt) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000130 if (*fmt != '%') {
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100131 call_tx(*fmt), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000132 continue;
133 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000134
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000135 /* process flags */
136 flags = 0;
Patrick Georgic5fc7db2012-03-07 15:55:47 +0100137repeat:
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000138 ++fmt; /* this also skips first '%' */
139 switch (*fmt) {
140 case '-': flags |= LEFT; goto repeat;
141 case '+': flags |= PLUS; goto repeat;
142 case ' ': flags |= SPACE; goto repeat;
143 case '#': flags |= SPECIAL; goto repeat;
144 case '0': flags |= ZEROPAD; goto repeat;
145 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000146
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000147 /* get field width */
148 field_width = -1;
Julius Werner66c77c22019-05-13 17:45:27 -0700149 if (isdigit(*fmt)) {
150 field_width = skip_atoi((char **)&fmt);
Elyes HAOUASa4184142018-05-15 20:57:01 +0200151 } else if (*fmt == '*') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000152 ++fmt;
153 /* it's the next argument */
154 field_width = va_arg(args, int);
155 if (field_width < 0) {
156 field_width = -field_width;
157 flags |= LEFT;
158 }
159 }
160
161 /* get the precision */
162 precision = -1;
163 if (*fmt == '.') {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000164 ++fmt;
Julius Werner66c77c22019-05-13 17:45:27 -0700165 if (isdigit(*fmt)) {
166 precision = skip_atoi((char **)&fmt);
Elyes HAOUASa4184142018-05-15 20:57:01 +0200167 } else if (*fmt == '*') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000168 ++fmt;
169 /* it's the next argument */
170 precision = va_arg(args, int);
171 }
Elyes HAOUASa4184142018-05-15 20:57:01 +0200172 if (precision < 0) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000173 precision = 0;
Elyes HAOUASa4184142018-05-15 20:57:01 +0200174 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000175 }
176
177 /* get the conversion qualifier */
178 qualifier = -1;
Jacob Garberc764fb22019-07-03 12:38:46 -0600179 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'z' || *fmt == 'j') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000180 qualifier = *fmt;
181 ++fmt;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000182 if (*fmt == 'l') {
183 qualifier = 'L';
184 ++fmt;
185 }
Patrick Georgid01ed752014-01-18 16:56:36 +0100186 if (*fmt == 'h') {
187 qualifier = 'H';
188 ++fmt;
189 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000190 }
191
192 /* default base */
193 base = 10;
194
195 switch (*fmt) {
196 case 'c':
197 if (!(flags & LEFT))
198 while (--field_width > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100199 call_tx(' '), count++;
200 call_tx((unsigned char) va_arg(args, int)), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000201 while (--field_width > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100202 call_tx(' '), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000203 continue;
204
205 case 's':
206 s = va_arg(args, char *);
207 if (!s)
208 s = "<NULL>";
209
Martin Rothad6c8852016-11-18 11:35:01 -0700210 len = strnlen(s, (size_t)precision);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000211
Elyes HAOUASa4184142018-05-15 20:57:01 +0200212 if (!(flags & LEFT)) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000213 while (len < field_width--)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100214 call_tx(' '), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +0200215 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000216 for (i = 0; i < len; ++i)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100217 call_tx(*s++), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000218 while (len < field_width--)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100219 call_tx(' '), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000220 continue;
221
222 case 'p':
Julius Werner540a9802019-12-09 13:03:29 -0800223 /* even on 64-bit systems, coreboot only resides in the
224 low 4GB so pad pointers to 32-bit for readability. */
225 if (field_width == -1 && precision == -1)
226 precision = 2*sizeof(uint32_t);
227 flags |= SPECIAL;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000228 count += number(tx_byte,
229 (unsigned long) va_arg(args, void *), 16,
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +0200230 field_width, precision, flags, data);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000231 continue;
232
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000233 case 'n':
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000234 if (qualifier == 'L') {
235 long long *ip = va_arg(args, long long *);
236 *ip = count;
237 } else if (qualifier == 'l') {
Elyes HAOUASa4184142018-05-15 20:57:01 +0200238 long *ip = va_arg(args, long *);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000239 *ip = count;
240 } else {
Elyes HAOUASa4184142018-05-15 20:57:01 +0200241 int *ip = va_arg(args, int *);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000242 *ip = count;
243 }
244 continue;
245
246 case '%':
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100247 call_tx('%'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000248 continue;
249
250 /* integer number formats - set up the flags and "break" */
251 case 'o':
252 base = 8;
253 break;
254
255 case 'X':
256 flags |= LARGE;
Jacob Garber4c33a3a2019-07-12 10:34:06 -0600257 /* fall through */
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000258 case 'x':
259 base = 16;
260 break;
261
262 case 'd':
263 case 'i':
264 flags |= SIGN;
265 case 'u':
266 break;
267
268 default:
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100269 call_tx('%'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000270 if (*fmt)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100271 call_tx(*fmt), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000272 else
273 --fmt;
274 continue;
275 }
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000276 if (qualifier == 'L') {
277 num = va_arg(args, unsigned long long);
278 } else if (qualifier == 'l') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000279 num = va_arg(args, unsigned long);
Stefan Reinauerbfff6de2012-05-15 13:28:07 -0700280 } else if (qualifier == 'z') {
281 num = va_arg(args, size_t);
Jacob Garberc764fb22019-07-03 12:38:46 -0600282 } else if (qualifier == 'j') {
283 num = va_arg(args, uintmax_t);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000284 } else if (qualifier == 'h') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000285 num = (unsigned short) va_arg(args, int);
286 if (flags & SIGN)
287 num = (short) num;
Patrick Georgid01ed752014-01-18 16:56:36 +0100288 } else if (qualifier == 'H') {
289 num = (unsigned char) va_arg(args, int);
290 if (flags & SIGN)
291 num = (signed char) num;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000292 } else if (flags & SIGN) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000293 num = va_arg(args, int);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000294 } else {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000295 num = va_arg(args, unsigned int);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000296 }
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +0200297 count += number(tx_byte, num, base, field_width, precision, flags, data);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000298 }
299 return count;
300}