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