blob: f6727360097095f45f2e4585333681ad61307351 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: LGPL-2.1+
Stefan Roese7d9cde12015-11-23 07:00:22 +01002/*
3 * Tiny printf version for SPL
4 *
5 * Copied from:
6 * http://www.sparetimelabs.com/printfrevisited/printfrevisited.php
7 *
8 * Copyright (C) 2004,2008 Kustaa Nyholm
Stefan Roese7d9cde12015-11-23 07:00:22 +01009 */
10
Matwey V. Kornilovc091f652021-08-05 22:06:05 +030011#include <log.h>
Stefan Roese7d9cde12015-11-23 07:00:22 +010012#include <serial.h>
Matwey V. Kornilovc091f652021-08-05 22:06:05 +030013#include <stdarg.h>
Vignesh Rcdce1f72017-04-10 12:23:22 +053014#include <linux/ctype.h>
Stefan Roese7d9cde12015-11-23 07:00:22 +010015
Simon Glass45313e82016-08-04 21:58:14 -060016struct 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 Roese7d9cde12015-11-23 07:00:22 +010020
Simon Glass45313e82016-08-04 21:58:14 -060021 /* Output a character */
22 void (*putc)(struct printf_info *info, char ch);
23};
Simon Glass5c411d82016-05-14 14:02:53 -060024
Simon Glass45313e82016-08-04 21:58:14 -060025static void out(struct printf_info *info, char c)
Stefan Roese7d9cde12015-11-23 07:00:22 +010026{
Simon Glass45313e82016-08-04 21:58:14 -060027 *info->bf++ = c;
Stefan Roese7d9cde12015-11-23 07:00:22 +010028}
29
Simon Glass45313e82016-08-04 21:58:14 -060030static void out_dgt(struct printf_info *info, char dgt)
31{
32 out(info, dgt + (dgt < 10 ? '0' : 'a' - 10));
33 info->zs = 1;
34}
35
Andre Przywaraa28e1d92017-01-02 11:48:28 +000036static void div_out(struct printf_info *info, unsigned long *num,
37 unsigned long div)
Stefan Roese7d9cde12015-11-23 07:00:22 +010038{
39 unsigned char dgt = 0;
40
Stefan Roesea5ecdd02015-11-16 15:26:34 +010041 while (*num >= div) {
42 *num -= div;
Stefan Roese7d9cde12015-11-23 07:00:22 +010043 dgt++;
44 }
45
Simon Glass45313e82016-08-04 21:58:14 -060046 if (info->zs || dgt > 0)
47 out_dgt(info, dgt);
Stefan Roese7d9cde12015-11-23 07:00:22 +010048}
49
Simon Glass5ed16a92021-08-08 12:20:30 -060050#ifdef CONFIG_SPL_NET
Vignesh Rcdce1f72017-04-10 12:23:22 +053051static void string(struct printf_info *info, char *s)
52{
53 char ch;
54
55 while ((ch = *s++))
56 out(info, ch);
57}
58
59static const char hex_asc[] = "0123456789abcdef";
60#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
61#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
62
63static inline char *pack_hex_byte(char *buf, u8 byte)
64{
65 *buf++ = hex_asc_hi(byte);
66 *buf++ = hex_asc_lo(byte);
67 return buf;
68}
69
70static void mac_address_string(struct printf_info *info, u8 *addr,
71 bool separator)
72{
73 /* (6 * 2 hex digits), 5 colons and trailing zero */
74 char mac_addr[6 * 3];
75 char *p = mac_addr;
76 int i;
77
78 for (i = 0; i < 6; i++) {
79 p = pack_hex_byte(p, addr[i]);
80 if (separator && i != 5)
81 *p++ = ':';
82 }
83 *p = '\0';
84
85 string(info, mac_addr);
86}
87
88static char *put_dec_trunc(char *buf, unsigned int q)
89{
90 unsigned int d3, d2, d1, d0;
91 d1 = (q >> 4) & 0xf;
92 d2 = (q >> 8) & 0xf;
93 d3 = (q >> 12);
94
95 d0 = 6 * (d3 + d2 + d1) + (q & 0xf);
96 q = (d0 * 0xcd) >> 11;
97 d0 = d0 - 10 * q;
98 *buf++ = d0 + '0'; /* least significant digit */
99 d1 = q + 9 * d3 + 5 * d2 + d1;
100 if (d1 != 0) {
101 q = (d1 * 0xcd) >> 11;
102 d1 = d1 - 10 * q;
103 *buf++ = d1 + '0'; /* next digit */
104
105 d2 = q + 2 * d2;
106 if ((d2 != 0) || (d3 != 0)) {
107 q = (d2 * 0xd) >> 7;
108 d2 = d2 - 10 * q;
109 *buf++ = d2 + '0'; /* next digit */
110
111 d3 = q + 4 * d3;
112 if (d3 != 0) {
113 q = (d3 * 0xcd) >> 11;
114 d3 = d3 - 10 * q;
115 *buf++ = d3 + '0'; /* next digit */
116 if (q != 0)
117 *buf++ = q + '0'; /* most sign. digit */
118 }
119 }
120 }
121 return buf;
122}
123
124static void ip4_addr_string(struct printf_info *info, u8 *addr)
125{
126 /* (4 * 3 decimal digits), 3 dots and trailing zero */
127 char ip4_addr[4 * 4];
128 char temp[3]; /* hold each IP quad in reverse order */
129 char *p = ip4_addr;
130 int i, digits;
131
132 for (i = 0; i < 4; i++) {
133 digits = put_dec_trunc(temp, addr[i]) - temp;
134 /* reverse the digits in the quad */
135 while (digits--)
136 *p++ = temp[digits];
137 if (i != 3)
138 *p++ = '.';
139 }
140 *p = '\0';
141
142 string(info, ip4_addr);
143}
144#endif
145
146/*
147 * Show a '%p' thing. A kernel extension is that the '%p' is followed
148 * by an extra set of characters that are extended format
149 * specifiers.
150 *
151 * Right now we handle:
152 *
153 * - 'M' For a 6-byte MAC address, it prints the address in the
154 * usual colon-separated hex notation.
155 * - 'm' Same as above except there is no colon-separator.
156 * - 'I4'for IPv4 addresses printed in the usual way (dot-separated
157 * decimal).
158 */
159
Simon Glass831c1612019-10-21 17:26:45 -0600160static void __maybe_unused pointer(struct printf_info *info, const char *fmt,
161 void *ptr)
Vignesh Rcdce1f72017-04-10 12:23:22 +0530162{
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
Simon Glass5ed16a92021-08-08 12:20:30 -0600180#ifdef CONFIG_SPL_NET
Vignesh Rcdce1f72017-04-10 12:23:22 +0530181 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 Yamada433cbfb2017-02-12 18:08:43 +0900199static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
Stefan Roese7d9cde12015-11-23 07:00:22 +0100200{
Stefan Roese7d9cde12015-11-23 07:00:22 +0100201 char ch;
202 char *p;
Andre Przywaraa28e1d92017-01-02 11:48:28 +0000203 unsigned long num;
Stefan Roesea5ecdd02015-11-16 15:26:34 +0100204 char buf[12];
Andre Przywaraa28e1d92017-01-02 11:48:28 +0000205 unsigned long div;
Stefan Roese7d9cde12015-11-23 07:00:22 +0100206
Stefan Roese7d9cde12015-11-23 07:00:22 +0100207 while ((ch = *(fmt++))) {
208 if (ch != '%') {
Simon Glass45313e82016-08-04 21:58:14 -0600209 info->putc(info, ch);
Stefan Roese7d9cde12015-11-23 07:00:22 +0100210 } else {
Simon Glass1fb67602016-05-14 14:02:52 -0600211 bool lz = false;
212 int width = 0;
Andre Przywaraa28e1d92017-01-02 11:48:28 +0000213 bool islong = false;
Stefan Roese7d9cde12015-11-23 07:00:22 +0100214
215 ch = *(fmt++);
Andre Przywara1c853622017-01-02 11:48:29 +0000216 if (ch == '-')
217 ch = *(fmt++);
218
Stefan Roese7d9cde12015-11-23 07:00:22 +0100219 if (ch == '0') {
220 ch = *(fmt++);
221 lz = 1;
222 }
223
224 if (ch >= '0' && ch <= '9') {
Simon Glass1fb67602016-05-14 14:02:52 -0600225 width = 0;
Stefan Roese7d9cde12015-11-23 07:00:22 +0100226 while (ch >= '0' && ch <= '9') {
Simon Glass1fb67602016-05-14 14:02:52 -0600227 width = (width * 10) + ch - '0';
Stefan Roese7d9cde12015-11-23 07:00:22 +0100228 ch = *fmt++;
229 }
230 }
Andre Przywaraa28e1d92017-01-02 11:48:28 +0000231 if (ch == 'l') {
232 ch = *(fmt++);
233 islong = true;
234 }
235
Simon Glass45313e82016-08-04 21:58:14 -0600236 info->bf = buf;
237 p = info->bf;
238 info->zs = 0;
Stefan Roese7d9cde12015-11-23 07:00:22 +0100239
240 switch (ch) {
Simon Glass1fb67602016-05-14 14:02:52 -0600241 case '\0':
Stefan Roese7d9cde12015-11-23 07:00:22 +0100242 goto abort;
243 case 'u':
244 case 'd':
Marek Vasute7882f62020-04-10 20:54:49 +0200245 case 'i':
Andre Przywaraa28e1d92017-01-02 11:48:28 +0000246 div = 1000000000;
247 if (islong) {
248 num = va_arg(va, unsigned long);
249 if (sizeof(long) > 4)
250 div *= div * 10;
251 } else {
252 num = va_arg(va, unsigned int);
253 }
254
Marek Vasute7882f62020-04-10 20:54:49 +0200255 if (ch != 'u') {
Andre Przywaraa28e1d92017-01-02 11:48:28 +0000256 if (islong && (long)num < 0) {
257 num = -(long)num;
258 out(info, '-');
259 } else if (!islong && (int)num < 0) {
260 num = -(int)num;
261 out(info, '-');
262 }
Stefan Roese7d9cde12015-11-23 07:00:22 +0100263 }
Simon Glass74b13202016-01-05 09:30:57 -0700264 if (!num) {
Simon Glass45313e82016-08-04 21:58:14 -0600265 out_dgt(info, 0);
Simon Glass74b13202016-01-05 09:30:57 -0700266 } else {
Andre Przywaraa28e1d92017-01-02 11:48:28 +0000267 for (; div; div /= 10)
Simon Glass45313e82016-08-04 21:58:14 -0600268 div_out(info, &num, div);
Simon Glass74b13202016-01-05 09:30:57 -0700269 }
Stefan Roese7d9cde12015-11-23 07:00:22 +0100270 break;
Simon Glass831c1612019-10-21 17:26:45 -0600271 case 'p':
Jerome Forissier2d9252c2024-08-07 19:11:54 +0200272 if (CONFIG_IS_ENABLED(NET) ||
273 CONFIG_IS_ENABLED(NET_LWIP) || _DEBUG) {
Matwey V. Kornilovc091f652021-08-05 22:06:05 +0300274 pointer(info, fmt, va_arg(va, void *));
275 /*
276 * Skip this because it pulls in _ctype which is
277 * 256 bytes, and we don't generally implement
278 * pointer anyway
279 */
280 while (isalnum(fmt[0]))
281 fmt++;
282 break;
283 }
Simon Glass831c1612019-10-21 17:26:45 -0600284 islong = true;
285 /* no break */
Stefan Roese7d9cde12015-11-23 07:00:22 +0100286 case 'x':
Andre Przywaraa28e1d92017-01-02 11:48:28 +0000287 if (islong) {
288 num = va_arg(va, unsigned long);
289 div = 1UL << (sizeof(long) * 8 - 4);
290 } else {
291 num = va_arg(va, unsigned int);
292 div = 0x10000000;
293 }
Simon Glass74b13202016-01-05 09:30:57 -0700294 if (!num) {
Simon Glass45313e82016-08-04 21:58:14 -0600295 out_dgt(info, 0);
Simon Glass74b13202016-01-05 09:30:57 -0700296 } else {
Andre Przywaraa28e1d92017-01-02 11:48:28 +0000297 for (; div; div /= 0x10)
Simon Glass45313e82016-08-04 21:58:14 -0600298 div_out(info, &num, div);
Simon Glass74b13202016-01-05 09:30:57 -0700299 }
Stefan Roese7d9cde12015-11-23 07:00:22 +0100300 break;
301 case 'c':
Simon Glass45313e82016-08-04 21:58:14 -0600302 out(info, (char)(va_arg(va, int)));
Stefan Roese7d9cde12015-11-23 07:00:22 +0100303 break;
304 case 's':
305 p = va_arg(va, char*);
306 break;
307 case '%':
Simon Glass45313e82016-08-04 21:58:14 -0600308 out(info, '%');
Stefan Roese7d9cde12015-11-23 07:00:22 +0100309 default:
310 break;
311 }
312
Simon Glass45313e82016-08-04 21:58:14 -0600313 *info->bf = 0;
314 info->bf = p;
315 while (*info->bf++ && width > 0)
Simon Glass1fb67602016-05-14 14:02:52 -0600316 width--;
317 while (width-- > 0)
Simon Glass45313e82016-08-04 21:58:14 -0600318 info->putc(info, lz ? '0' : ' ');
Simon Glass8e316812015-12-29 05:22:46 -0700319 if (p) {
320 while ((ch = *p++))
Simon Glass45313e82016-08-04 21:58:14 -0600321 info->putc(info, ch);
Simon Glass8e316812015-12-29 05:22:46 -0700322 }
Stefan Roese7d9cde12015-11-23 07:00:22 +0100323 }
324 }
325
326abort:
Stefan Roese7d9cde12015-11-23 07:00:22 +0100327 return 0;
328}
Sjoerd Simons962a43c2015-12-04 23:27:37 +0100329
Alex Kiernan4f1eed72018-04-19 04:32:55 +0000330#if CONFIG_IS_ENABLED(PRINTF)
331static void putc_normal(struct printf_info *info, char ch)
332{
333 putc(ch);
334}
335
Hans de Goededa70b4d2016-06-10 21:03:34 +0200336int vprintf(const char *fmt, va_list va)
337{
Simon Glass45313e82016-08-04 21:58:14 -0600338 struct printf_info info;
339
340 info.putc = putc_normal;
341 return _vprintf(&info, fmt, va);
Hans de Goededa70b4d2016-06-10 21:03:34 +0200342}
343
Sjoerd Simons962a43c2015-12-04 23:27:37 +0100344int printf(const char *fmt, ...)
345{
Simon Glass45313e82016-08-04 21:58:14 -0600346 struct printf_info info;
347
Sjoerd Simons962a43c2015-12-04 23:27:37 +0100348 va_list va;
349 int ret;
350
Simon Glass45313e82016-08-04 21:58:14 -0600351 info.putc = putc_normal;
Sjoerd Simons962a43c2015-12-04 23:27:37 +0100352 va_start(va, fmt);
Simon Glass45313e82016-08-04 21:58:14 -0600353 ret = _vprintf(&info, fmt, va);
Sjoerd Simons962a43c2015-12-04 23:27:37 +0100354 va_end(va);
355
356 return ret;
357}
Alex Kiernan4f1eed72018-04-19 04:32:55 +0000358#endif
Simon Glass5c411d82016-05-14 14:02:53 -0600359
Simon Glass45313e82016-08-04 21:58:14 -0600360static void putc_outstr(struct printf_info *info, char ch)
Simon Glass5c411d82016-05-14 14:02:53 -0600361{
Simon Glass45313e82016-08-04 21:58:14 -0600362 *info->outstr++ = ch;
Simon Glass5c411d82016-05-14 14:02:53 -0600363}
364
Marek Vasutabeb2722016-05-31 23:12:46 +0200365int sprintf(char *buf, const char *fmt, ...)
Simon Glass5c411d82016-05-14 14:02:53 -0600366{
Simon Glass45313e82016-08-04 21:58:14 -0600367 struct printf_info info;
Simon Glass5c411d82016-05-14 14:02:53 -0600368 va_list va;
369 int ret;
370
371 va_start(va, fmt);
Simon Glass45313e82016-08-04 21:58:14 -0600372 info.outstr = buf;
373 info.putc = putc_outstr;
374 ret = _vprintf(&info, fmt, va);
Simon Glass5c411d82016-05-14 14:02:53 -0600375 va_end(va);
Simon Glass45313e82016-08-04 21:58:14 -0600376 *info.outstr = '\0';
Simon Glass5c411d82016-05-14 14:02:53 -0600377
378 return ret;
379}
Marek Vasutabeb2722016-05-31 23:12:46 +0200380
Simon South9b3fbb22019-10-02 10:55:07 -0400381#if CONFIG_IS_ENABLED(LOG)
382/* Note that size is ignored */
383int vsnprintf(char *buf, size_t size, const char *fmt, va_list va)
384{
385 struct printf_info info;
386 int ret;
387
388 info.outstr = buf;
389 info.putc = putc_outstr;
390 ret = _vprintf(&info, fmt, va);
391 *info.outstr = '\0';
392
393 return ret;
394}
395#endif
396
Marek Vasutabeb2722016-05-31 23:12:46 +0200397/* Note that size is ignored */
398int snprintf(char *buf, size_t size, const char *fmt, ...)
399{
Simon Glass45313e82016-08-04 21:58:14 -0600400 struct printf_info info;
Marek Vasutabeb2722016-05-31 23:12:46 +0200401 va_list va;
402 int ret;
403
404 va_start(va, fmt);
Simon Glass45313e82016-08-04 21:58:14 -0600405 info.outstr = buf;
406 info.putc = putc_outstr;
407 ret = _vprintf(&info, fmt, va);
Marek Vasutabeb2722016-05-31 23:12:46 +0200408 va_end(va);
Simon Glass45313e82016-08-04 21:58:14 -0600409 *info.outstr = '\0';
Marek Vasutabeb2722016-05-31 23:12:46 +0200410
411 return ret;
412}
Simon Glassdee74e62019-10-21 17:26:44 -0600413
414void print_grouped_ull(unsigned long long int_val, int digits)
415{
416 /* Don't try to print the upper 32-bits */
417 printf("%ld ", (ulong)int_val);
418}