blob: 1efe55a332d9185fc26c148da51d32106af37b1a [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
Julius Wernercd49cce2019-03-05 16:53:33 -080023#if !CONFIG(ARCH_MIPS)
Vadim Bendebury9b0584672014-11-29 22:05:09 -080024#define SUPPORT_64BIT_INTS
25#endif
26
Stefan Reinauer34e3a142004-05-28 15:07:03 +000027#define ZEROPAD 1 /* pad with zero */
28#define SIGN 2 /* unsigned/signed long */
29#define PLUS 4 /* show plus */
30#define SPACE 8 /* space if plus */
31#define LEFT 16 /* left justified */
32#define SPECIAL 32 /* 0x */
33#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
34
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020035static int number(void (*tx_byte)(unsigned char byte, void *data),
Vadim Bendebury9b0584672014-11-29 22:05:09 -080036 unsigned long long inum, int base, int size, int precision, int type,
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020037 void *data)
Stefan Reinauer34e3a142004-05-28 15:07:03 +000038{
Elyes HAOUASa4184142018-05-15 20:57:01 +020039 char c, sign, tmp[66];
Jacob Garberb19946c2019-07-03 14:40:11 -060040 const char *digits = "0123456789abcdef";
Stefan Reinauer34e3a142004-05-28 15:07:03 +000041 int i;
42 int count = 0;
Vadim Bendebury9b0584672014-11-29 22:05:09 -080043#ifdef SUPPORT_64BIT_INTS
44 unsigned long long num = inum;
Julius Werner7c712bb2019-05-01 16:51:20 -070045 long long snum = num;
Vadim Bendebury9b0584672014-11-29 22:05:09 -080046#else
Julius Werner7c712bb2019-05-01 16:51:20 -070047 unsigned long num = (unsigned long)inum;
48 long snum = (long)num;
Vadim Bendebury9b0584672014-11-29 22:05:09 -080049
50 if (num != inum) {
51 /* Alert user to an incorrect result by printing #^!. */
52 call_tx('#');
53 call_tx('^');
54 call_tx('!');
55 }
56#endif
Stefan Reinauer34e3a142004-05-28 15:07:03 +000057
58 if (type & LARGE)
Jacob Garberb19946c2019-07-03 14:40:11 -060059 digits = "0123456789ABCDEF";
Stefan Reinauer34e3a142004-05-28 15:07:03 +000060 if (type & LEFT)
61 type &= ~ZEROPAD;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000062 c = (type & ZEROPAD) ? '0' : ' ';
63 sign = 0;
64 if (type & SIGN) {
Julius Werner7c712bb2019-05-01 16:51:20 -070065 if (snum < 0) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +000066 sign = '-';
Julius Werner7c712bb2019-05-01 16:51:20 -070067 num = -snum;
Stefan Reinauer34e3a142004-05-28 15:07:03 +000068 size--;
69 } else if (type & PLUS) {
70 sign = '+';
71 size--;
72 } else if (type & SPACE) {
73 sign = ' ';
74 size--;
75 }
76 }
77 if (type & SPECIAL) {
78 if (base == 16)
79 size -= 2;
80 else if (base == 8)
81 size--;
82 }
83 i = 0;
Elyes HAOUASa4184142018-05-15 20:57:01 +020084 if (num == 0) {
85 tmp[i++] = '0';
86 } else {
87 while (num != 0) {
88 tmp[i++] = digits[num % base];
89 num /= base;
90 }
Ronald G. Minnich79e36d92013-01-30 14:29:34 -080091 }
Elyes HAOUASa4184142018-05-15 20:57:01 +020092 if (i > precision) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +000093 precision = i;
Elyes HAOUASa4184142018-05-15 20:57:01 +020094 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +000095 size -= precision;
Elyes HAOUASa4184142018-05-15 20:57:01 +020096 if (!(type&(ZEROPAD+LEFT))) {
Elyes HAOUAS2e0f3d72018-05-03 09:36:28 +020097 while (size-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +010098 call_tx(' '), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +020099 }
100 if (sign) {
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100101 call_tx(sign), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +0200102 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000103 if (type & SPECIAL) {
Elyes HAOUAS20767ba2018-04-26 10:00:35 +0200104 if (base == 8)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100105 call_tx('0'), count++;
Elyes HAOUAS20767ba2018-04-26 10:00:35 +0200106 else if (base == 16) {
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100107 call_tx('0'), count++;
Jacob Garber5b994812019-07-10 11:44:45 -0600108 if (type & LARGE)
109 call_tx('X'), count++;
110 else
111 call_tx('x'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000112 }
113 }
Elyes HAOUASa4184142018-05-15 20:57:01 +0200114 if (!(type & LEFT)) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000115 while (size-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100116 call_tx(c), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +0200117 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000118 while (i < precision--)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100119 call_tx('0'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000120 while (i-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100121 call_tx(tmp[i]), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000122 while (size-- > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100123 call_tx(' '), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000124 return count;
125}
126
127
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +0200128int vtxprintf(void (*tx_byte)(unsigned char byte, void *data),
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100129 const char *fmt, va_list args, void *data)
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000130{
131 int len;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000132 unsigned long long num;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000133 int i, base;
134 const char *s;
135
136 int flags; /* flags to number() */
137
138 int field_width; /* width of output field */
139 int precision; /* min. # of digits for integers; max
140 number of chars for from string */
Jacob Garberc764fb22019-07-03 12:38:46 -0600141 int qualifier; /* 'h', 'H', 'l', 'L', 'z', or 'j' for integer fields */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000142
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000143 int count;
144
Elyes HAOUASa4184142018-05-15 20:57:01 +0200145 for (count = 0; *fmt ; ++fmt) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000146 if (*fmt != '%') {
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100147 call_tx(*fmt), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000148 continue;
149 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000150
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000151 /* process flags */
152 flags = 0;
Patrick Georgic5fc7db2012-03-07 15:55:47 +0100153repeat:
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000154 ++fmt; /* this also skips first '%' */
155 switch (*fmt) {
156 case '-': flags |= LEFT; goto repeat;
157 case '+': flags |= PLUS; goto repeat;
158 case ' ': flags |= SPACE; goto repeat;
159 case '#': flags |= SPECIAL; goto repeat;
160 case '0': flags |= ZEROPAD; goto repeat;
161 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000162
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000163 /* get field width */
164 field_width = -1;
Julius Werner66c77c22019-05-13 17:45:27 -0700165 if (isdigit(*fmt)) {
166 field_width = 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 field_width = va_arg(args, int);
171 if (field_width < 0) {
172 field_width = -field_width;
173 flags |= LEFT;
174 }
175 }
176
177 /* get the precision */
178 precision = -1;
179 if (*fmt == '.') {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000180 ++fmt;
Julius Werner66c77c22019-05-13 17:45:27 -0700181 if (isdigit(*fmt)) {
182 precision = skip_atoi((char **)&fmt);
Elyes HAOUASa4184142018-05-15 20:57:01 +0200183 } else if (*fmt == '*') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000184 ++fmt;
185 /* it's the next argument */
186 precision = va_arg(args, int);
187 }
Elyes HAOUASa4184142018-05-15 20:57:01 +0200188 if (precision < 0) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000189 precision = 0;
Elyes HAOUASa4184142018-05-15 20:57:01 +0200190 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000191 }
192
193 /* get the conversion qualifier */
194 qualifier = -1;
Jacob Garberc764fb22019-07-03 12:38:46 -0600195 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'z' || *fmt == 'j') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000196 qualifier = *fmt;
197 ++fmt;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000198 if (*fmt == 'l') {
199 qualifier = 'L';
200 ++fmt;
201 }
Patrick Georgid01ed752014-01-18 16:56:36 +0100202 if (*fmt == 'h') {
203 qualifier = 'H';
204 ++fmt;
205 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000206 }
207
208 /* default base */
209 base = 10;
210
211 switch (*fmt) {
212 case 'c':
213 if (!(flags & LEFT))
214 while (--field_width > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100215 call_tx(' '), count++;
216 call_tx((unsigned char) va_arg(args, int)), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000217 while (--field_width > 0)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100218 call_tx(' '), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000219 continue;
220
221 case 's':
222 s = va_arg(args, char *);
223 if (!s)
224 s = "<NULL>";
225
Martin Rothad6c8852016-11-18 11:35:01 -0700226 len = strnlen(s, (size_t)precision);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000227
Elyes HAOUASa4184142018-05-15 20:57:01 +0200228 if (!(flags & LEFT)) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000229 while (len < field_width--)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100230 call_tx(' '), count++;
Elyes HAOUASa4184142018-05-15 20:57:01 +0200231 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000232 for (i = 0; i < len; ++i)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100233 call_tx(*s++), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000234 while (len < field_width--)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100235 call_tx(' '), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000236 continue;
237
238 case 'p':
239 if (field_width == -1) {
240 field_width = 2*sizeof(void *);
241 flags |= ZEROPAD;
242 }
243 count += number(tx_byte,
244 (unsigned long) va_arg(args, void *), 16,
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +0200245 field_width, precision, flags, data);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000246 continue;
247
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000248 case 'n':
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000249 if (qualifier == 'L') {
250 long long *ip = va_arg(args, long long *);
251 *ip = count;
252 } else if (qualifier == 'l') {
Elyes HAOUASa4184142018-05-15 20:57:01 +0200253 long *ip = va_arg(args, long *);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000254 *ip = count;
255 } else {
Elyes HAOUASa4184142018-05-15 20:57:01 +0200256 int *ip = va_arg(args, int *);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000257 *ip = count;
258 }
259 continue;
260
261 case '%':
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100262 call_tx('%'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000263 continue;
264
265 /* integer number formats - set up the flags and "break" */
266 case 'o':
267 base = 8;
268 break;
269
270 case 'X':
271 flags |= LARGE;
Jacob Garber4c33a3a2019-07-12 10:34:06 -0600272 /* fall through */
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000273 case 'x':
274 base = 16;
275 break;
276
277 case 'd':
278 case 'i':
279 flags |= SIGN;
280 case 'u':
281 break;
282
283 default:
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100284 call_tx('%'), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000285 if (*fmt)
Vladimir Serbinenkof421b332013-11-26 21:43:05 +0100286 call_tx(*fmt), count++;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000287 else
288 --fmt;
289 continue;
290 }
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000291 if (qualifier == 'L') {
292 num = va_arg(args, unsigned long long);
293 } else if (qualifier == 'l') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000294 num = va_arg(args, unsigned long);
Stefan Reinauerbfff6de2012-05-15 13:28:07 -0700295 } else if (qualifier == 'z') {
296 num = va_arg(args, size_t);
Jacob Garberc764fb22019-07-03 12:38:46 -0600297 } else if (qualifier == 'j') {
298 num = va_arg(args, uintmax_t);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000299 } else if (qualifier == 'h') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000300 num = (unsigned short) va_arg(args, int);
301 if (flags & SIGN)
302 num = (short) num;
Patrick Georgid01ed752014-01-18 16:56:36 +0100303 } else if (qualifier == 'H') {
304 num = (unsigned char) va_arg(args, int);
305 if (flags & SIGN)
306 num = (signed char) num;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000307 } else if (flags & SIGN) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000308 num = va_arg(args, int);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000309 } else {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000310 num = va_arg(args, unsigned int);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000311 }
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +0200312 count += number(tx_byte, num, base, field_width, precision, flags, data);
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000313 }
314 return count;
315}