Statistics
| Branch: | Revision:

root / block / dmg.c @ f8bf8606

History | View | Annotate | Download (8.3 kB)

1 585d0ed9 bellard
/*
2 585d0ed9 bellard
 * QEMU Block driver for DMG images
3 5fafdf24 ths
 *
4 585d0ed9 bellard
 * Copyright (c) 2004 Johannes E. Schindelin
5 5fafdf24 ths
 *
6 585d0ed9 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 585d0ed9 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 585d0ed9 bellard
 * in the Software without restriction, including without limitation the rights
9 585d0ed9 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 585d0ed9 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 585d0ed9 bellard
 * furnished to do so, subject to the following conditions:
12 585d0ed9 bellard
 *
13 585d0ed9 bellard
 * The above copyright notice and this permission notice shall be included in
14 585d0ed9 bellard
 * all copies or substantial portions of the Software.
15 585d0ed9 bellard
 *
16 585d0ed9 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 585d0ed9 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 585d0ed9 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 585d0ed9 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 585d0ed9 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 585d0ed9 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 585d0ed9 bellard
 * THE SOFTWARE.
23 585d0ed9 bellard
 */
24 faf07963 pbrook
#include "qemu-common.h"
25 585d0ed9 bellard
#include "block_int.h"
26 585d0ed9 bellard
#include "bswap.h"
27 5efa9d5a Anthony Liguori
#include "module.h"
28 585d0ed9 bellard
#include <zlib.h>
29 585d0ed9 bellard
30 585d0ed9 bellard
typedef struct BDRVDMGState {
31 585d0ed9 bellard
    /* each chunk contains a certain number of sectors,
32 585d0ed9 bellard
     * offsets[i] is the offset in the .dmg file,
33 585d0ed9 bellard
     * lengths[i] is the length of the compressed chunk,
34 585d0ed9 bellard
     * sectors[i] is the sector beginning at offsets[i],
35 585d0ed9 bellard
     * sectorcounts[i] is the number of sectors in that chunk,
36 585d0ed9 bellard
     * the sectors array is ordered
37 585d0ed9 bellard
     * 0<=i<n_chunks */
38 585d0ed9 bellard
39 585d0ed9 bellard
    uint32_t n_chunks;
40 585d0ed9 bellard
    uint32_t* types;
41 585d0ed9 bellard
    uint64_t* offsets;
42 585d0ed9 bellard
    uint64_t* lengths;
43 585d0ed9 bellard
    uint64_t* sectors;
44 585d0ed9 bellard
    uint64_t* sectorcounts;
45 585d0ed9 bellard
    uint32_t current_chunk;
46 28a5c9c8 bellard
    uint8_t *compressed_chunk;
47 28a5c9c8 bellard
    uint8_t *uncompressed_chunk;
48 585d0ed9 bellard
    z_stream zstream;
49 585d0ed9 bellard
} BDRVDMGState;
50 585d0ed9 bellard
51 585d0ed9 bellard
static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
52 585d0ed9 bellard
{
53 585d0ed9 bellard
    int len=strlen(filename);
54 585d0ed9 bellard
    if(len>4 && !strcmp(filename+len-4,".dmg"))
55 585d0ed9 bellard
        return 2;
56 585d0ed9 bellard
    return 0;
57 585d0ed9 bellard
}
58 585d0ed9 bellard
59 64a31d5c Christoph Hellwig
static off_t read_off(BlockDriverState *bs, int64_t offset)
60 585d0ed9 bellard
{
61 585d0ed9 bellard
        uint64_t buffer;
62 64a31d5c Christoph Hellwig
        if (bdrv_pread(bs->file, offset, &buffer, 8) < 8)
63 585d0ed9 bellard
                return 0;
64 585d0ed9 bellard
        return be64_to_cpu(buffer);
65 585d0ed9 bellard
}
66 585d0ed9 bellard
67 64a31d5c Christoph Hellwig
static off_t read_uint32(BlockDriverState *bs, int64_t offset)
68 585d0ed9 bellard
{
69 585d0ed9 bellard
        uint32_t buffer;
70 64a31d5c Christoph Hellwig
        if (bdrv_pread(bs->file, offset, &buffer, 4) < 4)
71 585d0ed9 bellard
                return 0;
72 585d0ed9 bellard
        return be32_to_cpu(buffer);
73 585d0ed9 bellard
}
74 585d0ed9 bellard
75 64a31d5c Christoph Hellwig
static int dmg_open(BlockDriverState *bs, int flags)
76 585d0ed9 bellard
{
77 585d0ed9 bellard
    BDRVDMGState *s = bs->opaque;
78 585d0ed9 bellard
    off_t info_begin,info_end,last_in_offset,last_out_offset;
79 585d0ed9 bellard
    uint32_t count;
80 585d0ed9 bellard
    uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
81 16cdf7ce Christoph Hellwig
    int64_t offset;
82 585d0ed9 bellard
83 585d0ed9 bellard
    bs->read_only = 1;
84 585d0ed9 bellard
    s->n_chunks = 0;
85 511d2b14 blueswir1
    s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
86 3b46e624 ths
87 585d0ed9 bellard
    /* read offset of info blocks */
88 64a31d5c Christoph Hellwig
    offset = bdrv_getlength(bs->file);
89 16cdf7ce Christoph Hellwig
    if (offset < 0) {
90 1559ca00 Christoph Hellwig
        goto fail;
91 585d0ed9 bellard
    }
92 64a31d5c Christoph Hellwig
    offset -= 0x1d8;
93 1559ca00 Christoph Hellwig
94 64a31d5c Christoph Hellwig
    info_begin = read_off(bs, offset);
95 16cdf7ce Christoph Hellwig
    if (info_begin == 0) {
96 1559ca00 Christoph Hellwig
        goto fail;
97 16cdf7ce Christoph Hellwig
    }
98 16cdf7ce Christoph Hellwig
99 64a31d5c Christoph Hellwig
    if (read_uint32(bs, info_begin) != 0x100) {
100 16cdf7ce Christoph Hellwig
        goto fail;
101 16cdf7ce Christoph Hellwig
    }
102 16cdf7ce Christoph Hellwig
103 64a31d5c Christoph Hellwig
    count = read_uint32(bs, info_begin + 4);
104 16cdf7ce Christoph Hellwig
    if (count == 0) {
105 16cdf7ce Christoph Hellwig
        goto fail;
106 16cdf7ce Christoph Hellwig
    }
107 16cdf7ce Christoph Hellwig
    info_end = info_begin + count;
108 16cdf7ce Christoph Hellwig
109 16cdf7ce Christoph Hellwig
    offset = info_begin + 0x100;
110 585d0ed9 bellard
111 585d0ed9 bellard
    /* read offsets */
112 585d0ed9 bellard
    last_in_offset = last_out_offset = 0;
113 16cdf7ce Christoph Hellwig
    while (offset < info_end) {
114 995179f1 bellard
        uint32_t type;
115 995179f1 bellard
116 64a31d5c Christoph Hellwig
        count = read_uint32(bs, offset);
117 585d0ed9 bellard
        if(count==0)
118 1559ca00 Christoph Hellwig
            goto fail;
119 16cdf7ce Christoph Hellwig
        offset += 4;
120 16cdf7ce Christoph Hellwig
121 64a31d5c Christoph Hellwig
        type = read_uint32(bs, offset);
122 16cdf7ce Christoph Hellwig
        if (type == 0x6d697368 && count >= 244) {
123 585d0ed9 bellard
            int new_size, chunk_count;
124 16cdf7ce Christoph Hellwig
125 16cdf7ce Christoph Hellwig
            offset += 4;
126 16cdf7ce Christoph Hellwig
            offset += 200;
127 16cdf7ce Christoph Hellwig
128 585d0ed9 bellard
            chunk_count = (count-204)/40;
129 585d0ed9 bellard
            new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
130 2137b4cc ths
            s->types = qemu_realloc(s->types, new_size/2);
131 2137b4cc ths
            s->offsets = qemu_realloc(s->offsets, new_size);
132 2137b4cc ths
            s->lengths = qemu_realloc(s->lengths, new_size);
133 2137b4cc ths
            s->sectors = qemu_realloc(s->sectors, new_size);
134 2137b4cc ths
            s->sectorcounts = qemu_realloc(s->sectorcounts, new_size);
135 585d0ed9 bellard
136 585d0ed9 bellard
            for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) {
137 64a31d5c Christoph Hellwig
                s->types[i] = read_uint32(bs, offset);
138 16cdf7ce Christoph Hellwig
                offset += 4;
139 585d0ed9 bellard
                if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
140 585d0ed9 bellard
                    if(s->types[i]==0xffffffff) {
141 585d0ed9 bellard
                        last_in_offset = s->offsets[i-1]+s->lengths[i-1];
142 585d0ed9 bellard
                        last_out_offset = s->sectors[i-1]+s->sectorcounts[i-1];
143 585d0ed9 bellard
                    }
144 585d0ed9 bellard
                    chunk_count--;
145 585d0ed9 bellard
                    i--;
146 16cdf7ce Christoph Hellwig
                    offset += 36;
147 585d0ed9 bellard
                    continue;
148 585d0ed9 bellard
                }
149 16cdf7ce Christoph Hellwig
                offset += 4;
150 16cdf7ce Christoph Hellwig
151 64a31d5c Christoph Hellwig
                s->sectors[i] = last_out_offset+read_off(bs, offset);
152 16cdf7ce Christoph Hellwig
                offset += 8;
153 16cdf7ce Christoph Hellwig
154 64a31d5c Christoph Hellwig
                s->sectorcounts[i] = read_off(bs, offset);
155 16cdf7ce Christoph Hellwig
                offset += 8;
156 16cdf7ce Christoph Hellwig
157 64a31d5c Christoph Hellwig
                s->offsets[i] = last_in_offset+read_off(bs, offset);
158 16cdf7ce Christoph Hellwig
                offset += 8;
159 16cdf7ce Christoph Hellwig
160 64a31d5c Christoph Hellwig
                s->lengths[i] = read_off(bs, offset);
161 16cdf7ce Christoph Hellwig
                offset += 8;
162 16cdf7ce Christoph Hellwig
163 585d0ed9 bellard
                if(s->lengths[i]>max_compressed_size)
164 585d0ed9 bellard
                    max_compressed_size = s->lengths[i];
165 585d0ed9 bellard
                if(s->sectorcounts[i]>max_sectors_per_chunk)
166 585d0ed9 bellard
                    max_sectors_per_chunk = s->sectorcounts[i];
167 585d0ed9 bellard
            }
168 585d0ed9 bellard
            s->n_chunks+=chunk_count;
169 585d0ed9 bellard
        }
