ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
fileXio_rpc.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 
16 #include <tamtypes.h>
17 #include <kernel.h>
18 #include <sifrpc.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <malloc.h>
22 #include <stdarg.h>
23 #include <sys/fcntl.h>
24 #include <sys/stat.h>
25 #include <ps2sdkapi.h>
26 
27 #define NEWLIB_PORT_AWARE
28 #include <fileXio_rpc.h>
29 #include <errno.h>
30 
31 extern int _iop_reboot_count;
33 static unsigned int sbuff[0x1300] __attribute__((aligned (64)));
34 static int _intr_data[0xC00] __attribute__((aligned(64)));
35 static int fileXioInited = 0;
36 static int fileXioBlockMode;
37 static int fileXioCompletionSema = -1;
38 
39 static void _fxio_intr(void)
40 {
42 }
43 
44 static int _lock_sema_id = -1;
45 static inline int _lock(void)
46 {
47  return(WaitSema(_lock_sema_id));
48 }
49 
50 static inline int _unlock(void)
51 {
52  return(SignalSema(_lock_sema_id));
53 }
54 
55 static time_t io_to_posix_time(const unsigned char *ps2time)
56 {
57  struct tm tim;
58  tim.tm_sec = ps2time[1];
59  tim.tm_min = ps2time[2];
60  tim.tm_hour = ps2time[3];
61  tim.tm_mday = ps2time[4];
62  tim.tm_mon = ps2time[5] - 1;
63  tim.tm_year = ((u16)ps2time[6] | ((u16)ps2time[7] << 8)) - 1900;
64  return mktime(&tim);
65 }
66 
67 static mode_t iox_to_posix_mode(unsigned int ps2mode)
68 {
69  mode_t posixmode = 0;
70  if (ps2mode & FIO_S_IFREG) posixmode |= S_IFREG;
71  if (ps2mode & FIO_S_IFDIR) posixmode |= S_IFDIR;
72  if (ps2mode & FIO_S_IRUSR) posixmode |= S_IRUSR;
73  if (ps2mode & FIO_S_IWUSR) posixmode |= S_IWUSR;
74  if (ps2mode & FIO_S_IXUSR) posixmode |= S_IXUSR;
75  if (ps2mode & FIO_S_IRGRP) posixmode |= S_IRGRP;
76  if (ps2mode & FIO_S_IWGRP) posixmode |= S_IWGRP;
77  if (ps2mode & FIO_S_IXGRP) posixmode |= S_IXGRP;
78  if (ps2mode & FIO_S_IROTH) posixmode |= S_IROTH;
79  if (ps2mode & FIO_S_IWOTH) posixmode |= S_IWOTH;
80  if (ps2mode & FIO_S_IXOTH) posixmode |= S_IXOTH;
81  return posixmode;
82 }
83 
84 static void fill_stat(struct stat *stat, const iox_stat_t *fiostat)
85 {
86  stat->st_dev = 0;
87  stat->st_ino = 0;
88  stat->st_mode = iox_to_posix_mode(fiostat->mode);
89  stat->st_nlink = 0;
90  stat->st_uid = 0;
91  stat->st_gid = 0;
92  stat->st_rdev = 0;
93  stat->st_size = ((off_t)fiostat->hisize << 32) | (off_t)fiostat->size;
94  stat->st_atime = io_to_posix_time(fiostat->atime);
95  stat->st_mtime = io_to_posix_time(fiostat->mtime);
96  stat->st_ctime = io_to_posix_time(fiostat->ctime);
97  stat->st_blksize = 16*1024;
98  stat->st_blocks = stat->st_size / 512;
99 }
100 
101 static int fileXioGetstatHelper(const char *path, struct stat *buf) {
102  iox_stat_t fiostat;
103 
104  if (fileXioGetStat(path, &fiostat) < 0) {
105  // FIXME: set errno
106  return -1;
107  }
108 
109  fill_stat(buf, &fiostat);
110 
111  return 0;
112 }
113 
114 static DIR *fileXioOpendirHelper(const char *path)
115 {
116  int dd;
117  DIR *dir;
118 
119  dd = fileXioDopen(path);
120  if (dd < 0) {
121  // FIXME: set errno
122  //printf("%s: ERROR: fileXioDopen\n", __FUNCTION__);
123  return NULL;
124  }
125 
126  dir = malloc(sizeof(DIR));
127  dir->dd_fd = dd;
128  dir->dd_buf = malloc(sizeof(struct dirent));
129 
130  return dir;
131 }
132 
133 static struct dirent *fileXioReaddirHelper(DIR *dir)
134 {
135  int rv;
136  struct dirent *de;
137  iox_dirent_t fiode;
138 
139  if(dir == NULL) {
140  // FIXME: set errno
141  return NULL;
142  }
143 
144  de = (struct dirent *)dir->dd_buf;
145  rv = fileXioDread(dir->dd_fd, &fiode);
146  if (rv <= 0) {
147  // FIXME: set errno
148  return NULL;
149  }
150 
151  fill_stat(&de->d_stat, &fiode.stat);
152  strncpy(de->d_name, fiode.name, 255);
153  de->d_name[255] = 0;
154 
155  return de;
156 }
157 
158 static void fileXioRewinddirHelper(DIR *dir)
159 {
160  printf("rewinddir not implemented\n");
161 }
162 
163 static int fileXioClosedirHelper(DIR *dir)
164 {
165  if(dir == NULL) {
166  // FIXME: set errno
167  return -1;
168  }
169 
170  fileXioDclose(dir->dd_fd); // Check return value?
171  free(dir->dd_buf);
172  free(dir);
173  return 0;
174 }
175 
176 int fileXioInit(void)
177 {
178  int res;
179  ee_sema_t sp;
180  static int _rb_count = 0;
181 
182  if(_rb_count != _iop_reboot_count)
183  {
184  _rb_count = _iop_reboot_count;
185 
186  fileXioExit();
187  }
188 
189  if(fileXioInited)
190  {
191  return 0;
192  }
193 
194  sp.init_count = 1;
195  sp.max_count = 1;
196  sp.option = 0;
198 
199  while(((res = SifBindRpc(&cd0, FILEXIO_IRX, 0)) >= 0) && (cd0.server == NULL))
200  nopdelay();
201 
202  if(res < 0)
203  return res;
204 
205  sp.init_count = 1;
206  sp.max_count = 1;
207  sp.option = 0;
209  if (fileXioCompletionSema < 0)
210  return -1;
211 
212  fileXioInited = 1;
214 
226 
228 
233 
234  return 0;
235 }
236 
237 void fileXioExit(void)
238 {
239  if(fileXioInited)
240  {
243 
244  memset(&cd0, 0, sizeof(cd0));
245 
246  fileXioInited = 0;
247  }
248 }
249 
250 void fileXioStop(void)
251 {
252  if(fileXioInit() < 0)
253  return;
254 
255  SifCallRpc(&cd0, FILEXIO_STOP, 0, sbuff, 0, sbuff, 0, 0, 0);
256 
257  return;
258 }
259 
260 int fileXioGetDeviceList(struct fileXioDevice deviceEntry[], unsigned int req_entries)
261 {
262  int rv;
264 
265  if(fileXioInit() < 0)
266  return -ENOPKG;
267 
268  _lock();
270 
271  packet->deviceEntry = deviceEntry;
272  packet->reqEntries = req_entries;
273 
274  // This will get the directory contents, and fill dirEntry via DMA
275  if((rv = SifCallRpc(&cd0, FILEXIO_GETDEVICELIST, fileXioBlockMode, sbuff, sizeof(struct fxio_devlist_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
276  {
277  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
278  else { rv = sbuff[0]; }
279  }
280  else
282 
283  _unlock();
284  return(rv);
285 }
286 
287 int fileXioGetdir(const char* pathname, struct fileXioDirEntry dirEntry[], unsigned int req_entries)
288 {
289  int rv;
291 
292  if(fileXioInit() < 0)
293  return -ENOPKG;
294 
295  _lock();
297 
298  // copy the requested pathname to the rpc buffer
299  strncpy(packet->pathname, pathname, sizeof(packet->pathname));
300 
301  SifWriteBackDCache(dirEntry, (sizeof(struct fileXioDirEntry) * req_entries));
302 
303  packet->dirEntry = dirEntry;
304  packet->reqEntries = req_entries;
305 
306  // This will get the directory contents, and fill dirEntry via DMA
307  if((rv = SifCallRpc(&cd0, FILEXIO_GETDIR, fileXioBlockMode, sbuff, sizeof(struct fxio_getdir_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
308  {
309  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
310  else { rv = sbuff[0]; }
311  }
312  else
314 
315  _unlock();
316  return(rv);
317 }
318 
319 int fileXioMount(const char* mountpoint, const char* mountstring, int flag)
320 {
321  int rv;
323 
324  if(fileXioInit() < 0)
325  return -ENOPKG;
326 
327  _lock();
329 
330  strncpy(packet->blockdevice, mountstring, sizeof(packet->blockdevice));
331  strncpy(packet->mountpoint, mountpoint, sizeof(packet->mountpoint));
332  packet->flags = flag;
333 
334  if((rv = SifCallRpc(&cd0, FILEXIO_MOUNT, fileXioBlockMode, sbuff, sizeof(struct fxio_mount_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
335  {
336  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
337  else { rv = sbuff[0]; }
338  }
339  else
341 
342  _unlock();
343  return(rv);
344 }
345 
346 int fileXioUmount(const char* mountpoint)
347 {
348  int rv;
350 
351  if(fileXioInit() < 0)
352  return -ENOPKG;
353 
354  _lock();
356 
357  strncpy(packet->mountpoint, mountpoint, sizeof(packet->mountpoint));
358 
359  if((rv = SifCallRpc(&cd0, FILEXIO_UMOUNT, fileXioBlockMode, sbuff, sizeof(struct fxio_unmount_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
360  {
361  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
362  else { rv = sbuff[0]; }
363  }
364  else
366 
367  _unlock();
368  return(rv);
369 }
370 
371 int fileXioCopyfile(const char* source, const char* dest, int mode)
372 {
373  int rv;
375 
376  if(fileXioInit() < 0)
377  return -ENOPKG;
378 
379  _lock();
381 
382  strncpy(packet->source, source, sizeof(packet->source));
383  strncpy(packet->dest, dest, sizeof(packet->dest));
384  packet->mode = mode;
385 
386  if((rv = SifCallRpc(&cd0, FILEXIO_COPYFILE, fileXioBlockMode, sbuff, sizeof(struct fxio_copyfile_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
387  {
388  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
389  else { rv = sbuff[0]; }
390  }
391  else
393 
394  _unlock();
395  return(rv);
396 }
397 
398 int fileXioMkdir(const char* pathname, int mode)
399 {
400  int rv;
402 
403  if(fileXioInit() < 0)
404  return -ENOPKG;
405 
406  _lock();
408 
409  strncpy(packet->pathname, pathname, sizeof(packet->pathname));
410  packet->mode = mode;
411 
412  if((rv = SifCallRpc(&cd0, FILEXIO_MKDIR, fileXioBlockMode, sbuff, sizeof(struct fxio_mkdir_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
413  {
414  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
415  else { rv = sbuff[0]; }
416  }
417  else
419 
420  _unlock();
421  return(rv);
422 }
423 
424 int fileXioRmdir(const char* pathname)
425 {
426  int rv;
428 
429  if(fileXioInit() < 0)
430  return -ENOPKG;
431 
432  _lock();
434 
435  strncpy(packet->pathname, pathname, sizeof(packet->pathname));
436 
437  if((rv = SifCallRpc(&cd0, FILEXIO_RMDIR, fileXioBlockMode, sbuff, sizeof(struct fxio_pathsel_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
438  {
439  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
440  else { rv = sbuff[0]; }
441  }
442  else
444 
445  _unlock();
446  return(rv);
447 }
448 
449 int fileXioRemove(const char* pathname)
450 {
451  int rv;
453 
454  if(fileXioInit() < 0)
455  return -ENOPKG;
456 
457  _lock();
459 
460  strncpy(packet->pathname, pathname, sizeof(packet->pathname));
461 
462  if((rv = SifCallRpc(&cd0, FILEXIO_REMOVE, fileXioBlockMode, sbuff, sizeof(struct fxio_pathsel_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
463  {
464  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
465  else { rv = sbuff[0]; }
466  }
467  else
469 
470  _unlock();
471  return(rv);
472 }
473 
474 int fileXioRename(const char* source, const char* dest)
475 {
476  int rv;
478 
479  if(fileXioInit() < 0)
480  return -ENOPKG;
481 
482  _lock();
484 
485  strncpy(packet->source, source, sizeof(packet->source));
486  strncpy(packet->dest, dest, sizeof(packet->dest));
487 
488  if((rv = SifCallRpc(&cd0, FILEXIO_RENAME, fileXioBlockMode, sbuff, sizeof(struct fxio_rename_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
489  {
490  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
491  else { rv = sbuff[0]; }
492  }
493  else
495 
496  _unlock();
497  return(rv);
498 }
499 
500 int fileXioSymlink(const char* source, const char* dest)
501 {
502  int rv;
504 
505  if(fileXioInit() < 0)
506  return -ENOPKG;
507 
508  _lock();
510 
511  strncpy(packet->source, source, sizeof(packet->source));
512  strncpy(packet->dest, dest, sizeof(packet->dest));
513 
514  if((rv = SifCallRpc(&cd0, FILEXIO_SYMLINK, fileXioBlockMode, sbuff, sizeof(struct fxio_rename_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
515  {
516  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
517  else { rv = sbuff[0]; }
518  }
519  else
521 
522  _unlock();
523  return(rv);
524 }
525 
526 int fileXioReadlink(const char* source, char* buf, int buflen)
527 {
528  int rv;
530 
531  if(fileXioInit() < 0)
532  return -ENOPKG;
533 
534  _lock();
536 
537  if( !IS_UNCACHED_SEG(buf))
539 
540  strncpy(packet->source, source, sizeof(packet->source));
541  packet->buffer = buf;
542  packet->buflen = buflen;
543 
544  if((rv = SifCallRpc(&cd0, FILEXIO_READLINK, fileXioBlockMode, sbuff, sizeof(struct fxio_readlink_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
545  {
546  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
547  else { rv = sbuff[0]; }
548  }
549  else
551 
552  _unlock();
553  return(rv);
554 }
555 
556 int fileXioChdir(const char* pathname)
557 {
558  int rv;
560 
561  if(fileXioInit() < 0)
562  return -ENOPKG;
563 
564  _lock();
566 
567  strncpy(packet->pathname, pathname, sizeof(packet->pathname));
568 
569  if((rv = SifCallRpc(&cd0, FILEXIO_CHDIR, fileXioBlockMode, sbuff, sizeof(struct fxio_pathsel_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
570  {
571  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
572  else { rv = sbuff[0]; }
573  }
574  else
576 
577  _unlock();
578  return(rv);
579 }
580 
581 int fileXioOpen(const char* source, int flags, ...)
582 {
583  int rv, mode;
585  va_list alist;
586 
587  va_start(alist, flags);
588  mode = va_arg(alist, int); //Retrieve the mode argument, regardless of whether it is expected or not.
589  va_end(alist);
590 
591  if(fileXioInit() < 0)
592  return -ENOPKG;
593 
594  _lock();
596 
597  strncpy(packet->pathname, source, sizeof(packet->pathname));
598  packet->flags = flags;
599  packet->mode = mode;
600  if((rv = SifCallRpc(&cd0, FILEXIO_OPEN, fileXioBlockMode, sbuff, sizeof(struct fxio_open_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
601  {
602  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
603  else { rv = sbuff[0]; }
604  }
605  else
607 
608  _unlock();
609  return(rv);
610 }
611 
612 int fileXioClose(int fd)
613 {
614  int rv;
616 
617  if(fileXioInit() < 0)
618  return -ENOPKG;
619 
620  _lock();
622 
623  packet->fd = fd;
624 
625  if((rv = SifCallRpc(&cd0, FILEXIO_CLOSE, fileXioBlockMode, sbuff, sizeof(struct fxio_close_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
626  {
627  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
628  else { rv = sbuff[0]; }
629  }
630  else
632 
633  _unlock();
634  return(rv);
635 }
636 
637 static void recv_intr(void *data_raw)
638 {
639  rests_pkt *rests = UNCACHED_SEG(data_raw);
640 
641  if(rests->ssize) memcpy(rests->sbuf, rests->sbuffer, rests->ssize);
642  if(rests->esize) memcpy(rests->ebuf, rests->ebuffer, rests->esize);
643 
645 }
646 
647 int fileXioRead(int fd, void *buf, int size)
648 {
649  int rv;
651 
652  if(fileXioInit() < 0)
653  return -ENOPKG;
654 
655  _lock();
657 
658  packet->fd = fd;
659  packet->buffer = buf;
660  packet->size = size;
661  packet->intrData = _intr_data;
662 
663  if (!IS_UNCACHED_SEG(buf))
664  SifWriteBackDCache(buf, size);
665 
666  if((rv = SifCallRpc(&cd0, FILEXIO_READ, fileXioBlockMode, sbuff, sizeof(struct fxio_read_packet), sbuff, 4, &recv_intr, _intr_data)) >= 0)
667  {
668  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
669  else { rv = sbuff[0]; }
670  }
671  else
673 
674  _unlock();
675  return(rv);
676 }
677 
678 int fileXioWrite(int fd, const void *buf, int size)
679 {
680  unsigned int miss;
681  int rv;
683 
684  if(fileXioInit() < 0)
685  return -ENOPKG;
686 
687  _lock();
689 
690  if((unsigned int)buf & 0x3F)
691  {
692  miss = 64 - ((unsigned int)buf & 0x3F);
693  if(miss > size) miss = size;
694  } else {
695  miss = 0;
696  }
697 
698  packet->fd = fd;
699  packet->buffer = buf;
700  packet->size = size;
701  packet->unalignedDataLen = miss;
702 
703  memcpy(packet->unalignedData, buf, miss);
704 
705  if(!IS_UNCACHED_SEG(buf))
706  SifWriteBackDCache((void*)buf, size);
707 
708  if((rv = SifCallRpc(&cd0, FILEXIO_WRITE, fileXioBlockMode, sbuff, sizeof(struct fxio_write_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
709  {
710  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
711  else { rv = sbuff[0]; }
712  }
713  else
715 
716  _unlock();
717  return(rv);
718 }
719 
720 int fileXioLseek(int fd, int offset, int whence)
721 {
722  int rv;
724 
725  if(fileXioInit() < 0)
726  return -ENOPKG;
727 
728  _lock();
730 
731  packet->fd = fd;
732  packet->offset = (u32)offset;
733  packet->whence = whence;
734 
735  if((rv = SifCallRpc(&cd0, FILEXIO_LSEEK, fileXioBlockMode, sbuff, sizeof(struct fxio_lseek_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
736  {
737  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
738  else { rv = sbuff[0]; }
739  }
740  else
742 
743  _unlock();
744  return(rv);
745 }
746 
747 //
748 // NOTE: 64-bit
749 //
751 {
752  s64 rv;
754  struct fxio_lseek64_return_pkt *ret_packet=(struct fxio_lseek64_return_pkt*)sbuff;
755 
756  if(fileXioInit() < 0)
757  return -ENOPKG;
758 
759  _lock();
761 
762  packet->fd = fd;
763  packet->offset_lo = (u32)(offset & 0xffffffff);
764  packet->offset_hi = (u32)((offset >> 32) & 0xffffffff);
765  packet->whence = whence;
766 
767  if((rv = SifCallRpc(&cd0, FILEXIO_LSEEK64, fileXioBlockMode, sbuff, sizeof(struct fxio_lseek64_packet), sbuff, 8, (void *)&_fxio_intr, NULL)) >= 0)
768  {
769  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
770  else {
771  s64 rvHI = ret_packet->pos_hi;
772  rvHI = rvHI << 32;
773  rv = rvHI | ret_packet->pos_lo;
774  }
775  }
776  else
778 
779  _unlock();
780 
781  return(rv);
782 }
783 
784 int fileXioChStat(const char *name, iox_stat_t *stat, int mask)
785 {
786  int rv;
788 
789  if(fileXioInit() < 0)
790  return -ENOPKG;
791 
792  _lock();
794 
795  strncpy(packet->pathname, name, sizeof(packet->pathname));
796  packet->stat = stat;
797  packet->mask = mask;
798 
799  if(!IS_UNCACHED_SEG(stat))
801 
802  if((rv = SifCallRpc(&cd0, FILEXIO_CHSTAT, fileXioBlockMode, sbuff, sizeof(struct fxio_chstat_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
803  {
804  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
805  else { rv = sbuff[0]; }
806  }
807  else
809 
810  _unlock();
811  return(rv);
812 }
813 
814 int fileXioGetStat(const char *name, iox_stat_t *stat)
815 {
816  int rv;
818 
819  if(fileXioInit() < 0)
820  return -ENOPKG;
821 
822  _lock();
824 
825  strncpy(packet->pathname, name, sizeof(packet->pathname));
826  packet->stat = stat;
827 
828  if(!IS_UNCACHED_SEG(stat))
830 
831  if((rv = SifCallRpc(&cd0, FILEXIO_GETSTAT, fileXioBlockMode, sbuff, sizeof(struct fxio_getstat_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
832  {
833  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
834  else { rv = sbuff[0]; }
835  }
836  else
838 
839  _unlock();
840  return(rv);
841 }
842 
843 int fileXioFormat(const char *dev, const char *blockdev, const void *args, int arglen)
844 {
845  int rv;
847 
848  if(fileXioInit() < 0)
849  return -ENOPKG;
850 
851  _lock();
853 
854  strncpy(packet->device, dev, sizeof(packet->device));
855  if(blockdev)
856  strncpy(packet->blockDevice, blockdev, sizeof(packet->blockDevice));
857 
858  if(arglen > sizeof(packet->args)) arglen = sizeof(packet->args);
859  memcpy(packet->args, args, arglen);
860  packet->arglen = arglen;
861 
862  if((rv = SifCallRpc(&cd0, FILEXIO_FORMAT, fileXioBlockMode, sbuff, sizeof(struct fxio_format_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
863  {
864  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
865  else { rv = sbuff[0]; }
866  }
867  else
869 
870  _unlock();
871  return(rv);
872 }
873 
874 int fileXioSync(const char *devname, int flag)
875 {
876  int rv;
878 
879  if(fileXioInit() < 0)
880  return -ENOPKG;
881 
882  _lock();
884 
885  strncpy(packet->device, devname, sizeof(packet->device));
886  packet->flags = flag;
887 
888  if((rv = SifCallRpc(&cd0, FILEXIO_SYNC, fileXioBlockMode, sbuff, sizeof(struct fxio_sync_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
889  {
890  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
891  else { rv = sbuff[0]; }
892  }
893  else
895 
896  _unlock();
897  return(rv);
898 }
899 
900 int fileXioDopen(const char *name)
901 {
902  int rv;
904 
905  if(fileXioInit() < 0)
906  return -ENOPKG;
907 
908  _lock();
910 
911  strncpy(packet->pathname, name, sizeof(packet->pathname));
912  if((rv = SifCallRpc(&cd0, FILEXIO_DOPEN, fileXioBlockMode, sbuff, sizeof(struct fxio_pathsel_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
913  {
914  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
915  else { rv = sbuff[0]; }
916  }
917  else
919 
920  _unlock();
921  return(rv);
922 }
923 
924 int fileXioDclose(int fd)
925 {
926  int rv;
928 
929  if(fileXioInit() < 0)
930  return -ENOPKG;
931 
932  _lock();
934 
935  packet->fd = fd;
936  if((rv = SifCallRpc(&cd0, FILEXIO_DCLOSE, fileXioBlockMode, sbuff, sizeof(struct fxio_close_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
937  {
938  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
939  else { rv = sbuff[0]; }
940  }
941  else
943 
944  _unlock();
945  return(rv);
946 }
947 
948 int fileXioDread(int fd, iox_dirent_t *dirent)
949 {
950  int rv;
952 
953  if(fileXioInit() < 0)
954  return -ENOPKG;
955 
956  _lock();
958 
959  packet->fd = fd;
960  packet->dirent = dirent;
961 
962  if (!IS_UNCACHED_SEG(dirent))
964 
965  if((rv = SifCallRpc(&cd0, FILEXIO_DREAD, fileXioBlockMode, sbuff, sizeof(struct fxio_dread_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
966  {
967  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
968  else { rv = sbuff[0]; }
969  }
970  else
972 
973  _unlock();
974  return(rv);
975 }
976 
977 static void fxio_ctl_intr(void *data_raw)
978 {
979  struct fxio_ctl_return_pkt *pkt = UNCACHED_SEG(data_raw);
980 
981  memcpy(pkt->dest, pkt->buf, pkt->len);
982 
984 }
985 
986 int fileXioDevctl(const char *name, int cmd, void *arg, unsigned int arglen, void *buf, unsigned int buflen)
987 {
989  int rv;
990 
991  if(fileXioInit() < 0)
992  return -ENOPKG;
993 
994  _lock();
996 
999  strncpy(packet->name, name, CTL_BUF_SIZE);
1000  packet->name[CTL_BUF_SIZE-1] = '\0';
1001  memcpy(packet->arg, arg, arglen);
1002 
1003  packet->cmd = cmd;
1004  packet->arglen = arglen;
1005  packet->buf = buf;
1006  packet->buflen = buflen;
1007  packet->intr_data = _intr_data;
1008 
1010 
1011  if(buflen)
1013  else
1014  rv = SifCallRpc(&cd0, FILEXIO_DEVCTL, fileXioBlockMode, packet, sizeof(struct fxio_devctl_packet), sbuff, 4, (void *)&_fxio_intr, NULL);
1015 
1016  if(rv >= 0)
1017  {
1018  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
1019  else { rv = sbuff[0]; }
1020  }
1021  else
1023 
1024  _unlock();
1025  return(rv);
1026 }
1027 
1028 int fileXioIoctl(int fd, int cmd, void *arg){
1029  struct fxio_ioctl_packet *packet = (struct fxio_ioctl_packet *)sbuff;
1030  int rv;
1031 
1032  if(fileXioInit() < 0)
1033  return -ENOPKG;
1034 
1035  _lock();
1037 
1038  memcpy(packet->arg, arg, IOCTL_BUF_SIZE);
1039 
1040  packet->fd = fd;
1041  packet->cmd = cmd;
1042 
1043  if((rv = SifCallRpc(&cd0, FILEXIO_IOCTL, fileXioBlockMode, packet, sizeof(struct fxio_ioctl_packet), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
1044  {
1045  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
1046  else { rv = sbuff[0]; }
1047  }
1048  else
1050 
1051  _unlock();
1052  return(rv);
1053 }
1054 
1055 int fileXioIoctl2(int fd, int command, void *arg, unsigned int arglen, void *buf, unsigned int buflen)
1056 {
1057  struct fxio_ioctl2_packet *packet = (struct fxio_ioctl2_packet *)sbuff;
1058  int rv;
1059 
1060  if(fileXioInit() < 0)
1061  return -ENOPKG;
1062 
1063  _lock();
1065 
1068  memcpy(packet->arg, arg, arglen);
1069 
1070  packet->fd = fd;
1071  packet->cmd = command;
1072  packet->arglen = arglen;
1073  packet->buf = buf;
1074  packet->buflen = buflen;
1075  packet->intr_data = _intr_data;
1076 
1078 
1079  if(buflen)
1081  else
1082  rv = SifCallRpc(&cd0, FILEXIO_IOCTL2, fileXioBlockMode, packet, sizeof(struct fxio_ioctl2_packet), sbuff, 4, (void *)&_fxio_intr, NULL);
1083 
1084  if(rv >= 0)
1085  {
1086  if(fileXioBlockMode == FXIO_NOWAIT) { rv = 0; }
1087  else { rv = sbuff[0]; }
1088  }
1089  else
1091 
1092  _unlock();
1093  return(rv);
1094 }
1095 
1096 int fileXioWaitAsync(int mode, int *retVal)
1097 {
1098  if(fileXioInit() < 0)
1099  return -ENOPKG;
1100 
1101  if(fileXioBlockMode != FXIO_NOWAIT) return 0;
1102 
1103  switch(mode)
1104  {
1105  case FXIO_WAIT:
1106 
1109 
1110  if(retVal != NULL)
1111  *retVal = *(int *)UNCACHED_SEG(&sbuff[0]);
1112 
1113  return FXIO_COMPLETE;
1114 
1115  case FXIO_NOWAIT:
1116 
1118  return FXIO_INCOMPLETE;
1119 
1121 
1122  if(retVal != NULL)
1123  *retVal = *(int *)UNCACHED_SEG(&sbuff[0]);
1124 
1125  return FXIO_COMPLETE;
1126 
1127  default:
1128  return -1;
1129  }
1130 }
1131 
1132 void fileXioSetBlockMode(int blocking)
1133 {
1134  fileXioBlockMode = blocking;
1135 }
1136 
1138  struct fxio_rwbuff *packet = (struct fxio_rwbuff *)sbuff;
1139  int rv;
1140 
1141  if(fileXioInit() < 0)
1142  return -ENOPKG;
1143 
1144  _lock();
1146 
1147  packet->size = size;
1148 
1149  if((rv = SifCallRpc(&cd0, FILEXIO_SETRWBUFFSIZE, 0, packet, sizeof(struct fxio_rwbuff), sbuff, 4, (void *)&_fxio_intr, NULL)) >= 0)
1150  {
1151  rv = sbuff[0];
1152  }
1153  else
1155 
1156  _unlock();
1157  return(rv);
1158 }
1159 
#define sp
Definition: as_reg_compat.h:85
#define ENOPKG
Definition: errno.h:140
#define IOCTL_BUF_SIZE
Definition: fileXio.h:86
#define CTL_BUF_SIZE
Definition: fileXio.h:85
#define FILEXIO_IRX
Definition: fileXio.h:32
@ FILEXIO_MOUNT
Definition: fileXio.h:37
@ FILEXIO_MKDIR
Definition: fileXio.h:62
@ FILEXIO_GETDIR
Definition: fileXio.h:39
@ FILEXIO_RENAME
Definition: fileXio.h:54
@ FILEXIO_IOCTL2
Definition: fileXio.h:60
@ FILEXIO_OPEN
Definition: fileXio.h:42
@ FILEXIO_GETDEVICELIST
Definition: fileXio.h:64
@ FILEXIO_SYNC
Definition: fileXio.h:56
@ FILEXIO_CHDIR
Definition: fileXio.h:55
@ FILEXIO_READ
Definition: fileXio.h:44
@ FILEXIO_DEVCTL
Definition: fileXio.h:57
@ FILEXIO_READLINK
Definition: fileXio.h:59
@ FILEXIO_FORMAT
Definition: fileXio.h:51
@ FILEXIO_STOP
Definition: fileXio.h:40
@ FILEXIO_COPYFILE
Definition: fileXio.h:41
@ FILEXIO_SYMLINK
Definition: fileXio.h:58
@ FILEXIO_GETSTAT
Definition: fileXio.h:49
@ FILEXIO_DREAD
Definition: fileXio.h:35
@ FILEXIO_DCLOSE
Definition: fileXio.h:36
@ FILEXIO_LSEEK
Definition: fileXio.h:46
@ FILEXIO_WRITE
Definition: fileXio.h:45
@ FILEXIO_REMOVE
Definition: fileXio.h:63
@ FILEXIO_RMDIR
Definition: fileXio.h:48
@ FILEXIO_LSEEK64
Definition: fileXio.h:61
@ FILEXIO_UMOUNT
Definition: fileXio.h:38
@ FILEXIO_SETRWBUFFSIZE
Definition: fileXio.h:65
@ FILEXIO_DOPEN
Definition: fileXio.h:34
@ FILEXIO_CLOSE
Definition: fileXio.h:43
@ FILEXIO_IOCTL
Definition: fileXio.h:47
@ FILEXIO_CHSTAT
Definition: fileXio.h:50
static void fxio_ctl_intr(void *data_raw)
Definition: fileXio_rpc.c:977
int fileXioMkdir(const char *pathname, int mode)
Definition: fileXio_rpc.c:398
static void fileXioRewinddirHelper(DIR *dir)
Definition: fileXio_rpc.c:158
int fileXioMount(const char *mountpoint, const char *mountstring, int flag)
Definition: fileXio_rpc.c:319
int fileXioDread(int fd, iox_dirent_t *dirent)
Definition: fileXio_rpc.c:948
void fileXioExit(void)
Definition: fileXio_rpc.c:237
int fileXioRename(const char *source, const char *dest)
Definition: fileXio_rpc.c:474
int fileXioChdir(const char *pathname)
Definition: fileXio_rpc.c:556
static void fill_stat(struct stat *stat, const iox_stat_t *fiostat)
Definition: fileXio_rpc.c:84
static void recv_intr(void *data_raw)
Definition: fileXio_rpc.c:637
static int fileXioClosedirHelper(DIR *dir)
Definition: fileXio_rpc.c:163
static struct dirent * fileXioReaddirHelper(DIR *dir)
Definition: fileXio_rpc.c:133
int fileXioSymlink(const char *source, const char *dest)
Definition: fileXio_rpc.c:500
int fileXioClose(int fd)
Definition: fileXio_rpc.c:612
static DIR * fileXioOpendirHelper(const char *path)
Definition: fileXio_rpc.c:114
int fileXioCopyfile(const char *source, const char *dest, int mode)
Definition: fileXio_rpc.c:371
int fileXioDevctl(const char *name, int cmd, void *arg, unsigned int arglen, void *buf, unsigned int buflen)
Definition: fileXio_rpc.c:986
int fileXioRead(int fd, void *buf, int size)
Definition: fileXio_rpc.c:647
int fileXioIoctl(int fd, int cmd, void *arg)
Definition: fileXio_rpc.c:1028
int fileXioRmdir(const char *pathname)
Definition: fileXio_rpc.c:424
static SifRpcClientData_t cd0
Definition: fileXio_rpc.c:32
static void _fxio_intr(void)
Definition: fileXio_rpc.c:39
void fileXioSetBlockMode(int blocking)
Definition: fileXio_rpc.c:1132
int fileXioRemove(const char *pathname)
Definition: fileXio_rpc.c:449
static mode_t iox_to_posix_mode(unsigned int ps2mode)
Definition: fileXio_rpc.c:67
static int fileXioGetstatHelper(const char *path, struct stat *buf)
Definition: fileXio_rpc.c:101
int fileXioInit(void)
Definition: fileXio_rpc.c:176
int _iop_reboot_count
int fileXioDclose(int fd)
Definition: fileXio_rpc.c:924
int fileXioSetRWBufferSize(int size)
Definition: fileXio_rpc.c:1137
static int fileXioCompletionSema
Definition: fileXio_rpc.c:37
int fileXioDopen(const char *name)
Definition: fileXio_rpc.c:900
void fileXioStop(void)
Definition: fileXio_rpc.c:250
int fileXioSync(const char *devname, int flag)
Definition: fileXio_rpc.c:874
s64 fileXioLseek64(int fd, s64 offset, int whence)
Definition: fileXio_rpc.c:750
int fileXioUmount(const char *mountpoint)
Definition: fileXio_rpc.c:346
int fileXioReadlink(const char *source, char *buf, int buflen)
Definition: fileXio_rpc.c:526
static unsigned int sbuff[0x1300]
Definition: fileXio_rpc.c:33
static int _lock_sema_id
Definition: fileXio_rpc.c:44
int fileXioIoctl2(int fd, int command, void *arg, unsigned int arglen, void *buf, unsigned int buflen)
Definition: fileXio_rpc.c:1055
static int _intr_data[0xC00]
Definition: fileXio_rpc.c:34
int fileXioOpen(const char *source, int flags,...)
Definition: fileXio_rpc.c:581
static int fileXioBlockMode
Definition: fileXio_rpc.c:36
int fileXioWrite(int fd, const void *buf, int size)
Definition: fileXio_rpc.c:678
int fileXioGetStat(const char *name, iox_stat_t *stat)
Definition: fileXio_rpc.c:814
int fileXioGetdir(const char *pathname, struct fileXioDirEntry dirEntry[], unsigned int req_entries)
Definition: fileXio_rpc.c:287
int fileXioLseek(int fd, int offset, int whence)
Definition: fileXio_rpc.c:720
int fileXioFormat(const char *dev, const char *blockdev, const void *args, int arglen)
Definition: fileXio_rpc.c:843
int fileXioWaitAsync(int mode, int *retVal)
Definition: fileXio_rpc.c:1096
static int _unlock(void)
Definition: fileXio_rpc.c:50
static time_t io_to_posix_time(const unsigned char *ps2time)
Definition: fileXio_rpc.c:55
int fileXioGetDeviceList(struct fileXioDevice deviceEntry[], unsigned int req_entries)
Definition: fileXio_rpc.c:260
static int _lock(void)
Definition: fileXio_rpc.c:45
int fileXioChStat(const char *name, iox_stat_t *stat, int mask)
Definition: fileXio_rpc.c:784
static int fileXioInited
Definition: fileXio_rpc.c:35
#define FXIO_WAIT
Definition: fileXio_rpc.h:27
#define FXIO_NOWAIT
Definition: fileXio_rpc.h:28
#define FXIO_COMPLETE
Definition: fileXio_rpc.h:30
#define FXIO_INCOMPLETE
Definition: fileXio_rpc.h:31
packet_t packet
Definition: font.c:24
#define FIO_S_IXGRP
Definition: iox_stat.h:71
#define FIO_S_IROTH
Definition: iox_stat.h:76
#define FIO_S_IXUSR
Definition: iox_stat.h:62
#define FIO_S_IRGRP
Definition: iox_stat.h:67
#define FIO_S_IXOTH
Definition: iox_stat.h:80
#define FIO_S_IWOTH
Definition: iox_stat.h:78
#define FIO_S_IRUSR
Definition: iox_stat.h:58
#define FIO_S_IWGRP
Definition: iox_stat.h:69
#define FIO_S_IFDIR
Definition: iox_stat.h:45
#define FIO_S_IFREG
Definition: iox_stat.h:43
#define FIO_S_IWUSR
Definition: iox_stat.h:60
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
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
s32 command
Definition: libpad.c:156
DIR *(* _ps2sdk_opendir)(const char *path)
Definition: ps2sdkapi.c:185
int(* _ps2sdk_stat)(const char *path, struct stat *buf)
Definition: ps2sdkapi.c:183
void(* _ps2sdk_rewinddir)(DIR *dir)
Definition: ps2sdkapi.c:187
int64_t(* _ps2sdk_lseek64)(int, int64_t, int)
Definition: ps2sdkapi.c:175
int(* _ps2sdk_closedir)(DIR *dir)
Definition: ps2sdkapi.c:188
int(* _ps2sdk_lseek)(int, int, int)
Definition: ps2sdkapi.c:174
int(* _ps2sdk_write)(int, const void *, int)
Definition: ps2sdkapi.c:176
int(* _ps2sdk_close)(int)
Definition: ps2sdkapi.c:171
int(* _ps2sdk_ioctl)(int, int, void *)
Definition: ps2sdkapi.c:177
int(* _ps2sdk_remove)(const char *)
Definition: ps2sdkapi.c:178
int(* _ps2sdk_rename)(const char *, const char *)
Definition: ps2sdkapi.c:179
struct dirent *(* _ps2sdk_readdir)(DIR *dir)
Definition: ps2sdkapi.c:186
int(* _ps2sdk_read)(int, void *, int)
Definition: ps2sdkapi.c:173
int(* _ps2sdk_mkdir)(const char *, int)
Definition: ps2sdkapi.c:180
int(* _ps2sdk_rmdir)(const char *)
Definition: ps2sdkapi.c:181
int(* _ps2sdk_open)(const char *, int,...)
Definition: ps2sdkapi.c:172
s32 mode
Definition: rpc_client.c:15
void SifWriteBackDCache(void *ptr, int size)
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
iox_stat_t * stat
Definition: fileXio.h:203
char source[512]
Definition: fileXio.h:136
char dest[512]
Definition: fileXio.h:137
u8 buf[CTL_BUF_SIZE]
Definition: fileXio.h:258
u8 arg[CTL_BUF_SIZE]
Definition: fileXio.h:231
char name[CTL_BUF_SIZE]
Definition: fileXio.h:230
struct fileXioDevice * deviceEntry
Definition: fileXio.h:115
iox_dirent_t * dirent
Definition: fileXio.h:226
char args[512]
Definition: fileXio.h:215
char pathname[512]
Definition: fileXio.h:120
struct fileXioDirEntry * dirEntry
Definition: fileXio.h:121
iox_stat_t * stat
Definition: fileXio.h:209
u8 arg[CTL_BUF_SIZE]
Definition: fileXio.h:247
u8 arg[IOCTL_BUF_SIZE]
Definition: fileXio.h:241
char pathname[512]
Definition: fileXio.h:142
char mountpoint[512]
Definition: fileXio.h:127
char pathname[512]
Definition: fileXio.h:148
char source[512]
Definition: fileXio.h:153
char dest[512]
Definition: fileXio.h:154
int size
Definition: fileXio.h:267
char mountpoint[512]
Definition: fileXio.h:132
char name[256]
Definition: iox_stat.h:112
iox_stat_t stat
Definition: iox_stat.h:111
unsigned char ctime[8]
Definition: iox_stat.h:96
unsigned char mtime[8]
Definition: iox_stat.h:98
unsigned int hisize
Definition: iox_stat.h:99
unsigned int size
Definition: iox_stat.h:95
unsigned int mode
Definition: iox_stat.h:93
unsigned char atime[8]
Definition: iox_stat.h:97
u8 sbuffer[64]
Definition: fileXio.h:74
void * sbuf
Definition: fileXio.h:72
int ssize
Definition: fileXio.h:70
u8 ebuffer[64]
Definition: fileXio.h:75
void * ebuf
Definition: fileXio.h:73
int esize
Definition: fileXio.h:71
#define NULL
Definition: tamtypes.h:91
unsigned int u32
Definition: tamtypes.h:30
signed long s64
Definition: tamtypes.h:62
unsigned short u16
Definition: tamtypes.h:24