block/dmg: support bzip2 block entry types
[qemu/ar7.git] / block / dmg.c
blob797fda3ec8c979baffdf52a36889b81e2b678d6c
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 uncompressed_sectors = s->sectorcounts[chunk];
141 break;
144 if (compressed_size > *max_compressed_size) {
145 *max_compressed_size = compressed_size;
147 if (uncompressed_sectors > *max_sectors_per_chunk) {
148 *max_sectors_per_chunk = uncompressed_sectors;
152 static int64_t dmg_find_koly_offset(BlockDriverState *file_bs, Error **errp)
154 int64_t length;
155 int64_t offset = 0;
156 uint8_t buffer[515];
157 int i, ret;
159 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
160 * dmg images can have odd sizes, try to look for the "koly" magic which
161 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
162 * in the last 511 bytes of the second-last sector or the first 4 bytes of
163 * the last sector (search space: 515 bytes) */
164 length = bdrv_getlength(file_bs);
165 if (length < 0) {
166 error_setg_errno(errp, -length,
167 "Failed to get file size while reading UDIF trailer");
168 return length;
169 } else if (length < 512) {
170 error_setg(errp, "dmg file must be at least 512 bytes long");
171 return -EINVAL;
173 if (length > 511 + 512) {
174 offset = length - 511 - 512;
176 length = length < 515 ? length : 515;
177 ret = bdrv_pread(file_bs, offset, buffer, length);
178 if (ret < 0) {
179 error_setg_errno(errp, -ret, "Failed while reading UDIF trailer");
180 return ret;
182 for (i = 0; i < length - 3; i++) {
183 if (buffer[i] == 'k' && buffer[i+1] == 'o' &&
184 buffer[i+2] == 'l' && buffer[i+3] == 'y') {
185 return offset + i;
188 error_setg(errp, "Could not locate UDIF trailer in dmg file");
189 return -EINVAL;
192 /* used when building the sector table */
193 typedef struct DmgHeaderState {
194 /* used internally by dmg_read_mish_block to remember offsets of blocks
195 * across calls */
196 uint64_t data_fork_offset;
197 /* exported for dmg_open */
198 uint32_t max_compressed_size;
199 uint32_t max_sectors_per_chunk;
200 } DmgHeaderState;
202 static bool dmg_is_known_block_type(uint32_t entry_type)
204 switch (entry_type) {
205 case 0x00000001: /* uncompressed */
206 case 0x00000002: /* zeroes */
207 case 0x80000005: /* zlib */
208 #ifdef CONFIG_BZIP2
209 case 0x80000006: /* bzip2 */
210 #endif
211 return true;
212 default:
213 return false;
217 static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,
218 uint8_t *buffer, uint32_t count)
220 uint32_t type, i;
221 int ret;
222 size_t new_size;
223 uint32_t chunk_count;
224 int64_t offset = 0;
225 uint64_t data_offset;
226 uint64_t in_offset = ds->data_fork_offset;
227 uint64_t out_offset;
229 type = buff_read_uint32(buffer, offset);
230 /* skip data that is not a valid MISH block (invalid magic or too small) */
231 if (type != 0x6d697368 || count < 244) {
232 /* assume success for now */
233 return 0;
236 /* chunk offsets are relative to this sector number */
237 out_offset = buff_read_uint64(buffer, offset + 8);
239 /* location in data fork for (compressed) blob (in bytes) */
240 data_offset = buff_read_uint64(buffer, offset + 0x18);
241 in_offset += data_offset;
243 /* move to begin of chunk entries */
244 offset += 204;
246 chunk_count = (count - 204) / 40;
247 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
248 s->types = g_realloc(s->types, new_size / 2);
249 s->offsets = g_realloc(s->offsets, new_size);
250 s->lengths = g_realloc(s->lengths, new_size);
251 s->sectors = g_realloc(s->sectors, new_size);
252 s->sectorcounts = g_realloc(s->sectorcounts, new_size);
254 for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
255 s->types[i] = buff_read_uint32(buffer, offset);
256 if (!dmg_is_known_block_type(s->types[i])) {
257 chunk_count--;
258 i--;
259 offset += 40;
260 continue;
263 /* sector number */
264 s->sectors[i] = buff_read_uint64(buffer, offset + 8);
265 s->sectors[i] += out_offset;
267 /* sector count */
268 s->sectorcounts[i] = buff_read_uint64(buffer, offset + 0x10);
270 if (s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
271 error_report("sector count %" PRIu64 " for chunk %" PRIu32
272 " is larger than max (%u)",
273 s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
274 ret = -EINVAL;
275 goto fail;
278 /* offset in (compressed) data fork */
279 s->offsets[i] = buff_read_uint64(buffer, offset + 0x18);
280 s->offsets[i] += in_offset;
282 /* length in (compressed) data fork */
283 s->lengths[i] = buff_read_uint64(buffer, offset + 0x20);
285 if (s->lengths[i] > DMG_LENGTHS_MAX) {
286 error_report("length %" PRIu64 " for chunk %" PRIu32
287 " is larger than max (%u)",
288 s->lengths[i], i, DMG_LENGTHS_MAX);
289 ret = -EINVAL;
290 goto fail;
293 update_max_chunk_size(s, i, &ds->max_compressed_size,
294 &ds->max_sectors_per_chunk);
295 offset += 40;
297 s->n_chunks += chunk_count;
298 return 0;
300 fail:
301 return ret;
304 static int dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds,
305 uint64_t info_begin, uint64_t info_length)
307 BDRVDMGState *s = bs->opaque;
308 int ret;
309 uint32_t count, rsrc_data_offset;
310 uint8_t *buffer = NULL;
311 uint64_t info_end;
312 uint64_t offset;
314 /* read offset from begin of resource fork (info_begin) to resource data */
315 ret = read_uint32(bs, info_begin, &rsrc_data_offset);
316 if (ret < 0) {
317 goto fail;
318 } else if (rsrc_data_offset > info_length) {
319 ret = -EINVAL;
320 goto fail;
323 /* read length of resource data */
324 ret = read_uint32(bs, info_begin + 8, &count);
325 if (ret < 0) {
326 goto fail;
327 } else if (count == 0 || rsrc_data_offset + count > info_length) {
328 ret = -EINVAL;
329 goto fail;
332 /* begin of resource data (consisting of one or more resources) */
333 offset = info_begin + rsrc_data_offset;
335 /* end of resource data (there is possibly a following resource map
336 * which will be ignored). */
337 info_end = offset + count;
339 /* read offsets (mish blocks) from one or more resources in resource data */
340 while (offset < info_end) {
341 /* size of following resource */
342 ret = read_uint32(bs, offset, &count);
343 if (ret < 0) {
344 goto fail;
345 } else if (count == 0 || count > info_end - offset) {
346 ret = -EINVAL;
347 goto fail;
349 offset += 4;
351 buffer = g_realloc(buffer, count);
352 ret = bdrv_pread(bs->file, offset, buffer, count);
353 if (ret < 0) {
354 goto fail;
357 ret = dmg_read_mish_block(s, ds, buffer, count);
358 if (ret < 0) {
359 goto fail;
361 /* advance offset by size of resource */
362 offset += count;
364 ret = 0;
366 fail:
367 g_free(buffer);
368 return ret;
371 static int dmg_read_plist_xml(BlockDriverState *bs, DmgHeaderState *ds,
372 uint64_t info_begin, uint64_t info_length)
374 BDRVDMGState *s = bs->opaque;
375 int ret;
376 uint8_t *buffer = NULL;
377 char *data_begin, *data_end;
379 /* Have at least some length to avoid NULL for g_malloc. Attempt to set a
380 * safe upper cap on the data length. A test sample had a XML length of
381 * about 1 MiB. */
382 if (info_length == 0 || info_length > 16 * 1024 * 1024) {
383 ret = -EINVAL;
384 goto fail;
387 buffer = g_malloc(info_length + 1);
388 buffer[info_length] = '\0';
389 ret = bdrv_pread(bs->file, info_begin, buffer, info_length);
390 if (ret != info_length) {
391 ret = -EINVAL;
392 goto fail;
395 /* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
396 * decode. The actual data element has 431 (0x1af) bytes which includes tabs
397 * and line feeds. */
398 data_end = (char *)buffer;
399 while ((data_begin = strstr(data_end, "<data>")) != NULL) {
400 guchar *mish;
401 gsize out_len = 0;
403 data_begin += 6;
404 data_end = strstr(data_begin, "</data>");
405 /* malformed XML? */
406 if (data_end == NULL) {
407 ret = -EINVAL;
408 goto fail;
410 *data_end++ = '\0';
411 mish = g_base64_decode(data_begin, &out_len);
412 ret = dmg_read_mish_block(s, ds, mish, (uint32_t)out_len);
413 g_free(mish);
414 if (ret < 0) {
415 goto fail;
418 ret = 0;
420 fail:
421 g_free(buffer);
422 return ret;
425 static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
426 Error **errp)
428 BDRVDMGState *s = bs->opaque;
429 DmgHeaderState ds;
430 uint64_t rsrc_fork_offset, rsrc_fork_length;
431 uint64_t plist_xml_offset, plist_xml_length;
432 int64_t offset;
433 int ret;
435 bs->read_only = 1;
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;
443 /* locate the UDIF trailer */
444 offset = dmg_find_koly_offset(bs->file, errp);
445 if (offset < 0) {
446 ret = offset;
447 goto fail;
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;
459 /* offset of resource fork (RsrcForkOffset) */
460 ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
461 if (ret < 0) {
462 goto fail;
464 ret = read_uint64(bs, offset + 0x30, &rsrc_fork_length);
465 if (ret < 0) {
466 goto fail;
468 if (rsrc_fork_offset >= offset ||
469 rsrc_fork_length > offset - rsrc_fork_offset) {
470 ret = -EINVAL;
471 goto fail;
473 /* offset of property list (XMLOffset) */
474 ret = read_uint64(bs, offset + 0xd8, &plist_xml_offset);
475 if (ret < 0) {
476 goto fail;
478 ret = read_uint64(bs, offset + 0xe0, &plist_xml_length);
479 if (ret < 0) {
480 goto fail;
482 if (plist_xml_offset >= offset ||
483 plist_xml_length > offset - plist_xml_offset) {
484 ret = -EINVAL;
485 goto fail;
487 ret = read_uint64(bs, offset + 0x1ec, (uint64_t *)&bs->total_sectors);
488 if (ret < 0) {
489 goto fail;
491 if (bs->total_sectors < 0) {
492 ret = -EINVAL;
493 goto fail;
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;
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;
506 } else {
507 ret = -EINVAL;
508 goto fail;
511 /* initialize zlib engine */
512 s->compressed_chunk = qemu_try_blockalign(bs->file,
513 ds.max_compressed_size + 1);
514 s->uncompressed_chunk = qemu_try_blockalign(bs->file,
515 512 * ds.max_sectors_per_chunk);
516 if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
517 ret = -ENOMEM;
518 goto fail;
521 if (inflateInit(&s->zstream) != Z_OK) {
522 ret = -EINVAL;
523 goto fail;
526 s->current_chunk = s->n_chunks;
528 qemu_co_mutex_init(&s->lock);
529 return 0;
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;
542 static inline int is_sector_in_chunk(BDRVDMGState* s,
543 uint32_t chunk_num, uint64_t sector_num)
545 if (chunk_num >= s->n_chunks || s->sectors[chunk_num] > sector_num ||
546 s->sectors[chunk_num] + s->sectorcounts[chunk_num] <= sector_num) {
547 return 0;
548 } else {
549 return -1;
553 static inline uint32_t search_chunk(BDRVDMGState *s, uint64_t sector_num)
555 /* binary search */
556 uint32_t chunk1 = 0, chunk2 = s->n_chunks, chunk3;
557 while (chunk1 != chunk2) {
558 chunk3 = (chunk1 + chunk2) / 2;
559 if (s->sectors[chunk3] > sector_num) {
560 chunk2 = chunk3;
561 } else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) {
562 return chunk3;
563 } else {
564 chunk1 = chunk3;
567 return s->n_chunks; /* error */
570 static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
572 BDRVDMGState *s = bs->opaque;
574 if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
575 int ret;
576 uint32_t chunk = search_chunk(s, sector_num);
577 #ifdef CONFIG_BZIP2
578 uint64_t total_out;
579 #endif
581 if (chunk >= s->n_chunks) {
582 return -1;
585 s->current_chunk = s->n_chunks;
586 switch (s->types[chunk]) { /* block entry type */
587 case 0x80000005: { /* zlib compressed */
588 /* we need to buffer, because only the chunk as whole can be
589 * inflated. */
590 ret = bdrv_pread(bs->file, s->offsets[chunk],
591 s->compressed_chunk, s->lengths[chunk]);
592 if (ret != s->lengths[chunk]) {
593 return -1;
596 s->zstream.next_in = s->compressed_chunk;
597 s->zstream.avail_in = s->lengths[chunk];
598 s->zstream.next_out = s->uncompressed_chunk;
599 s->zstream.avail_out = 512 * s->sectorcounts[chunk];
600 ret = inflateReset(&s->zstream);
601 if (ret != Z_OK) {
602 return -1;
604 ret = inflate(&s->zstream, Z_FINISH);
605 if (ret != Z_STREAM_END ||
606 s->zstream.total_out != 512 * s->sectorcounts[chunk]) {
607 return -1;
609 break; }
610 #ifdef CONFIG_BZIP2
611 case 0x80000006: /* bzip2 compressed */
612 /* we need to buffer, because only the chunk as whole can be
613 * inflated. */
614 ret = bdrv_pread(bs->file, s->offsets[chunk],
615 s->compressed_chunk, s->lengths[chunk]);
616 if (ret != s->lengths[chunk]) {
617 return -1;
620 ret = BZ2_bzDecompressInit(&s->bzstream, 0, 0);
621 if (ret != BZ_OK) {
622 return -1;
624 s->bzstream.next_in = (char *)s->compressed_chunk;
625 s->bzstream.avail_in = (unsigned int) s->lengths[chunk];
626 s->bzstream.next_out = (char *)s->uncompressed_chunk;
627 s->bzstream.avail_out = (unsigned int) 512 * s->sectorcounts[chunk];
628 ret = BZ2_bzDecompress(&s->bzstream);
629 total_out = ((uint64_t)s->bzstream.total_out_hi32 << 32) +
630 s->bzstream.total_out_lo32;
631 BZ2_bzDecompressEnd(&s->bzstream);
632 if (ret != BZ_STREAM_END ||
633 total_out != 512 * s->sectorcounts[chunk]) {
634 return -1;
636 break;
637 #endif /* CONFIG_BZIP2 */
638 case 1: /* copy */
639 ret = bdrv_pread(bs->file, s->offsets[chunk],
640 s->uncompressed_chunk, s->lengths[chunk]);
641 if (ret != s->lengths[chunk]) {
642 return -1;
644 break;
645 case 2: /* zero */
646 memset(s->uncompressed_chunk, 0, 512 * s->sectorcounts[chunk]);
647 break;
649 s->current_chunk = chunk;
651 return 0;
654 static int dmg_read(BlockDriverState *bs, int64_t sector_num,
655 uint8_t *buf, int nb_sectors)
657 BDRVDMGState *s = bs->opaque;
658 int i;
660 for (i = 0; i < nb_sectors; i++) {
661 uint32_t sector_offset_in_chunk;
662 if (dmg_read_chunk(bs, sector_num + i) != 0) {
663 return -1;
665 sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
666 memcpy(buf + i * 512,
667 s->uncompressed_chunk + sector_offset_in_chunk * 512, 512);
669 return 0;
672 static coroutine_fn int dmg_co_read(BlockDriverState *bs, int64_t sector_num,
673 uint8_t *buf, int nb_sectors)
675 int ret;
676 BDRVDMGState *s = bs->opaque;
677 qemu_co_mutex_lock(&s->lock);
678 ret = dmg_read(bs, sector_num, buf, nb_sectors);
679 qemu_co_mutex_unlock(&s->lock);
680 return ret;
683 static void dmg_close(BlockDriverState *bs)
685 BDRVDMGState *s = bs->opaque;
687 g_free(s->types);
688 g_free(s->offsets);
689 g_free(s->lengths);
690 g_free(s->sectors);
691 g_free(s->sectorcounts);
692 qemu_vfree(s->compressed_chunk);
693 qemu_vfree(s->uncompressed_chunk);
695 inflateEnd(&s->zstream);
698 static BlockDriver bdrv_dmg = {
699 .format_name = "dmg",
700 .instance_size = sizeof(BDRVDMGState),
701 .bdrv_probe = dmg_probe,
702 .bdrv_open = dmg_open,
703 .bdrv_read = dmg_co_read,
704 .bdrv_close = dmg_close,
707 static void bdrv_dmg_init(void)
709 bdrv_register(&bdrv_dmg);
712 block_init(bdrv_dmg_init);