Skip to content

Commit

Permalink
Fixed Checksum Calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
bouletmarc committed May 30, 2022
1 parent 4982825 commit 54052ec
Show file tree
Hide file tree
Showing 11 changed files with 2,051 additions and 658 deletions.
1,252 changes: 957 additions & 295 deletions BootLoaderSumBytesList.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ClassEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ public void LoadThisECUDefinitions(string ThisECU, int ThisIndexfile)
Editortable_0.GForm_Main_0.method_1("Definitions loaded!");

//HERE
if (IsFileGenerated) DarkMessageBox.Show("This Definitions file as been generated to get the ROM Locations.\nThe ROM Locations can possibly be wrong and\nthe tables can display corrupted values!");
//if (IsFileGenerated) DarkMessageBox.Show("This Definitions file as been generated to get the ROM Locations.\nThe ROM Locations can possibly be wrong and\nthe tables can display corrupted values!");
return;
}
//}
Expand Down
35 changes: 20 additions & 15 deletions Class_Checksums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ public void Load(ref GForm_Main GForm_Main_1)
GForm_Main_0 = GForm_Main_1;
}

public byte GetNegativeChecksumArea(byte[] byte_1, int Start, int ChecksumLocation)
public byte GetChecksumArea(byte[] byte_1, int Start, int ChecksumLocation)
{
byte b = 0;
int BB = 0;
for (int i = Start; i < byte_1.Length; i++)
{
if (i != ChecksumLocation)
{
b -= byte_1[i];
BB -= byte_1[i];
if (BB < 0) BB += 255;
}
}
return b;
return (byte) BB;
}

public int GetChecksumLocationThisECU(string ThisECU)
Expand Down Expand Up @@ -104,7 +105,7 @@ public byte[] VerifyChecksumFullBin(byte[] BinFileBytes)
}

byte num = BufferBytes[CheckLocation];
byte num2 = GetNegativeChecksumArea(BufferBytes, 0, CheckLocation);
byte num2 = GetChecksumArea(BufferBytes, 0, CheckLocation);
if (num != num2)
{
GForm_Main_0.method_1("Checksum miss match.");
Expand Down Expand Up @@ -156,22 +157,26 @@ public byte[] VerifyChecksumFWBin(byte[] FWFileBytes)
}

byte num = Class_RWD.BootloaderSum;
byte num2 = Class_RWD.GetNegativeChecksumFWBin(BufferBytesForChecking, CheckLocation);
byte ThisSum = num;
ThisSum -= num2;
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("ThisSum: " + ThisSum.ToString("X"));
//Console.WriteLine("ThisCheck: " + chk.ToString("X"));
if (chk != ThisSum)
/*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] = ThisSum;
GForm_Main_0.method_1("Checksum fixed at 0x" + CheckLocation.ToString("X") + " | Checksum: 0x" + num2.ToString("X2"));
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" + num2.ToString("X2"));
GForm_Main_0.method_1("Checksum good at 0x" + CheckLocation.ToString("X") + " | Checksum: 0x" + ThisSum.ToString("X2"));
}
return BufferBytes;
}
Expand Down
48 changes: 31 additions & 17 deletions Class_DefinitionMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1649,44 +1649,58 @@ public void ExtractAllBootLoaderSum()
for (int i = 0; i < GForm_Main_0.Editortable_0.ClassEditor_0.Ecus_Definitions_Compatible.Count; i++) //Check within all ecus that has a definitions file
{
string ThisEcu = GForm_Main_0.Editortable_0.ClassEditor_0.Ecus_Definitions_Compatible[i];
string ThisEcuReducedName2 = "?????";
if (ThisEcu.Length >= 12) ThisEcuReducedName2 = ThisEcu.Substring(0, 12);

//Console.WriteLine("DEEEEE: " + ThisEcu);
//if (ThisEcu != "37805-RWC-A620") continue;

bool RWDFileFound = false;
for (int k = 0; k < AllFiles.Length; k++) //check within all RWD files
{
/*bool MatchingInside = false;
if (AllFiles[k].Contains(ThisEcuReducedName2))
{
Class_RWD.LoadRWD(AllFiles[k], false, false, false, false);
for (int m = 0; m < Class_RWD.SuppportedVersions.Length; m++)
{
if (Class_RWD.SuppportedVersions[m] == ThisEcu) MatchingInside = true;
}
}*/
Application.DoEvents();

