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