forked from mac2612/sshflash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cbf.py
311 lines (227 loc) · 6.97 KB
/
cbf.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env python
##############################################################################
# OpenLFConnect
#
# Copyright (c) 2012 Jason Pruitt <[email protected]>
#
# This file is part of OpenLFConnect.
# OpenLFConnect is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenLFConnect is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenLFConnect. If not, see <http://www.gnu.org/licenses/>.
##############################################################################
##############################################################################
# Title: OpenLFConnect
# Author: Jason Pruitt
# Email: [email protected]
# IRC: #didj irc.freenode.org
# Wiki: http://elinux.org/LeapFrog_Pollux_Platform:_OpenLFConnect
##############################################################################
#@
# cbf.py Version 0.1.2
import os
import array
import struct
#############################
# NOTES:
# kernel_load and kernel_jump
# newest make_cbf.py uses 0x8000
# old one has 0x100000
# latest cbf still have old number
##############################
PACKET_SIZE = 16384
MAGIC_NUMBER = '\xf0\xde\xbc\x9a'
COMPRESSION_SIG = '\x1F\x8B\x08\x00' #gunzip
CBF_VERSION = 1
BLOCK_SIZE = 0x20000
KERNEL_LOAD_08 = 0x8000
KERNEL_JUMP_08 = KERNEL_LOAD_08
KERNEL_LOAD_10 = 0x100000
KERNEL_JUMP_10 = KERNEL_LOAD_10
KERNEL_LOAD_28 = 0x00208000
KERNEL_JUMP_28 = KERNEL_LOAD_28
def error(e):
assert False, e
def check(path, ret_bool=False):
try:
if not os.path.exists(path):
error('Firmware not found')
else:
f = open(path, 'rb')
file_size = len(f.read())
f.seek(0)
magic = f.read(4)
f.close()
if not str(file_size/PACKET_SIZE).isdigit():
if ret_bool:
return False
else:
error('File is the wrong size, should be multiple of %s.' % PACKET_SIZE)
if magic != MAGIC_NUMBER:
if ret_bool:
return False
else:
error('File failed CBF Magic Number check.')
else:
return True
except Exception, e:
error(e)
def extract(path):
p = parse(path)
image = p.get_image()
if p.is_compressed:
kernel_name = 'zImage'
else:
kernel_name = 'Image'
kernel_path = os.path.join(os.path.dirname(path), kernel_name)
print 'Unwrapping Kernel from CBF'
fimg = open(kernel_path, 'wb')
fimg.write(image)
fimg.close()
print 'Saved as: %s' % kernel_name
def create(mem, opath, ipath):
try:
image_name = os.path.basename(ipath)
image_path = os.path.dirname(ipath)
if not mem in ('superhigh', 'high', 'low'):
error('Memory location should be high or low')
if not opath:
error('No output file selected.')
if not ipath:
error('No output file selected')
if not os.path.exists(ipath):
error('Path does not exist.')
elif os.path.isdir(ipath):
error('Path is not a file.')
elif 'zImage' not in image_name and 'Image' not in image_name:
error('Does not appear to be an Image or zImage file.')
if len(os.path.basename(opath)) > 64:
error('Output name is too long.')
if not opath.endswith('.cbf'):
opath = '%s.cbf' % opath
p = packer(mem, opath, ipath)
p.pack()
summary(os.path.join(image_path, opath))
except Exception, e:
error(e)
def summary(path):
check(path)
p = parse(path)
p.create_summary()
print 'CBF File Summary:'
for k,v in p.summary.iteritems():
if len(k) < 7:
tab = '\t\t'
else:
tab = '\t'
print '%s:%s0x%08x' % (k,tab,v)
print 'Compressed: \t %s' % p.is_compressed
class parse(object):
def __init__(self, path):
if os.path.isdir(path):
error('Path is not a file.')
self._path = path
self._word_width = 4
self._summary_names = ['cbf_magic','cbf_version','kernel_load','kernel_jump','size','sum_crc','kernel_crc']
self._kernel_offset = self._word_width * (len(self._summary_names) - 1)
self.summary = {}
self._u32 = struct.Struct("<L")
self.is_compressed = False
def error(self, e):
assert False, e
def create_summary(self):
self.get_image()
def get_image(self):
try:
if not check(self._path, True):
self.error('Problem with file.')
f = open(self._path, 'rb')
for i in range(0, len(self._summary_names)):
offset = i * self._word_width
f.seek(offset)
read_buf = self._u32.unpack(f.read(self._word_width))[0]
self.summary[self._summary_names[i]] = read_buf
f.seek(self._kernel_offset + self.summary['size'])
read_buf = self._u32.unpack(f.read(self._word_width))[0]
self.summary['kernel_crc'] = read_buf
f.seek(self._kernel_offset)
image = f.read(self.summary['size'])
if COMPRESSION_SIG in image:
self.is_compressed = True
f.close()
return image
except Exception, e:
self.error(e)
class packer(object):
def __init__(self, mem, opath, ipath):
self._in_path = ipath
self._out_path = opath
self._summary = ''
self._summary_crc = ''
self._buffer = ''
self._buffer_crc = ''
self._size = 0
if mem.lower() == 'superhigh':
self._kernel_jump = KERNEL_JUMP_28
self._kernel_load = KERNEL_LOAD_28
elif mem.lower() == 'high':
self._kernel_jump = KERNEL_JUMP_10
self._kernel_load = KERNEL_LOAD_10
elif mem.lower() == 'low':
self._kernel_jump = KERNEL_JUMP_08
self._kernel_load = KERNEL_LOAD_08
else:
self.error('Memory location must be high, or low.')
def error(self, e):
assert False, e
def crc(self, buf):
a = array.array('I', buf)
crc = 0;
for c in a:
crc = 1 + (crc ^ c)
return crc
def pack(self):
try:
self.set_buffer()
self.set_summary()
buf = self._summary
buf += self._buffer
buf_len = len(buf)
rem = buf_len % BLOCK_SIZE
pad_len = 0
if rem != 0:
pad_len = BLOCK_SIZE - rem
padding = pad_len * '\xFF'
buf += padding
print 'Writing CBF file.'
f = open(self._out_path, 'wb')
f.write(buf)
f.close()
except Exception, e:
error(e)
def set_buffer(self):
f = open(self._in_path, 'rb')
self._buffer = f.read()
self._size = len(self._buffer)
self._buffer_crc = self.crc(self._buffer)
self._buffer += struct.pack('I', self._buffer_crc & 0xFFFFFFFF)
f.close
def set_summary(self):
if not self._buffer_crc:
self.error('Pack buffer is empty.')
if not self._size:
self.error('Pack problem reading buffer for summary.')
self._summary = MAGIC_NUMBER
self._summary += struct.pack('IIII', CBF_VERSION, self._kernel_load, self._kernel_jump, self._size)
self._summary_crc = self.crc(self._summary)
self._summary += struct.pack('I', self._summary_crc)
if __name__ == '__main__':
print 'No examples yet.'