blob: 92a9232c2a1421fd3033f9248d23749cce9d07b0 [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>
Mike Frysinger6c6166f2009-02-16 23:21:36 -050026# define NUM_TYPE long long
Dirk Behme47910502009-07-22 17:51:56 +020027#define noinline __attribute__((noinline))
28
Sonny Rao046a37b2011-11-02 09:52:08 +000029/* some reluctance to put this into a new limits.h, so it is here */
30#define INT_MAX ((int)(~0U>>1))
31
Mike Frysinger6c6166f2009-02-16 23:21:36 -050032const char hex_asc[] = "0123456789abcdef";
33#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
34#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
35
36static inline char *pack_hex_byte(char *buf, u8 byte)
37{
38 *buf++ = hex_asc_hi(byte);
39 *buf++ = hex_asc_lo(byte);
40 return buf;
41}
42
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000043unsigned long simple_strtoul(const char *cp, char **endp,
44 unsigned int base)
wdenk153d5112002-08-30 11:07:04 +000045{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000046 unsigned long result = 0;
47 unsigned long value;
wdenk153d5112002-08-30 11:07:04 +000048
49 if (*cp == '0') {
50 cp++;
51 if ((*cp == 'x') && isxdigit(cp[1])) {
52 base = 16;
53 cp++;
54 }
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000055
56 if (!base)
wdenk153d5112002-08-30 11:07:04 +000057 base = 8;
wdenk153d5112002-08-30 11:07:04 +000058 }
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000059
60 if (!base)
wdenk153d5112002-08-30 11:07:04 +000061 base = 10;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000062
wdenk153d5112002-08-30 11:07:04 +000063 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
64 ? toupper(*cp) : *cp)-'A'+10) < base) {
65 result = result*base + value;
66 cp++;
67 }
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000068
wdenk153d5112002-08-30 11:07:04 +000069 if (endp)
70 *endp = (char *)cp;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000071
wdenk153d5112002-08-30 11:07:04 +000072 return result;
73}
74
Heiko Schochera7fd0d92011-05-02 21:33:49 +000075int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
76{
77 char *tail;
78 unsigned long val;
79 size_t len;
80
81 *res = 0;
82 len = strlen(cp);
83 if (len == 0)
84 return -EINVAL;
85
86 val = simple_strtoul(cp, &tail, base);
87 if (tail == cp)
88 return -EINVAL;
89
90 if ((*tail == '\0') ||
91 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
92 *res = val;
93 return 0;
94 }
95
96 return -EINVAL;
97}
98
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +000099long simple_strtol(const char *cp, char **endp, unsigned int base)
wdenk153d5112002-08-30 11:07:04 +0000100{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000101 if (*cp == '-')
102 return -simple_strtoul(cp + 1, endp, base);
103
104 return simple_strtoul(cp, endp, base);
wdenk153d5112002-08-30 11:07:04 +0000105}
106
Kyungmin Park7e6ee7a2008-11-19 16:32:36 +0100107int ustrtoul(const char *cp, char **endp, unsigned int base)
108{
109 unsigned long result = simple_strtoul(cp, endp, base);
110 switch (**endp) {
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000111 case 'G':
Kyungmin Park7e6ee7a2008-11-19 16:32:36 +0100112 result *= 1024;
113 /* fall through */
114 case 'M':
115 result *= 1024;
116 /* fall through */
117 case 'K':
118 case 'k':
119 result *= 1024;
120 if ((*endp)[1] == 'i') {
121 if ((*endp)[2] == 'B')
122 (*endp) += 3;
123 else
124 (*endp) += 2;
125 }
126 }
127 return result;
128}
129
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000130unsigned long long simple_strtoull(const char *cp, char **endp,
131 unsigned int base)
wdenkc40b2952004-03-13 23:29:43 +0000132{
133 unsigned long long result = 0, value;
134
135 if (*cp == '0') {
136 cp++;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000137 if ((*cp == 'x') && isxdigit(cp[1])) {
wdenkc40b2952004-03-13 23:29:43 +0000138 base = 16;
139 cp++;
140 }
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000141
142 if (!base)
wdenkc40b2952004-03-13 23:29:43 +0000143 base = 8;
wdenkc40b2952004-03-13 23:29:43 +0000144 }
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000145
146 if (!base)
wdenkc40b2952004-03-13 23:29:43 +0000147 base = 10;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000148
149 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0'
150 : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {
wdenkc40b2952004-03-13 23:29:43 +0000151 result = result * base + value;
152 cp++;
153 }
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000154
wdenkc40b2952004-03-13 23:29:43 +0000155 if (endp)
156 *endp = (char *) cp;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000157
wdenkc40b2952004-03-13 23:29:43 +0000158 return result;
159}
wdenkc40b2952004-03-13 23:29:43 +0000160
wdenk153d5112002-08-30 11:07:04 +0000161/* we use this so that we can do without the ctype library */
162#define is_digit(c) ((c) >= '0' && (c) <= '9')
163
164static int skip_atoi(const char **s)
165{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000166 int i = 0;
wdenk153d5112002-08-30 11:07:04 +0000167
168 while (is_digit(**s))
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000169 i = i * 10 + *((*s)++) - '0';
170
wdenk153d5112002-08-30 11:07:04 +0000171 return i;
172}
173
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500174/* Decimal conversion is by far the most typical, and is used
175 * for /proc and /sys data. This directly impacts e.g. top performance
176 * with many processes running. We optimize it for speed
177 * using code from
178 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
179 * (with permission from the author, Douglas W. Jones). */
180
181/* Formats correctly any integer in [0,99999].
182 * Outputs from one to five digits depending on input.
183 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000184static char *put_dec_trunc(char *buf, unsigned q)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500185{
186 unsigned d3, d2, d1, d0;
187 d1 = (q>>4) & 0xf;
188 d2 = (q>>8) & 0xf;
189 d3 = (q>>12);
190
191 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
192 q = (d0 * 0xcd) >> 11;
193 d0 = d0 - 10*q;
194 *buf++ = d0 + '0'; /* least significant digit */
195 d1 = q + 9*d3 + 5*d2 + d1;
196 if (d1 != 0) {
197 q = (d1 * 0xcd) >> 11;
198 d1 = d1 - 10*q;
199 *buf++ = d1 + '0'; /* next digit */
200
201 d2 = q + 2*d2;
202 if ((d2 != 0) || (d3 != 0)) {
203 q = (d2 * 0xd) >> 7;
204 d2 = d2 - 10*q;
205 *buf++ = d2 + '0'; /* next digit */
206
207 d3 = q + 4*d3;
208 if (d3 != 0) {
209 q = (d3 * 0xcd) >> 11;
210 d3 = d3 - 10*q;
211 *buf++ = d3 + '0'; /* next digit */
212 if (q != 0)
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000213 *buf++ = q + '0'; /* most sign. digit */
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500214 }
215 }
216 }
217 return buf;
218}
219/* Same with if's removed. Always emits five digits */
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000220static char *put_dec_full(char *buf, unsigned q)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500221{
222 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
223 /* but anyway, gcc produces better code with full-sized ints */
224 unsigned d3, d2, d1, d0;
225 d1 = (q>>4) & 0xf;
226 d2 = (q>>8) & 0xf;
227 d3 = (q>>12);
228
Wolfgang Denkc0a14ae2009-04-05 00:27:57 +0200229 /*
230 * Possible ways to approx. divide by 10
231 * gcc -O2 replaces multiply with shifts and adds
232 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
233 * (x * 0x67) >> 10: 1100111
234 * (x * 0x34) >> 9: 110100 - same
235 * (x * 0x1a) >> 8: 11010 - same
236 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
237 */
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500238
239 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
240 q = (d0 * 0xcd) >> 11;
241 d0 = d0 - 10*q;
242 *buf++ = d0 + '0';
243 d1 = q + 9*d3 + 5*d2 + d1;
244 q = (d1 * 0xcd) >> 11;
245 d1 = d1 - 10*q;
246 *buf++ = d1 + '0';
247
248 d2 = q + 2*d2;
249 q = (d2 * 0xd) >> 7;
250 d2 = d2 - 10*q;
251 *buf++ = d2 + '0';
252
253 d3 = q + 4*d3;
254 q = (d3 * 0xcd) >> 11; /* - shorter code */
255 /* q = (d3 * 0x67) >> 10; - would also work */
256 d3 = d3 - 10*q;
257 *buf++ = d3 + '0';
258 *buf++ = q + '0';
259 return buf;
260}
261/* No inlining helps gcc to use registers better */
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000262static noinline char *put_dec(char *buf, unsigned NUM_TYPE num)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500263{
264 while (1) {
265 unsigned rem;
266 if (num < 100000)
267 return put_dec_trunc(buf, num);
268 rem = do_div(num, 100000);
269 buf = put_dec_full(buf, rem);
270 }
271}
272
wdenk153d5112002-08-30 11:07:04 +0000273#define ZEROPAD 1 /* pad with zero */
274#define SIGN 2 /* unsigned/signed long */
275#define PLUS 4 /* show plus */
276#define SPACE 8 /* space if plus */
277#define LEFT 16 /* left justified */
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500278#define SMALL 32 /* Must be 32 == 0x20 */
279#define SPECIAL 64 /* 0x */
wdenk153d5112002-08-30 11:07:04 +0000280
Sonny Rao046a37b2011-11-02 09:52:08 +0000281#ifdef CONFIG_SYS_VSNPRINTF
282/*
283 * Macro to add a new character to our output string, but only if it will
284 * fit. The macro moves to the next character position in the output string.
285 */
286#define ADDCH(str, ch) do { \
287 if ((str) < end) \
288 *(str) = (ch); \
289 ++str; \
290 } while (0)
291#else
292#define ADDCH(str, ch) (*(str)++ = (ch))
293#endif
294
295static char *number(char *buf, char *end, unsigned NUM_TYPE num,
296 int base, int size, int precision, int type)
wdenk153d5112002-08-30 11:07:04 +0000297{
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500298 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000299 static const char digits[16] = "0123456789ABCDEF";
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500300
301 char tmp[66];
302 char sign;
303 char locase;
304 int need_pfx = ((type & SPECIAL) && base != 10);
wdenk153d5112002-08-30 11:07:04 +0000305 int i;
306
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500307 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
308 * produces same digits or (maybe lowercased) letters */
309 locase = (type & SMALL);
wdenk153d5112002-08-30 11:07:04 +0000310 if (type & LEFT)
311 type &= ~ZEROPAD;
wdenk153d5112002-08-30 11:07:04 +0000312 sign = 0;
313 if (type & SIGN) {
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500314 if ((signed NUM_TYPE) num < 0) {
wdenk153d5112002-08-30 11:07:04 +0000315 sign = '-';
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000316 num = -(signed NUM_TYPE) num;
wdenk153d5112002-08-30 11:07:04 +0000317 size--;
318 } else if (type & PLUS) {
319 sign = '+';
320 size--;
321 } else if (type & SPACE) {
322 sign = ' ';
323 size--;
324 }
325 }
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500326 if (need_pfx) {
327 size--;
wdenk153d5112002-08-30 11:07:04 +0000328 if (base == 16)
wdenk153d5112002-08-30 11:07:04 +0000329 size--;
330 }
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500331
332 /* generate full string in tmp[], in reverse order */
wdenk153d5112002-08-30 11:07:04 +0000333 i = 0;
334 if (num == 0)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500335 tmp[i++] = '0';
336 /* Generic code, for any base:
337 else do {
338 tmp[i++] = (digits[do_div(num,base)] | locase);
339 } while (num != 0);
340 */
341 else if (base != 10) { /* 8 or 16 */
342 int mask = base - 1;
343 int shift = 3;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000344
345 if (base == 16)
346 shift = 4;
347
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500348 do {
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000349 tmp[i++] = (digits[((unsigned char)num) & mask]
350 | locase);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500351 num >>= shift;
352 } while (num);
353 } else { /* base 10 */
354 i = put_dec(tmp, num) - tmp;
355 }
356
357 /* printing 100 using %2d gives "100", not "00" */
wdenk153d5112002-08-30 11:07:04 +0000358 if (i > precision)
359 precision = i;
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500360 /* leading space padding */
wdenk153d5112002-08-30 11:07:04 +0000361 size -= precision;
Sonny Rao046a37b2011-11-02 09:52:08 +0000362 if (!(type & (ZEROPAD + LEFT))) {
363 while (--size >= 0)
364 ADDCH(buf, ' ');
365 }
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500366 /* sign */
wdenk153d5112002-08-30 11:07:04 +0000367 if (sign)
Sonny Rao046a37b2011-11-02 09:52:08 +0000368 ADDCH(buf, sign);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500369 /* "0x" / "0" prefix */
370 if (need_pfx) {
Sonny Rao046a37b2011-11-02 09:52:08 +0000371 ADDCH(buf, '0');
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500372 if (base == 16)
Sonny Rao046a37b2011-11-02 09:52:08 +0000373 ADDCH(buf, 'X' | locase);
wdenk153d5112002-08-30 11:07:04 +0000374 }
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500375 /* zero or space padding */
376 if (!(type & LEFT)) {
377 char c = (type & ZEROPAD) ? '0' : ' ';
Sonny Rao046a37b2011-11-02 09:52:08 +0000378
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500379 while (--size >= 0)
Sonny Rao046a37b2011-11-02 09:52:08 +0000380 ADDCH(buf, c);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500381 }
382 /* hmm even more zero padding? */
383 while (i <= --precision)
Sonny Rao046a37b2011-11-02 09:52:08 +0000384 ADDCH(buf, '0');
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500385 /* actual digits of result */
386 while (--i >= 0)
Sonny Rao046a37b2011-11-02 09:52:08 +0000387 ADDCH(buf, tmp[i]);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500388 /* trailing space padding */
389 while (--size >= 0)
Sonny Rao046a37b2011-11-02 09:52:08 +0000390 ADDCH(buf, ' ');
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500391 return buf;
wdenk153d5112002-08-30 11:07:04 +0000392}
393
Sonny Rao046a37b2011-11-02 09:52:08 +0000394static char *string(char *buf, char *end, char *s, int field_width,
395 int precision, int flags)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500396{
397 int len, i;
wdenk153d5112002-08-30 11:07:04 +0000398
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500399 if (s == 0)
400 s = "<NULL>";
401
402 len = strnlen(s, precision);
403
404 if (!(flags & LEFT))
405 while (len < field_width--)
Sonny Rao046a37b2011-11-02 09:52:08 +0000406 ADDCH(buf, ' ');
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500407 for (i = 0; i < len; ++i)
Sonny Rao046a37b2011-11-02 09:52:08 +0000408 ADDCH(buf, *s++);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500409 while (len < field_width--)
Sonny Rao046a37b2011-11-02 09:52:08 +0000410 ADDCH(buf, ' ');
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500411 return buf;
412}
413
414#ifdef CONFIG_CMD_NET
Sonny Rao046a37b2011-11-02 09:52:08 +0000415static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500416 int precision, int flags)
417{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000418 /* (6 * 2 hex digits), 5 colons and trailing zero */
419 char mac_addr[6 * 3];
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500420 char *p = mac_addr;
421 int i;
422
423 for (i = 0; i < 6; i++) {
424 p = pack_hex_byte(p, addr[i]);
425 if (!(flags & SPECIAL) && i != 5)
426 *p++ = ':';
427 }
428 *p = '\0';
429
Sonny Rao046a37b2011-11-02 09:52:08 +0000430 return string(buf, end, mac_addr, field_width, precision,
431 flags & ~SPECIAL);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500432}
433
Sonny Rao046a37b2011-11-02 09:52:08 +0000434static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500435 int precision, int flags)
436{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000437 /* (8 * 4 hex digits), 7 colons and trailing zero */
438 char ip6_addr[8 * 5];
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500439 char *p = ip6_addr;
440 int i;
441
442 for (i = 0; i < 8; i++) {
443 p = pack_hex_byte(p, addr[2 * i]);
444 p = pack_hex_byte(p, addr[2 * i + 1]);
445 if (!(flags & SPECIAL) && i != 7)
446 *p++ = ':';
447 }
448 *p = '\0';
449
Sonny Rao046a37b2011-11-02 09:52:08 +0000450 return string(buf, end, ip6_addr, field_width, precision,
451 flags & ~SPECIAL);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500452}
453
Sonny Rao046a37b2011-11-02 09:52:08 +0000454static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500455 int precision, int flags)
456{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000457 /* (4 * 3 decimal digits), 3 dots and trailing zero */
458 char ip4_addr[4 * 4];
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500459 char temp[3]; /* hold each IP quad in reverse order */
460 char *p = ip4_addr;
461 int i, digits;
462
463 for (i = 0; i < 4; i++) {
464 digits = put_dec_trunc(temp, addr[i]) - temp;
465 /* reverse the digits in the quad */
466 while (digits--)
467 *p++ = temp[digits];
468 if (i != 3)
469 *p++ = '.';
470 }
471 *p = '\0';
472
Sonny Rao046a37b2011-11-02 09:52:08 +0000473 return string(buf, end, ip4_addr, field_width, precision,
474 flags & ~SPECIAL);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500475}
476#endif
477
478/*
479 * Show a '%p' thing. A kernel extension is that the '%p' is followed
480 * by an extra set of alphanumeric characters that are extended format
481 * specifiers.
482 *
483 * Right now we handle:
484 *
485 * - 'M' For a 6-byte MAC address, it prints the address in the
486 * usual colon-separated hex notation
487 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
488 * decimal for v4 and colon separated network-order 16 bit hex for v6)
489 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
490 * currently the same
491 *
492 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
493 * function pointers are really function descriptors, which contain a
494 * pointer to the real address.
495 */
Sonny Rao046a37b2011-11-02 09:52:08 +0000496static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
497 int field_width, int precision, int flags)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500498{
499 if (!ptr)
Sonny Rao046a37b2011-11-02 09:52:08 +0000500 return string(buf, end, "(null)", field_width, precision,
501 flags);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500502
503#ifdef CONFIG_CMD_NET
504 switch (*fmt) {
505 case 'm':
506 flags |= SPECIAL;
507 /* Fallthrough */
508 case 'M':
Sonny Rao046a37b2011-11-02 09:52:08 +0000509 return mac_address_string(buf, end, ptr, field_width,
510 precision, flags);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500511 case 'i':
512 flags |= SPECIAL;
513 /* Fallthrough */
514 case 'I':
515 if (fmt[1] == '6')
Sonny Rao046a37b2011-11-02 09:52:08 +0000516 return ip6_addr_string(buf, end, ptr, field_width,
517 precision, flags);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500518 if (fmt[1] == '4')
Sonny Rao046a37b2011-11-02 09:52:08 +0000519 return ip4_addr_string(buf, end, ptr, field_width,
520 precision, flags);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500521 flags &= ~SPECIAL;
522 break;
523 }
524#endif
525 flags |= SMALL;
526 if (field_width == -1) {
527 field_width = 2*sizeof(void *);
528 flags |= ZEROPAD;
529 }
Sonny Rao046a37b2011-11-02 09:52:08 +0000530 return number(buf, end, (unsigned long)ptr, 16, field_width,
531 precision, flags);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500532}
533
Sonny Rao046a37b2011-11-02 09:52:08 +0000534static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
535 va_list args)
wdenk153d5112002-08-30 11:07:04 +0000536{
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500537 unsigned NUM_TYPE num;
538 int base;
539 char *str;
wdenk153d5112002-08-30 11:07:04 +0000540
541 int flags; /* flags to number() */
542
543 int field_width; /* width of output field */
544 int precision; /* min. # of digits for integers; max
545 number of chars for from string */
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500546 int qualifier; /* 'h', 'l', or 'L' for integer fields */
547 /* 'z' support added 23/7/1999 S.H. */
548 /* 'z' changed to 'Z' --davidm 1/25/99 */
549 /* 't' added for ptrdiff_t */
Sonny Rao046a37b2011-11-02 09:52:08 +0000550 char *end = buf + size;
wdenk153d5112002-08-30 11:07:04 +0000551
Sonny Rao046a37b2011-11-02 09:52:08 +0000552#ifdef CONFIG_SYS_VSNPRINTF
553 /* Make sure end is always >= buf - do we want this in U-Boot? */
554 if (end < buf) {
555 end = ((void *)-1);
556 size = end - buf;
557 }
558#endif
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500559 str = buf;
560
561 for (; *fmt ; ++fmt) {
wdenk153d5112002-08-30 11:07:04 +0000562 if (*fmt != '%') {
Sonny Rao046a37b2011-11-02 09:52:08 +0000563 ADDCH(str, *fmt);
wdenk153d5112002-08-30 11:07:04 +0000564 continue;
565 }
566
567 /* process flags */
568 flags = 0;
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000569repeat:
wdenk153d5112002-08-30 11:07:04 +0000570 ++fmt; /* this also skips first '%' */
571 switch (*fmt) {
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000572 case '-':
573 flags |= LEFT;
574 goto repeat;
575 case '+':
576 flags |= PLUS;
577 goto repeat;
578 case ' ':
579 flags |= SPACE;
580 goto repeat;
581 case '#':
582 flags |= SPECIAL;
583 goto repeat;
584 case '0':
585 flags |= ZEROPAD;
586 goto repeat;
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500587 }
wdenk153d5112002-08-30 11:07:04 +0000588
589 /* get field width */
590 field_width = -1;
591 if (is_digit(*fmt))
592 field_width = skip_atoi(&fmt);
593 else if (*fmt == '*') {
594 ++fmt;
595 /* it's the next argument */
596 field_width = va_arg(args, int);
597 if (field_width < 0) {
598 field_width = -field_width;
599 flags |= LEFT;
600 }
601 }
602
603 /* get the precision */
604 precision = -1;
605 if (*fmt == '.') {
606 ++fmt;
607 if (is_digit(*fmt))
608 precision = skip_atoi(&fmt);
609 else if (*fmt == '*') {
610 ++fmt;
611 /* it's the next argument */
612 precision = va_arg(args, int);
613 }
614 if (precision < 0)
615 precision = 0;
616 }
617
618 /* get the conversion qualifier */
619 qualifier = -1;
Jean-Christophe PLAGNIOL-VILLARDf354b732008-07-14 14:11:45 +0200620 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500621 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
wdenk153d5112002-08-30 11:07:04 +0000622 qualifier = *fmt;
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500623 ++fmt;
624 if (qualifier == 'l' && *fmt == 'l') {
625 qualifier = 'L';
James Yangbf052932008-01-10 16:02:07 -0600626 ++fmt;
627 }
wdenk153d5112002-08-30 11:07:04 +0000628 }
629
630 /* default base */
631 base = 10;
632
633 switch (*fmt) {
634 case 'c':
Sonny Rao046a37b2011-11-02 09:52:08 +0000635 if (!(flags & LEFT)) {
wdenk153d5112002-08-30 11:07:04 +0000636 while (--field_width > 0)
Sonny Rao046a37b2011-11-02 09:52:08 +0000637 ADDCH(str, ' ');
638 }
639 ADDCH(str, (unsigned char) va_arg(args, int));
wdenk153d5112002-08-30 11:07:04 +0000640 while (--field_width > 0)
Sonny Rao046a37b2011-11-02 09:52:08 +0000641 ADDCH(str, ' ');
wdenk153d5112002-08-30 11:07:04 +0000642 continue;
643
644 case 's':
Sonny Rao046a37b2011-11-02 09:52:08 +0000645 str = string(str, end, va_arg(args, char *),
646 field_width, precision, flags);
wdenk153d5112002-08-30 11:07:04 +0000647 continue;
648
649 case 'p':
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000650 str = pointer(fmt + 1, str, end,
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500651 va_arg(args, void *),
652 field_width, precision, flags);
653 /* Skip all alphanumeric pointer suffixes */
654 while (isalnum(fmt[1]))
655 fmt++;
wdenk153d5112002-08-30 11:07:04 +0000656 continue;
657
wdenk153d5112002-08-30 11:07:04 +0000658 case 'n':
659 if (qualifier == 'l') {
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000660 long *ip = va_arg(args, long *);
wdenk153d5112002-08-30 11:07:04 +0000661 *ip = (str - buf);
662 } else {
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000663 int *ip = va_arg(args, int *);
wdenk153d5112002-08-30 11:07:04 +0000664 *ip = (str - buf);
665 }
666 continue;
667
668 case '%':
Sonny Rao046a37b2011-11-02 09:52:08 +0000669 ADDCH(str, '%');
wdenk153d5112002-08-30 11:07:04 +0000670 continue;
671
672 /* integer number formats - set up the flags and "break" */
673 case 'o':
674 base = 8;
675 break;
676
wdenk153d5112002-08-30 11:07:04 +0000677 case 'x':
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500678 flags |= SMALL;
679 case 'X':
wdenk153d5112002-08-30 11:07:04 +0000680 base = 16;
681 break;
682
683 case 'd':
684 case 'i':
685 flags |= SIGN;
686 case 'u':
687 break;
688
689 default:
Sonny Rao046a37b2011-11-02 09:52:08 +0000690 ADDCH(str, '%');
wdenk153d5112002-08-30 11:07:04 +0000691 if (*fmt)
Sonny Rao046a37b2011-11-02 09:52:08 +0000692 ADDCH(str, *fmt);
wdenk153d5112002-08-30 11:07:04 +0000693 else
694 --fmt;
695 continue;
696 }
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500697 if (qualifier == 'L') /* "quad" for 64 bit variables */
wdenkc40b2952004-03-13 23:29:43 +0000698 num = va_arg(args, unsigned long long);
Heiko Schocher4b142fe2009-12-03 11:21:21 +0100699 else if (qualifier == 'l') {
wdenk153d5112002-08-30 11:07:04 +0000700 num = va_arg(args, unsigned long);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500701 if (flags & SIGN)
702 num = (signed long) num;
Jean-Christophe PLAGNIOL-VILLARDf354b732008-07-14 14:11:45 +0200703 } else if (qualifier == 'Z' || qualifier == 'z') {
704 num = va_arg(args, size_t);
705 } else if (qualifier == 't') {
706 num = va_arg(args, ptrdiff_t);
707 } else if (qualifier == 'h') {
wdenk153d5112002-08-30 11:07:04 +0000708 num = (unsigned short) va_arg(args, int);
709 if (flags & SIGN)
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500710 num = (signed short) num;
711 } else {
wdenk153d5112002-08-30 11:07:04 +0000712 num = va_arg(args, unsigned int);
Mike Frysinger6c6166f2009-02-16 23:21:36 -0500713 if (flags & SIGN)
714 num = (signed int) num;
715 }
Sonny Rao046a37b2011-11-02 09:52:08 +0000716 str = number(str, end, num, base, field_width, precision,
717 flags);
wdenk153d5112002-08-30 11:07:04 +0000718 }
Sonny Rao046a37b2011-11-02 09:52:08 +0000719
720#ifdef CONFIG_SYS_VSNPRINTF
721 if (size > 0) {
722 ADDCH(str, '\0');
723 if (str > end)
724 end[-1] = '\0';
725 }
726#else
wdenk153d5112002-08-30 11:07:04 +0000727 *str = '\0';
Sonny Rao046a37b2011-11-02 09:52:08 +0000728#endif
729 /* the trailing null byte doesn't count towards the total */
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000730 return str - buf;
wdenk153d5112002-08-30 11:07:04 +0000731}
732
Sonny Rao046a37b2011-11-02 09:52:08 +0000733#ifdef CONFIG_SYS_VSNPRINTF
734int vsnprintf(char *buf, size_t size, const char *fmt,
735 va_list args)
736{
737 return vsnprintf_internal(buf, size, fmt, args);
738}
739
Sonny Rao046a37b2011-11-02 09:52:08 +0000740int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
741{
742 int i;
743
744 i = vsnprintf(buf, size, fmt, args);
745
746 if (likely(i < size))
747 return i;
748 if (size != 0)
749 return size - 1;
750 return 0;
751}
752
Sonny Rao046a37b2011-11-02 09:52:08 +0000753int snprintf(char *buf, size_t size, const char *fmt, ...)
754{
755 va_list args;
756 int i;
757
758 va_start(args, fmt);
759 i = vsnprintf(buf, size, fmt, args);
760 va_end(args);
761
762 return i;
763}
764
Sonny Rao046a37b2011-11-02 09:52:08 +0000765int scnprintf(char *buf, size_t size, const char *fmt, ...)
766{
767 va_list args;
768 int i;
769
770 va_start(args, fmt);
771 i = vscnprintf(buf, size, fmt, args);
772 va_end(args);
773
774 return i;
775}
776#endif /* CONFIG_SYS_VSNPRINT */
777
778/**
779 * Format a string and place it in a buffer (va_list version)
780 *
781 * @param buf The buffer to place the result into
782 * @param fmt The format string to use
783 * @param args Arguments for the format string
784 *
785 * The function returns the number of characters written
786 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
787 * buffer overflows.
788 *
789 * If you're not already dealing with a va_list consider using sprintf().
790 */
791int vsprintf(char *buf, const char *fmt, va_list args)
792{
793 return vsnprintf_internal(buf, INT_MAX, fmt, args);
794}
795
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000796int sprintf(char *buf, const char *fmt, ...)
wdenk153d5112002-08-30 11:07:04 +0000797{
798 va_list args;
799 int i;
800
801 va_start(args, fmt);
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000802 i = vsprintf(buf, fmt, args);
wdenk153d5112002-08-30 11:07:04 +0000803 va_end(args);
804 return i;
805}
806
807void panic(const char *fmt, ...)
808{
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000809 va_list args;
wdenk153d5112002-08-30 11:07:04 +0000810 va_start(args, fmt);
wdenk6dd652f2003-06-19 23:40:20 +0000811 vprintf(fmt, args);
wdenk153d5112002-08-30 11:07:04 +0000812 putc('\n');
813 va_end(args);
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000814#if defined(CONFIG_PANIC_HANG)
wdenk153d5112002-08-30 11:07:04 +0000815 hang();
816#else
Daniel Schwierzeck8acdae62012-09-16 06:55:03 +0000817 udelay(100000); /* allow messages to go out */
818 do_reset(NULL, 0, 0, NULL);
wdenk153d5112002-08-30 11:07:04 +0000819#endif
Heiko Schocher40e01882011-07-26 20:55:57 +0000820 while (1)
821 ;
wdenk153d5112002-08-30 11:07:04 +0000822}
Simon Glass21726a72011-06-29 09:49:34 +0000823
824void __assert_fail(const char *assertion, const char *file, unsigned line,
825 const char *function)
826{
827 /* This will not return */
828 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
829 assertion);
830}
Simon Glass3cce8a52011-10-21 18:51:33 +0000831
832char *simple_itoa(ulong i)
833{
834 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
835 static char local[22];
836 char *p = &local[21];
837
838 *p-- = '\0';
839 do {
840 *p-- = '0' + i % 10;
841 i /= 10;
842 } while (i > 0);
843 return p + 1;
844}