-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_dump.py
executable file
·51 lines (39 loc) · 1.13 KB
/
decode_dump.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
#!/usr/bin/env python
import binascii
import re
import sys
def main(args):
# in kilobytes
DUMP_SIZE = int(args[1]) if len(args) > 1 else False
HEADER = "DumpPebSPI.c"
BYTES_WRITTEN = 0
LAST_ADDR = -1
BLOCK_SIZE = 32
regex = re.compile("%s.+0x([0-9a-fA-F]+): ([0-9a-fA-F]+)" % HEADER)
with open(args[0]) as f:
with open(args[0]+".bin", "wb") as fw:
for line in f.readlines():
parts = regex.findall(line)
if len(parts) == 0:
continue
addr = int(parts[0][0], 16)
bytes = binascii.unhexlify(parts[0][1])
if LAST_ADDR == -1 or (LAST_ADDR + BLOCK_SIZE) == addr:
LAST_ADDR = addr
else:
# Skip duplicate bytes
if addr < LAST_ADDR:
continue
print "Seeking %d bytes" % (addr - LAST_ADDR)
fw.seek(addr - LAST_ADDR, 1)
LAST_ADDR = addr
BYTES_WRITTEN += len(bytes)
fw.write(bytes)
if DUMP_SIZE:
TOTAL = DUMP_SIZE * 1024
PERCENT = (float(BYTES_WRITTEN) / float(TOTAL)) * 100
print "Dumped %d bytes of %d (%2.2f%%)" % (BYTES_WRITTEN, TOTAL, PERCENT)
else:
print "Dumped %d bytes" % (BYTES_WRITTEN)
if __name__ == "__main__":
main(sys.argv[1:])