Working SDHOST/FatFS, boot partition mounts, some other minor fixes too.
[rpi-open-firmware.git] / lib / xprintf.c
1 /*------------------------------------------------------------------------/
2 / Universal string handler for user console interface
3 /-------------------------------------------------------------------------/
4 /
5 / Copyright (C) 2011, ChaN, all right reserved.
6 /
7 / * This software is a free software and there is NO WARRANTY.
8 / * No restriction on use. You can use, modify and redistribute it for
9 / personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
10 / * Redistributions of source code must retain the above copyright notice.
11 /
12 /-------------------------------------------------------------------------*/
13
14 #include "xprintf.h"
15
16 extern void uart_putc(unsigned int ch);
17
18 /*----------------------------------------------*/
19 /* Put a character */
20 /*----------------------------------------------*/
21
22 int putchar (int c)
23 {
24 if (_CR_CRLF && c == '\n') putchar('\r'); /* CR -> CRLF */
25
26 uart_putc(c);
27 }
28
29 /*----------------------------------------------*/
30 /* Put a null-terminated string */
31 /*----------------------------------------------*/
32
33 int puts ( /* Put a string to the default device */
34 const char* str /* Pointer to the string */
35 )
36 {
37 while (*str)
38 putchar(*str++);
39 }
40
41 /*----------------------------------------------*/
42 /* Formatted string output */
43 /*----------------------------------------------*/
44 /* xprintf("%d", 1234); "1234"
45 xprintf("%6d,%3d%%", -200, 5); " -200, 5%"
46 xprintf("%-6u", 100); "100 "
47 xprintf("%ld", 12345678L); "12345678"
48 xprintf("%04x", 0xA3); "00a3"
49 xprintf("%08LX", 0x123ABC); "00123ABC"
50 xprintf("%016b", 0x550F); "0101010100001111"
51 xprintf("%s", "String"); "String"
52 xprintf("%-4s", "abc"); "abc "
53 xprintf("%4s", "abc"); " abc"
54 xprintf("%c", 'a'); "a"
55 xprintf("%f", 10.0); <xprintf lacks floating point support>
56 */
57
58 int vprintf (
59 const char* fmt, /* Pointer to the format string */
60 va_list arp /* Pointer to arguments */
61 )
62 {
63 unsigned int r, i, j, w, f;
64 unsigned long v;
65 char s[16], c, d, *p;
66
67
68 for (;;) {
69 c = *fmt++; /* Get a char */
70 if (!c) break; /* End of format? */
71 if (c != '%') { /* Pass through it if not a % sequense */
72 putchar(c); continue;
73 }
74 f = 0;
75 c = *fmt++; /* Get first char of the sequense */
76 if (c == '0') { /* Flag: '0' padded */
77 f = 1; c = *fmt++;
78 } else {
79 if (c == '-') { /* Flag: left justified */
80 f = 2; c = *fmt++;
81 }
82 }
83 for (w = 0; c >= '0' && c <= '9'; c = *fmt++) /* Minimum width */
84 w = w * 10 + c - '0';
85 if (c == 'l' || c == 'L') { /* Prefix: Size is long int */
86 f |= 4; c = *fmt++;
87 }
88 if (!c) break; /* End of format? */
89 d = c;
90 if (d >= 'a') d -= 0x20;
91 switch (d) { /* Type is... */
92 case 'S' : /* String */
93 p = va_arg(arp, char*);
94 for (j = 0; p[j]; j++) ;
95 while (!(f & 2) && j++ < w) putchar(' ');
96 puts(p);
97 while (j++ < w) putchar(' ');
98 continue;
99 case 'C' : /* Character */
100 putchar((char)va_arg(arp, int)); continue;
101 case 'B' : /* Binary */
102 r = 2; break;
103 case 'O' : /* Octal */
104 r = 8; break;
105 case 'D' : /* Signed decimal */
106 case 'U' : /* Unsigned decimal */
107 r = 10; break;
108 case 'X' : /* Hexdecimal */
109 r = 16; break;
110 default: /* Unknown type (passthrough) */
111 putchar(c); continue;
112 }
113
114 /* Get an argument and put it in numeral */
115 v = (f & 4) ? va_arg(arp, long) : ((d == 'D') ? (long)va_arg(arp, int) : (long)va_arg(arp, unsigned int));
116 if (d == 'D' && (v & 0x80000000)) {
117 v = 0 - v;
118 f |= 8;
119 }
120 i = 0;
121 do {
122 d = (char)(v % r); v /= r;
123 if (d > 9) d += (c == 'x') ? 0x27 : 0x07;
124 s[i++] = d + '0';
125 } while (v && i < sizeof(s));
126 if (f & 8) s[i++] = '-';
127 j = i; d = (f & 1) ? '0' : ' ';
128 while (!(f & 2) && j++ < w) putchar(d);
129 do putchar(s[--i]); while(i);
130 while (j++ < w) putchar(' ');
131 }
132 }
133
134
135 int printf ( /* Put a formatted string to the default device */
136 const char* fmt, /* Pointer to the format string */
137 ... /* Optional arguments */
138 )
139 {
140 va_list arp;
141
142 va_start(arp, fmt);
143 vprintf(fmt, arp);
144 va_end(arp);
145 }
146
147
148 /*----------------------------------------------*/
149 /* Dump a line of binary dump */
150 /*----------------------------------------------*/
151
152 void put_dump (
153 const void* buff, /* Pointer to the array to be dumped */
154 unsigned long addr, /* Heading address value */
155 int len, /* Number of items to be dumped */
156 int width /* Size of the items (DF_CHAR, DF_SHORT, DF_LONG) */
157 )
158 {
159 int i;
160 const unsigned char *bp;
161 const unsigned short *sp;
162 const unsigned long *lp;
163
164
165 printf("%08lX ", addr); /* address */
166
167 switch (width) {
168 case DW_CHAR:
169 bp = buff;
170 for (i = 0; i < len; i++) /* Hexdecimal dump */
171 printf(" %02X", bp[i]);
172 putchar(' ');
173 for (i = 0; i < len; i++) /* ASCII dump */
174 putchar((bp[i] >= ' ' && bp[i] <= '~') ? bp[i] : '.');
175 break;
176 case DW_SHORT:
177 sp = buff;
178 do /* Hexdecimal dump */
179 printf(" %04X", *sp++);
180 while (--len);
181 break;
182 case DW_LONG:
183 lp = buff;
184 do /* Hexdecimal dump */
185 printf(" %08X", *lp++);
186 while (--len);
187 break;
188 }
189
190 putchar('\n');
191 }
This page took 0.103914 seconds and 4 git commands to generate.