blob: 3f32bcd8cf2dff807f64e8f0185f6f1fd8116e18 [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>
Simon Glassc6da9ae2014-10-15 04:38:35 -060010#include <inttypes.h>
Andreas Bießmann09c2e902011-07-18 20:24:04 +020011#include <version.h>
Grant Likelyc95c4282007-02-20 09:05:00 +010012#include <linux/ctype.h>
13#include <asm/io.h>
wdenk20e0e232002-09-08 16:09:41 +000014
15int display_options (void)
16{
wdenk20e0e232002-09-08 16:09:41 +000017#if defined(BUILD_TAG)
18 printf ("\n\n%s, Build: %s\n\n", version_string, BUILD_TAG);
19#else
20 printf ("\n\n%s\n\n", version_string);
21#endif
22 return 0;
23}
24
Simon Glassc6da9ae2014-10-15 04:38:35 -060025void print_size(uint64_t size, const char *s)
wdenk20e0e232002-09-08 16:09:41 +000026{
Timur Tabi52dbac62010-04-13 13:16:02 -050027 unsigned long m = 0, n;
Simon Glassc6da9ae2014-10-15 04:38:35 -060028 uint64_t f;
Timur Tabi4b42c902010-04-13 13:16:03 -050029 static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
Nick Thompsonf2d76ae2010-05-11 11:29:52 +010030 unsigned long d = 10 * ARRAY_SIZE(names);
Timur Tabi4b42c902010-04-13 13:16:03 -050031 char c = 0;
32 unsigned int i;
wdenk20e0e232002-09-08 16:09:41 +000033
Nick Thompsonf2d76ae2010-05-11 11:29:52 +010034 for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
35 if (size >> d) {
Timur Tabi4b42c902010-04-13 13:16:03 -050036 c = names[i];
37 break;
Becky Bruce417faf22008-07-09 11:09:41 -050038 }
wdenk20e0e232002-09-08 16:09:41 +000039 }
40
Timur Tabi4b42c902010-04-13 13:16:03 -050041 if (!c) {
Simon Glassc6da9ae2014-10-15 04:38:35 -060042 printf("%" PRIu64 " Bytes%s", size, s);
Timur Tabi4b42c902010-04-13 13:16:03 -050043 return;
44 }
45
Nick Thompsonf2d76ae2010-05-11 11:29:52 +010046 n = size >> d;
47 f = size & ((1ULL << d) - 1);
wdenk20e0e232002-09-08 16:09:41 +000048
Becky Bruce417faf22008-07-09 11:09:41 -050049 /* If there's a remainder, deal with it */
Nick Thompsonf2d76ae2010-05-11 11:29:52 +010050 if (f) {
51 m = (10ULL * f + (1ULL << (d - 1))) >> d;
wdenk20e0e232002-09-08 16:09:41 +000052
Becky Bruce417faf22008-07-09 11:09:41 -050053 if (m >= 10) {
54 m -= 10;
55 n += 1;
56 }
wdenk0d498392003-07-01 21:06:45 +000057 }
58
Timur Tabi4b42c902010-04-13 13:16:03 -050059 printf ("%lu", n);
wdenk20e0e232002-09-08 16:09:41 +000060 if (m) {
61 printf (".%ld", m);
62 }
Timur Tabi4b42c902010-04-13 13:16:03 -050063 printf (" %ciB%s", c, s);
wdenk20e0e232002-09-08 16:09:41 +000064}
Grant Likelyc95c4282007-02-20 09:05:00 +010065
Grant Likelyc95c4282007-02-20 09:05:00 +010066#define MAX_LINE_LENGTH_BYTES (64)
67#define DEFAULT_LINE_LENGTH_BYTES (16)
Simon Glassbda32ff2013-02-24 17:33:12 +000068int print_buffer(ulong addr, const void *data, uint width, uint count,
69 uint linelen)
Grant Likelyc95c4282007-02-20 09:05:00 +010070{
Reinhard Meyer150f7232010-09-08 12:25:40 +020071 /* linebuf as a union causes proper alignment */
72 union linebuf {
York Sun4d1fd7f2014-02-26 17:03:19 -080073#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
74 uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1];
75#endif
Reinhard Meyer150f7232010-09-08 12:25:40 +020076 uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
77 uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
78 uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
79 } lb;
Grant Likelyc95c4282007-02-20 09:05:00 +010080 int i;
York Sun4d1fd7f2014-02-26 17:03:19 -080081#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
82 uint64_t x;
83#else
84 uint32_t x;
85#endif
Grant Likelyc95c4282007-02-20 09:05:00 +010086
87 if (linelen*width > MAX_LINE_LENGTH_BYTES)
88 linelen = MAX_LINE_LENGTH_BYTES / width;
89 if (linelen < 1)
90 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
91
92 while (count) {
Andreas Bießmannefd7c112013-02-07 04:58:19 +000093 uint thislinelen = linelen;
Grant Likelyc95c4282007-02-20 09:05:00 +010094 printf("%08lx:", addr);
95
96 /* check for overflow condition */
Andreas Bießmannefd7c112013-02-07 04:58:19 +000097 if (count < thislinelen)
98 thislinelen = count;
Grant Likelyc95c4282007-02-20 09:05:00 +010099
100 /* Copy from memory into linebuf and print hex values */
Andreas Bießmannefd7c112013-02-07 04:58:19 +0000101 for (i = 0; i < thislinelen; i++) {
Mike Frysinger64419e42010-07-23 06:17:30 -0400102 if (width == 4)
Reinhard Meyer150f7232010-09-08 12:25:40 +0200103 x = lb.ui[i] = *(volatile uint32_t *)data;
York Sun4d1fd7f2014-02-26 17:03:19 -0800104#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
105 else if (width == 8)
106 x = lb.uq[i] = *(volatile uint64_t *)data;
107#endif
Mike Frysinger64419e42010-07-23 06:17:30 -0400108 else if (width == 2)
Reinhard Meyer150f7232010-09-08 12:25:40 +0200109 x = lb.us[i] = *(volatile uint16_t *)data;
Mike Frysinger64419e42010-07-23 06:17:30 -0400110 else
Reinhard Meyer150f7232010-09-08 12:25:40 +0200111 x = lb.uc[i] = *(volatile uint8_t *)data;
York Sun4d1fd7f2014-02-26 17:03:19 -0800112#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
Simon Glassc6da9ae2014-10-15 04:38:35 -0600113 printf(" %0*" PRIx64, width * 2, x);
York Sun4d1fd7f2014-02-26 17:03:19 -0800114#else
Mike Frysinger64419e42010-07-23 06:17:30 -0400115 printf(" %0*x", width * 2, x);
York Sun4d1fd7f2014-02-26 17:03:19 -0800116#endif
Grant Likelyc95c4282007-02-20 09:05:00 +0100117 data += width;
118 }
119
Andreas Bießmannefd7c112013-02-07 04:58:19 +0000120 while (thislinelen < linelen) {
121 /* fill line with whitespace for nice ASCII print */
122 for (i=0; i<width*2+1; i++)
123 puts(" ");
124 linelen--;
125 }
126
Grant Likelyc95c4282007-02-20 09:05:00 +0100127 /* Print data in ASCII characters */
Andreas Bießmannefd7c112013-02-07 04:58:19 +0000128 for (i = 0; i < thislinelen * width; i++) {
Reinhard Meyer150f7232010-09-08 12:25:40 +0200129 if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
130 lb.uc[i] = '.';
131 }
132 lb.uc[i] = '\0';
133 printf(" %s\n", lb.uc);
Grant Likelyc95c4282007-02-20 09:05:00 +0100134
135 /* update references */
Andreas Bießmannefd7c112013-02-07 04:58:19 +0000136 addr += thislinelen * width;
137 count -= thislinelen;
Grant Likelyc95c4282007-02-20 09:05:00 +0100138
139 if (ctrlc())
140 return -1;
141 }
142
143 return 0;
144}