Use the function patcher module for the plugin function patches

This commit is contained in:
Maschell 2020-06-13 16:57:53 +02:00
parent 4aeeb09901
commit ce73ac48e0
9 changed files with 25 additions and 485 deletions

View File

@ -41,9 +41,9 @@ CFLAGS += $(INCLUDE) -D__WIIU__ -D__WUT__
CXXFLAGS := $(CFLAGS) -std=c++17 CXXFLAGS := $(CFLAGS) -std=c++17
ASFLAGS := -g $(ARCH) ASFLAGS := -g $(ARCH)
LDFLAGS = -g $(ARCH) $(RPXSPECS) -Wl,-Map,$(notdir $*.map) -T$(WUMS_ROOT)/share/libkernel.ld -T$(WUMS_ROOT)/share/libfunctionpatcher.ld $(WUMSSPECS) LDFLAGS = -g $(ARCH) $(RPXSPECS) -Wl,-Map,$(notdir $*.map) -T$(WUMS_ROOT)/share/libfunctionpatcher.ld $(WUMSSPECS)
LIBS := -lwums -lwut -lwups -lkernel -lfunctionpatcher LIBS := -lwums -lwut -lwups -lfunctionpatcher
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level # list of directories containing libraries, this must be the top level

View File

@ -1,9 +1,9 @@
#include <plugin/PluginContainer.h> #include <plugin/PluginContainer.h>
#include <utils/ElfUtils.h> #include <utils/ElfUtils.h>
#include <coreinit/cache.h> #include <coreinit/cache.h>
#include <patcher/function_patcher.h>
#include <plugin/PluginMetaInformationFactory.h> #include <plugin/PluginMetaInformationFactory.h>
#include <plugin/PluginInformationFactory.h> #include <plugin/PluginInformationFactory.h>
#include <coreinit/dynload.h>
#include "patcher/hooks_patcher_static.h" #include "patcher/hooks_patcher_static.h"
#include "PluginManagement.h" #include "PluginManagement.h"
#include "hooks.h" #include "hooks.h"
@ -100,7 +100,7 @@ void PluginManagement::callDeinitHooks(plugin_information_t *pluginInformation)
void PluginManagement::RestorePatches(plugin_information_t *pluginInformation, BOOL pluginOnly) { void PluginManagement::RestorePatches(plugin_information_t *pluginInformation, BOOL pluginOnly) {
for (int32_t plugin_index = pluginInformation->number_used_plugins - 1; plugin_index >= 0; plugin_index--) { for (int32_t plugin_index = pluginInformation->number_used_plugins - 1; plugin_index >= 0; plugin_index--) {
new_RestoreInvidualInstructions(&(pluginInformation->plugin_data[plugin_index].info)); FunctionPatcherRestoreFunctions(pluginInformation->plugin_data[plugin_index].info.functions, pluginInformation->plugin_data[plugin_index].info.number_used_functions);
} }
if (!pluginOnly) { if (!pluginOnly) {
FunctionPatcherRestoreFunctions(method_hooks_hooks_static, method_hooks_size_hooks_static); FunctionPatcherRestoreFunctions(method_hooks_hooks_static, method_hooks_size_hooks_static);
@ -149,7 +149,6 @@ void PluginManagement::unloadPlugins(plugin_information_t *gPluginInformation, M
memset((void *) gPluginInformation, 0, sizeof(plugin_information_t)); memset((void *) gPluginInformation, 0, sizeof(plugin_information_t));
} }
void PluginManagement::callInitHooks(plugin_information_t *pluginInformation) { void PluginManagement::callInitHooks(plugin_information_t *pluginInformation) {
CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_VID_MEM); CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_VID_MEM);
CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_KERNEL); CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_KERNEL);
@ -171,7 +170,7 @@ void PluginManagement::PatchFunctionsAndCallHooks(plugin_information_t *gPluginI
CallHook(gPluginInformation, WUPS_LOADER_HOOK_INIT_WUT_DEVOPTAB); CallHook(gPluginInformation, WUPS_LOADER_HOOK_INIT_WUT_DEVOPTAB);
for (int32_t plugin_index = 0; plugin_index < gPluginInformation->number_used_plugins; plugin_index++) { for (int32_t plugin_index = 0; plugin_index < gPluginInformation->number_used_plugins; plugin_index++) {
CallHookEx(gPluginInformation, WUPS_LOADER_HOOK_APPLICATION_START, plugin_index); CallHookEx(gPluginInformation, WUPS_LOADER_HOOK_APPLICATION_START, plugin_index);
new_PatchInvidualMethodHooks(&(gPluginInformation->plugin_data[plugin_index].info)); FunctionPatcherPatchFunction(gPluginInformation->plugin_data[plugin_index].info.functions, gPluginInformation->plugin_data[plugin_index].info.number_used_functions);
CallHookEx(gPluginInformation, WUPS_LOADER_HOOK_FUNCTIONS_PATCHED, plugin_index); CallHookEx(gPluginInformation, WUPS_LOADER_HOOK_FUNCTIONS_PATCHED, plugin_index);
} }
} }