170 585d0ed9 bellard
    }
171 585d0ed9 bellard
172 585d0ed9 bellard
    /* initialize zlib engine */
173 3ec88e80 aliguori
    s->compressed_chunk = qemu_malloc(max_compressed_size+1);
174 3ec88e80 aliguori
    s->uncompressed_chunk = qemu_malloc(512*max_sectors_per_chunk);
175 585d0ed9 bellard
    if(inflateInit(&s->zstream) != Z_OK)
176 1559ca00 Christoph Hellwig
        goto fail;
177 585d0ed9 bellard
178 585d0ed9 bellard
    s->current_chunk = s->n_chunks;
179 3b46e624 ths
180 585d0ed9 bellard
    return 0;
181 1559ca00 Christoph Hellwig
fail:
182 1559ca00 Christoph Hellwig
    return -1;
183 585d0ed9 bellard
}
184 585d0ed9 bellard
185 585d0ed9 bellard
static inline int is_sector_in_chunk(BDRVDMGState* s,
186 585d0ed9 bellard
                uint32_t chunk_num,int sector_num)
187 585d0ed9 bellard
{
188 585d0ed9 bellard
    if(chunk_num>=s->n_chunks || s->sectors[chunk_num]>sector_num ||
189 585d0ed9 bellard
            s->sectors[chunk_num]+s->sectorcounts[chunk_num]<=sector_num)
190 585d0ed9 bellard
        return 0;
191 585d0ed9 bellard
    else
192 585d0ed9 bellard
        return -1;
193 585d0ed9 bellard
}
194 585d0ed9 bellard
195 585d0ed9 bellard
static inline uint32_t search_chunk(BDRVDMGState* s,int sector_num)
196 585d0ed9 bellard
{
197 585d0ed9 bellard
    /* binary search */
198 585d0ed9 bellard
    uint32_t chunk1=0,chunk2=s->n_chunks,chunk3;
199 585d0ed9 bellard
    while(chunk1!=chunk2) {
200 585d0ed9 bellard
        chunk3 = (chunk1+chunk2)/2;
201 585d0ed9 bellard
        if(s->sectors[chunk3]>sector_num)
202 585d0ed9 bellard
            chunk2 = chunk3;
203 585d0ed9 bellard
        else if(s->sectors[chunk3]+s->sectorcounts[chunk3]>sector_num)
204 585d0ed9 bellard
            return chunk3;
205 585d0ed9 bellard
        else
206 585d0ed9 bellard
            chunk1 = chunk3;
207 585d0ed9 bellard
    }
208 585d0ed9 bellard
    return s->n_chunks; /* error */
209 585d0ed9 bellard
}
210 585d0ed9 bellard
211 64a31d5c Christoph Hellwig
static inline int dmg_read_chunk(BlockDriverState *bs, int sector_num)
212 585d0ed9 bellard
{
213 64a31d5c Christoph Hellwig
    BDRVDMGState *s = bs->opaque;
214 64a31d5c Christoph Hellwig
215 585d0ed9 bellard
    if(!is_sector_in_chunk(s,s->current_chunk,sector_num)) {
216 585d0ed9 bellard
        int ret;
217 585d0ed9 bellard
        uint32_t chunk = search_chunk(s,sector_num);
218 585d0ed9 bellard
219 585d0ed9 bellard
        if(chunk>=s->n_chunks)
220 585d0ed9 bellard
            return -1;
221 585d0ed9 bellard
222 585d0ed9 bellard
        s->current_chunk = s->n_chunks;
223 585d0ed9 bellard
        switch(s->types[chunk]) {
224 585d0ed9 bellard
        case 0x80000005: { /* zlib compressed */
225 585d0ed9 bellard
            int i;
226 585d0ed9 bellard
227 585d0ed9 bellard
            /* we need to buffer, because only the chunk as whole can be
228 585d0ed9 bellard
             * inflated. */
229 585d0ed9 bellard
            i=0;
230 585d0ed9 bellard
            do {
231 64a31d5c Christoph Hellwig
                ret = bdrv_pread(bs->file, s->offsets[chunk] + i,
232 64a31d5c Christoph Hellwig
                                 s->compressed_chunk+i, s->lengths[chunk]-i);
233 585d0ed9 bellard
                if(ret<0 && errno==EINTR)
234 585d0ed9 bellard
                    ret=0;
235 585d0ed9 bellard
                i+=ret;
236 585d0ed9 bellard
            } while(ret>=0 && ret+i<s->lengths[chunk]);
237 585d0ed9 bellard
238 585d0ed9 bellard
            if (ret != s->lengths[chunk])
239 585d0ed9 bellard
                return -1;
240 5fafdf24 ths
241 585d0ed9 bellard
            s->zstream.next_in = s->compressed_chunk;
242 585d0ed9 bellard
            s->zstream.avail_in = s->lengths[chunk];
243 585d0ed9 bellard
            s->zstream.next_out = s->uncompressed_chunk;
244 585d0ed9 bellard
            s->zstream.avail_out = 512*s->sectorcounts[chunk];
245 585d0ed9 bellard
            ret = inflateReset(&s->zstream);
246 585d0ed9 bellard
            if(ret != Z_OK)
247 585d0ed9 bellard
                return -1;
248 585d0ed9 bellard
            ret = inflate(&s->zstream, Z_FINISH);
249 585d0ed9 bellard
            if(ret != Z_STREAM_END || s->zstream.total_out != 512*s->sectorcounts[chunk])
250 585d0ed9 bellard
                return -1;
251 585d0ed9 bellard
            break; }
252 585d0ed9 bellard
        case 1: /* copy */
253 64a31d5c Christoph Hellwig
            ret = bdrv_pread(bs->file, s->offsets[chunk],
254 64a31d5c Christoph Hellwig
                             s->uncompressed_chunk, s->lengths[chunk]);
255 585d0ed9 bellard
            if (ret != s->lengths[chunk])
256 585d0ed9 bellard
                return -1;
257 585d0ed9 bellard
            break;
258 585d0ed9 bellard
        case 2: /* zero */
259 585d0ed9 bellard
            memset(s->uncompressed_chunk, 0, 512*s->sectorcounts[chunk]);
260 585d0ed9 bellard
            break;
261 585d0ed9 bellard
        }
262 585d0ed9 bellard
        s->current_chunk = chunk;
263 585d0ed9 bellard
    }
264 585d0ed9 bellard
    return 0;
265 585d0ed9 bellard
}
266 585d0ed9 bellard
267 5fafdf24 ths
static int dmg_read(BlockDriverState *bs, int64_t sector_num,
268 585d0ed9 bellard
                    uint8_t *buf, int nb_sectors)
