Blame view

lib/display_options.c 3.26 KB
20e0e2331   wdenk   Initial revision
1
2
3
4
  /*
   * (C) Copyright 2000-2002
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
1a4596601   Wolfgang Denk   Add GPL-2.0+ SPDX...
5
   * SPDX-License-Identifier:	GPL-2.0+
20e0e2331   wdenk   Initial revision
6
   */
c95c4280d   Grant Likely   [PATCH 3_9] Move ...
7
  #include <config.h>
20e0e2331   wdenk   Initial revision
8
  #include <common.h>
09c2e90c1   Andreas Bießmann   unify version_string
9
  #include <version.h>
c95c4280d   Grant Likely   [PATCH 3_9] Move ...
10
11
  #include <linux/ctype.h>
  #include <asm/io.h>
20e0e2331   wdenk   Initial revision
12
13
14
  
  int display_options (void)
  {
20e0e2331   wdenk   Initial revision
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #if defined(BUILD_TAG)
  	printf ("
  
  %s, Build: %s
  
  ", version_string, BUILD_TAG);
  #else
  	printf ("
  
  %s
  
  ", version_string);
  #endif
  	return 0;
  }
  
  /*
4b42c9059   Timur Tabi   allow print_size ...
32
33
   * print sizes as "xxx KiB", "xxx.y KiB", "xxx MiB", "xxx.y MiB",
   * xxx GiB, xxx.y GiB, etc as needed; allow for optional trailing string
417faf285   Becky Bruce   Allow print_size ...
34
35
   * (like "
  ")
20e0e2331   wdenk   Initial revision
36
   */
4b42c9059   Timur Tabi   allow print_size ...
37
  void print_size(unsigned long long size, const char *s)
20e0e2331   wdenk   Initial revision
38
  {
52dbac69c   Timur Tabi   fix print_size pr...
39
  	unsigned long m = 0, n;
f2d76ae4f   Nick Thompson   Avoid use of divi...
40
  	unsigned long long f;
4b42c9059   Timur Tabi   allow print_size ...
41
  	static const char names[] = {'E', 'P', 'T', 'G', 'M', 'K'};
f2d76ae4f   Nick Thompson   Avoid use of divi...
42
  	unsigned long d = 10 * ARRAY_SIZE(names);
4b42c9059   Timur Tabi   allow print_size ...
43
44
  	char c = 0;
  	unsigned int i;
20e0e2331   wdenk   Initial revision
45

f2d76ae4f   Nick Thompson   Avoid use of divi...
46
47
  	for (i = 0; i < ARRAY_SIZE(names); i++, d -= 10) {
  		if (size >> d) {
4b42c9059   Timur Tabi   allow print_size ...
48
49
  			c = names[i];
  			break;
417faf285   Becky Bruce   Allow print_size ...
50
  		}
20e0e2331   wdenk   Initial revision
51
  	}
4b42c9059   Timur Tabi   allow print_size ...
52
53
54
55
  	if (!c) {
  		printf("%llu Bytes%s", size, s);
  		return;
  	}
f2d76ae4f   Nick Thompson   Avoid use of divi...
56
57
  	n = size >> d;
  	f = size & ((1ULL << d) - 1);
20e0e2331   wdenk   Initial revision
58

417faf285   Becky Bruce   Allow print_size ...
59
  	/* If there's a remainder, deal with it */
f2d76ae4f   Nick Thompson   Avoid use of divi...
60
61
  	if (f) {
  		m = (10ULL * f + (1ULL << (d - 1))) >> d;
20e0e2331   wdenk   Initial revision
62

417faf285   Becky Bruce   Allow print_size ...
63
64
65
66
  		if (m >= 10) {
  			m -= 10;
  			n += 1;
  		}
0d4983930   wdenk   Patch by Kenneth ...
67
  	}
4b42c9059   Timur Tabi   allow print_size ...
68
  	printf ("%lu", n);
20e0e2331   wdenk   Initial revision
69
70
71
  	if (m) {
  		printf (".%ld", m);
  	}
4b42c9059   Timur Tabi   allow print_size ...
72
  	printf (" %ciB%s", c, s);
20e0e2331   wdenk   Initial revision
73
  }
c95c4280d   Grant Likely   [PATCH 3_9] Move ...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  
  /*
   * Print data buffer in hex and ascii form to the terminal.
   *
   * data reads are buffered so that each memory address is only read once.
   * Useful when displaying the contents of volatile registers.
   *
   * parameters:
   *    addr: Starting address to display at start of line
   *    data: pointer to data buffer
   *    width: data value width.  May be 1, 2, or 4.
   *    count: number of values to display
   *    linelen: Number of values to print per line; specify 0 for default length
   */
  #define MAX_LINE_LENGTH_BYTES (64)
  #define DEFAULT_LINE_LENGTH_BYTES (16)
bda32ffcf   Simon Glass   Update print_buff...
90
91
  int print_buffer(ulong addr, const void *data, uint width, uint count,
  		 uint linelen)
c95c4280d   Grant Likely   [PATCH 3_9] Move ...
92
  {
150f72366   Reinhard Meyer   display_buffer: f...
93
94
95
96
97
98
  	/* linebuf as a union causes proper alignment */
  	union linebuf {
  		uint32_t ui[MAX_LINE_LENGTH_BYTES/sizeof(uint32_t) + 1];
  		uint16_t us[MAX_LINE_LENGTH_BYTES/sizeof(uint16_t) + 1];
  		uint8_t  uc[MAX_LINE_LENGTH_BYTES/sizeof(uint8_t) + 1];
  	} lb;
c95c4280d   Grant Likely   [PATCH 3_9] Move ...
99
100
101
102
103
104
105
106
  	int i;
  
  	if (linelen*width > MAX_LINE_LENGTH_BYTES)
  		linelen = MAX_LINE_LENGTH_BYTES / width;
  	if (linelen < 1)
  		linelen = DEFAULT_LINE_LENGTH_BYTES / width;
  
  	while (count) {
efd7c1140   Andreas Bießmann   display_options:p...
107
  		uint thislinelen = linelen;
c95c4280d   Grant Likely   [PATCH 3_9] Move ...
108
109
110
  		printf("%08lx:", addr);
  
  		/* check for overflow condition */
efd7c1140   Andreas Bießmann   display_options:p...
111
112
  		if (count < thislinelen)
  			thislinelen = count;
c95c4280d   Grant Likely   [PATCH 3_9] Move ...
113
114
  
  		/* Copy from memory into linebuf and print hex values */
efd7c1140   Andreas Bießmann   display_options:p...
115
  		for (i = 0; i < thislinelen; i++) {
64419e475   Mike Frysinger   print_buffer: opt...
116
117
  			uint32_t x;
  			if (width == 4)
150f72366   Reinhard Meyer   display_buffer: f...
118
  				x = lb.ui[i] = *(volatile uint32_t *)data;
64419e475   Mike Frysinger   print_buffer: opt...
119
  			else if (width == 2)
150f72366   Reinhard Meyer   display_buffer: f...
120
  				x = lb.us[i] = *(volatile uint16_t *)data;
64419e475   Mike Frysinger   print_buffer: opt...
121
  			else
150f72366   Reinhard Meyer   display_buffer: f...
122
  				x = lb.uc[i] = *(volatile uint8_t *)data;
64419e475   Mike Frysinger   print_buffer: opt...
123
  			printf(" %0*x", width * 2, x);
c95c4280d   Grant Likely   [PATCH 3_9] Move ...
124
125
  			data += width;
  		}
efd7c1140   Andreas Bießmann   display_options:p...
126
127
128
129
130
131
  		while (thislinelen < linelen) {
  			/* fill line with whitespace for nice ASCII print */
  			for (i=0; i<width*2+1; i++)
  				puts(" ");
  			linelen--;
  		}
c95c4280d   Grant Likely   [PATCH 3_9] Move ...
132
  		/* Print data in ASCII characters */
efd7c1140   Andreas Bießmann   display_options:p...
133
  		for (i = 0; i < thislinelen * width; i++) {
150f72366   Reinhard Meyer   display_buffer: f...
134
135
136
137
138
139
  			if (!isprint(lb.uc[i]) || lb.uc[i] >= 0x80)
  				lb.uc[i] = '.';
  		}
  		lb.uc[i] = '\0';
  		printf("    %s
  ", lb.uc);
c95c4280d   Grant Likely   [PATCH 3_9] Move ...
140
141
  
  		/* update references */
efd7c1140   Andreas Bießmann   display_options:p...
142
143
  		addr += thislinelen * width;
  		count -= thislinelen;
c95c4280d   Grant Likely   [PATCH 3_9] Move ...
144
145
146
147
148
149
150
  
  		if (ctrlc())
  			return -1;
  	}
  
  	return 0;
  }