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
);
36 int (*dmg_uncompress_lzfse
)(char *next_in
, unsigned int avail_in
,
37 char *next_out
, unsigned int avail_out
);
40 /* Limit chunk sizes to prevent unreasonable amounts of memory being used
41 * or truncating when converting to 32-bit types
43 DMG_LENGTHS_MAX
= 64 * 1024 * 1024, /* 64 MB */
44 DMG_SECTORCOUNTS_MAX
= DMG_LENGTHS_MAX
/ 512,
47 static int dmg_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
55 len
= strlen(filename
);
56 if (len
> 4 && !strcmp(filename
+ len
- 4, ".dmg")) {
62 static int read_uint64(BlockDriverState
*bs
, int64_t offset
, uint64_t *result
)
67 ret
= bdrv_pread(bs
->file
, offset
, &buffer
, 8);
72 *result
= be64_to_cpu(buffer
);
76 static int read_uint32(BlockDriverState
*bs
, int64_t offset
, uint32_t *result
)
81 ret
= bdrv_pread(bs
->file
, offset
, &buffer
, 4);
86 *result
= be32_to_cpu(buffer
);
90 static inline uint64_t buff_read_uint64(const uint8_t *buffer
, int64_t offset
)
92 return be64_to_cpu(*(uint64_t *)&buffer
[offset
]);
95 static inline uint32_t buff_read_uint32(const uint8_t *buffer
, int64_t offset
)
97 return be32_to_cpu(*(uint32_t *)&buffer
[offset
]);
100 /* Increase max chunk sizes, if necessary. This function is used to calculate
101 * the buffer sizes needed for compressed/uncompressed chunk I/O.
103 static void update_max_chunk_size(BDRVDMGState
*s
, uint32_t chunk
,
104 uint32_t *max_compressed_size
,
105 uint32_t *max_sectors_per_chunk
)
107 uint32_t compressed_size
= 0;
108 uint32_t uncompressed_sectors
= 0;
110 switch (s
->types
[chunk
]) {
111 case 0x80000005: /* zlib compressed */
112 case 0x80000006: /* bzip2 compressed */
113 case 0x80000007: /* lzfse compressed */
114 compressed_size
= s
->lengths
[chunk
];
115 uncompressed_sectors
= s
->sectorcounts
[chunk
];
118 uncompressed_sectors
= DIV_ROUND_UP(s
->lengths
[chunk
], 512);
121 /* as the all-zeroes block may be large, it is treated specially: the
122 * sector is not copied from a large buffer, a simple memset is used
123 * instead. Therefore uncompressed_sectors does not need to be set. */
127 if (compressed_size
> *max_compressed_size
) {
128 *max_compressed_size
= compressed_size
;
130 if (uncompressed_sectors
> *max_sectors_per_chunk
) {
131 *max_sectors_per_chunk
= uncompressed_sectors
;
135 static int64_t dmg_find_koly_offset(BdrvChild
*file
, Error
**errp
)
137 BlockDriverState
*file_bs
= file
->bs
;
143 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
144 * dmg images can have odd sizes, try to look for the "koly" magic which
145 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
146 * in the last 511 bytes of the second-last sector or the first 4 bytes of
147 * the last sector (search space: 515 bytes) */
148 length
= bdrv_getlength(file_bs
);
150 error_setg_errno(errp
, -length
,
151 "Failed to get file size while reading UDIF trailer");
153 } else if (length
< 512) {
154 error_setg(errp
, "dmg file must be at least 512 bytes long");
157 if (length
> 511 + 512) {
158 offset
= length
- 511 - 512;
160 length
= length
< 515 ? length
: 515;
161 ret
= bdrv_pread(file
, offset
, buffer
, length
);
163 error_setg_errno(errp
, -ret
, "Failed while reading UDIF trailer");
166 for (i
= 0; i
< length
- 3; i
++) {
167 if (buffer
[i
] == 'k' && buffer
[i
+1] == 'o' &&
168 buffer
[i
+2] == 'l' && buffer
[i
+3] == 'y') {
172 error_setg(errp
, "Could not locate UDIF trailer in dmg file");
176 /* used when building the sector table */
177 typedef struct DmgHeaderState
{
178 /* used internally by dmg_read_mish_block to remember offsets of blocks
180 uint64_t data_fork_offset
;
181 /* exported for dmg_open */
182 uint32_t max_compressed_size
;
183 uint32_t max_sectors_per_chunk
;
186 static bool dmg_is_known_block_type(uint32_t entry_type
)
188 switch (entry_type
) {
189 case 0x00000001: /* uncompressed */
190 case 0x00000002: /* zeroes */
191 case 0x80000005: /* zlib */
193 case 0x80000006: /* bzip2 */
194 return !!dmg_uncompress_bz2
;
195 case 0x80000007: /* lzfse */
196 return !!dmg_uncompress_lzfse
;
202 static int dmg_read_mish_block(BDRVDMGState
*s
, DmgHeaderState
*ds
,
203 uint8_t *buffer
, uint32_t count
)
208 uint32_t chunk_count
;
210 uint64_t data_offset
;
211 uint64_t in_offset
= ds
->data_fork_offset
;
214 type
= buff_read_uint32(buffer
, offset
);
215 /* skip data that is not a valid MISH block (invalid magic or too small) */
216 if (type
!= 0x6d697368 || count
< 244) {
217 /* assume success for now */
221 /* chunk offsets are relative to this sector number */
222 out_offset
= buff_read_uint64(buffer
, offset
+ 8);
224 /* location in data fork for (compressed) blob (in bytes) */
225 data_offset
= buff_read_uint64(buffer
, offset
+ 0x18);
226 in_offset
+= data_offset
;
228 /* move to begin of chunk entries */
231 chunk_count
= (count
- 204) / 40;
232 new_size
= sizeof(uint64_t) * (s
->n_chunks
+ chunk_count
);
233 s
->types
= g_realloc(s
->types
, new_size
/ 2);
234 s
->offsets
= g_realloc(s
->offsets
, new_size
);
235 s
->lengths
= g_realloc(s
->lengths
, new_size
);
236 s
->sectors
= g_realloc(s
->sectors
, new_size
);
237 s
->sectorcounts
= g_realloc(s
->sectorcounts
, new_size
);
239 for (i
= s
->n_chunks
; i
< s
->n_chunks
+ chunk_count
; i
++) {
240 s
->types
[i
] = buff_read_uint32(buffer
, offset
);
241 if (!dmg_is_known_block_type(s
->types
[i
])) {
249 s
->sectors
[i
] = buff_read_uint64(buffer
, offset
+ 8);
250 s
->sectors
[i
] += out_offset
;
253 s
->sectorcounts
[i
] = buff_read_uint64(buffer
, offset
+ 0x10);
255 /* all-zeroes sector (type 2) does not need to be "uncompressed" and can
256 * therefore be unbounded. */
257 if (s
->types
[i
] != 2 && s
->sectorcounts
[i
] > DMG_SECTORCOUNTS_MAX
) {
258 error_report("sector count %" PRIu64
" for chunk %" PRIu32
259 " is larger than max (%u)",
260 s
->sectorcounts
[i
], i
, DMG_SECTORCOUNTS_MAX
);
265 /* offset in (compressed) data fork */
266 s
->offsets
[i
] = buff_read_uint64(buffer
, offset
+ 0x18);
267 s
->offsets
[i
] += in_offset
;
269 /* length in (compressed) data fork */
270 s
->lengths
[i
] = buff_read_uint64(buffer
, offset
+ 0x20);
272 if (s
->lengths
[i
] > DMG_LENGTHS_MAX
) {
273 error_report("length %" PRIu64
" for chunk %" PRIu32
274 " is larger than max (%u)",
275 s
->lengths
[i
], i
, DMG_LENGTHS_MAX
);
280 update_max_chunk_size(s
, i
, &ds
->max_compressed_size
,
281 &ds
->max_sectors_per_chunk
);
284 s
->n_chunks
+= chunk_count
;
291 static int dmg_read_resource_fork(BlockDriverState
*bs
, DmgHeaderState
*ds
,
292 uint64_t info_begin
, uint64_t info_length
)
294 BDRVDMGState
*s
= bs
->opaque
;
296 uint32_t count
, rsrc_data_offset
;
297 uint8_t *buffer
= NULL
;
301 /* read offset from begin of resource fork (info_begin) to resource data */
302 ret
= read_uint32(bs
, info_begin
, &rsrc_data_offset
);
305 } else if (rsrc_data_offset
> info_length
) {
310 /* read length of resource data */
311 ret
= read_uint32(bs
, info_begin
+ 8, &count
);
314 } else if (count
== 0 || rsrc_data_offset
+ count
> info_length
) {
319 /* begin of resource data (consisting of one or more resources) */
320 offset
= info_begin
+ rsrc_data_offset
;
322 /* end of resource data (there is possibly a following resource map
323 * which will be ignored). */
324 info_end
= offset
+ count
;
326 /* read offsets (mish blocks) from one or more resources in resource data */
327 while (offset
< info_end
) {
328 /* size of following resource */
329 ret
= read_uint32(bs
, offset
, &count
);
332 } else if (count
== 0 || count
> info_end
- offset
) {
338 buffer
= g_realloc(buffer
, count
);
339 ret
= bdrv_pread(bs
->file
, offset
, buffer
, count
);
344 ret
= dmg_read_mish_block(s
, ds
, buffer
, count
);
348 /* advance offset by size of resource */
358 static int dmg_read_plist_xml(BlockDriverState
*bs
, DmgHeaderState
*ds
,
359 uint64_t info_begin
, uint64_t info_length
)
361 BDRVDMGState
*s
= bs
->opaque
;
363 uint8_t *buffer
= NULL
;
364 char *data_begin
, *data_end
;
366 /* Have at least some length to avoid NULL for g_malloc. Attempt to set a
367 * safe upper cap on the data length. A test sample had a XML length of
369 if (info_length
== 0 || info_length
> 16 * 1024 * 1024) {
374 buffer
= g_malloc(info_length
+ 1);
375 buffer
[info_length
] = '\0';
376 ret
= bdrv_pread(bs
->file
, info_begin
, buffer
, info_length
);
377 if (ret
!= info_length
) {
382 /* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
383 * decode. The actual data element has 431 (0x1af) bytes which includes tabs
385 data_end
= (char *)buffer
;
386 while ((data_begin
= strstr(data_end
, "<data>")) != NULL
) {
391 data_end
= strstr(data_begin
, "</data>");
393 if (data_end
== NULL
) {
398 mish
= g_base64_decode(data_begin
, &out_len
);
399 ret
= dmg_read_mish_block(s
, ds
, mish
, (uint32_t)out_len
);
412 static int dmg_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
415 BDRVDMGState
*s
= bs
->opaque
;
417 uint64_t rsrc_fork_offset
, rsrc_fork_length
;
418 uint64_t plist_xml_offset
, plist_xml_length
;
422 ret
= bdrv_apply_auto_read_only(bs
, NULL
, errp
);
427 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
433 block_module_load_one("dmg-bz2");
434 block_module_load_one("dmg-lzfse");
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
);
450 /* offset of data fork (DataForkOffset) */
451 ret
= read_uint64(bs
, offset
+ 0x18, &ds
.data_fork_offset
);
454 } else if (ds
.data_fork_offset
> offset
) {
459 /* offset of resource fork (RsrcForkOffset) */
460 ret
= read_uint64(bs
, offset
+ 0x28, &rsrc_fork_offset
);
464 ret
= read_uint64(bs
, offset
+ 0x30, &rsrc_fork_length
);
468 if (rsrc_fork_offset
>= offset
||
469 rsrc_fork_length
> offset
- rsrc_fork_offset
) {
473 /* offset of property list (XMLOffset) */
474 ret
= read_uint64(bs
, offset
+ 0xd8, &plist_xml_offset
);
478 ret
= read_uint64(bs
, offset
+ 0xe0, &plist_xml_length
);
482 if (plist_xml_offset
>= offset
||
483 plist_xml_length
> offset
- plist_xml_offset
) {
487 ret
= read_uint64(bs
, offset
+ 0x1ec, (uint64_t *)&bs
->total_sectors
);
491 if (bs
->total_sectors
< 0) {
495 if (rsrc_fork_length
!= 0) {
496 ret
= dmg_read_resource_fork(bs
, &ds
,
497 rsrc_fork_offset
, rsrc_fork_length
);
501 } else if (plist_xml_length
!= 0) {
502 ret
= dmg_read_plist_xml(bs
, &ds
, plist_xml_offset
, plist_xml_length
);
511 /* initialize zlib engine */
512 s
->compressed_chunk
= qemu_try_blockalign(bs
->file
->bs
,
513 ds
.max_compressed_size
+ 1);
514 s
->uncompressed_chunk
= qemu_try_blockalign(bs
->file
->bs
,
515 512 * ds
.max_sectors_per_chunk
);
516 if (s
->compressed_chunk
== NULL
|| s
->uncompressed_chunk
== NULL
) {
521 if (inflateInit(&s
->zstream
) != Z_OK
) {
526 s
->current_chunk
= s
->n_chunks
;
528 qemu_co_mutex_init(&s
->lock
);
536 g_free(s
->sectorcounts
);
537 qemu_vfree(s
->compressed_chunk
);
538 qemu_vfree(s
->uncompressed_chunk
);
542 static void dmg_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
544 bs
->bl
.request_alignment
= BDRV_SECTOR_SIZE
; /* No sub-sector I/O */
547 static inline int is_sector_in_chunk(BDRVDMGState
* s
,
548 uint32_t chunk_num
, uint64_t sector_num
)
550 if (chunk_num
>= s
->n_chunks
|| s
->sectors
[chunk_num
] > sector_num
||
551 s
->sectors
[chunk_num
] + s
->sectorcounts
[chunk_num
] <= sector_num
) {
558 static inline uint32_t search_chunk(BDRVDMGState
*s
, uint64_t sector_num
)
561 uint32_t chunk1
= 0, chunk2
= s
->n_chunks
, chunk3
;
562 while (chunk1
!= chunk2
) {
563 chunk3
= (chunk1
+ chunk2
) / 2;
564 if (s
->sectors
[chunk3
] > sector_num
) {
566 } else if (s
->sectors
[chunk3
] + s
->sectorcounts
[chunk3
] > sector_num
) {
572 return s
->n_chunks
; /* error */
575 static inline int dmg_read_chunk(BlockDriverState
*bs
, uint64_t sector_num
)
577 BDRVDMGState
*s
= bs
->opaque
;
579 if (!is_sector_in_chunk(s
, s
->current_chunk
, sector_num
)) {
581 uint32_t chunk
= search_chunk(s
, sector_num
);
583 if (chunk
>= s
->n_chunks
) {
587 s
->current_chunk
= s
->n_chunks
;
588 switch (s
->types
[chunk
]) { /* block entry type */
589 case 0x80000005: { /* zlib compressed */
590 /* we need to buffer, because only the chunk as whole can be
592 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
593 s
->compressed_chunk
, s
->lengths
[chunk
]);
594 if (ret
!= s
->lengths
[chunk
]) {
598 s
->zstream
.next_in
= s
->compressed_chunk
;
599 s
->zstream
.avail_in
= s
->lengths
[chunk
];
600 s
->zstream
.next_out
= s
->uncompressed_chunk
;
601 s
->zstream
.avail_out
= 512 * s
->sectorcounts
[chunk
];
602 ret
= inflateReset(&s
->zstream
);
606 ret
= inflate(&s
->zstream
, Z_FINISH
);
607 if (ret
!= Z_STREAM_END
||
608 s
->zstream
.total_out
!= 512 * s
->sectorcounts
[chunk
]) {
612 case 0x80000006: /* bzip2 compressed */
613 if (!dmg_uncompress_bz2
) {
616 /* we need to buffer, because only the chunk as whole can be
618 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
619 s
->compressed_chunk
, s
->lengths
[chunk
]);
620 if (ret
!= s
->lengths
[chunk
]) {
624 ret
= dmg_uncompress_bz2((char *)s
->compressed_chunk
,
625 (unsigned int) s
->lengths
[chunk
],
626 (char *)s
->uncompressed_chunk
,
628 (512 * s
->sectorcounts
[chunk
]));
634 if (!dmg_uncompress_lzfse
) {
637 /* we need to buffer, because only the chunk as whole can be
639 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
640 s
->compressed_chunk
, s
->lengths
[chunk
]);
641 if (ret
!= s
->lengths
[chunk
]) {
645 ret
= dmg_uncompress_lzfse((char *)s
->compressed_chunk
,
646 (unsigned int) s
->lengths
[chunk
],
647 (char *)s
->uncompressed_chunk
,
649 (512 * s
->sectorcounts
[chunk
]));
655 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
656 s
->uncompressed_chunk
, s
->lengths
[chunk
]);
657 if (ret
!= s
->lengths
[chunk
]) {
662 /* see dmg_read, it is treated specially. No buffer needs to be
663 * pre-filled, the zeroes can be set directly. */
666 s
->current_chunk
= chunk
;
671 static int coroutine_fn
672 dmg_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
673 QEMUIOVector
*qiov
, int flags
)
675 BDRVDMGState
*s
= bs
->opaque
;
676 uint64_t sector_num
= offset
>> BDRV_SECTOR_BITS
;
677 int nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
680 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
681 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
683 qemu_co_mutex_lock(&s
->lock
);
685 for (i
= 0; i
< nb_sectors
; i
++) {
686 uint32_t sector_offset_in_chunk
;
689 if (dmg_read_chunk(bs
, sector_num
+ i
) != 0) {
693 /* Special case: current chunk is all zeroes. Do not perform a memcpy as
694 * s->uncompressed_chunk may be too small to cover the large all-zeroes
695 * section. dmg_read_chunk is called to find s->current_chunk */
696 if (s
->types
[s
->current_chunk
] == 2) { /* all zeroes block entry */
697 qemu_iovec_memset(qiov
, i
* 512, 0, 512);
700 sector_offset_in_chunk
= sector_num
+ i
- s
->sectors
[s
->current_chunk
];
701 data
= s
->uncompressed_chunk
+ sector_offset_in_chunk
* 512;
702 qemu_iovec_from_buf(qiov
, i
* 512, data
, 512);
707 qemu_co_mutex_unlock(&s
->lock
);
711 static void dmg_close(BlockDriverState
*bs
)
713 BDRVDMGState
*s
= bs
->opaque
;
719 g_free(s
->sectorcounts
);
720 qemu_vfree(s
->compressed_chunk
);
721 qemu_vfree(s
->uncompressed_chunk
);
723 inflateEnd(&s
->zstream
);
726 static BlockDriver bdrv_dmg
= {
727 .format_name
= "dmg",
728 .instance_size
= sizeof(BDRVDMGState
),
729 .bdrv_probe
= dmg_probe
,
730 .bdrv_open
= dmg_open
,
731 .bdrv_refresh_limits
= dmg_refresh_limits
,
732 .bdrv_child_perm
= bdrv_format_default_perms
,
733 .bdrv_co_preadv
= dmg_co_preadv
,
734 .bdrv_close
= dmg_close
,
737 static void bdrv_dmg_init(void)
739 bdrv_register(&bdrv_dmg
);
742 block_init(bdrv_dmg_init
);