ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
slib.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright (c) 2003 Marcus R. Brown <mrbrown@0xd6.org>
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 */
10 
16 #include <tamtypes.h>
17 #include <kernel.h>
18 #include <string.h>
19 #include <sifrpc.h>
20 
21 #include "slib.h"
22 #include "smod.h"
23 
24 #include "common.h"
25 
27 
28 /* from common.c */
29 extern struct smem_buf smem_buf;
30 
32 {
33  SifRpcReceiveData_t RData;
34  slib_exp_lib_t *core_exps;
35  slib_exp_lib_list_t *exp_lib_list = NULL;
36  u32 i, addr, core_end, NextMod, *exp_func;
37  void *pGetLoadcoreInternalData;
38  smod_mod_info_t *ModInfo;
39 
40  /* Read the start of the global module table - this is where we will search. */
42  if(SifRpcGetOtherData(&RData, (void*)0x800, &smem_buf, sizeof(smod_mod_info_t), 0)>=0){
43  /* The first entry points to LOADCORE's module info. We then use the
44  module info to determine the end of LOADCORE's .text segment (just
45  past the export library we're trying to find. */
46  NextMod = *smem_buf.words;
48  if(SifRpcGetOtherData(&RData, (void*)NextMod, &smem_buf, sizeof(smod_mod_info_t), 0)>=0){
49  ModInfo = &smem_buf.mod_info;
50  core_end = ModInfo->text_start+ModInfo->text_size;
51 
52  /* Back up so we position ourselves infront of where the export
53  library will be. */
55  if(SifRpcGetOtherData(&RData, (void*)(core_end - 512), &smem_buf, 512, 0)>=0){
56  /* Search for LOADCORE's export library. */
57  for (i = 0; i < 512; i += 4) {
58  /* SYSMEM's export library sits at 0x830, so it should appear in
59  LOADCORE's prev pointer. */
60  if (smem_buf.words[i / sizeof(u32)] == 0x830) {
61  if (!__memcmp(smem_buf.bytes + i + 12, "loadcore", 8))
62  break;
63  }
64  }
65  if (i >= 512)
66  return NULL;
67 
68  /* Get to the start of the export table, and find the address of the
69  routine that will get us the export library list info. */
70  core_exps = (slib_exp_lib_t *)(smem_buf.bytes + i);
71  pGetLoadcoreInternalData = core_exps->exports[3];
72 
74  if(SifRpcGetOtherData(&RData, pGetLoadcoreInternalData, &smem_buf, 8, 0)>=0){
75  exp_func = smem_buf.words;
76 
77  /* Parse the two instructions that hold the address of the table. */
78  if ((exp_func[0] & 0xffff0000) != 0x3c020000) /* lui v0, XXXX */
79  return NULL;
80  if ((exp_func[1] & 0xffff0000) != 0x24420000) /* addiu v0, v0, XXXX */
81  return NULL;
82 
83  addr = ((exp_func[0] & 0xffff) << 16) | (exp_func[1] & 0xffff);
84 
86  if(SifRpcGetOtherData(&RData, (void*)addr, &smem_buf, 8, 0)>=0){
89  exp_lib_list = &_slib_cur_exp_lib_list;
90  }
91  }
92  }
93  }
94  }
95 
96  return exp_lib_list;
97 }
98 
99 #define EXP_LIB_MAX SMEM_BUF_SIZE /* We can even handle CDVDMAN's bloat! */
100 
101 int slib_get_exp_lib(const char *name, slib_exp_lib_t *library)
102 {
103  SifRpcReceiveData_t RData;
106  void *cur_lib;
107  int len = strlen(name), count = 0;
108 
109  if (!exp_lib_list->head && !(exp_lib_list = slib_exp_lib_list()))
110  return 0;
111 
112  /* Read the tail export library to initiate the search. */
113  cur_lib = exp_lib_list->tail;
114 
115  while (cur_lib) {
117  if(SifRpcGetOtherData(&RData, cur_lib, exp_lib, EXP_LIB_MAX, 0)>=0){
118  if (!__memcmp(exp_lib->name, name, len)) {
119  while (exp_lib->exports[count] != 0)
120  count++;
121 
122  if (library)
123  memcpy(library, exp_lib, sizeof(slib_exp_lib_t) + count * 4);
124 
125  return count;
126  }
127 
128  cur_lib = exp_lib->prev;
129  }
130  }
131 
132  return 0;
133 }
int __memcmp(const void *s1, const void *s2, unsigned int length)
Definition: common.c:27
void SyncDCache(void *start, void *end)
int SifRpcGetOtherData(SifRpcReceiveData_t *rd, void *src, void *dest, int size, int mode)
slib_exp_lib_list_t _slib_cur_exp_lib_list
Definition: slib.c:26
#define EXP_LIB_MAX
Definition: slib.c:99
int slib_get_exp_lib(const char *name, slib_exp_lib_t *library)
Definition: slib.c:101
slib_exp_lib_list_t * slib_exp_lib_list(void)
Definition: slib.c:31
struct _slib_exp_lib * head
Definition: slib.h:47
struct _slib_exp_lib * tail
Definition: slib.h:46
u8 name[8]
Definition: slib.h:37
struct _slib_exp_lib * prev
Definition: slib.h:33
void * exports[0]
Definition: slib.h:38
Definition: common.h:3
u8 bytes[SMEM_BUF_SIZE/sizeof(u8)]
Definition: common.h:5
u32 words[SMEM_BUF_SIZE/sizeof(u32)]
Definition: common.h:6
slib_exp_lib_t exp_lib
Definition: common.h:8
smod_mod_info_t mod_info
Definition: common.h:7
u32 text_start
Definition: smod.h:36
u32 text_size
Definition: smod.h:37
#define NULL
Definition: tamtypes.h:91
unsigned int u32
Definition: tamtypes.h:30