From 3f1d27f7b24c3684a956095aa007aba1727ec4e2 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 30 Jun 2018 16:49:49 +0200 Subject: [PATCH] Replace the "void *" of the fs and overlay access with proper function pointers --- loader/src/utils.cpp | 28 ++++++++-------- src/fs_function_wrapper.c | 70 +++++++++++++++++++-------------------- src/overlay.c | 6 ++-- wups_include/wups/hooks.h | 21 +----------- wups_include/wups/utils.h | 34 +++++++++++++++++++ 5 files changed, 86 insertions(+), 73 deletions(-) diff --git a/loader/src/utils.cpp b/loader/src/utils.cpp index 3b21158..d866e03 100644 --- a/loader/src/utils.cpp +++ b/loader/src/utils.cpp @@ -34,27 +34,25 @@ void CallHookEx(wups_loader_hook_type_t hook_type, int32_t plugin_index_needed) void * func_ptr = hook_data->func_pointer; //TODO: Switch cases depending on arguments etc. // Adding arguments! - if(func_ptr != NULL) { if(hook_type == WUPS_LOADER_HOOK_INIT_FS) { wups_loader_init_fs_args_t args; - args.open_repl = (const void*)&open; - args.close_repl = (const void*)&close; - args.write_repl = (const void*)&write; - args.read_repl = (const void*)&read; - args.lseek_repl = (const void*)&lseek; - args.stat_repl = (const void*)&stat; - args.fstat_repl = (const void*)&fstat; - args.opendir_repl = (const void*)&opendir; - args.closedir_repl = (const void*)&closedir; - args.readdir_repl = (const void*)&readdir; - args.mkdir_repl = (const void*)&mkdir; - + // open is defined as "extern int open (const char *, int, ...);", we are ignoring the varargs part + args.open_repl = (OpenFunction) &open; + args.close_repl = &close; + args.write_repl = &write; + args.read_repl = &read; + args.lseek_repl = &lseek; + args.stat_repl = &stat; + args.fstat_repl = &fstat; + args.opendir_repl = &opendir; + args.closedir_repl = &closedir; + args.readdir_repl = &readdir; + args.mkdir_repl = &mkdir; ((void (*)(wups_loader_init_fs_args_t))((uint32_t*)func_ptr) )(args); } else if(hook_type == WUPS_LOADER_HOOK_INIT_OVERLAY) { wups_loader_init_overlay_args_t args; - args.overlayfunction_ptr = (const void*)&overlay_helper; - + args.overlayfunction_ptr = &overlay_helper; ((void (*)(wups_loader_init_overlay_args_t))((uint32_t*)func_ptr) )(args); } else if(hook_type == WUPS_LOADER_HOOK_INIT_PLUGIN) { ((void (*)(void))((uint32_t*)func_ptr) )(); diff --git a/src/fs_function_wrapper.c b/src/fs_function_wrapper.c index d500dbd..d7c9bd3 100644 --- a/src/fs_function_wrapper.c +++ b/src/fs_function_wrapper.c @@ -7,89 +7,89 @@ #include #include -static void * new_open_ptr __attribute__((section(".data"))) = NULL; -static void * new_close_ptr __attribute__((section(".data"))) = NULL; -static void * new_write_ptr __attribute__((section(".data"))) = NULL; -static void * new_read_ptr __attribute__((section(".data"))) = NULL; -static void * new_lseek_ptr __attribute__((section(".data"))) = NULL; -static void * new_stat_ptr __attribute__((section(".data"))) = NULL; -static void * new_fstat_ptr __attribute__((section(".data"))) = NULL; -static void * new_opendir_ptr __attribute__((section(".data"))) = NULL; -static void * new_closedir_ptr __attribute__((section(".data"))) = NULL; -static void * new_readdir_ptr __attribute__((section(".data"))) = NULL; -static void * new_mkdir_ptr __attribute__((section(".data"))) = NULL; +static OpenFunction new_open_ptr __attribute__((section(".data"))) = NULL; +static CloseFunction new_close_ptr __attribute__((section(".data"))) = NULL; +static WriteFunction new_write_ptr __attribute__((section(".data"))) = NULL; +static ReadFunction new_read_ptr __attribute__((section(".data"))) = NULL; +static LSeekFunction new_lseek_ptr __attribute__((section(".data"))) = NULL; +static StatFunction new_stat_ptr __attribute__((section(".data"))) = NULL; +static FStatFunction new_fstat_ptr __attribute__((section(".data"))) = NULL; +static OpenDirFunction new_opendir_ptr __attribute__((section(".data"))) = NULL; +static CloseDirFunction new_closedir_ptr __attribute__((section(".data"))) = NULL; +static ReadDirFunction new_readdir_ptr __attribute__((section(".data"))) = NULL; +static MKDirFunction new_mkdir_ptr __attribute__((section(".data"))) = NULL; #ifdef __cplusplus extern "C" { #endif void WUPS_InitFS(wups_loader_init_fs_args_t args) { - new_open_ptr = (void*) args.open_repl; - new_close_ptr = (void*) args.close_repl; - new_write_ptr = (void*) args.write_repl; - new_read_ptr = (void*) args.read_repl; - new_lseek_ptr = (void*) args.lseek_repl; - new_stat_ptr = (void*) args.stat_repl; - new_fstat_ptr = (void*) args.fstat_repl; - new_opendir_ptr = (void*) args.opendir_repl; - new_closedir_ptr = (void*) args.closedir_repl; - new_readdir_ptr = (void*) args.readdir_repl; - new_mkdir_ptr = (void*) args.mkdir_repl; + new_open_ptr = args.open_repl; + new_close_ptr = args.close_repl; + new_write_ptr = args.write_repl; + new_read_ptr = args.read_repl; + new_lseek_ptr = args.lseek_repl; + new_stat_ptr = args.stat_repl; + new_fstat_ptr = args.fstat_repl; + new_opendir_ptr = args.opendir_repl; + new_closedir_ptr = args.closedir_repl; + new_readdir_ptr = args.readdir_repl; + new_mkdir_ptr = args.mkdir_repl; } int __real_open(const char *pathname, int flags); int __wrap_open(const char *pathname, int flags) { if(new_open_ptr == NULL) return __real_open(pathname,flags); - return ( (int (*)(const char *,int))((unsigned int*)new_open_ptr) )(pathname,flags); + return new_open_ptr(pathname,flags); } int __real_close(int fd); int __wrap_close(int fd) { if(new_close_ptr == NULL) return __real_close(fd); - return ( (int (*)(int))((unsigned int*)new_close_ptr) )(fd); + return new_close_ptr(fd); } ssize_t __real_write(int fd, const void *buf, size_t count); ssize_t __wrap_write(int fd, const void *buf, size_t count) { if(new_write_ptr == NULL) return __real_write(fd,buf,count); - return ( (ssize_t (*)(int, const void *, size_t))((unsigned int*)new_write_ptr) )(fd,buf,count); + return new_write_ptr(fd,buf,count); } -ssize_t __real_read(int fd, const void *buf, size_t count); -ssize_t __wrap_read(int fd, const void *buf, size_t count) { +ssize_t __real_read(int fd, void *buf, size_t count); +ssize_t __wrap_read(int fd, void *buf, size_t count) { if(new_read_ptr == NULL) return __real_read(fd,buf,count); - return ( (ssize_t (*)(int, const void *, size_t))((unsigned int*)new_read_ptr) )(fd,buf,count); + return new_read_ptr(fd,buf,count); } off_t __real_lseek(int fd, off_t offset, int whence); off_t __wrap_lseek(int fd, off_t offset, int whence) { if(new_lseek_ptr == NULL) return __real_lseek(fd, offset, whence); - return ( (off_t (*)(int, off_t, int))((unsigned int*)new_lseek_ptr) )(fd, offset, whence); + return new_lseek_ptr(fd, offset, whence); } int __real_stat(const char *pathname, struct stat *statbuf); int __wrap_stat(const char *pathname, struct stat *statbuf) { if(new_stat_ptr == NULL) return __real_stat(pathname,statbuf); - return ( (int (*)(const char *, struct stat *))((unsigned int*)new_stat_ptr) )(pathname,statbuf); + return new_stat_ptr(pathname,statbuf); } int __real_fstat(int fd, struct stat *statbuf); int __wrap_fstat(int fd, struct stat *statbuf) { if(new_fstat_ptr == NULL) return __real_fstat(fd,statbuf); - return ( (int (*)(int, struct stat *))((unsigned int*)new_fstat_ptr) )(fd,statbuf); + return new_fstat_ptr(fd,statbuf); } DIR* __real_opendir(const char * arg); DIR* __wrap_opendir(const char * arg) { if(new_opendir_ptr == NULL) return __real_opendir(arg); - return ( (DIR* (*)(const char *))((unsigned int*)new_opendir_ptr) )(arg); + return new_opendir_ptr(arg); } int __real_closedir(DIR *dirp); int __wrap_closedir(DIR *dirp) { if(new_closedir_ptr == NULL) return __real_closedir(dirp); - return ( (int (*)(DIR *))((unsigned int*)new_closedir_ptr) )(dirp); + return new_closedir_ptr(dirp); } struct dirent * __real_readdir(DIR *dirp); struct dirent * __wrap_readdir(DIR *dirp) { if(new_readdir_ptr == NULL) return __real_readdir(dirp); - return ( (struct dirent * (*)(DIR *))((unsigned int*)new_readdir_ptr) )(dirp); + return new_readdir_ptr(dirp); } int __real_mkdir(const char *path, mode_t mode); int __wrap_mkdir(const char *path, mode_t mode) { if(new_mkdir_ptr == NULL) return __real_mkdir(path, mode); - return ( (int (*)(const char *, mode_t))((unsigned int*) new_mkdir_ptr) )(path, mode); + return new_mkdir_ptr(path, mode); } #ifdef __cplusplus diff --git a/src/overlay.c b/src/overlay.c index 66469ce..0f56503 100644 --- a/src/overlay.c +++ b/src/overlay.c @@ -21,13 +21,13 @@ void OSScreenPutFontEx(uint32_t bufferNum, uint32_t posX, uint32_t posY, const c void OSScreenEnableEx(uint32_t bufferNum, int32_t enable); void OSScreenPutPixelEx(uint32_t bufferNum, uint32_t posX, uint32_t posY, uint32_t color); -static void * overlayfunction_ptr __attribute__((section(".data"))) = NULL; +static OverlayOpenFunction overlayfunction_ptr __attribute__((section(".data"))) = NULL; #ifdef __cplusplus extern "C" { #endif void WUPS_InitOverlay(wups_loader_init_overlay_args_t args) { - overlayfunction_ptr = (void*) args.overlayfunction_ptr; + overlayfunction_ptr = args.overlayfunction_ptr; } void WUPS_Overlay_PrintTextOnScreen(wups_overlay_options_type_t screen, int x,int y, const char * msg, ...) { @@ -80,7 +80,7 @@ void WUPS_Overlay_FlipBuffers(wups_overlay_options_type_t screen) { void WUPS_OpenOverlay(wups_overlay_options_type_t screen, overlay_callback callback) { if(overlayfunction_ptr != NULL) { - ( (void (*)(wups_overlay_options_type_t, overlay_callback))((unsigned int*)overlayfunction_ptr) )(screen,callback); + overlayfunction_ptr(screen,callback); } } diff --git a/wups_include/wups/hooks.h b/wups_include/wups/hooks.h index afcdef3..72d8df2 100644 --- a/wups_include/wups/hooks.h +++ b/wups_include/wups/hooks.h @@ -19,6 +19,7 @@ #define WUPS_HOOKS_DEF_H_ #include "common.h" +#include "utils.h" #ifdef __cplusplus extern "C" { @@ -57,31 +58,11 @@ typedef enum wups_loader_app_status_t { WUPS_APP_STATUS_UNKNOWN, /* Unknown status _should_ never happen.*/ } wups_loader_app_status_t; -typedef struct wups_loader_init_overlay_args_t { - const void * overlayfunction_ptr; -} wups_loader_init_overlay_args_t; - -typedef struct wups_loader_init_fs_args_t { - const void * open_repl; - const void * close_repl; - const void * write_repl; - const void * read_repl; - const void * lseek_repl; - const void * stat_repl; - const void * fstat_repl; - const void * opendir_repl; - const void * closedir_repl; - const void * readdir_repl; - const void * mkdir_repl; -} wups_loader_init_fs_args_t; - - typedef struct wups_loader_app_started_args_t { bool sd_mounted; bool usb_mounted; } wups_loader_app_started_args_t; - #define WUPS_FS_ACCESS() \ void init_fs(wups_loader_init_fs_args_t);\ WUPS_HOOK_EX(WUPS_LOADER_HOOK_INIT_FS,init_fs); \ diff --git a/wups_include/wups/utils.h b/wups_include/wups/utils.h index 3304da6..f5a28ea 100644 --- a/wups_include/wups/utils.h +++ b/wups_include/wups/utils.h @@ -35,6 +35,40 @@ wups_overlay_options_type_t; typedef void (*overlay_callback)(wups_overlay_options_type_t); +typedef void (*OverlayOpenFunction)(wups_overlay_options_type_t screen, overlay_callback callback); + +typedef struct wups_loader_init_overlay_args_t { + OverlayOpenFunction overlayfunction_ptr; +} wups_loader_init_overlay_args_t; + +typedef int (*OpenFunction) (const char *pathname, int flags); +typedef ssize_t (*WriteFunction) (int fd, const void *buf, size_t count); +typedef int (*CloseFunction) (int fd); +typedef ssize_t (*ReadFunction) (int fd, void *buf, size_t count); +typedef off_t (*LSeekFunction) (int fd, off_t offset, int whence); +typedef int (*StatFunction) (const char *pathname, struct stat *statbuf); +typedef int (*FStatFunction) (int fd, struct stat *statbuf); +typedef DIR* (*OpenDirFunction) (const char * arg); +typedef int (*CloseDirFunction) (DIR *dirp); +typedef struct dirent * (*ReadDirFunction) (DIR *dirp); +typedef int (*MKDirFunction) (const char *path, mode_t mode); + +typedef struct wups_loader_init_fs_args_t { + OpenFunction open_repl; + CloseFunction close_repl; + WriteFunction write_repl; + ReadFunction read_repl; + LSeekFunction lseek_repl; + StatFunction stat_repl; + FStatFunction fstat_repl; + OpenDirFunction opendir_repl; + CloseDirFunction closedir_repl; + ReadDirFunction readdir_repl; + MKDirFunction mkdir_repl; +} wups_loader_init_fs_args_t; + + + /* Gets called by the framework */