Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2002 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 28db7f5 | 2020-06-07 20:33:00 -0600 | [diff] [blame] | 7 | #include <compiler.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 8 | #include <console.h> |
Simon Glass | 1ab1692 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 9 | #include <display_options.h> |
Simon Glass | 1b5fe40 | 2015-04-29 07:56:43 -0600 | [diff] [blame] | 10 | #include <div64.h> |
Pali Rohár | ba87ddf | 2021-08-02 15:18:31 +0200 | [diff] [blame] | 11 | #include <version_string.h> |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 12 | #include <linux/ctype.h> |
Tom Rini | c4470ea | 2023-12-14 13:16:56 -0500 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 14 | #include <asm/io.h> |
Raymond Mao | 24da831 | 2024-05-16 14:11:52 -0700 | [diff] [blame] | 15 | #include <stdio.h> |
Tom Rini | c4470ea | 2023-12-14 13:16:56 -0500 | [diff] [blame] | 16 | #include <vsprintf.h> |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 17 | |
Simon Glass | e8583f9 | 2017-06-16 12:51:42 -0600 | [diff] [blame] | 18 | char *display_options_get_banner_priv(bool newlines, const char *build_tag, |
| 19 | char *buf, int size) |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 20 | { |
Simon Glass | e8583f9 | 2017-06-16 12:51:42 -0600 | [diff] [blame] | 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; |
Heinrich Schuchardt | b144ab1 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 30 | if (len < 0) |
| 31 | len = 0; |
| 32 | snprintf(buf + len, size - len, "\n\n"); |
Simon Glass | e8583f9 | 2017-06-16 12:51:42 -0600 | [diff] [blame] | 33 | |
| 34 | return buf; |
| 35 | } |
| 36 | |
| 37 | #ifndef BUILD_TAG |
| 38 | #define BUILD_TAG NULL |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 39 | #endif |
Simon Glass | e8583f9 | 2017-06-16 12:51:42 -0600 | [diff] [blame] | 40 | |
| 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 | |
| 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 | |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
Simon Glass | 1b5fe40 | 2015-04-29 07:56:43 -0600 | [diff] [blame] | 56 | void print_freq(uint64_t freq, const char *s) |
| 57 | { |
Heiko Schocher | 62cb156 | 2015-06-29 09:10:46 +0200 | [diff] [blame] | 58 | unsigned long m = 0; |
Simon Glass | 1b5fe40 | 2015-04-29 07:56:43 -0600 | [diff] [blame] | 59 | uint32_t f; |
Heinrich Schuchardt | ae1c957 | 2020-10-08 22:23:23 +0200 | [diff] [blame] | 60 | static const char names[] = {'G', 'M', 'k'}; |
Simon Glass | 1b5fe40 | 2015-04-29 07:56:43 -0600 | [diff] [blame] | 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) { |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 73 | printf("%llu Hz%s", freq, s); |
Simon Glass | 1b5fe40 | 2015-04-29 07:56:43 -0600 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
| 77 | f = do_div(freq, d); |
Simon Glass | 1b5fe40 | 2015-04-29 07:56:43 -0600 | [diff] [blame] | 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 | |
Suriyan Ramasami | 28ef5c5 | 2015-08-18 09:25:33 -0700 | [diff] [blame] | 90 | printf("%lu", (unsigned long) freq); |
Simon Glass | 1b5fe40 | 2015-04-29 07:56:43 -0600 | [diff] [blame] | 91 | if (m) |
| 92 | printf(".%ld", m); |
| 93 | printf(" %cHz%s", c, s); |
| 94 | } |
| 95 | |
Simon Glass | 08ea373 | 2014-10-15 04:38:35 -0600 | [diff] [blame] | 96 | void print_size(uint64_t size, const char *s) |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 97 | { |
Timur Tabi | 508a4e7 | 2010-04-13 13:16:02 -0500 | [diff] [blame] | 98 | unsigned long m = 0, n; |
Simon Glass | 08ea373 | 2014-10-15 04:38:35 -0600 | [diff] [blame] | 99 | uint64_t f; |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 100 | static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'}; |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 101 | unsigned long d = 10 * ARRAY_SIZE(names); |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 102 | char c = 0; |
| 103 | unsigned int i; |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 104 | |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 105 | for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) { |
| 106 | if (size >> d) { |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 107 | c = names[i]; |
| 108 | break; |
Becky Bruce | e9954d4 | 2008-07-09 11:09:41 -0500 | [diff] [blame] | 109 | } |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 112 | if (!c) { |
Matwey V. Kornilov | fae8875 | 2021-08-06 00:22:58 +0300 | [diff] [blame] | 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); |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 122 | n = size >> d; |
| 123 | f = size & ((1ULL << d) - 1); |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 124 | |
Becky Bruce | e9954d4 | 2008-07-09 11:09:41 -0500 | [diff] [blame] | 125 | /* If there's a remainder, deal with it */ |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 126 | if (f) { |
| 127 | m = (10ULL * f + (1ULL << (d - 1))) >> d; |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 128 | |
Becky Bruce | e9954d4 | 2008-07-09 11:09:41 -0500 | [diff] [blame] | 129 | if (m >= 10) { |
| 130 | m -= 10; |
| 131 | n += 1; |
Pali Rohár | c14fad5 | 2022-09-12 21:02:27 +0200 | [diff] [blame] | 132 | |
| 133 | if (n == 1024 && i > 0) { |
| 134 | n = 1; |
| 135 | m = 0; |
| 136 | c = names[i - 1]; |
| 137 | } |
Becky Bruce | e9954d4 | 2008-07-09 11:09:41 -0500 | [diff] [blame] | 138 | } |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 141 | printf ("%lu", n); |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 142 | if (m) { |
| 143 | printf (".%ld", m); |
| 144 | } |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 145 | printf (" %ciB%s", c, s); |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 146 | } |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 147 | |
Simon Glass | 02b8f1a | 2021-05-08 07:00:05 -0600 | [diff] [blame] | 148 | #define MAX_LINE_LENGTH_BYTES 64 |
| 149 | #define DEFAULT_LINE_LENGTH_BYTES 16 |
| 150 | |
| 151 | int hexdump_line(ulong addr, const void *data, uint width, uint count, |
| 152 | uint linelen, char *out, int size) |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 153 | { |
Reinhard Meyer | d9f13d0 | 2010-09-08 12:25:40 +0200 | [diff] [blame] | 154 | /* linebuf as a union causes proper alignment */ |
| 155 | union linebuf { |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 156 | uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1]; |
Reinhard Meyer | d9f13d0 | 2010-09-08 12:25:40 +0200 | [diff] [blame] | 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; |
Simon Glass | 02b8f1a | 2021-05-08 07:00:05 -0600 | [diff] [blame] | 161 | uint thislinelen; |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 162 | int i; |
Simon Glass | 2056b9e | 2020-06-02 19:26:47 -0600 | [diff] [blame] | 163 | ulong x; |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 164 | |
Simon Glass | 02b8f1a | 2021-05-08 07:00:05 -0600 | [diff] [blame] | 165 | if (linelen * width > MAX_LINE_LENGTH_BYTES) |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 166 | linelen = MAX_LINE_LENGTH_BYTES / width; |
| 167 | if (linelen < 1) |
| 168 | linelen = DEFAULT_LINE_LENGTH_BYTES / width; |
| 169 | |
Simon Glass | 02b8f1a | 2021-05-08 07:00:05 -0600 | [diff] [blame] | 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++ = ' '; |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 204 | |
Simon Glass | 02b8f1a | 2021-05-08 07:00:05 -0600 | [diff] [blame] | 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); |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 212 | |
Simon Glass | 02b8f1a | 2021-05-08 07:00:05 -0600 | [diff] [blame] | 213 | return thislinelen; |
| 214 | } |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 215 | |
Simon Glass | 02b8f1a | 2021-05-08 07:00:05 -0600 | [diff] [blame] | 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)]; |
Andreas Bießmann | b4ce02a | 2013-02-07 04:58:19 +0000 | [diff] [blame] | 227 | |
Simon Glass | 02b8f1a | 2021-05-08 07:00:05 -0600 | [diff] [blame] | 228 | thislinelen = hexdump_line(addr, data, width, count, linelen, |
| 229 | buf, sizeof(buf)); |
| 230 | assert(thislinelen >= 0); |
| 231 | puts(buf); |
| 232 | putc('\n'); |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 233 | |
| 234 | /* update references */ |
Simon Glass | 02b8f1a | 2021-05-08 07:00:05 -0600 | [diff] [blame] | 235 | data += thislinelen * width; |
Andreas Bießmann | b4ce02a | 2013-02-07 04:58:19 +0000 | [diff] [blame] | 236 | addr += thislinelen * width; |
| 237 | count -= thislinelen; |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 238 | |
Simon Glass | 02b8f1a | 2021-05-08 07:00:05 -0600 | [diff] [blame] | 239 | if (!IS_ENABLED(CONFIG_SPL_BUILD) && ctrlc()) |
| 240 | return -EINTR; |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |