]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
20e0e233 WD |
2 | /* |
3 | * (C) Copyright 2000-2002 | |
4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. | |
20e0e233 WD |
5 | */ |
6 | ||
7 | #include <common.h> | |
90606da0 | 8 | #include <compiler.h> |
24b852a7 | 9 | #include <console.h> |
4e4bf944 | 10 | #include <display_options.h> |
33eac2dc | 11 | #include <div64.h> |
bdfb6d70 | 12 | #include <version_string.h> |
c95c4280 GL |
13 | #include <linux/ctype.h> |
14 | #include <asm/io.h> | |
20e0e233 | 15 | |
6c519f2d SG |
16 | char *display_options_get_banner_priv(bool newlines, const char *build_tag, |
17 | char *buf, int size) | |
20e0e233 | 18 | { |
6c519f2d SG |
19 | int len; |
20 | ||
21 | len = snprintf(buf, size, "%s%s", newlines ? "\n\n" : "", | |
22 | version_string); | |
23 | if (build_tag && len < size) | |
24 | len += snprintf(buf + len, size - len, ", Build: %s", | |
25 | build_tag); | |
26 | if (len > size - 3) | |
27 | len = size - 3; | |
6c74e94a HS |
28 | if (len < 0) |
29 | len = 0; | |
30 | snprintf(buf + len, size - len, "\n\n"); | |
6c519f2d SG |
31 | |
32 | return buf; | |
33 | } | |
34 | ||
35 | #ifndef BUILD_TAG | |
36 | #define BUILD_TAG NULL | |
20e0e233 | 37 | #endif |
6c519f2d SG |
38 | |
39 | char *display_options_get_banner(bool newlines, char *buf, int size) | |
40 | { | |
41 | return display_options_get_banner_priv(newlines, BUILD_TAG, buf, size); | |
42 | } | |
43 | ||
44 | int display_options(void) | |
45 | { | |
46 | char buf[DISPLAY_OPTIONS_BANNER_LENGTH]; | |
47 | ||
48 | display_options_get_banner(true, buf, sizeof(buf)); | |
49 | printf("%s", buf); | |
50 | ||
20e0e233 WD |
51 | return 0; |
52 | } | |
53 | ||
33eac2dc SG |
54 | void print_freq(uint64_t freq, const char *s) |
55 | { | |
80402f34 | 56 | unsigned long m = 0; |
33eac2dc | 57 | uint32_t f; |
dcf16721 | 58 | static const char names[] = {'G', 'M', 'k'}; |
33eac2dc SG |
59 | unsigned long d = 1e9; |
60 | char c = 0; | |
61 | unsigned int i; | |
62 | ||
63 | for (i = 0; i < ARRAY_SIZE(names); i++, d /= 1000) { | |
64 | if (freq >= d) { | |
65 | c = names[i]; | |
66 | break; | |
67 | } | |
68 | } | |
69 | ||
70 | if (!c) { | |
dee37fc9 | 71 | printf("%llu Hz%s", freq, s); |
33eac2dc SG |
72 | return; |
73 | } | |
74 | ||
75 | f = do_div(freq, d); | |
33eac2dc SG |
76 | |
77 | /* If there's a remainder, show the first few digits */ | |
78 | if (f) { | |
79 | m = f; | |
80 | while (m > 1000) | |
81 | m /= 10; | |
82 | while (m && !(m % 10)) | |
83 | m /= 10; | |
84 | if (m >= 100) | |
85 | m = (m / 10) + (m % 100 >= 50); | |
86 | } | |
87 | ||
e9015b30 | 88 | printf("%lu", (unsigned long) freq); |
33eac2dc SG |
89 | if (m) |
90 | printf(".%ld", m); | |
91 | printf(" %cHz%s", c, s); | |
92 | } | |
93 | ||
c6da9ae8 | 94 | void print_size(uint64_t size, const char *s) |
20e0e233 | 95 | { |
52dbac69 | 96 | unsigned long m = 0, n; |
c6da9ae8 | 97 | uint64_t f; |
4b42c905 | 98 | static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'}; |
f2d76ae4 | 99 | unsigned long d = 10 * ARRAY_SIZE(names); |
4b42c905 TT |
100 | char c = 0; |
101 | unsigned int i; | |
20e0e233 | 102 | |
f2d76ae4 NT |
103 | for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) { |
104 | if (size >> d) { | |
4b42c905 TT |
105 | c = names[i]; |
106 | break; | |
417faf28 | 107 | } |
20e0e233 WD |
108 | } |
109 | ||
4b42c905 | 110 | if (!c) { |
f52352f6 MK |
111 | /* |
112 | * SPL tiny-printf is not capable for printing uint64_t. | |
113 | * We have just checked that the size is small enought to fit | |
114 | * unsigned int safely. | |
115 | */ | |
116 | printf("%u Bytes%s", (unsigned int)size, s); | |
4b42c905 TT |
117 | return; |
118 | } | |
119 | ||
f2d76ae4 NT |
120 | n = size >> d; |
121 | f = size & ((1ULL << d) - 1); | |
20e0e233 | 122 | |
417faf28 | 123 | /* If there's a remainder, deal with it */ |
f2d76ae4 NT |
124 | if (f) { |
125 | m = (10ULL * f + (1ULL << (d - 1))) >> d; | |
20e0e233 | 126 | |
417faf28 BB |
127 | if (m >= 10) { |
128 | m -= 10; | |
129 | n += 1; | |
130 | } | |
0d498393 WD |
131 | } |
132 | ||
4b42c905 | 133 | printf ("%lu", n); |
20e0e233 WD |
134 | if (m) { |
135 | printf (".%ld", m); | |
136 | } | |
4b42c905 | 137 | printf (" %ciB%s", c, s); |
20e0e233 | 138 | } |
c95c4280 | 139 | |
0cceb99a SG |
140 | #define MAX_LINE_LENGTH_BYTES 64 |
141 | #define DEFAULT_LINE_LENGTH_BYTES 16 | |
142 | ||
143 | int hexdump_line(ulong addr, const void *data, uint width, uint count, | |
144 | uint linelen, char *out, int size) | |
c95c4280 | 145 | { |
150f7236 RM |
146 | /* linebuf as a union causes proper alignment */ |
147 | union linebuf { | |
4d1fd7f1 | 148 | uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1]; |
150f7236 RM |
149 | uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1]; |
150 | uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1]; | |
151 | uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1]; | |
152 | } lb; | |
0cceb99a | 153 | uint thislinelen; |
c95c4280 | 154 | int i; |
677dbf5d | 155 | ulong x; |
c95c4280 | 156 | |
0cceb99a SG |
157 | if (linelen * width > MAX_LINE_LENGTH_BYTES) |
158 | linelen = MAX_LINE_LENGTH_BYTES / width; | |
159 | if (linelen < 1) | |
160 | linelen = DEFAULT_LINE_LENGTH_BYTES / width; | |
161 | ||
162 | /* | |
163 | * Check the size here so that we don't need to use snprintf(). This | |
164 | * helps to reduce code size | |
165 | */ | |
166 | if (size < HEXDUMP_MAX_BUF_LENGTH(linelen * width)) | |
167 | return -ENOSPC; | |
168 | ||
169 | thislinelen = linelen; | |
170 | out += sprintf(out, "%08lx:", addr); | |
171 | ||
172 | /* check for overflow condition */ | |
173 | if (count < thislinelen) | |
174 | thislinelen = count; | |
175 | ||
176 | /* Copy from memory into linebuf and print hex values */ | |
177 | for (i = 0; i < thislinelen; i++) { | |
178 | if (width == 4) | |
179 | x = lb.ui[i] = *(volatile uint32_t *)data; | |
180 | else if (MEM_SUPPORT_64BIT_DATA && width == 8) | |
181 | x = lb.uq[i] = *(volatile ulong *)data; | |
182 | else if (width == 2) | |
183 | x = lb.us[i] = *(volatile uint16_t *)data; | |
184 | else | |
185 | x = lb.uc[i] = *(volatile uint8_t *)data; | |
186 | if (CONFIG_IS_ENABLED(USE_TINY_PRINTF)) | |
187 | out += sprintf(out, " %x", (uint)x); | |
188 | else | |
189 | out += sprintf(out, " %0*lx", width * 2, x); | |
190 | data += width; | |
191 | } | |
192 | ||
193 | /* fill line with whitespace for nice ASCII print */ | |
194 | for (i = 0; i < (linelen - thislinelen) * (width * 2 + 1); i++) | |
195 | *out++ = ' '; | |
196 | ||
197 | /* Print data in ASCII characters */ | |
198 | for (i = 0; i < thislinelen * width; i++) { | |
199 | if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80) | |
200 | lb.uc[i] = '.'; | |
201 | } | |
202 | lb.uc[i] = '\0'; | |
203 | out += sprintf(out, " %s", lb.uc); | |
204 | ||
205 | return thislinelen; | |
206 | } | |
207 | ||
208 | int print_buffer(ulong addr, const void *data, uint width, uint count, | |
209 | uint linelen) | |
210 | { | |
c95c4280 GL |
211 | if (linelen*width > MAX_LINE_LENGTH_BYTES) |
212 | linelen = MAX_LINE_LENGTH_BYTES / width; | |
213 | if (linelen < 1) | |
214 | linelen = DEFAULT_LINE_LENGTH_BYTES / width; | |
215 | ||
216 | while (count) { | |
0cceb99a SG |
217 | uint thislinelen; |
218 | char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)]; | |
c95c4280 | 219 | |
0cceb99a SG |
220 | thislinelen = hexdump_line(addr, data, width, count, linelen, |
221 | buf, sizeof(buf)); | |
222 | assert(thislinelen >= 0); | |
223 | puts(buf); | |
224 | putc('\n'); | |
c95c4280 GL |
225 | |
226 | /* update references */ | |
0cceb99a | 227 | data += thislinelen * width; |
efd7c114 AB |
228 | addr += thislinelen * width; |
229 | count -= thislinelen; | |
c95c4280 | 230 | |
0cceb99a SG |
231 | if (!IS_ENABLED(CONFIG_SPL_BUILD) && ctrlc()) |
232 | return -EINTR; | |
c95c4280 GL |
233 | } |
234 | ||
235 | return 0; | |
236 | } |