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 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 24 | #include <config.h> |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 25 | #include <common.h> |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 26 | #include <linux/ctype.h> |
| 27 | #include <asm/io.h> |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 28 | |
| 29 | int display_options (void) |
| 30 | { |
| 31 | extern char version_string[]; |
| 32 | |
| 33 | #if defined(BUILD_TAG) |
| 34 | printf ("\n\n%s, Build: %s\n\n", version_string, BUILD_TAG); |
| 35 | #else |
| 36 | printf ("\n\n%s\n\n", version_string); |
| 37 | #endif |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | /* |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 42 | * print sizes as "xxx KiB", "xxx.y KiB", "xxx MiB", "xxx.y MiB", |
| 43 | * 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] | 44 | * (like "\n") |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 45 | */ |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 46 | void print_size(unsigned long long size, const char *s) |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 47 | { |
Timur Tabi | 508a4e7 | 2010-04-13 13:16:02 -0500 | [diff] [blame] | 48 | unsigned long m = 0, n; |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 49 | unsigned long long f; |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 50 | static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'}; |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 51 | unsigned long d = 10 * ARRAY_SIZE(names); |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 52 | char c = 0; |
| 53 | unsigned int i; |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 54 | |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 55 | for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) { |
| 56 | if (size >> d) { |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 57 | c = names[i]; |
| 58 | break; |
Becky Bruce | e9954d4 | 2008-07-09 11:09:41 -0500 | [diff] [blame] | 59 | } |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 62 | if (!c) { |
| 63 | printf("%llu Bytes%s", size, s); |
| 64 | return; |
| 65 | } |
| 66 | |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 67 | n = size >> d; |
| 68 | f = size & ((1ULL << d) - 1); |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 69 | |
Becky Bruce | e9954d4 | 2008-07-09 11:09:41 -0500 | [diff] [blame] | 70 | /* If there's a remainder, deal with it */ |
Nick Thompson | dd7e6c1 | 2010-05-11 11:29:52 +0100 | [diff] [blame] | 71 | if (f) { |
| 72 | m = (10ULL * f + (1ULL << (d - 1))) >> d; |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 73 | |
Becky Bruce | e9954d4 | 2008-07-09 11:09:41 -0500 | [diff] [blame] | 74 | if (m >= 10) { |
| 75 | m -= 10; |
| 76 | n += 1; |
| 77 | } |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 80 | printf ("%lu", n); |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 81 | if (m) { |
| 82 | printf (".%ld", m); |
| 83 | } |
Timur Tabi | 53258da | 2010-04-13 13:16:03 -0500 | [diff] [blame] | 84 | printf (" %ciB%s", c, s); |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 85 | } |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Print data buffer in hex and ascii form to the terminal. |
| 89 | * |
| 90 | * data reads are buffered so that each memory address is only read once. |
| 91 | * Useful when displaying the contents of volatile registers. |
| 92 | * |
| 93 | * parameters: |
| 94 | * addr: Starting address to display at start of line |
| 95 | * data: pointer to data buffer |
| 96 | * width: data value width. May be 1, 2, or 4. |
| 97 | * count: number of values to display |
| 98 | * linelen: Number of values to print per line; specify 0 for default length |
| 99 | */ |
| 100 | #define MAX_LINE_LENGTH_BYTES (64) |
| 101 | #define DEFAULT_LINE_LENGTH_BYTES (16) |
| 102 | int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen) |
| 103 | { |
Reinhard Meyer | d9f13d0 | 2010-09-08 12:25:40 +0200 | [diff] [blame^] | 104 | /* linebuf as a union causes proper alignment */ |
| 105 | union linebuf { |
| 106 | uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1]; |
| 107 | uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1]; |
| 108 | uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1]; |
| 109 | } lb; |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 110 | int i; |
| 111 | |
| 112 | if (linelen*width > MAX_LINE_LENGTH_BYTES) |
| 113 | linelen = MAX_LINE_LENGTH_BYTES / width; |
| 114 | if (linelen < 1) |
| 115 | linelen = DEFAULT_LINE_LENGTH_BYTES / width; |
| 116 | |
| 117 | while (count) { |
| 118 | printf("%08lx:", addr); |
| 119 | |
| 120 | /* check for overflow condition */ |
| 121 | if (count < linelen) |
| 122 | linelen = count; |
| 123 | |
| 124 | /* Copy from memory into linebuf and print hex values */ |
| 125 | for (i = 0; i < linelen; i++) { |
Mike Frysinger | a3a26fc | 2010-07-23 06:17:30 -0400 | [diff] [blame] | 126 | uint32_t x; |
| 127 | if (width == 4) |
Reinhard Meyer | d9f13d0 | 2010-09-08 12:25:40 +0200 | [diff] [blame^] | 128 | x = lb.ui[i] = *(volatile uint32_t *)data; |
Mike Frysinger | a3a26fc | 2010-07-23 06:17:30 -0400 | [diff] [blame] | 129 | else if (width == 2) |
Reinhard Meyer | d9f13d0 | 2010-09-08 12:25:40 +0200 | [diff] [blame^] | 130 | x = lb.us[i] = *(volatile uint16_t *)data; |
Mike Frysinger | a3a26fc | 2010-07-23 06:17:30 -0400 | [diff] [blame] | 131 | else |
Reinhard Meyer | d9f13d0 | 2010-09-08 12:25:40 +0200 | [diff] [blame^] | 132 | x = lb.uc[i] = *(volatile uint8_t *)data; |
Mike Frysinger | a3a26fc | 2010-07-23 06:17:30 -0400 | [diff] [blame] | 133 | printf(" %0*x", width * 2, x); |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 134 | data += width; |
| 135 | } |
| 136 | |
| 137 | /* Print data in ASCII characters */ |
Reinhard Meyer | d9f13d0 | 2010-09-08 12:25:40 +0200 | [diff] [blame^] | 138 | for (i = 0; i < linelen * width; i++) { |
| 139 | if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80) |
| 140 | lb.uc[i] = '.'; |
| 141 | } |
| 142 | lb.uc[i] = '\0'; |
| 143 | printf(" %s\n", lb.uc); |
Grant Likely | 02a0078 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 144 | |
| 145 | /* update references */ |
| 146 | addr += linelen * width; |
| 147 | count -= linelen; |
| 148 | |
| 149 | if (ctrlc()) |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |