Use DBOP to check for left button on C200v2 like we are supposed to instead of right...
[kugel-rb.git] / rbutil / sansapatcher / sansaio-win32.c
blob5797eae2aa9b87a6ffabcc2879806fd35237af20
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006-2007 Dave Chapman
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/
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.
23 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
24 * KIND, either express or implied.
26 ****************************************************************************/
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #ifdef __WIN32__
36 #include <windows.h>
37 #include <winioctl.h>
38 #endif
40 #include "sansaio.h"
42 static int lock_volume(HANDLE hDisk)
44 DWORD dummy;
46 return DeviceIoControl(hDisk, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0,
47 &dummy, NULL);
50 static int unlock_volume(HANDLE hDisk)
52 DWORD dummy;
54 return DeviceIoControl(hDisk, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0,
55 &dummy, NULL);
58 void sansa_print_error(char* msg)
60 LPSTR pMsgBuf = NULL;
62 printf(msg);
63 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
64 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
65 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), pMsgBuf,
66 0, NULL);
67 printf(pMsgBuf);
68 LocalFree(pMsgBuf);
71 int sansa_open(struct sansa_t* sansa, int silent)
73 DISK_GEOMETRY_EX diskgeometry_ex;
74 DISK_GEOMETRY diskgeometry;
75 unsigned long n;
77 sansa->dh = CreateFileA(sansa->diskname, GENERIC_READ,
78 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
79 FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
81 if (sansa->dh == INVALID_HANDLE_VALUE) {
82 if (!silent) sansa_print_error(" Error opening disk: ");
83 if(GetLastError() == ERROR_ACCESS_DENIED)
84 return -2;
85 else
86 return -1;
89 if (!lock_volume(sansa->dh)) {
90 if (!silent) sansa_print_error(" Error locking disk: ");
91 return -1;
94 if (!DeviceIoControl(sansa->dh,
95 IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
96 NULL,
98 &diskgeometry_ex,
99 sizeof(diskgeometry_ex),
101 NULL)) {
102 if (!DeviceIoControl(sansa->dh,
103 IOCTL_DISK_GET_DRIVE_GEOMETRY,
104 NULL,
106 &diskgeometry,
107 sizeof(diskgeometry),
109 NULL)) {
110 if (!silent) sansa_print_error(" Error reading disk geometry: ");
111 return -1;
112 } else {
113 sansa->sector_size=diskgeometry.BytesPerSector;
115 } else {
116 sansa->sector_size=diskgeometry_ex.Geometry.BytesPerSector;
119 return 0;
122 int sansa_reopen_rw(struct sansa_t* sansa)
124 /* Close existing file and re-open for writing */
125 unlock_volume(sansa->dh);
126 CloseHandle(sansa->dh);
128 sansa->dh = CreateFileA(sansa->diskname, GENERIC_READ | GENERIC_WRITE,
129 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
130 FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
132 if (sansa->dh == INVALID_HANDLE_VALUE) {
133 sansa_print_error(" Error opening disk: ");
134 return -1;
137 if (!lock_volume(sansa->dh)) {
138 sansa_print_error(" Error locking disk: ");
139 return -1;
142 return 0;
145 int sansa_close(struct sansa_t* sansa)
147 unlock_volume(sansa->dh);
148 CloseHandle(sansa->dh);
149 return 0;
152 int sansa_alloc_buffer(unsigned char** sectorbuf, int bufsize)
154 /* The ReadFile function requires a memory buffer aligned to a multiple of
155 the disk sector size. */
156 *sectorbuf = (unsigned char*)VirtualAlloc(NULL, bufsize, MEM_COMMIT, PAGE_READWRITE);
157 if (*sectorbuf == NULL) {
158 sansa_print_error(" Error allocating a buffer: ");
159 return -1;
161 return 0;
164 int sansa_seek(struct sansa_t* sansa, loff_t pos)
166 LARGE_INTEGER li;
168 li.QuadPart = pos;
170 li.LowPart = SetFilePointer (sansa->dh, li.LowPart, &li.HighPart, FILE_BEGIN);
172 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
173 sansa_print_error(" Seek error ");
174 return -1;
176 return 0;
179 int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes)
181 unsigned long count;
183 if (!ReadFile(sansa->dh, buf, nbytes, &count, NULL)) {
184 sansa_print_error(" Error reading from disk: ");
185 return -1;
188 return count;
191 int sansa_write(struct sansa_t* sansa, unsigned char* buf, int nbytes)
193 unsigned long count;
195 if (!WriteFile(sansa->dh, buf, nbytes, &count, NULL)) {
196 sansa_print_error(" Error writing to disk: ");
197 return -1;
200 return count;