blob: e6d684bd60bdc3e18ef077775d8f532eaeb8d352 [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 Likely02a00782007-02-20 09:05:00 +010024#include <config.h>
wdenk20e0e232002-09-08 16:09:41 +000025#include <common.h>
Andreas Bießmann61d01952011-07-18 20:24:04 +020026#include <version.h>
Grant Likely02a00782007-02-20 09:05:00 +010027#include <linux/ctype.h>
28#include <asm/io.h>
wdenk20e0e232002-09-08 16:09:41 +000029
30int display_options (void)
31{
wdenk20e0e232002-09-08 16:09:41 +000032#if defined(BUILD_TAG)
33 printf ("\n\n%s, Build: %s\n\n", version_string, BUILD_TAG);
34#else
35 printf ("\n\n%s\n\n", version_string);
36#endif
37 return 0;
38}
39
40/*
Timur Tabi53258da2010-04-13 13:16:03 -050041 * print sizes as "xxx KiB", "xxx.y KiB", "xxx MiB", "xxx.y MiB",
42 * xxx GiB, xxx.y GiB, etc as needed; allow for optional trailing string
Becky Brucee9954d42008-07-09 11:09:41 -050043 * (like "\n")
wdenk20e0e232002-09-08 16:09:41 +000044 */
Timur Tabi53258da2010-04-13 13:16:03 -050045void print_size(unsigned long long size, const char *s)
wdenk20e0e232002-09-08 16:09:41 +000046{
Timur Tabi508a4e72010-04-13 13:16:02 -050047 unsigned long m = 0, n;
Nick Thompsondd7e6c12010-05-11 11:29:52 +010048 unsigned long long f;
Timur Tabi53258da2010-04-13 13:16:03 -050049 static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
Nick Thompsondd7e6c12010-05-11 11:29:52 +010050 unsigned long d = 10 * ARRAY_SIZE(names);
Timur Tabi53258da2010-04-13 13:16:03 -050051 char c = 0;
52 unsigned int i;
wdenk20e0e232002-09-08 16:09:41 +000053
Nick Thompsondd7e6c12010-05-11 11:29:52 +010054 for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
55 if (size >> d) {
Timur Tabi53258da2010-04-13 13:16:03 -050056 c = names[i];
57 break;
Becky Brucee9954d42008-07-09 11:09:41 -050058 }
wdenk20e0e232002-09-08 16:09:41 +000059 }
60
Timur Tabi53258da2010-04-13 13:16:03 -050061 if (!c) {
62 printf("%llu Bytes%s", size, s);
63 return;
64 }
65
Nick Thompsondd7e6c12010-05-11 11:29:52 +010066 n = size >> d;
67 f = size & ((1ULL << d) - 1);
wdenk20e0e232002-09-08 16:09:41 +000068
Becky Brucee9954d42008-07-09 11:09:41 -050069 /* If there's a remainder, deal with it */
Nick Thompsondd7e6c12010-05-11 11:29:52 +010070 if (f) {
71 m = (10ULL * f + (1ULL << (d - 1))) >> d;
wdenk20e0e232002-09-08 16:09:41 +000072
Becky Brucee9954d42008-07-09 11:09:41 -050073 if (m >= 10) {
74 m -= 10;
75 n += 1;
76 }
wdenkf287a242003-07-01 21:06:45 +000077 }
78
Timur Tabi53258da2010-04-13 13:16:03 -050079 printf ("%lu", n);
wdenk20e0e232002-09-08 16:09:41 +000080 if (m) {
81 printf (".%ld", m);
82 }
Timur Tabi53258da2010-04-13 13:16:03 -050083 printf (" %ciB%s", c, s);
wdenk20e0e232002-09-08 16:09:41 +000084}
Grant Likely02a00782007-02-20 09:05:00 +010085
86/*
87 * Print data buffer in hex and ascii form to the terminal.
88 *
89 * data reads are buffered so that each memory address is only read once.
90 * Useful when displaying the contents of volatile registers.
91 *
92 * parameters:
93 * addr: Starting address to display at start of line
94 * data: pointer to data buffer
95 * width: data value width. May be 1, 2, or 4.
96 * count: number of values to display
97 * linelen: Number of values to print per line; specify 0 for default length
98 */
99#define MAX_LINE_LENGTH_BYTES (64)
100#define DEFAULT_LINE_LENGTH_BYTES (16)
Simon Glassd963b722013-02-24 17:33:12 +0000101int print_buffer(ulong addr, const void *data, uint width, uint count,
102 uint linelen)
Grant Likely02a00782007-02-20 09:05:00 +0100103{
Reinhard Meyerd9f13d02010-09-08 12:25:40 +0200104 /* 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 Likely02a00782007-02-20 09:05:00 +0100110 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) {
Andreas Bießmannb4ce02a2013-02-07 04:58:19 +0000118 uint thislinelen = linelen;
Grant Likely02a00782007-02-20 09:05:00 +0100119 printf("%08lx:", addr);
120
121 /* check for overflow condition */
Andreas Bießmannb4ce02a2013-02-07 04:58:19 +0000122 if (count < thislinelen)
123 thislinelen = count;
Grant Likely02a00782007-02-20 09:05:00 +0100124
125 /* Copy from memory into linebuf and print hex values */
Andreas Bießmannb4ce02a2013-02-07 04:58:19 +0000126 for (i = 0; i < thislinelen; i++) {
Mike Frysingera3a26fc2010-07-23 06:17:30 -0400127 uint32_t x;
128 if (width == 4)
Reinhard Meyerd9f13d02010-09-08 12:25:40 +0200129 x = lb.ui[i] = *(volatile uint32_t *)data;
Mike Frysingera3a26fc2010-07-23 06:17:30 -0400130 else if (width == 2)
Reinhard Meyerd9f13d02010-09-08 12:25:40 +0200131 x = lb.us[i] = *(volatile uint16_t *)data;
Mike Frysingera3a26fc2010-07-23 06:17:30 -0400132 else
Reinhard Meyerd9f13d02010-09-08 12:25:40 +0200133 x = lb.uc[i] = *(volatile uint8_t *)data;
Mike Frysingera3a26fc2010-07-23 06:17:30 -0400134 printf(" %0*x", width * 2, x);
Grant Likely02a00782007-02-20 09:05:00 +0100135 data += width;
136 }
137
Andreas Bießmannb4ce02a2013-02-07 04:58:19 +0000138 while (thislinelen < linelen) {
139 /* fill line with whitespace for nice ASCII print */
140 for (i=0; i<width*2+1; i++)
141 puts(" ");
142 linelen--;
143 }
144
Grant Likely02a00782007-02-20 09:05:00 +0100145 /* Print data in ASCII characters */
Andreas Bießmannb4ce02a2013-02-07 04:58:19 +0000146 for (i = 0; i < thislinelen * width; i++) {
Reinhard Meyerd9f13d02010-09-08 12:25:40 +0200147 if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
148 lb.uc[i] = '.';
149 }
150 lb.uc[i] = '\0';
151 printf(" %s\n", lb.uc);
Grant Likely02a00782007-02-20 09:05:00 +0100152
153 /* update references */
Andreas Bießmannb4ce02a2013-02-07 04:58:19 +0000154 addr += thislinelen * width;
155 count -= thislinelen;
Grant Likely02a00782007-02-20 09:05:00 +0100156
157 if (ctrlc())
158 return -1;
159 }
160
161 return 0;
162}