Debian Codegraph @

contrib/parsers/m920x/m920x_parse.pl

raw
1 #!/usr/bin/perl
2 #
3 # This ULi M920x specific script processes usbsnoop log files (as well as those which have been parsed by mrec's parser.pl utility).
4 # Taken from http://www.linuxtv.org/wiki/index.php/ULi_M920x_parse
5
6 use strict;
7 use warnings;
8 use Getopt::Std;
9
10 my $line;
11 my $mode;
12 my $input;
13 my %opt;
14
15 sub expand_string {
16 my @arr = ();
17 my ($str) = @_;
18
19 if (length($str) == 8) {
20 push(@arr, substr($str, 0, 2));
21 push(@arr, substr($str, 2, 2));
22 push(@arr, substr($str, 4, 2));
23 push(@arr, substr($str, 6, 2));
24 }elsif(length($str) == 4) {
25 push(@arr, substr($str, 0, 2));
26 push(@arr, substr($str, 2, 2));
27 }elsif(length($str) == 2) {
28 push(@arr, $str);
29 }elsif(length($str) == 1) {
30 return;
31 }
32 return @arr;
33 }
34
35 sub expand_string_long {
36 my @bytes = ();
37 my (@str) = @_;
38
39 foreach(@str) {
40 #@arr = expand_string($_);
41 #foreach(@arr){
42 # push(@bytes, $_);
43 #}
44 @bytes = ( @bytes, expand_string($_) );
45 }
46
47 return @bytes;
48 }
49
50 sub print_array_bytes {
51 my (@str) = @_;
52
53 foreach(expand_string_long(@str)){
54 print "$_ ";
55 }
56 }
57
58 sub print_bytes {
59 my ($str) = @_;
60
61 print_array_bytes(split(/ /, $str));
62 }
63
64 sub check {
65 my ($cmd, @bytes) = @_;
66 my @cmp;
67 my $i;
68 #print "cmd <$cmd>\n";
69 my $fail = 0;
70
71 @cmp = split(/ /, $cmd);
72 for ($i = 0; $i < scalar(@cmp); $i++) {
73 #print "check $bytes[$i] vs $cmp[$i]\n";
74 if ($cmp[$i] eq "-1") {
75 next;
76 }
77
78 if (not($bytes[$i] =~ m/$cmp[$i]/)) {
79 $fail = 1;
80 print "($bytes[$i]!=$cmp[$i], $i)";
81 }
82 }
83 if ($fail) {
84 print "\n";
85 print_array_bytes(@bytes);
86 print "\n$cmd\n";
87 }
88 }
89
90 sub get_line {
91 my ($cmd) = @_; # xxx: could be more flexible
92 my @ret;
93 my @cmp;
94 my $i;
95 my $foo;
96
97 again:
98 while($line = <STDIN>) {
99 #001295: OUT: 000002 ms 135775 ms 40 23 c0 00 80 00 00 00 >>>
100 if($input eq "us" && $line =~ m/\S+: \S+: \S+ ms \S+ ms ([a-fA-F0-9 ]+)/) {
101 @ret = split(/ /, $1); $foo = $1;
102 @ret[2,3,4,5,6,7] = @ret[3,2,5,4,7,6];
103 last;
104 }
105
106 #40 23 c0 00 80 00 00 00 >>>
107 if($input eq "us2" && $line =~ m/([a-fA-F0-9 ]+)/) {
108 @ret = split(/ /, $1); $foo = $1;
109 @ret[2,3,4,5,6,7] = @ret[3,2,5,4,7,6];
110 last;
111 }
112
113 if($input eq "um" && $line =~ m/\S+ \S+ \S+ \S+ s ([a-fA-F0-9 ]+)/) {
114 @ret = expand_string_long(split(/ /, $1)); $foo = $1;
115 last;
116 }
117 }
118 @cmp = split(/ /, $cmd);
119 for ($i = 0; $i < scalar(@cmp); $i++) {
120 if ($cmp[$i] eq "-1") {
121 next;
122 }
123
124 if (not($cmp[$i] eq $ret[$i])) {
125 #print "fail\n";
126 goto again;
127 }
128 }
129
130 return @ret;
131 }
132
133 sub us_get_write {
134 #print "<$line>\n";
135 if(($input eq "us" || $input eq "us2") && $line =~ m/>>>\s+([a-fA-F0-9 ]+)/) {
136 return split(/ /, $1);
137 }
138 if($input eq "um") {
139 if($line =~ m/\S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ \S+ = ([a-fA-F0-9 ]+)/) {
140 #print "read match $line\n";
141 return expand_string_long(split(/ /, $1));
142 }
143 }
144 }
145
146 sub get_read {
147 #print "<$line>\n";
148 if(($input eq "us" || $input eq "us2") && $line =~ m/<<<\s+([a-fA-F0-9 ]+)/) {
149 return split(/ /, $1);
150 }
151 if($input eq "um") {
152 while($line = <STDIN>) {
153 if($line =~ m/\S+ \S+ \S+ \S+ \S+ \S+ = ([a-fA-F0-9 ]+)/) {
154 return expand_string_long(split(/ /, $1));
155 }
156
157 }
158 }
159 }
160
161 sub usage {
162 print STDERR << "EOF";
163
164 -i um (usbmon)
165 us (usb snoop)
166 us2 (usb snoop as produced by parse-sniffusb2.pl)
167 sp (snoopy pro)
168
169 -m fw (extract firmware)
170 i2c (show i2c traffic)
171 EOF
172 exit;
173 }
174
175 getopts("m:i:", \%opt ) or usage();
176
177 $mode = $opt{m};
178 $input = $opt{i};
179
180 if ($input ne "um" && $input ne "us" && $input ne "us2" && $input ne "sp") {
181 usage();
182 }
183
184 if ($mode ne "fw" && $mode ne "i2c") {
185 usage();
186 }
187
188 my @bytes;
189
190 if ($mode eq "fw") {
191 open(OUT, ">", "fw") || die "Can't open fw";
192
193 while(@bytes = get_line("-1")) {
194 if(scalar(@bytes) <= 1) {
195 last;
196 }
197
198 my $is_fw_msg = $bytes[0] eq "40" && $bytes[1] eq "30";
199 my $len = hex($bytes[6] . $bytes[7]);
200 if (!$is_fw_msg || $len < 32) {
201 next;
202 }
203
204 my @fw_bytes = us_get_write();
205 if ($len != scalar(@fw_bytes)) {
206 #note: usbmon will not log bulk writes longer than 32 bytes by default
207 print "bulk size doesn't match! Check usbmon.\n";
208 print $len . " != " . scalar(@fw_bytes) . "\n";
209 exit(0);
210 }
211 print OUT pack("v", hex($bytes[2] . $bytes[3]));
212 print OUT pack("v", hex($bytes[4] . $bytes[5]));
213 print OUT pack("v", scalar(@fw_bytes));
214
215 foreach(@fw_bytes) {
216 print OUT pack("C", hex($_));
217 }
218 }
219 exit(1);
220 }
221
222 while(@bytes = get_line("-1")) {
223 if(scalar(@bytes) <= 1) {
224 last;
225 }
226
227 my $master_line = $. - 1;
228
229 if ($bytes[0] eq "40" && $bytes[1] eq "23") {
230
231 if ($bytes[4] eq "80" || $bytes[4] eq "00") {
232 my $multibyte = 0;
233 my $addr;
234
235 $addr = $bytes[2];
236
237 printf "%06d: ", $master_line;
238 print "addr $addr ";
239
240 if (hex($addr) & 0x1) {
241 print "Invalid address\n";
242 }
243
244 @bytes = get_line("40 23");
245
246 my $reg = $bytes[2];
247 if ($bytes[4] eq "80") {
248 $multibyte = 1;
249 } else {
250 @bytes = get_line("40 23");
251 }
252 #if ($bytes[4] ne "40") {
253 # print "(missing 40)";
254 #}
255
256 if ($bytes[4] eq "80") {
257 if ($multibyte == 0) {
258 my $raddr = sprintf("%02x", hex($addr) | 0x1);
259
260 check("40 23 $raddr 00 80 00 00 00", @bytes);
261
262 @bytes = get_line("c0 23");
263 print "reg $reg = ";
264 } else {
265 print "$reg = ";
266 @bytes = get_line("c0 23");
267 while ($bytes[4] eq "21") {
268 check("c0 23 00 00 21 00 -1 -1", @bytes);
269
270 @bytes = get_read();
271 print_array_bytes(@bytes);
272
273 @bytes = get_line("c0 23");
274 }
275 }
276
277 check("c0 23 -1 00 60 00 -1 -1", @bytes);
278
279 @bytes = get_read();
280 print_array_bytes(@bytes);
281 print "read\n";
282
283 } else {
284 check("40 23 -1 00 4|00 00 00 00", @bytes);
285 print "reg $reg = $bytes[2]";
286
287 while ($bytes[4] ne "40") {
288 @bytes = get_line("40 23");
289 check("40 23 -1 00 4|00 00 00 00", @bytes);
290 print " $bytes[2]";
291 }
292 print "\n";
293 }
294 }
295 }
296 }