wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 8 | #include <config.h> |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 9 | #include <common.h> |
Andreas Bießmann | 61d0195 | 2011-07-18 20:24:04 +0200 | [diff] [blame] | 10 | #include <version.h> |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 11 | #include <linux/ctype.h> |
| 12 | #include <asm/io.h> |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 13 | |
| 14 | int display_options (void) |
| 15 | { |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 16 | #if defined(BUILD_TAG) |
| 17 | printf ("\n\n%s, Build: %s\n\n", version_string, BUILD_TAG); |
| 18 | #else |
| 19 | printf ("\n\n%s\n\n", version_string); |
| 20 | #endif |
| 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | /* |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 25 | * print sizes as "xxx KiB", "xxx.y KiB", "xxx MiB", "xxx.y MiB", |
| 26 | * xxx GiB, xxx.y GiB, etc as needed; allow for optional trailing string |
Becky Bruce | e9954d4 | 2008-07-09 11:09:41 -0500 | [diff] [blame] | 27 | * (like "\n") |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 28 | */ |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 29 | void print_size(unsigned long long size, const char *s) |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 30 | { |
Timur Tabi | 508a4e7 | 2010-04-13 13:16:02 -0500 | [diff] [blame] | 31 | unsigned long m = 0, n; |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 32 | unsigned long long f; |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 33 | static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'}; |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 34 | unsigned long d = 10 * ARRAY_SIZE(names); |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 35 | char c = 0; |
| 36 | unsigned int i; |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 37 | |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 38 | for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) { |
| 39 | if (size >> d) { |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 40 | c = names[i]; |
| 41 | break; |
Becky Bruce | e9954d4 | 2008-07-09 11:09:41 -0500 | [diff] [blame] | 42 | } |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 45 | if (!c) { |
| 46 | printf("%llu Bytes%s", size, s); |
| 47 | return; |
| 48 | } |
| 49 | |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 50 | n = size >> d; |
| 51 | f = size & ((1ULL << d) - 1); |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 52 | |
Becky Bruce | e9954d4 | 2008-07-09 11:09:41 -0500 | [diff] [blame] | 53 | /* If there's a remainder, deal with it */ |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 54 | if (f) { |
| 55 | m = (10ULL * f + (1ULL << (d - 1))) >> d; |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 56 | |
Becky Bruce | e9954d4 | 2008-07-09 11:09:41 -0500 | [diff] [blame] | 57 | if (m >= 10) { |
| 58 | m -= 10; |
| 59 | n += 1; |
| 60 | } |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 63 | printf ("%lu", n); |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 64 | if (m) { |
| 65 | printf (".%ld", m); |
| 66 | } |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 67 | printf (" %ciB%s", c, s); |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 68 | } |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Print data buffer in hex and ascii form to the terminal. |
| 72 | * |
| 73 | * data reads are buffered so that each memory address is only read once. |
| 74 | * Useful when displaying the contents of volatile registers. |
| 75 | * |
| 76 | * parameters: |
| 77 | * addr: Starting address to display at start of line |
| 78 | * data: pointer to data buffer |
| 79 | * width: data value width. May be 1, 2, or 4. |
| 80 | * count: number of values to display |
| 81 | * linelen: Number of values to print per line; specify 0 for default length |
| 82 | */ |
| 83 | #define MAX_LINE_LENGTH_BYTES (64) |
| 84 | #define DEFAULT_LINE_LENGTH_BYTES (16) |
Simon Glass | d963b72 | 2013-02-24 17:33:12 +0000 | [diff] [blame] | 85 | int print_buffer(ulong addr, const void *data, uint width, uint count, |
| 86 | uint linelen) |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 87 | { |
Reinhard Meyer | d9f13d0 | 2010-09-08 12:25:40 +0200 | [diff] [blame] | 88 | /* linebuf as a union causes proper alignment */ |
| 89 | union linebuf { |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 90 | #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA |
| 91 | uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1]; |
| 92 | #endif |
Reinhard Meyer | d9f13d0 | 2010-09-08 12:25:40 +0200 | [diff] [blame] | 93 | uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1]; |
| 94 | uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1]; |
| 95 | uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1]; |
| 96 | } lb; |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 97 | int i; |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 98 | #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA |
| 99 | uint64_t x; |
| 100 | #else |
| 101 | uint32_t x; |
| 102 | #endif |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 103 | |
| 104 | if (linelen*width > MAX_LINE_LENGTH_BYTES) |
| 105 | linelen = MAX_LINE_LENGTH_BYTES / width; |
| 106 | if (linelen < 1) |
| 107 | linelen = DEFAULT_LINE_LENGTH_BYTES / width; |
| 108 | |
| 109 | while (count) { |
Andreas Bießmann | b4ce02a | 2013-02-07 04:58:19 +0000 | [diff] [blame] | 110 | uint thislinelen = linelen; |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 111 | printf("%08lx:", addr); |
| 112 | |
| 113 | /* check for overflow condition */ |
Andreas Bießmann | b4ce02a | 2013-02-07 04:58:19 +0000 | [diff] [blame] | 114 | if (count < thislinelen) |
| 115 | thislinelen = count; |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 116 | |
| 117 | /* Copy from memory into linebuf and print hex values */ |
Andreas Bießmann | b4ce02a | 2013-02-07 04:58:19 +0000 | [diff] [blame] | 118 | for (i = 0; i < thislinelen; i++) { |
Mike Frysinger | a3a26fc | 2010-07-23 06:17:30 -0400 | [diff] [blame] | 119 | if (width == 4) |
Reinhard Meyer | d9f13d0 | 2010-09-08 12:25:40 +0200 | [diff] [blame] | 120 | x = lb.ui[i] = *(volatile uint32_t *)data; |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 121 | #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA |
| 122 | else if (width == 8) |
| 123 | x = lb.uq[i] = *(volatile uint64_t *)data; |
| 124 | #endif |
Mike Frysinger | a3a26fc | 2010-07-23 06:17:30 -0400 | [diff] [blame] | 125 | else if (width == 2) |
Reinhard Meyer | d9f13d0 | 2010-09-08 12:25:40 +0200 | [diff] [blame] | 126 | x = lb.us[i] = *(volatile uint16_t *)data; |
Mike Frysinger | a3a26fc | 2010-07-23 06:17:30 -0400 | [diff] [blame] | 127 | else |
Reinhard Meyer | d9f13d0 | 2010-09-08 12:25:40 +0200 | [diff] [blame] | 128 | x = lb.uc[i] = *(volatile uint8_t *)data; |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 129 | #ifdef CONFIG_SYS_SUPPORT_64BIT_DATA |
| 130 | printf(" %0*llx", width * 2, x); |
| 131 | #else |
Mike Frysinger | a3a26fc | 2010-07-23 06:17:30 -0400 | [diff] [blame] | 132 | printf(" %0*x", width * 2, x); |
York Sun | 6c48001 | 2014-02-26 17:03:19 -0800 | [diff] [blame] | 133 | #endif |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 134 | data += width; |
| 135 | } |
| 136 | |
Andreas Bießmann | b4ce02a | 2013-02-07 04:58:19 +0000 | [diff] [blame] | 137 | while (thislinelen < linelen) { |
| 138 | /* fill line with whitespace for nice ASCII print */ |
| 139 | for (i=0; i<width*2+1; i++) |
| 140 | puts(" "); |
| 141 | linelen--; |
| 142 | } |
| 143 | |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 144 | /* Print data in ASCII characters */ |
Andreas Bießmann | b4ce02a | 2013-02-07 04:58:19 +0000 | [diff] [blame] | 145 | for (i = 0; i < thislinelen * width; i++) { |
Reinhard Meyer | d9f13d0 | 2010-09-08 12:25:40 +0200 | [diff] [blame] | 146 | if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80) |
| 147 | lb.uc[i] = '.'; |
| 148 | } |
| 149 | lb.uc[i] = '\0'; |
| 150 | printf(" %s\n", lb.uc); |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 151 | |
| 152 | /* update references */ |
Andreas Bießmann | b4ce02a | 2013-02-07 04:58:19 +0000 | [diff] [blame] | 153 | addr += thislinelen * width; |
| 154 | count -= thislinelen; |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 155 | |
| 156 | if (ctrlc()) |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |