diff --git a/src/main.cpp b/src/main.cpp index 2cfe38d..4089dc9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,6 +20,7 @@ #include "patcher/function_patcher_gx2.h" #include "patcher/function_patcher_coreinit.h" #include "utils/sd_ip_reader.hpp" +#include "sd_cheats.h" bool isCodeHandlerInstalled; diff --git a/src/sd_cheats.cpp b/src/sd_cheats.cpp new file mode 100644 index 0000000..869dfd9 --- /dev/null +++ b/src/sd_cheats.cpp @@ -0,0 +1,82 @@ +#include // snprintf +#include // memcpy, memset +#include "sd_cheats.h" +#include "utils/logger.h" +#include "fs/sd_fat_devoptab.h" +#include "fs/fs_utils.h" +#include "dynamic_libs/os_functions.h" +#include "tcp_gecko.h" +#include "kernel/syscalls.h" + +#define EXTENSION_SIZE 6 +#define SD_FILE_PATH_HEADER_LENGTH 10 +#define TITLE_ID_LEADING_ZEROS 3 +#define TITLE_ID_LENGTH 16 +#define CODES_FILE_PATH_SIZE (SD_FILE_PATH_HEADER_LENGTH + TITLE_ID_LENGTH + EXTENSION_SIZE) + +u64 cachedTitleID; + +unsigned char *kernelCopyBufferOld2[DATA_BUFFER_SIZE]; + +void kernelCopyData2(unsigned char *destinationBuffer, unsigned char *sourceBuffer, unsigned int length) { + if (length > DATA_BUFFER_SIZE) { + OSFatal("Kernel copy buffer size exceeded"); + } + + memcpy(kernelCopyBufferOld2, sourceBuffer, length); + SC0x25_KernelCopyData((unsigned int) OSEffectiveToPhysical(destinationBuffer), + (unsigned int) &kernelCopyBufferOld2, + length); + DCFlushRange(destinationBuffer, (u32) length); +} + +void considerApplyingSDCheats() { + u64 currentTitleID = OSGetTitleID(); + + if (cachedTitleID == currentTitleID) { + // log_print("Title ID NOT changed\n"); + } else { + log_print("Title ID changed\n"); + cachedTitleID = currentTitleID; + int result = mount_sd_fat("sd"); + + if (result < 0) { + log_printf("Mounting error: %i\n", result); + return; + } + + unsigned char filePath[CODES_FILE_PATH_SIZE]; + memset(filePath, '0', sizeof(filePath)); + memcpy(filePath, "sd:/codes/", SD_FILE_PATH_HEADER_LENGTH); // File path header + log_printf("Title ID: %lu\n", currentTitleID); + char asciiTitleID[TITLE_ID_LENGTH]; + snprintf(asciiTitleID, TITLE_ID_LENGTH, "%llX", currentTitleID); + memcpy(filePath + SD_FILE_PATH_HEADER_LENGTH + TITLE_ID_LEADING_ZEROS, asciiTitleID, + TITLE_ID_LENGTH); // Title ID + memcpy(filePath + SD_FILE_PATH_HEADER_LENGTH + TITLE_ID_LENGTH, ".gctu", EXTENSION_SIZE); // Extension + filePath[CODES_FILE_PATH_SIZE - 1] = '\0'; // Null-terminated + log_printf("File Path: %s\n", filePath); + + unsigned char *codes = NULL; + unsigned int codesSize = 0; + result = LoadFileToMem((const char *) filePath, &codes, &codesSize); + + if (result < 0) { + log_printf("Reading error: %i\n", result); + // Error, we won't write any codes + goto CLEANUP; + } + + log_print("Copying...\n"); + kernelCopyData2((unsigned char *) 0x01133000, codes, codesSize); + log_print("Copied!\n"); + + CLEANUP: + + result = unmount_sd_fat("sd"); + + if (result < 0) { + log_printf("Unmounting error: %i\n", result); + } + } +} \ No newline at end of file diff --git a/src/sd_cheats.h b/src/sd_cheats.h new file mode 100644 index 0000000..e2b6e3e --- /dev/null +++ b/src/sd_cheats.h @@ -0,0 +1,6 @@ +#ifndef TCPGECKO_SD_CHEATS_H +#define TCPGECKO_SD_CHEATS_H + +void considerApplyingSDCheats(); + +#endif \ No newline at end of file diff --git a/src/system/kernel.h b/src/system/kernel.h index 30f9d8c..e87d5e5 100644 --- a/src/system/kernel.h +++ b/src/system/kernel.h @@ -6,8 +6,6 @@ #include "../tcp_gecko.h" #include "../utils/logger.h" -unsigned char *kernelCopyBuffer[sizeof(int)]; - // TODO Variable size, not hard-coded unsigned char *kernelCopyBufferOld[DATA_BUFFER_SIZE]; @@ -22,6 +20,8 @@ void kernelCopyData(unsigned char *destinationBuffer, unsigned char *sourceBuffe DCFlushRange(destinationBuffer, (u32) length); } +unsigned char *kernelCopyBuffer[sizeof(int)]; + void kernelCopyInt(unsigned char *destinationBuffer, unsigned char *sourceBuffer, unsigned int length) { memcpy(kernelCopyBuffer, sourceBuffer, length); unsigned int destinationAddress = (unsigned int) OSEffectiveToPhysical(destinationBuffer); diff --git a/src/tcp_gecko.cpp b/src/tcp_gecko.cpp index 0a6a626..15b2419 100644 --- a/src/tcp_gecko.cpp +++ b/src/tcp_gecko.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +// #include #include "common/common.h" #include // Actually must be included before os_functions #include "dynamic_libs/os_functions.h" @@ -22,8 +22,7 @@ #include "utils/sd_ip_reader.hpp" #include "patcher/function_patcher_gx2.h" #include "system/raw_assembly_cheats.h" -#include "fs/fs_utils.h" -#include "fs/sd_fat_devoptab.h" +#include "sd_cheats.h" void *client; void *commandBlock; @@ -1480,64 +1479,6 @@ static int runTCPGeckoServer(int argc, void *argv) { return 0; } -#define EXTENSION_SIZE 6 -#define SD_FILE_PATH_HEADER_LENGTH 10 -#define TITLE_ID_LEADING_ZEROS 3 -#define TITLE_ID_LENGTH 16 -#define CODES_FILE_PATH_SIZE (SD_FILE_PATH_HEADER_LENGTH + TITLE_ID_LENGTH + EXTENSION_SIZE) - -u64 cachedTitleID = 0; - -void considerApplyingSDCheats() { - u64 currentTitleID = OSGetTitleID(); - - if (cachedTitleID == currentTitleID) { - // log_print("Title ID NOT changed\n"); - } else { - log_print("Title ID changed\n"); - cachedTitleID = currentTitleID; - int result = mount_sd_fat("sd"); - - if (result < 0) { - log_printf("Mounting error: %i\n", result); - return; - } - - unsigned char filePath[CODES_FILE_PATH_SIZE]; - memset(filePath, '0', sizeof(filePath)); - memcpy(filePath, "sd:/codes/", SD_FILE_PATH_HEADER_LENGTH); // File path header - log_printf("Title ID: %lu\n", currentTitleID); - char asciiTitleID[TITLE_ID_LENGTH]; - snprintf(asciiTitleID, TITLE_ID_LENGTH, "%llX", currentTitleID); - memcpy(filePath + SD_FILE_PATH_HEADER_LENGTH + TITLE_ID_LEADING_ZEROS, asciiTitleID, - TITLE_ID_LENGTH); // Title ID - memcpy(filePath + SD_FILE_PATH_HEADER_LENGTH + TITLE_ID_LENGTH, ".gctu", EXTENSION_SIZE); // Extension - filePath[CODES_FILE_PATH_SIZE - 1] = '\0'; // Null-terminated - log_printf("File Path: %s\n", filePath); - - unsigned char *codes = NULL; - unsigned int codesSize = 0; - result = LoadFileToMem((const char *) filePath, &codes, &codesSize); - - if (result < 0) { - log_printf("Reading error: %i\n", result); - // Error, we won't write any codes - goto CLEANUP; - } - - kernelCopyData((unsigned char *) 0x01133000, codes, codesSize); - log_print("Copied!\n"); - - CLEANUP: - - result = unmount_sd_fat("sd"); - - if (result < 0) { - log_printf("Unmounting error: %i\n", result); - } - } -} - static int startTCPGeckoThread(int argc, void *argv) { log_print("Starting TCP Gecko thread...\n"); @@ -1562,13 +1503,14 @@ static int startTCPGeckoThread(int argc, void *argv) { // Execute the code handler if it is installed if (isCodeHandlerInstalled) { + considerApplyingSDCheats(); log_print("Code handler installed...\n"); void (*codeHandlerFunction)() = (void (*)()) CODE_HANDLER_INSTALL_ADDRESS; while (true) { usleep(9000); - considerApplyingSDCheats(); + // considerApplyingSDCheats(); // log_print("Running code handler...\n"); codeHandlerFunction(); diff --git a/tcpgecko.elf b/tcpgecko.elf index 62f4239..d1d6e70 100644 Binary files a/tcpgecko.elf and b/tcpgecko.elf differ