1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "qemu/osdep.h"
25#include "qapi/error.h"
26#include "qemu-common.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 "dmg.h"
32
33int (*dmg_uncompress_bz2)(char *next_in, unsigned int avail_in,
34 char *next_out, unsigned int avail_out);
35
36enum {
37
38
39
40 DMG_LENGTHS_MAX = 64 * 1024 * 1024,
41 DMG_SECTORCOUNTS_MAX = DMG_LENGTHS_MAX / 512,
42};
43
44static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
45{
46 int len;
47
48 if (!filename) {
49 return 0;
50 }
51
52 len = strlen(filename);
53 if (len > 4 && !strcmp(filename + len - 4, ".dmg")) {
54 return 2;
55 }
56 return 0;
57}
58
59static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
60{
61 uint64_t buffer;
62 int ret;
63
64 ret = bdrv_pread(bs->file, offset, &buffer, 8);
65 if (ret < 0) {
66 return ret;
67 }
68
69 *result = be64_to_cpu(buffer);
70 return 0;
71}
72
73static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
74{
75 uint32_t buffer;
76 int ret;
77
78 ret = bdrv_pread(bs->file, offset, &buffer, 4);
79 if (ret < 0) {
80 return ret;
81 }
82
83 *result = be32_to_cpu(buffer);
84 return 0;
85}
86
87static inline uint64_t buff_read_uint64(const uint8_t *buffer, int64_t offset)
88{
89 return be64_to_cpu(*(uint64_t *)&buffer[offset]);
90}
91
92static inline uint32_t buff_read_uint32(const uint8_t *buffer, int64_t offset)
93{
94 return be32_to_cpu(*(uint32_t *)&buffer[offset]);
95}
96
97
98
99
100static void update_max_chunk_size(BDRVDMGState *s, uint32_t chunk,
101 uint32_t *max_compressed_size,
102 uint32_t *max_sectors_per_chunk)
103{
104 uint32_t compressed_size = 0;
105 uint32_t uncompressed_sectors = 0;
106
107 switch (s->types[chunk]) {
108 case 0x80000005:
109 case 0x80000006:
110 compressed_size = s->lengths[chunk];
111 uncompressed_sectors = s->sectorcounts[chunk];
112 break;
113 case 1:
114 uncompressed_sectors = DIV_ROUND_UP(s->lengths[chunk], 512);
115 break;
116 case 2:
117
118
119
120 break;
121 }
122
123 if (compressed_size > *max_compressed_size) {
124 *max_compressed_size = compressed_size;
125 }
126 if (uncompressed_sectors > *max_sectors_per_chunk) {
127 *max_sectors_per_chunk = uncompressed_sectors;
128 }
129}
130
131static int64_t dmg_find_koly_offset(BdrvChild *file, Error **errp)
132{
133 BlockDriverState *file_bs = file->bs;
134 int64_t length;
135 int64_t offset = 0;
136 uint8_t buffer[515];
137 int i, ret;
138
139
140
141
142
143
144 length = bdrv_getlength(file_bs);
145 if (length < 0) {
146 error_setg_errno(errp, -length,
147 "Failed to get file size while reading UDIF trailer");
148 return length;
149 } else if (length < 512) {
150 error_setg(errp, "dmg file must be at least 512 bytes long");
151 return -EINVAL;
152 }
153 if (length > 511 + 512) {
154 offset = length - 511 - 512;
155 }
156 length = length < 515 ? length : 515;
157 ret = bdrv_pread(file, offset, buffer, length);
158 if (ret < 0) {
159 error_setg_errno(errp, -ret, "Failed while reading UDIF trailer");
160 return ret;
161 }
162 for (i = 0; i < length - 3; i++) {
163 if (buffer[i] == 'k' && buffer[i+1] == 'o' &&
164 buffer[i+2] == 'l' && buffer[i+3] == 'y') {
165 return offset + i;
166 }
167 }
168 error_setg(errp, "Could not locate UDIF trailer in dmg file");
169 return -EINVAL;
170}
171
172
173typedef struct DmgHeaderState {
174
175
176 uint64_t data_fork_offset;
177
178 uint32_t max_compressed_size;
179 uint32_t max_sectors_per_chunk;
180} DmgHeaderState;
181
182static bool dmg_is_known_block_type(uint32_t entry_type)
183{
184 switch (entry_type) {
185 case 0x00000001:
186 case 0x00000002:
187 case 0x80000005:
188 return true;
189 case 0x80000006:
190 return !!dmg_uncompress_bz2;
191 default:
192 return false;
193 }
194}
195
196static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,
197 uint8_t *buffer, uint32_t count)
198{
199 uint32_t type, i;
200 int ret;
201 size_t new_size;
202 uint32_t chunk_count;
203 int64_t offset = 0;
204 uint64_t data_offset;
205 uint64_t in_offset = ds->data_fork_offset;
206 uint64_t out_offset;
207
208 type = buff_read_uint32(buffer, offset);
209
210 if (type != 0x6d697368 || count < 244) {
211
212 return 0;
213 }
214
215
216 out_offset = buff_read_uint64(buffer, offset + 8);
217
218
219 data_offset = buff_read_uint64(buffer, offset + 0x18);
220 in_offset += data_offset;
221
222
223 offset += 204;
224
225 chunk_count = (count - 204) / 40;
226 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
227 s->types = g_realloc(s->types, new_size / 2);
228 s->offsets = g_realloc(s->offsets, new_size);
229 s->lengths = g_realloc(s->lengths, new_size);
230 s->sectors = g_realloc(s->sectors, new_size);
231 s->sectorcounts = g_realloc(s->sectorcounts, new_size);
232
233 for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
234 s->types[i] = buff_read_uint32(buffer, offset);
235 if (!dmg_is_known_block_type(s->types[i])) {
236 chunk_count--;
237 i--;
238 offset += 40;
239 continue;
240 }
241
242
243 s->sectors[i] = buff_read_uint64(buffer, offset + 8);
244 s->sectors[i] += out_offset;
245
246
247 s->sectorcounts[i] = buff_read_uint64(buffer, offset + 0x10);
248
249
250
251 if (s->types[i] != 2 && s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
252 error_report("sector count %" PRIu64 " for chunk %" PRIu32
253 " is larger than max (%u)",
254 s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
255 ret = -EINVAL;
256 goto fail;
257 }
258
259
260 s->offsets[i] = buff_read_uint64(buffer, offset + 0x18);
261 s->offsets[i] += in_offset;
262
263
264 s->lengths[i] = buff_read_uint64(buffer, offset + 0x20);
265
266 if (s->lengths[i] > DMG_LENGTHS_MAX) {
267 error_report("length %" PRIu64 " for chunk %" PRIu32
268 " is larger than max (%u)",
269 s->lengths[i], i, DMG_LENGTHS_MAX);
270 ret = -EINVAL;
271 goto fail;
272 }
273
274 update_max_chunk_size(s, i, &ds->max_compressed_size,
275 &ds->max_sectors_per_chunk);
276 offset += 40;
277 }
278 s->n_chunks += chunk_count;
279 return 0;
280
281fail:
282 return ret;
283}
284
285static int dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds,
286 uint64_t info_begin, uint64_t info_length)
287{
288 BDRVDMGState *s = bs->opaque;
289 int ret;
290 uint32_t count, rsrc_data_offset;
291 uint8_t *buffer = NULL;
292 uint64_t info_end;
293 uint64_t offset;
294
295
296 ret = read_uint32(bs, info_begin, &rsrc_data_offset);
297 if (ret < 0) {
298 goto fail;
299 } else if (rsrc_data_offset > info_length) {
300 ret = -EINVAL;
301 goto fail;
302 }
303
304
305 ret = read_uint32(bs, info_begin + 8, &count);
306 if (ret < 0) {
307 goto fail;
308 } else if (count == 0 || rsrc_data_offset + count > info_length) {
309 ret = -EINVAL;
310 goto fail;
311 }
312
313
314 offset = info_begin + rsrc_data_offset;
315
316
317
318 info_end = offset + count;
319
320
321 while (offset < info_end) {
322
323 ret = read_uint32(bs, offset, &count);
324 if (ret < 0) {
325 goto fail;
326 } else if (count == 0 || count > info_end - offset) {
327 ret = -EINVAL;
328 goto fail;
329 }
330 offset += 4;
331
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);
339 if (ret < 0) {
340 goto fail;
341 }
342
343 offset += count;
344 }
345 ret = 0;
346
347fail:
348 g_free(buffer);
349 return ret;
350}
351
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
361
362
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
377
378
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
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
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;
412 uint64_t plist_xml_offset, plist_xml_length;
413 int64_t offset;
414 int ret;
415
416 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
417 false, errp);
418 if (!bs->file) {
419 return -EINVAL;
420 }
421
422 if (!bdrv_is_read_only(bs)) {
423 error_report("Opening dmg images without an explicit read-only=on "
424 "option is deprecated. Future versions will refuse to "
425 "open the image instead of automatically marking the "
426 "image read-only.");
427 ret = bdrv_set_read_only(bs, true, errp);
428 if (ret < 0) {
429 return ret;
430 }
431 }
432
433 block_module_load_one("dmg-bz2");
434
435 s->n_chunks = 0;
436 s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
437
438 ds.data_fork_offset = 0;
439 ds.max_compressed_size = 1;
440 ds.max_sectors_per_chunk = 1;
441
442
443 offset = dmg_find_koly_offset(bs->file, errp);
444 if (offset < 0) {
445 ret = offset;
446 goto fail;
447 }
448
449
450 ret = read_uint64(bs, offset + 0x18, &ds.data_fork_offset);
451 if (ret < 0) {
452 goto fail;
453 } else if (ds.data_fork_offset > offset) {
454 ret = -EINVAL;
455 goto fail;
456 }
457
458
459 ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
460 if (ret < 0) {
461 goto fail;
462 }
463 ret = read_uint64(bs, offset + 0x30, &rsrc_fork_length);
464 if (ret < 0) {
465 goto fail;
466 }
467 if (rsrc_fork_offset >= offset ||
468 rsrc_fork_length > offset - rsrc_fork_offset) {
469 ret = -EINVAL;
470 goto fail;
471 }
472
473 ret = read_uint64(bs, offset + 0xd8, &plist_xml_offset);
474 if (ret < 0) {
475 goto fail;
476 }
477 ret = read_uint64(bs, offset + 0xe0, &plist_xml_length);
478 if (ret < 0) {
479 goto fail;
480 }
481 if (plist_xml_offset >= offset ||
482 plist_xml_length > offset - plist_xml_offset) {
483 ret = -EINVAL;
484 goto fail;
485 }
486 ret = read_uint64(bs, offset + 0x1ec, (uint64_t *)&bs->total_sectors);
487 if (ret < 0) {
488 goto fail;
489 }
490 if (bs->total_sectors < 0) {
491 ret = -EINVAL;
492 goto fail;
493 }
494 if (rsrc_fork_length != 0) {
495 ret = dmg_read_resource_fork(bs, &ds,
496 rsrc_fork_offset, rsrc_fork_length);
497 if (ret < 0) {
498 goto fail;
499 }
500 } else if (plist_xml_length != 0) {
501 ret = dmg_read_plist_xml(bs, &ds, plist_xml_offset, plist_xml_length);
502 if (ret < 0) {
503 goto fail;
504 }
505 } else {
506 ret = -EINVAL;
507 goto fail;
508 }
509
510
511 s->compressed_chunk = qemu_try_blockalign(bs->file->bs,
512 ds.max_compressed_size + 1);
513 s->uncompressed_chunk = qemu_try_blockalign(bs->file->bs,
514 512 * ds.max_sectors_per_chunk);
515 if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
516 ret = -ENOMEM;
517 goto fail;
518 }
519
520 if (inflateInit(&s->zstream) != Z_OK) {
521 ret = -EINVAL;
522 goto fail;
523 }
524
525 s->current_chunk = s->n_chunks;
526
527 qemu_co_mutex_init(&s->lock);
528 return 0;
529
530fail:
531 g_free(s->types);
532 g_free(s->offsets);
533 g_free(s->lengths);
534 g_free(s->sectors);
535 g_free(s->sectorcounts);
536 qemu_vfree(s->compressed_chunk);
537 qemu_vfree(s->uncompressed_chunk);
538 return ret;
539}
540
541static void dmg_refresh_limits(BlockDriverState *bs, Error **errp)
542{
543 bs->bl.request_alignment = BDRV_SECTOR_SIZE;
544}
545
546static inline int is_sector_in_chunk(BDRVDMGState* s,
547 uint32_t chunk_num, uint64_t sector_num)
548{
549 if (chunk_num >= s->n_chunks || s->sectors[chunk_num] > sector_num ||
550 s->sectors[chunk_num] + s->sectorcounts[chunk_num] <= sector_num) {
551 return 0;
552 } else {
553 return -1;
554 }
555}
556
557static inline uint32_t search_chunk(BDRVDMGState *s, uint64_t sector_num)
558{
559
560 uint32_t chunk1 = 0, chunk2 = s->n_chunks, chunk3;
561 while (chunk1 != chunk2) {
562 chunk3 = (chunk1 + chunk2) / 2;
563 if (s->sectors[chunk3] > sector_num) {
564 chunk2 = chunk3;
565 } else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) {
566 return chunk3;
567 } else {
568 chunk1 = chunk3;
569 }
570 }
571 return s->n_chunks;
572}
573
574static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
575{
576 BDRVDMGState *s = bs->opaque;
577
578 if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
579 int ret;
580 uint32_t chunk = search_chunk(s, sector_num);
581
582 if (chunk >= s->n_chunks) {
583 return -1;
584 }
585
586 s->current_chunk = s->n_chunks;
587 switch (s->types[chunk]) {
588 case 0x80000005: {
589
590
591 ret = bdrv_pread(bs->file, s->offsets[chunk],
592 s->compressed_chunk, s->lengths[chunk]);
593 if (ret != s->lengths[chunk]) {
594 return -1;
595 }
596
597 s->zstream.next_in = s->compressed_chunk;
598 s->zstream.avail_in = s->lengths[chunk];
599 s->zstream.next_out = s->uncompressed_chunk;
600 s->zstream.avail_out = 512 * s->sectorcounts[chunk];
601 ret = inflateReset(&s->zstream);
602 if (ret != Z_OK) {
603 return -1;
604 }
605 ret = inflate(&s->zstream, Z_FINISH);
606 if (ret != Z_STREAM_END ||
607 s->zstream.total_out != 512 * s->sectorcounts[chunk]) {
608 return -1;
609 }
610 break; }
611 case 0x80000006:
612 if (!dmg_uncompress_bz2) {
613 break;
614 }
615
616
617 ret = bdrv_pread(bs->file, s->offsets[chunk],
618 s->compressed_chunk, s->lengths[chunk]);
619 if (ret != s->lengths[chunk]) {
620 return -1;
621 }
622
623 ret = dmg_uncompress_bz2((char *)s->compressed_chunk,
624 (unsigned int) s->lengths[chunk],
625 (char *)s->uncompressed_chunk,
626 (unsigned int)
627 (512 * s->sectorcounts[chunk]));
628 if (ret < 0) {
629 return ret;
630 }
631 break;
632 case 1:
633 ret = bdrv_pread(bs->file, s->offsets[chunk],
634 s->uncompressed_chunk, s->lengths[chunk]);
635 if (ret != s->lengths[chunk]) {
636 return -1;
637 }
638 break;
639 case 2:
640
641
642 break;
643 }
644 s->current_chunk = chunk;
645 }
646 return 0;
647}
648
649static int coroutine_fn
650dmg_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
651 QEMUIOVector *qiov, int flags)
652{
653 BDRVDMGState *s = bs->opaque;
654 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
655 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
656 int ret, i;
657
658 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
659 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
660
661 qemu_co_mutex_lock(&s->lock);
662
663 for (i = 0; i < nb_sectors; i++) {
664 uint32_t sector_offset_in_chunk;
665 void *data;
666
667 if (dmg_read_chunk(bs, sector_num + i) != 0) {
668 ret = -EIO;
669 goto fail;
670 }
671
672
673
674 if (s->types[s->current_chunk] == 2) {
675 qemu_iovec_memset(qiov, i * 512, 0, 512);
676 continue;
677 }
678 sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
679 data = s->uncompressed_chunk + sector_offset_in_chunk * 512;
680 qemu_iovec_from_buf(qiov, i * 512, data, 512);
681 }
682
683 ret = 0;
684fail:
685 qemu_co_mutex_unlock(&s->lock);
686 return ret;
687}
688
689static void dmg_close(BlockDriverState *bs)
690{
691 BDRVDMGState *s = bs->opaque;
692
693 g_free(s->types);
694 g_free(s->offsets);
695 g_free(s->lengths);
696 g_free(s->sectors);
697 g_free(s->sectorcounts);
698 qemu_vfree(s->compressed_chunk);
699 qemu_vfree(s->uncompressed_chunk);
700
701 inflateEnd(&s->zstream);
702}
703
704static BlockDriver bdrv_dmg = {
705 .format_name = "dmg",
706 .instance_size = sizeof(BDRVDMGState),
707 .bdrv_probe = dmg_probe,
708 .bdrv_open = dmg_open,
709 .bdrv_refresh_limits = dmg_refresh_limits,
710 .bdrv_child_perm = bdrv_format_default_perms,
711 .bdrv_co_preadv = dmg_co_preadv,
712 .bdrv_close = dmg_close,
713};
714
715static void bdrv_dmg_init(void)
716{
717 bdrv_register(&bdrv_dmg);
718}
719
720block_init(bdrv_dmg_init);
721