blob: d7627632d0ae1fa0a3582f2543633c4da7fec86b [file] [log] [blame]
wdenk153d5112002-08-30 11:07:04 +00001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
Simon Glass3cce8a52011-10-21 18:51:33 +000010 *
11 * from hush: simple_itoa() was lifted from boa-0.93.15
wdenk153d5112002-08-30 11:07:04 +000012 */
13
14#include <stdarg.h>
15#include <linux/types.h>
16#include <linux/string.h>
17#include <linux/ctype.h>
Heiko Schochera7fd0d92011-05-02 21:33:49 +000018#include <errno.h>
wdenk153d5112002-08-30 11:07:04 +000019
20#include <common.h>
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000021#if !defined(CONFIG_PANIC_HANG)
wdenk153d5112002-08-30 11:07:04 +000022#include <command.h>
wdenk153d5112002-08-30 11:07:04 +000023#endif
24
Dirk Behme47910502009-07-22 17:51:56 +020025#include <div64.h>
Dirk Behme47910502009-07-22 17:51:56 +020026#define noinline __attribute__((noinline))
27
Sonny Rao046a37b2011-11-02 09:52:08 +000028/* some reluctance to put this into a new limits.h, so it is here */
29#define INT_MAX ((int)(~0U>>1))
30
Mike Frysinger6c6166f2009-02-16 23:21:36 -050031const char hex_asc[] = "0123456789abcdef";
32#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
33#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
34
35static inline char *pack_hex_byte(char *buf, u8 byte)
36{
37 *buf++ = hex_asc_hi(byte);
38 *buf++ = hex_asc_lo(byte);
39 return buf;
40}
41
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000042unsigned long simple_strtoul(const char *cp, char **endp,
43 unsigned int base)
wdenk153d5112002-08-30 11:07:04 +000044{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000045 unsigned long result = 0;
46 unsigned long value;
wdenk153d5112002-08-30 11:07:04 +000047
48 if (*cp == '0') {
49 cp++;
50 if ((*cp == 'x') && isxdigit(cp[1])) {
51 base = 16;
52 cp++;
53 }
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000054
55 if (!base)
wdenk153d5112002-08-30 11:07:04 +000056 base = 8;
wdenk153d5112002-08-30 11:07:04 +000057 }
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000058
59 if (!base)
wdenk153d5112002-08-30 11:07:04 +000060 base = 10;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000061
wdenk153d5112002-08-30 11:07:04 +000062 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
63 ? toupper(*cp) : *cp)-'A'+10) < base) {
64 result = result*base + value;
65 cp++;
66 }
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000067
wdenk153d5112002-08-30 11:07:04 +000068 if (endp)
69 *endp = (char *)cp;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000070
wdenk153d5112002-08-30 11:07:04 +000071 return result;
72}
73
Heiko Schochera7fd0d92011-05-02 21:33:49 +000074int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
75{
76 char *tail;
77 unsigned long val;
78 size_t len;
79
80 *res = 0;
81 len = strlen(cp);
82 if (len == 0)
83 return -EINVAL;
84
85 val = simple_strtoul(cp, &tail, base);
86 if (tail == cp)
87 return -EINVAL;
88
89 if ((*tail == '\0') ||
90 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
91 *res = val;
92 return 0;
93 }
94
95 return -EINVAL;
96}
97
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000098long simple_strtol(const char *cp, char **endp, unsigned int base)
wdenk153d5112002-08-30 11:07:04 +000099{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000100 if (*cp == '-')
101 return -simple_strtoul(cp + 1, endp, base);
102
103 return simple_strtoul(cp, endp, base);
wdenk153d5112002-08-30 11:07:04 +0000104}
105
Kyungmin Park7e6ee7a2008-11-19 16:32:36 +0100106int ustrtoul(const char *cp, char **endp, unsigned int base)
107{
108 unsigned long result = simple_strtoul(cp, endp, base);
109 switch (**endp) {
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000110 case 'G':
Kyungmin Park7e6ee7a2008-11-19 16:32:36 +0100111 result *= 1024;
112 /* fall through */
113 case 'M':
114 result *= 1024;
115 /* fall through */
116 case 'K':
117 case 'k':
118 result *= 1024;
119 if ((*endp)[1] == 'i') {
120 if ((*endp)[2] == 'B')
121 (*endp) += 3;
122 else
123 (*endp) += 2;
124 }
125 }
126 return result;
127}
128
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000129unsigned long long simple_strtoull(const char *cp, char **endp,
130 unsigned int base)
wdenkc40b2952004-03-13 23:29:43 +0000131{
132 unsigned long long result = 0, value;
133
134 if (*cp == '0') {
135 cp++;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000136 if ((*cp == 'x') && isxdigit(cp[1])) {
wdenkc40b2952004-03-13 23:29:43 +0000137 base = 16;
138 cp++;
139 }
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000140
141 if (!base)
wdenkc40b2952004-03-13 23:29:43 +0000142 base = 8;
wdenkc40b2952004-03-13 23:29:43 +0000143 }
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000144
145 if (!base)
wdenkc40b2952004-03-13 23:29:43 +0000146 base = 10;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000147
148 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0'
149 : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {
wdenkc40b2952004-03-13 23:29:43 +0000150 result = result * base + value;
151 cp++;
152 }
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000153
wdenkc40b2952004-03-13 23:29:43 +0000154 if (endp)
155 *endp = (char *) cp;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000156
wdenkc40b2952004-03-13 23:29:43 +0000157 return result;
158}
wdenkc40b2952004-03-13 23:29:43 +0000159
wdenk153d5112002-08-30 11:07:04 +0000160/* we use this so that we can do without the ctype library */
161#define is_digit(c) ((c) >= '0' && (c) <= '9')
162
163static int skip_atoi(const char **s)
164{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000165 int i = 0;
wdenk153d5112002-08-30 11:07:04 +0000166
167 while (is_digit(**s))
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000168 i = i * 10 + *((*s)++) - '0';
169
wdenk153d5112002-08-30 11:07:04 +0000170 return i;
171}
172
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500173/* Decimal conversion is by far the most typical, and is used
174 * for /proc and /sys data. This directly impacts e.g. top performance
175 * with many processes running. We optimize it for speed
176 * using code from
177 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
178 * (with permission from the author, Douglas W. Jones). */
179
180/* Formats correctly any integer in [0,99999].
181 * Outputs from one to five digits depending on input.
182 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000183static char *put_dec_trunc(char *buf, unsigned q)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500184{
185 unsigned d3, d2, d1, d0;
186 d1 = (q>>4) & 0xf;
187 d2 = (q>>8) & 0xf;
188 d3 = (q>>12);
189
190 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
191 q = (d0 * 0xcd) >> 11;
192 d0 = d0 - 10*q;
193 *buf++ = d0 + '0'; /* least significant digit */
194 d1 = q + 9*d3 + 5*d2 + d1;
195 if (d1 != 0) {
196 q = (d1 * 0xcd) >> 11;
197 d1 = d1 - 10*q;
198 *buf++ = d1 + '0'; /* next digit */
199
200 d2 = q + 2*d2;
201 if ((d2 != 0) || (d3 != 0)) {
202 q = (d2 * 0xd) >> 7;
203 d2 = d2 - 10*q;
204 *buf++ = d2 + '0'; /* next digit */
205
206 d3 = q + 4*d3;
207 if (d3 != 0) {
208 q = (d3 * 0xcd) >> 11;
209 d3 = d3 - 10*q;
210 *buf++ = d3 + '0'; /* next digit */
211 if (q != 0)
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000212 *buf++ = q + '0'; /* most sign. digit */
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500213 }
214 }
215 }
216 return buf;
217}
218/* Same with if's removed. Always emits five digits */
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000219static char *put_dec_full(char *buf, unsigned q)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500220{
221 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
222 /* but anyway, gcc produces better code with full-sized ints */
223 unsigned d3, d2, d1, d0;
224 d1 = (q>>4) & 0xf;
225 d2 = (q>>8) & 0xf;
226 d3 = (q>>12);
227
Wolfgang Denkc0a14ae2009-04-05 00:27:57 +0200228 /*
229 * Possible ways to approx. divide by 10
230 * gcc -O2 replaces multiply with shifts and adds
231 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
232 * (x * 0x67) >> 10: 1100111
233 * (x * 0x34) >> 9: 110100 - same
234 * (x * 0x1a) >> 8: 11010 - same
235 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
236 */
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500237
238 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
239 q = (d0 * 0xcd) >> 11;
240 d0 = d0 - 10*q;
241 *buf++ = d0 + '0';
242 d1 = q + 9*d3 + 5*d2 + d1;
243 q = (d1 * 0xcd) >> 11;
244 d1 = d1 - 10*q;
245 *buf++ = d1 + '0';
246
247 d2 = q + 2*d2;
248 q = (d2 * 0xd) >> 7;
249 d2 = d2 - 10*q;
250 *buf++ = d2 + '0';
251
252 d3 = q + 4*d3;
253 q = (d3 * 0xcd) >> 11; /* - shorter code */
254 /* q = (d3 * 0x67) >> 10; - would also work */
255 d3 = d3 - 10*q;
256 *buf++ = d3 + '0';
257 *buf++ = q + '0';
258 return buf;
259}
260/* No inlining helps gcc to use registers better */
Daniel Schwierzeck7b64f662012-09-16 06:55:04 +0000261static noinline char *put_dec(char *buf, u64 num)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500262{
263 while (1) {
264 unsigned rem;
265 if (num < 100000)
266 return put_dec_trunc(buf, num);
267 rem = do_div(num, 100000);
268 buf = put_dec_full(buf, rem);
269 }
270}
271
wdenk153d5112002-08-30 11:07:04 +0000272#define ZEROPAD 1 /* pad with zero */
273#define SIGN 2 /* unsigned/signed long */
274#define PLUS 4 /* show plus */
275#define SPACE 8 /* space if plus */
276#define LEFT 16 /* left justified */
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500277#define SMALL 32 /* Must be 32 == 0x20 */
278#define SPECIAL 64 /* 0x */
wdenk153d5112002-08-30 11:07:04 +0000279
Sonny Rao046a37b2011-11-02 09:52:08 +0000280#ifdef CONFIG_SYS_VSNPRINTF
281/*
282 * Macro to add a new character to our output string, but only if it will
283 * fit. The macro moves to the next character position in the output string.
284 */
285#define ADDCH(str, ch) do { \
286 if ((str) < end) \
287 *(str) = (ch); \
288 ++str; \
289 } while (0)
290#else
291#define ADDCH(str, ch) (*(str)++ = (ch))
292#endif
293
Daniel Schwierzeck7b64f662012-09-16 06:55:04 +0000294static char *number(char *buf, char *end, u64 num,
Sonny Rao046a37b2011-11-02 09:52:08 +0000295 int base, int size, int precision, int type)
wdenk153d5112002-08-30 11:07:04 +0000296{
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500297 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000298 static const char digits[16] = "0123456789ABCDEF";
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500299
300 char tmp[66];
301 char sign;
302 char locase;
303 int need_pfx = ((type & SPECIAL) && base != 10);
wdenk153d5112002-08-30 11:07:04 +0000304 int i;
305
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500306 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
307 * produces same digits or (maybe lowercased) letters */
308 locase = (type & SMALL);
wdenk153d5112002-08-30 11:07:04 +0000309 if (type & LEFT)
310 type &= ~ZEROPAD;
wdenk153d5112002-08-30 11:07:04 +0000311 sign = 0;
312 if (type & SIGN) {
Daniel Schwierzeck7b64f662012-09-16 06:55:04 +0000313 if ((s64) num < 0) {
wdenk153d5112002-08-30 11:07:04 +0000314 sign = '-';
Daniel Schwierzeck7b64f662012-09-16 06:55:04 +0000315 num = -(s64) num;
wdenk153d5112002-08-30 11:07:04 +0000316 size--;
317 } else if (type & PLUS) {
318 sign = '+';
319 size--;
320 } else if (type & SPACE) {
321 sign = ' ';
322 size--;
323 }
324 }
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500325 if (need_pfx) {
326 size--;
wdenk153d5112002-08-30 11:07:04 +0000327 if (base == 16)
wdenk153d5112002-08-30 11:07:04 +0000328 size--;
329 }
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500330
331 /* generate full string in tmp[], in reverse order */
wdenk153d5112002-08-30 11:07:04 +0000332 i = 0;
333 if (num == 0)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500334 tmp[i++] = '0';
335 /* Generic code, for any base:
336 else do {
337 tmp[i++] = (digits[do_div(num,base)] | locase);
338 } while (num != 0);
339 */
340 else if (base != 10) { /* 8 or 16 */
341 int mask = base - 1;
342 int shift = 3;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000343
344 if (base == 16)
345 shift = 4;
346
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500347 do {
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000348 tmp[i++] = (digits[((unsigned char)num) & mask]
349 | locase);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500350 num >>= shift;
351 } while (num);
352 } else { /* base 10 */
353 i = put_dec(tmp, num) - tmp;
354 }
355
356 /* printing 100 using %2d gives "100", not "00" */
wdenk153d5112002-08-30 11:07:04 +0000357 if (i > precision)
358 precision = i;
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500359 /* leading space padding */
wdenk153d5112002-08-30 11:07:04 +0000360 size -= precision;
Sonny Rao046a37b2011-11-02 09:52:08 +0000361 if (!(type & (ZEROPAD + LEFT))) {
362 while (--size >= 0)
363 ADDCH(buf, ' ');
364 }
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500365 /* sign */
wdenk153d5112002-08-30 11:07:04 +0000366 if (sign)
Sonny Rao046a37b2011-11-02 09:52:08 +0000367 ADDCH(buf, sign);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500368 /* "0x" / "0" prefix */
369 if (need_pfx) {
Sonny Rao046a37b2011-11-02 09:52:08 +0000370 ADDCH(buf, '0');
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500371 if (base == 16)
Sonny Rao046a37b2011-11-02 09:52:08 +0000372 ADDCH(buf, 'X' | locase);
wdenk153d5112002-08-30 11:07:04 +0000373 }
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500374 /* zero or space padding */
375 if (!(type & LEFT)) {
376 char c = (type & ZEROPAD) ? '0' : ' ';
Sonny Rao046a37b2011-11-02 09:52:08 +0000377
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500378 while (--size >= 0)
Sonny Rao046a37b2011-11-02 09:52:08 +0000379 ADDCH(buf, c);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500380 }
381 /* hmm even more zero padding? */
382 while (i <= --precision)
Sonny Rao046a37b2011-11-02 09:52:08 +0000383 ADDCH(buf, '0');
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500384 /* actual digits of result */
385 while (--i >= 0)
Sonny Rao046a37b2011-11-02 09:52:08 +0000386 ADDCH(buf, tmp[i]);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500387 /* trailing space padding */
388 while (--size >= 0)
Sonny Rao046a37b2011-11-02 09:52:08 +0000389 ADDCH(buf, ' ');
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500390 return buf;
wdenk153d5112002-08-30 11:07:04 +0000391}
392
Sonny Rao046a37b2011-11-02 09:52:08 +0000393static char *string(char *buf, char *end, char *s, int field_width,
394 int precision, int flags)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500395{
396 int len, i;
wdenk153d5112002-08-30 11:07:04 +0000397
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500398 if (s == 0)
399 s = "<NULL>";
400
401 len = strnlen(s, precision);
402
403 if (!(flags & LEFT))
404 while (len < field_width--)
Sonny Rao046a37b2011-11-02 09:52:08 +0000405 ADDCH(buf, ' ');
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500406 for (i = 0; i < len; ++i)
Sonny Rao046a37b2011-11-02 09:52:08 +0000407 ADDCH(buf, *s++);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500408 while (len < field_width--)
Sonny Rao046a37b2011-11-02 09:52:08 +0000409 ADDCH(buf, ' ');
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500410 return buf;
411}
412
413#ifdef CONFIG_CMD_NET
Sonny Rao046a37b2011-11-02 09:52:08 +0000414static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500415 int precision, int flags)
416{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000417 /* (6 * 2 hex digits), 5 colons and trailing zero */
418 char mac_addr[6 * 3];
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500419 char *p = mac_addr;
420 int i;
421
422 for (i = 0; i < 6; i++) {
423 p = pack_hex_byte(p, addr[i]);
424 if (!(flags & SPECIAL) && i != 5)
425 *p++ = ':';
426 }
427 *p = '\0';
428
Sonny Rao046a37b2011-11-02 09:52:08 +0000429 return string(buf, end, mac_addr, field_width, precision,
430 flags & ~SPECIAL);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500431}
432
Sonny Rao046a37b2011-11-02 09:52:08 +0000433static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500434 int precision, int flags)
435{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000436 /* (8 * 4 hex digits), 7 colons and trailing zero */
437 char ip6_addr[8 * 5];
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500438 char *p = ip6_addr;
439 int i;
440
441 for (i = 0; i < 8; i++) {
442 p = pack_hex_byte(p, addr[2 * i]);
443 p = pack_hex_byte(p, addr[2 * i + 1]);
444 if (!(flags & SPECIAL) && i != 7)
445 *p++ = ':';
446 }
447 *p = '\0';
448
Sonny Rao046a37b2011-11-02 09:52:08 +0000449 return string(buf, end, ip6_addr, field_width, precision,
450 flags & ~SPECIAL);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500451}
452
Sonny Rao046a37b2011-11-02 09:52:08 +0000453static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500454 int precision, int flags)
455{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000456 /* (4 * 3 decimal digits), 3 dots and trailing zero */
457 char ip4_addr[4 * 4];
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500458 char temp[3]; /* hold each IP quad in reverse order */
459 char *p = ip4_addr;
460 int i, digits;
461
462 for (i = 0; i < 4; i++) {
463 digits = put_dec_trunc(temp, addr[i]) - temp;
464 /* reverse the digits in the quad */
465 while (digits--)
466 *p++ = temp[digits];
467 if (i != 3)
468 *p++ = '.';
469 }
470 *p = '\0';
471
Sonny Rao046a37b2011-11-02 09:52:08 +0000472 return string(buf, end, ip4_addr, field_width, precision,
473 flags & ~SPECIAL);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500474}
475#endif
476
477/*
478 * Show a '%p' thing. A kernel extension is that the '%p' is followed
479 * by an extra set of alphanumeric characters that are extended format
480 * specifiers.
481 *
482 * Right now we handle:
483 *
484 * - 'M' For a 6-byte MAC address, it prints the address in the
485 * usual colon-separated hex notation
486 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
487 * decimal for v4 and colon separated network-order 16 bit hex for v6)
488 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
489 * currently the same
490 *
491 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
492 * function pointers are really function descriptors, which contain a
493 * pointer to the real address.
494 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000495static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
496 int field_width, int precision, int flags)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500497{
498 if (!ptr)
Sonny Rao046a37b2011-11-02 09:52:08 +0000499 return string(buf, end, "(null)", field_width, precision,
500 flags);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500501
502#ifdef CONFIG_CMD_NET
503 switch (*fmt) {
504 case 'm':
505 flags |= SPECIAL;
506 /* Fallthrough */
507 case 'M':
Sonny Rao046a37b2011-11-02 09:52:08 +0000508 return mac_address_string(buf, end, ptr, field_width,
509 precision, flags);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500510 case 'i':
511 flags |= SPECIAL;
512 /* Fallthrough */
513 case 'I':
514 if (fmt[1] == '6')
Sonny Rao046a37b2011-11-02 09:52:08 +0000515 return ip6_addr_string(buf, end, ptr, field_width,
516 precision, flags);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500517 if (fmt[1] == '4')
Sonny Rao046a37b2011-11-02 09:52:08 +0000518 return ip4_addr_string(buf, end, ptr, field_width,
519 precision, flags);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500520 flags &= ~SPECIAL;
521 break;
522 }
523#endif
524 flags |= SMALL;
525 if (field_width == -1) {
526 field_width = 2*sizeof(void *);
527 flags |= ZEROPAD;
528 }
Sonny Rao046a37b2011-11-02 09:52:08 +0000529 return number(buf, end, (unsigned long)ptr, 16, field_width,
530 precision, flags);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500531}
532
Sonny Rao046a37b2011-11-02 09:52:08 +0000533static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
534 va_list args)
wdenk153d5112002-08-30 11:07:04 +0000535{
Daniel Schwierzeck7b64f662012-09-16 06:55:04 +0000536 u64 num;
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500537 int base;
538 char *str;
wdenk153d5112002-08-30 11:07:04 +0000539
540 int flags; /* flags to number() */
541
542 int field_width; /* width of output field */
543 int precision; /* min. # of digits for integers; max
544 number of chars for from string */
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500545 int qualifier; /* 'h', 'l', or 'L' for integer fields */
546 /* 'z' support added 23/7/1999 S.H. */
547 /* 'z' changed to 'Z' --davidm 1/25/99 */
548 /* 't' added for ptrdiff_t */
Sonny Rao046a37b2011-11-02 09:52:08 +0000549 char *end = buf + size;
wdenk153d5112002-08-30 11:07:04 +0000550
Sonny Rao046a37b2011-11-02 09:52:08 +0000551#ifdef CONFIG_SYS_VSNPRINTF
552 /* Make sure end is always >= buf - do we want this in U-Boot? */
553 if (end < buf) {
554 end = ((void *)-1);
555 size = end - buf;
556 }
557#endif
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500558 str = buf;
559
560 for (; *fmt ; ++fmt) {
wdenk153d5112002-08-30 11:07:04 +0000561 if (*fmt != '%') {
Sonny Rao046a37b2011-11-02 09:52:08 +0000562 ADDCH(str, *fmt);
wdenk153d5112002-08-30 11:07:04 +0000563 continue;
564 }
565
566 /* process flags */
567 flags = 0;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000568repeat:
wdenk153d5112002-08-30 11:07:04 +0000569 ++fmt; /* this also skips first '%' */
570 switch (*fmt) {
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000571 case '-':
572 flags |= LEFT;
573 goto repeat;
574 case '+':
575 flags |= PLUS;
576 goto repeat;
577 case ' ':
578 flags |= SPACE;
579 goto repeat;
580 case '#':
581 flags |= SPECIAL;
582 goto repeat;
583 case '0':
584 flags |= ZEROPAD;
585 goto repeat;
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500586 }
wdenk153d5112002-08-30 11:07:04 +0000587
588 /* get field width */
589 field_width = -1;
590 if (is_digit(*fmt))
591 field_width = skip_atoi(&fmt);
592 else if (*fmt == '*') {
593 ++fmt;
594 /* it's the next argument */
595 field_width = va_arg(args, int);
596 if (field_width < 0) {
597 field_width = -field_width;
598 flags |= LEFT;
599 }
600 }
601
602 /* get the precision */
603 precision = -1;
604 if (*fmt == '.') {
605 ++fmt;
606 if (is_digit(*fmt))
607 precision = skip_atoi(&fmt);
608 else if (*fmt == '*') {
609 ++fmt;
610 /* it's the next argument */
611 precision = va_arg(args, int);
612 }
613 if (precision < 0)
614 precision = 0;
615 }
616
617 /* get the conversion qualifier */
618 qualifier = -1;
Jean-Christophe PLAGNIOL-VILLARDf354b732008-07-14 14:11:45 +0200619 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500620 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
wdenk153d5112002-08-30 11:07:04 +0000621 qualifier = *fmt;
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500622 ++fmt;
623 if (qualifier == 'l' && *fmt == 'l') {
624 qualifier = 'L';
James Yangbf052932008-01-10 16:02:07 -0600625 ++fmt;
626 }
wdenk153d5112002-08-30 11:07:04 +0000627 }
628
629 /* default base */
630 base = 10;
631
632 switch (*fmt) {
633 case 'c':
Sonny Rao046a37b2011-11-02 09:52:08 +0000634 if (!(flags & LEFT)) {
wdenk153d5112002-08-30 11:07:04 +0000635 while (--field_width > 0)
Sonny Rao046a37b2011-11-02 09:52:08 +0000636 ADDCH(str, ' ');
637 }
638 ADDCH(str, (unsigned char) va_arg(args, int));
wdenk153d5112002-08-30 11:07:04 +0000639 while (--field_width > 0)
Sonny Rao046a37b2011-11-02 09:52:08 +0000640 ADDCH(str, ' ');
wdenk153d5112002-08-30 11:07:04 +0000641 continue;
642
643 case 's':
Sonny Rao046a37b2011-11-02 09:52:08 +0000644 str = string(str, end, va_arg(args, char *),
645 field_width, precision, flags);
wdenk153d5112002-08-30 11:07:04 +0000646 continue;
647
648 case 'p':
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000649 str = pointer(fmt + 1, str, end,
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500650 va_arg(args, void *),
651 field_width, precision, flags);
652 /* Skip all alphanumeric pointer suffixes */
653 while (isalnum(fmt[1]))
654 fmt++;
wdenk153d5112002-08-30 11:07:04 +0000655 continue;
656
wdenk153d5112002-08-30 11:07:04 +0000657 case 'n':
658 if (qualifier == 'l') {
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000659 long *ip = va_arg(args, long *);
wdenk153d5112002-08-30 11:07:04 +0000660 *ip = (str - buf);
661 } else {
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000662 int *ip = va_arg(args, int *);
wdenk153d5112002-08-30 11:07:04 +0000663 *ip = (str - buf);
664 }
665 continue;
666
667 case '%':
Sonny Rao046a37b2011-11-02 09:52:08 +0000668 ADDCH(str, '%');
wdenk153d5112002-08-30 11:07:04 +0000669 continue;
670
671 /* integer number formats - set up the flags and "break" */
672 case 'o':
673 base = 8;
674 break;
675
wdenk153d5112002-08-30 11:07:04 +0000676 case 'x':
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500677 flags |= SMALL;
678 case 'X':
wdenk153d5112002-08-30 11:07:04 +0000679 base = 16;
680 break;
681
682 case 'd':
683 case 'i':
684 flags |= SIGN;
685 case 'u':
686 break;
687
688 default:
Sonny Rao046a37b2011-11-02 09:52:08 +0000689 ADDCH(str, '%');
wdenk153d5112002-08-30 11:07:04 +0000690 if (*fmt)
Sonny Rao046a37b2011-11-02 09:52:08 +0000691 ADDCH(str, *fmt);
wdenk153d5112002-08-30 11:07:04 +0000692 else
693 --fmt;
694 continue;
695 }
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500696 if (qualifier == 'L') /* "quad" for 64 bit variables */
wdenkc40b2952004-03-13 23:29:43 +0000697 num = va_arg(args, unsigned long long);
Heiko Schocher4b142fe2009-12-03 11:21:21 +0100698 else if (qualifier == 'l') {
wdenk153d5112002-08-30 11:07:04 +0000699 num = va_arg(args, unsigned long);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500700 if (flags & SIGN)
701 num = (signed long) num;
Jean-Christophe PLAGNIOL-VILLARDf354b732008-07-14 14:11:45 +0200702 } else if (qualifier == 'Z' || qualifier == 'z') {
703 num = va_arg(args, size_t);
704 } else if (qualifier == 't') {
705 num = va_arg(args, ptrdiff_t);
706 } else if (qualifier == 'h') {
wdenk153d5112002-08-30 11:07:04 +0000707 num = (unsigned short) va_arg(args, int);
708 if (flags & SIGN)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500709 num = (signed short) num;
710 } else {
wdenk153d5112002-08-30 11:07:04 +0000711 num = va_arg(args, unsigned int);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500712 if (flags & SIGN)
713 num = (signed int) num;
714 }
Sonny Rao046a37b2011-11-02 09:52:08 +0000715 str = number(str, end, num, base, field_width, precision,
716 flags);
wdenk153d5112002-08-30 11:07:04 +0000717 }
Sonny Rao046a37b2011-11-02 09:52:08 +0000718
719#ifdef CONFIG_SYS_VSNPRINTF
720 if (size > 0) {
721 ADDCH(str, '\0');
722 if (str > end)
723 end[-1] = '\0';
724 }
725#else
wdenk153d5112002-08-30 11:07:04 +0000726 *str = '\0';
Sonny Rao046a37b2011-11-02 09:52:08 +0000727#endif
728 /* the trailing null byte doesn't count towards the total */
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000729 return str - buf;
wdenk153d5112002-08-30 11:07:04 +0000730}
731
Sonny Rao046a37b2011-11-02 09:52:08 +0000732#ifdef CONFIG_SYS_VSNPRINTF
733int vsnprintf(char *buf, size_t size, const char *fmt,
734 va_list args)
735{
736 return vsnprintf_internal(buf, size, fmt, args);
737}
738
Sonny Rao046a37b2011-11-02 09:52:08 +0000739int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
740{
741 int i;
742
743 i = vsnprintf(buf, size, fmt, args);
744
745 if (likely(i < size))
746 return i;
747 if (size != 0)
748 return size - 1;
749 return 0;
750}
751
Sonny Rao046a37b2011-11-02 09:52:08 +0000752int snprintf(char *buf, size_t size, const char *fmt, ...)
753{
754 va_list args;
755 int i;
756
757 va_start(args, fmt);
758 i = vsnprintf(buf, size, fmt, args);
759 va_end(args);
760
761 return i;
762}
763
Sonny Rao046a37b2011-11-02 09:52:08 +0000764int scnprintf(char *buf, size_t size, const char *fmt, ...)
765{
766 va_list args;
767 int i;
768
769 va_start(args, fmt);
770 i = vscnprintf(buf, size, fmt, args);
771 va_end(args);
772
773 return i;
774}
775#endif /* CONFIG_SYS_VSNPRINT */
776
777/**
778 * Format a string and place it in a buffer (va_list version)
779 *
780 * @param buf The buffer to place the result into
781 * @param fmt The format string to use
782 * @param args Arguments for the format string
783 *
784 * The function returns the number of characters written
785 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
786 * buffer overflows.
787 *
788 * If you're not already dealing with a va_list consider using sprintf().
789 */
790int vsprintf(char *buf, const char *fmt, va_list args)
791{
792 return vsnprintf_internal(buf, INT_MAX, fmt, args);
793}
794
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000795int sprintf(char *buf, const char *fmt, ...)
wdenk153d5112002-08-30 11:07:04 +0000796{
797 va_list args;
798 int i;
799
800 va_start(args, fmt);
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000801 i = vsprintf(buf, fmt, args);
wdenk153d5112002-08-30 11:07:04 +0000802 va_end(args);
803 return i;
804}
805
806void panic(const char *fmt, ...)
807{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000808 va_list args;
wdenk153d5112002-08-30 11:07:04 +0000809 va_start(args, fmt);
wdenk6dd652f2003-06-19 23:40:20 +0000810 vprintf(fmt, args);
wdenk153d5112002-08-30 11:07:04 +0000811 putc('\n');
812 va_end(args);
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000813#if defined(CONFIG_PANIC_HANG)
wdenk153d5112002-08-30 11:07:04 +0000814 hang();
815#else
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000816 udelay(100000); /* allow messages to go out */
817 do_reset(NULL, 0, 0, NULL);
wdenk153d5112002-08-30 11:07:04 +0000818#endif
Heiko Schocher40e01882011-07-26 20:55:57 +0000819 while (1)
820 ;
wdenk153d5112002-08-30 11:07:04 +0000821}
Simon Glass21726a72011-06-29 09:49:34 +0000822
823void __assert_fail(const char *assertion, const char *file, unsigned line,
824 const char *function)
825{
826 /* This will not return */
827 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
828 assertion);
829}
Simon Glass3cce8a52011-10-21 18:51:33 +0000830
831char *simple_itoa(ulong i)
832{
833 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
834 static char local[22];
835 char *p = &local[21];
836
837 *p-- = '\0';
838 do {
839 *p-- = '0' + i % 10;
840 i /= 10;
841 } while (i > 0);
842 return p + 1;
843}