From f864b601115465b9b72c94350970404eccb939b6 Mon Sep 17 00:00:00 2001 From: Nikita Krapivin Date: Fri, 21 Oct 2022 09:53:36 +0500 Subject: [PATCH] Publish v2.1 release source code --- .vscode/settings.json | 5 +- README.md | 2 +- REMoveAndroidApp/app/build.gradle | 6 +- .../org/nkrapivindev/remove/MainActivity.java | 2 + .../org/nkrapivindev/remove/REMoveClient.java | 55 +-- oobuild.py | 35 +- remove/goldhensdk/.gitignore | 52 +++ remove/goldhensdk/Detour.h | 55 --- remove/goldhensdk/README.md | 29 +- remove/goldhensdk/build_static.bat | 23 ++ remove/goldhensdk/include/Common.h | 53 +++ remove/goldhensdk/include/Detour.h | 53 +++ remove/goldhensdk/include/GoldHEN.h | 66 ++++ remove/goldhensdk/include/HDE64.h | 117 +++++++ remove/goldhensdk/include/Patcher.h | 32 ++ remove/goldhensdk/include/Syscall.h | 26 ++ remove/goldhensdk/include/Table64.h | 84 +++++ remove/goldhensdk/include/Utilities.h | 79 +++++ remove/goldhensdk/libGoldHEN_Hook.a | Bin 53266 -> 0 bytes remove/goldhensdk/source/Detour.c | 262 ++++++++++++++ remove/goldhensdk/source/GoldHEN.c | 34 ++ remove/goldhensdk/source/HDE64.c | 323 ++++++++++++++++++ remove/goldhensdk/source/Patcher.c | 77 +++++ remove/goldhensdk/source/Syscall.c | 39 +++ remove/goldhensdk/source/Utilities.c | 37 ++ remove/remove_goldhen_glue.cpp | 61 ---- remove/remove_goldhen_glue.h | 23 -- remove/remove_module.c | 136 +++----- 28 files changed, 1472 insertions(+), 294 deletions(-) create mode 100644 remove/goldhensdk/.gitignore delete mode 100644 remove/goldhensdk/Detour.h create mode 100644 remove/goldhensdk/build_static.bat create mode 100644 remove/goldhensdk/include/Common.h create mode 100644 remove/goldhensdk/include/Detour.h create mode 100644 remove/goldhensdk/include/GoldHEN.h create mode 100644 remove/goldhensdk/include/HDE64.h create mode 100644 remove/goldhensdk/include/Patcher.h create mode 100644 remove/goldhensdk/include/Syscall.h create mode 100644 remove/goldhensdk/include/Table64.h create mode 100644 remove/goldhensdk/include/Utilities.h delete mode 100644 remove/goldhensdk/libGoldHEN_Hook.a create mode 100644 remove/goldhensdk/source/Detour.c create mode 100644 remove/goldhensdk/source/GoldHEN.c create mode 100644 remove/goldhensdk/source/HDE64.c create mode 100644 remove/goldhensdk/source/Patcher.c create mode 100644 remove/goldhensdk/source/Syscall.c create mode 100644 remove/goldhensdk/source/Utilities.c delete mode 100644 remove/remove_goldhen_glue.cpp delete mode 100644 remove/remove_goldhen_glue.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 47c74f4..ae4be1a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -87,6 +87,9 @@ "user.h": "c", "errors.h": "c", "sysmodule.h": "c", - "move.h": "c" + "move.h": "c", + "common.h": "c", + "utilities.h": "c", + "strings.h": "c" } } \ No newline at end of file diff --git a/README.md b/README.md index f9f88ea..7e82273 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This is basically PS Move for the poor. # How to install? - For Mira CFW: Copy `libREMove.sprx` to `/data/mira/substitute/CUSAXXXXX/`, where `CUSAXXXXX` is the game you wish to patch. -- For GoldHEN (TODO!!!! NOT FINISHED!!!!): Rename `libREMove.sprx` to `fps.prx` and copy to `/data/GoldHEN/`. +- For GoldHEN 2.2.5 and newer: Rename `libREMove.sprx` to `test.prx` and copy to `/data/GoldHEN/plugins`. - Install the APK on your device. - Open the app, go to Settings and set your Console IP, usually you do not need to touch the port. - Run the game you wanted to patch, press "CONNECT" in the app, select your user, enjoy! diff --git a/REMoveAndroidApp/app/build.gradle b/REMoveAndroidApp/app/build.gradle index 6477840..c830e02 100644 --- a/REMoveAndroidApp/app/build.gradle +++ b/REMoveAndroidApp/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "org.nkrapivindev.remove" minSdk 25 targetSdk 33 - versionCode 2 - versionName "2.0" + versionCode 3 + versionName "2.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" targetSdkVersion 33 @@ -32,7 +32,7 @@ android { dependencies { - implementation 'androidx.appcompat:appcompat:1.5.0' + implementation 'androidx.appcompat:appcompat:1.5.1' implementation 'com.google.android.material:material:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.preference:preference:1.2.0' diff --git a/REMoveAndroidApp/app/src/main/java/org/nkrapivindev/remove/MainActivity.java b/REMoveAndroidApp/app/src/main/java/org/nkrapivindev/remove/MainActivity.java index 96e1a67..8fa258a 100644 --- a/REMoveAndroidApp/app/src/main/java/org/nkrapivindev/remove/MainActivity.java +++ b/REMoveAndroidApp/app/src/main/java/org/nkrapivindev/remove/MainActivity.java @@ -267,10 +267,12 @@ else if (act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_CANCEL) { getSystemService(Context.VIBRATOR_MANAGER_SERVICE); if (vibman != null) { vib = vibman.getDefaultVibrator(); + print("Using new vibration API"); } } else { vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); + print("Using old vibration API"); } btnCross = findViewById(R.id.button_cross); diff --git a/REMoveAndroidApp/app/src/main/java/org/nkrapivindev/remove/REMoveClient.java b/REMoveAndroidApp/app/src/main/java/org/nkrapivindev/remove/REMoveClient.java index 82369b8..db953ca 100644 --- a/REMoveAndroidApp/app/src/main/java/org/nkrapivindev/remove/REMoveClient.java +++ b/REMoveAndroidApp/app/src/main/java/org/nkrapivindev/remove/REMoveClient.java @@ -51,7 +51,9 @@ public void run() { REMoveNetPacketHelloV2 hello = new REMoveNetPacketHelloV2(); int got; REMoveApplicationData appData; - //long startTime; + long startTime; + + log("Entering thread loop..."); while (running.get()) { // initialize the socket: @@ -95,6 +97,7 @@ public void run() { // still negative? cancelled, kill us if (myuserind < 0) { + log("User selection cancelled"); break; } else { @@ -113,7 +116,7 @@ public void run() { } // main loop... - //startTime = System.nanoTime(); + startTime = System.nanoTime(); if (app != null) { appData = app.onProvideData(); if (appData != null) { @@ -138,37 +141,37 @@ public void run() { ); // obtain any update frames from the server: - got = sckIn.read(buff); - if (got != REMoveNetPacketToClientV2.EXPECTED_SIZEOF) { - throw new REMoveSizeOfException( - REMoveNetPacketToClientV2.EXPECTED_SIZEOF, - got - ); - } - buffstream.rewind(); - toUs.fromStream(buffstream); - - if (app != null) { - if ((toUs.updateFlags - & REMoveNetPacketToClientV2.UPDATE_FLAG_SET_LIGHTSPHERE_COLOR) != 0) { - // we have color update data, send that - app.onColorUpdate(toUs.red, toUs.green, toUs.blue); + //if (sckIn.available() >= REMoveNetPacketToClientV2.EXPECTED_SIZEOF) { + got = sckIn.read(buff, 0, REMoveNetPacketToClientV2.EXPECTED_SIZEOF); + if (got != REMoveNetPacketToClientV2.EXPECTED_SIZEOF) { + throw new REMoveSizeOfException( + REMoveNetPacketToClientV2.EXPECTED_SIZEOF, + got + ); } + buffstream.rewind(); + toUs.fromStream(buffstream); - if ((toUs.updateFlags - & REMoveNetPacketToClientV2.UPDATE_FLAG_SET_VIBRATION) != 0) { - // we have vibration data, send that - app.onMotorUpdate(toUs.motorValue); + if (app != null) { + if ((toUs.updateFlags + & REMoveNetPacketToClientV2.UPDATE_FLAG_SET_LIGHTSPHERE_COLOR) != 0) { + // we have color update data, send that + app.onColorUpdate(toUs.red, toUs.green, toUs.blue); + } + + if ((toUs.updateFlags + & REMoveNetPacketToClientV2.UPDATE_FLAG_SET_VIBRATION) != 0) { + // we have vibration data, send that + app.onMotorUpdate(toUs.motorValue); + } } - } + //} - /* long estimatedTime = System.nanoTime() - startTime; - if ((System.currentTimeMillis() % 10000) < 10) { - log("Estimated time = " + (((double) estimatedTime) / 1000000) + "ms"); + if ((System.currentTimeMillis() % 10000) < 100) { + log("Estimated time = " + (((double) estimatedTime) / 1000000.0) + "ms"); } - */ } log("The thread is requesting a close gracefully..."); diff --git a/oobuild.py b/oobuild.py index ba0b575..4a6dbef 100644 --- a/oobuild.py +++ b/oobuild.py @@ -8,10 +8,10 @@ print("> Building libREMove for PS4 via OO") -LLVM_BIN_PATH = os.environ.get("OPENALPS4_LLVM_BIN_PATH") +LLVM_BIN_PATH = os.environ.get("OO_LLVM_BIN_PATH") if LLVM_BIN_PATH is None: - LLVM_BIN_PATH = "D:\\SDK\\LLVM10\\bin" + LLVM_BIN_PATH = "D:/SDK/LLVM10/bin" # only works with the latest nightly release of the OpenOrbis PS4 Toolchain OO_PS4_TOOLCHAIN = os.environ.get("OO_PS4_TOOLCHAIN") @@ -27,10 +27,10 @@ BUILD_FOLDER = os.path.join(ROOT_DIR, "build", BUILD_FOLDER_NAME) # Dependencies: libunwind is auto-merged into libc++ in nightly OO builds for simplicity -LINK_WITH = "-lGoldHEN_Hook -lkernel -lc -lc++ -lSceUserService -lSceNet -lSceSysmodule" +LINK_WITH = "-lkernel -lc -lSceUserService -lSceNet" SRC_FOLDER = os.path.join(ROOT_DIR, "remove") -GOLDHEN_PLUGIN_SDK = os.path.join(SRC_FOLDER, "goldhensdk") +GOLDHEN_PLUGIN_SDK = os.path.join(SRC_FOLDER, "goldhensdk", "include") # name of the final output, must start with lib FINAL_NAME = "libREMove" @@ -44,25 +44,22 @@ COMPILER_WFLAGS = " -Wpedantic " -COMPILER_FFLAGS = " -fPIC -fvisibility=hidden -march=btver2 -O2 -c " +COMPILER_FFLAGS = " -fPIC -fvisibility=hidden -march=btver2 -mtune=btver2 -O2 -c " -COMPILER_CFLAGS = " -std=c99 " - -COMPILER_PFLAGS = f" -std=c++14 -isystem \"{OO_PS4_TOOLCHAIN}/include/c++/v1\" " +COMPILER_CFLAGS = " -std=c11 " COMPILER_IFLAGS = f" -isysroot \"{OO_PS4_TOOLCHAIN}\" -isystem \"{OO_PS4_TOOLCHAIN}/include\" " + \ - f" -I\"{SRC_FOLDER}\" " + f" -I\"{SRC_FOLDER}\" -I\"{GOLDHEN_PLUGIN_SDK}\" " # use freebsd12 target, define some generic ps4 defines, force exceptions to ON since we have to do that for now COMPILER_FLAGS = f" --target=x86_64-pc-freebsd12-elf -fexceptions -funwind-tables -fuse-init-array " + \ f" {COMPILER_WFLAGS} {COMPILER_FFLAGS} {COMPILER_IFLAGS} {COMPILER_DEFINES} " # link with PRX crtlib -LINKER_FLAGS = f" -m elf_x86_64 -pie --script \"{OO_PS4_TOOLCHAIN}/link.x\" " + \ - f" --eh-frame-hdr --verbose -e _init -L\"{GOLDHEN_PLUGIN_SDK}\" -L\"{OO_PS4_TOOLCHAIN}/lib\" {LINK_WITH} -o \"{ELF_PATH}\" " +LINKER_FLAGS = f" -m elf_x86_64 --script \"{OO_PS4_TOOLCHAIN}/link.x\" " + \ + f" --eh-frame-hdr --verbose -pie -e _init -L\"{OO_PS4_TOOLCHAIN}/lib\" {LINK_WITH} -o \"{ELF_PATH}\" " C_COMPILER_EXE = os.path.join(LLVM_BIN_PATH, "clang") -CPP_COMPILER_EXE = os.path.join(LLVM_BIN_PATH, "clang++") LINKER_EXE = os.path.join(LLVM_BIN_PATH, "ld.lld") TOOL_EXE = os.path.join(OO_PS4_TOOLCHAIN, "bin", "windows", "create-fself") @@ -71,7 +68,13 @@ remove/substitute.c remove/remove_ctx.c remove/remove_module.c -remove/remove_goldhen_glue.cpp + +remove/goldhensdk/source/GoldHEN.c +remove/goldhensdk/source/Utilities.c +remove/goldhensdk/source/Syscall.c +remove/goldhensdk/source/HDE64.c +remove/goldhensdk/source/Patcher.c +remove/goldhensdk/source/Detour.c """ # quoted .o paths @@ -107,11 +110,7 @@ def run_compiler_at(srcfile: str, objfile: str, params: str) -> int: global OBJECTS comp = C_COMPILER_EXE - if srcfile.endswith(".cpp") or srcfile.endswith(".cxx"): - params = COMPILER_PFLAGS + params - comp = CPP_COMPILER_EXE - else: - params = COMPILER_CFLAGS + params + params = COMPILER_CFLAGS + params runargs = f"{params} -o \"{objfile}\" \"{srcfile}\"" fullline = f"{comp} {runargs}" diff --git a/remove/goldhensdk/.gitignore b/remove/goldhensdk/.gitignore new file mode 100644 index 0000000..c6127b3 --- /dev/null +++ b/remove/goldhensdk/.gitignore @@ -0,0 +1,52 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf diff --git a/remove/goldhensdk/Detour.h b/remove/goldhensdk/Detour.h deleted file mode 100644 index 802ce42..0000000 --- a/remove/goldhensdk/Detour.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * GoldHEN Plugin SDK - a prx hook/patch sdk for Orbis OS - * - * Credits - * - OSM - * - jocover - * - bucanero - * - OpenOrbis Team - * - SiSTRo - */ - -#pragma once - -#include -#include - -enum DetourMode { - x64, x32 -}; - -class Detour { -private: - DetourMode Mode; - void *StubPtr = 0; - size_t StubSize = 0; - - void *FunctionPtr = 0; - void *HookPtr = 0; - - uint8_t JumpInstructions64[14] = {0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90}; // jmp QWORD PTR[Address] - uint8_t JumpInstructions32[05] = {0xE9, 0x00, 0x00, 0x00, 0x00}; // jmp 32 - - void WriteJump64(void *Address, uint64_t Destination); - - void WriteJump32(void *Address, uint64_t Destination); - - void *DetourFunction64(uint64_t FunctionPtr, void *HookPtr); - - void *DetourFunction32(uint64_t FunctionPtr, void *HookPtr); - -public: - template - result Stub(Args... args) { - result (*Stub_internal)(Args... args) = decltype(Stub_internal)(StubPtr); - return Stub_internal(args...); - } - - void *DetourFunction(uint64_t FunctionPtr, void *HookPtr); - - void RestoreFunction(); - - Detour(DetourMode Mode); - - ~Detour(); -}; diff --git a/remove/goldhensdk/README.md b/remove/goldhensdk/README.md index 4226c3c..9b19aa0 100644 --- a/remove/goldhensdk/README.md +++ b/remove/goldhensdk/README.md @@ -1,22 +1,31 @@ -# GoldHEN Plugin SDK Files +# GoldHEN Plugin SDK - a prx hook/patch sdk for Orbis OS -All files in this folder were provided by SiSTRo, the creator and maintainer of GoldHEN. +This is the GoldHEN Plugin SDK repository. -He permitted the REMove project to use these files so function hooks work in GoldHEN as well, not just Mira. +It requires OpenOrbis PS4 Toolchain and LLVM to be installed. -This requires very latest GoldHEN to be running. +## Including the SDK as source -If you're going to use these files in your project, I'd ask SiSTRo first. +It's better if you copy the SDK source files into your project and build the SDK from source *with* your project, +because there may be some compiler or OpenOrbis Toolchain bugfixes which would require rebuilding the GoldHEN SDK. -I still do not know why he didn't go the "pseudo-file" way like Mira did with it's `/dev/mira` ioctl-thing and made a static lib instead :/ +Just build all files from `source/` as C source code, and include stuff from `include/`. -It's also in C++ and was most likely compiled with OpenOrbis, so it relies on a mostly-specific OpenOrbis Toolchain version. +## Including the SDK as a static library -The toolchain is evolving constantly, a custom LLVM for OpenOrbis is on it's way, which will most likely break things. +If you are sure you won't be rebuilding the GoldHEN SDK, then you can include stuff from `include/`, +run `build_static.bat` to build a static library `libGoldHEN_Hook.a` and then use it in your project for the implementation of the headers. -Since my code *is* portable (doesn't even require malloc! just a proper linker to import SceNet/SceUserService/libkernel!), +## Credits -I had to make an extern "C" glue which makes the thing even worse. +- OSM +- jocover + +- bucanero + +- OpenOrbis Team + +- SiSTRo diff --git a/remove/goldhensdk/build_static.bat b/remove/goldhensdk/build_static.bat new file mode 100644 index 0000000..fbe0759 --- /dev/null +++ b/remove/goldhensdk/build_static.bat @@ -0,0 +1,23 @@ +@echo off +rem GoldHEN SDK static lib build script + + +rem change these if you need: +rem C compiler +set CC=clang +rem AR archiver +set AR=llvm-ar + +rem usually you do not need to change the stuff below: + +set CFLAGS=--target=x86_64-pc-freebsd12-elf -DORBIS=1 -D__ORBIS__=1 -DPS4=1 -DOO=1 -D__PS4__=1 -DOOPS4=1 -D__OOPS4__=1 -D__BSD_VISIBLE=1 -D_BSD_SOURCE=1 -march=btver2 -mtune=btver2 -fPIC -std=c11 -isysroot "%OO_PS4_TOOLCHAIN%" -isystem "%OO_PS4_TOOLCHAIN%\include" -Iinclude + +rmdir /s /q build +mkdir build + +for %%f in (source\*.c) do ( + %CC% %CFLAGS% -c %%f -o build\%%~nf.o +) + +%AR% --format=bsd rcs libGoldHEN_Hook.a build\*.o + diff --git a/remove/goldhensdk/include/Common.h b/remove/goldhensdk/include/Common.h new file mode 100644 index 0000000..cbf7549 --- /dev/null +++ b/remove/goldhensdk/include/Common.h @@ -0,0 +1,53 @@ +/* + * GoldHEN Plugin SDK - a prx hook/patch sdk for Orbis OS + * + * Credits + * - OSM + * - jocover + * - bucanero + * - OpenOrbis Team + * - SiSTRo + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#define DEBUG +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Detour.h" +#include "GoldHEN.h" +#include "Utilities.h" +#include "Syscall.h" + +#define STRINGIFY(x) #x +#define STRINGIFY_DEEP(x) STRINGIFY(x) + +#define ORBIS_SYSMODULE_INTERNAL_SYS_CORE 0x80000004 +#define ORBIS_SYSMODULE_INTERNAL_NETCTL 0x80000009 +#define ORBIS_SYSMODULE_INTERNAL_HTTP 0x8000000A +#define ORBIS_SYSMODULE_INTERNAL_SSL 0x8000000B +#define ORBIS_SYSMODULE_INTERNAL_NP_COMMON 0x8000000C +#define ORBIS_SYSMODULE_INTERNAL_SYSTEM_SERVICE 0x80000010 +#define ORBIS_SYSMODULE_INTERNAL_USER_SERVICE 0x80000011 +#define ORBIS_SYSMODULE_INTERNAL_APPINSTUTIL 0x80000014 +#define ORBIS_SYSMODULE_INTERNAL_NET 0x8000001C +#define ORBIS_SYSMODULE_INTERNAL_IPMI 0x8000001D +#define ORBIS_SYSMODULE_INTERNAL_VIDEO_OUT 0x80000022 +#define ORBIS_SYSMODULE_INTERNAL_BGFT 0x8000002A +#define ORBIS_SYSMODULE_INTERNAL_PRECOMPILED_SHADERS 0x80000064 + +#ifdef __cplusplus +} +#endif diff --git a/remove/goldhensdk/include/Detour.h b/remove/goldhensdk/include/Detour.h new file mode 100644 index 0000000..d466c68 --- /dev/null +++ b/remove/goldhensdk/include/Detour.h @@ -0,0 +1,53 @@ +/* + * GoldHEN Plugin SDK - a prx hook/patch sdk for Orbis OS + * + * Credits + * - OSM + * - jocover + * - bucanero + * - OpenOrbis Team + * - SiSTRo + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +typedef enum _GHSDK_DetourMode { + DetourMode_x64, + DetourMode_x32 +} DetourMode; + +typedef struct _GHSDK_Detour { + DetourMode Mode; + void *StubPtr; + size_t StubSize; + void *FunctionPtr; + void *TrampolinePtr; + void *HookPtr; + uint8_t JumpInstructions64[14]; // jmp QWORD PTR[Address] + uint8_t JumpInstructions32[05]; // jmp 32 +} Detour; + +// usage: +// typedef int(*somefunc_t)(int, void *, const char *); +// int result = Detour_Stub(&SomeHook, somefunc_t, /* arguments begin */ 1, NULL, "hi"); +#define Detour_Stub(This, FunctionPointerType, ...) (((FunctionPointerType)((This)->StubPtr))(/* Arguments */__VA_ARGS__)) + +void *Detour_DetourFunction(Detour *This, uint64_t FunctionPtr, void *HookPtr); + +void Detour_RestoreFunction(Detour *This); + +void Detour_Construct(Detour *This, DetourMode Mode); + +void Detour_Destroy(Detour *This); + +#ifdef __cplusplus +} +#endif diff --git a/remove/goldhensdk/include/GoldHEN.h b/remove/goldhensdk/include/GoldHEN.h new file mode 100644 index 0000000..1c7d703 --- /dev/null +++ b/remove/goldhensdk/include/GoldHEN.h @@ -0,0 +1,66 @@ +/* + * GoldHEN Plugin SDK - a prx hook/patch sdk for Orbis OS + * + * Credits + * - OSM + * - jocover + * - bucanero + * - OpenOrbis Team + * - SiSTRo + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +struct jailbreak_backup +{ + uint32_t cr_uid; + uint32_t cr_ruid; + uint32_t cr_rgid; + uint32_t cr_groups; + uint64_t cr_paid; + uint64_t cr_caps[2]; + void* cr_prison; + + void* fd_cdir; + void* fd_jdir; + void* fd_rdir; +}; + +struct proc_info { + int pid; + char name[40]; + char path[64]; + char titleid[16]; + char contentid[64]; + char version[6]; + uint64_t base_address; +} __attribute__((packed)); + +struct proc_hook { + uint64_t function; + uint64_t hook; + uint64_t trampoline; + uint64_t stub; + uint64_t stub_size; +} __attribute__((packed)); + +#define GOLDHEN_SDK_CMD_KLOG 1 +#define GOLDHEN_SDK_CMD_JAILBREAK 2 +#define GOLDHEN_SDK_CMD_UNJAILBREAK 3 +#define GOLDHEN_SDK_CMD_PROCESS_INFO 4 + +extern int sys_sdk_cmd(uint64_t cmd, void *data); + +extern int sys_sdk_jailbreak(struct jailbreak_backup* jb); + +extern int sys_sdk_unjailbreak(struct jailbreak_backup* jb); + +int sys_sdk_proc_info(struct proc_info* info); + +#ifdef __cplusplus +} +#endif diff --git a/remove/goldhensdk/include/HDE64.h b/remove/goldhensdk/include/HDE64.h new file mode 100644 index 0000000..d0d939b --- /dev/null +++ b/remove/goldhensdk/include/HDE64.h @@ -0,0 +1,117 @@ +/* +* Hacker Disassembler Engine 64 +* Copyright (c) 2008-2009, Vyacheslav Patkov. +* All rights reserved. +* +* hde64.h: C/C++ header file +* +*/ + +#pragma once + +/* stdint.h - C99 standard header +* http://en.wikipedia.org/wiki/stdint.h +* +* if your compiler doesn't contain "stdint.h" header (for +* example, Microsoft Visual C++), you can download file: +* http://www.azillionmonkeys.com/qed/pstdint.h +* and change next line to: +* #include "pstdint.h" +*/ + +// User mode +#if defined(__cplusplus) +#include +#else // defined(__cplusplus) + +#include + +#endif // defined(__cplusplus) + +#define F_MODRM 0x00000001 +#define F_SIB 0x00000002 +#define F_IMM8 0x00000004 +#define F_IMM16 0x00000008 +#define F_IMM32 0x00000010 +#define F_IMM64 0x00000020 +#define F_DISP8 0x00000040 +#define F_DISP16 0x00000080 +#define F_DISP32 0x00000100 +#define F_RELATIVE 0x00000200 +#define F_ERROR 0x00001000 +#define F_ERROR_OPCODE 0x00002000 +#define F_ERROR_LENGTH 0x00004000 +#define F_ERROR_LOCK 0x00008000 +#define F_ERROR_OPERAND 0x00010000 +#define F_PREFIX_REPNZ 0x01000000 +#define F_PREFIX_REPX 0x02000000 +#define F_PREFIX_REP 0x03000000 +#define F_PREFIX_66 0x04000000 +#define F_PREFIX_67 0x08000000 +#define F_PREFIX_LOCK 0x10000000 +#define F_PREFIX_SEG 0x20000000 +#define F_PREFIX_REX 0x40000000 +#define F_PREFIX_ANY 0x7f000000 + +#define PREFIX_SEGMENT_CS 0x2e +#define PREFIX_SEGMENT_SS 0x36 +#define PREFIX_SEGMENT_DS 0x3e +#define PREFIX_SEGMENT_ES 0x26 +#define PREFIX_SEGMENT_FS 0x64 +#define PREFIX_SEGMENT_GS 0x65 +#define PREFIX_LOCK 0xf0 +#define PREFIX_REPNZ 0xf2 +#define PREFIX_REPX 0xf3 +#define PREFIX_OPERAND_SIZE 0x66 +#define PREFIX_ADDRESS_SIZE 0x67 + +#pragma pack(push, 1) + +typedef struct { + uint8_t len; + uint8_t p_rep; + uint8_t p_lock; + uint8_t p_seg; + uint8_t p_66; + uint8_t p_67; + uint8_t rex; + uint8_t rex_w; + uint8_t rex_r; + uint8_t rex_x; + uint8_t rex_b; + uint8_t opcode; + uint8_t opcode2; + uint8_t modrm; + uint8_t modrm_mod; + uint8_t modrm_reg; + uint8_t modrm_rm; + uint8_t sib; + uint8_t sib_scale; + uint8_t sib_index; + uint8_t sib_base; + union { + uint8_t imm8; + uint16_t imm16; + uint32_t imm32; + uint64_t imm64; + } imm; + union { + uint8_t disp8; + uint16_t disp16; + uint32_t disp32; + } disp; + uint32_t flags; +} hde64s; + +#pragma pack(pop) + +#ifdef __cplusplus +extern "C" { +#endif + +/* __cdecl */ +unsigned int hde64_disasm(const void *code, hde64s *hs); + +#ifdef __cplusplus +} +#endif diff --git a/remove/goldhensdk/include/Patcher.h b/remove/goldhensdk/include/Patcher.h new file mode 100644 index 0000000..2b000b2 --- /dev/null +++ b/remove/goldhensdk/include/Patcher.h @@ -0,0 +1,32 @@ +/* + * GoldHEN Plugin SDK - a prx hook/patch sdk for Orbis OS + * + * Credits + * - OSM + * - jocover + * - bucanero + * - OpenOrbis Team + * - SiSTRo + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct _GHSDK_Patcher +{ + uint64_t Address; + void* OriginalData; + size_t Length; +} Patcher; + +void Patcher_Construct(Patcher *This); +void Patcher_Install_Patch(Patcher *This, uint64_t Address, const void* Data, size_t Length); +void Patcher_Restore_Patch(Patcher *This); +void Patcher_Destroy(Patcher *This); + +#ifdef __cplusplus +} +#endif diff --git a/remove/goldhensdk/include/Syscall.h b/remove/goldhensdk/include/Syscall.h new file mode 100644 index 0000000..86e6713 --- /dev/null +++ b/remove/goldhensdk/include/Syscall.h @@ -0,0 +1,26 @@ +/* + * GoldHEN Plugin SDK - a prx hook/patch sdk for Orbis OS + * + * Credits + * - OSM + * - jocover + * - bucanero + * - OpenOrbis Team + * - SiSTRo + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +int orbis_syscall(int num, ...); + +int sys_dynlib_dlsym(int loadedModuleID, const char *name, void *destination); + +int sys_dynlib_load_prx(const char *name, int *idDestination); + +#ifdef __cplusplus +} +#endif diff --git a/remove/goldhensdk/include/Table64.h b/remove/goldhensdk/include/Table64.h new file mode 100644 index 0000000..5fad5f8 --- /dev/null +++ b/remove/goldhensdk/include/Table64.h @@ -0,0 +1,84 @@ +/* +* Hacker Disassembler Engine 64 C +* Copyright (c) 2008-2009, Vyacheslav Patkov. +* All rights reserved. +* +*/ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#define C_NONE 0x00 +#define C_MODRM 0x01 +#define C_IMM8 0x02 +#define C_IMM16 0x04 +#define C_IMM_P66 0x10 +#define C_REL8 0x20 +#define C_REL32 0x40 +#define C_GROUP 0x80 +#define C_ERROR 0xff + +#define PRE_ANY 0x00 +#define PRE_NONE 0x01 +#define PRE_F2 0x02 +#define PRE_F3 0x04 +#define PRE_66 0x08 +#define PRE_67 0x10 +#define PRE_LOCK 0x20 +#define PRE_SEG 0x40 +#define PRE_ALL 0xff + +#define DELTA_OPCODES 0x4a +#define DELTA_FPU_REG 0xfd +#define DELTA_FPU_MODRM 0x104 +#define DELTA_PREFIXES 0x13c +#define DELTA_OP_LOCK_OK 0x1ae +#define DELTA_OP2_LOCK_OK 0x1c6 +#define DELTA_OP_ONLY_MEM 0x1d8 +#define DELTA_OP2_ONLY_MEM 0x1e7 + +unsigned char hde64_table[] = { + 0xa5,0xaa,0xa5,0xb8,0xa5,0xaa,0xa5,0xaa,0xa5,0xb8,0xa5,0xb8,0xa5,0xb8,0xa5, + 0xb8,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xac,0xc0,0xcc,0xc0,0xa1,0xa1, + 0xa1,0xa1,0xb1,0xa5,0xa5,0xa6,0xc0,0xc0,0xd7,0xda,0xe0,0xc0,0xe4,0xc0,0xea, + 0xea,0xe0,0xe0,0x98,0xc8,0xee,0xf1,0xa5,0xd3,0xa5,0xa5,0xa1,0xea,0x9e,0xc0, + 0xc0,0xc2,0xc0,0xe6,0x03,0x7f,0x11,0x7f,0x01,0x7f,0x01,0x3f,0x01,0x01,0xab, + 0x8b,0x90,0x64,0x5b,0x5b,0x5b,0x5b,0x5b,0x92,0x5b,0x5b,0x76,0x90,0x92,0x92, + 0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x6a,0x73,0x90, + 0x5b,0x52,0x52,0x52,0x52,0x5b,0x5b,0x5b,0x5b,0x77,0x7c,0x77,0x85,0x5b,0x5b, + 0x70,0x5b,0x7a,0xaf,0x76,0x76,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b, + 0x5b,0x5b,0x86,0x01,0x03,0x01,0x04,0x03,0xd5,0x03,0xd5,0x03,0xcc,0x01,0xbc, + 0x03,0xf0,0x03,0x03,0x04,0x00,0x50,0x50,0x50,0x50,0xff,0x20,0x20,0x20,0x20, + 0x01,0x01,0x01,0x01,0xc4,0x02,0x10,0xff,0xff,0xff,0x01,0x00,0x03,0x11,0xff, + 0x03,0xc4,0xc6,0xc8,0x02,0x10,0x00,0xff,0xcc,0x01,0x01,0x01,0x00,0x00,0x00, + 0x00,0x01,0x01,0x03,0x01,0xff,0xff,0xc0,0xc2,0x10,0x11,0x02,0x03,0x01,0x01, + 0x01,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0xff,0xff,0xff,0xff,0x10, + 0x10,0x10,0x10,0x02,0x10,0x00,0x00,0xc6,0xc8,0x02,0x02,0x02,0x02,0x06,0x00, + 0x04,0x00,0x02,0xff,0x00,0xc0,0xc2,0x01,0x01,0x03,0x03,0x03,0xca,0x40,0x00, + 0x0a,0x00,0x04,0x00,0x00,0x00,0x00,0x7f,0x00,0x33,0x01,0x00,0x00,0x00,0x00, + 0x00,0x00,0xff,0xbf,0xff,0xff,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xff,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff, + 0x00,0x00,0x00,0xbf,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0x00, + 0xff,0x40,0x40,0x40,0x40,0x41,0x49,0x40,0x40,0x40,0x40,0x4c,0x42,0x40,0x40, + 0x40,0x40,0x40,0x40,0x40,0x40,0x4f,0x44,0x53,0x40,0x40,0x40,0x44,0x57,0x43, + 0x5c,0x40,0x60,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40, + 0x40,0x40,0x64,0x66,0x6e,0x6b,0x40,0x40,0x6a,0x46,0x40,0x40,0x44,0x46,0x40, + 0x40,0x5b,0x44,0x40,0x40,0x00,0x00,0x00,0x00,0x06,0x06,0x06,0x06,0x01,0x06, + 0x06,0x02,0x06,0x06,0x00,0x06,0x00,0x0a,0x0a,0x00,0x00,0x00,0x02,0x07,0x07, + 0x06,0x02,0x0d,0x06,0x06,0x06,0x0e,0x05,0x05,0x02,0x02,0x00,0x00,0x04,0x04, + 0x04,0x04,0x05,0x06,0x06,0x06,0x00,0x00,0x00,0x0e,0x00,0x00,0x08,0x00,0x10, + 0x00,0x18,0x00,0x20,0x00,0x28,0x00,0x30,0x00,0x80,0x01,0x82,0x01,0x86,0x00, + 0xf6,0xcf,0xfe,0x3f,0xab,0x00,0xb0,0x00,0xb1,0x00,0xb3,0x00,0xba,0xf8,0xbb, + 0x00,0xc0,0x00,0xc1,0x00,0xc7,0xbf,0x62,0xff,0x00,0x8d,0xff,0x00,0xc4,0xff, + 0x00,0xc5,0xff,0x00,0xff,0xff,0xeb,0x01,0xff,0x0e,0x12,0x08,0x00,0x13,0x09, + 0x00,0x16,0x08,0x00,0x17,0x09,0x00,0x2b,0x09,0x00,0xae,0xff,0x07,0xb2,0xff, + 0x00,0xb4,0xff,0x00,0xb5,0xff,0x00,0xc3,0x01,0x00,0xc7,0xff,0xbf,0xe7,0x08, + 0x00,0xf0,0x02,0x00 +}; + +#ifdef __cplusplus +} +#endif diff --git a/remove/goldhensdk/include/Utilities.h b/remove/goldhensdk/include/Utilities.h new file mode 100644 index 0000000..66b1fbb --- /dev/null +++ b/remove/goldhensdk/include/Utilities.h @@ -0,0 +1,79 @@ +/* + * GoldHEN Plugin SDK - a prx hook/patch sdk for Orbis OS + * + * Credits + * - OSM + * - jocover + * - bucanero + * - OpenOrbis Team + * - SiSTRo + */ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#define ARRAY_COUNT(arry) sizeof(arry) / sizeof(arry[0]) + +typedef unsigned char vm_prot_t; /* protection codes */ + +#define VM_PROT_NONE ((vm_prot_t)0x00) +#define VM_PROT_READ ((vm_prot_t)0x01) +#define VM_PROT_WRITE ((vm_prot_t)0x02) +#define VM_PROT_EXECUTE ((vm_prot_t)0x04) +#define VM_PROT_COPY ((vm_prot_t)0x08) /* copy-on-read */ + +#define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE) +#define VM_PROT_RW (VM_PROT_READ | VM_PROT_WRITE) +#define VM_PROT_DEFAULT VM_PROT_ALL + +#define MNT_UPDATE 0x0000000000010000ULL /* not real mount, just update */ + +typedef unsigned long HA; +#define HA_MANYARGS HA arg1, HA arg2, HA arg3, HA arg4, HA arg5, HA arg6, HA arg7, HA arg8, HA arg9, HA arg10, HA arg11, HA arg12 +#define MANYARGS arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12 + +// if you need to define the hook in some header file, this only externs it. +#define HOOK_EXTERN(name) \ + extern Detour Detour_##name + +// this defines the actual implementation +#define HOOK_INIT(name) \ + Detour Detour_##name = { DetourMode_x64 } + +// this does a 64bit hook +#define HOOK(name) \ + if ((uint64_t)name > 0) { \ + Detour_Construct(&(Detour_##name), DetourMode_x64); \ + if (Detour_DetourFunction( \ + &(Detour_##name), (uint64_t)name, (void *)name##_hook) != NULL) {} \ + } + +// 32bit... +#define HOOK32(name) \ + if ((uint64_t)name > 0) { \ + Detour_Construct(&(Detour_##name), DetourMode_x32); \ + if (Detour_DetourFunction( \ + &(Detour_##name), (uint64_t)name, (void *)name##_hook) != NULL) {} \ + } + +// tries to pass arguments +#define HOOK_CONTINUE(name, type, ...) \ + ((uint64_t)name > 0) && ((Detour_##name).FunctionPtr) ? \ + Detour_Stub(&(Detour_##name), type, __VA_ARGS__) : 0 + +// unhooks, killing the defined hook, CANNOT BE REUSED +#define UNHOOK(name) \ + if ( ((uint64_t)name > 0) && ((Detour_##name).FunctionPtr) ) { \ + Detour_Destroy(&(Detour_##name)); \ + } + +void klog(const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); + +void hex_dump(void *data, size_t size); + +#ifdef __cplusplus +} +#endif diff --git a/remove/goldhensdk/libGoldHEN_Hook.a b/remove/goldhensdk/libGoldHEN_Hook.a deleted file mode 100644 index bc1b5cf6eb4e801baed8667edd20e4657e816bed..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 53266 zcmeI*e|%MUeLwJX^8-Q~dP56}6TSu)6dYtp8z`MYb8P}QHo;J!MGFmSav`z#MRG$^ z1)G?p<`B}s1{>R83ma^ppy|pgI<%qCmKL_fiWL==I_8M5i7l&CsrkO&=f2*#hZi?( zb>GMLuYTa>obx>I&*yy3k9*$do_mu!Ex)3vvic)8{9)#`CC4`z8~6Xr%*@TnMaZff z9LLFWoRqo0-QztUa-6q%Wc|G~Esw`veDlXsH#v?zj(y*8UU*F(h4*GRMOJ5*m#=AU zEpLo8H8<2()Ye2FE?<}9lz(c`+`>q-p`|G|_ui(OXyoH9b&a>(T2!{KF7dp%c|0#K zR)0ZDePy(!p}wfDY@NUAoVz2<(T1kT$oy;OV&OJZmQ2+tKo);(z@528xjC^Olr~gF zd`rIb*3sq^<`(%oQFyCPmRHtR)UPS8tf;Lmk4Bp6YU(SZ+FWjK^TW;MjZF=eY+L zFRO@FR!5q0Z!4~Ej_RiMK=$EXu>&j~fWrg<-u|(fvX8Hw_IB8(7TJ8;pCi+6zC*oD zR`qawZOsGaRkh6z*VUBWRT0i6|-u}&0sT+2G9B(7kvtX{99NM_xkGk(n@mdJJuT6ry<@b#38i?N5aV5}_zH&%0 z_A_^2<aZbIPTGoD=81XS2|M?7%p*N^VTSE`jd*JQC$PAFO~GP^p#3~{-vZR zGFa-?W+m8w#XW^tGm3jkvTPHUy6dwh%R>8#-NLL)X*TTsD88-2z0Ik`ZZs>Sv^ScS zCUZ;NSG+}b4FtQ(W$zXDd?eg89PC^o>68AS@_xoOGY0v63Z=L`1 zW4$RK4t?X-5?UBIKl4h+T@c$nzUB3l9`6Z++>|LBLn)!|!YL_5Gv6pouInpu>tnIf zhik*Vz=bJVYpgD6=)`Y^hrR91`zsvVU%}2PaumxB3wGY3Bi`W>c9#u!`)$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_eO;*at)FUI+Pt5RlZl}X zG**iZSNO)g8TirXpYV-aJ) + * - jocover + * - bucanero + * - OpenOrbis Team + * - SiSTRo + */ + +#include "Common.h" +#include "HDE64.h" + +size_t Detour_GetInstructionSize(Detour *This, uint64_t Address, size_t MinSize); +void Detour_WriteJump64(Detour *This, void *Address, uint64_t Destination); +void Detour_WriteJump32(Detour *This, void *Address, uint64_t Destination); +uint64_t Detour_GetJumpAddress64(Detour *This, void *Address); +void *Detour_DetourFunction(Detour *This, uint64_t FunctionPtr, void *HookPtr); +void *Detour_DetourFunction64(Detour *This, uint64_t FunctionPtr, void *HookPtr); +void *Detour_DetourFunction32(Detour *This, uint64_t FunctionPtr, void *HookPtr); +void Detour_RestoreFunction(Detour *This); +void Detour_Construct(Detour *This, DetourMode Mode); +void Detour_Destroy(Detour *This); + +size_t Detour_GetInstructionSize(Detour *This, uint64_t Address, size_t MinSize) { + size_t InstructionSize = 0; + + if (!Address) return 0; + + while (InstructionSize < MinSize) { + hde64s hs; + uint32_t temp = hde64_disasm((void *) (Address + InstructionSize), &hs); + + if (hs.flags & F_ERROR) return 0; + + InstructionSize += temp; + } + + return InstructionSize; +} + +void Detour_WriteJump64(Detour *This, void *Address, uint64_t Destination) { + // Write the address of our hook to the instruction. + *(uint64_t * )(This->JumpInstructions64 + 6) = Destination; + + sceKernelMprotect((void *) Address, sizeof(This->JumpInstructions64), VM_PROT_ALL); + memcpy(Address, This->JumpInstructions64, sizeof(This->JumpInstructions64)); +} + +void Detour_WriteJump32(Detour *This, void *Address, uint64_t Destination) { + uint32_t Offset = (uint32_t)(Destination - ((uint64_t) Address + sizeof(This->JumpInstructions32))); + + // Write the address of our hook to the instruction. + *(uint32_t * )(This->JumpInstructions32 + 1) = Offset; + + sceKernelMprotect((void *) Address, sizeof(This->JumpInstructions32), VM_PROT_ALL); + memcpy(Address, This->JumpInstructions32, sizeof(This->JumpInstructions32)); +} + +uint64_t Detour_GetJumpAddress64(Detour *This, void *Address) { + return *(uint64_t * )((uint64_t)Address + 6); +} + +void *Detour_DetourFunction(Detour *This, uint64_t FunctionPtr, void *HookPtr) { + switch (This->Mode) { + case DetourMode_x32: + return Detour_DetourFunction32(This, FunctionPtr, HookPtr); + case DetourMode_x64: + return Detour_DetourFunction64(This, FunctionPtr, HookPtr); + } + return NULL; +} + +void *Detour_DetourFunction64(Detour *This, uint64_t FunctionPtr, void *HookPtr) { + if (!FunctionPtr || !HookPtr) { +#ifdef DEBUG + klog("[Detour] %s: FunctionPtr or HookPtr NULL (%p -> %p)\n", __FUNCTION__, (void*)FunctionPtr, HookPtr); +#endif + return NULL; + } + + uint32_t InstructionSize = Detour_GetInstructionSize(This, FunctionPtr, sizeof(This->JumpInstructions64)); + +#ifdef DEBUG + klog("[Detour] %s - InstructionSize: %u\n", __FUNCTION__, InstructionSize); +#endif + + if (InstructionSize < sizeof(This->JumpInstructions64)) { +#ifdef DEBUG + klog("[Detour] %s: Hooking Requires a minimum of %d bytes to write jump!\n", __FUNCTION__, (int)sizeof(This->JumpInstructions64)); +#endif + return NULL; + } + + int res = sceKernelMmap(0, sizeof(This->JumpInstructions64), VM_PROT_ALL, 0x1000 | 0x2, -1, 0, &This->TrampolinePtr); + + if (res < 0 || This->TrampolinePtr == 0) { +#ifdef DEBUG + klog("[Detour] %s: sceKernelMmap failed (0x%X).\n", __FUNCTION__, res); +#endif + return 0; + } + + Detour_WriteJump64(This, This->TrampolinePtr, (uint64_t) HookPtr); + + // Save Pointers for later + This->FunctionPtr = (void *) FunctionPtr; + This->HookPtr = HookPtr; + + // Set protection. + sceKernelMprotect((void *) FunctionPtr, InstructionSize, VM_PROT_ALL); + + //Allocate Executable memory for stub and write instructions to stub and a jump back to original execution. + This->StubSize = (InstructionSize + sizeof(This->JumpInstructions64)); + res = sceKernelMmap(0, This->StubSize, VM_PROT_ALL, 0x1000 | 0x2, -1, 0, &This->StubPtr); + + if (res < 0 || This->StubPtr == 0) { +#ifdef DEBUG + klog("[Detour] %s: sceKernelMmap failed (0x%X).\n", __FUNCTION__, res); +#endif + return 0; + } + + memcpy(This->StubPtr, (void *) FunctionPtr, InstructionSize); + Detour_WriteJump64(This, (void *) ((uint64_t) This->StubPtr + InstructionSize), (uint64_t)(FunctionPtr + InstructionSize)); + + // Write jump from function to hook. + memset((void *) FunctionPtr, 0x90, InstructionSize); + Detour_WriteJump64(This, (void *) FunctionPtr, (uint64_t) This->TrampolinePtr); + +#ifdef DEBUG + klog("[Detour] %s: Detour Written Successfully! (FunctionPtr: %p - HookPtr: %p - HookPtrTrampoline: %p - StubPtr: %p - StubSize: %zu\n", __FUNCTION__, This->FunctionPtr, This->HookPtr, This->TrampolinePtr, This->StubPtr, This->StubSize); +#endif + + return This->StubPtr; +} + +void *Detour_DetourFunction32(Detour *This, uint64_t FunctionPtr, void *HookPtr) { + if (!FunctionPtr || !HookPtr) { +#ifdef DEBUG + klog("[Detour] %s: FunctionPtr or HookPtr NULL (%p -> %p)\n", __FUNCTION__, (void*)FunctionPtr, HookPtr); +#endif + return NULL; + } + + size_t InstructionSize = Detour_GetInstructionSize(This, FunctionPtr, sizeof(This->JumpInstructions32)); + +#ifdef DEBUG + klog("[Detour] %s - InstructionSize: %zu\n", __FUNCTION__, InstructionSize); +#endif + + if (InstructionSize < sizeof(This->JumpInstructions32)) { +#ifdef DEBUG + klog("[Detour] %s: Hooking Requires a minimum of %d bytes to write jump!\n", __FUNCTION__, (int)sizeof(This->JumpInstructions64)); +#endif + return NULL; + } + + if ((memcmp((void*) FunctionPtr, This->JumpInstructions32, 1) == 0) && InstructionSize == sizeof(This->JumpInstructions32)) { + uint32_t TrampolineOffest = *(uint32_t*)(FunctionPtr + 1); +#ifdef DEBUG + klog("[Detour] %s: Trampoline found at %p (Offset: %X). Detour called for Trampoline...\n", __FUNCTION__, (void*)(TrampolineOffest + FunctionPtr + 5), TrampolineOffest); +#endif + This->Mode = DetourMode_x64; + return Detour_DetourFunction64(This, (uint64_t)(TrampolineOffest + FunctionPtr + 5), HookPtr); + } + + This->TrampolinePtr = malloc(sizeof(This->JumpInstructions64)); + + if (This->TrampolinePtr == 0) { +#ifdef DEBUG + klog("[Detour] %s: malloc failed.\n", __FUNCTION__); +#endif + return 0; + } + + Detour_WriteJump64(This, This->TrampolinePtr, (uint64_t) HookPtr); + + // Save Pointers for later + This->FunctionPtr = (void *)FunctionPtr; + This->HookPtr = HookPtr; + + // Set protection. + sceKernelMprotect((void *) FunctionPtr, InstructionSize, VM_PROT_ALL); + + //Allocate Executable memory for stub and write instructions to stub and a jump back to original execution. + This->StubSize = (InstructionSize + sizeof(This->JumpInstructions64)); + + int res = sceKernelMmap(0, This->StubSize, VM_PROT_ALL, 0x1000 | 0x2, -1, 0, &This->StubPtr); + + if (res < 0 || This->StubPtr == 0) { +#ifdef DEBUG + klog("[Detour] %s: sceKernelMmap failed (0x%X).\n", __FUNCTION__, res); +#endif + return 0; + } + + memcpy(This->StubPtr, (void *) FunctionPtr, InstructionSize); + Detour_WriteJump64(This, (void *) ((uint64_t)This->StubPtr + InstructionSize), (uint64_t)(FunctionPtr + InstructionSize)); + + // Write jump from function to hook. + memset((void *) FunctionPtr, 0x90, InstructionSize); + Detour_WriteJump32(This, (void *) FunctionPtr, (uint64_t) This->TrampolinePtr); + +#ifdef DEBUG + klog("[Detour] %s: Detour Written Successfully! (FunctionPtr: %p - HookPtr: %p - HookPtrTrampoline: %p - StubPtr: %p - StubSize: %zu\n", __FUNCTION__, This->FunctionPtr, This->HookPtr, This->TrampolinePtr, This->StubPtr, This->StubSize); +#endif + + return This->StubPtr; +} + +void Detour_RestoreFunction(Detour *This) { + void* RestorePtr = 0; + size_t RestoreSize = 0; + uint64_t JmpAddress = 0; + + if (This->StubPtr) { + switch (This->Mode) { + case DetourMode_x32: + RestorePtr = This->FunctionPtr; + RestoreSize = This->StubSize - sizeof(This->JumpInstructions32); + break; + + case DetourMode_x64: + JmpAddress = Detour_GetJumpAddress64(This, This->FunctionPtr); + + RestorePtr = (JmpAddress != (uint64_t)This->TrampolinePtr) ? This->TrampolinePtr : This->FunctionPtr; + RestoreSize = This->StubSize - sizeof(This->JumpInstructions64); + break; + } + + if (RestorePtr != 0 && RestoreSize != 0) { + sceKernelMprotect((void *) RestorePtr, RestoreSize, VM_PROT_ALL); + memcpy((void *) RestorePtr, This->StubPtr, RestoreSize); + +#ifdef DEBUG + klog("[Detour] %s: (%p) has been Restored Successfully!\n", __FUNCTION__, RestorePtr); +#endif + } + } +} + +void Detour_Construct(Detour *This, DetourMode Mode) { + This->StubPtr = 0; + This->StubSize = 0; + This->FunctionPtr = 0; + This->TrampolinePtr = 0; + This->HookPtr = 0; + uint8_t ji32[] = { 0xE9, 0x00, 0x00, 0x00, 0x00 }; + memcpy(This->JumpInstructions32, ji32, sizeof(ji32)); + uint8_t ji64[] = { 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 }; + memcpy(This->JumpInstructions64, ji64, sizeof(ji64)); + This->Mode = Mode; +} + +void Detour_Destroy(Detour *This) { + Detour_RestoreFunction(This); + + // Clean up + sceKernelMunmap(This->StubPtr, This->StubSize); +} diff --git a/remove/goldhensdk/source/GoldHEN.c b/remove/goldhensdk/source/GoldHEN.c new file mode 100644 index 0000000..54f023d --- /dev/null +++ b/remove/goldhensdk/source/GoldHEN.c @@ -0,0 +1,34 @@ +/* + * GoldHEN Plugin SDK - a prx hook/patch sdk for Orbis OS + * + * Credits + * - OSM + * - jocover + * - bucanero + * - OpenOrbis Team + * - SiSTRo + */ + +#include "Common.h" + +/** GoldHEN **/ + +int sys_sdk_cmd(uint64_t cmd, void *data) { + return orbis_syscall(500, cmd, data); +} + +int sys_sdk_jailbreak(struct jailbreak_backup* jb) { + return sys_sdk_cmd(GOLDHEN_SDK_CMD_JAILBREAK, jb); +} + +int sys_sdk_unjailbreak(struct jailbreak_backup* jb) { + if (!jb) { + return -1; + } + + return sys_sdk_cmd(GOLDHEN_SDK_CMD_UNJAILBREAK, jb); +} + +int sys_sdk_proc_info(struct proc_info* info) { + return sys_sdk_cmd(GOLDHEN_SDK_CMD_PROCESS_INFO, info); +} diff --git a/remove/goldhensdk/source/HDE64.c b/remove/goldhensdk/source/HDE64.c new file mode 100644 index 0000000..cb918cb --- /dev/null +++ b/remove/goldhensdk/source/HDE64.c @@ -0,0 +1,323 @@ +/* + * Hacker Disassembler Engine 64 C + * Copyright (c) 2008-2009, Vyacheslav Patkov. + * All rights reserved. + * + */ + +#include +#include +#include "HDE64.h" +#include "Table64.h" + +unsigned int hde64_disasm(const void *code, hde64s *hs) { + uint8_t x, c, *p = (uint8_t *)code, cflags, opcode, pref = 0; + uint8_t *ht = hde64_table, m_mod, m_reg, m_rm, disp_size = 0; + uint8_t op64 = 0; + + memset(hs, 0, sizeof(hde64s)); + + for (x = 16; x; x--) switch (c = *p++) { + case 0xf3: + hs->p_rep = c; + pref |= PRE_F3; + break; + case 0xf2: + hs->p_rep = c; + pref |= PRE_F2; + break; + case 0xf0: + hs->p_lock = c; + pref |= PRE_LOCK; + break; + case 0x26: + case 0x2e: + case 0x36: + case 0x3e: + case 0x64: + case 0x65: + hs->p_seg = c; + pref |= PRE_SEG; + break; + case 0x66: + hs->p_66 = c; + pref |= PRE_66; + break; + case 0x67: + hs->p_67 = c; + pref |= PRE_67; + break; + default: + goto pref_done; + } +pref_done: + + hs->flags = (uint32_t)pref << 23; + + if (!pref) pref |= PRE_NONE; + + if ((c & 0xf0) == 0x40) { + hs->flags |= F_PREFIX_REX; + if ((hs->rex_w = (c & 0xf) >> 3) && (*p & 0xf8) == 0xb8) op64++; + hs->rex_r = (c & 7) >> 2; + hs->rex_x = (c & 3) >> 1; + hs->rex_b = c & 1; + if (((c = *p++) & 0xf0) == 0x40) { + opcode = c; + goto error_opcode; + } + } + + if ((hs->opcode = c) == 0x0f) { + hs->opcode2 = c = *p++; + ht += DELTA_OPCODES; + } else if (c >= 0xa0 && c <= 0xa3) { + op64++; + if (pref & PRE_67) + pref |= PRE_66; + else + pref &= ~PRE_66; + } + + opcode = c; + cflags = ht[ht[opcode / 4] + (opcode % 4)]; + + if (cflags == C_ERROR) { + error_opcode: + hs->flags |= F_ERROR | F_ERROR_OPCODE; + cflags = 0; + if ((opcode & -3) == 0x24) cflags++; + } + + x = 0; + if (cflags & C_GROUP) { + uint16_t t; + t = *(uint16_t *)(ht + (cflags & 0x7f)); + cflags = (uint8_t)t; + x = (uint8_t)(t >> 8); + } + + if (hs->opcode2) { + ht = hde64_table + DELTA_PREFIXES; + if (ht[ht[opcode / 4] + (opcode % 4)] & pref) + hs->flags |= F_ERROR | F_ERROR_OPCODE; + } + + if (cflags & C_MODRM) { + hs->flags |= F_MODRM; + hs->modrm = c = *p++; + hs->modrm_mod = m_mod = c >> 6; + hs->modrm_rm = m_rm = c & 7; + hs->modrm_reg = m_reg = (c & 0x3f) >> 3; + + if (x && ((x << m_reg) & 0x80)) hs->flags |= F_ERROR | F_ERROR_OPCODE; + + if (!hs->opcode2 && opcode >= 0xd9 && opcode <= 0xdf) { + uint8_t t = opcode - 0xd9; + if (m_mod == 3) { + ht = hde64_table + DELTA_FPU_MODRM + t * 8; + t = ht[m_reg] << m_rm; + } else { + ht = hde64_table + DELTA_FPU_REG; + t = ht[t] << m_reg; + } + if (t & 0x80) hs->flags |= F_ERROR | F_ERROR_OPCODE; + } + + if (pref & PRE_LOCK) { + if (m_mod == 3) { + hs->flags |= F_ERROR | F_ERROR_LOCK; + } else { + uint8_t *table_end, op = opcode; + if (hs->opcode2) { + ht = hde64_table + DELTA_OP2_LOCK_OK; + table_end = ht + DELTA_OP_ONLY_MEM - DELTA_OP2_LOCK_OK; + } else { + ht = hde64_table + DELTA_OP_LOCK_OK; + table_end = ht + DELTA_OP2_LOCK_OK - DELTA_OP_LOCK_OK; + op &= -2; + } + for (; ht != table_end; ht++) + if (*ht++ == op) { + if (!((*ht << m_reg) & 0x80)) + goto no_lock_error; + else + break; + } + hs->flags |= F_ERROR | F_ERROR_LOCK; + no_lock_error:; + } + } + + if (hs->opcode2) { + switch (opcode) { + case 0x20: + case 0x22: + m_mod = 3; + if (m_reg > 4 || m_reg == 1) + goto error_operand; + else + goto no_error_operand; + case 0x21: + case 0x23: + m_mod = 3; + if (m_reg == 4 || m_reg == 5) + goto error_operand; + else + goto no_error_operand; + } + } else { + switch (opcode) { + case 0x8c: + if (m_reg > 5) + goto error_operand; + else + goto no_error_operand; + case 0x8e: + if (m_reg == 1 || m_reg > 5) + goto error_operand; + else + goto no_error_operand; + } + } + + if (m_mod == 3) { + uint8_t *table_end; + if (hs->opcode2) { + ht = hde64_table + DELTA_OP2_ONLY_MEM; + table_end = ht + sizeof(hde64_table) - DELTA_OP2_ONLY_MEM; + } else { + ht = hde64_table + DELTA_OP_ONLY_MEM; + table_end = ht + DELTA_OP2_ONLY_MEM - DELTA_OP_ONLY_MEM; + } + for (; ht != table_end; ht += 2) + if (*ht++ == opcode) { + if (*ht++ & pref && !((*ht << m_reg) & 0x80)) + goto error_operand; + else + break; + } + goto no_error_operand; + } else if (hs->opcode2) { + switch (opcode) { + case 0x50: + case 0xd7: + case 0xf7: + if (pref & (PRE_NONE | PRE_66)) goto error_operand; + break; + case 0xd6: + if (pref & (PRE_F2 | PRE_F3)) goto error_operand; + break; + case 0xc5: + goto error_operand; + } + goto no_error_operand; + } else + goto no_error_operand; + + error_operand: + hs->flags |= F_ERROR | F_ERROR_OPERAND; + no_error_operand: + + c = *p++; + if (m_reg <= 1) { + if (opcode == 0xf6) + cflags |= C_IMM8; + else if (opcode == 0xf7) + cflags |= C_IMM_P66; + } + + switch (m_mod) { + case 0: + if (pref & PRE_67) { + if (m_rm == 6) disp_size = 2; + } else if (m_rm == 5) + disp_size = 4; + break; + case 1: + disp_size = 1; + break; + case 2: + disp_size = 2; + if (!(pref & PRE_67)) disp_size <<= 1; + } + + if (m_mod != 3 && m_rm == 4) { + hs->flags |= F_SIB; + p++; + hs->sib = c; + hs->sib_scale = c >> 6; + hs->sib_index = (c & 0x3f) >> 3; + if ((hs->sib_base = c & 7) == 5 && !(m_mod & 1)) disp_size = 4; + } + + p--; + switch (disp_size) { + case 1: + hs->flags |= F_DISP8; + hs->disp.disp8 = *p; + break; + case 2: + hs->flags |= F_DISP16; + hs->disp.disp16 = *(uint16_t *)p; + break; + case 4: + hs->flags |= F_DISP32; + hs->disp.disp32 = *(uint32_t *)p; + } + p += disp_size; + } else if (pref & PRE_LOCK) + hs->flags |= F_ERROR | F_ERROR_LOCK; + + if (cflags & C_IMM_P66) { + if (cflags & C_REL32) { + if (pref & PRE_66) { + hs->flags |= F_IMM16 | F_RELATIVE; + hs->imm.imm16 = *(uint16_t *)p; + p += 2; + goto disasm_done; + } + goto rel32_ok; + } + if (op64) { + hs->flags |= F_IMM64; + hs->imm.imm64 = *(uint64_t *)p; + p += 8; + } else if (!(pref & PRE_66)) { + hs->flags |= F_IMM32; + hs->imm.imm32 = *(uint32_t *)p; + p += 4; + } else + goto imm16_ok; + } + + if (cflags & C_IMM16) { + imm16_ok: + hs->flags |= F_IMM16; + hs->imm.imm16 = *(uint16_t *)p; + p += 2; + } + if (cflags & C_IMM8) { + hs->flags |= F_IMM8; + hs->imm.imm8 = *p++; + } + + if (cflags & C_REL32) { + rel32_ok: + hs->flags |= F_IMM32 | F_RELATIVE; + hs->imm.imm32 = *(uint32_t *)p; + p += 4; + } else if (cflags & C_REL8) { + hs->flags |= F_IMM8 | F_RELATIVE; + hs->imm.imm8 = *p++; + } + +disasm_done: + + if ((hs->len = (uint8_t)(p - (uint8_t *)code)) > 15) { + hs->flags |= F_ERROR | F_ERROR_LENGTH; + hs->len = 15; + } + + return (unsigned int)hs->len; +} diff --git a/remove/goldhensdk/source/Patcher.c b/remove/goldhensdk/source/Patcher.c new file mode 100644 index 0000000..c920e3d --- /dev/null +++ b/remove/goldhensdk/source/Patcher.c @@ -0,0 +1,77 @@ +/* + * GoldHEN Plugin SDK - a prx hook/patch sdk for Orbis OS + * + * Credits + * - OSM + * - jocover + * - bucanero + * - OpenOrbis Team + * - SiSTRo + */ + +#include "Common.h" +#include "Patcher.h" + +void Patcher_Construct(Patcher *This) +{ + This->Address = 0; + This->OriginalData = NULL; + This->Length = 0; +} + +void Patcher_Install_Patch(Patcher *This, uint64_t Address, const void* Data, size_t Length) +{ + //Backup Params. + This->Address = Address; + This->Length = Length; + + //Set protection to all + sceKernelMprotect((void*)Address, Length, VM_PROT_ALL); + + //Backup data. + int res = sceKernelMmap(0, Length, VM_PROT_ALL, 0x1000 | 0x2, -1, 0, &This->OriginalData); + if (res < 0) + { +#ifdef DEBUG + klog("[Patcher] sceKernelMmap Failed: 0x%X\n", res); +#endif + return; + } + memcpy(This->OriginalData, Data, Length); + + //Write Patch. + memcpy((void*)Address, Data, Length); + +#ifdef DEBUG + klog("[Patcher] Install_Patch: Patch (%p) Written Successfully!\n", (void*)Address); +#endif +} + +void Patcher_Restore_Patch(Patcher *This) +{ + if (This->OriginalData) + { + //Set protection to all + sceKernelMprotect((void*)This->Address, This->Length, VM_PROT_ALL); + + //Write original Data back. + memcpy((void*)This->Address, This->OriginalData, This->Length); + +#ifdef DEBUG + klog("[Patcher] Restore_Patch: Patch (%p) Restored Successfully!\n", (void*)This->Address); +#endif + } + else { +#ifdef DEBUG + klog("[Patcher] Patch not installed.\n"); +#endif + } +} + +void Patcher_Destroy(Patcher *This) +{ + Patcher_Restore_Patch(This); + + //Clean up + sceKernelMunmap(This->OriginalData, This->Length); +} diff --git a/remove/goldhensdk/source/Syscall.c b/remove/goldhensdk/source/Syscall.c new file mode 100644 index 0000000..be36fb4 --- /dev/null +++ b/remove/goldhensdk/source/Syscall.c @@ -0,0 +1,39 @@ +/* + * GoldHEN Plugin SDK - a prx hook/patch sdk for Orbis OS + * + * Credits + * - OSM + * - jocover + * - bucanero + * - OpenOrbis Team + * - SiSTRo + */ + +#include "Common.h" + +__asm__( + ".att_syntax prefix\n" + ".globl orbis_syscall\n" + "orbis_syscall:\n" + " movq $0, %rax\n" + " movq %rcx, %r10\n" + " syscall\n" + " jb err\n" + " retq\n" + "err:\n" + " pushq %rax\n" + " callq __error\n" + " popq %rcx\n" + " movl %ecx, 0(%rax)\n" + " movq $0xFFFFFFFFFFFFFFFF, %rax\n" + " movq $0xFFFFFFFFFFFFFFFF, %rdx\n" + " retq\n" +); + +int sys_dynlib_dlsym(int loadedModuleID, const char *name, void *destination) { + return orbis_syscall(591, loadedModuleID, name, destination); +} + +int sys_dynlib_load_prx(const char *name, int *idDestination) { + return orbis_syscall(594, name, 0, idDestination, 0); +} diff --git a/remove/goldhensdk/source/Utilities.c b/remove/goldhensdk/source/Utilities.c new file mode 100644 index 0000000..cdd2639 --- /dev/null +++ b/remove/goldhensdk/source/Utilities.c @@ -0,0 +1,37 @@ +/* + * GoldHEN Plugin SDK - a prx hook/patch sdk for Orbis OS + * + * Credits + * - OSM + * - jocover + * - bucanero + * - OpenOrbis Team + * - SiSTRo + */ + +#include "Common.h" + +void klog(const char* fmt, ...) { + va_list args; + char Buffer[0x100] = { '\0' }; + + va_start(args, fmt); + vsnprintf(Buffer, sizeof(Buffer), fmt, args); + va_end(args); + + sys_sdk_cmd(GOLDHEN_SDK_CMD_KLOG, Buffer); +} + +void hex_dump(void *data, size_t size) { + unsigned char *p = (unsigned char *)data; + int i; + + for(i = 0; i < size; i++) { + klog("%02X ", *p++); + if(!(i % 16) && i != 0) { + klog("\n"); + } + } + + klog("\n"); +} diff --git a/remove/remove_goldhen_glue.cpp b/remove/remove_goldhen_glue.cpp deleted file mode 100644 index 597e8bc..0000000 --- a/remove/remove_goldhen_glue.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include "remove_goldhen_glue.h" - -#ifdef BUILD_WITH_GOLDHEN_SUPPORT -#include -#include "goldhensdk/Detour.h" -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/* -- public api begin -- */ - -#ifdef BUILD_WITH_GOLDHEN_SUPPORT - -PRG_Detour RG_Detour_new() { - return reinterpret_cast(new(std::nothrow) Detour(DetourMode::x64)); -} - -void *RG_Detour_DetourFunction(PRG_Detour self, void *original, void *calltarget) { - if (self) { - return reinterpret_cast(self)->DetourFunction( - reinterpret_cast(original), /* wtf SiSTRo???? */ - calltarget - ); - } - - return 0; -} - -PRG_Detour RG_Detour_delete(PRG_Detour self) { - if (self) { - delete reinterpret_cast(self); - } - - /* - this is so the `hook = RG_Detour_delete(hook);` pattern can work, - both freeing and setting the pointer to null. - */ - return nullptr; -} - -#else /* no goldhen */ - -PRG_Detour RG_Detour_new() { - return nullptr; -} - -void *RG_Detour_DetourFunction(PRG_Detour self, void *original, void *calltarget) { - return nullptr; -} - -PRG_Detour RG_Detour_delete(PRG_Detour self) { - return nullptr; -} - -#endif /* end */ - -#ifdef __cplusplus -} -#endif diff --git a/remove/remove_goldhen_glue.h b/remove/remove_goldhen_glue.h deleted file mode 100644 index c6b64b0..0000000 --- a/remove/remove_goldhen_glue.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef _REMOVE_GOLDHEN_GLUE_H_ -#define _REMOVE_GOLDHEN_GLUE_H_ 1 -#pragma once -#ifdef __cplusplus -extern "C" { -#endif - -// REMove <3 GoldHEN extern-C glue. -// since I refuse to use C++ APIs in public. - -// opaque struct handle that can be passed to C and C++ equally. -typedef struct RG_Detour RG_Detour, *PRG_Detour; - -PRG_Detour RG_Detour_new(); - -void *RG_Detour_DetourFunction(PRG_Detour self, void *original, void *calltarget); - -PRG_Detour RG_Detour_delete(PRG_Detour self); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/remove/remove_module.c b/remove/remove_module.c index a5524f2..a0c0bbd 100644 --- a/remove/remove_module.c +++ b/remove/remove_module.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -9,10 +10,15 @@ #include "substitute.h" #include "remove_module.h" #include "remove_ctx.h" -#include "remove_goldhen_glue.h" +#include "goldhensdk/include/Detour.h" // -- REMove Main Module -- // +void logDlsym(int prxId, const char *name, void **outResult) { + int r = sceKernelDlsym(prxId, name, outResult); + kprintf("[remove]: sceKernelDlsym(%d, \"%s\", %p); => %d - %p\n", prxId, name, (void*)outResult, r, *outResult); +} + /* extern hook */ #define ehook(_OrigFunc) extern substitute_hook_info _OrigFunc##HookInfo /* define hook */ @@ -28,21 +34,17 @@ ) : /* already hooked, return success */ (0) ) // goldhen stuff #ifdef BUILD_WITH_GOLDHEN_SUPPORT -#define gehook(_OrigFunc) extern void *_OrigFunc##GoldHEN_Addr; extern PRG_Detour _OrigFunc##GoldHEN_HookInfo -#define gdhook(_OrigFunc) void *_OrigFunc##GoldHEN_Addr = 0; PRG_Detour _OrigFunc##GoldHEN_HookInfo = 0 +#define gehook(_OrigFunc) extern Detour _OrigFunc##GoldHEN_Hook; extern void *_OrigFunc##GoldHEN_Addr +#define gdhook(_OrigFunc) Detour _OrigFunc##GoldHEN_Hook = { DetourMode_x64 }; void *_OrigFunc##GoldHEN_Addr = 0 #define gihook(_OrigFunc) ( \ - (!(_OrigFunc##GoldHEN_HookInfo)) ? \ + (!(_OrigFunc##GoldHEN_Addr)) ? \ ( \ - (_OrigFunc##GoldHEN_HookInfo) = RG_Detour_new(), \ - sceKernelDlsym(psmove_prx_id, #_OrigFunc, &(_OrigFunc##GoldHEN_Addr)), \ - RG_Detour_DetourFunction( \ - (_OrigFunc##GoldHEN_HookInfo), \ - (void *)(_OrigFunc##GoldHEN_Addr), \ - (void *)(_OrigFunc##Impl) \ - ) \ + Detour_Construct((&(_OrigFunc##GoldHEN_Hook)), DetourMode_x64), \ + logDlsym(psmove_prx_id, #_OrigFunc, (&(_OrigFunc##GoldHEN_Addr))), \ + Detour_DetourFunction((&(_OrigFunc##GoldHEN_Hook)), (uint64_t)(_OrigFunc##GoldHEN_Addr), (void *)&(_OrigFunc##Impl)), \ + 0 \ ) \ - : \ - /* already hooked, return 'already' value */ (void*)(0x1) \ + : /* already hooked, return success */ (0) \ ) gdhook(sceMoveInit); @@ -214,12 +216,21 @@ sceError sceMoveSetExtensionPortOutputImpl( /* -- DEPRECATED -- */ sceHandle hDe return Context->v->SetExtensionPortOutput(Context, hDeviceHandle, baData); } + void REMove_module_start() { + char path[260 + 1] = { '\0' }; have_mira = substitute_is_present(); - psmove_prx_id = sceKernelLoadStartModule("libSceMove.sprx", 0, 0, 0, 0, 0); + strcat(path, "/"); + strcat(path, sceKernelGetFsSandboxRandomWord()); + strcat(path, "/common/lib/libSceMove.sprx"); + psmove_prx_id = sceKernelLoadStartModule(path, 0, 0, 0, 0, 0); + kprintf("psmove prx id = %d\n", psmove_prx_id); if (psmove_prx_id >= 0) { REMove_InitHooks(); } + else { + nprintf("REMove: libSceMove load fail 0x%X", psmove_prx_id); + } } void REMove_module_stop() { @@ -237,17 +248,8 @@ void REMove_module_stop() { static int is_initialized = 0; static int is_finalized = 0; #define prxex __attribute__((visibility("default"))) +prxex void *__dso_handle = &__dso_handle; -#ifndef BUILD_WITH_GOLDHEN_SUPPORT - -extern void(*__preinit_array_start[])(void); -extern void(*__preinit_array_end[])(void); -extern void(*__init_array_start[])(void); -extern void(*__init_array_end[])(void); -extern void(*__fini_array_start[])(void); -extern void(*__fini_array_end[])(void); - -// sce_module_param __asm__( ".intel_syntax noprefix \n" ".align 0x8 \n" @@ -258,96 +260,46 @@ __asm__( // magic " .quad 0x13C13F4BF \n" // SDK version -" .quad 0x4508101 \n" -".att_syntax prefix \n" -); - -// data globals -__asm__( -".intel_syntax noprefix \n" -".align 0x8 \n" +" .quad 0x4508001 \n" ".data \n" "_sceLibc: \n" " .quad 0 \n" ".att_syntax prefix \n" ); -prxex void _init() { +/* force visibility to default so it always gets exported */ +prxex int module_start(size_t argc, const void *args) { if (!is_initialized) { is_initialized = 1; - - /* usually preinit start==end so it's not used, but who knows? */ - for(void(**__i)(void) = __preinit_array_start; __i != __preinit_array_end; __i++) { - __i[0](); - } - - for(void(**__i)(void) = __init_array_start; __i != __init_array_end; __i++) { - __i[0](); - } - - /* call your entry point here: */ REMove_module_start(); } + + return 0; } -prxex void _fini() { +prxex int module_stop(size_t argc, const void *args) { if (!is_finalized) { is_finalized = 1; - - for(void(**__i)(void) = __fini_array_start; __i != __fini_array_end; __i++) { - __i[0](); - } - - /* call your quit point (LOL) here: */ REMove_module_stop(); } -} -prxex int module_start(unsigned long long argl, const void *argp) { - _init(); return 0; } -prxex int module_stop(unsigned long long argl, const void *argp) { - _fini(); - return 0; +prxex int _init() { + return module_start(0, 0); } -#else /* BUILD_WITH_GOLDHEN_SUPPORT: */ - -/* special GoldHEN plugin CRT */ -prxex void _init(); /* provided by crtprx.o */ -prxex void _fini(); /* provided by crtprx.o */ -/* for some bizzare reason if I don't do this, clang won't link GoldHEN's `crtprx.o` */ -prxex void *_init_addr_dummy = (void *)&_init; -prxex void *_fini_addr_dummy = (void *)&_fini; - -prxex int module_start(unsigned long long argl, const void *argp) { - if (!is_initialized) { - is_initialized = 1; - REMove_module_start(); - } - - return 0; +prxex int _fini() { + return module_stop(0, 0); } -prxex int module_stop(unsigned long long argl, const void *argp) { - if (!is_finalized) { - is_finalized = 1; - REMove_module_stop(); - } - - return 0; -} - -#endif - #pragma endregion /* end of CRT */ int kprintf(const char *fmt, ...) { va_list va_l; int wrote = 0; - char buff[4096] = { '\0' }; + char buff[0x100] = { '\0' }; va_start(va_l, fmt); wrote = vsnprintf(buff, sizeof(buff), fmt, va_l); @@ -357,17 +309,10 @@ int kprintf(const char *fmt, ...) { return wrote; } -#ifndef my_strncpy -#define my_strncpy(_Dest, _Src, _Len) \ - for (unsigned long long _Index = 0; _Index < _Len; ++_Index) \ - if ('\0' == _Src[_Index]) { _Dest[_Index] = '\0'; break; } \ - else _Dest[_Index] = _Src[_Index]; -#endif - int nprintf(const char *fmt, ...) { va_list va_l; int wrote = 0; - char buff[512] = { '\0' }; + char buff[0x100] = { '\0' }; OrbisNotificationRequest req = { 0 }; char icon[] = "cxml://psnotification/tex_device_move"; @@ -378,11 +323,10 @@ int nprintf(const char *fmt, ...) { req.type = NotificationRequest; req.useIconImageUri = 1; req.targetId = -1; - my_strncpy(req.message, buff, sizeof(req.message)); - my_strncpy(req.iconUri, icon, sizeof(req.iconUri)); + snprintf(req.message, sizeof(req.message), "%s", buff); + snprintf(req.iconUri, sizeof(req.iconUri), "%s", icon); sceKernelSendNotificationRequest(0, &req, sizeof(req), 0); return wrote; } -#undef my_strncpy