From: chaoskagami Date: Fri, 14 Oct 2016 07:08:26 +0000 (-0400) Subject: Clean up loader for ctrulib form X-Git-Tag: v0.3.1~66^2~9 X-Git-Url: https://chaos.moe/g/?a=commitdiff_plain;h=3559193f754bbfae824a3b549493f38cca025e40;p=corbenik%2Fcorbenik.git Clean up loader for ctrulib form --- diff --git a/external/Makefile.am b/external/Makefile.am index 52d2b6a..1508350 100644 --- a/external/Makefile.am +++ b/external/Makefile.am @@ -1,3 +1,3 @@ -SUBDIRS = libctr9 loader bits +SUBDIRS = libctr9 ctrulib/libctru loader bits install: diff --git a/external/loader/Makefile b/external/loader/Makefile index 81f1a7f..440f3a1 100644 --- a/external/loader/Makefile +++ b/external/loader/Makefile @@ -60,7 +60,7 @@ LIBS := -lctru # list of directories containing libraries, this must be the top level containing # include and lib #--------------------------------------------------------------------------------- -LIBDIRS := $(CTRULIB) +LIBDIRS := $(CURDIR)/../ctrulib/libctru #--------------------------------------------------------------------------------- diff --git a/external/loader/source/exheader.h b/external/loader/source/exheader.h deleted file mode 100644 index 8ea6792..0000000 --- a/external/loader/source/exheader.h +++ /dev/null @@ -1,112 +0,0 @@ -#ifndef __EXHEADER_H -#define __EXHEADER_H - -#include <3ds/types.h> - -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; - -typedef struct -{ - u8 reserved[5]; - u8 flag; // Maybe a feature - Bits 2-7 are unused. We could allow uh, custom flags here. Like zlib compression on code rather than lzss. - 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; - -// New3DS speed is flags[1]:1 - -typedef struct -{ - u64 programid; - u8 coreVersion[4]; // Kernel version required for this. - u8 flag2; - u8 flag1; - u8 flag0; // CPU speed settings. - u8 priority; - 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 -{ - exheader_codesetinfo codesetinfo; - exheader_dependencylist deplist; - exheader_systeminfo systeminfo; - 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; - -#endif diff --git a/external/loader/source/fsldr.c b/external/loader/source/fsldr.c deleted file mode 100644 index 6541e6d..0000000 --- a/external/loader/source/fsldr.c +++ /dev/null @@ -1,121 +0,0 @@ -/** - * Random annoyance; All the command headers etc for fsLdr are EXACTLY the same as fsUser. - * This is annpying mainly because - if I could open the file handle manually in ctrulib - - * e.g. not static and inaccessible - I could simply use the FSUSER API for this. ALL. OF. THIS. - */ -#include <3ds.h> -#include "fsldr.h" -#include "fsreg.h" -#include "srvsys.h" - -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_Initialize(fsldrHandle); - if (R_FAILED(ret)) - svcBreak(USERBREAK_ASSERT); // Can't properly panic here; no logger - } else { - AtomicDecrement(&fsldrRefCount); - } - - return ret; -} - -void -fsldrExit(void) -{ - if (AtomicDecrement(&fsldrRefCount)) - return; - svcCloseHandle(fsldrHandle); -} - -Result -FSLDR_Initialize(Handle session) -{ - u32 *cmdbuf = getThreadCommandBuffer(); - - cmdbuf[0] = IPC_MakeHeader(0x801, 0, 2); // 0x8010002 - cmdbuf[1] = 32; - - Result ret = 0; - if (R_FAILED(ret = svcSendSyncRequest(session))) - return ret; - - return cmdbuf[1]; -} - -Result -FSLDR_OpenFileDirectly(Handle *out, FS_ArchiveID archiveId, FS_Path archivePath, FS_Path filePath, u32 openFlags, u32 attributes) -{ - u32 *cmdbuf = getThreadCommandBuffer(); - - cmdbuf[0] = IPC_MakeHeader(0x803, 8, 4); // 0x8030204 - cmdbuf[1] = 0; - cmdbuf[2] = archiveId; - cmdbuf[3] = archivePath.type; - cmdbuf[4] = archivePath.size; - cmdbuf[5] = filePath.type; - cmdbuf[6] = filePath.size; - cmdbuf[7] = openFlags; - cmdbuf[8] = attributes; - cmdbuf[9] = IPC_Desc_StaticBuffer(archivePath.size, 2); - cmdbuf[10] = (u32)archivePath.data; - cmdbuf[11] = IPC_Desc_StaticBuffer(filePath.size, 0); - cmdbuf[12] = (u32)filePath.data; - - Result ret = 0; - if (R_FAILED(ret = svcSendSyncRequest(fsldrHandle))) - return ret; - - if (out) - *out = cmdbuf[3]; - - return cmdbuf[1]; -} - -Result -FSLDR_GetNandCid(u8* out, u32 length) -{ - u32 *cmdbuf = getThreadCommandBuffer(); - - cmdbuf[0] = IPC_MakeHeader(0x81A, 1, 2); // 0x81A0042 - cmdbuf[1] = length; - cmdbuf[2] = IPC_Desc_Buffer(length, IPC_BUFFER_W); - cmdbuf[3] = (u32) out; - - Result ret = 0; - if(R_FAILED(ret = svcSendSyncRequest(fsldrHandle))) - return ret; - - return cmdbuf[1]; -} diff --git a/external/loader/source/fsldr.h b/external/loader/source/fsldr.h deleted file mode 100644 index 9c24345..0000000 --- a/external/loader/source/fsldr.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef __FSLDR_H -#define __FSLDR_H - -#include <3ds/types.h> - -Result fsldrInit(void); -void fsldrExit(void); - -Result FSLDR_Initialize(Handle session); -Result FSLDR_SetPriority(u32 priority); -Result FSLDR_OpenFileDirectly(Handle *out, FS_ArchiveID archiveId, FS_Path archivePath, FS_Path filePath, u32 openFlags, u32 attributes); -Result FSLDR_GetNandCid(u8* out, u32 length); - -#endif diff --git a/external/loader/source/fsreg.c b/external/loader/source/fsreg.c deleted file mode 100644 index 4ac0abd..0000000 --- a/external/loader/source/fsreg.c +++ /dev/null @@ -1,133 +0,0 @@ -#include <3ds.h> -#include -#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]; -} diff --git a/external/loader/source/fsreg.h b/external/loader/source/fsreg.h deleted file mode 100644 index 6e366db..0000000 --- a/external/loader/source/fsreg.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef __FSREG_H -#define __FSREG_H - -#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); - -#endif diff --git a/external/loader/source/interp.c b/external/loader/source/interp.c index e1c5001..b715649 100644 --- a/external/loader/source/interp.c +++ b/external/loader/source/interp.c @@ -2,8 +2,6 @@ #include <3ds.h> #include #include "patcher.h" -#include "exheader.h" -#include "fsldr.h" #include #include #include diff --git a/external/loader/source/loader.c b/external/loader/source/loader.c index 23041c8..78609ec 100644 --- a/external/loader/source/loader.c +++ b/external/loader/source/loader.c @@ -1,10 +1,6 @@ #include <3ds.h> #include "patcher.h" -#include "exheader.h" -#include "fsldr.h" -#include "fsreg.h" #include "pxipm.h" -#include "srvsys.h" #include #include #include @@ -21,16 +17,16 @@ const char CODE_PATH[] = { 0x01, 0x00, 0x00, 0x00, 0x2E, 0x63, 0x6F, 0x64, 0x65, static Handle g_handles[MAX_SESSIONS + 2]; static int g_active_handles; static u64 g_cached_prog_handle; -static exheader_header g_exheader; +static EXHEADER_header g_exheader; static char g_ret_buf[1024]; static Result -allocate_shared_mem(prog_addrs_t *shared, prog_addrs_t *vaddr, int flags) +allocate_shared_mem(EXHEADER_prog_addrs *shared, EXHEADER_prog_addrs *vaddr, int flags) { // Somehow, we need to allow reallocating. u32 dummy; - memcpy(shared, vaddr, sizeof(prog_addrs_t)); + memcpy(shared, vaddr, sizeof(EXHEADER_prog_addrs)); shared->text_addr = 0x10000000; // Base virtual address for code. shared->ro_addr = shared->text_addr + (shared->text_size << 12); shared->data_addr = shared->ro_addr + (shared->ro_size << 12); @@ -38,7 +34,7 @@ allocate_shared_mem(prog_addrs_t *shared, prog_addrs_t *vaddr, int flags) } static Result -load_code(u64 progid, u16 progver, prog_addrs_t *shared, prog_addrs_t *original, u64 prog_handle, int is_compressed) +load_code(u64 progid, u16 progver, EXHEADER_prog_addrs *shared, EXHEADER_prog_addrs *original, u64 prog_handle, int is_compressed) { Handle handle; FS_Path archivePath; @@ -55,7 +51,7 @@ load_code(u64 progid, u16 progver, prog_addrs_t *shared, prog_addrs_t *original, path.data = CODE_PATH; path.size = sizeof(CODE_PATH); - if (R_FAILED(FSLDR_OpenFileDirectly(&handle, ARCHIVE_SAVEDATA_AND_CONTENT2, archivePath, path, FS_OPEN_READ, 0))) { + if (R_FAILED(FSUSER_OpenFileDirectly(&handle, ARCHIVE_SAVEDATA_AND_CONTENT2, archivePath, path, FS_OPEN_READ, 0))) { panicstr("Failed to open program code path.\n"); } @@ -94,7 +90,7 @@ load_code(u64 progid, u16 progver, prog_addrs_t *shared, prog_addrs_t *original, } static Result -loader_GetProgramInfo(exheader_header *exheader, u64 prog_handle) +loader_GetProgramInfo(EXHEADER_header *exheader, u64 prog_handle) { Result res; @@ -122,9 +118,9 @@ loader_LoadProcess(Handle *process, u64 prog_handle) u32 flags; u32 desc; u32 dummy; - prog_addrs_t shared_addr; - prog_addrs_t vaddr; - prog_addrs_t original_vaddr; + EXHEADER_prog_addrs shared_addr; + EXHEADER_prog_addrs vaddr; + EXHEADER_prog_addrs original_vaddr; Handle codeset; CodeSetInfo codesetinfo; u32 data_mem_size; @@ -370,7 +366,7 @@ should_terminate(int *term_request) u32 notid; Result ret; - ret = srvSysReceiveNotification(¬id); + ret = srvReceiveNotification(¬id); if (R_FAILED(ret)) { return ret; } @@ -399,11 +395,11 @@ main() srv_handle = &g_handles[1]; notification_handle = &g_handles[0]; - if (R_FAILED(srvSysRegisterService(srv_handle, "Loader", MAX_SESSIONS))) { + if (R_FAILED(srvRegisterService(srv_handle, "Loader", MAX_SESSIONS))) { panicstr("Failed to register loader service.\n"); } - if (R_FAILED(srvSysEnableNotification(notification_handle))) { + if (R_FAILED(srvEnableNotification(notification_handle))) { panicstr("Failed to enable notifcations to loader service.\n"); } @@ -472,7 +468,7 @@ main() } } while (!term_request || g_active_handles != 2); - srvSysUnregisterService("Loader"); + srvUnregisterService("Loader"); svcCloseHandle(*srv_handle); svcCloseHandle(*notification_handle); diff --git a/external/loader/source/logger.c b/external/loader/source/logger.c index 6ff8856..6bc3896 100644 --- a/external/loader/source/logger.c +++ b/external/loader/source/logger.c @@ -1,6 +1,5 @@ #include <3ds.h> #include "patcher.h" -#include "fsldr.h" #include #include #include diff --git a/external/loader/source/memory.c b/external/loader/source/memory.c index 416492b..a6dcccd 100644 --- a/external/loader/source/memory.c +++ b/external/loader/source/memory.c @@ -1,7 +1,6 @@ #include <3ds.h> #include #include "patcher.h" -#include "fsldr.h" #include #ifndef PATH_MAX diff --git a/external/loader/source/patcher.c b/external/loader/source/patcher.c index 0024bc7..2db0d42 100644 --- a/external/loader/source/patcher.c +++ b/external/loader/source/patcher.c @@ -1,8 +1,6 @@ #include <3ds.h> #include #include "patcher.h" -#include "exheader.h" -#include "fsldr.h" #include #include #include @@ -39,7 +37,7 @@ fileOpen(Handle *file, FS_ArchiveID id, const char *path, int flags) ppath.data = path; ppath.size = strnlen(path, PATH_MAX) + 1; - return FSLDR_OpenFileDirectly(file, id, apath, ppath, flags, 0); + return FSUSER_OpenFileDirectly(file, id, apath, ppath, flags, 0); } struct config_file config; @@ -53,7 +51,7 @@ load_config() static u32 cid[4]; static char config_file_path[] = SYSCONFDIR "/config-00000000"; - if (!R_SUCCEEDED(FSLDR_GetNandCid((u8*)cid, 0x10))) { + if (!R_SUCCEEDED(FSUSER_GetNandCid((u8*)cid, 0x10))) { return; } @@ -405,7 +403,7 @@ overlay_patch(_UNUSED u64 progId, _UNUSED u8 *code, _UNUSED u32 size) } void -code_handler(u64 progId, prog_addrs_t *shared) +code_handler(u64 progId, EXHEADER_prog_addrs *shared) { // If configuration was not loaded, or both options (load / dump) are disabled if (failed_load_config || (!config.options[OPTION_LOADER_DUMPCODE] && !config.options[OPTION_LOADER_LOADCODE])) diff --git a/external/loader/source/patcher.h b/external/loader/source/patcher.h index cf3b70b..8013352 100644 --- a/external/loader/source/patcher.h +++ b/external/loader/source/patcher.h @@ -2,11 +2,11 @@ #define __PATCHER_H #include <3ds/types.h> -#include "exheader.h" +#include <3ds/exheader.h> void patch_exe(u64 progId, u16 progver, u8 *text, u32 text_size, u32 orig_text, u8 *data, u32 data_size, u32 orig_data, u8 *ro, u32 ro_size, u32 orig_ro); -void code_handler(u64 progId, prog_addrs_t* shared); +void code_handler(u64 progId, EXHEADER_prog_addrs* shared); u32 get_text_extend(u64 progId, u16 progver, u32 size_orig); u32 get_ro_extend(u64 progId, u16 progver, u32 size_orig); diff --git a/external/loader/source/pxipm.c b/external/loader/source/pxipm.c index e89440a..b1cc6bd 100644 --- a/external/loader/source/pxipm.c +++ b/external/loader/source/pxipm.c @@ -51,7 +51,7 @@ PXIPM_RegisterProgram(u64 *prog_handle, FS_ProgramInfo *title, FS_ProgramInfo *u } Result -PXIPM_GetProgramInfo(exheader_header *exheader, u64 prog_handle) +PXIPM_GetProgramInfo(EXHEADER_header *exheader, u64 prog_handle) { u32 *cmdbuf = getThreadCommandBuffer(); diff --git a/external/loader/source/pxipm.h b/external/loader/source/pxipm.h index c8769fe..0784f41 100644 --- a/external/loader/source/pxipm.h +++ b/external/loader/source/pxipm.h @@ -2,12 +2,12 @@ #define __PXIPM_H #include <3ds/types.h> -#include "exheader.h" +#include <3ds/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_GetProgramInfo(EXHEADER_header *exheader, u64 prog_handle); Result PXIPM_UnregisterProgram(u64 prog_handle); #endif diff --git a/external/loader/source/statics.c b/external/loader/source/statics.c index e4c5dcd..2970ac8 100644 --- a/external/loader/source/statics.c +++ b/external/loader/source/statics.c @@ -1,10 +1,6 @@ #include <3ds.h> #include "patcher.h" -#include "exheader.h" -#include "fsldr.h" -#include "fsreg.h" #include "pxipm.h" -#include "srvsys.h" #include #include "logger.h" @@ -24,9 +20,9 @@ void __system_initSyscalls(void); void __appInit() { - srvSysInit(); + srvInit(); fsregInit(); - fsldrInit(); + fsInitFromService("fs:LDR"); pxipmInit(); } @@ -35,9 +31,9 @@ void __appExit() { pxipmExit(); - fsldrExit(); + fsExit(); fsregExit(); - srvSysExit(); + srvExit(); } void __system_allocateHeaps(void) {