Skip to content

Commit e688ec9

Browse files
committed
move patches to pm
1 parent ae1f85d commit e688ec9

9 files changed

Lines changed: 68 additions & 85 deletions

File tree

k11_extension/source/svc.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ void postprocessSvc(void)
8484
officialPostProcessSvc();
8585
}
8686

87-
static bool doingVeryShittyPmResLimitWorkaround = false; // I feel dirty
88-
8987
void *svcHook(u8 *pageEnd)
9088
{
9189
KProcess *currentProcess = currentCoreContext->objectContext.currentProcess;
@@ -97,13 +95,6 @@ void *svcHook(u8 *pageEnd)
9795
{
9896
case 0x01:
9997
return ControlMemoryHookWrapper;
100-
case 0x17:
101-
if(strcmp(codeSetOfProcess(currentProcess)->processName, "pm") == 0) // only called twice in pm, by the same function
102-
{
103-
*(vu32 *)(configPage + 0x44) += __end__ - __start__;
104-
doingVeryShittyPmResLimitWorkaround = true;
105-
}
106-
return officialSVCs[0x17];
10798
case 0x29:
10899
return GetHandleInfoHookWrapper;
109100
case 0x2A:
@@ -124,13 +115,6 @@ void *svcHook(u8 *pageEnd)
124115
return SetGpuProt;
125116
case 0x5A:
126117
return SetWifiEnabled;
127-
case 0x79:
128-
if(doingVeryShittyPmResLimitWorkaround)
129-
{
130-
*(vu32 *)(configPage + 0x44) -= __end__ - __start__;
131-
doingVeryShittyPmResLimitWorkaround = false;
132-
}
133-
return officialSVCs[0x79];
134118
case 0x7B:
135119
return Backdoor;
136120
case 0x7C:

sysmodules/loader/source/loader.c

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,6 @@ static u64 g_cached_prog_handle;
2828
static ExHeader_Info g_exheader;
2929
static char g_ret_buf[1024];
3030

31-
// MAKE SURE fsreg has been init before calling this
32-
static Result fsldrPatchPermissions(void)
33-
{
34-
u32 pid;
35-
Result res;
36-
FS_ProgramInfo info;
37-
ExHeader_Arm11StorageInfo storageInfo = {
38-
.fs_access_info = FSACCESS_NANDRW | FSACCESS_NANDRO_RO | FSACCESS_SDMC_RW,
39-
};
40-
41-
info.programId = 0x0004013000001302LL; // loader PID
42-
info.mediaType = MEDIATYPE_NAND;
43-
res = svcGetProcessId(&pid, CUR_PROCESS_HANDLE);
44-
if (R_SUCCEEDED(res))
45-
{
46-
res = FSREG_Register(pid, 0xFFFF000000000000LL, &info, &storageInfo);
47-
}
48-
return res;
49-
}
50-
5131
static inline void loadCFWInfo(void)
5232
{
5333
s64 out;
@@ -585,8 +565,10 @@ void __appInit()
585565
svcBreak(USERBREAK_PANIC);
586566
}
587567

588-
fsRegInit();
589-
fsldrPatchPermissions();
568+
// Wait for pm to call fs:REG Register on us
569+
bool registered = false;
570+
while (srvIsServiceRegistered(&registered, "pm:app"), registered)
571+
svcSleepThread(500 * 1000LL);
590572

591573
//fsldrInit();
592574
res = srvGetServiceHandle(fsGetSessionHandle(), "fs:LDR");

sysmodules/loader/source/patcher.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -737,17 +737,6 @@ void patchCode(u64 progId, u16 progVer, u8 *code, u32 size, u32 textSize, u32 ro
737737

738738
memset(start, 0, end - start);
739739
}
740-
741-
s64 nbSection0Modules;
742-
svcGetSystemInfo(&nbSection0Modules, 26, 0);
743-
744-
if(nbSection0Modules == 6)
745-
{
746-
// Makes ErrDisp to not start up
747-
static const u64 errDispTid = 0x0004003000008A02ULL;
748-
u32 *errDispTidLoc = (u32 *)memsearch(code, &errDispTid, size, sizeof(errDispTid));
749-
*(errDispTidLoc - 6) = 0xE3A00000; // mov r0, #0
750-
}
751740
}
752741

753742
else if(progId == 0x0004013000001702LL) //CFG

sysmodules/pm/source/launch.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "exheader_info_heap.h"
88
#include "task_runner.h"
99
#include "util.h"
10+
#include "luma.h"
1011

1112
static inline void removeAccessToService(const char *service, char (*serviceAccessList)[8])
1213
{
@@ -204,6 +205,11 @@ static Result loadWithDependencies(Handle *outDebug, ProcessData **outProcessDat
204205
static Result launchTitleImpl(Handle *debug, ProcessData **outProcessData, const FS_ProgramInfo *programInfo,
205206
const FS_ProgramInfo *programInfoUpdate, u32 launchFlags, ExHeader_Info *exheaderInfo)
206207
{
208+
if (isTitleLaunchPrevented(programInfo->programId)) {
209+
*debug = 0;
210+
return 0;
211+
}
212+
207213
if (launchFlags & PMLAUNCHFLAG_NORMAL_APPLICATION) {
208214
launchFlags |= PMLAUNCHFLAG_LOAD_DEPENDENCIES;
209215
} else {
@@ -299,7 +305,7 @@ static Result launchTitleImplWrapper(Handle *outDebug, u32 *outPid, const FS_Pro
299305
ProcessData *process;
300306
Result res = launchTitleImpl(outDebug, &process, programInfo, programInfoUpdate, launchFlags, exheaderInfo);
301307

302-
if (outPid != NULL) {
308+
if (outPid != NULL && process != NULL) {
303309
*outPid = process->pid;
304310
}
305311

sysmodules/pm/source/luma.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <3ds.h>
2+
#include <string.h>
3+
#include "luma.h"
4+
#include "util.h"
5+
6+
u32 getKExtSize(void)
7+
{
8+
s64 val;
9+
Result res = svcGetSystemInfo(&val, 0x10000, 0x300);
10+
return R_FAILED(res) ? 0 : (u32)val;
11+
}
12+
13+
bool isTitleLaunchPrevented(u64 titleId)
14+
{
15+
s64 numKips = 0;
16+
17+
svcGetSystemInfo(&numKips, 26, 0);
18+
return numKips >= 6 && (titleId & ~N3DS_TID_BIT) == 0x0004003000008A02ULL; // ErrDisp
19+
}
20+
21+
Result fsRegSetupPermissionsForKip(u32 pid, u64 titleId)
22+
{
23+
FS_ProgramInfo info;
24+
25+
ExHeader_Arm11StorageInfo storageInfo = {
26+
.fs_access_info = FSACCESS_NANDRO_RW | FSACCESS_NANDRW | FSACCESS_SDMC_RW,
27+
};
28+
29+
// Non-dummy TID
30+
info.programId = titleId;
31+
info.mediaType = MEDIATYPE_NAND;
32+
33+
return FSREG_Register(pid, 0xFFFF000000000000LL, &info, &storageInfo);;
34+
}

sysmodules/pm/source/luma.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include <3ds/types.h>
4+
5+
u32 getKExtSize(void);
6+
bool isTitleLaunchPrevented(u64 titleId);
7+
Result fsRegSetupPermissionsForKip(u32 pid, u64 titleId);

sysmodules/pm/source/manager.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "manager.h"
44
#include "reslimit.h"
55
#include "util.h"
6+
#include "luma.h"
67

78
Manager g_manager;
89

@@ -41,6 +42,13 @@ void Manager_RegisterKips(void)
4142
}
4243

4344
ProcessList_Unlock(&g_manager.processList);
45+
46+
// Register loader, pm, and rosalina (if applicable)
47+
assertSuccess(fsRegSetupPermissionsForKip(1, 0x0004013000001302LL)); // loader
48+
assertSuccess(fsRegSetupPermissionsForKip(2, 0x0004013000001202LL)); // pm
49+
if (numKips >= 6) {
50+
assertSuccess(fsRegSetupPermissionsForKip(5, 0x0004013000006902LL)); // rosalina
51+
}
4452
}
4553

4654
Result UnregisterProcess(u64 titleId)

sysmodules/pm/source/reslimit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "reslimit.h"
33
#include "util.h"
44
#include "manager.h"
5+
#include "luma.h"
56

67
typedef s64 ReslimitValues[10];
78

@@ -242,7 +243,7 @@ static ReslimitValues *fixupReslimitValues(void)
242243
{
243244
// In order: APPLICATION, SYS_APPLET, LIB_APPLET, OTHER
244245
// Fixup "commit" reslimit
245-
u32 sysmemalloc = SYSMEMALLOC;
246+
u32 sysmemalloc = SYSMEMALLOC + getKExtSize();
246247
ReslimitValues *values = !IS_N3DS ? g_o3dsReslimitValues : g_n3dsReslimitValues;
247248

248249
static const u32 minAppletMemAmount = 0x1200000;

sysmodules/rosalina/source/main.c

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,39 +37,6 @@
3737
#include "menus/miscellaneous.h"
3838
#include "menus/screen_filters.h"
3939

40-
static Result stealFsReg(void)
41-
{
42-
Result ret = 0;
43-
44-
ret = svcControlService(SERVICEOP_STEAL_CLIENT_SESSION, fsRegGetSessionHandle(), "fs:REG");
45-
while(ret == 0x9401BFE)
46-
{
47-
svcSleepThread(500 * 1000LL);
48-
ret = svcControlService(SERVICEOP_STEAL_CLIENT_SESSION, fsRegGetSessionHandle(), "fs:REG");
49-
}
50-
51-
return ret;
52-
}
53-
54-
static Result fsRegSetupPermissions(void)
55-
{
56-
u32 pid;
57-
Result res;
58-
FS_ProgramInfo info;
59-
60-
ExHeader_Arm11StorageInfo storageInfo = {
61-
.fs_access_info = FSACCESS_NANDRO_RW | FSACCESS_NANDRW | FSACCESS_SDMC_RW,
62-
};
63-
64-
info.programId = 0x0004013000006902LL; // Rosalina TID
65-
info.mediaType = MEDIATYPE_NAND;
66-
67-
if(R_SUCCEEDED(res = svcGetProcessId(&pid, CUR_PROCESS_HANDLE)))
68-
res = FSREG_Register(pid, 0xFFFF000000000000LL, &info, &storageInfo);
69-
70-
return res;
71-
}
72-
7340
// this is called before main
7441
bool isN3DS;
7542
void __appInit()
@@ -82,7 +49,12 @@ void __appInit()
8249
svcBreak(USERBREAK_PANIC);
8350
}
8451

85-
if (R_FAILED(stealFsReg()) || R_FAILED(fsRegSetupPermissions()) || R_FAILED(fsInit()))
52+
// Wait for pm to call fs:REG Register on us
53+
bool registered = false;
54+
while (srvIsServiceRegistered(&registered, "pm:app"), registered)
55+
svcSleepThread(500 * 1000LL);
56+
57+
if (R_FAILED(fsInit()))
8658
svcBreak(USERBREAK_PANIC);
8759
}
8860

0 commit comments

Comments
 (0)