2004-12-12 14:24:44 +03:00
|
|
|
/*
|
|
|
|
* QEMU Block driver for DMG images
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2004-12-12 14:24:44 +03:00
|
|
|
* Copyright (c) 2004 Johannes E. Schindelin
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2004-12-12 14:24:44 +03:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2007-11-11 05:51:17 +03:00
|
|
|
#include "qemu-common.h"
|
2012-12-17 21:19:44 +04:00
|
|
|
#include "block/block_int.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/bswap.h"
|
|
|
|
#include "qemu/module.h"
|
2004-12-12 14:24:44 +03:00
|
|
|
#include <zlib.h>
|
|
|
|
|
2014-03-26 16:05:58 +04:00
|
|
|
enum {
|
|
|
|
/* Limit chunk sizes to prevent unreasonable amounts of memory being used
|
|
|
|
* or truncating when converting to 32-bit types
|
|
|
|
*/
|
|
|
|
DMG_LENGTHS_MAX = 64 * 1024 * 1024, /* 64 MB */
|
|
|
|
DMG_SECTORCOUNTS_MAX = DMG_LENGTHS_MAX / 512,
|
|
|
|
};
|
|
|
|
|
2004-12-12 14:24:44 +03:00
|
|
|
typedef struct BDRVDMGState {
|
2011-10-20 15:16:21 +04:00
|
|
|
CoMutex lock;
|
2004-12-12 14:24:44 +03:00
|
|
|
/* each chunk contains a certain number of sectors,
|
|
|
|
* offsets[i] is the offset in the .dmg file,
|
|
|
|
* lengths[i] is the length of the compressed chunk,
|
|
|
|
* sectors[i] is the sector beginning at offsets[i],
|
|
|
|
* sectorcounts[i] is the number of sectors in that chunk,
|
|
|
|
* the sectors array is ordered
|
|
|
|
* 0<=i<n_chunks */
|
|
|
|
|
|
|
|
uint32_t n_chunks;
|
|
|
|
uint32_t* types;
|
|
|
|
uint64_t* offsets;
|
|
|
|
uint64_t* lengths;
|
|
|
|
uint64_t* sectors;
|
|
|
|
uint64_t* sectorcounts;
|
|
|
|
uint32_t current_chunk;
|
2006-02-02 00:43:52 +03:00
|
|
|
uint8_t *compressed_chunk;
|
|
|
|
uint8_t *uncompressed_chunk;
|
2004-12-12 14:24:44 +03:00
|
|
|
z_stream zstream;
|
|
|
|
} BDRVDMGState;
|
|
|
|
|
|
|
|
static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
|
|
|
|
{
|
2013-03-18 19:20:27 +04:00
|
|
|
int len;
|
|
|
|
|
|
|
|
if (!filename) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(filename);
|
|
|
|
if (len > 4 && !strcmp(filename + len - 4, ".dmg")) {
|
|
|
|
return 2;
|
|
|
|
}
|
2004-12-12 14:24:44 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-25 20:07:30 +04:00
|
|
|
static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
|
2004-12-12 14:24:44 +03:00
|
|
|
{
|
2013-01-25 20:07:30 +04:00
|
|
|
uint64_t buffer;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = bdrv_pread(bs->file, offset, &buffer, 8);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = be64_to_cpu(buffer);
|
|
|
|
return 0;
|
2004-12-12 14:24:44 +03:00
|
|
|
}
|
|
|
|
|
2013-01-25 20:07:30 +04:00
|
|
|
static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
|
2004-12-12 14:24:44 +03:00
|
|
|
{
|
2013-01-25 20:07:30 +04:00
|
|
|
uint32_t buffer;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = bdrv_pread(bs->file, offset, &buffer, 4);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = be32_to_cpu(buffer);
|
|
|
|
return 0;
|
2004-12-12 14:24:44 +03:00
|
|
|
}
|
|
|
|
|
2014-03-26 16:06:00 +04:00
|
|
|
/* Increase max chunk sizes, if necessary. This function is used to calculate
|
|
|
|
* the buffer sizes needed for compressed/uncompressed chunk I/O.
|
|
|
|
*/
|
|
|
|
static void update_max_chunk_size(BDRVDMGState *s, uint32_t chunk,
|
|
|
|
uint32_t *max_compressed_size,
|
|
|
|
uint32_t *max_sectors_per_chunk)
|
|
|
|
{
|
|
|
|
uint32_t compressed_size = 0;
|
|
|
|
uint32_t uncompressed_sectors = 0;
|
|
|
|
|
|
|
|
switch (s->types[chunk]) {
|
|
|
|
case 0x80000005: /* zlib compressed */
|
|
|
|
compressed_size = s->lengths[chunk];
|
|
|
|
uncompressed_sectors = s->sectorcounts[chunk];
|
|
|
|
break;
|
|
|
|
case 1: /* copy */
|
|
|
|
uncompressed_sectors = (s->lengths[chunk] + 511) / 512;
|
|
|
|
break;
|
|
|
|
case 2: /* zero */
|
|
|
|
uncompressed_sectors = s->sectorcounts[chunk];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compressed_size > *max_compressed_size) {
|
|
|
|
*max_compressed_size = compressed_size;
|
|
|
|
}
|
|
|
|
if (uncompressed_sectors > *max_sectors_per_chunk) {
|
|
|
|
*max_sectors_per_chunk = uncompressed_sectors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 16:22:29 +04:00
|
|
|
static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
2004-12-12 14:24:44 +03:00
|
|
|
{
|
|
|
|
BDRVDMGState *s = bs->opaque;
|
2014-03-26 16:05:54 +04:00
|
|
|
uint64_t info_begin, info_end, last_in_offset, last_out_offset;
|
2013-01-25 20:07:30 +04:00
|
|
|
uint32_t count, tmp;
|
2014-03-26 16:05:54 +04:00
|
|
|
uint32_t max_compressed_size = 1, max_sectors_per_chunk = 1, i;
|
2010-05-12 18:31:35 +04:00
|
|
|
int64_t offset;
|
2013-01-25 20:07:30 +04:00
|
|
|
int ret;
|
2004-12-12 14:24:44 +03:00
|
|
|
|
|
|
|
bs->read_only = 1;
|
|
|
|
s->n_chunks = 0;
|
2009-03-07 18:32:56 +03:00
|
|
|
s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2004-12-12 14:24:44 +03:00
|
|
|
/* read offset of info blocks */
|
2010-05-12 18:31:49 +04:00
|
|
|
offset = bdrv_getlength(bs->file);
|
2010-05-12 18:31:35 +04:00
|
|
|
if (offset < 0) {
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = offset;
|
2010-01-11 16:06:54 +03:00
|
|
|
goto fail;
|
2004-12-12 14:24:44 +03:00
|
|
|
}
|
2010-05-12 18:31:49 +04:00
|
|
|
offset -= 0x1d8;
|
2010-01-11 16:06:54 +03:00
|
|
|
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = read_uint64(bs, offset, &info_begin);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
} else if (info_begin == 0) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
2010-05-12 18:31:35 +04:00
|
|
|
}
|
|
|
|
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = read_uint32(bs, info_begin, &tmp);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
} else if (tmp != 0x100) {
|
|
|
|
ret = -EINVAL;
|
2010-05-12 18:31:35 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = read_uint32(bs, info_begin + 4, &count);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
} else if (count == 0) {
|
|
|
|
ret = -EINVAL;
|
2010-05-12 18:31:35 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
info_end = info_begin + count;
|
|
|
|
|
|
|
|
offset = info_begin + 0x100;
|
2004-12-12 14:24:44 +03:00
|
|
|
|
|
|
|
/* read offsets */
|
|
|
|
last_in_offset = last_out_offset = 0;
|
2010-05-12 18:31:35 +04:00
|
|
|
while (offset < info_end) {
|
2005-01-04 02:39:08 +03:00
|
|
|
uint32_t type;
|
|
|
|
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = read_uint32(bs, offset, &count);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
} else if (count == 0) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2010-05-12 18:31:35 +04:00
|
|
|
offset += 4;
|
|
|
|
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = read_uint32(bs, offset, &type);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-03-26 16:05:54 +04:00
|
|
|
if (type == 0x6d697368 && count >= 244) {
|
2014-03-26 16:05:57 +04:00
|
|
|
size_t new_size;
|
|
|
|
uint32_t chunk_count;
|
2010-05-12 18:31:35 +04:00
|
|
|
|
|
|
|
offset += 4;
|
|
|
|
offset += 200;
|
|
|
|
|
2014-03-26 16:05:54 +04:00
|
|
|
chunk_count = (count - 204) / 40;
|
|
|
|
new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
|
|
|
|
s->types = g_realloc(s->types, new_size / 2);
|
|
|
|
s->offsets = g_realloc(s->offsets, new_size);
|
|
|
|
s->lengths = g_realloc(s->lengths, new_size);
|
|
|
|
s->sectors = g_realloc(s->sectors, new_size);
|
|
|
|
s->sectorcounts = g_realloc(s->sectorcounts, new_size);
|
2004-12-12 14:24:44 +03:00
|
|
|
|
2013-01-25 20:07:30 +04:00
|
|
|
for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
|
|
|
|
ret = read_uint32(bs, offset, &s->types[i]);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2014-03-26 16:05:54 +04:00
|
|
|
offset += 4;
|
|
|
|
if (s->types[i] != 0x80000005 && s->types[i] != 1 &&
|
|
|
|
s->types[i] != 2) {
|
2014-03-26 16:05:55 +04:00
|
|
|
if (s->types[i] == 0xffffffff && i > 0) {
|
2014-03-26 16:05:54 +04:00
|
|
|
last_in_offset = s->offsets[i - 1] + s->lengths[i - 1];
|
|
|
|
last_out_offset = s->sectors[i - 1] +
|
|
|
|
s->sectorcounts[i - 1];
|
|
|
|
}
|
|
|
|
chunk_count--;
|
|
|
|
i--;
|
|
|
|
offset += 36;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
offset += 4;
|
2010-05-12 18:31:35 +04:00
|
|
|
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = read_uint64(bs, offset, &s->sectors[i]);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->sectors[i] += last_out_offset;
|
|
|
|
offset += 8;
|
2010-05-12 18:31:35 +04:00
|
|
|
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = read_uint64(bs, offset, &s->sectorcounts[i]);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
offset += 8;
|
2010-05-12 18:31:35 +04:00
|
|
|
|
2014-03-26 16:05:58 +04:00
|
|
|
if (s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
|
2014-04-29 21:03:12 +04:00
|
|
|
error_report("sector count %" PRIu64 " for chunk %" PRIu32
|
|
|
|
" is larger than max (%u)",
|
2014-03-26 16:05:58 +04:00
|
|
|
s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = read_uint64(bs, offset, &s->offsets[i]);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
s->offsets[i] += last_in_offset;
|
|
|
|
offset += 8;
|
2010-05-12 18:31:35 +04:00
|
|
|
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = read_uint64(bs, offset, &s->lengths[i]);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
offset += 8;
|
2010-05-12 18:31:35 +04:00
|
|
|
|
2014-03-26 16:05:58 +04:00
|
|
|
if (s->lengths[i] > DMG_LENGTHS_MAX) {
|
2014-04-29 21:03:12 +04:00
|
|
|
error_report("length %" PRIu64 " for chunk %" PRIu32
|
|
|
|
" is larger than max (%u)",
|
2014-03-26 16:05:58 +04:00
|
|
|
s->lengths[i], i, DMG_LENGTHS_MAX);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-03-26 16:06:00 +04:00
|
|
|
update_max_chunk_size(s, i, &max_compressed_size,
|
|
|
|
&max_sectors_per_chunk);
|
2014-03-26 16:05:54 +04:00
|
|
|
}
|
|
|
|
s->n_chunks += chunk_count;
|
|
|
|
}
|
2004-12-12 14:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize zlib engine */
|
2014-05-20 15:28:14 +04:00
|
|
|
s->compressed_chunk = qemu_try_blockalign(bs->file,
|
|
|
|
max_compressed_size + 1);
|
|
|
|
s->uncompressed_chunk = qemu_try_blockalign(bs->file,
|
|
|
|
512 * max_sectors_per_chunk);
|
|
|
|
if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-03-26 16:05:54 +04:00
|
|
|
if (inflateInit(&s->zstream) != Z_OK) {
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2004-12-12 14:24:44 +03:00
|
|
|
|
|
|
|
s->current_chunk = s->n_chunks;
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2011-10-20 15:16:21 +04:00
|
|
|
qemu_co_mutex_init(&s->lock);
|
2004-12-12 14:24:44 +03:00
|
|
|
return 0;
|
2013-01-25 20:07:30 +04:00
|
|
|
|
2010-01-11 16:06:54 +03:00
|
|
|
fail:
|
2013-01-25 20:07:30 +04:00
|
|
|
g_free(s->types);
|
|
|
|
g_free(s->offsets);
|
|
|
|
g_free(s->lengths);
|
|
|
|
g_free(s->sectors);
|
|
|
|
g_free(s->sectorcounts);
|
2014-05-20 15:28:14 +04:00
|
|
|
qemu_vfree(s->compressed_chunk);
|
|
|
|
qemu_vfree(s->uncompressed_chunk);
|
2013-01-25 20:07:30 +04:00
|
|
|
return ret;
|
2004-12-12 14:24:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int is_sector_in_chunk(BDRVDMGState* s,
|
2014-03-26 16:05:59 +04:00
|
|
|
uint32_t chunk_num, uint64_t sector_num)
|
2004-12-12 14:24:44 +03:00
|
|
|
{
|
2014-03-26 16:05:54 +04:00
|
|
|
if (chunk_num >= s->n_chunks || s->sectors[chunk_num] > sector_num ||
|
|
|
|
s->sectors[chunk_num] + s->sectorcounts[chunk_num] <= sector_num) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2004-12-12 14:24:44 +03:00
|
|
|
}
|
|
|
|
|
2014-03-26 16:05:59 +04:00
|
|
|
static inline uint32_t search_chunk(BDRVDMGState *s, uint64_t sector_num)
|
2004-12-12 14:24:44 +03:00
|
|
|
{
|
|
|
|
/* binary search */
|
2014-03-26 16:05:54 +04:00
|
|
|
uint32_t chunk1 = 0, chunk2 = s->n_chunks, chunk3;
|
|
|
|
while (chunk1 != chunk2) {
|
|
|
|
chunk3 = (chunk1 + chunk2) / 2;
|
|
|
|
if (s->sectors[chunk3] > sector_num) {
|
|
|
|
chunk2 = chunk3;
|
|
|
|
} else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) {
|
|
|
|
return chunk3;
|
|
|
|
} else {
|
|
|
|
chunk1 = chunk3;
|
|
|
|
}
|
2004-12-12 14:24:44 +03:00
|
|
|
}
|
|
|
|
return s->n_chunks; /* error */
|
|
|
|
}
|
|
|
|
|
2014-03-26 16:05:59 +04:00
|
|
|
static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
|
2004-12-12 14:24:44 +03:00
|
|
|
{
|
2010-05-12 18:31:49 +04:00
|
|
|
BDRVDMGState *s = bs->opaque;
|
|
|
|
|
2014-03-26 16:05:54 +04:00
|
|
|
if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
|
|
|
|
int ret;
|
|
|
|
uint32_t chunk = search_chunk(s, sector_num);
|
2004-12-12 14:24:44 +03:00
|
|
|
|
2014-03-26 16:05:54 +04:00
|
|
|
if (chunk >= s->n_chunks) {
|
|
|
|
return -1;
|
|
|
|
}
|
2004-12-12 14:24:44 +03:00
|
|
|
|
2014-03-26 16:05:54 +04:00
|
|
|
s->current_chunk = s->n_chunks;
|
|
|
|
switch (s->types[chunk]) {
|
|
|
|
case 0x80000005: { /* zlib compressed */
|
|
|
|
/* we need to buffer, because only the chunk as whole can be
|
|
|
|
* inflated. */
|
2014-03-26 16:05:56 +04:00
|
|
|
ret = bdrv_pread(bs->file, s->offsets[chunk],
|
|
|
|
s->compressed_chunk, s->lengths[chunk]);
|
2014-03-26 16:05:54 +04:00
|
|
|
if (ret != s->lengths[chunk]) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->zstream.next_in = s->compressed_chunk;
|
|
|
|
s->zstream.avail_in = s->lengths[chunk];
|
|
|
|
s->zstream.next_out = s->uncompressed_chunk;
|
|
|
|
s->zstream.avail_out = 512 * s->sectorcounts[chunk];
|
|
|
|
ret = inflateReset(&s->zstream);
|
|
|
|
if (ret != Z_OK) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret = inflate(&s->zstream, Z_FINISH);
|
|
|
|
if (ret != Z_STREAM_END ||
|
|
|
|
s->zstream.total_out != 512 * s->sectorcounts[chunk]) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break; }
|
|
|
|
case 1: /* copy */
|
|
|
|
ret = bdrv_pread(bs->file, s->offsets[chunk],
|
2010-05-12 18:31:49 +04:00
|
|
|
s->uncompressed_chunk, s->lengths[chunk]);
|
2014-03-26 16:05:54 +04:00
|
|
|
if (ret != s->lengths[chunk]) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2: /* zero */
|
|
|
|
memset(s->uncompressed_chunk, 0, 512 * s->sectorcounts[chunk]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->current_chunk = chunk;
|
2004-12-12 14:24:44 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-17 01:08:06 +04:00
|
|
|
static int dmg_read(BlockDriverState *bs, int64_t sector_num,
|
2004-12-12 14:24:44 +03:00
|
|
|
uint8_t *buf, int nb_sectors)
|
|
|
|
{
|
|
|
|
BDRVDMGState *s = bs->opaque;
|
|
|
|
int i;
|
|
|
|
|
2014-03-26 16:05:54 +04:00
|
|
|
for (i = 0; i < nb_sectors; i++) {
|
|
|
|
uint32_t sector_offset_in_chunk;
|
|
|
|
if (dmg_read_chunk(bs, sector_num + i) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
|
|
|
|
memcpy(buf + i * 512,
|
|
|
|
s->uncompressed_chunk + sector_offset_in_chunk * 512, 512);
|
2004-12-12 14:24:44 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-20 15:16:22 +04:00
|
|
|
static coroutine_fn int dmg_co_read(BlockDriverState *bs, int64_t sector_num,
|
|
|
|
uint8_t *buf, int nb_sectors)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
BDRVDMGState *s = bs->opaque;
|
|
|
|
qemu_co_mutex_lock(&s->lock);
|
|
|
|
ret = dmg_read(bs, sector_num, buf, nb_sectors);
|
|
|
|
qemu_co_mutex_unlock(&s->lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-12-12 14:24:44 +03:00
|
|
|
static void dmg_close(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVDMGState *s = bs->opaque;
|
2013-01-25 20:07:31 +04:00
|
|
|
|
|
|
|
g_free(s->types);
|
|
|
|
g_free(s->offsets);
|
|
|
|
g_free(s->lengths);
|
|
|
|
g_free(s->sectors);
|
|
|
|
g_free(s->sectorcounts);
|
2014-05-20 15:28:14 +04:00
|
|
|
qemu_vfree(s->compressed_chunk);
|
|
|
|
qemu_vfree(s->uncompressed_chunk);
|
2013-01-25 20:07:31 +04:00
|
|
|
|
2004-12-12 14:24:44 +03:00
|
|
|
inflateEnd(&s->zstream);
|
|
|
|
}
|
|
|
|
|
2009-05-10 02:03:42 +04:00
|
|
|
static BlockDriver bdrv_dmg = {
|
2014-03-26 16:05:54 +04:00
|
|
|
.format_name = "dmg",
|
|
|
|
.instance_size = sizeof(BDRVDMGState),
|
|
|
|
.bdrv_probe = dmg_probe,
|
|
|
|
.bdrv_open = dmg_open,
|
|
|
|
.bdrv_read = dmg_co_read,
|
|
|
|
.bdrv_close = dmg_close,
|
2004-12-12 14:24:44 +03:00
|
|
|
};
|
2009-05-10 02:03:42 +04:00
|
|
|
|
|
|
|
static void bdrv_dmg_init(void)
|
|
|
|
{
|
|
|
|
bdrv_register(&bdrv_dmg);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_dmg_init);
|