Commit c95c4280d751ca078c2ff58228d2f2b44ccf0600

Authored by Grant Likely
Committed by Stefan Roese
1 parent 99b0f0fd3f

[PATCH 3_9] Move buffer print code from md command to common function

Printing a buffer is a darn useful thing.  Move the buffer print code
into print_buffer() in lib_generic/

Signed-off-by: Grant Likely <grant.likely@secretlab.ca>

Showing 3 changed files with 86 additions and 51 deletions Side-by-side Diff

... ... @@ -92,8 +92,9 @@
92 92 int do_mem_md ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
93 93 {
94 94 ulong addr, length;
95   - ulong i, nbytes, linebytes;
96   - u_char *cp;
  95 +#if defined(CONFIG_HAS_DATAFLASH)
  96 + ulong nbytes, linebytes;
  97 +#endif
97 98 int size;
98 99 int rc = 0;
99 100  
... ... @@ -128,6 +129,7 @@
128 129 length = simple_strtoul(argv[2], NULL, 16);
129 130 }
130 131  
  132 +#if defined(CONFIG_HAS_DATAFLASH)
131 133 /* Print the lines.
132 134 *
133 135 * We buffer all read data, so we can make sure data is read only
134 136  
135 137  
136 138  
137 139  
... ... @@ -136,64 +138,25 @@
136 138 nbytes = length * size;
137 139 do {
138 140 char linebuf[DISP_LINE_LEN];
139   - uint *uip = (uint *)linebuf;
140   - ushort *usp = (ushort *)linebuf;
141   - u_char *ucp = (u_char *)linebuf;
142   -#ifdef CONFIG_HAS_DATAFLASH
143   - int rc;
144   -#endif
145   - printf("%08lx:", addr);
  141 + void* p;
146 142 linebytes = (nbytes>DISP_LINE_LEN)?DISP_LINE_LEN:nbytes;
147 143  
148   -#ifdef CONFIG_HAS_DATAFLASH
149   - if ((rc = read_dataflash(addr, (linebytes/size)*size, linebuf)) == DATAFLASH_OK){
150   - /* if outside dataflash */
151   - /*if (rc != 1) {
152   - dataflash_perror (rc);
153   - return (1);
154   - }*/
155   - for (i=0; i<linebytes; i+= size) {
156   - if (size == 4) {
157   - printf(" %08x", *uip++);
158   - } else if (size == 2) {
159   - printf(" %04x", *usp++);
160   - } else {
161   - printf(" %02x", *ucp++);
162   - }
163   - addr += size;
164   - }
  144 + rc = read_dataflash(addr, (linebytes/size)*size, linebuf);
  145 + p = (rc == DATAFLASH_OK) ? linebuf : (void*)addr;
  146 + print_buffer(addr, p, size, linebytes/size, DISP_LINE_LEN/size);
165 147  
166   - } else { /* addr does not correspond to DataFlash */
167   -#endif
168   - for (i=0; i<linebytes; i+= size) {
169   - if (size == 4) {
170   - printf(" %08x", (*uip++ = *((uint *)addr)));
171   - } else if (size == 2) {
172   - printf(" %04x", (*usp++ = *((ushort *)addr)));
173   - } else {
174   - printf(" %02x", (*ucp++ = *((u_char *)addr)));
175   - }
176   - addr += size;
177   - }
178   -#ifdef CONFIG_HAS_DATAFLASH
179   - }
180   -#endif
181   - puts (" ");
182   - cp = (u_char *)linebuf;
183   - for (i=0; i<linebytes; i++) {
184   - if ((*cp < 0x20) || (*cp > 0x7e))
185   - putc ('.');
186   - else
187   - printf("%c", *cp);
188   - cp++;
189   - }
190   - putc ('\n');
191 148 nbytes -= linebytes;
  149 + addr += linebytes;
