-
Notifications
You must be signed in to change notification settings - Fork 8
/
Class_Checksums.cs
183 lines (161 loc) · 7.31 KB
/
Class_Checksums.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
using System;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DarkUI.Forms;
public class Class_Checksums
{
private GForm_Main GForm_Main_0;
public int LastChecksumLocationLoaded = 0;
public string LastChecksumECUName = "";
public void Load(ref GForm_Main GForm_Main_1)
{
GForm_Main_0 = GForm_Main_1;
}
public byte GetChecksumArea(byte[] byte_1, int Start, int ChecksumLocation)
{
int BB = 0;
for (int i = Start; i < byte_1.Length; i++)
{
if (i != ChecksumLocation)
{
BB -= byte_1[i];
if (BB < 0) BB += 255;
}
}
return (byte) BB;
}
public int GetChecksumLocationThisECU(string ThisECU)
{
int CheckLocation = 0;
if (GForm_Main_0.Editortable_0.LoadDefinitionsFor(ThisECU))
{
if (GForm_Main_0.Editortable_0.ClassEditor_0.DefinitionsChecksumLocation != "")
{
CheckLocation = int.Parse(GForm_Main_0.Editortable_0.ClassEditor_0.DefinitionsChecksumLocation.Replace("0x", ""), System.Globalization.NumberStyles.HexNumber);
}
}
return CheckLocation;
}
public int GetChecksumLocation(byte[] BinFileBytes)
{
byte[] BufferBytes = BinFileBytes;
int CheckLocation = 0;
string Thisecuu = GForm_Main_0.Editortable_0.ExtractECUNameFromThisFile(BinFileBytes);
if (Thisecuu != "")
{
if (Thisecuu == LastChecksumECUName)
{
return LastChecksumLocationLoaded;
}
if (GForm_Main_0.Editortable_0.LoadDefinitionsFor(Thisecuu))
{
if (GForm_Main_0.Editortable_0.ClassEditor_0.DefinitionsChecksumLocation != "")
{
CheckLocation = int.Parse(GForm_Main_0.Editortable_0.ClassEditor_0.DefinitionsChecksumLocation.Replace("0x", ""), System.Globalization.NumberStyles.HexNumber);
}
}
}
if (CheckLocation == 0)
{
//HERE
//GForm_Main_0.method_1("Checksum location not definied for '" + Thisecuu + "', using known checksum location but can possibly be wrong on some ecu!");
DialogResult result = DarkMessageBox.Show("Checksum location not definied for '" + Thisecuu + "'" + Environment.NewLine + "Do you want to use 'known good' checksum location but they still can possibly be wrong on some ecu?", "Checksum location", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
if (result == DialogResult.Yes)
{
if (BufferBytes.Length - 1 == 0xF7FFF) CheckLocation = 0x8400; //1mb-fw -> 0x8400 in full bin but we dont have the bootloader 0x0000 to 0x8000
if (BufferBytes.Length - 1 == 0xFFFFF) CheckLocation = 0x8400; //1mb-full
if (BufferBytes.Length - 1 == 0x1EFFFF) CheckLocation = 0x10012; //2mb-fw
if (BufferBytes.Length - 1 == 0x1FFFFF) CheckLocation = 0x10012; //2mb-full
if (BufferBytes.Length - 1 == 0x26FFFF) CheckLocation = 0x2003E6; //4mb-fw
if (BufferBytes.Length - 1 == 0x27FFFF) CheckLocation = 0x2003E6; //4mb-full //0x3FFFFF
}
}
LastChecksumECUName = Thisecuu;
LastChecksumLocationLoaded = CheckLocation;
return CheckLocation;
}
public byte[] VerifyChecksumFullBin(byte[] BinFileBytes)
{
//###############################
//Get Checksum and Fix it
byte[] BufferBytes = BinFileBytes;
int CheckLocation = GetChecksumLocation(BinFileBytes);
if (CheckLocation == 0)
{
GForm_Main_0.method_1("Checksum location not found!");
return BufferBytes;
}
byte num = BufferBytes[CheckLocation];
byte num2 = GetChecksumArea(BufferBytes, 0, CheckLocation);
if (num != num2)
{
GForm_Main_0.method_1("Checksum miss match.");
BufferBytes[CheckLocation] = num2;
GForm_Main_0.method_1("Checksum fixed at 0x" + CheckLocation.ToString("X") + " | Checksum: 0x" + num2.ToString("X2"));
}
else
{
GForm_Main_0.method_1("Checksum are good at 0x" + CheckLocation.ToString("X") + " | Checksum: 0x" + num2.ToString("X2"));
}
return BufferBytes;
}
public byte[] VerifyChecksumFWBin(byte[] FWFileBytes)
{
//###############################
//Get Checksum and Fix it
byte[] BufferBytes = FWFileBytes;
int CheckLocation = GetChecksumLocation(FWFileBytes);
if (CheckLocation == 0)
{
GForm_Main_0.method_1("Checksum location not found!");
return BufferBytes;
}
int CheckLocationInBIN = CheckLocation;
if (FWFileBytes.Length - 1 == 0xF7FFF || FWFileBytes.Length - 1 == 0xFFFFF) CheckLocation -= 0x8000;
if (FWFileBytes.Length - 1 == 0x1EFFFF || FWFileBytes.Length - 1 == 0x1FFFFF ||
FWFileBytes.Length - 1 == 0x26FFFF || FWFileBytes.Length - 1 == 0x27FFFF) CheckLocation -= 0x10000;
if (FWFileBytes.Length - 1 == 0xF7FFF) CheckLocationInBIN -= 0x8000;
if (FWFileBytes.Length - 1 == 0x1EFFFF || FWFileBytes.Length - 1 == 0x1FFFFF) CheckLocationInBIN -= 0x10000;
//Console.WriteLine("Checksum location: " + CheckLocation.ToString("X"));
//Console.WriteLine("Checksum locationin bin: " + CheckLocationInBIN.ToString("X"));
//Remake/remove bootloader section in case the bytes array is a full binary
byte[] BufferBytesForChecking = new byte[] { };
if (FWFileBytes.Length - 1 == 0xFFFFF)
{
BufferBytesForChecking = new byte[FWFileBytes.Length - 0x8000];
for (int i = 0; i < BufferBytesForChecking.Length; i++) BufferBytesForChecking[i] = FWFileBytes[i + 0x8000];
}
if (FWFileBytes.Length - 1 == 0x26FFFF || FWFileBytes.Length - 1 == 0x27FFFF)
{
BufferBytesForChecking = new byte[FWFileBytes.Length - 0x10000];
for (int i = 0; i < BufferBytesForChecking.Length; i++) BufferBytesForChecking[i] = FWFileBytes[i + 0x10000];
}
byte num = Class_RWD.BootloaderSum;
byte num2 = Class_RWD.GetChecksumFWBin(BufferBytesForChecking, CheckLocation);
int ThisSum = num;
ThisSum += num2;
if (ThisSum >= 256) ThisSum -= 255;
byte chk = BufferBytes[CheckLocationInBIN];
/*Console.WriteLine("Bootsum: " + num.ToString("X"));
Console.WriteLine("FileSum: " + num2.ToString("X"));
Console.WriteLine("ThisSum: " + ThisSum.ToString("X"));
Console.WriteLine("ThisCheck: " + chk.ToString("X"));*/
if (chk != (byte) ThisSum)
{
GForm_Main_0.method_1("Checksum miss match.");
BufferBytes[CheckLocationInBIN] = (byte) ThisSum;
GForm_Main_0.method_1("Checksum fixed at 0x" + CheckLocation.ToString("X") + " | Checksum: 0x" + ThisSum.ToString("X2"));
}
else
{
GForm_Main_0.method_1("Checksum good at 0x" + CheckLocation.ToString("X") + " | Checksum: 0x" + ThisSum.ToString("X2"));
}
return BufferBytes;
}
}