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-common.h"
25 #include "block/block_int.h"
26 #include "qemu/bswap.h"
27 #include "qemu/module.h"
31 /* Limit chunk sizes to prevent unreasonable amounts of memory being used
32 * or truncating when converting to 32-bit types
34 DMG_LENGTHS_MAX
= 64 * 1024 * 1024, /* 64 MB */
35 DMG_SECTORCOUNTS_MAX
= DMG_LENGTHS_MAX
/ 512,
38 typedef struct BDRVDMGState
{
40 /* each chunk contains a certain number of sectors,
41 * offsets[i] is the offset in the .dmg file,
42 * lengths[i] is the length of the compressed chunk,
43 * sectors[i] is the sector beginning at offsets[i],
44 * sectorcounts[i] is the number of sectors in that chunk,
45 * the sectors array is ordered
53 uint64_t* sectorcounts
;
54 uint32_t current_chunk
;
55 uint8_t *compressed_chunk
;
56 uint8_t *uncompressed_chunk
;
60 static int dmg_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
68 len
= strlen(filename
);
69 if (len
> 4 && !strcmp(filename
+ len
- 4, ".dmg")) {
75 static int read_uint64(BlockDriverState
*bs
, int64_t offset
, uint64_t *result
)
80 ret
= bdrv_pread(bs
->file
, offset
, &buffer
, 8);
85 *result
= be64_to_cpu(buffer
);
89 static int read_uint32(BlockDriverState
*bs
, int64_t offset
, uint32_t *result
)
94 ret
= bdrv_pread(bs
->file
, offset
, &buffer
, 4);
99 *result
= be32_to_cpu(buffer
);
103 static inline uint64_t buff_read_uint64(const uint8_t *buffer
, int64_t offset
)
105 return be64_to_cpu(*(uint64_t *)&buffer
[offset
]);
108 static inline uint32_t buff_read_uint32(const uint8_t *buffer
, int64_t offset
)
110 return be32_to_cpu(*(uint32_t *)&buffer
[offset
]);
113 /* Increase max chunk sizes, if necessary. This function is used to calculate
114 * the buffer sizes needed for compressed/uncompressed chunk I/O.
116 static void update_max_chunk_size(BDRVDMGState
*s
, uint32_t chunk
,
117 uint32_t *max_compressed_size
,
118 uint32_t *max_sectors_per_chunk
)
120 uint32_t compressed_size
= 0;
121 uint32_t uncompressed_sectors
= 0;
123 switch (s
->types
[chunk
]) {
124 case 0x80000005: /* zlib compressed */
125 compressed_size
= s
->lengths
[chunk
];
126 uncompressed_sectors
= s
->sectorcounts
[chunk
];
129 uncompressed_sectors
= (s
->lengths
[chunk
] + 511) / 512;
132 uncompressed_sectors
= s
->sectorcounts
[chunk
];
136 if (compressed_size
> *max_compressed_size
) {
137 *max_compressed_size
= compressed_size
;
139 if (uncompressed_sectors
> *max_sectors_per_chunk
) {
140 *max_sectors_per_chunk
= uncompressed_sectors
;
144 static int64_t dmg_find_koly_offset(BlockDriverState
*file_bs
, Error
**errp
)
151 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
152 * dmg images can have odd sizes, try to look for the "koly" magic which
153 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
154 * in the last 511 bytes of the second-last sector or the first 4 bytes of
155 * the last sector (search space: 515 bytes) */
156 length
= bdrv_getlength(file_bs
);
158 error_setg_errno(errp
, -length
,
159 "Failed to get file size while reading UDIF trailer");
161 } else if (length
< 512) {
162 error_setg(errp
, "dmg file must be at least 512 bytes long");
165 if (length
> 511 + 512) {
166 offset
= length
- 511 - 512;
168 length
= length
< 515 ? length
: 515;
169 ret
= bdrv_pread(file_bs
, offset
, buffer
, length
);
171 error_setg_errno(errp
, -ret
, "Failed while reading UDIF trailer");
174 for (i
= 0; i
< length
- 3; i
++) {
175 if (buffer
[i
] == 'k' && buffer
[i
+1] == 'o' &&
176 buffer
[i
+2] == 'l' && buffer
[i
+3] == 'y') {
180 error_setg(errp
, "Could not locate UDIF trailer in dmg file");
184 /* used when building the sector table */
185 typedef struct DmgHeaderState
{
186 /* used internally by dmg_read_mish_block to remember offsets of blocks
188 uint64_t last_in_offset
;
189 uint64_t last_out_offset
;
190 /* exported for dmg_open */
191 uint32_t max_compressed_size
;
192 uint32_t max_sectors_per_chunk
;
195 static int dmg_read_mish_block(BDRVDMGState
*s
, DmgHeaderState
*ds
,
196 uint8_t *buffer
, uint32_t count
)
201 uint32_t chunk_count
;
204 type
= buff_read_uint32(buffer
, offset
);
205 /* skip data that is not a valid MISH block (invalid magic or too small) */
206 if (type
!= 0x6d697368 || count
< 244) {
207 /* assume success for now */
214 chunk_count
= (count
- 204) / 40;
215 new_size
= sizeof(uint64_t) * (s
->n_chunks
+ chunk_count
);
216 s
->types
= g_realloc(s
->types
, new_size
/ 2);
217 s
->offsets
= g_realloc(s
->offsets
, new_size
);
218 s
->lengths
= g_realloc(s
->lengths
, new_size
);
219 s
->sectors
= g_realloc(s
->sectors
, new_size
);
220 s
->sectorcounts
= g_realloc(s
->sectorcounts
, new_size
);
222 for (i
= s
->n_chunks
; i
< s
->n_chunks
+ chunk_count
; i
++) {
223 s
->types
[i
] = buff_read_uint32(buffer
, offset
);
225 if (s
->types
[i
] != 0x80000005 && s
->types
[i
] != 1 &&
227 if (s
->types
[i
] == 0xffffffff && i
> 0) {
228 ds
->last_in_offset
= s
->offsets
[i
- 1] + s
->lengths
[i
- 1];
229 ds
->last_out_offset
= s
->sectors
[i
- 1] +
230 s
->sectorcounts
[i
- 1];
239 s
->sectors
[i
] = buff_read_uint64(buffer
, offset
);
240 s
->sectors
[i
] += ds
->last_out_offset
;
243 s
->sectorcounts
[i
] = buff_read_uint64(buffer
, offset
);
246 if (s
->sectorcounts
[i
] > DMG_SECTORCOUNTS_MAX
) {
247 error_report("sector count %" PRIu64
" for chunk %" PRIu32
248 " is larger than max (%u)",
249 s
->sectorcounts
[i
], i
, DMG_SECTORCOUNTS_MAX
);
254 s
->offsets
[i
] = buff_read_uint64(buffer
, offset
);
255 s
->offsets
[i
] += ds
->last_in_offset
;
258 s
->lengths
[i
] = buff_read_uint64(buffer
, offset
);
261 if (s
->lengths
[i
] > DMG_LENGTHS_MAX
) {
262 error_report("length %" PRIu64
" for chunk %" PRIu32
263 " is larger than max (%u)",
264 s
->lengths
[i
], i
, DMG_LENGTHS_MAX
);
269 update_max_chunk_size(s
, i
, &ds
->max_compressed_size
,
270 &ds
->max_sectors_per_chunk
);
272 s
->n_chunks
+= chunk_count
;
279 static int dmg_read_resource_fork(BlockDriverState
*bs
, DmgHeaderState
*ds
,
280 uint64_t info_begin
, uint64_t info_length
)
282 BDRVDMGState
*s
= bs
->opaque
;
284 uint32_t count
, rsrc_data_offset
;
285 uint8_t *buffer
= NULL
;
289 /* read offset from begin of resource fork (info_begin) to resource data */
290 ret
= read_uint32(bs
, info_begin
, &rsrc_data_offset
);
293 } else if (rsrc_data_offset
> info_length
) {
298 /* read length of resource data */
299 ret
= read_uint32(bs
, info_begin
+ 8, &count
);
302 } else if (count
== 0 || rsrc_data_offset
+ count
> info_length
) {
307 /* begin of resource data (consisting of one or more resources) */
308 offset
= info_begin
+ rsrc_data_offset
;
310 /* end of resource data (there is possibly a following resource map
311 * which will be ignored). */
312 info_end
= offset
+ count
;
314 /* read offsets (mish blocks) from one or more resources in resource data */
315 while (offset
< info_end
) {
316 /* size of following resource */
317 ret
= read_uint32(bs
, offset
, &count
);
320 } else if (count
== 0) {
326 buffer
= g_realloc(buffer
, count
);
327 ret
= bdrv_pread(bs
->file
, offset
, buffer
, count
);
332 ret
= dmg_read_mish_block(s
, ds
, buffer
, count
);
336 /* advance offset by size of resource */
346 static int dmg_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
349 BDRVDMGState
*s
= bs
->opaque
;
351 uint64_t rsrc_fork_offset
, rsrc_fork_length
;
357 s
->offsets
= s
->lengths
= s
->sectors
= s
->sectorcounts
= NULL
;
358 /* used by dmg_read_mish_block to keep track of the current I/O position */
359 ds
.last_in_offset
= 0;
360 ds
.last_out_offset
= 0;
361 ds
.max_compressed_size
= 1;
362 ds
.max_sectors_per_chunk
= 1;
364 /* locate the UDIF trailer */
365 offset
= dmg_find_koly_offset(bs
->file
, errp
);
371 /* offset of resource fork (RsrcForkOffset) */
372 ret
= read_uint64(bs
, offset
+ 0x28, &rsrc_fork_offset
);
376 ret
= read_uint64(bs
, offset
+ 0x30, &rsrc_fork_length
);
380 if (rsrc_fork_length
!= 0) {
381 ret
= dmg_read_resource_fork(bs
, &ds
,
382 rsrc_fork_offset
, rsrc_fork_length
);
391 /* initialize zlib engine */
392 s
->compressed_chunk
= qemu_try_blockalign(bs
->file
,
393 ds
.max_compressed_size
+ 1);
394 s
->uncompressed_chunk
= qemu_try_blockalign(bs
->file
,
395 512 * ds
.max_sectors_per_chunk
);
396 if (s
->compressed_chunk
== NULL
|| s
->uncompressed_chunk
== NULL
) {
401 if (inflateInit(&s
->zstream
) != Z_OK
) {
406 s
->current_chunk
= s
->n_chunks
;
408 qemu_co_mutex_init(&s
->lock
);
416 g_free(s
->sectorcounts
);
417 qemu_vfree(s
->compressed_chunk
);
418 qemu_vfree(s
->uncompressed_chunk
);
422 static inline int is_sector_in_chunk(BDRVDMGState
* s
,
423 uint32_t chunk_num
, uint64_t sector_num
)
425 if (chunk_num
>= s
->n_chunks
|| s
->sectors
[chunk_num
] > sector_num
||
426 s
->sectors
[chunk_num
] + s
->sectorcounts
[chunk_num
] <= sector_num
) {
433 static inline uint32_t search_chunk(BDRVDMGState
*s
, uint64_t sector_num
)
436 uint32_t chunk1
= 0, chunk2
= s
->n_chunks
, chunk3
;
437 while (chunk1
!= chunk2
) {
438 chunk3
= (chunk1
+ chunk2
) / 2;
439 if (s
->sectors
[chunk3
] > sector_num
) {
441 } else if (s
->sectors
[chunk3
] + s
->sectorcounts
[chunk3
] > sector_num
) {
447 return s
->n_chunks
; /* error */
450 static inline int dmg_read_chunk(BlockDriverState
*bs
, uint64_t sector_num
)
452 BDRVDMGState
*s
= bs
->opaque
;
454 if (!is_sector_in_chunk(s
, s
->current_chunk
, sector_num
)) {
456 uint32_t chunk
= search_chunk(s
, sector_num
);
458 if (chunk
>= s
->n_chunks
) {
462 s
->current_chunk
= s
->n_chunks
;
463 switch (s
->types
[chunk
]) {
464 case 0x80000005: { /* zlib compressed */
465 /* we need to buffer, because only the chunk as whole can be
467 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
468 s
->compressed_chunk
, s
->lengths
[chunk
]);
469 if (ret
!= s
->lengths
[chunk
]) {
473 s
->zstream
.next_in
= s
->compressed_chunk
;
474 s
->zstream
.avail_in
= s
->lengths
[chunk
];
475 s
->zstream
.next_out
= s
->uncompressed_chunk
;
476 s
->zstream
.avail_out
= 512 * s
->sectorcounts
[chunk
];
477 ret
= inflateReset(&s
->zstream
);
481 ret
= inflate(&s
->zstream
, Z_FINISH
);
482 if (ret
!= Z_STREAM_END
||
483 s
->zstream
.total_out
!= 512 * s
->sectorcounts
[chunk
]) {
488 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
489 s
->uncompressed_chunk
, s
->lengths
[chunk
]);
490 if (ret
!= s
->lengths
[chunk
]) {
495 memset(s
->uncompressed_chunk
, 0, 512 * s
->sectorcounts
[chunk
]);
498 s
->current_chunk
= chunk
;
503 static int dmg_read(BlockDriverState
*bs
, int64_t sector_num
,
504 uint8_t *buf
, int nb_sectors
)
506 BDRVDMGState
*s
= bs
->opaque
;
509 for (i
= 0; i
< nb_sectors
; i
++) {
510 uint32_t sector_offset_in_chunk
;
511 if (dmg_read_chunk(bs
, sector_num
+ i
) != 0) {
514 sector_offset_in_chunk
= sector_num
+ i
- s
->sectors
[s
->current_chunk
];
515 memcpy(buf
+ i
* 512,
516 s
->uncompressed_chunk
+ sector_offset_in_chunk
* 512, 512);
521 static coroutine_fn
int dmg_co_read(BlockDriverState
*bs
, int64_t sector_num
,
522 uint8_t *buf
, int nb_sectors
)
525 BDRVDMGState
*s
= bs
->opaque
;
526 qemu_co_mutex_lock(&s
->lock
);
527 ret
= dmg_read(bs
, sector_num
, buf
, nb_sectors
);
528 qemu_co_mutex_unlock(&s
->lock
);
532 static void dmg_close(BlockDriverState
*bs
)
534 BDRVDMGState
*s
= bs
->opaque
;
540 g_free(s
->sectorcounts
);
541 qemu_vfree(s
->compressed_chunk
);
542 qemu_vfree(s
->uncompressed_chunk
);
544 inflateEnd(&s
->zstream
);
547 static BlockDriver bdrv_dmg
= {
548 .format_name
= "dmg",
549 .instance_size
= sizeof(BDRVDMGState
),
550 .bdrv_probe
= dmg_probe
,
551 .bdrv_open
= dmg_open
,
552 .bdrv_read
= dmg_co_read
,
553 .bdrv_close
= dmg_close
,
556 static void bdrv_dmg_init(void)
558 bdrv_register(&bdrv_dmg
);
561 block_init(bdrv_dmg_init
);