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