]> git.proxmox.com Git - mirror_qemu.git/blame - block.c
raw CDROM access for windows (Filip Navara)
[mirror_qemu.git] / block.c
CommitLineData
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
b338082b 27static BlockDriverState *bdrv_first;
ea2384d3
FB
28static BlockDriver *first_drv;
29
30void bdrv_register(BlockDriver *bdrv)
31{
32 bdrv->next = first_drv;
33 first_drv = bdrv;
34}
b338082b
FB
35
36/* create a new block device (by default it is empty) */
37BlockDriverState *bdrv_new(const char *device_name)
38{
39 BlockDriverState **pbs, *bs;
40
41 bs = qemu_mallocz(sizeof(BlockDriverState));
42 if(!bs)
43 return NULL;
44 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
ea2384d3
FB
45 if (device_name[0] != '\0') {
46 /* insert at the end */
47 pbs = &bdrv_first;
48 while (*pbs != NULL)
49 pbs = &(*pbs)->next;
50 *pbs = bs;
51 }
b338082b
FB
52 return bs;
53}
54
ea2384d3
FB
55BlockDriver *bdrv_find_format(const char *format_name)
56{
57 BlockDriver *drv1;
58 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
59 if (!strcmp(drv1->format_name, format_name))
60 return drv1;
61 }
62 return NULL;
63}
64
65int bdrv_create(BlockDriver *drv,
66 const char *filename, int64_t size_in_sectors,
67 const char *backing_file, int flags)
68{
69 if (!drv->bdrv_create)
70 return -ENOTSUP;
71 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
72}
73
d5249393
FB
74#ifdef _WIN32
75static void get_tmp_filename(char *filename, int size)
76{
77 /* XXX: find a better function */
78 tmpnam(filename);
79}
80#else
ea2384d3 81static void get_tmp_filename(char *filename, int size)
fc01f7e7 82{
67b915a5 83 int fd;
d5249393 84 /* XXX: race condition possible */
ea2384d3
FB
85 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
86 fd = mkstemp(filename);
87 close(fd);
88}
d5249393 89#endif
fc01f7e7 90
ea2384d3
FB
91static BlockDriver *find_image_format(const char *filename)
92{
93 int fd, ret, score, score_max;
94 BlockDriver *drv1, *drv;
95 uint8_t buf[1024];
33e3963e 96
ea2384d3
FB
97 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
98 if (fd < 0)
99 return NULL;
100 ret = read(fd, buf, sizeof(buf));
101 if (ret < 0) {
102 close(fd);
103 return NULL;
104 }
105 close(fd);
106
107 drv = NULL;
108 score_max = 0;
109 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
110 score = drv1->bdrv_probe(buf, ret, filename);
111 if (score > score_max) {
112 score_max = score;
113 drv = drv1;
0849bf08 114 }
fc01f7e7 115 }
ea2384d3
FB
116 return drv;
117}
118
119int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot)
120{
121 return bdrv_open2(bs, filename, snapshot, NULL);
122}
123
124int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot,
125 BlockDriver *drv)
126{
127 int ret;
128 char tmp_filename[1024];
129
130 bs->read_only = 0;
131 bs->is_temporary = 0;
132 bs->encrypted = 0;
133
134 if (snapshot) {
135 BlockDriverState *bs1;
136 int64_t total_size;
137
138 /* if snapshot, we create a temporary backing file and open it
139 instead of opening 'filename' directly */
33e3963e 140
ea2384d3
FB
141 /* if there is a backing file, use it */
142 bs1 = bdrv_new("");
143 if (!bs1) {
144 return -1;
145 }
146 if (bdrv_open(bs1, filename, 0) < 0) {
147 bdrv_delete(bs1);
148 return -1;
149 }
150 total_size = bs1->total_sectors;
151 bdrv_delete(bs1);
152
153 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
154 /* XXX: use cow for linux as it is more efficient ? */
155 if (bdrv_create(&bdrv_qcow, tmp_filename,
156 total_size, filename, 0) < 0) {
157 return -1;
158 }
159 filename = tmp_filename;
160 bs->is_temporary = 1;
161 }
162
163 pstrcpy(bs->filename, sizeof(bs->filename), filename);
164 if (!drv) {
165 drv = find_image_format(filename);
166 if (!drv)
167 return -1;
168 }
169 bs->drv = drv;
170 bs->opaque = qemu_mallocz(drv->instance_size);
171 if (bs->opaque == NULL && drv->instance_size > 0)
172 return -1;
173
174 ret = drv->bdrv_open(bs, filename);
175 if (ret < 0) {
176 qemu_free(bs->opaque);
177 return -1;
33e3963e 178 }
67b915a5 179#ifndef _WIN32
ea2384d3
FB
180 if (bs->is_temporary) {
181 unlink(filename);
182 }
183#endif
184 if (bs->backing_file[0] != '\0' && drv->bdrv_is_allocated) {
185 /* if there is a backing file, use it */
186 bs->backing_hd = bdrv_new("");
187 if (!bs->backing_hd) {
188 fail:
189 bdrv_close(bs);
190 return -1;
33e3963e 191 }
ea2384d3 192 if (bdrv_open(bs->backing_hd, bs->backing_file, 0) < 0)
33e3963e 193 goto fail;
33e3963e
FB
194 }
195
b338082b
FB
196 bs->inserted = 1;
197
198 /* call the change callback */
199 if (bs->change_cb)
200 bs->change_cb(bs->change_opaque);
201
202 return 0;
fc01f7e7
FB
203}
204
205void bdrv_close(BlockDriverState *bs)
206{
b338082b 207 if (bs->inserted) {
ea2384d3
FB
208 if (bs->backing_hd)
209 bdrv_delete(bs->backing_hd);
210 bs->drv->bdrv_close(bs);
211 qemu_free(bs->opaque);
212#ifdef _WIN32
213 if (bs->is_temporary) {
214 unlink(bs->filename);
215 }
67b915a5 216#endif
ea2384d3
FB
217 bs->opaque = NULL;
218 bs->drv = NULL;
b338082b
FB
219 bs->inserted = 0;
220
221 /* call the change callback */
222 if (bs->change_cb)
223 bs->change_cb(bs->change_opaque);
224 }
225}
226
227void bdrv_delete(BlockDriverState *bs)
228{
ea2384d3 229 /* XXX: remove the driver list */
b338082b
FB
230 bdrv_close(bs);
231 qemu_free(bs);
fc01f7e7
FB
232}
233
33e3963e
FB
234/* commit COW file into the raw image */
235int bdrv_commit(BlockDriverState *bs)
236{
237 int64_t i;
ea2384d3
FB
238 int n, j;
239 unsigned char sector[512];
33e3963e 240
b338082b 241 if (!bs->inserted)
ea2384d3 242 return -ENOENT;
33e3963e
FB
243
244 if (bs->read_only) {
ea2384d3 245 return -EACCES;
33e3963e
FB
246 }
247
ea2384d3
FB
248 if (!bs->backing_hd) {
249 return -ENOTSUP;
250 }
33e3963e 251
ea2384d3
FB
252 for (i = 0; i < bs->total_sectors;) {
253 if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) {
254 for(j = 0; j < n; j++) {
255 if (bdrv_read(bs, i, sector, 1) != 0) {
256 return -EIO;
257 }
258
259 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
260 return -EIO;
261 }
262 i++;
33e3963e 263 }
ea2384d3
FB
264 } else {
265 i += n;
266 }
33e3963e 267 }
33e3963e
FB
268 return 0;
269}
270
fc01f7e7
FB
271/* return -1 if error */
272int bdrv_read(BlockDriverState *bs, int64_t sector_num,
273 uint8_t *buf, int nb_sectors)
274{
ea2384d3
FB
275 int ret, n;
276 BlockDriver *drv = bs->drv;
277
b338082b
FB
278 if (!bs->inserted)
279 return -1;
280
33e3963e 281 while (nb_sectors > 0) {
ea2384d3 282 if (sector_num == 0 && bs->boot_sector_enabled) {
cf98951b
FB
283 memcpy(buf, bs->boot_sector_data, 512);
284 n = 1;
ea2384d3
FB
285 } else if (bs->backing_hd) {
286 if (drv->bdrv_is_allocated(bs, sector_num, nb_sectors, &n)) {
287 ret = drv->bdrv_read(bs, sector_num, buf, n);
288 if (ret < 0)
289 return -1;
290 } else {
291 /* read from the base image */
292 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
293 if (ret < 0)
294 return -1;
295 }
33e3963e 296 } else {
ea2384d3
FB
297 ret = drv->bdrv_read(bs, sector_num, buf, nb_sectors);
298 if (ret < 0)
33e3963e 299 return -1;
ea2384d3
FB
300 /* no need to loop */
301 break;
33e3963e
FB
302 }
303 nb_sectors -= n;
304 sector_num += n;
305 buf += n * 512;
306 }
307 return 0;
fc01f7e7
FB
308}
309
310/* return -1 if error */
311int bdrv_write(BlockDriverState *bs, int64_t sector_num,
312 const uint8_t *buf, int nb_sectors)
313{
b338082b
FB
314 if (!bs->inserted)
315 return -1;
0849bf08
FB
316 if (bs->read_only)
317 return -1;
ea2384d3 318 return bs->drv->bdrv_write(bs, sector_num, buf, nb_sectors);
fc01f7e7
FB
319}
320
321void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
322{
323 *nb_sectors_ptr = bs->total_sectors;
324}
cf98951b
FB
325
326/* force a given boot sector. */
327void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
328{
329 bs->boot_sector_enabled = 1;
330 if (size > 512)
331 size = 512;
332 memcpy(bs->boot_sector_data, data, size);
333 memset(bs->boot_sector_data + size, 0, 512 - size);
334}
b338082b
FB
335
336void bdrv_set_geometry_hint(BlockDriverState *bs,
337 int cyls, int heads, int secs)
338{
339 bs->cyls = cyls;
340 bs->heads = heads;
341 bs->secs = secs;
342}
343
344void bdrv_set_type_hint(BlockDriverState *bs, int type)
345{
346 bs->type = type;
347 bs->removable = ((type == BDRV_TYPE_CDROM ||
348 type == BDRV_TYPE_FLOPPY));
349}
350
46d4767d
FB
351void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
352{
353 bs->translation = translation;
354}
355
b338082b
FB
356void bdrv_get_geometry_hint(BlockDriverState *bs,
357 int *pcyls, int *pheads, int *psecs)
358{
359 *pcyls = bs->cyls;
360 *pheads = bs->heads;
361 *psecs = bs->secs;
362}
363
364int bdrv_get_type_hint(BlockDriverState *bs)
365{
366 return bs->type;
367}
368
46d4767d
FB
369int bdrv_get_translation_hint(BlockDriverState *bs)
370{
371 return bs->translation;
372}
373
b338082b
FB
374int bdrv_is_removable(BlockDriverState *bs)
375{
376 return bs->removable;
377}
378
379int bdrv_is_read_only(BlockDriverState *bs)
380{
381 return bs->read_only;
382}
383
384int bdrv_is_inserted(BlockDriverState *bs)
385{
386 return bs->inserted;
387}
388
389int bdrv_is_locked(BlockDriverState *bs)
390{
391 return bs->locked;
392}
393
394void bdrv_set_locked(BlockDriverState *bs, int locked)
395{
396 bs->locked = locked;
397}
398
399void bdrv_set_change_cb(BlockDriverState *bs,
400 void (*change_cb)(void *opaque), void *opaque)
401{
402 bs->change_cb = change_cb;
403 bs->change_opaque = opaque;
404}
405
ea2384d3
FB
406int bdrv_is_encrypted(BlockDriverState *bs)
407{
408 if (bs->backing_hd && bs->backing_hd->encrypted)
409 return 1;
410 return bs->encrypted;
411}
412
413int bdrv_set_key(BlockDriverState *bs, const char *key)
414{
415 int ret;
416 if (bs->backing_hd && bs->backing_hd->encrypted) {
417 ret = bdrv_set_key(bs->backing_hd, key);
418 if (ret < 0)
419 return ret;
420 if (!bs->encrypted)
421 return 0;
422 }
423 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
424 return -1;
425 return bs->drv->bdrv_set_key(bs, key);
426}
427
428void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
429{
430 if (!bs->inserted || !bs->drv) {
431 buf[0] = '\0';
432 } else {
433 pstrcpy(buf, buf_size, bs->drv->format_name);
434 }
435}
436
437void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
438 void *opaque)
439{
440 BlockDriver *drv;
441
442 for (drv = first_drv; drv != NULL; drv = drv->next) {
443 it(opaque, drv->format_name);
444 }
445}
446
b338082b
FB
447BlockDriverState *bdrv_find(const char *name)
448{
449 BlockDriverState *bs;
450
451 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
452 if (!strcmp(name, bs->device_name))
453 return bs;
454 }
455 return NULL;
456}
457
81d0912d
FB
458void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
459{
460 BlockDriverState *bs;
461
462 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
463 it(opaque, bs->device_name);
464 }
465}
466
ea2384d3
FB
467const char *bdrv_get_device_name(BlockDriverState *bs)
468{
469 return bs->device_name;
470}
471
b338082b
FB
472void bdrv_info(void)
473{
474 BlockDriverState *bs;
475
476 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
477 term_printf("%s:", bs->device_name);
478 term_printf(" type=");
479 switch(bs->type) {
480 case BDRV_TYPE_HD:
481 term_printf("hd");
482 break;
483 case BDRV_TYPE_CDROM:
484 term_printf("cdrom");
485 break;
486 case BDRV_TYPE_FLOPPY:
487 term_printf("floppy");
488 break;
489 }
490 term_printf(" removable=%d", bs->removable);
491 if (bs->removable) {
492 term_printf(" locked=%d", bs->locked);
493 }
494 if (bs->inserted) {
495 term_printf(" file=%s", bs->filename);
ea2384d3
FB
496 if (bs->backing_file[0] != '\0')
497 term_printf(" backing_file=%s", bs->backing_file);
b338082b 498 term_printf(" ro=%d", bs->read_only);
ea2384d3
FB
499 term_printf(" drv=%s", bs->drv->format_name);
500 if (bs->encrypted)
501 term_printf(" encrypted");
b338082b
FB
502 } else {
503 term_printf(" [not inserted]");
504 }
505 term_printf("\n");
506 }
507}
ea2384d3
FB
508
509
510/**************************************************************/
511/* RAW block driver */
512
513typedef struct BDRVRawState {
514 int fd;
515} BDRVRawState;
516
517static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
518{
519 return 1; /* maybe */
520}
521
522static int raw_open(BlockDriverState *bs, const char *filename)
523{
524 BDRVRawState *s = bs->opaque;
525 int fd;
526 int64_t size;
527
528 fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
529 if (fd < 0) {
530 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
531 if (fd < 0)
532 return -1;
533 bs->read_only = 1;
534 }
d5249393 535 size = lseek(fd, 0, SEEK_END);
c747cd1f
FB
536#ifdef _WIN32
537 /* On Windows hosts it can happen that we're unable to get file size
538 for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
539 if (size == -1)
540 size = LONG_LONG_MAX;
541#endif
ea2384d3
FB
542 bs->total_sectors = size / 512;
543 s->fd = fd;
544 return 0;
545}
546
547static int raw_read(BlockDriverState *bs, int64_t sector_num,
548 uint8_t *buf, int nb_sectors)
549{
550 BDRVRawState *s = bs->opaque;
551 int ret;
552
d5249393 553 lseek(s->fd, sector_num * 512, SEEK_SET);
ea2384d3
FB
554 ret = read(s->fd, buf, nb_sectors * 512);
555 if (ret != nb_sectors * 512)
556 return -1;
557 return 0;
558}
559
560static int raw_write(BlockDriverState *bs, int64_t sector_num,
561 const uint8_t *buf, int nb_sectors)
562{
563 BDRVRawState *s = bs->opaque;
564 int ret;
565
d5249393 566 lseek(s->fd, sector_num * 512, SEEK_SET);
ea2384d3
FB
567 ret = write(s->fd, buf, nb_sectors * 512);
568 if (ret != nb_sectors * 512)
569 return -1;
570 return 0;
571}
572
e2731add 573static void raw_close(BlockDriverState *bs)
ea2384d3
FB
574{
575 BDRVRawState *s = bs->opaque;
576 close(s->fd);
577}
578
579static int raw_create(const char *filename, int64_t total_size,
580 const char *backing_file, int flags)
581{
582 int fd;
583
584 if (flags || backing_file)
585 return -ENOTSUP;
586
587 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
588 0644);
589 if (fd < 0)
590 return -EIO;
d5249393 591 ftruncate(fd, total_size * 512);
ea2384d3
FB
592 close(fd);
593 return 0;
594}
595
596BlockDriver bdrv_raw = {
597 "raw",
598 sizeof(BDRVRawState),
599 raw_probe,
600 raw_open,
601 raw_read,
602 raw_write,
603 raw_close,
604 raw_create,
605};
606
607void bdrv_init(void)
608{
609 bdrv_register(&bdrv_raw);
610#ifndef _WIN32
611 bdrv_register(&bdrv_cow);
612#endif
613 bdrv_register(&bdrv_qcow);
614 bdrv_register(&bdrv_vmdk);
3c56521b 615 bdrv_register(&bdrv_cloop);
585d0ed9 616 bdrv_register(&bdrv_dmg);
a8753c34 617 bdrv_register(&bdrv_bochs);
ea2384d3 618}