pci: rename pci_register_bar_region() to pci_register_bar()
[qemu.git] / block / raw-win32.c
blobe47cfe0f4a47afa9e0b3c564c9aee4d113a4bfef
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 LONG high;
45 HANDLE h;
46 BOOL res;
48 if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
49 return -1;
51 h = (HANDLE)_get_osfhandle(fd);
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;
59 high = length >> 32;
60 if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
61 return -1;
62 res = SetEndOfFile(h);
64 /* back to old position */
65 SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
66 return res ? 0 : -1;
69 static int set_sparse(int fd)
71 DWORD returned;
72 return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
73 NULL, 0, NULL, 0, &returned, NULL);
76 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
78 BDRVRawState *s = bs->opaque;
79 int access_flags;
80 DWORD overlapped;
82 s->type = FTYPE_FILE;
84 if (flags & BDRV_O_RDWR) {
85 access_flags = GENERIC_READ | GENERIC_WRITE;
86 } else {
87 access_flags = GENERIC_READ;
90 overlapped = FILE_ATTRIBUTE_NORMAL;
91 if (flags & BDRV_O_NOCACHE)
92 overlapped |= FILE_FLAG_NO_BUFFERING;
93 if (!(flags & BDRV_O_CACHE_WB))
94 overlapped |= FILE_FLAG_WRITE_THROUGH;
95 s->hfile = CreateFile(filename, access_flags,
96 FILE_SHARE_READ, NULL,
97 OPEN_EXISTING, overlapped, NULL);
98 if (s->hfile == INVALID_HANDLE_VALUE) {
99 int err = GetLastError();
101 if (err == ERROR_ACCESS_DENIED)
102 return -EACCES;
103 return -1;
105 return 0;
108 static int raw_read(BlockDriverState *bs, int64_t sector_num,
109 uint8_t *buf, int nb_sectors)
111 BDRVRawState *s = bs->opaque;
112 OVERLAPPED ov;
113 DWORD ret_count;
114 int ret;
115 int64_t offset = sector_num * 512;
116 int count = nb_sectors * 512;
118 memset(&ov, 0, sizeof(ov));
119 ov.Offset = offset;
120 ov.OffsetHigh = offset >> 32;
121 ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
122 if (!ret)
123 return ret_count;
124 if (ret_count == count)
125 ret_count = 0;
126 return ret_count;
129 static int raw_write(BlockDriverState *bs, int64_t sector_num,
130 const uint8_t *buf, int nb_sectors)
132 BDRVRawState *s = bs->opaque;
133 OVERLAPPED ov;
134 DWORD ret_count;
135 int ret;
136 int64_t offset = sector_num * 512;
137 int count = nb_sectors * 512;
139 memset(&ov, 0, sizeof(ov));
140 ov.Offset = offset;
141 ov.OffsetHigh = offset >> 32;
142 ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
143 if (!ret)
144 return ret_count;
145 if (ret_count == count)
146 ret_count = 0;
147 return ret_count;
150 static int raw_flush(BlockDriverState *bs)
152 BDRVRawState *s = bs->opaque;
153 int ret;
155 ret = FlushFileBuffers(s->hfile);
156 if (ret == 0) {
157 return -EIO;
160 return 0;
163 static void raw_close(BlockDriverState *bs)
165 BDRVRawState *s = bs->opaque;
166 CloseHandle(s->hfile);
169 static int raw_truncate(BlockDriverState *bs, int64_t offset)
171 BDRVRawState *s = bs->opaque;
172 LONG low, high;
174 low = offset;
175 high = offset >> 32;
176 if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
177 return -EIO;
178 if (!SetEndOfFile(s->hfile))
179 return -EIO;
180 return 0;
183 static int64_t raw_getlength(BlockDriverState *bs)
185 BDRVRawState *s = bs->opaque;
186 LARGE_INTEGER l;
187 ULARGE_INTEGER available, total, total_free;
188 DISK_GEOMETRY_EX dg;
189 DWORD count;
190 BOOL status;
192 switch(s->type) {
193 case FTYPE_FILE:
194 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
195 if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
196 return -EIO;
197 break;
198 case FTYPE_CD:
199 if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
200 return -EIO;
201 l.QuadPart = total.QuadPart;
202 break;
203 case FTYPE_HARDDISK:
204 status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
205 NULL, 0, &dg, sizeof(dg), &count, NULL);
206 if (status != 0) {
207 l = dg.DiskSize;
209 break;
210 default:
211 return -EIO;
213 return l.QuadPart;
216 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
218 typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
219 DWORD * high);
220 get_compressed_t get_compressed;
221 struct _stati64 st;
222 const char *filename = bs->filename;
223 /* WinNT support GetCompressedFileSize to determine allocate size */
224 get_compressed =
225 (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
226 "GetCompressedFileSizeA");
227 if (get_compressed) {
228 DWORD high, low;
229 low = get_compressed(filename, &high);
230 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
231 return (((int64_t) high) << 32) + low;
235 if (_stati64(filename, &st) < 0) {
236 return -1;
238 return st.st_size;
241 static int raw_create(const char *filename, QEMUOptionParameter *options)
243 int fd;
244 int64_t total_size = 0;
246 /* Read out options */
247 while (options && options->name) {
248 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
249 total_size = options->value.n / 512;
251 options++;
254 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
255 0644);
256 if (fd < 0)
257 return -EIO;
258 set_sparse(fd);
259 ftruncate(fd, total_size * 512);
260 close(fd);
261 return 0;
264 static QEMUOptionParameter raw_create_options[] = {
266 .name = BLOCK_OPT_SIZE,
267 .type = OPT_SIZE,
268 .help = "Virtual disk size"
270 { NULL }
273 static BlockDriver bdrv_file = {
274 .format_name = "file",
275 .protocol_name = "file",
276 .instance_size = sizeof(BDRVRawState),
277 .bdrv_file_open = raw_open,
278 .bdrv_close = raw_close,
279 .bdrv_create = raw_create,
280 .bdrv_flush = raw_flush,
281 .bdrv_read = raw_read,
282 .bdrv_write = raw_write,
283 .bdrv_truncate = raw_truncate,
284 .bdrv_getlength = raw_getlength,
285 .bdrv_get_allocated_file_size
286 = raw_get_allocated_file_size,
288 .create_options = raw_create_options,
291 /***********************************************/
292 /* host device */
294 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
296 char drives[256], *pdrv = drives;
297 UINT type;
299 memset(drives, 0, sizeof(drives));
300 GetLogicalDriveStrings(sizeof(drives), drives);
301 while(pdrv[0] != '\0') {
302 type = GetDriveType(pdrv);
303 switch(type) {
304 case DRIVE_CDROM:
305 snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
306 return 0;
307 break;
309 pdrv += lstrlen(pdrv) + 1;
311 return -1;
314 static int find_device_type(BlockDriverState *bs, const char *filename)
316 BDRVRawState *s = bs->opaque;
317 UINT type;
318 const char *p;
320 if (strstart(filename, "\\\\.\\", &p) ||
321 strstart(filename, "//./", &p)) {
322 if (stristart(p, "PhysicalDrive", NULL))
323 return FTYPE_HARDDISK;
324 snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
325 type = GetDriveType(s->drive_path);
326 switch (type) {
327 case DRIVE_REMOVABLE:
328 case DRIVE_FIXED:
329 return FTYPE_HARDDISK;
330 case DRIVE_CDROM:
331 return FTYPE_CD;
332 default:
333 return FTYPE_FILE;
335 } else {
336 return FTYPE_FILE;
340 static int hdev_probe_device(const char *filename)
342 if (strstart(filename, "/dev/cdrom", NULL))
343 return 100;
344 if (is_windows_drive(filename))
345 return 100;
346 return 0;
349 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
351 BDRVRawState *s = bs->opaque;
352 int access_flags, create_flags;
353 DWORD overlapped;
354 char device_name[64];
356 if (strstart(filename, "/dev/cdrom", NULL)) {
357 if (find_cdrom(device_name, sizeof(device_name)) < 0)
358 return -ENOENT;
359 filename = device_name;
360 } else {
361 /* transform drive letters into device name */
362 if (((filename[0] >= 'a' && filename[0] <= 'z') ||
363 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
364 filename[1] == ':' && filename[2] == '\0') {
365 snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
366 filename = device_name;
369 s->type = find_device_type(bs, filename);
371 if (flags & BDRV_O_RDWR) {
372 access_flags = GENERIC_READ | GENERIC_WRITE;
373 } else {
374 access_flags = GENERIC_READ;
376 create_flags = OPEN_EXISTING;
378 overlapped = FILE_ATTRIBUTE_NORMAL;
379 if (flags & BDRV_O_NOCACHE)
380 overlapped |= FILE_FLAG_NO_BUFFERING;
381 if (!(flags & BDRV_O_CACHE_WB))
382 overlapped |= FILE_FLAG_WRITE_THROUGH;
383 s->hfile = CreateFile(filename, access_flags,
384 FILE_SHARE_READ, NULL,
385 create_flags, overlapped, NULL);
386 if (s->hfile == INVALID_HANDLE_VALUE) {
387 int err = GetLastError();
389 if (err == ERROR_ACCESS_DENIED)
390 return -EACCES;
391 return -1;
393 return 0;
396 static int hdev_has_zero_init(BlockDriverState *bs)
398 return 0;
401 static BlockDriver bdrv_host_device = {
402 .format_name = "host_device",
403 .protocol_name = "host_device",
404 .instance_size = sizeof(BDRVRawState),
405 .bdrv_probe_device = hdev_probe_device,
406 .bdrv_file_open = hdev_open,
407 .bdrv_close = raw_close,
408 .bdrv_flush = raw_flush,
409 .bdrv_has_zero_init = hdev_has_zero_init,
411 .bdrv_read = raw_read,
412 .bdrv_write = raw_write,
413 .bdrv_getlength = raw_getlength,
414 .bdrv_get_allocated_file_size
415 = raw_get_allocated_file_size,
418 static void bdrv_file_init(void)
420 bdrv_register(&bdrv_file);
421 bdrv_register(&bdrv_host_device);
424 block_init(bdrv_file_init);