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 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame] | 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. |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 22 | * |
| 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> |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 35 | #include <windows.h> |
Dave Chapman | 1eca02d | 2009-08-04 20:32:30 +0000 | [diff] [blame] | 36 | #include <stddef.h> |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 37 | #include <winioctl.h> |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 38 | |
| 39 | #include "ipodio.h" |
| 40 | |
| 41 | static int lock_volume(HANDLE hDisk) |
| 42 | { |
| 43 | DWORD dummy; |
| 44 | |
| 45 | return DeviceIoControl(hDisk, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, |
| 46 | &dummy, NULL); |
| 47 | } |
| 48 | |
| 49 | static int unlock_volume(HANDLE hDisk) |
| 50 | { |
| 51 | DWORD dummy; |
| 52 | |
| 53 | return DeviceIoControl(hDisk, FSCTL_UNLOCK_VOLUME, NULL, 0, NULL, 0, |
| 54 | &dummy, NULL); |
| 55 | } |
| 56 | |
Dominik Riebeling | 3e489c1 | 2009-11-08 13:38:10 +0000 | [diff] [blame] | 57 | void ipod_print_error(char* msg) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 58 | { |
Dominik Riebeling | 8b32a2d | 2009-09-20 17:03:58 +0000 | [diff] [blame] | 59 | LPSTR pMsgBuf = NULL; |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 60 | |
| 61 | printf(msg); |
Dominik Riebeling | 8b32a2d | 2009-09-20 17:03:58 +0000 | [diff] [blame] | 62 | FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 63 | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), |
Dominik Riebeling | 8b32a2d | 2009-09-20 17:03:58 +0000 | [diff] [blame] | 64 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), pMsgBuf, |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 65 | 0, NULL); |
| 66 | printf(pMsgBuf); |
| 67 | LocalFree(pMsgBuf); |
| 68 | } |
| 69 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 70 | int ipod_open(struct ipod_t* ipod, int silent) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 71 | { |
| 72 | DISK_GEOMETRY_EX diskgeometry_ex; |
| 73 | DISK_GEOMETRY diskgeometry; |
| 74 | unsigned long n; |
| 75 | |
Magnus Holmgren | 8a7c0ee | 2007-11-06 19:28:14 +0000 | [diff] [blame] | 76 | ipod->dh = CreateFileA(ipod->diskname, GENERIC_READ, |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 77 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, |
| 78 | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL); |
| 79 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 80 | if (ipod->dh == INVALID_HANDLE_VALUE) { |
Dominik Riebeling | 3e489c1 | 2009-11-08 13:38:10 +0000 | [diff] [blame] | 81 | if (!silent) ipod_print_error(" Error opening disk: "); |
Dominik Riebeling | 194b2ca | 2008-05-04 11:59:04 +0000 | [diff] [blame] | 82 | if(GetLastError() == ERROR_ACCESS_DENIED) |
| 83 | return -2; |
| 84 | else |
| 85 | return -1; |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 88 | if (!lock_volume(ipod->dh)) { |
Dominik Riebeling | 3e489c1 | 2009-11-08 13:38:10 +0000 | [diff] [blame] | 89 | if (!silent) ipod_print_error(" Error locking disk: "); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 90 | return -1; |
| 91 | } |
| 92 | |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 93 | /* Defaults */ |
| 94 | ipod->num_heads = 0; |
| 95 | ipod->sectors_per_track = 0; |
| 96 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 97 | if (!DeviceIoControl(ipod->dh, |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 98 | IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, |
| 99 | NULL, |
| 100 | 0, |
| 101 | &diskgeometry_ex, |
| 102 | sizeof(diskgeometry_ex), |
| 103 | &n, |
| 104 | NULL)) { |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 105 | if (!DeviceIoControl(ipod->dh, |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 106 | IOCTL_DISK_GET_DRIVE_GEOMETRY, |
| 107 | NULL, |
| 108 | 0, |
| 109 | &diskgeometry, |
| 110 | sizeof(diskgeometry), |
| 111 | &n, |
| 112 | NULL)) { |
Dominik Riebeling | 3e489c1 | 2009-11-08 13:38:10 +0000 | [diff] [blame] | 113 | if (!silent) ipod_print_error(" Error reading disk geometry: "); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 114 | return -1; |
| 115 | } else { |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 116 | ipod->sector_size = diskgeometry.BytesPerSector; |
| 117 | ipod->num_heads = diskgeometry.TracksPerCylinder; |
| 118 | ipod->sectors_per_track = diskgeometry.SectorsPerTrack; |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 119 | } |
| 120 | } else { |
Dave Chapman | 56780e3 | 2007-06-16 22:32:57 +0000 | [diff] [blame] | 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; |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 129 | int ipod_reopen_rw(struct ipod_t* ipod) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 130 | { |
| 131 | /* Close existing file and re-open for writing */ |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 132 | unlock_volume(ipod->dh); |
| 133 | CloseHandle(ipod->dh); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 134 | |
Magnus Holmgren | 8a7c0ee | 2007-11-06 19:28:14 +0000 | [diff] [blame] | 135 | ipod->dh = CreateFileA(ipod->diskname, GENERIC_READ | GENERIC_WRITE, |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 136 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, |
| 137 | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL); |
| 138 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 139 | if (ipod->dh == INVALID_HANDLE_VALUE) { |
Dominik Riebeling | 3e489c1 | 2009-11-08 13:38:10 +0000 | [diff] [blame] | 140 | ipod_print_error(" Error opening disk: "); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 141 | return -1; |
| 142 | } |
| 143 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 144 | if (!lock_volume(ipod->dh)) { |
Dominik Riebeling | 3e489c1 | 2009-11-08 13:38:10 +0000 | [diff] [blame] | 145 | ipod_print_error(" Error locking disk: "); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 152 | int ipod_close(struct ipod_t* ipod) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 153 | { |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 154 | unlock_volume(ipod->dh); |
| 155 | CloseHandle(ipod->dh); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | int ipod_alloc_buffer(unsigned char** sectorbuf, int bufsize) |
| 160 | { |
| 161 | /* The ReadFile function requires a memory buffer aligned to a multiple of |
| 162 | the disk sector size. */ |
Dave Chapman | e848375 | 2006-12-14 22:17:38 +0000 | [diff] [blame] | 163 | *sectorbuf = (unsigned char*)VirtualAlloc(NULL, bufsize, MEM_COMMIT, PAGE_READWRITE); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 164 | if (*sectorbuf == NULL) { |
Dominik Riebeling | 3e489c1 | 2009-11-08 13:38:10 +0000 | [diff] [blame] | 165 | ipod_print_error(" Error allocating a buffer: "); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 166 | return -1; |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 171 | int ipod_seek(struct ipod_t* ipod, unsigned long pos) |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 172 | { |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 173 | if (SetFilePointer(ipod->dh, pos, NULL, FILE_BEGIN)==0xffffffff) { |
Dominik Riebeling | 3e489c1 | 2009-11-08 13:38:10 +0000 | [diff] [blame] | 174 | ipod_print_error(" Seek error "); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 175 | return -1; |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | |
Dave Chapman | 2cc80f5 | 2007-07-29 21:19:14 +0000 | [diff] [blame] | 180 | 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] | 181 | { |
| 182 | unsigned long count; |
| 183 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 184 | if (!ReadFile(ipod->dh, buf, nbytes, &count, NULL)) { |
Dominik Riebeling | 3e489c1 | 2009-11-08 13:38:10 +0000 | [diff] [blame] | 185 | ipod_print_error(" Error reading from disk: "); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | return count; |
| 190 | } |
| 191 | |
Dave Chapman | 2cc80f5 | 2007-07-29 21:19:14 +0000 | [diff] [blame] | 192 | 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] | 193 | { |
| 194 | unsigned long count; |
| 195 | |
Dave Chapman | 31aa452 | 2007-02-04 11:42:11 +0000 | [diff] [blame] | 196 | if (!WriteFile(ipod->dh, buf, nbytes, &count, NULL)) { |
Dominik Riebeling | 3e489c1 | 2009-11-08 13:38:10 +0000 | [diff] [blame] | 197 | ipod_print_error(" Error writing to disk: "); |
Dave Chapman | 4b7e1e0 | 2006-12-13 09:02:18 +0000 | [diff] [blame] | 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | return count; |
| 202 | } |
Dave Chapman | 1eca02d | 2009-08-04 20:32:30 +0000 | [diff] [blame] | 203 | |