blob: 2c15cc5b5c4aa69568d7cede90a3deb003aa5dac [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk20e0e232002-09-08 16:09:41 +00002/*
3 * (C) Copyright 2000-2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk20e0e232002-09-08 16:09:41 +00005 */
6
Simon Glass28db7f52020-06-07 20:33:00 -06007#include <compiler.h>
Simon Glassa73bda42015-11-08 23:47:45 -07008#include <console.h>
Simon Glass1ab16922022-07-31 12:28:48 -06009#include <display_options.h>
Simon Glass1b5fe402015-04-29 07:56:43 -060010#include <div64.h>
Pali Rohárba87ddf2021-08-02 15:18:31 +020011#include <version_string.h>
Grant Likely02a00782007-02-20 09:05:00 +010012#include <linux/ctype.h>
Tom Rinic4470ea2023-12-14 13:16:56 -050013#include <linux/kernel.h>
Grant Likely02a00782007-02-20 09:05:00 +010014#include <asm/io.h>
Raymond Mao24da8312024-05-16 14:11:52 -070015#include <stdio.h>
Tom Rinic4470ea2023-12-14 13:16:56 -050016#include <vsprintf.h>
wdenk20e0e232002-09-08 16:09:41 +000017
Simon Glasse8583f92017-06-16 12:51:42 -060018char *display_options_get_banner_priv(bool newlines, const char *build_tag,
19 char *buf, int size)
wdenk20e0e232002-09-08 16:09:41 +000020{
Simon Glasse8583f92017-06-16 12:51:42 -060021 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;
Heinrich Schuchardtb144ab12019-04-26 18:39:00 +020030 if (len < 0)
31 len = 0;
32 snprintf(buf + len, size - len, "\n\n");
Simon Glasse8583f92017-06-16 12:51:42 -060033
34 return buf;
35}
36
37#ifndef BUILD_TAG
38#define BUILD_TAG NULL
wdenk20e0e232002-09-08 16:09:41 +000039#endif
Simon Glasse8583f92017-06-16 12:51:42 -060040
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
wdenk20e0e232002-09-08 16:09:41 +000053 return 0;
54}
55
Simon Glass1b5fe402015-04-29 07:56:43 -060056void print_freq(uint64_t freq, const char *s)
57{
Heiko Schocher62cb1562015-06-29 09:10:46 +020058 unsigned long m = 0;
Simon Glass1b5fe402015-04-29 07:56:43 -060059 uint32_t f;
Heinrich Schuchardtae1c9572020-10-08 22:23:23 +020060 static const char names[] = {'G', 'M', 'k'};
Simon Glass1b5fe402015-04-29 07:56:43 -060061 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) {
Masahiro Yamadac7570a32018-08-06 20:47:40 +090073 printf("%llu Hz%s", freq, s);
Simon Glass1b5fe402015-04-29 07:56:43 -060074 return;
75 }
76
77 f = do_div(freq, d);
Simon Glass1b5fe402015-04-29 07:56:43 -060078
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
Suriyan Ramasami28ef5c52015-08-18 09:25:33 -070090 printf("%lu", (unsigned long) freq);
Simon Glass1b5fe402015-04-29 07:56:43 -060091 if (m)
92 printf(".%ld", m);
93 printf(" %cHz%s", c, s);
94}
95
Simon Glass08ea3732014-10-15 04:38:35 -060096void print_size(uint64_t size, const char *s)
wdenk20e0e232002-09-08 16:09:41 +000097{
Timur Tabi508a4e72010-04-13 13:16:02 -050098 unsigned long m = 0, n;
Simon Glass08ea3732014-10-15 04:38:35 -060099 uint64_t f;
Timur Tabi53258da2010-04-13 13:16:03 -0500100 static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
Nick Thompsondd7e6c12010-05-11 11:29:52 +0100101 unsigned long d = 10 * ARRAY_SIZE(names);
Timur Tabi53258da2010-04-13 13:16:03 -0500102 char c = 0;
103 unsigned int i;
wdenk20e0e232002-09-08 16:09:41 +0000104
Nick Thompsondd7e6c12010-05-11 11:29:52 +0100105 for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
106 if (size >> d) {
Timur Tabi53258da2010-04-13 13:16:03 -0500107 c = names[i];
108 break;
Becky Brucee9954d42008-07-09 11:09:41 -0500109 }
wdenk20e0e232002-09-08 16:09:41 +0000110 }
111
Timur Tabi53258da2010-04-13 13:16:03 -0500112 if (!c) {
Matwey V. Kornilovfae88752021-08-06 00:22:58 +0300113 /*
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);
Timur Tabi53258da2010-04-13 13:16:03 -0500119 return;
120 }
121
Nick Thompsondd7e6c12010-05-11 11:29:52 +0100122 n = size >> d;
123 f = size & ((1ULL << d) - 1);
wdenk20e0e232002-09-08 16:09:41 +0000124
Becky Brucee9954d42008-07-09 11:09:41 -0500125 /* If there's a remainder, deal with it */
Nick Thompsondd7e6c12010-05-11 11:29:52 +0100126 if (f) {
127 m = (10ULL * f + (1ULL << (d - 1))) >> d;
wdenk20e0e232002-09-08 16:09:41 +0000128
Becky Brucee9954d42008-07-09 11:09:41 -0500129 if (m >= 10) {
130 m -= 10;
131 n += 1;
Pali Rohárc14fad52022-09-12 21:02:27 +0200132
133 if (n == 1024 && i > 0) {
134 n = 1;
135 m = 0;
136 c = names[i - 1];
137 }
Becky Brucee9954d42008-07-09 11:09:41 -0500138 }
wdenkf287a242003-07-01 21:06:45 +0000139 }
140
Timur Tabi53258da2010-04-13 13:16:03 -0500141 printf ("%lu", n);
wdenk20e0e232002-09-08 16:09:41 +0000142 if (m) {
143 printf (".%ld", m);
144 }
Timur Tabi53258da2010-04-13 13:16:03 -0500145 printf (" %ciB%s", c, s);
wdenk20e0e232002-09-08 16:09:41 +0000146}
Grant Likely02a00782007-02-20 09:05:00 +0100147
Simon Glass02b8f1a2021-05-08 07:00:05 -0600148#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)
Grant Likely02a00782007-02-20 09:05:00 +0100153{
Reinhard Meyerd9f13d02010-09-08 12:25:40 +0200154 /* linebuf as a union causes proper alignment */
155 union linebuf {
York Sun6c480012014-02-26 17:03:19 -0800156 uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1];
Reinhard Meyerd9f13d02010-09-08 12:25:40 +0200157 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;
Simon Glass02b8f1a2021-05-08 07:00:05 -0600161 uint thislinelen;
Grant Likely02a00782007-02-20 09:05:00 +0100162 int i;
Simon Glass2056b9e2020-06-02 19:26:47 -0600163 ulong x;
Grant Likely02a00782007-02-20 09:05:00 +0100164
Simon Glass02b8f1a2021-05-08 07:00:05 -0600165 if (linelen * width > MAX_LINE_LENGTH_BYTES)
Grant Likely02a00782007-02-20 09:05:00 +0100166 linelen = MAX_LINE_LENGTH_BYTES / width;
167 if (linelen < 1)
168 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
169
Simon Glass02b8f1a2021-05-08 07:00:05 -0600170 /*
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++ = ' ';
Grant Likely02a00782007-02-20 09:05:00 +0100204
Simon Glass02b8f1a2021-05-08 07:00:05 -0600205 /* 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);
Grant Likely02a00782007-02-20 09:05:00 +0100212
Simon Glass02b8f1a2021-05-08 07:00:05 -0600213 return thislinelen;
214}
Grant Likely02a00782007-02-20 09:05:00 +0100215
Simon Glass02b8f1a2021-05-08 07:00:05 -0600216int print_buffer(ulong addr, const void *data, uint width, uint count,
217 uint linelen)
218{
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) {
225 uint thislinelen;
226 char buf[HEXDUMP_MAX_BUF_LENGTH(width * linelen)];
Andreas Bießmannb4ce02a2013-02-07 04:58:19 +0000227
Simon Glass02b8f1a2021-05-08 07:00:05 -0600228 thislinelen = hexdump_line(addr, data, width, count, linelen,
229 buf, sizeof(buf));
230 assert(thislinelen >= 0);
231 puts(buf);
232 putc('\n');
Grant Likely02a00782007-02-20 09:05:00 +0100233
234 /* update references */
Simon Glass02b8f1a2021-05-08 07:00:05 -0600235 data += thislinelen * width;
Andreas Bießmannb4ce02a2013-02-07 04:58:19 +0000236 addr += thislinelen * width;
237 count -= thislinelen;
Grant Likely02a00782007-02-20 09:05:00 +0100238
Simon Glass0e84d962024-09-29 19:49:50 -0600239 if (!IS_ENABLED(CONFIG_XPL_BUILD) && ctrlc())
Simon Glass02b8f1a2021-05-08 07:00:05 -0600240 return -EINTR;
Grant Likely02a00782007-02-20 09:05:00 +0100241 }
242
243 return 0;
244}