1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2002
9 #include <display_options.h>
11 #include <version_string.h>
12 #include <linux/ctype.h>
13 #include <linux/kernel.h>
17 char *display_options_get_banner_priv(bool newlines, const char *build_tag,
22 len = snprintf(buf, size, "%s%s", newlines ? "\n\n" : "",
24 if (build_tag && len < size)
25 len += snprintf(buf + len, size - len, ", Build: %s",
31 snprintf(buf + len, size - len, "\n\n");
37 #define BUILD_TAG NULL
40 char *display_options_get_banner(bool newlines, char *buf, int size)
42 return display_options_get_banner_priv(newlines, BUILD_TAG, buf, size);
45 int display_options(void)
47 char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
49 display_options_get_banner(true, buf, sizeof(buf));
55 void print_freq(uint64_t freq, const char *s)
59 static const char names[] = {'G', 'M', 'k'};
60 unsigned long d = 1e9;
64 for (i = 0; i < ARRAY_SIZE(names); i++, d /= 1000) {
72 printf("%llu Hz%s", freq, s);
78 /* If there's a remainder, show the first few digits */
83 while (m && !(m % 10))
86 m = (m / 10) + (m % 100 >= 50);
89 printf("%lu", (unsigned long) freq);
92 printf(" %cHz%s", c, s);
95 void print_size(uint64_t size, const char *s)
97 unsigned long m = 0, n;
99 static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
100 unsigned long d = 10 * ARRAY_SIZE(names);
104 for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
113 * SPL tiny-printf is not capable for printing uint64_t.
114 * We have just checked that the size is small enought to fit
115 * unsigned int safely.
117 printf("%u Bytes%s", (unsigned int)size, s);
122 f = size & ((1ULL << d) - 1);
124 /* If there's a remainder, deal with it */
126 m = (10ULL * f + (1ULL << (d - 1))) >> d;
132 if (n == 1024 && i > 0) {
144 printf (" %ciB%s", c, s);
147 #define MAX_LINE_LENGTH_BYTES 64
148 #define DEFAULT_LINE_LENGTH_BYTES 16
150 int hexdump_line(ulong addr, const void *data, uint width, uint count,
151 uint linelen, char *out, int size)
153 /* linebuf as a union causes proper alignment */
155 uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1];
156 uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
157 uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
158 uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
164 if (linelen * width > MAX_LINE_LENGTH_BYTES)
165 linelen = MAX_LINE_LENGTH_BYTES / width;
167 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
170 * Check the size here so that we don't need to use snprintf(). This
171 * helps to reduce code size
173 if (size < HEXDUMP_MAX_BUF_LENGTH(linelen * width))
176 thislinelen = linelen;
177 out += sprintf(out, "%08lx:", addr);
179 /* check for overflow condition */
180 if (count < thislinelen)
183 /* Copy from memory into linebuf and print hex values */
184 for (i = 0; i < thislinelen; i++) {
186 x = lb.ui[i] = *(volatile uint32_t *)data;
187 else if (MEM_SUPPORT_64BIT_DATA && width == 8)
188 x = lb.uq[i] = *(volatile ulong *)data;
190 x = lb.us[i] = *(volatile uint16_t *)data;
192 x = lb.uc[i] = *(volatile uint8_t *)data;
193 if (CONFIG_IS_ENABLED(USE_TINY_PRINTF))
194 out += sprintf(out, " %x", (uint)x);
196 out += sprintf(out, " %0*lx", width * 2, x);
200 /* fill line with whitespace for nice ASCII print */
201 for (i = 0; i < (linelen - thislinelen) * (width * 2 + 1); i++)
204 /* Print data in ASCII characters */
205 for (i = 0; i < thislinelen * width; i++) {
206 if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
210 out += sprintf(out, " %s", lb.uc);
215 int print_buffer(ulong addr, const void *data, uint width, uint count,
218 if (linelen*width > MAX_LINE_LENGTH_BYTES)
219 linelen = MAX_LINE_LENGTH_BYTES / width;
221 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
225 char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)];
227 thislinelen = hexdump_line(addr, data, width, count, linelen,
229 assert(thislinelen >= 0);
233 /* update references */
234 data += thislinelen * width;
235 addr += thislinelen * width;
236 count -= thislinelen;
238 if (!IS_ENABLED(CONFIG_SPL_BUILD) && ctrlc())