]> Git Repo - J-u-boot.git/blame - lib/tiny-printf.c
Merge git://git.denx.de/u-boot-sunxi
[J-u-boot.git] / lib / tiny-printf.c
CommitLineData
7d9cde10
SR
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>
cdce1f76 15#include <linux/ctype.h>
7d9cde10 16
45313e83
SG
17struct 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() */
7d9cde10 21
45313e83
SG
22 /* Output a character */
23 void (*putc)(struct printf_info *info, char ch);
24};
5c411d88 25
45313e83 26static void out(struct printf_info *info, char c)
7d9cde10 27{
45313e83 28 *info->bf++ = c;
7d9cde10
SR
29}
30
45313e83
SG
31static 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
a28e1d98
AP
37static void div_out(struct printf_info *info, unsigned long *num,
38 unsigned long div)
7d9cde10
SR
39{
40 unsigned char dgt = 0;
41
a5ecdd08
SR
42 while (*num >= div) {
43 *num -= div;
7d9cde10
SR
44 dgt++;
45 }
46
45313e83
SG
47 if (info->zs || dgt > 0)
48 out_dgt(info, dgt);
7d9cde10
SR
49}
50
cdce1f76
V
51#ifdef CONFIG_SPL_NET_SUPPORT
52static void string(struct printf_info *info, char *s)
53{
54 char ch;
55
56 while ((ch = *s++))
57 out(info, ch);
58}
59
60static 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
64static 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
71static 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
89static 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
125static 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
161static 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
433cbfb3 199static int _vprintf(struct printf_info *info, const char *fmt, va_list va)
7d9cde10 200{
7d9cde10
SR
201 char ch;
202 char *p;
a28e1d98 203 unsigned long num;
a5ecdd08 204 char buf[12];
a28e1d98 205 unsigned long div;
7d9cde10 206
7d9cde10
SR
207 while ((ch = *(fmt++))) {
208 if (ch != '%') {
45313e83 209 info->putc(info, ch);
7d9cde10 210 } else {
1fb67608
SG
211 bool lz = false;
212 int width = 0;
a28e1d98 213 bool islong = false;
7d9cde10
SR
214
215 ch = *(fmt++);
1c853629
AP
216 if (ch == '-')
217 ch = *(fmt++);
218
7d9cde10
SR
219 if (ch == '0') {
220 ch = *(fmt++);
221 lz = 1;
222 }
223
224 if (ch >= '0' && ch <= '9') {
1fb67608 225 width = 0;
7d9cde10 226 while (ch >= '0' && ch <= '9') {
1fb67608 227 width = (width * 10) + ch - '0';
7d9cde10
SR
228 ch = *fmt++;
229 }
230 }
a28e1d98
AP
231 if (ch == 'l') {
232 ch = *(fmt++);
233 islong = true;
234 }
235
45313e83
SG
236 info->bf = buf;
237 p = info->bf;
238 info->zs = 0;
7d9cde10
SR
239
240 switch (ch) {
1fb67608 241 case '\0':
7d9cde10
SR
242 goto abort;
243 case 'u':
244 case 'd':
a28e1d98
AP
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 }
7d9cde10 262 }
74b1320a 263 if (!num) {
45313e83 264 out_dgt(info, 0);
74b1320a 265 } else {
a28e1d98 266 for (; div; div /= 10)
45313e83 267 div_out(info, &num, div);
74b1320a 268 }
7d9cde10
SR
269 break;
270 case 'x':
a28e1d98
AP
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 }
74b1320a 278 if (!num) {
45313e83 279 out_dgt(info, 0);
74b1320a 280 } else {
a28e1d98 281 for (; div; div /= 0x10)
45313e83 282 div_out(info, &num, div);
74b1320a 283 }
7d9cde10
SR
284 break;
285 case 'c':
45313e83 286 out(info, (char)(va_arg(va, int)));
7d9cde10
SR
287 break;
288 case 's':
289 p = va_arg(va, char*);
290 break;
cdce1f76
V
291 case 'p':
292 pointer(info, fmt, va_arg(va, void *));
293 while (isalnum(fmt[0]))
294 fmt++;
295 break;
7d9cde10 296 case '%':
45313e83 297 out(info, '%');
7d9cde10
SR
298 default:
299 break;
300 }
301
45313e83
SG
302 *info->bf = 0;
303 info->bf = p;
304 while (*info->bf++ && width > 0)
1fb67608
SG
305 width--;
306 while (width-- > 0)
45313e83 307 info->putc(info, lz ? '0' : ' ');
8e31681c
SG
308 if (p) {
309 while ((ch = *p++))
45313e83 310 info->putc(info, ch);
8e31681c 311 }
7d9cde10
SR
312 }
313 }
314
315abort:
7d9cde10
SR
316 return 0;
317}
962a43cc 318
4f1eed75
AK
319#if CONFIG_IS_ENABLED(PRINTF)
320static void putc_normal(struct printf_info *info, char ch)
321{
322 putc(ch);
323}
324
da70b4d1
HG
325int vprintf(const char *fmt, va_list va)
326{
45313e83
SG
327 struct printf_info info;
328
329 info.putc = putc_normal;
330 return _vprintf(&info, fmt, va);
da70b4d1
HG
331}
332
962a43cc
SS
333int printf(const char *fmt, ...)
334{
45313e83
SG
335 struct printf_info info;
336
962a43cc
SS
337 va_list va;
338 int ret;
339
45313e83 340 info.putc = putc_normal;
962a43cc 341 va_start(va, fmt);
45313e83 342 ret = _vprintf(&info, fmt, va);
5c411d88
SG
343 va_end(va);
344
345 return ret;
346}
4f1eed75 347#endif
5c411d88 348
45313e83 349static void putc_outstr(struct printf_info *info, char ch)
5c411d88 350{
45313e83 351 *info->outstr++ = ch;
5c411d88
SG
352}
353
abeb272d 354int sprintf(char *buf, const char *fmt, ...)
5c411d88 355{
45313e83 356 struct printf_info info;
5c411d88
SG
357 va_list va;
358 int ret;
359
360 va_start(va, fmt);
45313e83
SG
361 info.outstr = buf;
362 info.putc = putc_outstr;
363 ret = _vprintf(&info, fmt, va);
962a43cc 364 va_end(va);
45313e83 365 *info.outstr = '\0';
962a43cc
SS
366
367 return ret;
368}
abeb272d
MV
369
370/* Note that size is ignored */
371int snprintf(char *buf, size_t size, const char *fmt, ...)
372{
45313e83 373 struct printf_info info;
abeb272d
MV
374 va_list va;
375 int ret;
376
377 va_start(va, fmt);
45313e83
SG
378 info.outstr = buf;
379 info.putc = putc_outstr;
380 ret = _vprintf(&info, fmt, va);
abeb272d 381 va_end(va);
45313e83 382 *info.outstr = '\0';
abeb272d
MV
383
384 return ret;
385}
This page took 0.207401 seconds and 4 git commands to generate.