1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006-2007 Dave Chapman
12 * error(), lock_volume() and unlock_volume() functions and inspiration taken
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 ****************************************************************************/
33 #include <sys/types.h>
41 static int lock_volume(HANDLE hDisk
)
45 return DeviceIoControl(hDisk
, FSCTL_LOCK_VOLUME
, NULL
, 0, NULL
, 0,
49 static int unlock_volume(HANDLE hDisk
)
53 return DeviceIoControl(hDisk
, FSCTL_UNLOCK_VOLUME
, NULL
, 0, NULL
, 0,
57 void ipod_print_error(char* msg
)
62 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
|
63 FORMAT_MESSAGE_IGNORE_INSERTS
, NULL
, GetLastError(),
64 MAKELANGID(LANG_NEUTRAL
, SUBLANG_DEFAULT
), pMsgBuf
,
70 int ipod_open(struct ipod_t
* ipod
, int silent
)
72 DISK_GEOMETRY_EX diskgeometry_ex
;
73 DISK_GEOMETRY diskgeometry
;
76 ipod
->dh
= CreateFileA(ipod
->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 (ipod
->dh
== INVALID_HANDLE_VALUE
) {
81 if (!silent
) ipod_print_error(" Error opening disk: ");
82 if(GetLastError() == ERROR_ACCESS_DENIED
)
88 if (!lock_volume(ipod
->dh
)) {
89 if (!silent
) ipod_print_error(" Error locking disk: ");
95 ipod
->sectors_per_track
= 0;
97 if (!DeviceIoControl(ipod
->dh
,
98 IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
,
102 sizeof(diskgeometry_ex
),
105 if (!DeviceIoControl(ipod
->dh
,
106 IOCTL_DISK_GET_DRIVE_GEOMETRY
,
110 sizeof(diskgeometry
),
113 if (!silent
) ipod_print_error(" Error reading disk geometry: ");
116 ipod
->sector_size
= diskgeometry
.BytesPerSector
;
117 ipod
->num_heads
= diskgeometry
.TracksPerCylinder
;
118 ipod
->sectors_per_track
= diskgeometry
.SectorsPerTrack
;
121 ipod
->sector_size
= diskgeometry_ex
.Geometry
.BytesPerSector
;
122 ipod
->num_heads
= diskgeometry_ex
.Geometry
.TracksPerCylinder
;
123 ipod
->sectors_per_track
= diskgeometry_ex
.Geometry
.SectorsPerTrack
;
129 int ipod_reopen_rw(struct ipod_t
* ipod
)
131 /* Close existing file and re-open for writing */
132 unlock_volume(ipod
->dh
);
133 CloseHandle(ipod
->dh
);
135 ipod
->dh
= CreateFileA(ipod
->diskname
, GENERIC_READ
| GENERIC_WRITE
,
136 FILE_SHARE_READ
| FILE_SHARE_WRITE
, NULL
, OPEN_EXISTING
,
137 FILE_FLAG_WRITE_THROUGH
| FILE_FLAG_NO_BUFFERING
, NULL
);
139 if (ipod
->dh
== INVALID_HANDLE_VALUE
) {
140 ipod_print_error(" Error opening disk: ");
144 if (!lock_volume(ipod
->dh
)) {
145 ipod_print_error(" Error locking disk: ");
152 int ipod_close(struct ipod_t
* ipod
)
154 unlock_volume(ipod
->dh
);
155 CloseHandle(ipod
->dh
);
159 int ipod_alloc_buffer(unsigned char** sectorbuf
, int bufsize
)
161 /* The ReadFile function requires a memory buffer aligned to a multiple of
162 the disk sector size. */
163 *sectorbuf
= (unsigned char*)VirtualAlloc(NULL
, bufsize
, MEM_COMMIT
, PAGE_READWRITE
);
164 if (*sectorbuf
== NULL
) {
165 ipod_print_error(" Error allocating a buffer: ");
171 int ipod_seek(struct ipod_t
* ipod
, unsigned long pos
)
173 if (SetFilePointer(ipod
->dh
, pos
, NULL
, FILE_BEGIN
)==0xffffffff) {
174 ipod_print_error(" Seek error ");
180 ssize_t
ipod_read(struct ipod_t
* ipod
, unsigned char* buf
, int nbytes
)
184 if (!ReadFile(ipod
->dh
, buf
, nbytes
, &count
, NULL
)) {
185 ipod_print_error(" Error reading from disk: ");
192 ssize_t
ipod_write(struct ipod_t
* ipod
, unsigned char* buf
, int nbytes
)
196 if (!WriteFile(ipod
->dh
, buf
, nbytes
, &count
, NULL
)) {
197 ipod_print_error(" Error writing to disk: ");