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