Statistics
| Branch: | Revision:

root / block / dmg.c @ 7371d56f

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