Lots of changes/new features
- To override the last used boot mode on soft reboot, you only need to press A if you want to boot to the default option. Holding L(+payload button)/R is enough for the other modes. - Added version number to the config menu - Replaced the memsearch algorithm with a faster one - Integrated 3ds_injector from @yifanlu. This brings us region free and all the other FreeMultiPatcher patches. Other than that, you now have the possibility to display the currently booted NAND/FIRM in System Settings! - Rewritten most code for the config menu. You now can navigate to the first/last options with left and right. - You can now choose the 9.0 FIRM to be default in the config menu. This will essentially switch "no buttons" and L in both modes. - You can now choose the second emuNAND to be default in the config menu. This will essentially switch "B is not pressed" and "B is pressed". - When the second emuNAND is booted, it will persist like the other boot options on soft reboot - Bugfixes
This commit is contained in:
97
injector/source/exheader.h
Executable file
97
injector/source/exheader.h
Executable file
@@ -0,0 +1,97 @@
|
||||
#pragma once
|
||||
|
||||
#include <3ds/types.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u8 reserved[5];
|
||||
u8 flag;
|
||||
u8 remasterversion[2];
|
||||
} PACKED exheader_systeminfoflags;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u32 address;
|
||||
u32 nummaxpages;
|
||||
u32 codesize;
|
||||
} PACKED exheader_codesegmentinfo;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u8 name[8];
|
||||
exheader_systeminfoflags flags;
|
||||
exheader_codesegmentinfo text;
|
||||
u8 stacksize[4];
|
||||
exheader_codesegmentinfo ro;
|
||||
u8 reserved[4];
|
||||
exheader_codesegmentinfo data;
|
||||
u32 bsssize;
|
||||
} PACKED exheader_codesetinfo;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u64 programid[0x30];
|
||||
} PACKED exheader_dependencylist;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u8 savedatasize[4];
|
||||
u8 reserved[4];
|
||||
u8 jumpid[8];
|
||||
u8 reserved2[0x30];
|
||||
} PACKED exheader_systeminfo;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u8 extsavedataid[8];
|
||||
u8 systemsavedataid[8];
|
||||
u8 reserved[8];
|
||||
u8 accessinfo[7];
|
||||
u8 otherattributes;
|
||||
} PACKED exheader_storageinfo;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u64 programid;
|
||||
u8 flags[8];
|
||||
u16 resourcelimitdescriptor[0x10];
|
||||
exheader_storageinfo storageinfo;
|
||||
u64 serviceaccesscontrol[0x20];
|
||||
u8 reserved[0x1f];
|
||||
u8 resourcelimitcategory;
|
||||
} PACKED exheader_arm11systemlocalcaps;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u32 descriptors[28];
|
||||
u8 reserved[0x10];
|
||||
} PACKED exheader_arm11kernelcapabilities;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u8 descriptors[15];
|
||||
u8 descversion;
|
||||
} PACKED exheader_arm9accesscontrol;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
// systemcontrol info {
|
||||
// coreinfo {
|
||||
exheader_codesetinfo codesetinfo;
|
||||
exheader_dependencylist deplist;
|
||||
// }
|
||||
exheader_systeminfo systeminfo;
|
||||
// }
|
||||
// accesscontrolinfo {
|
||||
exheader_arm11systemlocalcaps arm11systemlocalcaps;
|
||||
exheader_arm11kernelcapabilities arm11kernelcaps;
|
||||
exheader_arm9accesscontrol arm9accesscontrol;
|
||||
// }
|
||||
struct {
|
||||
u8 signature[0x100];
|
||||
u8 ncchpubkeymodulus[0x100];
|
||||
exheader_arm11systemlocalcaps arm11systemlocalcaps;
|
||||
exheader_arm11kernelcapabilities arm11kernelcaps;
|
||||
exheader_arm9accesscontrol arm9accesscontrol;
|
||||
} PACKED accessdesc;
|
||||
} PACKED exheader_header;
|
||||
109
injector/source/fsldr.c
Normal file
109
injector/source/fsldr.c
Normal file
@@ -0,0 +1,109 @@
|
||||
#include <3ds.h>
|
||||
#include "fsldr.h"
|
||||
#include "fsreg.h"
|
||||
#include "srvsys.h"
|
||||
|
||||
#define SDK_VERSION 0x70200C8
|
||||
|
||||
static Handle fsldrHandle;
|
||||
static int fsldrRefCount;
|
||||
|
||||
// MAKE SURE fsreg has been init before calling this
|
||||
static Result fsldrPatchPermissions(void)
|
||||
{
|
||||
u32 pid;
|
||||
Result res;
|
||||
FS_ProgramInfo info;
|
||||
u32 storage[8] = {0};
|
||||
|
||||
storage[6] = 0x680; // SDMC access and NAND access flag
|
||||
info.programId = 0x0004013000001302LL; // loader PID
|
||||
info.mediaType = MEDIATYPE_NAND;
|
||||
res = svcGetProcessId(&pid, 0xFFFF8001);
|
||||
if (R_SUCCEEDED(res))
|
||||
{
|
||||
res = FSREG_Register(pid, 0xFFFF000000000000LL, &info, (u8 *)storage);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
Result fsldrInit(void)
|
||||
{
|
||||
Result ret = 0;
|
||||
|
||||
if (AtomicPostIncrement(&fsldrRefCount)) return 0;
|
||||
|
||||
ret = srvSysGetServiceHandle(&fsldrHandle, "fs:LDR");
|
||||
if (R_SUCCEEDED(ret))
|
||||
{
|
||||
fsldrPatchPermissions();
|
||||
ret = FSLDR_InitializeWithSdkVersion(fsldrHandle, SDK_VERSION);
|
||||
ret = FSLDR_SetPriority(0);
|
||||
if (R_FAILED(ret)) svcBreak(USERBREAK_ASSERT);
|
||||
}
|
||||
else
|
||||
{
|
||||
AtomicDecrement(&fsldrRefCount);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void fsldrExit(void)
|
||||
{
|
||||
if (AtomicDecrement(&fsldrRefCount)) return;
|
||||
svcCloseHandle(fsldrHandle);
|
||||
}
|
||||
|
||||
Result FSLDR_InitializeWithSdkVersion(Handle session, u32 version)
|
||||
{
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x861,1,2); // 0x8610042
|
||||
cmdbuf[1] = version;
|
||||
cmdbuf[2] = 32;
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(session))) return ret;
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result FSLDR_SetPriority(u32 priority)
|
||||
{
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x862,1,0); // 0x8620040
|
||||
cmdbuf[1] = priority;
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(fsldrHandle))) return ret;
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result FSLDR_OpenFileDirectly(Handle* out, FS_Archive archive, FS_Path path, u32 openFlags, u32 attributes)
|
||||
{
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x803,8,4); // 0x8030204
|
||||
cmdbuf[1] = 0;
|
||||
cmdbuf[2] = archive.id;
|
||||
cmdbuf[3] = archive.lowPath.type;
|
||||
cmdbuf[4] = archive.lowPath.size;
|
||||
cmdbuf[5] = path.type;
|
||||
cmdbuf[6] = path.size;
|
||||
cmdbuf[7] = openFlags;
|
||||
cmdbuf[8] = attributes;
|
||||
cmdbuf[9] = IPC_Desc_StaticBuffer(archive.lowPath.size, 2);
|
||||
cmdbuf[10] = (u32) archive.lowPath.data;
|
||||
cmdbuf[11] = IPC_Desc_StaticBuffer(path.size, 0);
|
||||
cmdbuf[12] = (u32) path.data;
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(fsldrHandle))) return ret;
|
||||
|
||||
if(out) *out = cmdbuf[3];
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
9
injector/source/fsldr.h
Normal file
9
injector/source/fsldr.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <3ds/types.h>
|
||||
|
||||
Result fsldrInit(void);
|
||||
void fsldrExit(void);
|
||||
Result FSLDR_InitializeWithSdkVersion(Handle session, u32 version);
|
||||
Result FSLDR_SetPriority(u32 priority);
|
||||
Result FSLDR_OpenFileDirectly(Handle* out, FS_Archive archive, FS_Path path, u32 openFlags, u32 attributes);
|
||||
116
injector/source/fsreg.c
Normal file
116
injector/source/fsreg.c
Normal file
@@ -0,0 +1,116 @@
|
||||
#include <3ds.h>
|
||||
#include <string.h>
|
||||
#include "fsreg.h"
|
||||
#include "srvsys.h"
|
||||
|
||||
static Handle fsregHandle;
|
||||
static int fsregRefCount;
|
||||
|
||||
Result fsregInit(void)
|
||||
{
|
||||
Result ret = 0;
|
||||
|
||||
if (AtomicPostIncrement(&fsregRefCount)) return 0;
|
||||
|
||||
ret = srvSysGetServiceHandle(&fsregHandle, "fs:REG");
|
||||
|
||||
if (R_FAILED(ret)) AtomicDecrement(&fsregRefCount);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void fsregExit(void)
|
||||
{
|
||||
if (AtomicDecrement(&fsregRefCount)) return;
|
||||
svcCloseHandle(fsregHandle);
|
||||
}
|
||||
|
||||
Result FSREG_CheckHostLoadId(u64 prog_handle)
|
||||
{
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x406,2,0); // 0x4060080
|
||||
cmdbuf[1] = (u32) (prog_handle);
|
||||
cmdbuf[2] = (u32) (prog_handle >> 32);
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(fsregHandle))) return ret;
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result FSREG_LoadProgram(u64 *prog_handle, FS_ProgramInfo *title)
|
||||
{
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x404,4,0); // 0x4040100
|
||||
memcpy(&cmdbuf[1], &title->programId, sizeof(u64));
|
||||
*(u8 *)&cmdbuf[3] = title->mediaType;
|
||||
memcpy(((u8 *)&cmdbuf[3])+1, &title->padding, 7);
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(fsregHandle))) return ret;
|
||||
*prog_handle = *(u64 *)&cmdbuf[2];
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result FSREG_GetProgramInfo(exheader_header *exheader, u32 entry_count, u64 prog_handle)
|
||||
{
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x403,3,0); // 0x40300C0
|
||||
cmdbuf[1] = entry_count;
|
||||
*(u64 *)&cmdbuf[2] = prog_handle;
|
||||
cmdbuf[64] = ((entry_count << 10) << 14) | 2;
|
||||
cmdbuf[65] = (u32) exheader;
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(fsregHandle))) return ret;
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result FSREG_UnloadProgram(u64 prog_handle)
|
||||
{
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x405,2,0); // 0x4050080
|
||||
cmdbuf[1] = (u32) (prog_handle);
|
||||
cmdbuf[2] = (u32) (prog_handle >> 32);
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(fsregHandle))) return ret;
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result FSREG_Unregister(u32 pid)
|
||||
{
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x402,1,0); // 0x4020040
|
||||
cmdbuf[1] = pid;
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(fsregHandle))) return ret;
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result FSREG_Register(u32 pid, u64 prog_handle, FS_ProgramInfo *info, void *storageinfo)
|
||||
{
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x401,0xf,0); // 0x40103C0
|
||||
cmdbuf[1] = pid;
|
||||
*(u64 *)&cmdbuf[2] = prog_handle;
|
||||
memcpy(&cmdbuf[4], &info->programId, sizeof(u64));
|
||||
*(u8 *)&cmdbuf[6] = info->mediaType;
|
||||
memcpy(((u8 *)&cmdbuf[6])+1, &info->padding, 7);
|
||||
memcpy((u8 *)&cmdbuf[8], storageinfo, 32);
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(fsregHandle))) return ret;
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
13
injector/source/fsreg.h
Normal file
13
injector/source/fsreg.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <3ds/types.h>
|
||||
#include "exheader.h"
|
||||
|
||||
Result fsregInit(void);
|
||||
void fsregExit(void);
|
||||
Result FSREG_CheckHostLoadId(u64 prog_handle);
|
||||
Result FSREG_LoadProgram(u64 *prog_handle, FS_ProgramInfo *title);
|
||||
Result FSREG_GetProgramInfo(exheader_header *exheader, u32 entry_count, u64 prog_handle);
|
||||
Result FSREG_UnloadProgram(u64 prog_handle);
|
||||
Result FSREG_Unregister(u32 pid);
|
||||
Result FSREG_Register(u32 pid, u64 prog_handle, FS_ProgramInfo *info, void *storageinfo);
|
||||
105
injector/source/ifile.c
Normal file
105
injector/source/ifile.c
Normal file
@@ -0,0 +1,105 @@
|
||||
#include <3ds.h>
|
||||
#include "ifile.h"
|
||||
#include "fsldr.h"
|
||||
|
||||
Result IFile_Open(IFile *file, FS_Archive archive, FS_Path path, u32 flags)
|
||||
{
|
||||
Result res;
|
||||
|
||||
res = FSLDR_OpenFileDirectly(&file->handle, archive, path, flags, 0);
|
||||
file->pos = 0;
|
||||
file->size = 0;
|
||||
return res;
|
||||
}
|
||||
|
||||
Result IFile_Close(IFile *file)
|
||||
{
|
||||
return FSFILE_Close(file->handle);
|
||||
}
|
||||
|
||||
Result IFile_GetSize(IFile *file, u64 *size)
|
||||
{
|
||||
Result res;
|
||||
|
||||
res = FSFILE_GetSize(file->handle, size);
|
||||
file->size = *size;
|
||||
return res;
|
||||
}
|
||||
|
||||
Result IFile_Read(IFile *file, u64 *total, void *buffer, u32 len)
|
||||
{
|
||||
u32 read;
|
||||
u32 left;
|
||||
char *buf;
|
||||
u64 cur;
|
||||
Result res;
|
||||
|
||||
if (len == 0)
|
||||
{
|
||||
*total = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf = (char *)buffer;
|
||||
cur = 0;
|
||||
left = len;
|
||||
while (1)
|
||||
{
|
||||
res = FSFILE_Read(file->handle, &read, file->pos, buf, left);
|
||||
if (R_FAILED(res))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
cur += read;
|
||||
file->pos += read;
|
||||
if (read == left)
|
||||
{
|
||||
break;
|
||||
}
|
||||
buf += read;
|
||||
left -= read;
|
||||
}
|
||||
|
||||
*total = cur;
|
||||
return res;
|
||||
}
|
||||
|
||||
Result IFile_Write(IFile *file, u64 *total, void *buffer, u32 len, u32 flags)
|
||||
{
|
||||
u32 written;
|
||||
u32 left;
|
||||
char *buf;
|
||||
u64 cur;
|
||||
Result res;
|
||||
|
||||
if (len == 0)
|
||||
{
|
||||
*total = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf = (char *)buffer;
|
||||
cur = 0;
|
||||
left = len;
|
||||
while (1)
|
||||
{
|
||||
res = FSFILE_Write(file->handle, &written, file->pos, buf, left, flags);
|
||||
if (R_FAILED(res))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
cur += written;
|
||||
file->pos += written;
|
||||
if (written == left)
|
||||
{
|
||||
break;
|
||||
}
|
||||
buf += written;
|
||||
left -= written;
|
||||
}
|
||||
|
||||
*total = cur;
|
||||
return res;
|
||||
}
|
||||
16
injector/source/ifile.h
Normal file
16
injector/source/ifile.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <3ds/types.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
Handle handle;
|
||||
u64 pos;
|
||||
u64 size;
|
||||
} IFile;
|
||||
|
||||
Result IFile_Open(IFile *file, FS_Archive archive, FS_Path path, u32 flags);
|
||||
Result IFile_Close(IFile *file);
|
||||
Result IFile_GetSize(IFile *file, u64 *size);
|
||||
Result IFile_Read(IFile *file, u64 *total, void *buffer, u32 len);
|
||||
Result IFile_Write(IFile *file, u64 *total, void *buffer, u32 len, u32 flags);
|
||||
593
injector/source/loader.c
Normal file
593
injector/source/loader.c
Normal file
@@ -0,0 +1,593 @@
|
||||
#include <3ds.h>
|
||||
#include <string.h>
|
||||
#include <sys/iosupport.h>
|
||||
#include "patcher.h"
|
||||
#include "exheader.h"
|
||||
#include "ifile.h"
|
||||
#include "fsldr.h"
|
||||
#include "fsreg.h"
|
||||
#include "pxipm.h"
|
||||
#include "srvsys.h"
|
||||
|
||||
#define MAX_SESSIONS 1
|
||||
|
||||
const char CODE_PATH[] = {0x01, 0x00, 0x00, 0x00, 0x2E, 0x63, 0x6F, 0x64, 0x65, 0x00, 0x00, 0x00};
|
||||
|
||||
typedef struct
|
||||
{
|
||||
u32 text_addr;
|
||||
u32 text_size;
|
||||
u32 ro_addr;
|
||||
u32 ro_size;
|
||||
u32 data_addr;
|
||||
u32 data_size;
|
||||
u32 total_size;
|
||||
} prog_addrs_t;
|
||||
|
||||
static Handle g_handles[MAX_SESSIONS+2];
|
||||
static int g_active_handles;
|
||||
static u64 g_cached_prog_handle;
|
||||
static exheader_header g_exheader;
|
||||
static char g_ret_buf[1024];
|
||||
|
||||
static int lzss_decompress(u8 *end)
|
||||
{
|
||||
unsigned int v1; // r1@2
|
||||
u8 *v2; // r2@2
|
||||
u8 *v3; // r3@2
|
||||
u8 *v4; // r1@2
|
||||
char v5; // r5@4
|
||||
char v6; // t1@4
|
||||
signed int v7; // r6@4
|
||||
int v9; // t1@7
|
||||
u8 *v11; // r3@8
|
||||
int v12; // r12@8
|
||||
int v13; // t1@8
|
||||
int v14; // t1@8
|
||||
unsigned int v15; // r7@8
|
||||
int v16; // r12@8
|
||||
int ret;
|
||||
|
||||
ret = 0;
|
||||
if ( end )
|
||||
{
|
||||
v1 = *((u32 *)end - 2);
|
||||
v2 = &end[*((u32 *)end - 1)];
|
||||
v3 = &end[-(v1 >> 24)];
|
||||
v4 = &end[-(v1 & 0xFFFFFF)];
|
||||
while ( v3 > v4 )
|
||||
{
|
||||
v6 = *(v3-- - 1);
|
||||
v5 = v6;
|
||||
v7 = 8;
|
||||
while ( 1 )
|
||||
{
|
||||
if ( (v7-- < 1) )
|
||||
break;
|
||||
if ( v5 & 0x80 )
|
||||
{
|
||||
v13 = *(v3 - 1);
|
||||
v11 = v3 - 1;
|
||||
v12 = v13;
|
||||
v14 = *(v11 - 1);
|
||||
v3 = v11 - 1;
|
||||
v15 = ((v14 | (v12 << 8)) & 0xFFFF0FFF) + 2;
|
||||
v16 = v12 + 32;
|
||||
do
|
||||
{
|
||||
ret = v2[v15];
|
||||
*(v2-- - 1) = ret;
|
||||
v16 -= 16;
|
||||
}
|
||||
while ( !(v16 < 0) );
|
||||
}
|
||||
else
|
||||
{
|
||||
v9 = *(v3-- - 1);
|
||||
ret = v9;
|
||||
*(v2-- - 1) = v9;
|
||||
}
|
||||
v5 *= 2;
|
||||
if ( v3 <= v4 )
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static Result allocate_shared_mem(prog_addrs_t *shared, prog_addrs_t *vaddr, int flags)
|
||||
{
|
||||
u32 dummy;
|
||||
|
||||
memcpy(shared, vaddr, sizeof(prog_addrs_t));
|
||||
shared->text_addr = 0x10000000;
|
||||
shared->ro_addr = shared->text_addr + (shared->text_size << 12);
|
||||
shared->data_addr = shared->ro_addr + (shared->ro_size << 12);
|
||||
return svcControlMemory(&dummy, shared->text_addr, 0, shared->total_size << 12, (flags & 0xF00) | MEMOP_ALLOC, MEMPERM_READ | MEMPERM_WRITE);
|
||||
}
|
||||
|
||||
static Result load_code(u64 progid, prog_addrs_t *shared, u64 prog_handle, int is_compressed)
|
||||
{
|
||||
IFile file;
|
||||
FS_Archive archive;
|
||||
FS_Path path;
|
||||
Result res;
|
||||
u64 size;
|
||||
u64 total;
|
||||
|
||||
archive.id = ARCHIVE_SAVEDATA_AND_CONTENT2;
|
||||
archive.lowPath.type = PATH_BINARY;
|
||||
archive.lowPath.data = &prog_handle;
|
||||
archive.lowPath.size = 8;
|
||||
//archive.handle = prog_handle; // not needed
|
||||
path.type = PATH_BINARY;
|
||||
path.data = CODE_PATH;
|
||||
path.size = sizeof(CODE_PATH);
|
||||
if (R_FAILED(IFile_Open(&file, archive, path, FS_OPEN_READ)))
|
||||
{
|
||||
svcBreak(USERBREAK_ASSERT);
|
||||
}
|
||||
|
||||
// get file size
|
||||
if (R_FAILED(IFile_GetSize(&file, &size)))
|
||||
{
|
||||
IFile_Close(&file);
|
||||
svcBreak(USERBREAK_ASSERT);
|
||||
}
|
||||
|
||||
// check size
|
||||
if (size > (u64)shared->total_size << 12)
|
||||
{
|
||||
IFile_Close(&file);
|
||||
return 0xC900464F;
|
||||
}
|
||||
|
||||
// read code
|
||||
res = IFile_Read(&file, &total, (void *)shared->text_addr, size);
|
||||
IFile_Close(&file); // done reading
|
||||
if (R_FAILED(res))
|
||||
{
|
||||
svcBreak(USERBREAK_ASSERT);
|
||||
}
|
||||
|
||||
// decompress
|
||||
if (is_compressed)
|
||||
{
|
||||
lzss_decompress((u8 *)shared->text_addr + size);
|
||||
}
|
||||
|
||||
// patch
|
||||
patch_code(progid, (u8 *)shared->text_addr, shared->total_size << 12);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static Result loader_GetProgramInfo(exheader_header *exheader, u64 prog_handle)
|
||||
{
|
||||
Result res;
|
||||
|
||||
if (prog_handle >> 32 == 0xFFFF0000)
|
||||
{
|
||||
return FSREG_GetProgramInfo(exheader, 1, prog_handle);
|
||||
}
|
||||
else
|
||||
{
|
||||
res = FSREG_CheckHostLoadId(prog_handle);
|
||||
//if ((res >= 0 && (unsigned)res >> 27) || (res < 0 && ((unsigned)res >> 27)-32))
|
||||
//so use PXIPM if FSREG fails OR returns "info", is the second condition a bug?
|
||||
if (R_FAILED(res) || (R_SUCCEEDED(res) && R_LEVEL(res) != RL_SUCCESS))
|
||||
{
|
||||
return PXIPM_GetProgramInfo(exheader, prog_handle);
|
||||
}
|
||||
else
|
||||
{
|
||||
return FSREG_GetProgramInfo(exheader, 1, prog_handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Result loader_LoadProcess(Handle *process, u64 prog_handle)
|
||||
{
|
||||
Result res;
|
||||
int count;
|
||||
u32 flags;
|
||||
u32 desc;
|
||||
u32 dummy;
|
||||
prog_addrs_t shared_addr;
|
||||
prog_addrs_t vaddr;
|
||||
Handle codeset;
|
||||
CodeSetInfo codesetinfo;
|
||||
u32 data_mem_size;
|
||||
u64 progid;
|
||||
|
||||
// make sure the cached info corrosponds to the current prog_handle
|
||||
if (g_cached_prog_handle != prog_handle)
|
||||
{
|
||||
res = loader_GetProgramInfo(&g_exheader, prog_handle);
|
||||
g_cached_prog_handle = prog_handle;
|
||||
if (res < 0)
|
||||
{
|
||||
g_cached_prog_handle = 0;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
// get kernel flags
|
||||
flags = 0;
|
||||
for (count = 0; count < 28; count++)
|
||||
{
|
||||
desc = g_exheader.arm11kernelcaps.descriptors[count];
|
||||
if (0x1FE == desc >> 23)
|
||||
{
|
||||
flags = desc & 0xF00;
|
||||
}
|
||||
}
|
||||
if (flags == 0)
|
||||
{
|
||||
return MAKERESULT(RL_PERMANENT, RS_INVALIDARG, 1, 2);
|
||||
}
|
||||
|
||||
// allocate process memory
|
||||
vaddr.text_addr = g_exheader.codesetinfo.text.address;
|
||||
vaddr.text_size = (g_exheader.codesetinfo.text.codesize + 4095) >> 12;
|
||||
vaddr.ro_addr = g_exheader.codesetinfo.ro.address;
|
||||
vaddr.ro_size = (g_exheader.codesetinfo.ro.codesize + 4095) >> 12;
|
||||
vaddr.data_addr = g_exheader.codesetinfo.data.address;
|
||||
vaddr.data_size = (g_exheader.codesetinfo.data.codesize + 4095) >> 12;
|
||||
data_mem_size = (g_exheader.codesetinfo.data.codesize + g_exheader.codesetinfo.bsssize + 4095) >> 12;
|
||||
vaddr.total_size = vaddr.text_size + vaddr.ro_size + vaddr.data_size;
|
||||
if ((res = allocate_shared_mem(&shared_addr, &vaddr, flags)) < 0)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
|
||||
// load code
|
||||
progid = g_exheader.arm11systemlocalcaps.programid;
|
||||
if ((res = load_code(progid, &shared_addr, prog_handle, g_exheader.codesetinfo.flags.flag & 1)) >= 0)
|
||||
{
|
||||
memcpy(&codesetinfo.name, g_exheader.codesetinfo.name, 8);
|
||||
codesetinfo.program_id = progid;
|
||||
codesetinfo.text_addr = vaddr.text_addr;
|
||||
codesetinfo.text_size = vaddr.text_size;
|
||||
codesetinfo.text_size_total = vaddr.text_size;
|
||||
codesetinfo.ro_addr = vaddr.ro_addr;
|
||||
codesetinfo.ro_size = vaddr.ro_size;
|
||||
codesetinfo.ro_size_total = vaddr.ro_size;
|
||||
codesetinfo.rw_addr = vaddr.data_addr;
|
||||
codesetinfo.rw_size = vaddr.data_size;
|
||||
codesetinfo.rw_size_total = data_mem_size;
|
||||
res = svcCreateCodeSet(&codeset, &codesetinfo, (void *)shared_addr.text_addr, (void *)shared_addr.ro_addr, (void *)shared_addr.data_addr);
|
||||
if (res >= 0)
|
||||
{
|
||||
res = svcCreateProcess(process, codeset, g_exheader.arm11kernelcaps.descriptors, count);
|
||||
svcCloseHandle(codeset);
|
||||
if (res >= 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
svcControlMemory(&dummy, shared_addr.text_addr, 0, shared_addr.total_size << 12, MEMOP_FREE, 0);
|
||||
return res;
|
||||
}
|
||||
|
||||
static Result loader_RegisterProgram(u64 *prog_handle, FS_ProgramInfo *title, FS_ProgramInfo *update)
|
||||
{
|
||||
Result res;
|
||||
u64 prog_id;
|
||||
|
||||
prog_id = title->programId;
|
||||
if (prog_id >> 32 != 0xFFFF0000)
|
||||
{
|
||||
res = FSREG_CheckHostLoadId(prog_id);
|
||||
//if ((res >= 0 && (unsigned)res >> 27) || (res < 0 && ((unsigned)res >> 27)-32))
|
||||
if (R_FAILED(res) || (R_SUCCEEDED(res) && R_LEVEL(res) != RL_SUCCESS))
|
||||
{
|
||||
res = PXIPM_RegisterProgram(prog_handle, title, update);
|
||||
if (res < 0)
|
||||
{
|
||||
return res;
|
||||
}
|
||||
if (*prog_handle >> 32 != 0xFFFF0000)
|
||||
{
|
||||
res = FSREG_CheckHostLoadId(*prog_handle);
|
||||
//if ((res >= 0 && (unsigned)res >> 27) || (res < 0 && ((unsigned)res >> 27)-32))
|
||||
if (R_FAILED(res) || (R_SUCCEEDED(res) && R_LEVEL(res) != RL_SUCCESS))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
svcBreak(USERBREAK_ASSERT);
|
||||
}
|
||||
}
|
||||
|
||||
if ((title->mediaType != update->mediaType) || (prog_id != update->programId))
|
||||
{
|
||||
svcBreak(USERBREAK_ASSERT);
|
||||
}
|
||||
res = FSREG_LoadProgram(prog_handle, title);
|
||||
if (R_SUCCEEDED(res))
|
||||
{
|
||||
if (*prog_handle >> 32 == 0xFFFF0000)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
res = FSREG_CheckHostLoadId(*prog_handle);
|
||||
//if ((res >= 0 && (unsigned)res >> 27) || (res < 0 && ((unsigned)res >> 27)-32))
|
||||
if (R_FAILED(res) || (R_SUCCEEDED(res) && R_LEVEL(res) != RL_SUCCESS))
|
||||
{
|
||||
svcBreak(USERBREAK_ASSERT);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
static Result loader_UnregisterProgram(u64 prog_handle)
|
||||
{
|
||||
Result res;
|
||||
|
||||
if (prog_handle >> 32 == 0xFFFF0000)
|
||||
{
|
||||
return FSREG_UnloadProgram(prog_handle);
|
||||
}
|
||||
else
|
||||
{
|
||||
res = FSREG_CheckHostLoadId(prog_handle);
|
||||
//if ((res >= 0 && (unsigned)res >> 27) || (res < 0 && ((unsigned)res >> 27)-32))
|
||||
if (R_FAILED(res) || (R_SUCCEEDED(res) && R_LEVEL(res) != RL_SUCCESS))
|
||||
{
|
||||
return PXIPM_UnregisterProgram(prog_handle);
|
||||
}
|
||||
else
|
||||
{
|
||||
return FSREG_UnloadProgram(prog_handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_commands(void)
|
||||
{
|
||||
FS_ProgramInfo title;
|
||||
FS_ProgramInfo update;
|
||||
u32* cmdbuf;
|
||||
u16 cmdid;
|
||||
int res;
|
||||
Handle handle;
|
||||
u64 prog_handle;
|
||||
|
||||
cmdbuf = getThreadCommandBuffer();
|
||||
cmdid = cmdbuf[0] >> 16;
|
||||
res = 0;
|
||||
switch (cmdid)
|
||||
{
|
||||
case 1: // LoadProcess
|
||||
{
|
||||
res = loader_LoadProcess(&handle, *(u64 *)&cmdbuf[1]);
|
||||
cmdbuf[0] = 0x10042;
|
||||
cmdbuf[1] = res;
|
||||
cmdbuf[2] = 16;
|
||||
cmdbuf[3] = handle;
|
||||
break;
|
||||
}
|
||||
case 2: // RegisterProgram
|
||||
{
|
||||
memcpy(&title, &cmdbuf[1], sizeof(FS_ProgramInfo));
|
||||
memcpy(&update, &cmdbuf[5], sizeof(FS_ProgramInfo));
|
||||
res = loader_RegisterProgram(&prog_handle, &title, &update);
|
||||
cmdbuf[0] = 0x200C0;
|
||||
cmdbuf[1] = res;
|
||||
*(u64 *)&cmdbuf[2] = prog_handle;
|
||||
break;
|
||||
}
|
||||
case 3: // UnregisterProgram
|
||||
{
|
||||
if (g_cached_prog_handle == prog_handle)
|
||||
{
|
||||
g_cached_prog_handle = 0;
|
||||
}
|
||||
cmdbuf[0] = 0x30040;
|
||||
cmdbuf[1] = loader_UnregisterProgram(*(u64 *)&cmdbuf[1]);
|
||||
break;
|
||||
}
|
||||
case 4: // GetProgramInfo
|
||||
{
|
||||
prog_handle = *(u64 *)&cmdbuf[1];
|
||||
if (prog_handle != g_cached_prog_handle)
|
||||
{
|
||||
res = loader_GetProgramInfo(&g_exheader, prog_handle);
|
||||
if (res >= 0)
|
||||
{
|
||||
g_cached_prog_handle = prog_handle;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_cached_prog_handle = 0;
|
||||
}
|
||||
}
|
||||
memcpy(&g_ret_buf, &g_exheader, 1024);
|
||||
cmdbuf[0] = 0x40042;
|
||||
cmdbuf[1] = res;
|
||||
cmdbuf[2] = 0x1000002;
|
||||
cmdbuf[3] = (u32) &g_ret_buf;
|
||||
break;
|
||||
}
|
||||
default: // error
|
||||
{
|
||||
cmdbuf[0] = 0x40;
|
||||
cmdbuf[1] = 0xD900182F;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static Result should_terminate(int *term_request)
|
||||
{
|
||||
u32 notid;
|
||||
Result ret;
|
||||
|
||||
ret = srvSysReceiveNotification(¬id);
|
||||
if (R_FAILED(ret))
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
if (notid == 0x100) // term request
|
||||
{
|
||||
*term_request = 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// this is called before main
|
||||
void __appInit()
|
||||
{
|
||||
srvSysInit();
|
||||
fsregInit();
|
||||
fsldrInit();
|
||||
pxipmInit();
|
||||
}
|
||||
|
||||
// this is called after main exits
|
||||
void __appExit()
|
||||
{
|
||||
pxipmExit();
|
||||
fsldrExit();
|
||||
fsregExit();
|
||||
srvSysExit();
|
||||
}
|
||||
|
||||
// stubs for non-needed pre-main functions
|
||||
void __sync_init();
|
||||
void __sync_fini();
|
||||
void __system_initSyscalls();
|
||||
|
||||
void __ctru_exit(int rc)
|
||||
{
|
||||
__appExit();
|
||||
__sync_fini();
|
||||
svcExitProcess();
|
||||
}
|
||||
|
||||
void initSystem(void (*retAddr)(void))
|
||||
{
|
||||
__sync_init();
|
||||
__system_initSyscalls();
|
||||
__appInit();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Result ret;
|
||||
Handle handle;
|
||||
Handle reply_target;
|
||||
Handle *srv_handle;
|
||||
Handle *notification_handle;
|
||||
s32 index;
|
||||
int i;
|
||||
int term_request;
|
||||
u32* cmdbuf;
|
||||
|
||||
ret = 0;
|
||||
|
||||
srv_handle = &g_handles[1];
|
||||
notification_handle = &g_handles[0];
|
||||
|
||||
if (R_FAILED(srvSysRegisterService(srv_handle, "Loader", MAX_SESSIONS)))
|
||||
{
|
||||
svcBreak(USERBREAK_ASSERT);
|
||||
}
|
||||
|
||||
if (R_FAILED(srvSysEnableNotification(notification_handle)))
|
||||
{
|
||||
svcBreak(USERBREAK_ASSERT);
|
||||
}
|
||||
|
||||
g_active_handles = 2;
|
||||
g_cached_prog_handle = 0;
|
||||
index = 1;
|
||||
|
||||
reply_target = 0;
|
||||
term_request = 0;
|
||||
do
|
||||
{
|
||||
if (reply_target == 0)
|
||||
{
|
||||
cmdbuf = getThreadCommandBuffer();
|
||||
cmdbuf[0] = 0xFFFF0000;
|
||||
}
|
||||
ret = svcReplyAndReceive(&index, g_handles, g_active_handles, reply_target);
|
||||
|
||||
if (R_FAILED(ret))
|
||||
{
|
||||
// check if any handle has been closed
|
||||
if (ret == 0xC920181A)
|
||||
{
|
||||
if (index == -1)
|
||||
{
|
||||
for (i = 2; i < MAX_SESSIONS+2; i++)
|
||||
{
|
||||
if (g_handles[i] == reply_target)
|
||||
{
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
svcCloseHandle(g_handles[index]);
|
||||
g_handles[index] = g_handles[g_active_handles-1];
|
||||
g_active_handles--;
|
||||
reply_target = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
svcBreak(USERBREAK_ASSERT);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// process responses
|
||||
reply_target = 0;
|
||||
switch (index)
|
||||
{
|
||||
case 0: // notification
|
||||
{
|
||||
if (R_FAILED(should_terminate(&term_request)))
|
||||
{
|
||||
svcBreak(USERBREAK_ASSERT);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 1: // new session
|
||||
{
|
||||
if (R_FAILED(svcAcceptSession(&handle, *srv_handle)))
|
||||
{
|
||||
svcBreak(USERBREAK_ASSERT);
|
||||
}
|
||||
if (g_active_handles < MAX_SESSIONS+2)
|
||||
{
|
||||
g_handles[g_active_handles] = handle;
|
||||
g_active_handles++;
|
||||
}
|
||||
else
|
||||
{
|
||||
svcCloseHandle(handle);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: // session
|
||||
{
|
||||
handle_commands();
|
||||
reply_target = g_handles[index];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} while (!term_request || g_active_handles != 2);
|
||||
|
||||
srvSysUnregisterService("Loader");
|
||||
svcCloseHandle(*srv_handle);
|
||||
svcCloseHandle(*notification_handle);
|
||||
|
||||
return 0;
|
||||
}
|
||||
274
injector/source/patcher.c
Normal file
274
injector/source/patcher.c
Normal file
@@ -0,0 +1,274 @@
|
||||
#include <3ds.h>
|
||||
#include <string.h>
|
||||
#include "patcher.h"
|
||||
#include "ifile.h"
|
||||
|
||||
#ifndef PATH_MAX
|
||||
#define PATH_MAX 255
|
||||
#endif
|
||||
|
||||
static u32 config = 0;
|
||||
static u8 secureinfo[0x111] = {0};
|
||||
|
||||
//Quick Search algorithm, adapted from http://igm.univ-mlv.fr/~lecroq/string/node19.html#SECTION00190
|
||||
static u8 *memsearch(u8 *startPos, const void *pattern, u32 size, u32 patternSize){
|
||||
const u8 *patternc = (const u8 *)pattern;
|
||||
|
||||
//Preprocessing
|
||||
int table[256];
|
||||
|
||||
for(u32 i = 0; i < 256; ++i)
|
||||
table[i] = patternSize + 1;
|
||||
for(u32 i = 0; i < patternSize; ++i)
|
||||
table[patternc[i]] = patternSize - i;
|
||||
|
||||
//Searching
|
||||
u32 j = 0;
|
||||
|
||||
while(j <= size - patternSize){
|
||||
if(memcmp(patternc, startPos + j, patternSize) == 0)
|
||||
return startPos + j;
|
||||
j += table[startPos[j + patternSize]];
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static u32 patch_memory(u8 *start, u32 size, const void *pattern, u32 patsize, int offset, const void *replace, u32 repsize, u32 count){
|
||||
u32 i;
|
||||
|
||||
for(i = 0; i < count; i++){
|
||||
u8 *found = memsearch(start, pattern, size, patsize);
|
||||
if(found == NULL)
|
||||
break;
|
||||
|
||||
memcpy(found + offset, replace, repsize);
|
||||
|
||||
u32 at = (u32)(found - start);
|
||||
|
||||
if(at + patsize > size) size = 0;
|
||||
else size = size - (at + patsize);
|
||||
|
||||
start = found + patsize;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
static int file_open(IFile *file, FS_ArchiveID id, const char *path, int flags){
|
||||
FS_Archive archive;
|
||||
FS_Path ppath;
|
||||
|
||||
size_t len = strnlen(path, PATH_MAX);
|
||||
archive.id = id;
|
||||
archive.lowPath.type = PATH_EMPTY;
|
||||
archive.lowPath.size = 1;
|
||||
archive.lowPath.data = (u8 *)"";
|
||||
ppath.type = PATH_ASCII;
|
||||
ppath.data = path;
|
||||
ppath.size = len+1;
|
||||
return IFile_Open(file, archive, ppath, flags);
|
||||
}
|
||||
|
||||
static int patch_secureinfo(){
|
||||
IFile file;
|
||||
Result ret;
|
||||
u64 total;
|
||||
|
||||
if(secureinfo[0] == 0xFF)
|
||||
return 0;
|
||||
|
||||
ret = file_open(&file, ARCHIVE_SDMC, "/SecureInfo_A", FS_OPEN_READ);
|
||||
if(R_SUCCEEDED(ret)){
|
||||
ret = IFile_Read(&file, &total, secureinfo, sizeof(secureinfo));
|
||||
IFile_Close(&file);
|
||||
if(R_SUCCEEDED(ret) && total == sizeof(secureinfo)){
|
||||
ret = file_open(&file, ARCHIVE_NAND_RW, "/sys/SecureInfo_C", FS_OPEN_WRITE | FS_OPEN_CREATE);
|
||||
if(R_SUCCEEDED(ret)){
|
||||
ret = IFile_Write(&file, &total, secureinfo, sizeof(secureinfo), FS_WRITE_FLUSH);
|
||||
IFile_Close(&file);
|
||||
}
|
||||
secureinfo[0] = 0xFF; // we repurpose this byte as status
|
||||
}
|
||||
}
|
||||
else { // get file from NAND
|
||||
ret = file_open(&file, ARCHIVE_NAND_RW, "/sys/SecureInfo_C", FS_OPEN_READ);
|
||||
if(R_SUCCEEDED(ret)){
|
||||
ret = IFile_Read(&file, &total, secureinfo, sizeof(secureinfo));
|
||||
IFile_Close(&file);
|
||||
if(R_SUCCEEDED(ret) && total == sizeof(secureinfo))
|
||||
secureinfo[0] = 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int open_config(){
|
||||
IFile file;
|
||||
Result ret;
|
||||
u64 total;
|
||||
|
||||
if(config)
|
||||
return 0;
|
||||
|
||||
ret = file_open(&file, ARCHIVE_SDMC, "/aurei/config.bin", FS_OPEN_READ);
|
||||
if(R_SUCCEEDED(ret)){
|
||||
ret = IFile_Read(&file, &total, (void *)&config, 3);
|
||||
IFile_Close(&file);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
u32 patch_code(u64 progid, u8 *code, u32 size){
|
||||
if( progid == 0x0004003000008F02LL || // USA Menu
|
||||
progid == 0x0004003000008202LL || // JPN Menu
|
||||
progid == 0x0004003000009802LL || // EUR Menu
|
||||
progid == 0x000400300000A102LL || // CHN Menu
|
||||
progid == 0x000400300000A902LL || // KOR Menu
|
||||
progid == 0x000400300000B102LL // TWN Menu
|
||||
){
|
||||
static const u8 regionFreePattern[] = {
|
||||
0x00, 0x00, 0x55, 0xE3, 0x01, 0x10, 0xA0, 0xE3
|
||||
};
|
||||
static const u8 regionFreePatch[] = {
|
||||
0x01, 0x00, 0xA0, 0xE3, 0x1E, 0xFF, 0x2F, 0xE1
|
||||
};
|
||||
|
||||
patch_memory(code, size,
|
||||
regionFreePattern,
|
||||
sizeof(regionFreePattern), -16,
|
||||
regionFreePatch,
|
||||
sizeof(regionFreePatch), 1
|
||||
);
|
||||
}
|
||||
else if(progid == 0x0004013000002C02LL){ // NIM
|
||||
static const u8 blockAutoUpdatesPattern[] = {
|
||||
0x25, 0x79, 0x0B, 0x99
|
||||
};
|
||||
static const u8 blockAutoUpdatesPatch[] = {
|
||||
0xE3, 0xA0
|
||||
};
|
||||
static const u8 blockEShopUpdateCheckPattern[] = {
|
||||
0x30, 0xB5, 0xF1, 0xB0
|
||||
};
|
||||
static const u8 blockEShopUpdateCheckPatch[] = {
|
||||
0x00, 0x20, 0x08, 0x60, 0x70, 0x47
|
||||
};
|
||||
static const u8 countryRespPattern[] = {
|
||||
0x01, 0x20, 0x01, 0x90, 0x22, 0x46, 0x06, 0x9B
|
||||
};
|
||||
static const char countryRespPatchModel[] = {
|
||||
0x06, 0x9A, 0x03, 0x20, 0x90, 0x47, 0x55, 0x21, 0x01, 0x70, 0x53, 0x21, 0x41, 0x70, 0x00, 0x21,
|
||||
0x81, 0x70, 0x60, 0x61, 0x00, 0x20
|
||||
};
|
||||
const char *country;
|
||||
char countryRespPatch[sizeof(countryRespPatchModel)];
|
||||
|
||||
patch_memory(code, size,
|
||||
blockAutoUpdatesPattern,
|
||||
sizeof(blockAutoUpdatesPattern), 0,
|
||||
blockAutoUpdatesPatch,
|
||||
sizeof(blockAutoUpdatesPatch), 1
|
||||
);
|
||||
patch_memory(code, size,
|
||||
blockEShopUpdateCheckPattern,
|
||||
sizeof(blockEShopUpdateCheckPattern), 0,
|
||||
blockEShopUpdateCheckPatch,
|
||||
sizeof(blockEShopUpdateCheckPatch), 1
|
||||
);
|
||||
if(R_SUCCEEDED(patch_secureinfo())){
|
||||
switch(secureinfo[0x100]){
|
||||
case 1: country = "US"; break;
|
||||
case 2: country = "GB"; break; // sorry rest-of-Europe, you have to change this
|
||||
case 3: country = "AU"; break;
|
||||
case 4: country = "CN"; break;
|
||||
case 5: country = "KR"; break;
|
||||
case 6: country = "TW"; break;
|
||||
default: case 0: country = "JP"; break;
|
||||
}
|
||||
|
||||
// patch XML response Country
|
||||
memcpy(countryRespPatch,
|
||||
countryRespPatchModel,
|
||||
sizeof(countryRespPatchModel)
|
||||
);
|
||||
countryRespPatch[6] = country[0];
|
||||
countryRespPatch[10] = country[1];
|
||||
patch_memory(code, size,
|
||||
countryRespPattern,
|
||||
sizeof(countryRespPattern), 0,
|
||||
countryRespPatch,
|
||||
sizeof(countryRespPatch), 1
|
||||
);
|
||||
}
|
||||
}
|
||||
else if(
|
||||
progid == 0x0004001000021000LL || // USA MSET
|
||||
progid == 0x0004001000020000LL || // JPN MSET
|
||||
progid == 0x0004001000022000LL || // EUR MSET
|
||||
progid == 0x0004001000026000LL || // CHN MSET
|
||||
progid == 0x0004001000027000LL || // KOR MSET
|
||||
progid == 0x0004001000028000LL // TWN MSET
|
||||
){
|
||||
if(R_SUCCEEDED(open_config()) && ((config >> 5) & 0x1)){
|
||||
static const u16 VerPattern[] = u"Ver.";
|
||||
const u32 currentFirm = ((config >> 12) & 0x1);
|
||||
const u32 currentNand = ((config >> 13) & 0x3);
|
||||
|
||||
patch_memory(code, size,
|
||||
VerPattern,
|
||||
sizeof(VerPattern) - sizeof(u16), 0,
|
||||
currentNand ? ((currentNand == 1) ? ((currentFirm == 1) ? u" Emu" : u"Emu9") : u"Emu2") :
|
||||
((currentFirm == 1) ? u" Sys" : u"Sys9"),
|
||||
sizeof(VerPattern) - sizeof(u16), 1
|
||||
);
|
||||
}
|
||||
}
|
||||
else if (progid == 0x0004013000008002LL){ // NS
|
||||
static const u8 stopCartUpdatesPattern[] = {
|
||||
0x0C, 0x18, 0xE1, 0xD8
|
||||
};
|
||||
static const u8 stopCartUpdatesPatch[] = {
|
||||
0x0B, 0x18, 0x21, 0xC8
|
||||
};
|
||||
|
||||
patch_memory(code, size,
|
||||
stopCartUpdatesPattern,
|
||||
sizeof(stopCartUpdatesPattern), 0,
|
||||
stopCartUpdatesPatch,
|
||||
sizeof(stopCartUpdatesPatch), 2
|
||||
);
|
||||
}
|
||||
else if(progid == 0x0004013000001702LL){ // CFG
|
||||
static const u8 secureinfoSigCheckPattern[] = {
|
||||
0x06, 0x46, 0x10, 0x48, 0xFC
|
||||
};
|
||||
static const u8 secureinfoSigCheckPatch[] = {
|
||||
0x00, 0x26
|
||||
};
|
||||
static const u16 secureinfoFilenamePattern[] = u"SecureInfo_";
|
||||
static const u16 secureinfoFilenamePatch[] = u"C";
|
||||
|
||||
// disable SecureInfo signature check
|
||||
patch_memory(code, size,
|
||||
secureinfoSigCheckPattern,
|
||||
sizeof(secureinfoSigCheckPattern), 0,
|
||||
secureinfoSigCheckPatch,
|
||||
sizeof(secureinfoSigCheckPatch), 1
|
||||
);
|
||||
if(R_SUCCEEDED(patch_secureinfo())){
|
||||
// use SecureInfo_C
|
||||
patch_memory(code, size,
|
||||
secureinfoFilenamePattern,
|
||||
sizeof(secureinfoFilenamePattern) - sizeof(u16),
|
||||
sizeof(secureinfoFilenamePattern) - sizeof(u16),
|
||||
secureinfoFilenamePatch,
|
||||
sizeof(secureinfoFilenamePatch) - sizeof(u16), 2
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
5
injector/source/patcher.h
Normal file
5
injector/source/patcher.h
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <3ds/types.h>
|
||||
|
||||
u32 patch_code(u64 progid, u8 *code, u32 size);
|
||||
74
injector/source/pxipm.c
Normal file
74
injector/source/pxipm.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <3ds.h>
|
||||
#include <string.h>
|
||||
#include "pxipm.h"
|
||||
#include "srvsys.h"
|
||||
|
||||
static Handle pxipmHandle;
|
||||
static int pxipmRefCount;
|
||||
|
||||
Result pxipmInit(void)
|
||||
{
|
||||
Result ret = 0;
|
||||
|
||||
if (AtomicPostIncrement(&pxipmRefCount)) return 0;
|
||||
|
||||
ret = srvSysGetServiceHandle(&pxipmHandle, "PxiPM");
|
||||
|
||||
if (R_FAILED(ret)) AtomicDecrement(&pxipmRefCount);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void pxipmExit(void)
|
||||
{
|
||||
if (AtomicDecrement(&pxipmRefCount)) return;
|
||||
svcCloseHandle(pxipmHandle);
|
||||
}
|
||||
|
||||
Result PXIPM_RegisterProgram(u64 *prog_handle, FS_ProgramInfo *title, FS_ProgramInfo *update)
|
||||
{
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x2,8,0); // 0x20200
|
||||
memcpy(&cmdbuf[1], &title->programId, sizeof(u64));
|
||||
*(u8 *)&cmdbuf[3] = title->mediaType;
|
||||
memcpy(((u8 *)&cmdbuf[3])+1, &title->padding, 7);
|
||||
memcpy(&cmdbuf[5], &update->programId, sizeof(u64));
|
||||
*(u8 *)&cmdbuf[7] = update->mediaType;
|
||||
memcpy(((u8 *)&cmdbuf[7])+1, &update->padding, 7);
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(pxipmHandle))) return ret;
|
||||
*prog_handle = *(u64*)&cmdbuf[2];
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result PXIPM_GetProgramInfo(exheader_header *exheader, u64 prog_handle)
|
||||
{
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x1,2,2); // 0x10082
|
||||
cmdbuf[1] = (u32) (prog_handle);
|
||||
cmdbuf[2] = (u32) (prog_handle >> 32);
|
||||
cmdbuf[3] = (0x400 << 8) | 0x4;
|
||||
cmdbuf[4] = (u32) exheader;
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(pxipmHandle))) return ret;
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result PXIPM_UnregisterProgram(u64 prog_handle)
|
||||
{
|
||||
u32 *cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x3,2,0); // 0x30080
|
||||
cmdbuf[1] = (u32) (prog_handle);
|
||||
cmdbuf[2] = (u32) (prog_handle >> 32);
|
||||
|
||||
Result ret = 0;
|
||||
if(R_FAILED(ret = svcSendSyncRequest(pxipmHandle))) return ret;
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
10
injector/source/pxipm.h
Normal file
10
injector/source/pxipm.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <3ds/types.h>
|
||||
#include "exheader.h"
|
||||
|
||||
Result pxipmInit(void);
|
||||
void pxipmExit(void);
|
||||
Result PXIPM_RegisterProgram(u64 *prog_handle, FS_ProgramInfo *title, FS_ProgramInfo *update);
|
||||
Result PXIPM_GetProgramInfo(exheader_header *exheader, u64 prog_handle);
|
||||
Result PXIPM_UnregisterProgram(u64 prog_handle);
|
||||
154
injector/source/srvsys.c
Normal file
154
injector/source/srvsys.c
Normal file
@@ -0,0 +1,154 @@
|
||||
#include <3ds.h>
|
||||
#include <string.h>
|
||||
#include "srvsys.h"
|
||||
|
||||
static Handle srvHandle;
|
||||
static int srvRefCount;
|
||||
static RecursiveLock initLock;
|
||||
static int initLockinit = 0;
|
||||
|
||||
Result srvSysInit()
|
||||
{
|
||||
Result rc = 0;
|
||||
|
||||
if (!initLockinit)
|
||||
{
|
||||
RecursiveLock_Init(&initLock);
|
||||
}
|
||||
|
||||
RecursiveLock_Lock(&initLock);
|
||||
|
||||
if (srvRefCount > 0)
|
||||
{
|
||||
RecursiveLock_Unlock(&initLock);
|
||||
return MAKERESULT(RL_INFO, RS_NOP, 25, RD_ALREADY_INITIALIZED);
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
rc = svcConnectToPort(&srvHandle, "srv:");
|
||||
if (R_LEVEL(rc) != RL_PERMANENT ||
|
||||
R_SUMMARY(rc) != RS_NOTFOUND ||
|
||||
R_DESCRIPTION(rc) != RD_NOT_FOUND
|
||||
) break;
|
||||
svcSleepThread(500000);
|
||||
}
|
||||
if (R_SUCCEEDED(rc))
|
||||
{
|
||||
rc = srvSysRegisterClient();
|
||||
srvRefCount++;
|
||||
}
|
||||
|
||||
RecursiveLock_Unlock(&initLock);
|
||||
return rc;
|
||||
}
|
||||
|
||||
Result srvSysRegisterClient(void)
|
||||
{
|
||||
Result rc = 0;
|
||||
u32* cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x1,0,2); // 0x10002
|
||||
cmdbuf[1] = IPC_Desc_CurProcessHandle();
|
||||
|
||||
if(R_FAILED(rc = svcSendSyncRequest(srvHandle)))return rc;
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result srvSysExit()
|
||||
{
|
||||
Result rc;
|
||||
RecursiveLock_Lock(&initLock);
|
||||
|
||||
if (srvRefCount > 1)
|
||||
{
|
||||
srvRefCount--;
|
||||
RecursiveLock_Unlock(&initLock);
|
||||
return MAKERESULT(RL_INFO, RS_NOP, 25, RD_BUSY);
|
||||
}
|
||||
|
||||
if (srvHandle != 0) svcCloseHandle(srvHandle);
|
||||
else svcBreak(USERBREAK_ASSERT);
|
||||
rc = (Result)srvHandle; // yeah, I think this is a benign bug
|
||||
srvHandle = 0;
|
||||
srvRefCount--;
|
||||
RecursiveLock_Unlock(&initLock);
|
||||
return rc;
|
||||
}
|
||||
|
||||
Result srvSysGetServiceHandle(Handle* out, const char* name)
|
||||
{
|
||||
Result rc = 0;
|
||||
u32* cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x5,4,0); // 0x50100
|
||||
strncpy((char*) &cmdbuf[1], name,8);
|
||||
cmdbuf[3] = strlen(name);
|
||||
cmdbuf[4] = 0x0;
|
||||
|
||||
if(R_FAILED(rc = svcSendSyncRequest(srvHandle)))return rc;
|
||||
|
||||
if(out) *out = cmdbuf[3];
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result srvSysEnableNotification(Handle* semaphoreOut)
|
||||
{
|
||||
Result rc = 0;
|
||||
u32* cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x2,0,0);
|
||||
|
||||
if(R_FAILED(rc = svcSendSyncRequest(srvHandle)))return rc;
|
||||
|
||||
if(semaphoreOut) *semaphoreOut = cmdbuf[3];
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result srvSysReceiveNotification(u32* notificationIdOut)
|
||||
{
|
||||
Result rc = 0;
|
||||
u32* cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0xB,0,0); // 0xB0000
|
||||
|
||||
if(R_FAILED(rc = svcSendSyncRequest(srvHandle)))return rc;
|
||||
|
||||
if(notificationIdOut) *notificationIdOut = cmdbuf[2];
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result srvSysRegisterService(Handle* out, const char* name, int maxSessions)
|
||||
{
|
||||
Result rc = 0;
|
||||
u32* cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x3,4,0); // 0x30100
|
||||
strncpy((char*) &cmdbuf[1], name,8);
|
||||
cmdbuf[3] = strlen(name);
|
||||
cmdbuf[4] = maxSessions;
|
||||
|
||||
if(R_FAILED(rc = svcSendSyncRequest(srvHandle)))return rc;
|
||||
|
||||
if(out) *out = cmdbuf[3];
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
|
||||
Result srvSysUnregisterService(const char* name)
|
||||
{
|
||||
Result rc = 0;
|
||||
u32* cmdbuf = getThreadCommandBuffer();
|
||||
|
||||
cmdbuf[0] = IPC_MakeHeader(0x4,3,0); // 0x400C0
|
||||
strncpy((char*) &cmdbuf[1], name,8);
|
||||
cmdbuf[3] = strlen(name);
|
||||
|
||||
if(R_FAILED(rc = svcSendSyncRequest(srvHandle)))return rc;
|
||||
|
||||
return cmdbuf[1];
|
||||
}
|
||||
47
injector/source/srvsys.h
Normal file
47
injector/source/srvsys.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* @file srv.h
|
||||
* @brief Service API.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
/// Initializes the service API.
|
||||
Result srvSysInit(void);
|
||||
|
||||
/// Exits the service API.
|
||||
Result srvSysExit(void);
|
||||
|
||||
/**
|
||||
* @brief Retrieves a service handle, retrieving from the environment handle list if possible.
|
||||
* @param out Pointer to write the handle to.
|
||||
* @param name Name of the service.
|
||||
*/
|
||||
Result srvSysGetServiceHandle(Handle* out, const char* name);
|
||||
|
||||
/// Registers the current process as a client to the service API.
|
||||
Result srvSysRegisterClient(void);
|
||||
|
||||
/**
|
||||
* @brief Enables service notificatios, returning a notification semaphore.
|
||||
* @param semaphoreOut Pointer to output the notification semaphore to.
|
||||
*/
|
||||
Result srvSysEnableNotification(Handle* semaphoreOut);
|
||||
|
||||
/**
|
||||
* @brief Receives a notification.
|
||||
* @param notificationIdOut Pointer to output the ID of the received notification to.
|
||||
*/
|
||||
Result srvSysReceiveNotification(u32* notificationIdOut);
|
||||
|
||||
/**
|
||||
* @brief Registers the current process as a service.
|
||||
* @param out Pointer to write the service handle to.
|
||||
* @param name Name of the service.
|
||||
* @param maxSessions Maximum number of sessions the service can handle.
|
||||
*/
|
||||
Result srvSysRegisterService(Handle* out, const char* name, int maxSessions);
|
||||
|
||||
/**
|
||||
* @brief Unregisters the current process as a service.
|
||||
* @param name Name of the service.
|
||||
*/
|
||||
Result srvSysUnregisterService(const char* name);
|
||||
Reference in New Issue
Block a user