Updated our source code header to explicitly mention that we are GPL v2 or
[Rockbox.git] / rbutil / sansapatcher / sansaio-win32.c
blob30c378b0fc48f814575124100b146dcc80c098be
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 #ifndef RBUTIL
59 void print_error(char* msg)
61 char* pMsgBuf;
63 printf(msg);
64 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
65 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
66 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&pMsgBuf,
67 0, NULL);
68 printf(pMsgBuf);
69 LocalFree(pMsgBuf);
71 #endif
72 int sansa_open(struct sansa_t* sansa, int silent)
74 DISK_GEOMETRY_EX diskgeometry_ex;
75 DISK_GEOMETRY diskgeometry;
76 unsigned long n;
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);
82 if (sansa->dh == INVALID_HANDLE_VALUE) {
83 if (!silent) print_error(" Error opening disk: ");
84 if(GetLastError() == ERROR_ACCESS_DENIED)
85 return -2;
86 else
87 return -1;
90 if (!lock_volume(sansa->dh)) {
91 if (!silent) print_error(" Error locking disk: ");
92 return -1;
95 if (!DeviceIoControl(sansa->dh,
96 IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
97 NULL,
99 &diskgeometry_ex,
100 sizeof(diskgeometry_ex),
102 NULL)) {
103 if (!DeviceIoControl(sansa->dh,
104 IOCTL_DISK_GET_DRIVE_GEOMETRY,
105 NULL,
107 &diskgeometry,
108 sizeof(diskgeometry),
110 NULL)) {
111 if (!silent) print_error(" Error reading disk geometry: ");
112 return -1;
113 } else {
114 sansa->sector_size=diskgeometry.BytesPerSector;
116 } else {
117 sansa->sector_size=diskgeometry_ex.Geometry.BytesPerSector;
120 return 0;
123 int sansa_reopen_rw(struct sansa_t* sansa)
125 /* Close existing file and re-open for writing */
126 unlock_volume(sansa->dh);
127 CloseHandle(sansa->dh);
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);
133 if (sansa->dh == INVALID_HANDLE_VALUE) {
134 print_error(" Error opening disk: ");
135 return -1;
138 if (!lock_volume(sansa->dh)) {
139 print_error(" Error locking disk: ");
140 return -1;
143 return 0;
146 int sansa_close(struct sansa_t* sansa)
148 unlock_volume(sansa->dh);
149 CloseHandle(sansa->dh);
150 return 0;
153 int sansa_alloc_buffer(unsigned char** sectorbuf, int bufsize)
155 /* The ReadFile function requires a memory buffer aligned to a multiple of
156 the disk sector size. */
157 *sectorbuf = (unsigned char*)VirtualAlloc(NULL, bufsize, MEM_COMMIT, PAGE_READWRITE);
158 if (*sectorbuf == NULL) {
159 print_error(" Error allocating a buffer: ");
160 return -1;
162 return 0;
165 int sansa_seek(struct sansa_t* sansa, loff_t pos)
167 LARGE_INTEGER li;
169 li.QuadPart = pos;
171 li.LowPart = SetFilePointer (sansa->dh, li.LowPart, &li.HighPart, FILE_BEGIN);
173 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
174 print_error(" Seek error ");
175 return -1;
177 return 0;
180 int sansa_read(struct sansa_t* sansa, unsigned char* buf, int nbytes)
182 unsigned long count;
184 if (!ReadFile(sansa->dh, buf, nbytes, &count, NULL)) {
185 print_error(" Error reading from disk: ");
186 return -1;
189 return count;
192 int sansa_write(struct sansa_t* sansa, unsigned char* buf, int nbytes)
194 unsigned long count;
196 if (!WriteFile(sansa->dh, buf, nbytes, &count, NULL)) {
197 print_error(" Error writing to disk: ");
198 return -1;
201 return count;