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