Skip to content

Instantly share code, notes, and snippets.

@TuxSH
Last active January 16, 2018 17:23
Show Gist options
  • Save TuxSH/38c3133ef8c71a5456745a222b53c3f2 to your computer and use it in GitHub Desktop.
Save TuxSH/38c3133ef8c71a5456745a222b53c3f2 to your computer and use it in GitHub Desktop.
1de27c5.diff
diff --git a/sysmodules/rosalina/source/menus/cheats.c b/sysmodules/rosalina/source/menus/cheats.c
index 3fbe273..5fb2ae7 100644
--- a/sysmodules/rosalina/source/menus/cheats.c
+++ b/sysmodules/rosalina/source/menus/cheats.c
@@ -70,8 +70,6 @@ static s32 Cheats_FetchProcessInfo(void)
u32 pidList[0x40];
s32 processAmount;
- s64 sa, textTotalRoundedSize, rodataTotalRoundedSize, dataTotalRoundedSize;
-
svcGetProcessList(&processAmount, pidList, 0x40);
for (s32 i = 0; i < processAmount; i++)
@@ -82,10 +80,6 @@ static s32 Cheats_FetchProcessInfo(void)
cheatinfo[i].pid = pidList[i];
svcGetProcessInfo((s64 *) &cheatinfo[i].titleId, processHandle, 0x10001);
- svcGetProcessInfo(&textTotalRoundedSize, processHandle, 0x10002);
- svcGetProcessInfo(&rodataTotalRoundedSize, processHandle, 0x10003);
- svcGetProcessInfo(&dataTotalRoundedSize, processHandle, 0x10004);
- svcGetProcessInfo(&sa, processHandle, 0x10005);
svcCloseHandle(processHandle);
}
@@ -95,6 +89,9 @@ static s32 Cheats_FetchProcessInfo(void)
typedef struct CheatState
{
+ Handle process;
+ MemInfo mappedMemInfo;
+
u32 index;
u32 offset;
u32 data;
@@ -118,77 +115,80 @@ u64 cheatTitleInfo = -1ULL;
char failureReason[64];
-static bool Cheat_IsValidAddress(u32 address, u32 size)
+static u8 *Cheat_TranslateAddress(u32 address)
{
- if (codeStartAddress > 0 && codeStartAddress <= address && address <= codeStartAddress + codeTotalSize - size)
- {
- return true;
- }
- if (heapStartAddress > 0 && heapStartAddress <= address && address <= heapStartAddress + heapTotalSize - size)
+ static u8 *const mapDst = (u8 *)0x00100000; // arbitrary addr for Rosalina
+ MemInfo *memInfo = &cheat_state.mappedMemInfo;
+ if(memInfo->base_addr == 0 || !(address >= memInfo->base_addr && address < memInfo->base_addr + memInfo->size)) // page not already mapped
{
- return true;
+ PageInfo pgInfo;
+ if(memInfo->base_addr != 0)
+ svcUnmapProcessMemoryEx(cheat_state.process, (u32)mapDst, memInfo->size); // shouldn't fail
+
+ memInfo->base_addr = 0;
+ if(R_FAILED(svcQueryProcessMemory(memInfo, &pgInfo, cheat_state.process, address)))
+ return NULL;
+ if(R_FAILED(svcMapProcessMemoryEx(cheat_state.process, (u32)mapDst, memInfo->base_addr, memInfo->size)))
+ {
+ memInfo->base_addr = 0;
+ return NULL;
+ }
}
- return false;
+ return mapDst + (address - memInfo->base_addr);
}
static bool Cheat_Write8(u32 offset, u8 value)
{
- if (Cheat_IsValidAddress(cheat_state.offset + offset, 1))
- {
- *((u8*) (cheat_state.offset + offset)) = value;
- return true;
- }
- return false;
+ u8 *addr = Cheat_TranslateAddress(cheat_state.offset + offset);
+ if(addr == NULL)
+ return false;
+ *addr = value;
+ return true;
}
static bool Cheat_Write16(u32 offset, u16 value)
{
- if (Cheat_IsValidAddress(cheat_state.offset + offset, 2))
- {
- *((u16*) (cheat_state.offset + offset)) = value;
- return true;
- }
- return false;
+ u16 *addr = (u16 *)Cheat_TranslateAddress(cheat_state.offset + offset);
+ if(addr == NULL)
+ return false;
+ *addr = value;
+ return true;
}
static bool Cheat_Write32(u32 offset, u32 value)
{
- if (Cheat_IsValidAddress(cheat_state.offset + offset, 4))
- {
- *((u32*) (cheat_state.offset + offset)) = value;
- return true;
- }
- return false;
+ u32 *addr = (u32 *)Cheat_TranslateAddress(cheat_state.offset + offset);
+ if(addr == NULL)
+ return false;
+ *addr = value;
+ return true;
}
-static bool Cheat_Read8(u32 offset, u8* retValue)
+static bool Cheat_Read8(u32 offset, u8 *retValue)
{
- if (Cheat_IsValidAddress(cheat_state.offset + offset, 1))
- {
- *retValue = *((u8*) (cheat_state.offset + offset));
- return true;
- }
- return false;
+ u8 *addr = Cheat_TranslateAddress(cheat_state.offset + offset);
+ if(addr == NULL)
+ return false;
+ *retValue = *addr;
+ return true;
}
-static bool Cheat_Read16(u32 offset, u16* retValue)
+static bool Cheat_Read16(u32 offset, u16 *retValue)
{
- if (Cheat_IsValidAddress(cheat_state.offset + offset, 2))
- {
- *retValue = *((u16*) (cheat_state.offset + offset));
- return true;
- }
- return false;
+ u16 *addr = (u16 *)Cheat_TranslateAddress(cheat_state.offset + offset);
+ if(addr == NULL)
+ return false;
+ *retValue = *addr;
+ return true;
}
-static bool Cheat_Read32(u32 offset, u32* retValue)
+static bool Cheat_Read32(u32 offset, u32 *retValue)
{
- if (Cheat_IsValidAddress(cheat_state.offset + offset, 4))
- {
- *retValue = *((u32*) (cheat_state.offset + offset));
- return true;
- }
- return false;
+ u32 *addr = (u32 *)Cheat_TranslateAddress(cheat_state.offset + offset);
+ if(addr == NULL)
+ return false;
+ *retValue = *addr;
+ return true;
}
static u8 typeEMapping[] = { 4 << 3, 5 << 3, 6 << 3, 7 << 3, 0 << 3, 1 << 3, 2 << 3, 3 << 3 };
@@ -797,72 +797,18 @@ static u32 Cheat_ApplyCheat(const CheatDescription* const cheat)
return 1;
}
-static Result Cheat_MapMemoryAndApplyCheat(u32 pid, CheatDescription* const cheat)
+static Result Cheat_OpenProcessAndApplyCheat(u32 pid, CheatDescription* const cheat)
{
- Handle processHandle;
Result res;
- res = svcOpenProcess(&processHandle, pid);
+ res = svcOpenProcess(&cheat_state.process, pid);
if (R_SUCCEEDED(res))
{
-
- u32 codeDestAddress, heapDestAddress;
-
- s64 textStartAddress, textTotalRoundedSize, rodataTotalRoundedSize, dataTotalRoundedSize;
-
- svcGetProcessInfo(&textTotalRoundedSize, processHandle, 0x10002);
- svcGetProcessInfo(&rodataTotalRoundedSize, processHandle, 0x10003);
- svcGetProcessInfo(&dataTotalRoundedSize, processHandle, 0x10004);
-
- svcGetProcessInfo(&textStartAddress, processHandle, 0x10005);
-
- codeTotalSize = (u32) (textTotalRoundedSize + rodataTotalRoundedSize + dataTotalRoundedSize);
- codeDestAddress = codeStartAddress = (u32) textStartAddress; //should be 0x00100000
-
- MemInfo info;
- PageInfo out;
-
- heapDestAddress = heapStartAddress = 0x08000000;
- svcQueryProcessMemory(&info, &out, processHandle, heapStartAddress);
- heapTotalSize = info.size;
-
- Result codeRes = svcMapProcessMemoryEx(processHandle, codeDestAddress, codeStartAddress, codeTotalSize);
- if (R_FAILED(codeRes))
- {
- codeStartAddress = codeTotalSize = 0;
- }
-
- Result heapRes = svcMapProcessMemoryEx(processHandle, heapDestAddress, heapStartAddress, heapTotalSize);
- if (R_FAILED(heapRes))
- {
- heapStartAddress = heapTotalSize = 0;
- }
-
- if (R_SUCCEEDED(codeRes) || R_SUCCEEDED(heapRes))
- {
cheat->valid = Cheat_ApplyCheat(cheat);
-
- if (R_SUCCEEDED(codeRes))
- {
- svcUnmapProcessMemoryEx(processHandle, codeDestAddress, codeTotalSize);
- }
- if (R_SUCCEEDED(heapRes))
- {
- svcUnmapProcessMemoryEx(processHandle, heapDestAddress, heapTotalSize);
- }
- svcCloseHandle(processHandle);
+ svcCloseHandle(cheat_state.process);
cheat->active = 1;
- }
- else
- {
- svcCloseHandle(processHandle);
- sprintf(failureReason, "Can not map any memory");
- return codeRes;
- }
}
else
- {
sprintf(failureReason, "Open process failed");
- }
return res;
}
@@ -1165,7 +1111,7 @@ void Cheat_ApplyKeyCheats(void)
{
if (cheats[i]->active && cheats[i]->keyActivated && (cheats[i]->keyCombo & keys) == keys)
{
- Cheat_MapMemoryAndApplyCheat(pid, cheats[i]);
+ Cheat_OpenProcessAndApplyCheat(pid, cheats[i]);
}
}
}
@@ -1262,7 +1208,7 @@ void RosalinaMenu_Cheats(void)
}
else
{
- r = Cheat_MapMemoryAndApplyCheat(pid, cheats[selected]);
+ r = Cheat_OpenProcessAndApplyCheat(pid, cheats[selected]);
}
hasKeyActivated = 0;
for (int i = 0; i < cheatCount; i++)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment