Repository navigation
Expand file tree
/
Copy pathVW_Flash.py
More file actions
405 lines (337 loc) · 12.8 KB
/
Copy pathVW_Flash.py
File metadata and controls
405 lines (337 loc) · 12.8 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import tqdm
import logging
import argparse
import sys
from os import path
import lib.simos_flash_utils as simos_flash_utils
import lib.constants as constants
import lib.simos_uds as simos_uds
import shutil
# Get an instance of logger, which we'll pull from the config file
logger = logging.getLogger("VWFlash")
try:
currentPath = path.dirname(path.abspath(__file__))
except NameError: # We are the main py2exe script, not a module
currentPath = path.dirname(path.abspath(sys.argv[0]))
logging.config.fileConfig(path.join(currentPath, "logging.conf"))
logger.info("Starting VW_Flash.py")
if sys.platform == "win32":
defaultInterface = "J2534"
else:
defaultInterface = "SocketCAN"
logger.debug("Default interface set to " + defaultInterface)
# build a List of valid block parameters for the help message
block_number_help = []
for name, number in constants.block_name_to_int.items():
block_number_help.append(name)
block_number_help.append(str(number))
# Set up the argument/parser with run options
parser = argparse.ArgumentParser(
description="VW_Flash CLI",
epilog="The MAIN CLI interface for using the tools herein",
)
parser.add_argument(
"--action",
help="The action you want to take",
choices=[
"checksum",
"checksum_fix",
"checksum_ecm3",
"checksum_fix_ecm3",
"lzss",
"encrypt",
"prepare",
"flash_cal",
"flash_bin",
"flash_prepared",
"get_ecu_info",
],
required=True,
)
parser.add_argument(
"--infile", help="the absolute path of an inputfile", action="append"
)
parser.add_argument(
"--outfile", help="the absolutepath of a file to output", action="store_true"
)
parser.add_argument(
"--block",
type=str,
help="The block name or number",
choices=block_number_help,
action="append",
required=False,
)
parser.add_argument(
"--simos12", help="specify simos12, available for checksumming", action="store_true"
)
parser.add_argument("--simos1810", help="specify simos18.10", action="store_true")
parser.add_argument(
"--is_early", help="specify an early car for ECM3 checksumming", action="store_true"
)
parser.add_argument(
"--interface",
help="specify an interface type",
choices=["J2534", "SocketCAN", "TEST"],
default=defaultInterface,
)
args = parser.parse_args()
flash_info = constants.s18_flash_info
if args.simos12:
flash_info = constants.s12_flash_info
if args.simos1810:
flash_info = constants.s1810_flash_info
# function that reads in from a file
def read_from_file(infile=None):
f = open(infile, "rb")
return f.read()
# function that writes out binary data to a file
def write_to_file(outfile=None, data_binary=None):
if outfile and data_binary:
with open(outfile, "wb") as fullDataFile:
fullDataFile.write(data_binary)
if args.action == "flash_cal":
if len(args.infile) != 1:
logger.critical(
"You chose to flash a calibration, but you must specify a single calibration file"
)
exit()
args.block = ["CAL"]
# if the number of block args doesn't match the number of file args, log it and exit
if (args.infile and not args.block) or (
args.infile and (len(args.block) != len(args.infile))
):
logger.critical("You must specify a block for every infile")
exit()
# convert --blocks on the command line into a list of ints
if args.block:
blocks = [int(constants.block_to_number(block)) for block in args.block]
# build the dict that's used to proces the blocks
# Everything is structured based on the following format:
# {'infile1': {'blocknum': num, 'binary_data': binary},
# 'infile2: {'blocknum': num2, 'binary_data': binary2}
# }
if args.infile and args.block:
blocks_infile = {}
for i in range(0, len(args.infile)):
blocks_infile[args.infile[i]] = {
"blocknum": blocks[i],
"binary_data": read_from_file(args.infile[i]),
}
# if there was no file specified, log it and exit
else:
logger.critical("No input file specified.")
# exit()
def callback_function(t, flasher_step, flasher_status, flasher_progress):
t.update(round(flasher_progress - t.n))
t.set_description(flasher_status, refresh=True)
# if statements for the various cli actions
if args.action == "checksum":
simos_flash_utils.checksum(flash_info=flash_info, blocks_infile=blocks_infile)
elif args.action == "checksum_fix":
blocks_infile = simos_flash_utils.checksum_fix(
flash_info=flash_info, blocks_infile=blocks_infile
)
# if outfile was specified in the arguments, go through the dict and write each block out
if args.outfile:
for filename in blocks_infile:
binary_data = blocks_infile[filename]["binary_data"]
blocknum = blocks_infile[filename]["blocknum"]
write_to_file(
data_binary=blocks_infile[filename]["binary_data"],
outfile=filename.rstrip(".bin")
+ ".checksummed_block"
+ str(blocknum)
+ ".bin",
)
else:
logger.critical("Outfile not specified, files not saved!!")
if args.action == "checksum_ecm3":
simos_flash_utils.checksum_ecm3(
flash_info, blocks_infile, is_early=(args.is_early or args.simos12)
)
elif args.action == "checksum_fix_ecm3":
blocks_infile = simos_flash_utils.checksum_ecm3(
flash_info, blocks_infile, should_fix=True, is_early=args.is_early
)
# if outfile was specified in the arguments, go through the dict and write each block out
if args.outfile:
for filename in blocks_infile:
binary_data = blocks_infile[filename]["binary_data"]
blocknum = blocks_infile[filename]["blocknum"]
write_to_file(
data_binary=blocks_infile[filename]["binary_data"],
outfile=filename.rstrip(".bin")
+ ".checksummed_block"
+ str(blocknum)
+ ".bin",
)
else:
logger.critical("Outfile not specified, files not saved!!")
elif args.action == "lzss":
simos_flash_utils.lzss_compress(blocks_infile, args.outfile)
elif args.action == "encrypt":
blocks_infile = simos_flash_utils.encrypt_blocks(
flash_info=flash_info, blocks_infile=blocks_infile
)
# if outfile was specified, go through each block in the dict and write it out
if args.outfile:
for filename in blocks_infile:
binary_data = blocks_infile[filename]["binary_data"]
blocknum = blocks_infile[filename]["blocknum"]
outfile = filename + ".flashable_block" + str(blocknum)
logger.info("Writing encrypted file to: " + outfile)
write_to_file(outfile=outfile, data_binary=binary_data)
else:
logger.critical("No outfile specified, skipping")
elif args.action == "prepare":
simos_flash_utils.prepareBlocks(flash_info, blocks_infile)
elif args.action == "flash_cal":
t = tqdm.tqdm(
total=100,
colour="green",
ncols=round(shutil.get_terminal_size().columns * 0.75),
)
def wrap_callback_function(flasher_step, flasher_status, flasher_progress):
callback_function(t, flasher_step, flasher_status, float(flasher_progress))
ecuInfo = simos_uds.read_ecu_data(
interface=args.interface, callback=wrap_callback_function
)
for did in ecuInfo:
logger.debug(did + " - " + ecuInfo[did])
print()
logger.info(
"Executing flash_bin with the following blocks:\n"
+ "\n".join(
[
" : ".join(
[
filename,
str(blocks_infile[filename]["blocknum"]),
constants.int_to_block_name[
blocks_infile[filename]["blocknum"]
],
str(
blocks_infile[filename]["binary_data"][
constants.software_version_location[
blocks_infile[filename]["blocknum"]
][0] : constants.software_version_location[
blocks_infile[filename]["blocknum"]
][
1
]
].decode()
),
str(
blocks_infile[filename]["binary_data"][
constants.box_code_location[
blocks_infile[filename]["blocknum"]
][0] : constants.box_code_location[
blocks_infile[filename]["blocknum"]
][
1
]
].decode()
),
]
)
for filename in blocks_infile
]
)
)
for filename in blocks_infile:
fileBoxCode = str(
blocks_infile[filename]["binary_data"][
constants.box_code_location[blocks_infile[filename]["blocknum"]][
0
] : constants.box_code_location[blocks_infile[filename]["blocknum"]][1]
].decode()
)
if ecuInfo["VW Spare Part Number"].strip() != fileBoxCode.strip():
logger.critical(
"Attempting to flash a file that doesn't match box codes, exiting!: "
+ ecuInfo["VW Spare Part Number"]
+ " != "
+ fileBoxCode
)
exit()
else:
logger.critical("File matches ECU box code")
simos_flash_utils.flash_bin(
flash_info, blocks_infile, wrap_callback_function, interface=args.interface
)
t.close()
elif args.action == "flash_bin":
t = tqdm.tqdm(
total=100,
colour="green",
ncols=round(shutil.get_terminal_size().columns * 0.75),
)
def wrap_callback_function(flasher_step, flasher_status, flasher_progress):
callback_function(t, flasher_step, flasher_status, float(flasher_progress))
logger.info(
"Executing flash_bin with the following blocks:\n"
+ "\n".join(
[
" : ".join(
[
filename,
str(blocks_infile[filename]["blocknum"]),
constants.int_to_block_name[
blocks_infile[filename]["blocknum"]
],
str(
blocks_infile[filename]["binary_data"][
constants.software_version_location[
blocks_infile[filename]["blocknum"]
][0] : constants.software_version_location[
blocks_infile[filename]["blocknum"]
][
1
]
].decode()
),
str(
blocks_infile[filename]["binary_data"][
constants.box_code_location[
blocks_infile[filename]["blocknum"]
][0] : constants.box_code_location[
blocks_infile[filename]["blocknum"]
][
1
]
].decode()
),
]
)
for filename in blocks_infile
]
)
)
simos_flash_utils.flash_bin(
flash_info, blocks_infile, wrap_callback_function, interface=args.interface
)
t.close()
elif args.action == "flash_prepared":
t = tqdm.tqdm(
total=100,
colour="green",
ncols=round(shutil.get_terminal_size().columns * 0.75),
)
def wrap_callback_function(flasher_step, flasher_status, flasher_progress):
callback_function(t, flasher_step, flasher_status, float(flasher_progress))
simos_flash_utils.flash_prepared(blocks_infile, wrap_callback_function)
t.close()
elif args.action == "get_ecu_info":
t = tqdm.tqdm(
total=100,
colour="green",
ncols=round(shutil.get_terminal_size().columns * 0.75),
)
def wrap_callback_function(flasher_step, flasher_status, flasher_progress):
callback_function(t, flasher_step, flasher_status, float(flasher_progress))
ecu_info = simos_uds.read_ecu_data(
interface=args.interface, callback=wrap_callback_function
)
t.close()
[print(did + " : " + ecu_info[did]) for did in ecu_info]