Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Tiny printf version for SPL |
| 3 | * |
| 4 | * Copied from: |
| 5 | * http://www.sparetimelabs.com/printfrevisited/printfrevisited.php |
| 6 | * |
| 7 | * Copyright (C) 2004,2008 Kustaa Nyholm |
| 8 | * |
| 9 | * SPDX-License-Identifier: LGPL-2.1+ |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <stdarg.h> |
| 14 | #include <serial.h> |
| 15 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 16 | struct printf_info { |
| 17 | char *bf; /* Digit buffer */ |
| 18 | char zs; /* non-zero if a digit has been written */ |
| 19 | char *outstr; /* Next output position for sprintf() */ |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 20 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 21 | /* Output a character */ |
| 22 | void (*putc)(struct printf_info *info, char ch); |
| 23 | }; |
Simon Glass | 49138f8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 24 | |
Masahiro Yamada | c1cb800 | 2017-02-12 18:08:43 +0900 | [diff] [blame] | 25 | static void putc_normal(struct printf_info *info, char ch) |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 26 | { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 27 | putc(ch); |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 28 | } |
| 29 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 30 | static void out(struct printf_info *info, char c) |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 31 | { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 32 | *info->bf++ = c; |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 33 | } |
| 34 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 35 | static void out_dgt(struct printf_info *info, char dgt) |
| 36 | { |
| 37 | out(info, dgt + (dgt < 10 ? '0' : 'a' - 10)); |
| 38 | info->zs = 1; |
| 39 | } |
| 40 | |
Andre Przywara | 3211868 | 2017-01-02 11:48:28 +0000 | [diff] [blame] | 41 | static void div_out(struct printf_info *info, unsigned long *num, |
| 42 | unsigned long div) |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 43 | { |
| 44 | unsigned char dgt = 0; |
| 45 | |
Stefan Roese | 26a552c | 2015-11-16 15:26:34 +0100 | [diff] [blame] | 46 | while (*num >= div) { |
| 47 | *num -= div; |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 48 | dgt++; |
| 49 | } |
| 50 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 51 | if (info->zs || dgt > 0) |
| 52 | out_dgt(info, dgt); |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Masahiro Yamada | c1cb800 | 2017-02-12 18:08:43 +0900 | [diff] [blame] | 55 | static int _vprintf(struct printf_info *info, const char *fmt, va_list va) |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 56 | { |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 57 | char ch; |
| 58 | char *p; |
Andre Przywara | 3211868 | 2017-01-02 11:48:28 +0000 | [diff] [blame] | 59 | unsigned long num; |
Stefan Roese | 26a552c | 2015-11-16 15:26:34 +0100 | [diff] [blame] | 60 | char buf[12]; |
Andre Przywara | 3211868 | 2017-01-02 11:48:28 +0000 | [diff] [blame] | 61 | unsigned long div; |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 62 | |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 63 | while ((ch = *(fmt++))) { |
| 64 | if (ch != '%') { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 65 | info->putc(info, ch); |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 66 | } else { |
Simon Glass | b5afcad | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 67 | bool lz = false; |
| 68 | int width = 0; |
Andre Przywara | 3211868 | 2017-01-02 11:48:28 +0000 | [diff] [blame] | 69 | bool islong = false; |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 70 | |
| 71 | ch = *(fmt++); |
Andre Przywara | 7b303b8 | 2017-01-02 11:48:29 +0000 | [diff] [blame] | 72 | if (ch == '-') |
| 73 | ch = *(fmt++); |
| 74 | |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 75 | if (ch == '0') { |
| 76 | ch = *(fmt++); |
| 77 | lz = 1; |
| 78 | } |
| 79 | |
| 80 | if (ch >= '0' && ch <= '9') { |
Simon Glass | b5afcad | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 81 | width = 0; |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 82 | while (ch >= '0' && ch <= '9') { |
Simon Glass | b5afcad | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 83 | width = (width * 10) + ch - '0'; |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 84 | ch = *fmt++; |
| 85 | } |
| 86 | } |
Andre Przywara | 3211868 | 2017-01-02 11:48:28 +0000 | [diff] [blame] | 87 | if (ch == 'l') { |
| 88 | ch = *(fmt++); |
| 89 | islong = true; |
| 90 | } |
| 91 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 92 | info->bf = buf; |
| 93 | p = info->bf; |
| 94 | info->zs = 0; |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 95 | |
| 96 | switch (ch) { |
Simon Glass | b5afcad | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 97 | case '\0': |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 98 | goto abort; |
| 99 | case 'u': |
| 100 | case 'd': |
Andre Przywara | 3211868 | 2017-01-02 11:48:28 +0000 | [diff] [blame] | 101 | div = 1000000000; |
| 102 | if (islong) { |
| 103 | num = va_arg(va, unsigned long); |
| 104 | if (sizeof(long) > 4) |
| 105 | div *= div * 10; |
| 106 | } else { |
| 107 | num = va_arg(va, unsigned int); |
| 108 | } |
| 109 | |
| 110 | if (ch == 'd') { |
| 111 | if (islong && (long)num < 0) { |
| 112 | num = -(long)num; |
| 113 | out(info, '-'); |
| 114 | } else if (!islong && (int)num < 0) { |
| 115 | num = -(int)num; |
| 116 | out(info, '-'); |
| 117 | } |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 118 | } |
Simon Glass | bdfb70b | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 119 | if (!num) { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 120 | out_dgt(info, 0); |
Simon Glass | bdfb70b | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 121 | } else { |
Andre Przywara | 3211868 | 2017-01-02 11:48:28 +0000 | [diff] [blame] | 122 | for (; div; div /= 10) |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 123 | div_out(info, &num, div); |
Simon Glass | bdfb70b | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 124 | } |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 125 | break; |
| 126 | case 'x': |
Andre Przywara | 3211868 | 2017-01-02 11:48:28 +0000 | [diff] [blame] | 127 | if (islong) { |
| 128 | num = va_arg(va, unsigned long); |
| 129 | div = 1UL << (sizeof(long) * 8 - 4); |
| 130 | } else { |
| 131 | num = va_arg(va, unsigned int); |
| 132 | div = 0x10000000; |
| 133 | } |
Simon Glass | bdfb70b | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 134 | if (!num) { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 135 | out_dgt(info, 0); |
Simon Glass | bdfb70b | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 136 | } else { |
Andre Przywara | 3211868 | 2017-01-02 11:48:28 +0000 | [diff] [blame] | 137 | for (; div; div /= 0x10) |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 138 | div_out(info, &num, div); |
Simon Glass | bdfb70b | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 139 | } |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 140 | break; |
| 141 | case 'c': |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 142 | out(info, (char)(va_arg(va, int))); |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 143 | break; |
| 144 | case 's': |
| 145 | p = va_arg(va, char*); |
| 146 | break; |
| 147 | case '%': |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 148 | out(info, '%'); |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 149 | default: |
| 150 | break; |
| 151 | } |
| 152 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 153 | *info->bf = 0; |
| 154 | info->bf = p; |
| 155 | while (*info->bf++ && width > 0) |
Simon Glass | b5afcad | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 156 | width--; |
| 157 | while (width-- > 0) |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 158 | info->putc(info, lz ? '0' : ' '); |
Simon Glass | 5bd9f6c | 2015-12-29 05:22:46 -0700 | [diff] [blame] | 159 | if (p) { |
| 160 | while ((ch = *p++)) |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 161 | info->putc(info, ch); |
Simon Glass | 5bd9f6c | 2015-12-29 05:22:46 -0700 | [diff] [blame] | 162 | } |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | abort: |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 167 | return 0; |
| 168 | } |
Sjoerd Simons | 1168ed5 | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 169 | |
Hans de Goede | 5f18398 | 2016-06-10 21:03:34 +0200 | [diff] [blame] | 170 | int vprintf(const char *fmt, va_list va) |
| 171 | { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 172 | struct printf_info info; |
| 173 | |
| 174 | info.putc = putc_normal; |
| 175 | return _vprintf(&info, fmt, va); |
Hans de Goede | 5f18398 | 2016-06-10 21:03:34 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Sjoerd Simons | 1168ed5 | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 178 | int printf(const char *fmt, ...) |
| 179 | { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 180 | struct printf_info info; |
| 181 | |
Sjoerd Simons | 1168ed5 | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 182 | va_list va; |
| 183 | int ret; |
| 184 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 185 | info.putc = putc_normal; |
Sjoerd Simons | 1168ed5 | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 186 | va_start(va, fmt); |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 187 | ret = _vprintf(&info, fmt, va); |
Simon Glass | 49138f8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 188 | va_end(va); |
| 189 | |
| 190 | return ret; |
| 191 | } |
| 192 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 193 | static void putc_outstr(struct printf_info *info, char ch) |
Simon Glass | 49138f8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 194 | { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 195 | *info->outstr++ = ch; |
Simon Glass | 49138f8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 196 | } |
| 197 | |
Marek Vasut | 8dc095b | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 198 | int sprintf(char *buf, const char *fmt, ...) |
Simon Glass | 49138f8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 199 | { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 200 | struct printf_info info; |
Simon Glass | 49138f8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 201 | va_list va; |
| 202 | int ret; |
| 203 | |
| 204 | va_start(va, fmt); |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 205 | info.outstr = buf; |
| 206 | info.putc = putc_outstr; |
| 207 | ret = _vprintf(&info, fmt, va); |
Sjoerd Simons | 1168ed5 | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 208 | va_end(va); |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 209 | *info.outstr = '\0'; |
Sjoerd Simons | 1168ed5 | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 210 | |
| 211 | return ret; |
| 212 | } |
Marek Vasut | 8dc095b | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 213 | |
| 214 | /* Note that size is ignored */ |
| 215 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
| 216 | { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 217 | struct printf_info info; |
Marek Vasut | 8dc095b | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 218 | va_list va; |
| 219 | int ret; |
| 220 | |
| 221 | va_start(va, fmt); |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 222 | info.outstr = buf; |
| 223 | info.putc = putc_outstr; |
| 224 | ret = _vprintf(&info, fmt, va); |
Marek Vasut | 8dc095b | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 225 | va_end(va); |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 226 | *info.outstr = '\0'; |
Marek Vasut | 8dc095b | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 227 | |
| 228 | return ret; |
| 229 | } |
Simon Glass | affe9ee | 2016-07-04 11:58:13 -0600 | [diff] [blame] | 230 | |
| 231 | void __assert_fail(const char *assertion, const char *file, unsigned line, |
| 232 | const char *function) |
| 233 | { |
| 234 | /* This will not return */ |
| 235 | printf("%s:%u: %s: Assertion `%s' failed.", file, line, function, |
| 236 | assertion); |
| 237 | hang(); |
| 238 | } |