]> git.proxmox.com Git - mirror_qemu.git/blob - block/dmg.c
block/dmg: properly detect the UDIF trailer
[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
30 enum {
31 /* Limit chunk sizes to prevent unreasonable amounts of memory being used
32 * or truncating when converting to 32-bit types
33 */
34 DMG_LENGTHS_MAX = 64 * 1024 * 1024, /* 64 MB */
35 DMG_SECTORCOUNTS_MAX = DMG_LENGTHS_MAX / 512,
36 };
37
38 typedef struct BDRVDMGState {
39 CoMutex lock;
40 /* each chunk contains a certain number of sectors,
41 * offsets[i] is the offset in the .dmg file,
42 * lengths[i] is the length of the compressed chunk,
43 * sectors[i] is the sector beginning at offsets[i],
44 * sectorcounts[i] is the number of sectors in that chunk,
45 * the sectors array is ordered
46 * 0<=i<n_chunks */
47
48 uint32_t n_chunks;
49 uint32_t* types;
50 uint64_t* offsets;
51 uint64_t* lengths;
52 uint64_t* sectors;
53 uint64_t* sectorcounts;
54 uint32_t current_chunk;
55 uint8_t *compressed_chunk;
56 uint8_t *uncompressed_chunk;
57 z_stream zstream;
58 } BDRVDMGState;
59
60 static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
61 {
62 int len;
63
64 if (!filename) {
65 return 0;
66 }
67
68 len = strlen(filename);
69 if (len > 4 && !strcmp(filename + len - 4, ".dmg")) {
70 return 2;
71 }
72 return 0;
73 }
74
75 static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
76 {
77 uint64_t buffer;
78 int ret;
79
80 ret = bdrv_pread(bs->file, offset, &buffer, 8);
81 if (ret < 0) {
82 return ret;
83 }
84
85 *result = be64_to_cpu(buffer);
86 return 0;
87 }
88
89 static int 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, &buffer, 4);
95 if (ret < 0) {
96 return ret;
97 }
98
99 *result = be32_to_cpu(buffer);
100 return 0;
101 }
102
103 /* Increase max chunk sizes, if necessary. This function is used to calculate
104 * the buffer sizes needed for compressed/uncompressed chunk I/O.
105 */
106 static void update_max_chunk_size(BDRVDMGState *s, uint32_t chunk,
107 uint32_t *max_compressed_size,
108 uint32_t *max_sectors_per_chunk)
109 {
110 uint32_t compressed_size = 0;
111 uint32_t uncompressed_sectors = 0;
112
113 switch (s->types[chunk]) {
114 case 0x80000005: /* zlib compressed */
115 compressed_size = s->lengths[chunk];
116 uncompressed_sectors = s->sectorcounts[chunk];
117 break;
118 case 1: /* copy */
119 uncompressed_sectors = (s->lengths[chunk] + 511) / 512;
120 break;
121 case 2: /* zero */
122 uncompressed_sectors = s->sectorcounts[chunk];
123 break;
124 }
125
126 if (compressed_size > *max_compressed_size) {
127 *max_compressed_size = compressed_size;
128 }
129 if (uncompressed_sectors > *max_sectors_per_chunk) {
130 *max_sectors_per_chunk = uncompressed_sectors;
131 }
132 }
133
134 static int64_t dmg_find_koly_offset(BlockDriverState *file_bs, Error **errp)
135 {
136 int64_t length;
137 int64_t offset = 0;
138 uint8_t buffer[515];
139 int i, ret;
140
141 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
142 * dmg images can have odd sizes, try to look for the "koly" magic which
143 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
144 * in the last 511 bytes of the second-last sector or the first 4 bytes of
145 * the last sector (search space: 515 bytes) */
146 length = bdrv_getlength(file_bs);
147 if (length < 0) {
148 error_setg_errno(errp, -length,
149 "Failed to get file size while reading UDIF trailer");
150 return length;
151 } else if (length < 512) {
152 error_setg(errp, "dmg file must be at least 512 bytes long");
153 return -EINVAL;
154 }
155 if (length > 511 + 512) {
156 offset = length - 511 - 512;
157 }
158 length = length < 515 ? length : 515;
159 ret = bdrv_pread(file_bs, offset, buffer, length);
160 if (ret < 0) {
161 error_setg_errno(errp, -ret, "Failed while reading UDIF trailer");
162 return ret;
163 }
164 for (i = 0; i < length - 3; i++) {
165 if (buffer[i] == 'k' && buffer[i+1] == 'o' &&
166 buffer[i+2] == 'l' && buffer[i+3] == 'y') {
167 return offset + i;
168 }
169 }
170 error_setg(errp, "Could not locate UDIF trailer in dmg file");
171 return -EINVAL;
172 }
173
174 static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
175 Error **errp)
176 {
177 BDRVDMGState *s = bs->opaque;
178 uint64_t info_begin, info_end, last_in_offset, last_out_offset;
179 uint32_t count, tmp;
180 uint32_t max_compressed_size = 1, max_sectors_per_chunk = 1, i;
181 int64_t offset;
182 int ret;
183
184 bs->read_only = 1;
185 s->n_chunks = 0;
186 s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
187
188 /* locate the UDIF trailer */
189 offset = dmg_find_koly_offset(bs->file, errp);
190 if (offset < 0) {
191 ret = offset;
192 goto fail;
193 }
194
195 ret = read_uint64(bs, offset + 0x28, &info_begin);
196 if (ret < 0) {
197 goto fail;
198 } else if (info_begin == 0) {
199 ret = -EINVAL;
200 goto fail;
201 }
202
203 ret = read_uint32(bs, info_begin, &tmp);
204 if (ret < 0) {
205 goto fail;
206 } else if (tmp != 0x100) {
207 ret = -EINVAL;
208 goto fail;
209 }
210
211 ret = read_uint32(bs, info_begin + 4, &count);
212 if (ret < 0) {
213 goto fail;
214 } else if (count == 0) {
215 ret = -EINVAL;
216 goto fail;
217 }
218 info_end = info_begin + count;
219
220 offset = info_begin + 0x100;
221
222 /* read offsets */
223 last_in_offset = last_out_offset = 0;
224 while (offset < info_end) {
225 uint32_t type;
226
227 ret = read_uint32(bs, offset, &count);
228 if (ret < 0) {
229 goto fail;
230 } else if (count == 0) {
231 ret = -EINVAL;
232 goto fail;
233 }
234 offset += 4;
235
236 ret = read_uint32(bs, offset, &type);
237 if (ret < 0) {
238 goto fail;
239 }
240
241 if (type == 0x6d697368 && count >= 244) {
242 size_t new_size;
243 uint32_t chunk_count;
244
245 offset += 4;
246 offset += 200;
247
248 chunk_count = (count - 204) / 40;
249 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
250 s->types = g_realloc(s->types, new_size / 2);
251 s->offsets = g_realloc(s->offsets, new_size);
252 s->lengths = g_realloc(s->lengths, new_size);
253 s->sectors = g_realloc(s->sectors, new_size);
254 s->sectorcounts = g_realloc(s->sectorcounts, new_size);
255
256 for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
257 ret = read_uint32(bs, offset, &s->types[i]);
258 if (ret < 0) {
259 goto fail;
260 }
261 offset += 4;
262 if (s->types[i] != 0x80000005 && s->types[i] != 1 &&
263 s->types[i] != 2) {
264 if (s->types[i] == 0xffffffff && i > 0) {
265 last_in_offset = s->offsets[i - 1] + s->lengths[i - 1];
266 last_out_offset = s->sectors[i - 1] +
267 s->sectorcounts[i - 1];
268 }
269 chunk_count--;
270 i--;
271 offset += 36;
272 continue;
273 }
274 offset += 4;
275
276 ret = read_uint64(bs, offset, &s->sectors[i]);
277 if (ret < 0) {
278 goto fail;
279 }
280 s->sectors[i] += last_out_offset;
281 offset += 8;
282
283 ret = read_uint64(bs, offset, &s->sectorcounts[i]);
284 if (ret < 0) {
285 goto fail;
286 }
287 offset += 8;
288
289 if (s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
290 error_report("sector count %" PRIu64 " for chunk %" PRIu32
291 " is larger than max (%u)",
292 s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
293 ret = -EINVAL;
294 goto fail;
295 }
296
297 ret = read_uint64(bs, offset, &s->offsets[i]);
298 if (ret < 0) {
299 goto fail;
300 }
301 s->offsets[i] += last_in_offset;
302 offset += 8;
303
304 ret = read_uint64(bs, offset, &s->lengths[i]);
305 if (ret < 0) {
306 goto fail;
307 }
308 offset += 8;
309
310 if (s->lengths[i] > DMG_LENGTHS_MAX) {
311 error_report("length %" PRIu64 " for chunk %" PRIu32
312 " is larger than max (%u)",
313 s->lengths[i], i, DMG_LENGTHS_MAX);
314 ret = -EINVAL;
315 goto fail;
316 }
317
318 update_max_chunk_size(s, i, &max_compressed_size,
319 &max_sectors_per_chunk);
320 }
321 s->n_chunks += chunk_count;
322 }
323 }
324
325 /* initialize zlib engine */
326 s->compressed_chunk = qemu_try_blockalign(bs->file,
327 max_compressed_size + 1);
328 s->uncompressed_chunk = qemu_try_blockalign(bs->file,
329 512 * max_sectors_per_chunk);
330 if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
331 ret = -ENOMEM;
332 goto fail;
333 }
334
335 if (inflateInit(&s->zstream) != Z_OK) {
336 ret = -EINVAL;
337 goto fail;
338 }
339
340 s->current_chunk = s->n_chunks;
341
342 qemu_co_mutex_init(&s->lock);
343 return 0;
344
345 fail:
346 g_free(s->types);
347 g_free(s->offsets);
348 g_free(s->lengths);
349 g_free(s->sectors);
350 g_free(s->sectorcounts);
351 qemu_vfree(s->compressed_chunk);
352 qemu_vfree(s->uncompressed_chunk);
353 return ret;
354 }
355
356 static inline int is_sector_in_chunk(BDRVDMGState* s,
357 uint32_t chunk_num, uint64_t sector_num)
358 {
359 if (chunk_num >= s->n_chunks || s->sectors[chunk_num] > sector_num ||
360 s->sectors[chunk_num] + s->sectorcounts[chunk_num] <= sector_num) {
361 return 0;
362 } else {
363 return -1;
364 }
365 }
366
367 static inline uint32_t search_chunk(BDRVDMGState *s, uint64_t sector_num)
368 {
369 /* binary search */
370 uint32_t chunk1 = 0, chunk2 = s->n_chunks, chunk3;
371 while (chunk1 != chunk2) {
372 chunk3 = (chunk1 + chunk2) / 2;
373 if (s->sectors[chunk3] > sector_num) {
374 chunk2 = chunk3;
375 } else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) {
376 return chunk3;
377 } else {
378 chunk1 = chunk3;
379 }
380 }
381 return s->n_chunks; /* error */
382 }
383
384 static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
385 {
386 BDRVDMGState *s = bs->opaque;
387
388 if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
389 int ret;
390 uint32_t chunk = search_chunk(s, sector_num);
391
392 if (chunk >= s->n_chunks) {
393 return -1;
394 }
395
396 s->current_chunk = s->n_chunks;
397 switch (s->types[chunk]) {
398 case 0x80000005: { /* zlib compressed */
399 /* we need to buffer, because only the chunk as whole can be
400 * inflated. */
401 ret = bdrv_pread(bs->file, s->offsets[chunk],
402 s->compressed_chunk, s->lengths[chunk]);
403 if (ret != s->lengths[chunk]) {
404 return -1;
405 }
406
407 s->zstream.next_in = s->compressed_chunk;
408 s->zstream.avail_in = s->lengths[chunk];
409 s->zstream.next_out = s->uncompressed_chunk;
410 s->zstream.avail_out = 512 * s->sectorcounts[chunk];
411 ret = inflateReset(&s->zstream);
412 if (ret != Z_OK) {
413 return -1;
414 }
415 ret = inflate(&s->zstream, Z_FINISH);
416 if (ret != Z_STREAM_END ||
417 s->zstream.total_out != 512 * s->sectorcounts[chunk]) {
418 return -1;
419 }
420 break; }
421 case 1: /* copy */
422 ret = bdrv_pread(bs->file, s->offsets[chunk],
423 s->uncompressed_chunk, s->lengths[chunk]);
424 if (ret != s->lengths[chunk]) {
425 return -1;
426 }
427 break;
428 case 2: /* zero */
429 memset(s->uncompressed_chunk, 0, 512 * s->sectorcounts[chunk]);
430 break;
431 }
432 s->current_chunk = chunk;
433 }
434 return 0;
435 }
436
437 static int dmg_read(BlockDriverState *bs, int64_t sector_num,
438 uint8_t *buf, int nb_sectors)
439 {
440 BDRVDMGState *s = bs->opaque;
441 int i;
442
443 for (i = 0; i < nb_sectors; i++) {
444 uint32_t sector_offset_in_chunk;
445 if (dmg_read_chunk(bs, sector_num + i) != 0) {
446 return -1;
447 }
448 sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
449 memcpy(buf + i * 512,
450 s->uncompressed_chunk + sector_offset_in_chunk * 512, 512);
451 }
452 return 0;
453 }
454
455 static coroutine_fn int dmg_co_read(BlockDriverState *bs, int64_t sector_num,
456 uint8_t *buf, int nb_sectors)
457 {
458 int ret;
459 BDRVDMGState *s = bs->opaque;
460 qemu_co_mutex_lock(&s->lock);
461 ret = dmg_read(bs, sector_num, buf, nb_sectors);
462 qemu_co_mutex_unlock(&s->lock);
463 return ret;
464 }
465
466 static void dmg_close(BlockDriverState *bs)
467 {
468 BDRVDMGState *s = bs->opaque;
469
470 g_free(s->types);
471 g_free(s->offsets);
472 g_free(s->lengths);
473 g_free(s->sectors);
474 g_free(s->sectorcounts);
475 qemu_vfree(s->compressed_chunk);
476 qemu_vfree(s->uncompressed_chunk);
477
478 inflateEnd(&s->zstream);
479 }
480
481 static BlockDriver bdrv_dmg = {
482 .format_name = "dmg",
483 .instance_size = sizeof(BDRVDMGState),
484 .bdrv_probe = dmg_probe,
485 .bdrv_open = dmg_open,
486 .bdrv_read = dmg_co_read,
487 .bdrv_close = dmg_close,
488 };
489
490 static void bdrv_dmg_init(void)
491 {
492 bdrv_register(&bdrv_dmg);
493 }
494
495 block_init(bdrv_dmg_init);