Skip to content

Commit

Permalink
Add P08 Checksum Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
antus committed Apr 27, 2023
1 parent c8c25ff commit 168bda0
Showing 1 changed file with 74 additions and 28 deletions.
102 changes: 74 additions & 28 deletions Apps/PcmLibrary/Misc/FileValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,36 +222,31 @@ public uint GetOsidFromImage()
private bool ValidateChecksums()
{
bool success = true;
UInt32 tableAddress;
UInt32 tableAddress = 0;
UInt32 segments = 0;

PcmType type = this.ValidateSignatures();

switch (type)
{
// have a segment table
case PcmType.P01_P59:
tableAddress = 0x50C;
segments = 8;
break;

case PcmType.P04:
tableAddress = 0x0;
segments = 0;
break;

case PcmType.P05:
tableAddress = 0x0;
segments = 0;
break;

case PcmType.P10:
tableAddress = 0x546;
segments = 5;
break;

// has a segment table, but handled in ValidateRangeP12()
case PcmType.P12:
tableAddress = 0x0;
segments = 0;

// no segment table
case PcmType.P04:
case PcmType.P05:
case PcmType.P08:
break;

case PcmType.Undefined:
Expand All @@ -269,6 +264,10 @@ private bool ValidateChecksums()
this.logger.AddUserMessage("\tStart\tEnd\tStored\t\tNeeded\t\tVerdict\tSegment Name");
success &= ValidateRangeP04(true);
break;
case PcmType.P08:
this.logger.AddUserMessage("\tStart\tEnd\tStored\tNeeded\tVerdict\tSegment Name");
success &= ValidateRangeByteSum(type, 0, 0x7FFFB, 0x8004, "Whole File");
break;
case PcmType.P12:
this.logger.AddUserMessage("\tStart\tEnd\tStored\tNeeded\tVerdict\tSegment Name");
success &= ValidateRangeP12(0x922, 0x900, 0x94A, 2, "Boot Block");
Expand Down Expand Up @@ -333,7 +332,7 @@ private bool ValidateChecksums()
return false;
}

success &= ValidateRange(type, startAddress, endAddress, checksumAddress, segmentName);
success &= ValidateRangeWordSum(type, startAddress, endAddress, checksumAddress, segmentName);
}
break;
}
Expand Down Expand Up @@ -472,9 +471,48 @@ private void PrintHeader()
}

/// <summary>
/// Validate a range.
/// Validate a range (8 bit bytes).
/// </summary>
private bool ValidateRangeByteSum(PcmType type, UInt32 start, UInt32 end, UInt32 storage, string description)
{
UInt16 storedChecksum = (UInt16)((this.image[storage] << 8) + this.image[storage + 1]);
UInt16 computedChecksum = 0;

for (UInt32 address = start; address <= end; address ++)
{
switch (type)
{
case PcmType.P08:
if (address == 0x4000)
{
address = 0x8010;
}

break;
}

computedChecksum += this.image[address];
}

bool verdict = storedChecksum == computedChecksum;

string error = string.Format(
"\t{0:X5}\t{1:X5}\t{2:X4}\t{3:X4}\t{4:X4}\t{5}",
start,
end,
storedChecksum,
computedChecksum,
verdict ? "Good" : "BAD",
description);

this.logger.AddUserMessage(error);
return verdict;
}

/// <summary>
/// Validate a range (16 bit words, 2's compliment).
/// </summary>
private bool ValidateRange(PcmType type, UInt32 start, UInt32 end, UInt32 storage, string description)
private bool ValidateRangeWordSum(PcmType type, UInt32 start, UInt32 end, UInt32 storage, string description)
{
UInt16 storedChecksum = (UInt16)((this.image[storage] << 8) + this.image[storage + 1]);
UInt16 computedChecksum = 0;
Expand All @@ -483,6 +521,26 @@ private bool ValidateRange(PcmType type, UInt32 start, UInt32 end, UInt32 storag
{
switch (type)
{
case PcmType.P01_P59:
if (address == 0x500)
{
address = 0x502;
}

if (address == 0x4000)
{
address = 0x20000;
}
break;

case PcmType.P08:
if (address == 0x4000)
{
address = 0x8010;
}

break;

case PcmType.P10:
switch (address)
{
Expand All @@ -499,18 +557,6 @@ private bool ValidateRange(PcmType type, UInt32 start, UInt32 end, UInt32 storag
break;
}
break;

case PcmType.P01_P59:
if (address == 0x500)
{
address = 0x502;
}

if (address == 0x4000)
{
address = 0x20000;
}
break;
}

UInt16 value = (UInt16)(this.image[address] << 8);
Expand Down

0 comments on commit 168bda0

Please sign in to comment.