4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
11 * from hush: simple_itoa() was lifted from boa-0.93.15
15 #include <linux/types.h>
16 #include <linux/string.h>
17 #include <linux/ctype.h>
21 #if !defined(CONFIG_PANIC_HANG)
26 #define noinline __attribute__((noinline))
28 unsigned long simple_strtoul(const char *cp, char **endp,
31 unsigned long result = 0;
36 if ((*cp == 'x') && isxdigit(cp[1])) {
48 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
49 ? toupper(*cp) : *cp)-'A'+10) < base) {
50 result = result*base + value;
60 int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
71 val = simple_strtoul(cp, &tail, base);
75 if ((*tail == '\0') ||
76 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
84 long simple_strtol(const char *cp, char **endp, unsigned int base)
87 return -simple_strtoul(cp + 1, endp, base);
89 return simple_strtoul(cp, endp, base);
92 unsigned long ustrtoul(const char *cp, char **endp, unsigned int base)
94 unsigned long result = simple_strtoul(cp, endp, base);
105 if ((*endp)[1] == 'i') {
106 if ((*endp)[2] == 'B')
115 unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base)
117 unsigned long long result = simple_strtoull(cp, endp, base);
128 if ((*endp)[1] == 'i') {
129 if ((*endp)[2] == 'B')
138 unsigned long long simple_strtoull(const char *cp, char **endp,
141 unsigned long long result = 0, value;
145 if ((*cp == 'x') && isxdigit(cp[1])) {
157 while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0'
158 : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) {
159 result = result * base + value;
169 /* we use this so that we can do without the ctype library */
170 #define is_digit(c) ((c) >= '0' && (c) <= '9')
172 static int skip_atoi(const char **s)
176 while (is_digit(**s))
177 i = i * 10 + *((*s)++) - '0';
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
186 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
187 * (with permission from the author, Douglas W. Jones). */
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. */
192 static char *put_dec_trunc(char *buf, unsigned q)
194 unsigned d3, d2, d1, d0;
199 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
200 q = (d0 * 0xcd) >> 11;
202 *buf++ = d0 + '0'; /* least significant digit */
203 d1 = q + 9*d3 + 5*d2 + d1;
205 q = (d1 * 0xcd) >> 11;
207 *buf++ = d1 + '0'; /* next digit */
210 if ((d2 != 0) || (d3 != 0)) {
213 *buf++ = d2 + '0'; /* next digit */
217 q = (d3 * 0xcd) >> 11;
219 *buf++ = d3 + '0'; /* next digit */
221 *buf++ = q + '0'; /* most sign. digit */
227 /* Same with if's removed. Always emits five digits */
228 static char *put_dec_full(char *buf, unsigned q)
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;
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)
247 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
248 q = (d0 * 0xcd) >> 11;
251 d1 = q + 9*d3 + 5*d2 + d1;
252 q = (d1 * 0xcd) >> 11;
262 q = (d3 * 0xcd) >> 11; /* - shorter code */
263 /* q = (d3 * 0x67) >> 10; - would also work */
269 /* No inlining helps gcc to use registers better */
270 static noinline char *put_dec(char *buf, uint64_t num)
275 return put_dec_trunc(buf, num);
276 rem = do_div(num, 100000);
277 buf = put_dec_full(buf, rem);
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 */
286 #define SMALL 32 /* Must be 32 == 0x20 */
287 #define SPECIAL 64 /* 0x */
289 #ifdef CONFIG_SYS_VSNPRINTF
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.
294 #define ADDCH(str, ch) do { \
300 #define ADDCH(str, ch) (*(str)++ = (ch))
303 static char *number(char *buf, char *end, u64 num,
304 int base, int size, int precision, int type)
306 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
307 static const char digits[16] = "0123456789ABCDEF";
312 int need_pfx = ((type & SPECIAL) && base != 10);
315 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
316 * produces same digits or (maybe lowercased) letters */
317 locase = (type & SMALL);
326 } else if (type & PLUS) {
329 } else if (type & SPACE) {
340 /* generate full string in tmp[], in reverse order */
344 /* Generic code, for any base:
346 tmp[i++] = (digits[do_div(num,base)] | locase);
349 else if (base != 10) { /* 8 or 16 */
357 tmp[i++] = (digits[((unsigned char)num) & mask]
361 } else { /* base 10 */
362 i = put_dec(tmp, num) - tmp;
365 /* printing 100 using %2d gives "100", not "00" */
368 /* leading space padding */
370 if (!(type & (ZEROPAD + LEFT))) {
377 /* "0x" / "0" prefix */
381 ADDCH(buf, 'X' | locase);
383 /* zero or space padding */
384 if (!(type & LEFT)) {
385 char c = (type & ZEROPAD) ? '0' : ' ';
390 /* hmm even more zero padding? */
391 while (i <= --precision)
393 /* actual digits of result */
396 /* trailing space padding */
402 static char *string(char *buf, char *end, char *s, int field_width,
403 int precision, int flags)
410 len = strnlen(s, precision);
413 while (len < field_width--)
415 for (i = 0; i < len; ++i)
417 while (len < field_width--)
422 #ifdef CONFIG_CMD_NET
423 static 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]
427 static inline char *pack_hex_byte(char *buf, u8 byte)
429 *buf++ = hex_asc_hi(byte);
430 *buf++ = hex_asc_lo(byte);
434 static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
435 int precision, int flags)
437 /* (6 * 2 hex digits), 5 colons and trailing zero */
438 char mac_addr[6 * 3];
442 for (i = 0; i < 6; i++) {
443 p = pack_hex_byte(p, addr[i]);
444 if (!(flags & SPECIAL) && i != 5)
449 return string(buf, end, mac_addr, field_width, precision,
453 static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
454 int precision, int flags)
456 /* (8 * 4 hex digits), 7 colons and trailing zero */
457 char ip6_addr[8 * 5];
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)
469 return string(buf, end, ip6_addr, field_width, precision,
473 static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
474 int precision, int flags)
476 /* (4 * 3 decimal digits), 3 dots and trailing zero */
477 char ip4_addr[4 * 4];
478 char temp[3]; /* hold each IP quad in reverse order */
482 for (i = 0; i < 4; i++) {
483 digits = put_dec_trunc(temp, addr[i]) - temp;
484 /* reverse the digits in the quad */
492 return string(buf, end, ip4_addr, field_width, precision,
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
502 * Right now we handle:
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
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.
515 static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
516 int field_width, int precision, int flags)
518 u64 num = (uintptr_t)ptr;
521 * Being a boot loader, we explicitly allow pointers to
522 * (physical) address null.
526 return string(buf, end, "(null)", field_width, precision,
530 #ifdef CONFIG_CMD_NET
533 flags |= SPECIAL | ZEROPAD;
538 field_width = sizeof(phys_addr_t) * 2 + 2;
539 num = *(phys_addr_t *)ptr;
547 return mac_address_string(buf, end, ptr, field_width,
554 return ip6_addr_string(buf, end, ptr, field_width,
557 return ip4_addr_string(buf, end, ptr, field_width,
564 if (field_width == -1) {
565 field_width = 2*sizeof(void *);
568 return number(buf, end, num, 16, field_width, precision, flags);
571 static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
578 int flags; /* flags to number() */
580 int field_width; /* width of output field */
581 int precision; /* min. # of digits for integers; max
582 number of chars for from string */
583 int qualifier; /* 'h', 'l', or 'L' for integer fields */
584 /* 'z' support added 23/7/1999 S.H. */
585 /* 'z' changed to 'Z' --davidm 1/25/99 */
586 /* 't' added for ptrdiff_t */
587 char *end = buf + size;
589 #ifdef CONFIG_SYS_VSNPRINTF
590 /* Make sure end is always >= buf - do we want this in U-Boot? */
598 for (; *fmt ; ++fmt) {
607 ++fmt; /* this also skips first '%' */
626 /* get field width */
629 field_width = skip_atoi(&fmt);
630 else if (*fmt == '*') {
632 /* it's the next argument */
633 field_width = va_arg(args, int);
634 if (field_width < 0) {
635 field_width = -field_width;
640 /* get the precision */
645 precision = skip_atoi(&fmt);
646 else if (*fmt == '*') {
648 /* it's the next argument */
649 precision = va_arg(args, int);
655 /* get the conversion qualifier */
657 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
658 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
661 if (qualifier == 'l' && *fmt == 'l') {
672 if (!(flags & LEFT)) {
673 while (--field_width > 0)
676 ADDCH(str, (unsigned char) va_arg(args, int));
677 while (--field_width > 0)
682 str = string(str, end, va_arg(args, char *),
683 field_width, precision, flags);
687 str = pointer(fmt + 1, str, end,
688 va_arg(args, void *),
689 field_width, precision, flags);
690 /* Skip all alphanumeric pointer suffixes */
691 while (isalnum(fmt[1]))
696 if (qualifier == 'l') {
697 long *ip = va_arg(args, long *);
700 int *ip = va_arg(args, int *);
709 /* integer number formats - set up the flags and "break" */
734 if (qualifier == 'L') /* "quad" for 64 bit variables */
735 num = va_arg(args, unsigned long long);
736 else if (qualifier == 'l') {
737 num = va_arg(args, unsigned long);
739 num = (signed long) num;
740 } else if (qualifier == 'Z' || qualifier == 'z') {
741 num = va_arg(args, size_t);
742 } else if (qualifier == 't') {
743 num = va_arg(args, ptrdiff_t);
744 } else if (qualifier == 'h') {
745 num = (unsigned short) va_arg(args, int);
747 num = (signed short) num;
749 num = va_arg(args, unsigned int);
751 num = (signed int) num;
753 str = number(str, end, num, base, field_width, precision,
757 #ifdef CONFIG_SYS_VSNPRINTF
767 /* the trailing null byte doesn't count towards the total */
771 #ifdef CONFIG_SYS_VSNPRINTF
772 int vsnprintf(char *buf, size_t size, const char *fmt,
775 return vsnprintf_internal(buf, size, fmt, args);
778 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
782 i = vsnprintf(buf, size, fmt, args);
784 if (likely(i < size))
791 int snprintf(char *buf, size_t size, const char *fmt, ...)
797 i = vsnprintf(buf, size, fmt, args);
803 int scnprintf(char *buf, size_t size, const char *fmt, ...)
809 i = vscnprintf(buf, size, fmt, args);
814 #endif /* CONFIG_SYS_VSNPRINT */
817 * Format a string and place it in a buffer (va_list version)
819 * @param buf The buffer to place the result into
820 * @param fmt The format string to use
821 * @param args Arguments for the format string
823 * The function returns the number of characters written
824 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
827 * If you're not already dealing with a va_list consider using sprintf().
829 int vsprintf(char *buf, const char *fmt, va_list args)
831 return vsnprintf_internal(buf, INT_MAX, fmt, args);
834 int sprintf(char *buf, const char *fmt, ...)
840 i = vsprintf(buf, fmt, args);
845 void panic(const char *fmt, ...)
852 #if defined(CONFIG_PANIC_HANG)
855 udelay(100000); /* allow messages to go out */
856 do_reset(NULL, 0, 0, NULL);
862 void __assert_fail(const char *assertion, const char *file, unsigned line,
863 const char *function)
865 /* This will not return */
866 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
870 char *simple_itoa(ulong i)
872 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
873 static char local[22];
874 char *p = &local[21];
884 /* We don't seem to have %'d in U-Boot */
885 void print_grouped_ull(unsigned long long int_val, int digits)
890 digits = (digits + 2) / 3;
891 sprintf(str, "%*llu", digits * 3, int_val);
892 for (s = str; *s; s += grab) {
894 putc(s[-1] != ' ' ? ',' : ' ');
895 printf("%.*s", grab, s);