blob: ceec4a3d6ce6cde259177a8efe7ffb250c7068e4 [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
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
39
40#include "ipodio.h"
41
42static int lock_volume(HANDLE hDisk)
43{
44 DWORD dummy;
45
46 return DeviceIoControl(hDisk, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0,
47 &dummy, NULL);
48}
49
50static int unlock_volume(HANDLE hDisk)
51{
52 DWORD dummy;
53
54 return DeviceIoControl(hDisk, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0,
55 &dummy, NULL);
56}
57
58void print_error(char* msg)
59{
60 char* pMsgBuf;
61
62 printf(msg);
63 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
64 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
65 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&pMsgBuf,
66 0, NULL);
67 printf(pMsgBuf);
68 LocalFree(pMsgBuf);
69}
70
Dave Chapman31aa4522007-02-04 11:42:11 +000071int ipod_open(struct ipod_t* ipod, int silent)
Dave Chapman4b7e1e02006-12-13 09:02:18 +000072{
73 DISK_GEOMETRY_EX diskgeometry_ex;
74 DISK_GEOMETRY diskgeometry;
75 unsigned long n;
76
Magnus Holmgren8a7c0ee2007-11-06 19:28:14 +000077 ipod->dh = CreateFileA(ipod->diskname, GENERIC_READ,
Dave Chapman4b7e1e02006-12-13 09:02:18 +000078 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
79 FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
80
Dave Chapman31aa4522007-02-04 11:42:11 +000081 if (ipod->dh == INVALID_HANDLE_VALUE) {
Dave Chapman57b84b62006-12-17 23:00:15 +000082 if (!silent) print_error(" Error opening disk: ");
Dominik Riebeling194b2ca2008-05-04 11:59:04 +000083 if(GetLastError() == ERROR_ACCESS_DENIED)
84 return -2;
85 else
86 return -1;
Dave Chapman4b7e1e02006-12-13 09:02:18 +000087 }
88
Dave Chapman31aa4522007-02-04 11:42:11 +000089 if (!lock_volume(ipod->dh)) {
Dave Chapman57b84b62006-12-17 23:00:15 +000090 if (!silent) print_error(" Error locking disk: ");
Dave Chapman4b7e1e02006-12-13 09:02:18 +000091 return -1;
92 }
93
Dave Chapman56780e32007-06-16 22:32:57 +000094 /* Defaults */
95 ipod->num_heads = 0;
96 ipod->sectors_per_track = 0;
97
Dave Chapman31aa4522007-02-04 11:42:11 +000098 if (!DeviceIoControl(ipod->dh,
Dave Chapman4b7e1e02006-12-13 09:02:18 +000099 IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
100 NULL,
101 0,
102 &diskgeometry_ex,
103 sizeof(diskgeometry_ex),
104 &n,
105 NULL)) {
Dave Chapman31aa4522007-02-04 11:42:11 +0000106 if (!DeviceIoControl(ipod->dh,
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000107 IOCTL_DISK_GET_DRIVE_GEOMETRY,
108 NULL,
109 0,
110 &diskgeometry,
111 sizeof(diskgeometry),
112 &n,
113 NULL)) {
Dave Chapman57b84b62006-12-17 23:00:15 +0000114 if (!silent) print_error(" Error reading disk geometry: ");
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000115 return -1;
116 } else {
Dave Chapman56780e32007-06-16 22:32:57 +0000117 ipod->sector_size = diskgeometry.BytesPerSector;
118 ipod->num_heads = diskgeometry.TracksPerCylinder;
119 ipod->sectors_per_track = diskgeometry.SectorsPerTrack;
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000120 }
121 } else {
Dave Chapman56780e32007-06-16 22:32:57 +0000122 ipod->sector_size = diskgeometry_ex.Geometry.BytesPerSector;
123 ipod->num_heads = diskgeometry_ex.Geometry.TracksPerCylinder;
124 ipod->sectors_per_track = diskgeometry_ex.Geometry.SectorsPerTrack;
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000125 }
126
127 return 0;
128}
129
Dave Chapman31aa4522007-02-04 11:42:11 +0000130int ipod_reopen_rw(struct ipod_t* ipod)
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000131{
132 /* Close existing file and re-open for writing */
Dave Chapman31aa4522007-02-04 11:42:11 +0000133 unlock_volume(ipod->dh);
134 CloseHandle(ipod->dh);
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000135
Magnus Holmgren8a7c0ee2007-11-06 19:28:14 +0000136 ipod->dh = CreateFileA(ipod->diskname, GENERIC_READ | GENERIC_WRITE,
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000137 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
138 FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL);
139
Dave Chapman31aa4522007-02-04 11:42:11 +0000140 if (ipod->dh == INVALID_HANDLE_VALUE) {
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000141 print_error(" Error opening disk: ");
142 return -1;
143 }
144
Dave Chapman31aa4522007-02-04 11:42:11 +0000145 if (!lock_volume(ipod->dh)) {
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000146 print_error(" Error locking disk: ");
147 return -1;
148 }
149
150 return 0;
151}
152
Dave Chapman31aa4522007-02-04 11:42:11 +0000153int ipod_close(struct ipod_t* ipod)
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000154{
Dave Chapman31aa4522007-02-04 11:42:11 +0000155 unlock_volume(ipod->dh);
156 CloseHandle(ipod->dh);
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000157 return 0;
158}
159
160int ipod_alloc_buffer(unsigned char** sectorbuf, int bufsize)
161{
162 /* The ReadFile function requires a memory buffer aligned to a multiple of
163 the disk sector size. */
Dave Chapmane8483752006-12-14 22:17:38 +0000164 *sectorbuf = (unsigned char*)VirtualAlloc(NULL, bufsize, MEM_COMMIT, PAGE_READWRITE);
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000165 if (*sectorbuf == NULL) {
166 print_error(" Error allocating a buffer: ");
167 return -1;
168 }
169 return 0;
170}
171
Dave Chapman31aa4522007-02-04 11:42:11 +0000172int ipod_seek(struct ipod_t* ipod, unsigned long pos)
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000173{
Dave Chapman31aa4522007-02-04 11:42:11 +0000174 if (SetFilePointer(ipod->dh, pos, NULL, FILE_BEGIN)==0xffffffff) {
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000175 print_error(" Seek error ");
176 return -1;
177 }
178 return 0;
179}
180
Dave Chapman2cc80f52007-07-29 21:19:14 +0000181ssize_t ipod_read(struct ipod_t* ipod, unsigned char* buf, int nbytes)
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000182{
183 unsigned long count;
184
Dave Chapman31aa4522007-02-04 11:42:11 +0000185 if (!ReadFile(ipod->dh, buf, nbytes, &count, NULL)) {
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000186 print_error(" Error reading from disk: ");
187 return -1;
188 }
189
190 return count;
191}
192
Dave Chapman2cc80f52007-07-29 21:19:14 +0000193ssize_t ipod_write(struct ipod_t* ipod, unsigned char* buf, int nbytes)
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000194{
195 unsigned long count;
196
Dave Chapman31aa4522007-02-04 11:42:11 +0000197 if (!WriteFile(ipod->dh, buf, nbytes, &count, NULL)) {
Dave Chapman4b7e1e02006-12-13 09:02:18 +0000198 print_error(" Error writing to disk: ");
199 return -1;
200 }
201
202 return count;
203}