summaryrefslogtreecommitdiff
path: root/utils/sansapatcher/sansaio-win32.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/sansapatcher/sansaio-win32.c')
-rw-r--r--utils/sansapatcher/sansaio-win32.c217
1 files changed, 217 insertions, 0 deletions
diff --git a/utils/sansapatcher/sansaio-win32.c b/utils/sansapatcher/sansaio-win32.c
new file mode 100644
index 0000000000..ee6a8cd93d
--- /dev/null
+++ b/utils/sansapatcher/sansaio-win32.c
@@ -0,0 +1,217 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version 2
21 * of the License, or (at your option) any later version.
22 *
23 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
24 * KIND, either express or implied.
25 *
26 ****************************************************************************/
27
28#if defined(_WIN32)
29#include <stdio.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <string.h>
33#include <stdlib.h>
34#include <sys/types.h>
35#include <sys/stat.h>
36#ifdef __WIN32__
37#include <windows.h>
38#include <winioctl.h>
39#endif
40
41#include "sansaio.h"
42
43static int lock_volume(HANDLE hDisk)
44{
45 DWORD dummy;
46
47 return DeviceIoControl(hDisk, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0,
48 &dummy, NULL);
49}
50
51static int unlock_volume(HANDLE hDisk)
52{
53 DWORD dummy;
54
55 return DeviceIoControl(hDisk, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0,
56 &dummy, NULL);
57}
58
59void sansa_print_error(char* msg)
60{
61 LPSTR pMsgBuf = NULL;
62
63 printf(msg);
64 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
65 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
66 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), pMsgBuf,
67 0, NULL);
68 printf(pMsgBuf);
69 LocalFree(pMsgBuf);
70}
71
72int sansa_open(struct sansa_t* sansa, int silent)
73{
74 DISK_GEOMETRY_EX diskgeometry_ex;
75 DISK_GEOMETRY diskgeometry;
76 unsigned long n;
77
78 sansa->dh = CreateFileA(sansa->diskname, GENERIC_READ,
79 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
80 FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
81
82 if (sansa->dh == INVALID_HANDLE_VALUE) {
83 if (!silent) sansa_print_error(" Error opening disk: ");
84 if(GetLastError() == ERROR_ACCESS_DENIED)
85 return -2;
86 else
87 return -1;
88 }
89
90 if (!lock_volume(sansa->dh)) {
91 if (!silent) sansa_print_error(" Error locking disk: ");
92 return -1;
93 }
94
95 if (!DeviceIoControl(sansa->dh,
96 IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
97 NULL,
98 0,
99 &diskgeometry_ex,
100 sizeof(diskgeometry_ex),
101 &n,
102 NULL)) {
103 if (!DeviceIoControl(sansa->dh,
104 IOCTL_DISK_GET_DRIVE_GEOMETRY,
105 NULL,
106 0,
107 &diskgeometry,
108 sizeof(diskgeometry),
109 &n,
110 NULL)) {
111 if (!silent) sansa_print_error(" Error reading disk geometry: ");
112 return -1;
113 } else {
114 sansa->sector_size=diskgeometry.BytesPerSector;
115 }
116 } else {
117 sansa->sector_size=diskgeometry_ex.Geometry.BytesPerSector;
118 }
119
120 return 0;
121}
122
123int sansa_reopen_rw(struct sansa_t* sansa)
124{
125 /* Close existing file and re-open for writing */
126 unlock_volume(sansa->dh);
127 CloseHandle(sansa->dh);
128
129 sansa->dh = CreateFileA(sansa->diskname, GENERIC_READ | GENERIC_WRITE,
130 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
131 FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
132
133 if (sansa->dh == INVALID_HANDLE_VALUE) {
134 sansa_print_error(" Error opening disk: ");
135 return -1;
136 }
137
138 if (!lock_volume(sansa->dh)) {
139 sansa_print_error(" Error locking disk: ");
140 return -1;
141 }
142
143 return 0;
144}
145
146int sansa_close(struct sansa_t* sansa)
147{
148 unlock_volume(sansa->dh);
149 CloseHandle(sansa->dh);
150 return 0;
151}
152
153int sansa_alloc_buffer(struct sansa_t* sansa, int bufsize)
154{
155 /* The ReadFile function requires a memory buffer aligned to a multiple of
156 the disk sector size. */
157 sansa->sectorbuf = (unsigned char*)VirtualAlloc(NULL, bufsize, MEM_COMMIT, PAGE_READWRITE);
158 if (sansa->sectorbuf == NULL) {
159 sansa_print_error(" Error allocating a buffer: ");
160 return -1;
161 }
162 return 0;
163}
164
165int sansa_dealloc_buffer(struct sansa_t* sansa)
166{
167 if (sansa->sectorbuf == NULL) {
168 return -1;
169 }
170 if(!VirtualFree(sansa->sectorbuf, 0, MEM_RELEASE)) {
171 sansa_print_error(" Error releasing buffer ");
172 return -1;
173 }
174 sansa->sectorbuf = NULL;
175 return 0;
176}
177
178int sansa_seek(struct sansa_t* sansa, loff_t pos)
179{
180 LARGE_INTEGER li;
181
182 li.QuadPart = pos;
183
184 li.LowPart = SetFilePointer (sansa->dh, li.LowPart, &li.HighPart, FILE_BEGIN);
185
186 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
187 sansa_print_error(" Seek error ");
188 return -1;
189 }
190 return 0;
191}
192
193int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes)
194{
195 unsigned long count;
196
197 if (!ReadFile(sansa->dh, buf, nbytes, &count, NULL)) {
198 sansa_print_error(" Error reading from disk: ");
199 return -1;
200 }
201
202 return count;
203}
204
205int sansa_write(struct sansa_t* sansa, int nbytes)
206{
207 unsigned long count;
208
209 if (!WriteFile(sansa->dh, sansa->sectorbuf, nbytes, &count, NULL)) {
210 sansa_print_error(" Error writing to disk: ");
211 return -1;
212 }
213
214 return count;
215}
216#endif
217