-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump_spi.py
More file actions
executable file
·111 lines (93 loc) · 3.92 KB
/
Copy pathdump_spi.py
File metadata and controls
executable file
·111 lines (93 loc) · 3.92 KB
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
#!/usr/bin/env python3
import argparse
import logging
import re
import sys
import textwrap
from pathlib import Path
LOGGER = logging.getLogger(__name__)
LOGGER.addHandler(logging.StreamHandler())
LOGGER.setLevel(logging.INFO)
try:
import hexdump
import serial
except ImportError as _:
LOGGER.error('Failed to load module. Please install hexdump and serialpyserial.')
sys.exit(1)
parser = argparse.ArgumentParser(description='Dump SPI flash content',
epilog='Report any issues to github.com/nabilbendafi',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('-d', '--device',
required=True,
help=textwrap.dedent('''\
The device name of USB/Serial interface.
ex: /dev/ttyUSB0'''))
parser.add_argument('-o', '--output',
dest='output',
default='dump_spi.kwb',
help='Output file')
parser.add_argument('-v', '--verbose',
action='store_true',
help='Increase output verbosity')
args = parser.parse_args()
def main():
if args.verbose:
LOGGER.setLevel(logging.DEBUG)
cur_dir = Path.cwd()
device = args.device
output = Path(args.output)
LOGGER.info('Open %s file' % output)
with open(output.as_posix(), 'w') as o:
LOGGER.info('Open %s serial device' % device)
try:
with serial.Serial(device, baudrate=115200, timeout=3.0) as s:
# Send Ctrl-C for sanity
cmd = b'\x03'
LOGGER.debug('Send %s to %s' % (cmd, device))
s.write(cmd)
_ = s.readlines() # Flush stdout content
address_format = '%08x' # 0x00000000
offset = 0x0
# Initialise SPI Flash read
cmd = b'sf probe 1\n'
LOGGER.debug('Send %s to %s' % (cmd, device))
s.write(cmd)
_ = s.readlines() # Flush stdout content
cmd = b'sf read 0x08000000 0 %x\n' % (512*1024) # MX25L4005 with page size 64 KiB, total 512 KiB
LOGGER.debug('Send %s to %s' % (cmd, device))
s.write(cmd)
_ = s.readlines() # Flush stdout content
cmd = b'md.b 0x08000000 %x\n' % (512*1024) # MX25L4005 with page size 64 KiB, total 512 KiB
LOGGER.debug('Send %s to %s' % (cmd, device))
s.write(cmd)
_ = s.readline() # Flush stdout content
while True:
line = s.readline()
line = line.decode('utf-8')
regex = '(?P<offset>[0-9a-fA-F]+): (?P<hex>([0-9a-fA-F]+ ){15}[0-9a-fA-F]+) *(?P<ascii>.*)'
regex = re.compile(regex)
m = regex.match(line)
if m:
line = m.groupdict()
hexa = ' '.join(re.findall('..',
line['hex'].replace(' ', '')))
line = "%s: %s %s\n" % (address_format % (offset * 16),
hexa,
line['ascii'])
offset += 1
LOGGER.debug('Write %s to %s' % (line, output))
o.write(line)
# No more dump data
if not line:
break
except serial.serialutil.SerialException as se:
LOGGER.error('Failed to open %s: %s' % (device, str(se)))
sys.exit(1)
with open(output.as_posix(),'r') as txt:
LOGGER.info('Convert dump to binrary')
hex_dump = txt.read()
with open(output.as_posix(),'bw') as binary:
binary.write(hexdump.restore(hex_dump))
if __name__ == "__main__":
# Rock'n'Roll
main()