4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * (C) Copyright 2000-2009
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
11 * Wirzenius wrote this portably, Torvalds fucked it up :-)
13 * from hush: simple_itoa() was lifted from boa-0.93.15
18 #include <efi_loader.h>
24 #include <linux/ctype.h>
25 #include <linux/err.h>
26 #include <linux/types.h>
27 #include <linux/string.h>
29 /* we use this so that we can do without the ctype library */
30 #define is_digit(c) ((c) >= '0' && (c) <= '9')
32 static int skip_atoi(const char **s)
37 i = i * 10 + *((*s)++) - '0';
42 /* Decimal conversion is by far the most typical, and is used
43 * for /proc and /sys data. This directly impacts e.g. top performance
44 * with many processes running. We optimize it for speed
46 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
47 * (with permission from the author, Douglas W. Jones). */
49 /* Formats correctly any integer in [0,99999].
50 * Outputs from one to five digits depending on input.
51 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
52 static char *put_dec_trunc(char *buf, unsigned q)
54 unsigned d3, d2, d1, d0;
59 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
60 q = (d0 * 0xcd) >> 11;
62 *buf++ = d0 + '0'; /* least significant digit */
63 d1 = q + 9*d3 + 5*d2 + d1;
65 q = (d1 * 0xcd) >> 11;
67 *buf++ = d1 + '0'; /* next digit */
70 if ((d2 != 0) || (d3 != 0)) {
73 *buf++ = d2 + '0'; /* next digit */
77 q = (d3 * 0xcd) >> 11;
79 *buf++ = d3 + '0'; /* next digit */
81 *buf++ = q + '0'; /* most sign. digit */
87 /* Same with if's removed. Always emits five digits */
88 static char *put_dec_full(char *buf, unsigned q)
90 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
91 /* but anyway, gcc produces better code with full-sized ints */
92 unsigned d3, d2, d1, d0;
98 * Possible ways to approx. divide by 10
99 * gcc -O2 replaces multiply with shifts and adds
100 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
101 * (x * 0x67) >> 10: 1100111
102 * (x * 0x34) >> 9: 110100 - same
103 * (x * 0x1a) >> 8: 11010 - same
104 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
107 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
108 q = (d0 * 0xcd) >> 11;
111 d1 = q + 9*d3 + 5*d2 + d1;
112 q = (d1 * 0xcd) >> 11;
122 q = (d3 * 0xcd) >> 11; /* - shorter code */
123 /* q = (d3 * 0x67) >> 10; - would also work */
129 /* No inlining helps gcc to use registers better */
130 static noinline char *put_dec(char *buf, uint64_t num)
135 return put_dec_trunc(buf, num);
136 rem = do_div(num, 100000);
137 buf = put_dec_full(buf, rem);
141 #define ZEROPAD 1 /* pad with zero */
142 #define SIGN 2 /* unsigned/signed long */
143 #define PLUS 4 /* show plus */
144 #define SPACE 8 /* space if plus */
145 #define LEFT 16 /* left justified */
146 #define SMALL 32 /* Must be 32 == 0x20 */
147 #define SPECIAL 64 /* 0x */
150 * Macro to add a new character to our output string, but only if it will
151 * fit. The macro moves to the next character position in the output string.
153 #define ADDCH(str, ch) do { \
159 static char *number(char *buf, char *end, u64 num,
160 int base, int size, int precision, int type)
162 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
163 static const char digits[16] = "0123456789ABCDEF";
168 int need_pfx = ((type & SPECIAL) && base != 10);
171 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
172 * produces same digits or (maybe lowercased) letters */
173 locase = (type & SMALL);
182 } else if (type & PLUS) {
185 } else if (type & SPACE) {
196 /* generate full string in tmp[], in reverse order */
200 /* Generic code, for any base:
202 tmp[i++] = (digits[do_div(num,base)] | locase);
205 else if (base != 10) { /* 8 or 16 */
213 tmp[i++] = (digits[((unsigned char)num) & mask]
217 } else { /* base 10 */
218 i = put_dec(tmp, num) - tmp;
221 /* printing 100 using %2d gives "100", not "00" */
224 /* leading space padding */
226 if (!(type & (ZEROPAD + LEFT))) {
233 /* "0x" / "0" prefix */
237 ADDCH(buf, 'X' | locase);
239 /* zero or space padding */
240 if (!(type & LEFT)) {
241 char c = (type & ZEROPAD) ? '0' : ' ';
246 /* hmm even more zero padding? */
247 while (i <= --precision)
249 /* actual digits of result */
252 /* trailing space padding */
258 static char *string(char *buf, char *end, char *s, int field_width,
259 int precision, int flags)
266 len = strnlen(s, precision);
269 while (len < field_width--)
271 for (i = 0; i < len; ++i)
273 while (len < field_width--)
278 /* U-Boot uses UTF-16 strings in the EFI context only. */
279 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
280 static char *string16(char *buf, char *end, u16 *s, int field_width,
281 int precision, int flags)
283 const u16 *str = s ? s : L"<NULL>";
284 ssize_t i, len = utf16_strnlen(str, precision);
287 for (; len < field_width; --field_width)
289 for (i = 0; i < len && buf + utf16_utf8_strnlen(str, 1) <= end; ++i) {
290 s32 s = utf16_get(&str);
296 for (; len < field_width; --field_width)
301 #if CONFIG_IS_ENABLED(EFI_DEVICE_PATH_TO_TEXT)
302 static char *device_path_string(char *buf, char *end, void *dp, int field_width,
303 int precision, int flags)
307 /* If dp == NULL output the string '<NULL>' */
309 return string16(buf, end, dp, field_width, precision, flags);
311 str = efi_dp_str((struct efi_device_path *)dp);
313 return ERR_PTR(-ENOMEM);
315 buf = string16(buf, end, str, field_width, precision, flags);
322 static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
323 int precision, int flags)
325 /* (6 * 2 hex digits), 5 colons and trailing zero */
326 char mac_addr[6 * 3];
330 for (i = 0; i < 6; i++) {
331 p = hex_byte_pack(p, addr[i]);
332 if (!(flags & SPECIAL) && i != 5)
337 return string(buf, end, mac_addr, field_width, precision,
341 static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
342 int precision, int flags)
344 /* (8 * 4 hex digits), 7 colons and trailing zero */
345 char ip6_addr[8 * 5];
349 for (i = 0; i < 8; i++) {
350 p = hex_byte_pack(p, addr[2 * i]);
351 p = hex_byte_pack(p, addr[2 * i + 1]);
352 if (!(flags & SPECIAL) && i != 7)
357 return string(buf, end, ip6_addr, field_width, precision,
361 static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
362 int precision, int flags)
364 /* (4 * 3 decimal digits), 3 dots and trailing zero */
365 char ip4_addr[4 * 4];
366 char temp[3]; /* hold each IP quad in reverse order */
370 for (i = 0; i < 4; i++) {
371 digits = put_dec_trunc(temp, addr[i]) - temp;
372 /* reverse the digits in the quad */
380 return string(buf, end, ip4_addr, field_width, precision,
384 #ifdef CONFIG_LIB_UUID
386 * This works (roughly) the same way as Linux's.
388 * %pUb: 01020304-0506-0708-090a-0b0c0d0e0f10
389 * %pUB: 01020304-0506-0708-090A-0B0C0D0E0F10
390 * %pUl: 04030201-0605-0807-090a-0b0c0d0e0f10
391 * %pUL: 04030201-0605-0807-090A-0B0C0D0E0F10
393 static char *uuid_string(char *buf, char *end, u8 *addr, int field_width,
394 int precision, int flags, const char *fmt)
396 char uuid[UUID_STR_LEN + 1];
401 str_format = UUID_STR_FORMAT_GUID | UUID_STR_UPPER_CASE;
404 str_format = UUID_STR_FORMAT_GUID;
407 str_format = UUID_STR_FORMAT_STD | UUID_STR_UPPER_CASE;
410 str_format = UUID_STR_FORMAT_STD;
415 uuid_bin_to_str(addr, uuid, str_format);
417 strcpy(uuid, "<NULL>");
419 return string(buf, end, uuid, field_width, precision, flags);
424 * Show a '%p' thing. A kernel extension is that the '%p' is followed
425 * by an extra set of alphanumeric characters that are extended format
428 * Right now we handle:
430 * - 'M' For a 6-byte MAC address, it prints the address in the
431 * usual colon-separated hex notation
432 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
433 * decimal for v4 and colon separated network-order 16 bit hex for v6)
434 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
438 static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
439 int field_width, int precision, int flags)
441 u64 num = (uintptr_t)ptr;
444 * Being a boot loader, we explicitly allow pointers to
445 * (physical) address null.
449 return string(buf, end, "(null)", field_width, precision,
454 /* Device paths only exist in the EFI context. */
455 #if CONFIG_IS_ENABLED(EFI_DEVICE_PATH_TO_TEXT) && !defined(API_BUILD)
457 return device_path_string(buf, end, ptr, field_width,
461 flags |= SPECIAL | ZEROPAD;
466 field_width = sizeof(phys_addr_t) * 2 + 2;
467 num = *(phys_addr_t *)ptr;
475 return mac_address_string(buf, end, ptr, field_width,
482 return ip6_addr_string(buf, end, ptr, field_width,
485 return ip4_addr_string(buf, end, ptr, field_width,
489 #ifdef CONFIG_LIB_UUID
491 return uuid_string(buf, end, ptr, field_width, precision,
498 if (field_width == -1) {
499 field_width = 2*sizeof(void *);
502 return number(buf, end, num, 16, field_width, precision, flags);
505 static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
512 int flags; /* flags to number() */
514 int field_width; /* width of output field */
515 int precision; /* min. # of digits for integers; max
516 number of chars for from string */
517 int qualifier; /* 'h', 'l', or 'L' for integer fields */
518 /* 'z' support added 23/7/1999 S.H. */
519 /* 'z' changed to 'Z' --davidm 1/25/99 */
520 /* 't' added for ptrdiff_t */
521 char *end = buf + size;
523 /* Make sure end is always >= buf - do we want this in U-Boot? */
530 for (; *fmt ; ++fmt) {
539 ++fmt; /* this also skips first '%' */
558 /* get field width */
561 field_width = skip_atoi(&fmt);
562 else if (*fmt == '*') {
564 /* it's the next argument */
565 field_width = va_arg(args, int);
566 if (field_width < 0) {
567 field_width = -field_width;
572 /* get the precision */
577 precision = skip_atoi(&fmt);
578 else if (*fmt == '*') {
580 /* it's the next argument */
581 precision = va_arg(args, int);
587 /* get the conversion qualifier */
589 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
590 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
593 if (qualifier == 'l' && *fmt == 'l') {
604 if (!(flags & LEFT)) {
605 while (--field_width > 0)
608 ADDCH(str, (unsigned char) va_arg(args, int));
609 while (--field_width > 0)
614 /* U-Boot uses UTF-16 strings in the EFI context only. */
615 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
616 if (qualifier == 'l') {
617 str = string16(str, end, va_arg(args, u16 *),
618 field_width, precision, flags);
622 str = string(str, end, va_arg(args, char *),
623 field_width, precision, flags);
628 str = pointer(fmt + 1, str, end,
629 va_arg(args, void *),
630 field_width, precision, flags);
633 /* Skip all alphanumeric pointer suffixes */
634 while (isalnum(fmt[1]))
639 if (qualifier == 'l') {
640 long *ip = va_arg(args, long *);
643 int *ip = va_arg(args, int *);
652 /* integer number formats - set up the flags and "break" */
677 if (qualifier == 'L') /* "quad" for 64 bit variables */
678 num = va_arg(args, unsigned long long);
679 else if (qualifier == 'l') {
680 num = va_arg(args, unsigned long);
682 num = (signed long) num;
683 } else if (qualifier == 'Z' || qualifier == 'z') {
684 num = va_arg(args, size_t);
685 } else if (qualifier == 't') {
686 num = va_arg(args, ptrdiff_t);
687 } else if (qualifier == 'h') {
688 num = (unsigned short) va_arg(args, int);
690 num = (signed short) num;
692 num = va_arg(args, unsigned int);
694 num = (signed int) num;
696 str = number(str, end, num, base, field_width, precision,
706 /* the trailing null byte doesn't count towards the total */
710 int vsnprintf(char *buf, size_t size, const char *fmt,
713 return vsnprintf_internal(buf, size, fmt, args);
716 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
720 i = vsnprintf(buf, size, fmt, args);
722 if (likely(i < size))
729 int snprintf(char *buf, size_t size, const char *fmt, ...)
735 i = vsnprintf(buf, size, fmt, args);
741 int scnprintf(char *buf, size_t size, const char *fmt, ...)
747 i = vscnprintf(buf, size, fmt, args);
754 * Format a string and place it in a buffer (va_list version)
756 * @param buf The buffer to place the result into
757 * @param fmt The format string to use
758 * @param args Arguments for the format string
760 * The function returns the number of characters written
761 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
764 * If you're not already dealing with a va_list consider using sprintf().
766 int vsprintf(char *buf, const char *fmt, va_list args)
768 return vsnprintf_internal(buf, INT_MAX, fmt, args);
771 int sprintf(char *buf, const char *fmt, ...)
777 i = vsprintf(buf, fmt, args);
782 #if CONFIG_IS_ENABLED(PRINTF)
783 int printf(const char *fmt, ...)
789 i = vprintf(fmt, args);
795 int vprintf(const char *fmt, va_list args)
798 char printbuffer[CONFIG_SYS_PBSIZE];
801 * For this to work, printbuffer must be larger than
802 * anything we ever want to print.
804 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
809 /* Print the string */
815 char *simple_itoa(ulong i)
817 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
818 static char local[22];
819 char *p = &local[21];
829 /* We don't seem to have %'d in U-Boot */
830 void print_grouped_ull(unsigned long long int_val, int digits)
835 digits = (digits + 2) / 3;
836 sprintf(str, "%*llu", digits * 3, int_val);
837 for (s = str; *s; s += grab) {
839 putc(s[-1] != ' ' ? ',' : ' ');
840 printf("%.*s", grab, s);
845 bool str2off(const char *p, loff_t *num)
849 *num = simple_strtoull(p, &endptr, 16);
850 return *p != '\0' && *endptr == '\0';
853 bool str2long(const char *p, ulong *num)
857 *num = simple_strtoul(p, &endptr, 16);
858 return *p != '\0' && *endptr == '\0';
861 char *strmhz(char *buf, unsigned long hz)
866 n = DIV_ROUND_CLOSEST(hz, 1000) / 1000L;
867 l = sprintf(buf, "%ld", n);
870 m = DIV_ROUND_CLOSEST(hz, 1000L);
872 sprintf(buf + l, ".%03ld", m);