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