View File

@ -20,10 +20,12 @@
#include <stdint.h> #include <stdint.h>
#include <stddef.h> #include <stddef.h>
#include <wums/defines/dynamic_linking_defines.h> #include <wums/defines/dynamic_linking_defines.h>
#include "replacement_defines.h"
#include <wums/defines/export_defines.h> #include <wums/defines/export_defines.h>
#include <wums/defines/relocation_defines.h> #include <wums/defines/relocation_defines.h>
#include <function_patcher/function_patching.h>
#include <wups.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -57,11 +59,16 @@ struct plugin_meta_info_t {
uint32_t size; uint32_t size;
}; };
struct replacement_data_hook_t {
void * func_pointer = NULL; /* [will be filled] */
wups_loader_hook_type_t type; /* [will be filled] */
};
struct plugin_info_t { struct plugin_info_t {
dyn_linking_relocation_entry_t linking_entries[PLUGIN_DYN_LINK_RELOCATION_LIST_LENGTH]; dyn_linking_relocation_entry_t linking_entries[PLUGIN_DYN_LINK_RELOCATION_LIST_LENGTH];
plugin_section_info_t sectionInfos[MAXIMUM_PLUGIN_SECTION_LENGTH]; plugin_section_info_t sectionInfos[MAXIMUM_PLUGIN_SECTION_LENGTH];
uint32_t number_used_functions; // Number of used function. Maximum is MAXIMUM_FUNCTION_PER_PLUGIN uint32_t number_used_functions; // Number of used function. Maximum is MAXIMUM_FUNCTION_PER_PLUGIN
replacement_data_function_t functions[MAXIMUM_FUNCTION_PER_PLUGIN]; // Replacement information for each function. function_replacement_data_t functions[MAXIMUM_FUNCTION_PER_PLUGIN]; // Replacement information for each function.
uint32_t number_used_hooks; // Number of used hooks. Maximum is MAXIMUM_HOOKS_PER_PLUGIN uint32_t number_used_hooks; // Number of used hooks. Maximum is MAXIMUM_HOOKS_PER_PLUGIN
replacement_data_hook_t hooks[MAXIMUM_HOOKS_PER_PLUGIN]; // Replacement information for each function. replacement_data_hook_t hooks[MAXIMUM_HOOKS_PER_PLUGIN]; // Replacement information for each function.
uint8_t trampolinId; uint8_t trampolinId;

View File

@ -1,26 +0,0 @@
#pragma once
#include <stdint.h>
#include <wups.h>
#define FUNCTION_PATCHER_METHOD_STORE_SIZE 20
#define MAXIMUM_FUNCTION_NAME_LENGTH 83
struct replacement_data_function_t {
uint32_t physicalAddr; /* [needs to be filled] */
uint32_t virtualAddr; /* [needs to be filled] */
uint32_t replaceAddr; /* [needs to be filled] Address of our replacement function */
uint32_t replaceCall; /* [needs to be filled] Address to access the real_function */
wups_loader_library_type_t library; /* [needs to be filled] rpl where the function we want to replace is. */
char function_name[MAXIMUM_FUNCTION_NAME_LENGTH]; /* [needs to be filled] name of the function we want to replace */
uint32_t realAddr; /* [will be filled] Address of the real function we want to replace. */
volatile uint32_t replace_data [FUNCTION_PATCHER_METHOD_STORE_SIZE]; /* [will be filled] Space for us to store some jump instructions */
uint32_t restoreInstruction; /* [will be filled] Copy of the instruction we replaced to jump to our code. */
uint8_t functionType; /* [will be filled] */
uint8_t alreadyPatched; /* [will be filled] */
};
struct replacement_data_hook_t {
void * func_pointer = NULL; /* [will be filled] */
wups_loader_hook_type_t type; /* [will be filled] */
};

View File

@ -1,391 +0,0 @@
/****************************************************************************
* Copyright (C) 2016-2018 Maschell
* With code from chadderz and dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <wups.h>
#include <coreinit/debug.h>
#include "function_patcher.h"
#include <utils/logger.h>
#include <utils/utils.h>
#include <coreinit/memorymap.h>
#include <coreinit/cache.h>
#include <kernel/kernel.h>
#include <whb/log.h>
#include <coreinit/dynload.h>
#define DEBUG_LOG_DYN 0
rpl_handling rpl_handles[] __attribute__((section(".data"))) = {
{WUPS_LOADER_LIBRARY_AVM, "avm.rpl", 0},
{WUPS_LOADER_LIBRARY_CAMERA, "camera.rpl", 0},
{WUPS_LOADER_LIBRARY_COREINIT, "coreinit.rpl", 0},
{WUPS_LOADER_LIBRARY_DC, "dc.rpl", 0},
{WUPS_LOADER_LIBRARY_DMAE, "dmae.rpl", 0},
{WUPS_LOADER_LIBRARY_DRMAPP, "drmapp.rpl", 0},
{WUPS_LOADER_LIBRARY_ERREULA, "erreula.rpl", 0},
{WUPS_LOADER_LIBRARY_GX2, "gx2.rpl", 0},
{WUPS_LOADER_LIBRARY_H264, "h264.rpl", 0},
{WUPS_LOADER_LIBRARY_LZMA920, "lzma920.rpl", 0},
{WUPS_LOADER_LIBRARY_MIC, "mic.rpl", 0},
{WUPS_LOADER_LIBRARY_NFC, "nfc.rpl", 0},
{WUPS_LOADER_LIBRARY_NIO_PROF, "nio_prof.rpl", 0},
{WUPS_LOADER_LIBRARY_NLIBCURL, "nlibcurl.rpl", 0},
{WUPS_LOADER_LIBRARY_NLIBNSS, "nlibnss.rpl", 0},
{WUPS_LOADER_LIBRARY_NLIBNSS2, "nlibnss2.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_AC, "nn_ac.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_ACP, "nn_acp.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_ACT, "nn_act.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_AOC, "nn_aoc.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_BOSS, "nn_boss.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_CCR, "nn_ccr.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_CMPT, "nn_cmpt.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_DLP, "nn_dlp.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_EC, "nn_ec.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_FP, "nn_fp.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_HAI, "nn_hai.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_HPAD, "nn_hpad.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_IDBE, "nn_idbe.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_NDM, "nn_ndm.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_NETS2, "nn_nets2.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_NFP, "nn_nfp.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_NIM, "nn_nim.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_OLV, "nn_olv.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_PDM, "nn_pdm.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_SAVE, "nn_save.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_SL, "nn_sl.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_SPM, "nn_spm.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_TEMP, "nn_temp.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_UDS, "nn_uds.rpl", 0},
{WUPS_LOADER_LIBRARY_NN_VCTL, "nn_vctl.rpl", 0},
{WUPS_LOADER_LIBRARY_NSYSCCR, "nsysccr.rpl", 0},
{WUPS_LOADER_LIBRARY_NSYSHID, "nsyshid.rpl", 0},
{WUPS_LOADER_LIBRARY_NSYSKBD, "nsyskbd.rpl", 0},
{WUPS_LOADER_LIBRARY_NSYSNET, "nsysnet.rpl", 0},
{WUPS_LOADER_LIBRARY_NSYSUHS, "nsysuhs.rpl", 0},
{WUPS_LOADER_LIBRARY_NSYSUVD, "nsysuvd.rpl", 0},
{WUPS_LOADER_LIBRARY_NTAG, "ntag.rpl", 0},
{WUPS_LOADER_LIBRARY_PADSCORE, "padscore.rpl", 0},
{WUPS_LOADER_LIBRARY_PROC_UI, "proc_ui.rpl", 0},
{WUPS_LOADER_LIBRARY_SNDCORE2, "sndcore2.rpl", 0},
{WUPS_LOADER_LIBRARY_SNDUSER2, "snduser2.rpl", 0},
{WUPS_LOADER_LIBRARY_SND_CORE, "snd_core.rpl", 0},
{WUPS_LOADER_LIBRARY_SND_USER, "snd_user.rpl", 0},
{WUPS_LOADER_LIBRARY_SWKBD, "swkbd.rpl", 0},
{WUPS_LOADER_LIBRARY_SYSAPP, "sysapp.rpl", 0},
{WUPS_LOADER_LIBRARY_TCL, "tcl.rpl", 0},
{WUPS_LOADER_LIBRARY_TVE, "tve.rpl", 0},
{WUPS_LOADER_LIBRARY_UAC, "uac.rpl", 0},
{WUPS_LOADER_LIBRARY_UAC_RPL, "uac_rpl.rpl", 0},
{WUPS_LOADER_LIBRARY_USB_MIC, "usb_mic.rpl", 0},
{WUPS_LOADER_LIBRARY_UVC, "uvc.rpl", 0},
{WUPS_LOADER_LIBRARY_UVD, "uvd.rpl", 0},
{WUPS_LOADER_LIBRARY_VPAD, "vpad.rpl", 0},
{WUPS_LOADER_LIBRARY_VPADBASE, "vpadbase.rpl", 0},
{WUPS_LOADER_LIBRARY_ZLIB125, "zlib125.rpl", 0}
};
void new_PatchInvidualMethodHooks(plugin_info_t *plugin_data) {
new_resetLibs();
DEBUG_FUNCTION_LINE("Patching %d given functions", plugin_data->number_used_functions);
int32_t method_hooks_count = plugin_data->number_used_functions;
uint32_t skip_instr = 1;
uint32_t my_instr_len = 4;
uint32_t instr_len = my_instr_len + skip_instr + 15;
uint32_t flush_len = 4 * instr_len;
for (int32_t i = 0; i < method_hooks_count; i++) {
replacement_data_function_t *function_data = &plugin_data->functions[i];
/* Patch branches to it. */
volatile uint32_t *space = function_data->replace_data;
DEBUG_FUNCTION_LINE_WRITE("Patching %s ...", function_data->function_name);
if (function_data->library == WUPS_LOADER_LIBRARY_OTHER) {
WHBLogWritef("Oh, using straight PA/VA");
if (function_data->alreadyPatched == 1) {
DEBUG_FUNCTION_LINE("Skipping %s, its already patched", function_data->function_name);
continue;
}
} else {
if (function_data->functionType == STATIC_FUNCTION && function_data->alreadyPatched == 1) {
if (new_isDynamicFunction((uint32_t) OSEffectiveToPhysical(function_data->realAddr))) {
DEBUG_FUNCTION_LINE("INFO: The function %s is a dynamic function.", function_data->function_name);
function_data->functionType = DYNAMIC_FUNCTION;
} else {
WHBLogWritef("Skipping %s, its already patched\n", function_data->function_name);
continue;
}
}
}
uint32_t physical = function_data->physicalAddr;
uint32_t repl_addr = (uint32_t) function_data->replaceAddr;
uint32_t call_addr = (uint32_t) function_data->replaceCall;
uint32_t real_addr = function_data->virtualAddr;
if (function_data->library != WUPS_LOADER_LIBRARY_OTHER) {
real_addr = new_GetAddressOfFunction(function_data->function_name, function_data->library);
}
if (!real_addr) {
WHBLogWritef("\n");
DEBUG_FUNCTION_LINE("OSDynLoad_FindExport failed for %s\n", function_data->function_name);
continue;
}
if (DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("%s is located at %08X!\n", function_data->function_name, real_addr);
}
if (function_data->library != WUPS_LOADER_LIBRARY_OTHER) {
physical = (uint32_t) OSEffectiveToPhysical(real_addr);
}
if (!physical) {
WHBLogWritef("Error. Something is wrong with the physical address\n");
continue;
}
if (DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("%s physical is located at %08X!\n", function_data->function_name, physical);
}
*(volatile uint32_t *) (call_addr) = (uint32_t) (space);
uint32_t targetAddr = (uint32_t) space;
if (targetAddr < 0x00800000 || targetAddr >= 0x01000000) {
targetAddr = (uint32_t) OSEffectiveToPhysical(targetAddr);
} else {
targetAddr = targetAddr + 0x30800000 - 0x00800000;
}
KernelCopyData(targetAddr, physical, 4);
ICInvalidateRange((void *) (space), 4);
DCFlushRange((void *) (space), 4);
space++;
//Only works if skip_instr == 1
if (skip_instr == 1) {
// fill the restore instruction section
function_data->realAddr = real_addr;
function_data->restoreInstruction = space[-1];
if (DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("function_data->realAddr = %08X!\n", function_data->realAddr);
}
if (DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("function_data->restoreInstruction = %08X!\n", function_data->restoreInstruction);
}
} else {
WHBLogWritef("Error. Can't save %s for restoring!\n", function_data->function_name);
}
/*
00808cfc 3d601234 lis r11 ,0x1234
00808d00 616b5678 ori r11 ,r11 ,0x5678
00808d04 7d6903a6 mtspr CTR ,r11
00808d08 4e800420 bctr
*/
uint32_t ptr = (uint32_t)space;
*space = 0x3d600000 | (((real_addr + (skip_instr * 4)) >> 16) & 0x0000FFFF); space++; // lis r11 ,0x1234
*space = 0x616b0000 | ((real_addr + (skip_instr * 4)) & 0x0000ffff); space++; // ori r11 ,r11 ,0x5678
*space = 0x7d6903a6; space++; // mtspr CTR ,r11
*space = 0x4e800420; space++;
// Only use patched function if OSGetUPID is 2 (wii u menu) or 15 (game)
uint32_t repl_addr_test = (uint32_t) space;
*space = 0x3d600000 | (((uint32_t*) OSGetUPID)[0] & 0x0000FFFF); space++; // lis r11 ,0x0
*space = 0x816b0000 | (((uint32_t*) OSGetUPID)[1] & 0x0000FFFF); space++; // lwz r11 ,0x0(r11)
*space = 0x2c0b0000 | 0x00000002; space++; // cmpwi r11 ,0x2
*space = 0x41820000 | 0x00000020; space++; // beq myfunc
*space = 0x2c0b0000 | 0x0000000F; space++; // cmpwi r11 ,0xF
*space = 0x41820000 | 0x00000018; space++; // beq myfunc
*space = 0x3d600000 | (((real_addr + (skip_instr * 4)) >> 16) & 0x0000FFFF); space++; // lis r11 ,0x1234
*space = 0x616b0000 | ((real_addr + (skip_instr * 4)) & 0x0000ffff); space++; // ori r11 ,r11 ,0x5678
*space = 0x7d6903a6; space++; // mtspr CTR ,r11
*space = function_data->restoreInstruction; space++; //
*space = 0x4e800420; space++; // bctr
// myfunc:
*space = 0x3d600000 | (((repl_addr) >> 16) & 0x0000FFFF); space++; // lis r11 ,0x1234
*space = 0x616b0000 | ((repl_addr) & 0x0000ffff); space++; // ori r11 ,r11 ,0x5678
*space = 0x7d6903a6; space++; // mtspr CTR ,r11
*space = 0x4e800420; space++; // bctr
DCFlushRange((void *) (((uint32_t) space) - flush_len), flush_len);
ICInvalidateRange((void *) (((uint32_t) space) - flush_len), flush_len);
//setting jump back
uint32_t replace_instr = 0x48000002 | (repl_addr_test & 0x03fffffc);
ICInvalidateRange(&replace_instr, 4);
DCFlushRange(&replace_instr, 4);
KernelCopyData(physical, (uint32_t) OSEffectiveToPhysical((uint32_t) &replace_instr), 4);
ICInvalidateRange((void *) (real_addr), 4);
DCFlushRange((void *) (real_addr), 4);
function_data->alreadyPatched = 1;
DEBUG_FUNCTION_LINE("done with patching %s!\n", function_data->function_name);
}
DEBUG_FUNCTION_LINE("Done with patching given functions!\n");
}
/* ****************************************************************** */
/* RESTORE ORIGINAL INSTRUCTIONS */
/* ****************************************************************** */
void new_RestoreInvidualInstructions(plugin_info_t *plugin_data) {
new_resetLibs();
DEBUG_FUNCTION_LINE("Restoring given functions!");
int32_t method_hooks_count = plugin_data->number_used_functions;
for (int32_t i = 0; i < method_hooks_count; i++) {
replacement_data_function_t *function_data = &plugin_data->functions[i];
DEBUG_FUNCTION_LINE("Restoring %s... ", function_data->function_name);
if (function_data->restoreInstruction == 0 || function_data->realAddr == 0) {
WHBLogPrintf("I dont have the information for the restore =( skip");
continue;
}
uint32_t real_addr = function_data->virtualAddr;
if (function_data->library != WUPS_LOADER_LIBRARY_OTHER) {
real_addr = new_GetAddressOfFunction(function_data->function_name, function_data->library);
}
if (!real_addr) {
WHBLogPrintf("OSDynLoad_FindExport failed for %s", function_data->function_name);
continue;
}
uint32_t physical = function_data->physicalAddr;
if (function_data->library != WUPS_LOADER_LIBRARY_OTHER) {
physical = (uint32_t) OSEffectiveToPhysical(real_addr);
}
if (!physical) {
WHBLogPrintf("Something is wrong with the physical address");
continue;
}
if ((function_data->library != WUPS_LOADER_LIBRARY_OTHER) && new_isDynamicFunction(physical)) {
WHBLogPrintf("Its a dynamic function. We don't need to restore it!", function_data->function_name);
} else {
if (function_data->library != WUPS_LOADER_LIBRARY_OTHER) {
physical = (uint32_t) OSEffectiveToPhysical(function_data->realAddr); //When its an static function, we need to use the old location
}
if (DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("Restoring %08X to %08X", (uint32_t) function_data->restoreInstruction, physical);
}
uint32_t targetAddr = (uint32_t) &(function_data->restoreInstruction);
if (targetAddr < 0x00800000 || targetAddr >= 0x01000000) {
targetAddr = (uint32_t) OSEffectiveToPhysical(targetAddr);
} else {
targetAddr = targetAddr + 0x30800000 - 0x00800000;
}
DEBUG_FUNCTION_LINE("Copy %d bytes from %08X to %08x", 4, targetAddr, physical);
KernelCopyData(physical, targetAddr, 4);
if (DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("ICInvalidateRange %08X", (void *) function_data->realAddr);
}
if (function_data->library != WUPS_LOADER_LIBRARY_OTHER) {
ICInvalidateRange((void *) function_data->realAddr, 4);
DCFlushRange((void *) function_data->realAddr, 4);
}
WHBLogPrintf("done");
}
function_data->alreadyPatched = 0; // In case a
}
DEBUG_FUNCTION_LINE("Done with restoring given functions!");
}
int32_t new_isDynamicFunction(uint32_t physicalAddress) {
if ((physicalAddress & 0x80000000) == 0x80000000) {
return 1;
}
return 0;
}
uint32_t new_GetAddressOfFunction(const char *functionName, wups_loader_library_type_t library) {
uint32_t real_addr = 0;
OSDynLoad_Module rpl_handle = 0;
int32_t rpl_handles_size = sizeof rpl_handles / sizeof rpl_handles[0];
for (int32_t i = 0; i < rpl_handles_size; i++) {
if (rpl_handles[i].library == library) {
if (rpl_handles[i].handle == 0) {
DEBUG_FUNCTION_LINE("Lets acquire handle for rpl: %s\n", rpl_handles[i].rplname);
OSDynLoad_Acquire((char *) rpl_handles[i].rplname, &rpl_handles[i].handle);
}
if (rpl_handles[i].handle == 0) {
WHBLogWritef("%s failed to acquire\n", rpl_handles[i].rplname);
return 0;
}
rpl_handle = rpl_handles[i].handle;
break;
}
}
if (!rpl_handle) {
DEBUG_FUNCTION_LINE("Failed to find the RPL handle for %s\n", functionName);
return 0;
}
OSDynLoad_FindExport(rpl_handle, 0, functionName, reinterpret_cast<void **>(&real_addr));
if (!real_addr) {
DEBUG_FUNCTION_LINE("OSDynLoad_FindExport failed for %s\n", functionName);
return 0;
}
if ((library == WUPS_LOADER_LIBRARY_NN_ACP) && (uint32_t) (*(volatile uint32_t *) (real_addr) & 0x48000002) == 0x48000000) {
uint32_t address_diff = (uint32_t) (*(volatile uint32_t *) (real_addr) & 0x03FFFFFC);
if ((address_diff & 0x03000000) == 0x03000000) {
address_diff |= 0xFC000000;
}
real_addr += (int32_t) address_diff;
if ((uint32_t) (*(volatile uint32_t *) (real_addr) & 0x48000002) == 0x48000000) {
return 0;
}
}
return real_addr;
}
void new_resetLibs() {
int32_t rpl_handles_size = sizeof rpl_handles / sizeof rpl_handles[0];
for (int32_t i = 0; i < rpl_handles_size; i++) {
if (rpl_handles[i].handle != 0) {
DEBUG_FUNCTION_LINE("Resetting handle for rpl: %s\n", rpl_handles[i].rplname);
}
rpl_handles[i].handle = 0;
// Release handle?
}
}

