summaryrefslogtreecommitdiff
path: root/rbutil/sansapatcher/sansaio-win32.c
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/sansapatcher/sansaio-win32.c')
-rw-r--r--rbutil/sansapatcher/sansaio-win32.c196
1 files changed, 196 insertions, 0 deletions
diff --git a/rbutil/sansapatcher/sansaio-win32.c b/rbutil/sansapatcher/sansaio-win32.c
new file mode 100644
index 0000000000..315b379a28
--- /dev/null
+++ b/rbutil/sansapatcher/sansaio-win32.c
@@ -0,0 +1,196 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: ipodio-win32.c 12263 2007-02-10 19:49:43Z dave $
9 *
10 * Copyright (C) 2006-2007 Dave Chapman
11 *
12 * error(), lock_volume() and unlock_volume() functions and inspiration taken
13 * from:
14 * RawDisk - Direct Disk Read/Write Access for NT/2000/XP
15 * Copyright (c) 2003 Jan Kiszka
16 * http://www.stud.uni-hannover.de/user/73174/RawDisk/
17 *
18 * All files in this archive are subject to the GNU General Public License.
19 * See the file COPYING in the source tree root for full license agreement.
20 *
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
23 *
24 ****************************************************************************/
25
26#include <stdio.h>
27#include <unistd.h>
28#include <fcntl.h>
29#include <string.h>
30#include <stdlib.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#ifdef __WIN32__
34#include <windows.h>
35#include <winioctl.h>
36#endif
37
38#include "sansaio.h"
39
40static int lock_volume(HANDLE hDisk)
41{
42 DWORD dummy;
43
44 return DeviceIoControl(hDisk, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0,
45 &dummy, NULL);
46}
47
48static int unlock_volume(HANDLE hDisk)
49{
50 DWORD dummy;
51
52 return DeviceIoControl(hDisk, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0,
53 &dummy, NULL);
54}
55
56void print_error(char* msg)
57{
58 char* pMsgBuf;
59
60 printf(msg);
61 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
62 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
63 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&pMsgBuf,
64 0, NULL);
65 printf(pMsgBuf);
66 LocalFree(pMsgBuf);
67}
68
69int sansa_open(struct sansa_t* sansa, int silent)
70{
71 DISK_GEOMETRY_EX diskgeometry_ex;
72 DISK_GEOMETRY diskgeometry;
73 unsigned long n;
74
75 sansa->dh = CreateFile(sansa->diskname, GENERIC_READ,
76 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
77 FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
78
79 if (sansa->dh == INVALID_HANDLE_VALUE) {
80 if (!silent) print_error(" Error opening disk: ");
81 return -1;
82 }
83
84 if (!lock_volume(sansa->dh)) {
85 if (!silent) print_error(" Error locking disk: ");
86 return -1;
87 }
88
89 if (!DeviceIoControl(sansa->dh,
90 IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
91 NULL,
92 0,
93 &diskgeometry_ex,
94 sizeof(diskgeometry_ex),
95 &n,
96 NULL)) {
97 if (!DeviceIoControl(sansa->dh,
98 IOCTL_DISK_GET_DRIVE_GEOMETRY,
99 NULL,
100 0,
101 &diskgeometry,
102 sizeof(diskgeometry),
103 &n,
104 NULL)) {
105 if (!silent) print_error(" Error reading disk geometry: ");
106 return -1;
107 } else {
108 sansa->sector_size=diskgeometry.BytesPerSector;
109 }
110 } else {
111 sansa->sector_size=diskgeometry_ex.Geometry.BytesPerSector;
112 }
113
114 return 0;
115}
116
117int sansa_reopen_rw(struct sansa_t* sansa)
118{
119 /* Close existing file and re-open for writing */
120 unlock_volume(sansa->dh);
121 CloseHandle(sansa->dh);
122
123 sansa->dh = CreateFile(sansa->diskname, GENERIC_READ | GENERIC_WRITE,
124 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
125 FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
126
127 if (sansa->dh == INVALID_HANDLE_VALUE) {
128 print_error(" Error opening disk: ");
129 return -1;
130 }
131
132 if (!lock_volume(sansa->dh)) {
133 print_error(" Error locking disk: ");
134 return -1;
135 }
136
137 return 0;
138}
139
140int sansa_close(struct sansa_t* sansa)
141{
142 unlock_volume(sansa->dh);
143 CloseHandle(sansa->dh);
144 return 0;
145}
146
147int sansa_alloc_buffer(unsigned char** sectorbuf, int bufsize)
148{
149 /* The ReadFile function requires a memory buffer aligned to a multiple of
150 the disk sector size. */
151 *sectorbuf = (unsigned char*)VirtualAlloc(NULL, bufsize, MEM_COMMIT, PAGE_READWRITE);
152 if (*sectorbuf == NULL) {
153 print_error(" Error allocating a buffer: ");
154 return -1;
155 }
156 return 0;
157}
158
159int sansa_seek(struct sansa_t* sansa, loff_t pos)
160{
161 LARGE_INTEGER li;
162
163 li.QuadPart = pos;
164
165 li.LowPart = SetFilePointer (sansa->dh, li.LowPart, &li.HighPart, FILE_BEGIN);
166
167 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
168 print_error(" Seek error ");
169 return -1;
170 }
171 return 0;
172}
173
174int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes)
175{
176 unsigned long count;
177
178 if (!ReadFile(sansa->dh, buf, nbytes, &count, NULL)) {
179 print_error(" Error reading from disk: ");
180 return -1;
181 }
182
183 return count;
184}
185
186int sansa_write(struct sansa_t* sansa, unsigned char* buf, int nbytes)
187{
188 unsigned long count;
189
190 if (!WriteFile(sansa->dh, buf, nbytes, &count, NULL)) {
191 print_error(" Error writing to disk: ");
192 return -1;
193 }
194
195 return count;
196}