2 * QEMU Block driver for DMG images
4 * Copyright (c) 2004 Johannes E. Schindelin
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 #include "qemu-common.h"
25 #include "block/block_int.h"
26 #include "qemu/bswap.h"
27 #include "qemu/module.h"
30 typedef struct BDRVDMGState
{
32 /* each chunk contains a certain number of sectors,
33 * offsets[i] is the offset in the .dmg file,
34 * lengths[i] is the length of the compressed chunk,
35 * sectors[i] is the sector beginning at offsets[i],
36 * sectorcounts[i] is the number of sectors in that chunk,
37 * the sectors array is ordered
45 uint64_t* sectorcounts
;
46 uint32_t current_chunk
;
47 uint8_t *compressed_chunk
;
48 uint8_t *uncompressed_chunk
;
52 static int dmg_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
54 int len
=strlen(filename
);
55 if(len
>4 && !strcmp(filename
+len
-4,".dmg"))
60 static int read_uint64(BlockDriverState
*bs
, int64_t offset
, uint64_t *result
)
65 ret
= bdrv_pread(bs
->file
, offset
, &buffer
, 8);
70 *result
= be64_to_cpu(buffer
);
74 static int read_uint32(BlockDriverState
*bs
, int64_t offset
, uint32_t *result
)
79 ret
= bdrv_pread(bs
->file
, offset
, &buffer
, 4);
84 *result
= be32_to_cpu(buffer
);
88 static int dmg_open(BlockDriverState
*bs
, int flags
)
90 BDRVDMGState
*s
= bs
->opaque
;
91 uint64_t info_begin
,info_end
,last_in_offset
,last_out_offset
;
93 uint32_t max_compressed_size
=1,max_sectors_per_chunk
=1,i
;
99 s
->offsets
= s
->lengths
= s
->sectors
= s
->sectorcounts
= NULL
;
101 /* read offset of info blocks */
102 offset
= bdrv_getlength(bs
->file
);
109 ret
= read_uint64(bs
, offset
, &info_begin
);
112 } else if (info_begin
== 0) {
117 ret
= read_uint32(bs
, info_begin
, &tmp
);
120 } else if (tmp
!= 0x100) {
125 ret
= read_uint32(bs
, info_begin
+ 4, &count
);
128 } else if (count
== 0) {
132 info_end
= info_begin
+ count
;
134 offset
= info_begin
+ 0x100;
137 last_in_offset
= last_out_offset
= 0;
138 while (offset
< info_end
) {
141 ret
= read_uint32(bs
, offset
, &count
);
144 } else if (count
== 0) {
150 ret
= read_uint32(bs
, offset
, &type
);
155 if (type
== 0x6d697368 && count
>= 244) {
156 int new_size
, chunk_count
;
161 chunk_count
= (count
-204)/40;
162 new_size
= sizeof(uint64_t) * (s
->n_chunks
+ chunk_count
);
163 s
->types
= g_realloc(s
->types
, new_size
/2);
164 s
->offsets
= g_realloc(s
->offsets
, new_size
);
165 s
->lengths
= g_realloc(s
->lengths
, new_size
);
166 s
->sectors
= g_realloc(s
->sectors
, new_size
);
167 s
->sectorcounts
= g_realloc(s
->sectorcounts
, new_size
);
169 for (i
= s
->n_chunks
; i
< s
->n_chunks
+ chunk_count
; i
++) {
170 ret
= read_uint32(bs
, offset
, &s
->types
[i
]);
175 if(s
->types
[i
]!=0x80000005 && s
->types
[i
]!=1 && s
->types
[i
]!=2) {
176 if(s
->types
[i
]==0xffffffff) {
177 last_in_offset
= s
->offsets
[i
-1]+s
->lengths
[i
-1];
178 last_out_offset
= s
->sectors
[i
-1]+s
->sectorcounts
[i
-1];
187 ret
= read_uint64(bs
, offset
, &s
->sectors
[i
]);
191 s
->sectors
[i
] += last_out_offset
;
194 ret
= read_uint64(bs
, offset
, &s
->sectorcounts
[i
]);
200 ret
= read_uint64(bs
, offset
, &s
->offsets
[i
]);
204 s
->offsets
[i
] += last_in_offset
;
207 ret
= read_uint64(bs
, offset
, &s
->lengths
[i
]);
213 if(s
->lengths
[i
]>max_compressed_size
)
214 max_compressed_size
= s
->lengths
[i
];
215 if(s
->sectorcounts
[i
]>max_sectors_per_chunk
)
216 max_sectors_per_chunk
= s
->sectorcounts
[i
];
218 s
->n_chunks
+=chunk_count
;
222 /* initialize zlib engine */
223 s
->compressed_chunk
= g_malloc(max_compressed_size
+1);
224 s
->uncompressed_chunk
= g_malloc(512*max_sectors_per_chunk
);
225 if(inflateInit(&s
->zstream
) != Z_OK
) {
230 s
->current_chunk
= s
->n_chunks
;
232 qemu_co_mutex_init(&s
->lock
);
240 g_free(s
->sectorcounts
);
241 g_free(s
->compressed_chunk
);
242 g_free(s
->uncompressed_chunk
);
246 static inline int is_sector_in_chunk(BDRVDMGState
* s
,
247 uint32_t chunk_num
,int sector_num
)
249 if(chunk_num
>=s
->n_chunks
|| s
->sectors
[chunk_num
]>sector_num
||
250 s
->sectors
[chunk_num
]+s
->sectorcounts
[chunk_num
]<=sector_num
)
256 static inline uint32_t search_chunk(BDRVDMGState
* s
,int sector_num
)
259 uint32_t chunk1
=0,chunk2
=s
->n_chunks
,chunk3
;
260 while(chunk1
!=chunk2
) {
261 chunk3
= (chunk1
+chunk2
)/2;
262 if(s
->sectors
[chunk3
]>sector_num
)
264 else if(s
->sectors
[chunk3
]+s
->sectorcounts
[chunk3
]>sector_num
)
269 return s
->n_chunks
; /* error */
272 static inline int dmg_read_chunk(BlockDriverState
*bs
, int sector_num
)
274 BDRVDMGState
*s
= bs
->opaque
;
276 if(!is_sector_in_chunk(s
,s
->current_chunk
,sector_num
)) {
278 uint32_t chunk
= search_chunk(s
,sector_num
);
280 if(chunk
>=s
->n_chunks
)
283 s
->current_chunk
= s
->n_chunks
;
284 switch(s
->types
[chunk
]) {
285 case 0x80000005: { /* zlib compressed */
288 /* we need to buffer, because only the chunk as whole can be
292 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
] + i
,
293 s
->compressed_chunk
+i
, s
->lengths
[chunk
]-i
);
294 if(ret
<0 && errno
==EINTR
)
297 } while(ret
>=0 && ret
+i
<s
->lengths
[chunk
]);
299 if (ret
!= s
->lengths
[chunk
])
302 s
->zstream
.next_in
= s
->compressed_chunk
;
303 s
->zstream
.avail_in
= s
->lengths
[chunk
];
304 s
->zstream
.next_out
= s
->uncompressed_chunk
;
305 s
->zstream
.avail_out
= 512*s
->sectorcounts
[chunk
];
306 ret
= inflateReset(&s
->zstream
);
309 ret
= inflate(&s
->zstream
, Z_FINISH
);
310 if(ret
!= Z_STREAM_END
|| s
->zstream
.total_out
!= 512*s
->sectorcounts
[chunk
])
314 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
315 s
->uncompressed_chunk
, s
->lengths
[chunk
]);
316 if (ret
!= s
->lengths
[chunk
])
320 memset(s
->uncompressed_chunk
, 0, 512*s
->sectorcounts
[chunk
]);
323 s
->current_chunk
= chunk
;
328 static int dmg_read(BlockDriverState
*bs
, int64_t sector_num
,
329 uint8_t *buf
, int nb_sectors
)
331 BDRVDMGState
*s
= bs
->opaque
;
334 for(i
=0;i
<nb_sectors
;i
++) {
335 uint32_t sector_offset_in_chunk
;
336 if(dmg_read_chunk(bs
, sector_num
+i
) != 0)
338 sector_offset_in_chunk
= sector_num
+i
-s
->sectors
[s
->current_chunk
];
339 memcpy(buf
+i
*512,s
->uncompressed_chunk
+sector_offset_in_chunk
*512,512);
344 static coroutine_fn
int dmg_co_read(BlockDriverState
*bs
, int64_t sector_num
,
345 uint8_t *buf
, int nb_sectors
)
348 BDRVDMGState
*s
= bs
->opaque
;
349 qemu_co_mutex_lock(&s
->lock
);
350 ret
= dmg_read(bs
, sector_num
, buf
, nb_sectors
);
351 qemu_co_mutex_unlock(&s
->lock
);
355 static void dmg_close(BlockDriverState
*bs
)
357 BDRVDMGState
*s
= bs
->opaque
;
363 g_free(s
->sectorcounts
);
364 g_free(s
->compressed_chunk
);
365 g_free(s
->uncompressed_chunk
);
367 inflateEnd(&s
->zstream
);
370 static BlockDriver bdrv_dmg
= {
371 .format_name
= "dmg",
372 .instance_size
= sizeof(BDRVDMGState
),
373 .bdrv_probe
= dmg_probe
,
374 .bdrv_open
= dmg_open
,
375 .bdrv_read
= dmg_co_read
,
376 .bdrv_close
= dmg_close
,
379 static void bdrv_dmg_init(void)
381 bdrv_register(&bdrv_dmg
);
384 block_init(bdrv_dmg_init
);