]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/display_options.c
Move display_options functions to their own header
[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 65
c95c4280
GL
66#define MAX_LINE_LENGTH_BYTES (64)
67#define DEFAULT_LINE_LENGTH_BYTES (16)
bda32ffc
SG
68int print_buffer(ulong addr, const void *data, uint width, uint count,
69 uint linelen)
c95c4280 70{
150f7236
RM
71 /* linebuf as a union causes proper alignment */
72 union linebuf {
4d1fd7f1
YS
73#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
74 uint64_t uq[MAX_LINE_LENGTH_BYTES/sizeof(uint64_t) + 1];
75#endif
150f7236
RM
76 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;
c95c4280 80 int i;
4d1fd7f1
YS
81#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
82 uint64_t x;
83#else
84 uint32_t x;
85#endif
c95c4280
GL
86
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) {
efd7c114 93 uint thislinelen = linelen;
c95c4280
GL
94 printf("%08lx:", addr);
95
96 /* check for overflow condition */
efd7c114
AB
97 if (count < thislinelen)
98 thislinelen = count;
c95c4280
GL
99
100 /* Copy from memory into linebuf and print hex values */
efd7c114 101 for (i = 0; i < thislinelen; i++) {
64419e47 102 if (width == 4)
150f7236 103 x = lb.ui[i] = *(volatile uint32_t *)data;
4d1fd7f1
YS
104#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
105 else if (width == 8)
106 x = lb.uq[i] = *(volatile uint64_t *)data;
107#endif
64419e47 108 else if (width == 2)
150f7236 109 x = lb.us[i] = *(volatile uint16_t *)data;
64419e47 110 else
150f7236 111 x = lb.uc[i] = *(volatile uint8_t *)data;
4d1fd7f1 112#ifdef CONFIG_SYS_SUPPORT_64BIT_DATA
c6da9ae8 113 printf(" %0*" PRIx64, width * 2, x);
4d1fd7f1 114#else
64419e47 115 printf(" %0*x", width * 2, x);
4d1fd7f1 116#endif
c95c4280
GL
117 data += width;
118 }
119
efd7c114
AB
120 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
c95c4280 127 /* Print data in ASCII characters */
efd7c114 128 for (i = 0; i < thislinelen * width; i++) {
150f7236
RM
129 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);
c95c4280
GL
134
135 /* update references */
efd7c114
AB
136 addr += thislinelen * width;
137 count -= thislinelen;
c95c4280
GL
138
139 if (ctrlc())
140 return -1;
141 }
142
143 return 0;
144}