]>
Commit | Line | Data |
---|---|---|
fc01f7e7 FB |
1 | /* |
2 | * QEMU System Emulator block driver | |
3 | * | |
4 | * Copyright (c) 2003 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 | */ | |
fc01f7e7 | 24 | #include "vl.h" |
ea2384d3 | 25 | #include "block_int.h" |
fc01f7e7 | 26 | |
7674e7bf FB |
27 | #ifdef _BSD |
28 | #include <sys/types.h> | |
29 | #include <sys/stat.h> | |
30 | #include <sys/ioctl.h> | |
31 | #include <sys/queue.h> | |
32 | #include <sys/disk.h> | |
33 | #endif | |
34 | ||
3b0d4f61 FB |
35 | #ifdef CONFIG_COCOA |
36 | #include <paths.h> | |
37 | #include <sys/param.h> | |
38 | #include <IOKit/IOKitLib.h> | |
39 | #include <IOKit/IOBSD.h> | |
40 | #include <IOKit/storage/IOMediaBSDClient.h> | |
41 | #include <IOKit/storage/IOMedia.h> | |
42 | #include <IOKit/storage/IOCDMedia.h> | |
43 | //#include <IOKit/storage/IOCDTypes.h> | |
44 | #include <CoreFoundation/CoreFoundation.h> | |
45 | #endif | |
46 | ||
b338082b | 47 | static BlockDriverState *bdrv_first; |
ea2384d3 FB |
48 | static BlockDriver *first_drv; |
49 | ||
3b0d4f61 FB |
50 | #ifdef CONFIG_COCOA |
51 | static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ); | |
52 | static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ); | |
53 | ||
54 | kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ) | |
55 | { | |
56 | kern_return_t kernResult; | |
57 | mach_port_t masterPort; | |
58 | CFMutableDictionaryRef classesToMatch; | |
59 | ||
60 | kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); | |
61 | if ( KERN_SUCCESS != kernResult ) { | |
62 | printf( "IOMasterPort returned %d\n", kernResult ); | |
63 | } | |
64 | ||
65 | classesToMatch = IOServiceMatching( kIOCDMediaClass ); | |
66 | if ( classesToMatch == NULL ) { | |
67 | printf( "IOServiceMatching returned a NULL dictionary.\n" ); | |
68 | } else { | |
69 | CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue ); | |
70 | } | |
71 | kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator ); | |
72 | if ( KERN_SUCCESS != kernResult ) | |
73 | { | |
74 | printf( "IOServiceGetMatchingServices returned %d\n", kernResult ); | |
75 | } | |
76 | ||
77 | return kernResult; | |
78 | } | |
79 | ||
80 | kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ) | |
81 | { | |
82 | io_object_t nextMedia; | |
83 | kern_return_t kernResult = KERN_FAILURE; | |
84 | *bsdPath = '\0'; | |
85 | nextMedia = IOIteratorNext( mediaIterator ); | |
86 | if ( nextMedia ) | |
87 | { | |
88 | CFTypeRef bsdPathAsCFString; | |
89 | bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 ); | |
90 | if ( bsdPathAsCFString ) { | |
91 | size_t devPathLength; | |
92 | strcpy( bsdPath, _PATH_DEV ); | |
93 | strcat( bsdPath, "r" ); | |
94 | devPathLength = strlen( bsdPath ); | |
95 | if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) { | |
96 | kernResult = KERN_SUCCESS; | |
97 | } | |
98 | CFRelease( bsdPathAsCFString ); | |
99 | } | |
100 | IOObjectRelease( nextMedia ); | |
101 | } | |
102 | ||
103 | return kernResult; | |
104 | } | |
105 | ||
106 | #endif | |
107 | ||
ea2384d3 FB |
108 | void bdrv_register(BlockDriver *bdrv) |
109 | { | |
110 | bdrv->next = first_drv; | |
111 | first_drv = bdrv; | |
112 | } | |
b338082b FB |
113 | |
114 | /* create a new block device (by default it is empty) */ | |
115 | BlockDriverState *bdrv_new(const char *device_name) | |
116 | { | |
117 | BlockDriverState **pbs, *bs; | |
118 | ||
119 | bs = qemu_mallocz(sizeof(BlockDriverState)); | |
120 | if(!bs) | |
121 | return NULL; | |
122 | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); | |
ea2384d3 FB |
123 | if (device_name[0] != '\0') { |
124 | /* insert at the end */ | |
125 | pbs = &bdrv_first; | |
126 | while (*pbs != NULL) | |
127 | pbs = &(*pbs)->next; | |
128 | *pbs = bs; | |
129 | } | |
b338082b FB |
130 | return bs; |
131 | } | |
132 | ||
ea2384d3 FB |
133 | BlockDriver *bdrv_find_format(const char *format_name) |
134 | { | |
135 | BlockDriver *drv1; | |
136 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { | |
137 | if (!strcmp(drv1->format_name, format_name)) | |
138 | return drv1; | |
139 | } | |
140 | return NULL; | |
141 | } | |
142 | ||
143 | int bdrv_create(BlockDriver *drv, | |
144 | const char *filename, int64_t size_in_sectors, | |
145 | const char *backing_file, int flags) | |
146 | { | |
147 | if (!drv->bdrv_create) | |
148 | return -ENOTSUP; | |
149 | return drv->bdrv_create(filename, size_in_sectors, backing_file, flags); | |
150 | } | |
151 | ||
d5249393 FB |
152 | #ifdef _WIN32 |
153 | static void get_tmp_filename(char *filename, int size) | |
154 | { | |
155 | /* XXX: find a better function */ | |
156 | tmpnam(filename); | |
157 | } | |
158 | #else | |
ea2384d3 | 159 | static void get_tmp_filename(char *filename, int size) |
fc01f7e7 | 160 | { |
67b915a5 | 161 | int fd; |
d5249393 | 162 | /* XXX: race condition possible */ |
ea2384d3 FB |
163 | pstrcpy(filename, size, "/tmp/vl.XXXXXX"); |
164 | fd = mkstemp(filename); | |
165 | close(fd); | |
166 | } | |
d5249393 | 167 | #endif |
fc01f7e7 | 168 | |
7674e7bf FB |
169 | /* XXX: force raw format if block or character device ? It would |
170 | simplify the BSD case */ | |
ea2384d3 FB |
171 | static BlockDriver *find_image_format(const char *filename) |
172 | { | |
173 | int fd, ret, score, score_max; | |
174 | BlockDriver *drv1, *drv; | |
7674e7bf FB |
175 | uint8_t *buf; |
176 | size_t bufsize = 1024; | |
33e3963e | 177 | |
ea2384d3 | 178 | fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
712e7874 FB |
179 | if (fd < 0) { |
180 | buf = NULL; | |
181 | ret = 0; | |
182 | } else { | |
7674e7bf | 183 | #ifdef DIOCGSECTORSIZE |
712e7874 FB |
184 | { |
185 | unsigned int sectorsize = 512; | |
186 | if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) && | |
187 | sectorsize > bufsize) | |
188 | bufsize = sectorsize; | |
189 | } | |
3b0d4f61 FB |
190 | #endif |
191 | #ifdef CONFIG_COCOA | |
192 | u_int32_t blockSize = 512; | |
193 | if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) { | |
194 | bufsize = blockSize; | |
195 | } | |
7674e7bf | 196 | #endif |
712e7874 FB |
197 | buf = qemu_malloc(bufsize); |
198 | if (!buf) | |
199 | return NULL; | |
200 | ret = read(fd, buf, bufsize); | |
201 | if (ret < 0) { | |
202 | close(fd); | |
203 | qemu_free(buf); | |
204 | return NULL; | |
205 | } | |
ea2384d3 | 206 | close(fd); |
ea2384d3 | 207 | } |
ea2384d3 FB |
208 | |
209 | drv = NULL; | |
210 | score_max = 0; | |
211 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { | |
212 | score = drv1->bdrv_probe(buf, ret, filename); | |
213 | if (score > score_max) { | |
214 | score_max = score; | |
215 | drv = drv1; | |
0849bf08 | 216 | } |
fc01f7e7 | 217 | } |
712e7874 | 218 | qemu_free(buf); |
ea2384d3 FB |
219 | return drv; |
220 | } | |
221 | ||
222 | int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot) | |
223 | { | |
3b0d4f61 FB |
224 | #ifdef CONFIG_COCOA |
225 | if ( strncmp( filename, "/dev/cdrom", 10 ) == 0 ) { | |
226 | kern_return_t kernResult; | |
227 | io_iterator_t mediaIterator; | |
228 | char bsdPath[ MAXPATHLEN ]; | |
229 | int fd; | |
230 | ||
231 | kernResult = FindEjectableCDMedia( &mediaIterator ); | |
232 | kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) ); | |
233 | ||
234 | if ( bsdPath[ 0 ] != '\0' ) { | |
235 | strcat(bsdPath,"s0"); | |
236 | /* some CDs don't have a partition 0 */ | |
237 | fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); | |
238 | if (fd < 0) { | |
239 | bsdPath[strlen(bsdPath)-1] = '1'; | |
240 | } else { | |
241 | close(fd); | |
242 | } | |
243 | filename = bsdPath; | |
244 | } | |
245 | ||
246 | if ( mediaIterator ) | |
247 | IOObjectRelease( mediaIterator ); | |
248 | } | |
249 | #endif | |
ea2384d3 FB |
250 | return bdrv_open2(bs, filename, snapshot, NULL); |
251 | } | |
252 | ||
253 | int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot, | |
254 | BlockDriver *drv) | |
255 | { | |
256 | int ret; | |
257 | char tmp_filename[1024]; | |
258 | ||
259 | bs->read_only = 0; | |
260 | bs->is_temporary = 0; | |
261 | bs->encrypted = 0; | |
712e7874 | 262 | |
ea2384d3 FB |
263 | if (snapshot) { |
264 | BlockDriverState *bs1; | |
265 | int64_t total_size; | |
266 | ||
267 | /* if snapshot, we create a temporary backing file and open it | |
268 | instead of opening 'filename' directly */ | |
33e3963e | 269 | |
ea2384d3 FB |
270 | /* if there is a backing file, use it */ |
271 | bs1 = bdrv_new(""); | |
272 | if (!bs1) { | |
273 | return -1; | |
274 | } | |
275 | if (bdrv_open(bs1, filename, 0) < 0) { | |
276 | bdrv_delete(bs1); | |
277 | return -1; | |
278 | } | |
279 | total_size = bs1->total_sectors; | |
280 | bdrv_delete(bs1); | |
281 | ||
282 | get_tmp_filename(tmp_filename, sizeof(tmp_filename)); | |
283 | /* XXX: use cow for linux as it is more efficient ? */ | |
284 | if (bdrv_create(&bdrv_qcow, tmp_filename, | |
285 | total_size, filename, 0) < 0) { | |
286 | return -1; | |
287 | } | |
288 | filename = tmp_filename; | |
289 | bs->is_temporary = 1; | |
290 | } | |
712e7874 | 291 | |
ea2384d3 FB |
292 | pstrcpy(bs->filename, sizeof(bs->filename), filename); |
293 | if (!drv) { | |
294 | drv = find_image_format(filename); | |
295 | if (!drv) | |
296 | return -1; | |
297 | } | |
298 | bs->drv = drv; | |
299 | bs->opaque = qemu_mallocz(drv->instance_size); | |
300 | if (bs->opaque == NULL && drv->instance_size > 0) | |
301 | return -1; | |
302 | ||
303 | ret = drv->bdrv_open(bs, filename); | |
304 | if (ret < 0) { | |
305 | qemu_free(bs->opaque); | |
306 | return -1; | |
33e3963e | 307 | } |
67b915a5 | 308 | #ifndef _WIN32 |
ea2384d3 FB |
309 | if (bs->is_temporary) { |
310 | unlink(filename); | |
311 | } | |
312 | #endif | |
313 | if (bs->backing_file[0] != '\0' && drv->bdrv_is_allocated) { | |
314 | /* if there is a backing file, use it */ | |
315 | bs->backing_hd = bdrv_new(""); | |
316 | if (!bs->backing_hd) { | |
317 | fail: | |
318 | bdrv_close(bs); | |
319 | return -1; | |
33e3963e | 320 | } |
ea2384d3 | 321 | if (bdrv_open(bs->backing_hd, bs->backing_file, 0) < 0) |
33e3963e | 322 | goto fail; |
33e3963e FB |
323 | } |
324 | ||
b338082b FB |
325 | bs->inserted = 1; |
326 | ||
327 | /* call the change callback */ | |
328 | if (bs->change_cb) | |
329 | bs->change_cb(bs->change_opaque); | |
330 | ||
331 | return 0; | |
fc01f7e7 FB |
332 | } |
333 | ||
334 | void bdrv_close(BlockDriverState *bs) | |
335 | { | |
b338082b | 336 | if (bs->inserted) { |
ea2384d3 FB |
337 | if (bs->backing_hd) |
338 | bdrv_delete(bs->backing_hd); | |
339 | bs->drv->bdrv_close(bs); | |
340 | qemu_free(bs->opaque); | |
341 | #ifdef _WIN32 | |
342 | if (bs->is_temporary) { | |
343 | unlink(bs->filename); | |
344 | } | |
67b915a5 | 345 | #endif |
ea2384d3 FB |
346 | bs->opaque = NULL; |
347 | bs->drv = NULL; | |
b338082b FB |
348 | bs->inserted = 0; |
349 | ||
350 | /* call the change callback */ | |
351 | if (bs->change_cb) | |
352 | bs->change_cb(bs->change_opaque); | |
353 | } | |
354 | } | |
355 | ||
356 | void bdrv_delete(BlockDriverState *bs) | |
357 | { | |
ea2384d3 | 358 | /* XXX: remove the driver list */ |
b338082b FB |
359 | bdrv_close(bs); |
360 | qemu_free(bs); | |
fc01f7e7 FB |
361 | } |
362 | ||
33e3963e FB |
363 | /* commit COW file into the raw image */ |
364 | int bdrv_commit(BlockDriverState *bs) | |
365 | { | |
366 | int64_t i; | |
ea2384d3 FB |
367 | int n, j; |
368 | unsigned char sector[512]; | |
33e3963e | 369 | |
b338082b | 370 | if (!bs->inserted) |
ea2384d3 | 371 | return -ENOENT; |
33e3963e FB |
372 | |
373 | if (bs->read_only) { | |
ea2384d3 | 374 | return -EACCES; |
33e3963e FB |
375 | } |
376 | ||
ea2384d3 FB |
377 | if (!bs->backing_hd) { |
378 | return -ENOTSUP; | |
379 | } | |
33e3963e | 380 | |
ea2384d3 FB |
381 | for (i = 0; i < bs->total_sectors;) { |
382 | if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) { | |
383 | for(j = 0; j < n; j++) { | |
384 | if (bdrv_read(bs, i, sector, 1) != 0) { | |
385 | return -EIO; | |
386 | } | |
387 | ||
388 | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { | |
389 | return -EIO; | |
390 | } | |
391 | i++; | |
33e3963e | 392 | } |
ea2384d3 FB |
393 | } else { |
394 | i += n; | |
395 | } | |
33e3963e | 396 | } |
33e3963e FB |
397 | return 0; |
398 | } | |
399 | ||
fc01f7e7 FB |
400 | /* return -1 if error */ |
401 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, | |
402 | uint8_t *buf, int nb_sectors) | |
403 | { | |
ea2384d3 FB |
404 | int ret, n; |
405 | BlockDriver *drv = bs->drv; | |
406 | ||
b338082b FB |
407 | if (!bs->inserted) |
408 | return -1; | |
409 | ||
33e3963e | 410 | while (nb_sectors > 0) { |
ea2384d3 | 411 | if (sector_num == 0 && bs->boot_sector_enabled) { |
cf98951b FB |
412 | memcpy(buf, bs->boot_sector_data, 512); |
413 | n = 1; | |
ea2384d3 FB |
414 | } else if (bs->backing_hd) { |
415 | if (drv->bdrv_is_allocated(bs, sector_num, nb_sectors, &n)) { | |
416 | ret = drv->bdrv_read(bs, sector_num, buf, n); | |
417 | if (ret < 0) | |
418 | return -1; | |
419 | } else { | |
420 | /* read from the base image */ | |
421 | ret = bdrv_read(bs->backing_hd, sector_num, buf, n); | |
422 | if (ret < 0) | |
423 | return -1; | |
424 | } | |
33e3963e | 425 | } else { |
ea2384d3 FB |
426 | ret = drv->bdrv_read(bs, sector_num, buf, nb_sectors); |
427 | if (ret < 0) | |
33e3963e | 428 | return -1; |
ea2384d3 FB |
429 | /* no need to loop */ |
430 | break; | |
33e3963e FB |
431 | } |
432 | nb_sectors -= n; | |
433 | sector_num += n; | |
434 | buf += n * 512; | |
435 | } | |
436 | return 0; | |
fc01f7e7 FB |
437 | } |
438 | ||
439 | /* return -1 if error */ | |
440 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, | |
441 | const uint8_t *buf, int nb_sectors) | |
442 | { | |
b338082b FB |
443 | if (!bs->inserted) |
444 | return -1; | |
0849bf08 FB |
445 | if (bs->read_only) |
446 | return -1; | |
ea2384d3 | 447 | return bs->drv->bdrv_write(bs, sector_num, buf, nb_sectors); |
fc01f7e7 FB |
448 | } |
449 | ||
450 | void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr) | |
451 | { | |
452 | *nb_sectors_ptr = bs->total_sectors; | |
453 | } | |
cf98951b FB |
454 | |
455 | /* force a given boot sector. */ | |
456 | void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size) | |
457 | { | |
458 | bs->boot_sector_enabled = 1; | |
459 | if (size > 512) | |
460 | size = 512; | |
461 | memcpy(bs->boot_sector_data, data, size); | |
462 | memset(bs->boot_sector_data + size, 0, 512 - size); | |
463 | } | |
b338082b FB |
464 | |
465 | void bdrv_set_geometry_hint(BlockDriverState *bs, | |
466 | int cyls, int heads, int secs) | |
467 | { | |
468 | bs->cyls = cyls; | |
469 | bs->heads = heads; | |
470 | bs->secs = secs; | |
471 | } | |
472 | ||
473 | void bdrv_set_type_hint(BlockDriverState *bs, int type) | |
474 | { | |
475 | bs->type = type; | |
476 | bs->removable = ((type == BDRV_TYPE_CDROM || | |
477 | type == BDRV_TYPE_FLOPPY)); | |
478 | } | |
479 | ||
46d4767d FB |
480 | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
481 | { | |
482 | bs->translation = translation; | |
483 | } | |
484 | ||
b338082b FB |
485 | void bdrv_get_geometry_hint(BlockDriverState *bs, |
486 | int *pcyls, int *pheads, int *psecs) | |
487 | { | |
488 | *pcyls = bs->cyls; | |
489 | *pheads = bs->heads; | |
490 | *psecs = bs->secs; | |
491 | } | |
492 | ||
493 | int bdrv_get_type_hint(BlockDriverState *bs) | |
494 | { | |
495 | return bs->type; | |
496 | } | |
497 | ||
46d4767d FB |
498 | int bdrv_get_translation_hint(BlockDriverState *bs) |
499 | { | |
500 | return bs->translation; | |
501 | } | |
502 | ||
b338082b FB |
503 | int bdrv_is_removable(BlockDriverState *bs) |
504 | { | |
505 | return bs->removable; | |
506 | } | |
507 | ||
508 | int bdrv_is_read_only(BlockDriverState *bs) | |
509 | { | |
510 | return bs->read_only; | |
511 | } | |
512 | ||
513 | int bdrv_is_inserted(BlockDriverState *bs) | |
514 | { | |
515 | return bs->inserted; | |
516 | } | |
517 | ||
518 | int bdrv_is_locked(BlockDriverState *bs) | |
519 | { | |
520 | return bs->locked; | |
521 | } | |
522 | ||
523 | void bdrv_set_locked(BlockDriverState *bs, int locked) | |
524 | { | |
525 | bs->locked = locked; | |
526 | } | |
527 | ||
528 | void bdrv_set_change_cb(BlockDriverState *bs, | |
529 | void (*change_cb)(void *opaque), void *opaque) | |
530 | { | |
531 | bs->change_cb = change_cb; | |
532 | bs->change_opaque = opaque; | |
533 | } | |
534 | ||
ea2384d3 FB |
535 | int bdrv_is_encrypted(BlockDriverState *bs) |
536 | { | |
537 | if (bs->backing_hd && bs->backing_hd->encrypted) | |
538 | return 1; | |
539 | return bs->encrypted; | |
540 | } | |
541 | ||
542 | int bdrv_set_key(BlockDriverState *bs, const char *key) | |
543 | { | |
544 | int ret; | |
545 | if (bs->backing_hd && bs->backing_hd->encrypted) { | |
546 | ret = bdrv_set_key(bs->backing_hd, key); | |
547 | if (ret < 0) | |
548 | return ret; | |
549 | if (!bs->encrypted) | |
550 | return 0; | |
551 | } | |
552 | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) | |
553 | return -1; | |
554 | return bs->drv->bdrv_set_key(bs, key); | |
555 | } | |
556 | ||
557 | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) | |
558 | { | |
559 | if (!bs->inserted || !bs->drv) { | |
560 | buf[0] = '\0'; | |
561 | } else { | |
562 | pstrcpy(buf, buf_size, bs->drv->format_name); | |
563 | } | |
564 | } | |
565 | ||
566 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), | |
567 | void *opaque) | |
568 | { | |
569 | BlockDriver *drv; | |
570 | ||
571 | for (drv = first_drv; drv != NULL; drv = drv->next) { | |
572 | it(opaque, drv->format_name); | |
573 | } | |
574 | } | |
575 | ||
b338082b FB |
576 | BlockDriverState *bdrv_find(const char *name) |
577 | { | |
578 | BlockDriverState *bs; | |
579 | ||
580 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { | |
581 | if (!strcmp(name, bs->device_name)) | |
582 | return bs; | |
583 | } | |
584 | return NULL; | |
585 | } | |
586 | ||
81d0912d FB |
587 | void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque) |
588 | { | |
589 | BlockDriverState *bs; | |
590 | ||
591 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { | |
592 | it(opaque, bs->device_name); | |
593 | } | |
594 | } | |
595 | ||
ea2384d3 FB |
596 | const char *bdrv_get_device_name(BlockDriverState *bs) |
597 | { | |
598 | return bs->device_name; | |
599 | } | |
600 | ||
b338082b FB |
601 | void bdrv_info(void) |
602 | { | |
603 | BlockDriverState *bs; | |
604 | ||
605 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { | |
606 | term_printf("%s:", bs->device_name); | |
607 | term_printf(" type="); | |
608 | switch(bs->type) { | |
609 | case BDRV_TYPE_HD: | |
610 | term_printf("hd"); | |
611 | break; | |
612 | case BDRV_TYPE_CDROM: | |
613 | term_printf("cdrom"); | |
614 | break; | |
615 | case BDRV_TYPE_FLOPPY: | |
616 | term_printf("floppy"); | |
617 | break; | |
618 | } | |
619 | term_printf(" removable=%d", bs->removable); | |
620 | if (bs->removable) { | |
621 | term_printf(" locked=%d", bs->locked); | |
622 | } | |
623 | if (bs->inserted) { | |
624 | term_printf(" file=%s", bs->filename); | |
ea2384d3 FB |
625 | if (bs->backing_file[0] != '\0') |
626 | term_printf(" backing_file=%s", bs->backing_file); | |
b338082b | 627 | term_printf(" ro=%d", bs->read_only); |
ea2384d3 FB |
628 | term_printf(" drv=%s", bs->drv->format_name); |
629 | if (bs->encrypted) | |
630 | term_printf(" encrypted"); | |
b338082b FB |
631 | } else { |
632 | term_printf(" [not inserted]"); | |
633 | } | |
634 | term_printf("\n"); | |
635 | } | |
636 | } | |
ea2384d3 FB |
637 | |
638 | ||
639 | /**************************************************************/ | |
640 | /* RAW block driver */ | |
641 | ||
642 | typedef struct BDRVRawState { | |
643 | int fd; | |
644 | } BDRVRawState; | |
645 | ||
646 | static int raw_probe(const uint8_t *buf, int buf_size, const char *filename) | |
647 | { | |
648 | return 1; /* maybe */ | |
649 | } | |
650 | ||
651 | static int raw_open(BlockDriverState *bs, const char *filename) | |
652 | { | |
653 | BDRVRawState *s = bs->opaque; | |
654 | int fd; | |
655 | int64_t size; | |
e5484d33 FB |
656 | #ifdef _BSD |
657 | struct stat sb; | |
658 | #endif | |
ea2384d3 FB |
659 | |
660 | fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); | |
661 | if (fd < 0) { | |
662 | fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); | |
663 | if (fd < 0) | |
664 | return -1; | |
665 | bs->read_only = 1; | |
666 | } | |
7674e7bf | 667 | #ifdef _BSD |
e5484d33 | 668 | if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) { |
7674e7bf | 669 | #ifdef DIOCGMEDIASIZE |
e5484d33 | 670 | if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size)) |
7674e7bf | 671 | #endif |
3b0d4f61 FB |
672 | #ifdef CONFIG_COCOA |
673 | size = LONG_LONG_MAX; | |
674 | #else | |
675 | size = lseek(fd, 0LL, SEEK_END); | |
676 | #endif | |
7674e7bf FB |
677 | } else |
678 | #endif | |
679 | { | |
680 | size = lseek(fd, 0, SEEK_END); | |
681 | } | |
c747cd1f FB |
682 | #ifdef _WIN32 |
683 | /* On Windows hosts it can happen that we're unable to get file size | |
684 | for CD-ROM raw device (it's inherent limitation of the CDFS driver). */ | |
685 | if (size == -1) | |
686 | size = LONG_LONG_MAX; | |
687 | #endif | |
ea2384d3 FB |
688 | bs->total_sectors = size / 512; |
689 | s->fd = fd; | |
690 | return 0; | |
691 | } | |
692 | ||
693 | static int raw_read(BlockDriverState *bs, int64_t sector_num, | |
694 | uint8_t *buf, int nb_sectors) | |
695 | { | |
696 | BDRVRawState *s = bs->opaque; | |
697 | int ret; | |
698 | ||
d5249393 | 699 | lseek(s->fd, sector_num * 512, SEEK_SET); |
ea2384d3 FB |
700 | ret = read(s->fd, buf, nb_sectors * 512); |
701 | if (ret != nb_sectors * 512) | |
702 | return -1; | |
703 | return 0; | |
704 | } | |
705 | ||
706 | static int raw_write(BlockDriverState *bs, int64_t sector_num, | |
707 | const uint8_t *buf, int nb_sectors) | |
708 | { | |
709 | BDRVRawState *s = bs->opaque; | |
710 | int ret; | |
711 | ||
d5249393 | 712 | lseek(s->fd, sector_num * 512, SEEK_SET); |
ea2384d3 FB |
713 | ret = write(s->fd, buf, nb_sectors * 512); |
714 | if (ret != nb_sectors * 512) | |
715 | return -1; | |
716 | return 0; | |
717 | } | |
718 | ||
e2731add | 719 | static void raw_close(BlockDriverState *bs) |
ea2384d3 FB |
720 | { |
721 | BDRVRawState *s = bs->opaque; | |
722 | close(s->fd); | |
723 | } | |
724 | ||
725 | static int raw_create(const char *filename, int64_t total_size, | |
726 | const char *backing_file, int flags) | |
727 | { | |
728 | int fd; | |
729 | ||
730 | if (flags || backing_file) | |
731 | return -ENOTSUP; | |
732 | ||
733 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, | |
734 | 0644); | |
735 | if (fd < 0) | |
736 | return -EIO; | |
d5249393 | 737 | ftruncate(fd, total_size * 512); |
ea2384d3 FB |
738 | close(fd); |
739 | return 0; | |
740 | } | |
741 | ||
742 | BlockDriver bdrv_raw = { | |
743 | "raw", | |
744 | sizeof(BDRVRawState), | |
745 | raw_probe, | |
746 | raw_open, | |
747 | raw_read, | |
748 | raw_write, | |
749 | raw_close, | |
750 | raw_create, | |
751 | }; | |
752 | ||
753 | void bdrv_init(void) | |
754 | { | |
755 | bdrv_register(&bdrv_raw); | |
756 | #ifndef _WIN32 | |
757 | bdrv_register(&bdrv_cow); | |
758 | #endif | |
759 | bdrv_register(&bdrv_qcow); | |
760 | bdrv_register(&bdrv_vmdk); | |
3c56521b | 761 | bdrv_register(&bdrv_cloop); |
585d0ed9 | 762 | bdrv_register(&bdrv_dmg); |
a8753c34 | 763 | bdrv_register(&bdrv_bochs); |
6a0f9e82 | 764 | bdrv_register(&bdrv_vpc); |
712e7874 | 765 | bdrv_register(&bdrv_vvfat); |
ea2384d3 | 766 | } |