-
Notifications
You must be signed in to change notification settings - Fork 5
/
createhexdump.py
executable file
·186 lines (125 loc) · 5.1 KB
/
createhexdump.py
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
#!/usr/bin/python3
#
# Reformat data of usb pcap
import sys
import re
import fileinput
import at_d878uv_mem as atdevmem # AT D878UV memory layout
expnextaddr = 0
blockstartaddr = 0
nummemoryblocks = 0
if len(sys.argv) > 1 :
if sys.argv[1] != '-':
# open file
f = open(sys.argv[1], "r")
else:
# open stdin
f = sys.stdin
else:
# open stdin
f = sys.stdin
# read input file or stdin
for line in f.readlines():
line = line.rstrip()
#print (line)
# parse line
m = re.search('^(57)(........)(..)(.*)(..)(06)$', line)
m_firmwarewrite = re.search('^(01)(........)(.*)(....)(06)$', line)
if m is not None:
# line with data (type W - 0x57)
datalen = len(m.group(4))
memoryaddr = m.group(2)
memorylen = m.group(3)
if int(memoryaddr, 16) != expnextaddr : #and memoryaddr != '02fa0020':
# new memory section started
if expnextaddr != 0:
print( '=> Block ' + str(nummemoryblocks) + ': ' )
print( '=> Size: ' + str(hex(blockstartaddr)) + ' .. ' + str(hex(expnextaddr-1)) + ': ' + str( expnextaddr - blockstartaddr ) + ' bytes' )
print( '=> Name: ' + atdevmem.getMemorySectionName(blockstartaddr) + ' .. ' + atdevmem.getMemorySectionName(expnextaddr-1) )
blockstartaddr = int(memoryaddr, 16)
nummemoryblocks += 1
print("")
expnextaddr = int(memoryaddr, 16) + int(memorylen, 16)
print( m.group(1) + ' | ' + memoryaddr + ' | ' + memorylen + ' | ', end = '' )
print( m.group(4)[0:int(datalen/4)] + ' ' + m.group(4)[int(datalen/4):int(datalen/2)] + ' ' + m.group(4)[int(datalen/2):int(3*datalen/4)] + ' ' + m.group(4)[int(3*datalen/4):] , end = '' )
print( ' | ' + m.group(5) + ' ' + m.group(6) + ' ||', end = '' )
# print printable chars
for i in range(0, len(m.group(4)), 2):
if i % 8 == 0:
print(' ', end = '')
idec = int(m.group(4)[i:i+2], 16)
if (32 <= idec and idec < 127) or (160 <= idec):
print( chr(idec), end = '')
else:
print('.', end = '')
print(' || ', end='')
# print everything again without spaces (better for searching)
for i in range(0, len(m.group(4)), 2):
idec = int(m.group(4)[i:i+2], 16)
if (32 <= idec and idec < 127) or (160 <= idec):
print( chr(idec), end = '')
else:
print('.', end = '')
print(" ||")
elif m_firmwarewrite is not None:
# line with firmware write data (type 0x01)
datalen = len(m_firmwarewrite.group(3))
memoryaddr = m_firmwarewrite.group(2)
data = m_firmwarewrite.group(3)
print( m_firmwarewrite.group(1) + ' | ' + memoryaddr + ' | ', end = '' )
print( m_firmwarewrite.group(3)[0:int(datalen/4)] + ' ' + m_firmwarewrite.group(3)[int(datalen/4):int(datalen/2)] + ' ' + m_firmwarewrite.group(3)[int(datalen/2):int(3*datalen/4)] + ' ' + m_firmwarewrite.group(3)[int(3*datalen/4):] , end = '' )
print( ' | ' + m_firmwarewrite.group(4) + ' ' + m_firmwarewrite.group(5) + ' ||', end = '' )
# print printable chars
for i in range(0, len(m_firmwarewrite.group(3)), 2):
if i % 8 == 0:
print(' ', end = '')
idec = int(m_firmwarewrite.group(3)[i:i+2], 16)
if (32 <= idec and idec < 127) or (160 <= idec):
print( chr(idec), end = '')
else:
print('.', end = '')
print(' || ', end='')
# print everything again without spaces (better for searching)
for i in range(0, len(m_firmwarewrite.group(3)), 2):
idec = int(m_firmwarewrite.group(3)[i:i+2], 16)
if (32 <= idec and idec < 127) or (160 <= idec):
print( chr(idec), end = '')
else:
print('.', end = '')
print(" ||")
elif line == '':
# throw away empty line
pass
elif line == '454e44':
# end of transmission. print size of last block
if expnextaddr != 0:
print( '=> Block ' + str(nummemoryblocks) + ': ' )
print( '=> Size: ' + str(hex(blockstartaddr)) + ' .. ' + str(hex(expnextaddr-1)) + ': ' + str( expnextaddr - blockstartaddr ) + ' bytes' )
print( '=> Name: ' + atdevmem.getMemorySectionName(blockstartaddr) + ' .. ' + atdevmem.getMemorySectionName(expnextaddr-1) )
print("")
print(line)
elif line == '5202fa002010':
# special address read by cps before writing to device
print("")
print(line)
elif line[0:2] == '52' and line != '5202fa002010':
# throw away read request
pass
elif len(line) > 2:
# other data. do some hex decode
print( line + ' || ', end = '')
# print printable chars
for i in range(0, len(line), 2):
idec = int(line[i:i+2], 16)
if (32 <= idec and idec < 127) or (160 <= idec):
print(chr(idec), end = '')
else:
print('.', end = '')
print("")
elif line == '06':
# throw away acknowledge message
pass
else:
# just print everything else
print( line )
f.close()