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+
17 * This code in here may execute before the DRAM is initialised, so
18 * we should make sure that it doesn't touch BSS, which some boards
21 static char *bf __attribute__ ((section(".data")));
22 static char zs __attribute__ ((section(".data")));
24 /* Current position in sprintf() output string */
25 static char *outstr __attribute__ ((section(".data")));
27 static void out(char c)
32 static void out_dgt(char dgt)
34 out(dgt + (dgt < 10 ? '0' : 'a' - 10));
38 static void div_out(unsigned int *num, unsigned int div)
40 unsigned char dgt = 0;
51 int _vprintf(const char *fmt, va_list va, void (*putc)(const char ch))
59 while ((ch = *(fmt++))) {
72 if (ch >= '0' && ch <= '9') {
74 while (ch >= '0' && ch <= '9') {
75 width = (width * 10) + ch - '0';
88 num = va_arg(va, unsigned int);
89 if (ch == 'd' && (int)num < 0) {
96 for (div = 1000000000; div; div /= 10)
101 num = va_arg(va, unsigned int);
105 for (div = 0x10000000; div; div /= 0x10)
110 out((char)(va_arg(va, int)));
113 p = va_arg(va, char*);
123 while (*bf++ && width > 0)
126 putc(lz ? '0' : ' ');
138 int vprintf(const char *fmt, va_list va)
140 return _vprintf(fmt, va, putc);
143 int printf(const char *fmt, ...)
149 ret = _vprintf(fmt, va, putc);
155 static void putc_outstr(char ch)
160 int sprintf(char *buf, const char *fmt, ...)
167 ret = _vprintf(fmt, va, putc_outstr);
174 /* Note that size is ignored */
175 int snprintf(char *buf, size_t size, const char *fmt, ...)
182 ret = _vprintf(fmt, va, putc_outstr);
189 void __assert_fail(const char *assertion, const char *file, unsigned line,
190 const char *function)
192 /* This will not return */
193 printf("%s:%u: %s: Assertion `%s' failed.", file, line, function,