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.
|
|
|
|
*/
|
2016-01-18 21:01:42 +03:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
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"
|
2015-03-17 20:29:20 +03:00
|
|
|
#include "qemu/error-report.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/module.h"
|
2004-12-12 14:24:44 +03:00
|
|
|
#include <zlib.h>
|
2015-01-06 20:48:14 +03:00
|
|
|
#ifdef CONFIG_BZIP2
|
|
|
|
#include <bzlib.h>
|
|
|
|
#endif
|
2015-01-06 20:48:09 +03:00
|
|
|
#include <glib.h>
|
2004-12-12 14:24:44 +03:00
|
|
|
|
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;
|
2015-01-06 20:48:14 +03:00
|
|
|
#ifdef CONFIG_BZIP2
|
|
|
|
bz_stream bzstream;
|
|
|
|
#endif
|
2004-12-12 14:24:44 +03:00
|
|
|
} 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;
|
|
|
|
|
2015-06-16 15:19:22 +03:00
|
|
|
ret = bdrv_pread(bs->file->bs, offset, &buffer, 8);
|
2013-01-25 20:07:30 +04:00
|
|
|
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;
|
|
|
|
|
2015-06-16 15:19:22 +03:00
|
|
|
ret = bdrv_pread(bs->file->bs, offset, &buffer, 4);
|
2013-01-25 20:07:30 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = be32_to_cpu(buffer);
|
|
|
|
return 0;
|
2004-12-12 14:24:44 +03:00
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:07 +03:00
|
|
|
static inline uint64_t buff_read_uint64(const uint8_t *buffer, int64_t offset)
|
|
|
|
{
|
|
|
|
return be64_to_cpu(*(uint64_t *)&buffer[offset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t buff_read_uint32(const uint8_t *buffer, int64_t offset)
|
|
|
|
{
|
|
|
|
return be32_to_cpu(*(uint32_t *)&buffer[offset]);
|
|
|
|
}
|
|
|
|
|
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 */
|
2015-01-06 20:48:14 +03:00
|
|
|
case 0x80000006: /* bzip2 compressed */
|
2014-03-26 16:06:00 +04:00
|
|
|
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 */
|
2015-01-06 20:48:15 +03:00
|
|
|
/* as the all-zeroes block may be large, it is treated specially: the
|
|
|
|
* sector is not copied from a large buffer, a simple memset is used
|
|
|
|
* instead. Therefore uncompressed_sectors does not need to be set. */
|
2014-03-26 16:06:00 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:04 +03:00
|
|
|
static int64_t dmg_find_koly_offset(BlockDriverState *file_bs, Error **errp)
|
|
|
|
{
|
|
|
|
int64_t length;
|
|
|
|
int64_t offset = 0;
|
|
|
|
uint8_t buffer[515];
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
/* bdrv_getlength returns a multiple of block size (512), rounded up. Since
|
|
|
|
* dmg images can have odd sizes, try to look for the "koly" magic which
|
|
|
|
* marks the begin of the UDIF trailer (512 bytes). This magic can be found
|
|
|
|
* in the last 511 bytes of the second-last sector or the first 4 bytes of
|
|
|
|
* the last sector (search space: 515 bytes) */
|
|
|
|
length = bdrv_getlength(file_bs);
|
|
|
|
if (length < 0) {
|
|
|
|
error_setg_errno(errp, -length,
|
|
|
|
"Failed to get file size while reading UDIF trailer");
|
|
|
|
return length;
|
|
|
|
} else if (length < 512) {
|
|
|
|
error_setg(errp, "dmg file must be at least 512 bytes long");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
if (length > 511 + 512) {
|
|
|
|
offset = length - 511 - 512;
|
|
|
|
}
|
|
|
|
length = length < 515 ? length : 515;
|
|
|
|
ret = bdrv_pread(file_bs, offset, buffer, length);
|
|
|
|
if (ret < 0) {
|
|
|
|
error_setg_errno(errp, -ret, "Failed while reading UDIF trailer");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
for (i = 0; i < length - 3; i++) {
|
|
|
|
if (buffer[i] == 'k' && buffer[i+1] == 'o' &&
|
|
|
|
buffer[i+2] == 'l' && buffer[i+3] == 'y') {
|
|
|
|
return offset + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
error_setg(errp, "Could not locate UDIF trailer in dmg file");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:05 +03:00
|
|
|
/* used when building the sector table */
|
|
|
|
typedef struct DmgHeaderState {
|
|
|
|
/* used internally by dmg_read_mish_block to remember offsets of blocks
|
|
|
|
* across calls */
|
2015-01-06 20:48:11 +03:00
|
|
|
uint64_t data_fork_offset;
|
2015-01-06 20:48:05 +03:00
|
|
|
/* exported for dmg_open */
|
|
|
|
uint32_t max_compressed_size;
|
|
|
|
uint32_t max_sectors_per_chunk;
|
|
|
|
} DmgHeaderState;
|
|
|
|
|
2015-01-06 20:48:13 +03:00
|
|
|
static bool dmg_is_known_block_type(uint32_t entry_type)
|
|
|
|
{
|
|
|
|
switch (entry_type) {
|
|
|
|
case 0x00000001: /* uncompressed */
|
|
|
|
case 0x00000002: /* zeroes */
|
|
|
|
case 0x80000005: /* zlib */
|
2015-01-06 20:48:14 +03:00
|
|
|
#ifdef CONFIG_BZIP2
|
|
|
|
case 0x80000006: /* bzip2 */
|
|
|
|
#endif
|
2015-01-06 20:48:13 +03:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:07 +03:00
|
|
|
static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,
|
|
|
|
uint8_t *buffer, uint32_t count)
|
2015-01-06 20:48:05 +03:00
|
|
|
{
|
|
|
|
uint32_t type, i;
|
|
|
|
int ret;
|
|
|
|
size_t new_size;
|
|
|
|
uint32_t chunk_count;
|
2015-01-06 20:48:07 +03:00
|
|
|
int64_t offset = 0;
|
2015-01-06 20:48:11 +03:00
|
|
|
uint64_t data_offset;
|
|
|
|
uint64_t in_offset = ds->data_fork_offset;
|
2015-01-06 20:48:12 +03:00
|
|
|
uint64_t out_offset;
|
2015-01-06 20:48:05 +03:00
|
|
|
|
2015-01-06 20:48:07 +03:00
|
|
|
type = buff_read_uint32(buffer, offset);
|
2015-01-06 20:48:05 +03:00
|
|
|
/* skip data that is not a valid MISH block (invalid magic or too small) */
|
|
|
|
if (type != 0x6d697368 || count < 244) {
|
|
|
|
/* assume success for now */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:12 +03:00
|
|
|
/* chunk offsets are relative to this sector number */
|
|
|
|
out_offset = buff_read_uint64(buffer, offset + 8);
|
|
|
|
|
2015-01-06 20:48:11 +03:00
|
|
|
/* location in data fork for (compressed) blob (in bytes) */
|
|
|
|
data_offset = buff_read_uint64(buffer, offset + 0x18);
|
|
|
|
in_offset += data_offset;
|
|
|
|
|
|
|
|
/* move to begin of chunk entries */
|
|
|
|
offset += 204;
|
2015-01-06 20:48:05 +03: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);
|
|
|
|
|
|
|
|
for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
|
2015-01-06 20:48:07 +03:00
|
|
|
s->types[i] = buff_read_uint32(buffer, offset);
|
2015-01-06 20:48:13 +03:00
|
|
|
if (!dmg_is_known_block_type(s->types[i])) {
|
2015-01-06 20:48:05 +03:00
|
|
|
chunk_count--;
|
|
|
|
i--;
|
2015-01-06 20:48:13 +03:00
|
|
|
offset += 40;
|
2015-01-06 20:48:05 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:13 +03:00
|
|
|
/* sector number */
|
|
|
|
s->sectors[i] = buff_read_uint64(buffer, offset + 8);
|
2015-01-06 20:48:12 +03:00
|
|
|
s->sectors[i] += out_offset;
|
2015-01-06 20:48:05 +03:00
|
|
|
|
2015-01-06 20:48:13 +03:00
|
|
|
/* sector count */
|
|
|
|
s->sectorcounts[i] = buff_read_uint64(buffer, offset + 0x10);
|
2015-01-06 20:48:05 +03:00
|
|
|
|
2015-01-06 20:48:15 +03:00
|
|
|
/* all-zeroes sector (type 2) does not need to be "uncompressed" and can
|
|
|
|
* therefore be unbounded. */
|
|
|
|
if (s->types[i] != 2 && s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
|
2015-01-06 20:48:05 +03:00
|
|
|
error_report("sector count %" PRIu64 " for chunk %" PRIu32
|
|
|
|
" is larger than max (%u)",
|
|
|
|
s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:13 +03:00
|
|
|
/* offset in (compressed) data fork */
|
|
|
|
s->offsets[i] = buff_read_uint64(buffer, offset + 0x18);
|
2015-01-06 20:48:11 +03:00
|
|
|
s->offsets[i] += in_offset;
|
2015-01-06 20:48:05 +03:00
|
|
|
|
2015-01-06 20:48:13 +03:00
|
|
|
/* length in (compressed) data fork */
|
|
|
|
s->lengths[i] = buff_read_uint64(buffer, offset + 0x20);
|
2015-01-06 20:48:05 +03:00
|
|
|
|
|
|
|
if (s->lengths[i] > DMG_LENGTHS_MAX) {
|
|
|
|
error_report("length %" PRIu64 " for chunk %" PRIu32
|
|
|
|
" is larger than max (%u)",
|
|
|
|
s->lengths[i], i, DMG_LENGTHS_MAX);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
update_max_chunk_size(s, i, &ds->max_compressed_size,
|
|
|
|
&ds->max_sectors_per_chunk);
|
2015-01-06 20:48:13 +03:00
|
|
|
offset += 40;
|
2015-01-06 20:48:05 +03:00
|
|
|
}
|
|
|
|
s->n_chunks += chunk_count;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:06 +03:00
|
|
|
static int dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds,
|
|
|
|
uint64_t info_begin, uint64_t info_length)
|
2004-12-12 14:24:44 +03:00
|
|
|
{
|
2015-01-06 20:48:07 +03:00
|
|
|
BDRVDMGState *s = bs->opaque;
|
2013-01-25 20:07:30 +04:00
|
|
|
int ret;
|
2015-01-06 20:48:06 +03:00
|
|
|
uint32_t count, rsrc_data_offset;
|
2015-01-06 20:48:07 +03:00
|
|
|
uint8_t *buffer = NULL;
|
2015-01-06 20:48:06 +03:00
|
|
|
uint64_t info_end;
|
|
|
|
uint64_t offset;
|
2004-12-12 14:24:44 +03:00
|
|
|
|
2015-01-06 20:48:06 +03:00
|
|
|
/* read offset from begin of resource fork (info_begin) to resource data */
|
2015-01-06 20:48:05 +03:00
|
|
|
ret = read_uint32(bs, info_begin, &rsrc_data_offset);
|
2013-01-25 20:07:30 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
2015-01-06 20:48:06 +03:00
|
|
|
} else if (rsrc_data_offset > info_length) {
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = -EINVAL;
|
2010-05-12 18:31:35 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:06 +03:00
|
|
|
/* read length of resource data */
|
|
|
|
ret = read_uint32(bs, info_begin + 8, &count);
|
2013-01-25 20:07:30 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
2015-01-06 20:48:06 +03:00
|
|
|
} else if (count == 0 || rsrc_data_offset + count > info_length) {
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = -EINVAL;
|
2010-05-12 18:31:35 +04:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:05 +03:00
|
|
|
/* begin of resource data (consisting of one or more resources) */
|
2015-01-06 20:48:06 +03:00
|
|
|
offset = info_begin + rsrc_data_offset;
|
|
|
|
|
|
|
|
/* end of resource data (there is possibly a following resource map
|
|
|
|
* which will be ignored). */
|
|
|
|
info_end = offset + count;
|
2004-12-12 14:24:44 +03:00
|
|
|
|
2015-01-06 20:48:05 +03:00
|
|
|
/* read offsets (mish blocks) from one or more resources in resource data */
|
2010-05-12 18:31:35 +04:00
|
|
|
while (offset < info_end) {
|
2015-01-06 20:48:05 +03:00
|
|
|
/* size of following resource */
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = read_uint32(bs, offset, &count);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
2015-01-06 20:48:08 +03:00
|
|
|
} else if (count == 0 || count > info_end - offset) {
|
2013-01-25 20:07:30 +04:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2010-05-12 18:31:35 +04:00
|
|
|
offset += 4;
|
|
|
|
|
2015-01-06 20:48:07 +03:00
|
|
|
buffer = g_realloc(buffer, count);
|
2015-06-16 15:19:22 +03:00
|
|
|
ret = bdrv_pread(bs->file->bs, offset, buffer, count);
|
2015-01-06 20:48:07 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = dmg_read_mish_block(s, ds, buffer, count);
|
2013-01-25 20:07:30 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-01-06 20:48:05 +03:00
|
|
|
/* advance offset by size of resource */
|
|
|
|
offset += count;
|
2004-12-12 14:24:44 +03:00
|
|
|
}
|
2015-01-06 20:48:07 +03:00
|
|
|
ret = 0;
|
2015-01-06 20:48:06 +03:00
|
|
|
|
|
|
|
fail:
|
2015-01-06 20:48:07 +03:00
|
|
|
g_free(buffer);
|
2015-01-06 20:48:06 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:09 +03:00
|
|
|
static int dmg_read_plist_xml(BlockDriverState *bs, DmgHeaderState *ds,
|
|
|
|
uint64_t info_begin, uint64_t info_length)
|
|
|
|
{
|
|
|
|
BDRVDMGState *s = bs->opaque;
|
|
|
|
int ret;
|
|
|
|
uint8_t *buffer = NULL;
|
|
|
|
char *data_begin, *data_end;
|
|
|
|
|
|
|
|
/* Have at least some length to avoid NULL for g_malloc. Attempt to set a
|
|
|
|
* safe upper cap on the data length. A test sample had a XML length of
|
|
|
|
* about 1 MiB. */
|
|
|
|
if (info_length == 0 || info_length > 16 * 1024 * 1024) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = g_malloc(info_length + 1);
|
|
|
|
buffer[info_length] = '\0';
|
2015-06-16 15:19:22 +03:00
|
|
|
ret = bdrv_pread(bs->file->bs, info_begin, buffer, info_length);
|
2015-01-06 20:48:09 +03:00
|
|
|
if (ret != info_length) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
|
|
|
|
* decode. The actual data element has 431 (0x1af) bytes which includes tabs
|
|
|
|
* and line feeds. */
|
|
|
|
data_end = (char *)buffer;
|
|
|
|
while ((data_begin = strstr(data_end, "<data>")) != NULL) {
|
|
|
|
guchar *mish;
|
|
|
|
gsize out_len = 0;
|
|
|
|
|
|
|
|
data_begin += 6;
|
|
|
|
data_end = strstr(data_begin, "</data>");
|
|
|
|
/* malformed XML? */
|
|
|
|
if (data_end == NULL) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
*data_end++ = '\0';
|
|
|
|
mish = g_base64_decode(data_begin, &out_len);
|
|
|
|
ret = dmg_read_mish_block(s, ds, mish, (uint32_t)out_len);
|
|
|
|
g_free(mish);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
g_free(buffer);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:06 +03:00
|
|
|
static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
BDRVDMGState *s = bs->opaque;
|
|
|
|
DmgHeaderState ds;
|
|
|
|
uint64_t rsrc_fork_offset, rsrc_fork_length;
|
2015-01-06 20:48:09 +03:00
|
|
|
uint64_t plist_xml_offset, plist_xml_length;
|
2015-01-06 20:48:06 +03:00
|
|
|
int64_t offset;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
bs->read_only = 1;
|
|
|
|
s->n_chunks = 0;
|
|
|
|
s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
|
|
|
|
/* used by dmg_read_mish_block to keep track of the current I/O position */
|
2015-01-06 20:48:11 +03:00
|
|
|
ds.data_fork_offset = 0;
|
2015-01-06 20:48:06 +03:00
|
|
|
ds.max_compressed_size = 1;
|
|
|
|
ds.max_sectors_per_chunk = 1;
|
|
|
|
|
|
|
|
/* locate the UDIF trailer */
|
2015-06-16 15:19:22 +03:00
|
|
|
offset = dmg_find_koly_offset(bs->file->bs, errp);
|
2015-01-06 20:48:06 +03:00
|
|
|
if (offset < 0) {
|
|
|
|
ret = offset;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:11 +03:00
|
|
|
/* offset of data fork (DataForkOffset) */
|
|
|
|
ret = read_uint64(bs, offset + 0x18, &ds.data_fork_offset);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
} else if (ds.data_fork_offset > offset) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2015-01-06 20:48:06 +03:00
|
|
|
/* offset of resource fork (RsrcForkOffset) */
|
|
|
|
ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ret = read_uint64(bs, offset + 0x30, &rsrc_fork_length);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-01-06 20:48:08 +03:00
|
|
|
if (rsrc_fork_offset >= offset ||
|
|
|
|
rsrc_fork_length > offset - rsrc_fork_offset) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-01-06 20:48:09 +03:00
|
|
|
/* offset of property list (XMLOffset) */
|
|
|
|
ret = read_uint64(bs, offset + 0xd8, &plist_xml_offset);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ret = read_uint64(bs, offset + 0xe0, &plist_xml_length);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (plist_xml_offset >= offset ||
|
|
|
|
plist_xml_length > offset - plist_xml_offset) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-01-06 20:48:10 +03:00
|
|
|
ret = read_uint64(bs, offset + 0x1ec, (uint64_t *)&bs->total_sectors);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (bs->total_sectors < 0) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-01-06 20:48:06 +03:00
|
|
|
if (rsrc_fork_length != 0) {
|
|
|
|
ret = dmg_read_resource_fork(bs, &ds,
|
|
|
|
rsrc_fork_offset, rsrc_fork_length);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-01-06 20:48:09 +03:00
|
|
|
} else if (plist_xml_length != 0) {
|
|
|
|
ret = dmg_read_plist_xml(bs, &ds, plist_xml_offset, plist_xml_length);
|
|
|
|
if (ret < 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-01-06 20:48:06 +03:00
|
|
|
} else {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
2004-12-12 14:24:44 +03:00
|
|
|
|
|
|
|
/* initialize zlib engine */
|
2015-06-16 15:19:22 +03:00
|
|
|
s->compressed_chunk = qemu_try_blockalign(bs->file->bs,
|
2015-01-06 20:48:05 +03:00
|
|
|
ds.max_compressed_size + 1);
|
2015-06-16 15:19:22 +03:00
|
|
|
s->uncompressed_chunk = qemu_try_blockalign(bs->file->bs,
|
2015-01-06 20:48:05 +03:00
|
|
|
512 * ds.max_sectors_per_chunk);
|
2014-05-20 15:28:14 +04:00
|
|
|
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);
|
2015-01-06 20:48:14 +03:00
|
|
|
#ifdef CONFIG_BZIP2
|
|
|
|
uint64_t total_out;
|
|
|
|
#endif
|
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;
|
2015-01-06 20:48:14 +03:00
|
|
|
switch (s->types[chunk]) { /* block entry type */
|
2014-03-26 16:05:54 +04:00
|
|
|
case 0x80000005: { /* zlib compressed */
|
|
|
|
/* we need to buffer, because only the chunk as whole can be
|
|
|
|
* inflated. */
|
2015-06-16 15:19:22 +03:00
|
|
|
ret = bdrv_pread(bs->file->bs, s->offsets[chunk],
|
2014-03-26 16:05:56 +04:00
|
|
|
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; }
|
2015-01-06 20:48:14 +03:00
|
|
|
#ifdef CONFIG_BZIP2
|
|
|
|
case 0x80000006: /* bzip2 compressed */
|
|
|
|
/* we need to buffer, because only the chunk as whole can be
|
|
|
|
* inflated. */
|
2015-06-16 15:19:22 +03:00
|
|
|
ret = bdrv_pread(bs->file->bs, s->offsets[chunk],
|
2015-01-06 20:48:14 +03:00
|
|
|
s->compressed_chunk, s->lengths[chunk]);
|
|
|
|
if (ret != s->lengths[chunk]) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = BZ2_bzDecompressInit(&s->bzstream, 0, 0);
|
|
|
|
if (ret != BZ_OK) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
s->bzstream.next_in = (char *)s->compressed_chunk;
|
|
|
|
s->bzstream.avail_in = (unsigned int) s->lengths[chunk];
|
|
|
|
s->bzstream.next_out = (char *)s->uncompressed_chunk;
|
|
|
|
s->bzstream.avail_out = (unsigned int) 512 * s->sectorcounts[chunk];
|
|
|
|
ret = BZ2_bzDecompress(&s->bzstream);
|
|
|
|
total_out = ((uint64_t)s->bzstream.total_out_hi32 << 32) +
|
|
|
|
s->bzstream.total_out_lo32;
|
|
|
|
BZ2_bzDecompressEnd(&s->bzstream);
|
|
|
|
if (ret != BZ_STREAM_END ||
|
|
|
|
total_out != 512 * s->sectorcounts[chunk]) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* CONFIG_BZIP2 */
|
2014-03-26 16:05:54 +04:00
|
|
|
case 1: /* copy */
|
2015-06-16 15:19:22 +03:00
|
|
|
ret = bdrv_pread(bs->file->bs, 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 */
|
2015-01-06 20:48:15 +03:00
|
|
|
/* see dmg_read, it is treated specially. No buffer needs to be
|
|
|
|
* pre-filled, the zeroes can be set directly. */
|
2014-03-26 16:05:54 +04:00
|
|
|
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;
|
|
|
|
}
|
2015-01-06 20:48:15 +03:00
|
|
|
/* Special case: current chunk is all zeroes. Do not perform a memcpy as
|
|
|
|
* s->uncompressed_chunk may be too small to cover the large all-zeroes
|
|
|
|
* section. dmg_read_chunk is called to find s->current_chunk */
|
|
|
|
if (s->types[s->current_chunk] == 2) { /* all zeroes block entry */
|
|
|
|
memset(buf + i * 512, 0, 512);
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-26 16:05:54 +04:00
|
|
|
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);
|