]> git.sur5r.net Git - u-boot/blob - lib/tiny-printf.c
Merge git://git.denx.de/u-boot-imx
[u-boot] / lib / tiny-printf.c
1 /*
2  * Tiny printf version for SPL
3  *
4  * Copied from:
5  * http://www.sparetimelabs.com/printfrevisited/printfrevisited.php
6  *
7  * Copyright (C) 2004,2008  Kustaa Nyholm
8  *
9  * SPDX-License-Identifier:     LGPL-2.1+
10  */
11
12 #include <common.h>
13 #include <stdarg.h>
14 #include <serial.h>
15 #include <linux/ctype.h>
16
17 struct printf_info {
18         char *bf;       /* Digit buffer */
19         char zs;        /* non-zero if a digit has been written */
20         char *outstr;   /* Next output position for sprintf() */
21
22         /* Output a character */
23         void (*putc)(struct printf_info *info, char ch);
24 };
25
26 static void out(struct printf_info *info, char c)
27 {
28         *info->bf++ = c;
29 }
30
31 static void out_dgt(struct printf_info *info, char dgt)
32 {
33         out(info, dgt + (dgt < 10 ? '0' : 'a' - 10));
34         info->zs = 1;
35 }
36
37 static void div_out(struct printf_info *info, unsigned long *num,
38                     unsigned long div)
39 {
40         unsigned char dgt = 0;
41
42         while (*num >= div) {
43                 *num -= div;
44                 dgt++;
45         }
46
47         if (info->zs || dgt > 0)
48                 out_dgt(info, dgt);
49 }
50
51 #ifdef CONFIG_SPL_NET_SUPPORT
52 static void string(struct printf_info *info, char *s)
53 {
54         char ch;
55
56         while ((ch = *s++))
57                 out(info, ch);
58 }
59
60 static const char hex_asc[] = "0123456789abcdef";
61 #define hex_asc_lo(x)   hex_asc[((x) & 0x0f)]
62 #define hex_asc_hi(x)   hex_asc[((x) & 0xf0) >> 4]
63
64 static inline char *pack_hex_byte(char *buf, u8 byte)
65 {
66         *buf++ = hex_asc_hi(byte);
67         *buf++ = hex_asc_lo(byte);
68         return buf;
69 }
70
71 static void mac_address_string(struct printf_info *info, u8 *addr,
72                                 bool separator)
73 {
74         /* (6 * 2 hex digits), 5 colons and trailing zero */
75         char mac_addr[6 * 3];
76         char *p = mac_addr;
77         int i;
78
79         for (i = 0; i < 6; i++) {
80                 p = pack_hex_byte(p, addr[i]);
81                 if (separator && i != 5)
82                         *p++ = ':';
83         }
84         *p = '\0';
85
86         string(info, mac_addr);
87 }
88
89 static char *put_dec_trunc(char *buf, unsigned int q)
90 {
91         unsigned int d3, d2, d1, d0;
92         d1 = (q >> 4) & 0xf;
93         d2 = (q >> 8) & 0xf;
94         d3 = (q >> 12);
95
96         d0 = 6 * (d3 + d2 + d1) + (q & 0xf);
97         q = (d0 * 0xcd) >> 11;
98         d0 = d0 - 10 * q;
99         *buf++ = d0 + '0'; /* least significant digit */
100         d1 = q + 9 * d3 + 5 * d2 + d1;
101         if (d1 != 0) {
102                 q = (d1 * 0xcd) >> 11;
103                 d1 = d1 - 10 * q;
104                 *buf++ = d1 + '0'; /* next digit */
105
106                 d2 = q + 2 * d2;
107                 if ((d2 != 0) || (d3 != 0)) {
108                         q = (d2 * 0xd) >> 7;
109                         d2 = d2 - 10 * q;
110                         *buf++ = d2 + '0'; /* next digit */
111
112                         d3 = q + 4 * d3;
113                         if (d3 != 0) {
114                                 q = (d3 * 0xcd) >> 11;
115                                 d3 = d3 - 10 * q;
116                                 *buf++ = d3 + '0';  /* next digit */
117                                 if (q != 0)
118                                         *buf++ = q + '0'; /* most sign. digit */
119                         }
120                 }
121         }
122         return buf;
123 }
124
125 static void ip4_addr_string(struct printf_info *info, u8 *addr)
126 {
127         /* (4 * 3 decimal digits), 3 dots and trailing zero */
128         char ip4_addr[4 * 4];
129         char temp[3];   /* hold each IP quad in reverse order */
130         char *p = ip4_addr;
131         int i, digits;
132
133         for (i = 0; i < 4; i++) {
134                 digits = put_dec_trunc(temp, addr[i]) - temp;
135                 /* reverse the digits in the quad */
136                 while (digits--)
137                         *p++ = temp[digits];
138                 if (i != 3)
139                         *p++ = '.';
140         }
141         *p = '\0';
142
143         string(info, ip4_addr);
144 }
145 #endif
146
147 /*
148  * Show a '%p' thing.  A kernel extension is that the '%p' is followed
149  * by an extra set of characters that are extended format
150  * specifiers.
151  *
152  * Right now we handle:
153  *
154  * - 'M' For a 6-byte MAC address, it prints the address in the
155  *       usual colon-separated hex notation.
156  * - 'm' Same as above except there is no colon-separator.
157  * - 'I4'for IPv4 addresses printed in the usual way (dot-separated
158  *       decimal).
159  */
160
161 static void pointer(struct printf_info *info, const char *fmt, void *ptr)
162 {
163 #ifdef DEBUG
164         unsigned long num = (uintptr_t)ptr;
165         unsigned long div;
166 #endif
167
168         switch (*fmt) {
169 #ifdef DEBUG
170         case 'a':
171
172                 switch (fmt[1]) {
173                 case 'p':
174                 default:
175                         num = *(phys_addr_t *)ptr;
176                         break;
177                 }
178                 break;
179 #endif
180 #ifdef CONFIG_SPL_NET_SUPPORT
181         case 'm':
182                 return mac_address_string(info, ptr, false);
183         case 'M':
184                 return mac_address_string(info, ptr, true);
185         case 'I':
186                 if (fmt[1] == '4')
187                         return ip4_addr_string(info, ptr);
188 #endif
189         default:
190                 break;
191         }
192 #ifdef DEBUG
193         div = 1UL << (sizeof(long) * 8 - 4);
194         for (; div; div /= 0x10)
195                 div_out(info, &num, div);
196 #endif
197 }
198
199 static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
200 {
201         char ch;
202         char *p;
203         unsigned long num;
204         char buf[12];
205         unsigned long div;
206
207         while ((ch = *(fmt++))) {
208                 if (ch != '%') {
209                         info->putc(info, ch);
210                 } else {
211                         bool lz = false;
212                         int width = 0;
213                         bool islong = false;
214
215                         ch = *(fmt++);
216                         if (ch == '-')
217                                 ch = *(fmt++);
218
219                         if (ch == '0') {
220                                 ch = *(fmt++);
221                                 lz = 1;
222                         }
223
224                         if (ch >= '0' && ch <= '9') {
225                                 width = 0;
226                                 while (ch >= '0' && ch <= '9') {
227                                         width = (width * 10) + ch - '0';
228                                         ch = *fmt++;
229                                 }
230                         }
231                         if (ch == 'l') {
232                                 ch = *(fmt++);
233                                 islong = true;
234                         }
235
236                         info->bf = buf;
237                         p = info->bf;
238                         info->zs = 0;
239
240                         switch (ch) {
241                         case '\0':
242                                 goto abort;
243                         case 'u':
244                         case 'd':
245                                 div = 1000000000;
246                                 if (islong) {
247                                         num = va_arg(va, unsigned long);
248                                         if (sizeof(long) > 4)
249                                                 div *= div * 10;
250                                 } else {
251                                         num = va_arg(va, unsigned int);
252                                 }
253
254                                 if (ch == 'd') {
255                                         if (islong && (long)num < 0) {
256                                                 num = -(long)num;
257                                                 out(info, '-');
258                                         } else if (!islong && (int)num < 0) {
259                                                 num = -(int)num;
260                                                 out(info, '-');
261                                         }
262                                 }
263                                 if (!num) {
264                                         out_dgt(info, 0);
265                                 } else {
266                                         for (; div; div /= 10)
267                                                 div_out(info, &num, div);
268                                 }
269                                 break;
270                         case 'x':
271                                 if (islong) {
272                                         num = va_arg(va, unsigned long);
273                                         div = 1UL << (sizeof(long) * 8 - 4);
274                                 } else {
275                                         num = va_arg(va, unsigned int);
276                                         div = 0x10000000;
277                                 }
278                                 if (!num) {
279                                         out_dgt(info, 0);
280                                 } else {
281                                         for (; div; div /= 0x10)
282                                                 div_out(info, &num, div);
283                                 }
284                                 break;
285                         case 'c':
286                                 out(info, (char)(va_arg(va, int)));
287                                 break;
288                         case 's':
289                                 p = va_arg(va, char*);
290                                 break;
291                         case 'p':
292                                 pointer(info, fmt, va_arg(va, void *));
293                                 while (isalnum(fmt[0]))
294                                         fmt++;
295                                 break;
296                         case '%':
297                                 out(info, '%');
298                         default:
299                                 break;
300                         }
301
302                         *info->bf = 0;
303                         info->bf = p;
304                         while (*info->bf++ && width > 0)
305                                 width--;
306                         while (width-- > 0)
307                                 info->putc(info, lz ? '0' : ' ');
308                         if (p) {
309                                 while ((ch = *p++))
310                                         info->putc(info, ch);
311                         }
312                 }
313         }
314
315 abort:
316         return 0;
317 }
318
319 #if CONFIG_IS_ENABLED(PRINTF)
320 static void putc_normal(struct printf_info *info, char ch)
321 {
322         putc(ch);
323 }
324
325 int vprintf(const char *fmt, va_list va)
326 {
327         struct printf_info info;
328
329         info.putc = putc_normal;
330         return _vprintf(&info, fmt, va);
331 }
332
333 int printf(const char *fmt, ...)
334 {
335         struct printf_info info;
336
337         va_list va;
338         int ret;
339
340         info.putc = putc_normal;
341         va_start(va, fmt);
342         ret = _vprintf(&info, fmt, va);
343         va_end(va);
344
345         return ret;
346 }
347 #endif
348
349 static void putc_outstr(struct printf_info *info, char ch)
350 {
351         *info->outstr++ = ch;
352 }
353
354 int sprintf(char *buf, const char *fmt, ...)
355 {
356         struct printf_info info;
357         va_list va;
358         int ret;
359
360         va_start(va, fmt);
361         info.outstr = buf;
362         info.putc = putc_outstr;
363         ret = _vprintf(&info, fmt, va);
364         va_end(va);
365         *info.outstr = '\0';
366
367         return ret;
368 }
369
370 /* Note that size is ignored */
371 int snprintf(char *buf, size_t size, const char *fmt, ...)
372 {
373         struct printf_info info;
374         va_list va;
375         int ret;
376
377         va_start(va, fmt);
378         info.outstr = buf;
379         info.putc = putc_outstr;
380         ret = _vprintf(&info, fmt, va);
381         va_end(va);
382         *info.outstr = '\0';
383
384         return ret;
385 }