Skip to content

Commit

Permalink
move patches to pm
Browse files Browse the repository at this point in the history
  • Loading branch information
TuxSH committed Mar 29, 2019
1 parent ae1f85d commit e688ec9
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 85 deletions.
16 changes: 0 additions & 16 deletions k11_extension/source/svc.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ void postprocessSvc(void)
officialPostProcessSvc();
}

static bool doingVeryShittyPmResLimitWorkaround = false; // I feel dirty

void *svcHook(u8 *pageEnd)
{
KProcess *currentProcess = currentCoreContext->objectContext.currentProcess;
Expand All @@ -97,13 +95,6 @@ void *svcHook(u8 *pageEnd)
{
case 0x01:
return ControlMemoryHookWrapper;
case 0x17:
if(strcmp(codeSetOfProcess(currentProcess)->processName, "pm") == 0) // only called twice in pm, by the same function
{
*(vu32 *)(configPage + 0x44) += __end__ - __start__;
doingVeryShittyPmResLimitWorkaround = true;
}
return officialSVCs[0x17];
case 0x29:
return GetHandleInfoHookWrapper;
case 0x2A:
Expand All @@ -124,13 +115,6 @@ void *svcHook(u8 *pageEnd)
return SetGpuProt;
case 0x5A:
return SetWifiEnabled;
case 0x79:
if(doingVeryShittyPmResLimitWorkaround)
{
*(vu32 *)(configPage + 0x44) -= __end__ - __start__;
doingVeryShittyPmResLimitWorkaround = false;
}
return officialSVCs[0x79];
case 0x7B:
return Backdoor;
case 0x7C:
Expand Down
26 changes: 4 additions & 22 deletions sysmodules/loader/source/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,6 @@ static u64 g_cached_prog_handle;
static ExHeader_Info g_exheader;
static char g_ret_buf[1024];

// MAKE SURE fsreg has been init before calling this
static Result fsldrPatchPermissions(void)
{
u32 pid;
Result res;
FS_ProgramInfo info;
ExHeader_Arm11StorageInfo storageInfo = {
.fs_access_info = FSACCESS_NANDRW | FSACCESS_NANDRO_RO | FSACCESS_SDMC_RW,
};

info.programId = 0x0004013000001302LL; // loader PID
info.mediaType = MEDIATYPE_NAND;
res = svcGetProcessId(&pid, CUR_PROCESS_HANDLE);
if (R_SUCCEEDED(res))
{
res = FSREG_Register(pid, 0xFFFF000000000000LL, &info, &storageInfo);
}
return res;
}

static inline void loadCFWInfo(void)
{
s64 out;
Expand Down Expand Up @@ -585,8 +565,10 @@ void __appInit()
svcBreak(USERBREAK_PANIC);
}

fsRegInit();
fsldrPatchPermissions();
// Wait for pm to call fs:REG Register on us
bool registered = false;
while (srvIsServiceRegistered(&registered, "pm:app"), registered)
svcSleepThread(500 * 1000LL);

//fsldrInit();
res = srvGetServiceHandle(fsGetSessionHandle(), "fs:LDR");
Expand Down
11 changes: 0 additions & 11 deletions sysmodules/loader/source/patcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,17 +737,6 @@ void patchCode(u64 progId, u16 progVer, u8 *code, u32 size, u32 textSize, u32 ro

memset(start, 0, end - start);
}

s64 nbSection0Modules;
svcGetSystemInfo(&nbSection0Modules, 26, 0);

if(nbSection0Modules == 6)
{
// Makes ErrDisp to not start up
static const u64 errDispTid = 0x0004003000008A02ULL;
u32 *errDispTidLoc = (u32 *)memsearch(code, &errDispTid, size, sizeof(errDispTid));
*(errDispTidLoc - 6) = 0xE3A00000; // mov r0, #0
}
}

else if(progId == 0x0004013000001702LL) //CFG
Expand Down
8 changes: 7 additions & 1 deletion sysmodules/pm/source/launch.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "exheader_info_heap.h"
#include "task_runner.h"
#include "util.h"
#include "luma.h"

static inline void removeAccessToService(const char *service, char (*serviceAccessList)[8])
{
Expand Down Expand Up @@ -204,6 +205,11 @@ static Result loadWithDependencies(Handle *outDebug, ProcessData **outProcessDat
static Result launchTitleImpl(Handle *debug, ProcessData **outProcessData, const FS_ProgramInfo *programInfo,
const FS_ProgramInfo *programInfoUpdate, u32 launchFlags, ExHeader_Info *exheaderInfo)
{
if (isTitleLaunchPrevented(programInfo->programId)) {
*debug = 0;
return 0;
}

if (launchFlags & PMLAUNCHFLAG_NORMAL_APPLICATION) {
launchFlags |= PMLAUNCHFLAG_LOAD_DEPENDENCIES;
} else {
Expand Down Expand Up @@ -299,7 +305,7 @@ static Result launchTitleImplWrapper(Handle *outDebug, u32 *outPid, const FS_Pro
ProcessData *process;
Result res = launchTitleImpl(outDebug, &process, programInfo, programInfoUpdate, launchFlags, exheaderInfo);

if (outPid != NULL) {
if (outPid != NULL && process != NULL) {
*outPid = process->pid;
}

Expand Down
34 changes: 34 additions & 0 deletions sysmodules/pm/source/luma.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <3ds.h>
#include <string.h>
#include "luma.h"
#include "util.h"

u32 getKExtSize(void)
{
s64 val;
Result res = svcGetSystemInfo(&val, 0x10000, 0x300);
return R_FAILED(res) ? 0 : (u32)val;
}

bool isTitleLaunchPrevented(u64 titleId)
{
s64 numKips = 0;

svcGetSystemInfo(&numKips, 26, 0);
return numKips >= 6 && (titleId & ~N3DS_TID_BIT) == 0x0004003000008A02ULL; // ErrDisp
}

Result fsRegSetupPermissionsForKip(u32 pid, u64 titleId)
{
FS_ProgramInfo info;

ExHeader_Arm11StorageInfo storageInfo = {
.fs_access_info = FSACCESS_NANDRO_RW | FSACCESS_NANDRW | FSACCESS_SDMC_RW,
};

// Non-dummy TID
info.programId = titleId;
info.mediaType = MEDIATYPE_NAND;

return FSREG_Register(pid, 0xFFFF000000000000LL, &info, &storageInfo);;
}
7 changes: 7 additions & 0 deletions sysmodules/pm/source/luma.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <3ds/types.h>

u32 getKExtSize(void);
bool isTitleLaunchPrevented(u64 titleId);
Result fsRegSetupPermissionsForKip(u32 pid, u64 titleId);
8 changes: 8 additions & 0 deletions sysmodules/pm/source/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "manager.h"
#include "reslimit.h"
#include "util.h"
#include "luma.h"

Manager g_manager;

Expand Down Expand Up @@ -41,6 +42,13 @@ void Manager_RegisterKips(void)
}

ProcessList_Unlock(&g_manager.processList);

// Register loader, pm, and rosalina (if applicable)
assertSuccess(fsRegSetupPermissionsForKip(1, 0x0004013000001302LL)); // loader
assertSuccess(fsRegSetupPermissionsForKip(2, 0x0004013000001202LL)); // pm
if (numKips >= 6) {
assertSuccess(fsRegSetupPermissionsForKip(5, 0x0004013000006902LL)); // rosalina
}
}

Result UnregisterProcess(u64 titleId)
Expand Down
3 changes: 2 additions & 1 deletion sysmodules/pm/source/reslimit.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "reslimit.h"
#include "util.h"
#include "manager.h"
#include "luma.h"

typedef s64 ReslimitValues[10];

Expand Down Expand Up @@ -242,7 +243,7 @@ static ReslimitValues *fixupReslimitValues(void)
{
// In order: APPLICATION, SYS_APPLET, LIB_APPLET, OTHER
// Fixup "commit" reslimit
u32 sysmemalloc = SYSMEMALLOC;
u32 sysmemalloc = SYSMEMALLOC + getKExtSize();
ReslimitValues *values = !IS_N3DS ? g_o3dsReslimitValues : g_n3dsReslimitValues;

static const u32 minAppletMemAmount = 0x1200000;
Expand Down
40 changes: 6 additions & 34 deletions sysmodules/rosalina/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,6 @@
#include "menus/miscellaneous.h"
#include "menus/screen_filters.h"

static Result stealFsReg(void)
{
Result ret = 0;

ret = svcControlService(SERVICEOP_STEAL_CLIENT_SESSION, fsRegGetSessionHandle(), "fs:REG");
while(ret == 0x9401BFE)
{
svcSleepThread(500 * 1000LL);
ret = svcControlService(SERVICEOP_STEAL_CLIENT_SESSION, fsRegGetSessionHandle(), "fs:REG");
}

return ret;
}

static Result fsRegSetupPermissions(void)
{
u32 pid;
Result res;
FS_ProgramInfo info;

ExHeader_Arm11StorageInfo storageInfo = {
.fs_access_info = FSACCESS_NANDRO_RW | FSACCESS_NANDRW | FSACCESS_SDMC_RW,
};

info.programId = 0x0004013000006902LL; // Rosalina TID
info.mediaType = MEDIATYPE_NAND;

if(R_SUCCEEDED(res = svcGetProcessId(&pid, CUR_PROCESS_HANDLE)))
res = FSREG_Register(pid, 0xFFFF000000000000LL, &info, &storageInfo);

return res;
}

// this is called before main
bool isN3DS;
void __appInit()
Expand All @@ -82,7 +49,12 @@ void __appInit()
svcBreak(USERBREAK_PANIC);
}

if (R_FAILED(stealFsReg()) || R_FAILED(fsRegSetupPermissions()) || R_FAILED(fsInit()))
// Wait for pm to call fs:REG Register on us
bool registered = false;
while (srvIsServiceRegistered(&registered, "pm:app"), registered)
svcSleepThread(500 * 1000LL);

if (R_FAILED(fsInit()))
svcBreak(USERBREAK_PANIC);
}

Expand Down

0 comments on commit e688ec9

Please sign in to comment.