]> git.proxmox.com Git - mirror_qemu.git/blob - block/dmg.c
dmg: including dmg-lzfse module inside dmg block driver.
[mirror_qemu.git] / block / dmg.c
1 /*
2 * QEMU Block driver for DMG images
3 *
4 * Copyright (c) 2004 Johannes E. Schindelin
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/osdep.h"
25 #include "qapi/error.h"
26 #include "qemu-common.h"
27 #include "block/block_int.h"
28 #include "qemu/bswap.h"
29 #include "qemu/error-report.h"
30 #include "qemu/module.h"
31 #include "dmg.h"
32
33 int (*dmg_uncompress_bz2)(char *next_in, unsigned int avail_in,
34 char *next_out, unsigned int avail_out);
35
36 int (*dmg_uncompress_lzfse)(char *next_in, unsigned int avail_in,
37 char *next_out, unsigned int avail_out);
38
39 enum {
40 /* Limit chunk sizes to prevent unreasonable amounts of memory being used
41 * or truncating when converting to 32-bit types
42 */
43 DMG_LENGTHS_MAX = 64 * 1024 * 1024, /* 64 MB */
44 DMG_SECTORCOUNTS_MAX = DMG_LENGTHS_MAX / 512,
45 };
46
47 static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
48 {
49 int len;
50
51 if (!filename) {
52 return 0;
53 }
54
55 len = strlen(filename);
56 if (len > 4 && !strcmp(filename + len - 4, ".dmg")) {
57 return 2;
58 }
59 return 0;
60 }
61
62 static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
63 {
64 uint64_t buffer;
65 int ret;
66
67 ret = bdrv_pread(bs->file, offset, &buffer, 8);
68 if (ret < 0) {
69 return ret;
70 }
71
72 *result = be64_to_cpu(buffer);
73 return 0;
74 }
75
76 static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
77 {
78 uint32_t buffer;
79 int ret;
80
81 ret = bdrv_pread(bs->file, offset, &buffer, 4);
82 if (ret < 0) {
83 return ret;
84 }
85
86 *result = be32_to_cpu(buffer);
87 return 0;
88 }
89
90 static inline uint64_t buff_read_uint64(const uint8_t *buffer, int64_t offset)
91 {
92 return be64_to_cpu(*(uint64_t *)&buffer[offset]);
93 }
94
95 static inline uint32_t buff_read_uint32(const uint8_t *buffer, int64_t offset)
96 {
97 return be32_to_cpu(*(uint32_t *)&buffer[offset]);
98 }
99
100 /* Increase max chunk sizes, if necessary. This function is used to calculate
101 * the buffer sizes needed for compressed/uncompressed chunk I/O.
102 */
103 static void update_max_chunk_size(BDRVDMGState *s, uint32_t chunk,
104 uint32_t *max_compressed_size,
105 uint32_t *max_sectors_per_chunk)
106 {
107 uint32_t compressed_size = 0;
108 uint32_t uncompressed_sectors = 0;
109
110 switch (s->types[chunk]) {
111 case 0x80000005: /* zlib compressed */
112 case 0x80000006: /* bzip2 compressed */
113 case 0x80000007: /* lzfse compressed */
114 compressed_size = s->lengths[chunk];
115 uncompressed_sectors = s->sectorcounts[chunk];
116 break;
117 case 1: /* copy */
118 uncompressed_sectors = DIV_ROUND_UP(s->lengths[chunk], 512);
119 break;
120 case 2: /* zero */
121 /* as the all-zeroes block may be large, it is treated specially: the
122 * sector is not copied from a large buffer, a simple memset is used
123 * instead. Therefore uncompressed_sectors does not need to be set. */
124 break;
125 }
126
127 if (compressed_size > *max_compressed_size) {
128 *max_compressed_size = compressed_size;
129 }
130 if (uncompressed_sectors > *max_sectors_per_chunk) {
131 *max_sectors_per_chunk = uncompressed_sectors;
132 }
133 }
134
135 static int64_t dmg_find_koly_offset(BdrvChild *file, Error **errp)
136 {
137 BlockDriverState *file_bs = file->bs;
138 int64_t length;
139 int64_t offset = 0;
140 uint8_t buffer[515];
141 int i, ret;
142
143 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
144 * dmg images can have odd sizes, try to look for the "koly" magic which
145 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
146 * in the last 511 bytes of the second-last sector or the first 4 bytes of
147 * the last sector (search space: 515 bytes) */
148 length = bdrv_getlength(file_bs);
149 if (length < 0) {
150 error_setg_errno(errp, -length,
151 "Failed to get file size while reading UDIF trailer");
152 return length;
153 } else if (length < 512) {
154 error_setg(errp, "dmg file must be at least 512 bytes long");
155 return -EINVAL;
156 }
157 if (length > 511 + 512) {
158 offset = length - 511 - 512;
159 }
160 length = length < 515 ? length : 515;
161 ret = bdrv_pread(file, offset, buffer, length);
162 if (ret < 0) {
163 error_setg_errno(errp, -ret, "Failed while reading UDIF trailer");
164 return ret;
165 }
166 for (i = 0; i < length - 3; i++) {
167 if (buffer[i] == 'k' && buffer[i+1] == 'o' &&
168 buffer[i+2] == 'l' && buffer[i+3] == 'y') {
169 return offset + i;
170 }
171 }
172 error_setg(errp, "Could not locate UDIF trailer in dmg file");
173 return -EINVAL;
174 }
175
176 /* used when building the sector table */
177 typedef struct DmgHeaderState {
178 /* used internally by dmg_read_mish_block to remember offsets of blocks
179 * across calls */
180 uint64_t data_fork_offset;
181 /* exported for dmg_open */
182 uint32_t max_compressed_size;
183 uint32_t max_sectors_per_chunk;
184 } DmgHeaderState;
185
186 static bool dmg_is_known_block_type(uint32_t entry_type)
187 {
188 switch (entry_type) {
189 case 0x00000001: /* uncompressed */
190 case 0x00000002: /* zeroes */
191 case 0x80000005: /* zlib */
192 return true;
193 case 0x80000006: /* bzip2 */
194 return !!dmg_uncompress_bz2;
195 case 0x80000007: /* lzfse */
196 return !!dmg_uncompress_lzfse;
197 default:
198 return false;
199 }
200 }
201
202 static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,
203 uint8_t *buffer, uint32_t count)
204 {
205 uint32_t type, i;
206 int ret;
207 size_t new_size;
208 uint32_t chunk_count;
209 int64_t offset = 0;
210 uint64_t data_offset;
211 uint64_t in_offset = ds->data_fork_offset;
212 uint64_t out_offset;
213
214 type = buff_read_uint32(buffer, offset);
215 /* skip data that is not a valid MISH block (invalid magic or too small) */
216 if (type != 0x6d697368 || count < 244) {
217 /* assume success for now */
218 return 0;
219 }
220
221 /* chunk offsets are relative to this sector number */
222 out_offset = buff_read_uint64(buffer, offset + 8);
223
224 /* location in data fork for (compressed) blob (in bytes) */
225 data_offset = buff_read_uint64(buffer, offset + 0x18);
226 in_offset += data_offset;
227
228 /* move to begin of chunk entries */
229 offset += 204;
230
231 chunk_count = (count - 204) / 40;
232 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
233 s->types = g_realloc(s->types, new_size / 2);
234 s->offsets = g_realloc(s->offsets, new_size);
235 s->lengths = g_realloc(s->lengths, new_size);
236 s->sectors = g_realloc(s->sectors, new_size);
237 s->sectorcounts = g_realloc(s->sectorcounts, new_size);
238
239 for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
240 s->types[i] = buff_read_uint32(buffer, offset);
241 if (!dmg_is_known_block_type(s->types[i])) {
242 chunk_count--;
243 i--;
244 offset += 40;
245 continue;
246 }
247
248 /* sector number */
249 s->sectors[i] = buff_read_uint64(buffer, offset + 8);
250 s->sectors[i] += out_offset;
251
252 /* sector count */
253 s->sectorcounts[i] = buff_read_uint64(buffer, offset + 0x10);
254
255 /* all-zeroes sector (type 2) does not need to be "uncompressed" and can
256 * therefore be unbounded. */
257 if (s->types[i] != 2 && s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
258 error_report("sector count %" PRIu64 " for chunk %" PRIu32
259 " is larger than max (%u)",
260 s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
261 ret = -EINVAL;
262 goto fail;
263 }
264
265 /* offset in (compressed) data fork */
266 s->offsets[i] = buff_read_uint64(buffer, offset + 0x18);
267 s->offsets[i] += in_offset;
268
269 /* length in (compressed) data fork */
270 s->lengths[i] = buff_read_uint64(buffer, offset + 0x20);
271
272 if (s->lengths[i] > DMG_LENGTHS_MAX) {
273 error_report("length %" PRIu64 " for chunk %" PRIu32
274 " is larger than max (%u)",
275 s->lengths[i], i, DMG_LENGTHS_MAX);
276 ret = -EINVAL;
277 goto fail;
278 }
279
280 update_max_chunk_size(s, i, &ds->max_compressed_size,
281 &ds->max_sectors_per_chunk);
282 offset += 40;
283 }
284 s->n_chunks += chunk_count;
285 return 0;
286
287 fail:
288 return ret;
289 }
290
291 static int dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds,
292 uint64_t info_begin, uint64_t info_length)
293 {
294 BDRVDMGState *s = bs->opaque;
295 int ret;
296 uint32_t count, rsrc_data_offset;
297 uint8_t *buffer = NULL;
298 uint64_t info_end;
299 uint64_t offset;
300
301 /* read offset from begin of resource fork (info_begin) to resource data */
302 ret = read_uint32(bs, info_begin, &rsrc_data_offset);
303 if (ret < 0) {
304 goto fail;
305 } else if (rsrc_data_offset > info_length) {
306 ret = -EINVAL;
307 goto fail;
308 }
309
310 /* read length of resource data */
311 ret = read_uint32(bs, info_begin + 8, &count);
312 if (ret < 0) {
313 goto fail;
314 } else if (count == 0 || rsrc_data_offset + count > info_length) {
315 ret = -EINVAL;
316 goto fail;
317 }
318
319 /* begin of resource data (consisting of one or more resources) */
320 offset = info_begin + rsrc_data_offset;
321
322 /* end of resource data (there is possibly a following resource map
323 * which will be ignored). */
324 info_end = offset + count;
325
326 /* read offsets (mish blocks) from one or more resources in resource data */
327 while (offset < info_end) {
328 /* size of following resource */
329 ret = read_uint32(bs, offset, &count);
330 if (ret < 0) {
331 goto fail;
332 } else if (count == 0 || count > info_end - offset) {
333 ret = -EINVAL;
334 goto fail;
335 }
336 offset += 4;
337
338 buffer = g_realloc(buffer, count);
339 ret = bdrv_pread(bs->file, offset, buffer, count);
340 if (ret < 0) {
341 goto fail;
342 }
343
344 ret = dmg_read_mish_block(s, ds, buffer, count);
345 if (ret < 0) {
346 goto fail;
347 }
348 /* advance offset by size of resource */
349 offset += count;
350 }
351 ret = 0;
352
353 fail:
354 g_free(buffer);
355 return ret;
356 }
357
358 static int dmg_read_plist_xml(BlockDriverState *bs, DmgHeaderState *ds,
359 uint64_t info_begin, uint64_t info_length)
360 {
361 BDRVDMGState *s = bs->opaque;
362 int ret;
363 uint8_t *buffer = NULL;
364 char *data_begin, *data_end;
365
366 /* Have at least some length to avoid NULL for g_malloc. Attempt to set a
367 * safe upper cap on the data length. A test sample had a XML length of
368 * about 1 MiB. */
369 if (info_length == 0 || info_length > 16 * 1024 * 1024) {
370 ret = -EINVAL;
371 goto fail;
372 }
373
374 buffer = g_malloc(info_length + 1);
375 buffer[info_length] = '\0';
376 ret = bdrv_pread(bs->file, info_begin, buffer, info_length);
377 if (ret != info_length) {
378 ret = -EINVAL;
379 goto fail;
380 }
381
382 /* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
383 * decode. The actual data element has 431 (0x1af) bytes which includes tabs
384 * and line feeds. */
385 data_end = (char *)buffer;
386 while ((data_begin = strstr(data_end, "<data>")) != NULL) {
387 guchar *mish;
388 gsize out_len = 0;
389
390 data_begin += 6;
391 data_end = strstr(data_begin, "</data>");
392 /* malformed XML? */
393 if (data_end == NULL) {
394 ret = -EINVAL;
395 goto fail;
396 }
397 *data_end++ = '\0';
398 mish = g_base64_decode(data_begin, &out_len);
399 ret = dmg_read_mish_block(s, ds, mish, (uint32_t)out_len);
400 g_free(mish);
401 if (ret < 0) {
402 goto fail;
403 }
404 }
405 ret = 0;
406
407 fail:
408 g_free(buffer);
409 return ret;
410 }
411
412 static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
413 Error **errp)
414 {
415 BDRVDMGState *s = bs->opaque;
416 DmgHeaderState ds;
417 uint64_t rsrc_fork_offset, rsrc_fork_length;
418 uint64_t plist_xml_offset, plist_xml_length;
419 int64_t offset;
420 int ret;
421
422 ret = bdrv_apply_auto_read_only(bs, NULL, errp);
423 if (ret < 0) {
424 return ret;
425 }
426
427 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
428 false, errp);
429 if (!bs->file) {
430 return -EINVAL;
431 }
432
433 block_module_load_one("dmg-bz2");
434 block_module_load_one("dmg-lzfse");
435
436 s->n_chunks = 0;
437 s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
438 /* used by dmg_read_mish_block to keep track of the current I/O position */
439 ds.data_fork_offset = 0;
440 ds.max_compressed_size = 1;
441 ds.max_sectors_per_chunk = 1;
442
443 /* locate the UDIF trailer */
444 offset = dmg_find_koly_offset(bs->file, errp);
445 if (offset < 0) {
446 ret = offset;
447 goto fail;
448 }
449
450 /* offset of data fork (DataForkOffset) */
451 ret = read_uint64(bs, offset + 0x18, &ds.data_fork_offset);
452 if (ret < 0) {
453 goto fail;
454 } else if (ds.data_fork_offset > offset) {
455 ret = -EINVAL;
456 goto fail;
457 }
458
459 /* offset of resource fork (RsrcForkOffset) */
460 ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
461 if (ret < 0) {
462 goto fail;
463 }
464 ret = read_uint64(bs, offset + 0x30, &rsrc_fork_length);
465 if (ret < 0) {
466 goto fail;
467 }
468 if (rsrc_fork_offset >= offset ||
469 rsrc_fork_length > offset - rsrc_fork_offset) {
470 ret = -EINVAL;
471 goto fail;
472 }
473 /* offset of property list (XMLOffset) */
474 ret = read_uint64(bs, offset + 0xd8, &plist_xml_offset);
475 if (ret < 0) {
476 goto fail;
477 }
478 ret = read_uint64(bs, offset + 0xe0, &plist_xml_length);
479 if (ret < 0) {
480 goto fail;
481 }
482 if (plist_xml_offset >= offset ||
483 plist_xml_length > offset - plist_xml_offset) {
484 ret = -EINVAL;
485 goto fail;
486 }
487 ret = read_uint64(bs, offset + 0x1ec, (uint64_t *)&bs->total_sectors);
488 if (ret < 0) {
489 goto fail;
490 }
491 if (bs->total_sectors < 0) {
492 ret = -EINVAL;
493 goto fail;
494 }
495 if (rsrc_fork_length != 0) {
496 ret = dmg_read_resource_fork(bs, &ds,
497 rsrc_fork_offset, rsrc_fork_length);
498 if (ret < 0) {
499 goto fail;
500 }
501 } else if (plist_xml_length != 0) {
502 ret = dmg_read_plist_xml(bs, &ds, plist_xml_offset, plist_xml_length);
503 if (ret < 0) {
504 goto fail;
505 }
506 } else {
507 ret = -EINVAL;
508 goto fail;
509 }
510
511 /* initialize zlib engine */
512 s->compressed_chunk = qemu_try_blockalign(bs->file->bs,
513 ds.max_compressed_size + 1);
514 s->uncompressed_chunk = qemu_try_blockalign(bs->file->bs,
515 512 * ds.max_sectors_per_chunk);
516 if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
517 ret = -ENOMEM;
518 goto fail;
519 }
520
521 if (inflateInit(&s->zstream) != Z_OK) {
522 ret = -EINVAL;
523 goto fail;
524 }
525
526 s->current_chunk = s->n_chunks;
527
528 qemu_co_mutex_init(&s->lock);
529 return 0;
530
531 fail:
532 g_free(s->types);
533 g_free(s->offsets);
534 g_free(s->lengths);
535 g_free(s->sectors);
536 g_free(s->sectorcounts);
537 qemu_vfree(s->compressed_chunk);
538 qemu_vfree(s->uncompressed_chunk);
539 return ret;
540 }
541
542 static void dmg_refresh_limits(BlockDriverState *bs, Error **errp)
543 {
544 bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
545 }
546
547 static inline int is_sector_in_chunk(BDRVDMGState* s,
548 uint32_t chunk_num, uint64_t sector_num)
549 {
550 if (chunk_num >= s->n_chunks || s->sectors[chunk_num] > sector_num ||
551 s->sectors[chunk_num] + s->sectorcounts[chunk_num] <= sector_num) {
552 return 0;
553 } else {
554 return -1;
555 }
556 }
557
558 static inline uint32_t search_chunk(BDRVDMGState *s, uint64_t sector_num)
559 {
560 /* binary search */
561 uint32_t chunk1 = 0, chunk2 = s->n_chunks, chunk3;
562 while (chunk1 != chunk2) {
563 chunk3 = (chunk1 + chunk2) / 2;
564 if (s->sectors[chunk3] > sector_num) {
565 chunk2 = chunk3;
566 } else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) {
567 return chunk3;
568 } else {
569 chunk1 = chunk3;
570 }
571 }
572 return s->n_chunks; /* error */
573 }
574
575 static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
576 {
577 BDRVDMGState *s = bs->opaque;
578
579 if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
580 int ret;
581 uint32_t chunk = search_chunk(s, sector_num);
582
583 if (chunk >= s->n_chunks) {
584 return -1;
585 }
586
587 s->current_chunk = s->n_chunks;
588 switch (s->types[chunk]) { /* block entry type */
589 case 0x80000005: { /* zlib compressed */
590 /* we need to buffer, because only the chunk as whole can be
591 * inflated. */
592 ret = bdrv_pread(bs->file, s->offsets[chunk],
593 s->compressed_chunk, s->lengths[chunk]);
594 if (ret != s->lengths[chunk]) {
595 return -1;
596 }
597
598 s->zstream.next_in = s->compressed_chunk;
599 s->zstream.avail_in = s->lengths[chunk];
600 s->zstream.next_out = s->uncompressed_chunk;
601 s->zstream.avail_out = 512 * s->sectorcounts[chunk];
602 ret = inflateReset(&s->zstream);
603 if (ret != Z_OK) {
604 return -1;
605 }
606 ret = inflate(&s->zstream, Z_FINISH);
607 if (ret != Z_STREAM_END ||
608 s->zstream.total_out != 512 * s->sectorcounts[chunk]) {
609 return -1;
610 }
611 break; }
612 case 0x80000006: /* bzip2 compressed */
613 if (!dmg_uncompress_bz2) {
614 break;
615 }
616 /* we need to buffer, because only the chunk as whole can be
617 * inflated. */
618 ret = bdrv_pread(bs->file, s->offsets[chunk],
619 s->compressed_chunk, s->lengths[chunk]);
620 if (ret != s->lengths[chunk]) {
621 return -1;
622 }
623
624 ret = dmg_uncompress_bz2((char *)s->compressed_chunk,
625 (unsigned int) s->lengths[chunk],
626 (char *)s->uncompressed_chunk,
627 (unsigned int)
628 (512 * s->sectorcounts[chunk]));
629 if (ret < 0) {
630 return ret;
631 }
632 break;
633 case 0x80000007:
634 if (!dmg_uncompress_lzfse) {
635 break;
636 }
637 /* we need to buffer, because only the chunk as whole can be
638 * inflated. */
639 ret = bdrv_pread(bs->file, s->offsets[chunk],
640 s->compressed_chunk, s->lengths[chunk]);
641 if (ret != s->lengths[chunk]) {
642 return -1;
643 }
644
645 ret = dmg_uncompress_lzfse((char *)s->compressed_chunk,
646 (unsigned int) s->lengths[chunk],
647 (char *)s->uncompressed_chunk,
648 (unsigned int)
649 (512 * s->sectorcounts[chunk]));
650 if (ret < 0) {
651 return ret;
652 }
653 break;
654 case 1: /* copy */
655 ret = bdrv_pread(bs->file, s->offsets[chunk],
656 s->uncompressed_chunk, s->lengths[chunk]);
657 if (ret != s->lengths[chunk]) {
658 return -1;
659 }
660 break;
661 case 2: /* zero */
662 /* see dmg_read, it is treated specially. No buffer needs to be
663 * pre-filled, the zeroes can be set directly. */
664 break;
665 }
666 s->current_chunk = chunk;
667 }
668 return 0;
669 }
670
671 static int coroutine_fn
672 dmg_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
673 QEMUIOVector *qiov, int flags)
674 {
675 BDRVDMGState *s = bs->opaque;
676 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
677 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
678 int ret, i;
679
680 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
681 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
682
683 qemu_co_mutex_lock(&s->lock);
684
685 for (i = 0; i < nb_sectors; i++) {
686 uint32_t sector_offset_in_chunk;
687 void *data;
688
689 if (dmg_read_chunk(bs, sector_num + i) != 0) {
690 ret = -EIO;
691 goto fail;
692 }
693 /* Special case: current chunk is all zeroes. Do not perform a memcpy as
694 * s->uncompressed_chunk may be too small to cover the large all-zeroes
695 * section. dmg_read_chunk is called to find s->current_chunk */
696 if (s->types[s->current_chunk] == 2) { /* all zeroes block entry */
697 qemu_iovec_memset(qiov, i * 512, 0, 512);
698 continue;
699 }
700 sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
701 data = s->uncompressed_chunk + sector_offset_in_chunk * 512;
702 qemu_iovec_from_buf(qiov, i * 512, data, 512);
703 }
704
705 ret = 0;
706 fail:
707 qemu_co_mutex_unlock(&s->lock);
708 return ret;
709 }
710
711 static void dmg_close(BlockDriverState *bs)
712 {
713 BDRVDMGState *s = bs->opaque;
714
715 g_free(s->types);
716 g_free(s->offsets);
717 g_free(s->lengths);
718 g_free(s->sectors);
719 g_free(s->sectorcounts);
720 qemu_vfree(s->compressed_chunk);
721 qemu_vfree(s->uncompressed_chunk);
722
723 inflateEnd(&s->zstream);
724 }
725
726 static BlockDriver bdrv_dmg = {
727 .format_name = "dmg",
728 .instance_size = sizeof(BDRVDMGState),
729 .bdrv_probe = dmg_probe,
730 .bdrv_open = dmg_open,
731 .bdrv_refresh_limits = dmg_refresh_limits,
732 .bdrv_child_perm = bdrv_format_default_perms,
733 .bdrv_co_preadv = dmg_co_preadv,
734 .bdrv_close = dmg_close,
735 };
736
737 static void bdrv_dmg_init(void)
738 {
739 bdrv_register(&bdrv_dmg);
740 }
741
742 block_init(bdrv_dmg_init);