tiny-printf.c 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Tiny printf version for SPL
 *
 * Copied from:
 * http://www.sparetimelabs.com/printfrevisited/printfrevisited.php
 *
 * Copyright (C) 2004,2008  Kustaa Nyholm
 *
 * SPDX-License-Identifier:	LGPL-2.1+
 */

#include <common.h>
#include <stdarg.h>
#include <serial.h>

16 17 18 19 20 21 22
/*
 * This code in here may execute before the DRAM is initialised, so
 * we should make sure that it doesn't touch BSS, which some boards
 * put in DRAM.
 */
static char *bf __attribute__ ((section(".data")));
static char zs __attribute__ ((section(".data")));
23

S
Simon Glass 已提交
24
/* Current position in sprintf() output string */
25
static char *outstr __attribute__ ((section(".data")));
S
Simon Glass 已提交
26

27 28 29 30 31 32 33
static void out(char c)
{
	*bf++ = c;
}

static void out_dgt(char dgt)
{
34
	out(dgt + (dgt < 10 ? '0' : 'a' - 10));
35 36 37
	zs = 1;
}

38
static void div_out(unsigned int *num, unsigned int div)
39 40 41
{
	unsigned char dgt = 0;

42 43
	while (*num >= div) {
		*num -= div;
44 45 46 47 48 49 50
		dgt++;
	}

	if (zs || dgt > 0)
		out_dgt(dgt);
}

S
Simon Glass 已提交
51
int _vprintf(const char *fmt, va_list va, void (*putc)(const char ch))
52 53 54
{
	char ch;
	char *p;
55 56 57
	unsigned int num;
	char buf[12];
	unsigned int div;
58 59 60 61 62

	while ((ch = *(fmt++))) {
		if (ch != '%') {
			putc(ch);
		} else {
S
Simon Glass 已提交
63 64
			bool lz = false;
			int width = 0;
65 66 67 68 69 70 71 72

			ch = *(fmt++);
			if (ch == '0') {
				ch = *(fmt++);
				lz = 1;
			}

			if (ch >= '0' && ch <= '9') {
S
Simon Glass 已提交
73
				width = 0;
74
				while (ch >= '0' && ch <= '9') {
S
Simon Glass 已提交
75
					width = (width * 10) + ch - '0';
76 77 78 79 80 81 82 83
					ch = *fmt++;
				}
			}
			bf = buf;
			p = bf;
			zs = 0;

			switch (ch) {
S
Simon Glass 已提交
84
			case '\0':
85 86 87 88 89 90 91 92
				goto abort;
			case 'u':
			case 'd':
				num = va_arg(va, unsigned int);
				if (ch == 'd' && (int)num < 0) {
					num = -(int)num;
					out('-');
				}
S
Simon Glass 已提交
93 94 95 96 97 98
				if (!num) {
					out_dgt(0);
				} else {
					for (div = 1000000000; div; div /= 10)
						div_out(&num, div);
				}
99 100 101
				break;
			case 'x':
				num = va_arg(va, unsigned int);
S
Simon Glass 已提交
102 103 104 105 106 107
				if (!num) {
					out_dgt(0);
				} else {
					for (div = 0x10000000; div; div /= 0x10)
						div_out(&num, div);
				}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
				break;
			case 'c':
				out((char)(va_arg(va, int)));
				break;
			case 's':
				p = va_arg(va, char*);
				break;
			case '%':
				out('%');
			default:
				break;
			}

			*bf = 0;
			bf = p;
S
Simon Glass 已提交
123 124 125
			while (*bf++ && width > 0)
				width--;
			while (width-- > 0)
126
				putc(lz ? '0' : ' ');
127 128 129 130
			if (p) {
				while ((ch = *p++))
					putc(ch);
			}
131 132 133 134 135 136
		}
	}

abort:
	return 0;
}
137

138 139 140 141 142
int vprintf(const char *fmt, va_list va)
{
	return _vprintf(fmt, va, putc);
}

143 144 145 146 147 148
int printf(const char *fmt, ...)
{
	va_list va;
	int ret;

	va_start(va, fmt);
S
Simon Glass 已提交
149 150 151 152 153 154 155 156 157 158 159
	ret = _vprintf(fmt, va, putc);
	va_end(va);

	return ret;
}

static void putc_outstr(char ch)
{
	*outstr++ = ch;
}

M
Marek Vasut 已提交
160
int sprintf(char *buf, const char *fmt, ...)
S
Simon Glass 已提交
161 162 163 164 165 166 167
{
	va_list va;
	int ret;

	va_start(va, fmt);
	outstr = buf;
	ret = _vprintf(fmt, va, putc_outstr);
168
	va_end(va);
S
Simon Glass 已提交
169
	*outstr = '\0';
170 171 172

	return ret;
}
M
Marek Vasut 已提交
173 174 175 176 177 178 179 180

/* Note that size is ignored */
int snprintf(char *buf, size_t size, const char *fmt, ...)
{
	va_list va;
	int ret;

	va_start(va, fmt);
181 182
	outstr = buf;
	ret = _vprintf(fmt, va, putc_outstr);
M
Marek Vasut 已提交
183
	va_end(va);
184
	*outstr = '\0';
M
Marek Vasut 已提交
185 186 187

	return ret;
}