blob: 86df05d9e5dde65fd4e7a0f427c5f5acda031543 [file] [log] [blame]
wdenk20e0e232002-09-08 16:09:41 +00001/*
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 Likelyc95c4282007-02-20 09:05:00 +010024#include <config.h>
wdenk20e0e232002-09-08 16:09:41 +000025#include <common.h>
Grant Likelyc95c4282007-02-20 09:05:00 +010026#include <linux/ctype.h>
27#include <asm/io.h>
wdenk20e0e232002-09-08 16:09:41 +000028
29int 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 Tabi4b42c902010-04-13 13:16:03 -050042 * 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 Bruce417faf22008-07-09 11:09:41 -050044 * (like "\n")
wdenk20e0e232002-09-08 16:09:41 +000045 */
Timur Tabi4b42c902010-04-13 13:16:03 -050046void print_size(unsigned long long size, const char *s)
wdenk20e0e232002-09-08 16:09:41 +000047{
Timur Tabi52dbac62010-04-13 13:16:02 -050048 unsigned long m = 0, n;
Timur Tabi4b42c902010-04-13 13:16:03 -050049 static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
50 unsigned long long d = 1ULL << (10 * ARRAY_SIZE(names));
51 char c = 0;
52 unsigned int i;
wdenk20e0e232002-09-08 16:09:41 +000053
Timur Tabi4b42c902010-04-13 13:16:03 -050054 for (i = 0; i < ARRAY_SIZE(names); i++, d >>= 10) {
55 if (size >= d) {
56 c = names[i];
57 break;
Becky Bruce417faf22008-07-09 11:09:41 -050058 }
wdenk20e0e232002-09-08 16:09:41 +000059 }
60
Timur Tabi4b42c902010-04-13 13:16:03 -050061 if (!c) {
62 printf("%llu Bytes%s", size, s);
63 return;
64 }
65
wdenk20e0e232002-09-08 16:09:41 +000066 n = size / d;
67
Becky Bruce417faf22008-07-09 11:09:41 -050068 /* If there's a remainder, deal with it */
69 if(size % d) {
70 m = (10 * (size - (n * d)) + (d / 2) ) / d;
wdenk20e0e232002-09-08 16:09:41 +000071
Becky Bruce417faf22008-07-09 11:09:41 -050072 if (m >= 10) {
73 m -= 10;
74 n += 1;
75 }
wdenk0d498392003-07-01 21:06:45 +000076 }
77
Timur Tabi4b42c902010-04-13 13:16:03 -050078 printf ("%lu", n);
wdenk20e0e232002-09-08 16:09:41 +000079 if (m) {
80 printf (".%ld", m);
81 }
Timur Tabi4b42c902010-04-13 13:16:03 -050082 printf (" %ciB%s", c, s);
wdenk20e0e232002-09-08 16:09:41 +000083}
Grant Likelyc95c4282007-02-20 09:05:00 +010084
85/*
86 * Print data buffer in hex and ascii form to the terminal.
87 *
88 * data reads are buffered so that each memory address is only read once.
89 * Useful when displaying the contents of volatile registers.
90 *
91 * parameters:
92 * addr: Starting address to display at start of line
93 * data: pointer to data buffer
94 * width: data value width. May be 1, 2, or 4.
95 * count: number of values to display
96 * linelen: Number of values to print per line; specify 0 for default length
97 */
98#define MAX_LINE_LENGTH_BYTES (64)
99#define DEFAULT_LINE_LENGTH_BYTES (16)
100int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen)
101{
102 uint8_t linebuf[MAX_LINE_LENGTH_BYTES];
103 uint32_t *uip = (void*)linebuf;
104 uint16_t *usp = (void*)linebuf;
105 uint8_t *ucp = (void*)linebuf;
106 int i;
107
108 if (linelen*width > MAX_LINE_LENGTH_BYTES)
109 linelen = MAX_LINE_LENGTH_BYTES / width;
110 if (linelen < 1)
111 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
112
113 while (count) {
114 printf("%08lx:", addr);
115
116 /* check for overflow condition */
117 if (count < linelen)
118 linelen = count;
119
120 /* Copy from memory into linebuf and print hex values */
121 for (i = 0; i < linelen; i++) {
122 if (width == 4) {
123 uip[i] = *(volatile uint32_t *)data;
124 printf(" %08x", uip[i]);
125 } else if (width == 2) {
126 usp[i] = *(volatile uint16_t *)data;
127 printf(" %04x", usp[i]);
128 } else {
129 ucp[i] = *(volatile uint8_t *)data;
130 printf(" %02x", ucp[i]);
131 }
132 data += width;
133 }
134
135 /* Print data in ASCII characters */
136 puts(" ");
137 for (i = 0; i < linelen * width; i++)
138 putc(isprint(ucp[i]) && (ucp[i] < 0x80) ? ucp[i] : '.');
139 putc ('\n');
140
141 /* update references */
142 addr += linelen * width;
143 count -= linelen;
144
145 if (ctrlc())
146 return -1;
147 }
148
149 return 0;
150}