dmg.c 15.5 KB
Newer Older
1 2
/*
 * QEMU Block driver for DMG images
3
 *
4
 * Copyright (c) 2004 Johannes E. Schindelin
5
 *
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 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.
 */
P
pbrook 已提交
24
#include "qemu-common.h"
25
#include "block/block_int.h"
26 27
#include "qemu/bswap.h"
#include "qemu/module.h"
28 29
#include <zlib.h>

30 31 32 33 34 35 36 37
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,
};

38
typedef struct BDRVDMGState {
39
    CoMutex lock;
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    /* 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;
B
bellard 已提交
55 56
    uint8_t *compressed_chunk;
    uint8_t *uncompressed_chunk;
57 58 59 60 61
    z_stream zstream;
} BDRVDMGState;

static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
{
62 63 64 65 66 67 68 69 70 71
    int len;

    if (!filename) {
        return 0;
    }

    len = strlen(filename);
    if (len > 4 && !strcmp(filename + len - 4, ".dmg")) {
        return 2;
    }
72 73 74
    return 0;
}

K
Kevin Wolf 已提交
75
static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
76
{
K
Kevin Wolf 已提交
77 78 79 80 81 82 83 84 85 86
    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;
87 88
}

K
Kevin Wolf 已提交
89
static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
90
{
K
Kevin Wolf 已提交
91 92 93 94 95 96 97 98 99 100
    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;
101 102
}

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
/* 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;
    }
}

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
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;
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
/* used when building the sector table */
typedef struct DmgHeaderState {
    /* used internally by dmg_read_mish_block to remember offsets of blocks
     * across calls */
    uint64_t last_in_offset;
    uint64_t last_out_offset;
    /* exported for dmg_open */
    uint32_t max_compressed_size;
    uint32_t max_sectors_per_chunk;
} DmgHeaderState;

static int dmg_read_mish_block(BlockDriverState *bs, DmgHeaderState *ds,
                               int64_t offset, uint32_t count)
{
    BDRVDMGState *s = bs->opaque;
    uint32_t type, i;
    int ret;
    size_t new_size;
    uint32_t chunk_count;

    ret = read_uint32(bs, offset, &type);
    if (ret < 0) {
        goto fail;
    }

    /* 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;
    }

    offset += 4;
    offset += 200;

    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++) {
        ret = read_uint32(bs, offset, &s->types[i]);
        if (ret < 0) {
            goto fail;
        }
        offset += 4;
        if (s->types[i] != 0x80000005 && s->types[i] != 1 &&
            s->types[i] != 2) {
            if (s->types[i] == 0xffffffff && i > 0) {
                ds->last_in_offset = s->offsets[i - 1] + s->lengths[i - 1];
                ds->last_out_offset = s->sectors[i - 1] +
                                      s->sectorcounts[i - 1];
            }
            chunk_count--;
            i--;
            offset += 36;
            continue;
        }
        offset += 4;

        ret = read_uint64(bs, offset, &s->sectors[i]);
        if (ret < 0) {
            goto fail;
        }
        s->sectors[i] += ds->last_out_offset;
        offset += 8;

        ret = read_uint64(bs, offset, &s->sectorcounts[i]);
        if (ret < 0) {
            goto fail;
        }
        offset += 8;

        if (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);
            ret = -EINVAL;
            goto fail;
        }

        ret = read_uint64(bs, offset, &s->offsets[i]);
        if (ret < 0) {
            goto fail;
        }
        s->offsets[i] += ds->last_in_offset;
        offset += 8;

        ret = read_uint64(bs, offset, &s->lengths[i]);
        if (ret < 0) {
            goto fail;
        }
        offset += 8;

        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);
    }
    s->n_chunks += chunk_count;
    return 0;

fail:
    return ret;
}

M
Max Reitz 已提交
288 289
static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
                    Error **errp)
290 291
{
    BDRVDMGState *s = bs->opaque;
292 293 294
    DmgHeaderState ds;
    uint64_t info_begin, info_end;
    uint32_t count, rsrc_data_offset;
C
Christoph Hellwig 已提交
295
    int64_t offset;
K
Kevin Wolf 已提交
296
    int ret;
297 298 299

    bs->read_only = 1;
    s->n_chunks = 0;
300
    s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
301 302 303 304 305
    /* used by dmg_read_mish_block to keep track of the current I/O position */
    ds.last_in_offset = 0;
    ds.last_out_offset = 0;
    ds.max_compressed_size = 1;
    ds.max_sectors_per_chunk = 1;
306

307 308
    /* locate the UDIF trailer */
    offset = dmg_find_koly_offset(bs->file, errp);
C
Christoph Hellwig 已提交
309
    if (offset < 0) {
K
Kevin Wolf 已提交
310
        ret = offset;
C
Christoph Hellwig 已提交
311
        goto fail;
312
    }
C
Christoph Hellwig 已提交
313

314
    ret = read_uint64(bs, offset + 0x28, &info_begin);
K
Kevin Wolf 已提交
315 316 317 318 319
    if (ret < 0) {
        goto fail;
    } else if (info_begin == 0) {
        ret = -EINVAL;
        goto fail;
C
Christoph Hellwig 已提交
320 321
    }

322
    ret = read_uint32(bs, info_begin, &rsrc_data_offset);
K
Kevin Wolf 已提交
323 324
    if (ret < 0) {
        goto fail;
325
    } else if (rsrc_data_offset != 0x100) {
K
Kevin Wolf 已提交
326
        ret = -EINVAL;
C
Christoph Hellwig 已提交
327 328 329
        goto fail;
    }

K
Kevin Wolf 已提交
330 331 332 333 334
    ret = read_uint32(bs, info_begin + 4, &count);
    if (ret < 0) {
        goto fail;
    } else if (count == 0) {
        ret = -EINVAL;
C
Christoph Hellwig 已提交
335 336
        goto fail;
    }
337
    /* end of resource data, ignoring the following resource map */
C
Christoph Hellwig 已提交
338 339
    info_end = info_begin + count;

340
    /* begin of resource data (consisting of one or more resources) */
C
Christoph Hellwig 已提交
341
    offset = info_begin + 0x100;
342

343
    /* read offsets (mish blocks) from one or more resources in resource data */
C
Christoph Hellwig 已提交
344
    while (offset < info_end) {
345
        /* size of following resource */
K
Kevin Wolf 已提交
346 347 348 349 350 351 352
        ret = read_uint32(bs, offset, &count);
        if (ret < 0) {
            goto fail;
        } else if (count == 0) {
            ret = -EINVAL;
            goto fail;
        }
C
Christoph Hellwig 已提交
353 354
        offset += 4;

355
        ret = dmg_read_mish_block(bs, &ds, offset, count);
K
Kevin Wolf 已提交
356 357 358
        if (ret < 0) {
            goto fail;
        }
359 360
        /* advance offset by size of resource */
        offset += count;
361 362 363
    }

    /* initialize zlib engine */
364
    s->compressed_chunk = qemu_try_blockalign(bs->file,
365
                                              ds.max_compressed_size + 1);
366
    s->uncompressed_chunk = qemu_try_blockalign(bs->file,
367
                                                512 * ds.max_sectors_per_chunk);
368 369 370 371 372
    if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
        ret = -ENOMEM;
        goto fail;
    }

373
    if (inflateInit(&s->zstream) != Z_OK) {
K
Kevin Wolf 已提交
374 375 376
        ret = -EINVAL;
        goto fail;
    }
377 378

    s->current_chunk = s->n_chunks;
379

380
    qemu_co_mutex_init(&s->lock);
381
    return 0;
K
Kevin Wolf 已提交
382

C
Christoph Hellwig 已提交
383
fail:
K
Kevin Wolf 已提交
384 385 386 387 388
    g_free(s->types);
    g_free(s->offsets);
    g_free(s->lengths);
    g_free(s->sectors);
    g_free(s->sectorcounts);
389 390
    qemu_vfree(s->compressed_chunk);
    qemu_vfree(s->uncompressed_chunk);
K
Kevin Wolf 已提交
391
    return ret;
392 393 394
}

