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