blob: 4a972b08a493b7f257dde0a332ad0816cbe576bd [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk20e0e232002-09-08 16:09:41 +00006 */
7
Grant Likelyc95c4282007-02-20 09:05:00 +01008#include <config.h>
wdenk20e0e232002-09-08 16:09:41 +00009#include <common.h>
Andreas Bießmann09c2e902011-07-18 20:24:04 +020010#include <version.h>
Grant Likelyc95c4282007-02-20 09:05:00 +010011#include <linux/ctype.h>
12#include <asm/io.h>
wdenk20e0e232002-09-08 16:09:41 +000013
14int display_options (void)
15{
wdenk20e0e232002-09-08 16:09:41 +000016#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 Tabi4b42c902010-04-13 13:16:03 -050025 * 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 Bruce417faf22008-07-09 11:09:41 -050027 * (like "\n")
wdenk20e0e232002-09-08 16:09:41 +000028 */
Timur Tabi4b42c902010-04-13 13:16:03 -050029void print_size(unsigned long long size, const char *s)
wdenk20e0e232002-09-08 16:09:41 +000030{
Timur Tabi52dbac62010-04-13 13:16:02 -050031 unsigned long m = 0, n;
Nick Thompsonf2d76ae2010-05-11 11:29:52 +010032 unsigned long long f;
Timur Tabi4b42c902010-04-13 13:16:03 -050033 static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
Nick Thompsonf2d76ae2010-05-11 11:29:52 +010034 unsigned long d = 10 * ARRAY_SIZE(names);
Timur Tabi4b42c902010-04-13 13:16:03 -050035 char c = 0;
36 unsigned int i;
wdenk20e0e232002-09-08 16:09:41 +000037
Nick Thompsonf2d76ae2010-05-11 11:29:52 +010038 for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
39 if (size >> d) {
Timur Tabi4b42c902010-04-13 13:16:03 -050040 c = names[i];
41 break;
Becky Bruce417faf22008-07-09 11:09:41 -050042 }
wdenk20e0e232002-09-08 16:09:41 +000043 }
44
Timur Tabi4b42c902010-04-13 13:16:03 -050045 if (!c) {
46 printf("%llu Bytes%s", size, s);
47 return;
48 }
49
Nick Thompsonf2d76ae2010-05-11 11:29:52 +010050 n = size >> d;
51 f = size & ((1ULL << d) - 1);
wdenk20e0e232002-09-08 16:09:41 +000052
Becky Bruce417faf22008-07-09 11:09:41 -050053 /* If there's a remainder, deal with it */
Nick Thompsonf2d76ae2010-05-11 11:29:52 +010054 if (f) {
55 m = (10ULL * f + (1ULL << (d - 1))) >> d;
wdenk20e0e232002-09-08 16:09:41 +000056
Becky Bruce417faf22008-07-09 11:09:41 -050057 if (m >= 10) {
58 m -= 10;
59 n += 1;
60 }
wdenk0d498392003-07-01 21:06:45 +000061 }
62
Timur Tabi4b42c902010-04-13 13:16:03 -050063 printf ("%lu", n);
wdenk20e0e232002-09-08 16:09:41 +000064 if (m) {
65 printf (".%ld", m);
66 }
Timur Tabi4b42c902010-04-13 13:16:03 -050067 printf (" %ciB%s", c, s);
wdenk20e0e232002-09-08 16:09:41 +000068}
Grant Likelyc95c4282007-02-20 09:05:00 +010069
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 Glassbda32ff2013-02-24 17:33:12 +000085int print_buffer(ulong addr, const void *data, uint width, uint count,
86 uint linelen)
Grant Likelyc95c4282007-02-20 09:05:00 +010087{
Reinhard Meyer150f7232010-09-08 12:25:40 +020088 /* linebuf as a union causes proper alignment */
89 union linebuf {
90 uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
91 uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
92 uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
93 } lb;
Grant Likelyc95c4282007-02-20 09:05:00 +010094 int i;
95
96 if (linelen*width > MAX_LINE_LENGTH_BYTES)
97 linelen = MAX_LINE_LENGTH_BYTES / width;
98 if (linelen < 1)
99 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
100
101 while (count) {
Andreas Bießmannefd7c112013-02-07 04:58:19 +0000102 uint thislinelen = linelen;
Grant Likelyc95c4282007-02-20 09:05:00 +0100103 printf("%08lx:", addr);
104
105 /* check for overflow condition */
Andreas Bießmannefd7c112013-02-07 04:58:19 +0000106 if (count < thislinelen)
107 thislinelen = count;
Grant Likelyc95c4282007-02-20 09:05:00 +0100108
109 /* Copy from memory into linebuf and print hex values */
Andreas Bießmannefd7c112013-02-07 04:58:19 +0000110 for (i = 0; i < thislinelen; i++) {
Mike Frysinger64419e42010-07-23 06:17:30 -0400111 uint32_t x;
112 if (width == 4)
Reinhard Meyer150f7232010-09-08 12:25:40 +0200113 x = lb.ui[i] = *(volatile uint32_t *)data;
Mike Frysinger64419e42010-07-23 06:17:30 -0400114 else if (width == 2)
Reinhard Meyer150f7232010-09-08 12:25:40 +0200115 x = lb.us[i] = *(volatile uint16_t *)data;
Mike Frysinger64419e42010-07-23 06:17:30 -0400116 else
Reinhard Meyer150f7232010-09-08 12:25:40 +0200117 x = lb.uc[i] = *(volatile uint8_t *)data;
Mike Frysinger64419e42010-07-23 06:17:30 -0400118 printf(" %0*x", width * 2, x);
Grant Likelyc95c4282007-02-20 09:05:00 +0100119 data += width;
120 }
121
Andreas Bießmannefd7c112013-02-07 04:58:19 +0000122 while (thislinelen < linelen) {
123 /* fill line with whitespace for nice ASCII print */
124 for (i=0; i<width*2+1; i++)
125 puts(" ");
126 linelen--;
127 }
128
Grant Likelyc95c4282007-02-20 09:05:00 +0100129 /* Print data in ASCII characters */
Andreas Bießmannefd7c112013-02-07 04:58:19 +0000130 for (i = 0; i < thislinelen * width; i++) {
Reinhard Meyer150f7232010-09-08 12:25:40 +0200131 if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
132 lb.uc[i] = '.';
133 }
134 lb.uc[i] = '\0';
135 printf(" %s\n", lb.uc);
Grant Likelyc95c4282007-02-20 09:05:00 +0100136
137 /* update references */
Andreas Bießmannefd7c112013-02-07 04:58:19 +0000138 addr += thislinelen * width;
139 count -= thislinelen;
Grant Likelyc95c4282007-02-20 09:05:00 +0100140
141 if (ctrlc())
142 return -1;
143 }
144
145 return 0;
146}