if (AllFiles[k].Contains(ThisEcu))
//if (AllFiles[k].Contains(ThisEcu) || MatchingInside)
{
RWDFileFound = true;
//Console.WriteLine("DOING: " + ThisEcu);
Console.WriteLine("DOING: " + ThisEcu);
GForm_Main_0.ClearLogs();

//Decrypt firmware file and get needed variable (Decryption byte)
Class_RWD.LoadRWD(AllFiles[k], true, false, false, true);

byte BootSumByte = 0;
for (int m = 0; m < Class_RWD.SuppportedVersions.Length; m++)
{
if (Class_RWD.SuppportedVersions[m] == ThisEcu) //Matching ecu definition and RWD file
{
//Console.WriteLine("MATCHING!");
byte[] AllBytes = Class_RWD.firmware_candidates[m];
byte BootLoaderSumByte = Class_RWD.BootloaderSum;

if (BootLoaderSumByte == 0)
{
Console.WriteLine("ERROR BOOTSUM FOR: " + ThisEcu);

}
else
{
AllFileNames.Add(ThisEcu);
AllBootLoaderSumBytes.Add(BootLoaderSumByte);
}
BootSumByte = Class_RWD.BootloaderSum;
if (BootSumByte == 0) Console.WriteLine("ERROR BOOTSUM FOR: " + ThisEcu);

m = Class_RWD.SuppportedVersions.Length;
k = AllFiles.Length;
//k = AllFiles.Length;
}
}

if (BootSumByte != 0)
{
for (int m = 0; m < Class_RWD.SuppportedVersions.Length; m++)
{
AllFileNames.Add(Class_RWD.SuppportedVersions[m]);
AllBootLoaderSumBytes.Add(BootSumByte);
}
}

k = AllFiles.Length;
}
}
if (!RWDFileFound) Console.WriteLine("RWD NOT FOUND FOR: " + ThisEcu);
Expand Down
44 changes: 32 additions & 12 deletions Class_RWD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,16 +457,16 @@ private static void DecryptRWD(string f_name, bool Saving, bool Logs, bool Check
if (fc.Length - 1 == 0xF7FFF) CheckLocation -= 0x8000;
if (fc.Length - 1 == 0x1EFFFF || fc.Length - 1 == 0x26FFFF) CheckLocation -= 0x10000;
byte num = GetBootloaderSum(fc, CheckLocation);
byte num2 = GetNegativeChecksumFWBin(fc, CheckLocation);
byte num2 = GetChecksumFWBin(fc, CheckLocation);
int ThisSumInt = num;
ThisSumInt -= num2;
if (ThisSumInt < 0) ThisSumInt += 255;
ThisSumInt += num2;
if (ThisSumInt >= 256) ThisSumInt -= 255;
byte ThisSum = (byte)ThisSumInt;
byte chk = fc[CheckLocation];
//Console.WriteLine("chk: " + chk.ToString("X2"));
//Console.WriteLine("num2: " + num2.ToString("X2"));
//Console.WriteLine("num: " + num.ToString("X2"));
//Console.WriteLine("ThisSum: " + ThisSum.ToString("X2"));
Console.WriteLine("chk: " + chk.ToString("X2"));
Console.WriteLine("num: " + num.ToString("X2"));
Console.WriteLine("num2: " + num2.ToString("X2"));
Console.WriteLine("ThisSum: " + ThisSum.ToString("X2"));
if (chk == ThisSum)
{
GForm_Main_0.method_1("checksums good!");
Expand Down Expand Up @@ -562,14 +562,32 @@ public static byte GetBootloaderSum(byte[] FWFileBytes, int CheckLocation)
//Get Checksum (sum)
byte[] BufferBytes = FWFileBytes;
byte num = BufferBytes[CheckLocation];
byte num2 = GetNegativeChecksumFWBin(BufferBytes, CheckLocation);
byte num2 = GetChecksumFWBin(BufferBytes, CheckLocation);
//Console.WriteLine("Val1: " + num.ToString("X"));
//Console.WriteLine("Val2: " + num2.ToString("X"));
int BTSum = num;
BTSum += num2;
if (BTSum > 255) BTSum -= 255;
BTSum -= num2;
if (BTSum < 0) BTSum += 255;
return (byte) BTSum;
}

public static byte GetNegativeChecksumFWBin(byte[] byte_1, int CheckLocation)
//#############################################################################################
//#############################################################################################
/*public static byte GetChecksumFWBin(byte[] byte_1, int CheckLocation)
{
int b = 0;
for (int i = 0; i < byte_1.Length; i++)
{
if (i != CheckLocation)
{
b += byte_1[i];
if (b >= 256) b -= 255;
}
}
return (byte) b;
}*/

public static byte GetChecksumFWBin(byte[] byte_1, int CheckLocation)
{
int b = 0;
for (int i = 0; i < byte_1.Length; i++)
Expand All @@ -580,8 +598,10 @@ public static byte GetNegativeChecksumFWBin(byte[] byte_1, int CheckLocation)
if (b < 0) b += 255;
}
}
return (byte) b;
return (byte)b;
}
//#############################################################################################
//#############################################################################################

public static byte[] Push(byte[] bArray, byte[] newBytes)
{
Expand Down
Loading

0 comments on commit 54052ec

Please sign in to comment.