blob: cea218774a4b5575bc6b032852a46f9b67aecd94 [file] [log] [blame]
Dave Chapman4b7e1e02006-12-13 09:02:18 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
Dave Chapmane332f4c2007-02-05 01:20:20 +000010 * Copyright (C) 2006-2007 Dave Chapman
Dave Chapman4b7e1e02006-12-13 09:02:18 +000011 *
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/
17 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000018 * 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.
Dave Chapman4b7e1e02006-12-13 09:02:18 +000022 *
23 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
24 * KIND, either express or implied.
25 *
26 ****************************************************************************/
27
Dominik Riebelinge23adb52011-12-07 20:06:48 +000028#if defined(_WIN32)
29
Dave Chapman4b7e1e02006-12-13 09:02:18 +000030#include <stdio.h>
31#include <unistd.h>
32#include <fcntl.h>
33#include <string.h>
34#include <stdlib.h>
35#include <sys/types.h>
36#include <sys/stat.h>
Dave Chapman4b7e1e02006-12-13 09:02:18 +000037#include <windows.h>
Dave Chapman1eca02d2009-08-04 20:32:30 +000038#include <stddef.h>
Dave Chapman4b7e1e02006-12-13 09:02:18 +000039#include <winioctl.h>
Dave Chapman4b7e1e02006-12-13 09:02:18 +000040
41#include "ipodio.h"
42
43static int lock_volume(HANDLE hDisk)
44{
45 DWORD dummy;
46
47 return DeviceIoControl(hDisk, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0,
48 &dummy, NULL);
49}
50
51static int unlock_volume(HANDLE hDisk)
52{
53 DWORD dummy;
54
55 return DeviceIoControl(hDisk, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0,
56 &dummy, NULL);
57}
58
Dominik Riebeling3e489c12009-11-08 13:38:10 +000059void ipod_print_error(char* msg)
Dave Chapman4b7e1e02006-12-13 09:02:18 +000060{
Dominik Riebeling8b32a2d2009-09-20 17:03:58 +000061 LPSTR pMsgBuf = NULL;
Dave Chapman4b7e1e02006-12-13 09:02:18 +000062
63 printf(msg);
Dominik Riebeling8b32a2d2009-09-20 17:03:58 +000064 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
Dave Chapman4b7e1e02006-12-13 09:02:18 +000065 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
Dominik Riebeling8b32a2d2009-09-20 17:03:58 +000066 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), pMsgBuf,
Dave Chapman4b7e1e02006-12-13 09:02:18 +000067 0, NULL);
68 printf(pMsgBuf);
69 LocalFree(pMsgBuf);
70}
71
Dave Chapman31aa4522007-02-04 11:42:11 +000072int ipod_open(struct ipod_t* ipod, int silent)
Dave Chapman4b7e1e02006-12-13 09:02:18 +000073{
74 DISK_GEOMETRY_EX diskgeometry_ex;
75 DISK_GEOMETRY diskgeometry;
76 unsigned long n;
77
Magnus Holmgren8a7c0ee2007-11-06 19:28:14 +000078 ipod->dh = CreateFileA(ipod->diskname, GENERIC_READ,
Dave Chapman4b7e1e02006-12-13 09:02:18 +000079 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
80 FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
81
Dave Chapman31aa4522007-02-04 11:42:11 +000082 if (ipod->dh == INVALID_HANDLE_VALUE) {
Dominik Riebeling3e489c12009-11-08 13:38:10 +000083 if (!silent) ipod_print_error(" Error opening disk: ");
Dominik Riebeling194b2ca2008-05-04 11:59:04 +000084 if(GetLastError() == ERROR_ACCESS_DENIED)
85 return -2;
86 else
87 return -1;
Dave Chapman4b7e1e02006-12-13 09:02:18 +000088 }
89
Dave Chapman31aa4522007-02-04 11:42:11 +000090 if (!lock_volume(ipod->dh)) {
Dominik Riebeling3e489c12009-11-08 13:38:10 +000091 if (!silent) ipod_print_error(" Error locking disk: ");
Dave Chapman4b7e1e02006-12-13 09:02:18 +000092 return -1;
93 }
94
Dave Chapman56780e32007-06-16 22:32:57 +000095 /* Defaults */
96 ipod->num_heads = 0;
97 ipod->sectors_per_track = 0;
98
Dave Chapman31aa4522007-02-04 11:42:11 +000099 if (!DeviceIoControl(ipod->dh,
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000100 IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
101 NULL,
102 0,
103 &diskgeometry_ex,
104 sizeof(diskgeometry_ex),
105 &n,
106 NULL)) {
Dave Chapman31aa4522007-02-04 11:42:11 +0000107 if (!DeviceIoControl(ipod->dh,
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000108 IOCTL_DISK_GET_DRIVE_GEOMETRY,
109 NULL,
110 0,
111 &diskgeometry,
112 sizeof(diskgeometry),
113 &n,
114 NULL)) {
Dominik Riebeling3e489c12009-11-08 13:38:10 +0000115 if (!silent) ipod_print_error(" Error reading disk geometry: ");
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000116 return -1;
117 } else {
Dave Chapman56780e32007-06-16 22:32:57 +0000118 ipod->sector_size = diskgeometry.BytesPerSector;
119 ipod->num_heads = diskgeometry.TracksPerCylinder;
120 ipod->sectors_per_track = diskgeometry.SectorsPerTrack;
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000121 }
122 } else {
Dave Chapman56780e32007-06-16 22:32:57 +0000123 ipod->sector_size = diskgeometry_ex.Geometry.BytesPerSector;
124 ipod->num_heads = diskgeometry_ex.Geometry.TracksPerCylinder;
125 ipod->sectors_per_track = diskgeometry_ex.Geometry.SectorsPerTrack;
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000126 }
127
128 return 0;
129}
130
Dave Chapman31aa4522007-02-04 11:42:11 +0000131int ipod_reopen_rw(struct ipod_t* ipod)
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000132{
133 /* Close existing file and re-open for writing */
Dave Chapman31aa4522007-02-04 11:42:11 +0000134 unlock_volume(ipod->dh);
135 CloseHandle(ipod->dh);
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000136
Magnus Holmgren8a7c0ee2007-11-06 19:28:14 +0000137 ipod->dh = CreateFileA(ipod->diskname, GENERIC_READ | GENERIC_WRITE,
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000138 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
139 FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
140
Dave Chapman31aa4522007-02-04 11:42:11 +0000141 if (ipod->dh == INVALID_HANDLE_VALUE) {
Dominik Riebeling3e489c12009-11-08 13:38:10 +0000142 ipod_print_error(" Error opening disk: ");
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000143 return -1;
144 }
145
Dave Chapman31aa4522007-02-04 11:42:11 +0000146 if (!lock_volume(ipod->dh)) {
Dominik Riebeling3e489c12009-11-08 13:38:10 +0000147 ipod_print_error(" Error locking disk: ");
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000148 return -1;
149 }
150
151 return 0;
152}
153
Dave Chapman31aa4522007-02-04 11:42:11 +0000154int ipod_close(struct ipod_t* ipod)
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000155{
Dave Chapman31aa4522007-02-04 11:42:11 +0000156 unlock_volume(ipod->dh);
157 CloseHandle(ipod->dh);
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000158 return 0;
159}
160
Dominik Riebeling24e37dd2012-12-23 23:30:57 +0100161int ipod_alloc_buffer(struct ipod_t* ipod, int bufsize)
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000162{
163 /* The ReadFile function requires a memory buffer aligned to a multiple of
164 the disk sector size. */
Dominik Riebeling24e37dd2012-12-23 23:30:57 +0100165 ipod->sectorbuf = (unsigned char*)VirtualAlloc(NULL, bufsize, MEM_COMMIT, PAGE_READWRITE);
166 if (ipod->sectorbuf== NULL) {
Dominik Riebeling3e489c12009-11-08 13:38:10 +0000167 ipod_print_error(" Error allocating a buffer: ");
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000168 return -1;
169 }
170 return 0;
171}
172
Dominik Riebelingb63d4292013-01-01 11:04:21 +0100173int ipod_dealloc_buffer(struct ipod_t* ipod)
174{
175 if (ipod->sectorbuf == NULL) {
176 return -1;
177 }
178 if(!VirtualFree(ipod->sectorbuf, 0, MEM_RELEASE)) {
179 ipod_print_error(" Error releasing buffer ");
180 return -1;
181 }
182 ipod->sectorbuf = NULL;
183 return 0;
184}
185
Dave Chapman31aa4522007-02-04 11:42:11 +0000186int ipod_seek(struct ipod_t* ipod, unsigned long pos)
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000187{
Dave Chapman31aa4522007-02-04 11:42:11 +0000188 if (SetFilePointer(ipod->dh, pos, NULL, FILE_BEGIN)==0xffffffff) {
Dominik Riebeling3e489c12009-11-08 13:38:10 +0000189 ipod_print_error(" Seek error ");
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000190 return -1;
191 }
192 return 0;
193}
194
Dominik Riebeling24e37dd2012-12-23 23:30:57 +0100195ssize_t ipod_read(struct ipod_t* ipod, int nbytes)
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000196{
197 unsigned long count;
198
Dominik Riebeling24e37dd2012-12-23 23:30:57 +0100199 if(ipod->sectorbuf == NULL) {
200 return -1;
201 }
202 if (!ReadFile(ipod->dh, ipod->sectorbuf, nbytes, &count, NULL)) {
Dominik Riebeling3e489c12009-11-08 13:38:10 +0000203 ipod_print_error(" Error reading from disk: ");
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000204 return -1;
205 }
206
207 return count;
208}
209
Dominik Riebeling24e37dd2012-12-23 23:30:57 +0100210ssize_t ipod_write(struct ipod_t* ipod, int nbytes)
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000211{
212 unsigned long count;
213
Dominik Riebeling24e37dd2012-12-23 23:30:57 +0100214 if(ipod->sectorbuf == NULL) {
215 return -1;
216 }
217 if (!WriteFile(ipod->dh, ipod->sectorbuf, nbytes, &count, NULL)) {
Dominik Riebeling3e489c12009-11-08 13:38:10 +0000218 ipod_print_error(" Error writing to disk: ");
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000219 return -1;
220 }
221
222 return count;
223}
Dave Chapman1eca02d2009-08-04 20:32:30 +0000224
Dominik Riebelinge23adb52011-12-07 20:06:48 +0000225#endif
226