Musepack seek hotfix. Do not dynamically allocate seek buffer but use a buffer of...
[kugel-rb.git] / rbutil / sansapatcher / sansaio-win32.c
blob15bf82be3c2fbc6b78c0c93a6837050d5593e84e
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 * 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.
21 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
22 * KIND, either express or implied.
24 ****************************************************************************/
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
38 #include "sansaio.h"
40 static int lock_volume(HANDLE hDisk)
42 DWORD dummy;
44 return DeviceIoControl(hDisk, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0,
45 &dummy, NULL);
48 static int unlock_volume(HANDLE hDisk)
50 DWORD dummy;
52 return DeviceIoControl(hDisk, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0,
53 &dummy, NULL);
56 #ifndef RBUTIL
57 void print_error(char* msg)
59 char* pMsgBuf;
61 printf(msg);
62 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
63 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
64 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&pMsgBuf,
65 0, NULL);
66 printf(pMsgBuf);
67 LocalFree(pMsgBuf);
69 #endif
70 int sansa_open(struct sansa_t* sansa, int silent)
72 DISK_GEOMETRY_EX diskgeometry_ex;
73 DISK_GEOMETRY diskgeometry;
74 unsigned long n;
76 sansa->dh = CreateFileA(sansa->diskname, GENERIC_READ,
77 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
78 FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
80 if (sansa->dh == INVALID_HANDLE_VALUE) {
81 if (!silent) print_error(" Error opening disk: ");
82 if(GetLastError() == ERROR_ACCESS_DENIED)
83 return -2;
84 else
85 return -1;
88 if (!lock_volume(sansa->dh)) {
89 if (!silent) print_error(" Error locking disk: ");
90 return -1;
93 if (!DeviceIoControl(sansa->dh,
94 IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
95 NULL,
97 &diskgeometry_ex,
98 sizeof(diskgeometry_ex),
99 &n,
100 NULL)) {
101 if (!DeviceIoControl(sansa->dh,
102 IOCTL_DISK_GET_DRIVE_GEOMETRY,
103 NULL,
105 &diskgeometry,
106 sizeof(diskgeometry),
108 NULL)) {
109 if (!silent) print_error(" Error reading disk geometry: ");
110 return -1;
111 } else {
112 sansa->sector_size=diskgeometry.BytesPerSector;
114 } else {
115 sansa->sector_size=diskgeometry_ex.Geometry.BytesPerSector;
118 return 0;
121 int sansa_reopen_rw(struct sansa_t* sansa)
123 /* Close existing file and re-open for writing */
124 unlock_volume(sansa->dh);
125 CloseHandle(sansa->dh);
127 sansa->dh = CreateFileA(sansa->diskname, GENERIC_READ | GENERIC_WRITE,
128 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
129 FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
131 if (sansa->dh == INVALID_HANDLE_VALUE) {
132 print_error(" Error opening disk: ");
133 return -1;
136 if (!lock_volume(sansa->dh)) {
137 print_error(" Error locking disk: ");
138 return -1;
141 return 0;
144 int sansa_close(struct sansa_t* sansa)
146 unlock_volume(sansa->dh);
147 CloseHandle(sansa->dh);
148 return 0;
151 int sansa_alloc_buffer(unsigned char** sectorbuf, int bufsize)
153 /* The ReadFile function requires a memory buffer aligned to a multiple of
154 the disk sector size. */
155 *sectorbuf = (unsigned char*)VirtualAlloc(NULL, bufsize, MEM_COMMIT, PAGE_READWRITE);
156 if (*sectorbuf == NULL) {
157 print_error(" Error allocating a buffer: ");
158 return -1;
160 return 0;
163 int sansa_seek(struct sansa_t* sansa, loff_t pos)
165 LARGE_INTEGER li;
167 li.QuadPart = pos;
169 li.LowPart = SetFilePointer (sansa->dh, li.LowPart, &li.HighPart, FILE_BEGIN);
171 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
172 print_error(" Seek error ");
173 return -1;
175 return 0;
178 int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes)
180 unsigned long count;
182 if (!ReadFile(sansa->dh, buf, nbytes, &count, NULL)) {
183 print_error(" Error reading from disk: ");
184 return -1;
187 return count;
190 int sansa_write(struct sansa_t* sansa, unsigned char* buf, int nbytes)
192 unsigned long count;
194 if (!WriteFile(sansa->dh, buf, nbytes, &count, NULL)) {
195 print_error(" Error writing to disk: ");
196 return -1;
199 return count;