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"
|
2016-09-05 05:50:45 +03:00
|
|
|
#include "dmg.h"
|
|
|
|
|
|
|
|
int (*dmg_uncompress_bz2)(char *next_in, unsigned int avail_in,
|
|
|
|
char *next_out, unsigned int avail_out);
|
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
|
|
|
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;
|
|
|
|
|
2016-06-20 19:24:02 +03:00
|
|
|
ret = bdrv_pread(bs->file, 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;
|
|
|
|
|
2016-06-20 19:24:02 +03:00
|
|
|
ret = bdrv_pread(bs->file, 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 */
|
2017-06-22 14:04:16 +03:00
|
|
|
uncompressed_sectors = DIV_ROUND_UP(s->lengths[chunk], 512);
|
2014-03-26 16:06:00 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 19:24:02 +03:00
|
|
|
static int64_t dmg_find_koly_offset(BdrvChild *file, Error **errp)
|
2015-01-06 20:48:04 +03:00
|
|
|
{
|
2016-06-20 19:24:02 +03:00
|
|
|
BlockDriverState *file_bs = file->bs;
|
2015-01-06 20:48:04 +03:00
|
|
|
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;
|
2016-06-20 19:24:02 +03:00
|
|
|
ret = bdrv_pread(file, offset, buffer, length);
|
2015-01-06 20:48:04 +03:00
|
|
|
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 */
|
|
|
|
return true;
|
2016-09-05 05:50:45 +03:00
|
|
|
case 0x80000006: /* bzip2 */
|
|
|
|
return !!dmg_uncompress_bz2;
|
2015-01-06 20:48:13 +03:00
|
|
|
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);
|
2016-06-20 19:24:02 +03:00
|
|
|
ret = bdrv_pread(bs->file, 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';
|
2016-06-20 19:24:02 +03:00
|
|
|
ret = bdrv_pread(bs->file, 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;
|
|
|
|
|
2016-12-16 20:52:37 +03:00
|
|
|
bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
|
|
|
|
false, errp);
|
|
|
|
if (!bs->file) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-11-07 20:21:41 +03:00
|
|
|
if (!bdrv_is_read_only(bs)) {
|
|
|
|
error_report("Opening dmg images without an explicit read-only=on "
|
|
|
|
"option is deprecated. Future versions will refuse to "
|
|
|
|
"open the image instead of automatically marking the "
|
|
|
|
"image read-only.");
|
|
|
|
ret = bdrv_set_read_only(bs, true, errp);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
block: do not set BDS read_only if copy_on_read enabled
A few block drivers will set the BDS read_only flag from their
.bdrv_open() function. This means the bs->read_only flag could
be set after we enable copy_on_read, as the BDRV_O_COPY_ON_READ
flag check occurs prior to the call to bdrv->bdrv_open().
This adds an error return to bdrv_set_read_only(), and an error will be
return if we try to set the BDS to read_only while copy_on_read is
enabled.
This patch also changes the behavior of vvfat. Before, vvfat could
override the drive 'readonly' flag with its own, internal 'rw' flag.
For instance, this -drive parameter would result in a writable image:
"-drive format=vvfat,dir=/tmp/vvfat,rw,if=virtio,readonly=on"
This is not correct. Now, attempting to use the above -drive parameter
will result in an error (i.e., 'rw' is incompatible with 'readonly=on').
Signed-off-by: Jeff Cody <jcody@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Reviewed-by: John Snow <jsnow@redhat.com>
Message-id: 0c5b4c1cc2c651471b131f21376dfd5ea24d2196.1491597120.git.jcody@redhat.com
2017-04-07 23:55:26 +03:00
|
|
|
}
|
|
|
|
|
2016-09-05 05:50:45 +03:00
|
|
|
block_module_load_one("dmg-bz2");
|
2016-04-25 16:43:09 +03:00
|
|
|
|
2015-01-06 20:48:06 +03:00
|
|
|
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 */
|
2016-06-20 19:24:02 +03:00
|
|
|
offset = dmg_find_koly_offset(bs->file, 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
|
|
|
}
|
|
|
|
|
2016-06-24 01:37:17 +03:00
|
|
|
static void dmg_refresh_limits(BlockDriverState *bs, Error **errp)
|
|
|
|
{
|
2016-06-24 01:37:24 +03:00
|
|
|
bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
|
2016-06-24 01:37:17 +03:00
|
|
|
}
|
|
|
|
|
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;
|
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. */
|
2016-06-20 19:24:02 +03:00
|
|
|
ret = bdrv_pread(bs->file, 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
|
|
|
case 0x80000006: /* bzip2 compressed */
|
2016-09-05 05:50:45 +03:00
|
|
|
if (!dmg_uncompress_bz2) {
|
|
|
|
break;
|
|
|
|
}
|
2015-01-06 20:48:14 +03:00
|
|
|
/* we need to buffer, because only the chunk as whole can be
|
|
|
|
* inflated. */
|
2016-06-20 19:24:02 +03:00
|
|
|
ret = bdrv_pread(bs->file, s->offsets[chunk],
|
2015-01-06 20:48:14 +03:00
|
|
|
s->compressed_chunk, s->lengths[chunk]);
|
|
|
|
if (ret != s->lengths[chunk]) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-09-05 05:50:45 +03:00
|
|
|
ret = dmg_uncompress_bz2((char *)s->compressed_chunk,
|
|
|
|
(unsigned int) s->lengths[chunk],
|
|
|
|
(char *)s->uncompressed_chunk,
|
|
|
|
(unsigned int)
|
|
|
|
(512 * s->sectorcounts[chunk]));
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
2015-01-06 20:48:14 +03:00
|
|
|
}
|
|
|
|
break;
|
2014-03-26 16:05:54 +04:00
|
|
|
case 1: /* copy */
|
2016-06-20 19:24:02 +03:00
|
|
|
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 */
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:43:09 +03:00
|
|
|
static int coroutine_fn
|
|
|
|
dmg_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
|
|
|
|
QEMUIOVector *qiov, int flags)
|
2004-12-12 14:24:44 +03:00
|
|
|
{
|
|
|
|
BDRVDMGState *s = bs->opaque;
|
2016-04-25 16:43:09 +03:00
|
|
|
uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
|
|
|
|
int nb_sectors = bytes >> BDRV_SECTOR_BITS;
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
|
|
|
|
assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
|
|
|
|
|
|
|
|
qemu_co_mutex_lock(&s->lock);
|
2004-12-12 14:24:44 +03:00
|
|
|
|
2014-03-26 16:05:54 +04:00
|
|
|
for (i = 0; i < nb_sectors; i++) {
|
|
|
|
uint32_t sector_offset_in_chunk;
|
2016-04-25 16:43:09 +03:00
|
|
|
void *data;
|
|
|
|
|
2014-03-26 16:05:54 +04:00
|
|
|
if (dmg_read_chunk(bs, sector_num + i) != 0) {
|
2016-04-25 16:43:09 +03:00
|
|
|
ret = -EIO;
|
|
|
|
goto fail;
|
2014-03-26 16:05:54 +04:00
|
|
|
}
|
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 */
|
2016-04-25 16:43:09 +03:00
|
|
|
qemu_iovec_memset(qiov, i * 512, 0, 512);
|
2015-01-06 20:48:15 +03:00
|
|
|
continue;
|
|
|
|
}
|
2014-03-26 16:05:54 +04:00
|
|
|
sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
|
2016-04-25 16:43:09 +03:00
|
|
|
data = s->uncompressed_chunk + sector_offset_in_chunk * 512;
|
|
|
|
qemu_iovec_from_buf(qiov, i * 512, data, 512);
|
2004-12-12 14:24:44 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:43:09 +03:00
|
|
|
ret = 0;
|
|
|
|
fail:
|
2011-10-20 15:16:22 +04:00
|
|
|
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,
|
2016-06-24 01:37:17 +03:00
|
|
|
.bdrv_refresh_limits = dmg_refresh_limits,
|
2016-12-19 18:36:02 +03:00
|
|
|
.bdrv_child_perm = bdrv_format_default_perms,
|
2016-04-25 16:43:09 +03:00
|
|
|
.bdrv_co_preadv = dmg_co_preadv,
|
2014-03-26 16:05:54 +04:00
|
|
|
.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);
|