269 585d0ed9 bellard
{
270 585d0ed9 bellard
    BDRVDMGState *s = bs->opaque;
271 585d0ed9 bellard
    int i;
272 585d0ed9 bellard
273 585d0ed9 bellard
    for(i=0;i<nb_sectors;i++) {
274 585d0ed9 bellard
        uint32_t sector_offset_in_chunk;
275 64a31d5c Christoph Hellwig
        if(dmg_read_chunk(bs, sector_num+i) != 0)
276 585d0ed9 bellard
            return -1;
277 585d0ed9 bellard
        sector_offset_in_chunk = sector_num+i-s->sectors[s->current_chunk];
278 585d0ed9 bellard
        memcpy(buf+i*512,s->uncompressed_chunk+sector_offset_in_chunk*512,512);
279 585d0ed9 bellard
    }
280 585d0ed9 bellard
    return 0;
281 585d0ed9 bellard
}
282 585d0ed9 bellard
283 585d0ed9 bellard
static void dmg_close(BlockDriverState *bs)
284 585d0ed9 bellard
{
285 585d0ed9 bellard
    BDRVDMGState *s = bs->opaque;
286 585d0ed9 bellard
    if(s->n_chunks>0) {
287 585d0ed9 bellard
        free(s->types);
288 585d0ed9 bellard
        free(s->offsets);
289 585d0ed9 bellard
        free(s->lengths);
290 585d0ed9 bellard
        free(s->sectors);
291 585d0ed9 bellard
        free(s->sectorcounts);
292 585d0ed9 bellard
    }
293 585d0ed9 bellard
    free(s->compressed_chunk);
294 585d0ed9 bellard
    free(s->uncompressed_chunk);
295 585d0ed9 bellard
    inflateEnd(&s->zstream);
296 585d0ed9 bellard
}
297 585d0ed9 bellard
298 5efa9d5a Anthony Liguori
static BlockDriver bdrv_dmg = {
299 e60f469c aurel32
    .format_name        = "dmg",
300 e60f469c aurel32
    .instance_size        = sizeof(BDRVDMGState),
301 e60f469c aurel32
    .bdrv_probe                = dmg_probe,
302 64a31d5c Christoph Hellwig
    .bdrv_open                = dmg_open,
303 e60f469c aurel32
    .bdrv_read                = dmg_read,
304 e60f469c aurel32
    .bdrv_close                = dmg_close,
305 585d0ed9 bellard
};
306 5efa9d5a Anthony Liguori
307 5efa9d5a Anthony Liguori
static void bdrv_dmg_init(void)
308 5efa9d5a Anthony Liguori
{
309 5efa9d5a Anthony Liguori
    bdrv_register(&bdrv_dmg);
310 5efa9d5a Anthony Liguori
}
311 5efa9d5a Anthony Liguori
312 5efa9d5a Anthony Liguori
block_init(bdrv_dmg_init);