*/
#include "qemu/osdep.h"
#include "qapi/error.h"
-#include "qemu-common.h"
#include "block/block_int.h"
#include "qemu/bswap.h"
#include "qemu/error-report.h"
int (*dmg_uncompress_bz2)(char *next_in, unsigned int avail_in,
char *next_out, unsigned int avail_out);
+int (*dmg_uncompress_lzfse)(char *next_in, unsigned int avail_in,
+ char *next_out, unsigned int avail_out);
+
enum {
/* Limit chunk sizes to prevent unreasonable amounts of memory being used
* or truncating when converting to 32-bit types
DMG_SECTORCOUNTS_MAX = DMG_LENGTHS_MAX / 512,
};
+enum {
+ /* DMG Block Type */
+ UDZE = 0, /* Zeroes */
+ UDRW, /* RAW type */
+ UDIG, /* Ignore */
+ UDCO = 0x80000004,
+ UDZO,
+ UDBZ,
+ ULFO,
+ UDCM = 0x7ffffffe, /* Comments */
+ UDLE = 0xffffffff /* Last Entry */
+};
+
static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
{
int len;
uint32_t uncompressed_sectors = 0;
switch (s->types[chunk]) {
- case 0x80000005: /* zlib compressed */
- case 0x80000006: /* bzip2 compressed */
+ case UDZO: /* zlib compressed */
+ case UDBZ: /* bzip2 compressed */
+ case ULFO: /* lzfse compressed */
compressed_size = s->lengths[chunk];
uncompressed_sectors = s->sectorcounts[chunk];
break;
- case 1: /* copy */
+ case UDRW: /* copy */
uncompressed_sectors = DIV_ROUND_UP(s->lengths[chunk], 512);
break;
- case 2: /* zero */
+ case UDZE: /* zero */
+ case UDIG: /* ignore */
/* 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. */
static bool dmg_is_known_block_type(uint32_t entry_type)
{
switch (entry_type) {
- case 0x00000001: /* uncompressed */
- case 0x00000002: /* zeroes */
- case 0x80000005: /* zlib */
+ case UDZE: /* zeros */
+ case UDRW: /* uncompressed */
+ case UDIG: /* ignore */
+ case UDZO: /* zlib */
return true;
- case 0x80000006: /* bzip2 */
+ case UDBZ: /* bzip2 */
return !!dmg_uncompress_bz2;
+ case ULFO: /* lzfse */
+ return !!dmg_uncompress_lzfse;
default:
return false;
}
/* sector count */
s->sectorcounts[i] = buff_read_uint64(buffer, offset + 0x10);
- /* 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) {
+ /* all-zeroes sector (type UDZE and UDIG) does not need to be
+ * "uncompressed" and can therefore be unbounded. */
+ if (s->types[i] != UDZE && s->types[i] != UDIG
+ && s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
error_report("sector count %" PRIu64 " for chunk %" PRIu32
" is larger than max (%u)",
s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
int64_t offset;
int ret;
+ ret = bdrv_apply_auto_read_only(bs, NULL, errp);
+ if (ret < 0) {
+ return ret;
+ }
+
bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
false, errp);
if (!bs->file) {
return -EINVAL;
}
- 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_module_load_one("dmg-bz2");
+ block_module_load_one("dmg-lzfse");
s->n_chunks = 0;
s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
{
/* binary search */
uint32_t chunk1 = 0, chunk2 = s->n_chunks, chunk3;
- while (chunk1 != chunk2) {
+ while (chunk1 <= chunk2) {
chunk3 = (chunk1 + chunk2) / 2;
if (s->sectors[chunk3] > sector_num) {
- chunk2 = chunk3;
+ if (chunk3 == 0) {
+ goto err;
+ }
+ chunk2 = chunk3 - 1;
} else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) {
return chunk3;
} else {
- chunk1 = chunk3;
+ chunk1 = chunk3 + 1;
}
}
+err:
return s->n_chunks; /* error */
}
s->current_chunk = s->n_chunks;
switch (s->types[chunk]) { /* block entry type */
- case 0x80000005: { /* zlib compressed */
+ case UDZO: { /* zlib compressed */
/* we need to buffer, because only the chunk as whole can be
* inflated. */
ret = bdrv_pread(bs->file, s->offsets[chunk],
return -1;
}
break; }
- case 0x80000006: /* bzip2 compressed */
+ case UDBZ: /* bzip2 compressed */
if (!dmg_uncompress_bz2) {
break;
}
return ret;
}
break;
- case 1: /* copy */
+ case ULFO:
+ if (!dmg_uncompress_lzfse) {
+ break;
+ }
+ /* we need to buffer, because only the chunk as whole can be
+ * inflated. */
+ ret = bdrv_pread(bs->file, s->offsets[chunk],
+ s->compressed_chunk, s->lengths[chunk]);
+ if (ret != s->lengths[chunk]) {
+ return -1;
+ }
+
+ ret = dmg_uncompress_lzfse((char *)s->compressed_chunk,
+ (unsigned int) s->lengths[chunk],
+ (char *)s->uncompressed_chunk,
+ (unsigned int)
+ (512 * s->sectorcounts[chunk]));
+ if (ret < 0) {
+ return ret;
+ }
+ break;
+ case UDRW: /* copy */
ret = bdrv_pread(bs->file, s->offsets[chunk],
s->uncompressed_chunk, s->lengths[chunk]);
if (ret != s->lengths[chunk]) {
return -1;
}
break;
- case 2: /* zero */
+ case UDZE: /* zeros */
+ case UDIG: /* ignore */
/* see dmg_read, it is treated specially. No buffer needs to be
* pre-filled, the zeroes can be set directly. */
break;
int nb_sectors = bytes >> BDRV_SECTOR_BITS;
int ret, i;
- assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
- assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
+ assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE));
+ assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
qemu_co_mutex_lock(&s->lock);
/* 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 */
+ if (s->types[s->current_chunk] == UDZE
+ || s->types[s->current_chunk] == UDIG) { /* all zeroes block entry */
qemu_iovec_memset(qiov, i * 512, 0, 512);
continue;
}