]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/display_options.c
fix print_size printing fractional gigabyte numbers on 32-bit platforms
[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 *
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
c95c4280 24#include <config.h>
20e0e233 25#include <common.h>
c95c4280
GL
26#include <linux/ctype.h>
27#include <asm/io.h>
20e0e233
WD
28
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/*
417faf28
BB
42 * print sizes as "xxx kB", "xxx.y kB", "xxx MB", "xxx.y MB",
43 * xxx GB, or xxx.y GB as needed; allow for optional trailing string
44 * (like "\n")
20e0e233 45 */
61b09fc2 46void print_size (phys_size_t size, const char *s)
20e0e233 47{
52dbac69
TT
48 unsigned long m = 0, n;
49 unsigned long long d = 1 << 30; /* 1 GB */
417faf28 50 char c = 'G';
20e0e233 51
417faf28
BB
52 if (size < d) { /* try MB */
53 c = 'M';
54 d = 1 << 20;
55 if (size < d) { /* print in kB */
56 c = 'k';
57 d = 1 << 10;
58 }
20e0e233
WD
59 }
60
61 n = size / d;
62
417faf28
BB
63 /* If there's a remainder, deal with it */
64 if(size % d) {
65 m = (10 * (size - (n * d)) + (d / 2) ) / d;
20e0e233 66
417faf28
BB
67 if (m >= 10) {
68 m -= 10;
69 n += 1;
70 }
0d498393
WD
71 }
72
20e0e233
WD
73 printf ("%2ld", n);
74 if (m) {
75 printf (".%ld", m);
76 }
77 printf (" %cB%s", c, s);
78}
c95c4280
GL
79
80/*
81 * Print data buffer in hex and ascii form to the terminal.
82 *
83 * data reads are buffered so that each memory address is only read once.
84 * Useful when displaying the contents of volatile registers.
85 *
86 * parameters:
87 * addr: Starting address to display at start of line
88 * data: pointer to data buffer
89 * width: data value width. May be 1, 2, or 4.
90 * count: number of values to display
91 * linelen: Number of values to print per line; specify 0 for default length
92 */
93#define MAX_LINE_LENGTH_BYTES (64)
94#define DEFAULT_LINE_LENGTH_BYTES (16)
95int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen)
96{
97 uint8_t linebuf[MAX_LINE_LENGTH_BYTES];
98 uint32_t *uip = (void*)linebuf;
99 uint16_t *usp = (void*)linebuf;
100 uint8_t *ucp = (void*)linebuf;
101 int i;
102
103 if (linelen*width > MAX_LINE_LENGTH_BYTES)
104 linelen = MAX_LINE_LENGTH_BYTES / width;
105 if (linelen < 1)
106 linelen = DEFAULT_LINE_LENGTH_BYTES / width;
107
108 while (count) {
109 printf("%08lx:", addr);
110
111 /* check for overflow condition */
112 if (count < linelen)
113 linelen = count;
114
115 /* Copy from memory into linebuf and print hex values */
116 for (i = 0; i < linelen; i++) {
117 if (width == 4) {
118 uip[i] = *(volatile uint32_t *)data;
119 printf(" %08x", uip[i]);
120 } else if (width == 2) {
121 usp[i] = *(volatile uint16_t *)data;
122 printf(" %04x", usp[i]);
123 } else {
124 ucp[i] = *(volatile uint8_t *)data;
125 printf(" %02x", ucp[i]);
126 }
127 data += width;
128 }
129
130 /* Print data in ASCII characters */
131 puts(" ");
132 for (i = 0; i < linelen * width; i++)
133 putc(isprint(ucp[i]) && (ucp[i] < 0x80) ? ucp[i] : '.');
134 putc ('\n');
135
136 /* update references */
137 addr += linelen * width;
138 count -= linelen;
139
140 if (ctrlc())
141 return -1;
142 }
143
144 return 0;
145}