blob: 5f7e27d76edf695216b46454725a9055b2d6dc25 [file] [log] [blame]
Stefan Roese363ab7b2015-11-23 07:00:22 +01001/*
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>
Vignesh R0bda3e42017-04-10 12:23:22 +053015#include <linux/ctype.h>
Stefan Roese363ab7b2015-11-23 07:00:22 +010016
Simon Glass5958cbe2016-08-04 21:58:14 -060017struct printf_info {
18 char *bf; /* Digit buffer */
19 char zs; /* non-zero if a digit has been written */
20 char *outstr; /* Next output position for sprintf() */
Stefan Roese363ab7b2015-11-23 07:00:22 +010021
Simon Glass5958cbe2016-08-04 21:58:14 -060022 /* Output a character */
23 void (*putc)(struct printf_info *info, char ch);
24};
Simon Glass49138f82016-05-14 14:02:53 -060025
Simon Glass5958cbe2016-08-04 21:58:14 -060026static void out(struct printf_info *info, char c)
Stefan Roese363ab7b2015-11-23 07:00:22 +010027{
Simon Glass5958cbe2016-08-04 21:58:14 -060028 *info->bf++ = c;
Stefan Roese363ab7b2015-11-23 07:00:22 +010029}
30
Simon Glass5958cbe2016-08-04 21:58:14 -060031static void out_dgt(struct printf_info *info, char dgt)
32{
33 out(info, dgt + (dgt < 10 ? '0' : 'a' - 10));
34 info->zs = 1;
35}
36
Andre Przywara32118682017-01-02 11:48:28 +000037static void div_out(struct printf_info *info, unsigned long *num,
38 unsigned long div)
Stefan Roese363ab7b2015-11-23 07:00:22 +010039{
40 unsigned char dgt = 0;
41
Stefan Roese26a552c2015-11-16 15:26:34 +010042 while (*num >= div) {
43 *num -= div;
Stefan Roese363ab7b2015-11-23 07:00:22 +010044 dgt++;
45 }
46
Simon Glass5958cbe2016-08-04 21:58:14 -060047 if (info->zs || dgt > 0)
48 out_dgt(info, dgt);
Stefan Roese363ab7b2015-11-23 07:00:22 +010049}
50
Vignesh R0bda3e42017-04-10 12:23:22 +053051#ifdef CONFIG_SPL_NET_SUPPORT
52static void string(struct printf_info *info, char *s)
53{
54 char ch;
55
56 while ((ch = *s++))
57 out(info, ch);
58}
59
60static const char hex_asc[] = "0123456789abcdef";
61#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
62#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
63
64static inline char *pack_hex_byte(char *buf, u8 byte)
65{
66 *buf++ = hex_asc_hi(byte);
67 *buf++ = hex_asc_lo(byte);
68 return buf;
69}
70
71static void mac_address_string(struct printf_info *info, u8 *addr,
72 bool separator)
73{
74 /* (6 * 2 hex digits), 5 colons and trailing zero */
75 char mac_addr[6 * 3];
76 char *p = mac_addr;
77 int i;
78
79 for (i = 0; i < 6; i++) {
80 p = pack_hex_byte(p, addr[i]);
81 if (separator && i != 5)
82 *p++ = ':';
83 }
84 *p = '\0';
85
86 string(info, mac_addr);
87}
88
89static char *put_dec_trunc(char *buf, unsigned int q)
90{
91 unsigned int d3, d2, d1, d0;
92 d1 = (q >> 4) & 0xf;
93 d2 = (q >> 8) & 0xf;
94 d3 = (q >> 12);
95
96 d0 = 6 * (d3 + d2 + d1) + (q & 0xf);
97 q = (d0 * 0xcd) >> 11;
98 d0 = d0 - 10 * q;
99 *buf++ = d0 + '0'; /* least significant digit */
100 d1 = q + 9 * d3 + 5 * d2 + d1;
101 if (d1 != 0) {
102 q = (d1 * 0xcd) >> 11;
103 d1 = d1 - 10 * q;
104 *buf++ = d1 + '0'; /* next digit */
105
106 d2 = q + 2 * d2;
107 if ((d2 != 0) || (d3 != 0)) {
108 q = (d2 * 0xd) >> 7;
109 d2 = d2 - 10 * q;
110 *buf++ = d2 + '0'; /* next digit */
111
112 d3 = q + 4 * d3;
113 if (d3 != 0) {
114 q = (d3 * 0xcd) >> 11;
115 d3 = d3 - 10 * q;
116 *buf++ = d3 + '0'; /* next digit */
117 if (q != 0)
118 *buf++ = q + '0'; /* most sign. digit */
119 }
120 }
121 }
122 return buf;
123}
124
125static void ip4_addr_string(struct printf_info *info, u8 *addr)
126{
127 /* (4 * 3 decimal digits), 3 dots and trailing zero */
128 char ip4_addr[4 * 4];
129 char temp[3]; /* hold each IP quad in reverse order */
130 char *p = ip4_addr;
131 int i, digits;
132
133 for (i = 0; i < 4; i++) {
134 digits = put_dec_trunc(temp, addr[i]) - temp;
135 /* reverse the digits in the quad */
136 while (digits--)
137 *p++ = temp[digits];
138 if (i != 3)
139 *p++ = '.';
140 }
141 *p = '\0';
142
143 string(info, ip4_addr);
144}
145#endif
146
147/*
148 * Show a '%p' thing. A kernel extension is that the '%p' is followed
149 * by an extra set of characters that are extended format
150 * specifiers.
151 *
152 * Right now we handle:
153 *
154 * - 'M' For a 6-byte MAC address, it prints the address in the
155 * usual colon-separated hex notation.
156 * - 'm' Same as above except there is no colon-separator.
157 * - 'I4'for IPv4 addresses printed in the usual way (dot-separated
158 * decimal).
159 */
160
161static void pointer(struct printf_info *info, const char *fmt, void *ptr)
162{
163#ifdef DEBUG
164 unsigned long num = (uintptr_t)ptr;
165 unsigned long div;
166#endif
167
168 switch (*fmt) {
169#ifdef DEBUG
170 case 'a':
171
172 switch (fmt[1]) {
173 case 'p':
174 default:
175 num = *(phys_addr_t *)ptr;
176 break;
177 }
178 break;
179#endif
180#ifdef CONFIG_SPL_NET_SUPPORT
181 case 'm':
182 return mac_address_string(info, ptr, false);
183 case 'M':
184 return mac_address_string(info, ptr, true);
185 case 'I':
186 if (fmt[1] == '4')
187 return ip4_addr_string(info, ptr);
188#endif
189 default:
190 break;
191 }
192#ifdef DEBUG
193 div = 1UL << (sizeof(long) * 8 - 4);
194 for (; div; div /= 0x10)
195 div_out(info, &num, div);
196#endif
197}
198
Masahiro Yamadac1cb8002017-02-12 18:08:43 +0900199static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
Stefan Roese363ab7b2015-11-23 07:00:22 +0100200{
Stefan Roese363ab7b2015-11-23 07:00:22 +0100201 char ch;
202 char *p;
Andre Przywara32118682017-01-02 11:48:28 +0000203 unsigned long num;
Stefan Roese26a552c2015-11-16 15:26:34 +0100204 char buf[12];
Andre Przywara32118682017-01-02 11:48:28 +0000205 unsigned long div;
Stefan Roese363ab7b2015-11-23 07:00:22 +0100206
Stefan Roese363ab7b2015-11-23 07:00:22 +0100207 while ((ch = *(fmt++))) {
208 if (ch != '%') {
Simon Glass5958cbe2016-08-04 21:58:14 -0600209 info->putc(info, ch);
Stefan Roese363ab7b2015-11-23 07:00:22 +0100210 } else {
Simon Glassb5afcad2016-05-14 14:02:52 -0600211 bool lz = false;
212 int width = 0;
Andre Przywara32118682017-01-02 11:48:28 +0000213 bool islong = false;
Stefan Roese363ab7b2015-11-23 07:00:22 +0100214
215 ch = *(fmt++);
Andre Przywara7b303b82017-01-02 11:48:29 +0000216 if (ch == '-')
217 ch = *(fmt++);
218
Stefan Roese363ab7b2015-11-23 07:00:22 +0100219 if (ch == '0') {
220 ch = *(fmt++);
221 lz = 1;
222 }
223
224 if (ch >= '0' && ch <= '9') {
Simon Glassb5afcad2016-05-14 14:02:52 -0600225 width = 0;
Stefan Roese363ab7b2015-11-23 07:00:22 +0100226 while (ch >= '0' && ch <= '9') {
Simon Glassb5afcad2016-05-14 14:02:52 -0600227 width = (width * 10) + ch - '0';
Stefan Roese363ab7b2015-11-23 07:00:22 +0100228 ch = *fmt++;
229 }
230 }
Andre Przywara32118682017-01-02 11:48:28 +0000231 if (ch == 'l') {
232 ch = *(fmt++);
233 islong = true;
234 }
235
Simon Glass5958cbe2016-08-04 21:58:14 -0600236 info->bf = buf;
237 p = info->bf;
238 info->zs = 0;
Stefan Roese363ab7b2015-11-23 07:00:22 +0100239
240 switch (ch) {
Simon Glassb5afcad2016-05-14 14:02:52 -0600241 case '\0':
Stefan Roese363ab7b2015-11-23 07:00:22 +0100242 goto abort;
243 case 'u':
244 case 'd':
Andre Przywara32118682017-01-02 11:48:28 +0000245 div = 1000000000;
246 if (islong) {
247 num = va_arg(va, unsigned long);
248 if (sizeof(long) > 4)
249 div *= div * 10;
250 } else {
251 num = va_arg(va, unsigned int);
252 }
253
254 if (ch == 'd') {
255 if (islong && (long)num < 0) {
256 num = -(long)num;
257 out(info, '-');
258 } else if (!islong && (int)num < 0) {
259 num = -(int)num;
260 out(info, '-');
261 }
Stefan Roese363ab7b2015-11-23 07:00:22 +0100262 }
Simon Glassbdfb70b2016-01-05 09:30:57 -0700263 if (!num) {
Simon Glass5958cbe2016-08-04 21:58:14 -0600264 out_dgt(info, 0);
Simon Glassbdfb70b2016-01-05 09:30:57 -0700265 } else {
Andre Przywara32118682017-01-02 11:48:28 +0000266 for (; div; div /= 10)
Simon Glass5958cbe2016-08-04 21:58:14 -0600267 div_out(info, &num, div);
Simon Glassbdfb70b2016-01-05 09:30:57 -0700268 }
Stefan Roese363ab7b2015-11-23 07:00:22 +0100269 break;
270 case 'x':
Andre Przywara32118682017-01-02 11:48:28 +0000271 if (islong) {
272 num = va_arg(va, unsigned long);
273 div = 1UL << (sizeof(long) * 8 - 4);
274 } else {
275 num = va_arg(va, unsigned int);
276 div = 0x10000000;
277 }
Simon Glassbdfb70b2016-01-05 09:30:57 -0700278 if (!num) {
Simon Glass5958cbe2016-08-04 21:58:14 -0600279 out_dgt(info, 0);
Simon Glassbdfb70b2016-01-05 09:30:57 -0700280 } else {
Andre Przywara32118682017-01-02 11:48:28 +0000281 for (; div; div /= 0x10)
Simon Glass5958cbe2016-08-04 21:58:14 -0600282 div_out(info, &num, div);
Simon Glassbdfb70b2016-01-05 09:30:57 -0700283 }
Stefan Roese363ab7b2015-11-23 07:00:22 +0100284 break;
285 case 'c':
Simon Glass5958cbe2016-08-04 21:58:14 -0600286 out(info, (char)(va_arg(va, int)));
Stefan Roese363ab7b2015-11-23 07:00:22 +0100287 break;
288 case 's':
289 p = va_arg(va, char*);
290 break;
Vignesh R0bda3e42017-04-10 12:23:22 +0530291 case 'p':
292 pointer(info, fmt, va_arg(va, void *));
293 while (isalnum(fmt[0]))
294 fmt++;
295 break;
Stefan Roese363ab7b2015-11-23 07:00:22 +0100296 case '%':
Simon Glass5958cbe2016-08-04 21:58:14 -0600297 out(info, '%');
Stefan Roese363ab7b2015-11-23 07:00:22 +0100298 default:
299 break;
300 }
301
Simon Glass5958cbe2016-08-04 21:58:14 -0600302 *info->bf = 0;
303 info->bf = p;
304 while (*info->bf++ && width > 0)
Simon Glassb5afcad2016-05-14 14:02:52 -0600305 width--;
306 while (width-- > 0)
Simon Glass5958cbe2016-08-04 21:58:14 -0600307 info->putc(info, lz ? '0' : ' ');
Simon Glass5bd9f6c2015-12-29 05:22:46 -0700308 if (p) {
309 while ((ch = *p++))
Simon Glass5958cbe2016-08-04 21:58:14 -0600310 info->putc(info, ch);
Simon Glass5bd9f6c2015-12-29 05:22:46 -0700311 }
Stefan Roese363ab7b2015-11-23 07:00:22 +0100312 }
313 }
314
315abort:
Stefan Roese363ab7b2015-11-23 07:00:22 +0100316 return 0;
317}
Sjoerd Simons1168ed52015-12-04 23:27:37 +0100318
Alex Kiernand07d0a02018-04-19 04:32:55 +0000319#if CONFIG_IS_ENABLED(PRINTF)
320static void putc_normal(struct printf_info *info, char ch)
321{
322 putc(ch);
323}
324
Hans de Goede5f183982016-06-10 21:03:34 +0200325int vprintf(const char *fmt, va_list va)
326{
Simon Glass5958cbe2016-08-04 21:58:14 -0600327 struct printf_info info;
328
329 info.putc = putc_normal;
330 return _vprintf(&info, fmt, va);
Hans de Goede5f183982016-06-10 21:03:34 +0200331}
332
Sjoerd Simons1168ed52015-12-04 23:27:37 +0100333int printf(const char *fmt, ...)
334{
Simon Glass5958cbe2016-08-04 21:58:14 -0600335 struct printf_info info;
336
Sjoerd Simons1168ed52015-12-04 23:27:37 +0100337 va_list va;
338 int ret;
339
Simon Glass5958cbe2016-08-04 21:58:14 -0600340 info.putc = putc_normal;
Sjoerd Simons1168ed52015-12-04 23:27:37 +0100341 va_start(va, fmt);
Simon Glass5958cbe2016-08-04 21:58:14 -0600342 ret = _vprintf(&info, fmt, va);
Simon Glass49138f82016-05-14 14:02:53 -0600343 va_end(va);
344
345 return ret;
346}
Alex Kiernand07d0a02018-04-19 04:32:55 +0000347#endif
Simon Glass49138f82016-05-14 14:02:53 -0600348
Simon Glass5958cbe2016-08-04 21:58:14 -0600349static void putc_outstr(struct printf_info *info, char ch)
Simon Glass49138f82016-05-14 14:02:53 -0600350{
Simon Glass5958cbe2016-08-04 21:58:14 -0600351 *info->outstr++ = ch;
Simon Glass49138f82016-05-14 14:02:53 -0600352}
353
Marek Vasut8dc095b2016-05-31 23:12:46 +0200354int sprintf(char *buf, const char *fmt, ...)
Simon Glass49138f82016-05-14 14:02:53 -0600355{
Simon Glass5958cbe2016-08-04 21:58:14 -0600356 struct printf_info info;
Simon Glass49138f82016-05-14 14:02:53 -0600357 va_list va;
358 int ret;
359
360 va_start(va, fmt);
Simon Glass5958cbe2016-08-04 21:58:14 -0600361 info.outstr = buf;
362 info.putc = putc_outstr;
363 ret = _vprintf(&info, fmt, va);
Sjoerd Simons1168ed52015-12-04 23:27:37 +0100364 va_end(va);
Simon Glass5958cbe2016-08-04 21:58:14 -0600365 *info.outstr = '\0';
Sjoerd Simons1168ed52015-12-04 23:27:37 +0100366
367 return ret;
368}
Marek Vasut8dc095b2016-05-31 23:12:46 +0200369
370/* Note that size is ignored */
371int snprintf(char *buf, size_t size, const char *fmt, ...)
372{
Simon Glass5958cbe2016-08-04 21:58:14 -0600373 struct printf_info info;
Marek Vasut8dc095b2016-05-31 23:12:46 +0200374 va_list va;
375 int ret;
376
377 va_start(va, fmt);
Simon Glass5958cbe2016-08-04 21:58:14 -0600378 info.outstr = buf;
379 info.putc = putc_outstr;
380 ret = _vprintf(&info, fmt, va);
Marek Vasut8dc095b2016-05-31 23:12:46 +0200381 va_end(va);
Simon Glass5958cbe2016-08-04 21:58:14 -0600382 *info.outstr = '\0';
Marek Vasut8dc095b2016-05-31 23:12:46 +0200383
384 return ret;
385}