master
c 835 lines 25.4 KB
Raw
1 /*
2 * QEMU Block driver for DMG images
3 *
4 * Copyright (c) 2004 Johannes E. Schindelin
5 *
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 */
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "block/block-io.h"
27 #include "block/block_int.h"
28 #include "qemu/bswap.h"
29 #include "qemu/error-report.h"
30 #include "qemu/module.h"
31 #include "qemu/memalign.h"
32 #include "dmg.h"
33
34 BdrvDmgUncompressFunc *dmg_uncompress_bz2;
35 BdrvDmgUncompressFunc *dmg_uncompress_lzfse;
36
37 enum {
38 /* Limit chunk sizes to prevent unreasonable amounts of memory being used
39 * or truncating when converting to 32-bit types
40 */
41 DMG_LENGTHS_MAX = 64 * 1024 * 1024, /* 64 MB */
42 DMG_SECTORCOUNTS_MAX = DMG_LENGTHS_MAX / 512,
43 };
44
45 enum {
46 /* DMG Block Type */
47 UDZE = 0, /* Zeroes */
48 UDRW, /* RAW type */
49 UDIG, /* Ignore */
50 UDCO = 0x80000004,
51 UDZO,
52 UDBZ,
53 ULFO,
54 UDCM = 0x7ffffffe, /* Comments */
55 UDLE = 0xffffffff /* Last Entry */
56 };
57
58 static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
59 {
60 int len;
61
62 if (!filename) {
63 return 0;
64 }
65
66 len = strlen(filename);
67 if (len > 4 && !strcmp(filename + len - 4, ".dmg")) {
68 return 2;
69 }
70 return 0;
71 }
72
73 static int GRAPH_RDLOCK
74 read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
75 {
76 uint64_t buffer;
77 int ret;
78
79 ret = bdrv_pread(bs->file, offset, 8, &buffer, 0);
80 if (ret < 0) {
81 return ret;
82 }
83
84 *result = be64_to_cpu(buffer);
85 return 0;
86 }
87
88 static int GRAPH_RDLOCK
89 read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
90 {
91 uint32_t buffer;
92 int ret;
93
94 ret = bdrv_pread(bs->file, offset, 4, &buffer, 0);
95 if (ret < 0) {
96 return ret;
97 }
98
99 *result = be32_to_cpu(buffer);
100 return 0;
101 }
102
103 static inline uint64_t buff_read_uint64(const uint8_t *buffer, int64_t offset)
104 {
105 return be64_to_cpu(*(uint64_t *)&buffer[offset]);
106 }
107
108 static inline uint32_t buff_read_uint32(const uint8_t *buffer, int64_t offset)
109 {
110 return be32_to_cpu(*(uint32_t *)&buffer[offset]);
111 }
112
113 /* Increase max chunk sizes, if necessary. This function is used to calculate
114 * the buffer sizes needed for compressed/uncompressed chunk I/O.
115 */
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)
119 {
120 uint32_t compressed_size = 0;
121 uint32_t uncompressed_sectors = 0;
122
123 switch (s->types[chunk]) {
124 case UDZO: /* zlib compressed */
125 case UDBZ: /* bzip2 compressed */
126 case ULFO: /* lzfse compressed */
127 compressed_size = s->lengths[chunk];
128 uncompressed_sectors = s->sectorcounts[chunk];
129 break;
130 case UDRW: /* copy */
131 uncompressed_sectors = DIV_ROUND_UP(s->lengths[chunk], 512);
132 break;
133 case UDZE: /* zero */
134 case UDIG: /* ignore */
135 /* as the all-zeroes block may be large, it is treated specially: the
136 * sector is not copied from a large buffer, a simple memset is used
137 * instead. Therefore uncompressed_sectors does not need to be set. */
138 break;
139 }
140
141 if (compressed_size > *max_compressed_size) {
142 *max_compressed_size = compressed_size;
143 }
144 if (uncompressed_sectors > *max_sectors_per_chunk) {
145 *max_sectors_per_chunk = uncompressed_sectors;
146 }
147 }
148
149 static int64_t dmg_find_koly_offset(BdrvChild *file, Error **errp)
150 {
151 BlockDriverState *file_bs = file->bs;
152 int64_t length;
153 int64_t offset = 0;
154 uint8_t buffer[515];
155 int i, ret;
156
157 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
158 * dmg images can have odd sizes, try to look for the "koly" magic which
159 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
160 * in the last 511 bytes of the second-last sector or the first 4 bytes of
161 * the last sector (search space: 515 bytes) */
162 length = bdrv_getlength(file_bs);
163 if (length < 0) {
164 error_setg_errno(errp, -length,
165 "Failed to get file size while reading UDIF trailer");
166 return length;
167 } else if (length < 512) {
168 error_setg(errp, "dmg file must be at least 512 bytes long");
169 return -EINVAL;
170 }
171 if (length > 511 + 512) {
172 offset = length - 511 - 512;
173 }
174 length = length < 515 ? length : 515;
175 ret = bdrv_pread(file, offset, length, buffer, 0);
176 if (ret < 0) {
177 error_setg_errno(errp, -ret, "Failed while reading UDIF trailer");
178 return ret;
179 }
180 for (i = 0; i < length - 3; i++) {
181 if (buffer[i] == 'k' && buffer[i+1] == 'o' &&
182 buffer[i+2] == 'l' && buffer[i+3] == 'y') {
183 return offset + i;
184 }
185 }
186 error_setg(errp, "Could not locate UDIF trailer in dmg file");
187 return -EINVAL;
188 }
189
190 /* used when building the sector table */
191 typedef struct DmgHeaderState {
192 /* used internally by dmg_read_mish_block to remember offsets of blocks
193 * across calls */
194 uint64_t data_fork_offset;
195 /* exported for dmg_open */
196 uint32_t max_compressed_size;
197 uint32_t max_sectors_per_chunk;
198 } DmgHeaderState;
199
200 static bool dmg_is_known_block_type(uint32_t entry_type)
201 {
202 switch (entry_type) {
203 case UDZE: /* zeros */
204 case UDRW: /* uncompressed */
205 case UDIG: /* ignore */
206 case UDZO: /* zlib */
207 return true;
208 case UDBZ: /* bzip2 */
209 return !!dmg_uncompress_bz2;
210 case ULFO: /* lzfse */
211 return !!dmg_uncompress_lzfse;
212 default:
213 return false;
214 }
215 }
216
217 static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,
218 uint8_t *buffer, uint32_t count)
219 {
220 uint32_t type, i;
221 int ret;
222 size_t new_size;
223 uint32_t chunk_count;
224 int64_t offset = 0;
225 uint64_t data_offset;
226 uint64_t in_offset = ds->data_fork_offset;
227 uint64_t out_offset;
228
229 type = buff_read_uint32(buffer, offset);
230 /* skip data that is not a valid MISH block (invalid magic or too small) */
231 if (type != 0x6d697368 || count < 244) {
232 /* assume success for now */
233 return 0;
234 }
235
236 /* chunk offsets are relative to this sector number */
237 out_offset = buff_read_uint64(buffer, offset + 8);
238
239 /* location in data fork for (compressed) blob (in bytes) */
240 data_offset = buff_read_uint64(buffer, offset + 0x18);
241 in_offset += data_offset;
242
243 /* move to begin of chunk entries */
244 offset += 204;
245
246 chunk_count = (count - 204) / 40;
247 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
248 s->types = g_realloc(s->types, new_size / 2);
249 s->offsets = g_realloc(s->offsets, new_size);
250 s->lengths = g_realloc(s->lengths, new_size);
251 s->sectors = g_realloc(s->sectors, new_size);
252 s->sectorcounts = g_realloc(s->sectorcounts, new_size);
253
254 for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
255 s->types[i] = buff_read_uint32(buffer, offset);
256 if (!dmg_is_known_block_type(s->types[i])) {
257 switch (s->types[i]) {
258 case UDBZ:
259 warn_report_once("dmg-bzip2 module is missing, accessing bzip2 "
260 "compressed blocks will result in I/O errors");
261 break;
262 case ULFO:
263 warn_report_once("dmg-lzfse module is missing, accessing lzfse "
264 "compressed blocks will result in I/O errors");
265 break;
266 case UDCM:
267 case UDLE:
268 /* Comments and last entry can be ignored without problems */
269 break;
270 default:
271 warn_report_once("Image contains chunks of unknown type %x, "
272 "accessing them will result in I/O errors",
273 s->types[i]);
274 break;
275 }
276 chunk_count--;
277 i--;
278 offset += 40;
279 continue;
280 }
281
282 /* sector number */
283 s->sectors[i] = buff_read_uint64(buffer, offset + 8);
284 s->sectors[i] += out_offset;
285
286 /* sector count */
287 s->sectorcounts[i] = buff_read_uint64(buffer, offset + 0x10);
288
289 /* all-zeroes sector (type UDZE and UDIG) does not need to be
290 * "uncompressed" and can therefore be unbounded. */
291 if (s->types[i] != UDZE && s->types[i] != UDIG
292 && s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
293 error_report("sector count %" PRIu64 " for chunk %" PRIu32
294 " is larger than max (%u)",
295 s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
296 ret = -EINVAL;
297 goto fail;
298 }
299
300 /* offset in (compressed) data fork */
301 s->offsets[i] = buff_read_uint64(buffer, offset + 0x18);
302 s->offsets[i] += in_offset;
303
304 /* length in (compressed) data fork */
305 s->lengths[i] = buff_read_uint64(buffer, offset + 0x20);
306
307 if (s->lengths[i] > DMG_LENGTHS_MAX) {
308 error_report("length %" PRIu64 " for chunk %" PRIu32
309 " is larger than max (%u)",
310 s->lengths[i], i, DMG_LENGTHS_MAX);
311 ret = -EINVAL;
312 goto fail;
313 }
314
315 /*
316 * Uncompressed chunk length must match sector count. Compressed chunks
317 * are validated during dmg_read_chunk() since the uncompressed size is
318 * not known ahead of time.
319 */
320 if (s->types[i] == UDRW) {
321 if (s->sectorcounts[i] != DIV_ROUND_UP(s->lengths[i], 512)) {
322 error_report("length %" PRIu64 " for chunk %" PRIu32
323 " is inconsistent with sector count %" PRIu64,
324 s->lengths[i], i, s->sectorcounts[i]);
325 ret = -EINVAL;
326 goto fail;
327 }
328 }
329
330 update_max_chunk_size(s, i, &ds->max_compressed_size,
331 &ds->max_sectors_per_chunk);
332 offset += 40;
333 }
334 s->n_chunks += chunk_count;
335 return 0;
336
337 fail:
338 return ret;
339 }
340
341 static int GRAPH_RDLOCK
342 dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds,
343 uint64_t info_begin, uint64_t info_length)
344 {
345 BDRVDMGState *s = bs->opaque;
346 int ret;
347 uint32_t count, rsrc_data_offset;
348 uint8_t *buffer = NULL;
349 uint64_t info_end;
350 uint64_t offset;
351
352 /* read offset from begin of resource fork (info_begin) to resource data */
353 ret = read_uint32(bs, info_begin, &rsrc_data_offset);
354 if (ret < 0) {
355 goto fail;
356 } else if (rsrc_data_offset > info_length) {
357 ret = -EINVAL;
358 goto fail;
359 }
360
361 /* read length of resource data */
362 ret = read_uint32(bs, info_begin + 8, &count);
363 if (ret < 0) {
364 goto fail;
365 } else if (count == 0 || rsrc_data_offset + count > info_length) {
366 ret = -EINVAL;
367 goto fail;
368 }
369
370 /* begin of resource data (consisting of one or more resources) */
371 offset = info_begin + rsrc_data_offset;
372
373 /* end of resource data (there is possibly a following resource map
374 * which will be ignored). */
375 info_end = offset + count;
376
377 /* read offsets (mish blocks) from one or more resources in resource data */
378 while (offset < info_end) {
379 /* size of following resource */
380 ret = read_uint32(bs, offset, &count);
381 if (ret < 0) {
382 goto fail;
383 } else if (count == 0 || count > info_end - offset) {
384 ret = -EINVAL;
385 goto fail;
386 }
387 offset += 4;
388
389 buffer = g_realloc(buffer, count);
390 ret = bdrv_pread(bs->file, offset, count, buffer, 0);
391 if (ret < 0) {
392 goto fail;
393 }
394
395 ret = dmg_read_mish_block(s, ds, buffer, count);
396 if (ret < 0) {
397 goto fail;
398 }
399 /* advance offset by size of resource */
400 offset += count;
401 }
402 ret = 0;
403
404 fail:
405 g_free(buffer);
406 return ret;
407 }
408
409 static int GRAPH_RDLOCK
410 dmg_read_plist_xml(BlockDriverState *bs, DmgHeaderState *ds,
411 uint64_t info_begin, uint64_t info_length)
412 {
413 BDRVDMGState *s = bs->opaque;
414 int ret;
415 uint8_t *buffer = NULL;
416 char *data_begin, *data_end;
417
418 /* Have at least some length to avoid NULL for g_malloc. Attempt to set a
419 * safe upper cap on the data length. A test sample had a XML length of
420 * about 1 MiB. */
421 if (info_length == 0 || info_length > 16 * 1024 * 1024) {
422 ret = -EINVAL;
423 goto fail;
424 }
425
426 buffer = g_malloc(info_length + 1);
427 buffer[info_length] = '\0';
428 ret = bdrv_pread(bs->file, info_begin, info_length, buffer, 0);
429 if (ret < 0) {
430 ret = -EINVAL;
431 goto fail;
432 }
433
434 /* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
435 * decode. The actual data element has 431 (0x1af) bytes which includes tabs
436 * and line feeds. */
437 data_end = (char *)buffer;
438 while ((data_begin = strstr(data_end, "<data>")) != NULL) {
439 guchar *mish;
440 gsize out_len = 0;
441
442 data_begin += 6;
443 data_end = strstr(data_begin, "</data>");
444 /* malformed XML? */
445 if (data_end == NULL) {
446 ret = -EINVAL;
447 goto fail;
448 }
449 *data_end++ = '\0';
450 mish = g_base64_decode(data_begin, &out_len);
451 ret = dmg_read_mish_block(s, ds, mish, (uint32_t)out_len);
452 g_free(mish);
453 if (ret < 0) {
454 goto fail;
455 }
456 }
457 ret = 0;
458
459 fail:
460 g_free(buffer);
461 return ret;
462 }
463
464 static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
465 Error **errp)
466 {
467 BDRVDMGState *s = bs->opaque;
468 DmgHeaderState ds;
469 uint64_t rsrc_fork_offset, rsrc_fork_length;
470 uint64_t plist_xml_offset, plist_xml_length;
471 int64_t offset;
472 int ret;
473
474 GLOBAL_STATE_CODE();
475
476 bdrv_graph_rdlock_main_loop();
477 ret = bdrv_apply_auto_read_only(bs, NULL, errp);
478 bdrv_graph_rdunlock_main_loop();
479 if (ret < 0) {
480 return ret;
481 }
482
483 ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
484 if (ret < 0) {
485 return ret;
486 }
487
488 GRAPH_RDLOCK_GUARD_MAINLOOP();
489
490 /*
491 * NB: if uncompress submodules are absent,
492 * ie block_module_load return value == 0, the function pointers
493 * dmg_uncompress_bz2 and dmg_uncompress_lzfse will be NULL.
494 */
495 if (block_module_load("dmg-bz2", errp) < 0) {
496 return -EINVAL;
497 }
498 if (block_module_load("dmg-lzfse", errp) < 0) {
499 return -EINVAL;
500 }
501
502 s->n_chunks = 0;
503 s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
504 /* used by dmg_read_mish_block to keep track of the current I/O position */
505 ds.data_fork_offset = 0;
506 ds.max_compressed_size = 1;
507 ds.max_sectors_per_chunk = 1;
508
509 /* locate the UDIF trailer */
510 offset = dmg_find_koly_offset(bs->file, errp);
511 if (offset < 0) {
512 ret = offset;
513 goto fail;
514 }
515
516 /* offset of data fork (DataForkOffset) */
517 ret = read_uint64(bs, offset + 0x18, &ds.data_fork_offset);
518 if (ret < 0) {
519 goto fail;
520 } else if (ds.data_fork_offset > offset) {
521 ret = -EINVAL;
522 goto fail;
523 }
524
525 /* offset of resource fork (RsrcForkOffset) */
526 ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
527 if (ret < 0) {
528 goto fail;
529 }
530 ret = read_uint64(bs, offset + 0x30, &rsrc_fork_length);
531 if (ret < 0) {
532 goto fail;
533 }
534 if (rsrc_fork_offset >= offset ||
535 rsrc_fork_length > offset - rsrc_fork_offset) {
536 ret = -EINVAL;
537 goto fail;
538 }
539 /* offset of property list (XMLOffset) */
540 ret = read_uint64(bs, offset + 0xd8, &plist_xml_offset);
541 if (ret < 0) {
542 goto fail;
543 }
544 ret = read_uint64(bs, offset + 0xe0, &plist_xml_length);
545 if (ret < 0) {
546 goto fail;
547 }
548 if (plist_xml_offset >= offset ||
549 plist_xml_length > offset - plist_xml_offset) {
550 ret = -EINVAL;
551 goto fail;
552 }
553 ret = read_uint64(bs, offset + 0x1ec, (uint64_t *)&bs->total_sectors);
554 if (ret < 0) {
555 goto fail;
556 }
557 if (bs->total_sectors < 0) {
558 ret = -EINVAL;
559 goto fail;
560 }
561 if (rsrc_fork_length != 0) {
562 ret = dmg_read_resource_fork(bs, &ds,
563 rsrc_fork_offset, rsrc_fork_length);
564 if (ret < 0) {
565 goto fail;
566 }
567 } else if (plist_xml_length != 0) {
568 ret = dmg_read_plist_xml(bs, &ds, plist_xml_offset, plist_xml_length);
569 if (ret < 0) {
570 goto fail;
571 }
572 } else {
573 ret = -EINVAL;
574 goto fail;
575 }
576
577 /* There must be at least one chunk */
578 if (s->n_chunks == 0) {
579 ret = -EINVAL;
580 goto fail;
581 }
582
583 /* initialize zlib engine */
584 s->compressed_chunk = qemu_try_blockalign(bs->file->bs,
585 ds.max_compressed_size + 1);
586 s->uncompressed_chunk = qemu_try_blockalign(bs->file->bs,
587 512 * ds.max_sectors_per_chunk);
588 if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
589 ret = -ENOMEM;
590 goto fail;
591 }
592
593 if (inflateInit(&s->zstream) != Z_OK) {
594 ret = -EINVAL;
595 goto fail;
596 }
597
598 s->current_chunk = s->n_chunks;
599
600 qemu_co_mutex_init(&s->lock);
601 return 0;
602
603 fail:
604 g_free(s->types);
605 g_free(s->offsets);
606 g_free(s->lengths);
607 g_free(s->sectors);
608 g_free(s->sectorcounts);
609 qemu_vfree(s->compressed_chunk);
610 qemu_vfree(s->uncompressed_chunk);
611 return ret;
612 }
613
614 static void dmg_refresh_limits(BlockDriverState *bs, Error **errp)
615 {
616 bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
617 }
618
619 static inline int is_sector_in_chunk(BDRVDMGState *s,
620 uint32_t chunk_num, uint64_t sector_num)
621 {
622 if (chunk_num >= s->n_chunks || s->sectors[chunk_num] > sector_num ||
623 s->sectors[chunk_num] + s->sectorcounts[chunk_num] <= sector_num) {
624 return 0;
625 } else {
626 return -1;
627 }
628 }
629
630 static inline uint32_t search_chunk(BDRVDMGState *s, uint64_t sector_num)
631 {
632 /* binary search */
633 uint32_t chunk1 = 0, chunk2 = s->n_chunks - 1, chunk3;
634 if (s->n_chunks == 0) {
635 goto err; /* should never happen */
636 }
637 while (chunk1 <= chunk2) {
638 chunk3 = (chunk1 + chunk2) / 2;
639 if (s->sectors[chunk3] > sector_num) {
640 if (chunk3 == 0) {
641 goto err;
642 }
643 chunk2 = chunk3 - 1;
644 } else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) {
645 return chunk3;
646 } else {
647 chunk1 = chunk3 + 1;
648 }
649 }
650 err:
651 return s->n_chunks; /* error */
652 }
653
654 static int coroutine_fn GRAPH_RDLOCK
655 dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
656 {
657 BDRVDMGState *s = bs->opaque;
658
659 if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
660 int ret;
661 uint32_t chunk = search_chunk(s, sector_num);
662
663 if (chunk >= s->n_chunks) {
664 return -1;
665 }
666
667 s->current_chunk = s->n_chunks;
668 switch (s->types[chunk]) { /* block entry type */
669 case UDZO: { /* zlib compressed */
670 /* we need to buffer, because only the chunk as whole can be
671 * inflated. */
672 ret = bdrv_co_pread(bs->file, s->offsets[chunk], s->lengths[chunk],
673 s->compressed_chunk, 0);
674 if (ret < 0) {
675 return -1;
676 }
677
678 s->zstream.next_in = s->compressed_chunk;
679 s->zstream.avail_in = s->lengths[chunk];
680 s->zstream.next_out = s->uncompressed_chunk;
681 s->zstream.avail_out = 512 * s->sectorcounts[chunk];
682 ret = inflateReset(&s->zstream);
683 if (ret != Z_OK) {
684 return -1;
685 }
686 ret = inflate(&s->zstream, Z_FINISH);
687 if (ret != Z_STREAM_END ||
688 s->zstream.total_out != 512 * s->sectorcounts[chunk]) {
689 return -1;
690 }
691 break; }
692 case UDBZ: /* bzip2 compressed */
693 if (!dmg_uncompress_bz2) {
694 break;
695 }
696 /* we need to buffer, because only the chunk as whole can be
697 * inflated. */
698 ret = bdrv_co_pread(bs->file, s->offsets[chunk], s->lengths[chunk],
699 s->compressed_chunk, 0);
700 if (ret < 0) {
701 return -1;
702 }
703
704 ret = dmg_uncompress_bz2((char *)s->compressed_chunk,
705 (unsigned int) s->lengths[chunk],
706 (char *)s->uncompressed_chunk,
707 (unsigned int)
708 (512 * s->sectorcounts[chunk]));
709 if (ret < 0) {
710 return ret;
711 }
712 break;
713 case ULFO:
714 if (!dmg_uncompress_lzfse) {
715 break;
716 }
717 /* we need to buffer, because only the chunk as whole can be
718 * inflated. */
719 ret = bdrv_co_pread(bs->file, s->offsets[chunk], s->lengths[chunk],
720 s->compressed_chunk, 0);
721 if (ret < 0) {
722 return -1;
723 }
724
725 ret = dmg_uncompress_lzfse((char *)s->compressed_chunk,
726 (unsigned int) s->lengths[chunk],
727 (char *)s->uncompressed_chunk,
728 (unsigned int)
729 (512 * s->sectorcounts[chunk]));
730 if (ret < 0) {
731 return ret;
732 }
733 break;
734 case UDRW: /* copy */
735 ret = bdrv_co_pread(bs->file, s->offsets[chunk], s->lengths[chunk],
736 s->uncompressed_chunk, 0);
737 if (ret < 0) {
738 return -1;
739 }
740
741 /*
742 * Zero the unread part of the last sector when chunk length is
743 * unaligned to avoid exposing uninitialized memory. Valid image
744 * files may never hit this case, but cover it to be safe.
745 */
746 if (s->lengths[chunk] & 511) {
747 size_t trailing_bytes = 512 - (s->lengths[chunk] & 511);
748 memset(s->uncompressed_chunk + s->lengths[chunk], 0, trailing_bytes);
749 }
750 break;
751 case UDZE: /* zeros */
752 case UDIG: /* ignore */
753 /* see dmg_read, it is treated specially. No buffer needs to be
754 * pre-filled, the zeroes can be set directly. */
755 break;
756 }
757 s->current_chunk = chunk;
758 }
759 return 0;
760 }
761
762 static int coroutine_fn GRAPH_RDLOCK
763 dmg_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
764 QEMUIOVector *qiov, BdrvRequestFlags flags)
765 {
766 BDRVDMGState *s = bs->opaque;
767 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
768 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
769 int ret, i;
770
771 assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE));
772 assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
773
774 qemu_co_mutex_lock(&s->lock);
775
776 for (i = 0; i < nb_sectors; i++) {
777 uint32_t sector_offset_in_chunk;
778 void *data;
779
780 if (dmg_read_chunk(bs, sector_num + i) != 0) {
781 ret = -EIO;
782 goto fail;
783 }
784 /* Special case: current chunk is all zeroes. Do not perform a memcpy as
785 * s->uncompressed_chunk may be too small to cover the large all-zeroes
786 * section. dmg_read_chunk is called to find s->current_chunk */
787 if (s->types[s->current_chunk] == UDZE
788 || s->types[s->current_chunk] == UDIG) { /* all zeroes block entry */
789 qemu_iovec_memset(qiov, i * 512, 0, 512);
790 continue;
791 }
792 sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
793 data = s->uncompressed_chunk + sector_offset_in_chunk * 512;
794 qemu_iovec_from_buf(qiov, i * 512, data, 512);
795 }
796
797 ret = 0;
798 fail:
799 qemu_co_mutex_unlock(&s->lock);
800 return ret;
801 }
802
803 static void dmg_close(BlockDriverState *bs)
804 {
805 BDRVDMGState *s = bs->opaque;
806
807 g_free(s->types);
808 g_free(s->offsets);
809 g_free(s->lengths);
810 g_free(s->sectors);
811 g_free(s->sectorcounts);
812 qemu_vfree(s->compressed_chunk);
813 qemu_vfree(s->uncompressed_chunk);
814
815 inflateEnd(&s->zstream);
816 }
817
818 static BlockDriver bdrv_dmg = {
819 .format_name = "dmg",
820 .instance_size = sizeof(BDRVDMGState),
821 .bdrv_probe = dmg_probe,
822 .bdrv_open = dmg_open,
823 .bdrv_refresh_limits = dmg_refresh_limits,
824 .bdrv_child_perm = bdrv_default_perms,
825 .bdrv_co_preadv = dmg_co_preadv,
826 .bdrv_close = dmg_close,
827 .is_format = true,
828 };
829
830 static void bdrv_dmg_init(void)
831 {
832 bdrv_register(&bdrv_dmg);
833 }
834
835 block_init(bdrv_dmg_init);