Skip to content

Commit

Permalink
Fix external firm booting with old firms, minor style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraWright committed Apr 6, 2019
1 parent 70757e5 commit 49fba5a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion arm9/source/firm.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStora
if(firmVersion >= (ISN3DS ? 0x21 : 0x52)) ret += patchTitleInstallMinVersionChecks(process9Offset, process9Size, firmVersion);

//patch P9 AM ticket wrapper on 11.8+ to use 0 Key and IV, only on UNITINFO patch to prevent NIM from actually send any
if(doUnitinfoPatch && firmVersion >= (ISN3DS ? 0x35 : 0x64)) ret += patchP9AMTicketWrapperZeroKeyIV(process9Offset, process9Size);
if(doUnitinfoPatch && firmVersion >= (ISN3DS ? 0x35 : 0x64)) ret += patchP9AMTicketWrapperZeroKeyIV(process9Offset, process9Size, firmVersion);

//Apply UNITINFO patches
if(doUnitinfoPatch)
Expand Down
44 changes: 22 additions & 22 deletions arm9/source/patches.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,28 @@ u32 patchUnitInfoValueSet(u8 *pos, u32 size)
return 0;
}

u32 patchP9AMTicketWrapperZeroKeyIV(u8 *pos, u32 size, u32 firmVersion)
{
static const u8 __rt_memclr_pattern[] = {0x00, 0x20, 0xA0, 0xE3, 0x04, 0x00, 0x51, 0xE3, 0x07, 0x00, 0x00, 0x3A};
static const u8 pattern[] = {0x20, 0x21, 0xA6, 0xA8};

u32 function = (u32)memsearch(pos, __rt_memclr_pattern, size, sizeof(__rt_memclr_pattern));
u32 *off = (u32 *)memsearch(pos, pattern, size, sizeof(pattern));

if(function == 0 || off == NULL) return firmVersion == 0xFFFFFFFF ? 0 : 1;

s32 opjumpdistance = (s32)(function - ((u32)&off[2])) / 2;

//Beyond limit
if(opjumpdistance < -0x1fffff || opjumpdistance > 0x1fffff) return 1;

//r0 and r1 for old call are already correct for this one
//BLX __rt_memclr
off[1] = 0xE800F000U | (((u32)opjumpdistance & 0x7FF) << 16) | (((u32)opjumpdistance >> 11) & 0x3FF) | (((u32)opjumpdistance >> 21) & 0x400);

return 0;
}

u32 patchLgySignatureChecks(u8 *pos, u32 size)
{
static const u8 pattern[] = {0x47, 0xC1, 0x17, 0x49};
Expand Down Expand Up @@ -667,25 +689,3 @@ u32 patchAgbBootSplash(u8 *pos, u32 size)

return 0;
}

u32 patchP9AMTicketWrapperZeroKeyIV(u8* pos, u32 size)
{
static const u8 __rt_memclr_pattern[] = {0x00, 0x20, 0xA0, 0xE3, 0x04, 0x00, 0x51, 0xE3, 0x07, 0x00, 0x00, 0x3A};
static const u8 pattern[] = {0x20, 0x21, 0xA6, 0xA8};

u32 function = (u32)memsearch(pos, __rt_memclr_pattern, size, sizeof(__rt_memclr_pattern));
u32 *off = (u32*)memsearch(pos, pattern, size, sizeof(pattern));

if(function == 0 || off == NULL) return 1;

s32 opjumpdistance = (s32)(function - ((u32)&off[2])) / 2;

//beyond limit
if(opjumpdistance < -0x1fffff || opjumpdistance > 0x1fffff) return 1;

//r0 and r1 for old call are already correctly for this one
//BLX __rt_memclr
off[1] = 0xE800F000U | (((u32)opjumpdistance & 0x7FF) << 16) | (((u32)opjumpdistance >> 11) & 0x3FF) | (((u32)opjumpdistance >> 21) & 0x400);

return 0;
}
2 changes: 1 addition & 1 deletion arm9/source/patches.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ u32 patchSvcBreak9(u8 *pos, u32 size, u32 kernel9Address);
u32 patchKernel9Panic(u8 *pos, u32 size);
u32 patchP9AccessChecks(u8 *pos, u32 size);
u32 patchUnitInfoValueSet(u8 *pos, u32 size);
u32 patchP9AMTicketWrapperZeroKeyIV(u8 *pos, u32 size, u32 firmVersion);
u32 patchLgySignatureChecks(u8 *pos, u32 size);
u32 patchTwlInvalidSignatureChecks(u8 *pos, u32 size);
u32 patchTwlNintendoLogoChecks(u8 *pos, u32 size);
Expand All @@ -64,4 +65,3 @@ u32 patchTwlFlashcartChecks(u8 *pos, u32 size, u32 firmVersion);
u32 patchOldTwlFlashcartChecks(u8 *pos, u32 size);
u32 patchTwlShaHashChecks(u8 *pos, u32 size);
u32 patchAgbBootSplash(u8 *pos, u32 size);
u32 patchP9AMTicketWrapperZeroKeyIV(u8* pos, u32 size);
9 changes: 5 additions & 4 deletions arm9/source/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ u32 waitInput(bool isMenu)

if(!key)
{
if (shouldShellShutdown) {
if(shouldShellShutdown)
{
u8 shellState = I2C_readReg(I2C_DEV_MCU, 0xF);
wait(3);
if (!(shellState & 2)) mcuPowerOff();
if(!(shellState & 2)) mcuPowerOff();
}

u8 intstatus = I2C_readReg(I2C_DEV_MCU, 0x10);
u8 intStatus = I2C_readReg(I2C_DEV_MCU, 0x10);
wait(3);
if (intstatus & 1) mcuPowerOff(); //Power button pressed
if(intStatus & 1) mcuPowerOff(); //Power button pressed

oldKey = 0;
dPadDelay = 0;
Expand Down

0 comments on commit 49fba5a

Please sign in to comment.