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