diff options
author | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2021-12-15 21:04:28 +0100 |
---|---|---|
committer | Dominik Riebeling <Dominik.Riebeling@gmail.com> | 2021-12-24 18:05:53 +0100 |
commit | c876d3bbefe0dc00c27ca0c12d29da5874946962 (patch) | |
tree | 69f468a185a369b01998314bc3ecc19b70f4fcaa /utils/ipodpatcher/ipodio-win32.c | |
parent | 6c6f0757d7a902feb293be165d1490c42bc8e7ad (diff) | |
download | rockbox-c876d3bbefe0dc00c27ca0c12d29da5874946962.tar.gz rockbox-c876d3bbefe0dc00c27ca0c12d29da5874946962.zip |
rbutil: Merge rbutil with utils folder.
rbutil uses several components from the utils folder, and can be
considered part of utils too. Having it in a separate folder is an
arbitrary split that doesn't help anymore these days, so merge them.
This also allows other utils to easily use libtools.make without the
need to navigate to a different folder.
Change-Id: I3fc2f4de19e3e776553efb5dea5f779dfec0dc21
Diffstat (limited to 'utils/ipodpatcher/ipodio-win32.c')
-rw-r--r-- | utils/ipodpatcher/ipodio-win32.c | 226 |
1 files changed, 226 insertions, 0 deletions
diff --git a/utils/ipodpatcher/ipodio-win32.c b/utils/ipodpatcher/ipodio-win32.c new file mode 100644 index 0000000000..cea218774a --- /dev/null +++ b/utils/ipodpatcher/ipodio-win32.c | |||
@@ -0,0 +1,226 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2006-2007 Dave Chapman | ||
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 | * 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. | ||
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 | #if defined(_WIN32) | ||
29 | |||
30 | #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> | ||
37 | #include <windows.h> | ||
38 | #include <stddef.h> | ||
39 | #include <winioctl.h> | ||
40 | |||
41 | #include "ipodio.h" | ||
42 | |||
43 | static 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 | |||
51 | static 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 | |||
59 | void ipod_print_error(char* msg) | ||
60 | { | ||
61 | LPSTR pMsgBuf = NULL; | ||
62 | |||
63 | printf(msg); | ||
64 | FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | | ||
65 | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), | ||
66 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), pMsgBuf, | ||
67 | 0, NULL); | ||
68 | printf(pMsgBuf); | ||
69 | LocalFree(pMsgBuf); | ||
70 | } | ||
71 | |||
72 | int ipod_open(struct ipod_t* ipod, int silent) | ||
73 | { | ||
74 | DISK_GEOMETRY_EX diskgeometry_ex; | ||
75 | DISK_GEOMETRY diskgeometry; | ||
76 | unsigned long n; | ||
77 | |||
78 | ipod->dh = CreateFileA(ipod->diskname, GENERIC_READ, | ||
79 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, | ||
80 | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL); | ||
81 | |||
82 | if (ipod->dh == INVALID_HANDLE_VALUE) { | ||
83 | if (!silent) ipod_print_error(" Error opening disk: "); | ||
84 | if(GetLastError() == ERROR_ACCESS_DENIED) | ||
85 | return -2; | ||
86 | else | ||
87 | return -1; | ||
88 | } | ||
89 | |||
90 | if (!lock_volume(ipod->dh)) { | ||
91 | if (!silent) ipod_print_error(" Error locking disk: "); | ||
92 | return -1; | ||
93 | } | ||
94 | |||
95 | /* Defaults */ | ||
96 | ipod->num_heads = 0; | ||
97 | ipod->sectors_per_track = 0; | ||
98 | |||
99 | if (!DeviceIoControl(ipod->dh, | ||
100 | IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, | ||
101 | NULL, | ||
102 | 0, | ||
103 | &diskgeometry_ex, | ||
104 | sizeof(diskgeometry_ex), | ||
105 | &n, | ||
106 | NULL)) { | ||
107 | if (!DeviceIoControl(ipod->dh, | ||
108 | IOCTL_DISK_GET_DRIVE_GEOMETRY, | ||
109 | NULL, | ||
110 | 0, | ||
111 | &diskgeometry, | ||
112 | sizeof(diskgeometry), | ||
113 | &n, | ||
114 | NULL)) { | ||
115 | if (!silent) ipod_print_error(" Error reading disk geometry: "); | ||
116 | return -1; | ||
117 | } else { | ||
118 | ipod->sector_size = diskgeometry.BytesPerSector; | ||
119 | ipod->num_heads = diskgeometry.TracksPerCylinder; | ||
120 | ipod->sectors_per_track = diskgeometry.SectorsPerTrack; | ||
121 | } | ||
122 | } else { | ||
123 | ipod->sector_size = diskgeometry_ex.Geometry.BytesPerSector; | ||
124 | ipod->num_heads = diskgeometry_ex.Geometry.TracksPerCylinder; | ||
125 | ipod->sectors_per_track = diskgeometry_ex.Geometry.SectorsPerTrack; | ||
126 | } | ||
127 | |||
128 | return 0; | ||
129 | } | ||
130 | |||
131 | int ipod_reopen_rw(struct ipod_t* ipod) | ||
132 | { | ||
133 | /* Close existing file and re-open for writing */ | ||
134 | unlock_volume(ipod->dh); | ||
135 | CloseHandle(ipod->dh); | ||
136 | |||
137 | ipod->dh = CreateFileA(ipod->diskname, GENERIC_READ | GENERIC_WRITE, | ||
138 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, | ||
139 | FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING, NULL); | ||
140 | |||
141 | if (ipod->dh == INVALID_HANDLE_VALUE) { | ||
142 | ipod_print_error(" Error opening disk: "); | ||
143 | return -1; | ||
144 | } | ||
145 | |||
146 | if (!lock_volume(ipod->dh)) { | ||
147 | ipod_print_error(" Error locking disk: "); | ||
148 | return -1; | ||
149 | } | ||
150 | |||
151 | return 0; | ||
152 | } | ||
153 | |||
154 | int ipod_close(struct ipod_t* ipod) | ||
155 | { | ||
156 | unlock_volume(ipod->dh); | ||
157 | CloseHandle(ipod->dh); | ||
158 | return 0; | ||
159 | } | ||
160 | |||
161 | int ipod_alloc_buffer(struct ipod_t* ipod, int bufsize) | ||
162 | { | ||
163 | /* The ReadFile function requires a memory buffer aligned to a multiple of | ||
164 | the disk sector size. */ | ||
165 | ipod->sectorbuf = (unsigned char*)VirtualAlloc(NULL, bufsize, MEM_COMMIT, PAGE_READWRITE); | ||
166 | if (ipod->sectorbuf== NULL) { | ||
167 | ipod_print_error(" Error allocating a buffer: "); | ||
168 | return -1; | ||
169 | } | ||
170 | return 0; | ||
171 | } | ||
172 | |||
173 | int 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 | |||
186 | int ipod_seek(struct ipod_t* ipod, unsigned long pos) | ||
187 | { | ||
188 | if (SetFilePointer(ipod->dh, pos, NULL, FILE_BEGIN)==0xffffffff) { | ||
189 | ipod_print_error(" Seek error "); | ||
190 | return -1; | ||
191 | } | ||
192 | return 0; | ||
193 | } | ||
194 | |||
195 | ssize_t ipod_read(struct ipod_t* ipod, int nbytes) | ||
196 | { | ||
197 | unsigned long count; | ||
198 | |||
199 | if(ipod->sectorbuf == NULL) { | ||
200 | return -1; | ||
201 | } | ||
202 | if (!ReadFile(ipod->dh, ipod->sectorbuf, nbytes, &count, NULL)) { | ||
203 | ipod_print_error(" Error reading from disk: "); | ||
204 | return -1; | ||
205 | } | ||
206 | |||
207 | return count; | ||
208 | } | ||
209 | |||
210 | ssize_t ipod_write(struct ipod_t* ipod, int nbytes) | ||
211 | { | ||
212 | unsigned long count; | ||
213 | |||
214 | if(ipod->sectorbuf == NULL) { | ||
215 | return -1; | ||
216 | } | ||
217 | if (!WriteFile(ipod->dh, ipod->sectorbuf, nbytes, &count, NULL)) { | ||
218 | ipod_print_error(" Error writing to disk: "); | ||
219 | return -1; | ||
220 | } | ||
221 | |||
222 | return count; | ||
223 | } | ||
224 | |||
225 | #endif | ||
226 | |||