forked from Blackfrosch/VAGEDCSuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSA15_checksum.cs
419 lines (340 loc) · 16.8 KB
/
MSA15_checksum.cs
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//---------------------------------------------------------------------------
//
// Bosch VAG TDI v4.1 - 2002 Checksum Algorithm Version 1.0
// Bosch VAG TDI v4.1 Checksum Algorithm Version 1.1
//
// Copyright 2007-2012 MTX Electronics
// Web: www.mtx-electronics.com
// Email: info@mtx-electronics.com
//
//---------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace VAGSuite
{
public class MSA15_checksum
{
public void DumpChecksumLocations(string info, byte[] allBytes)
{
//0x7BFFA & 0x7BFFB
byte b1 = allBytes[0x7BFFA];
byte b2 = allBytes[0x7BFFB];
int checksumAddress = 0;
bool found = true;
while (found)
{
found = false;
checksumAddress = Tools.Instance.findSequence(allBytes, checksumAddress, new byte[2] { b1, b2 }, new byte[2] { 1, 1 });
if (checksumAddress > 0)
{
if ((checksumAddress & 0x000F) == 0x000A)
{
int realAddress = checksumAddress + 2;
Console.WriteLine(info + " detected checksumAddress: " + realAddress.ToString("X8"));
}
checksumAddress += 2;
found = true;
}
}
}
private UInt16 chk_found = 0;
public UInt16 ChecksumsFound
{
get { return chk_found; }
set { chk_found = value; }
}
private UInt16 chk_fixed = 0;
public UInt16 ChecksumsIncorrect
{
get { return chk_fixed; }
set { chk_fixed = value; }
}
private void DumpChecksum(string remark, byte[] allBytes, bool debugInfo)
{
if (debugInfo) Console.WriteLine(remark);//+ " " + allBytes[0x5BFFA].ToString("X2") + " " + allBytes[0x5BFFB].ToString("X2") + " " + allBytes[0x5BFFC].ToString("X2") + " " + allBytes[0x5BFFD].ToString("X2") + " " + allBytes[0x5BFFE].ToString("X2") + " " + allBytes[0x5BFFF].ToString("X2"));
}
//
//? 0x000B80
//? 0x008000
//? 0x008B80
//? 0x010000
//0x014000
//0x038B80
//0x040000
public ChecksumResult tdi41_checksum_search(byte[] file_buffer, UInt32 file_size, bool debug)
{
bool first_pass = true;
UInt32 chk_oldvalue, chk_value, chk_start_addr, chk_end_addr;
//UInt32[] chk_array = new UInt32[7] { 0x000B80, 0x008000, 0x008B80, 0x010000, 0x014000, 0x38B80, 0x40000 };
UInt32[] chk_array = new UInt32[7] { 0x00000, 0x000B80, 0x008000, 0x030000, 0x038000, 0x038B80, 0x40000 };
UInt16 seed_a = 0, seed_b = 0;
chk_found = 0;
chk_fixed = 0;
for (; chk_found < chk_array.Length-1; chk_found++)
{
chk_start_addr = chk_array[chk_found];
chk_end_addr = chk_array[chk_found + 1];
if (!first_pass)
{
seed_a |= 0x8631;
seed_b |= 0xEFCD;
}
chk_oldvalue = ((UInt32)file_buffer[chk_end_addr - 1] << 24)
+ ((UInt32)file_buffer[chk_end_addr - 2] << 16)
+ ((UInt32)file_buffer[chk_end_addr - 3] << 8)
+ (UInt32)file_buffer[chk_end_addr - 4];
chk_value = tdi41_checksum_calculate(file_buffer, chk_start_addr, chk_end_addr - 4, seed_a, seed_b);
Console.WriteLine("chks " + chk_start_addr.ToString("X8") + "-" + chk_end_addr.ToString("X8") + " file: " + chk_oldvalue.ToString("X8") + " calc: " + chk_value.ToString("X8"));
if (chk_oldvalue != chk_value && chk_oldvalue != 0xC3C3C3C3 )
{
file_buffer[chk_end_addr - 4] = Convert.ToByte(chk_value & 0x000000ff);
file_buffer[chk_end_addr - 3] = Convert.ToByte((chk_value >> 8) & 0x000000ff);
file_buffer[chk_end_addr - 2] = Convert.ToByte((chk_value >> 16) & 0x000000ff);
file_buffer[chk_end_addr - 1] = Convert.ToByte((chk_value >> 24) & 0x000000ff);
Console.WriteLine("Checksum at address " + chk_end_addr.ToString("X8") + " failed");
chk_fixed++;
}
first_pass = false;
}
if (chk_fixed == 0) return ChecksumResult.ChecksumOK;
else if (chk_fixed >= 8) return ChecksumResult.ChecksumTypeError;
Console.WriteLine("chkfix: " + chk_fixed.ToString());
return ChecksumResult.ChecksumFail;
}
UInt32 tdi41_checksum_calculate(byte[] file_buffer, UInt32 chk_start_addr, UInt32 chk_end_addr, UInt16 seed_a, UInt16 seed_b)
{
UInt16 var_1;
byte var_2;
do
{
var_2 = 0;
seed_a ^= Convert.ToUInt16((((UInt16)file_buffer[chk_start_addr + 1] << 8) + (UInt16)file_buffer[chk_start_addr]));
chk_start_addr += 2;
if ((seed_b & 0xF) > 0)
{
var_1 = Convert.ToUInt16(seed_a >> (16 - (seed_b & 0xF)));
seed_a <<= (seed_b & 0xF);
seed_a |= var_1;
var_2 = Convert.ToByte(seed_a & 1);
}
seed_b -= Convert.ToUInt16((((UInt16)file_buffer[chk_start_addr + 1] << 8) + (UInt16)file_buffer[chk_start_addr]));
seed_b -= var_2;
chk_start_addr += 2;
seed_b ^= seed_a;
if (chk_start_addr == chk_end_addr)
break;
seed_a -= Convert.ToUInt16((((UInt16)file_buffer[chk_start_addr + 1] << 8) + (UInt16)file_buffer[chk_start_addr]));
chk_start_addr += 2;
seed_a += 0xDAAC;
seed_b ^= Convert.ToUInt16((((UInt16)file_buffer[chk_start_addr + 1] << 8) + (UInt16)file_buffer[chk_start_addr]));
chk_start_addr += 2;
if ((seed_a & 0xF) > 0)
{
var_1 = Convert.ToUInt16((seed_b << (16 - (seed_a & 0xF))) & 0xffff);
seed_b >>= (seed_a & 0xF);
seed_b |= var_1;
}
}
while (chk_start_addr != chk_end_addr);
seed_a -= 0x8631;
seed_a += 0xDAAC;
seed_b ^= 0xDF9B;
return (((UInt32)seed_b << 16) + seed_a);
}
public ChecksumResult tdi41_2002_checksum_search(byte[] file_buffer, UInt32 file_size, bool debug)
{
UInt32 seed_1, seed_2;
UInt16 seed_1_msb, seed_1_lsb, seed_2_lsb, seed_2_msb;
UInt32 chk_oldvalue, chk_value, chk_start_addr, chk_end_addr, chk_store_addr;
chk_found = 2;
chk_fixed = 0;
// Find seed 1
seed_1 = tdi41_2002_checksum_calculate(file_buffer, 0x14000, 0x4bffe, 0x8631, 0xefcd, 0, 0, true);
seed_1_msb = (UInt16)(seed_1 >> 16);
seed_1_lsb = (UInt16)seed_1;
// Find seed 2
seed_2 = tdi41_2002_checksum_calculate(file_buffer, 0, 0x7ffe, 0, 0, 0, 0, true);
seed_2_msb = (UInt16)(seed_2 >> 16);
seed_2_lsb = (UInt16)seed_2;
// checksum 1
chk_oldvalue = ((UInt32)file_buffer[0xffff] << 24)
+ ((UInt32)file_buffer[0xfffe] << 16)
+ ((UInt32)file_buffer[0xfffd] << 8)
+ (UInt32)file_buffer[0xfffc];
chk_value = tdi41_2002_checksum_calculate(file_buffer, 0x8000, 0xfffb, seed_2_lsb, seed_2_msb, 0x4531, 0x3550, false);
if (chk_oldvalue != chk_value)
{
file_buffer[0xfffc] = Convert.ToByte(chk_value & 0x000000ff);
file_buffer[0xfffd] = Convert.ToByte((chk_value >> 8) & 0x000000ff);
file_buffer[0xfffe] = Convert.ToByte((chk_value >> 16) & 0x000000ff);
file_buffer[0xffff] = Convert.ToByte((chk_value >> 24) & 0x000000ff);
chk_fixed++;
}
// Checksum 2
chk_oldvalue = ((UInt32)file_buffer[0x13fff] << 24)
+ ((UInt32)file_buffer[0x13ffe] << 16)
+ ((UInt32)file_buffer[0x13ffd] << 8)
+ (UInt32)file_buffer[0x13ffc];
chk_value = tdi41_2002_checksum_calculate(file_buffer, 0x10000, 0x13ffb, 0, 0, 0x8631, 0xefcd, false);
if (chk_oldvalue != chk_value)
{
file_buffer[0x13ffc] = Convert.ToByte(chk_value & 0x000000ff);
file_buffer[0x13ffd] = Convert.ToByte((chk_value >> 8) & 0x000000ff);
file_buffer[0x13ffe] = Convert.ToByte((chk_value >> 16) & 0x000000ff);
file_buffer[0x13fff] = Convert.ToByte((chk_value >> 24) & 0x000000ff);
chk_fixed++;
}
// Checksum blocks loop
chk_store_addr = 0x4fffb;
do
{
if ((file_buffer[chk_store_addr + 13] == 0x56) && //V
(file_buffer[chk_store_addr + 14] == 0x34) && //4
(file_buffer[chk_store_addr + 15] == 0x2e) && //.
(file_buffer[chk_store_addr + 16] == 0x31)) //1
{
// Checksum
chk_start_addr = chk_store_addr - 0x3ffb;
chk_end_addr = chk_store_addr;
chk_oldvalue = ((UInt32)file_buffer[chk_store_addr + 4] << 24)
+ ((UInt32)file_buffer[chk_store_addr + 3] << 16)
+ ((UInt32)file_buffer[chk_store_addr + 2] << 8)
+ (UInt32)file_buffer[chk_store_addr + 1];
chk_value = tdi41_2002_checksum_calculate(file_buffer, chk_start_addr, chk_end_addr, seed_1_lsb, seed_1_msb, seed_1_lsb, seed_1_msb, false);
if (chk_oldvalue != chk_value)
{
file_buffer[chk_store_addr + 1] = Convert.ToByte(chk_value & 0x000000ff);
file_buffer[chk_store_addr + 2] = Convert.ToByte((chk_value >> 8) & 0x000000ff);
file_buffer[chk_store_addr + 3] = Convert.ToByte((chk_value >> 16) & 0x000000ff);
file_buffer[chk_store_addr + 4] = Convert.ToByte((chk_value >> 24) & 0x000000ff);
chk_fixed++;
}
// Checksum
chk_start_addr = chk_store_addr + 5;
chk_end_addr = chk_store_addr + 0xb80;
chk_oldvalue = ((UInt32)file_buffer[chk_store_addr + 2948] << 24)
+ ((UInt32)file_buffer[chk_store_addr + 2947] << 16)
+ ((UInt32)file_buffer[chk_store_addr + 2946] << 8)
+ (UInt32)file_buffer[chk_store_addr + 2945];
chk_value = tdi41_2002_checksum_calculate(file_buffer, chk_start_addr, chk_end_addr, seed_1_lsb, seed_1_msb, seed_1_lsb, seed_1_msb, false);
if (chk_oldvalue != chk_value)
{
file_buffer[chk_store_addr + 2945] = Convert.ToByte(chk_value & 0x000000ff);
file_buffer[chk_store_addr + 2946] = Convert.ToByte((chk_value >> 8) & 0x000000ff);
file_buffer[chk_store_addr + 2947] = Convert.ToByte((chk_value >> 16) & 0x000000ff);
file_buffer[chk_store_addr + 2948] = Convert.ToByte((chk_value >> 24) & 0x000000ff);
chk_fixed++;
}
// Checksum
chk_start_addr = chk_store_addr + 0xb85;
chk_end_addr = chk_store_addr + 0xc000;
chk_oldvalue = ((UInt32)file_buffer[chk_store_addr + 49156] << 24)
+ ((UInt32)file_buffer[chk_store_addr + 49155] << 16)
+ ((UInt32)file_buffer[chk_store_addr + 49154] << 8)
+ (UInt32)file_buffer[chk_store_addr + 49153];
chk_value = tdi41_2002_checksum_calculate(file_buffer, chk_start_addr, chk_end_addr, seed_1_lsb, seed_1_msb, seed_1_lsb, seed_1_msb, false);
if (chk_oldvalue != chk_value)
{
file_buffer[chk_store_addr + 49153] = Convert.ToByte(chk_value & 0x000000ff);
file_buffer[chk_store_addr + 49154] = Convert.ToByte((chk_value >> 8) & 0x000000ff);
file_buffer[chk_store_addr + 49155] = Convert.ToByte((chk_value >> 16) & 0x000000ff);
file_buffer[chk_store_addr + 49156] = Convert.ToByte((chk_value >> 24) & 0x000000ff);
chk_fixed++;
}
chk_found += 3;
}
chk_store_addr += 0x10000;
} while (chk_store_addr + 5 < file_size);
if (chk_fixed == 0) return ChecksumResult.ChecksumOK;
else if (chk_fixed >= 8) return ChecksumResult.ChecksumTypeError;
Console.WriteLine("chkfix: " + chk_fixed.ToString());
return ChecksumResult.ChecksumFail;
}
UInt32 tdi41_2002_checksum_calculate(byte[] file_buffer, UInt32 chk_start_addr, UInt32 chk_end_addr, UInt16 seed_a, UInt16 seed_b, UInt16 seed_c, UInt16 seed_d, bool first_pass)
{
UInt32 count = chk_start_addr / 2;
UInt32 end_count = chk_end_addr / 2;
UInt32 buffer_addr = chk_start_addr;
UInt32 checksum, var_6, var_7 = 0;
UInt16 var_1 = 0, var_2 = 0, var_3, var_4, var_5;
if (count != end_count)
{
var_1 = seed_a;
var_2 = seed_b;
if (chk_start_addr == 0x8000)
{
var_1 = Convert.ToUInt16(var_1 ^ 0xD565);
var_2 += 0x308a;
}
do
{
var_1 ^= Convert.ToUInt16(((UInt16)file_buffer[buffer_addr + 1] << 8) + (UInt16)file_buffer[buffer_addr]);
var_3 = Convert.ToUInt16(var_2 & 0xf);
++count;
buffer_addr += 2;
var_4 = 0;
if ((var_2 & 0xf) > 0)
{
do
{
var_4 = (UInt16)(var_1 >> 15);
var_1 = (UInt16)(((var_1 * 2) + var_4));
--var_3;
} while (var_3 > 0);
}
var_2 -= (UInt16)((var_4 + ((UInt16)file_buffer[buffer_addr + 1] << 8) + (UInt16)file_buffer[buffer_addr]));
var_2 = (UInt16)(var_1 ^ var_2);
buffer_addr += 2;
++count;
if (count > end_count)
break;
var_5 = (UInt16)(((UInt16)file_buffer[buffer_addr + 1] << 8) + (UInt16)file_buffer[buffer_addr]);
buffer_addr += 4;
var_1 += (UInt16)((0xffff - var_5 + 0xdaad));
var_6 = (UInt32)((UInt16)file_buffer[buffer_addr - 1] << 8);
var_2 ^= (UInt16)((UInt16)var_6 + (UInt16)file_buffer[buffer_addr - 2]);
var_4 = (UInt16)(var_1 & 0xf);
count += 2;
if ((var_1 & 0xf) > 0)
{
do
{
var_6 = (var_6 | 0xffff) & var_2;
var_6 <<= 15;
var_2 = (UInt16)(((var_2 >> 1) + var_6));
--var_4;
} while (var_4 > 0);
}
} while (count <= end_count);
}
if (chk_start_addr == 0)
{
var_1 -= 0x79cf;
var_2 -= 0x1033;
}
if (!first_pass)
{
var_5 = seed_d;
var_1 -= seed_c;
var_6 = (UInt16)((seed_c | 0xffff) & 0xdaad);
var_1 += (UInt16)(var_6 - 1);
var_7 = var_7 & 0xffff;
for (count = (UInt32)(seed_c & 0xf); count > 0; var_5 = (UInt16)((((UInt32)var_5 >> 15) + var_7)))
{
--count;
var_7 = (var_7 | 0xffff) & var_5;
var_7 *= 2;
}
checksum = (UInt32)((var_1 + ((var_5 ^ var_2) << 16)));
}
else
{
checksum = (UInt32)((var_1 + (var_2 << 16)));
}
return (checksum);
}
}
}