2 * Tiny printf version for SPL
5 * http://www.sparetimelabs.com/printfrevisited/printfrevisited.php
7 * Copyright (C) 2004,2008 Kustaa Nyholm
9 * SPDX-License-Identifier: LGPL-2.1+
15 #include <linux/ctype.h>
18 char *bf; /* Digit buffer */
19 char zs; /* non-zero if a digit has been written */
20 char *outstr; /* Next output position for sprintf() */
22 /* Output a character */
23 void (*putc)(struct printf_info *info, char ch);
26 static void out(struct printf_info *info, char c)
31 static void out_dgt(struct printf_info *info, char dgt)
33 out(info, dgt + (dgt < 10 ? '0' : 'a' - 10));
37 static void div_out(struct printf_info *info, unsigned long *num,
40 unsigned char dgt = 0;
47 if (info->zs || dgt > 0)
51 #ifdef CONFIG_SPL_NET_SUPPORT
52 static void string(struct printf_info *info, char *s)
60 static 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]
64 static inline char *pack_hex_byte(char *buf, u8 byte)
66 *buf++ = hex_asc_hi(byte);
67 *buf++ = hex_asc_lo(byte);
71 static void mac_address_string(struct printf_info *info, u8 *addr,
74 /* (6 * 2 hex digits), 5 colons and trailing zero */
79 for (i = 0; i < 6; i++) {
80 p = pack_hex_byte(p, addr[i]);
81 if (separator && i != 5)
86 string(info, mac_addr);
89 static char *put_dec_trunc(char *buf, unsigned int q)
91 unsigned int d3, d2, d1, d0;
96 d0 = 6 * (d3 + d2 + d1) + (q & 0xf);
97 q = (d0 * 0xcd) >> 11;
99 *buf++ = d0 + '0'; /* least significant digit */
100 d1 = q + 9 * d3 + 5 * d2 + d1;
102 q = (d1 * 0xcd) >> 11;
104 *buf++ = d1 + '0'; /* next digit */
107 if ((d2 != 0) || (d3 != 0)) {
110 *buf++ = d2 + '0'; /* next digit */
114 q = (d3 * 0xcd) >> 11;
116 *buf++ = d3 + '0'; /* next digit */
118 *buf++ = q + '0'; /* most sign. digit */
125 static void ip4_addr_string(struct printf_info *info, u8 *addr)
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 */
133 for (i = 0; i < 4; i++) {
134 digits = put_dec_trunc(temp, addr[i]) - temp;
135 /* reverse the digits in the quad */
143 string(info, ip4_addr);
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
152 * Right now we handle:
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
161 static void pointer(struct printf_info *info, const char *fmt, void *ptr)
164 unsigned long num = (uintptr_t)ptr;
175 num = *(phys_addr_t *)ptr;
180 #ifdef CONFIG_SPL_NET_SUPPORT
182 return mac_address_string(info, ptr, false);
184 return mac_address_string(info, ptr, true);
187 return ip4_addr_string(info, ptr);
193 div = 1UL << (sizeof(long) * 8 - 4);
194 for (; div; div /= 0x10)
195 div_out(info, &num, div);
199 static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
207 while ((ch = *(fmt++))) {
209 info->putc(info, ch);
224 if (ch >= '0' && ch <= '9') {
226 while (ch >= '0' && ch <= '9') {
227 width = (width * 10) + ch - '0';
247 num = va_arg(va, unsigned long);
248 if (sizeof(long) > 4)
251 num = va_arg(va, unsigned int);
255 if (islong && (long)num < 0) {
258 } else if (!islong && (int)num < 0) {
266 for (; div; div /= 10)
267 div_out(info, &num, div);
272 num = va_arg(va, unsigned long);
273 div = 1UL << (sizeof(long) * 8 - 4);
275 num = va_arg(va, unsigned int);
281 for (; div; div /= 0x10)
282 div_out(info, &num, div);
286 out(info, (char)(va_arg(va, int)));
289 p = va_arg(va, char*);
292 pointer(info, fmt, va_arg(va, void *));
293 while (isalnum(fmt[0]))
304 while (*info->bf++ && width > 0)
307 info->putc(info, lz ? '0' : ' ');
310 info->putc(info, ch);
319 #if CONFIG_IS_ENABLED(PRINTF)
320 static void putc_normal(struct printf_info *info, char ch)
325 int vprintf(const char *fmt, va_list va)
327 struct printf_info info;
329 info.putc = putc_normal;
330 return _vprintf(&info, fmt, va);
333 int printf(const char *fmt, ...)
335 struct printf_info info;
340 info.putc = putc_normal;
342 ret = _vprintf(&info, fmt, va);
349 static void putc_outstr(struct printf_info *info, char ch)
351 *info->outstr++ = ch;
354 int sprintf(char *buf, const char *fmt, ...)
356 struct printf_info info;
362 info.putc = putc_outstr;
363 ret = _vprintf(&info, fmt, va);
370 /* Note that size is ignored */
371 int snprintf(char *buf, size_t size, const char *fmt, ...)
373 struct printf_info info;
379 info.putc = putc_outstr;
380 ret = _vprintf(&info, fmt, va);