1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
#!/usr/bin/ruby1.9.1
$acronym_regex = "."
#$acronym_regex = "[\w\d_]"
class Reg
private
def valid_bitnumber(current, new)
return (new == 31 or new == 15 or new == 7) if (not current) or
current < 0
return current == new
end
def warning(current, start, line)
expected = current
expected ||= "7, 15 or 31"
$stderr.puts "WARNING: Expected bit #{expected} but got bit #{start}: #{line}"
end
public
def initialize_parse lines
current = start = stop = nil
total_width = 0
acronym_begin = nil
for line in lines
re = %r<(?x)
^
\s*
(?:
(?'start'\d{1,2})
(?:\s+|\s*:\s*)
(?'stop'\d{1,2})
|
(?'something'\d{1,4})
)?
\s+
(?'acronym'#{$acronym_regex}+?)
\s*
$
>
m = re.match line
if not m
$stderr.puts "WARNING: Invalid line: #{line}"
next
end
if acronym_begin
acronym = acronym_begin + m[:acronym]
acronym_begin = nil
else
acronym = m[:acronym]
end
if m[:start]
start = m[:start].to_i
if not valid_bitnumber(current, start)
warning current, start, line
end
stop = m["stop"].to_i
elsif m[:something]
start = m[:something].to_i
if not valid_bitnumber(current, start)
start, stop = m[:something].scan(/^(\d{1,2})(\d{1,2})$/).first
start, stop = start.to_i, stop.to_i
start, stop = stop, start if start < stop
if not valid_bitnumber(current, start)
warning current, start, line
end
else
stop = start
end
else
acronym_begin = m[:acronym]
next
end
current = stop - 1
width = start - stop + 1
total_width += width
name = (acronym =~ /reserved|rsvd/i ? "" : acronym)
@fields << Field.new(start, stop, name)
end
if total_width != 8 and total_width != 16 and total_width != 32
$stderr.puts "WARNING: Total register width is #{total_width} bits"
end
end
def initialize_from_map m
@fields += m["fields"].map { |f|
start, stop = f["range"]
stop = start unless stop
Field.new(start, stop, f["acronym"]) }
end
class Field
attr_accessor :start, :stop, :name
def initialize start, stop, name=""
@start, @stop, @name = start, stop, name
end
end
def self.parse lines
self.new.tap {|s| s.initialize_parse lines }
end
def self.from_map m, name
self.new(name).tap {|s| s.initialize_from_map m }
end
attr_reader :name
def initialize name=nil
@name = name
@fields = []
end
def print_bitfield
for f in @fields
width = f.start - f.stop + 1
tabs = "\t" * [2 - f.name.length / 8, 0].max
puts "\t#{f.name}#{tabs}:#{width},"
end
end
def diffanalyze value, old=nil, stream=$stdout
fields = @fields.
zip(fvs(value), fvs(old)).
select {|f, fv, oldfv| fv != oldfv}
return if fields.empty?
longest, longest_name = find_longest fields.transpose[0]
for f, fv, _ in fields
analyze_one f, fv, longest, longest_name, stream
end
end
# FVS = return a table of values
def fvs value
@fields.map do |f|
width = f.start - f.stop + 1
((value >> f.stop) & ((1 << width) - 1))
end
end
def find_longest fields=@fields
longest = fields.map{|f| f.start - f.stop + 1 }.max
longest_name = fields.map{|f| f.name.length }.max
[longest, longest_name]
end
def analyze_and_or andv, orv, stream=$stdout
longest, longest_name = find_longest
value = orv # yo, lol
bits_changed = 0 | ~andv | orv
for f, fv in @fields.zip fvs(value)
width = f.start - f.stop + 1
if 0 != (bits_changed >> f.stop) & ((1 << width) - 1)
if 0 != ~(bits_changed >> f.stop) & ((1 << width) - 1)
stream << "WARNING: some bits not changed in following field!\n"
end
analyze_one f, fv, longest, longest_name, stream
end
end
end
def analyze value, stream=$stdout
longest, longest_name = find_longest
for f, fv in @fields.zip fvs(value)
analyze_one f, fv, longest, longest_name, stream
end
end
def cnalyze value, stream=$stdout
stream << @fields.zip(fvs(value)).
reject!{|f,fv| fv == 0 }.
map{|f,fv| "#{fv} << #{@name}_#{f.name}" }.
join(" | ") << "\n"
end
def analyze_one f, fv, longest, longest_name, stream=$stdout
tabs = "\t" * [2 - f.name.length / 8, 0].max
width = f.start - f.stop + 1
format = "%2s:%-2s %-#{longest_name}s %#{longest}sb " <<
"% #{(longest / 4.0).ceil}sh % #{(longest / 3.0).ceil}so\n"
stream << sprintf(
format,
f.start.to_s, f.stop.to_s, f.name, fv.to_bin(width),
fv.to_s(16), fv.to_s(8))
end
end
class Numeric
def between(a,b)
a <= self and self < b
end
end
class Integer
def to_bin(width)
s = to_s(2)
s = "0" * (width - s.length) + s if s.length < width
s
end
end
class BlackHole
class << self
def << a
end
end
end
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'trollop'
usage = <<END
Usage: #{$PROGRAM_NAME} <register dump> <register name or address> <value>
#{$PROGRAM_NAME} <register dump> <register name or address> <AND-ed value> <OR-ed value>
#{$PROGRAM_NAME} <value> < <copy-paste from Xpdf>
END
opts = Trollop::options do
banner usage
opt :l, "Read log and print a more verbose version"
opt :d, "Read memory dump and print a more verbose version"
opt :c, "Produce C code instead of human-readable description"
end
def load_data f
require 'yaml'
YAML.load_file f
end
$canonical = {}
def reg_from_name data, name, stream=BlackHole
regname = name.sub(/^E1000_/, "")
regname = $canonical[regname] if $canonical[regname]
unless data[regname]
regshort = regname[/^[\w\d_]+/]
if (regs = data.keys.select{|i|i.start_with? regshort}).empty?
stream << " (register #{regname} does not exist!)"
return nil
else
regname = $canonical[regname] = regs.first
stream << " (interpreted as register #{regname})"
end
end
reg = Reg.from_map(data[regname], regname)
end
def reg_from_number data, str, stream=BlackHole
rega = str.to_i 0
if rega != 0 or str =~ /^(0x)?0+$/
reg = data.select do |k,v|
if v["recurring"]
(rega - v["offset"] / (v["size"] / 8)).between(0, v["recurring"])
else
v["offset"] == rega
end
end.to_a[0]
if reg.nil?
stream << " Unknown register!"
return nil
end
return Reg.from_map(reg[1], reg[0])
end
nil
end
def reg_from_str data, str, stream=BlackHole
reg_from_number(data, str, stream) or reg_from_name(data, str, stream)
end
if opts.l
data = load_data ARGV[0]
state = {}
while (line = $stdin.gets)
line.chomp!
begin
annotation = ""
if line =~ /^([^ ]+) (<-|->) (0x.+)$/
regname = $1
value = $3.to_i(0)
is_read = ($2 == "->")
end
reg = reg_from_name data, regname, annotation
next unless reg
annotation << "\n"
if state[regname]
reg.diffanalyze(value, state[regname], annotation)
else
reg.analyze(value, annotation)
end
state[regname] = value
ensure
print line
puts "#{annotation}"
end
end
exit
end
if opts.d
data = load_data ARGV[0]
while (line = $stdin.gets)
line.chomp!
begin
annotation = ""
unless line =~ /^\s*([^\s]+)\s+([^\s]+)\s*$/
annotation << "isn't a valid line!\n"
next
end
rega = $1
value = $2.to_i 16
reg = reg_from_number data, rega, annotation
next unless reg
annotation << " " << reg.name << "\n"
reg.analyze(value, annotation)
ensure
print line
puts annotation
end
end
exit
end
case ARGV.length
when 0
Trollop::die "Not enough arguments"
when 1
lines = []
lines << $_ while $stdin.gets
reg = Reg.parse(lines)
reg.analyze(ARGV[0].to_i(0))
when 2
Trollop::die "Not enough arguments"
when 3
data = load_data ARGV[0]
reg = reg_from_str(data, ARGV[1], $stdout)
unless reg
puts
exit
end
value = ARGV[2].to_i 0
if opts.c
reg.cnalyze(value)
else
reg.analyze(value)
end
when 4
data = load_data ARGV[0]
reg = reg_from_str(data, ARGV[1], $stdout)
unless reg
puts
exit
end
andv = ARGV[2].to_i 0
orv = ARGV[3].to_i 0
reg.analyze_and_or(andv, orv)
end
|