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
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "block/block_int.h"
27 #include "qemu/bswap.h"
28 #include "qemu/error-report.h"
29 #include "qemu/module.h"
32 int (*dmg_uncompress_bz2
)(char *next_in
, unsigned int avail_in
,
33 char *next_out
, unsigned int avail_out
);
35 int (*dmg_uncompress_lzfse
)(char *next_in
, unsigned int avail_in
,
36 char *next_out
, unsigned int avail_out
);
39 /* Limit chunk sizes to prevent unreasonable amounts of memory being used
40 * or truncating when converting to 32-bit types
42 DMG_LENGTHS_MAX
= 64 * 1024 * 1024, /* 64 MB */
43 DMG_SECTORCOUNTS_MAX
= DMG_LENGTHS_MAX
/ 512,
48 UDZE
= 0, /* Zeroes */
55 UDCM
= 0x7ffffffe, /* Comments */
56 UDLE
= 0xffffffff /* Last Entry */
59 static int dmg_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
67 len
= strlen(filename
);
68 if (len
> 4 && !strcmp(filename
+ len
- 4, ".dmg")) {
74 static int read_uint64(BlockDriverState
*bs
, int64_t offset
, uint64_t *result
)
79 ret
= bdrv_pread(bs
->file
, offset
, &buffer
, 8);
84 *result
= be64_to_cpu(buffer
);
88 static int read_uint32(BlockDriverState
*bs
, int64_t offset
, uint32_t *result
)
93 ret
= bdrv_pread(bs
->file
, offset
, &buffer
, 4);
98 *result
= be32_to_cpu(buffer
);
102 static inline uint64_t buff_read_uint64(const uint8_t *buffer
, int64_t offset
)
104 return be64_to_cpu(*(uint64_t *)&buffer
[offset
]);
107 static inline uint32_t buff_read_uint32(const uint8_t *buffer
, int64_t offset
)
109 return be32_to_cpu(*(uint32_t *)&buffer
[offset
]);
112 /* Increase max chunk sizes, if necessary. This function is used to calculate
113 * the buffer sizes needed for compressed/uncompressed chunk I/O.
115 static void update_max_chunk_size(BDRVDMGState
*s
, uint32_t chunk
,
116 uint32_t *max_compressed_size
,
117 uint32_t *max_sectors_per_chunk
)
119 uint32_t compressed_size
= 0;
120 uint32_t uncompressed_sectors
= 0;
122 switch (s
->types
[chunk
]) {
123 case UDZO
: /* zlib compressed */
124 case UDBZ
: /* bzip2 compressed */
125 case ULFO
: /* lzfse compressed */
126 compressed_size
= s
->lengths
[chunk
];
127 uncompressed_sectors
= s
->sectorcounts
[chunk
];
129 case UDRW
: /* copy */
130 uncompressed_sectors
= DIV_ROUND_UP(s
->lengths
[chunk
], 512);
132 case UDZE
: /* zero */
133 case UDIG
: /* ignore */
134 /* as the all-zeroes block may be large, it is treated specially: the
135 * sector is not copied from a large buffer, a simple memset is used
136 * instead. Therefore uncompressed_sectors does not need to be set. */
140 if (compressed_size
> *max_compressed_size
) {
141 *max_compressed_size
= compressed_size
;
143 if (uncompressed_sectors
> *max_sectors_per_chunk
) {
144 *max_sectors_per_chunk
= uncompressed_sectors
;
148 static int64_t dmg_find_koly_offset(BdrvChild
*file
, Error
**errp
)
150 BlockDriverState
*file_bs
= file
->bs
;
156 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
157 * dmg images can have odd sizes, try to look for the "koly" magic which
158 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
159 * in the last 511 bytes of the second-last sector or the first 4 bytes of
160 * the last sector (search space: 515 bytes) */
161 length
= bdrv_getlength(file_bs
);
163 error_setg_errno(errp
, -length
,
164 "Failed to get file size while reading UDIF trailer");
166 } else if (length
< 512) {
167 error_setg(errp
, "dmg file must be at least 512 bytes long");
170 if (length
> 511 + 512) {
171 offset
= length
- 511 - 512;
173 length
= length
< 515 ? length
: 515;
174 ret
= bdrv_pread(file
, offset
, buffer
, length
);
176 error_setg_errno(errp
, -ret
, "Failed while reading UDIF trailer");
179 for (i
= 0; i
< length
- 3; i
++) {
180 if (buffer
[i
] == 'k' && buffer
[i
+1] == 'o' &&
181 buffer
[i
+2] == 'l' && buffer
[i
+3] == 'y') {
185 error_setg(errp
, "Could not locate UDIF trailer in dmg file");
189 /* used when building the sector table */
190 typedef struct DmgHeaderState
{
191 /* used internally by dmg_read_mish_block to remember offsets of blocks
193 uint64_t data_fork_offset
;
194 /* exported for dmg_open */
195 uint32_t max_compressed_size
;
196 uint32_t max_sectors_per_chunk
;
199 static bool dmg_is_known_block_type(uint32_t entry_type
)
201 switch (entry_type
) {
202 case UDZE
: /* zeros */
203 case UDRW
: /* uncompressed */
204 case UDIG
: /* ignore */
205 case UDZO
: /* zlib */
207 case UDBZ
: /* bzip2 */
208 return !!dmg_uncompress_bz2
;
209 case ULFO
: /* lzfse */
210 return !!dmg_uncompress_lzfse
;
216 static int dmg_read_mish_block(BDRVDMGState
*s
, DmgHeaderState
*ds
,
217 uint8_t *buffer
, uint32_t count
)
222 uint32_t chunk_count
;
224 uint64_t data_offset
;
225 uint64_t in_offset
= ds
->data_fork_offset
;
228 type
= buff_read_uint32(buffer
, offset
);
229 /* skip data that is not a valid MISH block (invalid magic or too small) */
230 if (type
!= 0x6d697368 || count
< 244) {
231 /* assume success for now */
235 /* chunk offsets are relative to this sector number */
236 out_offset
= buff_read_uint64(buffer
, offset
+ 8);
238 /* location in data fork for (compressed) blob (in bytes) */
239 data_offset
= buff_read_uint64(buffer
, offset
+ 0x18);
240 in_offset
+= data_offset
;
242 /* move to begin of chunk entries */
245 chunk_count
= (count
- 204) / 40;
246 new_size
= sizeof(uint64_t) * (s
->n_chunks
+ chunk_count
);
247 s
->types
= g_realloc(s
->types
, new_size
/ 2);
248 s
->offsets
= g_realloc(s
->offsets
, new_size
);
249 s
->lengths
= g_realloc(s
->lengths
, new_size
);
250 s
->sectors
= g_realloc(s
->sectors
, new_size
);
251 s
->sectorcounts
= g_realloc(s
->sectorcounts
, new_size
);
253 for (i
= s
->n_chunks
; i
< s
->n_chunks
+ chunk_count
; i
++) {
254 s
->types
[i
] = buff_read_uint32(buffer
, offset
);
255 if (!dmg_is_known_block_type(s
->types
[i
])) {
263 s
->sectors
[i
] = buff_read_uint64(buffer
, offset
+ 8);
264 s
->sectors
[i
] += out_offset
;
267 s
->sectorcounts
[i
] = buff_read_uint64(buffer
, offset
+ 0x10);
269 /* all-zeroes sector (type UDZE and UDIG) does not need to be
270 * "uncompressed" and can therefore be unbounded. */
271 if (s
->types
[i
] != UDZE
&& s
->types
[i
] != UDIG
272 && s
->sectorcounts
[i
] > DMG_SECTORCOUNTS_MAX
) {
273 error_report("sector count %" PRIu64
" for chunk %" PRIu32
274 " is larger than max (%u)",
275 s
->sectorcounts
[i
], i
, DMG_SECTORCOUNTS_MAX
);
280 /* offset in (compressed) data fork */
281 s
->offsets
[i
] = buff_read_uint64(buffer
, offset
+ 0x18);
282 s
->offsets
[i
] += in_offset
;
284 /* length in (compressed) data fork */
285 s
->lengths
[i
] = buff_read_uint64(buffer
, offset
+ 0x20);
287 if (s
->lengths
[i
] > DMG_LENGTHS_MAX
) {
288 error_report("length %" PRIu64
" for chunk %" PRIu32
289 " is larger than max (%u)",
290 s
->lengths
[i
], i
, DMG_LENGTHS_MAX
);
295 update_max_chunk_size(s
, i
, &ds
->max_compressed_size
,
296 &ds
->max_sectors_per_chunk
);
299 s
->n_chunks
+= chunk_count
;
306 static int dmg_read_resource_fork(BlockDriverState
*bs
, DmgHeaderState
*ds
,
307 uint64_t info_begin
, uint64_t info_length
)
309 BDRVDMGState
*s
= bs
->opaque
;
311 uint32_t count
, rsrc_data_offset
;
312 uint8_t *buffer
= NULL
;
316 /* read offset from begin of resource fork (info_begin) to resource data */
317 ret
= read_uint32(bs
, info_begin
, &rsrc_data_offset
);
320 } else if (rsrc_data_offset
> info_length
) {
325 /* read length of resource data */
326 ret
= read_uint32(bs
, info_begin
+ 8, &count
);
329 } else if (count
== 0 || rsrc_data_offset
+ count
> info_length
) {
334 /* begin of resource data (consisting of one or more resources) */
335 offset
= info_begin
+ rsrc_data_offset
;
337 /* end of resource data (there is possibly a following resource map
338 * which will be ignored). */
339 info_end
= offset
+ count
;
341 /* read offsets (mish blocks) from one or more resources in resource data */
342 while (offset
< info_end
) {
343 /* size of following resource */
344 ret
= read_uint32(bs
, offset
, &count
);
347 } else if (count
== 0 || count
> info_end
- offset
) {
353 buffer
= g_realloc(buffer
, count
);
354 ret
= bdrv_pread(bs
->file
, offset
, buffer
, count
);
359 ret
= dmg_read_mish_block(s
, ds
, buffer
, count
);
363 /* advance offset by size of resource */
373 static int dmg_read_plist_xml(BlockDriverState
*bs
, DmgHeaderState
*ds
,
374 uint64_t info_begin
, uint64_t info_length
)
376 BDRVDMGState
*s
= bs
->opaque
;
378 uint8_t *buffer
= NULL
;
379 char *data_begin
, *data_end
;
381 /* Have at least some length to avoid NULL for g_malloc. Attempt to set a
382 * safe upper cap on the data length. A test sample had a XML length of
384 if (info_length
== 0 || info_length
> 16 * 1024 * 1024) {
389 buffer
= g_malloc(info_length
+ 1);
390 buffer
[info_length
] = '\0';
391 ret
= bdrv_pread(bs
->file
, info_begin
, buffer
, info_length
);
392 if (ret
!= info_length
) {
397 /* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
398 * decode. The actual data element has 431 (0x1af) bytes which includes tabs
400 data_end
= (char *)buffer
;
401 while ((data_begin
= strstr(data_end
, "<data>")) != NULL
) {
406 data_end
= strstr(data_begin
, "</data>");
408 if (data_end
== NULL
) {
413 mish
= g_base64_decode(data_begin
, &out_len
);
414 ret
= dmg_read_mish_block(s
, ds
, mish
, (uint32_t)out_len
);
427 static int dmg_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
430 BDRVDMGState
*s
= bs
->opaque
;
432 uint64_t rsrc_fork_offset
, rsrc_fork_length
;
433 uint64_t plist_xml_offset
, plist_xml_length
;
437 ret
= bdrv_apply_auto_read_only(bs
, NULL
, errp
);
442 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_of_bds
,
443 BDRV_CHILD_IMAGE
, false, errp
);
448 block_module_load_one("dmg-bz2");
449 block_module_load_one("dmg-lzfse");
452 s
->offsets
= s
->lengths
= s
->sectors
= s
->sectorcounts
= NULL
;
453 /* used by dmg_read_mish_block to keep track of the current I/O position */
454 ds
.data_fork_offset
= 0;
455 ds
.max_compressed_size
= 1;
456 ds
.max_sectors_per_chunk
= 1;
458 /* locate the UDIF trailer */
459 offset
= dmg_find_koly_offset(bs
->file
, errp
);
465 /* offset of data fork (DataForkOffset) */
466 ret
= read_uint64(bs
, offset
+ 0x18, &ds
.data_fork_offset
);
469 } else if (ds
.data_fork_offset
> offset
) {
474 /* offset of resource fork (RsrcForkOffset) */
475 ret
= read_uint64(bs
, offset
+ 0x28, &rsrc_fork_offset
);
479 ret
= read_uint64(bs
, offset
+ 0x30, &rsrc_fork_length
);
483 if (rsrc_fork_offset
>= offset
||
484 rsrc_fork_length
> offset
- rsrc_fork_offset
) {
488 /* offset of property list (XMLOffset) */
489 ret
= read_uint64(bs
, offset
+ 0xd8, &plist_xml_offset
);
493 ret
= read_uint64(bs
, offset
+ 0xe0, &plist_xml_length
);
497 if (plist_xml_offset
>= offset
||
498 plist_xml_length
> offset
- plist_xml_offset
) {
502 ret
= read_uint64(bs
, offset
+ 0x1ec, (uint64_t *)&bs
->total_sectors
);
506 if (bs
->total_sectors
< 0) {
510 if (rsrc_fork_length
!= 0) {
511 ret
= dmg_read_resource_fork(bs
, &ds
,
512 rsrc_fork_offset
, rsrc_fork_length
);
516 } else if (plist_xml_length
!= 0) {
517 ret
= dmg_read_plist_xml(bs
, &ds
, plist_xml_offset
, plist_xml_length
);
526 /* initialize zlib engine */
527 s
->compressed_chunk
= qemu_try_blockalign(bs
->file
->bs
,
528 ds
.max_compressed_size
+ 1);
529 s
->uncompressed_chunk
= qemu_try_blockalign(bs
->file
->bs
,
530 512 * ds
.max_sectors_per_chunk
);
531 if (s
->compressed_chunk
== NULL
|| s
->uncompressed_chunk
== NULL
) {
536 if (inflateInit(&s
->zstream
) != Z_OK
) {
541 s
->current_chunk
= s
->n_chunks
;
543 qemu_co_mutex_init(&s
->lock
);
551 g_free(s
->sectorcounts
);
552 qemu_vfree(s
->compressed_chunk
);
553 qemu_vfree(s
->uncompressed_chunk
);
557 static void dmg_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
559 bs
->bl
.request_alignment
= BDRV_SECTOR_SIZE
; /* No sub-sector I/O */
562 static inline int is_sector_in_chunk(BDRVDMGState
* s
,
563 uint32_t chunk_num
, uint64_t sector_num
)
565 if (chunk_num
>= s
->n_chunks
|| s
->sectors
[chunk_num
] > sector_num
||
566 s
->sectors
[chunk_num
] + s
->sectorcounts
[chunk_num
] <= sector_num
) {
573 static inline uint32_t search_chunk(BDRVDMGState
*s
, uint64_t sector_num
)
576 uint32_t chunk1
= 0, chunk2
= s
->n_chunks
, chunk3
;
577 while (chunk1
<= chunk2
) {
578 chunk3
= (chunk1
+ chunk2
) / 2;
579 if (s
->sectors
[chunk3
] > sector_num
) {
584 } else if (s
->sectors
[chunk3
] + s
->sectorcounts
[chunk3
] > sector_num
) {
591 return s
->n_chunks
; /* error */
594 static inline int dmg_read_chunk(BlockDriverState
*bs
, uint64_t sector_num
)
596 BDRVDMGState
*s
= bs
->opaque
;
598 if (!is_sector_in_chunk(s
, s
->current_chunk
, sector_num
)) {
600 uint32_t chunk
= search_chunk(s
, sector_num
);
602 if (chunk
>= s
->n_chunks
) {
606 s
->current_chunk
= s
->n_chunks
;
607 switch (s
->types
[chunk
]) { /* block entry type */
608 case UDZO
: { /* zlib compressed */
609 /* we need to buffer, because only the chunk as whole can be
611 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
612 s
->compressed_chunk
, s
->lengths
[chunk
]);
613 if (ret
!= s
->lengths
[chunk
]) {
617 s
->zstream
.next_in
= s
->compressed_chunk
;
618 s
->zstream
.avail_in
= s
->lengths
[chunk
];
619 s
->zstream
.next_out
= s
->uncompressed_chunk
;
620 s
->zstream
.avail_out
= 512 * s
->sectorcounts
[chunk
];
621 ret
= inflateReset(&s
->zstream
);
625 ret
= inflate(&s
->zstream
, Z_FINISH
);
626 if (ret
!= Z_STREAM_END
||
627 s
->zstream
.total_out
!= 512 * s
->sectorcounts
[chunk
]) {
631 case UDBZ
: /* bzip2 compressed */
632 if (!dmg_uncompress_bz2
) {
635 /* we need to buffer, because only the chunk as whole can be
637 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
638 s
->compressed_chunk
, s
->lengths
[chunk
]);
639 if (ret
!= s
->lengths
[chunk
]) {
643 ret
= dmg_uncompress_bz2((char *)s
->compressed_chunk
,
644 (unsigned int) s
->lengths
[chunk
],
645 (char *)s
->uncompressed_chunk
,
647 (512 * s
->sectorcounts
[chunk
]));
653 if (!dmg_uncompress_lzfse
) {
656 /* we need to buffer, because only the chunk as whole can be
658 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
659 s
->compressed_chunk
, s
->lengths
[chunk
]);
660 if (ret
!= s
->lengths
[chunk
]) {
664 ret
= dmg_uncompress_lzfse((char *)s
->compressed_chunk
,
665 (unsigned int) s
->lengths
[chunk
],
666 (char *)s
->uncompressed_chunk
,
668 (512 * s
->sectorcounts
[chunk
]));
673 case UDRW
: /* copy */
674 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
675 s
->uncompressed_chunk
, s
->lengths
[chunk
]);
676 if (ret
!= s
->lengths
[chunk
]) {
680 case UDZE
: /* zeros */
681 case UDIG
: /* ignore */
682 /* see dmg_read, it is treated specially. No buffer needs to be
683 * pre-filled, the zeroes can be set directly. */
686 s
->current_chunk
= chunk
;
691 static int coroutine_fn
692 dmg_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
693 QEMUIOVector
*qiov
, int flags
)
695 BDRVDMGState
*s
= bs
->opaque
;
696 uint64_t sector_num
= offset
>> BDRV_SECTOR_BITS
;
697 int nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
700 assert(QEMU_IS_ALIGNED(offset
, BDRV_SECTOR_SIZE
));
701 assert(QEMU_IS_ALIGNED(bytes
, BDRV_SECTOR_SIZE
));
703 qemu_co_mutex_lock(&s
->lock
);
705 for (i
= 0; i
< nb_sectors
; i
++) {
706 uint32_t sector_offset_in_chunk
;
709 if (dmg_read_chunk(bs
, sector_num
+ i
) != 0) {
713 /* Special case: current chunk is all zeroes. Do not perform a memcpy as
714 * s->uncompressed_chunk may be too small to cover the large all-zeroes
715 * section. dmg_read_chunk is called to find s->current_chunk */
716 if (s
->types
[s
->current_chunk
] == UDZE
717 || s
->types
[s
->current_chunk
] == UDIG
) { /* all zeroes block entry */
718 qemu_iovec_memset(qiov
, i
* 512, 0, 512);
721 sector_offset_in_chunk
= sector_num
+ i
- s
->sectors
[s
->current_chunk
];
722 data
= s
->uncompressed_chunk
+ sector_offset_in_chunk
* 512;
723 qemu_iovec_from_buf(qiov
, i
* 512, data
, 512);
728 qemu_co_mutex_unlock(&s
->lock
);
732 static void dmg_close(BlockDriverState
*bs
)
734 BDRVDMGState
*s
= bs
->opaque
;
740 g_free(s
->sectorcounts
);
741 qemu_vfree(s
->compressed_chunk
);
742 qemu_vfree(s
->uncompressed_chunk
);
744 inflateEnd(&s
->zstream
);
747 static BlockDriver bdrv_dmg
= {
748 .format_name
= "dmg",
749 .instance_size
= sizeof(BDRVDMGState
),
750 .bdrv_probe
= dmg_probe
,
751 .bdrv_open
= dmg_open
,
752 .bdrv_refresh_limits
= dmg_refresh_limits
,
753 .bdrv_child_perm
= bdrv_default_perms
,
754 .bdrv_co_preadv
= dmg_co_preadv
,
755 .bdrv_close
= dmg_close
,
759 static void bdrv_dmg_init(void)
761 bdrv_register(&bdrv_dmg
);
764 block_init(bdrv_dmg_init
);