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