mirror of
https://github.com/Fishwaldo/u-boot.git
synced 2025-03-20 22:21:41 +00:00
This patch adds a small printf() version that supports all basic formats. Its intented to be used in U-Boot SPL versions on platforms with very limited internal RAM sizes. To enable it, just define CONFIG_USE_TINY_PRINTF in your defconfig. This will result in the SPL using this tiny function and the main U-Boot still using the full-blown printf() function. This code was copied from: http://www.sparetimelabs.com/printfrevisited With mostly only coding style related changes so that its checkpatch clean. The size reduction is about 2.5KiB. Here a comparison for the db-mv784mp-gp (Marvell AXP) SPL: Without this patch: 58963 18536 1928 79427 13643 ./spl/u-boot-spl With this patch: 56542 18536 1956 77034 12cea ./spl/u-boot-spl Note: To make it possible to compile tiny-printf.c instead of vsprintf.c when CONFIG_USE_TINY_PRINTF is defined, the functions printf() and vprintf() are moved from common/console.c into vsprintf.c in this patch. Signed-off-by: Stefan Roese <sr@denx.de> Cc: Simon Glass <sjg@chromium.org> Cc: Hans de Goede <hdegoede@redhat.com> Cc: Tom Rini <trini@konsulko.com> Cc: Albert Aribaud <albert.u.boot@aribaud.net>
130 lines
1.9 KiB
C
130 lines
1.9 KiB
C
/*
|
|
* 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>
|
|
|
|
static char *bf;
|
|
static char buf[12];
|
|
static unsigned int num;
|
|
static char uc;
|
|
static char zs;
|
|
|
|
static void out(char c)
|
|
{
|
|
*bf++ = c;
|
|
}
|
|
|
|
static void out_dgt(char dgt)
|
|
{
|
|
out(dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10));
|
|
zs = 1;
|
|
}
|
|
|
|
static void div_out(unsigned int div)
|
|
{
|
|
unsigned char dgt = 0;
|
|
|
|
num &= 0xffff; /* just for testing the code with 32 bit ints */
|
|
while (num >= div) {
|
|
num -= div;
|
|
dgt++;
|
|
}
|
|
|
|
if (zs || dgt > 0)
|
|
out_dgt(dgt);
|
|
}
|
|
|
|
int printf(const char *fmt, ...)
|
|
{
|
|
va_list va;
|
|
char ch;
|
|
char *p;
|
|
|
|
va_start(va, fmt);
|
|
|
|
while ((ch = *(fmt++))) {
|
|
if (ch != '%') {
|
|
putc(ch);
|
|
} else {
|
|
char lz = 0;
|
|
char w = 0;
|
|
|
|
ch = *(fmt++);
|
|
if (ch == '0') {
|
|
ch = *(fmt++);
|
|
lz = 1;
|
|
}
|
|
|
|
if (ch >= '0' && ch <= '9') {
|
|
w = 0;
|
|
while (ch >= '0' && ch <= '9') {
|
|
w = (((w << 2) + w) << 1) + ch - '0';
|
|
ch = *fmt++;
|
|
}
|
|
}
|
|
bf = buf;
|
|
p = bf;
|
|
zs = 0;
|
|
|
|
switch (ch) {
|
|
case 0:
|
|
goto abort;
|
|
case 'u':
|
|
case 'd':
|
|
num = va_arg(va, unsigned int);
|
|
if (ch == 'd' && (int)num < 0) {
|
|
num = -(int)num;
|
|
out('-');
|
|
}
|
|
div_out(10000);
|
|
div_out(1000);
|
|
div_out(100);
|
|
div_out(10);
|
|
out_dgt(num);
|
|
break;
|
|
case 'x':
|
|
case 'X':
|
|
uc = ch == 'X';
|
|
num = va_arg(va, unsigned int);
|
|
div_out(0x1000);
|
|
div_out(0x100);
|
|
div_out(0x10);
|
|
out_dgt(num);
|
|
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;
|
|
while (*bf++ && w > 0)
|
|
w--;
|
|
while (w-- > 0)
|
|
putc(lz ? '0' : ' ');
|
|
while ((ch = *p++))
|
|
putc(ch);
|
|
}
|
|
}
|
|
|
|
abort:
|
|
va_end(va);
|
|
return 0;
|
|
}
|