ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
fileio.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # (C)2001, Gustavo Scotti (gustavo@scotti.com)
7 # (c) 2003 Marcus R. Brown (mrbrown@0xd6.org)
8 # Licenced under Academic Free License version 2.0
9 # Review ps2sdk README & LICENSE files for further details.
10 */
11 
17 #include <tamtypes.h>
18 #include <ps2lib_err.h>
19 #include <kernel.h>
20 #include <sifrpc.h>
21 #define NEWLIB_PORT_AWARE
22 #include <fileio.h>
23 #include <string.h>
24 
25 #define D(fmt, args...) printf("(%s:%s:%i):" # fmt, __FILE__, __FUNCTION__, __LINE__, ## args)
26 
45 };
46 
52  void *dest1;
53  void *dest2;
54  u8 buf1[16];
55  u8 buf2[16];
56 };
57 
58 extern int _iop_reboot_count;
60 extern int _fio_init;
61 extern int _fio_block_mode;
62 extern int _fio_io_sema;
63 extern int _fio_completion_sema;
64 extern int _fio_recv_data[512];
65 extern int _fio_intr_data[32];
66 
68 void _fio_intr();
69 
70 #ifdef F___fio_internals
72 int _fio_recv_data[512] __attribute__((aligned(64)));
73 int _fio_intr_data[32] __attribute__((aligned(64)));
74 int _fio_init = 0;
75 int _fio_block_mode;
76 int _fio_io_sema = -1;
77 int _fio_completion_sema = -1;
78 #endif
79 
80 #ifdef F_fio_init
81 int fioInit(void)
82 {
83  int res;
84  ee_sema_t sema;
85  static int _rb_count = 0;
86 
87  if(_rb_count != _iop_reboot_count)
88  {
89  _rb_count = _iop_reboot_count;
90 
91  fioExit();
92  }
93 
94  if (_fio_init)
95  return 0;
96 
97  SifInitRpc(0);
98 
99  while (((res = SifBindRpc(&_fio_cd, 0x80000001, 0)) >= 0) &&
100  (_fio_cd.server == NULL))
101  nopdelay();
102 
103  if (res < 0)
104  return res;
105 
106  sema.init_count = 1;
107  sema.max_count = 1;
108  sema.option = 0;
110  if (_fio_completion_sema < 0)
111  return -E_LIB_SEMA_CREATE;
112 
113  //Unofficial: create a locking semaphore to prevent a thread from overwriting another thread's return status.
114  sema.init_count = 1;
115  sema.max_count = 1;
116  sema.option = 0;
117  _fio_io_sema = CreateSema(&sema);
118  if (_fio_io_sema < 0)
119  return -E_LIB_SEMA_CREATE;
120 
121  _fio_init = 1;
123 
124  return 0;
125 }
126 #endif
127 
128 #ifdef F__fio_intr
129 void _fio_intr(void)
130 {
132 }
133 #endif
134 
135 #ifdef F_fio_sync
136 int fioSync(int mode, int *retVal)
137 {
138  int res;
139 
140  if ((res = fioInit()) < 0)
141  return res;
142 
144 
145  switch(mode)
146  {
147  case FIO_WAIT:
148 
151 
152  if(retVal != NULL)
153  *retVal = *(int *)UNCACHED_SEG(&_fio_recv_data[0]);
154 
155  return FIO_COMPLETE;
156 
157  case FIO_NOWAIT:
158 
160  return FIO_INCOMPLETE;
161 
163 
164  if(retVal != NULL)
165  *retVal = *(int *)UNCACHED_SEG(&_fio_recv_data[0]);
166 
167  return FIO_COMPLETE;
168 
169  default:
170  return -E_LIB_UNSUPPORTED;
171  }
172 }
173 #endif
174 
175 #ifdef F_fio_setblockmode
176 void fioSetBlockMode(int blocking)
177 {
178  _fio_block_mode = blocking;
179 }
180 
181 #endif
182 
183 #ifdef F_fio_exit
184 void fioExit(void)
185 {
186  if(_fio_init)
187  {
188  _fio_init = 0;
189  memset(&_fio_cd, 0, sizeof _fio_cd);
190  if (_fio_completion_sema >= 0)
191  {
193  }
194  if(_fio_io_sema >= 0)
195  {
197  }
198  }
199 }
200 #endif
201 
202 #ifdef F_fio_open
203 struct _fio_open_arg {
204  int mode;
205  char name[FIO_PATH_MAX];
206 } ALIGNED(16);
207 
208 int fioOpen(const char *name, int mode)
209 {
210  struct _fio_open_arg arg;
211  int res, result;
212 
213  if ((res = fioInit()) < 0)
214  return res;
215 
218 
219  arg.mode = mode;
220  strncpy(arg.name, name, FIO_PATH_MAX - 1);
221  arg.name[FIO_PATH_MAX - 1] = 0;
222 
223  if ((res = SifCallRpc(&_fio_cd, FIO_F_OPEN, _fio_block_mode, &arg, sizeof arg,
224  _fio_recv_data, 4, (void *)_fio_intr, NULL)) >= 0)
225  {
227  }
228  else
229  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
231  result=res;
232  }
233 
235 
236  return result;
237 }
238 #endif
239 
240 #ifdef F_fio_close
241 int fioClose(int fd)
242 {
243  union { int fd; int result; } arg;
244  int res, result;
245 
246  if ((res = fioInit()) < 0)
247  return res;
248 
251 
252  arg.fd = fd;
253 
254  if ((res = SifCallRpc(&_fio_cd, FIO_F_CLOSE, 0, &arg, 4, &arg, 4,
255  (void *)_fio_intr, NULL)) >= 0)
256  {
257  result=arg.result;
258  }
259  else
260  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
262  result=res;
263  }
264 
266 
267  return result;
268 }
269 #endif
270 
271 #ifdef F__fio_read_intr
272 void _fio_read_intr(struct _fio_read_data *data)
273 {
274  struct _fio_read_data *uncached = UNCACHED_SEG(data);
275 
276  if (uncached->size1 && uncached->dest1) {
277  memcpy(uncached->dest1, uncached->buf1, uncached->size1);
278  }
279 
280  if (uncached->size2 && uncached->dest2) {
281  memcpy(uncached->dest2, uncached->buf2, uncached->size2);
282  }
283 
285 }
286 #endif
287 
288 #ifdef F_fio_read
289 struct _fio_read_arg {
290  int fd;
291  void *ptr;
292  int size;
293  struct _fio_read_data *read_data;
294 } ALIGNED(16);
295 
296 int fioRead(int fd, void *ptr, int size)
297 {
298  struct _fio_read_arg arg;
299  int res, result;
300 
301  if ((res = fioInit()) < 0)
302  return res;
303 
306 
307  arg.fd = fd;
308  arg.ptr = ptr;
309  arg.size = size;
310  arg.read_data = (struct _fio_read_data *)_fio_intr_data;
311 
312  if (!IS_UNCACHED_SEG(ptr))
313  SifWriteBackDCache(ptr, size);
314 
315  if ((res = SifCallRpc(&_fio_cd, FIO_F_READ, _fio_block_mode, &arg, sizeof arg,
316  _fio_recv_data, 4, (void *)_fio_read_intr, _fio_intr_data)) >= 0)
317  {
319  }
320  else
321  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
323  result=res;
324  }
325 
327 
328  return result;
329 }
330 #endif
331 
332 #ifdef F_fio_write
333 struct _fio_write_arg {
334  int fd;
335  const void *ptr;
336  u32 size;
337  u32 mis;
338  u8 aligned[16];
339 } ALIGNED(16);
340 
341 int fioWrite(int fd, const void *ptr, int size)
342 {
343  struct _fio_write_arg arg;
344  int mis, res, result;
345 
346  if ((res = fioInit()) < 0)
347  return res;
348 
351 
352  arg.fd = fd;
353  arg.ptr = ptr;
354  arg.size = size;
355 
356  /* Copy the unaligned (16-byte) portion into the argument */
357  mis = 0;
358  if ((u32)ptr & 0xf) {
359  mis = 16 - ((u32)ptr & 0xf);
360  if (mis > size)
361  mis = size;
362  }
363  arg.mis = mis;
364 
365  if (mis)
366  memcpy(arg.aligned, ptr, mis);
367 
368  if (!IS_UNCACHED_SEG(ptr))
369  SifWriteBackDCache((void*)ptr, size);
370 
371  if ((res = SifCallRpc(&_fio_cd, FIO_F_WRITE, _fio_block_mode, &arg, sizeof arg,
372  _fio_recv_data, 4, (void *)_fio_intr, NULL)) >= 0)
373  {
375  }
376  else
377  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
379  result=res;
380  }
381 
383 
384  return result;
385 }
386 #endif
387 
388 #ifdef F_fio_lseek
389 struct _fio_lseek_arg {
390  union {
391  int fd;
392  int result;
393  } p;
394  int offset;
395  int whence;
396 } ALIGNED(16);
397 
398 int fioLseek(int fd, int offset, int whence)
399 {
400  struct _fio_lseek_arg arg;
401  int res, result;
402 
403  if ((res = fioInit()) < 0)
404  return res;
405 
408 
409  arg.p.fd = fd;
410  arg.offset = offset;
411  arg.whence = whence;
412 
413  if ((res = SifCallRpc(&_fio_cd, FIO_F_LSEEK, 0, &arg, sizeof arg,
414  &arg, 4, (void *)_fio_intr, NULL)) >= 0)
415  {
416  result=arg.p.result;
417  }
418  else
419  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
421  result=res;
422  }
423 
425 
426  return result;
427 }
428 #endif
429 
430 #ifdef F_fio_ioctl
431 struct _fio_ioctl_arg {
432  union {
433  int fd;
434  int result;
435  } p;
436  int request;
437  u8 data[1024]; // Will this be ok ?
438 } ALIGNED(16);
439 
440 int fioIoctl(int fd, int request, void *data)
441 {
442  struct _fio_ioctl_arg arg;
443  int res, result;
444 
445  if ((res = fioInit()) < 0)
446  return res;
447 
450 
451  arg.p.fd = fd;
452  arg.request = request;
453 
454  if(data != NULL)
455  memcpy(arg.data, data, 1024);
456 
457  if ((res = SifCallRpc(&_fio_cd, FIO_F_IOCTL, 0, &arg, sizeof arg,
458  &arg, 4, (void *)_fio_intr, NULL)) >= 0)
459  {
460  result=arg.p.result;
461  }
462  else
463  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
465  result=res;
466  }
467 
469 
470  return result;
471 }
472 #endif
473 
474 #ifdef F_fio_remove
475 int fioRemove(const char *name)
476 {
477  union {
478  char path[FIO_PATH_MAX];
479  int result;
480  } arg;
481  int res, result;
482 
483  if ((res = fioInit()) < 0)
484  return res;
485 
488 
489  strncpy(arg.path, name, FIO_PATH_MAX - 1);
490  arg.path[FIO_PATH_MAX - 1] = 0;
491 
492  if ((res = SifCallRpc(&_fio_cd, FIO_F_REMOVE, 0, &arg, sizeof arg,
493  &arg, 4, (void *)_fio_intr, NULL)) >= 0)
494  {
495  result=arg.result;
496  }
497  else
498  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
500  result=res;
501  }
502 
504 
505  return result;
506 }
507 #endif
508 
509 #ifdef F_fio_mkdir
510 int fioMkdir(const char* path)
511 {
512  union {
513  char path[FIO_PATH_MAX];
514  int result;
515  } arg;
516  int res, result;
517 
518  if ((res = fioInit()) < 0)
519  return res;
520 
523 
524  strncpy(arg.path, path, FIO_PATH_MAX - 1);
525  arg.path[FIO_PATH_MAX - 1] = 0;
526 
527  if ((res = SifCallRpc(&_fio_cd, FIO_F_MKDIR, 0, &arg, sizeof arg,
528  &arg, 4, (void *)_fio_intr, NULL)) >= 0)
529  {
530  result=arg.result;
531  }
532  else
533  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
535  result=res;
536  }
537 
539 
540  return result;
541 }
542 #endif
543 
544 #ifdef F_fio_rmdir
545 int fioRmdir(const char* dirname)
546 {
547  union {
548  char path[FIO_PATH_MAX];
549  int result;
550  } arg;
551  int res, result;
552 
553  if ((res = fioInit()) < 0)
554  return res;
555 
558 
559  strncpy(arg.path, dirname, FIO_PATH_MAX - 1);
560  arg.path[FIO_PATH_MAX - 1] = 0;
561 
562  if ((res = SifCallRpc(&_fio_cd, FIO_F_RMDIR, 0, &arg, sizeof arg,
563  &arg, 4, (void *)_fio_intr, NULL)) >= 0)
564  {
565  result=arg.result;
566  }
567  else
568  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
570  result=res;
571  }
572 
574 
575  return result;
576 }
577 #endif
578 
579 #ifdef F_fio_putc
580 int fioPutc(int fd,int c)
581 {
582  return fioWrite(fd,&c,1);
583 }
584 #endif
585 
586 #ifdef F_fio_getc
587 int fioGetc(int fd)
588 {
589  int c;
590 
591  fioRead(fd,&c,1);
592  return c;
593 }
594 #endif
595 
596 #ifdef F_fio_gets
597 int fioGets(int fd, char* buff, int n)
598 {
599  // Rather than doing a slow byte-at-a-time read
600  // read upto the max langth, then re-position file afterwards
601  int read, i;
602 
603  read = fioRead(fd, buff, n);
604 
605  for (i=0; i<(read-1); i++)
606  {
607  switch (buff[i])
608  {
609  case '\n':
610  fioLseek(fd, (i + 1) - read, SEEK_CUR);
611  buff[i]=0; // terminate after newline
612  return i;
613 
614  case 0:
615  fioLseek(fd, i-read, SEEK_CUR);
616  return i;
617  }
618  }
619 
620  // if we reached here, then we havent found the end of a string
621  return i;
622 }
623 #endif
624 
625 #ifdef F_fio_dopen
626 int fioDopen(const char *name)
627 {
628  union {
629  char name[FIO_PATH_MAX];
630  int result;
631  } arg;
632  int res, result;
633 
634  if ((res = fioInit()) < 0)
635  return res;
636 
639 
640  strncpy(arg.name, name, FIO_PATH_MAX - 1);
641  arg.name[FIO_PATH_MAX - 1] = 0;
642 
643  if ((res = SifCallRpc(&_fio_cd, FIO_F_DOPEN, 0, &arg, sizeof arg,
644  &arg, 4, (void *)_fio_intr, NULL)) >= 0)
645  {
646  result=arg.result;
647  }
648  else
649  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
651  result=res;
652  }
653 
655 
656  return result;
657 }
658 #endif
659 
660 #ifdef F_fio_dclose
661 int fioDclose(int fd)
662 {
663  union {
664  int fd;
665  int result;
666  } arg;
667  int res, result;
668 
669  if ((res = fioInit()) < 0)
670  return res;
671 
674 
675  arg.fd = fd;
676 
677  if ((res = SifCallRpc(&_fio_cd, FIO_F_DCLOSE, 0, &arg, sizeof arg,
678  &arg, 4, (void *)_fio_intr, NULL)) >= 0)
679  {
680  result=arg.result;
681  }
682  else
683  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
685  result=res;
686  }
687 
689 
690  return result;
691 }
692 #endif
693 
694 #ifdef F_fio_dread
695 struct _fio_dread_arg {
696  union {
697  int fd;
698  int result;
699  } p;
700  io_dirent_t *buf;
701 } ALIGNED(16);
702 
703 int fioDread(int fd, io_dirent_t *buf)
704 {
705  struct _fio_dread_arg arg;
706  int res, result;
707 
708  if ((res = fioInit()) < 0)
709  return res;
710 
713 
714  arg.p.fd = fd;
715  arg.buf = buf;
716 
717  if (!IS_UNCACHED_SEG(buf))
718  SifWriteBackDCache(buf, sizeof(io_dirent_t));
719 
720  if ((res = SifCallRpc(&_fio_cd, FIO_F_DREAD, 0, &arg, sizeof arg,
721  &arg, 4, (void *)_fio_intr, NULL)) >= 0)
722  {
723  result=arg.p.result;
724  }
725  else
726  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
728  result=res;
729  }
730 
732 
733  return result;
734 }
735 #endif
736 
737 #ifdef F_fio_getstat
738 struct _fio_getstat_arg {
739  union {
740  io_stat_t *buf;
741  int result;
742  } p;
743  char name[FIO_PATH_MAX];
744 } ALIGNED(16);
745 
746 int fioGetstat(const char *name, io_stat_t *buf)
747 {
748  struct _fio_getstat_arg arg;
749  int res, result;
750 
751  if ((res = fioInit()) < 0)
752  return res;
753 
756 
757  arg.p.buf = buf;
758  strncpy(arg.name, name, FIO_PATH_MAX - 1);
759  arg.name[FIO_PATH_MAX - 1] = 0;
760 
761  if (!IS_UNCACHED_SEG(buf))
762  SifWriteBackDCache(buf, sizeof(io_stat_t));
763 
764  if ((res = SifCallRpc(&_fio_cd, FIO_F_GETSTAT, 0, &arg, sizeof arg,
765  &arg, 4, (void *)_fio_intr, NULL)) >= 0)
766  {
767  result=arg.p.result;
768  }
769  else
770  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
772  result=res;
773  }
774 
776 
777  return result;
778 }
779 #endif
780 
781 #ifdef F_fio_chstat
782 struct _fio_chstat_arg {
783  union {
784  int cbit;
785  int result;
786  } p;
787  io_stat_t stat;
788  char name[FIO_PATH_MAX];
789 };
790 
791 int fioChstat(const char *name, io_stat_t *buf, u32 cbit)
792 {
793  struct _fio_chstat_arg arg;
794  int res, result;
795 
796  if ((res = fioInit()) < 0)
797  return res;
798 
801 
802  arg.p.cbit = cbit;
803  memcpy(&arg.stat, buf, sizeof(io_stat_t));
804  strncpy(arg.name, name, FIO_PATH_MAX - 1);
805  arg.name[FIO_PATH_MAX - 1] = 0;
806 
807  if ((res = SifCallRpc(&_fio_cd, FIO_F_CHSTAT, 0, &arg, sizeof arg,
808  &arg, 4, (void *)_fio_intr, NULL)) >= 0)
809  {
810  result=arg.p.result;
811  }
812  else
813  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
815  result=res;
816  }
817 
819 
820  return result;
821 }
822 #endif
823 
824 #ifdef F_fio_format
825 int fioFormat(const char *name)
826 {
827  union {
828  char path[FIO_PATH_MAX];
829  int result;
830  } arg;
831  int res, result;
832 
833  if ((res = fioInit()) < 0)
834  return res;
835 
838 
839  strncpy(arg.path, name, FIO_PATH_MAX - 1);
840  arg.path[FIO_PATH_MAX - 1] = 0;
841 
842  if ((res = SifCallRpc(&_fio_cd, FIO_F_FORMAT, 0, &arg, sizeof arg,
843  &arg, 4, (void *)_fio_intr, NULL)) >= 0)
844  {
845  result=arg.result;
846  }
847  else
848  { //Signal semaphore to avoid a deadlock if SifCallRpc fails.
850  result=res;
851  }
852 
854 
855  return result;
856 }
857 #endif
void _fio_intr()
void _fio_read_intr(struct _fio_read_data *)
int _fio_init
int _fio_recv_data[512]
int _iop_reboot_count
int _fio_completion_sema
SifRpcClientData_t _fio_cd
int _fio_io_sema
_fio_functions
Definition: fileio.c:27
@ FIO_F_FORMAT
Definition: fileio.c:42
@ FIO_F_OPEN
Definition: fileio.c:28
@ FIO_F_LSEEK
Definition: fileio.c:32
@ FIO_F_CLOSE
Definition: fileio.c:29
@ FIO_F_DREAD
Definition: fileio.c:39
@ FIO_F_MKDIR
Definition: fileio.c:35
@ FIO_F_READ
Definition: fileio.c:30
@ FIO_F_ADDDRV
Definition: fileio.c:43
@ FIO_F_WRITE
Definition: fileio.c:31
@ FIO_F_RMDIR
Definition: fileio.c:36
@ FIO_F_DOPEN
Definition: fileio.c:37
@ FIO_F_CHSTAT
Definition: fileio.c:41
@ FIO_F_GETSTAT
Definition: fileio.c:40
@ FIO_F_REMOVE
Definition: fileio.c:34
@ FIO_F_DELDRV
Definition: fileio.c:44
@ FIO_F_DCLOSE
Definition: fileio.c:38
@ FIO_F_IOCTL
Definition: fileio.c:33
int _fio_intr_data[32]
int _fio_block_mode
int fioInit(void)
int fioRmdir(const char *dirname)
int fioSync(int mode, int *retVal)
int fioChstat(const char *name, io_stat_t *buf, unsigned int cbit)
#define FIO_COMPLETE
Definition: fileio.h:32
int fioWrite(int fd, const void *buff, int buff_size)
int fioPutc(int fd, int c)
int fioRead(int fd, void *buff, int buff_size)
#define FIO_WAIT
Definition: fileio.h:29
int fioGetc(int fd)
void fioSetBlockMode(int blocking)
void fioExit(void)
int fioFormat(const char *name)
int fioMkdir(const char *dirname)
#define FIO_NOWAIT
Definition: fileio.h:30
#define FIO_INCOMPLETE
Definition: fileio.h:33
int fioGetstat(const char *name, io_stat_t *buf)
int fioRemove(const char *name)
int fioDclose(int fd)
int fioIoctl(int fd, int request, void *data)
int fioDopen(const char *name)
#define FIO_PATH_MAX
Definition: fileio.h:27
int fioGets(int fd, char *buff, int n)
int fioClose(int fd)
int fioDread(int fd, io_dirent_t *buf)
int fioLseek(int fd, int offset, int whence)
int fioOpen(const char *fname, int mode)
s32 CreateSema(ee_sema_t *sema)
s32 SignalSema(s32 sema_id)
s32 DeleteSema(s32 sema_id)
s32 iSignalSema(s32 sema_id)
static void nopdelay(void)
Definition: kernel.h:141
#define ALIGNED(x)
Definition: kernel.h:50
s32 PollSema(s32 sema_id)
s32 WaitSema(s32 sema_id)
#define UNCACHED_SEG(x)
Definition: kernel.h:35
#define IS_UNCACHED_SEG(x)
Definition: kernel.h:38
u32 data
Definition: libmouse.c:36
@ E_LIB_SEMA_CREATE
Definition: ps2lib_err.h:70
@ E_LIB_UNSUPPORTED
Definition: ps2lib_err.h:76
s32 mode
Definition: rpc_client.c:15
s32 result
Definition: rpc_client.c:23
void SifWriteBackDCache(void *ptr, int size)
void SifInitRpc(int mode)
int SifBindRpc(SifRpcClientData_t *client, int rpc_number, int mode)
int SifCallRpc(SifRpcClientData_t *client, int rpc_number, int mode, void *send, int ssize, void *receive, int rsize, SifRpcEndFunc_t end_function, void *end_param)
struct t_SifRpcServerData * server
Definition: sifrpc.h:142
u8 buf1[16]
Definition: fileio.c:54
u8 buf2[16]
Definition: fileio.c:55
void * dest2
Definition: fileio.c:53
void * dest1
Definition: fileio.c:52
int init_count
Definition: kernel.h:218
int max_count
Definition: kernel.h:217
u32 option
Definition: kernel.h:221
Definition: thread.c:17
#define NULL
Definition: tamtypes.h:91
unsigned int u32
Definition: tamtypes.h:30
unsigned char u8
Definition: tamtypes.h:23