wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 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 :-) |
Simon Glass | 931948a | 2011-10-21 18:51:33 +0000 | [diff] [blame] | 10 | * |
| 11 | * from hush: simple_itoa() was lifted from boa-0.93.15 |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <stdarg.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/ctype.h> |
Heiko Schocher | 8d4f35a | 2011-05-02 21:33:49 +0000 | [diff] [blame] | 18 | #include <errno.h> |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 19 | |
| 20 | #include <common.h> |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 21 | #if !defined(CONFIG_PANIC_HANG) |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 22 | #include <command.h> |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
Dirk Behme | 3d8759e | 2009-07-22 17:51:56 +0200 | [diff] [blame] | 25 | #include <div64.h> |
Dirk Behme | 3d8759e | 2009-07-22 17:51:56 +0200 | [diff] [blame] | 26 | #define noinline __attribute__((noinline)) |
| 27 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 28 | /* some reluctance to put this into a new limits.h, so it is here */ |
| 29 | #define INT_MAX ((int)(~0U>>1)) |
| 30 | |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 31 | unsigned long simple_strtoul(const char *cp, char **endp, |
| 32 | unsigned int base) |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 33 | { |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 34 | unsigned long result = 0; |
| 35 | unsigned long value; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 36 | |
| 37 | if (*cp == '0') { |
| 38 | cp++; |
| 39 | if ((*cp == 'x') && isxdigit(cp[1])) { |
| 40 | base = 16; |
| 41 | cp++; |
| 42 | } |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 43 | |
| 44 | if (!base) |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 45 | base = 8; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 46 | } |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 47 | |
| 48 | if (!base) |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 49 | base = 10; |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 50 | |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 51 | while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp-'0' : (islower(*cp) |
| 52 | ? toupper(*cp) : *cp)-'A'+10) < base) { |
| 53 | result = result*base + value; |
| 54 | cp++; |
| 55 | } |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 56 | |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 57 | if (endp) |
| 58 | *endp = (char *)cp; |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 59 | |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 60 | return result; |
| 61 | } |
| 62 | |
Heiko Schocher | 8d4f35a | 2011-05-02 21:33:49 +0000 | [diff] [blame] | 63 | int strict_strtoul(const char *cp, unsigned int base, unsigned long *res) |
| 64 | { |
| 65 | char *tail; |
| 66 | unsigned long val; |
| 67 | size_t len; |
| 68 | |
| 69 | *res = 0; |
| 70 | len = strlen(cp); |
| 71 | if (len == 0) |
| 72 | return -EINVAL; |
| 73 | |
| 74 | val = simple_strtoul(cp, &tail, base); |
| 75 | if (tail == cp) |
| 76 | return -EINVAL; |
| 77 | |
| 78 | if ((*tail == '\0') || |
| 79 | ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) { |
| 80 | *res = val; |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 87 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 88 | { |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 89 | if (*cp == '-') |
| 90 | return -simple_strtoul(cp + 1, endp, base); |
| 91 | |
| 92 | return simple_strtoul(cp, endp, base); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Lukasz Majewski | 99180d6 | 2012-12-11 11:09:42 +0100 | [diff] [blame] | 95 | unsigned long ustrtoul(const char *cp, char **endp, unsigned int base) |
Kyungmin Park | ad7ae36 | 2008-11-19 16:32:36 +0100 | [diff] [blame] | 96 | { |
| 97 | unsigned long result = simple_strtoul(cp, endp, base); |
| 98 | switch (**endp) { |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 99 | case 'G': |
Kyungmin Park | ad7ae36 | 2008-11-19 16:32:36 +0100 | [diff] [blame] | 100 | result *= 1024; |
| 101 | /* fall through */ |
| 102 | case 'M': |
| 103 | result *= 1024; |
| 104 | /* fall through */ |
| 105 | case 'K': |
| 106 | case 'k': |
| 107 | result *= 1024; |
| 108 | if ((*endp)[1] == 'i') { |
| 109 | if ((*endp)[2] == 'B') |
| 110 | (*endp) += 3; |
| 111 | else |
| 112 | (*endp) += 2; |
| 113 | } |
| 114 | } |
| 115 | return result; |
| 116 | } |
| 117 | |
Piotr Wilczek | 219ad3c | 2013-01-27 22:59:24 +0000 | [diff] [blame] | 118 | unsigned long long ustrtoull(const char *cp, char **endp, unsigned int base) |
| 119 | { |
| 120 | unsigned long long result = simple_strtoull(cp, endp, base); |
| 121 | switch (**endp) { |
| 122 | case 'G': |
| 123 | result *= 1024; |
| 124 | /* fall through */ |
| 125 | case 'M': |
| 126 | result *= 1024; |
| 127 | /* fall through */ |
| 128 | case 'K': |
| 129 | case 'k': |
| 130 | result *= 1024; |
| 131 | if ((*endp)[1] == 'i') { |
| 132 | if ((*endp)[2] == 'B') |
| 133 | (*endp) += 3; |
| 134 | else |
| 135 | (*endp) += 2; |
| 136 | } |
| 137 | } |
| 138 | return result; |
| 139 | } |
| 140 | |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 141 | unsigned long long simple_strtoull(const char *cp, char **endp, |
| 142 | unsigned int base) |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 143 | { |
| 144 | unsigned long long result = 0, value; |
| 145 | |
| 146 | if (*cp == '0') { |
| 147 | cp++; |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 148 | if ((*cp == 'x') && isxdigit(cp[1])) { |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 149 | base = 16; |
| 150 | cp++; |
| 151 | } |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 152 | |
| 153 | if (!base) |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 154 | base = 8; |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 155 | } |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 156 | |
| 157 | if (!base) |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 158 | base = 10; |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 159 | |
| 160 | while (isxdigit(*cp) && (value = isdigit(*cp) ? *cp - '0' |
| 161 | : (islower(*cp) ? toupper(*cp) : *cp) - 'A' + 10) < base) { |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 162 | result = result * base + value; |
| 163 | cp++; |
| 164 | } |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 165 | |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 166 | if (endp) |
| 167 | *endp = (char *) cp; |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 168 | |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 169 | return result; |
| 170 | } |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 171 | |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 172 | /* we use this so that we can do without the ctype library */ |
| 173 | #define is_digit(c) ((c) >= '0' && (c) <= '9') |
| 174 | |
| 175 | static int skip_atoi(const char **s) |
| 176 | { |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 177 | int i = 0; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 178 | |
| 179 | while (is_digit(**s)) |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 180 | i = i * 10 + *((*s)++) - '0'; |
| 181 | |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 182 | return i; |
| 183 | } |
| 184 | |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 185 | /* Decimal conversion is by far the most typical, and is used |
| 186 | * for /proc and /sys data. This directly impacts e.g. top performance |
| 187 | * with many processes running. We optimize it for speed |
| 188 | * using code from |
| 189 | * http://www.cs.uiowa.edu/~jones/bcd/decimal.html |
| 190 | * (with permission from the author, Douglas W. Jones). */ |
| 191 | |
| 192 | /* Formats correctly any integer in [0,99999]. |
| 193 | * Outputs from one to five digits depending on input. |
| 194 | * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 195 | static char *put_dec_trunc(char *buf, unsigned q) |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 196 | { |
| 197 | unsigned d3, d2, d1, d0; |
| 198 | d1 = (q>>4) & 0xf; |
| 199 | d2 = (q>>8) & 0xf; |
| 200 | d3 = (q>>12); |
| 201 | |
| 202 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
| 203 | q = (d0 * 0xcd) >> 11; |
| 204 | d0 = d0 - 10*q; |
| 205 | *buf++ = d0 + '0'; /* least significant digit */ |
| 206 | d1 = q + 9*d3 + 5*d2 + d1; |
| 207 | if (d1 != 0) { |
| 208 | q = (d1 * 0xcd) >> 11; |
| 209 | d1 = d1 - 10*q; |
| 210 | *buf++ = d1 + '0'; /* next digit */ |
| 211 | |
| 212 | d2 = q + 2*d2; |
| 213 | if ((d2 != 0) || (d3 != 0)) { |
| 214 | q = (d2 * 0xd) >> 7; |
| 215 | d2 = d2 - 10*q; |
| 216 | *buf++ = d2 + '0'; /* next digit */ |
| 217 | |
| 218 | d3 = q + 4*d3; |
| 219 | if (d3 != 0) { |
| 220 | q = (d3 * 0xcd) >> 11; |
| 221 | d3 = d3 - 10*q; |
| 222 | *buf++ = d3 + '0'; /* next digit */ |
| 223 | if (q != 0) |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 224 | *buf++ = q + '0'; /* most sign. digit */ |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | return buf; |
| 229 | } |
| 230 | /* Same with if's removed. Always emits five digits */ |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 231 | static char *put_dec_full(char *buf, unsigned q) |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 232 | { |
| 233 | /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ |
| 234 | /* but anyway, gcc produces better code with full-sized ints */ |
| 235 | unsigned d3, d2, d1, d0; |
| 236 | d1 = (q>>4) & 0xf; |
| 237 | d2 = (q>>8) & 0xf; |
| 238 | d3 = (q>>12); |
| 239 | |
Wolfgang Denk | e366f15 | 2009-04-05 00:27:57 +0200 | [diff] [blame] | 240 | /* |
| 241 | * Possible ways to approx. divide by 10 |
| 242 | * gcc -O2 replaces multiply with shifts and adds |
| 243 | * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) |
| 244 | * (x * 0x67) >> 10: 1100111 |
| 245 | * (x * 0x34) >> 9: 110100 - same |
| 246 | * (x * 0x1a) >> 8: 11010 - same |
| 247 | * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) |
| 248 | */ |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 249 | |
| 250 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
| 251 | q = (d0 * 0xcd) >> 11; |
| 252 | d0 = d0 - 10*q; |
| 253 | *buf++ = d0 + '0'; |
| 254 | d1 = q + 9*d3 + 5*d2 + d1; |
| 255 | q = (d1 * 0xcd) >> 11; |
| 256 | d1 = d1 - 10*q; |
| 257 | *buf++ = d1 + '0'; |
| 258 | |
| 259 | d2 = q + 2*d2; |
| 260 | q = (d2 * 0xd) >> 7; |
| 261 | d2 = d2 - 10*q; |
| 262 | *buf++ = d2 + '0'; |
| 263 | |
| 264 | d3 = q + 4*d3; |
| 265 | q = (d3 * 0xcd) >> 11; /* - shorter code */ |
| 266 | /* q = (d3 * 0x67) >> 10; - would also work */ |
| 267 | d3 = d3 - 10*q; |
| 268 | *buf++ = d3 + '0'; |
| 269 | *buf++ = q + '0'; |
| 270 | return buf; |
| 271 | } |
| 272 | /* No inlining helps gcc to use registers better */ |
Daniel Schwierzeck | a3e5c52 | 2012-09-16 06:55:04 +0000 | [diff] [blame] | 273 | static noinline char *put_dec(char *buf, u64 num) |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 274 | { |
| 275 | while (1) { |
| 276 | unsigned rem; |
| 277 | if (num < 100000) |
| 278 | return put_dec_trunc(buf, num); |
| 279 | rem = do_div(num, 100000); |
| 280 | buf = put_dec_full(buf, rem); |
| 281 | } |
| 282 | } |
| 283 | |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 284 | #define ZEROPAD 1 /* pad with zero */ |
| 285 | #define SIGN 2 /* unsigned/signed long */ |
| 286 | #define PLUS 4 /* show plus */ |
| 287 | #define SPACE 8 /* space if plus */ |
| 288 | #define LEFT 16 /* left justified */ |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 289 | #define SMALL 32 /* Must be 32 == 0x20 */ |
| 290 | #define SPECIAL 64 /* 0x */ |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 291 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 292 | #ifdef CONFIG_SYS_VSNPRINTF |
| 293 | /* |
| 294 | * Macro to add a new character to our output string, but only if it will |
| 295 | * fit. The macro moves to the next character position in the output string. |
| 296 | */ |
| 297 | #define ADDCH(str, ch) do { \ |
| 298 | if ((str) < end) \ |
| 299 | *(str) = (ch); \ |
| 300 | ++str; \ |
| 301 | } while (0) |
| 302 | #else |
| 303 | #define ADDCH(str, ch) (*(str)++ = (ch)) |
| 304 | #endif |
| 305 | |
Daniel Schwierzeck | a3e5c52 | 2012-09-16 06:55:04 +0000 | [diff] [blame] | 306 | static char *number(char *buf, char *end, u64 num, |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 307 | int base, int size, int precision, int type) |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 308 | { |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 309 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 310 | static const char digits[16] = "0123456789ABCDEF"; |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 311 | |
| 312 | char tmp[66]; |
| 313 | char sign; |
| 314 | char locase; |
| 315 | int need_pfx = ((type & SPECIAL) && base != 10); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 316 | int i; |
| 317 | |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 318 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
| 319 | * produces same digits or (maybe lowercased) letters */ |
| 320 | locase = (type & SMALL); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 321 | if (type & LEFT) |
| 322 | type &= ~ZEROPAD; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 323 | sign = 0; |
| 324 | if (type & SIGN) { |
Daniel Schwierzeck | a3e5c52 | 2012-09-16 06:55:04 +0000 | [diff] [blame] | 325 | if ((s64) num < 0) { |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 326 | sign = '-'; |
Daniel Schwierzeck | a3e5c52 | 2012-09-16 06:55:04 +0000 | [diff] [blame] | 327 | num = -(s64) num; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 328 | size--; |
| 329 | } else if (type & PLUS) { |
| 330 | sign = '+'; |
| 331 | size--; |
| 332 | } else if (type & SPACE) { |
| 333 | sign = ' '; |
| 334 | size--; |
| 335 | } |
| 336 | } |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 337 | if (need_pfx) { |
| 338 | size--; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 339 | if (base == 16) |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 340 | size--; |
| 341 | } |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 342 | |
| 343 | /* generate full string in tmp[], in reverse order */ |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 344 | i = 0; |
| 345 | if (num == 0) |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 346 | tmp[i++] = '0'; |
| 347 | /* Generic code, for any base: |
| 348 | else do { |
| 349 | tmp[i++] = (digits[do_div(num,base)] | locase); |
| 350 | } while (num != 0); |
| 351 | */ |
| 352 | else if (base != 10) { /* 8 or 16 */ |
| 353 | int mask = base - 1; |
| 354 | int shift = 3; |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 355 | |
| 356 | if (base == 16) |
| 357 | shift = 4; |
| 358 | |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 359 | do { |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 360 | tmp[i++] = (digits[((unsigned char)num) & mask] |
| 361 | | locase); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 362 | num >>= shift; |
| 363 | } while (num); |
| 364 | } else { /* base 10 */ |
| 365 | i = put_dec(tmp, num) - tmp; |
| 366 | } |
| 367 | |
| 368 | /* printing 100 using %2d gives "100", not "00" */ |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 369 | if (i > precision) |
| 370 | precision = i; |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 371 | /* leading space padding */ |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 372 | size -= precision; |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 373 | if (!(type & (ZEROPAD + LEFT))) { |
| 374 | while (--size >= 0) |
| 375 | ADDCH(buf, ' '); |
| 376 | } |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 377 | /* sign */ |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 378 | if (sign) |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 379 | ADDCH(buf, sign); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 380 | /* "0x" / "0" prefix */ |
| 381 | if (need_pfx) { |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 382 | ADDCH(buf, '0'); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 383 | if (base == 16) |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 384 | ADDCH(buf, 'X' | locase); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 385 | } |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 386 | /* zero or space padding */ |
| 387 | if (!(type & LEFT)) { |
| 388 | char c = (type & ZEROPAD) ? '0' : ' '; |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 389 | |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 390 | while (--size >= 0) |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 391 | ADDCH(buf, c); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 392 | } |
| 393 | /* hmm even more zero padding? */ |
| 394 | while (i <= --precision) |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 395 | ADDCH(buf, '0'); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 396 | /* actual digits of result */ |
| 397 | while (--i >= 0) |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 398 | ADDCH(buf, tmp[i]); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 399 | /* trailing space padding */ |
| 400 | while (--size >= 0) |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 401 | ADDCH(buf, ' '); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 402 | return buf; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 405 | static char *string(char *buf, char *end, char *s, int field_width, |
| 406 | int precision, int flags) |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 407 | { |
| 408 | int len, i; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 409 | |
Kim Phillips | 7554150 | 2012-10-29 13:34:36 +0000 | [diff] [blame] | 410 | if (s == NULL) |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 411 | s = "<NULL>"; |
| 412 | |
| 413 | len = strnlen(s, precision); |
| 414 | |
| 415 | if (!(flags & LEFT)) |
| 416 | while (len < field_width--) |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 417 | ADDCH(buf, ' '); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 418 | for (i = 0; i < len; ++i) |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 419 | ADDCH(buf, *s++); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 420 | while (len < field_width--) |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 421 | ADDCH(buf, ' '); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 422 | return buf; |
| 423 | } |
| 424 | |
| 425 | #ifdef CONFIG_CMD_NET |
Jeroen Hofstee | 4659055 | 2014-07-10 22:33:00 +0200 | [diff] [blame] | 426 | static const char hex_asc[] = "0123456789abcdef"; |
| 427 | #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] |
| 428 | #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] |
| 429 | |
| 430 | static inline char *pack_hex_byte(char *buf, u8 byte) |
| 431 | { |
| 432 | *buf++ = hex_asc_hi(byte); |
| 433 | *buf++ = hex_asc_lo(byte); |
| 434 | return buf; |
| 435 | } |
| 436 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 437 | static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width, |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 438 | int precision, int flags) |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 439 | { |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 440 | /* (6 * 2 hex digits), 5 colons and trailing zero */ |
| 441 | char mac_addr[6 * 3]; |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 442 | char *p = mac_addr; |
| 443 | int i; |
| 444 | |
| 445 | for (i = 0; i < 6; i++) { |
| 446 | p = pack_hex_byte(p, addr[i]); |
| 447 | if (!(flags & SPECIAL) && i != 5) |
| 448 | *p++ = ':'; |
| 449 | } |
| 450 | *p = '\0'; |
| 451 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 452 | return string(buf, end, mac_addr, field_width, precision, |
| 453 | flags & ~SPECIAL); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 454 | } |
| 455 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 456 | static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width, |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 457 | int precision, int flags) |
| 458 | { |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 459 | /* (8 * 4 hex digits), 7 colons and trailing zero */ |
| 460 | char ip6_addr[8 * 5]; |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 461 | char *p = ip6_addr; |
| 462 | int i; |
| 463 | |
| 464 | for (i = 0; i < 8; i++) { |
| 465 | p = pack_hex_byte(p, addr[2 * i]); |
| 466 | p = pack_hex_byte(p, addr[2 * i + 1]); |
| 467 | if (!(flags & SPECIAL) && i != 7) |
| 468 | *p++ = ':'; |
| 469 | } |
| 470 | *p = '\0'; |
| 471 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 472 | return string(buf, end, ip6_addr, field_width, precision, |
| 473 | flags & ~SPECIAL); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 474 | } |
| 475 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 476 | static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width, |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 477 | int precision, int flags) |
| 478 | { |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 479 | /* (4 * 3 decimal digits), 3 dots and trailing zero */ |
| 480 | char ip4_addr[4 * 4]; |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 481 | char temp[3]; /* hold each IP quad in reverse order */ |
| 482 | char *p = ip4_addr; |
| 483 | int i, digits; |
| 484 | |
| 485 | for (i = 0; i < 4; i++) { |
| 486 | digits = put_dec_trunc(temp, addr[i]) - temp; |
| 487 | /* reverse the digits in the quad */ |
| 488 | while (digits--) |
| 489 | *p++ = temp[digits]; |
| 490 | if (i != 3) |
| 491 | *p++ = '.'; |
| 492 | } |
| 493 | *p = '\0'; |
| 494 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 495 | return string(buf, end, ip4_addr, field_width, precision, |
| 496 | flags & ~SPECIAL); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 497 | } |
| 498 | #endif |
| 499 | |
| 500 | /* |
| 501 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
| 502 | * by an extra set of alphanumeric characters that are extended format |
| 503 | * specifiers. |
| 504 | * |
| 505 | * Right now we handle: |
| 506 | * |
| 507 | * - 'M' For a 6-byte MAC address, it prints the address in the |
| 508 | * usual colon-separated hex notation |
| 509 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated |
| 510 | * decimal for v4 and colon separated network-order 16 bit hex for v6) |
| 511 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is |
| 512 | * currently the same |
| 513 | * |
| 514 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
| 515 | * function pointers are really function descriptors, which contain a |
| 516 | * pointer to the real address. |
| 517 | */ |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 518 | static char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
| 519 | int field_width, int precision, int flags) |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 520 | { |
Wolfgang Denk | 88a8b01 | 2012-10-30 09:19:52 +0000 | [diff] [blame] | 521 | /* |
| 522 | * Being a boot loader, we explicitly allow pointers to |
| 523 | * (physical) address null. |
| 524 | */ |
| 525 | #if 0 |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 526 | if (!ptr) |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 527 | return string(buf, end, "(null)", field_width, precision, |
| 528 | flags); |
Wolfgang Denk | 88a8b01 | 2012-10-30 09:19:52 +0000 | [diff] [blame] | 529 | #endif |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 530 | |
| 531 | #ifdef CONFIG_CMD_NET |
| 532 | switch (*fmt) { |
| 533 | case 'm': |
| 534 | flags |= SPECIAL; |
| 535 | /* Fallthrough */ |
| 536 | case 'M': |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 537 | return mac_address_string(buf, end, ptr, field_width, |
| 538 | precision, flags); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 539 | case 'i': |
| 540 | flags |= SPECIAL; |
| 541 | /* Fallthrough */ |
| 542 | case 'I': |
| 543 | if (fmt[1] == '6') |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 544 | return ip6_addr_string(buf, end, ptr, field_width, |
| 545 | precision, flags); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 546 | if (fmt[1] == '4') |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 547 | return ip4_addr_string(buf, end, ptr, field_width, |
| 548 | precision, flags); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 549 | flags &= ~SPECIAL; |
| 550 | break; |
| 551 | } |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 552 | #endif |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 553 | flags |= SMALL; |
| 554 | if (field_width == -1) { |
| 555 | field_width = 2*sizeof(void *); |
| 556 | flags |= ZEROPAD; |
| 557 | } |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 558 | return number(buf, end, (unsigned long)ptr, 16, field_width, |
| 559 | precision, flags); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 560 | } |
| 561 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 562 | static int vsnprintf_internal(char *buf, size_t size, const char *fmt, |
| 563 | va_list args) |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 564 | { |
Daniel Schwierzeck | a3e5c52 | 2012-09-16 06:55:04 +0000 | [diff] [blame] | 565 | u64 num; |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 566 | int base; |
| 567 | char *str; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 568 | |
| 569 | int flags; /* flags to number() */ |
| 570 | |
| 571 | int field_width; /* width of output field */ |
| 572 | int precision; /* min. # of digits for integers; max |
| 573 | number of chars for from string */ |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 574 | int qualifier; /* 'h', 'l', or 'L' for integer fields */ |
| 575 | /* 'z' support added 23/7/1999 S.H. */ |
| 576 | /* 'z' changed to 'Z' --davidm 1/25/99 */ |
| 577 | /* 't' added for ptrdiff_t */ |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 578 | char *end = buf + size; |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 579 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 580 | #ifdef CONFIG_SYS_VSNPRINTF |
| 581 | /* Make sure end is always >= buf - do we want this in U-Boot? */ |
| 582 | if (end < buf) { |
| 583 | end = ((void *)-1); |
| 584 | size = end - buf; |
| 585 | } |
| 586 | #endif |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 587 | str = buf; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 588 | |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 589 | for (; *fmt ; ++fmt) { |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 590 | if (*fmt != '%') { |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 591 | ADDCH(str, *fmt); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 592 | continue; |
| 593 | } |
| 594 | |
| 595 | /* process flags */ |
| 596 | flags = 0; |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 597 | repeat: |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 598 | ++fmt; /* this also skips first '%' */ |
| 599 | switch (*fmt) { |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 600 | case '-': |
| 601 | flags |= LEFT; |
| 602 | goto repeat; |
| 603 | case '+': |
| 604 | flags |= PLUS; |
| 605 | goto repeat; |
| 606 | case ' ': |
| 607 | flags |= SPACE; |
| 608 | goto repeat; |
| 609 | case '#': |
| 610 | flags |= SPECIAL; |
| 611 | goto repeat; |
| 612 | case '0': |
| 613 | flags |= ZEROPAD; |
| 614 | goto repeat; |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 615 | } |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 616 | |
| 617 | /* get field width */ |
| 618 | field_width = -1; |
| 619 | if (is_digit(*fmt)) |
| 620 | field_width = skip_atoi(&fmt); |
| 621 | else if (*fmt == '*') { |
| 622 | ++fmt; |
| 623 | /* it's the next argument */ |
| 624 | field_width = va_arg(args, int); |
| 625 | if (field_width < 0) { |
| 626 | field_width = -field_width; |
| 627 | flags |= LEFT; |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | /* get the precision */ |
| 632 | precision = -1; |
| 633 | if (*fmt == '.') { |
| 634 | ++fmt; |
| 635 | if (is_digit(*fmt)) |
| 636 | precision = skip_atoi(&fmt); |
| 637 | else if (*fmt == '*') { |
| 638 | ++fmt; |
| 639 | /* it's the next argument */ |
| 640 | precision = va_arg(args, int); |
| 641 | } |
| 642 | if (precision < 0) |
| 643 | precision = 0; |
| 644 | } |
| 645 | |
| 646 | /* get the conversion qualifier */ |
| 647 | qualifier = -1; |
Jean-Christophe PLAGNIOL-VILLARD | 0f72069 | 2008-07-14 14:11:45 +0200 | [diff] [blame] | 648 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 649 | *fmt == 'Z' || *fmt == 'z' || *fmt == 't') { |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 650 | qualifier = *fmt; |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 651 | ++fmt; |
| 652 | if (qualifier == 'l' && *fmt == 'l') { |
| 653 | qualifier = 'L'; |
James Yang | 7fc4015 | 2008-01-10 16:02:07 -0600 | [diff] [blame] | 654 | ++fmt; |
| 655 | } |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /* default base */ |
| 659 | base = 10; |
| 660 | |
| 661 | switch (*fmt) { |
| 662 | case 'c': |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 663 | if (!(flags & LEFT)) { |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 664 | while (--field_width > 0) |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 665 | ADDCH(str, ' '); |
| 666 | } |
| 667 | ADDCH(str, (unsigned char) va_arg(args, int)); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 668 | while (--field_width > 0) |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 669 | ADDCH(str, ' '); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 670 | continue; |
| 671 | |
| 672 | case 's': |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 673 | str = string(str, end, va_arg(args, char *), |
| 674 | field_width, precision, flags); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 675 | continue; |
| 676 | |
| 677 | case 'p': |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 678 | str = pointer(fmt + 1, str, end, |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 679 | va_arg(args, void *), |
| 680 | field_width, precision, flags); |
| 681 | /* Skip all alphanumeric pointer suffixes */ |
| 682 | while (isalnum(fmt[1])) |
| 683 | fmt++; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 684 | continue; |
| 685 | |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 686 | case 'n': |
| 687 | if (qualifier == 'l') { |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 688 | long *ip = va_arg(args, long *); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 689 | *ip = (str - buf); |
| 690 | } else { |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 691 | int *ip = va_arg(args, int *); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 692 | *ip = (str - buf); |
| 693 | } |
| 694 | continue; |
| 695 | |
| 696 | case '%': |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 697 | ADDCH(str, '%'); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 698 | continue; |
| 699 | |
| 700 | /* integer number formats - set up the flags and "break" */ |
| 701 | case 'o': |
| 702 | base = 8; |
| 703 | break; |
| 704 | |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 705 | case 'x': |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 706 | flags |= SMALL; |
| 707 | case 'X': |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 708 | base = 16; |
| 709 | break; |
| 710 | |
| 711 | case 'd': |
| 712 | case 'i': |
| 713 | flags |= SIGN; |
| 714 | case 'u': |
| 715 | break; |
| 716 | |
| 717 | default: |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 718 | ADDCH(str, '%'); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 719 | if (*fmt) |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 720 | ADDCH(str, *fmt); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 721 | else |
| 722 | --fmt; |
| 723 | continue; |
| 724 | } |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 725 | if (qualifier == 'L') /* "quad" for 64 bit variables */ |
wdenk | f602aa0 | 2004-03-13 23:29:43 +0000 | [diff] [blame] | 726 | num = va_arg(args, unsigned long long); |
Heiko Schocher | 0f602e1 | 2009-12-03 11:21:21 +0100 | [diff] [blame] | 727 | else if (qualifier == 'l') { |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 728 | num = va_arg(args, unsigned long); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 729 | if (flags & SIGN) |
| 730 | num = (signed long) num; |
Jean-Christophe PLAGNIOL-VILLARD | 0f72069 | 2008-07-14 14:11:45 +0200 | [diff] [blame] | 731 | } else if (qualifier == 'Z' || qualifier == 'z') { |
| 732 | num = va_arg(args, size_t); |
| 733 | } else if (qualifier == 't') { |
| 734 | num = va_arg(args, ptrdiff_t); |
| 735 | } else if (qualifier == 'h') { |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 736 | num = (unsigned short) va_arg(args, int); |
| 737 | if (flags & SIGN) |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 738 | num = (signed short) num; |
| 739 | } else { |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 740 | num = va_arg(args, unsigned int); |
Mike Frysinger | 2b745dc | 2009-02-16 23:21:36 -0500 | [diff] [blame] | 741 | if (flags & SIGN) |
| 742 | num = (signed int) num; |
| 743 | } |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 744 | str = number(str, end, num, base, field_width, precision, |
| 745 | flags); |
| 746 | } |
| 747 | |
| 748 | #ifdef CONFIG_SYS_VSNPRINTF |
| 749 | if (size > 0) { |
| 750 | ADDCH(str, '\0'); |
| 751 | if (str > end) |
| 752 | end[-1] = '\0'; |
Darwin Rambo | 6958ede | 2013-12-19 15:14:19 -0800 | [diff] [blame] | 753 | --str; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 754 | } |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 755 | #else |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 756 | *str = '\0'; |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 757 | #endif |
| 758 | /* the trailing null byte doesn't count towards the total */ |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 759 | return str - buf; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 762 | #ifdef CONFIG_SYS_VSNPRINTF |
| 763 | int vsnprintf(char *buf, size_t size, const char *fmt, |
| 764 | va_list args) |
| 765 | { |
| 766 | return vsnprintf_internal(buf, size, fmt, args); |
| 767 | } |
| 768 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 769 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 770 | { |
| 771 | int i; |
| 772 | |
| 773 | i = vsnprintf(buf, size, fmt, args); |
| 774 | |
| 775 | if (likely(i < size)) |
| 776 | return i; |
| 777 | if (size != 0) |
| 778 | return size - 1; |
| 779 | return 0; |
| 780 | } |
| 781 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 782 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
| 783 | { |
| 784 | va_list args; |
| 785 | int i; |
| 786 | |
| 787 | va_start(args, fmt); |
| 788 | i = vsnprintf(buf, size, fmt, args); |
| 789 | va_end(args); |
| 790 | |
| 791 | return i; |
| 792 | } |
| 793 | |
Sonny Rao | cd55bde | 2011-11-02 09:52:08 +0000 | [diff] [blame] | 794 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
| 795 | { |
| 796 | va_list args; |
| 797 | int i; |
| 798 | |
| 799 | va_start(args, fmt); |
| 800 | i = vscnprintf(buf, size, fmt, args); |
| 801 | va_end(args); |
| 802 | |
| 803 | return i; |
| 804 | } |
| 805 | #endif /* CONFIG_SYS_VSNPRINT */ |
| 806 | |
| 807 | /** |
| 808 | * Format a string and place it in a buffer (va_list version) |
| 809 | * |
| 810 | * @param buf The buffer to place the result into |
| 811 | * @param fmt The format string to use |
| 812 | * @param args Arguments for the format string |
| 813 | * |
| 814 | * The function returns the number of characters written |
| 815 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
| 816 | * buffer overflows. |
| 817 | * |
| 818 | * If you're not already dealing with a va_list consider using sprintf(). |
| 819 | */ |
| 820 | int vsprintf(char *buf, const char *fmt, va_list args) |
| 821 | { |
| 822 | return vsnprintf_internal(buf, INT_MAX, fmt, args); |
| 823 | } |
| 824 | |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 825 | int sprintf(char *buf, const char *fmt, ...) |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 826 | { |
| 827 | va_list args; |
| 828 | int i; |
| 829 | |
| 830 | va_start(args, fmt); |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 831 | i = vsprintf(buf, fmt, args); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 832 | va_end(args); |
| 833 | return i; |
| 834 | } |
| 835 | |
| 836 | void panic(const char *fmt, ...) |
| 837 | { |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 838 | va_list args; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 839 | va_start(args, fmt); |
wdenk | b00ec16 | 2003-06-19 23:40:20 +0000 | [diff] [blame] | 840 | vprintf(fmt, args); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 841 | putc('\n'); |
| 842 | va_end(args); |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 843 | #if defined(CONFIG_PANIC_HANG) |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 844 | hang(); |
| 845 | #else |
Daniel Schwierzeck | fc14785 | 2012-09-16 06:55:03 +0000 | [diff] [blame] | 846 | udelay(100000); /* allow messages to go out */ |
| 847 | do_reset(NULL, 0, 0, NULL); |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 848 | #endif |
Heiko Schocher | 8cce81f | 2011-07-26 20:55:57 +0000 | [diff] [blame] | 849 | while (1) |
| 850 | ; |
wdenk | 153d511 | 2002-08-30 11:07:04 +0000 | [diff] [blame] | 851 | } |
Simon Glass | 02aa9e6 | 2011-06-29 09:49:34 +0000 | [diff] [blame] | 852 | |
| 853 | void __assert_fail(const char *assertion, const char *file, unsigned line, |
| 854 | const char *function) |
| 855 | { |
| 856 | /* This will not return */ |
| 857 | panic("%s:%u: %s: Assertion `%s' failed.", file, line, function, |
| 858 | assertion); |
| 859 | } |
Simon Glass | 931948a | 2011-10-21 18:51:33 +0000 | [diff] [blame] | 860 | |
| 861 | char *simple_itoa(ulong i) |
| 862 | { |
| 863 | /* 21 digits plus null terminator, good for 64-bit or smaller ints */ |
| 864 | static char local[22]; |
| 865 | char *p = &local[21]; |
| 866 | |
| 867 | *p-- = '\0'; |
| 868 | do { |
| 869 | *p-- = '0' + i % 10; |
| 870 | i /= 10; |
| 871 | } while (i > 0); |
| 872 | return p + 1; |
| 873 | } |
Simon Glass | b09cfd8 | 2013-06-11 11:14:38 -0700 | [diff] [blame] | 874 | |
| 875 | /* We don't seem to have %'d in U-Boot */ |
| 876 | void print_grouped_ull(unsigned long long int_val, int digits) |
| 877 | { |
| 878 | char str[21], *s; |
| 879 | int grab = 3; |
| 880 | |
| 881 | digits = (digits + 2) / 3; |
| 882 | sprintf(str, "%*llu", digits * 3, int_val); |
| 883 | for (s = str; *s; s += grab) { |
| 884 | if (s != str) |
| 885 | putc(s[-1] != ' ' ? ',' : ' '); |
| 886 | printf("%.*s", grab, s); |
| 887 | grab = 3; |
| 888 | } |
| 889 | } |