static inline int is_sector_in_chunk(BDRVDMGState* s,
395
                uint32_t chunk_num, uint64_t sector_num)
396
{
397 398 399 400 401 402
    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;
    }
403 404
}

405
static inline uint32_t search_chunk(BDRVDMGState *s, uint64_t sector_num)
406 407
{
    /* binary search */
408 409 410 411 412 413 414 415 416 417
    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;
        }
418 419 420 421
    }
    return s->n_chunks; /* error */
}

422
static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
423
{
C
Christoph Hellwig 已提交
424 425
    BDRVDMGState *s = bs->opaque;

426 427 428
    if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
        int ret;
        uint32_t chunk = search_chunk(s, sector_num);
429

430 431 432
        if (chunk >= s->n_chunks) {
            return -1;
        }
433

434 435 436 437 438
        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. */
439 440
            ret = bdrv_pread(bs->file, s->offsets[chunk],
                             s->compressed_chunk, s->lengths[chunk]);
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
            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],
C
Christoph Hellwig 已提交
461
                             s->uncompressed_chunk, s->lengths[chunk]);
462 463 464 465 466 467 468 469 470
            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;
471 472 473 474
    }
    return 0;
}

475
static int dmg_read(BlockDriverState *bs, int64_t sector_num,
476 477 478 479 480
                    uint8_t *buf, int nb_sectors)
{
    BDRVDMGState *s = bs->opaque;
    int i;

481 482 483 484 485 486 487 488
    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);
489 490 491 492
    }
    return 0;
}

493 494 495 496 497 498 499 500 501 502 503
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;
}

504 505 506
static void dmg_close(BlockDriverState *bs)
{
    BDRVDMGState *s = bs->opaque;
K
Kevin Wolf 已提交
507 508 509 510 511 512

    g_free(s->types);
    g_free(s->offsets);
    g_free(s->lengths);
    g_free(s->sectors);
    g_free(s->sectorcounts);
513 514
    qemu_vfree(s->compressed_chunk);
    qemu_vfree(s->uncompressed_chunk);
K
Kevin Wolf 已提交
515

516 517 518
    inflateEnd(&s->zstream);
}

519
static BlockDriver bdrv_dmg = {
520 521 522 523 524 525
    .format_name    = "dmg",
    .instance_size  = sizeof(BDRVDMGState),
    .bdrv_probe     = dmg_probe,
    .bdrv_open      = dmg_open,
    .bdrv_read      = dmg_co_read,
    .bdrv_close     = dmg_close,
526
};
527 528 529 530 531 532 533

static void bdrv_dmg_init(void)
{
    bdrv_register(&bdrv_dmg);
}

block_init(bdrv_dmg_init);