]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/display_options.c
board/matrix_vision/mvblx/sys_eeprom.c: fix buffer overflow
[people/ms/u-boot.git] / lib / display_options.c
CommitLineData
20e0e233
WD
1/*
2 * (C) Copyright 2000-2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
20e0e233
WD
6 */
7
c95c4280 8#include <config.h>
20e0e233 9#include <common.h>
c6da9ae8 10#include <inttypes.h>
09c2e90c 11#include <version.h>
c95c4280
GL
12#include <linux/ctype.h>
13#include <asm/io.h>
20e0e233
WD
14
15int display_options (void)
16{
20e0e233
WD
17#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
c6da9ae8 25void print_size(uint64_t size, const char *s)
20e0e233 26{
52dbac69 27 unsigned long m = 0, n;
c6da9ae8 28 uint64_t f;
4b42c905 29 static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
f2d76ae4 30 unsigned long d = 10 * ARRAY_SIZE(names);
4b42c905
TT
31 char c = 0;
32 unsigned int i;
20e0e233 33
f2d76ae4
NT
34 for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
35 if (size >> d) {
4b42c905
TT
36 c = names[i];
37 break;
417faf28 38 }
20e0e233
WD
39 }
40
4b42c905 41 if (!c) {
c6da9ae8 42 printf("%" PRIu64 " Bytes%s", size, s);
4b42c905
TT
43 return;
44 }
45
f2d76ae4
NT
46 n = size >> d;
47 f = size & ((1ULL << d) - 1);
20e0e233 48
417faf28 49 /* If there's a remainder, deal with it */
f2d76ae4
NT
50 if (f) {
51 m = (10ULL * f + (1ULL << (d - 1))) >> d;
20e0e233 52
417faf28
BB
53 if (m >= 10) {
54 m -= 10;
55 n += 1;
56 }
0d498393
WD
57 }
58
4b42c905 59 printf ("%lu", n);
20e0e233
WD
60 if (m) {
61 printf (".%ld", m);
62 }
4b42c905 63 printf (" %ciB%s", c, s);
20e0e233 64}
c95c4280
GL
65
66/*
67 * Print data buffer in hex and ascii form to the terminal.
68 *
69 * data reads are buffered so that each memory address is only read once.
70 * Useful when displaying the contents of volatile registers.
71 *
72 * parameters:
73 * addr: Starting address to display at start of line
74 * data: pointer to data buffer
75 * width: data value width. May be 1, 2, or 4.
76 * count: number of values to display
77 * linelen: Number of values to print per line; specify 0 for default length
78 */
79#define MAX_LINE_LENGTH_BYTES (64)
80#define DEFAULT_LINE_LENGTH_BYTES (16)
bda32ffc
SG
81int print_buffer(ulong addr, const void *data, uint width, uint count,
82 uint linelen)
c95c4280 83{
150f7236
RM
84 /* linebuf as a union causes proper alignment */
85 union linebuf {
4d1fd7f1
YS
86#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
87 uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1];
88#endif
150f7236
RM
89 uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
90 uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
91 uint8_t uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
92 } lb;
c95c4280 93 int i;
4d1fd7f1
YS
94#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
95 uint64_t x;
96#else
97 uint32_t x;
98#endif
c95c4280
GL
99
100 if (linelen*width > MAX_LINE_LENGTH_BYTES)
101 linelen = MAX_LINE_LENGTH_BYTES / width;
102 if (linelen < 1)
103 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
104
105 while (count) {
efd7c114 106 uint thislinelen = linelen;
c95c4280
GL
107 printf("%08lx:", addr);
108
109 /* check for overflow condition */
efd7c114
AB
110 if (count < thislinelen)
111 thislinelen = count;
c95c4280
GL
112
113 /* Copy from memory into linebuf and print hex values */
efd7c114 114 for (i = 0; i < thislinelen; i++) {
64419e47 115 if (width == 4)
150f7236 116 x = lb.ui[i] = *(volatile uint32_t *)data;
4d1fd7f1
YS
117#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
118 else if (width == 8)
119 x = lb.uq[i] = *(volatile uint64_t *)data;
120#endif
64419e47 121 else if (width == 2)
150f7236 122 x = lb.us[i] = *(volatile uint16_t *)data;
64419e47 123 else
150f7236 124 x = lb.uc[i] = *(volatile uint8_t *)data;
4d1fd7f1 125#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
c6da9ae8 126 printf(" %0*" PRIx64, width * 2, x);
4d1fd7f1 127#else
64419e47 128 printf(" %0*x", width * 2, x);
4d1fd7f1 129#endif
c95c4280
GL
130 data += width;
131 }
132
efd7c114
AB
133 while (thislinelen < linelen) {
134 /* fill line with whitespace for nice ASCII print */
135 for (i=0; i<width*2+1; i++)
136 puts(" ");
137 linelen--;
138 }
139
c95c4280 140 /* Print data in ASCII characters */
efd7c114 141 for (i = 0; i < thislinelen * width; i++) {
150f7236
RM
142 if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
143 lb.uc[i] = '.';
144 }
145 lb.uc[i] = '\0';
146 printf(" %s\n", lb.uc);
c95c4280
GL
147
148 /* update references */
efd7c114
AB
149 addr += thislinelen * width;
150 count -= thislinelen;
c95c4280
GL
151
152 if (ctrlc())
153 return -1;
154 }
155
156 return 0;
157}