]> git.sur5r.net Git - u-boot/blobdiff - lib/tiny-printf.c
mx6slevk: Avoid hardcoded RAM size
[u-boot] / lib / tiny-printf.c
index d743a364e944497bd827af50e56625c0655cfcc4..b334f053cc0ee2645aa3669701f0ec22419bb572 100644 (file)
 #include <stdarg.h>
 #include <serial.h>
 
-static char *bf;
-static char buf[12];
-static unsigned int num;
-static char uc;
-static char zs;
+/*
+ * 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")));
+
+/* Current position in sprintf() output string */
+static char *outstr __attribute__ ((section(".data")));
 
 static void out(char c)
 {
@@ -26,17 +31,16 @@ static void out(char c)
 
 static void out_dgt(char dgt)
 {
-       out(dgt + (dgt < 10 ? '0' : (uc ? 'A' : 'a') - 10));
+       out(dgt + (dgt < 10 ? '0' : 'a' - 10));
        zs = 1;
 }
 
-static void div_out(unsigned int div)
+static void div_out(unsigned int *num, unsigned int div)
 {
        unsigned char dgt = 0;
 
-       num &= 0xffff; /* just for testing the code with 32 bit ints */
-       while (num >= div) {
-               num -= div;
+       while (*num >= div) {
+               *num -= div;
                dgt++;
        }
 
@@ -44,20 +48,20 @@ static void div_out(unsigned int div)
                out_dgt(dgt);
 }
 
-int printf(const char *fmt, ...)
+int _vprintf(const char *fmt, va_list va, void (*putc)(const char ch))
 {
-       va_list va;
        char ch;
        char *p;
-
-       va_start(va, fmt);
+       unsigned int num;
+       char buf[12];
+       unsigned int div;
 
        while ((ch = *(fmt++))) {
                if (ch != '%') {
                        putc(ch);
                } else {
-                       char lz = 0;
-                       char w = 0;
+                       bool lz = false;
+                       int width = 0;
 
                        ch = *(fmt++);
                        if (ch == '0') {
@@ -66,9 +70,9 @@ int printf(const char *fmt, ...)
                        }
 
                        if (ch >= '0' && ch <= '9') {
-                               w = 0;
+                               width = 0;
                                while (ch >= '0' && ch <= '9') {
-                                       w = (((w << 2) + w) << 1) + ch - '0';
+                                       width = (width * 10) + ch - '0';
                                        ch = *fmt++;
                                }
                        }
@@ -77,7 +81,7 @@ int printf(const char *fmt, ...)
                        zs = 0;
 
                        switch (ch) {
-                       case 0:
+                       case '\0':
                                goto abort;
                        case 'u':
                        case 'd':
@@ -86,20 +90,21 @@ int printf(const char *fmt, ...)
                                        num = -(int)num;
                                        out('-');
                                }
-                               div_out(10000);
-                               div_out(1000);
-                               div_out(100);
-                               div_out(10);
-                               out_dgt(num);
+                               if (!num) {
+                                       out_dgt(0);
+                               } else {
+                                       for (div = 1000000000; div; div /= 10)
+                                               div_out(&num, div);
+                               }
                                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);
+                               if (!num) {
+                                       out_dgt(0);
+                               } else {
+                                       for (div = 0x10000000; div; div /= 0x10)
+                                               div_out(&num, div);
+                               }
                                break;
                        case 'c':
                                out((char)(va_arg(va, int)));
@@ -115,16 +120,68 @@ int printf(const char *fmt, ...)
 
                        *bf = 0;
                        bf = p;
-                       while (*bf++ && w > 0)
-                               w--;
-                       while (w-- > 0)
+                       while (*bf++ && width > 0)
+                               width--;
+                       while (width-- > 0)
                                putc(lz ? '0' : ' ');
-                       while ((ch = *p++))
-                               putc(ch);
+                       if (p) {
+                               while ((ch = *p++))
+                                       putc(ch);
+                       }
                }
        }
 
 abort:
-       va_end(va);
        return 0;
 }
+
+int vprintf(const char *fmt, va_list va)
+{
+       return _vprintf(fmt, va, putc);
+}
+
+int printf(const char *fmt, ...)
+{
+       va_list va;
+       int ret;
+
+       va_start(va, fmt);
+       ret = _vprintf(fmt, va, putc);
+       va_end(va);
+
+       return ret;
+}
+
+static void putc_outstr(char ch)
+{
+       *outstr++ = ch;
+}
+
+int sprintf(char *buf, const char *fmt, ...)
+{
+       va_list va;
+       int ret;
+
+       va_start(va, fmt);
+       outstr = buf;
+       ret = _vprintf(fmt, va, putc_outstr);
+       va_end(va);
+       *outstr = '\0';
+
+       return ret;
+}
+
+/* Note that size is ignored */
+int snprintf(char *buf, size_t size, const char *fmt, ...)
+{
+       va_list va;
+       int ret;
+
+       va_start(va, fmt);
+       outstr = buf;
+       ret = _vprintf(fmt, va, putc_outstr);
+       va_end(va);
+       *outstr = '\0';
+
+       return ret;
+}