Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug where save size could be detected incorrectly, if multiple bytes in the save are identical #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions source/common/cardEeprom.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ u32 cardEepromReadID() {
return id;
}


//---------------------------------------------------------------------------------
int cardEepromGetType(void) {
//---------------------------------------------------------------------------------
Expand All @@ -79,7 +78,7 @@ int cardEepromGetType(void) {
if (( sr == 0xff && id == 0xffffff) || ( sr == 0 && id == 0 )) return -1;
if ( sr == 0xf0 && id == 0xffffff ) return 1;
if ( sr == 0x00 && id == 0xffffff ) return 2;
if ( id != 0xffffff) return 3;
if ( id != 0xffffff || ( sr == 0x02 && id == 0xffffff )) return 3;

return 0;
}
Expand All @@ -97,20 +96,38 @@ uint32 cardEepromGetSize() {
if(type == 1)
return 512;
if(type == 2) {
u32 buf1,buf2,buf3;
u32 buf1,buf2,buf3 = 0x54534554; // "TEST"
// Save the first word of the EEPROM
cardReadEeprom(0,(u8*)&buf1,4,type);
if ( !(buf1 != 0 || buf1 != 0xffffffff) ) {
buf3 = ~buf1;
cardWriteEeprom(0,(u8*)&buf3,4,type);
} else {
buf3 = buf1;
}

// Write "TEST" to it
cardWriteEeprom(0,(u8*)&buf3,4,type);

// Loop until the EEPROM mirrors and the first word shows up again
int size = 8192;
while (1) {
while (1) {
cardReadEeprom(size,(u8*)&buf2,4,type);
if ( buf2 == buf3 ) break;
// Check if it matches, if so check again with another value to ensure no false positives
if (buf2 == buf3) {
u32 buf4 = 0x74736574; // "test"
// Write "test" to the first word
cardWriteEeprom(0,(u8*)&buf4,4,type);

// Check if it still matches
cardReadEeprom(size,(u8*)&buf2,4,type);
if (buf2 == buf4) break;

// False match, write "TEST" back and keep going
cardWriteEeprom(0,(u8*)&buf3,4,type);
}
size += 8192;
};
}

// Restore the first word
cardWriteEeprom(0,(u8*)&buf1,4,type);

return size;
}

if ( buf1 != buf3 ) cardWriteEeprom(0,(u8*)&buf1,4,type);

Expand Down Expand Up @@ -153,7 +170,13 @@ uint32 cardEepromGetSize() {
if (device == 0x2211)
return 128*1024; // 1Mbit(128KByte) - MX25L1021E
}


if (id == 0xffffff) {
int sr = cardEepromCommand(SPI_EEPROM_RDSR);
if (sr == 2) { // Pokémon Mystery Dungeon - Explorers of Sky
return 128*1024; // 1Mbit (128KByte)
}
}

return 256*1024; // 2Mbit(256KByte)
}
Expand Down