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 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 25 | 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 | |
| 41 | static void div_out(struct printf_info *info, unsigned int *num, |
| 42 | unsigned int 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 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 55 | 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; |
Stefan Roese | 26a552c | 2015-11-16 15:26:34 +0100 | [diff] [blame] | 59 | unsigned int num; |
| 60 | char buf[12]; |
| 61 | unsigned int 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; |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 69 | |
| 70 | ch = *(fmt++); |
| 71 | if (ch == '0') { |
| 72 | ch = *(fmt++); |
| 73 | lz = 1; |
| 74 | } |
| 75 | |
| 76 | if (ch >= '0' && ch <= '9') { |
Simon Glass | b5afcad | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 77 | width = 0; |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 78 | while (ch >= '0' && ch <= '9') { |
Simon Glass | b5afcad | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 79 | width = (width * 10) + ch - '0'; |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 80 | ch = *fmt++; |
| 81 | } |
| 82 | } |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 83 | info->bf = buf; |
| 84 | p = info->bf; |
| 85 | info->zs = 0; |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 86 | |
| 87 | switch (ch) { |
Simon Glass | b5afcad | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 88 | case '\0': |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 89 | goto abort; |
| 90 | case 'u': |
| 91 | case 'd': |
| 92 | num = va_arg(va, unsigned int); |
| 93 | if (ch == 'd' && (int)num < 0) { |
| 94 | num = -(int)num; |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 95 | out(info, '-'); |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 96 | } |
Simon Glass | bdfb70b | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 97 | if (!num) { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 98 | out_dgt(info, 0); |
Simon Glass | bdfb70b | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 99 | } else { |
| 100 | for (div = 1000000000; div; div /= 10) |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 101 | div_out(info, &num, div); |
Simon Glass | bdfb70b | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 102 | } |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 103 | break; |
| 104 | case 'x': |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 105 | num = va_arg(va, unsigned int); |
Simon Glass | bdfb70b | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 106 | if (!num) { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 107 | out_dgt(info, 0); |
Simon Glass | bdfb70b | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 108 | } else { |
| 109 | for (div = 0x10000000; div; div /= 0x10) |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 110 | div_out(info, &num, div); |
Simon Glass | bdfb70b | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 111 | } |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 112 | break; |
| 113 | case 'c': |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 114 | out(info, (char)(va_arg(va, int))); |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 115 | break; |
| 116 | case 's': |
| 117 | p = va_arg(va, char*); |
| 118 | break; |
| 119 | case '%': |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 120 | out(info, '%'); |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 121 | default: |
| 122 | break; |
| 123 | } |
| 124 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 125 | *info->bf = 0; |
| 126 | info->bf = p; |
| 127 | while (*info->bf++ && width > 0) |
Simon Glass | b5afcad | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 128 | width--; |
| 129 | while (width-- > 0) |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 130 | info->putc(info, lz ? '0' : ' '); |
Simon Glass | 5bd9f6c | 2015-12-29 05:22:46 -0700 | [diff] [blame] | 131 | if (p) { |
| 132 | while ((ch = *p++)) |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 133 | info->putc(info, ch); |
Simon Glass | 5bd9f6c | 2015-12-29 05:22:46 -0700 | [diff] [blame] | 134 | } |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | abort: |
Stefan Roese | 363ab7b | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 139 | return 0; |
| 140 | } |
Sjoerd Simons | 1168ed5 | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 141 | |
Hans de Goede | 5f18398 | 2016-06-10 21:03:34 +0200 | [diff] [blame] | 142 | int vprintf(const char *fmt, va_list va) |
| 143 | { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 144 | struct printf_info info; |
| 145 | |
| 146 | info.putc = putc_normal; |
| 147 | return _vprintf(&info, fmt, va); |
Hans de Goede | 5f18398 | 2016-06-10 21:03:34 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Sjoerd Simons | 1168ed5 | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 150 | int printf(const char *fmt, ...) |
| 151 | { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 152 | struct printf_info info; |
| 153 | |
Sjoerd Simons | 1168ed5 | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 154 | va_list va; |
| 155 | int ret; |
| 156 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 157 | info.putc = putc_normal; |
Sjoerd Simons | 1168ed5 | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 158 | va_start(va, fmt); |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 159 | ret = _vprintf(&info, fmt, va); |
Simon Glass | 49138f8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 160 | va_end(va); |
| 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 165 | static void putc_outstr(struct printf_info *info, char ch) |
Simon Glass | 49138f8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 166 | { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 167 | *info->outstr++ = ch; |
Simon Glass | 49138f8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 168 | } |
| 169 | |
Marek Vasut | 8dc095b | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 170 | int sprintf(char *buf, const char *fmt, ...) |
Simon Glass | 49138f8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 171 | { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 172 | struct printf_info info; |
Simon Glass | 49138f8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 173 | va_list va; |
| 174 | int ret; |
| 175 | |
| 176 | va_start(va, fmt); |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 177 | info.outstr = buf; |
| 178 | info.putc = putc_outstr; |
| 179 | ret = _vprintf(&info, fmt, va); |
Sjoerd Simons | 1168ed5 | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 180 | va_end(va); |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 181 | *info.outstr = '\0'; |
Sjoerd Simons | 1168ed5 | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 182 | |
| 183 | return ret; |
| 184 | } |
Marek Vasut | 8dc095b | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 185 | |
| 186 | /* Note that size is ignored */ |
| 187 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
| 188 | { |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 189 | struct printf_info info; |
Marek Vasut | 8dc095b | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 190 | va_list va; |
| 191 | int ret; |
| 192 | |
| 193 | va_start(va, fmt); |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 194 | info.outstr = buf; |
| 195 | info.putc = putc_outstr; |
| 196 | ret = _vprintf(&info, fmt, va); |
Marek Vasut | 8dc095b | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 197 | va_end(va); |
Simon Glass | 5958cbe | 2016-08-04 21:58:14 -0600 | [diff] [blame] | 198 | *info.outstr = '\0'; |
Marek Vasut | 8dc095b | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 199 | |
| 200 | return ret; |
| 201 | } |
Simon Glass | affe9ee | 2016-07-04 11:58:13 -0600 | [diff] [blame] | 202 | |
| 203 | void __assert_fail(const char *assertion, const char *file, unsigned line, |
| 204 | const char *function) |
| 205 | { |
| 206 | /* This will not return */ |
| 207 | printf("%s:%u: %s: Assertion `%s' failed.", file, line, function, |
| 208 | assertion); |
| 209 | hang(); |
| 210 | } |