Skip to content

Commit

Permalink
Removed feature to auto fix corrupted cards and added it as the user …
Browse files Browse the repository at this point in the history
…selectable option because it conflicts with the newly discovered FreePSXBoot exploit
  • Loading branch information
ShendoXT committed Apr 23, 2021
1 parent aeaaecd commit 0952399
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 31 deletions.
Binary file modified .vs/MemcardRex/v16/.suo
Binary file not shown.
33 changes: 23 additions & 10 deletions MemcardRex/GUI/mainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public struct programSettings
public int backupMemcards; //Backup Memory Card settings
public int warningMessage; //Warning message settings
public int restoreWindowPosition; //Restore window position
public int fixCorruptedCards; //Try to fix corrupted memory cards
public int glassStatusBar; //Vista glass status bar
public int formatType; //Type of formatting for hardware interfaces
public string listFont; //List font
Expand Down Expand Up @@ -207,6 +208,9 @@ private void loadProgramSettings()
//Load format type
mainSettings.formatType = xmlAppSettings.readXmlEntryInt("HardwareFormatType", 0, 1);

//Load fix corrupted cards value
mainSettings.fixCorruptedCards = xmlAppSettings.readXmlEntryInt("FixCorruptedCards", 0, 1);

//Check if window position should be read
if (mainSettings.restoreWindowPosition == 1)
{
Expand Down Expand Up @@ -266,6 +270,9 @@ private void saveProgramSettings()
//Set format type
xmlAppSettings.writeXmlEntry("HardwareFormatType", mainSettings.formatType.ToString());

//Set fix corrupted cards value
xmlAppSettings.writeXmlEntry("FixCorruptedCards", mainSettings.fixCorruptedCards.ToString());

//Set window X coordinate
xmlAppSettings.writeXmlEntry("WindowX", this.Location.X.ToString());

Expand All @@ -276,6 +283,12 @@ private void saveProgramSettings()
xmlAppSettings.closeXmlWriter();
}

//Quick and dirty settings bool converter
private bool getSettingsBool(int intValue)
{
return (intValue == 1) ? true : false;
}

//Backup a Memory Card
private void backupMemcard(string fileName)
{
Expand Down Expand Up @@ -356,7 +369,7 @@ private void openCard(string fileName)
PScard.Add(new ps1card());

//Try to open card
errorMsg = PScard[PScard.Count - 1].openMemoryCard(fileName);
errorMsg = PScard[PScard.Count - 1].openMemoryCard(fileName, getSettingsBool(mainSettings.fixCorruptedCards));

//If card is sucesfully opened proceed further, else destroy it
if (errorMsg == null)
Expand Down Expand Up @@ -449,7 +462,7 @@ private void saveCardDialog(int listIndex)
//Save a Memory Card to a given filename
private void saveMemoryCard(int listIndex, string fileName, byte memoryCardType)
{
if (PScard[listIndex].saveMemoryCard(fileName, memoryCardType))
if (PScard[listIndex].saveMemoryCard(fileName, memoryCardType, getSettingsBool(mainSettings.fixCorruptedCards)))
{
refreshListView(listIndex, cardList[listIndex].SelectedIndices[0]);
refreshStatusStrip();
Expand Down Expand Up @@ -1660,7 +1673,7 @@ private void cardReaderRead(byte[] readData)
PScard.Add(new ps1card());

//Fill the card with the new data
PScard[PScard.Count - 1].openMemoryCardStream(readData);
PScard[PScard.Count - 1].openMemoryCardStream(readData, getSettingsBool(mainSettings.fixCorruptedCards));

//Temporary set a bogus file location (to fool filterNullCard function)
PScard[PScard.Count - 1].cardLocation = "\0";
Expand Down Expand Up @@ -2054,7 +2067,7 @@ private void dexDriveMenuWrite_Click(object sender, EventArgs e)
if (PScard.Count > 0)
{
//Open a DexDrive communication window
new cardReaderWindow().writeMemoryCardDexDrive(this, appName, mainSettings.communicationPort, PScard[listIndex].saveMemoryCardStream(), 1024);
new cardReaderWindow().writeMemoryCardDexDrive(this, appName, mainSettings.communicationPort, PScard[listIndex].saveMemoryCardStream(getSettingsBool(mainSettings.fixCorruptedCards)), 1024);
}
}

Expand Down Expand Up @@ -2087,7 +2100,7 @@ private void memCARDuinoMenuWrite_Click(object sender, EventArgs e)
if (PScard.Count > 0)
{
//Open a DexDrive communication window
new cardReaderWindow().writeMemoryCardCARDuino(this, appName, mainSettings.communicationPort, PScard[listIndex].saveMemoryCardStream(), 1024);
new cardReaderWindow().writeMemoryCardCARDuino(this, appName, mainSettings.communicationPort, PScard[listIndex].saveMemoryCardStream(getSettingsBool(mainSettings.fixCorruptedCards)), 1024);
}
}

Expand All @@ -2108,7 +2121,7 @@ private void pS1CardLinkMenuWrite_Click(object sender, EventArgs e)
if (PScard.Count > 0)
{
//Open a DexDrive communication window
new cardReaderWindow().writeMemoryCardPS1CLnk(this, appName, mainSettings.communicationPort, PScard[listIndex].saveMemoryCardStream(), 1024);
new cardReaderWindow().writeMemoryCardPS1CLnk(this, appName, mainSettings.communicationPort, PScard[listIndex].saveMemoryCardStream(getSettingsBool(mainSettings.fixCorruptedCards)), 1024);
}
}

