public tdi41_checksum_search ( byte file_buffer, |
||
file_buffer | byte | |
file_size | ||
debug | bool | |
return | ChecksumResult |
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;
// 58000 was not there
UInt32[] chk_array = new UInt32[12] { 0x10000, 0x14000, 0x4C000, 0x50000, 0x50B80, 0x5C000, 0x60000, 0x60B80, 0x6C000, 0x70000, 0x70B80, 0x7C000 };
UInt16 seed_a = 0, seed_b = 0;
chk_found = 0;
chk_fixed = 0;
chk_match = 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];
//DumpChecksum("chks " + chk_start_addr.ToString("X8") + "-" + chk_end_addr.ToString("X8"), file_buffer, debug);
if (!first_pass)
{
seed_a |= 0x8631;
seed_b |= 0xEFCD;
}
//if (CheckEmpty(file_buffer, chk_start_addr, chk_end_addr)) continue;
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("Checksum at address " + chk_end_addr.ToString("X8") + " - " + chk_end_addr.ToString("X8") + ", file: " + chk_oldvalue.ToString("X8") + " calculated: " + 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, file: " + chk_oldvalue.ToString("X8") + " calculated: " + chk_value.ToString("X8"));
chk_fixed++;
}
if (chk_oldvalue == chk_value)
{
chk_match++;
}
first_pass = false;
}
Console.WriteLine("tdi41_chkfixed: " + chk_fixed.ToString() + " / " + chk_found.ToString());
Console.WriteLine("tdi41_chkmatch: " + chk_match.ToString() + " / " + chk_found.ToString());
//return ChecksumResult.ChecksumTypeError;
if (chk_fixed == 0) return ChecksumResult.ChecksumOK;
else if (chk_match > 3) return ChecksumResult.ChecksumFail;
else if (chk_fixed >= 6) return ChecksumResult.ChecksumTypeError;
return ChecksumResult.ChecksumFail;
}
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); }