Skip to content

Instantly share code, notes, and snippets.

@archshift
Last active August 29, 2015 14:17
Show Gist options
  • Save archshift/381748149dd21887db17 to your computer and use it in GitHub Desktop.
Save archshift/381748149dd21887db17 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <3ds.h>
static const u32 KPROC_OFFSET_PID = 0xB4;
static u32 self_pid;
static u32 curr_kproc;
int __attribute__((naked))
arm11_kernel_execute(int (*func)(void))
{
asm volatile ("svc #0x7B \t\n"
"bx lr \t\n");
}
int patch_pid() {
// 0xFFFF9004 always points to the current KProcess
curr_kproc = *(u32*)0xFFFF9004;
*(u32*)(curr_kproc + KPROC_OFFSET_PID) = 0;
return 0;
}
int unpatch_pid() {
*(u32*)(curr_kproc + KPROC_OFFSET_PID) = self_pid;
return 0;
}
void reinit_srv() {
srvExit();
srvInit();
}
void patch_srv_access() {
svcGetProcessId(&self_pid, 0xFFFF8001);
printf("Current process id: %lu\n", self_pid);
printf("Patching srv access...");
arm11_kernel_execute(patch_pid);
reinit_srv();
u32 new_pid;
svcGetProcessId(&new_pid, 0xFFFF8001);
printf("%s\n", new_pid == 0 ? "succeeded!" : "failed!");
// Cleanup; won't take effect until srv is reinitialized
arm11_kernel_execute(unpatch_pid);
}
int main(int argc, char** argv)
{
gfxInitDefault();
consoleInit(GFX_TOP, NULL);
patch_srv_access();
// Insert fun service stuff here
// Main loop
while (aptMainLoop())
{
hidScanInput();
u32 kDown = hidKeysDown();
if (kDown & KEY_START)
break;
gspWaitForVBlank();
}
gfxExit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment