Revert the mainhook on error

This commit is contained in:
Maschell 2020-10-19 00:16:45 +02:00
parent 8b01f5ca75
commit f93fc73ace
4 changed files with 60 additions and 1 deletions

View File

@ -19,6 +19,7 @@
#include <stdio.h>
#include <string.h>
#include <coreinit/cache.h>
#include <coreinit/memorymap.h>
#include "kernel.h"
@ -30,6 +31,27 @@ extern "C" void SaveAndResetDataBATs_And_SRs_hook(void);
void __attribute__ ((noinline)) kern_write(void *addr, uint32_t value);
extern "C" void SC_0x25_KernelCopyData(unsigned int addr, unsigned int src, unsigned int len);
extern "C" void SCKernelCopyData(unsigned int addr, unsigned int src, unsigned int len);
void KernelWriteU32(uint32_t addr, uint32_t value) {
ICInvalidateRange(&value, 4);
DCFlushRange(&value, 4);
auto dst = (uint32_t) OSEffectiveToPhysical(addr);
auto src = (uint32_t) OSEffectiveToPhysical((uint32_t) &value);
SC_0x25_KernelCopyData(dst, src, 4);
DCFlushRange((void *) addr, 4);
ICInvalidateRange((void *) addr, 4);
}
void revertMainHook() {
KernelWriteU32(0x0101c56c, 0x4E800421);
}
void doKernelSetup() {
kern_write((void *) (KERN_SYSCALL_TBL_1 + (0x36 * 4)), (unsigned int) KernelPatches);
kern_write((void *) (KERN_SYSCALL_TBL_2 + (0x36 * 4)), (unsigned int) KernelPatches);
@ -52,6 +74,13 @@ void doKernelSetup2() {
kern_write((void *) (KERN_SYSCALL_TBL_5 + (0x36 * 4)), (unsigned int) KernelPatchesFinal);
Syscall_0x36();
kern_write((void *) (KERN_SYSCALL_TBL_1 + (0x25 * 4)), (unsigned int) SCKernelCopyData);
kern_write((void *) (KERN_SYSCALL_TBL_2 + (0x25 * 4)), (unsigned int) SCKernelCopyData);
kern_write((void *) (KERN_SYSCALL_TBL_3 + (0x25 * 4)), (unsigned int) SCKernelCopyData);
kern_write((void *) (KERN_SYSCALL_TBL_4 + (0x25 * 4)), (unsigned int) SCKernelCopyData);
kern_write((void *) (KERN_SYSCALL_TBL_5 + (0x25 * 4)), (unsigned int) SCKernelCopyData);
}
/* Write a 32-bit word with kernel permissions */

View File

@ -31,6 +31,8 @@ void doKernelSetup();
void doKernelSetup2();
void revertMainHook();
#ifdef __cplusplus
}
#endif

27
src/kernel_copy.S Normal file
View File

@ -0,0 +1,27 @@
.global SCKernelCopyData
SCKernelCopyData:
// Disable data address translation
mfmsr %r6
li %r7, 0x10
andc %r6, %r6, %r7
mtmsr %r6
// Copy data
addi %r3, %r3, -1
addi %r4, %r4, -1
mtctr %r5
SCKernelCopyData_loop:
lbzu %r5, 1(%r4)
stbu %r5, 1(%r3)
bdnz SCKernelCopyData_loop
// Enable data address translation
ori %r6, %r6, 0x10
mtmsr %r6
blr
.global SC_0x25_KernelCopyData
SC_0x25_KernelCopyData:
li %r0, 0x2500
sc
blr

View File

@ -105,7 +105,8 @@ extern "C" int _start(int argc, char **argv) {
((int (*)(int, char **)) moduleData->getEntrypoint())(argc, argv);
doProcUI = true;
} else {
DEBUG_FUNCTION_LINE("Failed to load module");
DEBUG_FUNCTION_LINE("Failed to load module, revert main_hook");
revertMainHook();
}
if (doProcUI) {