Move towards using C++

This commit is contained in:
BullyWiiPlaza 2017-07-04 10:34:47 +02:00
parent 19b8e017ef
commit 0d42c264e2
26 changed files with 342 additions and 251 deletions

View File

@ -59,7 +59,7 @@ MAKEFLAGS += --no-print-directory
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project # any extra libraries we wish to link with the project
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
LIBS := -lz -liosuhax -lgd LIBS := -lm -lfat -lz -liosuhax -lgd
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing # list of directories containing libraries, this must be the top level containing

View File

@ -1,11 +1,11 @@
#ifndef COMMON_H #pragma once
#define COMMON_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include "os_defs.h" #include "os_defs.h"
#include "fs_defs.h"
#define CODE_HANDLER_INSTALL_ADDRESS 0x010F4000 #define CODE_HANDLER_INSTALL_ADDRESS 0x010F4000
@ -58,17 +58,15 @@ extern "C" {
#define RESTORE_INSTR_ADDR ((restore_instructions_t*)(MEM_BASE + 0x1600)) #define RESTORE_INSTR_ADDR ((restore_instructions_t*)(MEM_BASE + 0x1600))
typedef struct _restore_instructions_t { typedef struct _restore_instructions_t {
unsigned int magic; unsigned int magic;
unsigned int instr_count; unsigned int instr_count;
struct { struct {
unsigned int addr; unsigned int addr;
unsigned int instr; unsigned int instr;
} data[0]; } data[0];
} restore_instructions_t; } restore_instructions_t;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* COMMON_H */

View File

@ -50,8 +50,7 @@ extern int (*FSUnmount)(void *pClient, void *pCmd, const char *target, int errHa
extern int (*FSRename)(void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error); extern int (*FSRename)(void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error);
extern int extern int (*FSRenameAsync)(void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error, void *asyncParams);
(*FSRenameAsync)(void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error, void *asyncParams);
extern int (*FSRemove)(void *pClient, void *pCmd, const char *path, int error); extern int (*FSRemove)(void *pClient, void *pCmd, const char *path, int error);
@ -63,8 +62,7 @@ extern int (*FSGetStatAsync)(void *pClient, void *pCmd, const char *path, void *
extern int (*FSRename)(void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error); extern int (*FSRename)(void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error);
extern int extern int (*FSRenameAsync)(void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error, void *asyncParams);
(*FSRenameAsync)(void *pClient, void *pCmd, const char *oldPath, const char *newPath, int error, void *asyncParams);
extern int (*FSRemove)(void *pClient, void *pCmd, const char *path, int error); extern int (*FSRemove)(void *pClient, void *pCmd, const char *path, int error);
@ -76,9 +74,7 @@ extern int (*FSFlushQuotaAsync)(void *pClient, void *pCmd, const char *path, int
extern int (*FSGetFreeSpaceSize)(void *pClient, void *pCmd, const char *path, uint64_t *returnedFreeSize, int error); extern int (*FSGetFreeSpaceSize)(void *pClient, void *pCmd, const char *path, uint64_t *returnedFreeSize, int error);
extern int extern int (*FSGetFreeSpaceSizeAsync)(void *pClient, void *pCmd, const char *path, uint64_t *returnedFreeSize, int error, void *asyncParams);
(*FSGetFreeSpaceSizeAsync)(void *pClient, void *pCmd, const char *path, uint64_t *returnedFreeSize, int error,
void *asyncParams);
extern int (*FSRollbackQuota)(void *pClient, void *pCmd, const char *path, int error); extern int (*FSRollbackQuota)(void *pClient, void *pCmd, const char *path, int error);
@ -104,11 +100,9 @@ extern int (*FSMakeDirAsync)(void *pClient, void *pCmd, const char *path, int er
extern int (*FSOpenFile)(void *pClient, void *pCmd, const char *path, const char *mode, int *fd, int errHandling); extern int (*FSOpenFile)(void *pClient, void *pCmd, const char *path, const char *mode, int *fd, int errHandling);
extern int (*FSOpenFileAsync)(void *pClient, void *pCmd, const char *path, const char *mode, int *handle, int error, extern int (*FSOpenFileAsync)(void *pClient, void *pCmd, const char *path, const char *mode, int *handle, int error, const void *asyncParams);
const void *asyncParams);
extern int extern int (*FSReadFile)(void *pClient, void *pCmd, void *buffer, int size, int count, int fd, int flag, int errHandling);
(*FSReadFile)(void *pClient, void *pCmd, void *buffer, int size, int count, int fd, int flag, int errHandling);
extern int (*FSCloseFile)(void *pClient, void *pCmd, int fd, int errHandling); extern int (*FSCloseFile)(void *pClient, void *pCmd, int fd, int errHandling);
@ -120,9 +114,7 @@ extern int (*FSGetStatFile)(void *pClient, void *pCmd, int fd, void *buffer, int
extern int (*FSSetPosFile)(void *pClient, void *pCmd, int fd, int pos, int error); extern int (*FSSetPosFile)(void *pClient, void *pCmd, int fd, int pos, int error);
extern int extern int (*FSWriteFile)(void *pClient, void *pCmd, const void *source, int block_size, int block_count, int fd, int flag, int error);
(*FSWriteFile)(void *pClient, void *pCmd, const void *source, int block_size, int block_count, int fd, int flag,
int error);
extern int (*FSBindMount)(void *pClient, void *pCmd, char *source, char *target, int error); extern int (*FSBindMount)(void *pClient, void *pCmd, char *source, char *target, int error);

View File

@ -57,38 +57,49 @@ extern u32 hostIpAddress;
struct in_addr { struct in_addr {
unsigned int s_addr; unsigned int s_addr;
}; };
struct sockaddr_in { struct sockaddr_in {
short sin_family; short sin_family;
unsigned short sin_port; unsigned short sin_port;
struct in_addr sin_addr; struct in_addr sin_addr;
char sin_zero[8]; char sin_zero[8];
}; };
struct sockaddr struct sockaddr {
{ unsigned short sa_family;
unsigned short sa_family; char sa_data[14];
char sa_data[14];
}; };
void InitSocketFunctionPointers(void); void InitSocketFunctionPointers(void);
extern int (*socket_lib_init)(void); extern int (*socket_lib_init)(void);
extern int (*socket_lib_finish)(void); extern int (*socket_lib_finish)(void);
extern int (*socket)(int domain, int type, int protocol); extern int (*socket)(int domain, int type, int protocol);
extern int (*socketclose)(int s); extern int (*socketclose)(int s);
extern int (*connect)(int s, void *addr, int addrlen); extern int (*connect)(int s, void *addr, int addrlen);
extern int (*bind)(s32 s,struct sockaddr *name,s32 namelen);
extern int (*listen)(s32 s,u32 backlog); extern int (*bind)(s32 s, struct sockaddr *name, s32 namelen);
extern int (*accept)(s32 s,struct sockaddr *addr,s32 *addrlen);
extern int (*listen)(s32 s, u32 backlog);
extern int (*accept)(s32 s, struct sockaddr *addr, s32 *addrlen);
extern int (*send)(int s, const void *buffer, int size, int flags); extern int (*send)(int s, const void *buffer, int size, int flags);
extern int (*recv)(int s, void *buffer, int size, int flags); extern int (*recv)(int s, void *buffer, int size, int flags);
extern int (*sendto)(int s, const void *buffer, int size, int flags, const struct sockaddr *dest, int dest_len); extern int (*sendto)(int s, const void *buffer, int size, int flags, const struct sockaddr *dest, int dest_len);
extern int (*setsockopt)(int s, int level, int optname, void *optval, int optlen); extern int (*setsockopt)(int s, int level, int optname, void *optval, int optlen);
extern char * (*inet_ntoa)(struct in_addr in); extern char *(*inet_ntoa)(struct in_addr in);
extern int (*inet_aton)(const char *cp, struct in_addr *inp); extern int (*inet_aton)(const char *cp, struct in_addr *inp);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -2,60 +2,54 @@
#include <stdlib.h> #include <stdlib.h>
#include "CFile.hpp" #include "CFile.hpp"
CFile::CFile() CFile::CFile() {
{
iFd = -1; iFd = -1;
mem_file = NULL; mem_file = NULL;
filesize = 0; filesize = 0;
pos = 0; pos = 0;
} }
CFile::CFile(const std::string & filepath, eOpenTypes mode) CFile::CFile(const std::string &filepath, eOpenTypes mode) {
{
iFd = -1; iFd = -1;
this->open(filepath, mode); this->open(filepath, mode);
} }
CFile::CFile(const u8 * mem, int size) CFile::CFile(const u8 *mem, int size) {
{
iFd = -1; iFd = -1;
this->open(mem, size); this->open(mem, size);
} }
CFile::~CFile() CFile::~CFile() {
{
this->close(); this->close();
} }
int CFile::open(const std::string & filepath, eOpenTypes mode) int CFile::open(const std::string &filepath, eOpenTypes mode) {
{
this->close(); this->close();
s32 openMode = 0; s32 openMode = 0;
switch(mode) switch (mode) {
{ default:
default: case ReadOnly:
case ReadOnly: openMode = O_RDONLY;
openMode = O_RDONLY; break;
break; case WriteOnly:
case WriteOnly: openMode = O_WRONLY;
openMode = O_WRONLY; break;
break; case ReadWrite:
case ReadWrite: openMode = O_RDWR;
openMode = O_RDWR; break;
break; case Append:
case Append: openMode = O_APPEND | O_WRONLY;
openMode = O_APPEND | O_WRONLY; break;
break;
} }
//! Using fopen works only on the first launch as expected //! Using fopen works only on the first launch as expected
//! on the second launch it causes issues because we don't overwrite //! on the second launch it causes issues because we don't overwrite
//! the .data sections which is needed for a normal application to re-init //! the .data sections which is needed for a normal application to re-init
//! this will be added with launching as RPX //! this will be added with launching as RPX
iFd = ::open(filepath.c_str(), openMode); iFd = ::open(filepath.c_str(), openMode);
if(iFd < 0) if (iFd < 0)
return iFd; return iFd;
@ -65,8 +59,7 @@ int CFile::open(const std::string & filepath, eOpenTypes mode)
return 0; return 0;
} }
int CFile::open(const u8 * mem, int size) int CFile::open(const u8 *mem, int size) {
{
this->close(); this->close();
mem_file = mem; mem_file = mem;
@ -75,9 +68,8 @@ int CFile::open(const u8 * mem, int size)
return 0; return 0;
} }
void CFile::close() void CFile::close() {
{ if (iFd >= 0)
if(iFd >= 0)
::close(iFd); ::close(iFd);
iFd = -1; iFd = -1;
@ -86,27 +78,24 @@ void CFile::close()
pos = 0; pos = 0;
} }
int CFile::read(u8 * ptr, size_t size) int CFile::read(u8 *ptr, size_t size) {
{ if (iFd >= 0) {
if(iFd >= 0) int ret = ::read(iFd, ptr, size);
{ if (ret > 0)
int ret = ::read(iFd, ptr,size);
if(ret > 0)
pos += ret; pos += ret;
return ret; return ret;
} }
int readsize = size; int readsize = size;
if(readsize > (s64) (filesize-pos)) if (readsize > (s64)(filesize - pos))
readsize = filesize-pos; readsize = filesize - pos;
if(readsize <= 0) if (readsize <= 0)
return readsize; return readsize;
if(mem_file != NULL) if (mem_file != NULL) {
{ memcpy(ptr, mem_file + pos, readsize);
memcpy(ptr, mem_file+pos, readsize);
pos += readsize; pos += readsize;
return readsize; return readsize;
} }
@ -114,60 +103,47 @@ int CFile::read(u8 * ptr, size_t size)
return -1; return -1;
} }
int CFile::write(const u8 * ptr, size_t size) int CFile::write(const u8 *ptr, size_t size) {
{ if (iFd >= 0) {
if(iFd >= 0) size_t done = 0;
{ while (done < size) {
size_t done = 0; int ret = ::write(iFd, ptr, size - done);
while(done < size) if (ret <= 0)
{ return ret;
int ret = ::write(iFd, ptr, size - done);
if(ret <= 0)
return ret;
ptr += ret; ptr += ret;
done += ret; done += ret;
pos += ret; pos += ret;
} }
return done; return done;
} }
return -1; return -1;
} }
int CFile::seek(long int offset, int origin) int CFile::seek(long int offset, int origin) {
{
int ret = 0; int ret = 0;
s64 newPos = pos; s64 newPos = pos;
if(origin == SEEK_SET) if (origin == SEEK_SET) {
{
newPos = offset; newPos = offset;
} } else if (origin == SEEK_CUR) {
else if(origin == SEEK_CUR)
{
newPos += offset; newPos += offset;
} } else if (origin == SEEK_END) {
else if(origin == SEEK_END) newPos = filesize + offset;
{
newPos = filesize+offset;
} }
if(newPos < 0) if (newPos < 0) {
{
pos = 0; pos = 0;
} } else {
else { pos = newPos;
pos = newPos;
} }
if(iFd >= 0) if (iFd >= 0)
ret = ::lseek(iFd, pos, SEEK_SET); ret = ::lseek(iFd, pos, SEEK_SET);
if(mem_file != NULL) if (mem_file != NULL) {
{ if (pos > filesize) {
if(pos > filesize)
{
pos = filesize; pos = filesize;
} }
} }
@ -175,23 +151,21 @@ int CFile::seek(long int offset, int origin)
return ret; return ret;
} }
int CFile::fwrite(const char *format, ...) int CFile::fwrite(const char *format, ...) {
{ int result = -1;
int result = -1; char *tmp = NULL;
char * tmp = NULL;
va_list va; va_list va;
va_start(va, format); va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp) if ((vasprintf(&tmp, format, va) >= 0) && tmp) {
{ result = this->write((u8 *) tmp, strlen(tmp));
result = this->write((u8 *)tmp, strlen(tmp));
} }
va_end(va); va_end(va);
if(tmp) if (tmp)
free(tmp); free(tmp);
return result; return result;
} }

View File

@ -7,51 +7,60 @@
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <gctypes.h> #include <gctypes.h>
#include "../common/fs_defs.h"
class CFile class CFile {
{ public:
public: enum eOpenTypes {
enum eOpenTypes ReadOnly,
{ WriteOnly,
ReadOnly, ReadWrite,
WriteOnly, Append
ReadWrite, };
Append
};
CFile(); CFile();
CFile(const std::string & filepath, eOpenTypes mode);
CFile(const u8 * memory, int memsize);
virtual ~CFile();
int open(const std::string & filepath, eOpenTypes mode); CFile(const std::string &filepath, eOpenTypes mode);
int open(const u8 * memory, int memsize);
bool isOpen() const { CFile(const u8 *memory, int memsize);
if(iFd >= 0)
return true;
if(mem_file) virtual ~CFile();
return true;
return false; int open(const std::string &filepath, eOpenTypes mode);
}
void close(); int open(const u8 *memory, int memsize);
int read(u8 * ptr, size_t size); bool isOpen() const {
int write(const u8 * ptr, size_t size); if (iFd >= 0)
int fwrite(const char *format, ...); return true;
int seek(long int offset, int origin);
u64 tell() { return pos; };
u64 size() { return filesize; };
void rewind() { this->seek(0, SEEK_SET); };
protected: if (mem_file)
int iFd; return true;
const u8 * mem_file;
u64 filesize; return false;
u64 pos; }
void close();
int read(u8 *ptr, size_t size);
int write(const u8 *ptr, size_t size);
int fwrite(const char *format, ...);
int seek(long int offset, int origin);
u64 tell() {return pos;};
u64 size() {return filesize;};
void rewind() {this->seek(0, SEEK_SET);};
protected:
int iFd;
const u8 *mem_file;
u64 filesize;
u64 pos;
}; };
#endif #endif

View File

@ -18,6 +18,8 @@
#include "utils/logger.h" #include "utils/logger.h"
#include "utils/function_patcher.h" #include "utils/function_patcher.h"
#include "patcher/function_patcher_gx2.h" #include "patcher/function_patcher_gx2.h"
#include "patcher/function_patcher_coreinit.h"
#include "utils/sd_ip_reader.hpp"
bool isCodeHandlerInstalled; bool isCodeHandlerInstalled;
@ -30,6 +32,7 @@ typedef enum {
void applyFunctionPatches() { void applyFunctionPatches() {
patchIndividualMethodHooks(method_hooks_gx2, method_hooks_size_gx2, method_calls_gx2); patchIndividualMethodHooks(method_hooks_gx2, method_hooks_size_gx2, method_calls_gx2);
patchIndividualMethodHooks(method_hooks_coreinit, method_hooks_size_coreinit, method_calls_coreinit);
} }
/* Entry point */ /* Entry point */
@ -91,7 +94,7 @@ int Menu_Main(void) {
int screenBuffer0Size = OSScreenGetBufferSizeEx(0); int screenBuffer0Size = OSScreenGetBufferSizeEx(0);
int screenBuffer1Size = OSScreenGetBufferSizeEx(1); int screenBuffer1Size = OSScreenGetBufferSizeEx(1);
unsigned char *screenBuffer = MEM1_alloc(screenBuffer0Size + screenBuffer1Size, 0x40); unsigned char *screenBuffer = (unsigned char *) MEM1_alloc(screenBuffer0Size + screenBuffer1Size, 0x40);
OSScreenSetBufferEx(0, screenBuffer); OSScreenSetBufferEx(0, screenBuffer);
OSScreenSetBufferEx(1, (screenBuffer + screenBuffer0Size)); OSScreenSetBufferEx(1, (screenBuffer + screenBuffer0Size));
@ -147,7 +150,7 @@ int Menu_Main(void) {
isCodeHandlerInstalled = true; isCodeHandlerInstalled = true;
launchMethod = TCP_GECKO; launchMethod = TCP_GECKO;
log_init(COMPUTER_IP_ADDRESS); initializeUDPLog();
log_print("Patching functions\n"); log_print("Patching functions\n");
applyFunctionPatches(); applyFunctionPatches();

View File

@ -0,0 +1,47 @@
#include "../utils/function_patcher.h"
#include "../utils/logger.h"
declareFunctionHook(int, FSOpenFile, void *pClient, void *pCmd, const char *path, const char *mode, int *fd, int errHandling) {
log_printf("FSOpenFile(): %s", path);
return real_FSOpenFile(pClient, pCmd, path, mode, fd, errHandling);
}
declareFunctionHook(int, FSOpenFileAsync, void *pClient, void *pCmd, const char *path, const char *mode, int *handle, int error, const void *asyncParams) {
log_printf("FSOpenFileAsync(): %s", path);
return real_FSOpenFileAsync(pClient, pCmd, path, mode, handle, error, asyncParams);
}
declareFunctionHook(int, FSOpenDir, void *pClient, void *pCmd, const char *path, int *dh, int errHandling) {
log_printf("FSOpenDir(): %s", path);
return real_FSOpenDir(pClient, pCmd, path, dh, errHandling);
}
declareFunctionHook(int, FSOpenDirAsync, void *pClient, void *pCmd, const char *path, int *handle, int error, void *asyncParams) {
log_printf("FSOpenDirAsync(): %s", path);
return real_FSOpenDirAsync(pClient, pCmd, path, handle, error, asyncParams);
}
declareFunctionHook(int, FSGetStat, void *pClient, void *pCmd, const char *path, FSStat *stats, int errHandling) {
log_printf("FSGetStat(): %s", path);
return real_FSGetStat(pClient, pCmd, path, stats, errHandling);
}
declareFunctionHook(int, FSGetStatAsync, void *pClient, void *pCmd, const char *path, FSStat *stats, int errHandling, void *asyncParams) {
log_printf("FSGetStatAsync(): %s", path);
return real_FSGetStatAsync(pClient, pCmd, path, stats, errHandling, asyncParams);
}
FunctionHook method_hooks_coreinit[] __attribute__((section(".data"))) = {
makeFunctionHook(FSOpenFile, LIB_CORE_INIT, STATIC_FUNCTION),
makeFunctionHook(FSOpenFileAsync, LIB_CORE_INIT, STATIC_FUNCTION),
makeFunctionHook(FSOpenDir, LIB_CORE_INIT, STATIC_FUNCTION),
makeFunctionHook(FSOpenDirAsync, LIB_CORE_INIT, STATIC_FUNCTION),
makeFunctionHook(FSGetStat, LIB_CORE_INIT, STATIC_FUNCTION),
makeFunctionHook(FSGetStatAsync, LIB_CORE_INIT, STATIC_FUNCTION),
};
u32 method_hooks_size_coreinit __attribute__((section(".data"))) = sizeof(method_hooks_coreinit) / sizeof(FunctionHook);
volatile unsigned int method_calls_coreinit[sizeof(method_hooks_coreinit) / sizeof(FunctionHook) * FUNCTION_PATCHER_METHOD_STORE_SIZE] __attribute__((section(".data")));

View File

@ -0,0 +1,15 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include "../utils/function_patcher.h"
extern FunctionHook method_hooks_coreinit[];
extern u32 method_hooks_size_coreinit;
extern volatile unsigned int method_calls_coreinit[];
#ifdef __cplusplus
}
#endif

View File

@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/ ****************************************************************************/
#ifndef FUNCTION_PATCHER_EXAMPLE_GX2_FUNCTION_PATCHER_H #pragma once
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -30,5 +30,3 @@ extern volatile unsigned int method_calls_gx2[];
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif

View File

@ -1,5 +1,5 @@
#include "address.h" #include "address.h"
#include "dynamic_libs/os_functions.h" #include "../dynamic_libs/os_functions.h"
int validateAddressRange(int starting_address, int ending_address) { int validateAddressRange(int starting_address, int ending_address) {
return __OSValidateAddressSpaceRange(1, (void *) starting_address, ending_address - starting_address + 1); return __OSValidateAddressSpaceRange(1, (void *) starting_address, ending_address - starting_address + 1);

View File

@ -1,6 +1,6 @@
#include "disassembler.h" #include "disassembler.h"
#include "assertions.h" #include "../utils/assertions.h"
#include "dynamic_libs/os_functions.h" #include "../dynamic_libs/os_functions.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>

View File

@ -140,5 +140,5 @@ unsigned char program_exception_cb(void *context) {
void setup_os_exceptions() { void setup_os_exceptions() {
OSSetExceptionCallback(OS_EXCEPTION_DSI, &dsi_exception_cb); OSSetExceptionCallback(OS_EXCEPTION_DSI, &dsi_exception_cb);
OSSetExceptionCallback(OS_EXCEPTION_ISI, &isi_exception_cb); OSSetExceptionCallback(OS_EXCEPTION_ISI, &isi_exception_cb);
// OSSetExceptionCallback(OS_EXCEPTION_PROGRAM, &program_exception_cb); OSSetExceptionCallback(OS_EXCEPTION_PROGRAM, &program_exception_cb);
} }

View File

@ -58,7 +58,6 @@ static inline int getIABRMatch(void *interruptedContext) {
unsigned char breakPointHandler(void *interruptedContext); unsigned char breakPointHandler(void *interruptedContext);
void registerBreakPointHandler() { void registerBreakPointHandler() {
log_init(COMPUTER_IP_ADDRESS);
log_print("Registering breakpoint handler...\n"); log_print("Registering breakpoint handler...\n");
// TODO Not working, never called? // TODO Not working, never called?
// OSSetExceptionCallback((u8) OS_EXCEPTION_DSI, &breakPointHandler); // OSSetExceptionCallback((u8) OS_EXCEPTION_DSI, &breakPointHandler);
@ -80,7 +79,6 @@ void forceDebuggerPresent() {
}*/ }*/
static inline void setupBreakpointSupport() { static inline void setupBreakpointSupport() {
log_init(COMPUTER_IP_ADDRESS);
/*log_print("Clear and enable...\n"); /*log_print("Clear and enable...\n");
__OSClearAndEnableInterrupt(); __OSClearAndEnableInterrupt();
log_print("Restore...\n"); log_print("Restore...\n");
@ -95,7 +93,6 @@ static inline void setupBreakpointSupport() {
void setDataBreakpoint(int address, bool read, bool write) { void setDataBreakpoint(int address, bool read, bool write) {
setupBreakpointSupport(); setupBreakpointSupport();
log_init(COMPUTER_IP_ADDRESS);
log_print("Setting DABR...\n"); log_print("Setting DABR...\n");
OSSetDABR(1, address, read, write); OSSetDABR(1, address, read, write);
log_print("DABR set\n"); log_print("DABR set\n");
@ -125,8 +122,6 @@ void setInstructionBreakpoint(unsigned int address) {
} }
unsigned char breakPointHandler(void *interruptedContext) { unsigned char breakPointHandler(void *interruptedContext) {
log_init(COMPUTER_IP_ADDRESS);
// Check for data breakpoints // Check for data breakpoints
int dataAddress = getDABRAddress(interruptedContext); int dataAddress = getDABRAddress(interruptedContext);
if (OSIsAddressValid((const void *) dataAddress)) { if (OSIsAddressValid((const void *) dataAddress)) {

View File

@ -1,10 +1,10 @@
#pragma once #pragma once
#include "kernel/syscalls.h" #include "../kernel/syscalls.h"
#include "assertions.h" #include "../utils/assertions.h"
#include "dynamic_libs/os_functions.h" #include "../dynamic_libs/os_functions.h"
#include "tcp_gecko.h" #include "../tcp_gecko.h"
#include "utils/logger.h" #include "../utils/logger.h"
unsigned char *kernelCopyBuffer[sizeof(int)]; unsigned char *kernelCopyBuffer[sizeof(int)];
@ -17,7 +17,8 @@ void kernelCopyData(unsigned char *destinationBuffer, unsigned char *sourceBuffe
} }
memcpy(kernelCopyBufferOld, sourceBuffer, length); memcpy(kernelCopyBufferOld, sourceBuffer, length);
SC0x25_KernelCopyData((unsigned int) OSEffectiveToPhysical(destinationBuffer), (unsigned int) &kernelCopyBufferOld, length); SC0x25_KernelCopyData((unsigned int) OSEffectiveToPhysical(destinationBuffer), (unsigned int) &kernelCopyBufferOld,
length);
DCFlushRange(destinationBuffer, (u32) length); DCFlushRange(destinationBuffer, (u32) length);
} }
@ -65,6 +66,8 @@ int kernelCopyService(int argc, void *argv) {
*(((int *) KERNEL_COPY_SOURCE_ADDRESS) + 1) = 0; *(((int *) KERNEL_COPY_SOURCE_ADDRESS) + 1) = 0;
} }
} }
return 0;
} }
void startKernelCopyService() { void startKernelCopyService() {
@ -84,8 +87,8 @@ void startKernelCopyService() {
#define MINIMUM_KERNEL_COMPARE_LENGTH 4 #define MINIMUM_KERNEL_COMPARE_LENGTH 4
#define KERNEL_MEMORY_COMPARE_STEP_SIZE 1 #define KERNEL_MEMORY_COMPARE_STEP_SIZE 1
int kernelMemoryCompare(const void *sourceBuffer, int kernelMemoryCompare(const char *sourceBuffer,
const void *destinationBuffer, const char *destinationBuffer,
int length) { int length) {
if (length < MINIMUM_KERNEL_COMPARE_LENGTH) { if (length < MINIMUM_KERNEL_COMPARE_LENGTH) {
ASSERT_MINIMUM_HOLDS(length, MINIMUM_KERNEL_COMPARE_LENGTH, "length"); ASSERT_MINIMUM_HOLDS(length, MINIMUM_KERNEL_COMPARE_LENGTH, "length");
@ -111,3 +114,17 @@ int kernelMemoryCompare(const void *sourceBuffer,
return kern_read(sourceBuffer) - kern_read(destinationBuffer); return kern_read(sourceBuffer) - kern_read(destinationBuffer);
} }
void executeAssembly(unsigned char buffer[], unsigned int size) {
// Write the assembly to an executable code region
int destinationAddress = 0x10000000 - size;
kernelCopyData((unsigned char *) destinationAddress, buffer, size);
// Execute the assembly from there
void (*function)() = (void (*)()) destinationAddress;
function();
// Clear the memory contents again
memset((void *) buffer, 0, size);
kernelCopyData((unsigned char *) destinationAddress, buffer, size);
}

View File

@ -1,9 +1,9 @@
#pragma once #pragma once
#include "utils/logger.h" #include "../utils/logger.h"
#include "assertions.h" #include "../utils/assertions.h"
#include "dynamic_libs/os_functions.h" #include "../dynamic_libs/os_functions.h"
#include "common/fs_defs.h" #include "../common/fs_defs.h"
#include "kernel.h" #include "kernel.h"
int (*AVMGetDRCScanMode)(int); int (*AVMGetDRCScanMode)(int);
@ -22,7 +22,7 @@ unsigned long getConsoleStatePatchAddress() {
log_print("Acquired!\n"); log_print("Acquired!\n");
} }
return (unsigned long) (AVMGetDRCScanMode + 0x44); return (unsigned long) ((char *) AVMGetDRCScanMode + 0x44);
} }
typedef enum { typedef enum {

View File

@ -2,11 +2,8 @@
#include "../utils/linked_list.h" #include "../utils/linked_list.h"
#include "../dynamic_libs/os_functions.h" #include "../dynamic_libs/os_functions.h"
#include "../utils/logger.h" #include "../utils/logger.h"
#include "../main.h"
struct node *getAllThreads() { struct node *getAllThreads() {
log_init(COMPUTER_IP_ADDRESS);
struct node *threads = NULL; struct node *threads = NULL;
int currentThreadAddress = OSGetCurrentThread(); int currentThreadAddress = OSGetCurrentThread();
log_printf("Thread address: %08x\n", currentThreadAddress); log_printf("Thread address: %08x\n", currentThreadAddress);

View File

@ -3,7 +3,7 @@
#include "../dynamic_libs/os_functions.h" #include "../dynamic_libs/os_functions.h"
#include "../utils/logger.h" #include "../utils/logger.h"
#include "../kernel.h" #include "kernel.h"
#include "../common/kernel_types.h" #include "../common/kernel_types.h"
void writeCode(u32 address, u32 instruction) { void writeCode(u32 address, u32 instruction) {
@ -13,7 +13,7 @@ void writeCode(u32 address, u32 instruction) {
ICInvalidateRange(pointer, 4); ICInvalidateRange(pointer, 4);
} }
void patchFunction(void *function, char *patchBytes, unsigned int patchBytesSize, int functionOffset) { void patchFunction(char *function, char *patchBytes, unsigned int patchBytesSize, int functionOffset) {
log_print("Patching function...\n"); log_print("Patching function...\n");
void *patchAddress = function + functionOffset; void *patchAddress = function + functionOffset;
log_printf("Patch address: %p\n", patchAddress); log_printf("Patch address: %p\n", patchAddress);

View File

@ -13,12 +13,12 @@
#include "dynamic_libs/gx2_functions.h" #include "dynamic_libs/gx2_functions.h"
#include "dynamic_libs/fs_functions.h" #include "dynamic_libs/fs_functions.h"
#include "utils/logger.h" #include "utils/logger.h"
#include "system/memory.h"
#include "system/hardware_breakpoints.h" #include "system/hardware_breakpoints.h"
#include "utils/linked_list.h" #include "utils/linked_list.h"
#include "address.h" #include "system/address.h"
#include "system/stack.h" #include "system/stack.h"
#include "pause.h" #include "system/pause.h"
#include "utils/sd_ip_reader.hpp"
void *client; void *client;
void *commandBlock; void *commandBlock;
@ -55,7 +55,7 @@ struct pygecko_bss_t {
#define COMMAND_FOLLOW_POINTER 0x60 #define COMMAND_FOLLOW_POINTER 0x60
#define COMMAND_REMOTE_PROCEDURE_CALL 0x70 #define COMMAND_REMOTE_PROCEDURE_CALL 0x70
#define COMMAND_GET_SYMBOL 0x71 #define COMMAND_GET_SYMBOL 0x71
#define COMMAND_MEMORY_SEARCH 0x72 #define COMMAND_MEMORY_SEARCH_32 0x72
#define COMMAND_ADVANCED_MEMORY_SEARCH 0x73 #define COMMAND_ADVANCED_MEMORY_SEARCH 0x73
#define COMMAND_EXECUTE_ASSEMBLY 0x81 #define COMMAND_EXECUTE_ASSEMBLY 0x81
#define COMMAND_PAUSE_CONSOLE 0x82 #define COMMAND_PAUSE_CONSOLE 0x82
@ -111,7 +111,7 @@ int flush
// ########## Being socket_functions.h ############ // ########## Being socket_functions.h ############
static int recvwait(struct pygecko_bss_t *bss, int sock, void *buffer, int len) { static int recvwait(struct pygecko_bss_t *bss, int sock, unsigned char *buffer, int len) {
int ret; int ret;
while (len > 0) { while (len > 0) {
ret = recv(sock, buffer, len, 0); ret = recv(sock, buffer, len, 0);
@ -145,7 +145,7 @@ static int checkbyte(int sock) {
return buffer[0]; return buffer[0];
} }
static int sendwait(struct pygecko_bss_t *bss, int sock, const void *buffer, int len) { static int sendwait(struct pygecko_bss_t *bss, int sock, unsigned char *buffer, int len) {
int ret; int ret;
while (len > 0) { while (len > 0) {
ret = send(sock, buffer, len, 0); ret = send(sock, buffer, len, 0);
@ -168,10 +168,10 @@ static int sendByte(struct pygecko_bss_t *bss, int sock, unsigned char byte) {
void receiveString(struct pygecko_bss_t *bss, void receiveString(struct pygecko_bss_t *bss,
int clientfd, int clientfd,
char *stringBuffer, unsigned char *stringBuffer,
int bufferSize) { int bufferSize) {
// Receive the string length // Receive the string length
char lengthBuffer[4] = {0}; unsigned char lengthBuffer[4] = {0};
int ret = recvwait(bss, clientfd, lengthBuffer, 4); int ret = recvwait(bss, clientfd, lengthBuffer, 4);
ASSERT_FUNCTION_SUCCEEDED(ret, "recvwait (string length)") ASSERT_FUNCTION_SUCCEEDED(ret, "recvwait (string length)")
int stringLength = ((int *) lengthBuffer)[0]; int stringLength = ((int *) lengthBuffer)[0];
@ -228,7 +228,7 @@ void considerInitializingFileSystem() {
void reportIllegalCommandByte(int commandByte) { void reportIllegalCommandByte(int commandByte) {
char errorBuffer[ERROR_BUFFER_SIZE]; char errorBuffer[ERROR_BUFFER_SIZE];
__os_snprintf(errorBuffer, ERROR_BUFFER_SIZE, __os_snprintf(errorBuffer, ERROR_BUFFER_SIZE,
"Illegal command byte received: 0x%02x\nServer Version: %s\nIf you see this, you most likely have to update your TCP Gecko Installer.", "Illegal command byte received: 0x%02x\nServer Version: %s\nIf you see this,\nplease report this bug.",
commandByte, SERVER_VERSION); commandByte, SERVER_VERSION);
OSFatal(errorBuffer); OSFatal(errorBuffer);
} }
@ -514,7 +514,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
&& (currentAddress < endingAddress)) { && (currentAddress < endingAddress)) {
int value = *(int *) currentAddress; int value = *(int *) currentAddress;
((int *) buffer)[currentIntegerIndex++] = value; ((int *) buffer)[currentIntegerIndex++] = value;
char *opCodeBuffer = malloc(bufferSize); char *opCodeBuffer = (char *) malloc(bufferSize);
bool status = DisassemblePPCOpcode((u32 *) currentAddress, opCodeBuffer, (u32) bufferSize, bool status = DisassemblePPCOpcode((u32 *) currentAddress, opCodeBuffer, (u32) bufferSize,
OSGetSymbolName, OSGetSymbolName,
(u32) disassemblerOptions); (u32) disassemblerOptions);
@ -536,7 +536,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
} }
int bytesToSend = currentIntegerIndex * integerSize; int bytesToSend = currentIntegerIndex * integerSize;
ret = sendwait(bss, clientfd, &bytesToSend, 4); ret = sendwait(bss, clientfd, (unsigned char *) &bytesToSend, 4);
ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (Buffer size)") ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (Buffer size)")
// VALUE(4)|STATUS(4)|LENGTH(4)|DISASSEMBLED(LENGTH) // VALUE(4)|STATUS(4)|LENGTH(4)|DISASSEMBLED(LENGTH)
@ -545,7 +545,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
} }
int bytesToSend = 0; int bytesToSend = 0;
ret = sendwait(bss, clientfd, &bytesToSend, 4); ret = sendwait(bss, clientfd, (unsigned char *) &bytesToSend, 4);
ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (No more bytes)") ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (No more bytes)")
break; break;
@ -656,7 +656,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
u32 image_size = surface.image_size; u32 image_size = surface.image_size;
// Send the image size so that the client knows how much to read // Send the image size so that the client knows how much to read
ret = sendwait(bss, clientfd, &image_size, 4); ret = sendwait(bss, clientfd, (unsigned char *) &image_size, 4);
ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (image size)") ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (image size)")
unsigned int imageBytesSent = 0; unsigned int imageBytesSent = 0;
@ -711,7 +711,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
} }
case COMMAND_READ_FILE: { case COMMAND_READ_FILE: {
char file_path[FS_MAX_FULLPATH_SIZE] = {0}; char file_path[FS_MAX_FULLPATH_SIZE] = {0};
receiveString(bss, clientfd, file_path, FS_MAX_FULLPATH_SIZE); receiveString(bss, clientfd, (unsigned char *) file_path, FS_MAX_FULLPATH_SIZE);
considerInitializingFileSystem(); considerInitializingFileSystem();
@ -747,7 +747,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
ASSERT_FUNCTION_SUCCEEDED(bytesRead, "FSReadFile") ASSERT_FUNCTION_SUCCEEDED(bytesRead, "FSReadFile")
// Send file bytes // Send file bytes
ret = sendwait(bss, clientfd, fileBuffer, bytesRead); ret = sendwait(bss, clientfd, (unsigned char *) fileBuffer, bytesRead);
ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (file buffer)") ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (file buffer)")
totalBytesRead += bytesRead; totalBytesRead += bytesRead;
@ -768,7 +768,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
} }
case COMMAND_READ_DIRECTORY: { case COMMAND_READ_DIRECTORY: {
char directory_path[FS_MAX_FULLPATH_SIZE] = {0}; char directory_path[FS_MAX_FULLPATH_SIZE] = {0};
receiveString(bss, clientfd, directory_path, FS_MAX_FULLPATH_SIZE); receiveString(bss, clientfd, (unsigned char *) directory_path, FS_MAX_FULLPATH_SIZE);
considerInitializingFileSystem(); considerInitializingFileSystem();
@ -793,7 +793,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (data coming)") ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (data coming)")
// Send the struct // Send the struct
ret = sendwait(bss, clientfd, &entry, entrySize); ret = sendwait(bss, clientfd, (unsigned char *) &entry, entrySize);
ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (directory entry)") ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (directory entry)")
} }
@ -819,7 +819,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
// Receive the file path // Receive the file path
char file_path[FS_MAX_FULLPATH_SIZE] = {0}; char file_path[FS_MAX_FULLPATH_SIZE] = {0};
receiveString(bss, clientfd, file_path, FS_MAX_FULLPATH_SIZE); receiveString(bss, clientfd, (unsigned char *) file_path, FS_MAX_FULLPATH_SIZE);
considerInitializingFileSystem(); considerInitializingFileSystem();
@ -843,19 +843,19 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
ASSERT_ALLOCATED(fileBuffer, "File buffer") ASSERT_ALLOCATED(fileBuffer, "File buffer")
// Send the maximum file buffer size // Send the maximum file buffer size
ret = sendwait(bss, clientfd, &file_buffer_size, 4); ret = sendwait(bss, clientfd, (unsigned char *) &file_buffer_size, 4);
ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (maximum file buffer size)") ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (maximum file buffer size)")
while (true) { while (true) {
// Receive the data bytes length // Receive the data bytes length
unsigned int dataLength; unsigned int dataLength;
ret = recvwait(bss, clientfd, &dataLength, 4); ret = recvwait(bss, clientfd, (unsigned char *) &dataLength, 4);
ASSERT_FUNCTION_SUCCEEDED(ret, "recvwait (File bytes length)") ASSERT_FUNCTION_SUCCEEDED(ret, "recvwait (File bytes length)")
ASSERT_MAXIMUM_HOLDS(file_buffer_size, dataLength, "File buffer overrun attempted") ASSERT_MAXIMUM_HOLDS(file_buffer_size, dataLength, "File buffer overrun attempted")
if (dataLength > 0) { if (dataLength > 0) {
// Receive the data // Receive the data
ret = recvwait(bss, clientfd, fileBuffer, dataLength); ret = recvwait(bss, clientfd, (unsigned char *) fileBuffer, dataLength);
ASSERT_FUNCTION_SUCCEEDED(ret, "recvwait (File buffer)") ASSERT_FUNCTION_SUCCEEDED(ret, "recvwait (File buffer)")
// Write the data (and advance file handle position implicitly) // Write the data (and advance file handle position implicitly)
@ -888,7 +888,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
break; break;
} }
case COMMAND_IOSU_HAX_READ_FILE: { case COMMAND_IOSU_HAX_READ_FILE: {
log_print("COMMAND_IOSUHAX_READ_FILE"); /*log_print("COMMAND_IOSUHAX_READ_FILE");
// TODO Crashes console on this call // TODO Crashes console on this call
int returnValue = IOSUHAX_Open(NULL); int returnValue = IOSUHAX_Open(NULL);
@ -965,7 +965,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
returnValue = IOSUHAX_Close(); returnValue = IOSUHAX_Close();
log_printf("IOSUHAX_Close: %i", returnValue); log_printf("IOSUHAX_Close: %i", returnValue);
IOSUHAX_OPEN_FAILED: IOSUHAX_OPEN_FAILED:*/
break; break;
} }
@ -1039,7 +1039,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
ASSERT_FUNCTION_SUCCEEDED(ret, "nn_act_Finalize"); ASSERT_FUNCTION_SUCCEEDED(ret, "nn_act_Finalize");
// Send it // Send it
ret = sendwait(bss, clientfd, &persistentIdentifier, 4); ret = sendwait(bss, clientfd, (unsigned char *) &persistentIdentifier, 4);
ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (persistent identifier)") ASSERT_FUNCTION_SUCCEEDED(ret, "sendwait (persistent identifier)")
break; break;
@ -1113,7 +1113,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
ret = recvwait(bss, clientfd, buffer, 4 + 8 * 4); ret = recvwait(bss, clientfd, buffer, 4 + 8 * 4);
CHECK_ERROR(ret < 0); CHECK_ERROR(ret < 0);
fun = ((void **) buffer)[0]; fun = (long long int (*)(int, int, int, int, int, int, int, int)) ((void **) buffer)[0];
r3 = ((int *) buffer)[1]; r3 = ((int *) buffer)[1];
r4 = ((int *) buffer)[2]; r4 = ((int *) buffer)[2];
r5 = ((int *) buffer)[3]; r5 = ((int *) buffer)[3];
@ -1139,15 +1139,15 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
CHECK_ERROR(ret < 0) CHECK_ERROR(ret < 0)
/* Identify the RPL name and symbol name */ /* Identify the RPL name and symbol name */
char *rplname = (char *) &((int *) buffer)[2]; char *rplName = (char *) &((int *) buffer)[2];
char *symname = (char *) (&buffer[0] + ((int *) buffer)[1]); char *symbolName = (char *) (&buffer[0] + ((int *) buffer)[1]);
/* Get the symbol and store it in the buffer */ /* Get the symbol and store it in the buffer */
unsigned int module_handle, function_address; unsigned int module_handle, function_address;
OSDynLoad_Acquire(rplname, &module_handle); OSDynLoad_Acquire(rplName, &module_handle);
char data = (char) recvbyte(bss, clientfd); char data = (char) recvbyte(bss, clientfd);
OSDynLoad_FindExport(module_handle, data, symname, &function_address); OSDynLoad_FindExport(module_handle, data, symbolName, &function_address);
((int *) buffer)[0] = (int) function_address; ((int *) buffer)[0] = (int) function_address;
ret = sendwait(bss, clientfd, buffer, 4); ret = sendwait(bss, clientfd, buffer, 4);
@ -1155,7 +1155,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
break; break;
} }
case COMMAND_MEMORY_SEARCH: { case COMMAND_MEMORY_SEARCH_32: {
ret = recvwait(bss, clientfd, buffer, sizeof(int) * 3); ret = recvwait(bss, clientfd, buffer, sizeof(int) * 3);
CHECK_ERROR(ret < 0); CHECK_ERROR(ret < 0);
int address = ((int *) buffer)[0]; int address = ((int *) buffer)[0];
@ -1178,7 +1178,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
} }
case COMMAND_ADVANCED_MEMORY_SEARCH: { case COMMAND_ADVANCED_MEMORY_SEARCH: {
// Receive the initial data // Receive the initial data
ret = recvwait(bss, clientfd, buffer, 4 * 6); ret = recvwait(bss, clientfd, buffer, 6 * sizeof(int));
ASSERT_FUNCTION_SUCCEEDED(ret, "recvwait (memory search information)") ASSERT_FUNCTION_SUCCEEDED(ret, "recvwait (memory search information)")
int bufferIndex = 0; int bufferIndex = 0;
int startingAddress = ((int *) buffer)[bufferIndex++]; int startingAddress = ((int *) buffer)[bufferIndex++];
@ -1190,7 +1190,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
// Receive the search bytes // Receive the search bytes
char searchBytes[searchBytesCount]; char searchBytes[searchBytesCount];
ret = recvwait(bss, clientfd, searchBytes, searchBytesCount); ret = recvwait(bss, clientfd, (unsigned char *) searchBytes, searchBytesCount);
ASSERT_FUNCTION_SUCCEEDED(ret, "recvwait (memory search bytes)") ASSERT_FUNCTION_SUCCEEDED(ret, "recvwait (memory search bytes)")
int iterationIncrement = aligned ? searchBytesCount : 1; int iterationIncrement = aligned ? searchBytesCount : 1;
@ -1204,7 +1204,7 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
int comparisonResult; int comparisonResult;
if (kernelRead) { if (kernelRead) {
comparisonResult = kernelMemoryCompare((void *) currentAddress, searchBytes, searchBytesCount); comparisonResult = kernelMemoryCompare((char *) currentAddress, searchBytes, searchBytesCount);
} else { } else {
comparisonResult = memcmp((void *) currentAddress, searchBytes, searchBytesCount); comparisonResult = memcmp((void *) currentAddress, searchBytes, searchBytesCount);
} }
@ -1228,20 +1228,8 @@ static int processCommands(struct pygecko_bss_t *bss, int clientfd) {
break; break;
} }
case COMMAND_EXECUTE_ASSEMBLY: { case COMMAND_EXECUTE_ASSEMBLY: {
// Receive the assembly receiveString(bss, clientfd, (unsigned char *) buffer, DATA_BUFFER_SIZE);
receiveString(bss, clientfd, (char *) buffer, DATA_BUFFER_SIZE); executeAssembly(buffer, DATA_BUFFER_SIZE);
// Write the assembly to an executable code region
int destinationAddress = 0x10000000 - DATA_BUFFER_SIZE;
kernelCopyData((unsigned char *) destinationAddress, buffer, DATA_BUFFER_SIZE);
// Execute the assembly from there
void (*function)() = (void (*)()) destinationAddress;
function();
// Clear the memory contents again
memset((void *) buffer, 0, DATA_BUFFER_SIZE);
kernelCopyData((unsigned char *) destinationAddress, buffer, DATA_BUFFER_SIZE);
break; break;
} }
@ -1408,8 +1396,7 @@ static int runTCPGeckoServer(int argc, void *argv) {
setup_os_exceptions(); setup_os_exceptions();
socket_lib_init(); socket_lib_init();
initializeUDPLog();
log_init(COMPUTER_IP_ADDRESS);
while (true) { while (true) {
socketAddress.sin_family = AF_INET; socketAddress.sin_family = AF_INET;
@ -1460,18 +1447,18 @@ static int runTCPGeckoServer(int argc, void *argv) {
} }
static int startTCPGeckoThread(int argc, void *argv) { static int startTCPGeckoThread(int argc, void *argv) {
log_init(COMPUTER_IP_ADDRESS);
log_print("Starting TCP Gecko thread...\n"); log_print("Starting TCP Gecko thread...\n");
// Run the TCP Gecko Installer server // Run the TCP Gecko Installer server
struct pygecko_bss_t *bss; struct pygecko_bss_t *bss;
bss = memalign(0x40, sizeof(struct pygecko_bss_t)); bss = (struct pygecko_bss_t *) memalign(0x40, sizeof(struct pygecko_bss_t));
if (bss == 0) if (bss == 0)
return 0; return 0;
memset(bss, 0, sizeof(struct pygecko_bss_t)); memset(bss, 0, sizeof(struct pygecko_bss_t));
if (OSCreateThread(&bss->thread, runTCPGeckoServer, 1, bss, (u32) bss->stack + sizeof(bss->stack), if (OSCreateThread(&bss->thread, (s32 (*)(s32, void *)) runTCPGeckoServer, 1, bss,
(u32) bss->stack + sizeof(bss->stack),
sizeof(bss->stack), 0, sizeof(bss->stack), 0,
0xc) == 1) { 0xc) == 1) {
OSResumeThread(&bss->thread); OSResumeThread(&bss->thread);
@ -1499,7 +1486,6 @@ static int startTCPGeckoThread(int argc, void *argv) {
} }
void startTCPGecko() { void startTCPGecko() {
log_init(COMPUTER_IP_ADDRESS);
log_print("Starting TCP Gecko...\n"); log_print("Starting TCP Gecko...\n");
// Force the debugger to be initialized by default // Force the debugger to be initialized by default

View File

@ -0,0 +1,40 @@
#include "sd_ip_reader.hpp"
char ipFromSd[16];
bool hasReadIP = false;
void initializeUDPLog() {
if (!hasReadIP) {
log_printf("Reading ip from sd card\n");
hasReadIP = true;
std::string ipFilePath = std::string(SD_PATH) + WIIU_PATH + "/" + IP_TXT;
CFile file(ipFilePath, CFile::ReadOnly);
if (!file.isOpen()) {
log_printf("File %s not found, using hard-coded\n", ipFilePath.c_str());
log_init(COMPUTER_IP_ADDRESS);
return;
}
std::string strBuffer;
strBuffer.resize(file.size());
file.read((u8 *) &strBuffer[0], strBuffer.size());
if (strBuffer.length() >= sizeof(ipFromSd)) {
log_printf("Loading ip from sd failed. String was too long: %s\n", strBuffer.c_str());
return;
}
memcpy(ipFromSd, strBuffer.c_str(), strBuffer.length());
ipFromSd[strBuffer.length()] = 0;
log_printf("Successfully read ip from sd! ip is: %s\n", ipFromSd);
log_init(ipFromSd);
}
if (strlen(ipFromSd) > 0) {
log_init(ipFromSd);
}
}

View File

@ -0,0 +1,9 @@
#pragma once
#include "logger.h"
#include "../common/common.h"
#include "../fs/CFile.hpp"
#define IP_TXT "ip.txt"
void initializeUDPLog();

Binary file not shown.