libwbfs_win32.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #ifdef WIN32
  2. #include <windows.h>
  3. #include <setupapi.h>
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <winioctl.h>
  10. #include "libwbfs.h"
  11. void *wbfs_open_file_for_read(char*filename)
  12. {
  13. HANDLE *handle = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
  14. if (handle == INVALID_HANDLE_VALUE)
  15. {
  16. fprintf(stderr, "unable to open disc file\n");
  17. return 0;
  18. }
  19. return (void*)handle;
  20. }
  21. void *wbfs_open_file_for_write(char*filename)
  22. {
  23. HANDLE *handle = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
  24. if (handle == INVALID_HANDLE_VALUE)
  25. {
  26. fprintf(stderr, "unable to open file\n");
  27. return 0;
  28. }
  29. return (void*)handle;
  30. }
  31. int wbfs_read_file(void*handle, int len, void *buf)
  32. {
  33. DWORD read;
  34. ReadFile((HANDLE)handle, buf, len, &read, NULL);
  35. return read;
  36. }
  37. int wbfs_close_file(void *handle)
  38. {
  39. return CloseHandle((HANDLE)handle) == 0 ? 1 : 0;
  40. }
  41. int wbfs_file_reserve_space(void*handle,long long size)
  42. {
  43. int result;
  44. LARGE_INTEGER large;
  45. large.QuadPart = size;
  46. result = SetFilePointerEx((HANDLE)handle, large, NULL, FILE_BEGIN);
  47. if ( result != 0 ) {
  48. result = SetEndOfFile((HANDLE)handle);
  49. }
  50. return result == 0 ? 1 : 0;
  51. }
  52. void wbfs_file_truncate(void *handle,long long size)
  53. {
  54. _chsize(fileno((FILE*)handle),size);
  55. }
  56. int wbfs_read_wii_file(void *_handle, u32 _offset, u32 count, void *buf)
  57. {
  58. HANDLE *handle = (HANDLE *)_handle;
  59. LARGE_INTEGER large;
  60. DWORD read;
  61. u64 offset = _offset;
  62. offset <<= 2;
  63. large.QuadPart = offset;
  64. if (SetFilePointerEx(handle, large, NULL, FILE_BEGIN) == FALSE)
  65. {
  66. wbfs_error("error seeking in disc file");
  67. return 1;
  68. }
  69. read = 0;
  70. if ((ReadFile(handle, buf, count, &read, NULL) == FALSE) || !read)
  71. {
  72. wbfs_error("error reading wii disc sector");
  73. return 1;
  74. }
  75. if (read < count)
  76. {
  77. wbfs_warning("warning: requested %d, but read only %d bytes (trimmed or bad padded ISO)", count, read);
  78. wbfs_memset((u8*)buf+read, 0, count-read);
  79. }
  80. return 0;
  81. }
  82. int wbfs_write_wii_file(void *_handle, u32 lba, u32 count, void *buf)
  83. {
  84. HANDLE *handle = (HANDLE *)_handle;
  85. LARGE_INTEGER large;
  86. DWORD written;
  87. u64 offset = lba;
  88. offset *= 0x8000;
  89. large.QuadPart = offset;
  90. if (SetFilePointerEx(handle, large, NULL, FILE_BEGIN) == FALSE)
  91. {
  92. fprintf(stderr,"\n\n%lld %p\n", offset, handle);
  93. wbfs_error("error seeking in wii disc sector (write)");
  94. return 1;
  95. }
  96. written = 0;
  97. if (WriteFile(handle, buf, count * 0x8000, &written, NULL) == FALSE)
  98. {
  99. wbfs_error("error writing wii disc sector");
  100. return 1;
  101. }
  102. if (written != count * 0x8000)
  103. {
  104. wbfs_error("error writing wii disc sector (size mismatch)");
  105. return 1;
  106. }
  107. return 0;
  108. }
  109. static int read_sector(void *_handle, u32 lba, u32 count, void *buf)
  110. {
  111. HANDLE *handle = (HANDLE *)_handle;
  112. LARGE_INTEGER large;
  113. DWORD read;
  114. u64 offset = lba;
  115. offset *= 512ULL;
  116. large.QuadPart = offset;
  117. if (SetFilePointerEx(handle, large, NULL, FILE_BEGIN) == FALSE)
  118. {
  119. fprintf(stderr, "\n\n%lld %d %p\n", offset, count, _handle);
  120. wbfs_error("error seeking in hd sector (read)");
  121. return 1;
  122. }
  123. read = 0;
  124. if (ReadFile(handle, buf, count * 512ULL, &read, NULL) == FALSE)
  125. {
  126. wbfs_error("error reading hd sector");
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. static int write_sector(void *_handle, u32 lba, u32 count, void *buf)
  132. {
  133. HANDLE *handle = (HANDLE *)_handle;
  134. LARGE_INTEGER large;
  135. DWORD written;
  136. u64 offset = lba;
  137. offset *= 512ULL;
  138. large.QuadPart = offset;
  139. if (SetFilePointerEx(handle, large, NULL, FILE_BEGIN) == FALSE)
  140. {
  141. wbfs_error("error seeking in hd sector (write)");
  142. return 1;
  143. }
  144. written = 0;
  145. if (WriteFile(handle, buf, count * 512ULL, &written, NULL) == FALSE)
  146. {
  147. wbfs_error("error writing hd sector");
  148. return 1;
  149. }
  150. return 0;
  151. }
  152. static void close_handle(void *handle)
  153. {
  154. CloseHandle((HANDLE *)handle);
  155. }
  156. static int get_capacity(char *fileName, u32 *sector_size, u32 *sector_count)
  157. {
  158. DISK_GEOMETRY dg;
  159. PARTITION_INFORMATION pi;
  160. DWORD bytes;
  161. HANDLE *handle = CreateFile(fileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL);
  162. if (handle == INVALID_HANDLE_VALUE)
  163. {
  164. wbfs_error("could not open drive");
  165. return 0;
  166. }
  167. if (DeviceIoControl(handle, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &dg, sizeof(DISK_GEOMETRY), &bytes, NULL) == FALSE)
  168. {
  169. CloseHandle(handle);
  170. wbfs_error("could not get drive geometry");
  171. return 0;
  172. }
  173. *sector_size = dg.BytesPerSector;
  174. if (DeviceIoControl(handle, IOCTL_DISK_GET_PARTITION_INFO, NULL, 0, &pi, sizeof(PARTITION_INFORMATION), &bytes, NULL) == FALSE)
  175. {
  176. CloseHandle(handle);
  177. wbfs_error("could not get partition info");
  178. return 0;
  179. }
  180. *sector_count = (u32)(pi.PartitionLength.QuadPart / dg.BytesPerSector);
  181. CloseHandle(handle);
  182. return 1;
  183. }
  184. wbfs_t *wbfs_try_open_hd(char *driveName, int reset)
  185. {
  186. wbfs_error("no direct harddrive support");
  187. return 0;
  188. }
  189. wbfs_t *wbfs_try_open_partition(char *partitionLetter, int reset)
  190. {
  191. HANDLE *handle;
  192. char drivePath[8] = "\\\\?\\Z:";
  193. u32 sector_size, sector_count;
  194. if (strlen(partitionLetter) != 1)
  195. {
  196. wbfs_error("bad drive name");
  197. return NULL;
  198. }
  199. drivePath[4] = partitionLetter[0];
  200. if (!get_capacity(drivePath, &sector_size, &sector_count))
  201. {
  202. return NULL;
  203. }
  204. handle = CreateFile(drivePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL);
  205. if (handle == INVALID_HANDLE_VALUE)
  206. {
  207. return NULL;
  208. }
  209. return wbfs_open_partition(read_sector, write_sector, close_handle, handle, sector_size, sector_count, 0, reset);
  210. }
  211. wbfs_t *wbfs_try_open(char *disc, char *partition, int reset)
  212. {
  213. wbfs_t *p = 0;
  214. if (partition)
  215. {
  216. p = wbfs_try_open_partition(partition,reset);
  217. }
  218. if (!p && !reset && disc)
  219. {
  220. p = 0;
  221. }
  222. else if(!p && !reset)
  223. {
  224. p = 0;
  225. }
  226. return p;
  227. }
  228. #endif