]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - lib_generic/vsprintf.c
Merge branch 'master' of git://git.denx.de/u-boot-arm
[karo-tx-uboot.git] / lib_generic / vsprintf.c
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 :-)
10  */
11
12 #include <stdarg.h>
13 #include <linux/types.h>
14 #include <linux/string.h>
15 #include <linux/ctype.h>
16
17 #include <common.h>
18 #if !defined (CONFIG_PANIC_HANG)
19 #include <command.h>
20 /*cmd_boot.c*/
21 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
22 #endif
23
24 #ifdef CONFIG_SYS_64BIT_VSPRINTF
25 #include <div64.h>
26 # define NUM_TYPE long long
27 #else
28 # define NUM_TYPE long
29 #define do_div(n, base) ({ \
30         unsigned int __res; \
31         __res = ((unsigned NUM_TYPE) n) % base; \
32         n = ((unsigned NUM_TYPE) n) / base; \
33         __res; \
34 })
35 #endif
36 #define noinline __attribute__((noinline))
37
38
39 const char hex_asc[] = "0123456789abcdef";
40 #define hex_asc_lo(x)   hex_asc[((x) & 0x0f)]
41 #define hex_asc_hi(x)   hex_asc[((x) & 0xf0) >> 4]
42
43 static inline char *pack_hex_byte(char *buf, u8 byte)
44 {
45         *buf++ = hex_asc_hi(byte);
46         *buf++ = hex_asc_lo(byte);
47         return buf;
48 }
49
50 unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
51 {
52         unsigned long result = 0,value;
53
54         if (*cp == '0') {
55                 cp++;
56                 if ((*cp == 'x') && isxdigit(cp[1])) {
57                         base = 16;
58                         cp++;
59                 }
60                 if (!base) {
61                         base = 8;
62                 }
63         }
64         if (!base) {
65                 base = 10;
66         }
67         while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp)
68             ? toupper(*cp) : *cp)-'A'+10) < base) {
69                 result = result*base + value;
70                 cp++;
71         }
72         if (endp)
73                 *endp = (char *)cp;
74         return result;
75 }
76
77 long simple_strtol(const char *cp,char **endp,unsigned int base)
78 {
79         if(*cp=='-')
80                 return -simple_strtoul(cp+1,endp,base);
81         return simple_strtoul(cp,endp,base);
82 }
83
84 int ustrtoul(const char *cp, char **endp, unsigned int base)
85 {
86         unsigned long result = simple_strtoul(cp, endp, base);
87         switch (**endp) {
88         case 'G' :
89                 result *= 1024;
90                 /* fall through */
91         case 'M':
92                 result *= 1024;
93                 /* fall through */
94         case 'K':
95         case 'k':
96                 result *= 1024;
97                 if ((*endp)[1] == 'i') {
98                         if ((*endp)[2] == 'B')
99                                 (*endp) += 3;
100                         else
101                                 (*endp) += 2;
102                 }
103         }
104         return result;
105 }
106
107 #ifdef CONFIG_SYS_64BIT_STRTOUL
108 unsigned long long simple_strtoull (const char *cp, char **endp, unsigned int base)
109 {
110         unsigned long long result = 0, value;
111
112         if (*cp == '0') {
113                 cp++;
114                 if ((*cp == 'x') && isxdigit (cp[1])) {
115                         base = 16;
116                         cp++;
117                 }
118                 if (!base) {
119                         base = 8;
120                 }
121         }
122         if (!base) {
123                 base = 10;
124         }
125         while (isxdigit (*cp) && (value = isdigit (*cp)
126                                 ? *cp - '0'
127                                 : (islower (*cp) ? toupper (*cp) : *cp) - 'A' + 10) < base) {
128                 result = result * base + value;
129                 cp++;
130         }
131         if (endp)
132                 *endp = (char *) cp;
133         return result;
134 }
135 #endif /* CONFIG_SYS_64BIT_STRTOUL */
136
137 /* we use this so that we can do without the ctype library */
138 #define is_digit(c)     ((c) >= '0' && (c) <= '9')
139
140 static int skip_atoi(const char **s)
141 {
142         int i=0;
143
144         while (is_digit(**s))
145                 i = i*10 + *((*s)++) - '0';
146         return i;
147 }
148
149 /* Decimal conversion is by far the most typical, and is used
150  * for /proc and /sys data. This directly impacts e.g. top performance
151  * with many processes running. We optimize it for speed
152  * using code from
153  * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
154  * (with permission from the author, Douglas W. Jones). */
155
156 /* Formats correctly any integer in [0,99999].
157  * Outputs from one to five digits depending on input.
158  * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
159 static char* put_dec_trunc(char *buf, unsigned q)
160 {
161         unsigned d3, d2, d1, d0;
162         d1 = (q>>4) & 0xf;
163         d2 = (q>>8) & 0xf;
164         d3 = (q>>12);
165
166         d0 = 6*(d3 + d2 + d1) + (q & 0xf);
167         q = (d0 * 0xcd) >> 11;
168         d0 = d0 - 10*q;
169         *buf++ = d0 + '0'; /* least significant digit */
170         d1 = q + 9*d3 + 5*d2 + d1;
171         if (d1 != 0) {
172                 q = (d1 * 0xcd) >> 11;
173                 d1 = d1 - 10*q;
174                 *buf++ = d1 + '0'; /* next digit */
175
176                 d2 = q + 2*d2;
177                 if ((d2 != 0) || (d3 != 0)) {
178                         q = (d2 * 0xd) >> 7;
179                         d2 = d2 - 10*q;
180                         *buf++ = d2 + '0'; /* next digit */
181
182                         d3 = q + 4*d3;
183                         if (d3 != 0) {
184                                 q = (d3 * 0xcd) >> 11;
185                                 d3 = d3 - 10*q;
186                                 *buf++ = d3 + '0';  /* next digit */
187                                 if (q != 0)
188                                         *buf++ = q + '0';  /* most sign. digit */
189                         }
190                 }
191         }
192         return buf;
193 }
194 /* Same with if's removed. Always emits five digits */
195 static char* put_dec_full(char *buf, unsigned q)
196 {
197         /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
198         /* but anyway, gcc produces better code with full-sized ints */
199         unsigned d3, d2, d1, d0;
200         d1 = (q>>4) & 0xf;
201         d2 = (q>>8) & 0xf;
202         d3 = (q>>12);
203
204         /*
205          * Possible ways to approx. divide by 10
206          * gcc -O2 replaces multiply with shifts and adds
207          * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
208          * (x * 0x67) >> 10:  1100111
209          * (x * 0x34) >> 9:    110100 - same
210          * (x * 0x1a) >> 8:     11010 - same
211          * (x * 0x0d) >> 7:      1101 - same, shortest code (on i386)
212          */
213
214         d0 = 6*(d3 + d2 + d1) + (q & 0xf);
215         q = (d0 * 0xcd) >> 11;
216         d0 = d0 - 10*q;
217         *buf++ = d0 + '0';
218         d1 = q + 9*d3 + 5*d2 + d1;
219                 q = (d1 * 0xcd) >> 11;
220                 d1 = d1 - 10*q;
221                 *buf++ = d1 + '0';
222
223                 d2 = q + 2*d2;
224                         q = (d2 * 0xd) >> 7;
225                         d2 = d2 - 10*q;
226                         *buf++ = d2 + '0';
227
228                         d3 = q + 4*d3;
229                                 q = (d3 * 0xcd) >> 11; /* - shorter code */
230                                 /* q = (d3 * 0x67) >> 10; - would also work */
231                                 d3 = d3 - 10*q;
232                                 *buf++ = d3 + '0';
233                                         *buf++ = q + '0';
234         return buf;
235 }
236 /* No inlining helps gcc to use registers better */
237 static noinline char* put_dec(char *buf, unsigned NUM_TYPE num)
238 {
239         while (1) {
240                 unsigned rem;
241                 if (num < 100000)
242                         return put_dec_trunc(buf, num);
243                 rem = do_div(num, 100000);
244                 buf = put_dec_full(buf, rem);
245         }
246 }
247
248 #define ZEROPAD 1               /* pad with zero */
249 #define SIGN    2               /* unsigned/signed long */
250 #define PLUS    4               /* show plus */
251 #define SPACE   8               /* space if plus */
252 #define LEFT    16              /* left justified */
253 #define SMALL   32              /* Must be 32 == 0x20 */
254 #define SPECIAL 64              /* 0x */
255
256 static char *number(char *buf, unsigned NUM_TYPE num, int base, int size, int precision, int type)
257 {
258         /* we are called with base 8, 10 or 16, only, thus don't need "G..."  */
259         static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
260
261         char tmp[66];
262         char sign;
263         char locase;
264         int need_pfx = ((type & SPECIAL) && base != 10);
265         int i;
266
267         /* locase = 0 or 0x20. ORing digits or letters with 'locase'
268          * produces same digits or (maybe lowercased) letters */
269         locase = (type & SMALL);
270         if (type & LEFT)
271                 type &= ~ZEROPAD;
272         sign = 0;
273         if (type & SIGN) {
274                 if ((signed NUM_TYPE) num < 0) {
275                         sign = '-';
276                         num = - (signed NUM_TYPE) num;
277                         size--;
278                 } else if (type & PLUS) {
279                         sign = '+';
280                         size--;
281                 } else if (type & SPACE) {
282                         sign = ' ';
283                         size--;
284                 }
285         }
286         if (need_pfx) {
287                 size--;
288                 if (base == 16)
289                         size--;
290         }
291
292         /* generate full string in tmp[], in reverse order */
293         i = 0;
294         if (num == 0)
295                 tmp[i++] = '0';
296         /* Generic code, for any base:
297         else do {
298                 tmp[i++] = (digits[do_div(num,base)] | locase);
299         } while (num != 0);
300         */
301         else if (base != 10) { /* 8 or 16 */
302                 int mask = base - 1;
303                 int shift = 3;
304                 if (base == 16) shift = 4;
305                 do {
306                         tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
307                         num >>= shift;
308                 } while (num);
309         } else { /* base 10 */
310                 i = put_dec(tmp, num) - tmp;
311         }
312
313         /* printing 100 using %2d gives "100", not "00" */
314         if (i > precision)
315                 precision = i;
316         /* leading space padding */
317         size -= precision;
318         if (!(type & (ZEROPAD+LEFT)))
319                 while(--size >= 0)
320                         *buf++ = ' ';
321         /* sign */
322         if (sign)
323                 *buf++ = sign;
324         /* "0x" / "0" prefix */
325         if (need_pfx) {
326                 *buf++ = '0';
327                 if (base == 16)
328                         *buf++ = ('X' | locase);
329         }
330         /* zero or space padding */
331         if (!(type & LEFT)) {
332                 char c = (type & ZEROPAD) ? '0' : ' ';
333                 while (--size >= 0)
334                         *buf++ = c;
335         }
336         /* hmm even more zero padding? */
337         while (i <= --precision)
338                 *buf++ = '0';
339         /* actual digits of result */
340         while (--i >= 0)
341                 *buf++ = tmp[i];
342         /* trailing space padding */
343         while (--size >= 0)
344                 *buf++ = ' ';
345         return buf;
346 }
347
348 static char *string(char *buf, char *s, int field_width, int precision, int flags)
349 {
350         int len, i;
351
352         if (s == 0)
353                 s = "<NULL>";
354
355         len = strnlen(s, precision);
356
357         if (!(flags & LEFT))
358                 while (len < field_width--)
359                         *buf++ = ' ';
360         for (i = 0; i < len; ++i)
361                 *buf++ = *s++;
362         while (len < field_width--)
363                 *buf++ = ' ';
364         return buf;
365 }
366
367 #ifdef CONFIG_CMD_NET
368 static char *mac_address_string(char *buf, u8 *addr, int field_width,
369                                 int precision, int flags)
370 {
371         char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
372         char *p = mac_addr;
373         int i;
374
375         for (i = 0; i < 6; i++) {
376                 p = pack_hex_byte(p, addr[i]);
377                 if (!(flags & SPECIAL) && i != 5)
378                         *p++ = ':';
379         }
380         *p = '\0';
381
382         return string(buf, mac_addr, field_width, precision, flags & ~SPECIAL);
383 }
384
385 static char *ip6_addr_string(char *buf, u8 *addr, int field_width,
386                          int precision, int flags)
387 {
388         char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
389         char *p = ip6_addr;
390         int i;
391
392         for (i = 0; i < 8; i++) {
393                 p = pack_hex_byte(p, addr[2 * i]);
394                 p = pack_hex_byte(p, addr[2 * i + 1]);
395                 if (!(flags & SPECIAL) && i != 7)
396                         *p++ = ':';
397         }
398         *p = '\0';
399
400         return string(buf, ip6_addr, field_width, precision, flags & ~SPECIAL);
401 }
402
403 static char *ip4_addr_string(char *buf, u8 *addr, int field_width,
404                          int precision, int flags)
405 {
406         char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
407         char temp[3];   /* hold each IP quad in reverse order */
408         char *p = ip4_addr;
409         int i, digits;
410
411         for (i = 0; i < 4; i++) {
412                 digits = put_dec_trunc(temp, addr[i]) - temp;
413                 /* reverse the digits in the quad */
414                 while (digits--)
415                         *p++ = temp[digits];
416                 if (i != 3)
417                         *p++ = '.';
418         }
419         *p = '\0';
420
421         return string(buf, ip4_addr, field_width, precision, flags & ~SPECIAL);
422 }
423 #endif
424
425 /*
426  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
427  * by an extra set of alphanumeric characters that are extended format
428  * specifiers.
429  *
430  * Right now we handle:
431  *
432  * - 'M' For a 6-byte MAC address, it prints the address in the
433  *       usual colon-separated hex notation
434  * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
435  *       decimal for v4 and colon separated network-order 16 bit hex for v6)
436  * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
437  *       currently the same
438  *
439  * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
440  * function pointers are really function descriptors, which contain a
441  * pointer to the real address.
442  */
443 static char *pointer(const char *fmt, char *buf, void *ptr, int field_width, int precision, int flags)
444 {
445         if (!ptr)
446                 return string(buf, "(null)", field_width, precision, flags);
447
448 #ifdef CONFIG_CMD_NET
449         switch (*fmt) {
450         case 'm':
451                 flags |= SPECIAL;
452                 /* Fallthrough */
453         case 'M':
454                 return mac_address_string(buf, ptr, field_width, precision, flags);
455         case 'i':
456                 flags |= SPECIAL;
457                 /* Fallthrough */
458         case 'I':
459                 if (fmt[1] == '6')
460                         return ip6_addr_string(buf, ptr, field_width, precision, flags);
461                 if (fmt[1] == '4')
462                         return ip4_addr_string(buf, ptr, field_width, precision, flags);
463                 flags &= ~SPECIAL;
464                 break;
465         }
466 #endif
467         flags |= SMALL;
468         if (field_width == -1) {
469                 field_width = 2*sizeof(void *);
470                 flags |= ZEROPAD;
471         }
472         return number(buf, (unsigned long) ptr, 16, field_width, precision, flags);
473 }
474
475 /**
476  * vsprintf - Format a string and place it in a buffer
477  * @buf: The buffer to place the result into
478  * @fmt: The format string to use
479  * @args: Arguments for the format string
480  *
481  * This function follows C99 vsprintf, but has some extensions:
482  * %pS output the name of a text symbol
483  * %pF output the name of a function pointer
484  * %pR output the address range in a struct resource
485  *
486  * The function returns the number of characters written
487  * into @buf.
488  *
489  * Call this function if you are already dealing with a va_list.
490  * You probably want sprintf() instead.
491  */
492 int vsprintf(char *buf, const char *fmt, va_list args)
493 {
494         unsigned NUM_TYPE num;
495         int base;
496         char *str;
497
498         int flags;              /* flags to number() */
499
500         int field_width;        /* width of output field */
501         int precision;          /* min. # of digits for integers; max
502                                    number of chars for from string */
503         int qualifier;          /* 'h', 'l', or 'L' for integer fields */
504                                 /* 'z' support added 23/7/1999 S.H.    */
505                                 /* 'z' changed to 'Z' --davidm 1/25/99 */
506                                 /* 't' added for ptrdiff_t */
507
508         str = buf;
509
510         for (; *fmt ; ++fmt) {
511                 if (*fmt != '%') {
512                         *str++ = *fmt;
513                         continue;
514                 }
515
516                 /* process flags */
517                 flags = 0;
518                 repeat:
519                         ++fmt;          /* this also skips first '%' */
520                         switch (*fmt) {
521                                 case '-': flags |= LEFT; goto repeat;
522                                 case '+': flags |= PLUS; goto repeat;
523                                 case ' ': flags |= SPACE; goto repeat;
524                                 case '#': flags |= SPECIAL; goto repeat;
525                                 case '0': flags |= ZEROPAD; goto repeat;
526                         }
527
528                 /* get field width */
529                 field_width = -1;
530                 if (is_digit(*fmt))
531                         field_width = skip_atoi(&fmt);
532                 else if (*fmt == '*') {
533                         ++fmt;
534                         /* it's the next argument */
535                         field_width = va_arg(args, int);
536                         if (field_width < 0) {
537                                 field_width = -field_width;
538                                 flags |= LEFT;
539                         }
540                 }
541
542                 /* get the precision */
543                 precision = -1;
544                 if (*fmt == '.') {
545                         ++fmt;
546                         if (is_digit(*fmt))
547                                 precision = skip_atoi(&fmt);
548                         else if (*fmt == '*') {
549                                 ++fmt;
550                                 /* it's the next argument */
551                                 precision = va_arg(args, int);
552                         }
553                         if (precision < 0)
554                                 precision = 0;
555                 }
556
557                 /* get the conversion qualifier */
558                 qualifier = -1;
559                 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
560                     *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
561                         qualifier = *fmt;
562                         ++fmt;
563                         if (qualifier == 'l' && *fmt == 'l') {
564                                 qualifier = 'L';
565                                 ++fmt;
566                         }
567                 }
568
569                 /* default base */
570                 base = 10;
571
572                 switch (*fmt) {
573                 case 'c':
574                         if (!(flags & LEFT))
575                                 while (--field_width > 0)
576                                         *str++ = ' ';
577                         *str++ = (unsigned char) va_arg(args, int);
578                         while (--field_width > 0)
579                                 *str++ = ' ';
580                         continue;
581
582                 case 's':
583                         str = string(str, va_arg(args, char *), field_width, precision, flags);
584                         continue;
585
586                 case 'p':
587                         str = pointer(fmt+1, str,
588                                         va_arg(args, void *),
589                                         field_width, precision, flags);
590                         /* Skip all alphanumeric pointer suffixes */
591                         while (isalnum(fmt[1]))
592                                 fmt++;
593                         continue;
594
595                 case 'n':
596                         if (qualifier == 'l') {
597                                 long * ip = va_arg(args, long *);
598                                 *ip = (str - buf);
599                         } else {
600                                 int * ip = va_arg(args, int *);
601                                 *ip = (str - buf);
602                         }
603                         continue;
604
605                 case '%':
606                         *str++ = '%';
607                         continue;
608
609                 /* integer number formats - set up the flags and "break" */
610                 case 'o':
611                         base = 8;
612                         break;
613
614                 case 'x':
615                         flags |= SMALL;
616                 case 'X':
617                         base = 16;
618                         break;
619
620                 case 'd':
621                 case 'i':
622                         flags |= SIGN;
623                 case 'u':
624                         break;
625
626                 default:
627                         *str++ = '%';
628                         if (*fmt)
629                                 *str++ = *fmt;
630                         else
631                                 --fmt;
632                         continue;
633                 }
634 #ifdef CONFIG_SYS_64BIT_VSPRINTF
635                 if (qualifier == 'L')  /* "quad" for 64 bit variables */
636                         num = va_arg(args, unsigned long long);
637                 else
638 #endif
639                 if (qualifier == 'l') {
640                         num = va_arg(args, unsigned long);
641                         if (flags & SIGN)
642                                 num = (signed long) num;
643                 } else if (qualifier == 'Z' || qualifier == 'z') {
644                         num = va_arg(args, size_t);
645                 } else if (qualifier == 't') {
646                         num = va_arg(args, ptrdiff_t);
647                 } else if (qualifier == 'h') {
648                         num = (unsigned short) va_arg(args, int);
649                         if (flags & SIGN)
650                                 num = (signed short) num;
651                 } else {
652                         num = va_arg(args, unsigned int);
653                         if (flags & SIGN)
654                                 num = (signed int) num;
655                 }
656                 str = number(str, num, base, field_width, precision, flags);
657         }
658         *str = '\0';
659         return str-buf;
660 }
661
662 /**
663  * sprintf - Format a string and place it in a buffer
664  * @buf: The buffer to place the result into
665  * @fmt: The format string to use
666  * @...: Arguments for the format string
667  *
668  * The function returns the number of characters written
669  * into @buf.
670  *
671  * See the vsprintf() documentation for format string extensions over C99.
672  */
673 int sprintf(char * buf, const char *fmt, ...)
674 {
675         va_list args;
676         int i;
677
678         va_start(args, fmt);
679         i=vsprintf(buf,fmt,args);
680         va_end(args);
681         return i;
682 }
683
684 void panic(const char *fmt, ...)
685 {
686         va_list args;
687         va_start(args, fmt);
688         vprintf(fmt, args);
689         putc('\n');
690         va_end(args);
691 #if defined (CONFIG_PANIC_HANG)
692         hang();
693 #else
694         udelay (100000);        /* allow messages to go out */
695         do_reset (NULL, 0, 0, NULL);
696 #endif
697 }