]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - lib/tiny-printf.c
1 // SPDX-License-Identifier: LGPL-2.1+
3 * Tiny printf version for SPL
6 * http://www.sparetimelabs.com/printfrevisited/printfrevisited.php
8 * Copyright (C) 2004,2008 Kustaa Nyholm
14 #include <linux/ctype.h>
17 char *bf
; /* Digit buffer */
18 char zs
; /* non-zero if a digit has been written */
19 char *outstr
; /* Next output position for sprintf() */
21 /* Output a character */
22 void (*putc
)(struct printf_info
*info
, char ch
);
25 static void out(struct printf_info
*info
, char c
)
30 static void out_dgt(struct printf_info
*info
, char dgt
)
32 out(info
, dgt
+ (dgt
< 10 ? '0' : 'a' - 10));
36 static void div_out(struct printf_info
*info
, unsigned long *num
,
39 unsigned char dgt
= 0;
46 if (info
->zs
|| dgt
> 0)
51 static void string(struct printf_info
*info
, char *s
)
59 static 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]
63 static inline char *pack_hex_byte(char *buf
, u8 byte
)
65 *buf
++ = hex_asc_hi(byte
);
66 *buf
++ = hex_asc_lo(byte
);
70 static void mac_address_string(struct printf_info
*info
, u8
*addr
,
73 /* (6 * 2 hex digits), 5 colons and trailing zero */
78 for (i
= 0; i
< 6; i
++) {
79 p
= pack_hex_byte(p
, addr
[i
]);
80 if (separator
&& i
!= 5)
85 string(info
, mac_addr
);
88 static char *put_dec_trunc(char *buf
, unsigned int q
)
90 unsigned int d3
, d2
, d1
, d0
;
95 d0
= 6 * (d3
+ d2
+ d1
) + (q
& 0xf);
96 q
= (d0
* 0xcd) >> 11;
98 *buf
++ = d0
+ '0'; /* least significant digit */
99 d1
= q
+ 9 * d3
+ 5 * d2
+ d1
;
101 q
= (d1
* 0xcd) >> 11;
103 *buf
++ = d1
+ '0'; /* next digit */
106 if ((d2
!= 0) || (d3
!= 0)) {
109 *buf
++ = d2
+ '0'; /* next digit */
113 q
= (d3
* 0xcd) >> 11;
115 *buf
++ = d3
+ '0'; /* next digit */
117 *buf
++ = q
+ '0'; /* most sign. digit */
124 static void ip4_addr_string(struct printf_info
*info
, u8
*addr
)
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 */
132 for (i
= 0; i
< 4; i
++) {
133 digits
= put_dec_trunc(temp
, addr
[i
]) - temp
;
134 /* reverse the digits in the quad */
142 string(info
, ip4_addr
);
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
151 * Right now we handle:
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
160 static void __maybe_unused
pointer(struct printf_info
*info
, const char *fmt
,
164 unsigned long num
= (uintptr_t)ptr
;
175 num
= *(phys_addr_t
*)ptr
;
180 #ifdef CONFIG_SPL_NET
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';
248 num
= va_arg(va
, unsigned long);
249 if (sizeof(long) > 4)
252 num
= va_arg(va
, unsigned int);
256 if (islong
&& (long)num
< 0) {
259 } else if (!islong
&& (int)num
< 0) {
267 for (; div
; div
/= 10)
268 div_out(info
, &num
, div
);
272 if (CONFIG_IS_ENABLED(NET
) || _DEBUG
) {
273 pointer(info
, fmt
, va_arg(va
, void *));
275 * Skip this because it pulls in _ctype which is
276 * 256 bytes, and we don't generally implement
279 while (isalnum(fmt
[0]))
287 num
= va_arg(va
, unsigned long);
288 div
= 1UL << (sizeof(long) * 8 - 4);
290 num
= va_arg(va
, unsigned int);
296 for (; div
; div
/= 0x10)
297 div_out(info
, &num
, div
);
301 out(info
, (char)(va_arg(va
, int)));
304 p
= va_arg(va
, char*);
314 while (*info
->bf
++ && width
> 0)
317 info
->putc(info
, lz
? '0' : ' ');
320 info
->putc(info
, ch
);
329 #if CONFIG_IS_ENABLED(PRINTF)
330 static void putc_normal(struct printf_info
*info
, char ch
)
335 int vprintf(const char *fmt
, va_list va
)
337 struct printf_info info
;
339 info
.putc
= putc_normal
;
340 return _vprintf(&info
, fmt
, va
);
343 int printf(const char *fmt
, ...)
345 struct printf_info info
;
350 info
.putc
= putc_normal
;
352 ret
= _vprintf(&info
, fmt
, va
);
359 static void putc_outstr(struct printf_info
*info
, char ch
)
361 *info
->outstr
++ = ch
;
364 int sprintf(char *buf
, const char *fmt
, ...)
366 struct printf_info info
;
372 info
.putc
= putc_outstr
;
373 ret
= _vprintf(&info
, fmt
, va
);
380 #if CONFIG_IS_ENABLED(LOG)
381 /* Note that size is ignored */
382 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list va
)
384 struct printf_info info
;
388 info
.putc
= putc_outstr
;
389 ret
= _vprintf(&info
, fmt
, va
);
396 /* Note that size is ignored */
397 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
399 struct printf_info info
;
405 info
.putc
= putc_outstr
;
406 ret
= _vprintf(&info
, fmt
, va
);
413 void print_grouped_ull(unsigned long long int_val
, int digits
)
415 /* Don't try to print the upper 32-bits */
416 printf("%ld ", (ulong
)int_val
);