Expand Down Expand Up @@ -2143,21 +2156,21 @@ private void formatHardwareCard(int hardDevice)
if (mainSettings.formatType == 0) frameNumber = 64;

//Create a new card by giving a null path
blankCard.openMemoryCard(null);
blankCard.openMemoryCard(null, true);

//Check what device to use
switch (hardDevice)
{
case 0: //DexDrive
new cardReaderWindow().writeMemoryCardDexDrive(this, appName, mainSettings.communicationPort, blankCard.saveMemoryCardStream(), frameNumber);
new cardReaderWindow().writeMemoryCardDexDrive(this, appName, mainSettings.communicationPort, blankCard.saveMemoryCardStream(true), frameNumber);
break;

case 1: //MemCARDuino
new cardReaderWindow().writeMemoryCardCARDuino(this, appName, mainSettings.communicationPort, blankCard.saveMemoryCardStream(), frameNumber);
new cardReaderWindow().writeMemoryCardCARDuino(this, appName, mainSettings.communicationPort, blankCard.saveMemoryCardStream(true), frameNumber);
break;

case 2: //PS1CardLink
new cardReaderWindow().writeMemoryCardPS1CLnk(this, appName, mainSettings.communicationPort, blankCard.saveMemoryCardStream(), frameNumber);
new cardReaderWindow().writeMemoryCardPS1CLnk(this, appName, mainSettings.communicationPort, blankCard.saveMemoryCardStream(true), frameNumber);
break;
}
}
Expand Down
13 changes: 13 additions & 0 deletions MemcardRex/GUI/preferencesWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions MemcardRex/GUI/preferencesWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void initializeDialog(mainWindow.programSettings progSettings)
if (progSettings.glassStatusBar == 1) glassCheckbox.Checked = true; else glassCheckbox.Checked = false;
if (progSettings.warningMessage == 1) backupWarningCheckBox.Checked = true; else backupWarningCheckBox.Checked = false;
if (progSettings.restoreWindowPosition == 1) restorePositionCheckbox.Checked = true; else restorePositionCheckbox.Checked = false;
if (progSettings.fixCorruptedCards == 1) fixCorruptedCardsCheckbox.Checked = true; else fixCorruptedCardsCheckbox.Checked = false;

//Load all COM ports found on the system
foreach (string port in SerialPort.GetPortNames())
Expand Down Expand Up @@ -77,6 +78,7 @@ private void applySettings()
if (glassCheckbox.Checked == true) progSettings.glassStatusBar = 1; else progSettings.glassStatusBar = 0;
if (backupWarningCheckBox.Checked == true) progSettings.warningMessage = 1; else progSettings.warningMessage = 0;
if (restorePositionCheckbox.Checked == true) progSettings.restoreWindowPosition = 1; else progSettings.restoreWindowPosition = 0;
if (fixCorruptedCardsCheckbox.Checked == true) progSettings.fixCorruptedCards = 1; else progSettings.fixCorruptedCards = 0;
if (fontCombo.SelectedIndex != -1) progSettings.listFont = fontCombo.SelectedItem.ToString();

