Skip to content

Commit

Permalink
add emunand support for TWL_FIRM
Browse files Browse the repository at this point in the history
  • Loading branch information
aspargas2 committed Mar 17, 2022
1 parent 046ff00 commit de7d576
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
19 changes: 17 additions & 2 deletions arm9/source/emunand.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,21 @@ static inline u32 getSdmmc(u8 *pos, u32 size, u32 *sdmmc)
return 0;
}

static inline u32 getTwlSdmmc(u8 *pos, u32 size, u32 *sdmmc)
{
static const u8 pattern[] = {0xF2, 0xD0, 0x12, 0x48},
pattern2[] = {0x3D, 0x18, 0x28, 0x79};

const u32 *off = (u32 *)memsearch(pos, pattern, size, sizeof(pattern));
const u16 *off2 = (u16 *)memsearch(pos, pattern2, size, sizeof(pattern2));

if(off == NULL || off2 == NULL) return 1;

*sdmmc = *(off + 0x13) + *(u32 *)(off2 + (*(off2 - 1) & 0xFF) * 2);

return 0;
}

static inline u32 patchNandRw(u8 *pos, u32 size, u32 branchOffset)
{
//Look for read/write code
Expand Down Expand Up @@ -187,7 +202,7 @@ static inline u32 patchMpu(u8 *pos, u32 size)
return 0;
}

u32 patchEmuNand(u8 *arm9Section, u32 kernel9Size, u8 *process9Offset, u32 process9Size, u8 *kernel9Address, u32 firmVersion)
u32 patchEmuNand(u8 *arm9Section, u32 kernel9Size, u8 *process9Offset, u32 process9Size, u8 *kernel9Address, u32 firmVersion, bool twl)
{
u8 *freeK9Space;

Expand All @@ -201,7 +216,7 @@ u32 patchEmuNand(u8 *arm9Section, u32 kernel9Size, u8 *process9Offset, u32 proce

//Find and add the SDMMC struct
u32 sdmmc;
ret += !ISN3DS && firmVersion < 0x25 ? getOldSdmmc(&sdmmc, firmVersion) : getSdmmc(process9Offset, process9Size, &sdmmc);
ret += twl ? getTwlSdmmc(process9Offset, process9Size, &sdmmc) : !ISN3DS && firmVersion < 0x25 ? getOldSdmmc(&sdmmc, firmVersion) : getSdmmc(process9Offset, process9Size, &sdmmc);
if(!ret) emunandPatchSdmmcStructPtr = sdmmc;

//Copy EmuNAND code
Expand Down
2 changes: 1 addition & 1 deletion arm9/source/emunand.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ extern u32 emuOffset,
emuHeader;

void locateEmuNand(FirmwareSource *nandType);
u32 patchEmuNand(u8 *arm9Section, u32 kernel9Size, u8 *process9Offset, u32 process9Size, u8 *kernel9Address, u32 firmVersion);
u32 patchEmuNand(u8 *arm9Section, u32 kernel9Size, u8 *process9Offset, u32 process9Size, u8 *kernel9Address, u32 firmVersion, bool twl);
7 changes: 5 additions & 2 deletions arm9/source/firm.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStora
ret += patchSignatureChecks(process9Offset, process9Size);

//Apply EmuNAND patches
if(nandType != FIRMWARE_SYSNAND) ret += patchEmuNand(arm9Section, kernel9Size, process9Offset, process9Size, firm->section[2].address, firmVersion);
if(nandType != FIRMWARE_SYSNAND) ret += patchEmuNand(arm9Section, kernel9Size, process9Offset, process9Size, firm->section[2].address, firmVersion, false);

//Apply FIRM0/1 writes patches on SysNAND to protect A9LH
else if(isFirmProtEnabled) ret += patchFirmWrites(process9Offset, process9Size);
Expand Down Expand Up @@ -429,7 +429,7 @@ u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStora
return ret;
}

u32 patchTwlFirm(u32 firmVersion, bool loadFromStorage, bool doUnitinfoPatch)
u32 patchTwlFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStorage, bool doUnitinfoPatch)
{
u8 *arm9Section = (u8 *)firm + firm->section[3].offset;

Expand Down Expand Up @@ -460,6 +460,9 @@ u32 patchTwlFirm(u32 firmVersion, bool loadFromStorage, bool doUnitinfoPatch)
else if(!ISN3DS && firmVersion == 0x11) ret += patchOldTwlFlashcartChecks(process9Offset, process9Size);
ret += patchTwlShaHashChecks(process9Offset, process9Size);

//Apply EmuNAND patches
if(nandType != FIRMWARE_SYSNAND) ret += patchEmuNand(arm9Section, kernel9Size, process9Offset, process9Size, firm->section[3].address, firmVersion, true);

//Apply UNITINFO patch
if(doUnitinfoPatch) ret += patchUnitInfoValueSet(arm9Section, kernel9Size);

Expand Down
2 changes: 1 addition & 1 deletion arm9/source/firm.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
u32 loadNintendoFirm(FirmwareType *firmType, FirmwareSource nandType, bool loadFromStorage, bool isSafeMode);
void loadHomebrewFirm(u32 pressed);
u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStorage, bool isFirmProtEnabled, bool needToInitSd, bool doUnitinfoPatch);
u32 patchTwlFirm(u32 firmVersion, bool loadFromStorage, bool doUnitinfoPatch);
u32 patchTwlFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStorage, bool doUnitinfoPatch);
u32 patchAgbFirm(bool loadFromStorage, bool doUnitinfoPatch);
u32 patch1x2xNativeAndSafeFirm(void);
void launchFirm(int argc, char **argv);
2 changes: 1 addition & 1 deletion arm9/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ void main(int argc, char **argv, u32 magicWord)
res = patchNativeFirm(firmVersion, nandType, loadFromStorage, isFirmProtEnabled, needToInitSd, doUnitinfoPatch);
break;
case TWL_FIRM:
res = patchTwlFirm(firmVersion, loadFromStorage, doUnitinfoPatch);
res = patchTwlFirm(firmVersion, nandType, loadFromStorage, doUnitinfoPatch);
break;
case AGB_FIRM:
res = patchAgbFirm(loadFromStorage, doUnitinfoPatch);
Expand Down

0 comments on commit de7d576

Please sign in to comment.