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 /* Increase max chunk sizes, if necessary. This function is used to calculate
104 * the buffer sizes needed for compressed/uncompressed chunk I/O.
106 static void update_max_chunk_size(BDRVDMGState
*s
, uint32_t chunk
,
107 uint32_t *max_compressed_size
,
108 uint32_t *max_sectors_per_chunk
)
110 uint32_t compressed_size
= 0;
111 uint32_t uncompressed_sectors
= 0;
113 switch (s
->types
[chunk
]) {
114 case 0x80000005: /* zlib compressed */
115 compressed_size
= s
->lengths
[chunk
];
116 uncompressed_sectors
= s
->sectorcounts
[chunk
];
119 uncompressed_sectors
= (s
->lengths
[chunk
] + 511) / 512;
122 uncompressed_sectors
= s
->sectorcounts
[chunk
];
126 if (compressed_size
> *max_compressed_size
) {
127 *max_compressed_size
= compressed_size
;
129 if (uncompressed_sectors
> *max_sectors_per_chunk
) {
130 *max_sectors_per_chunk
= uncompressed_sectors
;
134 static int dmg_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
137 BDRVDMGState
*s
= bs
->opaque
;
138 uint64_t info_begin
, info_end
, last_in_offset
, last_out_offset
;
140 uint32_t max_compressed_size
= 1, max_sectors_per_chunk
= 1, i
;
146 s
->offsets
= s
->lengths
= s
->sectors
= s
->sectorcounts
= NULL
;
148 /* read offset of info blocks */
149 offset
= bdrv_getlength(bs
->file
);
156 ret
= read_uint64(bs
, offset
, &info_begin
);
159 } else if (info_begin
== 0) {
164 ret
= read_uint32(bs
, info_begin
, &tmp
);
167 } else if (tmp
!= 0x100) {
172 ret
= read_uint32(bs
, info_begin
+ 4, &count
);
175 } else if (count
== 0) {
179 info_end
= info_begin
+ count
;
181 offset
= info_begin
+ 0x100;
184 last_in_offset
= last_out_offset
= 0;
185 while (offset
< info_end
) {
188 ret
= read_uint32(bs
, offset
, &count
);
191 } else if (count
== 0) {
197 ret
= read_uint32(bs
, offset
, &type
);
202 if (type
== 0x6d697368 && count
>= 244) {
204 uint32_t chunk_count
;
209 chunk_count
= (count
- 204) / 40;
210 new_size
= sizeof(uint64_t) * (s
->n_chunks
+ chunk_count
);
211 s
->types
= g_realloc(s
->types
, new_size
/ 2);
212 s
->offsets
= g_realloc(s
->offsets
, new_size
);
213 s
->lengths
= g_realloc(s
->lengths
, new_size
);
214 s
->sectors
= g_realloc(s
->sectors
, new_size
);
215 s
->sectorcounts
= g_realloc(s
->sectorcounts
, new_size
);
217 for (i
= s
->n_chunks
; i
< s
->n_chunks
+ chunk_count
; i
++) {
218 ret
= read_uint32(bs
, offset
, &s
->types
[i
]);
223 if (s
->types
[i
] != 0x80000005 && s
->types
[i
] != 1 &&
225 if (s
->types
[i
] == 0xffffffff && i
> 0) {
226 last_in_offset
= s
->offsets
[i
- 1] + s
->lengths
[i
- 1];
227 last_out_offset
= s
->sectors
[i
- 1] +
228 s
->sectorcounts
[i
- 1];
237 ret
= read_uint64(bs
, offset
, &s
->sectors
[i
]);
241 s
->sectors
[i
] += last_out_offset
;
244 ret
= read_uint64(bs
, offset
, &s
->sectorcounts
[i
]);
250 if (s
->sectorcounts
[i
] > DMG_SECTORCOUNTS_MAX
) {
251 error_report("sector count %" PRIu64
" for chunk %" PRIu32
252 " is larger than max (%u)",
253 s
->sectorcounts
[i
], i
, DMG_SECTORCOUNTS_MAX
);
258 ret
= read_uint64(bs
, offset
, &s
->offsets
[i
]);
262 s
->offsets
[i
] += last_in_offset
;
265 ret
= read_uint64(bs
, offset
, &s
->lengths
[i
]);
271 if (s
->lengths
[i
] > DMG_LENGTHS_MAX
) {
272 error_report("length %" PRIu64
" for chunk %" PRIu32
273 " is larger than max (%u)",
274 s
->lengths
[i
], i
, DMG_LENGTHS_MAX
);
279 update_max_chunk_size(s
, i
, &max_compressed_size
,
280 &max_sectors_per_chunk
);
282 s
->n_chunks
+= chunk_count
;
286 /* initialize zlib engine */
287 s
->compressed_chunk
= qemu_try_blockalign(bs
->file
,
288 max_compressed_size
+ 1);
289 s
->uncompressed_chunk
= qemu_try_blockalign(bs
->file
,
290 512 * max_sectors_per_chunk
);
291 if (s
->compressed_chunk
== NULL
|| s
->uncompressed_chunk
== NULL
) {
296 if (inflateInit(&s
->zstream
) != Z_OK
) {
301 s
->current_chunk
= s
->n_chunks
;
303 qemu_co_mutex_init(&s
->lock
);
311 g_free(s
->sectorcounts
);
312 qemu_vfree(s
->compressed_chunk
);
313 qemu_vfree(s
->uncompressed_chunk
);
317 static inline int is_sector_in_chunk(BDRVDMGState
* s
,
318 uint32_t chunk_num
, uint64_t sector_num
)
320 if (chunk_num
>= s
->n_chunks
|| s
->sectors
[chunk_num
] > sector_num
||
321 s
->sectors
[chunk_num
] + s
->sectorcounts
[chunk_num
] <= sector_num
) {
328 static inline uint32_t search_chunk(BDRVDMGState
*s
, uint64_t sector_num
)
331 uint32_t chunk1
= 0, chunk2
= s
->n_chunks
, chunk3
;
332 while (chunk1
!= chunk2
) {
333 chunk3
= (chunk1
+ chunk2
) / 2;
334 if (s
->sectors
[chunk3
] > sector_num
) {
336 } else if (s
->sectors
[chunk3
] + s
->sectorcounts
[chunk3
] > sector_num
) {
342 return s
->n_chunks
; /* error */
345 static inline int dmg_read_chunk(BlockDriverState
*bs
, uint64_t sector_num
)
347 BDRVDMGState
*s
= bs
->opaque
;
349 if (!is_sector_in_chunk(s
, s
->current_chunk
, sector_num
)) {
351 uint32_t chunk
= search_chunk(s
, sector_num
);
353 if (chunk
>= s
->n_chunks
) {
357 s
->current_chunk
= s
->n_chunks
;
358 switch (s
->types
[chunk
]) {
359 case 0x80000005: { /* zlib compressed */
360 /* we need to buffer, because only the chunk as whole can be
362 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
363 s
->compressed_chunk
, s
->lengths
[chunk
]);
364 if (ret
!= s
->lengths
[chunk
]) {
368 s
->zstream
.next_in
= s
->compressed_chunk
;
369 s
->zstream
.avail_in
= s
->lengths
[chunk
];
370 s
->zstream
.next_out
= s
->uncompressed_chunk
;
371 s
->zstream
.avail_out
= 512 * s
->sectorcounts
[chunk
];
372 ret
= inflateReset(&s
->zstream
);
376 ret
= inflate(&s
->zstream
, Z_FINISH
);
377 if (ret
!= Z_STREAM_END
||
378 s
->zstream
.total_out
!= 512 * s
->sectorcounts
[chunk
]) {
383 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
384 s
->uncompressed_chunk
, s
->lengths
[chunk
]);
385 if (ret
!= s
->lengths
[chunk
]) {
390 memset(s
->uncompressed_chunk
, 0, 512 * s
->sectorcounts
[chunk
]);
393 s
->current_chunk
= chunk
;
398 static int dmg_read(BlockDriverState
*bs
, int64_t sector_num
,
399 uint8_t *buf
, int nb_sectors
)
401 BDRVDMGState
*s
= bs
->opaque
;
404 for (i
= 0; i
< nb_sectors
; i
++) {
405 uint32_t sector_offset_in_chunk
;
406 if (dmg_read_chunk(bs
, sector_num
+ i
) != 0) {
409 sector_offset_in_chunk
= sector_num
+ i
- s
->sectors
[s
->current_chunk
];
410 memcpy(buf
+ i
* 512,
411 s
->uncompressed_chunk
+ sector_offset_in_chunk
* 512, 512);
416 static coroutine_fn
int dmg_co_read(BlockDriverState
*bs
, int64_t sector_num
,
417 uint8_t *buf
, int nb_sectors
)
420 BDRVDMGState
*s
= bs
->opaque
;
421 qemu_co_mutex_lock(&s
->lock
);
422 ret
= dmg_read(bs
, sector_num
, buf
, nb_sectors
);
423 qemu_co_mutex_unlock(&s
->lock
);
427 static void dmg_close(BlockDriverState
*bs
)
429 BDRVDMGState
*s
= bs
->opaque
;
435 g_free(s
->sectorcounts
);
436 qemu_vfree(s
->compressed_chunk
);
437 qemu_vfree(s
->uncompressed_chunk
);
439 inflateEnd(&s
->zstream
);
442 static BlockDriver bdrv_dmg
= {
443 .format_name
= "dmg",
444 .instance_size
= sizeof(BDRVDMGState
),
445 .bdrv_probe
= dmg_probe
,
446 .bdrv_open
= dmg_open
,
447 .bdrv_read
= dmg_co_read
,
448 .bdrv_close
= dmg_close
,
451 static void bdrv_dmg_init(void)
453 bdrv_register(&bdrv_dmg
);
456 block_init(bdrv_dmg_init
);