block-vpc: Fix disk size (Kevin Wolf)
[qemu] / block-vpc.c
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 "qemu-common.h"
25 #include "block_int.h"
26
27 /**************************************************************/
28
29 #define HEADER_SIZE 512
30
31 //#define CACHE
32
33 enum vhd_type {
34     VHD_FIXED           = 2,
35     VHD_DYNAMIC         = 3,
36     VHD_DIFFERENCING    = 4,
37 };
38
39 // always big-endian
40 struct vhd_footer {
41     char        creator[8]; // "conectix"
42     uint32_t    features;
43     uint32_t    version;
44
45     // Offset of next header structure, 0xFFFFFFFF if none
46     uint64_t    data_offset;
47
48     // Seconds since Jan 1, 2000 0:00:00 (UTC)
49     uint32_t    timestamp;
50
51     char        creator_app[4]; // "vpc "
52     uint16_t    major;
53     uint16_t    minor;
54     char        creator_os[4]; // "Wi2k"
55
56     uint64_t    orig_size;
57     uint64_t    size;
58
59     uint16_t    cyls;
60     uint8_t     heads;
61     uint8_t     secs_per_cyl;
62
63     uint32_t    type;
64
65     // Checksum of the Hard Disk Footer ("one's complement of the sum of all
66     // the bytes in the footer without the checksum field")
67     uint32_t    checksum;
68
69     // UUID used to identify a parent hard disk (backing file)
70     uint8_t     uuid[16];
71
72     uint8_t     in_saved_state;
73 };
74
75 struct vhd_dyndisk_header {
76     char        magic[8]; // "cxsparse"
77
78     // Offset of next header structure, 0xFFFFFFFF if none
79     uint64_t    data_offset;
80
81     // Offset of the Block Allocation Table (BAT)
82     uint64_t    table_offset;
83
84     uint32_t    version;
85     uint32_t    max_table_entries; // 32bit/entry
86
87     // 2 MB by default, must be a power of two
88     uint32_t    block_size;
89
90     uint32_t    checksum;
91     uint8_t     parent_uuid[16];
92     uint32_t    parent_timestamp;
93     uint32_t    reserved;
94
95     // Backing file name (in UTF-16)
96     uint8_t     parent_name[512];
97
98     struct {
99         uint32_t    platform;
100         uint32_t    data_space;
101         uint32_t    data_length;
102         uint32_t    reserved;
103         uint64_t    data_offset;
104     } parent_locator[8];
105 };
106
107 typedef struct BDRVVPCState {
108     int fd;
109
110     int max_table_entries;
111     uint32_t *pagetable;
112
113     uint32_t block_size;
114 #ifdef CACHE
115     uint8_t *pageentry_u8;
116     uint32_t *pageentry_u32;
117     uint16_t *pageentry_u16;
118
119     uint64_t last_bitmap;
120 #endif
121 } BDRVVPCState;
122
123 static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename)
124 {
125     if (buf_size >= 8 && !strncmp((char *)buf, "conectix", 8))
126         return 100;
127     return 0;
128 }
129
130 static int vpc_open(BlockDriverState *bs, const char *filename, int flags)
131 {
132     BDRVVPCState *s = bs->opaque;
133     int fd, i;
134     struct vhd_footer* footer;
135     struct vhd_dyndisk_header* dyndisk_header;
136     uint8_t buf[HEADER_SIZE];
137
138     fd = open(filename, O_RDONLY | O_BINARY);
139     if (fd < 0)
140         return -1;
141
142     bs->read_only = 1; // no write support yet
143
144     s->fd = fd;
145
146     if (read(fd, buf, HEADER_SIZE) != HEADER_SIZE)
147         goto fail;
148
149     footer = (struct vhd_footer*) buf;
150     if (strncmp(footer->creator, "conectix", 8))
151         goto fail;
152
153     // The visible size of a image in Virtual PC depends on the geometry
154     // rather than on the size stored in the footer (the size in the footer
155     // is too large usually)
156     bs->total_sectors = (int64_t)
157         be16_to_cpu(footer->cyls) * footer->heads * footer->secs_per_cyl;
158
159     lseek(s->fd, be64_to_cpu(footer->data_offset), SEEK_SET);
160     if (read(fd, buf, HEADER_SIZE) != HEADER_SIZE)
161         goto fail;
162
163     footer = NULL;
164     dyndisk_header = (struct vhd_dyndisk_header*) buf;
165
166     if (strncmp(dyndisk_header->magic, "cxsparse", 8))
167         goto fail;
168
169     lseek(s->fd, be64_to_cpu(dyndisk_header->table_offset), SEEK_SET);
170
171     s->max_table_entries = be32_to_cpu(dyndisk_header->max_table_entries);
172     s->pagetable = qemu_malloc(s->max_table_entries * 4);
173     if (!s->pagetable)
174         goto fail;
175     if (read(s->fd, s->pagetable, s->max_table_entries * 4) !=
176         s->max_table_entries * 4)
177         goto fail;
178     for (i = 0; i < s->max_table_entries; i++)
179         be32_to_cpus(&s->pagetable[i]);
180
181     s->block_size = be32_to_cpu(dyndisk_header->block_size);
182 #ifdef CACHE
183     s->pageentry_u8 = qemu_malloc(512);
184     if (!s->pageentry_u8)
185         goto fail;
186     s->pageentry_u32 = s->pageentry_u8;
187     s->pageentry_u16 = s->pageentry_u8;
188     s->last_pagetable = -1;
189 #endif
190
191     return 0;
192  fail:
193     close(fd);
194     return -1;
195 }
196
197 static inline int seek_to_sector(BlockDriverState *bs, int64_t sector_num)
198 {
199     BDRVVPCState *s = bs->opaque;
200     uint64_t offset = sector_num * 512;
201     uint64_t bitmap_offset, block_offset;
202     uint32_t pagetable_index, pageentry_index;
203
204     pagetable_index = offset / s->block_size;
205     pageentry_index = (offset % s->block_size) / 512;
206
207     if (pagetable_index > s->max_table_entries || s->pagetable[pagetable_index] == 0xffffffff)
208         return -1; // not allocated
209
210     bitmap_offset = 512 * s->pagetable[pagetable_index];
211     block_offset = bitmap_offset + 512 + (512 * pageentry_index);
212
213 //    printf("sector: %" PRIx64 ", index: %x, offset: %x, bioff: %" PRIx64 ", bloff: %" PRIx64 "\n",
214 //      sector_num, pagetable_index, pageentry_index,
215 //      bitmap_offset, block_offset);
216
217 // disabled by reason
218 #if 0
219 #ifdef CACHE
220     if (bitmap_offset != s->last_bitmap)
221     {
222         lseek(s->fd, bitmap_offset, SEEK_SET);
223
224         s->last_bitmap = bitmap_offset;
225
226         // Scary! Bitmap is stored as big endian 32bit entries,
227         // while we used to look it up byte by byte
228         read(s->fd, s->pageentry_u8, 512);
229         for (i = 0; i < 128; i++)
230             be32_to_cpus(&s->pageentry_u32[i]);
231     }
232
233     if ((s->pageentry_u8[pageentry_index / 8] >> (pageentry_index % 8)) & 1)
234         return -1;
235 #else
236     lseek(s->fd, bitmap_offset + (pageentry_index / 8), SEEK_SET);
237
238     read(s->fd, &bitmap_entry, 1);
239
240     if ((bitmap_entry >> (pageentry_index % 8)) & 1)
241         return -1; // not allocated
242 #endif
243 #endif
244     lseek(s->fd, block_offset, SEEK_SET);
245
246     return 0;
247 }
248
249 static int vpc_read(BlockDriverState *bs, int64_t sector_num,
250                     uint8_t *buf, int nb_sectors)
251 {
252     BDRVVPCState *s = bs->opaque;
253     int ret;
254
255     while (nb_sectors > 0) {
256         if (!seek_to_sector(bs, sector_num))
257         {
258             ret = read(s->fd, buf, 512);
259             if (ret != 512)
260                 return -1;
261         }
262         else
263             memset(buf, 0, 512);
264         nb_sectors--;
265         sector_num++;
266         buf += 512;
267     }
268     return 0;
269 }
270
271 static void vpc_close(BlockDriverState *bs)
272 {
273     BDRVVPCState *s = bs->opaque;
274     qemu_free(s->pagetable);
275 #ifdef CACHE
276     qemu_free(s->pageentry_u8);
277 #endif
278     close(s->fd);
279 }
280
281 BlockDriver bdrv_vpc = {
282     "vpc",
283     sizeof(BDRVVPCState),
284     vpc_probe,
285     vpc_open,
286     vpc_read,
287     NULL,
288     vpc_close,
289 };