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>
18 char *display_options_get_banner_priv(bool newlines, const char *build_tag,
23 len = snprintf(buf, size, "%s%s", newlines ? "\n\n" : "",
25 if (build_tag && len < size)
26 len += snprintf(buf + len, size - len, ", Build: %s",
32 snprintf(buf + len, size - len, "\n\n");
38 #define BUILD_TAG NULL
41 char *display_options_get_banner(bool newlines, char *buf, int size)
43 return display_options_get_banner_priv(newlines, BUILD_TAG, buf, size);
46 int display_options(void)
48 char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
50 display_options_get_banner(true, buf, sizeof(buf));
56 void print_freq(uint64_t freq, const char *s)
60 static const char names[] = {'G', 'M', 'k'};
61 unsigned long d = 1e9;
65 for (i = 0; i < ARRAY_SIZE(names); i++, d /= 1000) {
73 printf("%llu Hz%s", freq, s);
79 /* If there's a remainder, show the first few digits */
84 while (m && !(m % 10))
87 m = (m / 10) + (m % 100 >= 50);
90 printf("%lu", (unsigned long) freq);
93 printf(" %cHz%s", c, s);
96 void print_size(uint64_t size, const char *s)
98 unsigned long m = 0, n;
100 static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
101 unsigned long d = 10 * ARRAY_SIZE(names);
105 for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
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.
118 printf("%u Bytes%s", (unsigned int)size, s);
123 f = size & ((1ULL << d) - 1);
125 /* If there's a remainder, deal with it */
127 m = (10ULL * f + (1ULL << (d - 1))) >> d;
133 if (n == 1024 && i > 0) {
145 printf (" %ciB%s", c, s);
148 #define MAX_LINE_LENGTH_BYTES 64
149 #define DEFAULT_LINE_LENGTH_BYTES 16
151 int hexdump_line(ulong addr, const void *data, uint width, uint count,
152 uint linelen, char *out, int size)
154 /* linebuf as a union causes proper alignment */
156 uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1];
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];
165 if (linelen * width > MAX_LINE_LENGTH_BYTES)
166 linelen = MAX_LINE_LENGTH_BYTES / width;
168 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
171 * Check the size here so that we don't need to use snprintf(). This
172 * helps to reduce code size
174 if (size < HEXDUMP_MAX_BUF_LENGTH(linelen * width))
177 thislinelen = linelen;
178 out += sprintf(out, "%08lx:", addr);
180 /* check for overflow condition */
181 if (count < thislinelen)
184 /* Copy from memory into linebuf and print hex values */
185 for (i = 0; i < thislinelen; i++) {
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;
191 x = lb.us[i] = *(volatile uint16_t *)data;
193 x = lb.uc[i] = *(volatile uint8_t *)data;
194 if (CONFIG_IS_ENABLED(USE_TINY_PRINTF))
195 out += sprintf(out, " %x", (uint)x);
197 out += sprintf(out, " %0*lx", width * 2, x);
201 /* fill line with whitespace for nice ASCII print */
202 for (i = 0; i < (linelen - thislinelen) * (width * 2 + 1); i++)
205 /* Print data in ASCII characters */
206 for (i = 0; i < thislinelen * width; i++) {
207 if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
211 out += sprintf(out, " %s", lb.uc);
216 int print_buffer(ulong addr, const void *data, uint width, uint count,
219 if (linelen*width > MAX_LINE_LENGTH_BYTES)
220 linelen = MAX_LINE_LENGTH_BYTES / width;
222 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
226 char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)];
228 thislinelen = hexdump_line(addr, data, width, count, linelen,
230 assert(thislinelen >= 0);
234 /* update references */
235 data += thislinelen * width;
236 addr += thislinelen * width;
237 count -= thislinelen;
239 if (!IS_ENABLED(CONFIG_XPL_BUILD) && ctrlc())