View File

@ -1,52 +0,0 @@
/****************************************************************************
* Copyright (C) 2016-2018 Maschell
* With code from chadderz and dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#ifndef _FUNCTION_PATCHER_HOOKS_H_
#define _FUNCTION_PATCHER_HOOKS_H_
#include <coreinit/dynload.h>
#include <common/plugin_defines.h>
#include <common/replacement_defines.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <wups.h>
#define STATIC_FUNCTION 0
#define DYNAMIC_FUNCTION 1
struct rpl_handling {
wups_loader_library_type_t library;
const char rplname[15];
OSDynLoad_Module handle;
};
void new_PatchInvidualMethodHooks(plugin_info_t *plugin_data);
void new_RestoreInvidualInstructions(plugin_info_t *plugin_data);
uint32_t new_GetAddressOfFunction(const char *functionName, wups_loader_library_type_t library);
int32_t new_isDynamicFunction(uint32_t physicalAddress);
void new_resetLibs();
#ifdef __cplusplus
}
#endif
#endif /* _FUNCTION_PATCHER_HOOKS_H_ */

View File

@ -18,11 +18,12 @@
#include <wups.h> #include <wups.h>
#include <string> #include <string>
#include <function_patcher/fpatching_defines.h>
class FunctionData { class FunctionData {
public: public:
FunctionData(void *paddress, void *vaddress, const std::string &name, wups_loader_library_type_t library, void *replaceAddr, void *replaceCall) { FunctionData(void *paddress, void *vaddress, const std::string &name, function_replacement_library_type_t library, void *replaceAddr, void *replaceCall) {
this->paddress = paddress; this->paddress = paddress;
this->vaddress = vaddress; this->vaddress = vaddress;
this->name = name; this->name = name;
@ -39,7 +40,7 @@ public:
return this->name; return this->name;
} }
wups_loader_library_type_t getLibrary() const { function_replacement_library_type_t getLibrary() const {
return this->library; return this->library;
} }
@ -63,7 +64,7 @@ private:
void *paddress = NULL; void *paddress = NULL;
void *vaddress = NULL; void *vaddress = NULL;
std::string name; std::string name;
wups_loader_library_type_t library; function_replacement_library_type_t library;
void *replaceAddr = NULL; void *replaceAddr = NULL;
void *replaceCall = NULL; void *replaceCall = NULL;
}; };

