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