configure: require glib 2.22
[qemu.git] / block / dmg.c
blob825c49d59a9493b8afed4268dbfe68741ca1582f
1 /*
2 * QEMU Block driver for DMG images
4 * Copyright (c) 2004 Johannes E. Schindelin
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu-common.h"
25 #include "block/block_int.h"
26 #include "qemu/bswap.h"
27 #include "qemu/module.h"
28 #include <zlib.h>
29 #ifdef CONFIG_BZIP2
30 #include <bzlib.h>
31 #endif
32 #include <glib.h>
34 enum {
35 /* Limit chunk sizes to prevent unreasonable amounts of memory being used
36 * or truncating when converting to 32-bit types
38 DMG_LENGTHS_MAX = 64 * 1024 * 1024, /* 64 MB */
39 DMG_SECTORCOUNTS_MAX = DMG_LENGTHS_MAX / 512,
42 typedef struct BDRVDMGState {
43 CoMutex lock;
44 /* each chunk contains a certain number of sectors,
45 * offsets[i] is the offset in the .dmg file,
46 * lengths[i] is the length of the compressed chunk,
47 * sectors[i] is the sector beginning at offsets[i],
48 * sectorcounts[i] is the number of sectors in that chunk,
49 * the sectors array is ordered
50 * 0<=i<n_chunks */
52 uint32_t n_chunks;
53 uint32_t* types;
54 uint64_t* offsets;
55 uint64_t* lengths;
56 uint64_t* sectors;
57 uint64_t* sectorcounts;
58 uint32_t current_chunk;
59 uint8_t *compressed_chunk;
60 uint8_t *uncompressed_chunk;
61 z_stream zstream;
62 #ifdef CONFIG_BZIP2
63 bz_stream bzstream;
64 #endif
65 } BDRVDMGState;
67 static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
69 int len;
71 if (!filename) {
72 return 0;
75 len = strlen(filename);
76 if (len > 4 && !strcmp(filename + len - 4, ".dmg")) {
77 return 2;
79 return 0;
82 static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
84 uint64_t buffer;
85 int ret;
87 ret = bdrv_pread(bs->file, offset, &buffer, 8);
88 if (ret < 0) {
89 return ret;
92 *result = be64_to_cpu(buffer);
93 return 0;
96 static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
98 uint32_t buffer;
99 int ret;
101 ret = bdrv_pread(bs->file, offset, &buffer, 4);
102 if (ret < 0) {
103 return ret;
106 *result = be32_to_cpu(buffer);
107 return 0;
110 static inline uint64_t buff_read_uint64(const uint8_t *buffer, int64_t offset)
112 return be64_to_cpu(*(uint64_t *)&buffer[offset]);
115 static inline uint32_t buff_read_uint32(const uint8_t *buffer, int64_t offset)
117 return be32_to_cpu(*(uint32_t *)&buffer[offset]);
120 /* Increase max chunk sizes, if necessary. This function is used to calculate
121 * the buffer sizes needed for compressed/uncompressed chunk I/O.
123 static void update_max_chunk_size(BDRVDMGState *s, uint32_t chunk,
124 uint32_t *max_compressed_size,
125 uint32_t *max_sectors_per_chunk)
127 uint32_t compressed_size = 0;
128 uint32_t uncompressed_sectors = 0;
130 switch (s->types[chunk]) {
131 case 0x80000005: /* zlib compressed */
132 case 0x80000006: /* bzip2 compressed */
133 compressed_size = s->lengths[chunk];
134 uncompressed_sectors = s->sectorcounts[chunk];
135 break;
136 case 1: /* copy */
137 uncompressed_sectors = (s->lengths[chunk] + 511) / 512;
138 break;
139 case 2: /* zero */
140 /* as the all-zeroes block may be large, it is treated specially: the
141 * sector is not copied from a large buffer, a simple memset is used
142 * instead. Therefore uncompressed_sectors does not need to be set. */
143 break;
146 if (compressed_size > *max_compressed_size) {
147 *max_compressed_size = compressed_size;
149 if (uncompressed_sectors > *max_sectors_per_chunk) {
150 *max_sectors_per_chunk = uncompressed_sectors;
154 static int64_t dmg_find_koly_offset(BlockDriverState *file_bs, Error **errp)
156 int64_t length;
157 int64_t offset = 0;
158 uint8_t buffer[515];
159 int i, ret;
161 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
162 * dmg images can have odd sizes, try to look for the "koly" magic which
163 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
164 * in the last 511 bytes of the second-last sector or the first 4 bytes of
165 * the last sector (search space: 515 bytes) */
166 length = bdrv_getlength(file_bs);
167 if (length < 0) {
168 error_setg_errno(errp, -length,
169 "Failed to get file size while reading UDIF trailer");
170 return length;
171 } else if (length < 512) {
172 error_setg(errp, "dmg file must be at least 512 bytes long");
173 return -EINVAL;
175 if (length > 511 + 512) {
176 offset = length - 511 - 512;
178 length = length < 515 ? length : 515;
179 ret = bdrv_pread(file_bs, offset, buffer, length);
180 if (ret < 0) {
181 error_setg_errno(errp, -ret, "Failed while reading UDIF trailer");
182 return ret;
184 for (i = 0; i < length - 3; i++) {
185 if (buffer[i] == 'k' && buffer[i+1] == 'o' &&
186 buffer[i+2] == 'l' && buffer[i+3] == 'y') {
187 return offset + i;
190 error_setg(errp, "Could not locate UDIF trailer in dmg file");
191 return -EINVAL;
194 /* used when building the sector table */
195 typedef struct DmgHeaderState {
196 /* used internally by dmg_read_mish_block to remember offsets of blocks
197 * across calls */
198 uint64_t data_fork_offset;
199 /* exported for dmg_open */
200 uint32_t max_compressed_size;
201 uint32_t max_sectors_per_chunk;
202 } DmgHeaderState;
204 static bool dmg_is_known_block_type(uint32_t entry_type)
206 switch (entry_type) {
207 case 0x00000001: /* uncompressed */
208 case 0x00000002: /* zeroes */
209 case 0x80000005: /* zlib */
210 #ifdef CONFIG_BZIP2
211 case 0x80000006: /* bzip2 */
212 #endif
213 return true;
214 default:
215 return false;
219 static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,
220 uint8_t *buffer, uint32_t count)
222 uint32_t type, i;
223 int ret;
224 size_t new_size;
225 uint32_t chunk_count;
226 int64_t offset = 0;
227 uint64_t data_offset;
228 uint64_t in_offset = ds->data_fork_offset;
229 uint64_t out_offset;
231 type = buff_read_uint32(buffer, offset);
232 /* skip data that is not a valid MISH block (invalid magic or too small) */
233 if (type != 0x6d697368 || count < 244) {
234 /* assume success for now */
235 return 0;
238 /* chunk offsets are relative to this sector number */
239 out_offset = buff_read_uint64(buffer, offset + 8);
241 /* location in data fork for (compressed) blob (in bytes) */
242 data_offset = buff_read_uint64(buffer, offset + 0x18);
243 in_offset += data_offset;
245 /* move to begin of chunk entries */
246 offset += 204;
248 chunk_count = (count - 204) / 40;
249 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
250 s->types = g_realloc(s->types, new_size / 2);
251 s->offsets = g_realloc(s->offsets, new_size);
252 s->lengths = g_realloc(s->lengths, new_size);
253 s->sectors = g_realloc(s->sectors, new_size);
254 s->sectorcounts = g_realloc(s->sectorcounts, new_size);
256 for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
257 s->types[i] = buff_read_uint32(buffer, offset);
258 if (!dmg_is_known_block_type(s->types[i])) {
259 chunk_count--;
260 i--;
261 offset += 40;
262 continue;
265 /* sector number */
266 s->sectors[i] = buff_read_uint64(buffer, offset + 8);
267 s->sectors[i] += out_offset;
269 /* sector count */
270 s->sectorcounts[i] = buff_read_uint64(buffer, offset + 0x10);
272 /* all-zeroes sector (type 2) does not need to be "uncompressed" and can
273 * therefore be unbounded. */
274 if (s->types[i] != 2 && s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
275 error_report("sector count %" PRIu64 " for chunk %" PRIu32
276 " is larger than max (%u)",
277 s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
278 ret = -EINVAL;
279 goto fail;
282 /* offset in (compressed) data fork */
283 s->offsets[i] = buff_read_uint64(buffer, offset + 0x18);
284 s->offsets[i] += in_offset;
286 /* length in (compressed) data fork */
287 s->lengths[i] = buff_read_uint64(buffer, offset + 0x20);
289 if (s->lengths[i] > DMG_LENGTHS_MAX) {
290 error_report("length %" PRIu64 " for chunk %" PRIu32
291 " is larger than max (%u)",
292 s->lengths[i], i, DMG_LENGTHS_MAX);
293 ret = -EINVAL;
294 goto fail;
297 update_max_chunk_size(s, i, &ds->max_compressed_size,
298 &ds->max_sectors_per_chunk);
299 offset += 40;
301 s->n_chunks += chunk_count;
302 return 0;
304 fail:
305 return ret;
308 static int dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds,
309 uint64_t info_begin, uint64_t info_length)
311 BDRVDMGState *s = bs->opaque;
312 int ret;
313 uint32_t count, rsrc_data_offset;
314 uint8_t *buffer = NULL;
315 uint64_t info_end;
316 uint64_t offset;
318 /* read offset from begin of resource fork (info_begin) to resource data */
319 ret = read_uint32(bs, info_begin, &rsrc_data_offset);
320 if (ret < 0) {
321 goto fail;
322 } else if (rsrc_data_offset > info_length) {
323 ret = -EINVAL;
324 goto fail;
327 /* read length of resource data */
328 ret = read_uint32(bs, info_begin + 8, &count);
329 if (ret < 0) {
330 goto fail;
331 } else if (count == 0 || rsrc_data_offset + count > info_length) {
332 ret = -EINVAL;
333 goto fail;
336 /* begin of resource data (consisting of one or more resources) */
337 offset = info_begin + rsrc_data_offset;
339 /* end of resource data (there is possibly a following resource map
340 * which will be ignored). */
341 info_end = offset + count;
343 /* read offsets (mish blocks) from one or more resources in resource data */
344 while (offset < info_end) {
345 /* size of following resource */
346 ret = read_uint32(bs, offset, &count);
347 if (ret < 0) {
348 goto fail;
349 } else if (count == 0 || count > info_end - offset) {
350 ret = -EINVAL;
351 goto fail;
353 offset += 4;
355 buffer = g_realloc(buffer, count);
356 ret = bdrv_pread(bs->file, offset, buffer, count);
357 if (ret < 0) {
358 goto fail;
361 ret = dmg_read_mish_block(s, ds, buffer, count);
362 if (ret < 0) {
363 goto fail;
365 /* advance offset by size of resource */
366 offset += count;
368 ret = 0;
370 fail:
371 g_free(buffer);
372 return ret;
375 static int dmg_read_plist_xml(BlockDriverState *bs, DmgHeaderState *ds,
376 uint64_t info_begin, uint64_t info_length)
378 BDRVDMGState *s = bs->opaque;
379 int ret;
380 uint8_t *buffer = NULL;
381 char *data_begin, *data_end;
383 /* Have at least some length to avoid NULL for g_malloc. Attempt to set a
384 * safe upper cap on the data length. A test sample had a XML length of
385 * about 1 MiB. */
386 if (info_length == 0 || info_length > 16 * 1024 * 1024) {
387 ret = -EINVAL;
388 goto fail;
391 buffer = g_malloc(info_length + 1);
392 buffer[info_length] = '\0';
393 ret = bdrv_pread(bs->file, info_begin, buffer, info_length);
394 if (ret != info_length) {
395 ret = -EINVAL;
396 goto fail;
399 /* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
400 * decode. The actual data element has 431 (0x1af) bytes which includes tabs
401 * and line feeds. */
402 data_end = (char *)buffer;
403 while ((data_begin = strstr(data_end, "<data>")) != NULL) {
404 guchar *mish;
405 gsize out_len = 0;
407 data_begin += 6;
408 data_end = strstr(data_begin, "</data>");
409 /* malformed XML? */
410 if (data_end == NULL) {
411 ret = -EINVAL;
412 goto fail;
414 *data_end++ = '\0';
415 mish = g_base64_decode(data_begin, &out_len);
416 ret = dmg_read_mish_block(s, ds, mish, (uint32_t)out_len);
417 g_free(mish);
418 if (ret < 0) {
419 goto fail;
422 ret = 0;
424 fail:
425 g_free(buffer);
426 return ret;
429 static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
430 Error **errp)
432 BDRVDMGState *s = bs->opaque;
433 DmgHeaderState ds;
434 uint64_t rsrc_fork_offset, rsrc_fork_length;
435 uint64_t plist_xml_offset, plist_xml_length;
436 int64_t offset;
437 int ret;
439 bs->read_only = 1;
440 s->n_chunks = 0;
441 s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
442 /* used by dmg_read_mish_block to keep track of the current I/O position */
443 ds.data_fork_offset = 0;
444 ds.max_compressed_size = 1;
445 ds.max_sectors_per_chunk = 1;
447 /* locate the UDIF trailer */
448 offset = dmg_find_koly_offset(bs->file, errp);
449 if (offset < 0) {
450 ret = offset;
451 goto fail;
454 /* offset of data fork (DataForkOffset) */
455 ret = read_uint64(bs, offset + 0x18, &ds.data_fork_offset);
456 if (ret < 0) {
457 goto fail;
458 } else if (ds.data_fork_offset > offset) {
459 ret = -EINVAL;
460 goto fail;
463 /* offset of resource fork (RsrcForkOffset) */
464 ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
465 if (ret < 0) {
466 goto fail;
468 ret = read_uint64(bs, offset + 0x30, &rsrc_fork_length);
469 if (ret < 0) {
470 goto fail;
472 if (rsrc_fork_offset >= offset ||
473 rsrc_fork_length > offset - rsrc_fork_offset) {
474 ret = -EINVAL;
475 goto fail;
477 /* offset of property list (XMLOffset) */
478 ret = read_uint64(bs, offset + 0xd8, &plist_xml_offset);
479 if (ret < 0) {
480 goto fail;
482 ret = read_uint64(bs, offset + 0xe0, &plist_xml_length);
483 if (ret < 0) {
484 goto fail;
486 if (plist_xml_offset >= offset ||
487 plist_xml_length > offset - plist_xml_offset) {
488 ret = -EINVAL;
489 goto fail;
491 ret = read_uint64(bs, offset + 0x1ec, (uint64_t *)&bs->total_sectors);
492 if (ret < 0) {
493 goto fail;
495 if (bs->total_sectors < 0) {
496 ret = -EINVAL;
497 goto fail;
499 if (rsrc_fork_length != 0) {
500 ret = dmg_read_resource_fork(bs, &ds,
501 rsrc_fork_offset, rsrc_fork_length);
502 if (ret < 0) {
503 goto fail;
505 } else if (plist_xml_length != 0) {
506 ret = dmg_read_plist_xml(bs, &ds, plist_xml_offset, plist_xml_length);
507 if (ret < 0) {
508 goto fail;
510 } else {
511 ret = -EINVAL;
512 goto fail;
515 /* initialize zlib engine */
516 s->compressed_chunk = qemu_try_blockalign(bs->file,
517 ds.max_compressed_size + 1);
518 s->uncompressed_chunk = qemu_try_blockalign(bs->file,
519 512 * ds.max_sectors_per_chunk);
520 if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
521 ret = -ENOMEM;
522 goto fail;
525 if (inflateInit(&s->zstream) != Z_OK) {
526 ret = -EINVAL;
527 goto fail;
530 s->current_chunk = s->n_chunks;
532 qemu_co_mutex_init(&s->lock);
533 return 0;
535 fail:
536 g_free(s->types);
537 g_free(s->offsets);
538 g_free(s->lengths);
539 g_free(s->sectors);
540 g_free(s->sectorcounts);
541 qemu_vfree(s->compressed_chunk);
542 qemu_vfree(s->uncompressed_chunk);
543 return ret;
546 static inline int is_sector_in_chunk(BDRVDMGState* s,
547 uint32_t chunk_num, uint64_t sector_num)
549 if (chunk_num >= s->n_chunks || s->sectors[chunk_num] > sector_num ||
550 s->sectors[chunk_num] + s->sectorcounts[chunk_num] <= sector_num) {
551 return 0;
552 } else {
553 return -1;
557 static inline uint32_t search_chunk(BDRVDMGState *s, uint64_t sector_num)
559 /* binary search */
560 uint32_t chunk1 = 0, chunk2 = s->n_chunks, chunk3;
561 while (chunk1 != chunk2) {
562 chunk3 = (chunk1 + chunk2) / 2;
563 if (s->sectors[chunk3] > sector_num) {
564 chunk2 = chunk3;
565 } else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) {
566 return chunk3;
567 } else {
568 chunk1 = chunk3;
571 return s->n_chunks; /* error */
574 static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
576 BDRVDMGState *s = bs->opaque;
578 if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
579 int ret;
580 uint32_t chunk = search_chunk(s, sector_num);
581 #ifdef CONFIG_BZIP2
582 uint64_t total_out;
583 #endif
585 if (chunk >= s->n_chunks) {
586 return -1;
589 s->current_chunk = s->n_chunks;
590 switch (s->types[chunk]) { /* block entry type */
591 case 0x80000005: { /* zlib compressed */
592 /* we need to buffer, because only the chunk as whole can be
593 * inflated. */
594 ret = bdrv_pread(bs->file, s->offsets[chunk],
595 s->compressed_chunk, s->lengths[chunk]);
596 if (ret != s->lengths[chunk]) {
597 return -1;
600 s->zstream.next_in = s->compressed_chunk;
601 s->zstream.avail_in = s->lengths[chunk];
602 s->zstream.next_out = s->uncompressed_chunk;
603 s->zstream.avail_out = 512 * s->sectorcounts[chunk];
604 ret = inflateReset(&s->zstream);
605 if (ret != Z_OK) {
606 return -1;
608 ret = inflate(&s->zstream, Z_FINISH);
609 if (ret != Z_STREAM_END ||
610 s->zstream.total_out != 512 * s->sectorcounts[chunk]) {
611 return -1;
613 break; }
614 #ifdef CONFIG_BZIP2
615 case 0x80000006: /* bzip2 compressed */
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;
624 ret = BZ2_bzDecompressInit(&s->bzstream, 0, 0);
625 if (ret != BZ_OK) {
626 return -1;
628 s->bzstream.next_in = (char *)s->compressed_chunk;
629 s->bzstream.avail_in = (unsigned int) s->lengths[chunk];
630 s->bzstream.next_out = (char *)s->uncompressed_chunk;
631 s->bzstream.avail_out = (unsigned int) 512 * s->sectorcounts[chunk];
632 ret = BZ2_bzDecompress(&s->bzstream);
633 total_out = ((uint64_t)s->bzstream.total_out_hi32 << 32) +
634 s->bzstream.total_out_lo32;
635 BZ2_bzDecompressEnd(&s->bzstream);
636 if (ret != BZ_STREAM_END ||
637 total_out != 512 * s->sectorcounts[chunk]) {
638 return -1;
640 break;
641 #endif /* CONFIG_BZIP2 */
642 case 1: /* copy */
643 ret = bdrv_pread(bs->file, s->offsets[chunk],
644 s->uncompressed_chunk, s->lengths[chunk]);
645 if (ret != s->lengths[chunk]) {
646 return -1;
648 break;
649 case 2: /* zero */
650 /* see dmg_read, it is treated specially. No buffer needs to be
651 * pre-filled, the zeroes can be set directly. */
652 break;
654 s->current_chunk = chunk;
656 return 0;
659 static int dmg_read(BlockDriverState *bs, int64_t sector_num,
660 uint8_t *buf, int nb_sectors)
662 BDRVDMGState *s = bs->opaque;
663 int i;
665 for (i = 0; i < nb_sectors; i++) {
666 uint32_t sector_offset_in_chunk;
667 if (dmg_read_chunk(bs, sector_num + i) != 0) {
668 return -1;
670 /* Special case: current chunk is all zeroes. Do not perform a memcpy as
671 * s->uncompressed_chunk may be too small to cover the large all-zeroes
672 * section. dmg_read_chunk is called to find s->current_chunk */
673 if (s->types[s->current_chunk] == 2) { /* all zeroes block entry */
674 memset(buf + i * 512, 0, 512);
675 continue;
677 sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
678 memcpy(buf + i * 512,
679 s->uncompressed_chunk + sector_offset_in_chunk * 512, 512);
681 return 0;
684 static coroutine_fn int dmg_co_read(BlockDriverState *bs, int64_t sector_num,
685 uint8_t *buf, int nb_sectors)
687 int ret;
688 BDRVDMGState *s = bs->opaque;
689 qemu_co_mutex_lock(&s->lock);
690 ret = dmg_read(bs, sector_num, buf, nb_sectors);
691 qemu_co_mutex_unlock(&s->lock);
692 return ret;
695 static void dmg_close(BlockDriverState *bs)
697 BDRVDMGState *s = bs->opaque;
699 g_free(s->types);
700 g_free(s->offsets);
701 g_free(s->lengths);
702 g_free(s->sectors);
703 g_free(s->sectorcounts);
704 qemu_vfree(s->compressed_chunk);
705 qemu_vfree(s->uncompressed_chunk);
707 inflateEnd(&s->zstream);
710 static BlockDriver bdrv_dmg = {
711 .format_name = "dmg",
712 .instance_size = sizeof(BDRVDMGState),
713 .bdrv_probe = dmg_probe,
714 .bdrv_open = dmg_open,
715 .bdrv_read = dmg_co_read,
716 .bdrv_close = dmg_close,
719 static void bdrv_dmg_init(void)
721 bdrv_register(&bdrv_dmg);
724 block_init(bdrv_dmg_init);