blob: 9de25845bd61bc9bbcf2fb186c39a4c020480d06 [file] [log] [blame]
Stefan Reinauer34e3a142004-05-28 15:07:03 +00001/* vtxprintf.c, from
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
Stefan Reinauer34e3a142004-05-28 15:07:03 +00007#include <string.h>
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +00008#include <div64.h>
Stefan Reinauer3aa067f2012-04-02 13:24:04 -07009#include <console/console.h>
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000010#include <console/vtxprintf.h>
Stefan Reinauer34e3a142004-05-28 15:07:03 +000011
12/* haha, don't need ctype.c */
13#define isdigit(c) ((c) >= '0' && (c) <= '9')
14#define is_digit isdigit
15#define isxdigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
16
Stefan Reinauer34e3a142004-05-28 15:07:03 +000017static int skip_atoi(const char **s)
18{
19 int i=0;
20
21 while (is_digit(**s))
22 i = i*10 + *((*s)++) - '0';
23 return i;
24}
25
26#define ZEROPAD 1 /* pad with zero */
27#define SIGN 2 /* unsigned/signed long */
28#define PLUS 4 /* show plus */
29#define SPACE 8 /* space if plus */
30#define LEFT 16 /* left justified */
31#define SPECIAL 32 /* 0x */
32#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
33
Stefan Reinauer14e22772010-04-27 06:56:47 +000034static int number(void (*tx_byte)(unsigned char byte),
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +000035 unsigned long long num, int base, int size, int precision, int type)
Stefan Reinauer34e3a142004-05-28 15:07:03 +000036{
37 char c,sign,tmp[66];
38 const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
39 int i;
40 int count = 0;
41
42 if (type & LARGE)
43 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
44 if (type & LEFT)
45 type &= ~ZEROPAD;
46 if (base < 2 || base > 36)
47 return 0;
48 c = (type & ZEROPAD) ? '0' : ' ';
49 sign = 0;
50 if (type & SIGN) {
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +000051 if ((signed long long)num < 0) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +000052 sign = '-';
53 num = -num;
54 size--;
55 } else if (type & PLUS) {
56 sign = '+';
57 size--;
58 } else if (type & SPACE) {
59 sign = ' ';
60 size--;
61 }
62 }
63 if (type & SPECIAL) {
64 if (base == 16)
65 size -= 2;
66 else if (base == 8)
67 size--;
68 }
69 i = 0;
70 if (num == 0)
71 tmp[i++]='0';
Ronald G. Minnich79e36d92013-01-30 14:29:34 -080072 else while (num != 0){
73 /* there are some nice optimizations in the
74 * Macros-From-Hell that form the div64 code
75 * *IF* you call it with a constant.
76 * We're firmware, we only do bases
77 * 8, 10, and 16. Let's be smart.
78 * This greatly helps ARM, reduces the
79 * code footprint at compile time, and does not hurt x86.
80 */
81 if (base == 10)
82 tmp[i++] = digits[do_div(num,10)];
83 else if (base == 8)
84 tmp[i++] = digits[do_div(num,8)];
85 else /* sorry, you're out of choices */
86 tmp[i++] = digits[do_div(num,16)];
87 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +000088 if (i > precision)
89 precision = i;
90 size -= precision;
91 if (!(type&(ZEROPAD+LEFT)))
92 while(size-->0)
93 tx_byte(' '), count++;
94 if (sign)
95 tx_byte(sign), count++;
96 if (type & SPECIAL) {
97 if (base==8)
98 tx_byte('0'), count++;
99 else if (base==16) {
100 tx_byte('0'), count++;
101 tx_byte(digits[33]), count++;
102 }
103 }
104 if (!(type & LEFT))
105 while (size-- > 0)
106 tx_byte(c), count++;
107 while (i < precision--)
108 tx_byte('0'), count++;
109 while (i-- > 0)
110 tx_byte(tmp[i]), count++;
111 while (size-- > 0)
112 tx_byte(' '), count++;
113 return count;
114}
115
116
117int vtxprintf(void (*tx_byte)(unsigned char byte), const char *fmt, va_list args)
118{
119 int len;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000120 unsigned long long num;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000121 int i, base;
122 const char *s;
123
124 int flags; /* flags to number() */
125
126 int field_width; /* width of output field */
127 int precision; /* min. # of digits for integers; max
128 number of chars for from string */
129 int qualifier; /* 'h', 'l', or 'L' for integer fields */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000130
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000131 int count;
132
Stefan Reinauer3aa067f2012-04-02 13:24:04 -0700133#if defined(__SMM__) && CONFIG_SMM_TSEG
134 /* Fix pointer in TSEG */
135 tx_byte = console_tx_byte;
136#endif
137
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000138 for (count=0; *fmt ; ++fmt) {
139 if (*fmt != '%') {
140 tx_byte(*fmt), count++;
141 continue;
142 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000143
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000144 /* process flags */
145 flags = 0;
Patrick Georgic5fc7db2012-03-07 15:55:47 +0100146repeat:
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000147 ++fmt; /* this also skips first '%' */
148 switch (*fmt) {
149 case '-': flags |= LEFT; goto repeat;
150 case '+': flags |= PLUS; goto repeat;
151 case ' ': flags |= SPACE; goto repeat;
152 case '#': flags |= SPECIAL; goto repeat;
153 case '0': flags |= ZEROPAD; goto repeat;
154 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000155
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000156 /* get field width */
157 field_width = -1;
158 if (is_digit(*fmt))
159 field_width = skip_atoi(&fmt);
160 else if (*fmt == '*') {
161 ++fmt;
162 /* it's the next argument */
163 field_width = va_arg(args, int);
164 if (field_width < 0) {
165 field_width = -field_width;
166 flags |= LEFT;
167 }
168 }
169
170 /* get the precision */
171 precision = -1;
172 if (*fmt == '.') {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000173 ++fmt;
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000174 if (is_digit(*fmt))
175 precision = skip_atoi(&fmt);
176 else if (*fmt == '*') {
177 ++fmt;
178 /* it's the next argument */
179 precision = va_arg(args, int);
180 }
181 if (precision < 0)
182 precision = 0;
183 }
184
185 /* get the conversion qualifier */
186 qualifier = -1;
Stefan Reinauerbfff6de2012-05-15 13:28:07 -0700187 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'z') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000188 qualifier = *fmt;
189 ++fmt;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000190 if (*fmt == 'l') {
191 qualifier = 'L';
192 ++fmt;
193 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000194 }
195
196 /* default base */
197 base = 10;
198
199 switch (*fmt) {
200 case 'c':
201 if (!(flags & LEFT))
202 while (--field_width > 0)
203 tx_byte(' '), count++;
204 tx_byte((unsigned char) va_arg(args, int)), count++;
205 while (--field_width > 0)
206 tx_byte(' '), count++;
207 continue;
208
209 case 's':
210 s = va_arg(args, char *);
211 if (!s)
212 s = "<NULL>";
213
214 len = strnlen(s, precision);
215
216 if (!(flags & LEFT))
217 while (len < field_width--)
218 tx_byte(' '), count++;
219 for (i = 0; i < len; ++i)
220 tx_byte(*s++), count++;
221 while (len < field_width--)
222 tx_byte(' '), count++;
223 continue;
224
225 case 'p':
226 if (field_width == -1) {
227 field_width = 2*sizeof(void *);
228 flags |= ZEROPAD;
229 }
230 count += number(tx_byte,
231 (unsigned long) va_arg(args, void *), 16,
232 field_width, precision, flags);
233 continue;
234
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000235 case 'n':
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000236 if (qualifier == 'L') {
237 long long *ip = va_arg(args, long long *);
238 *ip = count;
239 } else if (qualifier == 'l') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000240 long * ip = va_arg(args, long *);
241 *ip = count;
242 } else {
243 int * ip = va_arg(args, int *);
244 *ip = count;
245 }
246 continue;
247
248 case '%':
249 tx_byte('%'), count++;
250 continue;
251
252 /* integer number formats - set up the flags and "break" */
253 case 'o':
254 base = 8;
255 break;
256
257 case 'X':
258 flags |= LARGE;
259 case 'x':
260 base = 16;
261 break;
262
263 case 'd':
264 case 'i':
265 flags |= SIGN;
266 case 'u':
267 break;
268
269 default:
270 tx_byte('%'), count++;
271 if (*fmt)
272 tx_byte(*fmt), count++;
273 else
274 --fmt;
275 continue;
276 }
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000277 if (qualifier == 'L') {
278 num = va_arg(args, unsigned long long);
279 } else if (qualifier == 'l') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000280 num = va_arg(args, unsigned long);
Stefan Reinauerbfff6de2012-05-15 13:28:07 -0700281 } else if (qualifier == 'z') {
282 num = va_arg(args, size_t);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000283 } else if (qualifier == 'h') {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000284 num = (unsigned short) va_arg(args, int);
285 if (flags & SIGN)
286 num = (short) num;
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000287 } else if (flags & SIGN) {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000288 num = va_arg(args, int);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000289 } else {
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000290 num = va_arg(args, unsigned int);
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000291 }
Stefan Reinauer34e3a142004-05-28 15:07:03 +0000292 count += number(tx_byte, num, base, field_width, precision, flags);
293 }
294 return count;
295}
296