]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/vsprintf.c
include: move various macros to include/linux/kernel.h
[people/ms/u-boot.git] / lib / vsprintf.c
CommitLineData
153d511e
WD
1/*
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 :-)
3cce8a54
SG
10 *
11 * from hush: simple_itoa() was lifted from boa-0.93.15
153d511e
WD
12 */
13
14#include <stdarg.h>
15#include <linux/types.h>
16#include <linux/string.h>
17#include <linux/ctype.h>
a7fd0d9f 18#include <errno.h>
153d511e
WD
19
20#include <common.h>
8acdae68 21#if !defined(CONFIG_PANIC_HANG)
153d511e 22#include <command.h>
153d511e
WD
23#endif
24
47910506 25#include <div64.h>
47910506
DB
26#define noinline __attribute__((noinline))
27
8acdae68
DS
28unsigned long simple_strtoul(const char *cp, char **endp,
29 unsigned int base)
153d511e 30{
8acdae68
DS
31 unsigned long result = 0;
32 unsigned long value;
153d511e
WD
33
34 if (*cp == '0') {
35 cp++;
36 if ((*cp == 'x') && isxdigit(cp[1])) {
37 base = 16;
38 cp++;
39 }
8acdae68
DS
40
41 if (!base)
153d511e 42 base = 8;
153d511e 43 }
8acdae68
DS
44
45 if (!base)
153d511e 46 base = 10;
8acdae68 47
153d511e
WD
48 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
49 ? toupper(*cp) : *cp)-'A'+10) < base) {
50 result = result*base + value;
51 cp++;
52 }
8acdae68 53
153d511e
WD
54 if (endp)
55 *endp = (char *)cp;
8acdae68 56
153d511e
WD
57 return result;
58}
59
a7fd0d9f
HS
60int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
61{
62 char *tail;
63 unsigned long val;
64 size_t len;
65
66 *res = 0;
67 len = strlen(cp);
68 if (len == 0)
69 return -EINVAL;
70
71 val = simple_strtoul(cp, &tail, base);
72 if (tail == cp)
73 return -EINVAL;
74
75 if ((*tail == '\0') ||
76 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
77 *res = val;
78 return 0;
79 }
80
81 return -EINVAL;
82}
83
8acdae68 84long simple_strtol(const char *cp, char **endp, unsigned int base)
153d511e 85{
8acdae68
DS
86 if (*cp == '-')
87 return -simple_strtoul(cp + 1, endp, base);
88
89 return simple_strtoul(cp, endp, base);
153d511e
WD
90}
91
3ec44111 92unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
7e6ee7ad
KP
93{
94 unsigned long result = simple_strtoul(cp, endp, base);
95 switch (**endp) {
8acdae68 96 case 'G':
7e6ee7ad
KP
97 result *= 1024;
98 /* fall through */
99 case 'M':
100 result *= 1024;
101 /* fall through */
102 case 'K':
103 case 'k':
104 result *= 1024;
105 if ((*endp)[1] == 'i') {
106 if ((*endp)[2] == 'B')
107 (*endp) += 3;
108 else
109 (*endp) += 2;
110 }
111 }
112 return result;
113}
114
7df54d31
PW
115unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
116{
117 unsigned long long result = simple_strtoull(cp, endp, base);
118 switch (**endp) {
119 case 'G':
120 result *= 1024;
121 /* fall through */
122 case 'M':
123 result *= 1024;
124 /* fall through */
125 case 'K':
126 case 'k':
127 result *= 1024;
128 if ((*endp)[1] == 'i') {
129 if ((*endp)[2] == 'B')
130 (*endp) += 3;
131 else
132 (*endp) += 2;
133 }
134 }
135 return result;
136}
137
8acdae68
DS
138unsigned long long simple_strtoull(const char *cp, char **endp,
139 unsigned int base)
c40b2956
WD
140{
141 unsigned long long result = 0, value;
142
143 if (*cp == '0') {
144 cp++;
8acdae68 145 if ((*cp == 'x') && isxdigit(cp[1])) {
c40b2956
WD
146 base = 16;
147 cp++;
148 }
8acdae68
DS
149
150 if (!base)
c40b2956 151 base = 8;
c40b2956 152 }
8acdae68
DS
153
154 if (!base)
c40b2956 155 base = 10;
8acdae68
DS
156
157 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0'
158 : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {
c40b2956
WD
159 result = result * base + value;
160 cp++;
161 }
8acdae68 162
c40b2956
WD
163 if (endp)
164 *endp = (char *) cp;
8acdae68 165
c40b2956
WD
166 return result;
167}
c40b2956 168
153d511e
WD
169/* we use this so that we can do without the ctype library */
170#define is_digit(c) ((c) >= '0' && (c) <= '9')
171
172static int skip_atoi(const char **s)
173{
8acdae68 174 int i = 0;
153d511e
WD
175
176 while (is_digit(**s))
8acdae68
DS
177 i = i * 10 + *((*s)++) - '0';
178
153d511e
WD
179 return i;
180}
181
6c6166f5
MF
182/* Decimal conversion is by far the most typical, and is used
183 * for /proc and /sys data. This directly impacts e.g. top performance
184 * with many processes running. We optimize it for speed
185 * using code from
186 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
187 * (with permission from the author, Douglas W. Jones). */
188
189/* Formats correctly any integer in [0,99999].
190 * Outputs from one to five digits depending on input.
191 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
8acdae68 192static char *put_dec_trunc(char *buf, unsigned q)
6c6166f5
MF
193{
194 unsigned d3, d2, d1, d0;
195 d1 = (q>>4) & 0xf;
196 d2 = (q>>8) & 0xf;
197 d3 = (q>>12);
198
199 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
200 q = (d0 * 0xcd) >> 11;
201 d0 = d0 - 10*q;
202 *buf++ = d0 + '0'; /* least significant digit */
203 d1 = q + 9*d3 + 5*d2 + d1;
204 if (d1 != 0) {
205 q = (d1 * 0xcd) >> 11;
206 d1 = d1 - 10*q;
207 *buf++ = d1 + '0'; /* next digit */
208
209 d2 = q + 2*d2;
210 if ((d2 != 0) || (d3 != 0)) {
211 q = (d2 * 0xd) >> 7;
212 d2 = d2 - 10*q;
213 *buf++ = d2 + '0'; /* next digit */
214
215 d3 = q + 4*d3;
216 if (d3 != 0) {
217 q = (d3 * 0xcd) >> 11;
218 d3 = d3 - 10*q;
219 *buf++ = d3 + '0'; /* next digit */
220 if (q != 0)
8acdae68 221 *buf++ = q + '0'; /* most sign. digit */
6c6166f5
MF
222 }
223 }
224 }
225 return buf;
226}
227/* Same with if's removed. Always emits five digits */
8acdae68 228static char *put_dec_full(char *buf, unsigned q)
6c6166f5
MF
229{
230 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
231 /* but anyway, gcc produces better code with full-sized ints */
232 unsigned d3, d2, d1, d0;
233 d1 = (q>>4) & 0xf;
234 d2 = (q>>8) & 0xf;
235 d3 = (q>>12);
236
c0a14aed
WD
237 /*
238 * Possible ways to approx. divide by 10
239 * gcc -O2 replaces multiply with shifts and adds
240 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
241 * (x * 0x67) >> 10: 1100111
242 * (x * 0x34) >> 9: 110100 - same
243 * (x * 0x1a) >> 8: 11010 - same
244 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
245 */
6c6166f5
MF
246
247 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
248 q = (d0 * 0xcd) >> 11;
249 d0 = d0 - 10*q;
250 *buf++ = d0 + '0';
251 d1 = q + 9*d3 + 5*d2 + d1;
252 q = (d1 * 0xcd) >> 11;
253 d1 = d1 - 10*q;
254 *buf++ = d1 + '0';
255
256 d2 = q + 2*d2;
257 q = (d2 * 0xd) >> 7;
258 d2 = d2 - 10*q;
259 *buf++ = d2 + '0';
260
261 d3 = q + 4*d3;
262 q = (d3 * 0xcd) >> 11; /* - shorter code */
263 /* q = (d3 * 0x67) >> 10; - would also work */
264 d3 = d3 - 10*q;
265 *buf++ = d3 + '0';
266 *buf++ = q + '0';
267 return buf;
268}
269/* No inlining helps gcc to use registers better */
6bf67259 270static noinline char *put_dec(char *buf, uint64_t num)
6c6166f5
MF
271{
272 while (1) {
273 unsigned rem;
274 if (num < 100000)
275 return put_dec_trunc(buf, num);
276 rem = do_div(num, 100000);
277 buf = put_dec_full(buf, rem);
278 }
279}
280
153d511e
WD
281#define ZEROPAD 1 /* pad with zero */
282#define SIGN 2 /* unsigned/signed long */
283#define PLUS 4 /* show plus */
284#define SPACE 8 /* space if plus */
285#define LEFT 16 /* left justified */
6c6166f5
MF
286#define SMALL 32 /* Must be 32 == 0x20 */
287#define SPECIAL 64 /* 0x */
153d511e 288
046a37bd
SR
289#ifdef CONFIG_SYS_VSNPRINTF
290/*
291 * Macro to add a new character to our output string, but only if it will
292 * fit. The macro moves to the next character position in the output string.
293 */
294#define ADDCH(str, ch) do { \
295 if ((str) < end) \
296 *(str) = (ch); \
297 ++str; \
298 } while (0)
299#else
300#define ADDCH(str, ch) (*(str)++ = (ch))
301#endif
302
7b64f66c 303static char *number(char *buf, char *end, u64 num,
046a37bd 304 int base, int size, int precision, int type)
153d511e 305{
6c6166f5 306 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
8acdae68 307 static const char digits[16] = "0123456789ABCDEF";
6c6166f5
MF
308
309 char tmp[66];
310 char sign;
311 char locase;
312 int need_pfx = ((type & SPECIAL) && base != 10);
153d511e
WD
313 int i;
314
6c6166f5
MF
315 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
316 * produces same digits or (maybe lowercased) letters */
317 locase = (type & SMALL);
153d511e
WD
318 if (type & LEFT)
319 type &= ~ZEROPAD;
153d511e
WD
320 sign = 0;
321 if (type & SIGN) {
7b64f66c 322 if ((s64) num < 0) {
153d511e 323 sign = '-';
7b64f66c 324 num = -(s64) num;
153d511e
WD
325 size--;
326 } else if (type & PLUS) {
327 sign = '+';
328 size--;
329 } else if (type & SPACE) {
330 sign = ' ';
331 size--;
332 }
333 }
6c6166f5
MF
334 if (need_pfx) {
335 size--;
153d511e 336 if (base == 16)
153d511e
WD
337 size--;
338 }
6c6166f5
MF
339
340 /* generate full string in tmp[], in reverse order */
153d511e
WD
341 i = 0;
342 if (num == 0)
6c6166f5
MF
343 tmp[i++] = '0';
344 /* Generic code, for any base:
345 else do {
346 tmp[i++] = (digits[do_div(num,base)] | locase);
347 } while (num != 0);
348 */
349 else if (base != 10) { /* 8 or 16 */
350 int mask = base - 1;
351 int shift = 3;
8acdae68
DS
352
353 if (base == 16)
354 shift = 4;
355
6c6166f5 356 do {
8acdae68
DS
357 tmp[i++] = (digits[((unsigned char)num) & mask]
358 | locase);
6c6166f5
MF
359 num >>= shift;
360 } while (num);
361 } else { /* base 10 */
362 i = put_dec(tmp, num) - tmp;
363 }
364
365 /* printing 100 using %2d gives "100", not "00" */
153d511e
WD
366 if (i > precision)
367 precision = i;
6c6166f5 368 /* leading space padding */
153d511e 369 size -= precision;
046a37bd
SR
370 if (!(type & (ZEROPAD + LEFT))) {
371 while (--size >= 0)
372 ADDCH(buf, ' ');
373 }
6c6166f5 374 /* sign */
153d511e 375 if (sign)
046a37bd 376 ADDCH(buf, sign);
6c6166f5
MF
377 /* "0x" / "0" prefix */
378 if (need_pfx) {
046a37bd 379 ADDCH(buf, '0');
6c6166f5 380 if (base == 16)
046a37bd 381 ADDCH(buf, 'X' | locase);
6c6166f5
MF
382 }
383 /* zero or space padding */
384 if (!(type & LEFT)) {
385 char c = (type & ZEROPAD) ? '0' : ' ';
046a37bd 386
6c6166f5 387 while (--size >= 0)
046a37bd 388 ADDCH(buf, c);
153d511e 389 }
6c6166f5
MF
390 /* hmm even more zero padding? */
391 while (i <= --precision)
046a37bd 392 ADDCH(buf, '0');
6c6166f5
MF
393 /* actual digits of result */
394 while (--i >= 0)
046a37bd 395 ADDCH(buf, tmp[i]);
6c6166f5
MF
396 /* trailing space padding */
397 while (--size >= 0)
046a37bd 398 ADDCH(buf, ' ');
6c6166f5 399 return buf;
153d511e
WD
400}
401
046a37bd
SR
402static char *string(char *buf, char *end, char *s, int field_width,
403 int precision, int flags)
6c6166f5
MF
404{
405 int len, i;
153d511e 406
0eb25768 407 if (s == NULL)
6c6166f5
MF
408 s = "<NULL>";
409
410 len = strnlen(s, precision);
411
412 if (!(flags & LEFT))
413 while (len < field_width--)
046a37bd 414 ADDCH(buf, ' ');
6c6166f5 415 for (i = 0; i < len; ++i)
046a37bd 416 ADDCH(buf, *s++);
6c6166f5 417 while (len < field_width--)
046a37bd 418 ADDCH(buf, ' ');
6c6166f5
MF
419 return buf;
420}
421
422#ifdef CONFIG_CMD_NET
d7b2d9df
JH
423static const char hex_asc[] = "0123456789abcdef";
424#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
425#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
426
427static inline char *pack_hex_byte(char *buf, u8 byte)
428{
429 *buf++ = hex_asc_hi(byte);
430 *buf++ = hex_asc_lo(byte);
431 return buf;
432}
433
046a37bd 434static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
6c6166f5 435 int precision, int flags)
153d511e 436{
8acdae68
DS
437 /* (6 * 2 hex digits), 5 colons and trailing zero */
438 char mac_addr[6 * 3];
6c6166f5
MF
439 char *p = mac_addr;
440 int i;
441
442 for (i = 0; i < 6; i++) {
443 p = pack_hex_byte(p, addr[i]);
444 if (!(flags & SPECIAL) && i != 5)
445 *p++ = ':';
446 }
447 *p = '\0';
448
046a37bd
SR
449 return string(buf, end, mac_addr, field_width, precision,
450 flags & ~SPECIAL);
6c6166f5
MF
451}
452
046a37bd 453static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
6c6166f5
MF
454 int precision, int flags)
455{
8acdae68
DS
456 /* (8 * 4 hex digits), 7 colons and trailing zero */
457 char ip6_addr[8 * 5];
6c6166f5
MF
458 char *p = ip6_addr;
459 int i;
460
461 for (i = 0; i < 8; i++) {
462 p = pack_hex_byte(p, addr[2 * i]);
463 p = pack_hex_byte(p, addr[2 * i + 1]);
464 if (!(flags & SPECIAL) && i != 7)
465 *p++ = ':';
466 }
467 *p = '\0';
468
046a37bd
SR
469 return string(buf, end, ip6_addr, field_width, precision,
470 flags & ~SPECIAL);
6c6166f5
MF
471}
472
046a37bd 473static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
6c6166f5
MF
474 int precision, int flags)
475{
8acdae68
DS
476 /* (4 * 3 decimal digits), 3 dots and trailing zero */
477 char ip4_addr[4 * 4];
6c6166f5
MF
478 char temp[3]; /* hold each IP quad in reverse order */
479 char *p = ip4_addr;
480 int i, digits;
481
482 for (i = 0; i < 4; i++) {
483 digits = put_dec_trunc(temp, addr[i]) - temp;
484 /* reverse the digits in the quad */
485 while (digits--)
486 *p++ = temp[digits];
487 if (i != 3)
488 *p++ = '.';
489 }
490 *p = '\0';
491
046a37bd
SR
492 return string(buf, end, ip4_addr, field_width, precision,
493 flags & ~SPECIAL);
6c6166f5 494}
c40b2956 495#endif
6c6166f5
MF
496
497/*
498 * Show a '%p' thing. A kernel extension is that the '%p' is followed
499 * by an extra set of alphanumeric characters that are extended format
500 * specifiers.
501 *
502 * Right now we handle:
503 *
504 * - 'M' For a 6-byte MAC address, it prints the address in the
505 * usual colon-separated hex notation
506 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
507 * decimal for v4 and colon separated network-order 16 bit hex for v6)
508 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
509 * currently the same
510 *
511 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
512 * function pointers are really function descriptors, which contain a
513 * pointer to the real address.
514 */
046a37bd
SR
515static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
516 int field_width, int precision, int flags)
6c6166f5 517{
d266f669
WD
518 /*
519 * Being a boot loader, we explicitly allow pointers to
520 * (physical) address null.
521 */
522#if 0
6c6166f5 523 if (!ptr)
046a37bd
SR
524 return string(buf, end, "(null)", field_width, precision,
525 flags);
d266f669 526#endif
6c6166f5
MF
527
528#ifdef CONFIG_CMD_NET
529 switch (*fmt) {
530 case 'm':
531 flags |= SPECIAL;
532 /* Fallthrough */
533 case 'M':
046a37bd
SR
534 return mac_address_string(buf, end, ptr, field_width,
535 precision, flags);
6c6166f5
MF
536 case 'i':
537 flags |= SPECIAL;
538 /* Fallthrough */
539 case 'I':
540 if (fmt[1] == '6')
046a37bd
SR
541 return ip6_addr_string(buf, end, ptr, field_width,
542 precision, flags);
6c6166f5 543 if (fmt[1] == '4')
046a37bd
SR
544 return ip4_addr_string(buf, end, ptr, field_width,
545 precision, flags);
6c6166f5
MF
546 flags &= ~SPECIAL;
547 break;
548 }
549#endif
550 flags |= SMALL;
551 if (field_width == -1) {
552 field_width = 2*sizeof(void *);
553 flags |= ZEROPAD;
554 }
046a37bd
SR
555 return number(buf, end, (unsigned long)ptr, 16, field_width,
556 precision, flags);
6c6166f5
MF
557}
558
046a37bd
SR
559static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
560 va_list args)
6c6166f5 561{
7b64f66c 562 u64 num;
6c6166f5
MF
563 int base;
564 char *str;
153d511e
WD
565
566 int flags; /* flags to number() */
567
568 int field_width; /* width of output field */
569 int precision; /* min. # of digits for integers; max
570 number of chars for from string */
6c6166f5
MF
571 int qualifier; /* 'h', 'l', or 'L' for integer fields */
572 /* 'z' support added 23/7/1999 S.H. */
573 /* 'z' changed to 'Z' --davidm 1/25/99 */
574 /* 't' added for ptrdiff_t */
046a37bd 575 char *end = buf + size;
6c6166f5 576
046a37bd
SR
577#ifdef CONFIG_SYS_VSNPRINTF
578 /* Make sure end is always >= buf - do we want this in U-Boot? */
579 if (end < buf) {
580 end = ((void *)-1);
581 size = end - buf;
582 }
583#endif
6c6166f5 584 str = buf;
153d511e 585
6c6166f5 586 for (; *fmt ; ++fmt) {
153d511e 587 if (*fmt != '%') {
046a37bd 588 ADDCH(str, *fmt);
153d511e
WD
589 continue;
590 }
591
592 /* process flags */
593 flags = 0;
8acdae68 594repeat:
153d511e
WD
595 ++fmt; /* this also skips first '%' */
596 switch (*fmt) {
8acdae68
DS
597 case '-':
598 flags |= LEFT;
599 goto repeat;
600 case '+':
601 flags |= PLUS;
602 goto repeat;
603 case ' ':
604 flags |= SPACE;
605 goto repeat;
606 case '#':
607 flags |= SPECIAL;
608 goto repeat;
609 case '0':
610 flags |= ZEROPAD;
611 goto repeat;
6c6166f5 612 }
153d511e
WD
613
614 /* get field width */
615 field_width = -1;
616 if (is_digit(*fmt))
617 field_width = skip_atoi(&fmt);
618 else if (*fmt == '*') {
619 ++fmt;
620 /* it's the next argument */
621 field_width = va_arg(args, int);
622 if (field_width < 0) {
623 field_width = -field_width;
624 flags |= LEFT;
625 }
626 }
627
628 /* get the precision */
629 precision = -1;
630 if (*fmt == '.') {
631 ++fmt;
632 if (is_digit(*fmt))
633 precision = skip_atoi(&fmt);
634 else if (*fmt == '*') {
635 ++fmt;
636 /* it's the next argument */
637 precision = va_arg(args, int);
638 }
639 if (precision < 0)
640 precision = 0;
641 }
642
643 /* get the conversion qualifier */
644 qualifier = -1;
f354b73e 645 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
6c6166f5 646 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
153d511e 647 qualifier = *fmt;
6c6166f5
MF
648 ++fmt;
649 if (qualifier == 'l' && *fmt == 'l') {
650 qualifier = 'L';
bf052939
JY
651 ++fmt;
652 }
153d511e
WD
653 }
654
655 /* default base */
656 base = 10;
657
658 switch (*fmt) {
659 case 'c':
046a37bd 660 if (!(flags & LEFT)) {
153d511e 661 while (--field_width > 0)
046a37bd
SR
662 ADDCH(str, ' ');
663 }
664 ADDCH(str, (unsigned char) va_arg(args, int));
153d511e 665 while (--field_width > 0)
046a37bd 666 ADDCH(str, ' ');
153d511e
WD
667 continue;
668
669 case 's':
046a37bd
SR
670 str = string(str, end, va_arg(args, char *),
671 field_width, precision, flags);
153d511e
WD
672 continue;
673
674 case 'p':
8acdae68 675 str = pointer(fmt + 1, str, end,
6c6166f5
MF
676 va_arg(args, void *),
677 field_width, precision, flags);
678 /* Skip all alphanumeric pointer suffixes */
679 while (isalnum(fmt[1]))
680 fmt++;
153d511e
WD
681 continue;
682
153d511e
WD
683 case 'n':
684 if (qualifier == 'l') {
8acdae68 685 long *ip = va_arg(args, long *);
153d511e
WD
686 *ip = (str - buf);
687 } else {
8acdae68 688 int *ip = va_arg(args, int *);
153d511e
WD
689 *ip = (str - buf);
690 }
691 continue;
692
693 case '%':
046a37bd 694 ADDCH(str, '%');
153d511e
WD
695 continue;
696
697 /* integer number formats - set up the flags and "break" */
698 case 'o':
699 base = 8;
700 break;
701
153d511e 702 case 'x':
6c6166f5
MF
703 flags |= SMALL;
704 case 'X':
153d511e
WD
705 base = 16;
706 break;
707
708 case 'd':
709 case 'i':
710 flags |= SIGN;
711 case 'u':
712 break;
713
714 default:
046a37bd 715 ADDCH(str, '%');
153d511e 716 if (*fmt)
046a37bd 717 ADDCH(str, *fmt);
153d511e
WD
718 else
719 --fmt;
720 continue;
721 }
6c6166f5 722 if (qualifier == 'L') /* "quad" for 64 bit variables */
c40b2956 723 num = va_arg(args, unsigned long long);
4b142feb 724 else if (qualifier == 'l') {
153d511e 725 num = va_arg(args, unsigned long);
6c6166f5
MF
726 if (flags & SIGN)
727 num = (signed long) num;
f354b73e
JCPV
728 } else if (qualifier == 'Z' || qualifier == 'z') {
729 num = va_arg(args, size_t);
730 } else if (qualifier == 't') {
731 num = va_arg(args, ptrdiff_t);
732 } else if (qualifier == 'h') {
153d511e
WD
733 num = (unsigned short) va_arg(args, int);
734 if (flags & SIGN)
6c6166f5
MF
735 num = (signed short) num;
736 } else {
153d511e 737 num = va_arg(args, unsigned int);
6c6166f5
MF
738 if (flags & SIGN)
739 num = (signed int) num;
740 }
046a37bd
SR
741 str = number(str, end, num, base, field_width, precision,
742 flags);
743 }
744
745#ifdef CONFIG_SYS_VSNPRINTF
746 if (size > 0) {
747 ADDCH(str, '\0');
748 if (str > end)
749 end[-1] = '\0';
686f60f5 750 --str;
153d511e 751 }
046a37bd 752#else
153d511e 753 *str = '\0';
046a37bd
SR
754#endif
755 /* the trailing null byte doesn't count towards the total */
8acdae68 756 return str - buf;
153d511e
WD
757}
758
046a37bd
SR
759#ifdef CONFIG_SYS_VSNPRINTF
760int vsnprintf(char *buf, size_t size, const char *fmt,
761 va_list args)
762{
763 return vsnprintf_internal(buf, size, fmt, args);
764}
765
046a37bd
SR
766int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
767{
768 int i;
769
770 i = vsnprintf(buf, size, fmt, args);
771
772 if (likely(i < size))
773 return i;
774 if (size != 0)
775 return size - 1;
776 return 0;
777}
778
046a37bd
SR
779int snprintf(char *buf, size_t size, const char *fmt, ...)
780{
781 va_list args;
782 int i;
783
784 va_start(args, fmt);
785 i = vsnprintf(buf, size, fmt, args);
786 va_end(args);
787
788 return i;
789}
790
046a37bd
SR
791int scnprintf(char *buf, size_t size, const char *fmt, ...)
792{
793 va_list args;
794 int i;
795
796 va_start(args, fmt);
797 i = vscnprintf(buf, size, fmt, args);
798 va_end(args);
799
800 return i;
801}
802#endif /* CONFIG_SYS_VSNPRINT */
803
804/**
805 * Format a string and place it in a buffer (va_list version)
806 *
807 * @param buf The buffer to place the result into
808 * @param fmt The format string to use
809 * @param args Arguments for the format string
810 *
811 * The function returns the number of characters written
812 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
813 * buffer overflows.
814 *
815 * If you're not already dealing with a va_list consider using sprintf().
816 */
817int vsprintf(char *buf, const char *fmt, va_list args)
818{
819 return vsnprintf_internal(buf, INT_MAX, fmt, args);
820}
821
8acdae68 822int sprintf(char *buf, const char *fmt, ...)
153d511e
WD
823{
824 va_list args;
825 int i;
826
827 va_start(args, fmt);
8acdae68 828 i = vsprintf(buf, fmt, args);
153d511e
WD
829 va_end(args);
830 return i;
831}
832
833void panic(const char *fmt, ...)
834{
8acdae68 835 va_list args;
153d511e 836 va_start(args, fmt);
6dd652fa 837 vprintf(fmt, args);
153d511e
WD
838 putc('\n');
839 va_end(args);
8acdae68 840#if defined(CONFIG_PANIC_HANG)
153d511e
WD
841 hang();
842#else
8acdae68
DS
843 udelay(100000); /* allow messages to go out */
844 do_reset(NULL, 0, 0, NULL);
153d511e 845#endif
40e01881
HS
846 while (1)
847 ;
153d511e 848}
21726a7a
SG
849
850void __assert_fail(const char *assertion, const char *file, unsigned line,
851 const char *function)
852{
853 /* This will not return */
854 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
855 assertion);
856}
3cce8a54
SG
857
858char *simple_itoa(ulong i)
859{
860 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
861 static char local[22];
862 char *p = &local[21];
863
864 *p-- = '\0';
865 do {
866 *p-- = '0' + i % 10;
867 i /= 10;
868 } while (i > 0);
869 return p + 1;
870}
b8bcaa3a
SG
871
872/* We don't seem to have %'d in U-Boot */
873void print_grouped_ull(unsigned long long int_val, int digits)
874{
875 char str[21], *s;
876 int grab = 3;
877
878 digits = (digits + 2) / 3;
879 sprintf(str, "%*llu", digits * 3, int_val);
880 for (s = str; *s; s += grab) {
881 if (s != str)
882 putc(s[-1] != ' ' ? ',' : ' ');
883 printf("%.*s", grab, s);
884 grab = 3;
885 }
886}