blob: 43497bf343088504c71d6ba04829c6ddbff30053 [file] [log] [blame]
bellard585d0ed2004-12-12 11:24:44 +00001/*
2 * QEMU Block driver for DMG images
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard585d0ed2004-12-12 11:24:44 +00004 * Copyright (c) 2004 Johannes E. Schindelin
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard585d0ed2004-12-12 11:24:44 +00006 * 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 */
Peter Maydell80c71a22016-01-18 18:01:42 +000024#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
pbrookfaf07962007-11-11 02:51:17 +000026#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010027#include "block/block_int.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/bswap.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010029#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010030#include "qemu/module.h"
Fam Zheng27685a82016-09-05 10:50:45 +080031#include "dmg.h"
32
33int (*dmg_uncompress_bz2)(char *next_in, unsigned int avail_in,
34 char *next_out, unsigned int avail_out);
bellard585d0ed2004-12-12 11:24:44 +000035
Julio Faracco7a40b412018-11-05 13:08:05 -020036int (*dmg_uncompress_lzfse)(char *next_in, unsigned int avail_in,
37 char *next_out, unsigned int avail_out);
38
Stefan Hajnoczic165f772014-03-26 13:05:58 +010039enum {
40 /* Limit chunk sizes to prevent unreasonable amounts of memory being used
41 * or truncating when converting to 32-bit types
42 */
43 DMG_LENGTHS_MAX = 64 * 1024 * 1024, /* 64 MB */
44 DMG_SECTORCOUNTS_MAX = DMG_LENGTHS_MAX / 512,
45};
46
Julio Faracco95a156f2018-11-05 13:08:06 -020047enum {
48 /* DMG Block Type */
49 UDZE = 0, /* Zeroes */
50 UDRW, /* RAW type */
51 UDIG, /* Ignore */
52 UDCO = 0x80000004,
53 UDZO,
54 UDBZ,
55 ULFO,
56 UDCM = 0x7ffffffe, /* Comments */
Julio Faraccob47c7d52018-12-28 12:50:55 -020057 UDLE = 0xffffffff /* Last Entry */
Julio Faracco95a156f2018-11-05 13:08:06 -020058};
59
bellard585d0ed2004-12-12 11:24:44 +000060static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
61{
Kevin Wolff5866fa2013-03-18 16:20:27 +010062 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 }
bellard585d0ed2004-12-12 11:24:44 +000072 return 0;
73}
74
Kevin Wolf69d34a32013-01-25 17:07:30 +010075static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
bellard585d0ed2004-12-12 11:24:44 +000076{
Kevin Wolf69d34a32013-01-25 17:07:30 +010077 uint64_t buffer;
78 int ret;
79
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +020080 ret = bdrv_pread(bs->file, offset, &buffer, 8);
Kevin Wolf69d34a32013-01-25 17:07:30 +010081 if (ret < 0) {
82 return ret;
83 }
84
85 *result = be64_to_cpu(buffer);
86 return 0;
bellard585d0ed2004-12-12 11:24:44 +000087}
88
Kevin Wolf69d34a32013-01-25 17:07:30 +010089static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
bellard585d0ed2004-12-12 11:24:44 +000090{
Kevin Wolf69d34a32013-01-25 17:07:30 +010091 uint32_t buffer;
92 int ret;
93
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +020094 ret = bdrv_pread(bs->file, offset, &buffer, 4);
Kevin Wolf69d34a32013-01-25 17:07:30 +010095 if (ret < 0) {
96 return ret;
97 }
98
99 *result = be32_to_cpu(buffer);
100 return 0;
bellard585d0ed2004-12-12 11:24:44 +0000101}
102
Peter Wu7aee37b2015-01-06 18:48:07 +0100103static inline uint64_t buff_read_uint64(const uint8_t *buffer, int64_t offset)
104{
105 return be64_to_cpu(*(uint64_t *)&buffer[offset]);
106}
107
108static inline uint32_t buff_read_uint32(const uint8_t *buffer, int64_t offset)
109{
110 return be32_to_cpu(*(uint32_t *)&buffer[offset]);
111}
112
Stefan Hajnoczif0dce232014-03-26 13:06:00 +0100113/* Increase max chunk sizes, if necessary. This function is used to calculate
114 * the buffer sizes needed for compressed/uncompressed chunk I/O.
115 */
116static void update_max_chunk_size(BDRVDMGState *s, uint32_t chunk,
117 uint32_t *max_compressed_size,
118 uint32_t *max_sectors_per_chunk)
119{
120 uint32_t compressed_size = 0;
121 uint32_t uncompressed_sectors = 0;
122
123 switch (s->types[chunk]) {
Julio Faracco95a156f2018-11-05 13:08:06 -0200124 case UDZO: /* zlib compressed */
125 case UDBZ: /* bzip2 compressed */
126 case ULFO: /* lzfse compressed */
Stefan Hajnoczif0dce232014-03-26 13:06:00 +0100127 compressed_size = s->lengths[chunk];
128 uncompressed_sectors = s->sectorcounts[chunk];
129 break;
Julio Faracco95a156f2018-11-05 13:08:06 -0200130 case UDRW: /* copy */
Marc-André Lureau6fb00222017-06-22 13:04:16 +0200131 uncompressed_sectors = DIV_ROUND_UP(s->lengths[chunk], 512);
Stefan Hajnoczif0dce232014-03-26 13:06:00 +0100132 break;
yuchenlin39a04082019-01-03 19:47:00 +0800133 case UDZE: /* zero */
134 case UDIG: /* ignore */
Peter Wu177b7512015-01-06 18:48:15 +0100135 /* as the all-zeroes block may be large, it is treated specially: the
136 * sector is not copied from a large buffer, a simple memset is used
137 * instead. Therefore uncompressed_sectors does not need to be set. */
Stefan Hajnoczif0dce232014-03-26 13:06:00 +0100138 break;
139 }
140
141 if (compressed_size > *max_compressed_size) {
142 *max_compressed_size = compressed_size;
143 }
144 if (uncompressed_sectors > *max_sectors_per_chunk) {
145 *max_sectors_per_chunk = uncompressed_sectors;
146 }
147}
148
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200149static int64_t dmg_find_koly_offset(BdrvChild *file, Error **errp)
Peter Wufa8354b2015-01-06 18:48:04 +0100150{
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200151 BlockDriverState *file_bs = file->bs;
Peter Wufa8354b2015-01-06 18:48:04 +0100152 int64_t length;
153 int64_t offset = 0;
154 uint8_t buffer[515];
155 int i, ret;
156
157 /* bdrv_getlength returns a multiple of block size (512), rounded up. Since
158 * dmg images can have odd sizes, try to look for the "koly" magic which
159 * marks the begin of the UDIF trailer (512 bytes). This magic can be found
160 * in the last 511 bytes of the second-last sector or the first 4 bytes of
161 * the last sector (search space: 515 bytes) */
162 length = bdrv_getlength(file_bs);
163 if (length < 0) {
164 error_setg_errno(errp, -length,
165 "Failed to get file size while reading UDIF trailer");
166 return length;
167 } else if (length < 512) {
168 error_setg(errp, "dmg file must be at least 512 bytes long");
169 return -EINVAL;
170 }
171 if (length > 511 + 512) {
172 offset = length - 511 - 512;
173 }
174 length = length < 515 ? length : 515;
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200175 ret = bdrv_pread(file, offset, buffer, length);
Peter Wufa8354b2015-01-06 18:48:04 +0100176 if (ret < 0) {
177 error_setg_errno(errp, -ret, "Failed while reading UDIF trailer");
178 return ret;
179 }
180 for (i = 0; i < length - 3; i++) {
181 if (buffer[i] == 'k' && buffer[i+1] == 'o' &&
182 buffer[i+2] == 'l' && buffer[i+3] == 'y') {
183 return offset + i;
184 }
185 }
186 error_setg(errp, "Could not locate UDIF trailer in dmg file");
187 return -EINVAL;
188}
189
Peter Wu65a1c7c2015-01-06 18:48:05 +0100190/* used when building the sector table */
191typedef struct DmgHeaderState {
192 /* used internally by dmg_read_mish_block to remember offsets of blocks
193 * across calls */
Peter Wuc6d34862015-01-06 18:48:11 +0100194 uint64_t data_fork_offset;
Peter Wu65a1c7c2015-01-06 18:48:05 +0100195 /* exported for dmg_open */
196 uint32_t max_compressed_size;
197 uint32_t max_sectors_per_chunk;
198} DmgHeaderState;
199
Peter Wua8b10c62015-01-06 18:48:13 +0100200static bool dmg_is_known_block_type(uint32_t entry_type)
201{
202 switch (entry_type) {
yuchenlin39a04082019-01-03 19:47:00 +0800203 case UDZE: /* zeros */
Julio Faracco95a156f2018-11-05 13:08:06 -0200204 case UDRW: /* uncompressed */
yuchenlin39a04082019-01-03 19:47:00 +0800205 case UDIG: /* ignore */
Julio Faracco95a156f2018-11-05 13:08:06 -0200206 case UDZO: /* zlib */
Peter Wua8b10c62015-01-06 18:48:13 +0100207 return true;
Julio Faracco95a156f2018-11-05 13:08:06 -0200208 case UDBZ: /* bzip2 */
Fam Zheng27685a82016-09-05 10:50:45 +0800209 return !!dmg_uncompress_bz2;
Julio Faracco95a156f2018-11-05 13:08:06 -0200210 case ULFO: /* lzfse */
Julio Faracco7a40b412018-11-05 13:08:05 -0200211 return !!dmg_uncompress_lzfse;
Peter Wua8b10c62015-01-06 18:48:13 +0100212 default:
213 return false;
214 }
215}
216
Peter Wu7aee37b2015-01-06 18:48:07 +0100217static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,
218 uint8_t *buffer, uint32_t count)
Peter Wu65a1c7c2015-01-06 18:48:05 +0100219{
Peter Wu65a1c7c2015-01-06 18:48:05 +0100220 uint32_t type, i;
221 int ret;
222 size_t new_size;
223 uint32_t chunk_count;
Peter Wu7aee37b2015-01-06 18:48:07 +0100224 int64_t offset = 0;
Peter Wuc6d34862015-01-06 18:48:11 +0100225 uint64_t data_offset;
226 uint64_t in_offset = ds->data_fork_offset;
Peter Wu66ec3bb2015-01-06 18:48:12 +0100227 uint64_t out_offset;
Peter Wu65a1c7c2015-01-06 18:48:05 +0100228
Peter Wu7aee37b2015-01-06 18:48:07 +0100229 type = buff_read_uint32(buffer, offset);
Peter Wu65a1c7c2015-01-06 18:48:05 +0100230 /* skip data that is not a valid MISH block (invalid magic or too small) */
231 if (type != 0x6d697368 || count < 244) {
232 /* assume success for now */
233 return 0;
234 }
235
Peter Wu66ec3bb2015-01-06 18:48:12 +0100236 /* chunk offsets are relative to this sector number */
237 out_offset = buff_read_uint64(buffer, offset + 8);
238
Peter Wuc6d34862015-01-06 18:48:11 +0100239 /* location in data fork for (compressed) blob (in bytes) */
240 data_offset = buff_read_uint64(buffer, offset + 0x18);
241 in_offset += data_offset;
242
243 /* move to begin of chunk entries */
244 offset += 204;
Peter Wu65a1c7c2015-01-06 18:48:05 +0100245
246 chunk_count = (count - 204) / 40;
247 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
248 s->types = g_realloc(s->types, new_size / 2);
249 s->offsets = g_realloc(s->offsets, new_size);
250 s->lengths = g_realloc(s->lengths, new_size);
251 s->sectors = g_realloc(s->sectors, new_size);
252 s->sectorcounts = g_realloc(s->sectorcounts, new_size);
253
254 for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
Peter Wu7aee37b2015-01-06 18:48:07 +0100255 s->types[i] = buff_read_uint32(buffer, offset);
Peter Wua8b10c62015-01-06 18:48:13 +0100256 if (!dmg_is_known_block_type(s->types[i])) {
Peter Wu65a1c7c2015-01-06 18:48:05 +0100257 chunk_count--;
258 i--;
Peter Wua8b10c62015-01-06 18:48:13 +0100259 offset += 40;
Peter Wu65a1c7c2015-01-06 18:48:05 +0100260 continue;
261 }
Peter Wu65a1c7c2015-01-06 18:48:05 +0100262
Peter Wua8b10c62015-01-06 18:48:13 +0100263 /* sector number */
264 s->sectors[i] = buff_read_uint64(buffer, offset + 8);
Peter Wu66ec3bb2015-01-06 18:48:12 +0100265 s->sectors[i] += out_offset;
Peter Wu65a1c7c2015-01-06 18:48:05 +0100266
Peter Wua8b10c62015-01-06 18:48:13 +0100267 /* sector count */
268 s->sectorcounts[i] = buff_read_uint64(buffer, offset + 0x10);
Peter Wu65a1c7c2015-01-06 18:48:05 +0100269
yuchenlin39a04082019-01-03 19:47:00 +0800270 /* all-zeroes sector (type UDZE and UDIG) does not need to be
271 * "uncompressed" and can therefore be unbounded. */
272 if (s->types[i] != UDZE && s->types[i] != UDIG
273 && s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
Peter Wu65a1c7c2015-01-06 18:48:05 +0100274 error_report("sector count %" PRIu64 " for chunk %" PRIu32
275 " is larger than max (%u)",
276 s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
277 ret = -EINVAL;
278 goto fail;
279 }
280
Peter Wua8b10c62015-01-06 18:48:13 +0100281 /* offset in (compressed) data fork */
282 s->offsets[i] = buff_read_uint64(buffer, offset + 0x18);
Peter Wuc6d34862015-01-06 18:48:11 +0100283 s->offsets[i] += in_offset;
Peter Wu65a1c7c2015-01-06 18:48:05 +0100284
Peter Wua8b10c62015-01-06 18:48:13 +0100285 /* length in (compressed) data fork */
286 s->lengths[i] = buff_read_uint64(buffer, offset + 0x20);
Peter Wu65a1c7c2015-01-06 18:48:05 +0100287
288 if (s->lengths[i] > DMG_LENGTHS_MAX) {
289 error_report("length %" PRIu64 " for chunk %" PRIu32
290 " is larger than max (%u)",
291 s->lengths[i], i, DMG_LENGTHS_MAX);
292 ret = -EINVAL;
293 goto fail;
294 }
295
296 update_max_chunk_size(s, i, &ds->max_compressed_size,
297 &ds->max_sectors_per_chunk);
Peter Wua8b10c62015-01-06 18:48:13 +0100298 offset += 40;
Peter Wu65a1c7c2015-01-06 18:48:05 +0100299 }
300 s->n_chunks += chunk_count;
301 return 0;
302
303fail:
304 return ret;
305}
306
Peter Wub0e8dc52015-01-06 18:48:06 +0100307static int dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds,
308 uint64_t info_begin, uint64_t info_length)
309{
Peter Wu7aee37b2015-01-06 18:48:07 +0100310 BDRVDMGState *s = bs->opaque;
Peter Wub0e8dc52015-01-06 18:48:06 +0100311 int ret;
312 uint32_t count, rsrc_data_offset;
Peter Wu7aee37b2015-01-06 18:48:07 +0100313 uint8_t *buffer = NULL;
Peter Wub0e8dc52015-01-06 18:48:06 +0100314 uint64_t info_end;
315 uint64_t offset;
316
317 /* read offset from begin of resource fork (info_begin) to resource data */
318 ret = read_uint32(bs, info_begin, &rsrc_data_offset);
319 if (ret < 0) {
320 goto fail;
321 } else if (rsrc_data_offset > info_length) {
322 ret = -EINVAL;
323 goto fail;
324 }
325
326 /* read length of resource data */
327 ret = read_uint32(bs, info_begin + 8, &count);
328 if (ret < 0) {
329 goto fail;
330 } else if (count == 0 || rsrc_data_offset + count > info_length) {
331 ret = -EINVAL;
332 goto fail;
333 }
334
335 /* begin of resource data (consisting of one or more resources) */
336 offset = info_begin + rsrc_data_offset;
337
338 /* end of resource data (there is possibly a following resource map
339 * which will be ignored). */
340 info_end = offset + count;
341
342 /* read offsets (mish blocks) from one or more resources in resource data */
343 while (offset < info_end) {
344 /* size of following resource */
345 ret = read_uint32(bs, offset, &count);
346 if (ret < 0) {
347 goto fail;
Peter Wuf6e66522015-01-06 18:48:08 +0100348 } else if (count == 0 || count > info_end - offset) {
Peter Wub0e8dc52015-01-06 18:48:06 +0100349 ret = -EINVAL;
350 goto fail;
351 }
352 offset += 4;
353
Peter Wu7aee37b2015-01-06 18:48:07 +0100354 buffer = g_realloc(buffer, count);
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200355 ret = bdrv_pread(bs->file, offset, buffer, count);
Peter Wu7aee37b2015-01-06 18:48:07 +0100356 if (ret < 0) {
357 goto fail;
358 }
359
360 ret = dmg_read_mish_block(s, ds, buffer, count);
Peter Wub0e8dc52015-01-06 18:48:06 +0100361 if (ret < 0) {
362 goto fail;
363 }
364 /* advance offset by size of resource */
365 offset += count;
366 }
Peter Wu7aee37b2015-01-06 18:48:07 +0100367 ret = 0;
Peter Wub0e8dc52015-01-06 18:48:06 +0100368
369fail:
Peter Wu7aee37b2015-01-06 18:48:07 +0100370 g_free(buffer);
Peter Wub0e8dc52015-01-06 18:48:06 +0100371 return ret;
372}
373
Peter Wu0599e562015-01-06 18:48:09 +0100374static int dmg_read_plist_xml(BlockDriverState *bs, DmgHeaderState *ds,
375 uint64_t info_begin, uint64_t info_length)
376{
377 BDRVDMGState *s = bs->opaque;
378 int ret;
379 uint8_t *buffer = NULL;
380 char *data_begin, *data_end;
381
382 /* Have at least some length to avoid NULL for g_malloc. Attempt to set a
383 * safe upper cap on the data length. A test sample had a XML length of
384 * about 1 MiB. */
385 if (info_length == 0 || info_length > 16 * 1024 * 1024) {
386 ret = -EINVAL;
387 goto fail;
388 }
389
390 buffer = g_malloc(info_length + 1);
391 buffer[info_length] = '\0';
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200392 ret = bdrv_pread(bs->file, info_begin, buffer, info_length);
Peter Wu0599e562015-01-06 18:48:09 +0100393 if (ret != info_length) {
394 ret = -EINVAL;
395 goto fail;
396 }
397
398 /* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
399 * decode. The actual data element has 431 (0x1af) bytes which includes tabs
400 * and line feeds. */
401 data_end = (char *)buffer;
402 while ((data_begin = strstr(data_end, "<data>")) != NULL) {
403 guchar *mish;
404 gsize out_len = 0;
405
406 data_begin += 6;
407 data_end = strstr(data_begin, "</data>");
408 /* malformed XML? */
409 if (data_end == NULL) {
410 ret = -EINVAL;
411 goto fail;
412 }
413 *data_end++ = '\0';
414 mish = g_base64_decode(data_begin, &out_len);
415 ret = dmg_read_mish_block(s, ds, mish, (uint32_t)out_len);
416 g_free(mish);
417 if (ret < 0) {
418 goto fail;
419 }
420 }
421 ret = 0;
422
423fail:
424 g_free(buffer);
425 return ret;
426}
427
Max Reitz015a1032013-09-05 14:22:29 +0200428static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
429 Error **errp)
bellard585d0ed2004-12-12 11:24:44 +0000430{
431 BDRVDMGState *s = bs->opaque;
Peter Wu65a1c7c2015-01-06 18:48:05 +0100432 DmgHeaderState ds;
Peter Wub0e8dc52015-01-06 18:48:06 +0100433 uint64_t rsrc_fork_offset, rsrc_fork_length;
Peter Wu0599e562015-01-06 18:48:09 +0100434 uint64_t plist_xml_offset, plist_xml_length;
Christoph Hellwig16cdf7c2010-05-12 16:31:35 +0200435 int64_t offset;
Kevin Wolf69d34a32013-01-25 17:07:30 +0100436 int ret;
bellard585d0ed2004-12-12 11:24:44 +0000437
Kevin Wolfeaa24102018-10-12 11:27:41 +0200438 ret = bdrv_apply_auto_read_only(bs, NULL, errp);
439 if (ret < 0) {
440 return ret;
441 }
442
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +0100443 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
444 false, errp);
445 if (!bs->file) {
446 return -EINVAL;
447 }
448
Fam Zheng27685a82016-09-05 10:50:45 +0800449 block_module_load_one("dmg-bz2");
Julio Faracco7a40b412018-11-05 13:08:05 -0200450 block_module_load_one("dmg-lzfse");
Kevin Wolf3edf1e72016-04-25 15:43:09 +0200451
bellard585d0ed2004-12-12 11:24:44 +0000452 s->n_chunks = 0;
blueswir1511d2b12009-03-07 15:32:56 +0000453 s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
Peter Wu65a1c7c2015-01-06 18:48:05 +0100454 /* used by dmg_read_mish_block to keep track of the current I/O position */
Peter Wuc6d34862015-01-06 18:48:11 +0100455 ds.data_fork_offset = 0;
Peter Wu65a1c7c2015-01-06 18:48:05 +0100456 ds.max_compressed_size = 1;
457 ds.max_sectors_per_chunk = 1;
ths3b46e622007-09-17 08:09:54 +0000458
Peter Wufa8354b2015-01-06 18:48:04 +0100459 /* locate the UDIF trailer */
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200460 offset = dmg_find_koly_offset(bs->file, errp);
Christoph Hellwig16cdf7c2010-05-12 16:31:35 +0200461 if (offset < 0) {
Kevin Wolf69d34a32013-01-25 17:07:30 +0100462 ret = offset;
Christoph Hellwig1559ca02010-01-11 14:06:54 +0100463 goto fail;
bellard585d0ed2004-12-12 11:24:44 +0000464 }
Christoph Hellwig1559ca02010-01-11 14:06:54 +0100465
Peter Wuc6d34862015-01-06 18:48:11 +0100466 /* offset of data fork (DataForkOffset) */
467 ret = read_uint64(bs, offset + 0x18, &ds.data_fork_offset);
468 if (ret < 0) {
469 goto fail;
470 } else if (ds.data_fork_offset > offset) {
471 ret = -EINVAL;
472 goto fail;
473 }
474
Peter Wub0e8dc52015-01-06 18:48:06 +0100475 /* offset of resource fork (RsrcForkOffset) */
476 ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
Kevin Wolf69d34a32013-01-25 17:07:30 +0100477 if (ret < 0) {
478 goto fail;
Christoph Hellwig16cdf7c2010-05-12 16:31:35 +0200479 }
Peter Wub0e8dc52015-01-06 18:48:06 +0100480 ret = read_uint64(bs, offset + 0x30, &rsrc_fork_length);
Kevin Wolf69d34a32013-01-25 17:07:30 +0100481 if (ret < 0) {
482 goto fail;
Kevin Wolf69d34a32013-01-25 17:07:30 +0100483 }
Peter Wuf6e66522015-01-06 18:48:08 +0100484 if (rsrc_fork_offset >= offset ||
485 rsrc_fork_length > offset - rsrc_fork_offset) {
486 ret = -EINVAL;
487 goto fail;
488 }
Peter Wu0599e562015-01-06 18:48:09 +0100489 /* offset of property list (XMLOffset) */
490 ret = read_uint64(bs, offset + 0xd8, &plist_xml_offset);
491 if (ret < 0) {
492 goto fail;
493 }
494 ret = read_uint64(bs, offset + 0xe0, &plist_xml_length);
495 if (ret < 0) {
496 goto fail;
497 }
498 if (plist_xml_offset >= offset ||
499 plist_xml_length > offset - plist_xml_offset) {
500 ret = -EINVAL;
501 goto fail;
502 }
Peter Wu8daf4252015-01-06 18:48:10 +0100503 ret = read_uint64(bs, offset + 0x1ec, (uint64_t *)&bs->total_sectors);
504 if (ret < 0) {
505 goto fail;
506 }
507 if (bs->total_sectors < 0) {
508 ret = -EINVAL;
509 goto fail;
510 }
Peter Wub0e8dc52015-01-06 18:48:06 +0100511 if (rsrc_fork_length != 0) {
512 ret = dmg_read_resource_fork(bs, &ds,
513 rsrc_fork_offset, rsrc_fork_length);
Kevin Wolf69d34a32013-01-25 17:07:30 +0100514 if (ret < 0) {
515 goto fail;
516 }
Peter Wu0599e562015-01-06 18:48:09 +0100517 } else if (plist_xml_length != 0) {
518 ret = dmg_read_plist_xml(bs, &ds, plist_xml_offset, plist_xml_length);
519 if (ret < 0) {
520 goto fail;
521 }
Peter Wub0e8dc52015-01-06 18:48:06 +0100522 } else {
523 ret = -EINVAL;
524 goto fail;
bellard585d0ed2004-12-12 11:24:44 +0000525 }
526
527 /* initialize zlib engine */
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200528 s->compressed_chunk = qemu_try_blockalign(bs->file->bs,
Peter Wu65a1c7c2015-01-06 18:48:05 +0100529 ds.max_compressed_size + 1);
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200530 s->uncompressed_chunk = qemu_try_blockalign(bs->file->bs,
Peter Wu65a1c7c2015-01-06 18:48:05 +0100531 512 * ds.max_sectors_per_chunk);
Kevin Wolfb546a942014-05-20 13:28:14 +0200532 if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
533 ret = -ENOMEM;
534 goto fail;
535 }
536
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100537 if (inflateInit(&s->zstream) != Z_OK) {
Kevin Wolf69d34a32013-01-25 17:07:30 +0100538 ret = -EINVAL;
539 goto fail;
540 }
bellard585d0ed2004-12-12 11:24:44 +0000541
542 s->current_chunk = s->n_chunks;
ths3b46e622007-09-17 08:09:54 +0000543
Paolo Bonzini848c66e2011-10-20 13:16:21 +0200544 qemu_co_mutex_init(&s->lock);
bellard585d0ed2004-12-12 11:24:44 +0000545 return 0;
Kevin Wolf69d34a32013-01-25 17:07:30 +0100546
Christoph Hellwig1559ca02010-01-11 14:06:54 +0100547fail:
Kevin Wolf69d34a32013-01-25 17:07:30 +0100548 g_free(s->types);
549 g_free(s->offsets);
550 g_free(s->lengths);
551 g_free(s->sectors);
552 g_free(s->sectorcounts);
Kevin Wolfb546a942014-05-20 13:28:14 +0200553 qemu_vfree(s->compressed_chunk);
554 qemu_vfree(s->uncompressed_chunk);
Kevin Wolf69d34a32013-01-25 17:07:30 +0100555 return ret;
bellard585d0ed2004-12-12 11:24:44 +0000556}
557
Eric Blakea6506482016-06-23 16:37:17 -0600558static void dmg_refresh_limits(BlockDriverState *bs, Error **errp)
559{
Eric Blakea5b8dd22016-06-23 16:37:24 -0600560 bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
Eric Blakea6506482016-06-23 16:37:17 -0600561}
562
bellard585d0ed2004-12-12 11:24:44 +0000563static inline int is_sector_in_chunk(BDRVDMGState* s,
Stefan Hajnoczi686d7142014-03-26 13:05:59 +0100564 uint32_t chunk_num, uint64_t sector_num)
bellard585d0ed2004-12-12 11:24:44 +0000565{
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100566 if (chunk_num >= s->n_chunks || s->sectors[chunk_num] > sector_num ||
567 s->sectors[chunk_num] + s->sectorcounts[chunk_num] <= sector_num) {
568 return 0;
569 } else {
570 return -1;
571 }
bellard585d0ed2004-12-12 11:24:44 +0000572}
573
Stefan Hajnoczi686d7142014-03-26 13:05:59 +0100574static inline uint32_t search_chunk(BDRVDMGState *s, uint64_t sector_num)
bellard585d0ed2004-12-12 11:24:44 +0000575{
576 /* binary search */
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100577 uint32_t chunk1 = 0, chunk2 = s->n_chunks, chunk3;
yuchenlin5ef40822019-01-03 19:46:58 +0800578 while (chunk1 <= chunk2) {
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100579 chunk3 = (chunk1 + chunk2) / 2;
580 if (s->sectors[chunk3] > sector_num) {
yuchenlin5ef40822019-01-03 19:46:58 +0800581 if (chunk3 == 0) {
582 goto err;
583 }
584 chunk2 = chunk3 - 1;
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100585 } else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) {
586 return chunk3;
587 } else {
yuchenlin5ef40822019-01-03 19:46:58 +0800588 chunk1 = chunk3 + 1;
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100589 }
bellard585d0ed2004-12-12 11:24:44 +0000590 }
yuchenlin5ef40822019-01-03 19:46:58 +0800591err:
bellard585d0ed2004-12-12 11:24:44 +0000592 return s->n_chunks; /* error */
593}
594
Stefan Hajnoczi686d7142014-03-26 13:05:59 +0100595static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
bellard585d0ed2004-12-12 11:24:44 +0000596{
Christoph Hellwig64a31d52010-05-12 16:31:49 +0200597 BDRVDMGState *s = bs->opaque;
598
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100599 if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
600 int ret;
601 uint32_t chunk = search_chunk(s, sector_num);
bellard585d0ed2004-12-12 11:24:44 +0000602
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100603 if (chunk >= s->n_chunks) {
604 return -1;
605 }
bellard585d0ed2004-12-12 11:24:44 +0000606
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100607 s->current_chunk = s->n_chunks;
Peter Wu6b383c02015-01-06 18:48:14 +0100608 switch (s->types[chunk]) { /* block entry type */
Julio Faracco95a156f2018-11-05 13:08:06 -0200609 case UDZO: { /* zlib compressed */
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100610 /* we need to buffer, because only the chunk as whole can be
611 * inflated. */
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200612 ret = bdrv_pread(bs->file, s->offsets[chunk],
Stefan Hajnoczib404bf82014-03-26 13:05:56 +0100613 s->compressed_chunk, s->lengths[chunk]);
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100614 if (ret != s->lengths[chunk]) {
615 return -1;
616 }
ths5fafdf22007-09-16 21:08:06 +0000617
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100618 s->zstream.next_in = s->compressed_chunk;
619 s->zstream.avail_in = s->lengths[chunk];
620 s->zstream.next_out = s->uncompressed_chunk;
621 s->zstream.avail_out = 512 * s->sectorcounts[chunk];
622 ret = inflateReset(&s->zstream);
623 if (ret != Z_OK) {
624 return -1;
625 }
626 ret = inflate(&s->zstream, Z_FINISH);
627 if (ret != Z_STREAM_END ||
628 s->zstream.total_out != 512 * s->sectorcounts[chunk]) {
629 return -1;
630 }
631 break; }
Julio Faracco95a156f2018-11-05 13:08:06 -0200632 case UDBZ: /* bzip2 compressed */
Fam Zheng27685a82016-09-05 10:50:45 +0800633 if (!dmg_uncompress_bz2) {
634 break;
635 }
Peter Wu6b383c02015-01-06 18:48:14 +0100636 /* we need to buffer, because only the chunk as whole can be
637 * inflated. */
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200638 ret = bdrv_pread(bs->file, s->offsets[chunk],
Peter Wu6b383c02015-01-06 18:48:14 +0100639 s->compressed_chunk, s->lengths[chunk]);
640 if (ret != s->lengths[chunk]) {
641 return -1;
642 }
643
Fam Zheng27685a82016-09-05 10:50:45 +0800644 ret = dmg_uncompress_bz2((char *)s->compressed_chunk,
645 (unsigned int) s->lengths[chunk],
646 (char *)s->uncompressed_chunk,
647 (unsigned int)
648 (512 * s->sectorcounts[chunk]));
649 if (ret < 0) {
650 return ret;
Peter Wu6b383c02015-01-06 18:48:14 +0100651 }
652 break;
Julio Faracco95a156f2018-11-05 13:08:06 -0200653 case ULFO:
Julio Faracco7a40b412018-11-05 13:08:05 -0200654 if (!dmg_uncompress_lzfse) {
655 break;
656 }
657 /* we need to buffer, because only the chunk as whole can be
658 * inflated. */
659 ret = bdrv_pread(bs->file, s->offsets[chunk],
660 s->compressed_chunk, s->lengths[chunk]);
661 if (ret != s->lengths[chunk]) {
662 return -1;
663 }
664
665 ret = dmg_uncompress_lzfse((char *)s->compressed_chunk,
666 (unsigned int) s->lengths[chunk],
667 (char *)s->uncompressed_chunk,
668 (unsigned int)
669 (512 * s->sectorcounts[chunk]));
670 if (ret < 0) {
671 return ret;
672 }
673 break;
Julio Faracco95a156f2018-11-05 13:08:06 -0200674 case UDRW: /* copy */
Kevin Wolfcf2ab8f2016-06-20 18:24:02 +0200675 ret = bdrv_pread(bs->file, s->offsets[chunk],
Christoph Hellwig64a31d52010-05-12 16:31:49 +0200676 s->uncompressed_chunk, s->lengths[chunk]);
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100677 if (ret != s->lengths[chunk]) {
678 return -1;
679 }
680 break;
yuchenlin39a04082019-01-03 19:47:00 +0800681 case UDZE: /* zeros */
682 case UDIG: /* ignore */
Peter Wu177b7512015-01-06 18:48:15 +0100683 /* see dmg_read, it is treated specially. No buffer needs to be
684 * pre-filled, the zeroes can be set directly. */
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100685 break;
686 }
687 s->current_chunk = chunk;
bellard585d0ed2004-12-12 11:24:44 +0000688 }
689 return 0;
690}
691
Kevin Wolf3edf1e72016-04-25 15:43:09 +0200692static int coroutine_fn
693dmg_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
694 QEMUIOVector *qiov, int flags)
bellard585d0ed2004-12-12 11:24:44 +0000695{
696 BDRVDMGState *s = bs->opaque;
Kevin Wolf3edf1e72016-04-25 15:43:09 +0200697 uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
698 int nb_sectors = bytes >> BDRV_SECTOR_BITS;
699 int ret, i;
700
701 assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
702 assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
703
704 qemu_co_mutex_lock(&s->lock);
bellard585d0ed2004-12-12 11:24:44 +0000705
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100706 for (i = 0; i < nb_sectors; i++) {
707 uint32_t sector_offset_in_chunk;
Kevin Wolf3edf1e72016-04-25 15:43:09 +0200708 void *data;
709
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100710 if (dmg_read_chunk(bs, sector_num + i) != 0) {
Kevin Wolf3edf1e72016-04-25 15:43:09 +0200711 ret = -EIO;
712 goto fail;
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100713 }
Peter Wu177b7512015-01-06 18:48:15 +0100714 /* Special case: current chunk is all zeroes. Do not perform a memcpy as
715 * s->uncompressed_chunk may be too small to cover the large all-zeroes
716 * section. dmg_read_chunk is called to find s->current_chunk */
yuchenlin39a04082019-01-03 19:47:00 +0800717 if (s->types[s->current_chunk] == UDZE
718 || s->types[s->current_chunk] == UDIG) { /* all zeroes block entry */
Kevin Wolf3edf1e72016-04-25 15:43:09 +0200719 qemu_iovec_memset(qiov, i * 512, 0, 512);
Peter Wu177b7512015-01-06 18:48:15 +0100720 continue;
721 }
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100722 sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
Kevin Wolf3edf1e72016-04-25 15:43:09 +0200723 data = s->uncompressed_chunk + sector_offset_in_chunk * 512;
724 qemu_iovec_from_buf(qiov, i * 512, data, 512);
bellard585d0ed2004-12-12 11:24:44 +0000725 }
bellard585d0ed2004-12-12 11:24:44 +0000726
Kevin Wolf3edf1e72016-04-25 15:43:09 +0200727 ret = 0;
728fail:
Paolo Bonzini2914caa2011-10-20 13:16:22 +0200729 qemu_co_mutex_unlock(&s->lock);
730 return ret;
731}
732
bellard585d0ed2004-12-12 11:24:44 +0000733static void dmg_close(BlockDriverState *bs)
734{
735 BDRVDMGState *s = bs->opaque;
Kevin Wolf4f8aa2e2013-01-25 17:07:31 +0100736
737 g_free(s->types);
738 g_free(s->offsets);
739 g_free(s->lengths);
740 g_free(s->sectors);
741 g_free(s->sectorcounts);
Kevin Wolfb546a942014-05-20 13:28:14 +0200742 qemu_vfree(s->compressed_chunk);
743 qemu_vfree(s->uncompressed_chunk);
Kevin Wolf4f8aa2e2013-01-25 17:07:31 +0100744
bellard585d0ed2004-12-12 11:24:44 +0000745 inflateEnd(&s->zstream);
746}
747
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500748static BlockDriver bdrv_dmg = {
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100749 .format_name = "dmg",
750 .instance_size = sizeof(BDRVDMGState),
751 .bdrv_probe = dmg_probe,
752 .bdrv_open = dmg_open,
Eric Blakea6506482016-06-23 16:37:17 -0600753 .bdrv_refresh_limits = dmg_refresh_limits,
Kevin Wolf862f2152016-12-19 16:36:02 +0100754 .bdrv_child_perm = bdrv_format_default_perms,
Kevin Wolf3edf1e72016-04-25 15:43:09 +0200755 .bdrv_co_preadv = dmg_co_preadv,
Stefan Hajnoczi2c1885a2014-03-26 13:05:54 +0100756 .bdrv_close = dmg_close,
bellard585d0ed2004-12-12 11:24:44 +0000757};
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500758
759static void bdrv_dmg_init(void)
760{
761 bdrv_register(&bdrv_dmg);
762}
763
764block_init(bdrv_dmg_init);