Skip to content

Commit

Permalink
Fix a LGY K11 bug where user TLS was not accessible to user mode
Browse files Browse the repository at this point in the history
  • Loading branch information
TuxSH committed Sep 13, 2023
1 parent a9fcca5 commit c0e0f02
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions arm9/source/firm.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,9 @@ u32 patchNativeFirm(u32 firmVersion, FirmwareSource nandType, bool loadFromStora

u32 patchTwlFirm(u32 firmVersion, bool loadFromStorage, bool doUnitinfoPatch)
{
u8 *section1 = (u8 *)firm + firm->section[1].offset;
u32 section1Size = firm->section[1].size;

u8 *arm9Section = (u8 *)firm + firm->section[3].offset;

// Below 3.0, do not actually do anything.
Expand Down Expand Up @@ -627,6 +630,8 @@ u32 patchTwlFirm(u32 firmVersion, bool loadFromStorage, bool doUnitinfoPatch)
//Apply UNITINFO patch
if(doUnitinfoPatch) ret += patchUnitInfoValueSet(arm9Section, kernel9Size);

ret += patchLgyK11(section1, section1Size);

// Also patch TwlBg here
mergeSection0(TWL_FIRM, 0, loadFromStorage);
firm->section[0].size = 0;
Expand Down
21 changes: 20 additions & 1 deletion arm9/source/patches.c
Original file line number Diff line number Diff line change
Expand Up @@ -808,4 +808,23 @@ void patchTwlBg(u8 *pos, u32 size)
off2[i] = 0x46C0;
}
}
}
}

u32 patchLgyK11(u8 *section1, u32 section1Size)
{
u32 *off;

// Fix a bug where Legacy K11 maps user TLS with "user no access" permissions
// Map it as RWX (just like the rest of other user-accessible pages) instead
for (off = (u32 *)section1; (u8 *)off <= section1 + section1Size && *off != 0xE0100000; off++);

if ((u8 *)off >= section1 + section1Size)
return 1;

++off;

*off &= ~0x231; // clear APX mask and XN
*off |= 0x030; // re-set APX (to user/kernel RW)

return 0;
}
1 change: 1 addition & 0 deletions arm9/source/patches.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ u32 patchOldTwlFlashcartChecks(u8 *pos, u32 size);
u32 patchTwlShaHashChecks(u8 *pos, u32 size);
u32 patchAgbBootSplash(u8 *pos, u32 size);
void patchTwlBg(u8 *pos, u32 size); // silently fails
u32 patchLgyK11(u8 *section1, u32 section1Size);

0 comments on commit c0e0f02

Please sign in to comment.