blob: 82e5c13653b68d490257a244701966051d05d2fe [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 Glass931948a2011-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 Schocher8d4f35a2011-05-02 21:33:49 +000018#include <errno.h>
wdenk153d5112002-08-30 11:07:04 +000019
20#include <common.h>
Daniel Schwierzeckfc147852012-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 Behme3d8759e2009-07-22 17:51:56 +020025#include <div64.h>
Dirk Behme3d8759e2009-07-22 17:51:56 +020026#define noinline __attribute__((noinline))
27
Sonny Raocd55bde2011-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
Kim Phillips75541502012-10-29 13:34:36 +000031static const char hex_asc[] = "0123456789abcdef";
Mike Frysinger2b745dc2009-02-16 23:21:36 -050032#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 Schwierzeckfc147852012-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 Schwierzeckfc147852012-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 Schwierzeckfc147852012-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 Schwierzeckfc147852012-09-16 06:55:03 +000058
59 if (!base)
wdenk153d5112002-08-30 11:07:04 +000060 base = 10;
Daniel Schwierzeckfc147852012-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 Schwierzeckfc147852012-09-16 06:55:03 +000067
wdenk153d5112002-08-30 11:07:04 +000068 if (endp)
69 *endp = (char *)cp;
Daniel Schwierzeckfc147852012-09-16 06:55:03 +000070
wdenk153d5112002-08-30 11:07:04 +000071 return result;
72}
73
Heiko Schocher8d4f35a2011-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 Schwierzeckfc147852012-09-16 06:55:03 +000098long simple_strtol(const char *cp, char **endp, unsigned int base)
wdenk153d5112002-08-30 11:07:04 +000099{
Daniel Schwierzeckfc147852012-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
Lukasz Majewski99180d62012-12-11 11:09:42 +0100106unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
Kyungmin Parkad7ae362008-11-19 16:32:36 +0100107{
108 unsigned long result = simple_strtoul(cp, endp, base);
109 switch (**endp) {
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000110 case 'G':
Kyungmin Parkad7ae362008-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
Piotr Wilczek219ad3c2013-01-27 22:59:24 +0000129unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
130{
131 unsigned long long result = simple_strtoull(cp, endp, base);
132 switch (**endp) {
133 case 'G':
134 result *= 1024;
135 /* fall through */
136 case 'M':
137 result *= 1024;
138 /* fall through */
139 case 'K':
140 case 'k':
141 result *= 1024;
142 if ((*endp)[1] == 'i') {
143 if ((*endp)[2] == 'B')
144 (*endp) += 3;
145 else
146 (*endp) += 2;
147 }
148 }
149 return result;
150}
151
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000152unsigned long long simple_strtoull(const char *cp, char **endp,
153 unsigned int base)
wdenkf602aa02004-03-13 23:29:43 +0000154{
155 unsigned long long result = 0, value;
156
157 if (*cp == '0') {
158 cp++;
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000159 if ((*cp == 'x') && isxdigit(cp[1])) {
wdenkf602aa02004-03-13 23:29:43 +0000160 base = 16;
161 cp++;
162 }
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000163
164 if (!base)
wdenkf602aa02004-03-13 23:29:43 +0000165 base = 8;
wdenkf602aa02004-03-13 23:29:43 +0000166 }
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000167
168 if (!base)
wdenkf602aa02004-03-13 23:29:43 +0000169 base = 10;
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000170
171 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0'
172 : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {
wdenkf602aa02004-03-13 23:29:43 +0000173 result = result * base + value;
174 cp++;
175 }
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000176
wdenkf602aa02004-03-13 23:29:43 +0000177 if (endp)
178 *endp = (char *) cp;
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000179
wdenkf602aa02004-03-13 23:29:43 +0000180 return result;
181}
wdenkf602aa02004-03-13 23:29:43 +0000182
wdenk153d5112002-08-30 11:07:04 +0000183/* we use this so that we can do without the ctype library */
184#define is_digit(c) ((c) >= '0' && (c) <= '9')
185
186static int skip_atoi(const char **s)
187{
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000188 int i = 0;
wdenk153d5112002-08-30 11:07:04 +0000189
190 while (is_digit(**s))
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000191 i = i * 10 + *((*s)++) - '0';
192
wdenk153d5112002-08-30 11:07:04 +0000193 return i;
194}
195
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500196/* Decimal conversion is by far the most typical, and is used
197 * for /proc and /sys data. This directly impacts e.g. top performance
198 * with many processes running. We optimize it for speed
199 * using code from
200 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
201 * (with permission from the author, Douglas W. Jones). */
202
203/* Formats correctly any integer in [0,99999].
204 * Outputs from one to five digits depending on input.
205 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000206static char *put_dec_trunc(char *buf, unsigned q)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500207{
208 unsigned d3, d2, d1, d0;
209 d1 = (q>>4) & 0xf;
210 d2 = (q>>8) & 0xf;
211 d3 = (q>>12);
212
213 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
214 q = (d0 * 0xcd) >> 11;
215 d0 = d0 - 10*q;
216 *buf++ = d0 + '0'; /* least significant digit */
217 d1 = q + 9*d3 + 5*d2 + d1;
218 if (d1 != 0) {
219 q = (d1 * 0xcd) >> 11;
220 d1 = d1 - 10*q;
221 *buf++ = d1 + '0'; /* next digit */
222
223 d2 = q + 2*d2;
224 if ((d2 != 0) || (d3 != 0)) {
225 q = (d2 * 0xd) >> 7;
226 d2 = d2 - 10*q;
227 *buf++ = d2 + '0'; /* next digit */
228
229 d3 = q + 4*d3;
230 if (d3 != 0) {
231 q = (d3 * 0xcd) >> 11;
232 d3 = d3 - 10*q;
233 *buf++ = d3 + '0'; /* next digit */
234 if (q != 0)
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000235 *buf++ = q + '0'; /* most sign. digit */
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500236 }
237 }
238 }
239 return buf;
240}
241/* Same with if's removed. Always emits five digits */
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000242static char *put_dec_full(char *buf, unsigned q)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500243{
244 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
245 /* but anyway, gcc produces better code with full-sized ints */
246 unsigned d3, d2, d1, d0;
247 d1 = (q>>4) & 0xf;
248 d2 = (q>>8) & 0xf;
249 d3 = (q>>12);
250
Wolfgang Denke366f152009-04-05 00:27:57 +0200251 /*
252 * Possible ways to approx. divide by 10
253 * gcc -O2 replaces multiply with shifts and adds
254 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
255 * (x * 0x67) >> 10: 1100111
256 * (x * 0x34) >> 9: 110100 - same
257 * (x * 0x1a) >> 8: 11010 - same
258 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
259 */
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500260
261 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
262 q = (d0 * 0xcd) >> 11;
263 d0 = d0 - 10*q;
264 *buf++ = d0 + '0';
265 d1 = q + 9*d3 + 5*d2 + d1;
266 q = (d1 * 0xcd) >> 11;
267 d1 = d1 - 10*q;
268 *buf++ = d1 + '0';
269
270 d2 = q + 2*d2;
271 q = (d2 * 0xd) >> 7;
272 d2 = d2 - 10*q;
273 *buf++ = d2 + '0';
274
275 d3 = q + 4*d3;
276 q = (d3 * 0xcd) >> 11; /* - shorter code */
277 /* q = (d3 * 0x67) >> 10; - would also work */
278 d3 = d3 - 10*q;
279 *buf++ = d3 + '0';
280 *buf++ = q + '0';
281 return buf;
282}
283/* No inlining helps gcc to use registers better */
Daniel Schwierzecka3e5c522012-09-16 06:55:04 +0000284static noinline char *put_dec(char *buf, u64 num)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500285{
286 while (1) {
287 unsigned rem;
288 if (num < 100000)
289 return put_dec_trunc(buf, num);
290 rem = do_div(num, 100000);
291 buf = put_dec_full(buf, rem);
292 }
293}
294
wdenk153d5112002-08-30 11:07:04 +0000295#define ZEROPAD 1 /* pad with zero */
296#define SIGN 2 /* unsigned/signed long */
297#define PLUS 4 /* show plus */
298#define SPACE 8 /* space if plus */
299#define LEFT 16 /* left justified */
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500300#define SMALL 32 /* Must be 32 == 0x20 */
301#define SPECIAL 64 /* 0x */
wdenk153d5112002-08-30 11:07:04 +0000302
Sonny Raocd55bde2011-11-02 09:52:08 +0000303#ifdef CONFIG_SYS_VSNPRINTF
304/*
305 * Macro to add a new character to our output string, but only if it will
306 * fit. The macro moves to the next character position in the output string.
307 */
308#define ADDCH(str, ch) do { \
309 if ((str) < end) \
310 *(str) = (ch); \
311 ++str; \
312 } while (0)
313#else
314#define ADDCH(str, ch) (*(str)++ = (ch))
315#endif
316
Daniel Schwierzecka3e5c522012-09-16 06:55:04 +0000317static char *number(char *buf, char *end, u64 num,
Sonny Raocd55bde2011-11-02 09:52:08 +0000318 int base, int size, int precision, int type)
wdenk153d5112002-08-30 11:07:04 +0000319{
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500320 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000321 static const char digits[16] = "0123456789ABCDEF";
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500322
323 char tmp[66];
324 char sign;
325 char locase;
326 int need_pfx = ((type & SPECIAL) && base != 10);
wdenk153d5112002-08-30 11:07:04 +0000327 int i;
328
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500329 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
330 * produces same digits or (maybe lowercased) letters */
331 locase = (type & SMALL);
wdenk153d5112002-08-30 11:07:04 +0000332 if (type & LEFT)
333 type &= ~ZEROPAD;
wdenk153d5112002-08-30 11:07:04 +0000334 sign = 0;
335 if (type & SIGN) {
Daniel Schwierzecka3e5c522012-09-16 06:55:04 +0000336 if ((s64) num < 0) {
wdenk153d5112002-08-30 11:07:04 +0000337 sign = '-';
Daniel Schwierzecka3e5c522012-09-16 06:55:04 +0000338 num = -(s64) num;
wdenk153d5112002-08-30 11:07:04 +0000339 size--;
340 } else if (type & PLUS) {
341 sign = '+';
342 size--;
343 } else if (type & SPACE) {
344 sign = ' ';
345 size--;
346 }
347 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500348 if (need_pfx) {
349 size--;
wdenk153d5112002-08-30 11:07:04 +0000350 if (base == 16)
wdenk153d5112002-08-30 11:07:04 +0000351 size--;
352 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500353
354 /* generate full string in tmp[], in reverse order */
wdenk153d5112002-08-30 11:07:04 +0000355 i = 0;
356 if (num == 0)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500357 tmp[i++] = '0';
358 /* Generic code, for any base:
359 else do {
360 tmp[i++] = (digits[do_div(num,base)] | locase);
361 } while (num != 0);
362 */
363 else if (base != 10) { /* 8 or 16 */
364 int mask = base - 1;
365 int shift = 3;
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000366
367 if (base == 16)
368 shift = 4;
369
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500370 do {
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000371 tmp[i++] = (digits[((unsigned char)num) & mask]
372 | locase);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500373 num >>= shift;
374 } while (num);
375 } else { /* base 10 */
376 i = put_dec(tmp, num) - tmp;
377 }
378
379 /* printing 100 using %2d gives "100", not "00" */
wdenk153d5112002-08-30 11:07:04 +0000380 if (i > precision)
381 precision = i;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500382 /* leading space padding */
wdenk153d5112002-08-30 11:07:04 +0000383 size -= precision;
Sonny Raocd55bde2011-11-02 09:52:08 +0000384 if (!(type & (ZEROPAD + LEFT))) {
385 while (--size >= 0)
386 ADDCH(buf, ' ');
387 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500388 /* sign */
wdenk153d5112002-08-30 11:07:04 +0000389 if (sign)
Sonny Raocd55bde2011-11-02 09:52:08 +0000390 ADDCH(buf, sign);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500391 /* "0x" / "0" prefix */
392 if (need_pfx) {
Sonny Raocd55bde2011-11-02 09:52:08 +0000393 ADDCH(buf, '0');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500394 if (base == 16)
Sonny Raocd55bde2011-11-02 09:52:08 +0000395 ADDCH(buf, 'X' | locase);
wdenk153d5112002-08-30 11:07:04 +0000396 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500397 /* zero or space padding */
398 if (!(type & LEFT)) {
399 char c = (type & ZEROPAD) ? '0' : ' ';
Sonny Raocd55bde2011-11-02 09:52:08 +0000400
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500401 while (--size >= 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000402 ADDCH(buf, c);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500403 }
404 /* hmm even more zero padding? */
405 while (i <= --precision)
Sonny Raocd55bde2011-11-02 09:52:08 +0000406 ADDCH(buf, '0');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500407 /* actual digits of result */
408 while (--i >= 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000409 ADDCH(buf, tmp[i]);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500410 /* trailing space padding */
411 while (--size >= 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000412 ADDCH(buf, ' ');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500413 return buf;
wdenk153d5112002-08-30 11:07:04 +0000414}
415
Sonny Raocd55bde2011-11-02 09:52:08 +0000416static char *string(char *buf, char *end, char *s, int field_width,
417 int precision, int flags)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500418{
419 int len, i;
wdenk153d5112002-08-30 11:07:04 +0000420
Kim Phillips75541502012-10-29 13:34:36 +0000421 if (s == NULL)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500422 s = "<NULL>";
423
424 len = strnlen(s, precision);
425
426 if (!(flags & LEFT))
427 while (len < field_width--)
Sonny Raocd55bde2011-11-02 09:52:08 +0000428 ADDCH(buf, ' ');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500429 for (i = 0; i < len; ++i)
Sonny Raocd55bde2011-11-02 09:52:08 +0000430 ADDCH(buf, *s++);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500431 while (len < field_width--)
Sonny Raocd55bde2011-11-02 09:52:08 +0000432 ADDCH(buf, ' ');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500433 return buf;
434}
435
436#ifdef CONFIG_CMD_NET
Sonny Raocd55bde2011-11-02 09:52:08 +0000437static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500438 int precision, int flags)
wdenk153d5112002-08-30 11:07:04 +0000439{
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000440 /* (6 * 2 hex digits), 5 colons and trailing zero */
441 char mac_addr[6 * 3];
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500442 char *p = mac_addr;
443 int i;
444
445 for (i = 0; i < 6; i++) {
446 p = pack_hex_byte(p, addr[i]);
447 if (!(flags & SPECIAL) && i != 5)
448 *p++ = ':';
449 }
450 *p = '\0';
451
Sonny Raocd55bde2011-11-02 09:52:08 +0000452 return string(buf, end, mac_addr, field_width, precision,
453 flags & ~SPECIAL);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500454}
455
Sonny Raocd55bde2011-11-02 09:52:08 +0000456static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500457 int precision, int flags)
458{
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000459 /* (8 * 4 hex digits), 7 colons and trailing zero */
460 char ip6_addr[8 * 5];
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500461 char *p = ip6_addr;
462 int i;
463
464 for (i = 0; i < 8; i++) {
465 p = pack_hex_byte(p, addr[2 * i]);
466 p = pack_hex_byte(p, addr[2 * i + 1]);
467 if (!(flags & SPECIAL) && i != 7)
468 *p++ = ':';
469 }
470 *p = '\0';
471
Sonny Raocd55bde2011-11-02 09:52:08 +0000472 return string(buf, end, ip6_addr, field_width, precision,
473 flags & ~SPECIAL);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500474}
475
Sonny Raocd55bde2011-11-02 09:52:08 +0000476static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500477 int precision, int flags)
478{
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000479 /* (4 * 3 decimal digits), 3 dots and trailing zero */
480 char ip4_addr[4 * 4];
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500481 char temp[3]; /* hold each IP quad in reverse order */
482 char *p = ip4_addr;
483 int i, digits;
484
485 for (i = 0; i < 4; i++) {
486 digits = put_dec_trunc(temp, addr[i]) - temp;
487 /* reverse the digits in the quad */
488 while (digits--)
489 *p++ = temp[digits];
490 if (i != 3)
491 *p++ = '.';
492 }
493 *p = '\0';
494
Sonny Raocd55bde2011-11-02 09:52:08 +0000495 return string(buf, end, ip4_addr, field_width, precision,
496 flags & ~SPECIAL);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500497}
498#endif
499
500/*
501 * Show a '%p' thing. A kernel extension is that the '%p' is followed
502 * by an extra set of alphanumeric characters that are extended format
503 * specifiers.
504 *
505 * Right now we handle:
506 *
507 * - 'M' For a 6-byte MAC address, it prints the address in the
508 * usual colon-separated hex notation
509 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
510 * decimal for v4 and colon separated network-order 16 bit hex for v6)
511 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
512 * currently the same
513 *
514 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
515 * function pointers are really function descriptors, which contain a
516 * pointer to the real address.
517 */
Sonny Raocd55bde2011-11-02 09:52:08 +0000518static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
519 int field_width, int precision, int flags)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500520{
Wolfgang Denk88a8b012012-10-30 09:19:52 +0000521 /*
522 * Being a boot loader, we explicitly allow pointers to
523 * (physical) address null.
524 */
525#if 0
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500526 if (!ptr)
Sonny Raocd55bde2011-11-02 09:52:08 +0000527 return string(buf, end, "(null)", field_width, precision,
528 flags);
Wolfgang Denk88a8b012012-10-30 09:19:52 +0000529#endif
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500530
531#ifdef CONFIG_CMD_NET
532 switch (*fmt) {
533 case 'm':
534 flags |= SPECIAL;
535 /* Fallthrough */
536 case 'M':
Sonny Raocd55bde2011-11-02 09:52:08 +0000537 return mac_address_string(buf, end, ptr, field_width,
538 precision, flags);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500539 case 'i':
540 flags |= SPECIAL;
541 /* Fallthrough */
542 case 'I':
543 if (fmt[1] == '6')
Sonny Raocd55bde2011-11-02 09:52:08 +0000544 return ip6_addr_string(buf, end, ptr, field_width,
545 precision, flags);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500546 if (fmt[1] == '4')
Sonny Raocd55bde2011-11-02 09:52:08 +0000547 return ip4_addr_string(buf, end, ptr, field_width,
548 precision, flags);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500549 flags &= ~SPECIAL;
550 break;
551 }
wdenkf602aa02004-03-13 23:29:43 +0000552#endif
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500553 flags |= SMALL;
554 if (field_width == -1) {
555 field_width = 2*sizeof(void *);
556 flags |= ZEROPAD;
557 }
Sonny Raocd55bde2011-11-02 09:52:08 +0000558 return number(buf, end, (unsigned long)ptr, 16, field_width,
559 precision, flags);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500560}
561
Sonny Raocd55bde2011-11-02 09:52:08 +0000562static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
563 va_list args)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500564{
Daniel Schwierzecka3e5c522012-09-16 06:55:04 +0000565 u64 num;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500566 int base;
567 char *str;
wdenk153d5112002-08-30 11:07:04 +0000568
569 int flags; /* flags to number() */
570
571 int field_width; /* width of output field */
572 int precision; /* min. # of digits for integers; max
573 number of chars for from string */
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500574 int qualifier; /* 'h', 'l', or 'L' for integer fields */
575 /* 'z' support added 23/7/1999 S.H. */
576 /* 'z' changed to 'Z' --davidm 1/25/99 */
577 /* 't' added for ptrdiff_t */
Sonny Raocd55bde2011-11-02 09:52:08 +0000578 char *end = buf + size;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500579
Sonny Raocd55bde2011-11-02 09:52:08 +0000580#ifdef CONFIG_SYS_VSNPRINTF
581 /* Make sure end is always >= buf - do we want this in U-Boot? */
582 if (end < buf) {
583 end = ((void *)-1);
584 size = end - buf;
585 }
586#endif
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500587 str = buf;
wdenk153d5112002-08-30 11:07:04 +0000588
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500589 for (; *fmt ; ++fmt) {
wdenk153d5112002-08-30 11:07:04 +0000590 if (*fmt != '%') {
Sonny Raocd55bde2011-11-02 09:52:08 +0000591 ADDCH(str, *fmt);
wdenk153d5112002-08-30 11:07:04 +0000592 continue;
593 }
594
595 /* process flags */
596 flags = 0;
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000597repeat:
wdenk153d5112002-08-30 11:07:04 +0000598 ++fmt; /* this also skips first '%' */
599 switch (*fmt) {
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000600 case '-':
601 flags |= LEFT;
602 goto repeat;
603 case '+':
604 flags |= PLUS;
605 goto repeat;
606 case ' ':
607 flags |= SPACE;
608 goto repeat;
609 case '#':
610 flags |= SPECIAL;
611 goto repeat;
612 case '0':
613 flags |= ZEROPAD;
614 goto repeat;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500615 }
wdenk153d5112002-08-30 11:07:04 +0000616
617 /* get field width */
618 field_width = -1;
619 if (is_digit(*fmt))
620 field_width = skip_atoi(&fmt);
621 else if (*fmt == '*') {
622 ++fmt;
623 /* it's the next argument */
624 field_width = va_arg(args, int);
625 if (field_width < 0) {
626 field_width = -field_width;
627 flags |= LEFT;
628 }
629 }
630
631 /* get the precision */
632 precision = -1;
633 if (*fmt == '.') {
634 ++fmt;
635 if (is_digit(*fmt))
636 precision = skip_atoi(&fmt);
637 else if (*fmt == '*') {
638 ++fmt;
639 /* it's the next argument */
640 precision = va_arg(args, int);
641 }
642 if (precision < 0)
643 precision = 0;
644 }
645
646 /* get the conversion qualifier */
647 qualifier = -1;
Jean-Christophe PLAGNIOL-VILLARD0f720692008-07-14 14:11:45 +0200648 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500649 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
wdenk153d5112002-08-30 11:07:04 +0000650 qualifier = *fmt;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500651 ++fmt;
652 if (qualifier == 'l' && *fmt == 'l') {
653 qualifier = 'L';
James Yang7fc40152008-01-10 16:02:07 -0600654 ++fmt;
655 }
wdenk153d5112002-08-30 11:07:04 +0000656 }
657
658 /* default base */
659 base = 10;
660
661 switch (*fmt) {
662 case 'c':
Sonny Raocd55bde2011-11-02 09:52:08 +0000663 if (!(flags & LEFT)) {
wdenk153d5112002-08-30 11:07:04 +0000664 while (--field_width > 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000665 ADDCH(str, ' ');
666 }
667 ADDCH(str, (unsigned char) va_arg(args, int));
wdenk153d5112002-08-30 11:07:04 +0000668 while (--field_width > 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000669 ADDCH(str, ' ');
wdenk153d5112002-08-30 11:07:04 +0000670 continue;
671
672 case 's':
Sonny Raocd55bde2011-11-02 09:52:08 +0000673 str = string(str, end, va_arg(args, char *),
674 field_width, precision, flags);
wdenk153d5112002-08-30 11:07:04 +0000675 continue;
676
677 case 'p':
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000678 str = pointer(fmt + 1, str, end,
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500679 va_arg(args, void *),
680 field_width, precision, flags);
681 /* Skip all alphanumeric pointer suffixes */
682 while (isalnum(fmt[1]))
683 fmt++;
wdenk153d5112002-08-30 11:07:04 +0000684 continue;
685
wdenk153d5112002-08-30 11:07:04 +0000686 case 'n':
687 if (qualifier == 'l') {
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000688 long *ip = va_arg(args, long *);
wdenk153d5112002-08-30 11:07:04 +0000689 *ip = (str - buf);
690 } else {
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000691 int *ip = va_arg(args, int *);
wdenk153d5112002-08-30 11:07:04 +0000692 *ip = (str - buf);
693 }
694 continue;
695
696 case '%':
Sonny Raocd55bde2011-11-02 09:52:08 +0000697 ADDCH(str, '%');
wdenk153d5112002-08-30 11:07:04 +0000698 continue;
699
700 /* integer number formats - set up the flags and "break" */
701 case 'o':
702 base = 8;
703 break;
704
wdenk153d5112002-08-30 11:07:04 +0000705 case 'x':
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500706 flags |= SMALL;
707 case 'X':
wdenk153d5112002-08-30 11:07:04 +0000708 base = 16;
709 break;
710
711 case 'd':
712 case 'i':
713 flags |= SIGN;
714 case 'u':
715 break;
716
717 default:
Sonny Raocd55bde2011-11-02 09:52:08 +0000718 ADDCH(str, '%');
wdenk153d5112002-08-30 11:07:04 +0000719 if (*fmt)
Sonny Raocd55bde2011-11-02 09:52:08 +0000720 ADDCH(str, *fmt);
wdenk153d5112002-08-30 11:07:04 +0000721 else
722 --fmt;
723 continue;
724 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500725 if (qualifier == 'L') /* "quad" for 64 bit variables */
wdenkf602aa02004-03-13 23:29:43 +0000726 num = va_arg(args, unsigned long long);
Heiko Schocher0f602e12009-12-03 11:21:21 +0100727 else if (qualifier == 'l') {
wdenk153d5112002-08-30 11:07:04 +0000728 num = va_arg(args, unsigned long);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500729 if (flags & SIGN)
730 num = (signed long) num;
Jean-Christophe PLAGNIOL-VILLARD0f720692008-07-14 14:11:45 +0200731 } else if (qualifier == 'Z' || qualifier == 'z') {
732 num = va_arg(args, size_t);
733 } else if (qualifier == 't') {
734 num = va_arg(args, ptrdiff_t);
735 } else if (qualifier == 'h') {
wdenk153d5112002-08-30 11:07:04 +0000736 num = (unsigned short) va_arg(args, int);
737 if (flags & SIGN)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500738 num = (signed short) num;
739 } else {
wdenk153d5112002-08-30 11:07:04 +0000740 num = va_arg(args, unsigned int);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500741 if (flags & SIGN)
742 num = (signed int) num;
743 }
Sonny Raocd55bde2011-11-02 09:52:08 +0000744 str = number(str, end, num, base, field_width, precision,
745 flags);
746 }
747
748#ifdef CONFIG_SYS_VSNPRINTF
749 if (size > 0) {
750 ADDCH(str, '\0');
751 if (str > end)
752 end[-1] = '\0';
wdenk153d5112002-08-30 11:07:04 +0000753 }
Sonny Raocd55bde2011-11-02 09:52:08 +0000754#else
wdenk153d5112002-08-30 11:07:04 +0000755 *str = '\0';
Sonny Raocd55bde2011-11-02 09:52:08 +0000756#endif
757 /* the trailing null byte doesn't count towards the total */
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000758 return str - buf;
wdenk153d5112002-08-30 11:07:04 +0000759}
760
Sonny Raocd55bde2011-11-02 09:52:08 +0000761#ifdef CONFIG_SYS_VSNPRINTF
762int vsnprintf(char *buf, size_t size, const char *fmt,
763 va_list args)
764{
765 return vsnprintf_internal(buf, size, fmt, args);
766}
767
Sonny Raocd55bde2011-11-02 09:52:08 +0000768int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
769{
770 int i;
771
772 i = vsnprintf(buf, size, fmt, args);
773
774 if (likely(i < size))
775 return i;
776 if (size != 0)
777 return size - 1;
778 return 0;
779}
780
Sonny Raocd55bde2011-11-02 09:52:08 +0000781int snprintf(char *buf, size_t size, const char *fmt, ...)
782{
783 va_list args;
784 int i;
785
786 va_start(args, fmt);
787 i = vsnprintf(buf, size, fmt, args);
788 va_end(args);
789
790 return i;
791}
792
Sonny Raocd55bde2011-11-02 09:52:08 +0000793int scnprintf(char *buf, size_t size, const char *fmt, ...)
794{
795 va_list args;
796 int i;
797
798 va_start(args, fmt);
799 i = vscnprintf(buf, size, fmt, args);
800 va_end(args);
801
802 return i;
803}
804#endif /* CONFIG_SYS_VSNPRINT */
805
806/**
807 * Format a string and place it in a buffer (va_list version)
808 *
809 * @param buf The buffer to place the result into
810 * @param fmt The format string to use
811 * @param args Arguments for the format string
812 *
813 * The function returns the number of characters written
814 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
815 * buffer overflows.
816 *
817 * If you're not already dealing with a va_list consider using sprintf().
818 */
819int vsprintf(char *buf, const char *fmt, va_list args)
820{
821 return vsnprintf_internal(buf, INT_MAX, fmt, args);
822}
823
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000824int sprintf(char *buf, const char *fmt, ...)
wdenk153d5112002-08-30 11:07:04 +0000825{
826 va_list args;
827 int i;
828
829 va_start(args, fmt);
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000830 i = vsprintf(buf, fmt, args);
wdenk153d5112002-08-30 11:07:04 +0000831 va_end(args);
832 return i;
833}
834
835void panic(const char *fmt, ...)
836{
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000837 va_list args;
wdenk153d5112002-08-30 11:07:04 +0000838 va_start(args, fmt);
wdenkb00ec162003-06-19 23:40:20 +0000839 vprintf(fmt, args);
wdenk153d5112002-08-30 11:07:04 +0000840 putc('\n');
841 va_end(args);
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000842#if defined(CONFIG_PANIC_HANG)
wdenk153d5112002-08-30 11:07:04 +0000843 hang();
844#else
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000845 udelay(100000); /* allow messages to go out */
846 do_reset(NULL, 0, 0, NULL);
wdenk153d5112002-08-30 11:07:04 +0000847#endif
Heiko Schocher8cce81f2011-07-26 20:55:57 +0000848 while (1)
849 ;
wdenk153d5112002-08-30 11:07:04 +0000850}
Simon Glass02aa9e62011-06-29 09:49:34 +0000851
852void __assert_fail(const char *assertion, const char *file, unsigned line,
853 const char *function)
854{
855 /* This will not return */
856 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
857 assertion);
858}
Simon Glass931948a2011-10-21 18:51:33 +0000859
860char *simple_itoa(ulong i)
861{
862 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
863 static char local[22];
864 char *p = &local[21];
865
866 *p-- = '\0';
867 do {
868 *p-- = '0' + i % 10;
869 i /= 10;
870 } while (i > 0);
871 return p + 1;
872}
Simon Glassb09cfd82013-06-11 11:14:38 -0700873
874/* We don't seem to have %'d in U-Boot */
875void print_grouped_ull(unsigned long long int_val, int digits)
876{
877 char str[21], *s;
878 int grab = 3;
879
880 digits = (digits + 2) / 3;
881 sprintf(str, "%*llu", digits * 3, int_val);
882 for (s = str; *s; s += grab) {
883 if (s != str)
884 putc(s[-1] != ' ' ? ',' : ' ');
885 printf("%.*s", grab, s);
886 grab = 3;
887 }
888}