]> git.proxmox.com Git - qemu.git/blame - block/dmg.c
Merge branch 'for-upstream-0.15' of git://git.linaro.org/people/pmaydell/qemu-arm
[qemu.git] / block / dmg.c
CommitLineData
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"
585d0ed9
FB
25#include "block_int.h"
26#include "bswap.h"
5efa9d5a 27#include "module.h"
585d0ed9
FB
28#include <zlib.h>
29
30typedef struct BDRVDMGState {
585d0ed9
FB
31 /* each chunk contains a certain number of sectors,
32 * offsets[i] is the offset in the .dmg file,
33 * lengths[i] is the length of the compressed chunk,
34 * sectors[i] is the sector beginning at offsets[i],
35 * sectorcounts[i] is the number of sectors in that chunk,
36 * the sectors array is ordered
37 * 0<=i<n_chunks */
38
39 uint32_t n_chunks;
40 uint32_t* types;
41 uint64_t* offsets;
42 uint64_t* lengths;
43 uint64_t* sectors;
44 uint64_t* sectorcounts;
45 uint32_t current_chunk;
28a5c9c8
FB
46 uint8_t *compressed_chunk;
47 uint8_t *uncompressed_chunk;
585d0ed9
FB
48 z_stream zstream;
49} BDRVDMGState;
50
51static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
52{
53 int len=strlen(filename);
54 if(len>4 && !strcmp(filename+len-4,".dmg"))
55 return 2;
56 return 0;
57}
58
64a31d5c 59static off_t read_off(BlockDriverState *bs, int64_t offset)
585d0ed9
FB
60{
61 uint64_t buffer;
64a31d5c 62 if (bdrv_pread(bs->file, offset, &buffer, 8) < 8)
585d0ed9
FB
63 return 0;
64 return be64_to_cpu(buffer);
65}
66
64a31d5c 67static off_t read_uint32(BlockDriverState *bs, int64_t offset)
585d0ed9
FB
68{
69 uint32_t buffer;
64a31d5c 70 if (bdrv_pread(bs->file, offset, &buffer, 4) < 4)
585d0ed9
FB
71 return 0;
72 return be32_to_cpu(buffer);
73}
74
64a31d5c 75static int dmg_open(BlockDriverState *bs, int flags)
585d0ed9
FB
76{
77 BDRVDMGState *s = bs->opaque;
78 off_t info_begin,info_end,last_in_offset,last_out_offset;
79 uint32_t count;
80 uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
16cdf7ce 81 int64_t offset;
585d0ed9 82
585d0ed9
FB
83 bs->read_only = 1;
84 s->n_chunks = 0;
511d2b14 85 s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
3b46e624 86
585d0ed9 87 /* read offset of info blocks */
64a31d5c 88 offset = bdrv_getlength(bs->file);
16cdf7ce 89 if (offset < 0) {
1559ca00 90 goto fail;
585d0ed9 91 }
64a31d5c 92 offset -= 0x1d8;
1559ca00 93
64a31d5c 94 info_begin = read_off(bs, offset);
16cdf7ce 95 if (info_begin == 0) {
1559ca00 96 goto fail;
16cdf7ce
CH
97 }
98
64a31d5c 99 if (read_uint32(bs, info_begin) != 0x100) {
16cdf7ce
CH
100 goto fail;
101 }
102
64a31d5c 103 count = read_uint32(bs, info_begin + 4);
16cdf7ce
CH
104 if (count == 0) {
105 goto fail;
106 }
107 info_end = info_begin + count;
108
109 offset = info_begin + 0x100;
585d0ed9
FB
110
111 /* read offsets */
112 last_in_offset = last_out_offset = 0;
16cdf7ce 113 while (offset < info_end) {
995179f1
FB
114 uint32_t type;
115
64a31d5c 116 count = read_uint32(bs, offset);
585d0ed9 117 if(count==0)
1559ca00 118 goto fail;
16cdf7ce
CH
119 offset += 4;
120
64a31d5c 121 type = read_uint32(bs, offset);
16cdf7ce 122 if (type == 0x6d697368 && count >= 244) {
585d0ed9 123 int new_size, chunk_count;
16cdf7ce
CH
124
125 offset += 4;
126 offset += 200;
127
585d0ed9
FB
128 chunk_count = (count-204)/40;
129 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
2137b4cc
TS
130 s->types = qemu_realloc(s->types, new_size/2);
131 s->offsets = qemu_realloc(s->offsets, new_size);
132 s->lengths = qemu_realloc(s->lengths, new_size);
133 s->sectors = qemu_realloc(s->sectors, new_size);
134 s->sectorcounts = qemu_realloc(s->sectorcounts, new_size);
585d0ed9
FB
135
136 for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) {
64a31d5c 137 s->types[i] = read_uint32(bs, offset);
16cdf7ce 138 offset += 4;
585d0ed9
FB
139 if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
140 if(s->types[i]==0xffffffff) {
141 last_in_offset = s->offsets[i-1]+s->lengths[i-1];
142 last_out_offset = s->sectors[i-1]+s->sectorcounts[i-1];
143 }
144 chunk_count--;
145 i--;
16cdf7ce 146 offset += 36;
585d0ed9
FB
147 continue;
148 }
16cdf7ce
CH
149 offset += 4;
150
64a31d5c 151 s->sectors[i] = last_out_offset+read_off(bs, offset);
16cdf7ce
CH
152 offset += 8;
153
64a31d5c 154 s->sectorcounts[i] = read_off(bs, offset);
16cdf7ce
CH
155 offset += 8;
156
64a31d5c 157 s->offsets[i] = last_in_offset+read_off(bs, offset);
16cdf7ce
CH
158 offset += 8;
159
64a31d5c 160 s->lengths[i] = read_off(bs, offset);
16cdf7ce
CH
161 offset += 8;
162
585d0ed9
FB
163 if(s->lengths[i]>max_compressed_size)
164 max_compressed_size = s->lengths[i];
165 if(s->sectorcounts[i]>max_sectors_per_chunk)
166 max_sectors_per_chunk = s->sectorcounts[i];
167 }
168 s->n_chunks+=chunk_count;
169 }
170 }
171
172 /* initialize zlib engine */
3ec88e80
AL
173 s->compressed_chunk = qemu_malloc(max_compressed_size+1);
174 s->uncompressed_chunk = qemu_malloc(512*max_sectors_per_chunk);
585d0ed9 175 if(inflateInit(&s->zstream) != Z_OK)
1559ca00 176 goto fail;
585d0ed9
FB
177
178 s->current_chunk = s->n_chunks;
3b46e624 179
585d0ed9 180 return 0;
1559ca00 181fail:
1559ca00 182 return -1;
585d0ed9
FB
183}
184
185static inline int is_sector_in_chunk(BDRVDMGState* s,
186 uint32_t chunk_num,int sector_num)
187{
188 if(chunk_num>=s->n_chunks || s->sectors[chunk_num]>sector_num ||
189 s->sectors[chunk_num]+s->sectorcounts[chunk_num]<=sector_num)
190 return 0;
191 else
192 return -1;
193}
194
195static inline uint32_t search_chunk(BDRVDMGState* s,int sector_num)
196{
197 /* binary search */
198 uint32_t chunk1=0,chunk2=s->n_chunks,chunk3;
199 while(chunk1!=chunk2) {
200 chunk3 = (chunk1+chunk2)/2;
201 if(s->sectors[chunk3]>sector_num)
202 chunk2 = chunk3;
203 else if(s->sectors[chunk3]+s->sectorcounts[chunk3]>sector_num)
204 return chunk3;
205 else
206 chunk1 = chunk3;
207 }
208 return s->n_chunks; /* error */
209}
210
64a31d5c 211static inline int dmg_read_chunk(BlockDriverState *bs, int sector_num)
585d0ed9 212{
64a31d5c
CH
213 BDRVDMGState *s = bs->opaque;
214
585d0ed9
FB
215 if(!is_sector_in_chunk(s,s->current_chunk,sector_num)) {
216 int ret;
217 uint32_t chunk = search_chunk(s,sector_num);
218
219 if(chunk>=s->n_chunks)
220 return -1;
221
222 s->current_chunk = s->n_chunks;
223 switch(s->types[chunk]) {
224 case 0x80000005: { /* zlib compressed */
225 int i;
226
585d0ed9
FB
227 /* we need to buffer, because only the chunk as whole can be
228 * inflated. */
229 i=0;
230 do {
64a31d5c
CH
231 ret = bdrv_pread(bs->file, s->offsets[chunk] + i,
232 s->compressed_chunk+i, s->lengths[chunk]-i);
585d0ed9
FB
233 if(ret<0 && errno==EINTR)
234 ret=0;
235 i+=ret;
236 } while(ret>=0 && ret+i<s->lengths[chunk]);
237
238 if (ret != s->lengths[chunk])
239 return -1;
5fafdf24 240
585d0ed9
FB
241 s->zstream.next_in = s->compressed_chunk;
242 s->zstream.avail_in = s->lengths[chunk];
243 s->zstream.next_out = s->uncompressed_chunk;
244 s->zstream.avail_out = 512*s->sectorcounts[chunk];
245 ret = inflateReset(&s->zstream);
246 if(ret != Z_OK)
247 return -1;
248 ret = inflate(&s->zstream, Z_FINISH);
249 if(ret != Z_STREAM_END || s->zstream.total_out != 512*s->sectorcounts[chunk])
250 return -1;
251 break; }
252 case 1: /* copy */
64a31d5c
CH
253 ret = bdrv_pread(bs->file, s->offsets[chunk],
254 s->uncompressed_chunk, s->lengths[chunk]);
585d0ed9
FB
255 if (ret != s->lengths[chunk])
256 return -1;
257 break;
258 case 2: /* zero */
259 memset(s->uncompressed_chunk, 0, 512*s->sectorcounts[chunk]);
260 break;
261 }
262 s->current_chunk = chunk;
263 }
264 return 0;
265}
266
5fafdf24 267static int dmg_read(BlockDriverState *bs, int64_t sector_num,
585d0ed9
FB
268 uint8_t *buf, int nb_sectors)
269{
270 BDRVDMGState *s = bs->opaque;
271 int i;
272
273 for(i=0;i<nb_sectors;i++) {
274 uint32_t sector_offset_in_chunk;
64a31d5c 275 if(dmg_read_chunk(bs, sector_num+i) != 0)
585d0ed9
FB
276 return -1;
277 sector_offset_in_chunk = sector_num+i-s->sectors[s->current_chunk];
278 memcpy(buf+i*512,s->uncompressed_chunk+sector_offset_in_chunk*512,512);
279 }
280 return 0;
281}
282
283static void dmg_close(BlockDriverState *bs)
284{
285 BDRVDMGState *s = bs->opaque;
585d0ed9
FB
286 if(s->n_chunks>0) {
287 free(s->types);
288 free(s->offsets);
289 free(s->lengths);
290 free(s->sectors);
291 free(s->sectorcounts);
292 }
293 free(s->compressed_chunk);
294 free(s->uncompressed_chunk);
295 inflateEnd(&s->zstream);
296}
297
5efa9d5a 298static BlockDriver bdrv_dmg = {
e60f469c
AJ
299 .format_name = "dmg",
300 .instance_size = sizeof(BDRVDMGState),
301 .bdrv_probe = dmg_probe,
64a31d5c 302 .bdrv_open = dmg_open,
e60f469c
AJ
303 .bdrv_read = dmg_read,
304 .bdrv_close = dmg_close,
585d0ed9 305};
5efa9d5a
AL
306
307static void bdrv_dmg_init(void)
308{
309 bdrv_register(&bdrv_dmg);
310}
311
312block_init(bdrv_dmg_init);