libwbfs_unix.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #if defined( __linux__) || defined(__APPLE__) || defined(__CYGWIN__)
  2. #include <stdio.h>
  3. #include <sys/ioctl.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #if defined(__linux__)
  7. #include <linux/fs.h>
  8. #elif defined(__CYGWIN__)
  9. #include <cygwin/fs.h>
  10. #else
  11. #include <sys/disk.h>
  12. #endif
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include "libwbfs.h"
  16. void *wbfs_open_file_for_read(char*filename)
  17. {
  18. FILE*f = fopen(filename,"r");
  19. if (!f)
  20. wbfs_fatal("unable to open file\n");
  21. return (void*)f;
  22. }
  23. void *wbfs_open_file_for_write(char*filename)
  24. {
  25. FILE*f = fopen(filename,"w");
  26. if (!f)
  27. wbfs_fatal("unable to open file\n");
  28. return (void*)f;
  29. }
  30. int wbfs_read_file(void*handle, int len, void *buf)
  31. {
  32. return fread(buf,len,1,(FILE*)handle);
  33. }
  34. int wbfs_close_file(void *handle)
  35. {
  36. return fclose((FILE*)handle) == 0 ? 0 : 1;
  37. }
  38. int wbfs_file_reserve_space(void*handle, long long size)
  39. {
  40. FILE*f=(FILE*)handle;
  41. int result;
  42. fseeko(f, size-1ULL, SEEK_SET);
  43. result = fwrite("", 1, 1, f);
  44. return result == 1 ? 0 : 1;
  45. }
  46. void wbfs_file_truncate(void *handle,long long size)
  47. {
  48. ftruncate(fileno((FILE*)handle),size);
  49. }
  50. int wbfs_read_wii_file(void*handle,u32 offset,u32 count,void*iobuf)
  51. {
  52. FILE*fp =handle;
  53. u64 off = offset;
  54. off<<=2;
  55. if (fseeko(fp, off, SEEK_SET))
  56. {
  57. wbfs_error("error seeking in disc file (%ld)",off);
  58. return 1;
  59. }
  60. if (fread(iobuf, count, 1, fp) != 1){
  61. wbfs_error("error reading disc 2");
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. int wbfs_write_wii_file(void*handle,u32 lba,u32 count,void*iobuf)
  67. {
  68. FILE*fp=handle;
  69. u64 off = lba;
  70. off *=0x8000;
  71. if (fseeko(fp, off, SEEK_SET))
  72. {
  73. wbfs_error("error seeking in disc file (%lld)",off);
  74. return 1;
  75. }
  76. if (fwrite(iobuf, count*0x8000, 1, fp) != 1){
  77. wbfs_error("error writing disc file");
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. static int wbfs_fread_sector(void *_fp,u32 lba,u32 count,void*buf)
  83. {
  84. FILE*fp =_fp;
  85. u64 off = lba;
  86. off*=512ULL;
  87. if (fseeko(fp, off, SEEK_SET))
  88. {
  89. fprintf(stderr,"\n\n%lld %d %p\n",off,count,_fp);
  90. wbfs_error("error seeking in disc partition");
  91. return 1;
  92. }
  93. if (fread(buf, count*512ULL, 1, fp) != 1){
  94. wbfs_error("error reading disc 1");
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. static int wbfs_fwrite_sector(void *_fp,u32 lba,u32 count,void*buf)
  100. {
  101. FILE*fp =_fp;
  102. u64 off = lba;
  103. off*=512ULL;
  104. if (fseeko(fp, off, SEEK_SET))
  105. {
  106. wbfs_error("error seeking in disc file");
  107. return 1;
  108. }
  109. if (fwrite(buf, count*512ULL, 1, fp) != 1){
  110. wbfs_error("error writing disc");
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. static int get_capacity(char *file,u32 *sector_size,u32 *n_sector)
  116. {
  117. int fd = open(file,O_RDONLY);
  118. int ret;
  119. if(fd<0){
  120. return 0;
  121. }
  122. #if defined(__linux__) || defined(__CYGWIN__)
  123. if (sizeof(void *) == 8) {
  124. unsigned long long sec_size;
  125. ret = ioctl(fd,BLKSSZGET,&sec_size);
  126. *sector_size = (unsigned int) sec_size;
  127. } else
  128. ret = ioctl(fd,BLKSSZGET,sector_size);
  129. #else //__APPLE__
  130. ret = ioctl(fd,DKIOCGETBLOCKSIZE,sector_size);
  131. #endif
  132. if(ret<0)
  133. {
  134. FILE *f;
  135. close(fd);
  136. f = fopen(file,"r");
  137. fseeko(f,0,SEEK_END);
  138. *n_sector = ftello(f)/512;
  139. *sector_size = 512;
  140. fclose(f);
  141. return 1;
  142. }
  143. #if defined(__linux__) || defined(__CYGWIN__)
  144. if (sizeof(void *) == 8) {
  145. unsigned long long n_sec;
  146. ret = ioctl(fd,BLKGETSIZE,&n_sec);
  147. *n_sector = (unsigned int) n_sec;
  148. } else
  149. ret = ioctl(fd,BLKGETSIZE,n_sector);
  150. #else //__APPLE__
  151. long long my_n_sector;
  152. ret = ioctl(fd,DKIOCGETBLOCKCOUNT,&my_n_sector);
  153. *n_sector = (long)my_n_sector;
  154. #endif
  155. close(fd);
  156. if(*sector_size>512)
  157. *n_sector*=*sector_size/512;
  158. if(*sector_size<512)
  159. *n_sector/=512/ *sector_size;
  160. return 1;
  161. }
  162. wbfs_t *wbfs_try_open_hd(char *fn,int reset)
  163. {
  164. u32 sector_size, n_sector;
  165. if(!get_capacity(fn,&sector_size,&n_sector))
  166. return NULL;
  167. FILE *f = fopen(fn,"r+");
  168. if (!f)
  169. return NULL;
  170. return wbfs_open_hd(wbfs_fread_sector,wbfs_fwrite_sector,f,
  171. sector_size ,n_sector,reset);
  172. }
  173. wbfs_t *wbfs_try_open_partition(char *fn,int reset)
  174. {
  175. u32 sector_size, n_sector;
  176. if(!get_capacity(fn,&sector_size,&n_sector))
  177. return NULL;
  178. FILE *f = fopen(fn,"r+");
  179. if (!f)
  180. return NULL;
  181. return wbfs_open_partition(wbfs_fread_sector,wbfs_fwrite_sector,f,
  182. sector_size ,n_sector,0,reset);
  183. }
  184. wbfs_t *wbfs_try_open(char *disc,char *partition, int reset)
  185. {
  186. wbfs_t *p = 0;
  187. if(partition)
  188. p = wbfs_try_open_partition(partition,reset);
  189. if (!p && !reset && disc)
  190. p = wbfs_try_open_hd(disc,0);
  191. else if(!p && !reset){
  192. char buffer[32];
  193. int i;
  194. #if defined(__linux__) || defined(__CYGWIN__)
  195. for (i='b';i<'z';i++)
  196. {
  197. snprintf(buffer,32,"/dev/sd%c",i);
  198. p = wbfs_try_open_hd(buffer,0);
  199. if (p)
  200. {
  201. fprintf(stderr,"using %s\n",buffer);
  202. return p;
  203. }
  204. snprintf(buffer,32,"/dev/hd%c",i);
  205. p = wbfs_try_open_hd(buffer,0);
  206. if (p)
  207. {
  208. fprintf(stderr,"using %s\n",buffer);
  209. return p;
  210. }
  211. }
  212. #else
  213. int j;
  214. for (i=0;i<10;i++)
  215. for (j=0;j<10;j++){
  216. snprintf(buffer,32,"/dev/disk%ds%d",i,j);
  217. p = wbfs_try_open_partition(buffer,0);
  218. if (p)
  219. {
  220. fprintf(stderr,"using %s\n",buffer);
  221. return p;
  222. }
  223. }
  224. #endif
  225. wbfs_error("cannot find any wbfs partition (verify permissions))");
  226. }
  227. return p;
  228. }
  229. #endif //__linux__ or __APPLE__