Only dump the RSA key on trimmed ROMs when it exists (#215)

* Only dump the RSA key on trimmed ROMs when it exists

* Check for 'ac' magic number instead of 0xFF

Thanks @bWFpbA
This commit is contained in:
Pk11 2023-12-30 18:44:16 -06:00 committed by GitHub
parent 088fd0d383
commit d588ac07a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 5 deletions

View File

@ -892,10 +892,23 @@ void ndsCardDump(void) {
for (u32 i = 0; i < 0x8000; i += 0x200) {
cardRead (src+i, copyBuf+i, false);
}
if (fwrite(copyBuf, 1, (currentSize>=0x8000 ? 0x8000 : currentSize), destinationFile) < 1) {
if (currentSize < 0x8000) {
if (romSize == ndsCardHeader.romSize + 0x88) {
// Trimming, check for RSA key
// 'ac', auth code -- magic number
if (*(u16 *)(copyBuf + (ndsCardHeader.romSize % 0x8000)) != 0x6361) {
romSize -= 0x88;
currentSize -= 0x88;
}
}
fwrite(copyBuf, 1, currentSize, destinationFile);
} else if (fwrite(copyBuf, 1, 0x8000, destinationFile) < 1) {
dumpFailMsg(STR_FAILED_TO_DUMP_ROM);
break;
}
currentSize -= 0x8000;
}
fclose(destinationFile);

View File

@ -136,12 +136,27 @@ int trimNds(const char *fileName) {
fseek(file, 0, SEEK_END);
u32 fileSize = ftell(file);
fseek(file, 0, SEEK_SET);
u32 romSize;
if((ndsCardHeader.unitCode != 0) && (ndsCardHeader.twlRomSize > 0)) {
romSize = ndsCardHeader.twlRomSize;
} else {
romSize = ndsCardHeader.romSize;
// Check if it has an RSA key or not
fseek(file, romSize, SEEK_SET);
u16 magic;
if(fread(&magic, sizeof(u16), 1, file) == 1) {
// 'ac', auth code -- magic number
if(magic == 0x6361)
romSize += 0x88;
}
}
fclose(file);
u32 romSize = ((ndsCardHeader.unitCode != 0) && (ndsCardHeader.twlRomSize > 0))
? ndsCardHeader.twlRomSize : ndsCardHeader.romSize + 0x88;
if(fileSize == romSize) {
font->clear(false);
font->print(firstCol, 0, false, STR_FILE_ALREADY_TRIMMED + "\n\n" + STR_A_OK, alignStart);