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