forked from esaulenka/vbf_parser
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvbf_parser.py
More file actions
61 lines (41 loc) · 1.23 KB
/
vbf_parser.py
File metadata and controls
61 lines (41 loc) · 1.23 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
#!/usr/bin/python
import struct
import intelhex
import sys
def crc16(data):
data = bytearray(data)
crc = 0xFFFF
for b in data:
crc ^= b << 8
for _ in xrange(8):
if crc & 0x8000:
crc = (crc << 1) ^ 0x1021
else:
crc = crc << 1
crc &= 0xffff
return crc
if len(sys.argv) != 2:
print('Please specify VBF file')
quit(-1)
inname = sys.argv[1]
with open(inname, 'rb') as f:
data = f.read()
header_len = data.find('\x3B\x0D\x0A\x7D') + 4
print 'Header length: 0x%04X' % header_len
if header_len < 100:
print 'Unknown format'
quit(-1)
out_hex = intelhex.IntelHex()
offset = header_len
while offset < len(data):
[block_addr, block_len] = struct.unpack('>2L', data[offset: offset + 8])
offset += 8
block_data = bytearray(data[offset: offset + block_len])
offset += block_len
crc = struct.unpack('>H', data[offset: offset + 2])[0]
offset += 2
crc_res = 'ok ' if crc16(block_data) == crc else 'error'
print "Block adr: 0x%X length: 0x%X crc %s" % (block_addr, block_len, crc_res)
out_hex.frombytes(block_data, block_addr)
out_hex.tofile(inname + '.hex', 'hex')
out_hex.tofile(inname + '.bin', 'bin')