Implement almost working SD card cheats

This commit is contained in:
BullyWiiPlaza 2017-08-27 12:58:57 +02:00
parent e354bd4dc2
commit 738a750593
5 changed files with 836 additions and 861 deletions

View File

@ -5,10 +5,10 @@
#include <fcntl.h>
#include "common/fs_defs.h"
#include "../dynamic_libs/fs_functions.h"
#include "../utils/logger.h"
int MountFS(void *pClient, void *pCmd, char **mount_path)
{
int MountFS(void *pClient, void *pCmd, char **mount_path) {
int result = -1;
void *mountSrc = malloc(FS_MOUNT_SOURCE_SIZE);
@ -25,31 +25,31 @@ int MountFS(void *pClient, void *pCmd, char **mount_path)
memset(mountPath, 0, FS_MAX_MOUNTPATH_SIZE);
// Mount sdcard
if (FSGetMountSource(pClient, pCmd, FS_SOURCETYPE_EXTERNAL, mountSrc, -1) == 0)
{
result = FSGetMountSource(pClient, pCmd, FS_SOURCETYPE_EXTERNAL, mountSrc, -1);
if (result == 0) {
result = FSMount(pClient, pCmd, mountSrc, mountPath, FS_MAX_MOUNTPATH_SIZE, -1);
if ((result == 0) && mount_path) {
*mount_path = (char *) malloc(strlen(mountPath) + 1);
if (*mount_path)
strcpy(*mount_path, mountPath);
} else {
log_printf("FSMount error: %i\n", result);
}
} else {
log_printf("FSGetMountSource error: %i\n", result);
}
free(mountPath);
free(mountSrc);
return result;
}
int UmountFS(void *pClient, void *pCmd, const char *mountPath)
{
int result = -1;
result = FSUnmount(pClient, pCmd, mountPath, -1);
return result;
}
int LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size)
{
int UmountFS(void *pClient, void *pCmd, const char *mountPath) {
return FSUnmount(pClient, pCmd, mountPath, -1);
}
int LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size) {
// Always initialize input
*inbuffer = NULL;
if (size)
@ -63,8 +63,7 @@ int LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size)
lseek(iFd, 0, SEEK_SET);
u8 *buffer = (u8 *) malloc(filesize);
if (buffer == NULL)
{
if (buffer == NULL) {
close(iFd);
return -2;
}
@ -73,8 +72,7 @@ int LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size)
u32 done = 0;
int readBytes = 0;
while(done < filesize)
{
while (done < filesize) {
if (done + blocksize > filesize) {
blocksize = filesize - done;
}
@ -86,8 +84,7 @@ int LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size)
close(iFd);
if (done != filesize)
{
if (done != filesize) {
free(buffer);
return -3;
}
@ -101,8 +98,7 @@ int LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size)
return filesize;
}
int CheckFile(const char * filepath)
{
int CheckFile(const char *filepath) {
if (!filepath)
return 0;
@ -115,8 +111,7 @@ int CheckFile(const char * filepath)
dirnoslash[strlen(dirnoslash) - 1] = '\0';
char *notRoot = strrchr(dirnoslash, '/');
if(!notRoot)
{
if (!notRoot) {
strcat(dirnoslash, "/");
}
@ -126,8 +121,7 @@ int CheckFile(const char * filepath)
return 0;
}
int CreateSubfolder(const char * fullpath)
{
int CreateSubfolder(const char *fullpath) {
if (!fullpath)
return 0;
@ -137,24 +131,19 @@ int CreateSubfolder(const char * fullpath)
strcpy(dirnoslash, fullpath);
int pos = strlen(dirnoslash) - 1;
while(dirnoslash[pos] == '/')
{
while (dirnoslash[pos] == '/') {
dirnoslash[pos] = '\0';
pos--;
}
if(CheckFile(dirnoslash))
{
if (CheckFile(dirnoslash)) {
return 1;
}
else
{
} else {
char parentpath[strlen(dirnoslash) + 2];
strcpy(parentpath, dirnoslash);
char *ptr = strrchr(parentpath, '/');
if(!ptr)
{
if (!ptr) {
//!Device root directory (must be with '/')
strcat(parentpath, "/");
struct stat filestat;
@ -173,8 +162,7 @@ int CreateSubfolder(const char * fullpath)
if (!result)
return 0;
if (mkdir(dirnoslash, 0777) == -1)
{
if (mkdir(dirnoslash, 0777) == -1) {
return 0;
}

View File

@ -6,6 +6,7 @@ extern "C" {
#endif
#include <gctypes.h>
#include "../common/fs_defs.h"
int MountFS(void *pClient, void *pCmd, char **mount_path);
int UmountFS(void *pClient, void *pCmd, const char *mountPath);

View File

@ -32,6 +32,7 @@
#include "../dynamic_libs/fs_functions.h"
#include "../dynamic_libs/os_functions.h"
#include "fs_utils.h"
#include "../utils/logger.h"
#define FS_ALIGNMENT 0x40
#define FS_ALIGN(x) (((x) + FS_ALIGNMENT - 1) & ~(FS_ALIGNMENT - 1))
@ -61,8 +62,7 @@ typedef struct _sd_fat_dir_entry_t {
int dirHandle;
} sd_fat_dir_entry_t;
static sd_fat_private_t *sd_fat_get_device_data(const char *path)
{
static sd_fat_private_t *sd_fat_get_device_data(const char *path) {
const devoptab_t *devoptab = NULL;
char name[128] = {0};
int i;
@ -87,8 +87,7 @@ static sd_fat_private_t *sd_fat_get_device_data(const char *path)
return NULL;
}
static char *sd_fat_real_path (const char *path, sd_fat_private_t *dev)
{
static char *sd_fat_real_path(const char *path, sd_fat_private_t *dev) {
// Sanity check
if (!path)
return NULL;
@ -109,8 +108,7 @@ static char *sd_fat_real_path (const char *path, sd_fat_private_t *dev)
return new_name;
}
static int sd_fat_open_r (struct _reent *r, void *fileStruct, const char *path, int flags, int mode)
{
static int sd_fat_open_r(struct _reent *r, void *fileStruct, const char *path, int flags, int mode) {
sd_fat_private_t *dev = sd_fat_get_device_data(path);
if (!dev) {
r->_errno = ENODEV;
@ -160,8 +158,7 @@ static int sd_fat_open_r (struct _reent *r, void *fileStruct, const char *path,
free(real_path);
if(result == 0)
{
if (result == 0) {
FSStat stats;
result = FSGetStatFile(dev->pClient, dev->pCmd, fd, &stats, -1);
if (result != 0) {
@ -183,8 +180,7 @@ static int sd_fat_open_r (struct _reent *r, void *fileStruct, const char *path,
}
static int sd_fat_close_r (struct _reent *r, int fd)
{
static int sd_fat_close_r(struct _reent *r, int fd) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *) fd;
if (!file->dev) {
r->_errno = ENODEV;
@ -197,16 +193,14 @@ static int sd_fat_close_r (struct _reent *r, int fd)
OSUnlockMutex(file->dev->pMutex);
if(result < 0)
{
if (result < 0) {
r->_errno = result;
return -1;
}
return 0;
}
static off_t sd_fat_seek_r (struct _reent *r, int fd, off_t pos, int dir)
{
static off_t sd_fat_seek_r(struct _reent *r, int fd, off_t pos, int dir) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *) fd;
if (!file->dev) {
r->_errno = ENODEV;
@ -215,8 +209,7 @@ static off_t sd_fat_seek_r (struct _reent *r, int fd, off_t pos, int dir)
OSLockMutex(file->dev->pMutex);
switch(dir)
{
switch (dir) {
case SEEK_SET:
file->pos = pos;
break;
@ -235,24 +228,21 @@ static off_t sd_fat_seek_r (struct _reent *r, int fd, off_t pos, int dir)
OSUnlockMutex(file->dev->pMutex);
if(result == 0)
{
if (result == 0) {
return file->pos;
}
return result;
}
static ssize_t sd_fat_write_r (struct _reent *r, int fd, const char *ptr, size_t len)
{
static ssize_t sd_fat_write_r(struct _reent *r, int fd, const char *ptr, size_t len) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *) fd;
if (!file->dev) {
r->_errno = ENODEV;
return 0;
}
if(!file->write)
{
if (!file->write) {
r->_errno = EACCES;
return 0;
}
@ -272,25 +262,19 @@ static ssize_t sd_fat_write_r (struct _reent *r, int fd, const char *ptr, size_t
size_t done = 0;
while(done < len)
{
while (done < len) {
size_t write_size = (len_aligned < (len - done)) ? len_aligned : (len - done);
memcpy(tmpBuf, ptr + done, write_size);
int result = FSWriteFile(file->dev->pClient, file->dev->pCmd, tmpBuf, 0x01, write_size, file->fd, 0, -1);
if(result < 0)
{
if (result < 0) {
r->_errno = result;
break;
}
else if(result == 0)
{
} else if (result == 0) {
if (write_size > 0)
done = 0;
break;
}
else
{
} else {
done += result;
file->pos += result;
}
@ -301,16 +285,14 @@ static ssize_t sd_fat_write_r (struct _reent *r, int fd, const char *ptr, size_t
return done;
}
static ssize_t sd_fat_read_r (struct _reent *r, int fd, char *ptr, size_t len)
{
static ssize_t sd_fat_read_r(struct _reent *r, int fd, char *ptr, size_t len) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *) fd;
if (!file->dev) {
r->_errno = ENODEV;
return 0;
}
if(!file->read)
{
if (!file->read) {
r->_errno = EACCES;
return 0;
}
@ -330,24 +312,18 @@ static ssize_t sd_fat_read_r (struct _reent *r, int fd, char *ptr, size_t len)
size_t done = 0;
while(done < len)
{
while (done < len) {
size_t read_size = (len_aligned < (len - done)) ? len_aligned : (len - done);
int result = FSReadFile(file->dev->pClient, file->dev->pCmd, tmpBuf, 0x01, read_size, file->fd, 0, -1);
if(result < 0)
{
if (result < 0) {
r->_errno = result;
done = 0;
break;
}
else if(result == 0)
{
} else if (result == 0) {
//! TODO: error on read_size > 0
break;
}
else
{
} else {
memcpy(ptr + done, tmpBuf, read_size);
done += result;
file->pos += result;
@ -360,8 +336,7 @@ static ssize_t sd_fat_read_r (struct _reent *r, int fd, char *ptr, size_t len)
}
static int sd_fat_fstat_r (struct _reent *r, int fd, struct stat *st)
{
static int sd_fat_fstat_r(struct _reent *r, int fd, struct stat *st) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *) fd;
if (!file->dev) {
r->_errno = ENODEV;
@ -398,8 +373,7 @@ static int sd_fat_fstat_r (struct _reent *r, int fd, struct stat *st)
return 0;
}
static int sd_fat_ftruncate_r (struct _reent *r, int fd, off_t len)
{
static int sd_fat_ftruncate_r(struct _reent *r, int fd, off_t len) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *) fd;
if (!file->dev) {
r->_errno = ENODEV;
@ -420,8 +394,7 @@ static int sd_fat_ftruncate_r (struct _reent *r, int fd, off_t len)
return 0;
}
static int sd_fat_fsync_r (struct _reent *r, int fd)
{
static int sd_fat_fsync_r(struct _reent *r, int fd) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *) fd;
if (!file->dev) {
r->_errno = ENODEV;
@ -442,8 +415,7 @@ static int sd_fat_fsync_r (struct _reent *r, int fd)
return 0;
}
static int sd_fat_stat_r (struct _reent *r, const char *path, struct stat *st)
{
static int sd_fat_stat_r(struct _reent *r, const char *path, struct stat *st) {
sd_fat_private_t *dev = sd_fat_get_device_data(path);
if (!dev) {
r->_errno = ENODEV;
@ -493,14 +465,12 @@ static int sd_fat_stat_r (struct _reent *r, const char *path, struct stat *st)
return 0;
}
static int sd_fat_link_r (struct _reent *r, const char *existing, const char *newLink)
{
static int sd_fat_link_r(struct _reent *r, const char *existing, const char *newLink) {
r->_errno = ENOTSUP;
return -1;
}
static int sd_fat_unlink_r (struct _reent *r, const char *name)
{
static int sd_fat_unlink_r(struct _reent *r, const char *name) {
sd_fat_private_t *dev = sd_fat_get_device_data(name);
if (!dev) {
r->_errno = ENODEV;
@ -531,8 +501,7 @@ static int sd_fat_unlink_r (struct _reent *r, const char *name)
return 0;
}
static int sd_fat_chdir_r (struct _reent *r, const char *name)
{
static int sd_fat_chdir_r(struct _reent *r, const char *name) {
sd_fat_private_t *dev = sd_fat_get_device_data(name);
if (!dev) {
r->_errno = ENODEV;
@ -562,8 +531,7 @@ static int sd_fat_chdir_r (struct _reent *r, const char *name)
return 0;
}
static int sd_fat_rename_r (struct _reent *r, const char *oldName, const char *newName)
{
static int sd_fat_rename_r(struct _reent *r, const char *oldName, const char *newName) {
sd_fat_private_t *dev = sd_fat_get_device_data(oldName);
if (!dev) {
r->_errno = ENODEV;
@ -602,8 +570,7 @@ static int sd_fat_rename_r (struct _reent *r, const char *oldName, const char *n
}
static int sd_fat_mkdir_r (struct _reent *r, const char *path, int mode)
{
static int sd_fat_mkdir_r(struct _reent *r, const char *path, int mode) {
sd_fat_private_t *dev = sd_fat_get_device_data(path);
if (!dev) {
r->_errno = ENODEV;
@ -633,8 +600,7 @@ static int sd_fat_mkdir_r (struct _reent *r, const char *path, int mode)
return 0;
}
static int sd_fat_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf)
{
static int sd_fat_statvfs_r(struct _reent *r, const char *path, struct statvfs *buf) {
sd_fat_private_t *dev = sd_fat_get_device_data(path);
if (!dev) {
r->_errno = ENODEV;
@ -697,8 +663,7 @@ static int sd_fat_statvfs_r (struct _reent *r, const char *path, struct statvfs
return 0;
}
static DIR_ITER *sd_fat_diropen_r (struct _reent *r, DIR_ITER *dirState, const char *path)
{
static DIR_ITER *sd_fat_diropen_r(struct _reent *r, DIR_ITER *dirState, const char *path) {
sd_fat_private_t *dev = sd_fat_get_device_data(path);
if (!dev) {
r->_errno = ENODEV;
@ -724,8 +689,7 @@ static DIR_ITER *sd_fat_diropen_r (struct _reent *r, DIR_ITER *dirState, const c
OSUnlockMutex(dev->pMutex);
if(result < 0)
{
if (result < 0) {
r->_errno = result;
return NULL;
}
@ -736,8 +700,7 @@ static DIR_ITER *sd_fat_diropen_r (struct _reent *r, DIR_ITER *dirState, const c
return dirState;
}
static int sd_fat_dirclose_r (struct _reent *r, DIR_ITER *dirState)
{
static int sd_fat_dirclose_r(struct _reent *r, DIR_ITER *dirState) {
sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *) dirState->dirStruct;
if (!dirIter->dev) {
r->_errno = ENODEV;
@ -750,16 +713,14 @@ static int sd_fat_dirclose_r (struct _reent *r, DIR_ITER *dirState)
OSUnlockMutex(dirIter->dev->pMutex);
if(result < 0)
{
if (result < 0) {
r->_errno = result;
return -1;
}
return 0;
}
static int sd_fat_dirreset_r (struct _reent *r, DIR_ITER *dirState)
{
static int sd_fat_dirreset_r(struct _reent *r, DIR_ITER *dirState) {
sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *) dirState->dirStruct;
if (!dirIter->dev) {
r->_errno = ENODEV;
@ -772,16 +733,14 @@ static int sd_fat_dirreset_r (struct _reent *r, DIR_ITER *dirState)
OSUnlockMutex(dirIter->dev->pMutex);
if(result < 0)
{
if (result < 0) {
r->_errno = result;
return -1;
}
return 0;
}
static int sd_fat_dirnext_r (struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *st)
{
static int sd_fat_dirnext_r(struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *st) {
sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *) dirState->dirStruct;
if (!dirIter->dev) {
r->_errno = ENODEV;
@ -793,8 +752,7 @@ static int sd_fat_dirnext_r (struct _reent *r, DIR_ITER *dirState, char *filenam
FSDirEntry *dir_entry = malloc(sizeof(FSDirEntry));
int result = FSReadDir(dirIter->dev->pClient, dirIter->dev->pCmd, dirIter->dirHandle, dir_entry, -1);
if(result < 0)
{
if (result < 0) {
free(dir_entry);
r->_errno = result;
OSUnlockMutex(dirIter->dev->pMutex);
@ -804,8 +762,7 @@ static int sd_fat_dirnext_r (struct _reent *r, DIR_ITER *dirState, char *filenam
// Fetch the current entry
strcpy(filename, dir_entry->name);
if(st)
{
if (st) {
memset(st, 0, sizeof(struct stat));
st->st_mode = (dir_entry->stat.flag & 0x80000000) ? S_IFDIR : S_IFREG;
st->st_nlink = 1;
@ -854,8 +811,7 @@ static const devoptab_t devops_sd_fat = {
NULL /* Device data */
};
static int sd_fat_add_device (const char *name, const char *mount_path, void *pClient, void *pCmd)
{
static int sd_fat_add_device(const char *name, const char *mount_path, void *pClient, void *pCmd) {
devoptab_t *dev = NULL;
char *devname = NULL;
char *devpath = NULL;
@ -926,8 +882,7 @@ static int sd_fat_add_device (const char *name, const char *mount_path, void *pC
return -1;
}
static int sd_fat_remove_device (const char *path, void **pClient, void **pCmd, char **mountPath)
{
static int sd_fat_remove_device(const char *path, void **pClient, void **pCmd, char **mountPath) {
const devoptab_t *devoptab = NULL;
char name[128] = {0};
int i;
@ -946,8 +901,7 @@ static int sd_fat_remove_device (const char *path, void **pClient, void **pCmd,
if (strcmp(name, devoptab->name) == 0) {
devoptab_list[i] = devoptab_list[0];
if(devoptab->deviceData)
{
if (devoptab->deviceData) {
sd_fat_private_t *priv = (sd_fat_private_t *) devoptab->deviceData;
*pClient = priv->pClient;
*pCmd = priv->pCmd;
@ -968,8 +922,7 @@ static int sd_fat_remove_device (const char *path, void **pClient, void **pCmd,
return -1;
}
int mount_sd_fat(const char *path)
{
int mount_sd_fat(const char *path) {
int result = -1;
// get command and client
@ -991,23 +944,26 @@ int mount_sd_fat(const char *path)
char *mountPath = NULL;
if(MountFS(pClient, pCmd, &mountPath) == 0) {
result = MountFS(pClient, pCmd, &mountPath);
if (result == 0) {
log_print("MountFS success\n");
result = sd_fat_add_device(path, mountPath, pClient, pCmd);
log_printf("sd_fat_add_device result: %i\n", result);
free(mountPath);
} else {
log_printf("MountFS error: %i\n", result);
}
return result;
}
int unmount_sd_fat(const char *path)
{
int unmount_sd_fat(const char *path) {
void *pClient = 0;
void *pCmd = 0;
char *mountPath = 0;
int result = sd_fat_remove_device(path, &pClient, &pCmd, &mountPath);
if(result == 0)
{
if (result == 0) {
UmountFS(pClient, pCmd, mountPath);
FSDelClient(pClient);
free(pClient);

View File

@ -23,6 +23,7 @@
#include "patcher/function_patcher_gx2.h"
#include "system/raw_assembly_cheats.h"
#include "fs/fs_utils.h"
#include "fs/sd_fat_devoptab.h"
void *client;
void *commandBlock;
@ -1485,27 +1486,56 @@ static int runTCPGeckoServer(int argc, void *argv) {
#define TITLE_ID_LENGTH 16
#define CODES_FILE_PATH_SIZE (SD_FILE_PATH_HEADER_LENGTH + TITLE_ID_LENGTH + EXTENSION_SIZE)
void applyCheatCodes() {
unsigned char filePath[CODES_FILE_PATH_SIZE];
memset(filePath, '0', sizeof(filePath));
memcpy(filePath, "sd:/codes/", SD_FILE_PATH_HEADER_LENGTH); // File path header
u64 titleID = OSGetTitleID();
char asciiTitleID[TITLE_ID_LENGTH];
snprintf(asciiTitleID, TITLE_ID_LENGTH, "%llX", titleID);
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
u64 cachedTitleID = 0;
unsigned char *codes = NULL;
unsigned int codesSize = 0;
int result = LoadFileToMem((const char *) filePath, &codes, &codesSize);
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) {
// Error, we won't write any codes
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) {
@ -1538,7 +1568,7 @@ static int startTCPGeckoThread(int argc, void *argv) {
while (true) {
usleep(9000);
applyCheatCodes();
considerApplyingSDCheats();
// log_print("Running code handler...\n");
codeHandlerFunction();

Binary file not shown.