-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_flash.py
56 lines (47 loc) · 1.75 KB
/
extract_flash.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
import xml.etree.ElementTree as ET
import binascii
import sys
import os
def bits(byte):
return ((byte >> 7) & 1,
(byte >> 6) & 1,
(byte >> 5) & 1,
(byte >> 4) & 1,
(byte >> 3) & 1,
(byte >> 2) & 1,
(byte >> 1) & 1,
(byte) & 1)
def decrypt_simos8_block(arr):
result = bytearray()
dupl_list = []
for i, v in enumerate(arr):
result.append((v ^ i) & 0xFF)
dupl_list.append((v ^ i) & 0xFF)
# arr[i] = (v ^ i) & 0xFF
return result, dupl_list
test_odx = '~/Downloads/FL_8K0907551D__0003.odx'
if sys.argv[1] == 'test':
tree = ET.parse(test_odx)
else:
test_odx = sys.argv[1]
tree = ET.parse(test_odx)
prefix = os.path.splitext(os.path.basename(test_odx))[0]
root = tree.getroot()
flashdata = root.findall('./FLASH/ECU-MEMS/ECU-MEM/MEM/FLASHDATAS/FLASHDATA')
fulldata = bytearray()
for data in flashdata:
dataContent = data.findall('./DATA')[0].text
dataId = data.get('ID')
length = int(root.findall("./FLASH/ECU-MEMS/ECU-MEM/MEM/DATABLOCKS/DATABLOCK/FLASHDATA-REF[@ID-REF='{}']/../SEGMENTS/SEGMENT/UNCOMPRESSED-SIZE".format(dataId))[0].text)
if len(dataContent) == 2:
# These are ERASE blocks with no data so skip them
continue
list = [int(dataContent[start:start + 2], 16) for start in range(0, len(dataContent), 2)]
# dataBinary = binascii.unhexlify(dataContent)
decryptedContent, dupl_list = decrypt_simos8_block(list)
fulldata.extend(decryptedContent)
# prefix = os.path.splitext(os.path.basename(test_odx))[0]
with open(prefix + data[0].text, 'wb') as dataFile:
dataFile.write(decryptedContent)
with open("{}.bin".format(prefix), 'wb') as fullDataFile:
fullDataFile.write(fulldata)