diff --git a/src/fs/fs_utils.c b/src/fs/fs_utils.c index ef8885e..abb2dc1 100644 --- a/src/fs/fs_utils.c +++ b/src/fs/fs_utils.c @@ -5,118 +5,113 @@ #include #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 result = -1; +int MountFS(void *pClient, void *pCmd, char **mount_path) { + int result = -1; - void *mountSrc = malloc(FS_MOUNT_SOURCE_SIZE); - if(!mountSrc) - return -3; + void *mountSrc = malloc(FS_MOUNT_SOURCE_SIZE); + if (!mountSrc) + return -3; - char* mountPath = (char*) malloc(FS_MAX_MOUNTPATH_SIZE); - if(!mountPath) { - free(mountSrc); - return -4; - } + char *mountPath = (char *) malloc(FS_MAX_MOUNTPATH_SIZE); + if (!mountPath) { + free(mountSrc); + return -4; + } - memset(mountSrc, 0, FS_MOUNT_SOURCE_SIZE); - memset(mountPath, 0, FS_MAX_MOUNTPATH_SIZE); + memset(mountSrc, 0, FS_MOUNT_SOURCE_SIZE); + memset(mountPath, 0, FS_MAX_MOUNTPATH_SIZE); - // Mount sdcard - if (FSGetMountSource(pClient, pCmd, FS_SOURCETYPE_EXTERNAL, mountSrc, -1) == 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); - } - } + // Mount sdcard + 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; + 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 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 +int LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size) { + // Always initialize input *inbuffer = NULL; - if(size) - *size = 0; + if (size) + *size = 0; - int iFd = open(filepath, O_RDONLY); + int iFd = open(filepath, O_RDONLY); if (iFd < 0) return -1; u32 filesize = lseek(iFd, 0, SEEK_END); - lseek(iFd, 0, SEEK_SET); + lseek(iFd, 0, SEEK_SET); u8 *buffer = (u8 *) malloc(filesize); - if (buffer == NULL) - { - close(iFd); + if (buffer == NULL) { + close(iFd); return -2; } - u32 blocksize = 0x4000; - u32 done = 0; - int readBytes = 0; + u32 blocksize = 0x4000; + u32 done = 0; + int readBytes = 0; - while(done < filesize) - { - if(done + blocksize > filesize) { - blocksize = filesize - done; - } - readBytes = read(iFd, buffer + done, blocksize); - if(readBytes <= 0) - break; - done += readBytes; - } + while (done < filesize) { + if (done + blocksize > filesize) { + blocksize = filesize - done; + } + readBytes = read(iFd, buffer + done, blocksize); + if (readBytes <= 0) + break; + done += readBytes; + } - close(iFd); + close(iFd); - if (done != filesize) - { + if (done != filesize) { free(buffer); return -3; } *inbuffer = buffer; - //! sign is optional input - if(size) - *size = filesize; + //! sign is optional input + 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; } diff --git a/src/fs/fs_utils.h b/src/fs/fs_utils.h index 7022695..95f9847 100644 --- a/src/fs/fs_utils.h +++ b/src/fs/fs_utils.h @@ -6,6 +6,7 @@ extern "C" { #endif #include +#include "../common/fs_defs.h" int MountFS(void *pClient, void *pCmd, char **mount_path); int UmountFS(void *pClient, void *pCmd, const char *mountPath); diff --git a/src/fs/sd_fat_devoptab.c b/src/fs/sd_fat_devoptab.c index c458ad2..c7d338f 100644 --- a/src/fs/sd_fat_devoptab.c +++ b/src/fs/sd_fat_devoptab.c @@ -32,988 +32,944 @@ #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)) typedef struct _sd_fat_private_t { - char *mount_path; - void *pClient; - void *pCmd; - void *pMutex; + char *mount_path; + void *pClient; + void *pCmd; + void *pMutex; } sd_fat_private_t; typedef struct _sd_fat_file_state_t { - sd_fat_private_t *dev; - int fd; /* File descriptor */ - int flags; /* Opening flags */ - bool read; /* True if allowed to read from file */ - bool write; /* True if allowed to write to file */ - bool append; /* True if allowed to append to file */ - u64 pos; /* Current position within the file (in bytes) */ - u64 len; /* Total length of the file (in bytes) */ - struct _sd_fat_file_state_t *prevOpenFile; /* The previous entry in a double-linked FILO list of open files */ - struct _sd_fat_file_state_t *nextOpenFile; /* The next entry in a double-linked FILO list of open files */ + sd_fat_private_t *dev; + int fd; /* File descriptor */ + int flags; /* Opening flags */ + bool read; /* True if allowed to read from file */ + bool write; /* True if allowed to write to file */ + bool append; /* True if allowed to append to file */ + u64 pos; /* Current position within the file (in bytes) */ + u64 len; /* Total length of the file (in bytes) */ + struct _sd_fat_file_state_t *prevOpenFile; /* The previous entry in a double-linked FILO list of open files */ + struct _sd_fat_file_state_t *nextOpenFile; /* The next entry in a double-linked FILO list of open files */ } sd_fat_file_state_t; typedef struct _sd_fat_dir_entry_t { - sd_fat_private_t *dev; - int dirHandle; + sd_fat_private_t *dev; + int dirHandle; } sd_fat_dir_entry_t; -static sd_fat_private_t *sd_fat_get_device_data(const char *path) -{ - const devoptab_t *devoptab = NULL; - char name[128] = {0}; - int i; +static sd_fat_private_t *sd_fat_get_device_data(const char *path) { + const devoptab_t *devoptab = NULL; + char name[128] = {0}; + int i; - // Get the device name from the path - strncpy(name, path, 127); - strtok(name, ":/"); + // Get the device name from the path + strncpy(name, path, 127); + strtok(name, ":/"); - // Search the devoptab table for the specified device name - // NOTE: We do this manually due to a 'bug' in GetDeviceOpTab - // which ignores names with suffixes and causes names - // like "ntfs" and "ntfs1" to be seen as equals - for (i = 3; i < STD_MAX; i++) { - devoptab = devoptab_list[i]; - if (devoptab && devoptab->name) { - if (strcmp(name, devoptab->name) == 0) { - return (sd_fat_private_t *)devoptab->deviceData; - } - } - } + // Search the devoptab table for the specified device name + // NOTE: We do this manually due to a 'bug' in GetDeviceOpTab + // which ignores names with suffixes and causes names + // like "ntfs" and "ntfs1" to be seen as equals + for (i = 3; i < STD_MAX; i++) { + devoptab = devoptab_list[i]; + if (devoptab && devoptab->name) { + if (strcmp(name, devoptab->name) == 0) { + return (sd_fat_private_t *) devoptab->deviceData; + } + } + } - return NULL; + return NULL; } -static char *sd_fat_real_path (const char *path, sd_fat_private_t *dev) -{ - // Sanity check - if (!path) - return NULL; +static char *sd_fat_real_path(const char *path, sd_fat_private_t *dev) { + // Sanity check + if (!path) + return NULL; - // Move the path pointer to the start of the actual path - if (strchr(path, ':') != NULL) { - path = strchr(path, ':') + 1; - } + // Move the path pointer to the start of the actual path + if (strchr(path, ':') != NULL) { + path = strchr(path, ':') + 1; + } - int mount_len = strlen(dev->mount_path); + int mount_len = strlen(dev->mount_path); - 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; - } - return 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; + } + return new_name; } -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; - return -1; - } +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; + 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 - file->flags = flags; + file->dev = dev; + // Determine which mode the file is opened for + file->flags = flags; - const char *mode_str; + const char *mode_str; - if ((flags & 0x03) == O_RDONLY) { - file->read = true; - file->write = false; - file->append = false; - mode_str = "r"; - } else if ((flags & 0x03) == O_WRONLY) { - file->read = false; - file->write = true; - file->append = (flags & O_APPEND); - mode_str = file->append ? "a" : "w"; - } else if ((flags & 0x03) == O_RDWR) { - file->read = true; - file->write = true; - file->append = (flags & O_APPEND); - mode_str = file->append ? "a+" : "r+"; - } else { - r->_errno = EACCES; - return -1; - } + if ((flags & 0x03) == O_RDONLY) { + file->read = true; + file->write = false; + file->append = false; + mode_str = "r"; + } else if ((flags & 0x03) == O_WRONLY) { + file->read = false; + file->write = true; + file->append = (flags & O_APPEND); + mode_str = file->append ? "a" : "w"; + } else if ((flags & 0x03) == O_RDWR) { + file->read = true; + file->write = true; + file->append = (flags & O_APPEND); + mode_str = file->append ? "a+" : "r+"; + } else { + r->_errno = EACCES; + return -1; + } - int fd = -1; + int fd = -1; - OSLockMutex(dev->pMutex); + OSLockMutex(dev->pMutex); - char *real_path = sd_fat_real_path(path, dev); - if(!path) { - r->_errno = ENOMEM; - OSUnlockMutex(dev->pMutex); - return -1; - } + char *real_path = sd_fat_real_path(path, dev); + if (!path) { + r->_errno = ENOMEM; + OSUnlockMutex(dev->pMutex); + return -1; + } - int result = FSOpenFile(dev->pClient, dev->pCmd, real_path, mode_str, &fd, -1); + int result = FSOpenFile(dev->pClient, dev->pCmd, real_path, mode_str, &fd, -1); - free(real_path); + free(real_path); - if(result == 0) - { - FSStat stats; - result = FSGetStatFile(dev->pClient, dev->pCmd, fd, &stats, -1); - if(result != 0) { - FSCloseFile(dev->pClient, dev->pCmd, fd, -1); - r->_errno = result; - OSUnlockMutex(dev->pMutex); - return -1; - } - file->fd = fd; - file->pos = 0; - file->len = stats.size; - OSUnlockMutex(dev->pMutex); - return (int)file; - } + if (result == 0) { + FSStat stats; + result = FSGetStatFile(dev->pClient, dev->pCmd, fd, &stats, -1); + if (result != 0) { + FSCloseFile(dev->pClient, dev->pCmd, fd, -1); + r->_errno = result; + OSUnlockMutex(dev->pMutex); + return -1; + } + file->fd = fd; + file->pos = 0; + file->len = stats.size; + OSUnlockMutex(dev->pMutex); + return (int) file; + } - r->_errno = result; - OSUnlockMutex(dev->pMutex); - return -1; + r->_errno = result; + OSUnlockMutex(dev->pMutex); + return -1; } -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; - } +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; + } - OSLockMutex(file->dev->pMutex); + OSLockMutex(file->dev->pMutex); - int result = FSCloseFile(file->dev->pClient, file->dev->pCmd, file->fd, -1); + int result = FSCloseFile(file->dev->pClient, file->dev->pCmd, file->fd, -1); - OSUnlockMutex(file->dev->pMutex); + OSUnlockMutex(file->dev->pMutex); - if(result < 0) - { - r->_errno = result; - return -1; - } - return 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) { - r->_errno = ENODEV; - 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) { + r->_errno = ENODEV; + return 0; + } - OSLockMutex(file->dev->pMutex); + OSLockMutex(file->dev->pMutex); - switch(dir) - { - case SEEK_SET: - file->pos = pos; - break; - case SEEK_CUR: - file->pos += pos; - break; - case SEEK_END: - file->pos = file->len + pos; - break; - default: - r->_errno = EINVAL; - return -1; - } + switch (dir) { + case SEEK_SET: + file->pos = pos; + break; + case SEEK_CUR: + file->pos += pos; + break; + case SEEK_END: + file->pos = file->len + pos; + break; + default: + r->_errno = EINVAL; + return -1; + } - int result = FSSetPosFile(file->dev->pClient, file->dev->pCmd, file->fd, file->pos, -1); + int result = FSSetPosFile(file->dev->pClient, file->dev->pCmd, file->fd, file->pos, -1); - OSUnlockMutex(file->dev->pMutex); + OSUnlockMutex(file->dev->pMutex); - if(result == 0) - { - return file->pos; - } + if (result == 0) { + return file->pos; + } - return result; + 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) { - r->_errno = ENODEV; - return 0; - } +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) - { - r->_errno = EACCES; - return 0; - } + if (!file->write) { + r->_errno = EACCES; + return 0; + } - OSLockMutex(file->dev->pMutex); + OSLockMutex(file->dev->pMutex); - size_t len_aligned = FS_ALIGN(len); - if(len_aligned > 0x4000) - len_aligned = 0x4000; + size_t len_aligned = FS_ALIGN(len); + if (len_aligned > 0x4000) + len_aligned = 0x4000; - unsigned char *tmpBuf = (unsigned char *)memalign(FS_ALIGNMENT, len_aligned); - if(!tmpBuf) { - r->_errno = ENOMEM; - OSUnlockMutex(file->dev->pMutex); - return 0; - } + unsigned char *tmpBuf = (unsigned char *) memalign(FS_ALIGNMENT, len_aligned); + if (!tmpBuf) { + r->_errno = ENOMEM; + OSUnlockMutex(file->dev->pMutex); + return 0; + } - size_t done = 0; + size_t done = 0; - while(done < len) - { - size_t write_size = (len_aligned < (len - done)) ? len_aligned : (len - done); - memcpy(tmpBuf, ptr + done, write_size); + 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) - { - r->_errno = result; - break; - } - else if(result == 0) - { - if(write_size > 0) - done = 0; - break; - } - else - { - done += result; - file->pos += result; - } - } + int result = FSWriteFile(file->dev->pClient, file->dev->pCmd, tmpBuf, 0x01, write_size, file->fd, 0, -1); + if (result < 0) { + r->_errno = result; + break; + } else if (result == 0) { + if (write_size > 0) + done = 0; + break; + } else { + done += result; + file->pos += result; + } + } - free(tmpBuf); - OSUnlockMutex(file->dev->pMutex); - return done; + free(tmpBuf); + OSUnlockMutex(file->dev->pMutex); + 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) { - r->_errno = ENODEV; - return 0; - } +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) - { - r->_errno = EACCES; - return 0; - } + if (!file->read) { + r->_errno = EACCES; + return 0; + } - OSLockMutex(file->dev->pMutex); + OSLockMutex(file->dev->pMutex); - size_t len_aligned = FS_ALIGN(len); - if(len_aligned > 0x4000) - len_aligned = 0x4000; + size_t len_aligned = FS_ALIGN(len); + if (len_aligned > 0x4000) + len_aligned = 0x4000; - unsigned char *tmpBuf = (unsigned char *)memalign(FS_ALIGNMENT, len_aligned); - if(!tmpBuf) { - r->_errno = ENOMEM; - OSUnlockMutex(file->dev->pMutex); - return 0; - } + unsigned char *tmpBuf = (unsigned char *) memalign(FS_ALIGNMENT, len_aligned); + if (!tmpBuf) { + r->_errno = ENOMEM; + OSUnlockMutex(file->dev->pMutex); + return 0; + } - size_t done = 0; + size_t done = 0; - while(done < len) - { - size_t read_size = (len_aligned < (len - done)) ? len_aligned : (len - done); + 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) - { - r->_errno = result; - done = 0; - break; - } - else if(result == 0) - { - //! TODO: error on read_size > 0 - break; - } - else - { - memcpy(ptr + done, tmpBuf, read_size); - done += result; - file->pos += result; - } - } + int result = FSReadFile(file->dev->pClient, file->dev->pCmd, tmpBuf, 0x01, read_size, file->fd, 0, -1); + if (result < 0) { + r->_errno = result; + done = 0; + break; + } else if (result == 0) { + //! TODO: error on read_size > 0 + break; + } else { + memcpy(ptr + done, tmpBuf, read_size); + done += result; + file->pos += result; + } + } - free(tmpBuf); - OSUnlockMutex(file->dev->pMutex); - return done; + free(tmpBuf); + OSUnlockMutex(file->dev->pMutex); + return done; } -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; - } +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; + } - OSLockMutex(file->dev->pMutex); + OSLockMutex(file->dev->pMutex); - // Zero out the stat buffer - memset(st, 0, sizeof(struct stat)); + // Zero out the stat buffer + memset(st, 0, sizeof(struct stat)); - FSStat stats; - int result = FSGetStatFile(file->dev->pClient, file->dev->pCmd, file->fd, &stats, -1); - if(result != 0) { - r->_errno = result; - OSUnlockMutex(file->dev->pMutex); - return -1; - } + FSStat stats; + int result = FSGetStatFile(file->dev->pClient, file->dev->pCmd, file->fd, &stats, -1); + if (result != 0) { + r->_errno = result; + OSUnlockMutex(file->dev->pMutex); + return -1; + } - st->st_mode = S_IFREG; - st->st_size = stats.size; - st->st_blocks = (stats.size + 511) >> 9; - st->st_nlink = 1; + st->st_mode = S_IFREG; + st->st_size = stats.size; + st->st_blocks = (stats.size + 511) >> 9; + st->st_nlink = 1; - // Fill in the generic entry stats - st->st_dev = stats.ent_id; - st->st_uid = stats.owner_id; - st->st_gid = stats.group_id; - st->st_ino = stats.ent_id; - st->st_atime = stats.mtime; - st->st_ctime = stats.ctime; - st->st_mtime = stats.mtime; - OSUnlockMutex(file->dev->pMutex); - return 0; + // Fill in the generic entry stats + st->st_dev = stats.ent_id; + st->st_uid = stats.owner_id; + st->st_gid = stats.group_id; + st->st_ino = stats.ent_id; + st->st_atime = stats.mtime; + st->st_ctime = stats.ctime; + st->st_mtime = stats.mtime; + OSUnlockMutex(file->dev->pMutex); + 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) { - r->_errno = ENODEV; - return -1; - } +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; + } - OSLockMutex(file->dev->pMutex); + OSLockMutex(file->dev->pMutex); - int result = FSTruncateFile(file->dev->pClient, file->dev->pCmd, file->fd, -1); + int result = FSTruncateFile(file->dev->pClient, file->dev->pCmd, file->fd, -1); - OSUnlockMutex(file->dev->pMutex); + OSUnlockMutex(file->dev->pMutex); - if(result < 0) { - r->_errno = result; - return -1; - } + if (result < 0) { + r->_errno = result; + return -1; + } - return 0; + 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) { - r->_errno = ENODEV; - return -1; - } +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; + } - OSLockMutex(file->dev->pMutex); + OSLockMutex(file->dev->pMutex); - int result = FSFlushFile(file->dev->pClient, file->dev->pCmd, file->fd, -1); + int result = FSFlushFile(file->dev->pClient, file->dev->pCmd, file->fd, -1); - OSUnlockMutex(file->dev->pMutex); + OSUnlockMutex(file->dev->pMutex); - if(result < 0) { - r->_errno = result; - return -1; - } + if (result < 0) { + r->_errno = result; + return -1; + } - return 0; + return 0; } -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; - return -1; - } +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; + return -1; + } - OSLockMutex(dev->pMutex); + OSLockMutex(dev->pMutex); - // Zero out the stat buffer - memset(st, 0, sizeof(struct stat)); + // Zero out the stat buffer + memset(st, 0, sizeof(struct stat)); - char *real_path = sd_fat_real_path(path, dev); - if(!real_path) { - r->_errno = ENOMEM; - OSUnlockMutex(dev->pMutex); - return -1; - } + char *real_path = sd_fat_real_path(path, dev); + if (!real_path) { + r->_errno = ENOMEM; + OSUnlockMutex(dev->pMutex); + return -1; + } - FSStat stats; + FSStat stats; - int result = FSGetStat(dev->pClient, dev->pCmd, real_path, &stats, -1); + int result = FSGetStat(dev->pClient, dev->pCmd, real_path, &stats, -1); - free(real_path); + free(real_path); - if(result < 0) { - r->_errno = result; - OSUnlockMutex(dev->pMutex); - return -1; - } + 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_nlink = 1; - st->st_size = stats.size; - st->st_blocks = (stats.size + 511) >> 9; - // Fill in the generic entry stats - st->st_dev = stats.ent_id; - st->st_uid = stats.owner_id; - st->st_gid = stats.group_id; - st->st_ino = stats.ent_id; - st->st_atime = stats.mtime; - st->st_ctime = stats.ctime; - st->st_mtime = stats.mtime; + // 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_nlink = 1; + st->st_size = stats.size; + st->st_blocks = (stats.size + 511) >> 9; + // Fill in the generic entry stats + st->st_dev = stats.ent_id; + st->st_uid = stats.owner_id; + st->st_gid = stats.group_id; + st->st_ino = stats.ent_id; + st->st_atime = stats.mtime; + st->st_ctime = stats.ctime; + st->st_mtime = stats.mtime; - OSUnlockMutex(dev->pMutex); + OSUnlockMutex(dev->pMutex); - return 0; + return 0; } -static int sd_fat_link_r (struct _reent *r, const char *existing, const char *newLink) -{ - r->_errno = ENOTSUP; - return -1; +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) -{ - sd_fat_private_t *dev = sd_fat_get_device_data(name); - if(!dev) { - r->_errno = ENODEV; - return -1; - } +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; + return -1; + } - OSLockMutex(dev->pMutex); + OSLockMutex(dev->pMutex); - char *real_path = sd_fat_real_path(name, dev); - if(!real_path) { - r->_errno = ENOMEM; - OSUnlockMutex(dev->pMutex); - return -1; - } + char *real_path = sd_fat_real_path(name, dev); + if (!real_path) { + r->_errno = ENOMEM; + OSUnlockMutex(dev->pMutex); + return -1; + } - int result = FSRemove(dev->pClient, dev->pCmd, real_path, -1); + int result = FSRemove(dev->pClient, dev->pCmd, real_path, -1); - free(real_path); + free(real_path); - OSUnlockMutex(dev->pMutex); + OSUnlockMutex(dev->pMutex); - if(result < 0) { - r->_errno = result; - return -1; - } + if (result < 0) { + r->_errno = result; + return -1; + } - return 0; + return 0; } -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; - return -1; - } +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; + return -1; + } - OSLockMutex(dev->pMutex); + OSLockMutex(dev->pMutex); - char *real_path = sd_fat_real_path(name, dev); - if(!real_path) { - r->_errno = ENOMEM; - OSUnlockMutex(dev->pMutex); - return -1; - } + char *real_path = sd_fat_real_path(name, dev); + if (!real_path) { + r->_errno = ENOMEM; + OSUnlockMutex(dev->pMutex); + return -1; + } - int result = FSChangeDir(dev->pClient, dev->pCmd, real_path, -1); + int result = FSChangeDir(dev->pClient, dev->pCmd, real_path, -1); - free(real_path); + free(real_path); - OSUnlockMutex(dev->pMutex); + OSUnlockMutex(dev->pMutex); - if(result < 0) { - r->_errno = result; - return -1; - } + if (result < 0) { + r->_errno = result; + return -1; + } - return 0; + return 0; } -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; - return -1; - } +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; + return -1; + } - OSLockMutex(dev->pMutex); + OSLockMutex(dev->pMutex); - char *real_oldpath = sd_fat_real_path(oldName, dev); - if(!real_oldpath) { - r->_errno = ENOMEM; - OSUnlockMutex(dev->pMutex); - return -1; - } - char *real_newpath = sd_fat_real_path(newName, dev); - if(!real_newpath) { - r->_errno = ENOMEM; - free(real_oldpath); - OSUnlockMutex(dev->pMutex); - return -1; - } + char *real_oldpath = sd_fat_real_path(oldName, dev); + if (!real_oldpath) { + r->_errno = ENOMEM; + OSUnlockMutex(dev->pMutex); + return -1; + } + char *real_newpath = sd_fat_real_path(newName, dev); + if (!real_newpath) { + r->_errno = ENOMEM; + free(real_oldpath); + OSUnlockMutex(dev->pMutex); + return -1; + } - int result = FSRename(dev->pClient, dev->pCmd, real_oldpath, real_newpath, -1); + int result = FSRename(dev->pClient, dev->pCmd, real_oldpath, real_newpath, -1); - free(real_oldpath); - free(real_newpath); + free(real_oldpath); + free(real_newpath); - OSUnlockMutex(dev->pMutex); + OSUnlockMutex(dev->pMutex); - if(result < 0) { - r->_errno = result; - return -1; - } + if (result < 0) { + r->_errno = result; + return -1; + } - return 0; + return 0; } -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; - return -1; - } +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; + return -1; + } - OSLockMutex(dev->pMutex); + OSLockMutex(dev->pMutex); - char *real_path = sd_fat_real_path(path, dev); - if(!real_path) { - r->_errno = ENOMEM; - OSUnlockMutex(dev->pMutex); - return -1; - } + char *real_path = sd_fat_real_path(path, dev); + if (!real_path) { + r->_errno = ENOMEM; + OSUnlockMutex(dev->pMutex); + return -1; + } - int result = FSMakeDir(dev->pClient, dev->pCmd, real_path, -1); + int result = FSMakeDir(dev->pClient, dev->pCmd, real_path, -1); - free(real_path); + free(real_path); - OSUnlockMutex(dev->pMutex); + OSUnlockMutex(dev->pMutex); - if(result < 0) { - r->_errno = result; - return -1; - } + if (result < 0) { + r->_errno = result; + return -1; + } - return 0; + return 0; } -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; - return -1; - } +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; + return -1; + } - OSLockMutex(dev->pMutex); + OSLockMutex(dev->pMutex); - // Zero out the stat buffer - memset(buf, 0, sizeof(struct statvfs)); + // Zero out the stat buffer + memset(buf, 0, sizeof(struct statvfs)); - char *real_path = sd_fat_real_path(path, dev); - if(!real_path) { - r->_errno = ENOMEM; - OSUnlockMutex(dev->pMutex); - return -1; - } + char *real_path = sd_fat_real_path(path, dev); + if (!real_path) { + r->_errno = ENOMEM; + OSUnlockMutex(dev->pMutex); + return -1; + } - u64 size; + u64 size; - int result = FSGetFreeSpaceSize(dev->pClient, dev->pCmd, real_path, &size, -1); + int result = FSGetFreeSpaceSize(dev->pClient, dev->pCmd, real_path, &size, -1); - free(real_path); + free(real_path); - if(result < 0) { - r->_errno = result; - OSUnlockMutex(dev->pMutex); - return -1; - } + if (result < 0) { + r->_errno = result; + OSUnlockMutex(dev->pMutex); + return -1; + } - // File system block size - buf->f_bsize = 512; + // File system block size + buf->f_bsize = 512; - // Fundamental file system block size - buf->f_frsize = 512; + // Fundamental file system block size + buf->f_frsize = 512; - // Total number of blocks on file system in units of f_frsize - buf->f_blocks = size >> 9; // this is unknown + // Total number of blocks on file system in units of f_frsize + buf->f_blocks = size >> 9; // this is unknown - // Free blocks available for all and for non-privileged processes - buf->f_bfree = buf->f_bavail = size >> 9; + // Free blocks available for all and for non-privileged processes + buf->f_bfree = buf->f_bavail = size >> 9; - // Number of inodes at this point in time - buf->f_files = 0xffffffff; + // Number of inodes at this point in time + buf->f_files = 0xffffffff; - // Free inodes available for all and for non-privileged processes - buf->f_ffree = 0xffffffff; + // Free inodes available for all and for non-privileged processes + buf->f_ffree = 0xffffffff; - // File system id - buf->f_fsid = (int)dev; + // File system id + buf->f_fsid = (int) dev; - // Bit mask of f_flag values. - buf->f_flag = 0; + // Bit mask of f_flag values. + buf->f_flag = 0; - // Maximum length of filenames - buf->f_namemax = 255; + // Maximum length of filenames + buf->f_namemax = 255; - OSUnlockMutex(dev->pMutex); + OSUnlockMutex(dev->pMutex); - return 0; + return 0; } -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; - return NULL; - } +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; + 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); + OSLockMutex(dev->pMutex); - char *real_path = sd_fat_real_path(path, dev); - if(!real_path) { - r->_errno = ENOMEM; - OSUnlockMutex(dev->pMutex); - return NULL; - } + char *real_path = sd_fat_real_path(path, dev); + if (!real_path) { + r->_errno = ENOMEM; + OSUnlockMutex(dev->pMutex); + return NULL; + } - int dirHandle; + int dirHandle; - int result = FSOpenDir(dev->pClient, dev->pCmd, real_path, &dirHandle, -1); + int result = FSOpenDir(dev->pClient, dev->pCmd, real_path, &dirHandle, -1); - free(real_path); + free(real_path); - OSUnlockMutex(dev->pMutex); + OSUnlockMutex(dev->pMutex); - if(result < 0) - { - r->_errno = result; - return NULL; - } + if (result < 0) { + r->_errno = result; + return NULL; + } - dirIter->dev = dev; - dirIter->dirHandle = dirHandle; + dirIter->dev = dev; + dirIter->dirHandle = dirHandle; - return dirState; + 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) { - r->_errno = ENODEV; - return -1; - } +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; + } - OSLockMutex(dirIter->dev->pMutex); + OSLockMutex(dirIter->dev->pMutex); - int result = FSCloseDir(dirIter->dev->pClient, dirIter->dev->pCmd, dirIter->dirHandle, -1); + int result = FSCloseDir(dirIter->dev->pClient, dirIter->dev->pCmd, dirIter->dirHandle, -1); - OSUnlockMutex(dirIter->dev->pMutex); + OSUnlockMutex(dirIter->dev->pMutex); - if(result < 0) - { - r->_errno = result; - return -1; - } - return 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) { - r->_errno = ENODEV; - return -1; - } +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; + } - OSLockMutex(dirIter->dev->pMutex); + OSLockMutex(dirIter->dev->pMutex); - int result = FSRewindDir(dirIter->dev->pClient, dirIter->dev->pCmd, dirIter->dirHandle, -1); + int result = FSRewindDir(dirIter->dev->pClient, dirIter->dev->pCmd, dirIter->dirHandle, -1); - OSUnlockMutex(dirIter->dev->pMutex); + OSUnlockMutex(dirIter->dev->pMutex); - if(result < 0) - { - r->_errno = result; - return -1; - } - return 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) { - r->_errno = ENODEV; - return -1; - } +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); + 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) - { - free(dir_entry); - r->_errno = result; - OSUnlockMutex(dirIter->dev->pMutex); - return -1; - } + int result = FSReadDir(dirIter->dev->pClient, dirIter->dev->pCmd, dirIter->dirHandle, dir_entry, -1); + if (result < 0) { + free(dir_entry); + r->_errno = result; + OSUnlockMutex(dirIter->dev->pMutex); + return -1; + } - // Fetch the current entry - strcpy(filename, dir_entry->name); + // Fetch the current entry + strcpy(filename, dir_entry->name); - if(st) - { - memset(st, 0, sizeof(struct stat)); - st->st_mode = (dir_entry->stat.flag & 0x80000000) ? S_IFDIR : S_IFREG; - st->st_nlink = 1; - st->st_size = dir_entry->stat.size; - st->st_blocks = (dir_entry->stat.size + 511) >> 9; - st->st_dev = dir_entry->stat.ent_id; - st->st_uid = dir_entry->stat.owner_id; - st->st_gid = dir_entry->stat.group_id; - st->st_ino = dir_entry->stat.ent_id; - st->st_atime = dir_entry->stat.mtime; - st->st_ctime = dir_entry->stat.ctime; - st->st_mtime = dir_entry->stat.mtime; - } + if (st) { + memset(st, 0, sizeof(struct stat)); + st->st_mode = (dir_entry->stat.flag & 0x80000000) ? S_IFDIR : S_IFREG; + st->st_nlink = 1; + st->st_size = dir_entry->stat.size; + st->st_blocks = (dir_entry->stat.size + 511) >> 9; + st->st_dev = dir_entry->stat.ent_id; + st->st_uid = dir_entry->stat.owner_id; + st->st_gid = dir_entry->stat.group_id; + st->st_ino = dir_entry->stat.ent_id; + st->st_atime = dir_entry->stat.mtime; + st->st_ctime = dir_entry->stat.ctime; + st->st_mtime = dir_entry->stat.mtime; + } - free(dir_entry); - OSUnlockMutex(dirIter->dev->pMutex); - return 0; + free(dir_entry); + OSUnlockMutex(dirIter->dev->pMutex); + return 0; } // NTFS device driver devoptab static const devoptab_t devops_sd_fat = { - NULL, /* Device name */ - sizeof (sd_fat_file_state_t), - sd_fat_open_r, - sd_fat_close_r, - sd_fat_write_r, - sd_fat_read_r, - sd_fat_seek_r, - sd_fat_fstat_r, - sd_fat_stat_r, - sd_fat_link_r, - sd_fat_unlink_r, - sd_fat_chdir_r, - sd_fat_rename_r, - sd_fat_mkdir_r, - sizeof (sd_fat_dir_entry_t), - sd_fat_diropen_r, - sd_fat_dirreset_r, - sd_fat_dirnext_r, - sd_fat_dirclose_r, - sd_fat_statvfs_r, - sd_fat_ftruncate_r, - sd_fat_fsync_r, - NULL, /* sd_fat_chmod_r */ - NULL, /* sd_fat_fchmod_r */ - NULL /* Device data */ + NULL, /* Device name */ + sizeof(sd_fat_file_state_t), + sd_fat_open_r, + sd_fat_close_r, + sd_fat_write_r, + sd_fat_read_r, + sd_fat_seek_r, + sd_fat_fstat_r, + sd_fat_stat_r, + sd_fat_link_r, + sd_fat_unlink_r, + sd_fat_chdir_r, + sd_fat_rename_r, + sd_fat_mkdir_r, + sizeof(sd_fat_dir_entry_t), + sd_fat_diropen_r, + sd_fat_dirreset_r, + sd_fat_dirnext_r, + sd_fat_dirclose_r, + sd_fat_statvfs_r, + sd_fat_ftruncate_r, + sd_fat_fsync_r, + NULL, /* sd_fat_chmod_r */ + NULL, /* sd_fat_fchmod_r */ + NULL /* Device data */ }; -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; - int i; +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; + int i; - // Sanity check - if (!name) { - errno = EINVAL; - return -1; - } + // Sanity check + if (!name) { + errno = EINVAL; + return -1; + } - // Allocate a devoptab for this device - dev = (devoptab_t *) malloc(sizeof(devoptab_t) + strlen(name) + 1); - if (!dev) { - errno = ENOMEM; - return -1; - } + // Allocate a devoptab for this device + dev = (devoptab_t *) malloc(sizeof(devoptab_t) + strlen(name) + 1); + if (!dev) { + errno = ENOMEM; + return -1; + } - // Use the space allocated at the end of the devoptab for storing the device name - devname = (char*)(dev + 1); - strcpy(devname, name); + // Use the space allocated at the end of the devoptab for storing the device name + 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) { - free(dev); - errno = ENOMEM; - return -1; - } + // create private data + 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); - strcpy(devpath, mount_path); + devpath = (char *) (priv + 1); + strcpy(devpath, mount_path); - // setup private data - priv->mount_path = devpath; - priv->pClient = pClient; - priv->pCmd = pCmd; - priv->pMutex = malloc(OS_MUTEX_SIZE); + // setup private data + priv->mount_path = devpath; + priv->pClient = pClient; + priv->pCmd = pCmd; + priv->pMutex = malloc(OS_MUTEX_SIZE); - if(!priv->pMutex) { - free(dev); - free(priv); - errno = ENOMEM; - return -1; - } + if (!priv->pMutex) { + free(dev); + free(priv); + errno = ENOMEM; + return -1; + } - OSInitMutex(priv->pMutex); + OSInitMutex(priv->pMutex); - // Setup the devoptab - memcpy(dev, &devops_sd_fat, sizeof(devoptab_t)); - dev->name = devname; - dev->deviceData = priv; + // Setup the devoptab + memcpy(dev, &devops_sd_fat, sizeof(devoptab_t)); + dev->name = devname; + dev->deviceData = priv; - // Add the device to the devoptab table (if there is a free slot) - for (i = 3; i < STD_MAX; i++) { - if (devoptab_list[i] == devoptab_list[0]) { - devoptab_list[i] = dev; - return 0; - } - } + // Add the device to the devoptab table (if there is a free slot) + for (i = 3; i < STD_MAX; i++) { + if (devoptab_list[i] == devoptab_list[0]) { + devoptab_list[i] = dev; + return 0; + } + } - // failure, free all memory - free(priv); - free(dev); + // failure, free all memory + free(priv); + free(dev); - // If we reach here then there are no free slots in the devoptab table for this device - errno = EADDRNOTAVAIL; - return -1; + // If we reach here then there are no free slots in the devoptab table for this device + errno = EADDRNOTAVAIL; + return -1; } -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; +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; - // Get the device name from the path - strncpy(name, path, 127); - strtok(name, ":/"); + // Get the device name from the path + strncpy(name, path, 127); + strtok(name, ":/"); - // Find and remove the specified device from the devoptab table - // NOTE: We do this manually due to a 'bug' in RemoveDevice - // which ignores names with suffixes and causes names - // like "ntfs" and "ntfs1" to be seen as equals - for (i = 3; i < STD_MAX; i++) { - devoptab = devoptab_list[i]; - if (devoptab && devoptab->name) { - if (strcmp(name, devoptab->name) == 0) { - devoptab_list[i] = devoptab_list[0]; + // Find and remove the specified device from the devoptab table + // NOTE: We do this manually due to a 'bug' in RemoveDevice + // which ignores names with suffixes and causes names + // like "ntfs" and "ntfs1" to be seen as equals + for (i = 3; i < STD_MAX; i++) { + devoptab = devoptab_list[i]; + if (devoptab && devoptab->name) { + 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; - *pClient = priv->pClient; - *pCmd = priv->pCmd; - *mountPath = (char*) malloc(strlen(priv->mount_path)+1); - if(*mountPath) - strcpy(*mountPath, priv->mount_path); - if(priv->pMutex) - free(priv->pMutex); - free(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) + strcpy(*mountPath, priv->mount_path); + if (priv->pMutex) + free(priv->pMutex); + free(devoptab->deviceData); + } - free((devoptab_t*)devoptab); - return 0; - } - } - } + free((devoptab_t *) devoptab); + return 0; + } + } + } - return -1; + return -1; } -int mount_sd_fat(const char *path) -{ - int result = -1; +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); + // get command and client + void *pClient = malloc(FS_CLIENT_SIZE); + void *pCmd = malloc(FS_CMD_BLOCK_SIZE); - if(!pClient || !pCmd) { - // just in case free if not 0 - if(pClient) - free(pClient); - if(pCmd) - free(pCmd); - return -2; - } + if (!pClient || !pCmd) { + // just in case free if not 0 + if (pClient) + free(pClient); + if (pCmd) + free(pCmd); + return -2; + } - FSInit(); - FSInitCmdBlock(pCmd); - FSAddClientEx(pClient, 0, -1); + FSInit(); + FSInitCmdBlock(pCmd); + FSAddClientEx(pClient, 0, -1); - char *mountPath = NULL; + char *mountPath = NULL; - if(MountFS(pClient, pCmd, &mountPath) == 0) { - result = sd_fat_add_device(path, mountPath, pClient, pCmd); - free(mountPath); - } + 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; + return result; } -int unmount_sd_fat(const char *path) -{ - void *pClient = 0; - void *pCmd = 0; - char *mountPath = 0; +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) - { - UmountFS(pClient, pCmd, mountPath); - FSDelClient(pClient); - free(pClient); - free(pCmd); - free(mountPath); - //FSShutdown(); - } - return result; + int result = sd_fat_remove_device(path, &pClient, &pCmd, &mountPath); + if (result == 0) { + UmountFS(pClient, pCmd, mountPath); + FSDelClient(pClient); + free(pClient); + free(pCmd); + free(mountPath); + //FSShutdown(); + } + return result; } diff --git a/src/tcp_gecko.cpp b/src/tcp_gecko.cpp index a4fd548..0a6a626 100644 --- a/src/tcp_gecko.cpp +++ b/src/tcp_gecko.cpp @@ -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 (result < 0) { - // Error, we won't write any codes - return; + if (cachedTitleID == currentTitleID) { + // log_print("Title ID NOT changed\n"); + } else { + log_print("Title ID changed\n"); + cachedTitleID = currentTitleID; + int result = mount_sd_fat("sd"); + + if (result < 0) { + log_printf("Mounting error: %i\n", result); + return; + } + + unsigned char filePath[CODES_FILE_PATH_SIZE]; + memset(filePath, '0', sizeof(filePath)); + memcpy(filePath, "sd:/codes/", SD_FILE_PATH_HEADER_LENGTH); // File path header + log_printf("Title ID: %lu\n", currentTitleID); + char asciiTitleID[TITLE_ID_LENGTH]; + snprintf(asciiTitleID, TITLE_ID_LENGTH, "%llX", currentTitleID); + memcpy(filePath + SD_FILE_PATH_HEADER_LENGTH + TITLE_ID_LEADING_ZEROS, asciiTitleID, + TITLE_ID_LENGTH); // Title ID + memcpy(filePath + SD_FILE_PATH_HEADER_LENGTH + TITLE_ID_LENGTH, ".gctu", EXTENSION_SIZE); // Extension + filePath[CODES_FILE_PATH_SIZE - 1] = '\0'; // Null-terminated + log_printf("File Path: %s\n", filePath); + + unsigned char *codes = NULL; + unsigned int codesSize = 0; + result = LoadFileToMem((const char *) filePath, &codes, &codesSize); + + if (result < 0) { + log_printf("Reading error: %i\n", result); + // Error, we won't write any codes + goto CLEANUP; + } + + kernelCopyData((unsigned char *) 0x01133000, codes, codesSize); + log_print("Copied!\n"); + + CLEANUP: + + result = unmount_sd_fat("sd"); + + if (result < 0) { + log_printf("Unmounting error: %i\n", result); + } } - - kernelCopyData((unsigned char *) 0x01133000, codes, codesSize); } 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(); diff --git a/tcpgecko.elf b/tcpgecko.elf index 7a26bf0..62f4239 100644 Binary files a/tcpgecko.elf and b/tcpgecko.elf differ