root / block-vpc.c @ bc2c3909
History | View | Annotate | Download (6.5 kB)
1 |
/*
|
---|---|
2 |
* Block driver for Conectix/Microsoft Virtual PC images
|
3 |
*
|
4 |
* Copyright (c) 2005 Alex Beregszaszi
|
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 "vl.h" |
25 |
#include "block_int.h" |
26 |
|
27 |
/**************************************************************/
|
28 |
|
29 |
#define HEADER_SIZE 512 |
30 |
|
31 |
//#define CACHE
|
32 |
|
33 |
// always big-endian
|
34 |
struct vpc_subheader {
|
35 |
char magic[8]; // "conectix" / "cxsparse" |
36 |
union {
|
37 |
struct {
|
38 |
uint32_t unk1[2];
|
39 |
uint32_t unk2; // always zero?
|
40 |
uint32_t subheader_offset; |
41 |
uint32_t unk3; // some size?
|
42 |
char creator[4]; // "vpc " |
43 |
uint16_t major; |
44 |
uint16_t minor; |
45 |
char guest[4]; // "Wi2k" |
46 |
uint32_t unk4[7];
|
47 |
uint8_t vnet_id[16]; // virtual network id, purpose unknown |
48 |
// next 16 longs are used, but dunno the purpose
|
49 |
// next 6 longs unknown, following 7 long maybe a serial
|
50 |
char padding[HEADER_SIZE - 84]; |
51 |
} main; |
52 |
struct {
|
53 |
uint32_t unk1[2]; // all bits set |
54 |
uint32_t unk2; // always zero?
|
55 |
uint32_t pagetable_offset; |
56 |
uint32_t unk3; |
57 |
uint32_t pagetable_entries; // 32bit/entry
|
58 |
uint32_t pageentry_size; // 512*8*512
|
59 |
uint32_t nb_sectors; |
60 |
char padding[HEADER_SIZE - 40]; |
61 |
} sparse; |
62 |
char padding[HEADER_SIZE - 8]; |
63 |
} type; |
64 |
}; |
65 |
|
66 |
typedef struct BDRVVPCState { |
67 |
int fd;
|
68 |
|
69 |
int pagetable_entries;
|
70 |
uint32_t *pagetable; |
71 |
|
72 |
uint32_t pageentry_size; |
73 |
#ifdef CACHE
|
74 |
uint8_t *pageentry_u8; |
75 |
uint32_t *pageentry_u32; |
76 |
uint16_t *pageentry_u16; |
77 |
|
78 |
uint64_t last_bitmap; |
79 |
#endif
|
80 |
} BDRVVPCState; |
81 |
|
82 |
static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename) |
83 |
{ |
84 |
if (buf_size >= 8 && !strncmp(buf, "conectix", 8)) |
85 |
return 100; |
86 |
return 0; |
87 |
} |
88 |
|
89 |
static int vpc_open(BlockDriverState *bs, const char *filename) |
90 |
{ |
91 |
BDRVVPCState *s = bs->opaque; |
92 |
int fd, i;
|
93 |
struct vpc_subheader header;
|
94 |
|
95 |
fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); |
96 |
if (fd < 0) { |
97 |
fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
98 |
if (fd < 0) |
99 |
return -1; |
100 |
} |
101 |
|
102 |
bs->read_only = 1; // no write support yet |
103 |
|
104 |
s->fd = fd; |
105 |
|
106 |
if (read(fd, &header, HEADER_SIZE) != HEADER_SIZE)
|
107 |
goto fail;
|
108 |
|
109 |
if (strncmp(header.magic, "conectix", 8)) |
110 |
goto fail;
|
111 |
lseek(s->fd, be32_to_cpu(header.type.main.subheader_offset), SEEK_SET); |
112 |
|
113 |
if (read(fd, &header, HEADER_SIZE) != HEADER_SIZE)
|
114 |
goto fail;
|
115 |
|
116 |
if (strncmp(header.magic, "cxsparse", 8)) |
117 |
goto fail;
|
118 |
|
119 |
bs->total_sectors = ((uint64_t)be32_to_cpu(header.type.sparse.pagetable_entries) * |
120 |
be32_to_cpu(header.type.sparse.pageentry_size)) / 512;
|
121 |
|
122 |
lseek(s->fd, be32_to_cpu(header.type.sparse.pagetable_offset), SEEK_SET); |
123 |
|
124 |
s->pagetable_entries = be32_to_cpu(header.type.sparse.pagetable_entries); |
125 |
s->pagetable = qemu_malloc(s->pagetable_entries * 4);
|
126 |
if (!s->pagetable)
|
127 |
goto fail;
|
128 |
if (read(s->fd, s->pagetable, s->pagetable_entries * 4) != |
129 |
s->pagetable_entries * 4)
|
130 |
goto fail;
|
131 |
for (i = 0; i < s->pagetable_entries; i++) |
132 |
be32_to_cpus(&s->pagetable[i]); |
133 |
|
134 |
s->pageentry_size = be32_to_cpu(header.type.sparse.pageentry_size); |
135 |
#ifdef CACHE
|
136 |
s->pageentry_u8 = qemu_malloc(512);
|
137 |
if (!s->pageentry_u8)
|
138 |
goto fail;
|
139 |
s->pageentry_u32 = s->pageentry_u8; |
140 |
s->pageentry_u16 = s->pageentry_u8; |
141 |
s->last_pagetable = -1;
|
142 |
#endif
|
143 |
|
144 |
return 0; |
145 |
fail:
|
146 |
close(fd); |
147 |
return -1; |
148 |
} |
149 |
|
150 |
static inline int seek_to_sector(BlockDriverState *bs, int64_t sector_num) |
151 |
{ |
152 |
BDRVVPCState *s = bs->opaque; |
153 |
uint64_t offset = sector_num * 512;
|
154 |
uint64_t bitmap_offset, block_offset; |
155 |
uint32_t pagetable_index, pageentry_index; |
156 |
|
157 |
pagetable_index = offset / s->pageentry_size; |
158 |
pageentry_index = (offset % s->pageentry_size) / 512;
|
159 |
|
160 |
if (pagetable_index > s->pagetable_entries || s->pagetable[pagetable_index] == 0xffffffff) |
161 |
return -1; // not allocated |
162 |
|
163 |
bitmap_offset = 512 * s->pagetable[pagetable_index];
|
164 |
block_offset = bitmap_offset + 512 + (512 * pageentry_index); |
165 |
|
166 |
// printf("sector: %llx, index: %x, offset: %x, bioff: %llx, bloff: %llx\n",
|
167 |
// sector_num, pagetable_index, pageentry_index,
|
168 |
// bitmap_offset, block_offset);
|
169 |
|
170 |
// disabled by reason
|
171 |
#if 0
|
172 |
#ifdef CACHE
|
173 |
if (bitmap_offset != s->last_bitmap)
|
174 |
{
|
175 |
lseek(s->fd, bitmap_offset, SEEK_SET);
|
176 |
|
177 |
s->last_bitmap = bitmap_offset;
|
178 |
|
179 |
// Scary! Bitmap is stored as big endian 32bit entries,
|
180 |
// while we used to look it up byte by byte
|
181 |
read(s->fd, s->pageentry_u8, 512);
|
182 |
for (i = 0; i < 128; i++)
|
183 |
be32_to_cpus(&s->pageentry_u32[i]);
|
184 |
}
|
185 |
|
186 |
if ((s->pageentry_u8[pageentry_index / 8] >> (pageentry_index % 8)) & 1)
|
187 |
return -1;
|
188 |
#else
|
189 |
lseek(s->fd, bitmap_offset + (pageentry_index / 8), SEEK_SET);
|
190 |
|
191 |
read(s->fd, &bitmap_entry, 1);
|
192 |
|
193 |
if ((bitmap_entry >> (pageentry_index % 8)) & 1) |
194 |
return -1; // not allocated |
195 |
#endif
|
196 |
#endif
|
197 |
lseek(s->fd, block_offset, SEEK_SET); |
198 |
|
199 |
return 0; |
200 |
} |
201 |
|
202 |
static int vpc_read(BlockDriverState *bs, int64_t sector_num, |
203 |
uint8_t *buf, int nb_sectors)
|
204 |
{ |
205 |
BDRVVPCState *s = bs->opaque; |
206 |
int ret;
|
207 |
|
208 |
while (nb_sectors > 0) { |
209 |
if (!seek_to_sector(bs, sector_num))
|
210 |
{ |
211 |
ret = read(s->fd, buf, 512);
|
212 |
if (ret != 512) |
213 |
return -1; |
214 |
} |
215 |
else
|
216 |
memset(buf, 0, 512); |
217 |
nb_sectors--; |
218 |
sector_num++; |
219 |
buf += 512;
|
220 |
} |
221 |
return 0; |
222 |
} |
223 |
|
224 |
static void vpc_close(BlockDriverState *bs) |
225 |
{ |
226 |
BDRVVPCState *s = bs->opaque; |
227 |
qemu_free(s->pagetable); |
228 |
#ifdef CACHE
|
229 |
qemu_free(s->pageentry_u8); |
230 |
#endif
|
231 |
close(s->fd); |
232 |
} |
233 |
|
234 |
BlockDriver bdrv_vpc = { |
235 |
"vpc",
|
236 |
sizeof(BDRVVPCState),
|
237 |
vpc_probe, |
238 |
vpc_open, |
239 |
vpc_read, |
240 |
NULL,
|
241 |
vpc_close, |
242 |
}; |