PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
ioplib.c
1#include "ioplib.h"
2#include <intrman.h>
3
4iop_library_t *ioplib_getByName(const char *name)
5{
6 iop_library_t *libptr;
7 int i;
8
9 // Get first loaded library
10 libptr = GetLoadcoreInternalData()->let_next;
11 // Loop through all loaded libraries
12 while (libptr != NULL) {
13 // Compare library name only
14 for (i = 0; i < 8; i++) {
15 if (libptr->name[i] != name[i])
16 break;
17 }
18
19 // Return if match
20 if (i == 8)
21 return libptr;
22
23 // Next library
24 libptr = libptr->prev;
25 }
26
27 return NULL;
28}
29
30unsigned int ioplib_getTableSize(iop_library_t *lib)
31{
32 void **exp;
33 unsigned int size;
34
35 exp = NULL;
36 if (lib != NULL) {
37 exp = lib->exports;
38 }
39 size = 0;
40
41 if (exp != NULL)
42 while (*exp++ != NULL)
43 size++;
44
45 return size;
46}
47
48void *ioplib_hookExportEntry(iop_library_t *lib, unsigned int entry, void *func)
49{
50 if (entry < ioplib_getTableSize(lib)) {
51 int oldstate;
52 void **exp, *temp;
53
54 exp = &lib->exports[entry];
55
56 CpuSuspendIntr(&oldstate);
57 temp = *exp;
58 *exp = func;
59 func = temp;
60 CpuResumeIntr(oldstate);
61
62 return func;
63 }
64
65 return NULL;
66}
67
68void ioplib_relinkExports(iop_library_t *lib)
69{
70 struct irx_import_table *table;
71 struct irx_import_stub *stub;
72
73 // go through each table that imports the library
74 for (table = lib->caller; table != NULL; table = table->next) {
75 // go through each import in the table
76 for (stub = (struct irx_import_stub *)table->stubs; stub->jump != 0; stub++) {
77 // patch the stub to jump to the address specified in the library export table for "fno"
78 stub->jump = 0x08000000 | (((u32)lib->exports[stub->fno] << 4) >> 6);
79 }
80 }
81}
int CpuResumeIntr(int state)
Definition intrman.c:227
int CpuSuspendIntr(int *state)
Definition intrman.c:205