Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert ioplib in a standalone iop library #648

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion iop/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ SUBDIRS = \
startup \
system \
tcpip \
usb
usb \
utilities

include $(PS2SDKSRC)/Defs.make
include $(PS2SDKSRC)/Rules.make
Expand Down
14 changes: 12 additions & 2 deletions iop/sio/mx4sio_bd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions iop/sio/mx4sio_bd/src/ioplib.h

This file was deleted.

5 changes: 3 additions & 2 deletions iop/sio/mx4sio_bd/src/mx4sio.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#include <thsemap.h>
#include <xsio2man.h>

#include <modhook.h>

#include "mx4sio.h"
#include "crc16.h"
#include "ioplib.h"
#include "sio2man_hook.h"
#include "sio2regs.h"
#include "spi_sdcard_driver.h"
Expand Down Expand Up @@ -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
Expand Down
68 changes: 34 additions & 34 deletions iop/sio/mx4sio_bd/src/sio2man_hook.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <stdio.h>
#include <string.h>
#include <thsemap.h>
#include <modhook.h>

#include "ioplib.h"
#include "sio2man.h"
#include "sio2man_hook.h"

Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did the CI build this succesfully? modhookk with double 'k'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, didn't saw that. Will recheck this later!

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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions iop/utilities/Makefile
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions iop/utilities/modhook/Makefile
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions iop/utilities/modhook/include/modhook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef __MODHOOK_H__
#define __MODHOOK_H__

#include <loadcore.h>

/**
* @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
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "ioplib.h"
#include "modhook.h"
#include <intrman.h>
#include <stdint.h>

iop_library_t *ioplib_getByName(const char *name)
iop_library_t *modhook_getModule(const char *name)
{
iop_library_t *libptr;
int i;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -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);
}
}
}
}