blob: 97bed9d36dc2878be42e017166739e7912808ae9 [file] [log] [blame]
wdenk153d5112002-08-30 11:07:04 +00001/*
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 :-)
Simon Glass931948a2011-10-21 18:51:33 +000010 *
11 * from hush: simple_itoa() was lifted from boa-0.93.15
wdenk153d5112002-08-30 11:07:04 +000012 */
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>
Rob Clark8adf37f2017-09-09 06:47:41 -040020#include <charset.h>
wdenk153d5112002-08-30 11:07:04 +000021
Dirk Behme3d8759e2009-07-22 17:51:56 +020022#include <div64.h>
Dirk Behme3d8759e2009-07-22 17:51:56 +020023#define noinline __attribute__((noinline))
24
wdenk153d5112002-08-30 11:07:04 +000025/* we use this so that we can do without the ctype library */
26#define is_digit(c) ((c) >= '0' && (c) <= '9')
27
28static int skip_atoi(const char **s)
29{
Daniel Schwierzeckfc147852012-09-16 06:55:03 +000030 int i = 0;
wdenk153d5112002-08-30 11:07:04 +000031
32 while (is_digit(**s))
Daniel Schwierzeckfc147852012-09-16 06:55:03 +000033 i = i * 10 + *((*s)++) - '0';
34
wdenk153d5112002-08-30 11:07:04 +000035 return i;
36}
37
Mike Frysinger2b745dc2009-02-16 23:21:36 -050038/* Decimal conversion is by far the most typical, and is used
39 * for /proc and /sys data. This directly impacts e.g. top performance
40 * with many processes running. We optimize it for speed
41 * using code from
42 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
43 * (with permission from the author, Douglas W. Jones). */
44
45/* Formats correctly any integer in [0,99999].
46 * Outputs from one to five digits depending on input.
47 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
Daniel Schwierzeckfc147852012-09-16 06:55:03 +000048static char *put_dec_trunc(char *buf, unsigned q)
Mike Frysinger2b745dc2009-02-16 23:21:36 -050049{
50 unsigned d3, d2, d1, d0;
51 d1 = (q>>4) & 0xf;
52 d2 = (q>>8) & 0xf;
53 d3 = (q>>12);
54
55 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
56 q = (d0 * 0xcd) >> 11;
57 d0 = d0 - 10*q;
58 *buf++ = d0 + '0'; /* least significant digit */
59 d1 = q + 9*d3 + 5*d2 + d1;
60 if (d1 != 0) {
61 q = (d1 * 0xcd) >> 11;
62 d1 = d1 - 10*q;
63 *buf++ = d1 + '0'; /* next digit */
64
65 d2 = q + 2*d2;
66 if ((d2 != 0) || (d3 != 0)) {
67 q = (d2 * 0xd) >> 7;
68 d2 = d2 - 10*q;
69 *buf++ = d2 + '0'; /* next digit */
70
71 d3 = q + 4*d3;
72 if (d3 != 0) {
73 q = (d3 * 0xcd) >> 11;
74 d3 = d3 - 10*q;
75 *buf++ = d3 + '0'; /* next digit */
76 if (q != 0)
Daniel Schwierzeckfc147852012-09-16 06:55:03 +000077 *buf++ = q + '0'; /* most sign. digit */
Mike Frysinger2b745dc2009-02-16 23:21:36 -050078 }
79 }
80 }
81 return buf;
82}
83/* Same with if's removed. Always emits five digits */
Daniel Schwierzeckfc147852012-09-16 06:55:03 +000084static char *put_dec_full(char *buf, unsigned q)
Mike Frysinger2b745dc2009-02-16 23:21:36 -050085{
86 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
87 /* but anyway, gcc produces better code with full-sized ints */
88 unsigned d3, d2, d1, d0;
89 d1 = (q>>4) & 0xf;
90 d2 = (q>>8) & 0xf;
91 d3 = (q>>12);
92
Wolfgang Denke366f152009-04-05 00:27:57 +020093 /*
94 * Possible ways to approx. divide by 10
95 * gcc -O2 replaces multiply with shifts and adds
96 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
97 * (x * 0x67) >> 10: 1100111
98 * (x * 0x34) >> 9: 110100 - same
99 * (x * 0x1a) >> 8: 11010 - same
100 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
101 */
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500102
103 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
104 q = (d0 * 0xcd) >> 11;
105 d0 = d0 - 10*q;
106 *buf++ = d0 + '0';
107 d1 = q + 9*d3 + 5*d2 + d1;
108 q = (d1 * 0xcd) >> 11;
109 d1 = d1 - 10*q;
110 *buf++ = d1 + '0';
111
112 d2 = q + 2*d2;
113 q = (d2 * 0xd) >> 7;
114 d2 = d2 - 10*q;
115 *buf++ = d2 + '0';
116
117 d3 = q + 4*d3;
118 q = (d3 * 0xcd) >> 11; /* - shorter code */
119 /* q = (d3 * 0x67) >> 10; - would also work */
120 d3 = d3 - 10*q;
121 *buf++ = d3 + '0';
122 *buf++ = q + '0';
123 return buf;
124}
125/* No inlining helps gcc to use registers better */
Simon Glassd1c377f2014-10-15 04:38:34 -0600126static noinline char *put_dec(char *buf, uint64_t num)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500127{
128 while (1) {
129 unsigned rem;
130 if (num < 100000)
131 return put_dec_trunc(buf, num);
132 rem = do_div(num, 100000);
133 buf = put_dec_full(buf, rem);
134 }
135}
136
wdenk153d5112002-08-30 11:07:04 +0000137#define ZEROPAD 1 /* pad with zero */
138#define SIGN 2 /* unsigned/signed long */
139#define PLUS 4 /* show plus */
140#define SPACE 8 /* space if plus */
141#define LEFT 16 /* left justified */
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500142#define SMALL 32 /* Must be 32 == 0x20 */
143#define SPECIAL 64 /* 0x */
wdenk153d5112002-08-30 11:07:04 +0000144
Sonny Raocd55bde2011-11-02 09:52:08 +0000145/*
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)
Sonny Raocd55bde2011-11-02 09:52:08 +0000154
Daniel Schwierzecka3e5c522012-09-16 06:55:04 +0000155static char *number(char *buf, char *end, u64 num,
Sonny Raocd55bde2011-11-02 09:52:08 +0000156 int base, int size, int precision, int type)
wdenk153d5112002-08-30 11:07:04 +0000157{
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500158 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000159 static const char digits[16] = "0123456789ABCDEF";
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500160
161 char tmp[66];
162 char sign;
163 char locase;
164 int need_pfx = ((type & SPECIAL) && base != 10);
wdenk153d5112002-08-30 11:07:04 +0000165 int i;
166
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500167 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
168 * produces same digits or (maybe lowercased) letters */
169 locase = (type & SMALL);
wdenk153d5112002-08-30 11:07:04 +0000170 if (type & LEFT)
171 type &= ~ZEROPAD;
wdenk153d5112002-08-30 11:07:04 +0000172 sign = 0;
173 if (type & SIGN) {
Daniel Schwierzecka3e5c522012-09-16 06:55:04 +0000174 if ((s64) num < 0) {
wdenk153d5112002-08-30 11:07:04 +0000175 sign = '-';
Daniel Schwierzecka3e5c522012-09-16 06:55:04 +0000176 num = -(s64) num;
wdenk153d5112002-08-30 11:07:04 +0000177 size--;
178 } else if (type & PLUS) {
179 sign = '+';
180 size--;
181 } else if (type & SPACE) {
182 sign = ' ';
183 size--;
184 }
185 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500186 if (need_pfx) {
187 size--;
wdenk153d5112002-08-30 11:07:04 +0000188 if (base == 16)
wdenk153d5112002-08-30 11:07:04 +0000189 size--;
190 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500191
192 /* generate full string in tmp[], in reverse order */
wdenk153d5112002-08-30 11:07:04 +0000193 i = 0;
194 if (num == 0)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500195 tmp[i++] = '0';
196 /* Generic code, for any base:
197 else do {
198 tmp[i++] = (digits[do_div(num,base)] | locase);
199 } while (num != 0);
200 */
201 else if (base != 10) { /* 8 or 16 */
202 int mask = base - 1;
203 int shift = 3;
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000204
205 if (base == 16)
206 shift = 4;
207
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500208 do {
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000209 tmp[i++] = (digits[((unsigned char)num) & mask]
210 | locase);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500211 num >>= shift;
212 } while (num);
213 } else { /* base 10 */
214 i = put_dec(tmp, num) - tmp;
215 }
216
217 /* printing 100 using %2d gives "100", not "00" */
wdenk153d5112002-08-30 11:07:04 +0000218 if (i > precision)
219 precision = i;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500220 /* leading space padding */
wdenk153d5112002-08-30 11:07:04 +0000221 size -= precision;
Sonny Raocd55bde2011-11-02 09:52:08 +0000222 if (!(type & (ZEROPAD + LEFT))) {
223 while (--size >= 0)
224 ADDCH(buf, ' ');
225 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500226 /* sign */
wdenk153d5112002-08-30 11:07:04 +0000227 if (sign)
Sonny Raocd55bde2011-11-02 09:52:08 +0000228 ADDCH(buf, sign);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500229 /* "0x" / "0" prefix */
230 if (need_pfx) {
Sonny Raocd55bde2011-11-02 09:52:08 +0000231 ADDCH(buf, '0');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500232 if (base == 16)
Sonny Raocd55bde2011-11-02 09:52:08 +0000233 ADDCH(buf, 'X' | locase);
wdenk153d5112002-08-30 11:07:04 +0000234 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500235 /* zero or space padding */
236 if (!(type & LEFT)) {
237 char c = (type & ZEROPAD) ? '0' : ' ';
Sonny Raocd55bde2011-11-02 09:52:08 +0000238
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500239 while (--size >= 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000240 ADDCH(buf, c);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500241 }
242 /* hmm even more zero padding? */
243 while (i <= --precision)
Sonny Raocd55bde2011-11-02 09:52:08 +0000244 ADDCH(buf, '0');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500245 /* actual digits of result */
246 while (--i >= 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000247 ADDCH(buf, tmp[i]);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500248 /* trailing space padding */
249 while (--size >= 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000250 ADDCH(buf, ' ');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500251 return buf;
wdenk153d5112002-08-30 11:07:04 +0000252}
253
Sonny Raocd55bde2011-11-02 09:52:08 +0000254static char *string(char *buf, char *end, char *s, int field_width,
255 int precision, int flags)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500256{
257 int len, i;
wdenk153d5112002-08-30 11:07:04 +0000258
Kim Phillips75541502012-10-29 13:34:36 +0000259 if (s == NULL)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500260 s = "<NULL>";
261
262 len = strnlen(s, precision);
263
264 if (!(flags & LEFT))
265 while (len < field_width--)
Sonny Raocd55bde2011-11-02 09:52:08 +0000266 ADDCH(buf, ' ');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500267 for (i = 0; i < len; ++i)
Sonny Raocd55bde2011-11-02 09:52:08 +0000268 ADDCH(buf, *s++);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500269 while (len < field_width--)
Sonny Raocd55bde2011-11-02 09:52:08 +0000270 ADDCH(buf, ' ');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500271 return buf;
272}
273
Rob Clark8adf37f2017-09-09 06:47:41 -0400274static char *string16(char *buf, char *end, u16 *s, int field_width,
275 int precision, int flags)
276{
277 u16 *str = s ? s : L"<NULL>";
278 int utf16_len = utf16_strnlen(str, precision);
279 u8 utf8[utf16_len * MAX_UTF8_PER_UTF16];
280 int utf8_len, i;
281
282 utf8_len = utf16_to_utf8(utf8, str, utf16_len) - utf8;
283
284 if (!(flags & LEFT))
285 while (utf8_len < field_width--)
286 ADDCH(buf, ' ');
287 for (i = 0; i < utf8_len; ++i)
288 ADDCH(buf, utf8[i]);
289 while (utf8_len < field_width--)
290 ADDCH(buf, ' ');
291 return buf;
292}
293
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500294#ifdef CONFIG_CMD_NET
Jeroen Hofstee46590552014-07-10 22:33:00 +0200295static const char hex_asc[] = "0123456789abcdef";
296#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
297#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
298
299static inline char *pack_hex_byte(char *buf, u8 byte)
300{
301 *buf++ = hex_asc_hi(byte);
302 *buf++ = hex_asc_lo(byte);
303 return buf;
304}
305
Sonny Raocd55bde2011-11-02 09:52:08 +0000306static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500307 int precision, int flags)
wdenk153d5112002-08-30 11:07:04 +0000308{
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000309 /* (6 * 2 hex digits), 5 colons and trailing zero */
310 char mac_addr[6 * 3];
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500311 char *p = mac_addr;
312 int i;
313
314 for (i = 0; i < 6; i++) {
315 p = pack_hex_byte(p, addr[i]);
316 if (!(flags & SPECIAL) && i != 5)
317 *p++ = ':';
318 }
319 *p = '\0';
320
Sonny Raocd55bde2011-11-02 09:52:08 +0000321 return string(buf, end, mac_addr, field_width, precision,
322 flags & ~SPECIAL);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500323}
324
Sonny Raocd55bde2011-11-02 09:52:08 +0000325static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500326 int precision, int flags)
327{
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000328 /* (8 * 4 hex digits), 7 colons and trailing zero */
329 char ip6_addr[8 * 5];
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500330 char *p = ip6_addr;
331 int i;
332
333 for (i = 0; i < 8; i++) {
334 p = pack_hex_byte(p, addr[2 * i]);
335 p = pack_hex_byte(p, addr[2 * i + 1]);
336 if (!(flags & SPECIAL) && i != 7)
337 *p++ = ':';
338 }
339 *p = '\0';
340
Sonny Raocd55bde2011-11-02 09:52:08 +0000341 return string(buf, end, ip6_addr, field_width, precision,
342 flags & ~SPECIAL);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500343}
344
Sonny Raocd55bde2011-11-02 09:52:08 +0000345static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500346 int precision, int flags)
347{
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000348 /* (4 * 3 decimal digits), 3 dots and trailing zero */
349 char ip4_addr[4 * 4];
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500350 char temp[3]; /* hold each IP quad in reverse order */
351 char *p = ip4_addr;
352 int i, digits;
353
354 for (i = 0; i < 4; i++) {
355 digits = put_dec_trunc(temp, addr[i]) - temp;
356 /* reverse the digits in the quad */
357 while (digits--)
358 *p++ = temp[digits];
359 if (i != 3)
360 *p++ = '.';
361 }
362 *p = '\0';
363
Sonny Raocd55bde2011-11-02 09:52:08 +0000364 return string(buf, end, ip4_addr, field_width, precision,
365 flags & ~SPECIAL);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500366}
367#endif
368
369/*
370 * Show a '%p' thing. A kernel extension is that the '%p' is followed
371 * by an extra set of alphanumeric characters that are extended format
372 * specifiers.
373 *
374 * Right now we handle:
375 *
376 * - 'M' For a 6-byte MAC address, it prints the address in the
377 * usual colon-separated hex notation
378 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
379 * decimal for v4 and colon separated network-order 16 bit hex for v6)
380 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
381 * currently the same
382 *
383 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
384 * function pointers are really function descriptors, which contain a
385 * pointer to the real address.
386 */
Sonny Raocd55bde2011-11-02 09:52:08 +0000387static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
388 int field_width, int precision, int flags)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500389{
Thierry Reding3e336c82014-11-12 18:26:47 -0700390 u64 num = (uintptr_t)ptr;
391
Wolfgang Denk88a8b012012-10-30 09:19:52 +0000392 /*
393 * Being a boot loader, we explicitly allow pointers to
394 * (physical) address null.
395 */
396#if 0
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500397 if (!ptr)
Sonny Raocd55bde2011-11-02 09:52:08 +0000398 return string(buf, end, "(null)", field_width, precision,
399 flags);
Wolfgang Denk88a8b012012-10-30 09:19:52 +0000400#endif
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500401
402#ifdef CONFIG_CMD_NET
403 switch (*fmt) {
Thierry Reding3e336c82014-11-12 18:26:47 -0700404 case 'a':
405 flags |= SPECIAL | ZEROPAD;
406
407 switch (fmt[1]) {
408 case 'p':
409 default:
410 field_width = sizeof(phys_addr_t) * 2 + 2;
411 num = *(phys_addr_t *)ptr;
412 break;
413 }
414 break;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500415 case 'm':
416 flags |= SPECIAL;
417 /* Fallthrough */
418 case 'M':
Sonny Raocd55bde2011-11-02 09:52:08 +0000419 return mac_address_string(buf, end, ptr, field_width,
420 precision, flags);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500421 case 'i':
422 flags |= SPECIAL;
423 /* Fallthrough */
424 case 'I':
425 if (fmt[1] == '6')
Sonny Raocd55bde2011-11-02 09:52:08 +0000426 return ip6_addr_string(buf, end, ptr, field_width,
427 precision, flags);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500428 if (fmt[1] == '4')
Sonny Raocd55bde2011-11-02 09:52:08 +0000429 return ip4_addr_string(buf, end, ptr, field_width,
430 precision, flags);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500431 flags &= ~SPECIAL;
432 break;
433 }
wdenkf602aa02004-03-13 23:29:43 +0000434#endif
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500435 flags |= SMALL;
436 if (field_width == -1) {
437 field_width = 2*sizeof(void *);
438 flags |= ZEROPAD;
439 }
Thierry Reding3e336c82014-11-12 18:26:47 -0700440 return number(buf, end, num, 16, field_width, precision, flags);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500441}
442
Sonny Raocd55bde2011-11-02 09:52:08 +0000443static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
444 va_list args)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500445{
Daniel Schwierzecka3e5c522012-09-16 06:55:04 +0000446 u64 num;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500447 int base;
448 char *str;
wdenk153d5112002-08-30 11:07:04 +0000449
450 int flags; /* flags to number() */
451
452 int field_width; /* width of output field */
453 int precision; /* min. # of digits for integers; max
454 number of chars for from string */
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500455 int qualifier; /* 'h', 'l', or 'L' for integer fields */
456 /* 'z' support added 23/7/1999 S.H. */
457 /* 'z' changed to 'Z' --davidm 1/25/99 */
458 /* 't' added for ptrdiff_t */
Sonny Raocd55bde2011-11-02 09:52:08 +0000459 char *end = buf + size;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500460
Sonny Raocd55bde2011-11-02 09:52:08 +0000461 /* Make sure end is always >= buf - do we want this in U-Boot? */
462 if (end < buf) {
463 end = ((void *)-1);
464 size = end - buf;
465 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500466 str = buf;
wdenk153d5112002-08-30 11:07:04 +0000467
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500468 for (; *fmt ; ++fmt) {
wdenk153d5112002-08-30 11:07:04 +0000469 if (*fmt != '%') {
Sonny Raocd55bde2011-11-02 09:52:08 +0000470 ADDCH(str, *fmt);
wdenk153d5112002-08-30 11:07:04 +0000471 continue;
472 }
473
474 /* process flags */
475 flags = 0;
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000476repeat:
wdenk153d5112002-08-30 11:07:04 +0000477 ++fmt; /* this also skips first '%' */
478 switch (*fmt) {
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000479 case '-':
480 flags |= LEFT;
481 goto repeat;
482 case '+':
483 flags |= PLUS;
484 goto repeat;
485 case ' ':
486 flags |= SPACE;
487 goto repeat;
488 case '#':
489 flags |= SPECIAL;
490 goto repeat;
491 case '0':
492 flags |= ZEROPAD;
493 goto repeat;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500494 }
wdenk153d5112002-08-30 11:07:04 +0000495
496 /* get field width */
497 field_width = -1;
498 if (is_digit(*fmt))
499 field_width = skip_atoi(&fmt);
500 else if (*fmt == '*') {
501 ++fmt;
502 /* it's the next argument */
503 field_width = va_arg(args, int);
504 if (field_width < 0) {
505 field_width = -field_width;
506 flags |= LEFT;
507 }
508 }
509
510 /* get the precision */
511 precision = -1;
512 if (*fmt == '.') {
513 ++fmt;
514 if (is_digit(*fmt))
515 precision = skip_atoi(&fmt);
516 else if (*fmt == '*') {
517 ++fmt;
518 /* it's the next argument */
519 precision = va_arg(args, int);
520 }
521 if (precision < 0)
522 precision = 0;
523 }
524
525 /* get the conversion qualifier */
526 qualifier = -1;
Jean-Christophe PLAGNIOL-VILLARD0f720692008-07-14 14:11:45 +0200527 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500528 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
wdenk153d5112002-08-30 11:07:04 +0000529 qualifier = *fmt;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500530 ++fmt;
531 if (qualifier == 'l' && *fmt == 'l') {
532 qualifier = 'L';
James Yang7fc40152008-01-10 16:02:07 -0600533 ++fmt;
534 }
wdenk153d5112002-08-30 11:07:04 +0000535 }
536
537 /* default base */
538 base = 10;
539
540 switch (*fmt) {
541 case 'c':
Sonny Raocd55bde2011-11-02 09:52:08 +0000542 if (!(flags & LEFT)) {
wdenk153d5112002-08-30 11:07:04 +0000543 while (--field_width > 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000544 ADDCH(str, ' ');
545 }
546 ADDCH(str, (unsigned char) va_arg(args, int));
wdenk153d5112002-08-30 11:07:04 +0000547 while (--field_width > 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000548 ADDCH(str, ' ');
wdenk153d5112002-08-30 11:07:04 +0000549 continue;
550
551 case 's':
Rob Clark8adf37f2017-09-09 06:47:41 -0400552 if (qualifier == 'l' && !IS_ENABLED(CONFIG_SPL_BUILD)) {
553 str = string16(str, end, va_arg(args, u16 *),
554 field_width, precision, flags);
555 } else {
556 str = string(str, end, va_arg(args, char *),
557 field_width, precision, flags);
558 }
wdenk153d5112002-08-30 11:07:04 +0000559 continue;
560
561 case 'p':
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000562 str = pointer(fmt + 1, str, end,
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500563 va_arg(args, void *),
564 field_width, precision, flags);
565 /* Skip all alphanumeric pointer suffixes */
566 while (isalnum(fmt[1]))
567 fmt++;
wdenk153d5112002-08-30 11:07:04 +0000568 continue;
569
wdenk153d5112002-08-30 11:07:04 +0000570 case 'n':
571 if (qualifier == 'l') {
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000572 long *ip = va_arg(args, long *);
wdenk153d5112002-08-30 11:07:04 +0000573 *ip = (str - buf);
574 } else {
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000575 int *ip = va_arg(args, int *);
wdenk153d5112002-08-30 11:07:04 +0000576 *ip = (str - buf);
577 }
578 continue;
579
580 case '%':
Sonny Raocd55bde2011-11-02 09:52:08 +0000581 ADDCH(str, '%');
wdenk153d5112002-08-30 11:07:04 +0000582 continue;
583
584 /* integer number formats - set up the flags and "break" */
585 case 'o':
586 base = 8;
587 break;
588
wdenk153d5112002-08-30 11:07:04 +0000589 case 'x':
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500590 flags |= SMALL;
591 case 'X':
wdenk153d5112002-08-30 11:07:04 +0000592 base = 16;
593 break;
594
595 case 'd':
596 case 'i':
597 flags |= SIGN;
598 case 'u':
599 break;
600
601 default:
Sonny Raocd55bde2011-11-02 09:52:08 +0000602 ADDCH(str, '%');
wdenk153d5112002-08-30 11:07:04 +0000603 if (*fmt)
Sonny Raocd55bde2011-11-02 09:52:08 +0000604 ADDCH(str, *fmt);
wdenk153d5112002-08-30 11:07:04 +0000605 else
606 --fmt;
607 continue;
608 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500609 if (qualifier == 'L') /* "quad" for 64 bit variables */
wdenkf602aa02004-03-13 23:29:43 +0000610 num = va_arg(args, unsigned long long);
Heiko Schocher0f602e12009-12-03 11:21:21 +0100611 else if (qualifier == 'l') {
wdenk153d5112002-08-30 11:07:04 +0000612 num = va_arg(args, unsigned long);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500613 if (flags & SIGN)
614 num = (signed long) num;
Jean-Christophe PLAGNIOL-VILLARD0f720692008-07-14 14:11:45 +0200615 } else if (qualifier == 'Z' || qualifier == 'z') {
616 num = va_arg(args, size_t);
617 } else if (qualifier == 't') {
618 num = va_arg(args, ptrdiff_t);
619 } else if (qualifier == 'h') {
wdenk153d5112002-08-30 11:07:04 +0000620 num = (unsigned short) va_arg(args, int);
621 if (flags & SIGN)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500622 num = (signed short) num;
623 } else {
wdenk153d5112002-08-30 11:07:04 +0000624 num = va_arg(args, unsigned int);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500625 if (flags & SIGN)
626 num = (signed int) num;
627 }
Sonny Raocd55bde2011-11-02 09:52:08 +0000628 str = number(str, end, num, base, field_width, precision,
629 flags);
630 }
631
Sonny Raocd55bde2011-11-02 09:52:08 +0000632 if (size > 0) {
633 ADDCH(str, '\0');
634 if (str > end)
635 end[-1] = '\0';
Darwin Rambo6958ede2013-12-19 15:14:19 -0800636 --str;
wdenk153d5112002-08-30 11:07:04 +0000637 }
Sonny Raocd55bde2011-11-02 09:52:08 +0000638 /* the trailing null byte doesn't count towards the total */
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000639 return str - buf;
wdenk153d5112002-08-30 11:07:04 +0000640}
641
Sonny Raocd55bde2011-11-02 09:52:08 +0000642int vsnprintf(char *buf, size_t size, const char *fmt,
643 va_list args)
644{
645 return vsnprintf_internal(buf, size, fmt, args);
646}
647
Sonny Raocd55bde2011-11-02 09:52:08 +0000648int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
649{
650 int i;
651
652 i = vsnprintf(buf, size, fmt, args);
653
654 if (likely(i < size))
655 return i;
656 if (size != 0)
657 return size - 1;
658 return 0;
659}
660
Sonny Raocd55bde2011-11-02 09:52:08 +0000661int snprintf(char *buf, size_t size, const char *fmt, ...)
662{
663 va_list args;
664 int i;
665
666 va_start(args, fmt);
667 i = vsnprintf(buf, size, fmt, args);
668 va_end(args);
669
670 return i;
671}
672
Sonny Raocd55bde2011-11-02 09:52:08 +0000673int scnprintf(char *buf, size_t size, const char *fmt, ...)
674{
675 va_list args;
676 int i;
677
678 va_start(args, fmt);
679 i = vscnprintf(buf, size, fmt, args);
680 va_end(args);
681
682 return i;
683}
Sonny Raocd55bde2011-11-02 09:52:08 +0000684
685/**
686 * Format a string and place it in a buffer (va_list version)
687 *
688 * @param buf The buffer to place the result into
689 * @param fmt The format string to use
690 * @param args Arguments for the format string
691 *
692 * The function returns the number of characters written
693 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
694 * buffer overflows.
695 *
696 * If you're not already dealing with a va_list consider using sprintf().
697 */
698int vsprintf(char *buf, const char *fmt, va_list args)
699{
700 return vsnprintf_internal(buf, INT_MAX, fmt, args);
701}
702
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000703int sprintf(char *buf, const char *fmt, ...)
wdenk153d5112002-08-30 11:07:04 +0000704{
705 va_list args;
706 int i;
707
708 va_start(args, fmt);
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000709 i = vsprintf(buf, fmt, args);
wdenk153d5112002-08-30 11:07:04 +0000710 va_end(args);
711 return i;
712}
713
Stefan Roese363ab7b2015-11-23 07:00:22 +0100714int printf(const char *fmt, ...)
715{
716 va_list args;
717 uint i;
718 char printbuffer[CONFIG_SYS_PBSIZE];
719
720 va_start(args, fmt);
721
722 /*
723 * For this to work, printbuffer must be larger than
724 * anything we ever want to print.
725 */
726 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
727 va_end(args);
728
729 /* Print the string */
730 puts(printbuffer);
731 return i;
732}
733
734int vprintf(const char *fmt, va_list args)
735{
736 uint i;
737 char printbuffer[CONFIG_SYS_PBSIZE];
738
739 /*
740 * For this to work, printbuffer must be larger than
741 * anything we ever want to print.
742 */
743 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
744
745 /* Print the string */
746 puts(printbuffer);
747 return i;
748}
749
Simon Glass509dbf92015-02-27 22:06:32 -0700750
Simon Glass02aa9e62011-06-29 09:49:34 +0000751void __assert_fail(const char *assertion, const char *file, unsigned line,
752 const char *function)
753{
754 /* This will not return */
755 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
756 assertion);
757}
Simon Glass931948a2011-10-21 18:51:33 +0000758
759char *simple_itoa(ulong i)
760{
761 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
762 static char local[22];
763 char *p = &local[21];
764
765 *p-- = '\0';
766 do {
767 *p-- = '0' + i % 10;
768 i /= 10;
769 } while (i > 0);
770 return p + 1;
771}
Simon Glassb09cfd82013-06-11 11:14:38 -0700772
773/* We don't seem to have %'d in U-Boot */
774void print_grouped_ull(unsigned long long int_val, int digits)
775{
776 char str[21], *s;
777 int grab = 3;
778
779 digits = (digits + 2) / 3;
780 sprintf(str, "%*llu", digits * 3, int_val);
781 for (s = str; *s; s += grab) {
782 if (s != str)
783 putc(s[-1] != ' ' ? ',' : ' ');
784 printf("%.*s", grab, s);
785 grab = 3;
786 }
787}
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200788
789bool str2off(const char *p, loff_t *num)
790{
791 char *endptr;
792
793 *num = simple_strtoull(p, &endptr, 16);
794 return *p != '\0' && *endptr == '\0';
795}
796
797bool str2long(const char *p, ulong *num)
798{
799 char *endptr;
800
801 *num = simple_strtoul(p, &endptr, 16);
802 return *p != '\0' && *endptr == '\0';
803}