diff --git a/iop/Makefile b/iop/Makefile index 125d5a2623f..443ec9cb705 100644 --- a/iop/Makefile +++ b/iop/Makefile @@ -24,7 +24,8 @@ SUBDIRS = \ startup \ system \ tcpip \ - usb + usb \ + utilities include $(PS2SDKSRC)/Defs.make include $(PS2SDKSRC)/Rules.make diff --git a/iop/sio/mx4sio_bd/Makefile b/iop/sio/mx4sio_bd/Makefile index ddbbd06c4c5..59393359551 100644 --- a/iop/sio/mx4sio_bd/Makefile +++ b/iop/sio/mx4sio_bd/Makefile @@ -6,12 +6,22 @@ # Licenced under Academic Free License version 2.0 # Review ps2sdk README & LICENSE files for further details. -IOP_INCS += -I$(PS2SDKSRC)/iop/fs/bdm/include -I$(PS2SDKSRC)/iop/system/sio2man/include +MODHOOK_PATH = $(PS2SDKSRC)/iop/utilities/modhook/ +IOP_INCS += -I$(PS2SDKSRC)/iop/fs/bdm/include -I$(PS2SDKSRC)/iop/system/sio2man/include -I$(MODHOOK_PATH)include +IOP_LIB_ARCHIVES = $(MODHOOK_PATH)lib/libmodhook.a -IOP_OBJS += spi_sdcard_crc7.o spi_sdcard_driver.o crc16.o ioplib.o sio2man_hook.o mx4sio.o imports.o +IOP_OBJS += spi_sdcard_crc7.o spi_sdcard_driver.o crc16.o sio2man_hook.o mx4sio.o imports.o IOP_LDFLAGS = -lgcc + +$(MODHOOK_PATH)lib/libmodhook.a: + $(MAKEREC) $(MODHOOK_PATH) + +.NOTPARALLEL:: \ + $(IOP_LIB_ARCHIVES) + + include $(PS2SDKSRC)/Defs.make include $(PS2SDKSRC)/iop/Rules.bin.make include $(PS2SDKSRC)/iop/Rules.make diff --git a/iop/sio/mx4sio_bd/src/ioplib.h b/iop/sio/mx4sio_bd/src/ioplib.h deleted file mode 100644 index 7ff39a4940d..00000000000 --- a/iop/sio/mx4sio_bd/src/ioplib.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef IOPLIB_H -#define IOPLIB_H - - -#include - - -iop_library_t *ioplib_getByName(const char *name); -unsigned int ioplib_getTableSize(iop_library_t *lib); -void *ioplib_hookExportEntry(iop_library_t *lib, unsigned int entry, void *func); -void ioplib_relinkExports(iop_library_t *lib); - - -#endif diff --git a/iop/sio/mx4sio_bd/src/mx4sio.c b/iop/sio/mx4sio_bd/src/mx4sio.c index ac96710f326..01acf5e58bd 100644 --- a/iop/sio/mx4sio_bd/src/mx4sio.c +++ b/iop/sio/mx4sio_bd/src/mx4sio.c @@ -7,9 +7,10 @@ #include #include +#include + #include "mx4sio.h" #include "crc16.h" -#include "ioplib.h" #include "sio2man_hook.h" #include "sio2regs.h" #include "spi_sdcard_driver.h" @@ -793,7 +794,7 @@ int module_start(int argc, char *argv[]) goto error4; } - lib_modload = ioplib_getByName("modload"); + lib_modload = modhook_getModule("modload"); if (lib_modload != NULL) { M_DEBUG("modload 0x%x detected\n", lib_modload->version); // Newer modload versions allow modules to be unloaded diff --git a/iop/sio/mx4sio_bd/src/sio2man_hook.c b/iop/sio/mx4sio_bd/src/sio2man_hook.c index 33e3d1a5a08..09bac6c3d40 100644 --- a/iop/sio/mx4sio_bd/src/sio2man_hook.c +++ b/iop/sio/mx4sio_bd/src/sio2man_hook.c @@ -1,8 +1,8 @@ #include #include #include +#include -#include "ioplib.h" #include "sio2man.h" #include "sio2man_hook.h" @@ -97,26 +97,26 @@ static void _sio2man_unhook(iop_library_t *lib) return; } - ioplib_hookExportEntry(lib, 23, _23_psio2_pad_transfer_init); - ioplib_hookExportEntry(lib, 24, _24_psio2_mc_transfer_init); - ioplib_hookExportEntry(lib, 25, _25_psio2_transfer); - ioplib_hookExportEntry(lib, 26, _26_psio2_transfer_reset); - ioplib_hookExportEntry(lib, 46, _46_psio2_pad_transfer_init); - ioplib_hookExportEntry(lib, 47, _47_psio2_mc_transfer_init); - ioplib_hookExportEntry(lib, 48, _48_psio2_mtap_transfer_init); + modhookk_hookExportEntry(lib, 23, _23_psio2_pad_transfer_init); + modhook_hookExportEntry(lib, 24, _24_psio2_mc_transfer_init); + modhook_hookExportEntry(lib, 25, _25_psio2_transfer); + modhookk_hookExportEntry(lib, 26, _26_psio2_transfer_reset); + modhook_hookExportEntry(lib, 46, _46_psio2_pad_transfer_init); + modhook_hookExportEntry(lib, 47, _47_psio2_mc_transfer_init); + modhook_hookExportEntry(lib, 48, _48_psio2_mtap_transfer_init); if ((hooked_version >= IRX_VER(1, 2)) && (hooked_version < IRX_VER(2, 0))) { // Only for the newer rom0:XSIO2MAN // Assume all v1.x libraries to use this interface (reset at 50) - ioplib_hookExportEntry(lib, 49, _51_psio2_transfer); - ioplib_hookExportEntry(lib, 50, _52_psio2_transfer_reset); + modhook_hookExportEntry(lib, 49, _51_psio2_transfer); + modhook_hookExportEntry(lib, 50, _52_psio2_transfer_reset); } else /*if (hooked_version >= IRX_VER(2, 3))*/ { // Only for the newer rom1:SIO2MAN // Assume all v2.x libraries to use this interface (reset at 52) - ioplib_hookExportEntry(lib, 49, _49_psio2_rm_transfer_init); - ioplib_hookExportEntry(lib, 50, _50_psio2_unk_transfer_init); - ioplib_hookExportEntry(lib, 51, _51_psio2_transfer); - ioplib_hookExportEntry(lib, 52, _52_psio2_transfer_reset); + modhook_hookExportEntry(lib, 49, _49_psio2_rm_transfer_init); + modhook_hookExportEntry(lib, 50, _50_psio2_unk_transfer_init); + modhook_hookExportEntry(lib, 51, _51_psio2_transfer); + modhook_hookExportEntry(lib, 52, _52_psio2_transfer_reset); } hooked_version = 0; @@ -134,29 +134,29 @@ static void _sio2man_hook(iop_library_t *lib) if (lib->version > IRX_VER(1, 1)) { M_DEBUG("Installing sio2man hooks for version 0x%x\n", lib->version); - _23_psio2_pad_transfer_init = ioplib_hookExportEntry(lib, 23, _23_sio2_pad_transfer_init); + _23_psio2_pad_transfer_init = modhook_hookExportEntry(lib, 23, _23_sio2_pad_transfer_init); // Lock sio2 to prevent race conditions with MC/PAD libraries _23_psio2_pad_transfer_init(); - _24_psio2_mc_transfer_init = ioplib_hookExportEntry(lib, 24, _24_sio2_mc_transfer_init); - _25_psio2_transfer = ioplib_hookExportEntry(lib, 25, _25_sio2_transfer); - _26_psio2_transfer_reset = ioplib_hookExportEntry(lib, 26, _26_sio2_transfer_reset); - _46_psio2_pad_transfer_init = ioplib_hookExportEntry(lib, 46, _46_sio2_pad_transfer_init); - _47_psio2_mc_transfer_init = ioplib_hookExportEntry(lib, 47, _47_sio2_mc_transfer_init); - _48_psio2_mtap_transfer_init = ioplib_hookExportEntry(lib, 48, _48_sio2_mtap_transfer_init); + _24_psio2_mc_transfer_init = modhook_hookExportEntry(lib, 24, _24_sio2_mc_transfer_init); + _25_psio2_transfer = modhook_hookExportEntry(lib, 25, _25_sio2_transfer); + _26_psio2_transfer_reset = modhook_hookExportEntry(lib, 26, _26_sio2_transfer_reset); + _46_psio2_pad_transfer_init = modhook_hookExportEntry(lib, 46, _46_sio2_pad_transfer_init); + _47_psio2_mc_transfer_init = modhook_hookExportEntry(lib, 47, _47_sio2_mc_transfer_init); + _48_psio2_mtap_transfer_init = modhook_hookExportEntry(lib, 48, _48_sio2_mtap_transfer_init); if ((lib->version >= IRX_VER(1, 2)) && (lib->version < IRX_VER(2, 0))) { // Only for the newer rom0:XSIO2MAN // Assume all v1.x libraries to use this interface (reset at 50) - _51_psio2_transfer = ioplib_hookExportEntry(lib, 49, _51_sio2_transfer); - _52_psio2_transfer_reset = ioplib_hookExportEntry(lib, 50, _52_sio2_transfer_reset); + _51_psio2_transfer = modhook_hookExportEntry(lib, 49, _51_sio2_transfer); + _52_psio2_transfer_reset = modhook_hookExportEntry(lib, 50, _52_sio2_transfer_reset); } else /*if (lib->version >= IRX_VER(2, 3))*/ { // Only for the newer rom1:SIO2MAN // Assume all v2.x libraries to use this interface (reset at 52) - _49_psio2_rm_transfer_init = ioplib_hookExportEntry(lib, 49, _49_sio2_rm_transfer_init); - _50_psio2_unk_transfer_init = ioplib_hookExportEntry(lib, 50, _50_sio2_unk_transfer_init); - _51_psio2_transfer = ioplib_hookExportEntry(lib, 51, _51_sio2_transfer); - _52_psio2_transfer_reset = ioplib_hookExportEntry(lib, 52, _52_sio2_transfer_reset); + _49_psio2_rm_transfer_init = modhook_hookExportEntry(lib, 49, _49_sio2_rm_transfer_init); + _50_psio2_unk_transfer_init = modhook_hookExportEntry(lib, 50, _50_sio2_unk_transfer_init); + _51_psio2_transfer = modhook_hookExportEntry(lib, 51, _51_sio2_transfer); + _52_psio2_transfer_reset = modhook_hookExportEntry(lib, 52, _52_sio2_transfer_reset); } // Unlock sio2 @@ -195,19 +195,19 @@ int sio2man_hook_init() lock_sema2 = CreateSema(&sema); // Hook into 'loadcore' so we know when sio2man is loaded in the future - lib = ioplib_getByName("loadcore"); + lib = modhook_getModule("loadcore"); if (lib == NULL) { DeleteSema(lock_sema); DeleteSema(lock_sema2); return -1; } - pRegisterLibraryEntries = ioplib_hookExportEntry(lib, 6, hookRegisterLibraryEntries); + pRegisterLibraryEntries = modhook_hookExportEntry(lib, 6, hookRegisterLibraryEntries); // Hook into 'sio2man' now if it's already loaded - lib = ioplib_getByName("sio2man"); + lib = modhook_getModule("sio2man"); if (lib != NULL) { _sio2man_hook(lib); - ioplib_relinkExports(lib); + modhook_relinkExports(lib); } return 0; @@ -220,14 +220,14 @@ void sio2man_hook_deinit() M_DEBUG("%s\n", __FUNCTION__); // Unhook 'sio2man' - lib = ioplib_getByName("sio2man"); + lib = modhook_getModule("sio2man"); if (lib != NULL) { _sio2man_unhook(lib); - ioplib_relinkExports(lib); + modhook_relinkExports(lib); } // Unhook 'loadcore' - ioplib_hookExportEntry(lib, 6, pRegisterLibraryEntries); + modhook_hookExportEntry(lib, 6, pRegisterLibraryEntries); // Delete locking semaphore DeleteSema(lock_sema); diff --git a/iop/utilities/Makefile b/iop/utilities/Makefile new file mode 100644 index 00000000000..5aa4e74dfa5 --- /dev/null +++ b/iop/utilities/Makefile @@ -0,0 +1,12 @@ +# _____ ___ ____ ___ ____ +# ____| | ____| | | |____| +# | ___| |____ ___| ____| | \ PS2DEV Open Source Project. +#----------------------------------------------------------------------- +# Copyright 2001-2004, ps2dev - http://www.ps2dev.org +# Licenced under Academic Free License version 2.0 +# Review ps2sdk README & LICENSE files for further details. + +SUBDIRS = modhook + +include $(PS2SDKSRC)/Defs.make +include $(PS2SDKSRC)/Rules.make diff --git a/iop/utilities/modhook/Makefile b/iop/utilities/modhook/Makefile new file mode 100644 index 00000000000..eacbc473221 --- /dev/null +++ b/iop/utilities/modhook/Makefile @@ -0,0 +1,15 @@ +# _____ ___ ____ ___ ____ +# ____| | ____| | | |____| +# | ___| |____ ___| ____| | \ PS2DEV Open Source Project. +#----------------------------------------------------------------------- +# Copyright 2001-2024, ps2dev - http://www.ps2dev.org +# Licenced under Academic Free License version 2.0 +# Review ps2sdk README & LICENSE files for further details. + +IOP_OBJS = modhook.o +IOP_LIB = libmodhook.a + +include $(PS2SDKSRC)/Defs.make +include $(PS2SDKSRC)/iop/Rules.lib.make +include $(PS2SDKSRC)/iop/Rules.make +include $(PS2SDKSRC)/iop/Rules.release diff --git a/iop/utilities/modhook/include/modhook.h b/iop/utilities/modhook/include/modhook.h new file mode 100644 index 00000000000..913c7ff3660 --- /dev/null +++ b/iop/utilities/modhook/include/modhook.h @@ -0,0 +1,36 @@ +#ifndef __MODHOOK_H__ +#define __MODHOOK_H__ + +#include + +/** + * @file modhook.c + * @brief IOP module manipulation library for hooking exports. + * @note depends on: `CpuSuspendIntr` `CpuResumeIntr` `GetLoadcoreInternalData` + */ + +/** + * @brief returns an iop library pointer for the specified IRX module + * @returns NULL on error, else, a pointer to the struct + */ +iop_library_t *modhook_getModule(const char *name); + +/** + * @brief returns the size of the export table for the specified module + * @param lib the library to obtain the export table size + * @returns the ammount of exports registered for that module + */ +unsigned int modhook_getTableSize(iop_library_t *lib); + +/** + * @brief replaces the function called as a module export + * @param lib The iop_library_t struct of the module to modify + * @param entry the export number to be modified + * @param func the function to replace the export with + * @returns a pointer to the original function + */ +void *modhook_hookExportEntry(iop_library_t *lib, unsigned int entry, void *func); +void modhook_relinkExports(iop_library_t *lib); + + +#endif \ No newline at end of file diff --git a/iop/sio/mx4sio_bd/src/ioplib.c b/iop/utilities/modhook/src/modhook.c similarity index 78% rename from iop/sio/mx4sio_bd/src/ioplib.c rename to iop/utilities/modhook/src/modhook.c index 59028a3de81..6f4153c8572 100644 --- a/iop/sio/mx4sio_bd/src/ioplib.c +++ b/iop/utilities/modhook/src/modhook.c @@ -1,7 +1,8 @@ -#include "ioplib.h" +#include "modhook.h" #include +#include -iop_library_t *ioplib_getByName(const char *name) +iop_library_t *modhook_getModule(const char *name) { iop_library_t *libptr; int i; @@ -27,7 +28,7 @@ iop_library_t *ioplib_getByName(const char *name) return NULL; } -unsigned int ioplib_getTableSize(iop_library_t *lib) +unsigned int modhook_getTableSize(iop_library_t *lib) { void **exp; unsigned int size; @@ -45,9 +46,9 @@ unsigned int ioplib_getTableSize(iop_library_t *lib) return size; } -void *ioplib_hookExportEntry(iop_library_t *lib, unsigned int entry, void *func) +void *modhook_hookExportEntry(iop_library_t *lib, unsigned int entry, void *func) { - if (entry < ioplib_getTableSize(lib)) { + if (entry < modhook_getTableSize(lib)) { int oldstate; void **exp, *temp; @@ -65,7 +66,7 @@ void *ioplib_hookExportEntry(iop_library_t *lib, unsigned int entry, void *func) return NULL; } -void ioplib_relinkExports(iop_library_t *lib) +void modhook_relinkExports(iop_library_t *lib) { struct irx_import_table *table; struct irx_import_stub *stub; @@ -75,7 +76,7 @@ void ioplib_relinkExports(iop_library_t *lib) // go through each import in the table for (stub = (struct irx_import_stub *)table->stubs; stub->jump != 0; stub++) { // patch the stub to jump to the address specified in the library export table for "fno" - stub->jump = 0x08000000 | (((u32)lib->exports[stub->fno] << 4) >> 6); + stub->jump = 0x08000000 | (((uint32_t)lib->exports[stub->fno] << 4) >> 6); } } -} +} \ No newline at end of file