Use safer EEPROM save size check (#94)

This commit is contained in:
Pk11 2021-05-20 16:22:38 -05:00 committed by GitHub
parent fb7371de4a
commit 1708f51963
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -51,18 +51,30 @@ uint32 cardEepromGetSizeFixed() {
if(type == 1)
return 512;
if(type == 2) {
u32 buf1,buf2,buf3 = 0x54536554; // "TeST"
u32 buf1,buf2,buf3 = 0x54534554; // "TEST"
// Save the first word of the EEPROM
cardReadEeprom(0,(u8*)&buf1,4,type);
// Write "TeST" to it
// 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) {
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;
}