]> git.proxmox.com Git - mirror_qemu.git/blob - block/dmg.c
block/dmg: use SectorNumber from BLKX header
[mirror_qemu.git] / block / dmg.c
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-common.h"
25 #include "block/block_int.h"
26 #include "qemu/bswap.h"
27 #include "qemu/module.h"
28 #include <zlib.h>
29 #include <glib.h>
30
31 enum {
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
39 typedef struct BDRVDMGState {
40 CoMutex lock;
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;
56 uint8_t *compressed_chunk;
57 uint8_t *uncompressed_chunk;
58 z_stream zstream;
59 } BDRVDMGState;
60
61 static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
62 {
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 }
73 return 0;
74 }
75
76 static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
77 {
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;
88 }
89
90 static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
91 {
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;
102 }
103
104 static 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
109 static 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
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 */
117 static 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
145 static 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
185 /* used when building the sector table */
186 typedef struct DmgHeaderState {
187 /* used internally by dmg_read_mish_block to remember offsets of blocks
188 * across calls */
189 uint64_t data_fork_offset;
190 /* exported for dmg_open */
191 uint32_t max_compressed_size;
192 uint32_t max_sectors_per_chunk;
193 } DmgHeaderState;
194
195 static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,
196 uint8_t *buffer, uint32_t count)
197 {
198 uint32_t type, i;
199 int ret;
200 size_t new_size;
201 uint32_t chunk_count;
202 int64_t offset = 0;
203 uint64_t data_offset;
204 uint64_t in_offset = ds->data_fork_offset;
205 uint64_t out_offset;
206
207 type = buff_read_uint32(buffer, offset);
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
214 /* chunk offsets are relative to this sector number */
215 out_offset = buff_read_uint64(buffer, offset + 8);
216
217 /* location in data fork for (compressed) blob (in bytes) */
218 data_offset = buff_read_uint64(buffer, offset + 0x18);
219 in_offset += data_offset;
220
221 /* move to begin of chunk entries */
222 offset += 204;
223
224 chunk_count = (count - 204) / 40;
225 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
226 s->types = g_realloc(s->types, new_size / 2);
227 s->offsets = g_realloc(s->offsets, new_size);
228 s->lengths = g_realloc(s->lengths, new_size);
229 s->sectors = g_realloc(s->sectors, new_size);
230 s->sectorcounts = g_realloc(s->sectorcounts, new_size);
231
232 for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
233 s->types[i] = buff_read_uint32(buffer, offset);
234 offset += 4;
235 if (s->types[i] != 0x80000005 && s->types[i] != 1 &&
236 s->types[i] != 2) {
237 chunk_count--;
238 i--;
239 offset += 36;
240 continue;
241 }
242 offset += 4;
243
244 s->sectors[i] = buff_read_uint64(buffer, offset);
245 s->sectors[i] += out_offset;
246 offset += 8;
247
248 s->sectorcounts[i] = buff_read_uint64(buffer, offset);
249 offset += 8;
250
251 if (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 s->offsets[i] = buff_read_uint64(buffer, offset);
260 s->offsets[i] += in_offset;
261 offset += 8;
262
263 s->lengths[i] = buff_read_uint64(buffer, offset);
264 offset += 8;
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 }
277 s->n_chunks += chunk_count;
278 return 0;
279
280 fail:
281 return ret;
282 }
283
284 static int dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds,
285 uint64_t info_begin, uint64_t info_length)
286 {
287 BDRVDMGState *s = bs->opaque;
288 int ret;
289 uint32_t count, rsrc_data_offset;
290 uint8_t *buffer = NULL;
291 uint64_t info_end;
292 uint64_t offset;
293
294 /* read offset from begin of resource fork (info_begin) to resource data */
295 ret = read_uint32(bs, info_begin, &rsrc_data_offset);
296 if (ret < 0) {
297 goto fail;
298 } else if (rsrc_data_offset > info_length) {
299 ret = -EINVAL;
300 goto fail;
301 }
302
303 /* read length of resource data */
304 ret = read_uint32(bs, info_begin + 8, &count);
305 if (ret < 0) {
306 goto fail;
307 } else if (count == 0 || rsrc_data_offset + count > info_length) {
308 ret = -EINVAL;
309 goto fail;
310 }
311
312 /* begin of resource data (consisting of one or more resources) */
313 offset = info_begin + rsrc_data_offset;
314
315 /* end of resource data (there is possibly a following resource map
316 * which will be ignored). */
317 info_end = offset + count;
318
319 /* read offsets (mish blocks) from one or more resources in resource data */
320 while (offset < info_end) {
321 /* size of following resource */
322 ret = read_uint32(bs, offset, &count);
323 if (ret < 0) {
324 goto fail;
325 } else if (count == 0 || count > info_end - offset) {
326 ret = -EINVAL;
327 goto fail;
328 }
329 offset += 4;
330
331 buffer = g_realloc(buffer, count);
332 ret = bdrv_pread(bs->file, offset, buffer, count);
333 if (ret < 0) {
334 goto fail;
335 }
336
337 ret = dmg_read_mish_block(s, ds, buffer, count);
338 if (ret < 0) {
339 goto fail;
340 }
341 /* advance offset by size of resource */
342 offset += count;
343 }
344 ret = 0;
345
346 fail:
347 g_free(buffer);
348 return ret;
349 }
350
351 static int dmg_read_plist_xml(BlockDriverState *bs, DmgHeaderState *ds,
352 uint64_t info_begin, uint64_t info_length)
353 {
354 BDRVDMGState *s = bs->opaque;
355 int ret;
356 uint8_t *buffer = NULL;
357 char *data_begin, *data_end;
358
359 /* Have at least some length to avoid NULL for g_malloc. Attempt to set a
360 * safe upper cap on the data length. A test sample had a XML length of
361 * about 1 MiB. */
362 if (info_length == 0 || info_length > 16 * 1024 * 1024) {
363 ret = -EINVAL;
364 goto fail;
365 }
366
367 buffer = g_malloc(info_length + 1);
368 buffer[info_length] = '\0';
369 ret = bdrv_pread(bs->file, info_begin, buffer, info_length);
370 if (ret != info_length) {
371 ret = -EINVAL;
372 goto fail;
373 }
374
375 /* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
376 * decode. The actual data element has 431 (0x1af) bytes which includes tabs
377 * and line feeds. */
378 data_end = (char *)buffer;
379 while ((data_begin = strstr(data_end, "<data>")) != NULL) {
380 guchar *mish;
381 gsize out_len = 0;
382
383 data_begin += 6;
384 data_end = strstr(data_begin, "</data>");
385 /* malformed XML? */
386 if (data_end == NULL) {
387 ret = -EINVAL;
388 goto fail;
389 }
390 *data_end++ = '\0';
391 mish = g_base64_decode(data_begin, &out_len);
392 ret = dmg_read_mish_block(s, ds, mish, (uint32_t)out_len);
393 g_free(mish);
394 if (ret < 0) {
395 goto fail;
396 }
397 }
398 ret = 0;
399
400 fail:
401 g_free(buffer);
402 return ret;
403 }
404
405 static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
406 Error **errp)
407 {
408 BDRVDMGState *s = bs->opaque;
409 DmgHeaderState ds;
410 uint64_t rsrc_fork_offset, rsrc_fork_length;
411 uint64_t plist_xml_offset, plist_xml_length;
412 int64_t offset;
413 int ret;
414
415 bs->read_only = 1;
416 s->n_chunks = 0;
417 s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
418 /* used by dmg_read_mish_block to keep track of the current I/O position */
419 ds.data_fork_offset = 0;
420 ds.max_compressed_size = 1;
421 ds.max_sectors_per_chunk = 1;
422
423 /* locate the UDIF trailer */
424 offset = dmg_find_koly_offset(bs->file, errp);
425 if (offset < 0) {
426 ret = offset;
427 goto fail;
428 }
429
430 /* offset of data fork (DataForkOffset) */
431 ret = read_uint64(bs, offset + 0x18, &ds.data_fork_offset);
432 if (ret < 0) {
433 goto fail;
434 } else if (ds.data_fork_offset > offset) {
435 ret = -EINVAL;
436 goto fail;
437 }
438
439 /* offset of resource fork (RsrcForkOffset) */
440 ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
441 if (ret < 0) {
442 goto fail;
443 }
444 ret = read_uint64(bs, offset + 0x30, &rsrc_fork_length);
445 if (ret < 0) {
446 goto fail;
447 }
448 if (rsrc_fork_offset >= offset ||
449 rsrc_fork_length > offset - rsrc_fork_offset) {
450 ret = -EINVAL;
451 goto fail;
452 }
453 /* offset of property list (XMLOffset) */
454 ret = read_uint64(bs, offset + 0xd8, &plist_xml_offset);
455 if (ret < 0) {
456 goto fail;
457 }
458 ret = read_uint64(bs, offset + 0xe0, &plist_xml_length);
459 if (ret < 0) {
460 goto fail;
461 }
462 if (plist_xml_offset >= offset ||
463 plist_xml_length > offset - plist_xml_offset) {
464 ret = -EINVAL;
465 goto fail;
466 }
467 ret = read_uint64(bs, offset + 0x1ec, (uint64_t *)&bs->total_sectors);
468 if (ret < 0) {
469 goto fail;
470 }
471 if (bs->total_sectors < 0) {
472 ret = -EINVAL;
473 goto fail;
474 }
475 if (rsrc_fork_length != 0) {
476 ret = dmg_read_resource_fork(bs, &ds,
477 rsrc_fork_offset, rsrc_fork_length);
478 if (ret < 0) {
479 goto fail;
480 }
481 } else if (plist_xml_length != 0) {
482 ret = dmg_read_plist_xml(bs, &ds, plist_xml_offset, plist_xml_length);
483 if (ret < 0) {
484 goto fail;
485 }
486 } else {
487 ret = -EINVAL;
488 goto fail;
489 }
490
491 /* initialize zlib engine */
492 s->compressed_chunk = qemu_try_blockalign(bs->file,
493 ds.max_compressed_size + 1);
494 s->uncompressed_chunk = qemu_try_blockalign(bs->file,
495 512 * ds.max_sectors_per_chunk);
496 if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
497 ret = -ENOMEM;
498 goto fail;
499 }
500
501 if (inflateInit(&s->zstream) != Z_OK) {
502 ret = -EINVAL;
503 goto fail;
504 }
505
506 s->current_chunk = s->n_chunks;
507
508 qemu_co_mutex_init(&s->lock);
509 return 0;
510
511 fail:
512 g_free(s->types);
513 g_free(s->offsets);
514 g_free(s->lengths);
515 g_free(s->sectors);
516 g_free(s->sectorcounts);
517 qemu_vfree(s->compressed_chunk);
518 qemu_vfree(s->uncompressed_chunk);
519 return ret;
520 }
521
522 static inline int is_sector_in_chunk(BDRVDMGState* s,
523 uint32_t chunk_num, uint64_t sector_num)
524 {
525 if (chunk_num >= s->n_chunks || s->sectors[chunk_num] > sector_num ||
526 s->sectors[chunk_num] + s->sectorcounts[chunk_num] <= sector_num) {
527 return 0;
528 } else {
529 return -1;
530 }
531 }
532
533 static inline uint32_t search_chunk(BDRVDMGState *s, uint64_t sector_num)
534 {
535 /* binary search */
536 uint32_t chunk1 = 0, chunk2 = s->n_chunks, chunk3;
537 while (chunk1 != chunk2) {
538 chunk3 = (chunk1 + chunk2) / 2;
539 if (s->sectors[chunk3] > sector_num) {
540 chunk2 = chunk3;
541 } else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) {
542 return chunk3;
543 } else {
544 chunk1 = chunk3;
545 }
546 }
547 return s->n_chunks; /* error */
548 }
549
550 static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
551 {
552 BDRVDMGState *s = bs->opaque;
553
554 if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
555 int ret;
556 uint32_t chunk = search_chunk(s, sector_num);
557
558 if (chunk >= s->n_chunks) {
559 return -1;
560 }
561
562 s->current_chunk = s->n_chunks;
563 switch (s->types[chunk]) {
564 case 0x80000005: { /* zlib compressed */
565 /* we need to buffer, because only the chunk as whole can be
566 * inflated. */
567 ret = bdrv_pread(bs->file, s->offsets[chunk],
568 s->compressed_chunk, s->lengths[chunk]);
569 if (ret != s->lengths[chunk]) {
570 return -1;
571 }
572
573 s->zstream.next_in = s->compressed_chunk;
574 s->zstream.avail_in = s->lengths[chunk];
575 s->zstream.next_out = s->uncompressed_chunk;
576 s->zstream.avail_out = 512 * s->sectorcounts[chunk];
577 ret = inflateReset(&s->zstream);
578 if (ret != Z_OK) {
579 return -1;
580 }
581 ret = inflate(&s->zstream, Z_FINISH);
582 if (ret != Z_STREAM_END ||
583 s->zstream.total_out != 512 * s->sectorcounts[chunk]) {
584 return -1;
585 }
586 break; }
587 case 1: /* copy */
588 ret = bdrv_pread(bs->file, s->offsets[chunk],
589 s->uncompressed_chunk, s->lengths[chunk]);
590 if (ret != s->lengths[chunk]) {
591 return -1;
592 }
593 break;
594 case 2: /* zero */
595 memset(s->uncompressed_chunk, 0, 512 * s->sectorcounts[chunk]);
596 break;
597 }
598 s->current_chunk = chunk;
599 }
600 return 0;
601 }
602
603 static int dmg_read(BlockDriverState *bs, int64_t sector_num,
604 uint8_t *buf, int nb_sectors)
605 {
606 BDRVDMGState *s = bs->opaque;
607 int i;
608
609 for (i = 0; i < nb_sectors; i++) {
610 uint32_t sector_offset_in_chunk;
611 if (dmg_read_chunk(bs, sector_num + i) != 0) {
612 return -1;
613 }
614 sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
615 memcpy(buf + i * 512,
616 s->uncompressed_chunk + sector_offset_in_chunk * 512, 512);
617 }
618 return 0;
619 }
620
621 static coroutine_fn int dmg_co_read(BlockDriverState *bs, int64_t sector_num,
622 uint8_t *buf, int nb_sectors)
623 {
624 int ret;
625 BDRVDMGState *s = bs->opaque;
626 qemu_co_mutex_lock(&s->lock);
627 ret = dmg_read(bs, sector_num, buf, nb_sectors);
628 qemu_co_mutex_unlock(&s->lock);
629 return ret;
630 }
631
632 static void dmg_close(BlockDriverState *bs)
633 {
634 BDRVDMGState *s = bs->opaque;
635
636 g_free(s->types);
637 g_free(s->offsets);
638 g_free(s->lengths);
639 g_free(s->sectors);
640 g_free(s->sectorcounts);
641 qemu_vfree(s->compressed_chunk);
642 qemu_vfree(s->uncompressed_chunk);
643
644 inflateEnd(&s->zstream);
645 }
646
647 static BlockDriver bdrv_dmg = {
648 .format_name = "dmg",
649 .instance_size = sizeof(BDRVDMGState),
650 .bdrv_probe = dmg_probe,
651 .bdrv_open = dmg_open,
652 .bdrv_read = dmg_co_read,
653 .bdrv_close = dmg_close,
654 };
655
656 static void bdrv_dmg_init(void)
657 {
658 bdrv_register(&bdrv_dmg);
659 }
660
661 block_init(bdrv_dmg_init);