hostWindow.applyProgramSettings(progSettings);
Expand Down
4 changes: 2 additions & 2 deletions MemcardRex/GUI/preferencesWindow.resx
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
47 changes: 28 additions & 19 deletions MemcardRex/ps1card.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//PS1 Memory Card class
//Shendo 2009-2013
//Shendo 2009-2021

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -88,20 +88,25 @@ private void loadDataFromRawCard()
}

//Recreate raw Memory Card
private void loadDataToRawCard()
private void loadDataToRawCard(bool fixData)
{
//Clear existing data
rawMemoryCard = new byte[131072];
//Check if data needs to be fixed or left as is (mandatory for FreePSXBoot)
if (fixData)
{
//Clear existing data
rawMemoryCard = new byte[131072];

//Recreate the signature
rawMemoryCard[0] = 0x4D; //M
rawMemoryCard[1] = 0x43; //C
rawMemoryCard[127] = 0x0E; //XOR (precalculated)
//Recreate the signature
rawMemoryCard[0] = 0x4D; //M
rawMemoryCard[1] = 0x43; //C
rawMemoryCard[127] = 0x0E; //XOR (precalculated)

rawMemoryCard[8064] = 0x4D; //M
rawMemoryCard[8065] = 0x43; //C
rawMemoryCard[8191] = 0x0E; //XOR (precalculated)
rawMemoryCard[8064] = 0x4D; //M
rawMemoryCard[8065] = 0x43; //C
rawMemoryCard[8191] = 0x0E; //XOR (precalculated)
}

//This can be copied freely without fixing
for (int slotNumber = 0; slotNumber < 15; slotNumber++)
{
//Load header data
Expand All @@ -117,6 +122,10 @@ private void loadDataToRawCard()
}
}


//Skip fixing data if it's not needed
if (!fixData) return;

//Create authentic data (just for completeness)
for (int i = 0; i < 20; i++)
{
Expand Down Expand Up @@ -901,7 +910,7 @@ public bool openSingleSave(string fileName, int slotNumber, out int requiredSlot
}

//Save Memory Card to the given filename
public bool saveMemoryCard(string fileName, int memoryCardType)
public bool saveMemoryCard(string fileName, int memoryCardType, bool fixData)
{
BinaryWriter binWriter = null;

Expand All @@ -916,7 +925,7 @@ public bool saveMemoryCard(string fileName, int memoryCardType)
}

//Prepare data for saving
loadDataToRawCard();
loadDataToRawCard(fixData);

//Check what kind of file to output according to memoryCardType
switch (memoryCardType)
Expand Down Expand Up @@ -953,18 +962,18 @@ public bool saveMemoryCard(string fileName, int memoryCardType)
}

//Save (export) Memory Card to a given byte stream
public byte[] saveMemoryCardStream()
public byte[] saveMemoryCardStream(bool fixData)
{
//Prepare data for saving
loadDataToRawCard();
loadDataToRawCard(fixData);

//Return complete Memory Card data
return rawMemoryCard;
}


//Open memory card from the given byte stream
public void openMemoryCardStream(byte[] memCardData)
public void openMemoryCardStream(byte[] memCardData, bool fixData)
{
//Set the reference for the recieved data
rawMemoryCard = memCardData;
Expand All @@ -974,7 +983,7 @@ public void openMemoryCardStream(byte[] memCardData)

cardName = "Untitled";

calculateXOR();
if(fixData) calculateXOR();
loadStringData();
loadGMEComments();
loadSlotTypes();
Expand All @@ -989,7 +998,7 @@ public void openMemoryCardStream(byte[] memCardData)
}

//Open Memory Card from the given filename (return error message if operation is not sucessfull)
public string openMemoryCard(string fileName)
public string openMemoryCard(string fileName, bool fixData)
{
//Check if the Memory Card should be opened or created
if (fileName != null)
Expand Down Expand Up @@ -1070,7 +1079,7 @@ public string openMemoryCard(string fileName)
}

//Calculate XOR checksum (in case if any of the saveHeaders have corrputed XOR)
calculateXOR();
if(fixData) calculateXOR();

//Convert various Memory Card data to strings
loadStringData();
Expand Down

0 comments on commit 0952399

Please sign in to comment.