blob: 411ae6189f29bcc3d81c0ae2f24623d2603878f6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: LGPL-2.1+
Stefan Roese363ab7b2015-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 Roese363ab7b2015-11-23 07:00:22 +01009 */
10
Matwey V. Kornilov4ae458c2021-08-05 22:06:05 +030011#include <log.h>
Stefan Roese363ab7b2015-11-23 07:00:22 +010012#include <serial.h>
Matwey V. Kornilov4ae458c2021-08-05 22:06:05 +030013#include <stdarg.h>
Vignesh R0bda3e42017-04-10 12:23:22 +053014#include <linux/ctype.h>
Stefan Roese363ab7b2015-11-23 07:00:22 +010015
Simon Glass5958cbe2016-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 Roese363ab7b2015-11-23 07:00:22 +010020
Simon Glass5958cbe2016-08-04 21:58:14 -060021 /* Output a character */
22 void (*putc)(struct printf_info *info, char ch);
23};
Simon Glass49138f82016-05-14 14:02:53 -060024
Simon Glass5958cbe2016-08-04 21:58:14 -060025static void out(struct printf_info *info, char c)
Stefan Roese363ab7b2015-11-23 07:00:22 +010026{
Simon Glass5958cbe2016-08-04 21:58:14 -060027 *info->bf++ = c;
Stefan Roese363ab7b2015-11-23 07:00:22 +010028}
29
Simon Glass5958cbe2016-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 Przywara32118682017-01-02 11:48:28 +000036static void div_out(struct printf_info *info, unsigned long *num,
37 unsigned long div)
Stefan Roese363ab7b2015-11-23 07:00:22 +010038{
39 unsigned char dgt = 0;
40
Stefan Roese26a552c2015-11-16 15:26:34 +010041 while (*num >= div) {
42 *num -= div;
Stefan Roese363ab7b2015-11-23 07:00:22 +010043 dgt++;
44 }
45
Simon Glass5958cbe2016-08-04 21:58:14 -060046 if (info->zs || dgt > 0)
47 out_dgt(info, dgt);
Stefan Roese363ab7b2015-11-23 07:00:22 +010048}
49
Simon Glass041a0ac2021-08-08 12:20:30 -060050#ifdef CONFIG_SPL_NET
Vignesh R0bda3e42017-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}
Christoph Niedermaier6a227952025-05-08 13:57:25 +0200144#endif /* CONFIG_SPL_NET */
Vignesh R0bda3e42017-04-10 12:23:22 +0530145
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
Christoph Niedermaier6a227952025-05-08 13:57:25 +0200160#if defined(CONFIG_SPL_USE_TINY_PRINTF_POINTER_SUPPORT) || defined(DEBUG)
161static void pointer(struct printf_info *info, const char *fmt, void *ptr)
Vignesh R0bda3e42017-04-10 12:23:22 +0530162{
Vignesh R0bda3e42017-04-10 12:23:22 +0530163 unsigned long num = (uintptr_t)ptr;
164 unsigned long div;
Vignesh R0bda3e42017-04-10 12:23:22 +0530165
166 switch (*fmt) {
Vignesh R0bda3e42017-04-10 12:23:22 +0530167 case 'a':
Vignesh R0bda3e42017-04-10 12:23:22 +0530168 switch (fmt[1]) {
169 case 'p':
170 default:
171 num = *(phys_addr_t *)ptr;
172 break;
173 }
174 break;
Simon Glass041a0ac2021-08-08 12:20:30 -0600175#ifdef CONFIG_SPL_NET
Vignesh R0bda3e42017-04-10 12:23:22 +0530176 case 'm':
177 return mac_address_string(info, ptr, false);
178 case 'M':
179 return mac_address_string(info, ptr, true);
180 case 'I':
181 if (fmt[1] == '4')
182 return ip4_addr_string(info, ptr);
Christoph Niedermaier6a227952025-05-08 13:57:25 +0200183#else
184 case 'm':
185 case 'M':
186 case 'I':
187 out(info, '?');
188 return;
Vignesh R0bda3e42017-04-10 12:23:22 +0530189#endif
190 default:
191 break;
192 }
Christoph Niedermaier6a227952025-05-08 13:57:25 +0200193
Vignesh R0bda3e42017-04-10 12:23:22 +0530194 div = 1UL << (sizeof(long) * 8 - 4);
195 for (; div; div /= 0x10)
196 div_out(info, &num, div);
Vignesh R0bda3e42017-04-10 12:23:22 +0530197}
Christoph Niedermaier6a227952025-05-08 13:57:25 +0200198#endif
Vignesh R0bda3e42017-04-10 12:23:22 +0530199
Masahiro Yamadac1cb8002017-02-12 18:08:43 +0900200static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
Stefan Roese363ab7b2015-11-23 07:00:22 +0100201{
Stefan Roese363ab7b2015-11-23 07:00:22 +0100202 char ch;
203 char *p;
Andre Przywara32118682017-01-02 11:48:28 +0000204 unsigned long num;
Stefan Roese26a552c2015-11-16 15:26:34 +0100205 char buf[12];
Andre Przywara32118682017-01-02 11:48:28 +0000206 unsigned long div;
Stefan Roese363ab7b2015-11-23 07:00:22 +0100207
Stefan Roese363ab7b2015-11-23 07:00:22 +0100208 while ((ch = *(fmt++))) {
209 if (ch != '%') {
Simon Glass5958cbe2016-08-04 21:58:14 -0600210 info->putc(info, ch);
Stefan Roese363ab7b2015-11-23 07:00:22 +0100211 } else {
Simon Glassb5afcad2016-05-14 14:02:52 -0600212 bool lz = false;
213 int width = 0;
Andre Przywara32118682017-01-02 11:48:28 +0000214 bool islong = false;
Alexander Sverdlin330bcd02025-02-20 13:49:07 +0100215 bool force_char = false;
Stefan Roese363ab7b2015-11-23 07:00:22 +0100216
217 ch = *(fmt++);
Andre Przywara7b303b82017-01-02 11:48:29 +0000218 if (ch == '-')
219 ch = *(fmt++);
220
Stefan Roese363ab7b2015-11-23 07:00:22 +0100221 if (ch == '0') {
222 ch = *(fmt++);
223 lz = 1;
224 }
225
226 if (ch >= '0' && ch <= '9') {
Simon Glassb5afcad2016-05-14 14:02:52 -0600227 width = 0;
Stefan Roese363ab7b2015-11-23 07:00:22 +0100228 while (ch >= '0' && ch <= '9') {
Simon Glassb5afcad2016-05-14 14:02:52 -0600229 width = (width * 10) + ch - '0';
Stefan Roese363ab7b2015-11-23 07:00:22 +0100230 ch = *fmt++;
231 }
232 }
Andre Przywara32118682017-01-02 11:48:28 +0000233 if (ch == 'l') {
234 ch = *(fmt++);
235 islong = true;
236 }
237
Simon Glass5958cbe2016-08-04 21:58:14 -0600238 info->bf = buf;
239 p = info->bf;
240 info->zs = 0;
Stefan Roese363ab7b2015-11-23 07:00:22 +0100241
242 switch (ch) {
Simon Glassb5afcad2016-05-14 14:02:52 -0600243 case '\0':
Stefan Roese363ab7b2015-11-23 07:00:22 +0100244 goto abort;
245 case 'u':
246 case 'd':
Marek Vasut3a6e18d2020-04-10 20:54:49 +0200247 case 'i':
Andre Przywara32118682017-01-02 11:48:28 +0000248 div = 1000000000;
249 if (islong) {
250 num = va_arg(va, unsigned long);
251 if (sizeof(long) > 4)
252 div *= div * 10;
253 } else {
254 num = va_arg(va, unsigned int);
255 }
256
Marek Vasut3a6e18d2020-04-10 20:54:49 +0200257 if (ch != 'u') {
Andre Przywara32118682017-01-02 11:48:28 +0000258 if (islong && (long)num < 0) {
259 num = -(long)num;
260 out(info, '-');
261 } else if (!islong && (int)num < 0) {
262 num = -(int)num;
263 out(info, '-');
264 }
Stefan Roese363ab7b2015-11-23 07:00:22 +0100265 }
Simon Glassbdfb70b2016-01-05 09:30:57 -0700266 if (!num) {
Simon Glass5958cbe2016-08-04 21:58:14 -0600267 out_dgt(info, 0);
Simon Glassbdfb70b2016-01-05 09:30:57 -0700268 } else {
Andre Przywara32118682017-01-02 11:48:28 +0000269 for (; div; div /= 10)
Simon Glass5958cbe2016-08-04 21:58:14 -0600270 div_out(info, &num, div);
Simon Glassbdfb70b2016-01-05 09:30:57 -0700271 }
Stefan Roese363ab7b2015-11-23 07:00:22 +0100272 break;
Christoph Niedermaier6a227952025-05-08 13:57:25 +0200273#if defined(CONFIG_SPL_USE_TINY_PRINTF_POINTER_SUPPORT) || defined(DEBUG)
Simon Glassbf295d72019-10-21 17:26:45 -0600274 case 'p':
Christoph Niedermaier6a227952025-05-08 13:57:25 +0200275 pointer(info, fmt, va_arg(va, void *));
276 /*
277 * Skip this because it pulls in _ctype which is
278 * 256 bytes, and we don't generally implement
279 * pointer anyway
280 */
281 while (isalnum(fmt[0]))
282 fmt++;
283 break;
284#endif
Stefan Roese363ab7b2015-11-23 07:00:22 +0100285 case 'x':
Christoph Niedermaier023c4862025-03-20 20:01:47 +0100286 case 'X':
Andre Przywara32118682017-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 Glassbdfb70b2016-01-05 09:30:57 -0700294 if (!num) {
Simon Glass5958cbe2016-08-04 21:58:14 -0600295 out_dgt(info, 0);
Simon Glassbdfb70b2016-01-05 09:30:57 -0700296 } else {
Andre Przywara32118682017-01-02 11:48:28 +0000297 for (; div; div /= 0x10)
Simon Glass5958cbe2016-08-04 21:58:14 -0600298 div_out(info, &num, div);
Simon Glassbdfb70b2016-01-05 09:30:57 -0700299 }
Stefan Roese363ab7b2015-11-23 07:00:22 +0100300 break;
301 case 'c':
Simon Glass5958cbe2016-08-04 21:58:14 -0600302 out(info, (char)(va_arg(va, int)));
Alexander Sverdlin330bcd02025-02-20 13:49:07 +0100303 /* For the case when it's \0 char */
304 force_char = true;
Stefan Roese363ab7b2015-11-23 07:00:22 +0100305 break;
306 case 's':
307 p = va_arg(va, char*);
308 break;
309 case '%':
Simon Glass5958cbe2016-08-04 21:58:14 -0600310 out(info, '%');
Christoph Niedermaier6a227952025-05-08 13:57:25 +0200311 break;
Stefan Roese363ab7b2015-11-23 07:00:22 +0100312 default:
Christoph Niedermaier6a227952025-05-08 13:57:25 +0200313 out(info, '?');
Stefan Roese363ab7b2015-11-23 07:00:22 +0100314 break;
315 }
316
Simon Glass5958cbe2016-08-04 21:58:14 -0600317 *info->bf = 0;
318 info->bf = p;
Benedikt Spranger076afcd2024-10-18 10:30:02 +0200319 while (width > 0 && info->bf && *info->bf++)
Simon Glassb5afcad2016-05-14 14:02:52 -0600320 width--;
321 while (width-- > 0)
Simon Glass5958cbe2016-08-04 21:58:14 -0600322 info->putc(info, lz ? '0' : ' ');
Simon Glass5bd9f6c2015-12-29 05:22:46 -0700323 if (p) {
Alexander Sverdlin330bcd02025-02-20 13:49:07 +0100324 while ((ch = *p++) || force_char) {
Simon Glass5958cbe2016-08-04 21:58:14 -0600325 info->putc(info, ch);
Alexander Sverdlin330bcd02025-02-20 13:49:07 +0100326 force_char = false;
327 }
Simon Glass5bd9f6c2015-12-29 05:22:46 -0700328 }
Stefan Roese363ab7b2015-11-23 07:00:22 +0100329 }
330 }
331
332abort:
Stefan Roese363ab7b2015-11-23 07:00:22 +0100333 return 0;
334}
Sjoerd Simons1168ed52015-12-04 23:27:37 +0100335
Alex Kiernand07d0a02018-04-19 04:32:55 +0000336#if CONFIG_IS_ENABLED(PRINTF)
337static void putc_normal(struct printf_info *info, char ch)
338{
339 putc(ch);
340}
341
Hans de Goede5f183982016-06-10 21:03:34 +0200342int vprintf(const char *fmt, va_list va)
343{
Simon Glass5958cbe2016-08-04 21:58:14 -0600344 struct printf_info info;
345
346 info.putc = putc_normal;
347 return _vprintf(&info, fmt, va);
Hans de Goede5f183982016-06-10 21:03:34 +0200348}
349
Sjoerd Simons1168ed52015-12-04 23:27:37 +0100350int printf(const char *fmt, ...)
351{
Simon Glass5958cbe2016-08-04 21:58:14 -0600352 struct printf_info info;
353
Sjoerd Simons1168ed52015-12-04 23:27:37 +0100354 va_list va;
355 int ret;
356
Simon Glass5958cbe2016-08-04 21:58:14 -0600357 info.putc = putc_normal;
Sjoerd Simons1168ed52015-12-04 23:27:37 +0100358 va_start(va, fmt);
Simon Glass5958cbe2016-08-04 21:58:14 -0600359 ret = _vprintf(&info, fmt, va);
Simon Glass49138f82016-05-14 14:02:53 -0600360 va_end(va);
361
362 return ret;
363}
Alex Kiernand07d0a02018-04-19 04:32:55 +0000364#endif
Simon Glass49138f82016-05-14 14:02:53 -0600365
Simon Glass5958cbe2016-08-04 21:58:14 -0600366static void putc_outstr(struct printf_info *info, char ch)
Simon Glass49138f82016-05-14 14:02:53 -0600367{
Simon Glass5958cbe2016-08-04 21:58:14 -0600368 *info->outstr++ = ch;
Simon Glass49138f82016-05-14 14:02:53 -0600369}
370
Marek Vasut8dc095b2016-05-31 23:12:46 +0200371int sprintf(char *buf, const char *fmt, ...)
Simon Glass49138f82016-05-14 14:02:53 -0600372{
Simon Glass5958cbe2016-08-04 21:58:14 -0600373 struct printf_info info;
Simon Glass49138f82016-05-14 14:02:53 -0600374 va_list va;
Simon Glass49138f82016-05-14 14:02:53 -0600375
376 va_start(va, fmt);
Simon Glass5958cbe2016-08-04 21:58:14 -0600377 info.outstr = buf;
378 info.putc = putc_outstr;
Simon Glass121dd492024-09-20 09:24:29 +0200379 _vprintf(&info, fmt, va);
Sjoerd Simons1168ed52015-12-04 23:27:37 +0100380 va_end(va);
Simon Glass5958cbe2016-08-04 21:58:14 -0600381 *info.outstr = '\0';
Sjoerd Simons1168ed52015-12-04 23:27:37 +0100382
Simon Glass121dd492024-09-20 09:24:29 +0200383 return info.outstr - buf;
Sjoerd Simons1168ed52015-12-04 23:27:37 +0100384}
Marek Vasut8dc095b2016-05-31 23:12:46 +0200385
Simon Southc38ca5f2019-10-02 10:55:07 -0400386#if CONFIG_IS_ENABLED(LOG)
387/* Note that size is ignored */
388int vsnprintf(char *buf, size_t size, const char *fmt, va_list va)
389{
390 struct printf_info info;
Simon Southc38ca5f2019-10-02 10:55:07 -0400391
392 info.outstr = buf;
393 info.putc = putc_outstr;
Simon Glass121dd492024-09-20 09:24:29 +0200394 _vprintf(&info, fmt, va);
Simon Southc38ca5f2019-10-02 10:55:07 -0400395 *info.outstr = '\0';
396
Simon Glass121dd492024-09-20 09:24:29 +0200397 return info.outstr - buf;
Simon Southc38ca5f2019-10-02 10:55:07 -0400398}
399#endif
400
Marek Vasut8dc095b2016-05-31 23:12:46 +0200401/* Note that size is ignored */
402int snprintf(char *buf, size_t size, const char *fmt, ...)
403{
Simon Glass5958cbe2016-08-04 21:58:14 -0600404 struct printf_info info;
Marek Vasut8dc095b2016-05-31 23:12:46 +0200405 va_list va;
Marek Vasut8dc095b2016-05-31 23:12:46 +0200406
407 va_start(va, fmt);
Simon Glass5958cbe2016-08-04 21:58:14 -0600408 info.outstr = buf;
409 info.putc = putc_outstr;
Simon Glass121dd492024-09-20 09:24:29 +0200410 _vprintf(&info, fmt, va);
Marek Vasut8dc095b2016-05-31 23:12:46 +0200411 va_end(va);
Simon Glass5958cbe2016-08-04 21:58:14 -0600412 *info.outstr = '\0';
Marek Vasut8dc095b2016-05-31 23:12:46 +0200413
Simon Glass121dd492024-09-20 09:24:29 +0200414 return info.outstr - buf;
Marek Vasut8dc095b2016-05-31 23:12:46 +0200415}
Simon Glassf6562312019-10-21 17:26:44 -0600416
417void print_grouped_ull(unsigned long long int_val, int digits)
418{
419 /* Don't try to print the upper 32-bits */
420 printf("%ld ", (ulong)int_val);
421}