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,18 +5,18 @@
#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);
if(!mountSrc)
if (!mountSrc)
return -3;
char* mountPath = (char*) malloc(FS_MAX_MOUNTPATH_SIZE);
if(!mountPath) {
char *mountPath = (char *) malloc(FS_MAX_MOUNTPATH_SIZE);
if (!mountPath) {
free(mountSrc);
return -4;
}
@ -25,34 +25,34 @@ 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)
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)
if (size)
*size = 0;
int iFd = open(filepath, O_RDONLY);
@ -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,21 +72,19 @@ int LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size)
u32 done = 0;
int readBytes = 0;
while(done < filesize)
{
if(done + blocksize > filesize) {
while (done < filesize) {
if (done + blocksize > filesize) {
blocksize = filesize - done;
}
readBytes = read(iFd, buffer + done, blocksize);
if(readBytes <= 0)
if (readBytes <= 0)
break;
done += readBytes;
}
close(iFd);
if (done != filesize)
{
if (done != filesize) {
free(buffer);
return -3;
}
@ -95,28 +92,26 @@ int LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size)
*inbuffer = buffer;
//! sign is optional input
if(size)
if (size)
*size = filesize;
return filesize;
}
int CheckFile(const char * filepath)
{
if(!filepath)
int CheckFile(const char *filepath) {
if (!filepath)
return 0;
struct stat filestat;
char dirnoslash[strlen(filepath)+2];
char dirnoslash[strlen(filepath) + 2];
snprintf(dirnoslash, sizeof(dirnoslash), "%s", filepath);
while(dirnoslash[strlen(dirnoslash)-1] == '/')
dirnoslash[strlen(dirnoslash)-1] = '\0';
while (dirnoslash[strlen(dirnoslash) - 1] == '/')
dirnoslash[strlen(dirnoslash) - 1] = '\0';
char * notRoot = strrchr(dirnoslash, '/');
if(!notRoot)
{
char *notRoot = strrchr(dirnoslash, '/');
if (!notRoot) {
strcat(dirnoslash, "/");
}
@ -126,35 +121,29 @@ int CheckFile(const char * filepath)
return 0;
}
int CreateSubfolder(const char * fullpath)
{
if(!fullpath)
int CreateSubfolder(const char *fullpath) {
if (!fullpath)
return 0;
int result = 0;
char dirnoslash[strlen(fullpath)+1];
char dirnoslash[strlen(fullpath) + 1];
strcpy(dirnoslash, fullpath);
int pos = strlen(dirnoslash)-1;
while(dirnoslash[pos] == '/')
{
int pos = strlen(dirnoslash) - 1;
while (dirnoslash[pos] == '/') {
dirnoslash[pos] = '\0';
pos--;
}
if(CheckFile(dirnoslash))
{
if (CheckFile(dirnoslash)) {
return 1;
}
else
{
char parentpath[strlen(dirnoslash)+2];
} else {
char parentpath[strlen(dirnoslash) + 2];
strcpy(parentpath, dirnoslash);
char * ptr = strrchr(parentpath, '/');
char *ptr = strrchr(parentpath, '/');
if(!ptr)
{
if (!ptr) {
//!Device root directory (must be with '/')
strcat(parentpath, "/");
struct stat filestat;
@ -170,11 +159,10 @@ int CreateSubfolder(const char * fullpath)
result = CreateSubfolder(parentpath);
}
if(!result)
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;
@ -79,7 +79,7 @@ static sd_fat_private_t *sd_fat_get_device_data(const char *path)
devoptab = devoptab_list[i];
if (devoptab && devoptab->name) {
if (strcmp(name, devoptab->name) == 0) {
return (sd_fat_private_t *)devoptab->deviceData;
return (sd_fat_private_t *) devoptab->deviceData;
}
}
}
@ -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;
@ -100,8 +99,8 @@ static char *sd_fat_real_path (const char *path, sd_fat_private_t *dev)
int mount_len = strlen(dev->mount_path);
char *new_name = (char*)malloc(mount_len + strlen(path) + 1);
if(new_name) {
char *new_name = (char *) malloc(mount_len + strlen(path) + 1);
if (new_name) {
strcpy(new_name, dev->mount_path);
strcpy(new_name + mount_len, path);
return new_name;
@ -109,15 +108,14 @@ 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) {
if (!dev) {
r->_errno = ENODEV;
return -1;
}
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fileStruct;
sd_fat_file_state_t *file = (sd_fat_file_state_t *) fileStruct;
file->dev = dev;
// Determine which mode the file is opened for
@ -150,7 +148,7 @@ static int sd_fat_open_r (struct _reent *r, void *fileStruct, const char *path,
OSLockMutex(dev->pMutex);
char *real_path = sd_fat_real_path(path, dev);
if(!path) {
if (!path) {
r->_errno = ENOMEM;
OSUnlockMutex(dev->pMutex);
return -1;
@ -160,11 +158,10 @@ 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) {
if (result != 0) {
FSCloseFile(dev->pClient, dev->pCmd, fd, -1);
r->_errno = result;
OSUnlockMutex(dev->pMutex);
@ -174,7 +171,7 @@ static int sd_fat_open_r (struct _reent *r, void *fileStruct, const char *path,
file->pos = 0;
file->len = stats.size;
OSUnlockMutex(dev->pMutex);
return (int)file;
return (int) file;
}
r->_errno = result;
@ -183,10 +180,9 @@ 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)
{
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
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;
return -1;
}
@ -197,26 +193,23 @@ 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)
{
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
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;
return 0;
}
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)
{
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
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;
}
@ -260,11 +250,11 @@ static ssize_t sd_fat_write_r (struct _reent *r, int fd, const char *ptr, size_t
OSLockMutex(file->dev->pMutex);
size_t len_aligned = FS_ALIGN(len);
if(len_aligned > 0x4000)
if (len_aligned > 0x4000)
len_aligned = 0x4000;
unsigned char *tmpBuf = (unsigned char *)memalign(FS_ALIGNMENT, len_aligned);
if(!tmpBuf) {
unsigned char *tmpBuf = (unsigned char *) memalign(FS_ALIGNMENT, len_aligned);
if (!tmpBuf) {
r->_errno = ENOMEM;
OSUnlockMutex(file->dev->pMutex);
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)
{
if(write_size > 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)
{
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
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;
}
@ -318,11 +300,11 @@ static ssize_t sd_fat_read_r (struct _reent *r, int fd, char *ptr, size_t len)
OSLockMutex(file->dev->pMutex);
size_t len_aligned = FS_ALIGN(len);
if(len_aligned > 0x4000)
if (len_aligned > 0x4000)
len_aligned = 0x4000;
unsigned char *tmpBuf = (unsigned char *)memalign(FS_ALIGNMENT, len_aligned);
if(!tmpBuf) {
unsigned char *tmpBuf = (unsigned char *) memalign(FS_ALIGNMENT, len_aligned);
if (!tmpBuf) {
r->_errno = ENOMEM;
OSUnlockMutex(file->dev->pMutex);
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,10 +336,9 @@ 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)
{
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
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;
return -1;
}
@ -375,7 +350,7 @@ static int sd_fat_fstat_r (struct _reent *r, int fd, struct stat *st)
FSStat stats;
int result = FSGetStatFile(file->dev->pClient, file->dev->pCmd, file->fd, &stats, -1);
if(result != 0) {
if (result != 0) {
r->_errno = result;
OSUnlockMutex(file->dev->pMutex);
return -1;
@ -398,10 +373,9 @@ 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)
{
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
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;
return -1;
}
@ -412,7 +386,7 @@ static int sd_fat_ftruncate_r (struct _reent *r, int fd, off_t len)
OSUnlockMutex(file->dev->pMutex);
if(result < 0) {
if (result < 0) {
r->_errno = result;
return -1;
}
@ -420,10 +394,9 @@ 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)
{
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
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;
return -1;
}
@ -434,7 +407,7 @@ static int sd_fat_fsync_r (struct _reent *r, int fd)
OSUnlockMutex(file->dev->pMutex);
if(result < 0) {
if (result < 0) {
r->_errno = result;
return -1;
}
@ -442,10 +415,9 @@ 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) {
if (!dev) {
r->_errno = ENODEV;
return -1;
}
@ -456,7 +428,7 @@ static int sd_fat_stat_r (struct _reent *r, const char *path, struct stat *st)
memset(st, 0, sizeof(struct stat));
char *real_path = sd_fat_real_path(path, dev);
if(!real_path) {
if (!real_path) {
r->_errno = ENOMEM;
OSUnlockMutex(dev->pMutex);
return -1;
@ -468,14 +440,14 @@ static int sd_fat_stat_r (struct _reent *r, const char *path, struct stat *st)
free(real_path);
if(result < 0) {
if (result < 0) {
r->_errno = result;
OSUnlockMutex(dev->pMutex);
return -1;
}
// mark root also as directory
st->st_mode = ((stats.flag & 0x80000000) || (strlen(dev->mount_path) + 1 == strlen(real_path)))? S_IFDIR : S_IFREG;
st->st_mode = ((stats.flag & 0x80000000) || (strlen(dev->mount_path) + 1 == strlen(real_path))) ? S_IFDIR : S_IFREG;
st->st_nlink = 1;
st->st_size = stats.size;
st->st_blocks = (stats.size + 511) >> 9;
@ -493,16 +465,14 @@ 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) {
if (!dev) {
r->_errno = ENODEV;
return -1;
}
@ -510,7 +480,7 @@ static int sd_fat_unlink_r (struct _reent *r, const char *name)
OSLockMutex(dev->pMutex);
char *real_path = sd_fat_real_path(name, dev);
if(!real_path) {
if (!real_path) {
r->_errno = ENOMEM;
OSUnlockMutex(dev->pMutex);
return -1;
@ -523,7 +493,7 @@ static int sd_fat_unlink_r (struct _reent *r, const char *name)
OSUnlockMutex(dev->pMutex);
if(result < 0) {
if (result < 0) {
r->_errno = result;
return -1;
}
@ -531,10 +501,9 @@ 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) {
if (!dev) {
r->_errno = ENODEV;
return -1;
}
@ -542,7 +511,7 @@ static int sd_fat_chdir_r (struct _reent *r, const char *name)
OSLockMutex(dev->pMutex);
char *real_path = sd_fat_real_path(name, dev);
if(!real_path) {
if (!real_path) {
r->_errno = ENOMEM;
OSUnlockMutex(dev->pMutex);
return -1;
@ -554,7 +523,7 @@ static int sd_fat_chdir_r (struct _reent *r, const char *name)
OSUnlockMutex(dev->pMutex);
if(result < 0) {
if (result < 0) {
r->_errno = result;
return -1;
}
@ -562,10 +531,9 @@ 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) {
if (!dev) {
r->_errno = ENODEV;
return -1;
}
@ -573,13 +541,13 @@ static int sd_fat_rename_r (struct _reent *r, const char *oldName, const char *n
OSLockMutex(dev->pMutex);
char *real_oldpath = sd_fat_real_path(oldName, dev);
if(!real_oldpath) {
if (!real_oldpath) {
r->_errno = ENOMEM;
OSUnlockMutex(dev->pMutex);
return -1;
}
char *real_newpath = sd_fat_real_path(newName, dev);
if(!real_newpath) {
if (!real_newpath) {
r->_errno = ENOMEM;
free(real_oldpath);
OSUnlockMutex(dev->pMutex);
@ -593,7 +561,7 @@ static int sd_fat_rename_r (struct _reent *r, const char *oldName, const char *n
OSUnlockMutex(dev->pMutex);
if(result < 0) {
if (result < 0) {
r->_errno = result;
return -1;
}
@ -602,10 +570,9 @@ 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) {
if (!dev) {
r->_errno = ENODEV;
return -1;
}
@ -613,7 +580,7 @@ static int sd_fat_mkdir_r (struct _reent *r, const char *path, int mode)
OSLockMutex(dev->pMutex);
char *real_path = sd_fat_real_path(path, dev);
if(!real_path) {
if (!real_path) {
r->_errno = ENOMEM;
OSUnlockMutex(dev->pMutex);
return -1;
@ -625,7 +592,7 @@ static int sd_fat_mkdir_r (struct _reent *r, const char *path, int mode)
OSUnlockMutex(dev->pMutex);
if(result < 0) {
if (result < 0) {
r->_errno = result;
return -1;
}
@ -633,10 +600,9 @@ 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) {
if (!dev) {
r->_errno = ENODEV;
return -1;
}
@ -647,7 +613,7 @@ static int sd_fat_statvfs_r (struct _reent *r, const char *path, struct statvfs
memset(buf, 0, sizeof(struct statvfs));
char *real_path = sd_fat_real_path(path, dev);
if(!real_path) {
if (!real_path) {
r->_errno = ENOMEM;
OSUnlockMutex(dev->pMutex);
return -1;
@ -659,7 +625,7 @@ static int sd_fat_statvfs_r (struct _reent *r, const char *path, struct statvfs
free(real_path);
if(result < 0) {
if (result < 0) {
r->_errno = result;
OSUnlockMutex(dev->pMutex);
return -1;
@ -684,7 +650,7 @@ static int sd_fat_statvfs_r (struct _reent *r, const char *path, struct statvfs
buf->f_ffree = 0xffffffff;
// File system id
buf->f_fsid = (int)dev;
buf->f_fsid = (int) dev;
// Bit mask of f_flag values.
buf->f_flag = 0;
@ -697,20 +663,19 @@ 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) {
if (!dev) {
r->_errno = ENODEV;
return NULL;
}
sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *)dirState->dirStruct;
sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *) dirState->dirStruct;
OSLockMutex(dev->pMutex);
char *real_path = sd_fat_real_path(path, dev);
if(!real_path) {
if (!real_path) {
r->_errno = ENOMEM;
OSUnlockMutex(dev->pMutex);
return NULL;
@ -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,10 +700,9 @@ 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)
{
sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *)dirState->dirStruct;
if(!dirIter->dev) {
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;
return -1;
}
@ -750,18 +713,16 @@ 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)
{
sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *)dirState->dirStruct;
if(!dirIter->dev) {
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;
return -1;
}
@ -772,29 +733,26 @@ 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)
{
sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *)dirState->dirStruct;
if(!dirIter->dev) {
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;
return -1;
}
OSLockMutex(dirIter->dev->pMutex);
FSDirEntry * dir_entry = malloc(sizeof(FSDirEntry));
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;
@ -828,7 +785,7 @@ static int sd_fat_dirnext_r (struct _reent *r, DIR_ITER *dirState, char *filenam
// NTFS device driver devoptab
static const devoptab_t devops_sd_fat = {
NULL, /* Device name */
sizeof (sd_fat_file_state_t),
sizeof(sd_fat_file_state_t),
sd_fat_open_r,
sd_fat_close_r,
sd_fat_write_r,
@ -841,7 +798,7 @@ static const devoptab_t devops_sd_fat = {
sd_fat_chdir_r,
sd_fat_rename_r,
sd_fat_mkdir_r,
sizeof (sd_fat_dir_entry_t),
sizeof(sd_fat_dir_entry_t),
sd_fat_diropen_r,
sd_fat_dirreset_r,
sd_fat_dirnext_r,
@ -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;
@ -875,18 +831,18 @@ static int sd_fat_add_device (const char *name, const char *mount_path, void *pC
}
// Use the space allocated at the end of the devoptab for storing the device name
devname = (char*)(dev + 1);
devname = (char *) (dev + 1);
strcpy(devname, name);
// create private data
sd_fat_private_t *priv = (sd_fat_private_t *)malloc(sizeof(sd_fat_private_t) + strlen(mount_path) + 1);
if(!priv) {
sd_fat_private_t *priv = (sd_fat_private_t *) malloc(sizeof(sd_fat_private_t) + strlen(mount_path) + 1);
if (!priv) {
free(dev);
errno = ENOMEM;
return -1;
}
devpath = (char*)(priv+1);
devpath = (char *) (priv + 1);
strcpy(devpath, mount_path);
// setup private data
@ -895,7 +851,7 @@ static int sd_fat_add_device (const char *name, const char *mount_path, void *pC
priv->pCmd = pCmd;
priv->pMutex = malloc(OS_MUTEX_SIZE);
if(!priv->pMutex) {
if (!priv->pMutex) {
free(dev);
free(priv);
errno = ENOMEM;
@ -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,20 +901,19 @@ 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)
{
sd_fat_private_t *priv = (sd_fat_private_t *)devoptab->deviceData;
if (devoptab->deviceData) {
sd_fat_private_t *priv = (sd_fat_private_t *) devoptab->deviceData;
*pClient = priv->pClient;
*pCmd = priv->pCmd;
*mountPath = (char*) malloc(strlen(priv->mount_path)+1);
if(*mountPath)
*mountPath = (char *) malloc(strlen(priv->mount_path) + 1);
if (*mountPath)
strcpy(*mountPath, priv->mount_path);
if(priv->pMutex)
if (priv->pMutex)
free(priv->pMutex);
free(devoptab->deviceData);
}
free((devoptab_t*)devoptab);
free((devoptab_t *) devoptab);
return 0;
}
}
@ -968,19 +922,18 @@ 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
void* pClient = malloc(FS_CLIENT_SIZE);
void* pCmd = malloc(FS_CMD_BLOCK_SIZE);
void *pClient = malloc(FS_CLIENT_SIZE);
void *pCmd = malloc(FS_CMD_BLOCK_SIZE);
if(!pClient || !pCmd) {
if (!pClient || !pCmd) {
// just in case free if not 0
if(pClient)
if (pClient)
free(pClient);
if(pCmd)
if (pCmd)
free(pCmd);
return -2;
}
@ -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.