]> git.proxmox.com Git - qemu.git/blame - block/dmg.c
e1000/rtl8139: update HMP NIC when every bit is written
[qemu.git] / block / dmg.c
CommitLineData
585d0ed9
FB
1/*
2 * QEMU Block driver for DMG images
5fafdf24 3 *
585d0ed9 4 * Copyright (c) 2004 Johannes E. Schindelin
5fafdf24 5 *
585d0ed9
FB
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 */
faf07963 24#include "qemu-common.h"
737e150e 25#include "block/block_int.h"
1de7afc9
PB
26#include "qemu/bswap.h"
27#include "qemu/module.h"
585d0ed9
FB
28#include <zlib.h>
29
30typedef struct BDRVDMGState {
848c66e8 31 CoMutex lock;
585d0ed9
FB
32 /* each chunk contains a certain number of sectors,
33 * offsets[i] is the offset in the .dmg file,
34 * lengths[i] is the length of the compressed chunk,
35 * sectors[i] is the sector beginning at offsets[i],
36 * sectorcounts[i] is the number of sectors in that chunk,
37 * the sectors array is ordered
38 * 0<=i<n_chunks */
39
40 uint32_t n_chunks;
41 uint32_t* types;
42 uint64_t* offsets;
43 uint64_t* lengths;
44 uint64_t* sectors;
45 uint64_t* sectorcounts;
46 uint32_t current_chunk;
28a5c9c8
FB
47 uint8_t *compressed_chunk;
48 uint8_t *uncompressed_chunk;
585d0ed9
FB
49 z_stream zstream;
50} BDRVDMGState;
51
52static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
53{
f5866fa4
KW
54 int len;
55
56 if (!filename) {
57 return 0;
58 }
59
60 len = strlen(filename);
61 if (len > 4 && !strcmp(filename + len - 4, ".dmg")) {
62 return 2;
63 }
585d0ed9
FB
64 return 0;
65}
66
69d34a36 67static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
585d0ed9 68{
69d34a36
KW
69 uint64_t buffer;
70 int ret;
71
72 ret = bdrv_pread(bs->file, offset, &buffer, 8);
73 if (ret < 0) {
74 return ret;
75 }
76
77 *result = be64_to_cpu(buffer);
78 return 0;
585d0ed9
FB
79}
80
69d34a36 81static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
585d0ed9 82{
69d34a36
KW
83 uint32_t buffer;
84 int ret;
85
86 ret = bdrv_pread(bs->file, offset, &buffer, 4);
87 if (ret < 0) {
88 return ret;
89 }
90
91 *result = be32_to_cpu(buffer);
92 return 0;
585d0ed9
FB
93}
94
015a1036
MR
95static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
96 Error **errp)
585d0ed9
FB
97{
98 BDRVDMGState *s = bs->opaque;
69d34a36
KW
99 uint64_t info_begin,info_end,last_in_offset,last_out_offset;
100 uint32_t count, tmp;
585d0ed9 101 uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
16cdf7ce 102 int64_t offset;
69d34a36 103 int ret;
585d0ed9 104
585d0ed9
FB
105 bs->read_only = 1;
106 s->n_chunks = 0;
511d2b14 107 s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
3b46e624 108
585d0ed9 109 /* read offset of info blocks */
64a31d5c 110 offset = bdrv_getlength(bs->file);
16cdf7ce 111 if (offset < 0) {
69d34a36 112 ret = offset;
1559ca00 113 goto fail;
585d0ed9 114 }
64a31d5c 115 offset -= 0x1d8;
1559ca00 116
69d34a36
KW
117 ret = read_uint64(bs, offset, &info_begin);
118 if (ret < 0) {
119 goto fail;
120 } else if (info_begin == 0) {
121 ret = -EINVAL;
122 goto fail;
16cdf7ce
CH
123 }
124
69d34a36
KW
125 ret = read_uint32(bs, info_begin, &tmp);
126 if (ret < 0) {
127 goto fail;
128 } else if (tmp != 0x100) {
129 ret = -EINVAL;
16cdf7ce
CH
130 goto fail;
131 }
132
69d34a36
KW
133 ret = read_uint32(bs, info_begin + 4, &count);
134 if (ret < 0) {
135 goto fail;
136 } else if (count == 0) {
137 ret = -EINVAL;
16cdf7ce
CH
138 goto fail;
139 }
140 info_end = info_begin + count;
141
142 offset = info_begin + 0x100;
585d0ed9
FB
143
144 /* read offsets */
145 last_in_offset = last_out_offset = 0;
16cdf7ce 146 while (offset < info_end) {
995179f1
FB
147 uint32_t type;
148
69d34a36
KW
149 ret = read_uint32(bs, offset, &count);
150 if (ret < 0) {
151 goto fail;
152 } else if (count == 0) {
153 ret = -EINVAL;
154 goto fail;
155 }
16cdf7ce
CH
156 offset += 4;
157
69d34a36
KW
158 ret = read_uint32(bs, offset, &type);
159 if (ret < 0) {
160 goto fail;
161 }
162
16cdf7ce 163 if (type == 0x6d697368 && count >= 244) {
585d0ed9 164 int new_size, chunk_count;
16cdf7ce
CH
165
166 offset += 4;
167 offset += 200;
168
585d0ed9
FB
169 chunk_count = (count-204)/40;
170 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
7267c094
AL
171 s->types = g_realloc(s->types, new_size/2);
172 s->offsets = g_realloc(s->offsets, new_size);
173 s->lengths = g_realloc(s->lengths, new_size);
174 s->sectors = g_realloc(s->sectors, new_size);
175 s->sectorcounts = g_realloc(s->sectorcounts, new_size);
585d0ed9 176
69d34a36
KW
177 for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
178 ret = read_uint32(bs, offset, &s->types[i]);
179 if (ret < 0) {
180 goto fail;
181 }
16cdf7ce 182 offset += 4;
585d0ed9
FB
183 if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
184 if(s->types[i]==0xffffffff) {
185 last_in_offset = s->offsets[i-1]+s->lengths[i-1];
186 last_out_offset = s->sectors[i-1]+s->sectorcounts[i-1];
187 }
188 chunk_count--;
189 i--;
16cdf7ce 190 offset += 36;
585d0ed9
FB
191 continue;
192 }
16cdf7ce
CH
193 offset += 4;
194
69d34a36
KW
195 ret = read_uint64(bs, offset, &s->sectors[i]);
196 if (ret < 0) {
197 goto fail;
198 }
199 s->sectors[i] += last_out_offset;
200 offset += 8;
16cdf7ce 201
69d34a36
KW
202 ret = read_uint64(bs, offset, &s->sectorcounts[i]);
203 if (ret < 0) {
204 goto fail;
205 }
206 offset += 8;
16cdf7ce 207
69d34a36
KW
208 ret = read_uint64(bs, offset, &s->offsets[i]);
209 if (ret < 0) {
210 goto fail;
211 }
212 s->offsets[i] += last_in_offset;
213 offset += 8;
16cdf7ce 214
69d34a36
KW
215 ret = read_uint64(bs, offset, &s->lengths[i]);
216 if (ret < 0) {
217 goto fail;
218 }
219 offset += 8;
16cdf7ce 220
585d0ed9
FB
221 if(s->lengths[i]>max_compressed_size)
222 max_compressed_size = s->lengths[i];
223 if(s->sectorcounts[i]>max_sectors_per_chunk)
224 max_sectors_per_chunk = s->sectorcounts[i];
225 }
226 s->n_chunks+=chunk_count;
227 }
228 }
229
230 /* initialize zlib engine */
7267c094
AL
231 s->compressed_chunk = g_malloc(max_compressed_size+1);
232 s->uncompressed_chunk = g_malloc(512*max_sectors_per_chunk);
69d34a36
KW
233 if(inflateInit(&s->zstream) != Z_OK) {
234 ret = -EINVAL;
235 goto fail;
236 }
585d0ed9
FB
237
238 s->current_chunk = s->n_chunks;
3b46e624 239
848c66e8 240 qemu_co_mutex_init(&s->lock);
585d0ed9 241 return 0;
69d34a36 242
1559ca00 243fail:
69d34a36
KW
244 g_free(s->types);
245 g_free(s->offsets);
246 g_free(s->lengths);
247 g_free(s->sectors);
248 g_free(s->sectorcounts);
249 g_free(s->compressed_chunk);
250 g_free(s->uncompressed_chunk);
251 return ret;
585d0ed9
FB
252}
253
254static inline int is_sector_in_chunk(BDRVDMGState* s,
255 uint32_t chunk_num,int sector_num)
256{
257 if(chunk_num>=s->n_chunks || s->sectors[chunk_num]>sector_num ||
258 s->sectors[chunk_num]+s->sectorcounts[chunk_num]<=sector_num)
259 return 0;
260 else
261 return -1;
262}
263
264static inline uint32_t search_chunk(BDRVDMGState* s,int sector_num)
265{
266 /* binary search */
267 uint32_t chunk1=0,chunk2=s->n_chunks,chunk3;
268 while(chunk1!=chunk2) {
269 chunk3 = (chunk1+chunk2)/2;
270 if(s->sectors[chunk3]>sector_num)
271 chunk2 = chunk3;
272 else if(s->sectors[chunk3]+s->sectorcounts[chunk3]>sector_num)
273 return chunk3;
274 else
275 chunk1 = chunk3;
276 }
277 return s->n_chunks; /* error */
278}
279
64a31d5c 280static inline int dmg_read_chunk(BlockDriverState *bs, int sector_num)
585d0ed9 281{
64a31d5c
CH
282 BDRVDMGState *s = bs->opaque;
283
585d0ed9
FB
284 if(!is_sector_in_chunk(s,s->current_chunk,sector_num)) {
285 int ret;
286 uint32_t chunk = search_chunk(s,sector_num);
287
288 if(chunk>=s->n_chunks)
289 return -1;
290
291 s->current_chunk = s->n_chunks;
292 switch(s->types[chunk]) {
293 case 0x80000005: { /* zlib compressed */
294 int i;
295
585d0ed9
FB
296 /* we need to buffer, because only the chunk as whole can be
297 * inflated. */
298 i=0;
299 do {
64a31d5c
CH
300 ret = bdrv_pread(bs->file, s->offsets[chunk] + i,
301 s->compressed_chunk+i, s->lengths[chunk]-i);
585d0ed9
FB
302 if(ret<0 && errno==EINTR)
303 ret=0;
304 i+=ret;
305 } while(ret>=0 && ret+i<s->lengths[chunk]);
306
307 if (ret != s->lengths[chunk])
308 return -1;
5fafdf24 309
585d0ed9
FB
310 s->zstream.next_in = s->compressed_chunk;
311 s->zstream.avail_in = s->lengths[chunk];
312 s->zstream.next_out = s->uncompressed_chunk;
313 s->zstream.avail_out = 512*s->sectorcounts[chunk];
314 ret = inflateReset(&s->zstream);
315 if(ret != Z_OK)
316 return -1;
317 ret = inflate(&s->zstream, Z_FINISH);
318 if(ret != Z_STREAM_END || s->zstream.total_out != 512*s->sectorcounts[chunk])
319 return -1;
320 break; }
321 case 1: /* copy */
64a31d5c
CH
322 ret = bdrv_pread(bs->file, s->offsets[chunk],
323 s->uncompressed_chunk, s->lengths[chunk]);
585d0ed9
FB
324 if (ret != s->lengths[chunk])
325 return -1;
326 break;
327 case 2: /* zero */
328 memset(s->uncompressed_chunk, 0, 512*s->sectorcounts[chunk]);
329 break;
330 }
331 s->current_chunk = chunk;
332 }
333 return 0;
334}
335
5fafdf24 336static int dmg_read(BlockDriverState *bs, int64_t sector_num,
585d0ed9
FB
337 uint8_t *buf, int nb_sectors)
338{
339 BDRVDMGState *s = bs->opaque;
340 int i;
341
342 for(i=0;i<nb_sectors;i++) {
343 uint32_t sector_offset_in_chunk;
64a31d5c 344 if(dmg_read_chunk(bs, sector_num+i) != 0)
585d0ed9
FB
345 return -1;
346 sector_offset_in_chunk = sector_num+i-s->sectors[s->current_chunk];
347 memcpy(buf+i*512,s->uncompressed_chunk+sector_offset_in_chunk*512,512);
348 }
349 return 0;
350}
351
2914caa0
PB
352static coroutine_fn int dmg_co_read(BlockDriverState *bs, int64_t sector_num,
353 uint8_t *buf, int nb_sectors)
354{
355 int ret;
356 BDRVDMGState *s = bs->opaque;
357 qemu_co_mutex_lock(&s->lock);
358 ret = dmg_read(bs, sector_num, buf, nb_sectors);
359 qemu_co_mutex_unlock(&s->lock);
360 return ret;
361}
362
585d0ed9
FB
363static void dmg_close(BlockDriverState *bs)
364{
365 BDRVDMGState *s = bs->opaque;
4f8aa2e1
KW
366
367 g_free(s->types);
368 g_free(s->offsets);
369 g_free(s->lengths);
370 g_free(s->sectors);
371 g_free(s->sectorcounts);
372 g_free(s->compressed_chunk);
373 g_free(s->uncompressed_chunk);
374
585d0ed9
FB
375 inflateEnd(&s->zstream);
376}
377
5efa9d5a 378static BlockDriver bdrv_dmg = {
e60f469c
AJ
379 .format_name = "dmg",
380 .instance_size = sizeof(BDRVDMGState),
381 .bdrv_probe = dmg_probe,
64a31d5c 382 .bdrv_open = dmg_open,
2914caa0 383 .bdrv_read = dmg_co_read,
e60f469c 384 .bdrv_close = dmg_close,
585d0ed9 385};
5efa9d5a
AL
386
387static void bdrv_dmg_init(void)
388{
389 bdrv_register(&bdrv_dmg);
390}
391
392block_init(bdrv_dmg_init);