1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2002
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 
7 #include <compiler.h>
8 #include <console.h>
9 #include <display_options.h>
10 #include <div64.h>
11 #include <version_string.h>
12 #include <linux/ctype.h>
13 #include <linux/kernel.h>
14 #include <asm/io.h>
15 #include <stdio.h>
16 #include <vsprintf.h>
17 
display_options_get_banner_priv(bool newlines,const char * build_tag,char * buf,int size)18 char *display_options_get_banner_priv(bool newlines, const char *build_tag,
19 				      char *buf, int size)
20 {
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;
30 	if (len < 0)
31 		len = 0;
32 	snprintf(buf + len, size - len, "\n\n");
33 
34 	return buf;
35 }
36 
37 #ifndef BUILD_TAG
38 #define BUILD_TAG NULL
39 #endif
40 
display_options_get_banner(bool newlines,char * buf,int size)41 char *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 
display_options(void)46 int 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 
53 	return 0;
54 }
55 
print_freq(uint64_t freq,const char * s)56 void print_freq(uint64_t freq, const char *s)
57 {
58 	unsigned long m = 0;
59 	uint32_t f;
60 	static const char names[] = {'G', 'M', 'k'};
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) {
73 		printf("%llu Hz%s", freq, s);
74 		return;
75 	}
76 
77 	f = do_div(freq, d);
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 
90 	printf("%lu", (unsigned long) freq);
91 	if (m)
92 		printf(".%ld", m);
93 	printf(" %cHz%s", c, s);
94 }
95 
print_size(uint64_t size,const char * s)96 void print_size(uint64_t size, const char *s)
97 {
98 	unsigned long m = 0, n;
99 	uint64_t f;
100 	static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
101 	unsigned long d = 10 * ARRAY_SIZE(names);
102 	char c = 0;
103 	unsigned int i;
104 
105 	for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
106 		if (size >> d) {
107 			c = names[i];
108 			break;
109 		}
110 	}
111 
112 	if (!c) {
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);
119 		return;
120 	}
121 
122 	n = size >> d;
123 	f = size & ((1ULL << d) - 1);
124 
125 	/* If there's a remainder, deal with it */
126 	if (f) {
127 		m = (10ULL * f + (1ULL << (d - 1))) >> d;
128 
129 		if (m >= 10) {
130 			m -= 10;
131 			n += 1;
132 
133 			if (n == 1024 && i > 0) {
134 				n = 1;
135 				m = 0;
136 				c = names[i - 1];
137 			}
138 		}
139 	}
140 
141 	printf ("%lu", n);
142 	if (m) {
143 		printf (".%ld", m);
144 	}
145 	printf (" %ciB%s", c, s);
146 }
147 
148 #define MAX_LINE_LENGTH_BYTES		64
149 #define DEFAULT_LINE_LENGTH_BYTES	16
150 
hexdump_line(ulong addr,const void * data,uint width,uint count,uint linelen,char * out,int size)151 int hexdump_line(ulong addr, const void *data, uint width, uint count,
152 		 uint linelen, char *out, int size)
153 {
154 	/* linebuf as a union causes proper alignment */
155 	union linebuf {
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];
160 	} lb;
161 	uint thislinelen;
162 	int i;
163 	ulong x;
164 
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 
print_buffer(ulong addr,const void * data,uint width,uint count,uint linelen)216 int 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)];
227 
228 		thislinelen = hexdump_line(addr, data, width, count, linelen,
229 					   buf, sizeof(buf));
230 		assert(thislinelen >= 0);
231 		puts(buf);
232 		putc('\n');
233 
234 		/* update references */
235 		data += thislinelen * width;
236 		addr += thislinelen * width;
237 		count -= thislinelen;
238 
239 		if (!IS_ENABLED(CONFIG_XPL_BUILD) && ctrlc())
240 			return -EINTR;
241 	}
242 
243 	return 0;
244 }
245