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