Skip to content

Commit 0952399

Browse files
committed
Removed feature to auto fix corrupted cards and added it as the user selectable option because it conflicts with the newly discovered FreePSXBoot exploit
1 parent aeaaecd commit 0952399

File tree

6 files changed

+68
-31
lines changed

6 files changed

+68
-31
lines changed

.vs/MemcardRex/v16/.suo

26.5 KB
Binary file not shown.

MemcardRex/GUI/mainWindow.cs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public struct programSettings
7272
public int backupMemcards; //Backup Memory Card settings
7373
public int warningMessage; //Warning message settings
7474
public int restoreWindowPosition; //Restore window position
75+
public int fixCorruptedCards; //Try to fix corrupted memory cards
7576
public int glassStatusBar; //Vista glass status bar
7677
public int formatType; //Type of formatting for hardware interfaces
7778
public string listFont; //List font
@@ -207,6 +208,9 @@ private void loadProgramSettings()
207208
//Load format type
208209
mainSettings.formatType = xmlAppSettings.readXmlEntryInt("HardwareFormatType", 0, 1);
209210

211+
//Load fix corrupted cards value
212+
mainSettings.fixCorruptedCards = xmlAppSettings.readXmlEntryInt("FixCorruptedCards", 0, 1);
213+
210214
//Check if window position should be read
211215
if (mainSettings.restoreWindowPosition == 1)
212216
{
@@ -266,6 +270,9 @@ private void saveProgramSettings()
266270
//Set format type
267271
xmlAppSettings.writeXmlEntry("HardwareFormatType", mainSettings.formatType.ToString());
268272

273+
//Set fix corrupted cards value
274+
xmlAppSettings.writeXmlEntry("FixCorruptedCards", mainSettings.fixCorruptedCards.ToString());
275+
269276
//Set window X coordinate
270277
xmlAppSettings.writeXmlEntry("WindowX", this.Location.X.ToString());
271278

@@ -276,6 +283,12 @@ private void saveProgramSettings()
276283
xmlAppSettings.closeXmlWriter();
277284
}
278285

286+
//Quick and dirty settings bool converter
287+
private bool getSettingsBool(int intValue)
288+
{
289+
return (intValue == 1) ? true : false;
290+
}
291+
279292
//Backup a Memory Card
280293
private void backupMemcard(string fileName)
281294
{
@@ -356,7 +369,7 @@ private void openCard(string fileName)
356369
PScard.Add(new ps1card());
357370

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

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

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

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

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

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

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

21452158
//Create a new card by giving a null path
2146-
blankCard.openMemoryCard(null);
2159+
blankCard.openMemoryCard(null, true);
21472160

21482161
//Check what device to use
21492162
switch (hardDevice)
21502163
{
21512164
case 0: //DexDrive
2152-
new cardReaderWindow().writeMemoryCardDexDrive(this, appName, mainSettings.communicationPort, blankCard.saveMemoryCardStream(), frameNumber);
2165+
new cardReaderWindow().writeMemoryCardDexDrive(this, appName, mainSettings.communicationPort, blankCard.saveMemoryCardStream(true), frameNumber);
21532166
break;
21542167

21552168
case 1: //MemCARDuino
2156-
new cardReaderWindow().writeMemoryCardCARDuino(this, appName, mainSettings.communicationPort, blankCard.saveMemoryCardStream(), frameNumber);
2169+
new cardReaderWindow().writeMemoryCardCARDuino(this, appName, mainSettings.communicationPort, blankCard.saveMemoryCardStream(true), frameNumber);
21572170
break;
21582171

21592172
case 2: //PS1CardLink
2160-
new cardReaderWindow().writeMemoryCardPS1CLnk(this, appName, mainSettings.communicationPort, blankCard.saveMemoryCardStream(), frameNumber);
2173+
new cardReaderWindow().writeMemoryCardPS1CLnk(this, appName, mainSettings.communicationPort, blankCard.saveMemoryCardStream(true), frameNumber);
21612174
break;
21622175
}
21632176
}

MemcardRex/GUI/preferencesWindow.Designer.cs

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MemcardRex/GUI/preferencesWindow.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public void initializeDialog(mainWindow.programSettings progSettings)
3636
if (progSettings.glassStatusBar == 1) glassCheckbox.Checked = true; else glassCheckbox.Checked = false;
3737
if (progSettings.warningMessage == 1) backupWarningCheckBox.Checked = true; else backupWarningCheckBox.Checked = false;
3838
if (progSettings.restoreWindowPosition == 1) restorePositionCheckbox.Checked = true; else restorePositionCheckbox.Checked = false;
39+
if (progSettings.fixCorruptedCards == 1) fixCorruptedCardsCheckbox.Checked = true; else fixCorruptedCardsCheckbox.Checked = false;
3940

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

8284
hostWindow.applyProgramSettings(progSettings);

