-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tools.cs
1005 lines (920 loc) · 38.1 KB
/
Tools.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace VAGSuite
{
public enum CarMakes : int
{
Audi,
BMW,
Volkswagen,
Unknown
}
public sealed class Tools
{
private static volatile Tools instance;
private static object syncRoot = new Object();
public EDCFileType m_currentFileType = EDCFileType.EDC15P; // default
public TransactionLog m_ProjectTransactionLog;
public string m_CurrentWorkingProject = string.Empty;
public ProjectLog m_ProjectLog = new ProjectLog();
public string m_currentfile = string.Empty;
public int m_currentfilelength = 0x80000;
public int m_codeBlock5ID = 0;
public int m_codeBlock6ID = 0;
public int m_codeBlock7ID = 0;
public CarMakes m_carMake = CarMakes.Unknown; // used for EDC17
public List<CodeBlock> codeBlockList = new List<CodeBlock>();
public SymbolCollection m_symbols = new SymbolCollection();
public List<AxisHelper> AxisList = new List<AxisHelper>();
public int TorqueToPowerkW(int torque, int rpm)
{
double power = (torque * rpm) / 7121;
// convert to kW in stead of horsepower
power *= 0.73549875;
return Convert.ToInt32(power);
}
public string ExtractBoschPartnumber(byte[] allBytes)
{
string retval = string.Empty;
try
{
int partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[5] { 0x45, 0x44, 0x43, 0x20, 0x20 }, new byte[5] { 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
// for EDC
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress + 23, 10).Trim();
if (StripNonAscii(retval).Length < 10)
{
// try again, read from "EDC" id - 0x100 to EDC id + 100 and find 10 digit sequence
retval = FindDigits(allBytes, partnumberAddress - 0x100, partnumberAddress + 0x100, 10);
}
if (retval == string.Empty)
{
// try EDC16, other partnumber struct
retval = FindAscii(allBytes, partnumberAddress - 0x100, partnumberAddress + 0x100, 11);
if (retval.StartsWith("ECM"))
{
retval = FindAscii(allBytes, partnumberAddress - 0x100, partnumberAddress + 0x100, 19);
}
}
}
else
{
partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[4] { 0x30, 0x32, 0x38, 0x31}, new byte[4] { 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress, 10).Trim();
}
}
if (retval == string.Empty)
{
partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[4] { 0x30, 0x32, 0x38, 0x31 }, new byte[4] { 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress, 10).Trim();
}
}
if (retval == string.Empty)
{
// check 512 kB EDC17 file, audi Q7 3.0TDI
partnumberAddress = Tools.Instance.findSequence(allBytes, 0, new byte[7] { 0x45, 0x44, 0x43, 0x31, 0x37, 0x20, 0x20 }, new byte[7] { 1, 1, 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = "EDC17" + System.Text.ASCIIEncoding.ASCII.GetString(allBytes, partnumberAddress - 68, 10).Trim();
}
if (retval == string.Empty)
{
//EDC17_CPx4 = Audi
//EDC17_CPx2 = BMW?
partnumberAddress = Tools.Instance.findSequence(allBytes, 0, Encoding.ASCII.GetBytes("EDC17_"), new byte[6] { 1, 1, 1, 1, 1, 1 });
if (partnumberAddress > 0)
{
retval = "EDC17";
}
}
}
if(retval == string.Empty) {
// 1. General lookup for EDC17-string in file
int pos = Tools.Instance.findSequence(allBytes, 0, Encoding.ASCII.GetBytes("ME(D)/EDC17"), new byte[11] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 });
if(pos > 0) {
Console.WriteLine("Found ME(D)/EDC17 in firmware!");
retval = "EDC17";
}
// 2. BMW has 0281xxxxxx (boschnumber) written at FD00 and FE33
// 3. BMW has 10373xxxxx (softwarenumber)written at 0x4001A, use this as a backup if boschnumber can't be found
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
bool identified = true;
// Check1
for(int offset = 0xFD00;offset < 0xFD00 + 10;offset++) {
byte b = allBytes[offset];
b -= 0x30;
if(b < 0 || b > 9)
identified = false;
sb1.Append(b.ToString());
}
for(int offset = 0xFE33;offset < 0xFE33 + 10;offset++) {
byte b = allBytes[offset];
b -= 0x30;
if(b < 0 || b > 9)
identified = false;
sb2.Append(b.ToString());
}
//Console.WriteLine("sb1=" + sb1.ToString());
//Console.WriteLine("sb2=" + sb2.ToString());
if(identified && sb1.ToString() == sb2.ToString()) {
partnumberAddress = 0xFD00;
retval = sb1.ToString();
}
StringBuilder sb3 = new StringBuilder();
identified = true;
// Check2
for(int offset = 0x4001A;offset < 0x4001A + 10;offset++) {
byte b = allBytes[offset];
b -= 0x30;
if(b < 0 || b > 9)
identified = false;
sb3.Append(b.ToString());
}
//Console.WriteLine("sb3=" + sb3.ToString());
if(identified) {
partnumberAddress = 0x4001A;
retval = sb3.ToString();
}
}
if (retval.StartsWith("EDC17"))
{
if (Tools.Instance.findSequence(allBytes, 0, Encoding.ASCII.GetBytes("EDC17_CPx4"), new byte[10] { 1, 1, 1, 1, 1, 1, 1, 1, 0, 1 }) > 0)
{
Tools.Instance.m_carMake = CarMakes.Audi;
}
else if (Tools.Instance.findSequence(allBytes, 0, Encoding.ASCII.GetBytes("EDC17_CPx2"), new byte[10] { 1, 1, 1, 1, 1, 1, 1, 1, 0, 1 }) > 0)
{
Tools.Instance.m_carMake = CarMakes.BMW;
}
else if (Tools.Instance.findSequence(allBytes, 0, Encoding.ASCII.GetBytes("EDC17_C06"), new byte[9] { 1, 1, 1, 1, 1, 1, 1, 1, 1 }) > 0)
{
Tools.Instance.m_carMake = CarMakes.BMW;
}
}
}
catch (Exception)
{
}
retval = StripNonAscii(retval);
return retval;
}
private string FindAscii(byte[] allBytes, int start, int end, int length)
{
for (int i = start; i < end; i++)
{
string testStr = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, i, length);
testStr = StripNonAsciiCapital(testStr);
if (testStr.Length == length) return testStr;
}
return "";
}
private string FindDigits(byte[] allBytes, int start, int end, int length)
{
for (int i = start; i < end; i++)
{
string testStr = System.Text.ASCIIEncoding.ASCII.GetString(allBytes, i, length);
testStr = StripNonDigit(testStr);
if (testStr.Length == length) return testStr;
}
return "";
}
private string StripNonAsciiCapital(string input)
{
string retval = string.Empty;
foreach (char c in input)
{
if (c >= 0x30 && c <= 0x39) retval += c;
if (c >= 0x41 && c <= 0x5A) retval += c;
}
return retval;
}
private string StripNonDigit(string input)
{
string retval = string.Empty;
foreach (char c in input)
{
if (c >= 0x30 && c <= 0x39) retval += c;
}
return retval;
}
public string StripNonAscii(string input)
{
string retval = string.Empty;
foreach (char c in input)
{
if (c >= 0x30 && c <= 0x39) retval += c;
else if (c >= 0x41 && c <= 0x5A) retval += c;
else if (c >= 0x61 && c <= 0x7A) retval += c;
}
return retval;
}
public EDCFileType DetermineFileType(string fileName, bool isPrimaryFile)
{
byte[] allBytes = File.ReadAllBytes(fileName);
string boschnumber = ExtractBoschPartnumber(allBytes);
//Console.WriteLine("Bosch number: " + boschnumber);
partNumberConverter pnc = new partNumberConverter();
ECUInfo info = pnc.ConvertPartnumber(boschnumber, allBytes.Length);
if (info.EcuType.Contains("EDC15P-6"))
{
if (isPrimaryFile) m_currentFileType = EDCFileType.EDC15P6;
return EDCFileType.EDC15P6;
}
else if (info.EcuType.Contains("EDC15P"))
{
if (isPrimaryFile) m_currentFileType = EDCFileType.EDC15P;
return EDCFileType.EDC15P;
}
else if (info.EcuType.Contains("EDC15M"))
{
if (isPrimaryFile) m_currentFileType = EDCFileType.EDC15M;
return EDCFileType.EDC15M;
}
else if (info.EcuType.Contains("MSA15") || info.EcuType.Contains("EDC15V-5"))
{
if (isPrimaryFile) m_currentFileType = EDCFileType.MSA15;
return EDCFileType.MSA15;
}
else if (info.EcuType.Contains("MSA12"))
{
if (isPrimaryFile) m_currentFileType = EDCFileType.MSA12;
return EDCFileType.MSA12;
}
else if (info.EcuType.Contains("MSA11"))
{
if (isPrimaryFile) m_currentFileType = EDCFileType.MSA11;
return EDCFileType.MSA11;
}
else if (info.EcuType.Contains("MSA6"))
{
if (isPrimaryFile) m_currentFileType = EDCFileType.MSA6;
return EDCFileType.MSA6;
}
else if (info.EcuType.Contains("EDC15V"))
{
if (isPrimaryFile) m_currentFileType = EDCFileType.EDC15V;
return EDCFileType.EDC15V;
}
if (info.EcuType.Contains("EDC15C"))
{
if (isPrimaryFile) m_currentFileType = EDCFileType.EDC15C;
return EDCFileType.EDC15C;
}
else if (info.EcuType.Contains("EDC16"))
{
if (isPrimaryFile) m_currentFileType = EDCFileType.EDC16;
return EDCFileType.EDC16;
}
else if (info.EcuType.Contains("EDC17"))
{
if (isPrimaryFile) m_currentFileType = EDCFileType.EDC17;
return EDCFileType.EDC17;
}
else if (IsEDC16Partnumber(boschnumber))
{
if (isPrimaryFile) m_currentFileType = EDCFileType.EDC16;
return EDCFileType.EDC16;
}
else if (boschnumber != string.Empty)
{
if (allBytes.Length == 1024 * 1024 * 2)
{
if (isPrimaryFile) m_currentFileType = EDCFileType.EDC17;
return EDCFileType.EDC17;
}
else if (boschnumber.StartsWith("EDC17"))
{
if (isPrimaryFile) m_currentFileType = EDCFileType.EDC17;
return EDCFileType.EDC17;
}
else
{
if (isPrimaryFile) m_currentFileType = EDCFileType.EDC15V;
return EDCFileType.EDC15V;
}
}
else
{
if (isPrimaryFile) m_currentFileType = EDCFileType.EDC16;
return EDCFileType.EDC16; // default to EDC16???
}
}
public bool IsEDC16Partnumber(string partnumber)
{
//03G906021AB
if(partnumber.Length != 11) return false;
if(!isDigit(partnumber[0])) return false;
if(!isDigit(partnumber[1])) return false;
if(!isLetter(partnumber[2])) return false;
if(!isDigit(partnumber[3])) return false;
if(!isDigit(partnumber[4])) return false;
if(!isDigit(partnumber[5])) return false;
if(!isDigit(partnumber[6])) return false;
if(!isDigit(partnumber[7])) return false;
if(!isDigit(partnumber[8])) return false;
if(!isLetter(partnumber[9])) return false;
if(!isLetter(partnumber[10])) return false;
return true;
}
private bool isLetter(char c)
{
if (c >= 0x41 && c <= 0x5A) return true;
return false;
}
private bool isDigit(char c)
{
if (c >= 0x30 && c <= 0x39) return true;
return false;
}
public IEDCFileParser GetParserForFile(string filename, bool isPrimaryFile)
{
IEDCFileParser parser = null;
EDCFileType fileType = DetermineFileType(filename, isPrimaryFile);
switch (fileType)
{
case EDCFileType.EDC15P:
parser = new EDC15PFileParser();
break;
case EDCFileType.EDC15P6:
parser = new EDC15P6FileParser();
break;
case EDCFileType.EDC15V:
parser = new EDC15VFileParser();
break;
case EDCFileType.EDC15C:
parser = new EDC15CFileParser();
break;
case EDCFileType.EDC15M:
parser = new EDC15MFileParser();
break;
case EDCFileType.EDC16:
parser = new EDC16FileParser();
break;
case EDCFileType.EDC17:
parser = new EDC17FileParser();
break;
case EDCFileType.MSA15: //?
case EDCFileType.MSA12:
case EDCFileType.MSA11:
parser = new MSA15FileParser();
break;
case EDCFileType.MSA6:
parser = new MSA6FileParser();
break;
}
return parser;
}
public int PowerToTorque(int power, int rpm)
{
double torque = (power * 7121) / rpm;
return Convert.ToInt32(torque);
}
public int TorqueToPower(int torque, int rpm)
{
double power = (torque * rpm) / 7121;
return Convert.ToInt32(power);
}
public double GetCorrectionFactorForRpm(int rpm, int numberCylinders)
{
double correction = 1;
if (numberCylinders == 6)
{
if (rpm >= 4000) correction = 0.80;
else if (rpm >= 3500) correction = 0.90;
else if (rpm >= 3250) correction = 0.90;
else if (rpm >= 3000) correction = 0.93;
else if (rpm >= 2500) correction = 0.90;
else if (rpm >= 2250) correction = 0.90;
else if (rpm >= 1700) correction = 0.90;
else correction = 0.9;
}
else
{
if (rpm >= 4000) correction = 0.75;
else if (rpm >= 3500) correction = 0.83;
else if (rpm >= 3250) correction = 0.89;
else if (rpm >= 3000) correction = 0.96;
else if (rpm >= 2500) correction = 0.98;
else if (rpm >= 2250) correction = 0.99;
else correction = 1.00;
}
return correction;
}
public int IQToTorque(int IQ, int rpm, int numberCylinders)
{
double tq = Convert.ToDouble(IQ) * 6;
// correct for number of cylinders
tq *= numberCylinders;
tq /= 4;
double correction = GetCorrectionFactorForRpm(rpm, numberCylinders);
tq *= correction;
return Convert.ToInt32(tq);
}
public int TorqueToIQ(int torque, int rpm, int numberCylinders)
{
double iq = Convert.ToDouble(torque) / 6;
// correct for number of cylinders
iq *= 4;
iq /= numberCylinders;
double correction = GetCorrectionFactorForRpm(rpm, numberCylinders);
iq /= correction;
return Convert.ToInt32(iq);
}
public Int64 GetSymbolAddressLike(SymbolCollection curSymbolCollection, string symbolname)
{
foreach (SymbolHelper sh in curSymbolCollection)
{
if (sh.Varname.StartsWith(symbolname) || sh.Userdescription.StartsWith(symbolname))
{
return sh.Flash_start_address;
}
}
return 0;
}
public static Tools Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
instance = new Tools();
}
}
}
return instance;
}
}
public string GetWorkingDirectory()
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "VAGEDCSuite");
}
public string GetSymbolNameByAddress(Int32 address)
{
foreach (SymbolHelper sh in m_symbols)
{
if (sh.Flash_start_address == address) return sh.Varname;
}
return address.ToString();
}
public string GetSymbolNameByAddressInRange(Int32 address, Int32 addressTo)
{
foreach (SymbolHelper sh in m_symbols)
{
if (sh.Flash_start_address <= address && (sh.Flash_start_address + sh.Length) >= address) return sh.Varname;
if (sh.Flash_start_address <= addressTo && (sh.Flash_start_address + sh.Length) >= addressTo) return sh.Varname;
if (sh.Flash_start_address >= address && (sh.Flash_start_address + sh.Length) <= addressTo) return sh.Varname;
}
return string.Empty;
}
public void savedatatobinary(int address, int length, byte[] data, string filename, bool DoTransActionEntry, string note, EDCFileType type)
{
// depends on filetype (EDC16 is not reversed)
if (type != EDCFileType.EDC16)
{
data = reverseEndian(data);
}
if (address > 0 && address < m_currentfilelength)
{
try
{
byte[] beforedata = readdatafromfile(filename, address, length, type);
FileStream fsi1 = File.OpenWrite(filename);
BinaryWriter bw1 = new BinaryWriter(fsi1);
fsi1.Position = address;
for (int i = 0; i < length; i++)
{
bw1.Write((byte)data.GetValue(i));
}
fsi1.Flush();
bw1.Close();
fsi1.Close();
fsi1.Dispose();
if (m_ProjectTransactionLog != null && DoTransActionEntry)
{
// depends on filetype (EDC16 is not reversed)
if (type != EDCFileType.EDC16)
{
data = reverseEndian(data);
}
TransactionEntry tentry = new TransactionEntry(DateTime.Now, address, length, beforedata, data, 0, 0, note);
m_ProjectTransactionLog.AddToTransactionLog(tentry);
if (m_CurrentWorkingProject != string.Empty)
{
m_ProjectLog.WriteLogbookEntry(LogbookEntryType.TransactionExecuted, GetSymbolNameByAddress(address) + " " + note);
}
}
}
catch (Exception E)
{
frmInfoBox info = new frmInfoBox("Failed to write to binary. Is it read-only? Details: " + E.Message);
}
}
}
public int findSequence(byte[] fileData, int offset, byte[] sequence, byte[] mask)
{
byte data;
int i, max;
i = 0;
max = 0;
int position = offset;
while (position < fileData.Length)
{
data = (byte)fileData[position++];
if (data == sequence[i] || mask[i] == 0)
{
i++;
}
else
{
if (i > max) max = i;
position -= i;
i = 0;
}
if (i == sequence.Length) break;
}
if (i == sequence.Length)
{
return ((int)position - sequence.Length);
}
else
{
return -1;
}
}
public ChecksumResultDetails UpdateChecksum(string filename, bool verifyOnly)
{
byte[] allBytes;
ChecksumResult res = new ChecksumResult(); ;
ChecksumResultDetails result = new ChecksumResultDetails();
EDCFileType fileType = DetermineFileType(filename, false);
switch (fileType)
{
case EDCFileType.EDC15P:
case EDCFileType.EDC15P6:
allBytes = File.ReadAllBytes(filename);
res = CalculateEDC15PChecksum(filename, allBytes, verifyOnly, out result);
break;
case EDCFileType.EDC15V:
// EDC15VM+ is similar to EDC15P
allBytes = File.ReadAllBytes(filename);
res = CalculateEDC15VMChecksum(filename, allBytes, verifyOnly, out result);
break;
case EDCFileType.EDC15C:
//TODO: Implement EDC15C checksum routine here
break;
case EDCFileType.EDC15M:
//TODO: Implement EDC15M checksum routine here
break;
case EDCFileType.EDC16:
//TODO: Implement EDC16x checksum routine here
break;
case EDCFileType.EDC17:
//TODO: Implement EDC17x checksum routine here
break;
case EDCFileType.MSA15:
case EDCFileType.MSA12:
case EDCFileType.MSA11:
//TODO: Implement MSA15 checksum routine here
// this should be Bosch TDI V3.1 (Version 2.04)
/* result.TypeResult = ChecksumType.VAG_EDC15P_V41;
allBytes = File.ReadAllBytes(filename);
//allBytes = reverseEndian(allBytes);
MSA15_checksum msa15chks = new MSA15_checksum();
res = msa15chks.tdi41_checksum_search(allBytes, (uint)allBytes.Length, false);
result.NumberChecksumsTotal = msa15chks.ChecksumsFound;
result.NumberChecksumsFail = msa15chks.ChecksumsIncorrect;
result.NumberChecksumsOk = msa15chks.ChecksumsFound - msa15chks.ChecksumsIncorrect;
if (res == ChecksumResult.ChecksumOK)
{
//Console.WriteLine("Checksum matched");
result.CalculationOk = true;
}
else if (res == ChecksumResult.ChecksumFail)
{
Console.WriteLine("UpdateChecksum: Checksum failed " + filename);
if (!verifyOnly)
{
File.WriteAllBytes(filename, allBytes);
result.CalculationOk = true;
Console.WriteLine("UpdateChecksum: Checksum fixed");
}
}*/
break;
case EDCFileType.MSA6:
//TODO: Implement MSA6 checksum routine here
break;
}
if (result.CalculationOk) result.CalculationResult = ChecksumResult.ChecksumOK;
return result;
}
private ChecksumResult CalculateEDC15PChecksum(string filename, byte[] allBytes, bool verifyOnly, out ChecksumResultDetails result)
{
ChecksumResult res = new ChecksumResult();
// checksum for EDC15P is implemented
result = new ChecksumResultDetails();
result.CalculationResult = ChecksumResult.ChecksumFail; // default = failed
result.TypeResult = ChecksumType.VAG_EDC15P_V41;
if (allBytes.Length != 0x80000) return res;
if (allBytes[0x50008] == 'V' && allBytes[0x50009] == '4' && allBytes[0x5000A] == '.' && allBytes[0x5000B] == '1')
{
// checksum V4.1 rev.1
result.TypeResult = ChecksumType.VAG_EDC15P_V41;
}
else if (allBytes[0x58008] == 'V' && allBytes[0x58009] == '4' && allBytes[0x5800A] == '.' && allBytes[0x5800B] == '1')
{
// checksum V4.1 rev.2
result.TypeResult = ChecksumType.VAG_EDC15P_V41V2;
}
//allBytes = reverseEndian(allBytes);
EDC15P_checksum chks = new EDC15P_checksum();
if (result.TypeResult == ChecksumType.VAG_EDC15P_V41)
{
res = chks.tdi41_checksum_search(allBytes, (uint)allBytes.Length, false);
}
else
{
res = chks.tdi41v2_checksum_search(allBytes, (uint)allBytes.Length, false);
}
result.NumberChecksumsTotal = chks.ChecksumsFound;
result.NumberChecksumsFail = chks.ChecksumsIncorrect;
result.NumberChecksumsOk = chks.ChecksumsFound - chks.ChecksumsIncorrect;
if (res == ChecksumResult.ChecksumOK)
{
Console.WriteLine("Checksum V4.1 matched");
result.CalculationOk = true;
}
else if (res == ChecksumResult.ChecksumFail)
{
Console.WriteLine("UpdateChecksum: Checksum failed " + filename);
if (!verifyOnly)
{
File.WriteAllBytes(filename, allBytes);
result.CalculationOk = true;
Console.WriteLine("UpdateChecksum: Checksum fixed");
}
}
else if (res == ChecksumResult.ChecksumTypeError)
{
result.TypeResult = ChecksumType.VAG_EDC15P_V41_2002;
EDC15P_checksum chks2002 = new EDC15P_checksum();
allBytes = File.ReadAllBytes(filename);
//chks2002.DumpChecksumLocations("V41 2002", allBytes); // for debug info only
ChecksumResult res2002 = chks2002.tdi41_2002_checksum_search(allBytes, (uint)allBytes.Length, false);
result.NumberChecksumsTotal = chks2002.ChecksumsFound;
result.NumberChecksumsFail = chks2002.ChecksumsIncorrect;
result.NumberChecksumsOk = chks2002.ChecksumsFound - chks2002.ChecksumsIncorrect;
if (res2002 == ChecksumResult.ChecksumOK)
{
Console.WriteLine("Checksum 2002 matched " + filename);
result.CalculationOk = true;
}
else if (res2002 == ChecksumResult.ChecksumFail)
{
Console.WriteLine("UpdateChecksum: Checksum 2002 failed " + filename);
if (!verifyOnly)
{
File.WriteAllBytes(filename, allBytes);
result.CalculationOk = true;
Console.WriteLine("UpdateChecksum: Checksum fixed");
}
}
else if (res2002 == ChecksumResult.ChecksumTypeError)
{
// unknown checksum type
result.CalculationOk = false;
result.CalculationResult = ChecksumResult.ChecksumTypeError;
result.TypeResult = ChecksumType.Unknown;
}
}
return res;
}
private ChecksumResult CalculateEDC15VMChecksum(string filename, byte[] allBytes, bool verifyOnly, out ChecksumResultDetails result)
{
ChecksumResult res = new ChecksumResult();
result = new ChecksumResultDetails();
result.CalculationResult = ChecksumResult.ChecksumFail; // default = failed
result.TypeResult = ChecksumType.VAG_EDC15VM_V41;
if (/*allBytes.Length != 0x40000 && */allBytes.Length != 0x80000 && allBytes.Length != 0x100000) return res;
if (allBytes.Length >= 0x80000)
{
if (allBytes[0x50008] == 'V' && allBytes[0x50009] == '4' && allBytes[0x5000A] == '.' && allBytes[0x5000B] == '1')
{
// checksum V4.1 rev.1
result.TypeResult = ChecksumType.VAG_EDC15VM_V41;
}
else if (allBytes[0x58008] == 'V' && allBytes[0x58009] == '4' && allBytes[0x5800A] == '.' && allBytes[0x5800B] == '1')
{
// checksum V4.1 rev.2
result.TypeResult = ChecksumType.VAG_EDC15VM_V41V2;
}
}
//allBytes = reverseEndian(allBytes);
EDC15VM_checksum chks = new EDC15VM_checksum();
if (result.TypeResult == ChecksumType.VAG_EDC15VM_V41)
{
res = chks.tdi41_checksum_search(allBytes, (uint)allBytes.Length, false);
}
else
{
res = chks.tdi41v2_checksum_search(allBytes, (uint)allBytes.Length, false);
}
result.NumberChecksumsTotal = chks.ChecksumsFound;
result.NumberChecksumsFail = chks.ChecksumsIncorrect;
result.NumberChecksumsOk = chks.ChecksumsFound - chks.ChecksumsIncorrect;
if (res == ChecksumResult.ChecksumOK)
{
Console.WriteLine("Checksum V4.1 matched");
result.CalculationOk = true;
}
else if (res == ChecksumResult.ChecksumFail)
{
Console.WriteLine("UpdateChecksum: Checksum failed " + filename);
if (!verifyOnly)
{
File.WriteAllBytes(filename, allBytes);
result.CalculationOk = true;
Console.WriteLine("UpdateChecksum: Checksum fixed");
}
}
else if (res == ChecksumResult.ChecksumTypeError)
{
result.TypeResult = ChecksumType.VAG_EDC15VM_V41_2002;
EDC15VM_checksum chks2002 = new EDC15VM_checksum();
allBytes = File.ReadAllBytes(filename);
ChecksumResult res2002 = chks2002.tdi41_2002_checksum_search(allBytes, (uint)allBytes.Length, false);
result.NumberChecksumsTotal = chks2002.ChecksumsFound;
result.NumberChecksumsFail = chks2002.ChecksumsIncorrect;
result.NumberChecksumsOk = chks2002.ChecksumsFound - chks2002.ChecksumsIncorrect;
if (res2002 == ChecksumResult.ChecksumOK)
{
Console.WriteLine("Checksum 2002 matched " + filename);
result.CalculationOk = true;
}
else if (res2002 == ChecksumResult.ChecksumFail)
{
Console.WriteLine("UpdateChecksum: Checksum 2002 failed " + filename);
if (!verifyOnly)
{
File.WriteAllBytes(filename, allBytes);
result.CalculationOk = true;
Console.WriteLine("UpdateChecksum: Checksum fixed");
}
}
else if (res2002 == ChecksumResult.ChecksumTypeError)
{
// unknown checksum type
result.CalculationOk = false;
result.CalculationResult = ChecksumResult.ChecksumTypeError;
result.TypeResult = ChecksumType.Unknown;
}
}
return res;
}
public void savedatatobinary(int address, int length, byte[] data, string filename, bool DoTransActionEntry, EDCFileType type)
{
// depends on filetype (EDC16 is not reversed)
if (type != EDCFileType.EDC16)
{
data = reverseEndian(data);
}
if (address > 0 && address < Tools.Instance.m_currentfilelength)
{
try
{
byte[] beforedata = readdatafromfile(filename, address, length, type);
FileStream fsi1 = File.OpenWrite(filename);
BinaryWriter bw1 = new BinaryWriter(fsi1);
fsi1.Position = address;
for (int i = 0; i < length; i++)
{
bw1.Write((byte)data.GetValue(i));
}
fsi1.Flush();
bw1.Close();
fsi1.Close();
fsi1.Dispose();
}
catch (Exception E)
{
// MessageBox.Show("Failed to write to binary. Is it read-only? Details: " + E.Message);
}
}
}
public int[] readdatafromfileasint(string filename, int address, int length, EDCFileType type)
{
int[] retval = new int[length];
FileStream fsi1 = File.OpenRead(filename);
while (address > fsi1.Length) address -= (int)fsi1.Length;
BinaryReader br1 = new BinaryReader(fsi1);
fsi1.Position = address;
string temp = string.Empty;
for (int i = 0; i < length; i++)
{
if (type != EDCFileType.EDC16)
{
int iVal = Convert.ToInt32(br1.ReadByte());
iVal += Convert.ToInt32(br1.ReadByte()) * 256;
retval.SetValue(iVal, i);
}
else
{
int iVal = Convert.ToInt32(br1.ReadByte()) * 256;
iVal += Convert.ToInt32(br1.ReadByte());
retval.SetValue(iVal, i);
}
}
// little endian, reverse bytes
//retval = reverseEndian(retval);
fsi1.Flush();
br1.Close();
fsi1.Close();
fsi1.Dispose();
return retval;
}
public byte[] reverseEndian(byte[] retval)
{
byte[] ret = new byte[retval.Length];
try
{
if (retval.Length > 0 && retval.Length % 2 == 0)
{
for (int i = 0; i < retval.Length; i += 2)
{
byte b1 = retval[i];
byte b2 = retval[i + 1];
ret[i] = b2;
ret[i + 1] = b1;
}
}
}
catch (Exception E)
{
}
return ret;
}
public int[] reverseEndian(int[] retval)
{
int[] ret = new int[retval.Length];
try
{
if (retval.Length > 0 && retval.Length % 2 == 0)
{
for (int i = 0; i < retval.Length; i += 2)
{
int b1 = retval[i];
int b2 = retval[i + 1];
ret[i] = b2;
ret[i + 1] = b1;
}
}
}
catch (Exception E)
{
}
return ret;
}
public byte[] readdatafromfile(string filename, int address, int length, EDCFileType type)
{
byte[] retval = new byte[length];
try
{
FileStream fsi1 = File.OpenRead(filename);
while (address > fsi1.Length) address -= (int)fsi1.Length;
BinaryReader br1 = new BinaryReader(fsi1);
fsi1.Position = address;
string temp = string.Empty;
for (int i = 0; i < length; i++)
{
retval.SetValue(br1.ReadByte(), i);
}
// depends on filetype (EDC16 is not reversed)
if (type != EDCFileType.EDC16)
{
retval = reverseEndian(retval);
}
fsi1.Flush();
br1.Close();
fsi1.Close();
fsi1.Dispose();
}
catch (Exception E)
{
Console.WriteLine(E.Message);