192 150 if (ctrlc()) {
193 151 rc = 1;
194 152 break;
195 153 }
196 154 } while (nbytes > 0);
  155 +#else
  156 + /* Print the lines. */
  157 + print_buffer(addr, (void*)addr, size, length, DISP_LINE_LEN/size);
  158 + addr += size*length;
  159 +#endif
197 160  
198 161 dp_last_addr = addr;
199 162 dp_last_length = length;
... ... @@ -187,6 +187,8 @@
187 187 long int initdram (int);
188 188 int display_options (void);
189 189 void print_size (ulong, const char *);
  190 +int print_buffer (ulong addr, void* data, uint width, uint count,
  191 + uint linelen);
190 192  
191 193 /* common/main.c */
192 194 void main_loop (void);
lib_generic/display_options.c
... ... @@ -21,7 +21,10 @@
21 21 * MA 02111-1307 USA
22 22 */
23 23  
  24 +#include <config.h>
24 25 #include <common.h>
  26 +#include <linux/ctype.h>
  27 +#include <asm/io.h>
25 28  
26 29 int display_options (void)
27 30 {
... ... @@ -64,5 +67,72 @@
64 67 printf (".%ld", m);
65 68 }
66 69 printf (" %cB%s", c, s);
  70 +}
  71 +
  72 +/*
  73 + * Print data buffer in hex and ascii form to the terminal.
  74 + *
  75 + * data reads are buffered so that each memory address is only read once.
  76 + * Useful when displaying the contents of volatile registers.
  77 + *
  78 + * parameters:
  79 + * addr: Starting address to display at start of line
  80 + * data: pointer to data buffer
  81 + * width: data value width. May be 1, 2, or 4.
  82 + * count: number of values to display
  83 + * linelen: Number of values to print per line; specify 0 for default length
  84 + */
  85 +#define MAX_LINE_LENGTH_BYTES (64)
  86 +#define DEFAULT_LINE_LENGTH_BYTES (16)
  87 +int print_buffer (ulong addr, void* data, uint width, uint count, uint linelen)
  88 +{
  89 + uint8_t linebuf[MAX_LINE_LENGTH_BYTES];
  90 + uint32_t *uip = (void*)linebuf;
  91 + uint16_t *usp = (void*)linebuf;
  92 + uint8_t *ucp = (void*)linebuf;
  93 + int i;
  94 +
  95 + if (linelen*width > MAX_LINE_LENGTH_BYTES)
  96 + linelen = MAX_LINE_LENGTH_BYTES / width;
  97 + if (linelen < 1)
  98 + linelen = DEFAULT_LINE_LENGTH_BYTES / width;
  99 +
  100 + while (count) {
  101 + printf("%08lx:", addr);
  102 +
  103 + /* check for overflow condition */
  104 + if (count < linelen)
  105 + linelen = count;
  106 +
  107 + /* Copy from memory into linebuf and print hex values */
  108 + for (i = 0; i < linelen; i++) {
  109 + if (width == 4) {
  110 + uip[i] = *(volatile uint32_t *)data;
  111 + printf(" %08x", uip[i]);
  112 + } else if (width == 2) {
  113 + usp[i] = *(volatile uint16_t *)data;
  114 + printf(" %04x", usp[i]);
  115 + } else {
  116 + ucp[i] = *(volatile uint8_t *)data;
  117 + printf(" %02x", ucp[i]);
  118 + }
  119 + data += width;
  120 + }
  121 +
  122 + /* Print data in ASCII characters */
  123 + puts(" ");
  124 + for (i = 0; i < linelen * width; i++)
  125 + putc(isprint(ucp[i]) && (ucp[i] < 0x80) ? ucp[i] : '.');
  126 + putc ('\n');
  127 +
  128 + /* update references */
  129 + addr += linelen * width;
  130 + count -= linelen;
  131 +
  132 + if (ctrlc())
  133 + return -1;
  134 + }
  135 +
  136 + return 0;
67 137 }