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