1 | /* |
---|
2 | * MDUMP Shell Command Implmentation |
---|
3 | * |
---|
4 | * Author: Fernando RUIZ CASAS |
---|
5 | * Work: fernando.ruiz@ctv.es |
---|
6 | * Home: correo@fernando-ruiz.com |
---|
7 | * |
---|
8 | * Reworked by Ric Claus at SLAC.Stanford.edu |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.com/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <ctype.h> |
---|
20 | #include <stdio.h> |
---|
21 | #include <inttypes.h> |
---|
22 | #include <string.h> |
---|
23 | |
---|
24 | #include <rtems.h> |
---|
25 | #include <rtems/shell.h> |
---|
26 | #include <rtems/stringto.h> |
---|
27 | #include "internal.h" |
---|
28 | |
---|
29 | static int args_parse(int argc, char* argv[], void** addr, int* max, int* sz); |
---|
30 | |
---|
31 | static void mdumpB(void* addr, int m); |
---|
32 | static void mdumpW(void* addr, int m); |
---|
33 | static void mdumpL(void* addr, int m); |
---|
34 | |
---|
35 | int rtems_mdump(void* addr, int max, int sz); |
---|
36 | |
---|
37 | int rtems_shell_main_mdump( |
---|
38 | int argc, |
---|
39 | char *argv[] |
---|
40 | ) |
---|
41 | { |
---|
42 | void *addr; |
---|
43 | int max; |
---|
44 | int sz; |
---|
45 | |
---|
46 | if (args_parse(argc, argv, &addr, &max, &sz)) |
---|
47 | return -1; |
---|
48 | return rtems_mdump(addr, max, sz); |
---|
49 | } |
---|
50 | |
---|
51 | int rtems_shell_main_wdump( |
---|
52 | int argc, |
---|
53 | char *argv[] |
---|
54 | ) |
---|
55 | { |
---|
56 | void *addr; |
---|
57 | int max; |
---|
58 | int sz; |
---|
59 | |
---|
60 | if (args_parse(argc, argv, &addr, &max, &sz)) |
---|
61 | return -1; |
---|
62 | return rtems_mdump(addr, max, 2); |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | int rtems_shell_main_ldump( |
---|
67 | int argc, |
---|
68 | char *argv[] |
---|
69 | ) |
---|
70 | { |
---|
71 | void *addr; |
---|
72 | int max; |
---|
73 | int sz; |
---|
74 | |
---|
75 | if (args_parse(argc, argv, &addr, &max, &sz)) |
---|
76 | return -1; |
---|
77 | return rtems_mdump(addr, max, 4); |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | int args_parse(int argc, |
---|
82 | char* argv[], |
---|
83 | void** addr, |
---|
84 | int* max, |
---|
85 | int* sz) |
---|
86 | { |
---|
87 | *addr = NULL; |
---|
88 | *max = 320; |
---|
89 | *sz = 1; |
---|
90 | |
---|
91 | if (argc > 1) { |
---|
92 | if ( rtems_string_to_pointer(argv[1], addr, NULL) ) { |
---|
93 | printf( "Address argument (%s) is not a number\n", argv[1] ); |
---|
94 | return -1; |
---|
95 | } |
---|
96 | |
---|
97 | if (argc > 2) { |
---|
98 | if ( rtems_string_to_int(argv[2], max, NULL, 0) ) { |
---|
99 | printf( "Length argument (%s) is not a number\n", argv[2] ); |
---|
100 | return -1; |
---|
101 | } |
---|
102 | |
---|
103 | if (argc > 3) { |
---|
104 | if ( rtems_string_to_int(argv[3], sz, NULL, 0) ) { |
---|
105 | printf( "Size argument (%s) is not a valid number\n", argv[3] ); |
---|
106 | return -1; |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | return 0; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | int rtems_mdump(void* addr, int max, int sz) |
---|
116 | { |
---|
117 | unsigned char n; |
---|
118 | unsigned char m; |
---|
119 | volatile unsigned char *pb; |
---|
120 | int res; |
---|
121 | int cnt; |
---|
122 | |
---|
123 | if (!((sz == 1) || (sz == 2) || (sz == 4))) { |
---|
124 | printf( "Size argument (%d) is not one of 1 (bytes), " |
---|
125 | " 2 (words) or 4 (longwords)\n", sz); |
---|
126 | return -1; |
---|
127 | } |
---|
128 | |
---|
129 | if (max <= 0) { |
---|
130 | max = 1; /* print 1 item if 0 or neg. */ |
---|
131 | res = 0; |
---|
132 | } else { |
---|
133 | max--; |
---|
134 | res = max & 0xf; /* num bytes in last row */ |
---|
135 | max >>= 4; /* div by 16 */ |
---|
136 | max++; /* num of rows to print */ |
---|
137 | if (max > 64) { /* limit to 64 lines */ |
---|
138 | max = 64; |
---|
139 | res = 0xf; /* 16 bytes print in last row */ |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | pb = addr; |
---|
144 | for (m=0; m<max; m++) { |
---|
145 | cnt = m==(max-1)?res:0xf; |
---|
146 | printf("%10p ", pb); |
---|
147 | if (sz == 1) mdumpB(pb, cnt); |
---|
148 | else if (sz == 2) mdumpW(pb, cnt); |
---|
149 | else if (sz == 4) mdumpL(pb, cnt); |
---|
150 | for (n=0;n<=cnt;n++) { |
---|
151 | printf("%c", isprint(pb[n]) ? pb[n] : '.'); |
---|
152 | } |
---|
153 | printf("\n"); |
---|
154 | pb += 16; |
---|
155 | } |
---|
156 | |
---|
157 | return 0; |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | void mdumpB(void* addr, int m) |
---|
162 | { |
---|
163 | volatile unsigned char* pb = addr; |
---|
164 | int n; |
---|
165 | for (n=0;n<=m;n++) |
---|
166 | printf("%02X%c",*pb++,n==7?'-':' '); |
---|
167 | for (;n<=0xf;n++) |
---|
168 | printf(" %c",n==7?'-':' '); |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | void mdumpW(void* addr, int m) |
---|
173 | { |
---|
174 | volatile unsigned short* pb = addr; |
---|
175 | int n; |
---|
176 | for (n=0;n<=m;n+=2) |
---|
177 | printf("%04X%c",*pb++,n==6?'-':' '); |
---|
178 | for (;n<=0xf;n+=2) |
---|
179 | printf(" %c", n==6?'-':' '); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | void mdumpL(void* addr, int m) |
---|
184 | { |
---|
185 | volatile unsigned int* pb = addr; |
---|
186 | int n; |
---|
187 | for (n=0;n<=m;n+=4) |
---|
188 | printf("%08X%c",*pb++,n==4?'-':' '); |
---|
189 | for (;n<=0xf;n+=4) |
---|
190 | printf(" %c", n==4?'-':' '); |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | rtems_shell_cmd_t rtems_shell_MDUMP_Command = { |
---|
195 | "mdump", /* name */ |
---|
196 | "mdump [address [length [size]]]", /* usage */ |
---|
197 | "mem", /* topic */ |
---|
198 | rtems_shell_main_mdump, /* command */ |
---|
199 | NULL, /* alias */ |
---|
200 | NULL /* next */ |
---|
201 | }; |
---|
202 | |
---|
203 | |
---|
204 | rtems_shell_cmd_t rtems_shell_WDUMP_Command = { |
---|
205 | "wdump", /* name */ |
---|
206 | "wdump [address [length]]", /* usage */ |
---|
207 | "mem", /* topic */ |
---|
208 | rtems_shell_main_wdump, /* command */ |
---|
209 | NULL, /* alias */ |
---|
210 | NULL /* next */ |
---|
211 | }; |
---|
212 | |
---|
213 | |
---|
214 | rtems_shell_cmd_t rtems_shell_LDUMP_Command = { |
---|
215 | "ldump", /* name */ |
---|
216 | "ldump [address [length]]", /* usage */ |
---|
217 | "mem", /* topic */ |
---|
218 | rtems_shell_main_ldump, /* command */ |
---|
219 | NULL, /* alias */ |
---|
220 | NULL /* next */ |
---|
221 | }; |
---|
222 | |
---|