blob: 1aa43aba44b5b2428033638574f49e76058f80bc [file] [log] [blame]
Stefan Roese7d9cde12015-11-23 07:00:22 +01001/*
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
Andre Przywara59d07ee2016-07-08 15:18:35 +010016/*
17 * This code in here may execute before the DRAM is initialised, so
18 * we should make sure that it doesn't touch BSS, which some boards
19 * put in DRAM.
20 */
21static char *bf __attribute__ ((section(".data")));
22static char zs __attribute__ ((section(".data")));
Stefan Roese7d9cde12015-11-23 07:00:22 +010023
Simon Glass5c411d82016-05-14 14:02:53 -060024/* Current position in sprintf() output string */
Andre Przywara59d07ee2016-07-08 15:18:35 +010025static char *outstr __attribute__ ((section(".data")));
Simon Glass5c411d82016-05-14 14:02:53 -060026
Stefan Roese7d9cde12015-11-23 07:00:22 +010027static void out(char c)
28{
29 *bf++ = c;
30}
31
32static void out_dgt(char dgt)
33{
Stefan Roesea5ecdd02015-11-16 15:26:34 +010034 out(dgt + (dgt < 10 ? '0' : 'a' - 10));
Stefan Roese7d9cde12015-11-23 07:00:22 +010035 zs = 1;
36}
37
Stefan Roesea5ecdd02015-11-16 15:26:34 +010038static void div_out(unsigned int *num, unsigned int div)
Stefan Roese7d9cde12015-11-23 07:00:22 +010039{
40 unsigned char dgt = 0;
41
Stefan Roesea5ecdd02015-11-16 15:26:34 +010042 while (*num >= div) {
43 *num -= div;
Stefan Roese7d9cde12015-11-23 07:00:22 +010044 dgt++;
45 }
46
47 if (zs || dgt > 0)
48 out_dgt(dgt);
49}
50
Simon Glass5c411d82016-05-14 14:02:53 -060051int _vprintf(const char *fmt, va_list va, void (*putc)(const char ch))
Stefan Roese7d9cde12015-11-23 07:00:22 +010052{
Stefan Roese7d9cde12015-11-23 07:00:22 +010053 char ch;
54 char *p;
Stefan Roesea5ecdd02015-11-16 15:26:34 +010055 unsigned int num;
56 char buf[12];
57 unsigned int div;
Stefan Roese7d9cde12015-11-23 07:00:22 +010058
Stefan Roese7d9cde12015-11-23 07:00:22 +010059 while ((ch = *(fmt++))) {
60 if (ch != '%') {
61 putc(ch);
62 } else {
Simon Glass1fb67602016-05-14 14:02:52 -060063 bool lz = false;
64 int width = 0;
Stefan Roese7d9cde12015-11-23 07:00:22 +010065
66 ch = *(fmt++);
67 if (ch == '0') {
68 ch = *(fmt++);
69 lz = 1;
70 }
71
72 if (ch >= '0' && ch <= '9') {
Simon Glass1fb67602016-05-14 14:02:52 -060073 width = 0;
Stefan Roese7d9cde12015-11-23 07:00:22 +010074 while (ch >= '0' && ch <= '9') {
Simon Glass1fb67602016-05-14 14:02:52 -060075 width = (width * 10) + ch - '0';
Stefan Roese7d9cde12015-11-23 07:00:22 +010076 ch = *fmt++;
77 }
78 }
79 bf = buf;
80 p = bf;
81 zs = 0;
82
83 switch (ch) {
Simon Glass1fb67602016-05-14 14:02:52 -060084 case '\0':
Stefan Roese7d9cde12015-11-23 07:00:22 +010085 goto abort;
86 case 'u':
87 case 'd':
88 num = va_arg(va, unsigned int);
89 if (ch == 'd' && (int)num < 0) {
90 num = -(int)num;
91 out('-');
92 }
Simon Glass74b13202016-01-05 09:30:57 -070093 if (!num) {
94 out_dgt(0);
95 } else {
96 for (div = 1000000000; div; div /= 10)
97 div_out(&num, div);
98 }
Stefan Roese7d9cde12015-11-23 07:00:22 +010099 break;
100 case 'x':
Stefan Roese7d9cde12015-11-23 07:00:22 +0100101 num = va_arg(va, unsigned int);
Simon Glass74b13202016-01-05 09:30:57 -0700102 if (!num) {
103 out_dgt(0);
104 } else {
105 for (div = 0x10000000; div; div /= 0x10)
106 div_out(&num, div);
107 }
Stefan Roese7d9cde12015-11-23 07:00:22 +0100108 break;
109 case 'c':
110 out((char)(va_arg(va, int)));
111 break;
112 case 's':
113 p = va_arg(va, char*);
114 break;
115 case '%':
116 out('%');
117 default:
118 break;
119 }
120
121 *bf = 0;
122 bf = p;
Simon Glass1fb67602016-05-14 14:02:52 -0600123 while (*bf++ && width > 0)
124 width--;
125 while (width-- > 0)
Stefan Roese7d9cde12015-11-23 07:00:22 +0100126 putc(lz ? '0' : ' ');
Simon Glass8e316812015-12-29 05:22:46 -0700127 if (p) {
128 while ((ch = *p++))
129 putc(ch);
130 }
Stefan Roese7d9cde12015-11-23 07:00:22 +0100131 }
132 }
133
134abort:
Stefan Roese7d9cde12015-11-23 07:00:22 +0100135 return 0;
136}
Sjoerd Simons962a43c2015-12-04 23:27:37 +0100137
Hans de Goededa70b4d2016-06-10 21:03:34 +0200138int vprintf(const char *fmt, va_list va)
139{
140 return _vprintf(fmt, va, putc);
141}
142
Sjoerd Simons962a43c2015-12-04 23:27:37 +0100143int printf(const char *fmt, ...)
144{
145 va_list va;
146 int ret;
147
148 va_start(va, fmt);
Simon Glass5c411d82016-05-14 14:02:53 -0600149 ret = _vprintf(fmt, va, putc);
Sjoerd Simons962a43c2015-12-04 23:27:37 +0100150 va_end(va);
151
152 return ret;
153}
Simon Glass5c411d82016-05-14 14:02:53 -0600154
155static void putc_outstr(char ch)
156{
157 *outstr++ = ch;
158}
159
Marek Vasutabeb2722016-05-31 23:12:46 +0200160int sprintf(char *buf, const char *fmt, ...)
Simon Glass5c411d82016-05-14 14:02:53 -0600161{
162 va_list va;
163 int ret;
164
165 va_start(va, fmt);
166 outstr = buf;
167 ret = _vprintf(fmt, va, putc_outstr);
168 va_end(va);
169 *outstr = '\0';
170
171 return ret;
172}
Marek Vasutabeb2722016-05-31 23:12:46 +0200173
174/* Note that size is ignored */
175int snprintf(char *buf, size_t size, const char *fmt, ...)
176{
177 va_list va;
178 int ret;
179
180 va_start(va, fmt);
Simon Glass3191d842016-06-08 20:55:15 -0600181 outstr = buf;
182 ret = _vprintf(fmt, va, putc_outstr);
Marek Vasutabeb2722016-05-31 23:12:46 +0200183 va_end(va);
Simon Glass3191d842016-06-08 20:55:15 -0600184 *outstr = '\0';
Marek Vasutabeb2722016-05-31 23:12:46 +0200185
186 return ret;
187}
Simon Glasse2409132016-07-04 11:58:13 -0600188
189void __assert_fail(const char *assertion, const char *file, unsigned line,
190 const char *function)
191{
192 /* This will not return */
193 printf("%s:%u: %s: Assertion `%s' failed.", file, line, function,
194 assertion);
195 hang();
196}