]> git.proxmox.com Git - mirror_qemu.git/blame - block/dmg.c
block: Convert bdrv_pread(v) to BdrvChild
[mirror_qemu.git] / block / dmg.c
CommitLineData
585d0ed9
FB
1/*
2 * QEMU Block driver for DMG images
5fafdf24 3 *
585d0ed9 4 * Copyright (c) 2004 Johannes E. Schindelin
5fafdf24 5 *
585d0ed9
FB
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
80c71a24 24#include "qemu/osdep.h"
da34e65c 25#include "qapi/error.h"
faf07963 26#include "qemu-common.h"
737e150e 27#include "block/block_int.h"
1de7afc9 28#include "qemu/bswap.h"
d49b6836 29#include "qemu/error-report.h"
1de7afc9 30#include "qemu/module.h"
585d0ed9 31#include <zlib.h>
6b383c08
PW
32#ifdef CONFIG_BZIP2
33#include <bzlib.h>
34#endif
585d0ed9 35
c165f775
SH
36enum {
37 /* Limit chunk sizes to prevent unreasonable amounts of memory being used
38 * or truncating when converting to 32-bit types
39 */
40 DMG_LENGTHS_MAX = 64 * 1024 * 1024, /* 64 MB */
41 DMG_SECTORCOUNTS_MAX = DMG_LENGTHS_MAX / 512,
42};
43
585d0ed9 44typedef struct BDRVDMGState {
848c66e8 45 CoMutex lock;
585d0ed9
FB
46 /* each chunk contains a certain number of sectors,
47 * offsets[i] is the offset in the .dmg file,
48 * lengths[i] is the length of the compressed chunk,
49 * sectors[i] is the sector beginning at offsets[i],
50 * sectorcounts[i] is the number of sectors in that chunk,
51 * the sectors array is ordered
52 * 0<=i<n_chunks */
53
54 uint32_t n_chunks;
55 uint32_t* types;
56 uint64_t* offsets;
57 uint64_t* lengths;
58 uint64_t* sectors;
59 uint64_t* sectorcounts;
60 uint32_t current_chunk;
28a5c9c8
FB
61 uint8_t *compressed_chunk;
62 uint8_t *uncompressed_chunk;
585d0ed9 63 z_stream zstream;
6b383c08
PW
64#ifdef CONFIG_BZIP2
65 bz_stream bzstream;
66#endif
585d0ed9
FB
67} BDRVDMGState;
68
69static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
70{
f5866fa4
KW
71 int len;
72
73 if (!filename) {
74 return 0;
75 }
76
77 len = strlen(filename);
78 if (len > 4 && !strcmp(filename + len - 4, ".dmg")) {
79 return 2;
80 }
585d0ed9
FB
81 return 0;
82}
83
69d34a36 84static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
585d0ed9 85{
69d34a36
KW
86 uint64_t buffer;
87 int ret;
88
cf2ab8fc 89 ret = bdrv_pread(bs->file, offset, &buffer, 8);
69d34a36
KW
90 if (ret < 0) {
91 return ret;
92 }
93
94 *result = be64_to_cpu(buffer);
95 return 0;
585d0ed9
FB
96}
97
69d34a36 98static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
585d0ed9 99{
69d34a36
KW
100 uint32_t buffer;
101 int ret;
102
cf2ab8fc 103 ret = bdrv_pread(bs->file, offset, &buffer, 4);
69d34a36
KW
104 if (ret < 0) {
105 return ret;
106 }
107
108 *result = be32_to_cpu(buffer);
109 return 0;
585d0ed9
FB
110}
111
7aee37b9
PW
112static inline uint64_t buff_read_uint64(const uint8_t *buffer, int64_t offset)
113{
114 return be64_to_cpu(*(uint64_t *)&buffer[offset]);
115}
116
117static inline uint32_t buff_read_uint32(const uint8_t *buffer, int64_t offset)
118{
119 return be32_to_cpu(*(uint32_t *)&buffer[offset]);
120}
121
f0dce234
SH
122/* Increase max chunk sizes, if necessary. This function is used to calculate
123 * the buffer sizes needed for compressed/uncompressed chunk I/O.
124 */
125static void update_max_chunk_size(BDRVDMGState *s, uint32_t chunk,
126 uint32_t *max_compressed_size,
127 uint32_t *max_sectors_per_chunk)
128{
129 uint32_t compressed_size = 0;
130 uint32_t uncompressed_sectors = 0;
131
132 switch (s->types[chunk]) {
133 case 0x80000005: /* zlib compressed */
6b383c08 134 case 0x80000006: /* bzip2 compressed */
f0dce234
SH
135 compressed_size = s->lengths[chunk];
136 uncompressed_sectors = s->sectorcounts[chunk];
137 break;
138 case 1: /* copy */
139 uncompressed_sectors = (s->lengths[chunk] + 511) / 512;
140 break;
141 case 2: /* zero */
177b7510
PW
142 /* as the all-zeroes block may be large, it is treated specially: the
143 * sector is not copied from a large buffer, a simple memset is used
144 * instead. Therefore uncompressed_sectors does not need to be set. */
f0dce234
SH
145 break;
146 }
147
148 if (compressed_size > *max_compressed_size) {
149 *max_compressed_size = compressed_size;
150 }
151 if (uncompressed_sectors > *max_sectors_per_chunk) {
152 *max_sectors_per_chunk = uncompressed_sectors;
153 }
154}
155
cf2ab8fc 156static int64_t dmg_find_koly_offset(BdrvChild *file, Error **errp)
fa8354bd 157{
cf2ab8fc 158 BlockDriverState *file_bs = file->bs;
fa8354bd
PW
159 int64_t length;
160 int64_t offset = 0;
161 uint8_t buffer[515];
162 int i, ret;
163
164 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
165 * dmg images can have odd sizes, try to look for the "koly" magic which
166 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
167 * in the last 511 bytes of the second-last sector or the first 4 bytes of
168 * the last sector (search space: 515 bytes) */
169 length = bdrv_getlength(file_bs);
170 if (length < 0) {
171 error_setg_errno(errp, -length,
172 "Failed to get file size while reading UDIF trailer");
173 return length;
174 } else if (length < 512) {
175 error_setg(errp, "dmg file must be at least 512 bytes long");
176 return -EINVAL;
177 }
178 if (length > 511 + 512) {
179 offset = length - 511 - 512;
180 }
181 length = length < 515 ? length : 515;
cf2ab8fc 182 ret = bdrv_pread(file, offset, buffer, length);
fa8354bd
PW
183 if (ret < 0) {
184 error_setg_errno(errp, -ret, "Failed while reading UDIF trailer");
185 return ret;
186 }
187 for (i = 0; i < length - 3; i++) {
188 if (buffer[i] == 'k' && buffer[i+1] == 'o' &&
189 buffer[i+2] == 'l' && buffer[i+3] == 'y') {
190 return offset + i;
191 }
192 }
193 error_setg(errp, "Could not locate UDIF trailer in dmg file");
194 return -EINVAL;
195}
196
65a1c7c9
PW
197/* used when building the sector table */
198typedef struct DmgHeaderState {
199 /* used internally by dmg_read_mish_block to remember offsets of blocks
200 * across calls */
c6d34865 201 uint64_t data_fork_offset;
65a1c7c9
PW
202 /* exported for dmg_open */
203 uint32_t max_compressed_size;
204 uint32_t max_sectors_per_chunk;
205} DmgHeaderState;
206
a8b10c6e
PW
207static bool dmg_is_known_block_type(uint32_t entry_type)
208{
209 switch (entry_type) {
210 case 0x00000001: /* uncompressed */
211 case 0x00000002: /* zeroes */
212 case 0x80000005: /* zlib */
6b383c08
PW
213#ifdef CONFIG_BZIP2
214 case 0x80000006: /* bzip2 */
215#endif
a8b10c6e
PW
216 return true;
217 default:
218 return false;
219 }
220}
221
7aee37b9
PW
222static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,
223 uint8_t *buffer, uint32_t count)
65a1c7c9 224{
65a1c7c9
PW
225 uint32_t type, i;
226 int ret;
227 size_t new_size;
228 uint32_t chunk_count;
7aee37b9 229 int64_t offset = 0;
c6d34865
PW
230 uint64_t data_offset;
231 uint64_t in_offset = ds->data_fork_offset;
66ec3bba 232 uint64_t out_offset;
65a1c7c9 233
7aee37b9 234 type = buff_read_uint32(buffer, offset);
65a1c7c9
PW
235 /* skip data that is not a valid MISH block (invalid magic or too small) */
236 if (type != 0x6d697368 || count < 244) {
237 /* assume success for now */
238 return 0;
239 }
240
66ec3bba
PW
241 /* chunk offsets are relative to this sector number */
242 out_offset = buff_read_uint64(buffer, offset + 8);
243
c6d34865
PW
244 /* location in data fork for (compressed) blob (in bytes) */
245 data_offset = buff_read_uint64(buffer, offset + 0x18);
246 in_offset += data_offset;
247
248 /* move to begin of chunk entries */
249 offset += 204;
65a1c7c9
PW
250
251 chunk_count = (count - 204) / 40;
252 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
253 s->types = g_realloc(s->types, new_size / 2);
254 s->offsets = g_realloc(s->offsets, new_size);
255 s->lengths = g_realloc(s->lengths, new_size);
256 s->sectors = g_realloc(s->sectors, new_size);
257 s->sectorcounts = g_realloc(s->sectorcounts, new_size);
258
259 for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
7aee37b9 260 s->types[i] = buff_read_uint32(buffer, offset);
a8b10c6e 261 if (!dmg_is_known_block_type(s->types[i])) {
65a1c7c9
PW
262 chunk_count--;
263 i--;
a8b10c6e 264 offset += 40;
65a1c7c9
PW
265 continue;
266 }
65a1c7c9 267
a8b10c6e
PW
268 /* sector number */
269 s->sectors[i] = buff_read_uint64(buffer, offset + 8);
66ec3bba 270 s->sectors[i] += out_offset;
65a1c7c9 271
a8b10c6e
PW
272 /* sector count */
273 s->sectorcounts[i] = buff_read_uint64(buffer, offset + 0x10);
65a1c7c9 274
177b7510
PW
275 /* all-zeroes sector (type 2) does not need to be "uncompressed" and can
276 * therefore be unbounded. */
277 if (s->types[i] != 2 && s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
65a1c7c9
PW
278 error_report("sector count %" PRIu64 " for chunk %" PRIu32
279 " is larger than max (%u)",
280 s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
281 ret = -EINVAL;
282 goto fail;
283 }
284
a8b10c6e
PW
285 /* offset in (compressed) data fork */
286 s->offsets[i] = buff_read_uint64(buffer, offset + 0x18);
c6d34865 287 s->offsets[i] += in_offset;
65a1c7c9 288
a8b10c6e
PW
289 /* length in (compressed) data fork */
290 s->lengths[i] = buff_read_uint64(buffer, offset + 0x20);
65a1c7c9
PW
291
292 if (s->lengths[i] > DMG_LENGTHS_MAX) {
293 error_report("length %" PRIu64 " for chunk %" PRIu32
294 " is larger than max (%u)",
295 s->lengths[i], i, DMG_LENGTHS_MAX);
296 ret = -EINVAL;
297 goto fail;
298 }
299
300 update_max_chunk_size(s, i, &ds->max_compressed_size,
301 &ds->max_sectors_per_chunk);
a8b10c6e 302 offset += 40;
65a1c7c9
PW
303 }
304 s->n_chunks += chunk_count;
305 return 0;
306
307fail:
308 return ret;
309}
310
b0e8dc5d
PW
311static int dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds,
312 uint64_t info_begin, uint64_t info_length)
585d0ed9 313{
7aee37b9 314 BDRVDMGState *s = bs->opaque;
69d34a36 315 int ret;
b0e8dc5d 316 uint32_t count, rsrc_data_offset;
7aee37b9 317 uint8_t *buffer = NULL;
b0e8dc5d
PW
318 uint64_t info_end;
319 uint64_t offset;
585d0ed9 320
b0e8dc5d 321 /* read offset from begin of resource fork (info_begin) to resource data */
65a1c7c9 322 ret = read_uint32(bs, info_begin, &rsrc_data_offset);
69d34a36
KW
323 if (ret < 0) {
324 goto fail;
b0e8dc5d 325 } else if (rsrc_data_offset > info_length) {
69d34a36 326 ret = -EINVAL;
16cdf7ce
CH
327 goto fail;
328 }
329
b0e8dc5d
PW
330 /* read length of resource data */
331 ret = read_uint32(bs, info_begin + 8, &count);
69d34a36
KW
332 if (ret < 0) {
333 goto fail;
b0e8dc5d 334 } else if (count == 0 || rsrc_data_offset + count > info_length) {
69d34a36 335 ret = -EINVAL;
16cdf7ce
CH
336 goto fail;
337 }
16cdf7ce 338
65a1c7c9 339 /* begin of resource data (consisting of one or more resources) */
b0e8dc5d
PW
340 offset = info_begin + rsrc_data_offset;
341
342 /* end of resource data (there is possibly a following resource map
343 * which will be ignored). */
344 info_end = offset + count;
585d0ed9 345
65a1c7c9 346 /* read offsets (mish blocks) from one or more resources in resource data */
16cdf7ce 347 while (offset < info_end) {
65a1c7c9 348 /* size of following resource */
69d34a36
KW
349 ret = read_uint32(bs, offset, &count);
350 if (ret < 0) {
351 goto fail;
f6e6652d 352 } else if (count == 0 || count > info_end - offset) {
69d34a36
KW
353 ret = -EINVAL;
354 goto fail;
355 }
16cdf7ce
CH
356 offset += 4;
357
7aee37b9 358 buffer = g_realloc(buffer, count);
cf2ab8fc 359 ret = bdrv_pread(bs->file, offset, buffer, count);
7aee37b9
PW
360 if (ret < 0) {
361 goto fail;
362 }
363
364 ret = dmg_read_mish_block(s, ds, buffer, count);
69d34a36
KW
365 if (ret < 0) {
366 goto fail;
367 }
65a1c7c9
PW
368 /* advance offset by size of resource */
369 offset += count;
585d0ed9 370 }
7aee37b9 371 ret = 0;
b0e8dc5d
PW
372
373fail:
7aee37b9 374 g_free(buffer);
b0e8dc5d
PW
375 return ret;
376}
377
0599e56e
PW
378static int dmg_read_plist_xml(BlockDriverState *bs, DmgHeaderState *ds,
379 uint64_t info_begin, uint64_t info_length)
380{
381 BDRVDMGState *s = bs->opaque;
382 int ret;
383 uint8_t *buffer = NULL;
384 char *data_begin, *data_end;
385
386 /* Have at least some length to avoid NULL for g_malloc. Attempt to set a
387 * safe upper cap on the data length. A test sample had a XML length of
388 * about 1 MiB. */
389 if (info_length == 0 || info_length > 16 * 1024 * 1024) {
390 ret = -EINVAL;
391 goto fail;
392 }
393
394 buffer = g_malloc(info_length + 1);
395 buffer[info_length] = '\0';
cf2ab8fc 396 ret = bdrv_pread(bs->file, info_begin, buffer, info_length);
0599e56e
PW
397 if (ret != info_length) {
398 ret = -EINVAL;
399 goto fail;
400 }
401
402 /* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
403 * decode. The actual data element has 431 (0x1af) bytes which includes tabs
404 * and line feeds. */
405 data_end = (char *)buffer;
406 while ((data_begin = strstr(data_end, "<data>")) != NULL) {
407 guchar *mish;
408 gsize out_len = 0;
409
410 data_begin += 6;
411 data_end = strstr(data_begin, "</data>");
412 /* malformed XML? */
413 if (data_end == NULL) {
414 ret = -EINVAL;
415 goto fail;
416 }
417 *data_end++ = '\0';
418 mish = g_base64_decode(data_begin, &out_len);
419 ret = dmg_read_mish_block(s, ds, mish, (uint32_t)out_len);
420 g_free(mish);
421 if (ret < 0) {
422 goto fail;
423 }
424 }
425 ret = 0;
426
427fail:
428 g_free(buffer);
429 return ret;
430}
431
b0e8dc5d
PW
432static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
433 Error **errp)
434{
435 BDRVDMGState *s = bs->opaque;
436 DmgHeaderState ds;
437 uint64_t rsrc_fork_offset, rsrc_fork_length;
0599e56e 438 uint64_t plist_xml_offset, plist_xml_length;
b0e8dc5d
PW
439 int64_t offset;
440 int ret;
441
54115412 442 bs->read_only = true;
3edf1e73 443
b0e8dc5d
PW
444 s->n_chunks = 0;
445 s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
446 /* used by dmg_read_mish_block to keep track of the current I/O position */
c6d34865 447 ds.data_fork_offset = 0;
b0e8dc5d
PW
448 ds.max_compressed_size = 1;
449 ds.max_sectors_per_chunk = 1;
450
451 /* locate the UDIF trailer */
cf2ab8fc 452 offset = dmg_find_koly_offset(bs->file, errp);
b0e8dc5d
PW
453 if (offset < 0) {
454 ret = offset;
455 goto fail;
456 }
457
c6d34865
PW
458 /* offset of data fork (DataForkOffset) */
459 ret = read_uint64(bs, offset + 0x18, &ds.data_fork_offset);
460 if (ret < 0) {
461 goto fail;
462 } else if (ds.data_fork_offset > offset) {
463 ret = -EINVAL;
464 goto fail;
465 }
466
b0e8dc5d
PW
467 /* offset of resource fork (RsrcForkOffset) */
468 ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
469 if (ret < 0) {
470 goto fail;
471 }
472 ret = read_uint64(bs, offset + 0x30, &rsrc_fork_length);
473 if (ret < 0) {
474 goto fail;
475 }
f6e6652d
PW
476 if (rsrc_fork_offset >= offset ||
477 rsrc_fork_length > offset - rsrc_fork_offset) {
478 ret = -EINVAL;
479 goto fail;
480 }
0599e56e
PW
481 /* offset of property list (XMLOffset) */
482 ret = read_uint64(bs, offset + 0xd8, &plist_xml_offset);
483 if (ret < 0) {
484 goto fail;
485 }
486 ret = read_uint64(bs, offset + 0xe0, &plist_xml_length);
487 if (ret < 0) {
488 goto fail;
489 }
490 if (plist_xml_offset >= offset ||
491 plist_xml_length > offset - plist_xml_offset) {
492 ret = -EINVAL;
493 goto fail;
494 }
8daf4257
PW
495 ret = read_uint64(bs, offset + 0x1ec, (uint64_t *)&bs->total_sectors);
496 if (ret < 0) {
497 goto fail;
498 }
499 if (bs->total_sectors < 0) {
500 ret = -EINVAL;
501 goto fail;
502 }
b0e8dc5d
PW
503 if (rsrc_fork_length != 0) {
504 ret = dmg_read_resource_fork(bs, &ds,
505 rsrc_fork_offset, rsrc_fork_length);
506 if (ret < 0) {
507 goto fail;
508 }
0599e56e
PW
509 } else if (plist_xml_length != 0) {
510 ret = dmg_read_plist_xml(bs, &ds, plist_xml_offset, plist_xml_length);
511 if (ret < 0) {
512 goto fail;
513 }
b0e8dc5d
PW
514 } else {
515 ret = -EINVAL;
516 goto fail;
517 }
585d0ed9
FB
518
519 /* initialize zlib engine */
9a4f4c31 520 s->compressed_chunk = qemu_try_blockalign(bs->file->bs,
65a1c7c9 521 ds.max_compressed_size + 1);
9a4f4c31 522 s->uncompressed_chunk = qemu_try_blockalign(bs->file->bs,
65a1c7c9 523 512 * ds.max_sectors_per_chunk);
b546a944
KW
524 if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
525 ret = -ENOMEM;
526 goto fail;
527 }
528
2c1885ad 529 if (inflateInit(&s->zstream) != Z_OK) {
69d34a36
KW
530 ret = -EINVAL;
531 goto fail;
532 }
585d0ed9
FB
533
534 s->current_chunk = s->n_chunks;
3b46e624 535
848c66e8 536 qemu_co_mutex_init(&s->lock);
585d0ed9 537 return 0;
69d34a36 538
1559ca00 539fail:
69d34a36
KW
540 g_free(s->types);
541 g_free(s->offsets);
542 g_free(s->lengths);
543 g_free(s->sectors);
544 g_free(s->sectorcounts);
b546a944
KW
545 qemu_vfree(s->compressed_chunk);
546 qemu_vfree(s->uncompressed_chunk);
69d34a36 547 return ret;
585d0ed9
FB
548}
549
a6506481
EB
550static void dmg_refresh_limits(BlockDriverState *bs, Error **errp)
551{
a5b8dd2c 552 bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
a6506481
EB
553}
554
585d0ed9 555static inline int is_sector_in_chunk(BDRVDMGState* s,
686d7148 556 uint32_t chunk_num, uint64_t sector_num)
585d0ed9 557{
2c1885ad
SH
558 if (chunk_num >= s->n_chunks || s->sectors[chunk_num] > sector_num ||
559 s->sectors[chunk_num] + s->sectorcounts[chunk_num] <= sector_num) {
560 return 0;
561 } else {
562 return -1;
563 }
585d0ed9
FB
564}
565
686d7148 566static inline uint32_t search_chunk(BDRVDMGState *s, uint64_t sector_num)
585d0ed9
FB
567{
568 /* binary search */
2c1885ad
SH
569 uint32_t chunk1 = 0, chunk2 = s->n_chunks, chunk3;
570 while (chunk1 != chunk2) {
571 chunk3 = (chunk1 + chunk2) / 2;
572 if (s->sectors[chunk3] > sector_num) {
573 chunk2 = chunk3;
574 } else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) {
575 return chunk3;
576 } else {
577 chunk1 = chunk3;
578 }
585d0ed9
FB
579 }
580 return s->n_chunks; /* error */
581}
582
686d7148 583static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
585d0ed9 584{
64a31d5c
CH
585 BDRVDMGState *s = bs->opaque;
586
2c1885ad
SH
587 if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
588 int ret;
589 uint32_t chunk = search_chunk(s, sector_num);
6b383c08
PW
590#ifdef CONFIG_BZIP2
591 uint64_t total_out;
592#endif
585d0ed9 593
2c1885ad
SH
594 if (chunk >= s->n_chunks) {
595 return -1;
596 }
585d0ed9 597
2c1885ad 598 s->current_chunk = s->n_chunks;
6b383c08 599 switch (s->types[chunk]) { /* block entry type */
2c1885ad 600 case 0x80000005: { /* zlib compressed */
2c1885ad
SH
601 /* we need to buffer, because only the chunk as whole can be
602 * inflated. */
cf2ab8fc 603 ret = bdrv_pread(bs->file, s->offsets[chunk],
b404bf85 604 s->compressed_chunk, s->lengths[chunk]);
2c1885ad
SH
605 if (ret != s->lengths[chunk]) {
606 return -1;
607 }
608
609 s->zstream.next_in = s->compressed_chunk;
610 s->zstream.avail_in = s->lengths[chunk];
611 s->zstream.next_out = s->uncompressed_chunk;
612 s->zstream.avail_out = 512 * s->sectorcounts[chunk];
613 ret = inflateReset(&s->zstream);
614 if (ret != Z_OK) {
615 return -1;
616 }
617 ret = inflate(&s->zstream, Z_FINISH);
618 if (ret != Z_STREAM_END ||
619 s->zstream.total_out != 512 * s->sectorcounts[chunk]) {
620 return -1;
621 }
622 break; }
6b383c08
PW
623#ifdef CONFIG_BZIP2
624 case 0x80000006: /* bzip2 compressed */
625 /* we need to buffer, because only the chunk as whole can be
626 * inflated. */
cf2ab8fc 627 ret = bdrv_pread(bs->file, s->offsets[chunk],
6b383c08
PW
628 s->compressed_chunk, s->lengths[chunk]);
629 if (ret != s->lengths[chunk]) {
630 return -1;
631 }
632
633 ret = BZ2_bzDecompressInit(&s->bzstream, 0, 0);
634 if (ret != BZ_OK) {
635 return -1;
636 }
637 s->bzstream.next_in = (char *)s->compressed_chunk;
638 s->bzstream.avail_in = (unsigned int) s->lengths[chunk];
639 s->bzstream.next_out = (char *)s->uncompressed_chunk;
640 s->bzstream.avail_out = (unsigned int) 512 * s->sectorcounts[chunk];
641 ret = BZ2_bzDecompress(&s->bzstream);
642 total_out = ((uint64_t)s->bzstream.total_out_hi32 << 32) +
643 s->bzstream.total_out_lo32;
644 BZ2_bzDecompressEnd(&s->bzstream);
645 if (ret != BZ_STREAM_END ||
646 total_out != 512 * s->sectorcounts[chunk]) {
647 return -1;
648 }
649 break;
650#endif /* CONFIG_BZIP2 */
2c1885ad 651 case 1: /* copy */
cf2ab8fc 652 ret = bdrv_pread(bs->file, s->offsets[chunk],
64a31d5c 653 s->uncompressed_chunk, s->lengths[chunk]);
2c1885ad
SH
654 if (ret != s->lengths[chunk]) {
655 return -1;
656 }
657 break;
658 case 2: /* zero */
177b7510
PW
659 /* see dmg_read, it is treated specially. No buffer needs to be
660 * pre-filled, the zeroes can be set directly. */
2c1885ad
SH
661 break;
662 }
663 s->current_chunk = chunk;
585d0ed9
FB
664 }
665 return 0;
666}
667
3edf1e73
KW
668static int coroutine_fn
669dmg_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
670 QEMUIOVector *qiov, int flags)
585d0ed9
FB
671{
672 BDRVDMGState *s = bs->opaque;
3edf1e73
KW
673 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
674 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
675 int ret, i;
676
677 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
678 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
679
680 qemu_co_mutex_lock(&s->lock);
585d0ed9 681
2c1885ad
SH
682 for (i = 0; i < nb_sectors; i++) {
683 uint32_t sector_offset_in_chunk;
3edf1e73
KW
684 void *data;
685
2c1885ad 686 if (dmg_read_chunk(bs, sector_num + i) != 0) {
3edf1e73
KW
687 ret = -EIO;
688 goto fail;
2c1885ad 689 }
177b7510
PW
690 /* Special case: current chunk is all zeroes. Do not perform a memcpy as
691 * s->uncompressed_chunk may be too small to cover the large all-zeroes
692 * section. dmg_read_chunk is called to find s->current_chunk */
693 if (s->types[s->current_chunk] == 2) { /* all zeroes block entry */
3edf1e73 694 qemu_iovec_memset(qiov, i * 512, 0, 512);
177b7510
PW
695 continue;
696 }
2c1885ad 697 sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
3edf1e73
KW
698 data = s->uncompressed_chunk + sector_offset_in_chunk * 512;
699 qemu_iovec_from_buf(qiov, i * 512, data, 512);
585d0ed9 700 }
585d0ed9 701
3edf1e73
KW
702 ret = 0;
703fail:
2914caa0
PB
704 qemu_co_mutex_unlock(&s->lock);
705 return ret;
706}
707
585d0ed9
FB
708static void dmg_close(BlockDriverState *bs)
709{
710 BDRVDMGState *s = bs->opaque;
4f8aa2e1
KW
711
712 g_free(s->types);
713 g_free(s->offsets);
714 g_free(s->lengths);
715 g_free(s->sectors);
716 g_free(s->sectorcounts);
b546a944
KW
717 qemu_vfree(s->compressed_chunk);
718 qemu_vfree(s->uncompressed_chunk);
4f8aa2e1 719
585d0ed9
FB
720 inflateEnd(&s->zstream);
721}
722
5efa9d5a 723static BlockDriver bdrv_dmg = {
2c1885ad
SH
724 .format_name = "dmg",
725 .instance_size = sizeof(BDRVDMGState),
726 .bdrv_probe = dmg_probe,
727 .bdrv_open = dmg_open,
a6506481 728 .bdrv_refresh_limits = dmg_refresh_limits,
3edf1e73 729 .bdrv_co_preadv = dmg_co_preadv,
2c1885ad 730 .bdrv_close = dmg_close,
585d0ed9 731};
5efa9d5a
AL
732
733static void bdrv_dmg_init(void)
734{
735 bdrv_register(&bdrv_dmg);
736}
737
738block_init(bdrv_dmg_init);