configure: Remove unused parameters from main function
[qemu/rayw.git] / block / raw-win32.c
blob78c830648bd24625068d79a0331216c00cc4bc58
1 /*
2 * Block driver for RAW files (win32)
4 * Copyright (c) 2006 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu-common.h"
25 #include "qemu-timer.h"
26 #include "block_int.h"
27 #include "module.h"
28 #include <windows.h>
29 #include <winioctl.h>
31 #define FTYPE_FILE 0
32 #define FTYPE_CD 1
33 #define FTYPE_HARDDISK 2
35 typedef struct BDRVRawState {
36 HANDLE hfile;
37 int type;
38 char drive_path[16]; /* format: "d:\" */
39 } BDRVRawState;
41 int qemu_ftruncate64(int fd, int64_t length)
43 LARGE_INTEGER li;
44 DWORD dw;
45 LONG high;
46 HANDLE h;
47 BOOL res;
49 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
50 return -1;
52 h = (HANDLE)_get_osfhandle(fd);
54 /* get current position, ftruncate do not change position */
55 li.HighPart = 0;
56 li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
57 if (li.LowPart == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
58 return -1;
61 high = length >> 32;
62 dw = SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN);
63 if (dw == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
64 return -1;
66 res = SetEndOfFile(h);
68 /* back to old position */
69 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
70 return res ? 0 : -1;
73 static int set_sparse(int fd)
75 DWORD returned;
76 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
77 NULL, 0, NULL, 0, &returned, NULL);
80 static void raw_parse_flags(int flags, int *access_flags, DWORD *overlapped)
82 assert(access_flags != NULL);
83 assert(overlapped != NULL);
85 if (flags & BDRV_O_RDWR) {
86 *access_flags = GENERIC_READ | GENERIC_WRITE;
87 } else {
88 *access_flags = GENERIC_READ;
91 *overlapped = FILE_ATTRIBUTE_NORMAL;
92 if (flags & BDRV_O_NOCACHE) {
93 *overlapped |= FILE_FLAG_NO_BUFFERING;
97 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
99 BDRVRawState *s = bs->opaque;
100 int access_flags;
101 DWORD overlapped;
103 s->type = FTYPE_FILE;
105 raw_parse_flags(flags, &access_flags, &overlapped);
107 s->hfile = CreateFile(filename, access_flags,
108 FILE_SHARE_READ, NULL,
109 OPEN_EXISTING, overlapped, NULL);
110 if (s->hfile == INVALID_HANDLE_VALUE) {
111 int err = GetLastError();
113 if (err == ERROR_ACCESS_DENIED)
114 return -EACCES;
115 return -1;
117 return 0;
120 static int raw_read(BlockDriverState *bs, int64_t sector_num,
121 uint8_t *buf, int nb_sectors)
123 BDRVRawState *s = bs->opaque;
124 OVERLAPPED ov;
125 DWORD ret_count;
126 int ret;
127 int64_t offset = sector_num * 512;
128 int count = nb_sectors * 512;
130 memset(&ov, 0, sizeof(ov));
131 ov.Offset = offset;
132 ov.OffsetHigh = offset >> 32;
133 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
134 if (!ret)
135 return ret_count;
136 if (ret_count == count)
137 ret_count = 0;
138 return ret_count;
141 static int raw_write(BlockDriverState *bs, int64_t sector_num,
142 const uint8_t *buf, int nb_sectors)
144 BDRVRawState *s = bs->opaque;
145 OVERLAPPED ov;
146 DWORD ret_count;
147 int ret;
148 int64_t offset = sector_num * 512;
149 int count = nb_sectors * 512;
151 memset(&ov, 0, sizeof(ov));
152 ov.Offset = offset;
153 ov.OffsetHigh = offset >> 32;
154 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
155 if (!ret)
156 return ret_count;
157 if (ret_count == count)
158 ret_count = 0;
159 return ret_count;
162 static int raw_flush(BlockDriverState *bs)
164 BDRVRawState *s = bs->opaque;
165 int ret;
167 ret = FlushFileBuffers(s->hfile);
168 if (ret == 0) {
169 return -EIO;
172 return 0;
175 static void raw_close(BlockDriverState *bs)
177 BDRVRawState *s = bs->opaque;
178 CloseHandle(s->hfile);
181 static int raw_truncate(BlockDriverState *bs, int64_t offset)
183 BDRVRawState *s = bs->opaque;
184 LONG low, high;
186 low = offset;
187 high = offset >> 32;
188 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
189 return -EIO;
190 if (!SetEndOfFile(s->hfile))
191 return -EIO;
192 return 0;
195 static int64_t raw_getlength(BlockDriverState *bs)
197 BDRVRawState *s = bs->opaque;
198 LARGE_INTEGER l;
199 ULARGE_INTEGER available, total, total_free;
200 DISK_GEOMETRY_EX dg;
201 DWORD count;
202 BOOL status;
204 switch(s->type) {
205 case FTYPE_FILE:
206 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
207 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
208 return -EIO;
209 break;
210 case FTYPE_CD:
211 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
212 return -EIO;
213 l.QuadPart = total.QuadPart;
214 break;
215 case FTYPE_HARDDISK:
216 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
217 NULL, 0, &dg, sizeof(dg), &count, NULL);
218 if (status != 0) {
219 l = dg.DiskSize;
221 break;
222 default:
223 return -EIO;
225 return l.QuadPart;
228 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
230 typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
231 DWORD * high);
232 get_compressed_t get_compressed;
233 struct _stati64 st;
234 const char *filename = bs->filename;
235 /* WinNT support GetCompressedFileSize to determine allocate size */
236 get_compressed =
237 (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
238 "GetCompressedFileSizeA");
239 if (get_compressed) {
240 DWORD high, low;
241 low = get_compressed(filename, &high);
242 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
243 return (((int64_t) high) << 32) + low;
247 if (_stati64(filename, &st) < 0) {
248 return -1;
250 return st.st_size;
253 static int raw_create(const char *filename, QEMUOptionParameter *options)
255 int fd;
256 int64_t total_size = 0;
258 /* Read out options */
259 while (options && options->name) {
260 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
261 total_size = options->value.n / 512;
263 options++;
266 fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
267 0644);
268 if (fd < 0)
269 return -EIO;
270 set_sparse(fd);
271 ftruncate(fd, total_size * 512);
272 qemu_close(fd);
273 return 0;
276 static QEMUOptionParameter raw_create_options[] = {
278 .name = BLOCK_OPT_SIZE,
279 .type = OPT_SIZE,
280 .help = "Virtual disk size"
282 { NULL }
285 static BlockDriver bdrv_file = {
286 .format_name = "file",
287 .protocol_name = "file",
288 .instance_size = sizeof(BDRVRawState),
289 .bdrv_file_open = raw_open,
290 .bdrv_close = raw_close,
291 .bdrv_create = raw_create,
293 .bdrv_read = raw_read,
294 .bdrv_write = raw_write,
295 .bdrv_co_flush_to_disk = raw_flush,
297 .bdrv_truncate = raw_truncate,
298 .bdrv_getlength = raw_getlength,
299 .bdrv_get_allocated_file_size
300 = raw_get_allocated_file_size,
302 .create_options = raw_create_options,
305 /***********************************************/
306 /* host device */
308 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
310 char drives[256], *pdrv = drives;
311 UINT type;
313 memset(drives, 0, sizeof(drives));
314 GetLogicalDriveStrings(sizeof(drives), drives);
315 while(pdrv[0] != '\0') {
316 type = GetDriveType(pdrv);
317 switch(type) {
318 case DRIVE_CDROM:
319 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
320 return 0;
321 break;
323 pdrv += lstrlen(pdrv) + 1;
325 return -1;
328 static int find_device_type(BlockDriverState *bs, const char *filename)
330 BDRVRawState *s = bs->opaque;
331 UINT type;
332 const char *p;
334 if (strstart(filename, "\\\\.\\", &p) ||
335 strstart(filename, "//./", &p)) {
336 if (stristart(p, "PhysicalDrive", NULL))
337 return FTYPE_HARDDISK;
338 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
339 type = GetDriveType(s->drive_path);
340 switch (type) {
341 case DRIVE_REMOVABLE:
342 case DRIVE_FIXED:
343 return FTYPE_HARDDISK;
344 case DRIVE_CDROM:
345 return FTYPE_CD;
346 default:
347 return FTYPE_FILE;
349 } else {
350 return FTYPE_FILE;
354 static int hdev_probe_device(const char *filename)
356 if (strstart(filename, "/dev/cdrom", NULL))
357 return 100;
358 if (is_windows_drive(filename))
359 return 100;
360 return 0;
363 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
365 BDRVRawState *s = bs->opaque;
366 int access_flags, create_flags;
367 DWORD overlapped;
368 char device_name[64];
370 if (strstart(filename, "/dev/cdrom", NULL)) {
371 if (find_cdrom(device_name, sizeof(device_name)) < 0)
372 return -ENOENT;
373 filename = device_name;
374 } else {
375 /* transform drive letters into device name */
376 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
377 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
378 filename[1] == ':' && filename[2] == '\0') {
379 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
380 filename = device_name;
383 s->type = find_device_type(bs, filename);
385 raw_parse_flags(flags, &access_flags, &overlapped);
387 create_flags = OPEN_EXISTING;
389 s->hfile = CreateFile(filename, access_flags,
390 FILE_SHARE_READ, NULL,
391 create_flags, overlapped, NULL);
392 if (s->hfile == INVALID_HANDLE_VALUE) {
393 int err = GetLastError();
395 if (err == ERROR_ACCESS_DENIED)
396 return -EACCES;
397 return -1;
399 return 0;
402 static int hdev_has_zero_init(BlockDriverState *bs)
404 return 0;
407 static BlockDriver bdrv_host_device = {
408 .format_name = "host_device",
409 .protocol_name = "host_device",
410 .instance_size = sizeof(BDRVRawState),
411 .bdrv_probe_device = hdev_probe_device,
412 .bdrv_file_open = hdev_open,
413 .bdrv_close = raw_close,
414 .bdrv_has_zero_init = hdev_has_zero_init,
416 .bdrv_read = raw_read,
417 .bdrv_write = raw_write,
418 .bdrv_co_flush_to_disk = raw_flush,
420 .bdrv_getlength = raw_getlength,
421 .bdrv_get_allocated_file_size
422 = raw_get_allocated_file_size,
425 static void bdrv_file_init(void)
427 bdrv_register(&bdrv_file);
428 bdrv_register(&bdrv_host_device);
431 block_init(bdrv_file_init);