]> git.proxmox.com Git - mirror_qemu.git/blame - block-raw-win32.c
target-mips: optimize decode_opc()
[mirror_qemu.git] / block-raw-win32.c
CommitLineData
223d4670
TS
1/*
2 * Block driver for RAW files (win32)
3 *
4 * Copyright (c) 2006 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include "qemu-common.h"
223d4670 25#include "qemu-timer.h"
223d4670
TS
26#include "block_int.h"
27#include <assert.h>
49dc768d 28#include <windows.h>
223d4670
TS
29#include <winioctl.h>
30
31#define FTYPE_FILE 0
32#define FTYPE_CD 1
33#define FTYPE_HARDDISK 2
34
35typedef struct BDRVRawState {
36 HANDLE hfile;
37 int type;
38 char drive_path[16]; /* format: "d:\" */
39} BDRVRawState;
40
223d4670
TS
41int qemu_ftruncate64(int fd, int64_t length)
42{
43 LARGE_INTEGER li;
44 LONG high;
45 HANDLE h;
46 BOOL res;
47
48 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
49 return -1;
50
51 h = (HANDLE)_get_osfhandle(fd);
52
53 /* get current position, ftruncate do not change position */
54 li.HighPart = 0;
55 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
56 if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
57 return -1;
58
59 high = length >> 32;
60 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
61 return -1;
62 res = SetEndOfFile(h);
63
64 /* back to old position */
65 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
66 return res ? 0 : -1;
67}
68
69static int set_sparse(int fd)
70{
71 DWORD returned;
72 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
73 NULL, 0, NULL, 0, &returned, NULL);
74}
75
76static int raw_open(BlockDriverState *bs, const char *filename, int flags)
77{
78 BDRVRawState *s = bs->opaque;
79 int access_flags, create_flags;
80 DWORD overlapped;
81
82 s->type = FTYPE_FILE;
83
84 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
85 access_flags = GENERIC_READ | GENERIC_WRITE;
86 } else {
87 access_flags = GENERIC_READ;
88 }
89 if (flags & BDRV_O_CREAT) {
90 create_flags = CREATE_ALWAYS;
91 } else {
92 create_flags = OPEN_EXISTING;
93 }
03ff3ca3 94 overlapped = FILE_ATTRIBUTE_NORMAL;
9f7965c7 95 if ((flags & BDRV_O_NOCACHE))
33f00271 96 overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
9f7965c7
AL
97 else if (!(flags & BDRV_O_CACHE_WB))
98 overlapped |= FILE_FLAG_WRITE_THROUGH;
223d4670
TS
99 s->hfile = CreateFile(filename, access_flags,
100 FILE_SHARE_READ, NULL,
101 create_flags, overlapped, NULL);
102 if (s->hfile == INVALID_HANDLE_VALUE) {
103 int err = GetLastError();
104
105 if (err == ERROR_ACCESS_DENIED)
106 return -EACCES;
107 return -1;
108 }
109 return 0;
110}
111
eda578e5
AL
112static int raw_read(BlockDriverState *bs, int64_t sector_num,
113 uint8_t *buf, int nb_sectors)
223d4670
TS
114{
115 BDRVRawState *s = bs->opaque;
116 OVERLAPPED ov;
117 DWORD ret_count;
118 int ret;
eda578e5
AL
119 int64_t offset = sector_num * 512;
120 int count = nb_sectors * 512;
223d4670
TS
121
122 memset(&ov, 0, sizeof(ov));
123 ov.Offset = offset;
124 ov.OffsetHigh = offset >> 32;
125 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
9c39be47
AL
126 if (!ret)
127 return ret_count;
537a1d4b
AL
128 if (ret_count == count)
129 ret_count = 0;
223d4670
TS
130 return ret_count;
131}
132
eda578e5
AL
133static int raw_write(BlockDriverState *bs, int64_t sector_num,
134 const uint8_t *buf, int nb_sectors)
223d4670
TS
135{
136 BDRVRawState *s = bs->opaque;
137 OVERLAPPED ov;
138 DWORD ret_count;
139 int ret;
eda578e5
AL
140 int64_t offset = sector_num * 512;
141 int count = nb_sectors * 512;
223d4670
TS
142
143 memset(&ov, 0, sizeof(ov));
144 ov.Offset = offset;
145 ov.OffsetHigh = offset >> 32;
146 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
9c39be47
AL
147 if (!ret)
148 return ret_count;
537a1d4b
AL
149 if (ret_count == count)
150 ret_count = 0;
223d4670
TS
151 return ret_count;
152}
153
223d4670
TS
154static void raw_flush(BlockDriverState *bs)
155{
156 BDRVRawState *s = bs->opaque;
157 FlushFileBuffers(s->hfile);
158}
159
160static void raw_close(BlockDriverState *bs)
161{
162 BDRVRawState *s = bs->opaque;
163 CloseHandle(s->hfile);
164}
165
166static int raw_truncate(BlockDriverState *bs, int64_t offset)
167{
168 BDRVRawState *s = bs->opaque;
b9e82a59 169 LONG low, high;
223d4670
TS
170
171 low = offset;
172 high = offset >> 32;
173 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
174 return -EIO;
175 if (!SetEndOfFile(s->hfile))
176 return -EIO;
177 return 0;
178}
179
180static int64_t raw_getlength(BlockDriverState *bs)
181{
182 BDRVRawState *s = bs->opaque;
183 LARGE_INTEGER l;
184 ULARGE_INTEGER available, total, total_free;
185 DISK_GEOMETRY_EX dg;
186 DWORD count;
187 BOOL status;
188
189 switch(s->type) {
190 case FTYPE_FILE:
b9e82a59 191 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
223d4670
TS
192 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
193 return -EIO;
194 break;
195 case FTYPE_CD:
196 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
197 return -EIO;
198 l.QuadPart = total.QuadPart;
199 break;
200 case FTYPE_HARDDISK:
201 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
202 NULL, 0, &dg, sizeof(dg), &count, NULL);
203 if (status != 0) {
204 l = dg.DiskSize;
205 }
206 break;
207 default:
208 return -EIO;
209 }
210 return l.QuadPart;
211}
212
213static int raw_create(const char *filename, int64_t total_size,
214 const char *backing_file, int flags)
215{
216 int fd;
217
218 if (flags || backing_file)
219 return -ENOTSUP;
220
221 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
222 0644);
223 if (fd < 0)
224 return -EIO;
225 set_sparse(fd);
226 ftruncate(fd, total_size * 512);
227 close(fd);
228 return 0;
229}
230
223d4670 231BlockDriver bdrv_raw = {
f1b2f712
AL
232 .format_name = "raw",
233 .instance_size = sizeof(BDRVRawState),
234 .bdrv_open = raw_open,
235 .bdrv_close = raw_close,
236 .bdrv_create = raw_create,
237 .bdrv_flush = raw_flush,
238 .bdrv_read = raw_read,
239 .bdrv_write = raw_write,
240 .bdrv_truncate = raw_truncate,
241 .bdrv_getlength = raw_getlength,
223d4670
TS
242};
243
244/***********************************************/
245/* host device */
246
247static int find_cdrom(char *cdrom_name, int cdrom_name_size)
248{
249 char drives[256], *pdrv = drives;
250 UINT type;
251
252 memset(drives, 0, sizeof(drives));
253 GetLogicalDriveStrings(sizeof(drives), drives);
254 while(pdrv[0] != '\0') {
255 type = GetDriveType(pdrv);
256 switch(type) {
257 case DRIVE_CDROM:
258 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
259 return 0;
260 break;
261 }
262 pdrv += lstrlen(pdrv) + 1;
263 }
264 return -1;
265}
266
267static int find_device_type(BlockDriverState *bs, const char *filename)
268{
269 BDRVRawState *s = bs->opaque;
270 UINT type;
271 const char *p;
272
273 if (strstart(filename, "\\\\.\\", &p) ||
274 strstart(filename, "//./", &p)) {
275 if (stristart(p, "PhysicalDrive", NULL))
276 return FTYPE_HARDDISK;
277 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
278 type = GetDriveType(s->drive_path);
3060cd14
AL
279 switch (type) {
280 case DRIVE_REMOVABLE:
281 case DRIVE_FIXED:
282 return FTYPE_HARDDISK;
283 case DRIVE_CDROM:
223d4670 284 return FTYPE_CD;
3060cd14 285 default:
223d4670 286 return FTYPE_FILE;
3060cd14 287 }
223d4670
TS
288 } else {
289 return FTYPE_FILE;
290 }
291}
292
293static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
294{
295 BDRVRawState *s = bs->opaque;
296 int access_flags, create_flags;
297 DWORD overlapped;
298 char device_name[64];
299
300 if (strstart(filename, "/dev/cdrom", NULL)) {
301 if (find_cdrom(device_name, sizeof(device_name)) < 0)
302 return -ENOENT;
303 filename = device_name;
304 } else {
305 /* transform drive letters into device name */
306 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
307 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
308 filename[1] == ':' && filename[2] == '\0') {
309 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
310 filename = device_name;
311 }
312 }
313 s->type = find_device_type(bs, filename);
314
315 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
316 access_flags = GENERIC_READ | GENERIC_WRITE;
317 } else {
318 access_flags = GENERIC_READ;
319 }
320 create_flags = OPEN_EXISTING;
321
03ff3ca3 322 overlapped = FILE_ATTRIBUTE_NORMAL;
9f7965c7 323 if ((flags & BDRV_O_NOCACHE))
33f00271 324 overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
9f7965c7
AL
325 else if (!(flags & BDRV_O_CACHE_WB))
326 overlapped |= FILE_FLAG_WRITE_THROUGH;
223d4670
TS
327 s->hfile = CreateFile(filename, access_flags,
328 FILE_SHARE_READ, NULL,
329 create_flags, overlapped, NULL);
330 if (s->hfile == INVALID_HANDLE_VALUE) {
331 int err = GetLastError();
332
333 if (err == ERROR_ACCESS_DENIED)
334 return -EACCES;
335 return -1;
336 }
337 return 0;
338}
339
340#if 0
341/***********************************************/
342/* removable device additional commands */
343
344static int raw_is_inserted(BlockDriverState *bs)
345{
346 return 1;
347}
348
349static int raw_media_changed(BlockDriverState *bs)
350{
351 return -ENOTSUP;
352}
353
354static int raw_eject(BlockDriverState *bs, int eject_flag)
355{
356 DWORD ret_count;
357
358 if (s->type == FTYPE_FILE)
359 return -ENOTSUP;
360 if (eject_flag) {
361 DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
362 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
363 } else {
364 DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
365 NULL, 0, NULL, 0, &lpBytesReturned, NULL);
366 }
367}
368
369static int raw_set_locked(BlockDriverState *bs, int locked)
370{
371 return -ENOTSUP;
372}
373#endif
374
375BlockDriver bdrv_host_device = {
e60f469c
AJ
376 .format_name = "host_device",
377 .instance_size = sizeof(BDRVRawState),
378 .bdrv_open = hdev_open,
379 .bdrv_close = raw_close,
380 .bdrv_flush = raw_flush,
223d4670 381
eda578e5
AL
382 .bdrv_read = raw_read,
383 .bdrv_write = raw_write,
e60f469c 384 .bdrv_getlength = raw_getlength,
223d4670 385};