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 "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"
33 int (*dmg_uncompress_bz2
)(char *next_in
, unsigned int avail_in
,
34 char *next_out
, unsigned int avail_out
);
37 /* Limit chunk sizes to prevent unreasonable amounts of memory being used
38 * or truncating when converting to 32-bit types
40 DMG_LENGTHS_MAX
= 64 * 1024 * 1024, /* 64 MB */
41 DMG_SECTORCOUNTS_MAX
= DMG_LENGTHS_MAX
/ 512,
44 static int dmg_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
52 len
= strlen(filename
);
53 if (len
> 4 && !strcmp(filename
+ len
- 4, ".dmg")) {
59 static int read_uint64(BlockDriverState
*bs
, int64_t offset
, uint64_t *result
)
64 ret
= bdrv_pread(bs
->file
, offset
, &buffer
, 8);
69 *result
= be64_to_cpu(buffer
);
73 static int read_uint32(BlockDriverState
*bs
, int64_t offset
, uint32_t *result
)
78 ret
= bdrv_pread(bs
->file
, offset
, &buffer
, 4);
83 *result
= be32_to_cpu(buffer
);
87 static inline uint64_t buff_read_uint64(const uint8_t *buffer
, int64_t offset
)
89 return be64_to_cpu(*(uint64_t *)&buffer
[offset
]);
92 static inline uint32_t buff_read_uint32(const uint8_t *buffer
, int64_t offset
)
94 return be32_to_cpu(*(uint32_t *)&buffer
[offset
]);
97 /* Increase max chunk sizes, if necessary. This function is used to calculate
98 * the buffer sizes needed for compressed/uncompressed chunk I/O.
100 static void update_max_chunk_size(BDRVDMGState
*s
, uint32_t chunk
,
101 uint32_t *max_compressed_size
,
102 uint32_t *max_sectors_per_chunk
)
104 uint32_t compressed_size
= 0;
105 uint32_t uncompressed_sectors
= 0;
107 switch (s
->types
[chunk
]) {
108 case 0x80000005: /* zlib compressed */
109 case 0x80000006: /* bzip2 compressed */
110 compressed_size
= s
->lengths
[chunk
];
111 uncompressed_sectors
= s
->sectorcounts
[chunk
];
114 uncompressed_sectors
= DIV_ROUND_UP(s
->lengths
[chunk
], 512);
117 /* as the all-zeroes block may be large, it is treated specially: the
118 * sector is not copied from a large buffer, a simple memset is used
119 * instead. Therefore uncompressed_sectors does not need to be set. */
123 if (compressed_size
> *max_compressed_size
) {
124 *max_compressed_size
= compressed_size
;
126 if (uncompressed_sectors
> *max_sectors_per_chunk
) {
127 *max_sectors_per_chunk
= uncompressed_sectors
;
131 static int64_t dmg_find_koly_offset(BdrvChild
*file
, Error
**errp
)
133 BlockDriverState
*file_bs
= file
->bs
;
139 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
140 * dmg images can have odd sizes, try to look for the "koly" magic which
141 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
142 * in the last 511 bytes of the second-last sector or the first 4 bytes of
143 * the last sector (search space: 515 bytes) */
144 length
= bdrv_getlength(file_bs
);
146 error_setg_errno(errp
, -length
,
147 "Failed to get file size while reading UDIF trailer");
149 } else if (length
< 512) {
150 error_setg(errp
, "dmg file must be at least 512 bytes long");
153 if (length
> 511 + 512) {
154 offset
= length
- 511 - 512;
156 length
= length
< 515 ? length
: 515;
157 ret
= bdrv_pread(file
, offset
, buffer
, length
);
159 error_setg_errno(errp
, -ret
, "Failed while reading UDIF trailer");
162 for (i
= 0; i
< length
- 3; i
++) {
163 if (buffer
[i
] == 'k' && buffer
[i
+1] == 'o' &&
164 buffer
[i
+2] == 'l' && buffer
[i
+3] == 'y') {
168 error_setg(errp
, "Could not locate UDIF trailer in dmg file");
172 /* used when building the sector table */
173 typedef struct DmgHeaderState
{
174 /* used internally by dmg_read_mish_block to remember offsets of blocks
176 uint64_t data_fork_offset
;
177 /* exported for dmg_open */
178 uint32_t max_compressed_size
;
179 uint32_t max_sectors_per_chunk
;
182 static bool dmg_is_known_block_type(uint32_t entry_type
)
184 switch (entry_type
) {
185 case 0x00000001: /* uncompressed */
186 case 0x00000002: /* zeroes */
187 case 0x80000005: /* zlib */
189 case 0x80000006: /* bzip2 */
190 return !!dmg_uncompress_bz2
;
196 static int dmg_read_mish_block(BDRVDMGState
*s
, DmgHeaderState
*ds
,
197 uint8_t *buffer
, uint32_t count
)
202 uint32_t chunk_count
;
204 uint64_t data_offset
;
205 uint64_t in_offset
= ds
->data_fork_offset
;
208 type
= buff_read_uint32(buffer
, offset
);
209 /* skip data that is not a valid MISH block (invalid magic or too small) */
210 if (type
!= 0x6d697368 || count
< 244) {
211 /* assume success for now */
215 /* chunk offsets are relative to this sector number */
216 out_offset
= buff_read_uint64(buffer
, offset
+ 8);
218 /* location in data fork for (compressed) blob (in bytes) */
219 data_offset
= buff_read_uint64(buffer
, offset
+ 0x18);
220 in_offset
+= data_offset
;
222 /* move to begin of chunk entries */
225 chunk_count
= (count
- 204) / 40;
226 new_size
= sizeof(uint64_t) * (s
->n_chunks
+ chunk_count
);
227 s
->types
= g_realloc(s
->types
, new_size
/ 2);
228 s
->offsets
= g_realloc(s
->offsets
, new_size
);
229 s
->lengths
= g_realloc(s
->lengths
, new_size
);
230 s
->sectors
= g_realloc(s
->sectors
, new_size
);
231 s
->sectorcounts
= g_realloc(s
->sectorcounts
, new_size
);
233 for (i
= s
->n_chunks
; i
< s
->n_chunks
+ chunk_count
; i
++) {
234 s
->types
[i
] = buff_read_uint32(buffer
, offset
);
235 if (!dmg_is_known_block_type(s
->types
[i
])) {
243 s
->sectors
[i
] = buff_read_uint64(buffer
, offset
+ 8);
244 s
->sectors
[i
] += out_offset
;
247 s
->sectorcounts
[i
] = buff_read_uint64(buffer
, offset
+ 0x10);
249 /* all-zeroes sector (type 2) does not need to be "uncompressed" and can
250 * therefore be unbounded. */
251 if (s
->types
[i
] != 2 && s
->sectorcounts
[i
] > DMG_SECTORCOUNTS_MAX
) {
252 error_report("sector count %" PRIu64
" for chunk %" PRIu32
253 " is larger than max (%u)",
254 s
->sectorcounts
[i
], i
, DMG_SECTORCOUNTS_MAX
);
259 /* offset in (compressed) data fork */
260 s
->offsets
[i
] = buff_read_uint64(buffer
, offset
+ 0x18);
261 s
->offsets
[i
] += in_offset
;
263 /* length in (compressed) data fork */
264 s
->lengths
[i
] = buff_read_uint64(buffer
, offset
+ 0x20);
266 if (s
->lengths
[i
] > DMG_LENGTHS_MAX
) {
267 error_report("length %" PRIu64
" for chunk %" PRIu32
268 " is larger than max (%u)",
269 s
->lengths
[i
], i
, DMG_LENGTHS_MAX
);
274 update_max_chunk_size(s
, i
, &ds
->max_compressed_size
,
275 &ds
->max_sectors_per_chunk
);
278 s
->n_chunks
+= chunk_count
;
285 static int dmg_read_resource_fork(BlockDriverState
*bs
, DmgHeaderState
*ds
,
286 uint64_t info_begin
, uint64_t info_length
)
288 BDRVDMGState
*s
= bs
->opaque
;
290 uint32_t count
, rsrc_data_offset
;
291 uint8_t *buffer
= NULL
;
295 /* read offset from begin of resource fork (info_begin) to resource data */
296 ret
= read_uint32(bs
, info_begin
, &rsrc_data_offset
);
299 } else if (rsrc_data_offset
> info_length
) {
304 /* read length of resource data */
305 ret
= read_uint32(bs
, info_begin
+ 8, &count
);
308 } else if (count
== 0 || rsrc_data_offset
+ count
> info_length
) {
313 /* begin of resource data (consisting of one or more resources) */
314 offset
= info_begin
+ rsrc_data_offset
;
316 /* end of resource data (there is possibly a following resource map
317 * which will be ignored). */
318 info_end
= offset
+ count
;
320 /* read offsets (mish blocks) from one or more resources in resource data */
321 while (offset
< info_end
) {
322 /* size of following resource */
323 ret
= read_uint32(bs
, offset
, &count
);
326 } else if (count
== 0 || count
> info_end
- offset
) {
332 buffer
= g_realloc(buffer
, count
);
333 ret
= bdrv_pread(bs
->file
, offset
, buffer
, count
);
338 ret
= dmg_read_mish_block(s
, ds
, buffer
, count
);
342 /* advance offset by size of resource */
352 static int dmg_read_plist_xml(BlockDriverState
*bs
, DmgHeaderState
*ds
,
353 uint64_t info_begin
, uint64_t info_length
)
355 BDRVDMGState
*s
= bs
->opaque
;
357 uint8_t *buffer
= NULL
;
358 char *data_begin
, *data_end
;
360 /* Have at least some length to avoid NULL for g_malloc. Attempt to set a
361 * safe upper cap on the data length. A test sample had a XML length of
363 if (info_length
== 0 || info_length
> 16 * 1024 * 1024) {
368 buffer
= g_malloc(info_length
+ 1);
369 buffer
[info_length
] = '\0';
370 ret
= bdrv_pread(bs
->file
, info_begin
, buffer
, info_length
);
371 if (ret
!= info_length
) {
376 /* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
377 * decode. The actual data element has 431 (0x1af) bytes which includes tabs
379 data_end
= (char *)buffer
;
380 while ((data_begin
= strstr(data_end
, "<data>")) != NULL
) {
385 data_end
= strstr(data_begin
, "</data>");
387 if (data_end
== NULL
) {
392 mish
= g_base64_decode(data_begin
, &out_len
);
393 ret
= dmg_read_mish_block(s
, ds
, mish
, (uint32_t)out_len
);
406 static int dmg_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
409 BDRVDMGState
*s
= bs
->opaque
;
411 uint64_t rsrc_fork_offset
, rsrc_fork_length
;
412 uint64_t plist_xml_offset
, plist_xml_length
;
416 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
422 if (!bdrv_is_read_only(bs
)) {
423 error_report("Opening dmg images without an explicit read-only=on "
424 "option is deprecated. Future versions will refuse to "
425 "open the image instead of automatically marking the "
427 ret
= bdrv_set_read_only(bs
, true, errp
);
433 block_module_load_one("dmg-bz2");
436 s
->offsets
= s
->lengths
= s
->sectors
= s
->sectorcounts
= NULL
;
437 /* used by dmg_read_mish_block to keep track of the current I/O position */
438 ds
.data_fork_offset
= 0;
439 ds
.max_compressed_size
= 1;
440 ds
.max_sectors_per_chunk
= 1;
442 /* locate the UDIF trailer */
443 offset
= dmg_find_koly_offset(bs
->file
, errp
);
449 /* offset of data fork (DataForkOffset) */
450 ret
= read_uint64(bs
, offset
+ 0x18, &ds
.data_fork_offset
);
453 } else if (ds
.data_fork_offset
> offset
) {
458 /* offset of resource fork (RsrcForkOffset) */
459 ret
= read_uint64(bs
, offset
+ 0x28, &rsrc_fork_offset
);
463 ret
= read_uint64(bs
, offset
+ 0x30, &rsrc_fork_length
);
467 if (rsrc_fork_offset
>= offset
||
468 rsrc_fork_length
> offset
- rsrc_fork_offset
) {
472 /* offset of property list (XMLOffset) */
473 ret
= read_uint64(bs
, offset
+ 0xd8, &plist_xml_offset
);
477 ret
= read_uint64(bs
, offset
+ 0xe0, &plist_xml_length
);
481 if (plist_xml_offset
>= offset
||
482 plist_xml_length
> offset
- plist_xml_offset
) {
486 ret
= read_uint64(bs
, offset
+ 0x1ec, (uint64_t *)&bs
->total_sectors
);
490 if (bs
->total_sectors
< 0) {
494 if (rsrc_fork_length
!= 0) {
495 ret
= dmg_read_resource_fork(bs
, &ds
,
496 rsrc_fork_offset
, rsrc_fork_length
);
500 } else if (plist_xml_length
!= 0) {
501 ret
= dmg_read_plist_xml(bs
, &ds
, plist_xml_offset
, plist_xml_length
);
510 /* initialize zlib engine */
511 s
->compressed_chunk
= qemu_try_blockalign(bs
->file
->bs
,
512 ds
.max_compressed_size
+ 1);
513 s
->uncompressed_chunk
= qemu_try_blockalign(bs
->file
->bs
,
514 512 * ds
.max_sectors_per_chunk
);
515 if (s
->compressed_chunk
== NULL
|| s
->uncompressed_chunk
== NULL
) {
520 if (inflateInit(&s
->zstream
) != Z_OK
) {
525 s
->current_chunk
= s
->n_chunks
;
527 qemu_co_mutex_init(&s
->lock
);
535 g_free(s
->sectorcounts
);
536 qemu_vfree(s
->compressed_chunk
);
537 qemu_vfree(s
->uncompressed_chunk
);
541 static void dmg_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
543 bs
->bl
.request_alignment
= BDRV_SECTOR_SIZE
; /* No sub-sector I/O */
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
) {
557 static inline uint32_t search_chunk(BDRVDMGState
*s
, uint64_t sector_num
)
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
) {
565 } else if (s
->sectors
[chunk3
] + s
->sectorcounts
[chunk3
] > sector_num
) {
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
)) {
580 uint32_t chunk
= search_chunk(s
, sector_num
);
582 if (chunk
>= s
->n_chunks
) {
586 s
->current_chunk
= s
->n_chunks
;
587 switch (s
->types
[chunk
]) { /* block entry type */
588 case 0x80000005: { /* zlib compressed */
589 /* we need to buffer, because only the chunk as whole can be
591 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
592 s
->compressed_chunk
, s
->lengths
[chunk
]);
593 if (ret
!= s
->lengths
[chunk
]) {
597 s
->zstream
.next_in
= s
->compressed_chunk
;
598 s
->zstream
.avail_in
= s
->lengths
[chunk
];
599 s
->zstream
.next_out
= s
->uncompressed_chunk
;
600 s
->zstream
.avail_out
= 512 * s
->sectorcounts
[chunk
];
601 ret
= inflateReset(&s
->zstream
);
605 ret
= inflate(&s
->zstream
, Z_FINISH
);
606 if (ret
!= Z_STREAM_END
||
607 s
->zstream
.total_out
!= 512 * s
->sectorcounts
[chunk
]) {
611 case 0x80000006: /* bzip2 compressed */
612 if (!dmg_uncompress_bz2
) {
615 /* we need to buffer, because only the chunk as whole can be
617 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
618 s
->compressed_chunk
, s
->lengths
[chunk
]);
619 if (ret
!= s
->lengths
[chunk
]) {
623 ret
= dmg_uncompress_bz2((char *)s
->compressed_chunk
,
624 (unsigned int) s
->lengths
[chunk
],
625 (char *)s
->uncompressed_chunk
,
627 (512 * s
->sectorcounts
[chunk
]));
633 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
634 s
->uncompressed_chunk
, s
->lengths
[chunk
]);
635 if (ret
!= s
->lengths
[chunk
]) {
640 /* see dmg_read, it is treated specially. No buffer needs to be
641 * pre-filled, the zeroes can be set directly. */
644 s
->current_chunk
= chunk
;
649 static int coroutine_fn
650 dmg_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
651 QEMUIOVector
*qiov
, int flags
)
653 BDRVDMGState
*s
= bs
->opaque
;
654 uint64_t sector_num
= offset
>> BDRV_SECTOR_BITS
;
655 int nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
658 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
659 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
661 qemu_co_mutex_lock(&s
->lock
);
663 for (i
= 0; i
< nb_sectors
; i
++) {
664 uint32_t sector_offset_in_chunk
;
667 if (dmg_read_chunk(bs
, sector_num
+ i
) != 0) {
671 /* Special case: current chunk is all zeroes. Do not perform a memcpy as
672 * s->uncompressed_chunk may be too small to cover the large all-zeroes
673 * section. dmg_read_chunk is called to find s->current_chunk */
674 if (s
->types
[s
->current_chunk
] == 2) { /* all zeroes block entry */
675 qemu_iovec_memset(qiov
, i
* 512, 0, 512);
678 sector_offset_in_chunk
= sector_num
+ i
- s
->sectors
[s
->current_chunk
];
679 data
= s
->uncompressed_chunk
+ sector_offset_in_chunk
* 512;
680 qemu_iovec_from_buf(qiov
, i
* 512, data
, 512);
685 qemu_co_mutex_unlock(&s
->lock
);
689 static void dmg_close(BlockDriverState
*bs
)
691 BDRVDMGState
*s
= bs
->opaque
;
697 g_free(s
->sectorcounts
);
698 qemu_vfree(s
->compressed_chunk
);
699 qemu_vfree(s
->uncompressed_chunk
);
701 inflateEnd(&s
->zstream
);
704 static BlockDriver bdrv_dmg
= {
705 .format_name
= "dmg",
706 .instance_size
= sizeof(BDRVDMGState
),
707 .bdrv_probe
= dmg_probe
,
708 .bdrv_open
= dmg_open
,
709 .bdrv_refresh_limits
= dmg_refresh_limits
,
710 .bdrv_child_perm
= bdrv_format_default_perms
,
711 .bdrv_co_preadv
= dmg_co_preadv
,
712 .bdrv_close
= dmg_close
,
715 static void bdrv_dmg_init(void)
717 bdrv_register(&bdrv_dmg
);
720 block_init(bdrv_dmg_init
);