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"
38 /* Limit chunk sizes to prevent unreasonable amounts of memory being used
39 * or truncating when converting to 32-bit types
41 DMG_LENGTHS_MAX
= 64 * 1024 * 1024, /* 64 MB */
42 DMG_SECTORCOUNTS_MAX
= DMG_LENGTHS_MAX
/ 512,
45 typedef struct BDRVDMGState
{
47 /* each chunk contains a certain number of sectors,
48 * offsets[i] is the offset in the .dmg file,
49 * lengths[i] is the length of the compressed chunk,
50 * sectors[i] is the sector beginning at offsets[i],
51 * sectorcounts[i] is the number of sectors in that chunk,
52 * the sectors array is ordered
60 uint64_t* sectorcounts
;
61 uint32_t current_chunk
;
62 uint8_t *compressed_chunk
;
63 uint8_t *uncompressed_chunk
;
70 static int dmg_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
78 len
= strlen(filename
);
79 if (len
> 4 && !strcmp(filename
+ len
- 4, ".dmg")) {
85 static int read_uint64(BlockDriverState
*bs
, int64_t offset
, uint64_t *result
)
90 ret
= bdrv_pread(bs
->file
->bs
, offset
, &buffer
, 8);
95 *result
= be64_to_cpu(buffer
);
99 static int read_uint32(BlockDriverState
*bs
, int64_t offset
, uint32_t *result
)
104 ret
= bdrv_pread(bs
->file
->bs
, offset
, &buffer
, 4);
109 *result
= be32_to_cpu(buffer
);
113 static inline uint64_t buff_read_uint64(const uint8_t *buffer
, int64_t offset
)
115 return be64_to_cpu(*(uint64_t *)&buffer
[offset
]);
118 static inline uint32_t buff_read_uint32(const uint8_t *buffer
, int64_t offset
)
120 return be32_to_cpu(*(uint32_t *)&buffer
[offset
]);
123 /* Increase max chunk sizes, if necessary. This function is used to calculate
124 * the buffer sizes needed for compressed/uncompressed chunk I/O.
126 static void update_max_chunk_size(BDRVDMGState
*s
, uint32_t chunk
,
127 uint32_t *max_compressed_size
,
128 uint32_t *max_sectors_per_chunk
)
130 uint32_t compressed_size
= 0;
131 uint32_t uncompressed_sectors
= 0;
133 switch (s
->types
[chunk
]) {
134 case 0x80000005: /* zlib compressed */
135 case 0x80000006: /* bzip2 compressed */
136 compressed_size
= s
->lengths
[chunk
];
137 uncompressed_sectors
= s
->sectorcounts
[chunk
];
140 uncompressed_sectors
= (s
->lengths
[chunk
] + 511) / 512;
143 /* as the all-zeroes block may be large, it is treated specially: the
144 * sector is not copied from a large buffer, a simple memset is used
145 * instead. Therefore uncompressed_sectors does not need to be set. */
149 if (compressed_size
> *max_compressed_size
) {
150 *max_compressed_size
= compressed_size
;
152 if (uncompressed_sectors
> *max_sectors_per_chunk
) {
153 *max_sectors_per_chunk
= uncompressed_sectors
;
157 static int64_t dmg_find_koly_offset(BlockDriverState
*file_bs
, Error
**errp
)
164 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
165 * dmg images can have odd sizes, try to look for the "koly" magic which
166 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
167 * in the last 511 bytes of the second-last sector or the first 4 bytes of
168 * the last sector (search space: 515 bytes) */
169 length
= bdrv_getlength(file_bs
);
171 error_setg_errno(errp
, -length
,
172 "Failed to get file size while reading UDIF trailer");
174 } else if (length
< 512) {
175 error_setg(errp
, "dmg file must be at least 512 bytes long");
178 if (length
> 511 + 512) {
179 offset
= length
- 511 - 512;
181 length
= length
< 515 ? length
: 515;
182 ret
= bdrv_pread(file_bs
, offset
, buffer
, length
);
184 error_setg_errno(errp
, -ret
, "Failed while reading UDIF trailer");
187 for (i
= 0; i
< length
- 3; i
++) {
188 if (buffer
[i
] == 'k' && buffer
[i
+1] == 'o' &&
189 buffer
[i
+2] == 'l' && buffer
[i
+3] == 'y') {
193 error_setg(errp
, "Could not locate UDIF trailer in dmg file");
197 /* used when building the sector table */
198 typedef struct DmgHeaderState
{
199 /* used internally by dmg_read_mish_block to remember offsets of blocks
201 uint64_t data_fork_offset
;
202 /* exported for dmg_open */
203 uint32_t max_compressed_size
;
204 uint32_t max_sectors_per_chunk
;
207 static bool dmg_is_known_block_type(uint32_t entry_type
)
209 switch (entry_type
) {
210 case 0x00000001: /* uncompressed */
211 case 0x00000002: /* zeroes */
212 case 0x80000005: /* zlib */
214 case 0x80000006: /* bzip2 */
222 static int dmg_read_mish_block(BDRVDMGState
*s
, DmgHeaderState
*ds
,
223 uint8_t *buffer
, uint32_t count
)
228 uint32_t chunk_count
;
230 uint64_t data_offset
;
231 uint64_t in_offset
= ds
->data_fork_offset
;
234 type
= buff_read_uint32(buffer
, offset
);
235 /* skip data that is not a valid MISH block (invalid magic or too small) */
236 if (type
!= 0x6d697368 || count
< 244) {
237 /* assume success for now */
241 /* chunk offsets are relative to this sector number */
242 out_offset
= buff_read_uint64(buffer
, offset
+ 8);
244 /* location in data fork for (compressed) blob (in bytes) */
245 data_offset
= buff_read_uint64(buffer
, offset
+ 0x18);
246 in_offset
+= data_offset
;
248 /* move to begin of chunk entries */
251 chunk_count
= (count
- 204) / 40;
252 new_size
= sizeof(uint64_t) * (s
->n_chunks
+ chunk_count
);
253 s
->types
= g_realloc(s
->types
, new_size
/ 2);
254 s
->offsets
= g_realloc(s
->offsets
, new_size
);
255 s
->lengths
= g_realloc(s
->lengths
, new_size
);
256 s
->sectors
= g_realloc(s
->sectors
, new_size
);
257 s
->sectorcounts
= g_realloc(s
->sectorcounts
, new_size
);
259 for (i
= s
->n_chunks
; i
< s
->n_chunks
+ chunk_count
; i
++) {
260 s
->types
[i
] = buff_read_uint32(buffer
, offset
);
261 if (!dmg_is_known_block_type(s
->types
[i
])) {
269 s
->sectors
[i
] = buff_read_uint64(buffer
, offset
+ 8);
270 s
->sectors
[i
] += out_offset
;
273 s
->sectorcounts
[i
] = buff_read_uint64(buffer
, offset
+ 0x10);
275 /* all-zeroes sector (type 2) does not need to be "uncompressed" and can
276 * therefore be unbounded. */
277 if (s
->types
[i
] != 2 && s
->sectorcounts
[i
] > DMG_SECTORCOUNTS_MAX
) {
278 error_report("sector count %" PRIu64
" for chunk %" PRIu32
279 " is larger than max (%u)",
280 s
->sectorcounts
[i
], i
, DMG_SECTORCOUNTS_MAX
);
285 /* offset in (compressed) data fork */
286 s
->offsets
[i
] = buff_read_uint64(buffer
, offset
+ 0x18);
287 s
->offsets
[i
] += in_offset
;
289 /* length in (compressed) data fork */
290 s
->lengths
[i
] = buff_read_uint64(buffer
, offset
+ 0x20);
292 if (s
->lengths
[i
] > DMG_LENGTHS_MAX
) {
293 error_report("length %" PRIu64
" for chunk %" PRIu32
294 " is larger than max (%u)",
295 s
->lengths
[i
], i
, DMG_LENGTHS_MAX
);
300 update_max_chunk_size(s
, i
, &ds
->max_compressed_size
,
301 &ds
->max_sectors_per_chunk
);
304 s
->n_chunks
+= chunk_count
;
311 static int dmg_read_resource_fork(BlockDriverState
*bs
, DmgHeaderState
*ds
,
312 uint64_t info_begin
, uint64_t info_length
)
314 BDRVDMGState
*s
= bs
->opaque
;
316 uint32_t count
, rsrc_data_offset
;
317 uint8_t *buffer
= NULL
;
321 /* read offset from begin of resource fork (info_begin) to resource data */
322 ret
= read_uint32(bs
, info_begin
, &rsrc_data_offset
);
325 } else if (rsrc_data_offset
> info_length
) {
330 /* read length of resource data */
331 ret
= read_uint32(bs
, info_begin
+ 8, &count
);
334 } else if (count
== 0 || rsrc_data_offset
+ count
> info_length
) {
339 /* begin of resource data (consisting of one or more resources) */
340 offset
= info_begin
+ rsrc_data_offset
;
342 /* end of resource data (there is possibly a following resource map
343 * which will be ignored). */
344 info_end
= offset
+ count
;
346 /* read offsets (mish blocks) from one or more resources in resource data */
347 while (offset
< info_end
) {
348 /* size of following resource */
349 ret
= read_uint32(bs
, offset
, &count
);
352 } else if (count
== 0 || count
> info_end
- offset
) {
358 buffer
= g_realloc(buffer
, count
);
359 ret
= bdrv_pread(bs
->file
->bs
, offset
, buffer
, count
);
364 ret
= dmg_read_mish_block(s
, ds
, buffer
, count
);
368 /* advance offset by size of resource */
378 static int dmg_read_plist_xml(BlockDriverState
*bs
, DmgHeaderState
*ds
,
379 uint64_t info_begin
, uint64_t info_length
)
381 BDRVDMGState
*s
= bs
->opaque
;
383 uint8_t *buffer
= NULL
;
384 char *data_begin
, *data_end
;
386 /* Have at least some length to avoid NULL for g_malloc. Attempt to set a
387 * safe upper cap on the data length. A test sample had a XML length of
389 if (info_length
== 0 || info_length
> 16 * 1024 * 1024) {
394 buffer
= g_malloc(info_length
+ 1);
395 buffer
[info_length
] = '\0';
396 ret
= bdrv_pread(bs
->file
->bs
, info_begin
, buffer
, info_length
);
397 if (ret
!= info_length
) {
402 /* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
403 * decode. The actual data element has 431 (0x1af) bytes which includes tabs
405 data_end
= (char *)buffer
;
406 while ((data_begin
= strstr(data_end
, "<data>")) != NULL
) {
411 data_end
= strstr(data_begin
, "</data>");
413 if (data_end
== NULL
) {
418 mish
= g_base64_decode(data_begin
, &out_len
);
419 ret
= dmg_read_mish_block(s
, ds
, mish
, (uint32_t)out_len
);
432 static int dmg_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
435 BDRVDMGState
*s
= bs
->opaque
;
437 uint64_t rsrc_fork_offset
, rsrc_fork_length
;
438 uint64_t plist_xml_offset
, plist_xml_length
;
443 bs
->request_alignment
= BDRV_SECTOR_SIZE
; /* No sub-sector I/O supported */
446 s
->offsets
= s
->lengths
= s
->sectors
= s
->sectorcounts
= NULL
;
447 /* used by dmg_read_mish_block to keep track of the current I/O position */
448 ds
.data_fork_offset
= 0;
449 ds
.max_compressed_size
= 1;
450 ds
.max_sectors_per_chunk
= 1;
452 /* locate the UDIF trailer */
453 offset
= dmg_find_koly_offset(bs
->file
->bs
, errp
);
459 /* offset of data fork (DataForkOffset) */
460 ret
= read_uint64(bs
, offset
+ 0x18, &ds
.data_fork_offset
);
463 } else if (ds
.data_fork_offset
> offset
) {
468 /* offset of resource fork (RsrcForkOffset) */
469 ret
= read_uint64(bs
, offset
+ 0x28, &rsrc_fork_offset
);
473 ret
= read_uint64(bs
, offset
+ 0x30, &rsrc_fork_length
);
477 if (rsrc_fork_offset
>= offset
||
478 rsrc_fork_length
> offset
- rsrc_fork_offset
) {
482 /* offset of property list (XMLOffset) */
483 ret
= read_uint64(bs
, offset
+ 0xd8, &plist_xml_offset
);
487 ret
= read_uint64(bs
, offset
+ 0xe0, &plist_xml_length
);
491 if (plist_xml_offset
>= offset
||
492 plist_xml_length
> offset
- plist_xml_offset
) {
496 ret
= read_uint64(bs
, offset
+ 0x1ec, (uint64_t *)&bs
->total_sectors
);
500 if (bs
->total_sectors
< 0) {
504 if (rsrc_fork_length
!= 0) {
505 ret
= dmg_read_resource_fork(bs
, &ds
,
506 rsrc_fork_offset
, rsrc_fork_length
);
510 } else if (plist_xml_length
!= 0) {
511 ret
= dmg_read_plist_xml(bs
, &ds
, plist_xml_offset
, plist_xml_length
);
520 /* initialize zlib engine */
521 s
->compressed_chunk
= qemu_try_blockalign(bs
->file
->bs
,
522 ds
.max_compressed_size
+ 1);
523 s
->uncompressed_chunk
= qemu_try_blockalign(bs
->file
->bs
,
524 512 * ds
.max_sectors_per_chunk
);
525 if (s
->compressed_chunk
== NULL
|| s
->uncompressed_chunk
== NULL
) {
530 if (inflateInit(&s
->zstream
) != Z_OK
) {
535 s
->current_chunk
= s
->n_chunks
;
537 qemu_co_mutex_init(&s
->lock
);
545 g_free(s
->sectorcounts
);
546 qemu_vfree(s
->compressed_chunk
);
547 qemu_vfree(s
->uncompressed_chunk
);
551 static inline int is_sector_in_chunk(BDRVDMGState
* s
,
552 uint32_t chunk_num
, uint64_t sector_num
)
554 if (chunk_num
>= s
->n_chunks
|| s
->sectors
[chunk_num
] > sector_num
||
555 s
->sectors
[chunk_num
] + s
->sectorcounts
[chunk_num
] <= sector_num
) {
562 static inline uint32_t search_chunk(BDRVDMGState
*s
, uint64_t sector_num
)
565 uint32_t chunk1
= 0, chunk2
= s
->n_chunks
, chunk3
;
566 while (chunk1
!= chunk2
) {
567 chunk3
= (chunk1
+ chunk2
) / 2;
568 if (s
->sectors
[chunk3
] > sector_num
) {
570 } else if (s
->sectors
[chunk3
] + s
->sectorcounts
[chunk3
] > sector_num
) {
576 return s
->n_chunks
; /* error */
579 static inline int dmg_read_chunk(BlockDriverState
*bs
, uint64_t sector_num
)
581 BDRVDMGState
*s
= bs
->opaque
;
583 if (!is_sector_in_chunk(s
, s
->current_chunk
, sector_num
)) {
585 uint32_t chunk
= search_chunk(s
, sector_num
);
590 if (chunk
>= s
->n_chunks
) {
594 s
->current_chunk
= s
->n_chunks
;
595 switch (s
->types
[chunk
]) { /* block entry type */
596 case 0x80000005: { /* zlib compressed */
597 /* we need to buffer, because only the chunk as whole can be
599 ret
= bdrv_pread(bs
->file
->bs
, s
->offsets
[chunk
],
600 s
->compressed_chunk
, s
->lengths
[chunk
]);
601 if (ret
!= s
->lengths
[chunk
]) {
605 s
->zstream
.next_in
= s
->compressed_chunk
;
606 s
->zstream
.avail_in
= s
->lengths
[chunk
];
607 s
->zstream
.next_out
= s
->uncompressed_chunk
;
608 s
->zstream
.avail_out
= 512 * s
->sectorcounts
[chunk
];
609 ret
= inflateReset(&s
->zstream
);
613 ret
= inflate(&s
->zstream
, Z_FINISH
);
614 if (ret
!= Z_STREAM_END
||
615 s
->zstream
.total_out
!= 512 * s
->sectorcounts
[chunk
]) {
620 case 0x80000006: /* bzip2 compressed */
621 /* we need to buffer, because only the chunk as whole can be
623 ret
= bdrv_pread(bs
->file
->bs
, s
->offsets
[chunk
],
624 s
->compressed_chunk
, s
->lengths
[chunk
]);
625 if (ret
!= s
->lengths
[chunk
]) {
629 ret
= BZ2_bzDecompressInit(&s
->bzstream
, 0, 0);
633 s
->bzstream
.next_in
= (char *)s
->compressed_chunk
;
634 s
->bzstream
.avail_in
= (unsigned int) s
->lengths
[chunk
];
635 s
->bzstream
.next_out
= (char *)s
->uncompressed_chunk
;
636 s
->bzstream
.avail_out
= (unsigned int) 512 * s
->sectorcounts
[chunk
];
637 ret
= BZ2_bzDecompress(&s
->bzstream
);
638 total_out
= ((uint64_t)s
->bzstream
.total_out_hi32
<< 32) +
639 s
->bzstream
.total_out_lo32
;
640 BZ2_bzDecompressEnd(&s
->bzstream
);
641 if (ret
!= BZ_STREAM_END
||
642 total_out
!= 512 * s
->sectorcounts
[chunk
]) {
646 #endif /* CONFIG_BZIP2 */
648 ret
= bdrv_pread(bs
->file
->bs
, s
->offsets
[chunk
],
649 s
->uncompressed_chunk
, s
->lengths
[chunk
]);
650 if (ret
!= s
->lengths
[chunk
]) {
655 /* see dmg_read, it is treated specially. No buffer needs to be
656 * pre-filled, the zeroes can be set directly. */
659 s
->current_chunk
= chunk
;
664 static int coroutine_fn
665 dmg_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
666 QEMUIOVector
*qiov
, int flags
)
668 BDRVDMGState
*s
= bs
->opaque
;
669 uint64_t sector_num
= offset
>> BDRV_SECTOR_BITS
;
670 int nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
673 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
674 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
676 qemu_co_mutex_lock(&s
->lock
);
678 for (i
= 0; i
< nb_sectors
; i
++) {
679 uint32_t sector_offset_in_chunk
;
682 if (dmg_read_chunk(bs
, sector_num
+ i
) != 0) {
686 /* Special case: current chunk is all zeroes. Do not perform a memcpy as
687 * s->uncompressed_chunk may be too small to cover the large all-zeroes
688 * section. dmg_read_chunk is called to find s->current_chunk */
689 if (s
->types
[s
->current_chunk
] == 2) { /* all zeroes block entry */
690 qemu_iovec_memset(qiov
, i
* 512, 0, 512);
693 sector_offset_in_chunk
= sector_num
+ i
- s
->sectors
[s
->current_chunk
];
694 data
= s
->uncompressed_chunk
+ sector_offset_in_chunk
* 512;
695 qemu_iovec_from_buf(qiov
, i
* 512, data
, 512);
700 qemu_co_mutex_unlock(&s
->lock
);
704 static void dmg_close(BlockDriverState
*bs
)
706 BDRVDMGState
*s
= bs
->opaque
;
712 g_free(s
->sectorcounts
);
713 qemu_vfree(s
->compressed_chunk
);
714 qemu_vfree(s
->uncompressed_chunk
);
716 inflateEnd(&s
->zstream
);
719 static BlockDriver bdrv_dmg
= {
720 .format_name
= "dmg",
721 .instance_size
= sizeof(BDRVDMGState
),
722 .bdrv_probe
= dmg_probe
,
723 .bdrv_open
= dmg_open
,
724 .bdrv_co_preadv
= dmg_co_preadv
,
725 .bdrv_close
= dmg_close
,
728 static void bdrv_dmg_init(void)
730 bdrv_register(&bdrv_dmg
);
733 block_init(bdrv_dmg_init
);