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