MemcardRex/GUI/preferencesWindow.resx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@
112112
<value>2.0</value>
113113
</resheader>
114114
<resheader name="reader">
115-
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
115+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
116116
</resheader>
117117
<resheader name="writer">
118-
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
118+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120120
</root>

MemcardRex/ps1card.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//PS1 Memory Card class
2-
//Shendo 2009-2013
2+
//Shendo 2009-2021
33

44
using System;
55
using System.Collections.Generic;
@@ -88,20 +88,25 @@ private void loadDataFromRawCard()
8888
}
8989

9090
//Recreate raw Memory Card
91-
private void loadDataToRawCard()
91+
private void loadDataToRawCard(bool fixData)
9292
{
93-
//Clear existing data
94-
rawMemoryCard = new byte[131072];
93+
//Check if data needs to be fixed or left as is (mandatory for FreePSXBoot)
94+
if (fixData)
95+
{
96+
//Clear existing data
97+
rawMemoryCard = new byte[131072];
9598

96-
//Recreate the signature
97-
rawMemoryCard[0] = 0x4D; //M
98-
rawMemoryCard[1] = 0x43; //C
99-
rawMemoryCard[127] = 0x0E; //XOR (precalculated)
99+
//Recreate the signature
100+
rawMemoryCard[0] = 0x4D; //M
101+
rawMemoryCard[1] = 0x43; //C
102+
rawMemoryCard[127] = 0x0E; //XOR (precalculated)
100103

101-
rawMemoryCard[8064] = 0x4D; //M
102-
rawMemoryCard[8065] = 0x43; //C
103-
rawMemoryCard[8191] = 0x0E; //XOR (precalculated)
104+
rawMemoryCard[8064] = 0x4D; //M
105+
rawMemoryCard[8065] = 0x43; //C
106+
rawMemoryCard[8191] = 0x0E; //XOR (precalculated)
107+
}
104108

109+
//This can be copied freely without fixing
105110
for (int slotNumber = 0; slotNumber < 15; slotNumber++)
106111
{
107112
//Load header data
@@ -117,6 +122,10 @@ private void loadDataToRawCard()
117122
}
118123
}
119124

125+
126+
//Skip fixing data if it's not needed
127+
if (!fixData) return;
128+
120129
//Create authentic data (just for completeness)
121130
for (int i = 0; i < 20; i++)
122131
{
@@ -901,7 +910,7 @@ public bool openSingleSave(string fileName, int slotNumber, out int requiredSlot
901910
}
902911

903912
//Save Memory Card to the given filename
904-
public bool saveMemoryCard(string fileName, int memoryCardType)
913+
public bool saveMemoryCard(string fileName, int memoryCardType, bool fixData)
905914
{
906915
BinaryWriter binWriter = null;
907916

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

918927
//Prepare data for saving
919-
loadDataToRawCard();
928+
loadDataToRawCard(fixData);
920929

921930
//Check what kind of file to output according to memoryCardType
922931
switch (memoryCardType)
@@ -953,18 +962,18 @@ public bool saveMemoryCard(string fileName, int memoryCardType)
953962
}
954963

955964
//Save (export) Memory Card to a given byte stream
956-
public byte[] saveMemoryCardStream()
965+
public byte[] saveMemoryCardStream(bool fixData)
957966
{
958967
//Prepare data for saving
959-
loadDataToRawCard();
968+
loadDataToRawCard(fixData);
960969

961970
//Return complete Memory Card data
962971
return rawMemoryCard;
963972
}
964973

965974

966975
//Open memory card from the given byte stream
967-
public void openMemoryCardStream(byte[] memCardData)
976+
public void openMemoryCardStream(byte[] memCardData, bool fixData)
968977
{
969978
//Set the reference for the recieved data
970979
rawMemoryCard = memCardData;
@@ -974,7 +983,7 @@ public void openMemoryCardStream(byte[] memCardData)
974983

975984
cardName = "Untitled";
976985

977-
calculateXOR();
986+
if(fixData) calculateXOR();
978987
loadStringData();
979988
loadGMEComments();
980989
loadSlotTypes();
@@ -989,7 +998,7 @@ public void openMemoryCardStream(byte[] memCardData)
989998
}
990999

9911000
//Open Memory Card from the given filename (return error message if operation is not sucessfull)
992-
public string openMemoryCard(string fileName)
1001+
public string openMemoryCard(string fileName, bool fixData)
9931002
{
9941003
//Check if the Memory Card should be opened or created
9951004
if (fileName != null)
@@ -1070,7 +1079,7 @@ public string openMemoryCard(string fileName)
10701079
}
10711080

10721081
//Calculate XOR checksum (in case if any of the saveHeaders have corrputed XOR)
1073-
calculateXOR();
1082+
if(fixData) calculateXOR();
10741083

10751084
//Convert various Memory Card data to strings
10761085
loadStringData();

0 commit comments

Comments
 (0)