PS2SDK
PS2 Homebrew Libraries
fakehost.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 */
10 
20 //#define DEBUG
21 
22 #include "types.h"
23 #include "loadcore.h"
24 #include "stdio.h"
25 #include "sysclib.h"
26 #include <errno.h>
27 #include "thbase.h"
28 #include "intrman.h"
29 #include "sysmem.h"
30 #include "sifman.h"
31 #include "sifcmd.h"
32 
33 #include "iomanX.h"
34 #include "ioman_mod.h"
35 
36 #define TRUE 1
37 #define FALSE 0
38 
41 #define MODNAME "fakehost"
42 
43 // This is the filesystem to replace
44 #define FS_REPNAME "host"
45 
46 IRX_ID(MODNAME, 1, 1);
47 
48 #define M_PRINTF(format, args...) \
49  printf(MODNAME ": " format, ##args)
50 
51 #ifdef DEBUG
52 #define M_DEBUG M_PRINTF
53 #else
54 #define M_DEBUG(format, args...)
55 #endif
56 
57 // Host base location
58 static char base[100];
59 
60 static int fd_global;
61 
62 extern int ttyMount(void);
63 extern int naplinkRpcInit(void);
64 
75 char * fd_name( char * buffer, const char * name )
76 {
77  strcpy( buffer, base );
78  strcat( buffer, name );
79  return buffer;
80 }
81 
89 int fd_save( int fd, iop_io_file_t *f )
90 {
91  f->unit = ++fd_global;
92  f->privdata = (void *) fd;
93  return f->unit;
94 }
95 
103 {
104  return (int) f->privdata;
105 }
106 
115 int fd_open( iop_io_file_t *f, const char *name, int mode)
116 {
117  char nameBuffer[ 250 ];
118  int fd;
119 
120  M_DEBUG("open %i %s %s\n", f->unit, name ,base);
121  fd = iomanX_open( fd_name( nameBuffer, name), mode, 0 );
122  if ( fd < 0 ) return fd;
123 
124  return fd_save( fd, f );
125 }
126 
134 {
135  return iomanX_close( realfd(f) );
136 }
137 
146 int fd_read( iop_io_file_t *f, void * buffer, int size )
147 {
148  return iomanX_read( realfd(f), buffer, size );
149 }
150 
159 int fd_write( iop_io_file_t *fd, void *buffer, int size )
160 {
161  return iomanX_write( realfd(fd), buffer, size );
162 }
163 
172 int fd_lseek( iop_io_file_t *fd, int offset, int whence)
173 {
174  return iomanX_lseek( realfd(fd), offset, whence );
175 }
176 
177 IOMAN_RETURN_VALUE_IMPL(0);
178 IOMAN_RETURN_VALUE_IMPL(EIO);
179 
180 // Function array for fileio structure.
181 static iop_io_device_ops_t functions = {
182  IOMAN_RETURN_VALUE(0), // init
183  IOMAN_RETURN_VALUE(0), // deinit
184  IOMAN_RETURN_VALUE(EIO), // format
185  &fd_open, // open
186  &fd_close, // close
187  &fd_read, // read
188  IOMAN_RETURN_VALUE(EIO), // write
189  &fd_lseek, // lseek
190  IOMAN_RETURN_VALUE(EIO), // ioctl
191  IOMAN_RETURN_VALUE(EIO), // remove
192  IOMAN_RETURN_VALUE(EIO), // mkdir
193  IOMAN_RETURN_VALUE(EIO), // rmdir
194  IOMAN_RETURN_VALUE(EIO), // dopen
195  IOMAN_RETURN_VALUE(EIO), // dclose
196  IOMAN_RETURN_VALUE(EIO), // dread
197  IOMAN_RETURN_VALUE(EIO), // getstat
198  IOMAN_RETURN_VALUE(EIO), // chstat
199 };
200 
201 // FileIO structure.
202 static iop_io_device_t driver = {
203  FS_REPNAME,
204  16,
205  1,
206  "host redirection driver",
207  &functions,
208 };
209 
227 int _start( int argc, char *argv[] )
228 {
229  M_PRINTF( "Copyright (c) 2004 adresd\n" );
230 
231  if ( argc != 2 )
232  {
233  M_PRINTF( "HOST requires based argument\n" );
234  return MODULE_NO_RESIDENT_END;
235  }
236  else
237  {
238  // Copy the base location.
239  strncpy( base, argv[1] ,sizeof(base) - 1);
240  base[sizeof(base) - 1] = '\0';
241 
242  M_PRINTF( "redirecting '%s:' to '%s'\n",FS_REPNAME,base);
243  fd_global = 1;
244 
245  M_PRINTF( "HOST final step, bye\n" );
246 
247  // Install naplink RPC handler
248  naplinkRpcInit();
249 
250  // now install the fileio driver
251  io_DelDrv( FS_REPNAME );
252  io_AddDrv( &driver );
253  }
254  return MODULE_RESIDENT_END;
255 }
256 
257 
iomanX.h
fd_save
int fd_save(int fd, iop_io_file_t *f)
Definition: fakehost.c:89
_iop_io_device_ops
Definition: ioman_mod.h:81
thbase.h
sifman.h
sysclib.h
fd_read
int fd_read(iop_io_file_t *f, void *buffer, int size)
Definition: fakehost.c:146
_iop_io_file
Definition: ioman_mod.h:55
fd_write
int fd_write(iop_io_file_t *fd, void *buffer, int size)
Definition: fakehost.c:159
realfd
int realfd(iop_io_file_t *f)
Definition: fakehost.c:102
EIO
#define EIO
Definition: errno.h:29
loadcore.h
fd_close
int fd_close(iop_io_file_t *f)
Definition: fakehost.c:133
naplinkRpcInit
int naplinkRpcInit(void)
Definition: nprintf.c:80
_iop_io_file::unit
int unit
Definition: ioman_mod.h:59
fd_open
int fd_open(iop_io_file_t *f, const char *name, int mode)
Definition: fakehost.c:115
fd_lseek
int fd_lseek(iop_io_file_t *fd, int offset, int whence)
Definition: fakehost.c:172
_iop_io_file::privdata
void * privdata
Definition: ioman_mod.h:63
stdio.h
ioman_mod.h
sysmem.h
_iop_io_device
Definition: ioman_mod.h:66
intrman.h
_start
int _start(int argc, char *argv[])
Definition: fakehost.c:227
fd_name
char * fd_name(char *buffer, const char *name)
Definition: fakehost.c:75
errno.h