Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 1 | /* vtxprintf.c, from |
| 2 | * linux/lib/vsprintf.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
Stefan Reinauer | 3aa067f | 2012-04-02 13:24:04 -0700 | [diff] [blame] | 7 | #include <console/console.h> |
Stefan Reinauer | 52fc6b12 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 8 | #include <console/vtxprintf.h> |
Edward O'Callaghan | 0ddb826 | 2014-06-17 18:37:08 +1000 | [diff] [blame] | 9 | #include <string.h> |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 10 | |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 11 | #define call_tx(x) tx_byte(x, data) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 12 | |
Vadim Bendebury | 9b058467 | 2014-11-29 22:05:09 -0800 | [diff] [blame] | 13 | #if !CONFIG_ARCH_MIPS |
| 14 | #define SUPPORT_64BIT_INTS |
| 15 | #endif |
| 16 | |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 17 | /* haha, don't need ctype.c */ |
| 18 | #define isdigit(c) ((c) >= '0' && (c) <= '9') |
| 19 | #define is_digit isdigit |
| 20 | #define isxdigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F')) |
| 21 | |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 22 | static int skip_atoi(const char **s) |
| 23 | { |
| 24 | int i=0; |
| 25 | |
| 26 | while (is_digit(**s)) |
| 27 | i = i*10 + *((*s)++) - '0'; |
| 28 | return i; |
| 29 | } |
| 30 | |
| 31 | #define ZEROPAD 1 /* pad with zero */ |
| 32 | #define SIGN 2 /* unsigned/signed long */ |
| 33 | #define PLUS 4 /* show plus */ |
| 34 | #define SPACE 8 /* space if plus */ |
| 35 | #define LEFT 16 /* left justified */ |
| 36 | #define SPECIAL 32 /* 0x */ |
| 37 | #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */ |
| 38 | |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 39 | static int number(void (*tx_byte)(unsigned char byte, void *data), |
Vadim Bendebury | 9b058467 | 2014-11-29 22:05:09 -0800 | [diff] [blame] | 40 | unsigned long long inum, int base, int size, int precision, int type, |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 41 | void *data) |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 42 | { |
| 43 | char c,sign,tmp[66]; |
| 44 | const char *digits="0123456789abcdefghijklmnopqrstuvwxyz"; |
| 45 | int i; |
| 46 | int count = 0; |
Vadim Bendebury | 9b058467 | 2014-11-29 22:05:09 -0800 | [diff] [blame] | 47 | #ifdef SUPPORT_64BIT_INTS |
| 48 | unsigned long long num = inum; |
| 49 | #else |
| 50 | unsigned long num = (long)inum; |
| 51 | |
| 52 | if (num != inum) { |
| 53 | /* Alert user to an incorrect result by printing #^!. */ |
| 54 | call_tx('#'); |
| 55 | call_tx('^'); |
| 56 | call_tx('!'); |
| 57 | } |
| 58 | #endif |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 59 | |
| 60 | if (type & LARGE) |
| 61 | digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 62 | if (type & LEFT) |
| 63 | type &= ~ZEROPAD; |
| 64 | if (base < 2 || base > 36) |
| 65 | return 0; |
| 66 | c = (type & ZEROPAD) ? '0' : ' '; |
| 67 | sign = 0; |
| 68 | if (type & SIGN) { |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 69 | if ((signed long long)num < 0) { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 70 | sign = '-'; |
| 71 | num = -num; |
| 72 | size--; |
| 73 | } else if (type & PLUS) { |
| 74 | sign = '+'; |
| 75 | size--; |
| 76 | } else if (type & SPACE) { |
| 77 | sign = ' '; |
| 78 | size--; |
| 79 | } |
| 80 | } |
| 81 | if (type & SPECIAL) { |
| 82 | if (base == 16) |
| 83 | size -= 2; |
| 84 | else if (base == 8) |
| 85 | size--; |
| 86 | } |
| 87 | i = 0; |
| 88 | if (num == 0) |
| 89 | tmp[i++]='0'; |
Ronald G. Minnich | 79e36d9 | 2013-01-30 14:29:34 -0800 | [diff] [blame] | 90 | else while (num != 0){ |
David Hendricks | ae0e8d3 | 2013-03-06 20:43:55 -0800 | [diff] [blame] | 91 | tmp[i++] = digits[num % base]; |
| 92 | num /= base; |
Ronald G. Minnich | 79e36d9 | 2013-01-30 14:29:34 -0800 | [diff] [blame] | 93 | } |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 94 | if (i > precision) |
| 95 | precision = i; |
| 96 | size -= precision; |
| 97 | if (!(type&(ZEROPAD+LEFT))) |
| 98 | while(size-->0) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 99 | call_tx(' '), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 100 | if (sign) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 101 | call_tx(sign), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 102 | if (type & SPECIAL) { |
| 103 | if (base==8) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 104 | call_tx('0'), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 105 | else if (base==16) { |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 106 | call_tx('0'), count++; |
| 107 | call_tx(digits[33]), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | if (!(type & LEFT)) |
| 111 | while (size-- > 0) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 112 | call_tx(c), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 113 | while (i < precision--) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 114 | call_tx('0'), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 115 | while (i-- > 0) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 116 | call_tx(tmp[i]), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 117 | while (size-- > 0) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 118 | call_tx(' '), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 119 | return count; |
| 120 | } |
| 121 | |
| 122 | |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 123 | int vtxprintf(void (*tx_byte)(unsigned char byte, void *data), |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 124 | const char *fmt, va_list args, void *data) |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 125 | { |
| 126 | int len; |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 127 | unsigned long long num; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 128 | int i, base; |
| 129 | const char *s; |
| 130 | |
| 131 | int flags; /* flags to number() */ |
| 132 | |
| 133 | int field_width; /* width of output field */ |
| 134 | int precision; /* min. # of digits for integers; max |
| 135 | number of chars for from string */ |
Patrick Georgi | d01ed75 | 2014-01-18 16:56:36 +0100 | [diff] [blame] | 136 | int qualifier; /* 'h', 'H', 'l', or 'L' for integer fields */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 137 | |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 138 | int count; |
| 139 | |
| 140 | for (count=0; *fmt ; ++fmt) { |
| 141 | if (*fmt != '%') { |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 142 | call_tx(*fmt), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 143 | continue; |
| 144 | } |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 145 | |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 146 | /* process flags */ |
| 147 | flags = 0; |
Patrick Georgi | c5fc7db | 2012-03-07 15:55:47 +0100 | [diff] [blame] | 148 | repeat: |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 149 | ++fmt; /* this also skips first '%' */ |
| 150 | switch (*fmt) { |
| 151 | case '-': flags |= LEFT; goto repeat; |
| 152 | case '+': flags |= PLUS; goto repeat; |
| 153 | case ' ': flags |= SPACE; goto repeat; |
| 154 | case '#': flags |= SPECIAL; goto repeat; |
| 155 | case '0': flags |= ZEROPAD; goto repeat; |
| 156 | } |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 157 | |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 158 | /* get field width */ |
| 159 | field_width = -1; |
| 160 | if (is_digit(*fmt)) |
| 161 | field_width = skip_atoi(&fmt); |
| 162 | else if (*fmt == '*') { |
| 163 | ++fmt; |
| 164 | /* it's the next argument */ |
| 165 | field_width = va_arg(args, int); |
| 166 | if (field_width < 0) { |
| 167 | field_width = -field_width; |
| 168 | flags |= LEFT; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /* get the precision */ |
| 173 | precision = -1; |
| 174 | if (*fmt == '.') { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 175 | ++fmt; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 176 | if (is_digit(*fmt)) |
| 177 | precision = skip_atoi(&fmt); |
| 178 | else if (*fmt == '*') { |
| 179 | ++fmt; |
| 180 | /* it's the next argument */ |
| 181 | precision = va_arg(args, int); |
| 182 | } |
| 183 | if (precision < 0) |
| 184 | precision = 0; |
| 185 | } |
| 186 | |
| 187 | /* get the conversion qualifier */ |
| 188 | qualifier = -1; |
Stefan Reinauer | bfff6de | 2012-05-15 13:28:07 -0700 | [diff] [blame] | 189 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'z') { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 190 | qualifier = *fmt; |
| 191 | ++fmt; |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 192 | if (*fmt == 'l') { |
| 193 | qualifier = 'L'; |
| 194 | ++fmt; |
| 195 | } |
Patrick Georgi | d01ed75 | 2014-01-18 16:56:36 +0100 | [diff] [blame] | 196 | if (*fmt == 'h') { |
| 197 | qualifier = 'H'; |
| 198 | ++fmt; |
| 199 | } |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* default base */ |
| 203 | base = 10; |
| 204 | |
| 205 | switch (*fmt) { |
| 206 | case 'c': |
| 207 | if (!(flags & LEFT)) |
| 208 | while (--field_width > 0) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 209 | call_tx(' '), count++; |
| 210 | call_tx((unsigned char) va_arg(args, int)), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 211 | while (--field_width > 0) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 212 | call_tx(' '), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 213 | continue; |
| 214 | |
| 215 | case 's': |
| 216 | s = va_arg(args, char *); |
| 217 | if (!s) |
| 218 | s = "<NULL>"; |
| 219 | |
| 220 | len = strnlen(s, precision); |
| 221 | |
| 222 | if (!(flags & LEFT)) |
| 223 | while (len < field_width--) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 224 | call_tx(' '), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 225 | for (i = 0; i < len; ++i) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 226 | call_tx(*s++), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 227 | while (len < field_width--) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 228 | call_tx(' '), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 229 | continue; |
| 230 | |
| 231 | case 'p': |
| 232 | if (field_width == -1) { |
| 233 | field_width = 2*sizeof(void *); |
| 234 | flags |= ZEROPAD; |
| 235 | } |
| 236 | count += number(tx_byte, |
| 237 | (unsigned long) va_arg(args, void *), 16, |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 238 | field_width, precision, flags, data); |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 239 | continue; |
| 240 | |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 241 | case 'n': |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 242 | if (qualifier == 'L') { |
| 243 | long long *ip = va_arg(args, long long *); |
| 244 | *ip = count; |
| 245 | } else if (qualifier == 'l') { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 246 | long * ip = va_arg(args, long *); |
| 247 | *ip = count; |
| 248 | } else { |
| 249 | int * ip = va_arg(args, int *); |
| 250 | *ip = count; |
| 251 | } |
| 252 | continue; |
| 253 | |
| 254 | case '%': |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 255 | call_tx('%'), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 256 | continue; |
| 257 | |
| 258 | /* integer number formats - set up the flags and "break" */ |
| 259 | case 'o': |
| 260 | base = 8; |
| 261 | break; |
| 262 | |
| 263 | case 'X': |
| 264 | flags |= LARGE; |
| 265 | case 'x': |
| 266 | base = 16; |
| 267 | break; |
| 268 | |
| 269 | case 'd': |
| 270 | case 'i': |
| 271 | flags |= SIGN; |
| 272 | case 'u': |
| 273 | break; |
| 274 | |
| 275 | default: |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 276 | call_tx('%'), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 277 | if (*fmt) |
Vladimir Serbinenko | f421b33 | 2013-11-26 21:43:05 +0100 | [diff] [blame] | 278 | call_tx(*fmt), count++; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 279 | else |
| 280 | --fmt; |
| 281 | continue; |
| 282 | } |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 283 | if (qualifier == 'L') { |
| 284 | num = va_arg(args, unsigned long long); |
| 285 | } else if (qualifier == 'l') { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 286 | num = va_arg(args, unsigned long); |
Stefan Reinauer | bfff6de | 2012-05-15 13:28:07 -0700 | [diff] [blame] | 287 | } else if (qualifier == 'z') { |
| 288 | num = va_arg(args, size_t); |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 289 | } else if (qualifier == 'h') { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 290 | num = (unsigned short) va_arg(args, int); |
| 291 | if (flags & SIGN) |
| 292 | num = (short) num; |
Patrick Georgi | d01ed75 | 2014-01-18 16:56:36 +0100 | [diff] [blame] | 293 | } else if (qualifier == 'H') { |
| 294 | num = (unsigned char) va_arg(args, int); |
| 295 | if (flags & SIGN) |
| 296 | num = (signed char) num; |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 297 | } else if (flags & SIGN) { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 298 | num = va_arg(args, int); |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 299 | } else { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 300 | num = va_arg(args, unsigned int); |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 301 | } |
Kyösti Mälkki | b04e0ff | 2014-02-04 14:28:17 +0200 | [diff] [blame] | 302 | count += number(tx_byte, num, base, field_width, precision, flags, data); |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 303 | } |
| 304 | return count; |
| 305 | } |