block/dmg: process a buffer instead of reading ints
[qemu/ar7.git] / block / dmg.c
blob4f56227fc5f55593049ea776974cb5a603b6dd67
1 /*
2 * QEMU Block driver for DMG images
4 * Copyright (c) 2004 Johannes E. Schindelin
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu-common.h"
25 #include "block/block_int.h"
26 #include "qemu/bswap.h"
27 #include "qemu/module.h"
28 #include <zlib.h>
30 enum {
31 /* Limit chunk sizes to prevent unreasonable amounts of memory being used
32 * or truncating when converting to 32-bit types
34 DMG_LENGTHS_MAX = 64 * 1024 * 1024, /* 64 MB */
35 DMG_SECTORCOUNTS_MAX = DMG_LENGTHS_MAX / 512,
38 typedef struct BDRVDMGState {
39 CoMutex lock;
40 /* each chunk contains a certain number of sectors,
41 * offsets[i] is the offset in the .dmg file,
42 * lengths[i] is the length of the compressed chunk,
43 * sectors[i] is the sector beginning at offsets[i],
44 * sectorcounts[i] is the number of sectors in that chunk,
45 * the sectors array is ordered
46 * 0<=i<n_chunks */
48 uint32_t n_chunks;
49 uint32_t* types;
50 uint64_t* offsets;
51 uint64_t* lengths;
52 uint64_t* sectors;
53 uint64_t* sectorcounts;
54 uint32_t current_chunk;
55 uint8_t *compressed_chunk;
56 uint8_t *uncompressed_chunk;
57 z_stream zstream;
58 } BDRVDMGState;
60 static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
62 int len;
64 if (!filename) {
65 return 0;
68 len = strlen(filename);
69 if (len > 4 && !strcmp(filename + len - 4, ".dmg")) {
70 return 2;
72 return 0;
75 static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
77 uint64_t buffer;
78 int ret;
80 ret = bdrv_pread(bs->file, offset, &buffer, 8);
81 if (ret < 0) {
82 return ret;
85 *result = be64_to_cpu(buffer);
86 return 0;
89 static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
91 uint32_t buffer;
92 int ret;
94 ret = bdrv_pread(bs->file, offset, &buffer, 4);
95 if (ret < 0) {
96 return ret;
99 *result = be32_to_cpu(buffer);
100 return 0;
103 static inline uint64_t buff_read_uint64(const uint8_t *buffer, int64_t offset)
105 return be64_to_cpu(*(uint64_t *)&buffer[offset]);
108 static inline uint32_t buff_read_uint32(const uint8_t *buffer, int64_t offset)
110 return be32_to_cpu(*(uint32_t *)&buffer[offset]);
113 /* Increase max chunk sizes, if necessary. This function is used to calculate
114 * the buffer sizes needed for compressed/uncompressed chunk I/O.
116 static void update_max_chunk_size(BDRVDMGState *s, uint32_t chunk,
117 uint32_t *max_compressed_size,
118 uint32_t *max_sectors_per_chunk)
120 uint32_t compressed_size = 0;
121 uint32_t uncompressed_sectors = 0;
123 switch (s->types[chunk]) {
124 case 0x80000005: /* zlib compressed */
125 compressed_size = s->lengths[chunk];
126 uncompressed_sectors = s->sectorcounts[chunk];
127 break;
128 case 1: /* copy */
129 uncompressed_sectors = (s->lengths[chunk] + 511) / 512;
130 break;
131 case 2: /* zero */
132 uncompressed_sectors = s->sectorcounts[chunk];
133 break;
136 if (compressed_size > *max_compressed_size) {
137 *max_compressed_size = compressed_size;
139 if (uncompressed_sectors > *max_sectors_per_chunk) {
140 *max_sectors_per_chunk = uncompressed_sectors;
144 static int64_t dmg_find_koly_offset(BlockDriverState *file_bs, Error **errp)
146 int64_t length;
147 int64_t offset = 0;
148 uint8_t buffer[515];
149 int i, ret;
151 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
152 * dmg images can have odd sizes, try to look for the "koly" magic which
153 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
154 * in the last 511 bytes of the second-last sector or the first 4 bytes of
155 * the last sector (search space: 515 bytes) */
156 length = bdrv_getlength(file_bs);
157 if (length < 0) {
158 error_setg_errno(errp, -length,
159 "Failed to get file size while reading UDIF trailer");
160 return length;
161 } else if (length < 512) {
162 error_setg(errp, "dmg file must be at least 512 bytes long");
163 return -EINVAL;
165 if (length > 511 + 512) {
166 offset = length - 511 - 512;
168 length = length < 515 ? length : 515;
169 ret = bdrv_pread(file_bs, offset, buffer, length);
170 if (ret < 0) {
171 error_setg_errno(errp, -ret, "Failed while reading UDIF trailer");
172 return ret;
174 for (i = 0; i < length - 3; i++) {
175 if (buffer[i] == 'k' && buffer[i+1] == 'o' &&
176 buffer[i+2] == 'l' && buffer[i+3] == 'y') {
177 return offset + i;
180 error_setg(errp, "Could not locate UDIF trailer in dmg file");
181 return -EINVAL;
184 /* used when building the sector table */
185 typedef struct DmgHeaderState {
186 /* used internally by dmg_read_mish_block to remember offsets of blocks
187 * across calls */
188 uint64_t last_in_offset;
189 uint64_t last_out_offset;
190 /* exported for dmg_open */
191 uint32_t max_compressed_size;
192 uint32_t max_sectors_per_chunk;
193 } DmgHeaderState;
195 static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,
196 uint8_t *buffer, uint32_t count)
198 uint32_t type, i;
199 int ret;
200 size_t new_size;
201 uint32_t chunk_count;
202 int64_t offset = 0;
204 type = buff_read_uint32(buffer, offset);
205 /* skip data that is not a valid MISH block (invalid magic or too small) */
206 if (type != 0x6d697368 || count < 244) {
207 /* assume success for now */
208 return 0;
211 offset += 4;
212 offset += 200;
214 chunk_count = (count - 204) / 40;
215 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
216 s->types = g_realloc(s->types, new_size / 2);
217 s->offsets = g_realloc(s->offsets, new_size);
218 s->lengths = g_realloc(s->lengths, new_size);
219 s->sectors = g_realloc(s->sectors, new_size);
220 s->sectorcounts = g_realloc(s->sectorcounts, new_size);
222 for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
223 s->types[i] = buff_read_uint32(buffer, offset);
224 offset += 4;
225 if (s->types[i] != 0x80000005 && s->types[i] != 1 &&
226 s->types[i] != 2) {
227 if (s->types[i] == 0xffffffff && i > 0) {
228 ds->last_in_offset = s->offsets[i - 1] + s->lengths[i - 1];
229 ds->last_out_offset = s->sectors[i - 1] +
230 s->sectorcounts[i - 1];
232 chunk_count--;
233 i--;
234 offset += 36;
235 continue;
237 offset += 4;
239 s->sectors[i] = buff_read_uint64(buffer, offset);
240 s->sectors[i] += ds->last_out_offset;
241 offset += 8;
243 s->sectorcounts[i] = buff_read_uint64(buffer, offset);
244 offset += 8;
246 if (s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
247 error_report("sector count %" PRIu64 " for chunk %" PRIu32
248 " is larger than max (%u)",
249 s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
250 ret = -EINVAL;
251 goto fail;
254 s->offsets[i] = buff_read_uint64(buffer, offset);
255 s->offsets[i] += ds->last_in_offset;
256 offset += 8;
258 s->lengths[i] = buff_read_uint64(buffer, offset);
259 offset += 8;
261 if (s->lengths[i] > DMG_LENGTHS_MAX) {
262 error_report("length %" PRIu64 " for chunk %" PRIu32
263 " is larger than max (%u)",
264 s->lengths[i], i, DMG_LENGTHS_MAX);
265 ret = -EINVAL;
266 goto fail;
269 update_max_chunk_size(s, i, &ds->max_compressed_size,
270 &ds->max_sectors_per_chunk);
272 s->n_chunks += chunk_count;
273 return 0;
275 fail:
276 return ret;
279 static int dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds,
280 uint64_t info_begin, uint64_t info_length)
282 BDRVDMGState *s = bs->opaque;
283 int ret;
284 uint32_t count, rsrc_data_offset;
285 uint8_t *buffer = NULL;
286 uint64_t info_end;
287 uint64_t offset;
289 /* read offset from begin of resource fork (info_begin) to resource data */
290 ret = read_uint32(bs, info_begin, &rsrc_data_offset);
291 if (ret < 0) {
292 goto fail;
293 } else if (rsrc_data_offset > info_length) {
294 ret = -EINVAL;
295 goto fail;
298 /* read length of resource data */
299 ret = read_uint32(bs, info_begin + 8, &count);
300 if (ret < 0) {
301 goto fail;
302 } else if (count == 0 || rsrc_data_offset + count > info_length) {
303 ret = -EINVAL;
304 goto fail;
307 /* begin of resource data (consisting of one or more resources) */
308 offset = info_begin + rsrc_data_offset;
310 /* end of resource data (there is possibly a following resource map
311 * which will be ignored). */
312 info_end = offset + count;
314 /* read offsets (mish blocks) from one or more resources in resource data */
315 while (offset < info_end) {
316 /* size of following resource */
317 ret = read_uint32(bs, offset, &count);
318 if (ret < 0) {
319 goto fail;
320 } else if (count == 0) {
321 ret = -EINVAL;
322 goto fail;
324 offset += 4;
326 buffer = g_realloc(buffer, count);
327 ret = bdrv_pread(bs->file, offset, buffer, count);
328 if (ret < 0) {
329 goto fail;
332 ret = dmg_read_mish_block(s, ds, buffer, count);
333 if (ret < 0) {
334 goto fail;
336 /* advance offset by size of resource */
337 offset += count;
339 ret = 0;
341 fail:
342 g_free(buffer);
343 return ret;
346 static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
347 Error **errp)
349 BDRVDMGState *s = bs->opaque;
350 DmgHeaderState ds;
351 uint64_t rsrc_fork_offset, rsrc_fork_length;
352 int64_t offset;
353 int ret;
355 bs->read_only = 1;
356 s->n_chunks = 0;
357 s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
358 /* used by dmg_read_mish_block to keep track of the current I/O position */
359 ds.last_in_offset = 0;
360 ds.last_out_offset = 0;
361 ds.max_compressed_size = 1;
362 ds.max_sectors_per_chunk = 1;
364 /* locate the UDIF trailer */
365 offset = dmg_find_koly_offset(bs->file, errp);
366 if (offset < 0) {
367 ret = offset;
368 goto fail;
371 /* offset of resource fork (RsrcForkOffset) */
372 ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
373 if (ret < 0) {
374 goto fail;
376 ret = read_uint64(bs, offset + 0x30, &rsrc_fork_length);
377 if (ret < 0) {
378 goto fail;
380 if (rsrc_fork_length != 0) {
381 ret = dmg_read_resource_fork(bs, &ds,
382 rsrc_fork_offset, rsrc_fork_length);
383 if (ret < 0) {
384 goto fail;
386 } else {
387 ret = -EINVAL;
388 goto fail;
391 /* initialize zlib engine */
392 s->compressed_chunk = qemu_try_blockalign(bs->file,
393 ds.max_compressed_size + 1);
394 s->uncompressed_chunk = qemu_try_blockalign(bs->file,
395 512 * ds.max_sectors_per_chunk);
396 if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
397 ret = -ENOMEM;
398 goto fail;
401 if (inflateInit(&s->zstream) != Z_OK) {
402 ret = -EINVAL;
403 goto fail;
406 s->current_chunk = s->n_chunks;
408 qemu_co_mutex_init(&s->lock);
409 return 0;
411 fail:
412 g_free(s->types);
413 g_free(s->offsets);
414 g_free(s->lengths);
415 g_free(s->sectors);
416 g_free(s->sectorcounts);
417 qemu_vfree(s->compressed_chunk);
418 qemu_vfree(s->uncompressed_chunk);
419 return ret;
422 static inline int is_sector_in_chunk(BDRVDMGState* s,
423 uint32_t chunk_num, uint64_t sector_num)
425 if (chunk_num >= s->n_chunks || s->sectors[chunk_num] > sector_num ||
426 s->sectors[chunk_num] + s->sectorcounts[chunk_num] <= sector_num) {
427 return 0;
428 } else {
429 return -1;
433 static inline uint32_t search_chunk(BDRVDMGState *s, uint64_t sector_num)
435 /* binary search */
436 uint32_t chunk1 = 0, chunk2 = s->n_chunks, chunk3;
437 while (chunk1 != chunk2) {
438 chunk3 = (chunk1 + chunk2) / 2;
439 if (s->sectors[chunk3] > sector_num) {
440 chunk2 = chunk3;
441 } else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) {
442 return chunk3;
443 } else {
444 chunk1 = chunk3;
447 return s->n_chunks; /* error */
450 static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
452 BDRVDMGState *s = bs->opaque;
454 if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
455 int ret;
456 uint32_t chunk = search_chunk(s, sector_num);
458 if (chunk >= s->n_chunks) {
459 return -1;
462 s->current_chunk = s->n_chunks;
463 switch (s->types[chunk]) {
464 case 0x80000005: { /* zlib compressed */
465 /* we need to buffer, because only the chunk as whole can be
466 * inflated. */
467 ret = bdrv_pread(bs->file, s->offsets[chunk],
468 s->compressed_chunk, s->lengths[chunk]);
469 if (ret != s->lengths[chunk]) {
470 return -1;
473 s->zstream.next_in = s->compressed_chunk;
474 s->zstream.avail_in = s->lengths[chunk];
475 s->zstream.next_out = s->uncompressed_chunk;
476 s->zstream.avail_out = 512 * s->sectorcounts[chunk];
477 ret = inflateReset(&s->zstream);
478 if (ret != Z_OK) {
479 return -1;
481 ret = inflate(&s->zstream, Z_FINISH);
482 if (ret != Z_STREAM_END ||
483 s->zstream.total_out != 512 * s->sectorcounts[chunk]) {
484 return -1;
486 break; }
487 case 1: /* copy */
488 ret = bdrv_pread(bs->file, s->offsets[chunk],
489 s->uncompressed_chunk, s->lengths[chunk]);
490 if (ret != s->lengths[chunk]) {
491 return -1;
493 break;
494 case 2: /* zero */
495 memset(s->uncompressed_chunk, 0, 512 * s->sectorcounts[chunk]);
496 break;
498 s->current_chunk = chunk;
500 return 0;
503 static int dmg_read(BlockDriverState *bs, int64_t sector_num,
504 uint8_t *buf, int nb_sectors)
506 BDRVDMGState *s = bs->opaque;
507 int i;
509 for (i = 0; i < nb_sectors; i++) {
510 uint32_t sector_offset_in_chunk;
511 if (dmg_read_chunk(bs, sector_num + i) != 0) {
512 return -1;
514 sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
515 memcpy(buf + i * 512,
516 s->uncompressed_chunk + sector_offset_in_chunk * 512, 512);
518 return 0;
521 static coroutine_fn int dmg_co_read(BlockDriverState *bs, int64_t sector_num,
522 uint8_t *buf, int nb_sectors)
524 int ret;
525 BDRVDMGState *s = bs->opaque;
526 qemu_co_mutex_lock(&s->lock);
527 ret = dmg_read(bs, sector_num, buf, nb_sectors);
528 qemu_co_mutex_unlock(&s->lock);
529 return ret;
532 static void dmg_close(BlockDriverState *bs)
534 BDRVDMGState *s = bs->opaque;
536 g_free(s->types);
537 g_free(s->offsets);
538 g_free(s->lengths);
539 g_free(s->sectors);
540 g_free(s->sectorcounts);
541 qemu_vfree(s->compressed_chunk);
542 qemu_vfree(s->uncompressed_chunk);
544 inflateEnd(&s->zstream);
547 static BlockDriver bdrv_dmg = {
548 .format_name = "dmg",
549 .instance_size = sizeof(BDRVDMGState),
550 .bdrv_probe = dmg_probe,
551 .bdrv_open = dmg_open,
552 .bdrv_read = dmg_co_read,
553 .bdrv_close = dmg_close,
556 static void bdrv_dmg_init(void)
558 bdrv_register(&bdrv_dmg);
561 block_init(bdrv_dmg_init);