2 * Block driver for the various disk image formats used by Bochs
3 * Currently only for "growing" type in read-only mode
5 * Copyright (c) 2005 Alex Beregszaszi
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "block_int.h"
28 /**************************************************************/
30 #define HEADER_MAGIC "Bochs Virtual HD Image"
31 #define HEADER_VERSION 0x00010000
32 #define HEADER_SIZE 512
34 #define REDOLOG_TYPE "Redolog"
35 #define GROWING_TYPE "Growing"
37 // not allocated: 0xffffffff
39 // always little-endian
41 char magic
[32]; // "Bochs Virtual HD Image"
42 char type
[16]; // "Redolog"
43 char subtype
[16]; // "Undoable" / "Volatile" / "Growing"
45 uint32_t header
; // size of header
49 uint32_t catalog
; // num of entries
50 uint32_t bitmap
; // bitmap size
51 uint32_t extent
; // extent size
52 uint64_t disk
; // disk size
53 char padding
[HEADER_SIZE
- 64 - 8 - 20];
55 char padding
[HEADER_SIZE
- 64 - 8];
59 typedef struct BDRVBochsState
{
62 uint32_t *catalog_bitmap
;
72 static int bochs_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
74 const struct bochs_header
*bochs
= (const void *)buf
;
76 if (buf_size
< HEADER_SIZE
)
79 if (!strcmp(bochs
->magic
, HEADER_MAGIC
) &&
80 !strcmp(bochs
->type
, REDOLOG_TYPE
) &&
81 !strcmp(bochs
->subtype
, GROWING_TYPE
) &&
82 (le32_to_cpu(bochs
->version
) == HEADER_VERSION
))
88 static int bochs_open(BlockDriverState
*bs
, const char *filename
)
90 BDRVBochsState
*s
= bs
->opaque
;
92 struct bochs_header bochs
;
94 fd
= open(filename
, O_RDWR
| O_BINARY
| O_LARGEFILE
);
96 fd
= open(filename
, O_RDONLY
| O_BINARY
| O_LARGEFILE
);
101 bs
->read_only
= 1; // no write support yet
105 if (read(fd
, &bochs
, sizeof(bochs
)) != sizeof(bochs
)) {
109 if (strcmp(bochs
.magic
, HEADER_MAGIC
) ||
110 strcmp(bochs
.type
, REDOLOG_TYPE
) ||
111 strcmp(bochs
.subtype
, GROWING_TYPE
) ||
112 (le32_to_cpu(bochs
.version
) != HEADER_VERSION
)) {
116 bs
->total_sectors
= le64_to_cpu(bochs
.extra
.redolog
.disk
) / 512;
118 lseek(s
->fd
, le32_to_cpu(bochs
.header
), SEEK_SET
);
120 s
->catalog_size
= le32_to_cpu(bochs
.extra
.redolog
.catalog
);
121 s
->catalog_bitmap
= qemu_malloc(s
->catalog_size
* 4);
122 if (!s
->catalog_bitmap
)
124 if (read(s
->fd
, s
->catalog_bitmap
, s
->catalog_size
* 4) !=
127 for (i
= 0; i
< s
->catalog_size
; i
++)
128 le32_to_cpus(&s
->catalog_bitmap
[i
]);
130 s
->data_offset
= le32_to_cpu(bochs
.header
) + (s
->catalog_size
* 4);
132 s
->bitmap_blocks
= 1 + (le32_to_cpu(bochs
.extra
.redolog
.bitmap
) - 1) / 512;
133 s
->extent_blocks
= 1 + (le32_to_cpu(bochs
.extra
.redolog
.extent
) - 1) / 512;
135 s
->extent_size
= le32_to_cpu(bochs
.extra
.redolog
.extent
);
143 static inline int seek_to_sector(BlockDriverState
*bs
, int64_t sector_num
)
145 BDRVBochsState
*s
= bs
->opaque
;
146 int64_t offset
= sector_num
* 512;
147 int64_t extent_index
, extent_offset
, bitmap_offset
, block_offset
;
151 extent_index
= offset
/ s
->extent_size
;
152 extent_offset
= (offset
% s
->extent_size
) / 512;
154 if (s
->catalog_bitmap
[extent_index
] == 0xffffffff)
156 // fprintf(stderr, "page not allocated [%x - %x:%x]\n",
157 // sector_num, extent_index, extent_offset);
158 return -1; // not allocated
161 bitmap_offset
= s
->data_offset
+ (512 * s
->catalog_bitmap
[extent_index
] *
162 (s
->extent_blocks
+ s
->bitmap_blocks
));
163 block_offset
= bitmap_offset
+ (512 * (s
->bitmap_blocks
+ extent_offset
));
165 // fprintf(stderr, "sect: %x [ext i: %x o: %x] -> %x bitmap: %x block: %x\n",
166 // sector_num, extent_index, extent_offset,
167 // le32_to_cpu(s->catalog_bitmap[extent_index]),
168 // bitmap_offset, block_offset);
170 // read in bitmap for current extent
171 lseek(s
->fd
, bitmap_offset
+ (extent_offset
/ 8), SEEK_SET
);
173 read(s
->fd
, &bitmap_entry
, 1);
175 if (!((bitmap_entry
>> (extent_offset
% 8)) & 1))
177 // fprintf(stderr, "sector (%x) in bitmap not allocated\n",
179 return -1; // not allocated
182 lseek(s
->fd
, block_offset
, SEEK_SET
);
187 static int bochs_read(BlockDriverState
*bs
, int64_t sector_num
,
188 uint8_t *buf
, int nb_sectors
)
190 BDRVBochsState
*s
= bs
->opaque
;
193 while (nb_sectors
> 0) {
194 if (!seek_to_sector(bs
, sector_num
))
196 ret
= read(s
->fd
, buf
, 512);
209 static void bochs_close(BlockDriverState
*bs
)
211 BDRVBochsState
*s
= bs
->opaque
;
212 qemu_free(s
->catalog_bitmap
);
216 BlockDriver bdrv_bochs
= {
218 sizeof(BDRVBochsState
),