From 797e58f57661f41eba371e7f01a3fac84076606c Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 16 Feb 2018 21:51:35 +0100 Subject: [PATCH] [General] Moving the FS-wrapper into an own lib, no more macro madness The library now needs to be build with "make" and installed with "make install". Make sure to include the -lwups into the plugin projecs. If you call "WUPS_InitFS(args);" in the INITIALIZE method, the plugin has full SD/USB access. --- .gitignore | 1 + .travis.yml | 2 +- Makefile | 127 +++++++++++++++++++++++++++++--- plugins/example_plugin/Makefile | 5 +- plugins/hid_to_vpad/Makefile | 5 +- plugins/padcon/Makefile | 5 +- plugins/sdcafiine/Makefile | 7 +- plugins/sdcafiine/src/main.cpp | 1 + plugins/swipswapme/Makefile | 5 +- src/fs_function_wrapper.c | 92 +++++++++++++++++++++++ wups_include/wups.h | 79 +------------------- 11 files changed, 230 insertions(+), 99 deletions(-) create mode 100644 src/fs_function_wrapper.c diff --git a/.gitignore b/.gitignore index 8418054..ebb316c 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ plugins/*/bin/* plugins/*/build/* loader/WiiUPluginLoader.cscope_file_list loader/WiiUPluginLoader.layout +release/* *.mod *.cbp diff --git a/.travis.yml b/.travis.yml index 73157b3..9d239d5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,7 +52,7 @@ install: - (cd controller_patcher-master && make -j8 && make install) script: -- make +- make && make install - (cd loader && make) - (cd plugins && make) diff --git a/Makefile b/Makefile index eab5791..bfb94b0 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,101 @@ -#--------------------------------------------------------------------------------- -# Clear the implicit built in rules +DO_LOGGING := 0 + #--------------------------------------------------------------------------------- .SUFFIXES: #--------------------------------------------------------------------------------- ifeq ($(strip $(DEVKITPPC)),) -$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=devkitPPC") +$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=devkitPPC) endif -ifeq ($(strip $(DEVKITPRO)),) -$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=devkitPRO") + +include $(DEVKITPPC)/wii_rules + +#--------------------------------------------------------------------------------- +# BUILD is the directory where object files & intermediate files will be placed +# SOURCES is a list of directories containing source code +# INCLUDES is a list of directories containing extra header files +# DATA is a list of directories containing binary files +# LIBDIR is where the built library will be placed +# all directories are relative to this makefile +#--------------------------------------------------------------------------------- +BUILD ?= release +SOURCES := src + +INCLUDES := src \ + wups_include +DATA := +LIB := lib + +#--------------------------------------------------------------------------------- +# options for code generation +#--------------------------------------------------------------------------------- +CFLAGS = -g -Os -Wall -D__wiiu__ -D_GNU_SOURCE $(MACHDEP) $(INCLUDE) +CXXFLAGS = $(CFLAGS) + +ifeq ($(DO_LOGGING), 1) + CFLAGS += -D__LOGGING__ + CXXFLAGS += -D__LOGGING__ endif +ASFLAGS := -mregnames + +export WIIUBIN := $(LIB)/libwups.a + +#--------------------------------------------------------------------------------- +# any extra libraries we wish to link with the project +#--------------------------------------------------------------------------------- +LIBS := + +#--------------------------------------------------------------------------------- +# list of directories containing libraries, this must be the top level containing +# include and lib +#--------------------------------------------------------------------------------- +LIBDIRS := + +#--------------------------------------------------------------------------------- +# no real need to edit anything past this point unless you need to add additional +# rules for different file extensions +#--------------------------------------------------------------------------------- +ifneq ($(BUILD),$(notdir $(CURDIR))) +#--------------------------------------------------------------------------------- + +export TOPDIR ?= $(CURDIR)/.. +export DEPSDIR := $(CURDIR)/$(BUILD) + +export INCLUDEDIR := $(PORTLIBS)/include/libgui + +export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ + $(foreach dir,$(DATA),$(CURDIR)/$(dir)) + +CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) +CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) +sFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) +SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S))) +BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) + + +export OFILES := $(addsuffix .o,$(BINFILES)) \ + $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) $(sFILES:.s=.o) + +export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ + $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ + $(foreach dir,$(LIBDIRS),-I$(dir)/include) + +export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) + +.PHONY: $(BUILD) clean + +#--------------------------------------------------------------------------------- +$(BUILD): + @[ -d $@ ] || mkdir -p $@ + @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile + +#--------------------------------------------------------------------------------- +clean: + @echo clean ... + @rm -fr debug release $(LIB) include + +all: $(WIIUBIN) + WUPSDIR := $(DEVKITPRO)/wups # Rule to install wups. @@ -17,11 +103,32 @@ PHONY += install install : wups.ld wups_elf.ld $(addprefix $Qrm -rf ,$(wildcard $(WUPSDIR))) $Qmkdir $(WUPSDIR) + $Qmkdir $(WUPSDIR)/lib/ $Qcp -r wups_include $(WUPSDIR)/include $Qcp -r wups.ld $(WUPSDIR) $Qcp -r wups_elf.ld $(WUPSDIR) - -# Rule to install wups. -PHONY += uninstall -uninstall : - $(addprefix $Qrm -rf ,$(wildcard $(WUPSDIR))) \ No newline at end of file + @cp $(BUILD)/lib/libwups.a $(WUPSDIR)/lib/ + +#--------------------------------------------------------------------------------- +else + +DEPENDS := $(OFILES:.o=.d) + +#--------------------------------------------------------------------------------- +# main targets +#--------------------------------------------------------------------------------- +$(WIIUBIN) : $(OFILES) $(LIB) + @rm -f "$(WIIUBIN)" + @$(AR) rcs "$(WIIUBIN)" $(OFILES) + @echo built ... $(notdir $@) + + +$(LIB): + mkdir $(LIB) + +-include $(DEPENDS) + +#--------------------------------------------------------------------------------------- +endif +#--------------------------------------------------------------------------------------- + diff --git a/plugins/example_plugin/Makefile b/plugins/example_plugin/Makefile index 7296bb4..a9f10c7 100644 --- a/plugins/example_plugin/Makefile +++ b/plugins/example_plugin/Makefile @@ -96,7 +96,7 @@ MAKEFLAGS += --no-print-directory #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project #--------------------------------------------------------------------------------- -LIBS := -lutils -ldynamiclibs +LIBS := -lwups -lutils -ldynamiclibs # #--------------------------------------------------------------------------------- @@ -153,7 +153,8 @@ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ # build a list of library paths #--------------------------------------------------------------------------------- export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) \ - -L$(PORTLIBS)/lib + -L$(PORTLIBS)/lib \ + -L$(WUPSDIR)/lib export OUTPUT := $(CURDIR)/$(TARGET) .PHONY: $(BUILD) clean install diff --git a/plugins/hid_to_vpad/Makefile b/plugins/hid_to_vpad/Makefile index f7d8f75..45b6da6 100644 --- a/plugins/hid_to_vpad/Makefile +++ b/plugins/hid_to_vpad/Makefile @@ -99,7 +99,7 @@ MAKEFLAGS += --no-print-directory #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project #--------------------------------------------------------------------------------- -LIBS := -lcontrollerpatcher -lutils -ldynamiclibs +LIBS := -lwups -lcontrollerpatcher -lutils -ldynamiclibs # #--------------------------------------------------------------------------------- @@ -156,7 +156,8 @@ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ # build a list of library paths #--------------------------------------------------------------------------------- export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) \ - -L$(PORTLIBS)/lib + -L$(PORTLIBS)/lib \ + -L$(WUPSDIR)/lib export OUTPUT := $(CURDIR)/$(TARGET) .PHONY: $(BUILD) clean install diff --git a/plugins/padcon/Makefile b/plugins/padcon/Makefile index 179c086..d89c552 100644 --- a/plugins/padcon/Makefile +++ b/plugins/padcon/Makefile @@ -97,7 +97,7 @@ MAKEFLAGS += --no-print-directory #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project #--------------------------------------------------------------------------------- -LIBS := -lutils -ldynamiclibs +LIBS := -lwups -lutils -ldynamiclibs # #--------------------------------------------------------------------------------- @@ -154,7 +154,8 @@ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ # build a list of library paths #--------------------------------------------------------------------------------- export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) \ - -L$(PORTLIBS)/lib + -L$(PORTLIBS)/lib \ + -L$(WUPSDIR)/lib export OUTPUT := $(CURDIR)/$(TARGET) .PHONY: $(BUILD) clean install diff --git a/plugins/sdcafiine/Makefile b/plugins/sdcafiine/Makefile index 22e9221..6d5d176 100644 --- a/plugins/sdcafiine/Makefile +++ b/plugins/sdcafiine/Makefile @@ -100,7 +100,7 @@ MAKEFLAGS += --no-print-directory #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project #--------------------------------------------------------------------------------- -LIBS := -liosuhax -lfswrapper -lutils -ldynamiclibs +LIBS := -lwups -liosuhax -lfswrapper -lutils -ldynamiclibs # #--------------------------------------------------------------------------------- @@ -152,13 +152,14 @@ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ -I$(CURDIR)/$(BUILD) -I$(PORTLIBS)/include \ -I$(PORTLIBS)/include/libutils -I$(PORTLIBS)/include/libfswrapper \ - -I$(WUPSDIR)/include + -I$(WUPSDIR)/include #--------------------------------------------------------------------------------- # build a list of library paths #--------------------------------------------------------------------------------- export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) \ - -L$(PORTLIBS)/lib + -L$(PORTLIBS)/lib \ + -L$(WUPSDIR)/lib export OUTPUT := $(CURDIR)/$(TARGET) .PHONY: all $(BUILD) clean install diff --git a/plugins/sdcafiine/src/main.cpp b/plugins/sdcafiine/src/main.cpp index 51b76aa..54948a8 100644 --- a/plugins/sdcafiine/src/main.cpp +++ b/plugins/sdcafiine/src/main.cpp @@ -20,6 +20,7 @@ WUPS_MODULE_AUTHOR("Maschell"); WUPS_MODULE_LICENSE("GPL"); INITIALIZE(args){ + WUPS_InitFS(args); InitOSFunctionPointers(); InitSocketFunctionPointers(); //For logging InitVPadFunctionPointers(); //For logging diff --git a/plugins/swipswapme/Makefile b/plugins/swipswapme/Makefile index f68cc76..51d4093 100644 --- a/plugins/swipswapme/Makefile +++ b/plugins/swipswapme/Makefile @@ -99,7 +99,7 @@ MAKEFLAGS += --no-print-directory #--------------------------------------------------------------------------------- # any extra libraries we wish to link with the project #--------------------------------------------------------------------------------- -LIBS := -lutils -ldynamiclibs +LIBS := -lwups -lutils -ldynamiclibs # #--------------------------------------------------------------------------------- @@ -156,7 +156,8 @@ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ # build a list of library paths #--------------------------------------------------------------------------------- export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) \ - -L$(PORTLIBS)/lib + -L$(PORTLIBS)/lib \ + -L$(WUPSDIR)/lib export OUTPUT := $(CURDIR)/$(TARGET) .PHONY: $(BUILD) clean install diff --git a/src/fs_function_wrapper.c b/src/fs_function_wrapper.c new file mode 100644 index 0000000..1836fa9 --- /dev/null +++ b/src/fs_function_wrapper.c @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include +#include +#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; + +#ifdef __cplusplus + extern "C" { +#endif + void WUPS_InitFS(wups_loader_init_args_t* args){ + if(args != NULL){ + new_open_ptr = (void*) args->fs_wrapper.open_repl; + new_close_ptr = (void*) args->fs_wrapper.close_repl; + new_write_ptr = (void*) args->fs_wrapper.write_repl; + new_read_ptr = (void*) args->fs_wrapper.read_repl; + new_lseek_ptr = (void*) args->fs_wrapper.lseek_repl; + new_stat_ptr = (void*) args->fs_wrapper.stat_repl; + new_fstat_ptr = (void*) args->fs_wrapper.fstat_repl; + new_opendir_ptr = (void*) args->fs_wrapper.opendir_repl; + new_closedir_ptr = (void*) args->fs_wrapper.closedir_repl; + new_readdir_ptr = (void*) args->fs_wrapper.readdir_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); + } + 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); + } + 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); + } + ssize_t __real_read(int fd, const void *buf, size_t count); + ssize_t __wrap_read(int fd, const 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); + } + 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); + } + 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); + } + 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); + } + 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); + } + 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); + } + 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); + } + +#ifdef __cplusplus + } +#endif \ No newline at end of file diff --git a/wups_include/wups.h b/wups_include/wups.h index 0d66843..95c7567 100644 --- a/wups_include/wups.h +++ b/wups_include/wups.h @@ -113,8 +113,6 @@ typedef enum wups_loader_library_type_t { WUPS_LOADER_LIBRARY_ZLIB125, } wups_loader_library_type_t; - - typedef enum wups_loader_hook_type_t { WUPS_LOADER_HOOK_INIT_FUNCTION } wups_loader_hook_type_t; @@ -173,86 +171,11 @@ typedef struct wups_loader_init_args_t { void init(wups_loader_init_args_t*);\ void myInit(wups_loader_init_args_t*);\ WUPS_HOOK_INIT(init); \ - 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; \ void init(wups_loader_init_args_t* args){ \ if(args != NULL){\ - new_open_ptr = (void*) args->fs_wrapper.open_repl; \ - new_close_ptr = (void*) args->fs_wrapper.close_repl; \ - new_write_ptr = (void*) args->fs_wrapper.write_repl; \ - new_read_ptr = (void*) args->fs_wrapper.read_repl; \ - new_lseek_ptr = (void*) args->fs_wrapper.lseek_repl; \ - new_stat_ptr = (void*) args->fs_wrapper.stat_repl; \ - new_fstat_ptr = (void*) args->fs_wrapper.fstat_repl; \ - new_opendir_ptr = (void*) args->fs_wrapper.opendir_repl; \ - new_closedir_ptr = (void*) args->fs_wrapper.closedir_repl; \ - new_readdir_ptr = (void*) args->fs_wrapper.readdir_repl; \ }\ myInit(args);\ } \ - \ - EXTERN_C_START \ - \ - 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);\ - }\ - 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);\ - }\ - 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);\ - }\ - ssize_t __real_read(int fd, const void *buf, size_t count);\ - ssize_t __wrap_read(int fd, const 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);\ - }\ - 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);\ - }\ - 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);\ - }\ - 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);\ - }\ - 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);\ - }\ - 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);\ - }\ - 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);\ - }\ - \ - EXTERN_C_END\ void myInit(wups_loader_init_args_t* my_args) typedef enum wups_loader_entry_type_t { @@ -297,6 +220,8 @@ typedef struct wups_loader_entry_t { #define WUPS_MODULE_VERSION(x) WUPS_META(version, x) #define WUPS_MODULE_LICENSE(x) WUPS_META(license, x) +void WUPS_InitFS(wups_loader_init_args_t* args); + #ifdef __cplusplus } #endif