Convert mulscc
[qemu/hppa.git] / block-raw-win32.c
blobb5287d2a56fbafa8d5c0be818eb703ffb4ea6998
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 <assert.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, create_flags;
80 DWORD overlapped;
82 s->type = FTYPE_FILE;
84 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
85 access_flags = GENERIC_READ | GENERIC_WRITE;
86 } else {
87 access_flags = GENERIC_READ;
89 if (flags & BDRV_O_CREAT) {
90 create_flags = CREATE_ALWAYS;
91 } else {
92 create_flags = OPEN_EXISTING;
94 overlapped = FILE_ATTRIBUTE_NORMAL;
95 if ((flags & BDRV_O_NOCACHE))
96 overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
97 else if (!(flags & BDRV_O_CACHE_WB))
98 overlapped |= FILE_FLAG_WRITE_THROUGH;
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();
105 if (err == ERROR_ACCESS_DENIED)
106 return -EACCES;
107 return -1;
109 return 0;
112 static int raw_read(BlockDriverState *bs, int64_t sector_num,
113 uint8_t *buf, int nb_sectors)
115 BDRVRawState *s = bs->opaque;
116 OVERLAPPED ov;
117 DWORD ret_count;
118 int ret;
119 int64_t offset = sector_num * 512;
120 int count = nb_sectors * 512;
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);
126 if (!ret)
127 return ret_count;
128 if (ret_count == count)
129 ret_count = 0;
130 return ret_count;
133 static int raw_write(BlockDriverState *bs, int64_t sector_num,
134 const uint8_t *buf, int nb_sectors)
136 BDRVRawState *s = bs->opaque;
137 OVERLAPPED ov;
138 DWORD ret_count;
139 int ret;
140 int64_t offset = sector_num * 512;
141 int count = nb_sectors * 512;
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);
147 if (!ret)
148 return ret_count;
149 if (ret_count == count)
150 ret_count = 0;
151 return ret_count;
154 static void raw_flush(BlockDriverState *bs)
156 BDRVRawState *s = bs->opaque;
157 FlushFileBuffers(s->hfile);
160 static void raw_close(BlockDriverState *bs)
162 BDRVRawState *s = bs->opaque;
163 CloseHandle(s->hfile);
166 static int raw_truncate(BlockDriverState *bs, int64_t offset)
168 BDRVRawState *s = bs->opaque;
169 LONG low, high;
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;
180 static int64_t raw_getlength(BlockDriverState *bs)
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;
189 switch(s->type) {
190 case FTYPE_FILE:
191 l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
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;
206 break;
207 default:
208 return -EIO;
210 return l.QuadPart;
213 static int raw_create(const char *filename, int64_t total_size,
214 const char *backing_file, int flags)
216 int fd;
218 if (flags || backing_file)
219 return -ENOTSUP;
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;
231 BlockDriver bdrv_raw = {
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,
244 /***********************************************/
245 /* host device */
247 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
249 char drives[256], *pdrv = drives;
250 UINT type;
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;
262 pdrv += lstrlen(pdrv) + 1;
264 return -1;
267 static int find_device_type(BlockDriverState *bs, const char *filename)
269 BDRVRawState *s = bs->opaque;
270 UINT type;
271 const char *p;
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);
279 switch (type) {
280 case DRIVE_REMOVABLE:
281 case DRIVE_FIXED:
282 return FTYPE_HARDDISK;
283 case DRIVE_CDROM:
284 return FTYPE_CD;
285 default:
286 return FTYPE_FILE;
288 } else {
289 return FTYPE_FILE;
293 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
295 BDRVRawState *s = bs->opaque;
296 int access_flags, create_flags;
297 DWORD overlapped;
298 char device_name[64];
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;
313 s->type = find_device_type(bs, filename);
315 if ((flags & BDRV_O_ACCESS) == O_RDWR) {
316 access_flags = GENERIC_READ | GENERIC_WRITE;
317 } else {
318 access_flags = GENERIC_READ;
320 create_flags = OPEN_EXISTING;
322 overlapped = FILE_ATTRIBUTE_NORMAL;
323 if ((flags & BDRV_O_NOCACHE))
324 overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
325 else if (!(flags & BDRV_O_CACHE_WB))
326 overlapped |= FILE_FLAG_WRITE_THROUGH;
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();
333 if (err == ERROR_ACCESS_DENIED)
334 return -EACCES;
335 return -1;
337 return 0;
340 #if 0
341 /***********************************************/
342 /* removable device additional commands */
344 static int raw_is_inserted(BlockDriverState *bs)
346 return 1;
349 static int raw_media_changed(BlockDriverState *bs)
351 return -ENOTSUP;
354 static int raw_eject(BlockDriverState *bs, int eject_flag)
356 DWORD ret_count;
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);
369 static int raw_set_locked(BlockDriverState *bs, int locked)
371 return -ENOTSUP;
373 #endif
375 BlockDriver bdrv_host_device = {
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,
382 .bdrv_read = raw_read,
383 .bdrv_write = raw_write,
384 .bdrv_getlength = raw_getlength,