colibri_imx6: fix video stdout in default environment
[oweals/u-boot.git] / lib / display_options.c
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 <common.h>
8 #include <compiler.h>
9 #include <console.h>
10 #include <div64.h>
11 #include <version.h>
12 #include <linux/ctype.h>
13 #include <asm/io.h>
14
15 char *display_options_get_banner_priv(bool newlines, const char *build_tag,
16                                       char *buf, int size)
17 {
18         int len;
19
20         len = snprintf(buf, size, "%s%s", newlines ? "\n\n" : "",
21                        version_string);
22         if (build_tag && len < size)
23                 len += snprintf(buf + len, size - len, ", Build: %s",
24                                 build_tag);
25         if (len > size - 3)
26                 len = size - 3;
27         if (len < 0)
28                 len = 0;
29         snprintf(buf + len, size - len, "\n\n");
30
31         return buf;
32 }
33
34 #ifndef BUILD_TAG
35 #define BUILD_TAG NULL
36 #endif
37
38 char *display_options_get_banner(bool newlines, char *buf, int size)
39 {
40         return display_options_get_banner_priv(newlines, BUILD_TAG, buf, size);
41 }
42
43 int display_options(void)
44 {
45         char buf[DISPLAY_OPTIONS_BANNER_LENGTH];
46
47         display_options_get_banner(true, buf, sizeof(buf));
48         printf("%s", buf);
49
50         return 0;
51 }
52
53 void print_freq(uint64_t freq, const char *s)
54 {
55         unsigned long m = 0;
56         uint32_t f;
57         static const char names[] = {'G', 'M', 'K'};
58         unsigned long d = 1e9;
59         char c = 0;
60         unsigned int i;
61
62         for (i = 0; i < ARRAY_SIZE(names); i++, d /= 1000) {
63                 if (freq >= d) {
64                         c = names[i];
65                         break;
66                 }
67         }
68
69         if (!c) {
70                 printf("%llu Hz%s", freq, s);
71                 return;
72         }
73
74         f = do_div(freq, d);
75
76         /* If there's a remainder, show the first few digits */
77         if (f) {
78                 m = f;
79                 while (m > 1000)
80                         m /= 10;
81                 while (m && !(m % 10))
82                         m /= 10;
83                 if (m >= 100)
84                         m = (m / 10) + (m % 100 >= 50);
85         }
86
87         printf("%lu", (unsigned long) freq);
88         if (m)
89                 printf(".%ld", m);
90         printf(" %cHz%s", c, s);
91 }
92
93 void print_size(uint64_t size, const char *s)
94 {
95         unsigned long m = 0, n;
96         uint64_t f;
97         static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
98         unsigned long d = 10 * ARRAY_SIZE(names);
99         char c = 0;
100         unsigned int i;
101
102         for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
103                 if (size >> d) {
104                         c = names[i];
105                         break;
106                 }
107         }
108
109         if (!c) {
110                 printf("%llu Bytes%s", size, s);
111                 return;
112         }
113
114         n = size >> d;
115         f = size & ((1ULL << d) - 1);
116
117         /* If there's a remainder, deal with it */
118         if (f) {
119                 m = (10ULL * f + (1ULL << (d - 1))) >> d;
120
121                 if (m >= 10) {
122                         m -= 10;
123                         n += 1;
124                 }
125         }
126
127         printf ("%lu", n);
128         if (m) {
129                 printf (".%ld", m);
130         }
131         printf (" %ciB%s", c, s);
132 }
133
134 #define MAX_LINE_LENGTH_BYTES (64)
135 #define DEFAULT_LINE_LENGTH_BYTES (16)
136 int print_buffer(ulong addr, const void *data, uint width, uint count,
137                  uint linelen)
138 {
139         /* linebuf as a union causes proper alignment */
140         union linebuf {
141 #ifdef MEM_SUPPORT_64BIT_DATA
142                 uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1];
143 #endif
144                 uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
145                 uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
146                 uint8_t  uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
147         } lb;
148         int i;
149 #ifdef MEM_SUPPORT_64BIT_DATA
150         uint64_t __maybe_unused x;
151 #else
152         uint32_t __maybe_unused x;
153 #endif
154
155         if (linelen*width > MAX_LINE_LENGTH_BYTES)
156                 linelen = MAX_LINE_LENGTH_BYTES / width;
157         if (linelen < 1)
158                 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
159
160         while (count) {
161                 uint thislinelen = linelen;
162                 printf("%08lx:", addr);
163
164                 /* check for overflow condition */
165                 if (count < thislinelen)
166                         thislinelen = count;
167
168                 /* Copy from memory into linebuf and print hex values */
169                 for (i = 0; i < thislinelen; i++) {
170                         if (width == 4)
171                                 x = lb.ui[i] = *(volatile uint32_t *)data;
172 #ifdef MEM_SUPPORT_64BIT_DATA
173                         else if (width == 8)
174                                 x = lb.uq[i] = *(volatile uint64_t *)data;
175 #endif
176                         else if (width == 2)
177                                 x = lb.us[i] = *(volatile uint16_t *)data;
178                         else
179                                 x = lb.uc[i] = *(volatile uint8_t *)data;
180 #if defined(CONFIG_SPL_BUILD)
181                         printf(" %x", (uint)x);
182 #elif defined(MEM_SUPPORT_64BIT_DATA)
183                         printf(" %0*llx", width * 2, (long long)x);
184 #else
185                         printf(" %0*x", width * 2, x);
186 #endif
187                         data += width;
188                 }
189
190                 while (thislinelen < linelen) {
191                         /* fill line with whitespace for nice ASCII print */
192                         for (i=0; i<width*2+1; i++)
193                                 puts(" ");
194                         linelen--;
195                 }
196
197                 /* Print data in ASCII characters */
198                 for (i = 0; i < thislinelen * width; i++) {
199                         if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
200                                 lb.uc[i] = '.';
201                 }
202                 lb.uc[i] = '\0';
203                 printf("    %s\n", lb.uc);
204
205                 /* update references */
206                 addr += thislinelen * width;
207                 count -= thislinelen;
208
209 #ifndef CONFIG_SPL_BUILD
210                 if (ctrlc())
211                         return -1;
212 #endif
213         }
214
215         return 0;
216 }