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 putc_normal(struct printf_info *info, char ch)
31 static void out(struct printf_info *info, char c)
36 static void out_dgt(struct printf_info *info, char dgt)
38 out(info, dgt + (dgt < 10 ? '0' : 'a' - 10));
42 static void div_out(struct printf_info *info, unsigned long *num,
45 unsigned char dgt = 0;
52 if (info->zs || dgt > 0)
56 #ifdef CONFIG_SPL_NET_SUPPORT
57 static void string(struct printf_info *info, char *s)
65 static const char hex_asc[] = "0123456789abcdef";
66 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
67 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
69 static inline char *pack_hex_byte(char *buf, u8 byte)
71 *buf++ = hex_asc_hi(byte);
72 *buf++ = hex_asc_lo(byte);
76 static void mac_address_string(struct printf_info *info, u8 *addr,
79 /* (6 * 2 hex digits), 5 colons and trailing zero */
84 for (i = 0; i < 6; i++) {
85 p = pack_hex_byte(p, addr[i]);
86 if (separator && i != 5)
91 string(info, mac_addr);
94 static char *put_dec_trunc(char *buf, unsigned int q)
96 unsigned int d3, d2, d1, d0;
101 d0 = 6 * (d3 + d2 + d1) + (q & 0xf);
102 q = (d0 * 0xcd) >> 11;
104 *buf++ = d0 + '0'; /* least significant digit */
105 d1 = q + 9 * d3 + 5 * d2 + d1;
107 q = (d1 * 0xcd) >> 11;
109 *buf++ = d1 + '0'; /* next digit */
112 if ((d2 != 0) || (d3 != 0)) {
115 *buf++ = d2 + '0'; /* next digit */
119 q = (d3 * 0xcd) >> 11;
121 *buf++ = d3 + '0'; /* next digit */
123 *buf++ = q + '0'; /* most sign. digit */
130 static void ip4_addr_string(struct printf_info *info, u8 *addr)
132 /* (4 * 3 decimal digits), 3 dots and trailing zero */
133 char ip4_addr[4 * 4];
134 char temp[3]; /* hold each IP quad in reverse order */
138 for (i = 0; i < 4; i++) {
139 digits = put_dec_trunc(temp, addr[i]) - temp;
140 /* reverse the digits in the quad */
148 string(info, ip4_addr);
153 * Show a '%p' thing. A kernel extension is that the '%p' is followed
154 * by an extra set of characters that are extended format
157 * Right now we handle:
159 * - 'M' For a 6-byte MAC address, it prints the address in the
160 * usual colon-separated hex notation.
161 * - 'm' Same as above except there is no colon-separator.
162 * - 'I4'for IPv4 addresses printed in the usual way (dot-separated
166 static void pointer(struct printf_info *info, const char *fmt, void *ptr)
169 unsigned long num = (uintptr_t)ptr;
180 num = *(phys_addr_t *)ptr;
185 #ifdef CONFIG_SPL_NET_SUPPORT
187 return mac_address_string(info, ptr, false);
189 return mac_address_string(info, ptr, true);
192 return ip4_addr_string(info, ptr);
198 div = 1UL << (sizeof(long) * 8 - 4);
199 for (; div; div /= 0x10)
200 div_out(info, &num, div);
204 static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
212 while ((ch = *(fmt++))) {
214 info->putc(info, ch);
229 if (ch >= '0' && ch <= '9') {
231 while (ch >= '0' && ch <= '9') {
232 width = (width * 10) + ch - '0';
252 num = va_arg(va, unsigned long);
253 if (sizeof(long) > 4)
256 num = va_arg(va, unsigned int);
260 if (islong && (long)num < 0) {
263 } else if (!islong && (int)num < 0) {
271 for (; div; div /= 10)
272 div_out(info, &num, div);
277 num = va_arg(va, unsigned long);
278 div = 1UL << (sizeof(long) * 8 - 4);
280 num = va_arg(va, unsigned int);
286 for (; div; div /= 0x10)
287 div_out(info, &num, div);
291 out(info, (char)(va_arg(va, int)));
294 p = va_arg(va, char*);
297 pointer(info, fmt, va_arg(va, void *));
298 while (isalnum(fmt[0]))
309 while (*info->bf++ && width > 0)
312 info->putc(info, lz ? '0' : ' ');
315 info->putc(info, ch);
324 int vprintf(const char *fmt, va_list va)
326 struct printf_info info;
328 info.putc = putc_normal;
329 return _vprintf(&info, fmt, va);
332 int printf(const char *fmt, ...)
334 struct printf_info info;
339 info.putc = putc_normal;
341 ret = _vprintf(&info, fmt, va);
347 static void putc_outstr(struct printf_info *info, char ch)
349 *info->outstr++ = ch;
352 int sprintf(char *buf, const char *fmt, ...)
354 struct printf_info info;
360 info.putc = putc_outstr;
361 ret = _vprintf(&info, fmt, va);
368 /* Note that size is ignored */
369 int snprintf(char *buf, size_t size, const char *fmt, ...)
371 struct printf_info info;
377 info.putc = putc_outstr;
378 ret = _vprintf(&info, fmt, va);
385 void __assert_fail(const char *assertion, const char *file, unsigned line,
386 const char *function)
388 /* This will not return */
389 printf("%s:%u: %s: Assertion `%s' failed.", file, line, function,