View File

@ -97,7 +97,7 @@ bool PluginContainerPersistence::savePlugin(plugin_information_t *pluginInformat
/* Store function replacement information */ /* Store function replacement information */
uint32_t i = 0; uint32_t i = 0;
for (auto &curFunction : pluginInfo.getFunctionDataList()) { for (auto &curFunction : pluginInfo.getFunctionDataList()) {
replacement_data_function_t *function_data = &plugin_data->info.functions[i]; function_replacement_data_t *function_data = &plugin_data->info.functions[i];
if (strlen(curFunction.getName().c_str()) > MAXIMUM_FUNCTION_NAME_LENGTH - 1) { if (strlen(curFunction.getName().c_str()) > MAXIMUM_FUNCTION_NAME_LENGTH - 1) {
DEBUG_FUNCTION_LINE("Could not add function \"%s\" for plugin \"%s\" function name is too long.", curFunction.getName().c_str(), pluginName.c_str()); DEBUG_FUNCTION_LINE("Could not add function \"%s\" for plugin \"%s\" function name is too long.", curFunction.getName().c_str(), pluginName.c_str());
continue; continue;
@ -107,11 +107,13 @@ bool PluginContainerPersistence::savePlugin(plugin_information_t *pluginInformat
strncpy(function_data->function_name, curFunction.getName().c_str(), MAXIMUM_FUNCTION_NAME_LENGTH - 1); strncpy(function_data->function_name, curFunction.getName().c_str(), MAXIMUM_FUNCTION_NAME_LENGTH - 1);
function_data->library = curFunction.getLibrary(); function_data->VERSION = FUNCTION_REPLACEMENT_DATA_STRUCT_VERSION;
function_data->library = (function_replacement_library_type_t) curFunction.getLibrary();
function_data->replaceAddr = (uint32_t) curFunction.getReplaceAddress(); function_data->replaceAddr = (uint32_t) curFunction.getReplaceAddress();
function_data->replaceCall = (uint32_t) curFunction.getReplaceCall(); function_data->replaceCall = (uint32_t) curFunction.getReplaceCall();
function_data->physicalAddr = (uint32_t) curFunction.getPhysicalAddress(); function_data->physicalAddr = (uint32_t) curFunction.getPhysicalAddress();
function_data->virtualAddr = (uint32_t) curFunction.getVirtualAddress(); function_data->virtualAddr = (uint32_t) curFunction.getVirtualAddress();
function_data->targetProcess = FP_TARGET_PROCESS_GAME_AND_MENU;
plugin_data->info.number_used_functions++; plugin_data->info.number_used_functions++;
i++; i++;
@ -247,8 +249,8 @@ std::vector<PluginContainer> PluginContainerPersistence::loadPlugins(plugin_info
} }
for (uint32_t j = 0; j < functionReplaceCount; j++) { for (uint32_t j = 0; j < functionReplaceCount; j++) {
replacement_data_function_t *entry = &(plugin_data->info.functions[j]); function_replacement_data_t *entry = &(plugin_data->info.functions[j]);
FunctionData func((void *) entry->physicalAddr, (void *) entry->virtualAddr, entry->function_name, entry->library, (void *) entry->replaceAddr, (void *) entry->replaceCall); FunctionData func((void *) entry->physicalAddr, (void *) entry->virtualAddr, entry->function_name, (function_replacement_library_type_t) entry->library, (void *) entry->replaceAddr, (void *) entry->replaceCall);
pluginInformation.addFunctionData(func); pluginInformation.addFunctionData(func);
} }

View File

@ -197,7 +197,7 @@ std::optional<PluginInformation> PluginInformationFactory::load(const PluginData
wups_loader_entry_t *cur_function = &entries[j]; wups_loader_entry_t *cur_function = &entries[j];
DEBUG_FUNCTION_LINE("Saving function \"%s\" of plugin . PA:%08X VA:%08X Library: %08X, target: %08X, call_addr: %08X", cur_function->_function.name/*,pluginData.getPluginInformation()->getName().c_str()*/, DEBUG_FUNCTION_LINE("Saving function \"%s\" of plugin . PA:%08X VA:%08X Library: %08X, target: %08X, call_addr: %08X", cur_function->_function.name/*,pluginData.getPluginInformation()->getName().c_str()*/,
cur_function->_function.physical_address, cur_function->_function.virtual_address, cur_function->_function.library, cur_function->_function.target, (void *) cur_function->_function.call_addr); cur_function->_function.physical_address, cur_function->_function.virtual_address, cur_function->_function.library, cur_function->_function.target, (void *) cur_function->_function.call_addr);
FunctionData function_data((void *) cur_function->_function.physical_address, (void *) cur_function->_function.virtual_address, cur_function->_function.name, cur_function->_function.library, FunctionData function_data((void *) cur_function->_function.physical_address, (void *) cur_function->_function.virtual_address, cur_function->_function.name, (function_replacement_library_type_t) cur_function->_function.library,
(void *) cur_function->_function.target, (void *) cur_function->_function.call_addr); (void *) cur_function->_function.target, (void *) cur_function->_function.call_addr);
pluginInfo.addFunctionData(function_data); pluginInfo.addFunctionData(function_data);
} }