]> Git Repo - qemu.git/blob - block/raw-win32.c
vvfat: fix out of bounds array_get usage
[qemu.git] / block / raw-win32.c
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"
25 #include "qemu-timer.h"
26 #include "block_int.h"
27 #include "module.h"
28 #include <windows.h>
29 #include <winioctl.h>
30
31 #define FTYPE_FILE 0
32 #define FTYPE_CD     1
33 #define FTYPE_HARDDISK 2
34
35 typedef struct BDRVRawState {
36     HANDLE hfile;
37     int type;
38     char drive_path[16]; /* format: "d:\" */
39 } BDRVRawState;
40
41 int qemu_ftruncate64(int fd, int64_t length)
42 {
43     LARGE_INTEGER li;
44     DWORD dw;
45     LONG high;
46     HANDLE h;
47     BOOL res;
48
49     if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
50         return -1;
51
52     h = (HANDLE)_get_osfhandle(fd);
53
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;
59     }
60
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;
65     }
66     res = SetEndOfFile(h);
67
68     /* back to old position */
69     SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
70     return res ? 0 : -1;
71 }
72
73 static int set_sparse(int fd)
74 {
75     DWORD returned;
76     return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
77                                  NULL, 0, NULL, 0, &returned, NULL);
78 }
79
80 static int raw_open(BlockDriverState *bs, const char *filename, int flags)
81 {
82     BDRVRawState *s = bs->opaque;
83     int access_flags;
84     DWORD overlapped;
85
86     s->type = FTYPE_FILE;
87
88     if (flags & BDRV_O_RDWR) {
89         access_flags = GENERIC_READ | GENERIC_WRITE;
90     } else {
91         access_flags = GENERIC_READ;
92     }
93
94     overlapped = FILE_ATTRIBUTE_NORMAL;
95     if (flags & BDRV_O_NOCACHE)
96         overlapped |= FILE_FLAG_NO_BUFFERING;
97     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                           OPEN_EXISTING, 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
112 static int raw_read(BlockDriverState *bs, int64_t sector_num,
113                     uint8_t *buf, int nb_sectors)
114 {
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;
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);
126     if (!ret)
127         return ret_count;
128     if (ret_count == count)
129         ret_count = 0;
130     return ret_count;
131 }
132
133 static int raw_write(BlockDriverState *bs, int64_t sector_num,
134                      const uint8_t *buf, int nb_sectors)
135 {
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;
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);
147     if (!ret)
148         return ret_count;
149     if (ret_count == count)
150         ret_count = 0;
151     return ret_count;
152 }
153
154 static int raw_flush(BlockDriverState *bs)
155 {
156     BDRVRawState *s = bs->opaque;
157     int ret;
158
159     ret = FlushFileBuffers(s->hfile);
160     if (ret == 0) {
161         return -EIO;
162     }
163
164     return 0;
165 }
166
167 static void raw_close(BlockDriverState *bs)
168 {
169     BDRVRawState *s = bs->opaque;
170     CloseHandle(s->hfile);
171 }
172
173 static int raw_truncate(BlockDriverState *bs, int64_t offset)
174 {
175     BDRVRawState *s = bs->opaque;
176     LONG low, high;
177
178     low = offset;
179     high = offset >> 32;
180     if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
181         return -EIO;
182     if (!SetEndOfFile(s->hfile))
183         return -EIO;
184     return 0;
185 }
186
187 static int64_t raw_getlength(BlockDriverState *bs)
188 {
189     BDRVRawState *s = bs->opaque;
190     LARGE_INTEGER l;
191     ULARGE_INTEGER available, total, total_free;
192     DISK_GEOMETRY_EX dg;
193     DWORD count;
194     BOOL status;
195
196     switch(s->type) {
197     case FTYPE_FILE:
198         l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
199         if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
200             return -EIO;
201         break;
202     case FTYPE_CD:
203         if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
204             return -EIO;
205         l.QuadPart = total.QuadPart;
206         break;
207     case FTYPE_HARDDISK:
208         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
209                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
210         if (status != 0) {
211             l = dg.DiskSize;
212         }
213         break;
214     default:
215         return -EIO;
216     }
217     return l.QuadPart;
218 }
219
220 static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
221 {
222     typedef DWORD (WINAPI * get_compressed_t)(const char *filename,
223                                               DWORD * high);
224     get_compressed_t get_compressed;
225     struct _stati64 st;
226     const char *filename = bs->filename;
227     /* WinNT support GetCompressedFileSize to determine allocate size */
228     get_compressed =
229         (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"),
230                                             "GetCompressedFileSizeA");
231     if (get_compressed) {
232         DWORD high, low;
233         low = get_compressed(filename, &high);
234         if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) {
235             return (((int64_t) high) << 32) + low;
236         }
237     }
238
239     if (_stati64(filename, &st) < 0) {
240         return -1;
241     }
242     return st.st_size;
243 }
244
245 static int raw_create(const char *filename, QEMUOptionParameter *options)
246 {
247     int fd;
248     int64_t total_size = 0;
249
250     /* Read out options */
251     while (options && options->name) {
252         if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
253             total_size = options->value.n / 512;
254         }
255         options++;
256     }
257
258     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
259               0644);
260     if (fd < 0)
261         return -EIO;
262     set_sparse(fd);
263     ftruncate(fd, total_size * 512);
264     close(fd);
265     return 0;
266 }
267
268 static QEMUOptionParameter raw_create_options[] = {
269     {
270         .name = BLOCK_OPT_SIZE,
271         .type = OPT_SIZE,
272         .help = "Virtual disk size"
273     },
274     { NULL }
275 };
276
277 static BlockDriver bdrv_file = {
278     .format_name        = "file",
279     .protocol_name      = "file",
280     .instance_size      = sizeof(BDRVRawState),
281     .bdrv_file_open     = raw_open,
282     .bdrv_close         = raw_close,
283     .bdrv_create        = raw_create,
284     .bdrv_co_flush      = raw_flush,
285     .bdrv_read          = raw_read,
286     .bdrv_write         = raw_write,
287     .bdrv_truncate      = raw_truncate,
288     .bdrv_getlength     = raw_getlength,
289     .bdrv_get_allocated_file_size
290                         = raw_get_allocated_file_size,
291
292     .create_options = raw_create_options,
293 };
294
295 /***********************************************/
296 /* host device */
297
298 static int find_cdrom(char *cdrom_name, int cdrom_name_size)
299 {
300     char drives[256], *pdrv = drives;
301     UINT type;
302
303     memset(drives, 0, sizeof(drives));
304     GetLogicalDriveStrings(sizeof(drives), drives);
305     while(pdrv[0] != '\0') {
306         type = GetDriveType(pdrv);
307         switch(type) {
308         case DRIVE_CDROM:
309             snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
310             return 0;
311             break;
312         }
313         pdrv += lstrlen(pdrv) + 1;
314     }
315     return -1;
316 }
317
318 static int find_device_type(BlockDriverState *bs, const char *filename)
319 {
320     BDRVRawState *s = bs->opaque;
321     UINT type;
322     const char *p;
323
324     if (strstart(filename, "\\\\.\\", &p) ||
325         strstart(filename, "//./", &p)) {
326         if (stristart(p, "PhysicalDrive", NULL))
327             return FTYPE_HARDDISK;
328         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
329         type = GetDriveType(s->drive_path);
330         switch (type) {
331         case DRIVE_REMOVABLE:
332         case DRIVE_FIXED:
333             return FTYPE_HARDDISK;
334         case DRIVE_CDROM:
335             return FTYPE_CD;
336         default:
337             return FTYPE_FILE;
338         }
339     } else {
340         return FTYPE_FILE;
341     }
342 }
343
344 static int hdev_probe_device(const char *filename)
345 {
346     if (strstart(filename, "/dev/cdrom", NULL))
347         return 100;
348     if (is_windows_drive(filename))
349         return 100;
350     return 0;
351 }
352
353 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
354 {
355     BDRVRawState *s = bs->opaque;
356     int access_flags, create_flags;
357     DWORD overlapped;
358     char device_name[64];
359
360     if (strstart(filename, "/dev/cdrom", NULL)) {
361         if (find_cdrom(device_name, sizeof(device_name)) < 0)
362             return -ENOENT;
363         filename = device_name;
364     } else {
365         /* transform drive letters into device name */
366         if (((filename[0] >= 'a' && filename[0] <= 'z') ||
367              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
368             filename[1] == ':' && filename[2] == '\0') {
369             snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
370             filename = device_name;
371         }
372     }
373     s->type = find_device_type(bs, filename);
374
375     if (flags & BDRV_O_RDWR) {
376         access_flags = GENERIC_READ | GENERIC_WRITE;
377     } else {
378         access_flags = GENERIC_READ;
379     }
380     create_flags = OPEN_EXISTING;
381
382     overlapped = FILE_ATTRIBUTE_NORMAL;
383     if (flags & BDRV_O_NOCACHE)
384         overlapped |= FILE_FLAG_NO_BUFFERING;
385     if (!(flags & BDRV_O_CACHE_WB))
386         overlapped |= FILE_FLAG_WRITE_THROUGH;
387     s->hfile = CreateFile(filename, access_flags,
388                           FILE_SHARE_READ, NULL,
389                           create_flags, overlapped, NULL);
390     if (s->hfile == INVALID_HANDLE_VALUE) {
391         int err = GetLastError();
392
393         if (err == ERROR_ACCESS_DENIED)
394             return -EACCES;
395         return -1;
396     }
397     return 0;
398 }
399
400 static int hdev_has_zero_init(BlockDriverState *bs)
401 {
402     return 0;
403 }
404
405 static BlockDriver bdrv_host_device = {
406     .format_name        = "host_device",
407     .protocol_name      = "host_device",
408     .instance_size      = sizeof(BDRVRawState),
409     .bdrv_probe_device  = hdev_probe_device,
410     .bdrv_file_open     = hdev_open,
411     .bdrv_close         = raw_close,
412     .bdrv_co_flush      = raw_flush,
413     .bdrv_has_zero_init = hdev_has_zero_init,
414
415     .bdrv_read          = raw_read,
416     .bdrv_write         = raw_write,
417     .bdrv_getlength     = raw_getlength,
418     .bdrv_get_allocated_file_size
419                         = raw_get_allocated_file_size,
420 };
421
422 static void bdrv_file_init(void)
423 {
424     bdrv_register(&bdrv_file);
425     bdrv_register(&bdrv_host_device);
426 }
427
428 block_init(bdrv_file_init);
This page took 0.049856 seconds and 4 git commands to generate.