From a21eee9207e39947f5b9340e107748349efcd374 Mon Sep 17 00:00:00 2001 From: TuxSH <1922548+TuxSH@users.noreply.github.com> Date: Fri, 15 May 2020 22:15:55 +0100 Subject: [PATCH] rosalina/pm: remove fs patch, use pm instead --- sysmodules/pm/source/launch.c | 6 ++- sysmodules/pm/source/manager.c | 25 ++++++++- sysmodules/pm/source/termination.c | 20 -------- .../include/{menus => }/process_patches.h | 2 +- sysmodules/rosalina/include/utils.h | 2 - sysmodules/rosalina/source/hbloader.c | 2 +- .../rosalina/source/input_redirection.c | 1 + sysmodules/rosalina/source/main.c | 51 +++---------------- sysmodules/rosalina/source/menus.c | 1 - .../rosalina/source/menus/process_list.c | 1 + .../source/{menus => }/process_patches.c | 50 ++---------------- 11 files changed, 44 insertions(+), 117 deletions(-) rename sysmodules/rosalina/include/{menus => }/process_patches.h (95%) rename sysmodules/rosalina/source/{menus => }/process_patches.c (67%) diff --git a/sysmodules/pm/source/launch.c b/sysmodules/pm/source/launch.c index e5720e9..be6accf 100644 --- a/sysmodules/pm/source/launch.c +++ b/sysmodules/pm/source/launch.c @@ -73,7 +73,11 @@ static Result loadWithoutDependencies(Handle *outDebug, ProcessData **outProcess u32 serviceCount; for(serviceCount = 0; serviceCount < 34 && *(u64 *)localcaps->service_access[serviceCount] != 0; serviceCount++); - TRY(FSREG_Register(pid, programHandle, programInfo, &localcaps->storage_info)); + // Not in official PM: patch local caps to give access to everything + ExHeader_Arm11StorageInfo storageInfo = localcaps->storage_info; + storageInfo.fs_access_info = 0xFFFFFFFF; + + TRY(FSREG_Register(pid, programHandle, programInfo, &storageInfo)); TRY(SRVPM_RegisterProcess(pid, serviceCount, localcaps->service_access)); if (localcaps->reslimit_category <= RESLIMIT_CATEGORY_OTHER) { diff --git a/sysmodules/pm/source/manager.c b/sysmodules/pm/source/manager.c index bd698af..b527db6 100644 --- a/sysmodules/pm/source/manager.c +++ b/sysmodules/pm/source/manager.c @@ -7,6 +7,21 @@ Manager g_manager; +static void giveAllFsArchiveAccessToKip(u32 pid, u64 tid) +{ + static const ExHeader_Arm11StorageInfo storageInfo = { + .fs_access_info = 0xFFFFFFFF, + }; + static const u64 programHandle = 0xFFFF000000000000LL; + + FS_ProgramInfo info = { + .programId = tid, + .mediaType = MEDIATYPE_NAND, + }; + + assertSuccess(FSREG_Register(pid, programHandle, &info, &storageInfo)); +} + void Manager_Init(void *procBuf, size_t numProc) { memset(&g_manager, 0, sizeof(Manager)); @@ -34,7 +49,7 @@ void Manager_RegisterKips(void) process->handle = processHandle; process->pid = i; process->refcount = 1; - process->titleId = 0x0004000100001000ULL; // note: same TID for all builtins + process->titleId = 0x0004000100001000ULL; // note: same internal TID for all builtins process->flags = PROCESSFLAG_KIP; process->terminationStatus = TERMSTATUS_RUNNING; @@ -43,8 +58,14 @@ void Manager_RegisterKips(void) assertSuccess(svcSetProcessResourceLimits(processHandle, g_manager.reslimits[RESLIMIT_CATEGORY_OTHER])); } } - ProcessList_Unlock(&g_manager.processList); + + // Give full archive access to us (PM) and Rosalina (real PIDs don't matter, they just have to be unique (?)) + // Loader doesn't depend on PM and has its own fs:REG handle so it must do it itself. + giveAllFsArchiveAccessToKip(2, 0x0004013000001202LL); // PM + if (numKips > 5) { + giveAllFsArchiveAccessToKip(5, 0x0004013000006902LL); // Rosalina + } } Result UnregisterProcess(u64 titleId) diff --git a/sysmodules/pm/source/termination.c b/sysmodules/pm/source/termination.c index 72241ca..156109c 100644 --- a/sysmodules/pm/source/termination.c +++ b/sysmodules/pm/source/termination.c @@ -6,30 +6,10 @@ #include "exheader_info_heap.h" #include "task_runner.h" -static Result fsRegSetupPermissions(void) -{ - u32 pid; - Result res; - FS_ProgramInfo info; - - ExHeader_Arm11StorageInfo storageInfo = { - .fs_access_info = FSACCESS_SDMC_RW, - }; - - info.programId = 0x0004013000001202LL; // PM's TID - info.mediaType = MEDIATYPE_NAND; - - if(R_SUCCEEDED(res = svcGetProcessId(&pid, CUR_PROCESS_HANDLE))) - res = FSREG_Register(pid, 0xFFFF000000000000LL, &info, &storageInfo); - - return res; -} - void forceMountSdCard(void) { FS_Archive sdmcArchive; - assertSuccess(fsRegSetupPermissions()); assertSuccess(fsInit()); assertSuccess(FSUSER_OpenArchive(&sdmcArchive, ARCHIVE_SDMC, fsMakePath(PATH_EMPTY, ""))); // No need to clean up things as we will firmlaunch straight away diff --git a/sysmodules/rosalina/include/menus/process_patches.h b/sysmodules/rosalina/include/process_patches.h similarity index 95% rename from sysmodules/rosalina/include/menus/process_patches.h rename to sysmodules/rosalina/include/process_patches.h index 6ce5496..698efb7 100644 --- a/sysmodules/rosalina/include/menus/process_patches.h +++ b/sysmodules/rosalina/include/process_patches.h @@ -29,4 +29,4 @@ #include <3ds/types.h> #include "menu.h" -void ProcessPatchesMenu_PatchUnpatchFSDirectly(void); +Result OpenProcessByName(const char *name, Handle *h); diff --git a/sysmodules/rosalina/include/utils.h b/sysmodules/rosalina/include/utils.h index c48fcc7..ae51b9e 100644 --- a/sysmodules/rosalina/include/utils.h +++ b/sysmodules/rosalina/include/utils.h @@ -62,5 +62,3 @@ static inline bool isServiceUsable(const char *name) bool r; return R_SUCCEEDED(srvIsServiceRegistered(&r, name)) && r; } - -Result OpenProcessByName(const char *name, Handle *h); diff --git a/sysmodules/rosalina/source/hbloader.c b/sysmodules/rosalina/source/hbloader.c index 8d2959b..de2c08d 100644 --- a/sysmodules/rosalina/source/hbloader.c +++ b/sysmodules/rosalina/source/hbloader.c @@ -346,7 +346,7 @@ void HBLDR_HandleCommands(void *ctx) // See the big comment in sysmodules/pm/source/reslimit.c for technical details. localcaps0->reslimits[0] = BIT(7) | 89; - localcaps0->storage_info.fs_access_info = 0xFFFFFFFF; // Give access to everything + //localcaps0->storage_info.fs_access_info = 0xFFFFFFFF; // Give access to everything localcaps0->storage_info.no_romfs = true; localcaps0->storage_info.use_extended_savedata_access = true; // Whatever diff --git a/sysmodules/rosalina/source/input_redirection.c b/sysmodules/rosalina/source/input_redirection.c index 2c498d0..631cca4 100644 --- a/sysmodules/rosalina/source/input_redirection.c +++ b/sysmodules/rosalina/source/input_redirection.c @@ -29,6 +29,7 @@ #include "utils.h" // for makeArmBranch #include "minisoc.h" #include "input_redirection.h" +#include "process_patches.h" #include "menus.h" #include "memory.h" diff --git a/sysmodules/rosalina/source/main.c b/sysmodules/rosalina/source/main.c index af6ea94..c835049 100644 --- a/sysmodules/rosalina/source/main.c +++ b/sysmodules/rosalina/source/main.c @@ -33,7 +33,6 @@ #include "3dsx.h" #include "utils.h" #include "MyThread.h" -#include "menus/process_patches.h" #include "menus/miscellaneous.h" #include "menus/debugger.h" #include "menus/screen_filters.h" @@ -47,39 +46,6 @@ bool isN3DS; -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; -} - Result __sync_init(void); Result __sync_fini(void); void __libc_init_array(void); @@ -124,8 +90,6 @@ void initSystem(void) miscellaneousMenu.items[0].title = HBLDR_3DSX_TID == HBLDR_DEFAULT_3DSX_TID ? "Switch the hb. title to the current app." : "Switch the hb. title to hblauncher_loader"; - ProcessPatchesMenu_PatchUnpatchFSDirectly(); - for(res = 0xD88007FA; res == (Result)0xD88007FA; svcSleepThread(500 * 1000LL)) { res = srvInit(); @@ -133,10 +97,10 @@ void initSystem(void) svcBreak(USERBREAK_PANIC); } - if (R_FAILED(stealFsReg()) || R_FAILED(fsRegSetupPermissions()) || R_FAILED(fsInit())) + if (R_FAILED(pmAppInit()) || R_FAILED(pmDbgInit())) svcBreak(USERBREAK_PANIC); - if (R_FAILED(pmAppInit()) || R_FAILED(pmDbgInit())) + if (R_FAILED(fsInit())) svcBreak(USERBREAK_PANIC); // **** DO NOT init services that don't come from KIPs here **** @@ -144,14 +108,15 @@ void initSystem(void) __libc_init_array(); - // ROSALINA HACKJOB BEGIN - // NORMAL APPS SHOULD NOT DO THIS, EVER - u32 *tls = (u32 *)getThreadLocalStorage(); - memset(tls, 0, 0x80); - tls[0] = 0x21545624; // ROSALINA HACKJOB END // Rosalina specific: + u32 *tls = (u32 *)getThreadLocalStorage(); + memset(tls, 0, 0x80); + tls[0] = 0x21545624; + + // ROSALINA HACKJOB BEGIN + // NORMAL APPS SHOULD NOT DO THIS, EVER srvSetBlockingPolicy(true); // GetServiceHandle nonblocking if service port is full } diff --git a/sysmodules/rosalina/source/menus.c b/sysmodules/rosalina/source/menus.c index f978f63..8870768 100644 --- a/sysmodules/rosalina/source/menus.c +++ b/sysmodules/rosalina/source/menus.c @@ -30,7 +30,6 @@ #include "menu.h" #include "draw.h" #include "menus/process_list.h" -#include "menus/process_patches.h" #include "menus/n3ds.h" #include "menus/debugger.h" #include "menus/miscellaneous.h" diff --git a/sysmodules/rosalina/source/menus/process_list.c b/sysmodules/rosalina/source/menus/process_list.c index 096cc8a..b2c9a74 100644 --- a/sysmodules/rosalina/source/menus/process_list.c +++ b/sysmodules/rosalina/source/menus/process_list.c @@ -26,6 +26,7 @@ #include <3ds.h> #include "menus/process_list.h" +#include "process_patches.h" #include "memory.h" #include "csvc.h" #include "draw.h" diff --git a/sysmodules/rosalina/source/menus/process_patches.c b/sysmodules/rosalina/source/process_patches.c similarity index 67% rename from sysmodules/rosalina/source/menus/process_patches.c rename to sysmodules/rosalina/source/process_patches.c index 4f9b39a..3535780 100644 --- a/sysmodules/rosalina/source/menus/process_patches.c +++ b/sysmodules/rosalina/source/process_patches.c @@ -25,48 +25,11 @@ */ #include <3ds.h> +#include #include "csvc.h" -#include "menus/process_patches.h" -#include "memory.h" -#include "draw.h" -#include "hbloader.h" -#include "fmt.h" +#include "process_patches.h" #include "utils.h" -static Result ProcessPatchesMenu_DoPatchUnpatchFS(u32 textTotalRoundedSize) -{ - static bool patched = false; - static u16 *off; - static u16 origData[2]; - static const u16 pattern[2] = { - 0x7401, // strb r1, [r0, #16] - 0x2000, // movs r0, #0 - }; - - if(patched) - { - memcpy(off, &origData, sizeof(origData)); - patched = false; - } - else - { - off = (u16 *)memsearch((u8 *)0x00100000, &pattern, textTotalRoundedSize, sizeof(pattern)); - if(off == NULL) - return -1; - - for(; (*off & 0xFF00) != 0xB500; off++); // Find function start - - memcpy(origData, off, 4); - off[0] = 0x2001; // mov r0, #1 - off[1] = 0x4770; // bx lr - - patched = true; - } - - //processPatchesMenu.items[1].title = patched ? "Unpatch FS for the archive checks" : "Patch FS for the archive checks"; - return 0; -} - Result OpenProcessByName(const char *name, Handle *h) { u32 pidList[0x40]; @@ -96,7 +59,7 @@ Result OpenProcessByName(const char *name, Handle *h) return 0; } -static u32 ProcessPatchesMenu_PatchUnpatchProcessByName(const char *name, Result (*func)(u32 size)) +/*static u32 ProcessPatchesMenu_PatchUnpatchProcessByName(const char *name, Result (*func)(u32 size)) { Result res; Handle processHandle; @@ -112,9 +75,4 @@ static u32 ProcessPatchesMenu_PatchUnpatchProcessByName(const char *name, Result svcUnmapProcessMemoryEx(processHandle, 0x00100000, textTotalRoundedSize); return res; -} - -void ProcessPatchesMenu_PatchUnpatchFSDirectly(void) -{ - ProcessPatchesMenu_PatchUnpatchProcessByName("fs", &ProcessPatchesMenu_DoPatchUnpatchFS); -} +}*/