Skip to content

Commit

Permalink
Only dump the RSA key on trimmed ROMs when it exists (#215)
Browse files Browse the repository at this point in the history
* Only dump the RSA key on trimmed ROMs when it exists

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

Thanks @bWFpbA
  • Loading branch information
Epicpkmn11 committed Dec 31, 2023
1 parent 088fd0d commit d588ac0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
17 changes: 15 additions & 2 deletions arm9/source/dumpOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ void ndsCardDump(void) {
u32 romSize;
if (dumpOption & DumpOption::romTrimmed) {
romSize = (isDSiMode() && (ndsCardHeader.unitCode != 0) && (ndsCardHeader.twlRomSize > 0))
? ndsCardHeader.twlRomSize : ndsCardHeader.romSize+0x88;
? ndsCardHeader.twlRomSize : ndsCardHeader.romSize + 0x88;
} else {
romSize = 0x20000 << ndsCardHeader.deviceSize;
}
Expand All @@ -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);
Expand Down
21 changes: 18 additions & 3 deletions arm9/source/fileOperations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,26 @@ int trimNds(const char *fileName) {

fseek(file, 0, SEEK_END);
u32 fileSize = ftell(file);
fseek(file, 0, SEEK_SET);

fclose(file);
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;
}

u32 romSize = ((ndsCardHeader.unitCode != 0) && (ndsCardHeader.twlRomSize > 0))
? ndsCardHeader.twlRomSize : ndsCardHeader.romSize + 0x88;
}

fclose(file);

if(fileSize == romSize) {
font->clear(false);
Expand Down

0 comments on commit d588ac0

Please sign in to comment.