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