-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrom.py
executable file
·285 lines (233 loc) · 7.59 KB
/
rom.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
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
#!/usr/bin/python
#
# Ericsson Bluetooth Baseband Controller
# ROM Bootloader toolkit
#
# (c) 2012 <[email protected]>
import sys
import serial
import struct
import hashlib
import time
class IRMAFlasher:
def __init__(self):
self.s = serial.Serial('/dev/ttyUSB1', 9600, timeout = 0.01)
self.rs = serial.Serial('/dev/ttyUSB0')
def blobhash(self, filename):
b = file(filename).read()
m = hashlib.md5()
m.update(b[8:])
return m.hexdigest()[:8]
def asicNameById(self, id):
if id == 0x01:
return 'Irma-B P1A'
elif id == 0x02:
return 'Irma-B P2A'
elif id == 0x03:
return 'Irma-B P3A'
elif id == 0x04:
return 'Irma-B P3B'
elif id == 0x05:
return 'Irma-B P4A'
elif id == 0x06:
return 'Irma-B P5A'
elif id == 0x07:
return 'Irma-C P1A'
elif id == 0x08:
return 'Irma-B P4B'
elif id == 0x09:
return 'Irma-B P4C'
elif id == 0x0B:
return 'Irma-C P1B'
elif id == 0x0D:
return 'Irma-C P2A'
elif id == 0x10:
return 'Blink PBM 990 90/2'
else:
return 'Unknown (%08x)' % id
# Perform a hard reset on the module by pulling /RESET
def hard_reset(self):
self.rs.setRTS(True)
self.rs.setRTS(False)
self.rs.setRTS(True)
# Try invoke the ROM bootloader by repeatedly pulling /RESET
# and sending the magic word until we get a response.
def connect(self):
sys.stdout.write('Detecting hardware... ')
spinner = '/-\|'
spinidx = 0
while True:
self.hard_reset()
sys.stdout.write('\b' + spinner[spinidx])
sys.stdout.flush()
spinidx = (spinidx + 1) % 4
for _ in xrange(40):
self.s.write("A\x55\x33")
d = self.s.read(9)
if len(d) == 9 and d[8] == 'R':
(self.romrev, self.flashrev, c) = struct.unpack('<IIc', d)
print '\bfound!\n'
print 'Chip Revision: %s\nFlash Revision: %08x\n' % (self.asicNameById(self.romrev), self.flashrev)
self.s.timeout = None
return
# Send a generic blob while drawing a progress bar
def write_blob(self, data):
size = len(data)
written = 0
while True:
self.s.write(data[:256])
written += len(data[:256])
data = data[256:]
width = 32
sys.stdout.write('\r[');
dots = width*written/size
for i in xrange(width):
if i < dots:
sys.stdout.write('=')
else:
sys.stdout.write(' ')
sys.stdout.write('] %3d %%' % (100*written/size))
sys.stdout.flush()
if len(data) == 0:
break
sys.stdout.write('\n')
# Download a blob, preceded by an offset-size tuple
def load(self, filename, addr = 0):
print 'Loading %s...' % filename
f = file(filename)
boot = f.read()
bootsz = len(boot)
print 'Downloading (%d bytes)...' % bootsz
self.s.write(struct.pack('<II', addr, bootsz))
self.write_blob(boot[:bootsz])
# Download the RAM bootloader
def bootstrap(self, filename='irma_fl_ebt_1_00_(ecs412mod).axf'):
print 'Bootstrapping %s...' % filename
f = file(filename)
# Parse AIF header
boot = f.read()
(entry, rosz, rwsz) = struct.unpack('<12xI4xII', boot[:28])
entry = (entry & 0xffffff) << 2
print ' entry=0x%08x, ro=%04x, rw=0x%04x\n' % (entry, rosz, rwsz)
bootsz = rosz+rwsz
print 'Downloading bootstrap (%d bytes)...' % bootsz
self.s.write(struct.pack('<II', 0x00000000, bootsz))
self.write_blob(boot[:bootsz])
d = self.s.read(1)
if d != '>':
raise Exception('Unexpected bootstrap response "%s"' % d)
print 'Bootstrap is running'
def command(self, cmd, params = ""):
self.s.write(cmd+params)
#while True:
d = self.s.readline()
self.s.read(2) #vask
return d[:-2]
def version(self):
return self.command('V')
def memory(self):
return self.command('M')
def erase(self, bank = 0):
if bank == 0:
print 'Erasing program banks'
elif bank == 1:
print 'Erasing parameter banks'
elif bank == 2:
print 'Erasing all banks'
else:
raise ValueException('Invalid bank')
return self.command('D%d' % bank)
def read(self, addr):
return self.command('L%06x' % int(addr))
def set_baud(self, baud):
print 'Setting baud rate to %d' % baud
if baud == 4800:
cmd = 'C0'
elif baud == 9600:
cmd = 'C1'
elif baud == 19200:
cmd = 'C2'
elif baud == 38400:
cmd = 'C3'
elif baud == 57600:
cmd = 'C4'
elif baud == 115200:
cmd = 'C5'
elif baud == 230400:
cmd = 'C6'
elif baud == 460800:
cmd = 'C7'
else:
raise ValueException('Invalid baud rate %d' % baud)
self.s.write(cmd)
t = self.s.read(1)
print ' '.join(map(hex, map(ord, t)))
self.s.baudrate = baud
self.s.write('!')
d = self.s.readline()
x = self.s.read(2) #vask
print ' '.join(map(hex, map(ord, d)))
print ' '.join(map(hex, map(ord, x)))
return d[:-2]
def program(self, filename):
print 'Programming %s to flash...' % filename
f.s.write('P')
f.load(filename, 0x01000000)
d = f.s.readline()
f.s.read(2) #vask
if d[:-2] != '!:':
raise Exception('Flashing failed! Returned "%s"' % d[:-2])
else:
print 'Done.'
f.status()
def status(self):
s = f.command('E')
if s == 'E9':
print 'No errors.'
elif s == 'E0':
raise Exception('Invalid command')
elif s == 'E2':
raise Exception('Program error')
elif s == 'E3':
raise Exception('Erase error')
elif s == 'E4':
raise Exception('Flash image too large')
elif s == 'E5':
raise Exception('Flash not erased')
elif s == 'E6':
raise Exception('Unknown device')
else:
raise Exception('Unknown status %s' % s)
def leave(self):
return f.s.write('Q')
def terminal(self, baudrate=9600, hex=False):
self.s.baudrate = baudrate
while True:
d = self.s.read(1)
if len(d) < 1:
continue
if hex:
sys.stdout.write('%02x ' % ord(d[0]))
else:
sys.stdout.write(d)
sys.stdout.flush()
def flash(self, filename):
self.connect()
fwrev = self.blobhash(filename)
print 'Firmware to download: %s' % fwrev
if fwrev == '%08x' % self.flashrev:
print 'Same version! (%08x)' % self.flashrev
self.bootstrap()
print
print 'Version: %s' % f.version()
print 'Flash: %s' % f.memory()
print
self.set_baud(460800)
self.erase()
self.program(filename)
print 'Rebooting...'
self.leave()
if __name__ == '__main__':
f = IRMAFlasher()
f.flash('flash_rok101008_p.bin')
f.terminal()