Skip to content

Instantly share code, notes, and snippets.

@archshift
Created April 2, 2015 22:51
Show Gist options
  • Save archshift/e460014a2bf893e9020c to your computer and use it in GitHub Desktop.
Save archshift/e460014a2bf893e9020c to your computer and use it in GitHub Desktop.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <3ds.h>
#define wait() svcSleepThread(2000000000ull)
static const u32 KPROC_OFFSET_PID = 0xB4;
static u32 self_pid;
static u32 curr_kproc;
s32 __attribute__((naked))
patch_pid() {
asm volatile ("cpsid aif \t\n");
// 0xFFFF9004 always points to the current KProcess
curr_kproc = *(u32*)0xFFFF9004;
*(u32*)(curr_kproc + KPROC_OFFSET_PID) = 0;
return 0;
}
s32 __attribute__((naked))
unpatch_pid() {
asm volatile ("cpsid aif \t\n");
*(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...");
wait();
svcBackdoor(patch_pid);
reinit_srv();
u32 new_pid;
svcGetProcessId(&new_pid, 0xFFFF8001);
printf("%s\n", new_pid == 0 ? "succeeded!" : "failed!");
wait();
// Cleanup; won't take effect until srv is reinitialized
svcBackdoor(unpatch_pid);
}
void print_titles(u8 mediatype) {
Result rc = 0;
u32 cia_nums = 0;
rc = AM_GetTitleCount(mediatype, &cia_nums);
u64* title_ids = (u64*) malloc(cia_nums * sizeof(u64));
rc = AM_GetTitleIdList(mediatype, cia_nums, title_ids);
for (size_t i = 0; i < cia_nums; i++) {
char product_id[16];
AM_GetTitleProductCode(mediatype, title_ids[i], product_id);
printf("%*s: %016llX\n", 16, product_id, title_ids[i]);
}
}
int main(int argc, char** argv)
{
gfxInitDefault();
consoleInit(GFX_TOP, NULL);
patch_srv_access();
Result rc;
rc = amInit();
printf("0x%08X - amInit\n", rc);
print_titles(mediatype_SDMC);
print_titles(mediatype_NAND);
// Main loop
while (aptMainLoop())
{
hidScanInput();
u32 kDown = hidKeysDown();
if (kDown & KEY_START)
break;
gspWaitForVBlank();
}
amExit();
gfxExit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment