1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2002
10 #include <display_options.h>
12 #include <version_string.h>
13 #include <linux/ctype.h>
16 char *display_options_get_banner_priv(bool newlines, const char *build_tag,
21 len = snprintf(buf, size, "%s%s", newlines ? "\n\n" : "",
23 if (build_tag && len < size)
24 len += snprintf(buf + len, size - len, ", Build: %s",
30 snprintf(buf + len, size - len, "\n\n");
36 #define BUILD_TAG NULL
39 char *display_options_get_banner(bool newlines, char *buf, int size)
41 return display_options_get_banner_priv(newlines, BUILD_TAG, buf, size);
44 int display_options(void)
46 char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
48 display_options_get_banner(true, buf, sizeof(buf));
54 void print_freq(uint64_t freq, const char *s)
58 static const char names[] = {'G', 'M', 'k'};
59 unsigned long d = 1e9;
63 for (i = 0; i < ARRAY_SIZE(names); i++, d /= 1000) {
71 printf("%llu Hz%s", freq, s);
77 /* If there's a remainder, show the first few digits */
82 while (m && !(m % 10))
85 m = (m / 10) + (m % 100 >= 50);
88 printf("%lu", (unsigned long) freq);
91 printf(" %cHz%s", c, s);
94 void print_size(uint64_t size, const char *s)
96 unsigned long m = 0, n;
98 static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
99 unsigned long d = 10 * ARRAY_SIZE(names);
103 for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
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.
116 printf("%u Bytes%s", (unsigned int)size, s);
121 f = size & ((1ULL << d) - 1);
123 /* If there's a remainder, deal with it */
125 m = (10ULL * f + (1ULL << (d - 1))) >> d;
131 if (n == 1024 && i > 0) {
143 printf (" %ciB%s", c, s);
146 #define MAX_LINE_LENGTH_BYTES 64
147 #define DEFAULT_LINE_LENGTH_BYTES 16
149 int hexdump_line(ulong addr, const void *data, uint width, uint count,
150 uint linelen, char *out, int size)
152 /* linebuf as a union causes proper alignment */
154 uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1];
155 uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
156 uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
157 uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
163 if (linelen * width > MAX_LINE_LENGTH_BYTES)
164 linelen = MAX_LINE_LENGTH_BYTES / width;
166 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
169 * Check the size here so that we don't need to use snprintf(). This
170 * helps to reduce code size
172 if (size < HEXDUMP_MAX_BUF_LENGTH(linelen * width))
175 thislinelen = linelen;
176 out += sprintf(out, "%08lx:", addr);
178 /* check for overflow condition */
179 if (count < thislinelen)
182 /* Copy from memory into linebuf and print hex values */
183 for (i = 0; i < thislinelen; i++) {
185 x = lb.ui[i] = *(volatile uint32_t *)data;
186 else if (MEM_SUPPORT_64BIT_DATA && width == 8)
187 x = lb.uq[i] = *(volatile ulong *)data;
189 x = lb.us[i] = *(volatile uint16_t *)data;
191 x = lb.uc[i] = *(volatile uint8_t *)data;
192 if (CONFIG_IS_ENABLED(USE_TINY_PRINTF))
193 out += sprintf(out, " %x", (uint)x);
195 out += sprintf(out, " %0*lx", width * 2, x);
199 /* fill line with whitespace for nice ASCII print */
200 for (i = 0; i < (linelen - thislinelen) * (width * 2 + 1); i++)
203 /* Print data in ASCII characters */
204 for (i = 0; i < thislinelen * width; i++) {
205 if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
209 out += sprintf(out, " %s", lb.uc);
214 int print_buffer(ulong addr, const void *data, uint width, uint count,
217 if (linelen*width > MAX_LINE_LENGTH_BYTES)
218 linelen = MAX_LINE_LENGTH_BYTES / width;
220 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
224 char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)];
226 thislinelen = hexdump_line(addr, data, width, count, linelen,
228 assert(thislinelen >= 0);
232 /* update references */
233 data += thislinelen * width;
234 addr += thislinelen * width;
235 count -= thislinelen;
237 if (!IS_ENABLED(CONFIG_SPL_BUILD) && ctrlc())