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 | c95c428 | 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 | c95c428 | 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 | /* |
| 42 | * print sizes as "xxx kB", "xxx.y kB", "xxx MB" or "xxx.y MB" as needed; |
| 43 | * allow for optional trailing string (like "\n") |
| 44 | */ |
| 45 | void print_size (ulong size, const char *s) |
| 46 | { |
| 47 | ulong m, n; |
| 48 | ulong d = 1 << 20; /* 1 MB */ |
| 49 | char c = 'M'; |
| 50 | |
| 51 | if (size < d) { /* print in kB */ |
| 52 | c = 'k'; |
| 53 | d = 1 << 10; |
| 54 | } |
| 55 | |
| 56 | n = size / d; |
| 57 | |
| 58 | m = (10 * (size - (n * d)) + (d / 2) ) / d; |
| 59 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 60 | if (m >= 10) { |
| 61 | m -= 10; |
| 62 | n += 1; |
| 63 | } |
| 64 | |
wdenk | 20e0e23 | 2002-09-08 16:09:41 +0000 | [diff] [blame] | 65 | printf ("%2ld", n); |
| 66 | if (m) { |
| 67 | printf (".%ld", m); |
| 68 | } |
| 69 | printf (" %cB%s", c, s); |
| 70 | } |
Grant Likely | c95c428 | 2007-02-20 09:05:00 +0100 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * Print data buffer in hex and ascii form to the terminal. |
| 74 | * |
| 75 | * data reads are buffered so that each memory address is only read once. |
| 76 | * Useful when displaying the contents of volatile registers. |
| 77 | * |
| 78 | * parameters: |
| 79 | * addr: Starting address to display at start of line |
| 80 | * data: pointer to data buffer |
| 81 | * width: data value width. May be 1, 2, or 4. |
| 82 | * count: number of values to display |
| 83 | * linelen: Number of values to print per line; specify 0 for default length |
| 84 | */ |
| 85 | #define MAX_LINE_LENGTH_BYTES (64) |
| 86 | #define DEFAULT_LINE_LENGTH_BYTES (16) |
| 87 | int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen) |
| 88 | { |
| 89 | uint8_t linebuf[MAX_LINE_LENGTH_BYTES]; |
| 90 | uint32_t *uip = (void*)linebuf; |
| 91 | uint16_t *usp = (void*)linebuf; |
| 92 | uint8_t *ucp = (void*)linebuf; |
| 93 | int i; |
| 94 | |
| 95 | if (linelen*width > MAX_LINE_LENGTH_BYTES) |
| 96 | linelen = MAX_LINE_LENGTH_BYTES / width; |
| 97 | if (linelen < 1) |
| 98 | linelen = DEFAULT_LINE_LENGTH_BYTES / width; |
| 99 | |
| 100 | while (count) { |
| 101 | printf("%08lx:", addr); |
| 102 | |
| 103 | /* check for overflow condition */ |
| 104 | if (count < linelen) |
| 105 | linelen = count; |
| 106 | |
| 107 | /* Copy from memory into linebuf and print hex values */ |
| 108 | for (i = 0; i < linelen; i++) { |
| 109 | if (width == 4) { |
| 110 | uip[i] = *(volatile uint32_t *)data; |
| 111 | printf(" %08x", uip[i]); |
| 112 | } else if (width == 2) { |
| 113 | usp[i] = *(volatile uint16_t *)data; |
| 114 | printf(" %04x", usp[i]); |
| 115 | } else { |
| 116 | ucp[i] = *(volatile uint8_t *)data; |
| 117 | printf(" %02x", ucp[i]); |
| 118 | } |
| 119 | data += width; |
| 120 | } |
| 121 | |
| 122 | /* Print data in ASCII characters */ |
| 123 | puts(" "); |
| 124 | for (i = 0; i < linelen * width; i++) |
| 125 | putc(isprint(ucp[i]) && (ucp[i] < 0x80) ? ucp[i] : '.'); |
| 126 | putc ('\n'); |
| 127 | |
| 128 | /* update references */ |
| 129 | addr += linelen * width; |
| 130 | count -= linelen; |
| 131 | |
| 132 | if (ctrlc()) |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |