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