]>
git.proxmox.com Git - mirror_qemu.git/blob - block/dmg.c
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"
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 typedef struct BDRVDMGState
{
46 /* each chunk contains a certain number of sectors,
47 * offsets[i] is the offset in the .dmg file,
48 * lengths[i] is the length of the compressed chunk,
49 * sectors[i] is the sector beginning at offsets[i],
50 * sectorcounts[i] is the number of sectors in that chunk,
51 * the sectors array is ordered
59 uint64_t* sectorcounts
;
60 uint32_t current_chunk
;
61 uint8_t *compressed_chunk
;
62 uint8_t *uncompressed_chunk
;
69 static int dmg_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
77 len
= strlen(filename
);
78 if (len
> 4 && !strcmp(filename
+ len
- 4, ".dmg")) {
84 static int read_uint64(BlockDriverState
*bs
, int64_t offset
, uint64_t *result
)
89 ret
= bdrv_pread(bs
->file
->bs
, offset
, &buffer
, 8);
94 *result
= be64_to_cpu(buffer
);
98 static int read_uint32(BlockDriverState
*bs
, int64_t offset
, uint32_t *result
)
103 ret
= bdrv_pread(bs
->file
->bs
, offset
, &buffer
, 4);
108 *result
= be32_to_cpu(buffer
);
112 static inline uint64_t buff_read_uint64(const uint8_t *buffer
, int64_t offset
)
114 return be64_to_cpu(*(uint64_t *)&buffer
[offset
]);
117 static inline uint32_t buff_read_uint32(const uint8_t *buffer
, int64_t offset
)
119 return be32_to_cpu(*(uint32_t *)&buffer
[offset
]);
122 /* Increase max chunk sizes, if necessary. This function is used to calculate
123 * the buffer sizes needed for compressed/uncompressed chunk I/O.
125 static void update_max_chunk_size(BDRVDMGState
*s
, uint32_t chunk
,
126 uint32_t *max_compressed_size
,
127 uint32_t *max_sectors_per_chunk
)
129 uint32_t compressed_size
= 0;
130 uint32_t uncompressed_sectors
= 0;
132 switch (s
->types
[chunk
]) {
133 case 0x80000005: /* zlib compressed */
134 case 0x80000006: /* bzip2 compressed */
135 compressed_size
= s
->lengths
[chunk
];
136 uncompressed_sectors
= s
->sectorcounts
[chunk
];
139 uncompressed_sectors
= (s
->lengths
[chunk
] + 511) / 512;
142 /* as the all-zeroes block may be large, it is treated specially: the
143 * sector is not copied from a large buffer, a simple memset is used
144 * instead. Therefore uncompressed_sectors does not need to be set. */
148 if (compressed_size
> *max_compressed_size
) {
149 *max_compressed_size
= compressed_size
;
151 if (uncompressed_sectors
> *max_sectors_per_chunk
) {
152 *max_sectors_per_chunk
= uncompressed_sectors
;
156 static int64_t dmg_find_koly_offset(BlockDriverState
*file_bs
, Error
**errp
)
163 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
164 * dmg images can have odd sizes, try to look for the "koly" magic which
165 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
166 * in the last 511 bytes of the second-last sector or the first 4 bytes of
167 * the last sector (search space: 515 bytes) */
168 length
= bdrv_getlength(file_bs
);
170 error_setg_errno(errp
, -length
,
171 "Failed to get file size while reading UDIF trailer");
173 } else if (length
< 512) {
174 error_setg(errp
, "dmg file must be at least 512 bytes long");
177 if (length
> 511 + 512) {
178 offset
= length
- 511 - 512;
180 length
= length
< 515 ? length
: 515;
181 ret
= bdrv_pread(file_bs
, offset
, buffer
, length
);
183 error_setg_errno(errp
, -ret
, "Failed while reading UDIF trailer");
186 for (i
= 0; i
< length
- 3; i
++) {
187 if (buffer
[i
] == 'k' && buffer
[i
+1] == 'o' &&
188 buffer
[i
+2] == 'l' && buffer
[i
+3] == 'y') {
192 error_setg(errp
, "Could not locate UDIF trailer in dmg file");
196 /* used when building the sector table */
197 typedef struct DmgHeaderState
{
198 /* used internally by dmg_read_mish_block to remember offsets of blocks
200 uint64_t data_fork_offset
;
201 /* exported for dmg_open */
202 uint32_t max_compressed_size
;
203 uint32_t max_sectors_per_chunk
;
206 static bool dmg_is_known_block_type(uint32_t entry_type
)
208 switch (entry_type
) {
209 case 0x00000001: /* uncompressed */
210 case 0x00000002: /* zeroes */
211 case 0x80000005: /* zlib */
213 case 0x80000006: /* bzip2 */
221 static int dmg_read_mish_block(BDRVDMGState
*s
, DmgHeaderState
*ds
,
222 uint8_t *buffer
, uint32_t count
)
227 uint32_t chunk_count
;
229 uint64_t data_offset
;
230 uint64_t in_offset
= ds
->data_fork_offset
;
233 type
= buff_read_uint32(buffer
, offset
);
234 /* skip data that is not a valid MISH block (invalid magic or too small) */
235 if (type
!= 0x6d697368 || count
< 244) {
236 /* assume success for now */
240 /* chunk offsets are relative to this sector number */
241 out_offset
= buff_read_uint64(buffer
, offset
+ 8);
243 /* location in data fork for (compressed) blob (in bytes) */
244 data_offset
= buff_read_uint64(buffer
, offset
+ 0x18);
245 in_offset
+= data_offset
;
247 /* move to begin of chunk entries */
250 chunk_count
= (count
- 204) / 40;
251 new_size
= sizeof(uint64_t) * (s
->n_chunks
+ chunk_count
);
252 s
->types
= g_realloc(s
->types
, new_size
/ 2);
253 s
->offsets
= g_realloc(s
->offsets
, new_size
);
254 s
->lengths
= g_realloc(s
->lengths
, new_size
);
255 s
->sectors
= g_realloc(s
->sectors
, new_size
);
256 s
->sectorcounts
= g_realloc(s
->sectorcounts
, new_size
);
258 for (i
= s
->n_chunks
; i
< s
->n_chunks
+ chunk_count
; i
++) {
259 s
->types
[i
] = buff_read_uint32(buffer
, offset
);
260 if (!dmg_is_known_block_type(s
->types
[i
])) {
268 s
->sectors
[i
] = buff_read_uint64(buffer
, offset
+ 8);
269 s
->sectors
[i
] += out_offset
;
272 s
->sectorcounts
[i
] = buff_read_uint64(buffer
, offset
+ 0x10);
274 /* all-zeroes sector (type 2) does not need to be "uncompressed" and can
275 * therefore be unbounded. */
276 if (s
->types
[i
] != 2 && s
->sectorcounts
[i
] > DMG_SECTORCOUNTS_MAX
) {
277 error_report("sector count %" PRIu64
" for chunk %" PRIu32
278 " is larger than max (%u)",
279 s
->sectorcounts
[i
], i
, DMG_SECTORCOUNTS_MAX
);
284 /* offset in (compressed) data fork */
285 s
->offsets
[i
] = buff_read_uint64(buffer
, offset
+ 0x18);
286 s
->offsets
[i
] += in_offset
;
288 /* length in (compressed) data fork */
289 s
->lengths
[i
] = buff_read_uint64(buffer
, offset
+ 0x20);
291 if (s
->lengths
[i
] > DMG_LENGTHS_MAX
) {
292 error_report("length %" PRIu64
" for chunk %" PRIu32
293 " is larger than max (%u)",
294 s
->lengths
[i
], i
, DMG_LENGTHS_MAX
);
299 update_max_chunk_size(s
, i
, &ds
->max_compressed_size
,
300 &ds
->max_sectors_per_chunk
);
303 s
->n_chunks
+= chunk_count
;
310 static int dmg_read_resource_fork(BlockDriverState
*bs
, DmgHeaderState
*ds
,
311 uint64_t info_begin
, uint64_t info_length
)
313 BDRVDMGState
*s
= bs
->opaque
;
315 uint32_t count
, rsrc_data_offset
;
316 uint8_t *buffer
= NULL
;
320 /* read offset from begin of resource fork (info_begin) to resource data */
321 ret
= read_uint32(bs
, info_begin
, &rsrc_data_offset
);
324 } else if (rsrc_data_offset
> info_length
) {
329 /* read length of resource data */
330 ret
= read_uint32(bs
, info_begin
+ 8, &count
);
333 } else if (count
== 0 || rsrc_data_offset
+ count
> info_length
) {
338 /* begin of resource data (consisting of one or more resources) */
339 offset
= info_begin
+ rsrc_data_offset
;
341 /* end of resource data (there is possibly a following resource map
342 * which will be ignored). */
343 info_end
= offset
+ count
;
345 /* read offsets (mish blocks) from one or more resources in resource data */
346 while (offset
< info_end
) {
347 /* size of following resource */
348 ret
= read_uint32(bs
, offset
, &count
);
351 } else if (count
== 0 || count
> info_end
- offset
) {
357 buffer
= g_realloc(buffer
, count
);
358 ret
= bdrv_pread(bs
->file
->bs
, offset
, buffer
, count
);
363 ret
= dmg_read_mish_block(s
, ds
, buffer
, count
);
367 /* advance offset by size of resource */
377 static int dmg_read_plist_xml(BlockDriverState
*bs
, DmgHeaderState
*ds
,
378 uint64_t info_begin
, uint64_t info_length
)
380 BDRVDMGState
*s
= bs
->opaque
;
382 uint8_t *buffer
= NULL
;
383 char *data_begin
, *data_end
;
385 /* Have at least some length to avoid NULL for g_malloc. Attempt to set a
386 * safe upper cap on the data length. A test sample had a XML length of
388 if (info_length
== 0 || info_length
> 16 * 1024 * 1024) {
393 buffer
= g_malloc(info_length
+ 1);
394 buffer
[info_length
] = '\0';
395 ret
= bdrv_pread(bs
->file
->bs
, info_begin
, buffer
, info_length
);
396 if (ret
!= info_length
) {
401 /* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
402 * decode. The actual data element has 431 (0x1af) bytes which includes tabs
404 data_end
= (char *)buffer
;
405 while ((data_begin
= strstr(data_end
, "<data>")) != NULL
) {
410 data_end
= strstr(data_begin
, "</data>");
412 if (data_end
== NULL
) {
417 mish
= g_base64_decode(data_begin
, &out_len
);
418 ret
= dmg_read_mish_block(s
, ds
, mish
, (uint32_t)out_len
);
431 static int dmg_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
434 BDRVDMGState
*s
= bs
->opaque
;
436 uint64_t rsrc_fork_offset
, rsrc_fork_length
;
437 uint64_t plist_xml_offset
, plist_xml_length
;
444 s
->offsets
= s
->lengths
= s
->sectors
= s
->sectorcounts
= NULL
;
445 /* used by dmg_read_mish_block to keep track of the current I/O position */
446 ds
.data_fork_offset
= 0;
447 ds
.max_compressed_size
= 1;
448 ds
.max_sectors_per_chunk
= 1;
450 /* locate the UDIF trailer */
451 offset
= dmg_find_koly_offset(bs
->file
->bs
, errp
);
457 /* offset of data fork (DataForkOffset) */
458 ret
= read_uint64(bs
, offset
+ 0x18, &ds
.data_fork_offset
);
461 } else if (ds
.data_fork_offset
> offset
) {
466 /* offset of resource fork (RsrcForkOffset) */
467 ret
= read_uint64(bs
, offset
+ 0x28, &rsrc_fork_offset
);
471 ret
= read_uint64(bs
, offset
+ 0x30, &rsrc_fork_length
);
475 if (rsrc_fork_offset
>= offset
||
476 rsrc_fork_length
> offset
- rsrc_fork_offset
) {
480 /* offset of property list (XMLOffset) */
481 ret
= read_uint64(bs
, offset
+ 0xd8, &plist_xml_offset
);
485 ret
= read_uint64(bs
, offset
+ 0xe0, &plist_xml_length
);
489 if (plist_xml_offset
>= offset
||
490 plist_xml_length
> offset
- plist_xml_offset
) {
494 ret
= read_uint64(bs
, offset
+ 0x1ec, (uint64_t *)&bs
->total_sectors
);
498 if (bs
->total_sectors
< 0) {
502 if (rsrc_fork_length
!= 0) {
503 ret
= dmg_read_resource_fork(bs
, &ds
,
504 rsrc_fork_offset
, rsrc_fork_length
);
508 } else if (plist_xml_length
!= 0) {
509 ret
= dmg_read_plist_xml(bs
, &ds
, plist_xml_offset
, plist_xml_length
);
518 /* initialize zlib engine */
519 s
->compressed_chunk
= qemu_try_blockalign(bs
->file
->bs
,
520 ds
.max_compressed_size
+ 1);
521 s
->uncompressed_chunk
= qemu_try_blockalign(bs
->file
->bs
,
522 512 * ds
.max_sectors_per_chunk
);
523 if (s
->compressed_chunk
== NULL
|| s
->uncompressed_chunk
== NULL
) {
528 if (inflateInit(&s
->zstream
) != Z_OK
) {
533 s
->current_chunk
= s
->n_chunks
;
535 qemu_co_mutex_init(&s
->lock
);
543 g_free(s
->sectorcounts
);
544 qemu_vfree(s
->compressed_chunk
);
545 qemu_vfree(s
->uncompressed_chunk
);
549 static void dmg_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
551 bs
->request_alignment
= BDRV_SECTOR_SIZE
; /* No sub-sector I/O supported */
554 static inline int is_sector_in_chunk(BDRVDMGState
* s
,
555 uint32_t chunk_num
, uint64_t sector_num
)
557 if (chunk_num
>= s
->n_chunks
|| s
->sectors
[chunk_num
] > sector_num
||
558 s
->sectors
[chunk_num
] + s
->sectorcounts
[chunk_num
] <= sector_num
) {
565 static inline uint32_t search_chunk(BDRVDMGState
*s
, uint64_t sector_num
)
568 uint32_t chunk1
= 0, chunk2
= s
->n_chunks
, chunk3
;
569 while (chunk1
!= chunk2
) {
570 chunk3
= (chunk1
+ chunk2
) / 2;
571 if (s
->sectors
[chunk3
] > sector_num
) {
573 } else if (s
->sectors
[chunk3
] + s
->sectorcounts
[chunk3
] > sector_num
) {
579 return s
->n_chunks
; /* error */
582 static inline int dmg_read_chunk(BlockDriverState
*bs
, uint64_t sector_num
)
584 BDRVDMGState
*s
= bs
->opaque
;
586 if (!is_sector_in_chunk(s
, s
->current_chunk
, sector_num
)) {
588 uint32_t chunk
= search_chunk(s
, sector_num
);
593 if (chunk
>= s
->n_chunks
) {
597 s
->current_chunk
= s
->n_chunks
;
598 switch (s
->types
[chunk
]) { /* block entry type */
599 case 0x80000005: { /* zlib compressed */
600 /* we need to buffer, because only the chunk as whole can be
602 ret
= bdrv_pread(bs
->file
->bs
, s
->offsets
[chunk
],
603 s
->compressed_chunk
, s
->lengths
[chunk
]);
604 if (ret
!= s
->lengths
[chunk
]) {
608 s
->zstream
.next_in
= s
->compressed_chunk
;
609 s
->zstream
.avail_in
= s
->lengths
[chunk
];
610 s
->zstream
.next_out
= s
->uncompressed_chunk
;
611 s
->zstream
.avail_out
= 512 * s
->sectorcounts
[chunk
];
612 ret
= inflateReset(&s
->zstream
);
616 ret
= inflate(&s
->zstream
, Z_FINISH
);
617 if (ret
!= Z_STREAM_END
||
618 s
->zstream
.total_out
!= 512 * s
->sectorcounts
[chunk
]) {
623 case 0x80000006: /* bzip2 compressed */
624 /* we need to buffer, because only the chunk as whole can be
626 ret
= bdrv_pread(bs
->file
->bs
, s
->offsets
[chunk
],
627 s
->compressed_chunk
, s
->lengths
[chunk
]);
628 if (ret
!= s
->lengths
[chunk
]) {
632 ret
= BZ2_bzDecompressInit(&s
->bzstream
, 0, 0);
636 s
->bzstream
.next_in
= (char *)s
->compressed_chunk
;
637 s
->bzstream
.avail_in
= (unsigned int) s
->lengths
[chunk
];
638 s
->bzstream
.next_out
= (char *)s
->uncompressed_chunk
;
639 s
->bzstream
.avail_out
= (unsigned int) 512 * s
->sectorcounts
[chunk
];
640 ret
= BZ2_bzDecompress(&s
->bzstream
);
641 total_out
= ((uint64_t)s
->bzstream
.total_out_hi32
<< 32) +
642 s
->bzstream
.total_out_lo32
;
643 BZ2_bzDecompressEnd(&s
->bzstream
);
644 if (ret
!= BZ_STREAM_END
||
645 total_out
!= 512 * s
->sectorcounts
[chunk
]) {
649 #endif /* CONFIG_BZIP2 */
651 ret
= bdrv_pread(bs
->file
->bs
, s
->offsets
[chunk
],
652 s
->uncompressed_chunk
, s
->lengths
[chunk
]);
653 if (ret
!= s
->lengths
[chunk
]) {
658 /* see dmg_read, it is treated specially. No buffer needs to be
659 * pre-filled, the zeroes can be set directly. */
662 s
->current_chunk
= chunk
;
667 static int coroutine_fn
668 dmg_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
669 QEMUIOVector
*qiov
, int flags
)
671 BDRVDMGState
*s
= bs
->opaque
;
672 uint64_t sector_num
= offset
>> BDRV_SECTOR_BITS
;
673 int nb_sectors
= bytes
>> BDRV_SECTOR_BITS
;
676 assert((offset
& (BDRV_SECTOR_SIZE
- 1)) == 0);
677 assert((bytes
& (BDRV_SECTOR_SIZE
- 1)) == 0);
679 qemu_co_mutex_lock(&s
->lock
);
681 for (i
= 0; i
< nb_sectors
; i
++) {
682 uint32_t sector_offset_in_chunk
;
685 if (dmg_read_chunk(bs
, sector_num
+ i
) != 0) {
689 /* Special case: current chunk is all zeroes. Do not perform a memcpy as
690 * s->uncompressed_chunk may be too small to cover the large all-zeroes
691 * section. dmg_read_chunk is called to find s->current_chunk */
692 if (s
->types
[s
->current_chunk
] == 2) { /* all zeroes block entry */
693 qemu_iovec_memset(qiov
, i
* 512, 0, 512);
696 sector_offset_in_chunk
= sector_num
+ i
- s
->sectors
[s
->current_chunk
];
697 data
= s
->uncompressed_chunk
+ sector_offset_in_chunk
* 512;
698 qemu_iovec_from_buf(qiov
, i
* 512, data
, 512);
703 qemu_co_mutex_unlock(&s
->lock
);
707 static void dmg_close(BlockDriverState
*bs
)
709 BDRVDMGState
*s
= bs
->opaque
;
715 g_free(s
->sectorcounts
);
716 qemu_vfree(s
->compressed_chunk
);
717 qemu_vfree(s
->uncompressed_chunk
);
719 inflateEnd(&s
->zstream
);
722 static BlockDriver bdrv_dmg
= {
723 .format_name
= "dmg",
724 .instance_size
= sizeof(BDRVDMGState
),
725 .bdrv_probe
= dmg_probe
,
726 .bdrv_open
= dmg_open
,
727 .bdrv_refresh_limits
= dmg_refresh_limits
,
728 .bdrv_co_preadv
= dmg_co_preadv
,
729 .bdrv_close
= dmg_close
,
732 static void bdrv_dmg_init(void)
734 bdrv_register(&bdrv_dmg
);
737 block_init(bdrv_dmg_init
);