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