Stefan Roese | 7d9cde1 | 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 | |
Andre Przywara | 59d07ee | 2016-07-08 15:18:35 +0100 | [diff] [blame^] | 16 | /* |
| 17 | * This code in here may execute before the DRAM is initialised, so |
| 18 | * we should make sure that it doesn't touch BSS, which some boards |
| 19 | * put in DRAM. |
| 20 | */ |
| 21 | static char *bf __attribute__ ((section(".data"))); |
| 22 | static char zs __attribute__ ((section(".data"))); |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 23 | |
Simon Glass | 5c411d8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 24 | /* Current position in sprintf() output string */ |
Andre Przywara | 59d07ee | 2016-07-08 15:18:35 +0100 | [diff] [blame^] | 25 | static char *outstr __attribute__ ((section(".data"))); |
Simon Glass | 5c411d8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 26 | |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 27 | static void out(char c) |
| 28 | { |
| 29 | *bf++ = c; |
| 30 | } |
| 31 | |
| 32 | static void out_dgt(char dgt) |
| 33 | { |
Stefan Roese | a5ecdd0 | 2015-11-16 15:26:34 +0100 | [diff] [blame] | 34 | out(dgt + (dgt < 10 ? '0' : 'a' - 10)); |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 35 | zs = 1; |
| 36 | } |
| 37 | |
Stefan Roese | a5ecdd0 | 2015-11-16 15:26:34 +0100 | [diff] [blame] | 38 | static void div_out(unsigned int *num, unsigned int div) |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 39 | { |
| 40 | unsigned char dgt = 0; |
| 41 | |
Stefan Roese | a5ecdd0 | 2015-11-16 15:26:34 +0100 | [diff] [blame] | 42 | while (*num >= div) { |
| 43 | *num -= div; |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 44 | dgt++; |
| 45 | } |
| 46 | |
| 47 | if (zs || dgt > 0) |
| 48 | out_dgt(dgt); |
| 49 | } |
| 50 | |
Simon Glass | 5c411d8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 51 | int _vprintf(const char *fmt, va_list va, void (*putc)(const char ch)) |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 52 | { |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 53 | char ch; |
| 54 | char *p; |
Stefan Roese | a5ecdd0 | 2015-11-16 15:26:34 +0100 | [diff] [blame] | 55 | unsigned int num; |
| 56 | char buf[12]; |
| 57 | unsigned int div; |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 58 | |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 59 | while ((ch = *(fmt++))) { |
| 60 | if (ch != '%') { |
| 61 | putc(ch); |
| 62 | } else { |
Simon Glass | 1fb6760 | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 63 | bool lz = false; |
| 64 | int width = 0; |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 65 | |
| 66 | ch = *(fmt++); |
| 67 | if (ch == '0') { |
| 68 | ch = *(fmt++); |
| 69 | lz = 1; |
| 70 | } |
| 71 | |
| 72 | if (ch >= '0' && ch <= '9') { |
Simon Glass | 1fb6760 | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 73 | width = 0; |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 74 | while (ch >= '0' && ch <= '9') { |
Simon Glass | 1fb6760 | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 75 | width = (width * 10) + ch - '0'; |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 76 | ch = *fmt++; |
| 77 | } |
| 78 | } |
| 79 | bf = buf; |
| 80 | p = bf; |
| 81 | zs = 0; |
| 82 | |
| 83 | switch (ch) { |
Simon Glass | 1fb6760 | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 84 | case '\0': |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 85 | goto abort; |
| 86 | case 'u': |
| 87 | case 'd': |
| 88 | num = va_arg(va, unsigned int); |
| 89 | if (ch == 'd' && (int)num < 0) { |
| 90 | num = -(int)num; |
| 91 | out('-'); |
| 92 | } |
Simon Glass | 74b1320 | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 93 | if (!num) { |
| 94 | out_dgt(0); |
| 95 | } else { |
| 96 | for (div = 1000000000; div; div /= 10) |
| 97 | div_out(&num, div); |
| 98 | } |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 99 | break; |
| 100 | case 'x': |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 101 | num = va_arg(va, unsigned int); |
Simon Glass | 74b1320 | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 102 | if (!num) { |
| 103 | out_dgt(0); |
| 104 | } else { |
| 105 | for (div = 0x10000000; div; div /= 0x10) |
| 106 | div_out(&num, div); |
| 107 | } |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 108 | break; |
| 109 | case 'c': |
| 110 | out((char)(va_arg(va, int))); |
| 111 | break; |
| 112 | case 's': |
| 113 | p = va_arg(va, char*); |
| 114 | break; |
| 115 | case '%': |
| 116 | out('%'); |
| 117 | default: |
| 118 | break; |
| 119 | } |
| 120 | |
| 121 | *bf = 0; |
| 122 | bf = p; |
Simon Glass | 1fb6760 | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 123 | while (*bf++ && width > 0) |
| 124 | width--; |
| 125 | while (width-- > 0) |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 126 | putc(lz ? '0' : ' '); |
Simon Glass | 8e31681 | 2015-12-29 05:22:46 -0700 | [diff] [blame] | 127 | if (p) { |
| 128 | while ((ch = *p++)) |
| 129 | putc(ch); |
| 130 | } |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | abort: |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 135 | return 0; |
| 136 | } |
Sjoerd Simons | 962a43c | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 137 | |
Hans de Goede | da70b4d | 2016-06-10 21:03:34 +0200 | [diff] [blame] | 138 | int vprintf(const char *fmt, va_list va) |
| 139 | { |
| 140 | return _vprintf(fmt, va, putc); |
| 141 | } |
| 142 | |
Sjoerd Simons | 962a43c | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 143 | int printf(const char *fmt, ...) |
| 144 | { |
| 145 | va_list va; |
| 146 | int ret; |
| 147 | |
| 148 | va_start(va, fmt); |
Simon Glass | 5c411d8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 149 | ret = _vprintf(fmt, va, putc); |
Sjoerd Simons | 962a43c | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 150 | va_end(va); |
| 151 | |
| 152 | return ret; |
| 153 | } |
Simon Glass | 5c411d8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 154 | |
| 155 | static void putc_outstr(char ch) |
| 156 | { |
| 157 | *outstr++ = ch; |
| 158 | } |
| 159 | |
Marek Vasut | abeb272 | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 160 | int sprintf(char *buf, const char *fmt, ...) |
Simon Glass | 5c411d8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 161 | { |
| 162 | va_list va; |
| 163 | int ret; |
| 164 | |
| 165 | va_start(va, fmt); |
| 166 | outstr = buf; |
| 167 | ret = _vprintf(fmt, va, putc_outstr); |
| 168 | va_end(va); |
| 169 | *outstr = '\0'; |
| 170 | |
| 171 | return ret; |
| 172 | } |
Marek Vasut | abeb272 | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 173 | |
| 174 | /* Note that size is ignored */ |
| 175 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
| 176 | { |
| 177 | va_list va; |
| 178 | int ret; |
| 179 | |
| 180 | va_start(va, fmt); |
Simon Glass | 3191d84 | 2016-06-08 20:55:15 -0600 | [diff] [blame] | 181 | outstr = buf; |
| 182 | ret = _vprintf(fmt, va, putc_outstr); |
Marek Vasut | abeb272 | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 183 | va_end(va); |
Simon Glass | 3191d84 | 2016-06-08 20:55:15 -0600 | [diff] [blame] | 184 | *outstr = '\0'; |
Marek Vasut | abeb272 | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 185 | |
| 186 | return ret; |
| 187 | } |