blob: dd572d2868a5453c8cd99326a944f3ee4e20cd25 [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>
Rob Clarkc82342f2017-09-09 06:47:42 -040021#include <uuid.h>
wdenk153d5112002-08-30 11:07:04 +000022
Dirk Behme3d8759e2009-07-22 17:51:56 +020023#include <div64.h>
Dirk Behme3d8759e2009-07-22 17:51:56 +020024#define noinline __attribute__((noinline))
25
wdenk153d5112002-08-30 11:07:04 +000026/* we use this so that we can do without the ctype library */
27#define is_digit(c) ((c) >= '0' && (c) <= '9')
28
29static int skip_atoi(const char **s)
30{
Daniel Schwierzeckfc147852012-09-16 06:55:03 +000031 int i = 0;
wdenk153d5112002-08-30 11:07:04 +000032
33 while (is_digit(**s))
Daniel Schwierzeckfc147852012-09-16 06:55:03 +000034 i = i * 10 + *((*s)++) - '0';
35
wdenk153d5112002-08-30 11:07:04 +000036 return i;
37}
38
Mike Frysinger2b745dc2009-02-16 23:21:36 -050039/* Decimal conversion is by far the most typical, and is used
40 * for /proc and /sys data. This directly impacts e.g. top performance
41 * with many processes running. We optimize it for speed
42 * using code from
43 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
44 * (with permission from the author, Douglas W. Jones). */
45
46/* Formats correctly any integer in [0,99999].
47 * Outputs from one to five digits depending on input.
48 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
Daniel Schwierzeckfc147852012-09-16 06:55:03 +000049static char *put_dec_trunc(char *buf, unsigned q)
Mike Frysinger2b745dc2009-02-16 23:21:36 -050050{
51 unsigned d3, d2, d1, d0;
52 d1 = (q>>4) & 0xf;
53 d2 = (q>>8) & 0xf;
54 d3 = (q>>12);
55
56 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
57 q = (d0 * 0xcd) >> 11;
58 d0 = d0 - 10*q;
59 *buf++ = d0 + '0'; /* least significant digit */
60 d1 = q + 9*d3 + 5*d2 + d1;
61 if (d1 != 0) {
62 q = (d1 * 0xcd) >> 11;
63 d1 = d1 - 10*q;
64 *buf++ = d1 + '0'; /* next digit */
65
66 d2 = q + 2*d2;
67 if ((d2 != 0) || (d3 != 0)) {
68 q = (d2 * 0xd) >> 7;
69 d2 = d2 - 10*q;
70 *buf++ = d2 + '0'; /* next digit */
71
72 d3 = q + 4*d3;
73 if (d3 != 0) {
74 q = (d3 * 0xcd) >> 11;
75 d3 = d3 - 10*q;
76 *buf++ = d3 + '0'; /* next digit */
77 if (q != 0)
Daniel Schwierzeckfc147852012-09-16 06:55:03 +000078 *buf++ = q + '0'; /* most sign. digit */
Mike Frysinger2b745dc2009-02-16 23:21:36 -050079 }
80 }
81 }
82 return buf;
83}
84/* Same with if's removed. Always emits five digits */
Daniel Schwierzeckfc147852012-09-16 06:55:03 +000085static char *put_dec_full(char *buf, unsigned q)
Mike Frysinger2b745dc2009-02-16 23:21:36 -050086{
87 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
88 /* but anyway, gcc produces better code with full-sized ints */
89 unsigned d3, d2, d1, d0;
90 d1 = (q>>4) & 0xf;
91 d2 = (q>>8) & 0xf;
92 d3 = (q>>12);
93
Wolfgang Denke366f152009-04-05 00:27:57 +020094 /*
95 * Possible ways to approx. divide by 10
96 * gcc -O2 replaces multiply with shifts and adds
97 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
98 * (x * 0x67) >> 10: 1100111
99 * (x * 0x34) >> 9: 110100 - same
100 * (x * 0x1a) >> 8: 11010 - same
101 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
102 */
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500103
104 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
105 q = (d0 * 0xcd) >> 11;
106 d0 = d0 - 10*q;
107 *buf++ = d0 + '0';
108 d1 = q + 9*d3 + 5*d2 + d1;
109 q = (d1 * 0xcd) >> 11;
110 d1 = d1 - 10*q;
111 *buf++ = d1 + '0';
112
113 d2 = q + 2*d2;
114 q = (d2 * 0xd) >> 7;
115 d2 = d2 - 10*q;
116 *buf++ = d2 + '0';
117
118 d3 = q + 4*d3;
119 q = (d3 * 0xcd) >> 11; /* - shorter code */
120 /* q = (d3 * 0x67) >> 10; - would also work */
121 d3 = d3 - 10*q;
122 *buf++ = d3 + '0';
123 *buf++ = q + '0';
124 return buf;
125}
126/* No inlining helps gcc to use registers better */
Simon Glassd1c377f2014-10-15 04:38:34 -0600127static noinline char *put_dec(char *buf, uint64_t num)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500128{
129 while (1) {
130 unsigned rem;
131 if (num < 100000)
132 return put_dec_trunc(buf, num);
133 rem = do_div(num, 100000);
134 buf = put_dec_full(buf, rem);
135 }
136}
137
wdenk153d5112002-08-30 11:07:04 +0000138#define ZEROPAD 1 /* pad with zero */
139#define SIGN 2 /* unsigned/signed long */
140#define PLUS 4 /* show plus */
141#define SPACE 8 /* space if plus */
142#define LEFT 16 /* left justified */
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500143#define SMALL 32 /* Must be 32 == 0x20 */
144#define SPECIAL 64 /* 0x */
wdenk153d5112002-08-30 11:07:04 +0000145
Sonny Raocd55bde2011-11-02 09:52:08 +0000146/*
147 * Macro to add a new character to our output string, but only if it will
148 * fit. The macro moves to the next character position in the output string.
149 */
150#define ADDCH(str, ch) do { \
151 if ((str) < end) \
152 *(str) = (ch); \
153 ++str; \
154 } while (0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000155
Daniel Schwierzecka3e5c522012-09-16 06:55:04 +0000156static char *number(char *buf, char *end, u64 num,
Sonny Raocd55bde2011-11-02 09:52:08 +0000157 int base, int size, int precision, int type)
wdenk153d5112002-08-30 11:07:04 +0000158{
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500159 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000160 static const char digits[16] = "0123456789ABCDEF";
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500161
162 char tmp[66];
163 char sign;
164 char locase;
165 int need_pfx = ((type & SPECIAL) && base != 10);
wdenk153d5112002-08-30 11:07:04 +0000166 int i;
167
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500168 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
169 * produces same digits or (maybe lowercased) letters */
170 locase = (type & SMALL);
wdenk153d5112002-08-30 11:07:04 +0000171 if (type & LEFT)
172 type &= ~ZEROPAD;
wdenk153d5112002-08-30 11:07:04 +0000173 sign = 0;
174 if (type & SIGN) {
Daniel Schwierzecka3e5c522012-09-16 06:55:04 +0000175 if ((s64) num < 0) {
wdenk153d5112002-08-30 11:07:04 +0000176 sign = '-';
Daniel Schwierzecka3e5c522012-09-16 06:55:04 +0000177 num = -(s64) num;
wdenk153d5112002-08-30 11:07:04 +0000178 size--;
179 } else if (type & PLUS) {
180 sign = '+';
181 size--;
182 } else if (type & SPACE) {
183 sign = ' ';
184 size--;
185 }
186 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500187 if (need_pfx) {
188 size--;
wdenk153d5112002-08-30 11:07:04 +0000189 if (base == 16)
wdenk153d5112002-08-30 11:07:04 +0000190 size--;
191 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500192
193 /* generate full string in tmp[], in reverse order */
wdenk153d5112002-08-30 11:07:04 +0000194 i = 0;
195 if (num == 0)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500196 tmp[i++] = '0';
197 /* Generic code, for any base:
198 else do {
199 tmp[i++] = (digits[do_div(num,base)] | locase);
200 } while (num != 0);
201 */
202 else if (base != 10) { /* 8 or 16 */
203 int mask = base - 1;
204 int shift = 3;
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000205
206 if (base == 16)
207 shift = 4;
208
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500209 do {
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000210 tmp[i++] = (digits[((unsigned char)num) & mask]
211 | locase);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500212 num >>= shift;
213 } while (num);
214 } else { /* base 10 */
215 i = put_dec(tmp, num) - tmp;
216 }
217
218 /* printing 100 using %2d gives "100", not "00" */
wdenk153d5112002-08-30 11:07:04 +0000219 if (i > precision)
220 precision = i;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500221 /* leading space padding */
wdenk153d5112002-08-30 11:07:04 +0000222 size -= precision;
Sonny Raocd55bde2011-11-02 09:52:08 +0000223 if (!(type & (ZEROPAD + LEFT))) {
224 while (--size >= 0)
225 ADDCH(buf, ' ');
226 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500227 /* sign */
wdenk153d5112002-08-30 11:07:04 +0000228 if (sign)
Sonny Raocd55bde2011-11-02 09:52:08 +0000229 ADDCH(buf, sign);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500230 /* "0x" / "0" prefix */
231 if (need_pfx) {
Sonny Raocd55bde2011-11-02 09:52:08 +0000232 ADDCH(buf, '0');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500233 if (base == 16)
Sonny Raocd55bde2011-11-02 09:52:08 +0000234 ADDCH(buf, 'X' | locase);
wdenk153d5112002-08-30 11:07:04 +0000235 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500236 /* zero or space padding */
237 if (!(type & LEFT)) {
238 char c = (type & ZEROPAD) ? '0' : ' ';
Sonny Raocd55bde2011-11-02 09:52:08 +0000239
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500240 while (--size >= 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000241 ADDCH(buf, c);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500242 }
243 /* hmm even more zero padding? */
244 while (i <= --precision)
Sonny Raocd55bde2011-11-02 09:52:08 +0000245 ADDCH(buf, '0');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500246 /* actual digits of result */
247 while (--i >= 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000248 ADDCH(buf, tmp[i]);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500249 /* trailing space padding */
250 while (--size >= 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000251 ADDCH(buf, ' ');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500252 return buf;
wdenk153d5112002-08-30 11:07:04 +0000253}
254
Sonny Raocd55bde2011-11-02 09:52:08 +0000255static char *string(char *buf, char *end, char *s, int field_width,
256 int precision, int flags)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500257{
258 int len, i;
wdenk153d5112002-08-30 11:07:04 +0000259
Kim Phillips75541502012-10-29 13:34:36 +0000260 if (s == NULL)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500261 s = "<NULL>";
262
263 len = strnlen(s, precision);
264
265 if (!(flags & LEFT))
266 while (len < field_width--)
Sonny Raocd55bde2011-11-02 09:52:08 +0000267 ADDCH(buf, ' ');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500268 for (i = 0; i < len; ++i)
Sonny Raocd55bde2011-11-02 09:52:08 +0000269 ADDCH(buf, *s++);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500270 while (len < field_width--)
Sonny Raocd55bde2011-11-02 09:52:08 +0000271 ADDCH(buf, ' ');
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500272 return buf;
273}
274
Rob Clark8adf37f2017-09-09 06:47:41 -0400275static char *string16(char *buf, char *end, u16 *s, int field_width,
276 int precision, int flags)
277{
278 u16 *str = s ? s : L"<NULL>";
279 int utf16_len = utf16_strnlen(str, precision);
280 u8 utf8[utf16_len * MAX_UTF8_PER_UTF16];
281 int utf8_len, i;
282
283 utf8_len = utf16_to_utf8(utf8, str, utf16_len) - utf8;
284
285 if (!(flags & LEFT))
286 while (utf8_len < field_width--)
287 ADDCH(buf, ' ');
288 for (i = 0; i < utf8_len; ++i)
289 ADDCH(buf, utf8[i]);
290 while (utf8_len < field_width--)
291 ADDCH(buf, ' ');
292 return buf;
293}
294
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500295#ifdef CONFIG_CMD_NET
Jeroen Hofstee46590552014-07-10 22:33:00 +0200296static const char hex_asc[] = "0123456789abcdef";
297#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
298#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
299
300static inline char *pack_hex_byte(char *buf, u8 byte)
301{
302 *buf++ = hex_asc_hi(byte);
303 *buf++ = hex_asc_lo(byte);
304 return buf;
305}
306
Sonny Raocd55bde2011-11-02 09:52:08 +0000307static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500308 int precision, int flags)
wdenk153d5112002-08-30 11:07:04 +0000309{
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000310 /* (6 * 2 hex digits), 5 colons and trailing zero */
311 char mac_addr[6 * 3];
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500312 char *p = mac_addr;
313 int i;
314
315 for (i = 0; i < 6; i++) {
316 p = pack_hex_byte(p, addr[i]);
317 if (!(flags & SPECIAL) && i != 5)
318 *p++ = ':';
319 }
320 *p = '\0';
321
Sonny Raocd55bde2011-11-02 09:52:08 +0000322 return string(buf, end, mac_addr, field_width, precision,
323 flags & ~SPECIAL);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500324}
325
Sonny Raocd55bde2011-11-02 09:52:08 +0000326static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500327 int precision, int flags)
328{
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000329 /* (8 * 4 hex digits), 7 colons and trailing zero */
330 char ip6_addr[8 * 5];
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500331 char *p = ip6_addr;
332 int i;
333
334 for (i = 0; i < 8; i++) {
335 p = pack_hex_byte(p, addr[2 * i]);
336 p = pack_hex_byte(p, addr[2 * i + 1]);
337 if (!(flags & SPECIAL) && i != 7)
338 *p++ = ':';
339 }
340 *p = '\0';
341
Sonny Raocd55bde2011-11-02 09:52:08 +0000342 return string(buf, end, ip6_addr, field_width, precision,
343 flags & ~SPECIAL);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500344}
345
Sonny Raocd55bde2011-11-02 09:52:08 +0000346static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500347 int precision, int flags)
348{
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000349 /* (4 * 3 decimal digits), 3 dots and trailing zero */
350 char ip4_addr[4 * 4];
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500351 char temp[3]; /* hold each IP quad in reverse order */
352 char *p = ip4_addr;
353 int i, digits;
354
355 for (i = 0; i < 4; i++) {
356 digits = put_dec_trunc(temp, addr[i]) - temp;
357 /* reverse the digits in the quad */
358 while (digits--)
359 *p++ = temp[digits];
360 if (i != 3)
361 *p++ = '.';
362 }
363 *p = '\0';
364
Sonny Raocd55bde2011-11-02 09:52:08 +0000365 return string(buf, end, ip4_addr, field_width, precision,
366 flags & ~SPECIAL);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500367}
368#endif
369
Rob Clarkc82342f2017-09-09 06:47:42 -0400370#ifdef CONFIG_LIB_UUID
371/*
372 * This works (roughly) the same way as linux's, but we currently always
373 * print lower-case (ie. we just keep %pUB and %pUL for compat with linux),
374 * mostly just because that is what uuid_bin_to_str() supports.
375 *
376 * %pUb: 01020304-0506-0708-090a-0b0c0d0e0f10
377 * %pUl: 04030201-0605-0807-090a-0b0c0d0e0f10
378 */
379static char *uuid_string(char *buf, char *end, u8 *addr, int field_width,
380 int precision, int flags, const char *fmt)
381{
382 char uuid[UUID_STR_LEN + 1];
383 int str_format = UUID_STR_FORMAT_STD;
384
385 switch (*(++fmt)) {
386 case 'L':
387 case 'l':
388 str_format = UUID_STR_FORMAT_GUID;
389 break;
390 case 'B':
391 case 'b':
392 /* this is the default */
393 break;
394 default:
395 break;
396 }
397
398 uuid_bin_to_str(addr, uuid, str_format);
399
400 return string(buf, end, uuid, field_width, precision, flags);
401}
402#endif
403
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500404/*
405 * Show a '%p' thing. A kernel extension is that the '%p' is followed
406 * by an extra set of alphanumeric characters that are extended format
407 * specifiers.
408 *
409 * Right now we handle:
410 *
411 * - 'M' For a 6-byte MAC address, it prints the address in the
412 * usual colon-separated hex notation
413 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
414 * decimal for v4 and colon separated network-order 16 bit hex for v6)
415 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
416 * currently the same
417 *
418 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
419 * function pointers are really function descriptors, which contain a
420 * pointer to the real address.
421 */
Sonny Raocd55bde2011-11-02 09:52:08 +0000422static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
423 int field_width, int precision, int flags)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500424{
Thierry Reding3e336c82014-11-12 18:26:47 -0700425 u64 num = (uintptr_t)ptr;
426
Wolfgang Denk88a8b012012-10-30 09:19:52 +0000427 /*
428 * Being a boot loader, we explicitly allow pointers to
429 * (physical) address null.
430 */
431#if 0
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500432 if (!ptr)
Sonny Raocd55bde2011-11-02 09:52:08 +0000433 return string(buf, end, "(null)", field_width, precision,
434 flags);
Wolfgang Denk88a8b012012-10-30 09:19:52 +0000435#endif
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500436
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500437 switch (*fmt) {
Rob Clarkc82342f2017-09-09 06:47:42 -0400438#ifdef CONFIG_CMD_NET
Thierry Reding3e336c82014-11-12 18:26:47 -0700439 case 'a':
440 flags |= SPECIAL | ZEROPAD;
441
442 switch (fmt[1]) {
443 case 'p':
444 default:
445 field_width = sizeof(phys_addr_t) * 2 + 2;
446 num = *(phys_addr_t *)ptr;
447 break;
448 }
449 break;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500450 case 'm':
451 flags |= SPECIAL;
452 /* Fallthrough */
453 case 'M':
Sonny Raocd55bde2011-11-02 09:52:08 +0000454 return mac_address_string(buf, end, ptr, field_width,
455 precision, flags);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500456 case 'i':
457 flags |= SPECIAL;
458 /* Fallthrough */
459 case 'I':
460 if (fmt[1] == '6')
Sonny Raocd55bde2011-11-02 09:52:08 +0000461 return ip6_addr_string(buf, end, ptr, field_width,
462 precision, flags);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500463 if (fmt[1] == '4')
Sonny Raocd55bde2011-11-02 09:52:08 +0000464 return ip4_addr_string(buf, end, ptr, field_width,
465 precision, flags);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500466 flags &= ~SPECIAL;
467 break;
wdenkf602aa02004-03-13 23:29:43 +0000468#endif
Rob Clarkc82342f2017-09-09 06:47:42 -0400469#ifdef CONFIG_LIB_UUID
470 case 'U':
471 return uuid_string(buf, end, ptr, field_width, precision,
472 flags, fmt);
473#endif
474 default:
475 break;
476 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500477 flags |= SMALL;
478 if (field_width == -1) {
479 field_width = 2*sizeof(void *);
480 flags |= ZEROPAD;
481 }
Thierry Reding3e336c82014-11-12 18:26:47 -0700482 return number(buf, end, num, 16, field_width, precision, flags);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500483}
484
Sonny Raocd55bde2011-11-02 09:52:08 +0000485static int vsnprintf_internal(char *buf, size_t size, const char *fmt,
486 va_list args)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500487{
Daniel Schwierzecka3e5c522012-09-16 06:55:04 +0000488 u64 num;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500489 int base;
490 char *str;
wdenk153d5112002-08-30 11:07:04 +0000491
492 int flags; /* flags to number() */
493
494 int field_width; /* width of output field */
495 int precision; /* min. # of digits for integers; max
496 number of chars for from string */
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500497 int qualifier; /* 'h', 'l', or 'L' for integer fields */
498 /* 'z' support added 23/7/1999 S.H. */
499 /* 'z' changed to 'Z' --davidm 1/25/99 */
500 /* 't' added for ptrdiff_t */
Sonny Raocd55bde2011-11-02 09:52:08 +0000501 char *end = buf + size;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500502
Sonny Raocd55bde2011-11-02 09:52:08 +0000503 /* Make sure end is always >= buf - do we want this in U-Boot? */
504 if (end < buf) {
505 end = ((void *)-1);
506 size = end - buf;
507 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500508 str = buf;
wdenk153d5112002-08-30 11:07:04 +0000509
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500510 for (; *fmt ; ++fmt) {
wdenk153d5112002-08-30 11:07:04 +0000511 if (*fmt != '%') {
Sonny Raocd55bde2011-11-02 09:52:08 +0000512 ADDCH(str, *fmt);
wdenk153d5112002-08-30 11:07:04 +0000513 continue;
514 }
515
516 /* process flags */
517 flags = 0;
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000518repeat:
wdenk153d5112002-08-30 11:07:04 +0000519 ++fmt; /* this also skips first '%' */
520 switch (*fmt) {
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000521 case '-':
522 flags |= LEFT;
523 goto repeat;
524 case '+':
525 flags |= PLUS;
526 goto repeat;
527 case ' ':
528 flags |= SPACE;
529 goto repeat;
530 case '#':
531 flags |= SPECIAL;
532 goto repeat;
533 case '0':
534 flags |= ZEROPAD;
535 goto repeat;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500536 }
wdenk153d5112002-08-30 11:07:04 +0000537
538 /* get field width */
539 field_width = -1;
540 if (is_digit(*fmt))
541 field_width = skip_atoi(&fmt);
542 else if (*fmt == '*') {
543 ++fmt;
544 /* it's the next argument */
545 field_width = va_arg(args, int);
546 if (field_width < 0) {
547 field_width = -field_width;
548 flags |= LEFT;
549 }
550 }
551
552 /* get the precision */
553 precision = -1;
554 if (*fmt == '.') {
555 ++fmt;
556 if (is_digit(*fmt))
557 precision = skip_atoi(&fmt);
558 else if (*fmt == '*') {
559 ++fmt;
560 /* it's the next argument */
561 precision = va_arg(args, int);
562 }
563 if (precision < 0)
564 precision = 0;
565 }
566
567 /* get the conversion qualifier */
568 qualifier = -1;
Jean-Christophe PLAGNIOL-VILLARD0f720692008-07-14 14:11:45 +0200569 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500570 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
wdenk153d5112002-08-30 11:07:04 +0000571 qualifier = *fmt;
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500572 ++fmt;
573 if (qualifier == 'l' && *fmt == 'l') {
574 qualifier = 'L';
James Yang7fc40152008-01-10 16:02:07 -0600575 ++fmt;
576 }
wdenk153d5112002-08-30 11:07:04 +0000577 }
578
579 /* default base */
580 base = 10;
581
582 switch (*fmt) {
583 case 'c':
Sonny Raocd55bde2011-11-02 09:52:08 +0000584 if (!(flags & LEFT)) {
wdenk153d5112002-08-30 11:07:04 +0000585 while (--field_width > 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000586 ADDCH(str, ' ');
587 }
588 ADDCH(str, (unsigned char) va_arg(args, int));
wdenk153d5112002-08-30 11:07:04 +0000589 while (--field_width > 0)
Sonny Raocd55bde2011-11-02 09:52:08 +0000590 ADDCH(str, ' ');
wdenk153d5112002-08-30 11:07:04 +0000591 continue;
592
593 case 's':
Rob Clark8adf37f2017-09-09 06:47:41 -0400594 if (qualifier == 'l' && !IS_ENABLED(CONFIG_SPL_BUILD)) {
595 str = string16(str, end, va_arg(args, u16 *),
596 field_width, precision, flags);
597 } else {
598 str = string(str, end, va_arg(args, char *),
599 field_width, precision, flags);
600 }
wdenk153d5112002-08-30 11:07:04 +0000601 continue;
602
603 case 'p':
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000604 str = pointer(fmt + 1, str, end,
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500605 va_arg(args, void *),
606 field_width, precision, flags);
607 /* Skip all alphanumeric pointer suffixes */
608 while (isalnum(fmt[1]))
609 fmt++;
wdenk153d5112002-08-30 11:07:04 +0000610 continue;
611
wdenk153d5112002-08-30 11:07:04 +0000612 case 'n':
613 if (qualifier == 'l') {
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000614 long *ip = va_arg(args, long *);
wdenk153d5112002-08-30 11:07:04 +0000615 *ip = (str - buf);
616 } else {
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000617 int *ip = va_arg(args, int *);
wdenk153d5112002-08-30 11:07:04 +0000618 *ip = (str - buf);
619 }
620 continue;
621
622 case '%':
Sonny Raocd55bde2011-11-02 09:52:08 +0000623 ADDCH(str, '%');
wdenk153d5112002-08-30 11:07:04 +0000624 continue;
625
626 /* integer number formats - set up the flags and "break" */
627 case 'o':
628 base = 8;
629 break;
630
wdenk153d5112002-08-30 11:07:04 +0000631 case 'x':
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500632 flags |= SMALL;
633 case 'X':
wdenk153d5112002-08-30 11:07:04 +0000634 base = 16;
635 break;
636
637 case 'd':
638 case 'i':
639 flags |= SIGN;
640 case 'u':
641 break;
642
643 default:
Sonny Raocd55bde2011-11-02 09:52:08 +0000644 ADDCH(str, '%');
wdenk153d5112002-08-30 11:07:04 +0000645 if (*fmt)
Sonny Raocd55bde2011-11-02 09:52:08 +0000646 ADDCH(str, *fmt);
wdenk153d5112002-08-30 11:07:04 +0000647 else
648 --fmt;
649 continue;
650 }
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500651 if (qualifier == 'L') /* "quad" for 64 bit variables */
wdenkf602aa02004-03-13 23:29:43 +0000652 num = va_arg(args, unsigned long long);
Heiko Schocher0f602e12009-12-03 11:21:21 +0100653 else if (qualifier == 'l') {
wdenk153d5112002-08-30 11:07:04 +0000654 num = va_arg(args, unsigned long);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500655 if (flags & SIGN)
656 num = (signed long) num;
Jean-Christophe PLAGNIOL-VILLARD0f720692008-07-14 14:11:45 +0200657 } else if (qualifier == 'Z' || qualifier == 'z') {
658 num = va_arg(args, size_t);
659 } else if (qualifier == 't') {
660 num = va_arg(args, ptrdiff_t);
661 } else if (qualifier == 'h') {
wdenk153d5112002-08-30 11:07:04 +0000662 num = (unsigned short) va_arg(args, int);
663 if (flags & SIGN)
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500664 num = (signed short) num;
665 } else {
wdenk153d5112002-08-30 11:07:04 +0000666 num = va_arg(args, unsigned int);
Mike Frysinger2b745dc2009-02-16 23:21:36 -0500667 if (flags & SIGN)
668 num = (signed int) num;
669 }
Sonny Raocd55bde2011-11-02 09:52:08 +0000670 str = number(str, end, num, base, field_width, precision,
671 flags);
672 }
673
Sonny Raocd55bde2011-11-02 09:52:08 +0000674 if (size > 0) {
675 ADDCH(str, '\0');
676 if (str > end)
677 end[-1] = '\0';
Darwin Rambo6958ede2013-12-19 15:14:19 -0800678 --str;
wdenk153d5112002-08-30 11:07:04 +0000679 }
Sonny Raocd55bde2011-11-02 09:52:08 +0000680 /* the trailing null byte doesn't count towards the total */
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000681 return str - buf;
wdenk153d5112002-08-30 11:07:04 +0000682}
683
Sonny Raocd55bde2011-11-02 09:52:08 +0000684int vsnprintf(char *buf, size_t size, const char *fmt,
685 va_list args)
686{
687 return vsnprintf_internal(buf, size, fmt, args);
688}
689
Sonny Raocd55bde2011-11-02 09:52:08 +0000690int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
691{
692 int i;
693
694 i = vsnprintf(buf, size, fmt, args);
695
696 if (likely(i < size))
697 return i;
698 if (size != 0)
699 return size - 1;
700 return 0;
701}
702
Sonny Raocd55bde2011-11-02 09:52:08 +0000703int snprintf(char *buf, size_t size, const char *fmt, ...)
704{
705 va_list args;
706 int i;
707
708 va_start(args, fmt);
709 i = vsnprintf(buf, size, fmt, args);
710 va_end(args);
711
712 return i;
713}
714
Sonny Raocd55bde2011-11-02 09:52:08 +0000715int scnprintf(char *buf, size_t size, const char *fmt, ...)
716{
717 va_list args;
718 int i;
719
720 va_start(args, fmt);
721 i = vscnprintf(buf, size, fmt, args);
722 va_end(args);
723
724 return i;
725}
Sonny Raocd55bde2011-11-02 09:52:08 +0000726
727/**
728 * Format a string and place it in a buffer (va_list version)
729 *
730 * @param buf The buffer to place the result into
731 * @param fmt The format string to use
732 * @param args Arguments for the format string
733 *
734 * The function returns the number of characters written
735 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
736 * buffer overflows.
737 *
738 * If you're not already dealing with a va_list consider using sprintf().
739 */
740int vsprintf(char *buf, const char *fmt, va_list args)
741{
742 return vsnprintf_internal(buf, INT_MAX, fmt, args);
743}
744
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000745int sprintf(char *buf, const char *fmt, ...)
wdenk153d5112002-08-30 11:07:04 +0000746{
747 va_list args;
748 int i;
749
750 va_start(args, fmt);
Daniel Schwierzeckfc147852012-09-16 06:55:03 +0000751 i = vsprintf(buf, fmt, args);
wdenk153d5112002-08-30 11:07:04 +0000752 va_end(args);
753 return i;
754}
755
Stefan Roese363ab7b2015-11-23 07:00:22 +0100756int printf(const char *fmt, ...)
757{
758 va_list args;
759 uint i;
760 char printbuffer[CONFIG_SYS_PBSIZE];
761
762 va_start(args, fmt);
763
764 /*
765 * For this to work, printbuffer must be larger than
766 * anything we ever want to print.
767 */
768 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
769 va_end(args);
770
771 /* Print the string */
772 puts(printbuffer);
773 return i;
774}
775
776int vprintf(const char *fmt, va_list args)
777{
778 uint i;
779 char printbuffer[CONFIG_SYS_PBSIZE];
780
781 /*
782 * For this to work, printbuffer must be larger than
783 * anything we ever want to print.
784 */
785 i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
786
787 /* Print the string */
788 puts(printbuffer);
789 return i;
790}
791
Simon Glass509dbf92015-02-27 22:06:32 -0700792
Simon Glass02aa9e62011-06-29 09:49:34 +0000793void __assert_fail(const char *assertion, const char *file, unsigned line,
794 const char *function)
795{
796 /* This will not return */
797 panic("%s:%u: %s: Assertion `%s' failed.", file, line, function,
798 assertion);
799}
Simon Glass931948a2011-10-21 18:51:33 +0000800
801char *simple_itoa(ulong i)
802{
803 /* 21 digits plus null terminator, good for 64-bit or smaller ints */
804 static char local[22];
805 char *p = &local[21];
806
807 *p-- = '\0';
808 do {
809 *p-- = '0' + i % 10;
810 i /= 10;
811 } while (i > 0);
812 return p + 1;
813}
Simon Glassb09cfd82013-06-11 11:14:38 -0700814
815/* We don't seem to have %'d in U-Boot */
816void print_grouped_ull(unsigned long long int_val, int digits)
817{
818 char str[21], *s;
819 int grab = 3;
820
821 digits = (digits + 2) / 3;
822 sprintf(str, "%*llu", digits * 3, int_val);
823 for (s = str; *s; s += grab) {
824 if (s != str)
825 putc(s[-1] != ' ' ? ',' : ' ');
826 printf("%.*s", grab, s);
827 grab = 3;
828 }
829}
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200830
831bool str2off(const char *p, loff_t *num)
832{
833 char *endptr;
834
835 *num = simple_strtoull(p, &endptr, 16);
836 return *p != '\0' && *endptr == '\0';
837}
838
839bool str2long(const char *p, ulong *num)
840{
841 char *endptr;
842
843 *num = simple_strtoul(p, &endptr, 16);
844 return *p != '\0' && *endptr == '\0';
845}