Statistics
| Branch: | Revision:

root / block-dmg.c @ 83f64091

History | View | Annotate | Download (8.1 kB)

1 585d0ed9 bellard
/*
2 585d0ed9 bellard
 * QEMU Block driver for DMG images
3 585d0ed9 bellard
 * 
4 585d0ed9 bellard
 * Copyright (c) 2004 Johannes E. Schindelin
5 585d0ed9 bellard
 * 
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 585d0ed9 bellard
#include "vl.h"
25 585d0ed9 bellard
#include "block_int.h"
26 585d0ed9 bellard
#include "bswap.h"
27 585d0ed9 bellard
#include <zlib.h>
28 585d0ed9 bellard
29 585d0ed9 bellard
typedef struct BDRVDMGState {
30 585d0ed9 bellard
    int fd;
31 585d0ed9 bellard
    
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 585d0ed9 bellard
static off_t read_off(int fd)
61 585d0ed9 bellard
{
62 585d0ed9 bellard
        uint64_t buffer;
63 585d0ed9 bellard
        if(read(fd,&buffer,8)<8)
64 585d0ed9 bellard
                return 0;
65 585d0ed9 bellard
        return be64_to_cpu(buffer);
66 585d0ed9 bellard
}
67 585d0ed9 bellard
68 585d0ed9 bellard
static off_t read_uint32(int fd)
69 585d0ed9 bellard
{
70 585d0ed9 bellard
        uint32_t buffer;
71 585d0ed9 bellard
        if(read(fd,&buffer,4)<4)
72 585d0ed9 bellard
                return 0;
73 585d0ed9 bellard
        return be32_to_cpu(buffer);
74 585d0ed9 bellard
}
75 585d0ed9 bellard
76 83f64091 bellard
static int dmg_open(BlockDriverState *bs, const char *filename, 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 585d0ed9 bellard
83 83f64091 bellard
    s->fd = open(filename, O_RDONLY | O_BINARY);
84 585d0ed9 bellard
    if (s->fd < 0)
85 83f64091 bellard
        return -errno;
86 585d0ed9 bellard
    bs->read_only = 1;
87 585d0ed9 bellard
    s->n_chunks = 0;
88 585d0ed9 bellard
    s->offsets = s->lengths = s->sectors = s->sectorcounts = 0;
89 585d0ed9 bellard
    
90 585d0ed9 bellard
    /* read offset of info blocks */
91 585d0ed9 bellard
    if(lseek(s->fd,-0x1d8,SEEK_END)<0) {
92 585d0ed9 bellard
dmg_close:
93 585d0ed9 bellard
        close(s->fd);
94 7c35359c bellard
        /* open raw instead */
95 7c35359c bellard
        bs->drv=&bdrv_raw;
96 83f64091 bellard
        return bs->drv->bdrv_open(bs, filename, flags);
97 585d0ed9 bellard
    }
98 585d0ed9 bellard
    info_begin=read_off(s->fd);
99 585d0ed9 bellard
    if(info_begin==0)
100 585d0ed9 bellard
        goto dmg_close;
101 585d0ed9 bellard
    if(lseek(s->fd,info_begin,SEEK_SET)<0)
102 585d0ed9 bellard
        goto dmg_close;
103 585d0ed9 bellard
    if(read_uint32(s->fd)!=0x100)
104 585d0ed9 bellard
        goto dmg_close;
105 585d0ed9 bellard
    if((count = read_uint32(s->fd))==0)
106 585d0ed9 bellard
        goto dmg_close;
107 585d0ed9 bellard
    info_end = info_begin+count;
108 585d0ed9 bellard
    if(lseek(s->fd,0xf8,SEEK_CUR)<0)
109 585d0ed9 bellard
        goto dmg_close;
110 585d0ed9 bellard
111 585d0ed9 bellard
    /* read offsets */
112 585d0ed9 bellard
    last_in_offset = last_out_offset = 0;
113 585d0ed9 bellard
    while(lseek(s->fd,0,SEEK_CUR)<info_end) {
114 995179f1 bellard
        uint32_t type;
115 995179f1 bellard
116 585d0ed9 bellard
        count = read_uint32(s->fd);
117 585d0ed9 bellard
        if(count==0)
118 585d0ed9 bellard
            goto dmg_close;
119 995179f1 bellard
        type = read_uint32(s->fd);
120 585d0ed9 bellard
        if(type!=0x6d697368 || count<244)
121 585d0ed9 bellard
            lseek(s->fd,count-4,SEEK_CUR);
122 585d0ed9 bellard
        else {
123 585d0ed9 bellard
            int new_size, chunk_count;
124 585d0ed9 bellard
            if(lseek(s->fd,200,SEEK_CUR)<0)
125 585d0ed9 bellard
                goto dmg_close;
126 585d0ed9 bellard
            chunk_count = (count-204)/40;
127 585d0ed9 bellard
            new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
128 585d0ed9 bellard
            s->types = realloc(s->types, new_size/2);
129 585d0ed9 bellard
            s->offsets = realloc(s->offsets, new_size);
130 585d0ed9 bellard
            s->lengths = realloc(s->lengths, new_size);
131 585d0ed9 bellard
            s->sectors = realloc(s->sectors, new_size);
132 585d0ed9 bellard
            s->sectorcounts = realloc(s->sectorcounts, new_size);
133 585d0ed9 bellard
134 585d0ed9 bellard
            for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) {
135 585d0ed9 bellard
                s->types[i] = read_uint32(s->fd);
136 585d0ed9 bellard
                if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
137 585d0ed9 bellard
                    if(s->types[i]==0xffffffff) {
138 585d0ed9 bellard
                        last_in_offset = s->offsets[i-1]+s->lengths[i-1];
139 585d0ed9 bellard
                        last_out_offset = s->sectors[i-1]+s->sectorcounts[i-1];
140 585d0ed9 bellard
                    }
141 585d0ed9 bellard
                    chunk_count--;
142 585d0ed9 bellard
                    i--;
143 585d0ed9 bellard
                    if(lseek(s->fd,36,SEEK_CUR)<0)
144 585d0ed9 bellard
                        goto dmg_close;
145 585d0ed9 bellard
                    continue;
146 585d0ed9 bellard
                }
147 585d0ed9 bellard
                read_uint32(s->fd);
148 585d0ed9 bellard
                s->sectors[i] = last_out_offset+read_off(s->fd);
149 585d0ed9 bellard
                s->sectorcounts[i] = read_off(s->fd);
150 585d0ed9 bellard
                s->offsets[i] = last_in_offset+read_off(s->fd);
151 585d0ed9 bellard
                s->lengths[i] = read_off(s->fd);
152 585d0ed9 bellard
                if(s->lengths[i]>max_compressed_size)
153 585d0ed9 bellard
                    max_compressed_size = s->lengths[i];
154 585d0ed9 bellard
                if(s->sectorcounts[i]>max_sectors_per_chunk)
155 585d0ed9 bellard
                    max_sectors_per_chunk = s->sectorcounts[i];
156 585d0ed9 bellard
            }
157 585d0ed9 bellard
            s->n_chunks+=chunk_count;
158 585d0ed9 bellard
        }
159 585d0ed9 bellard
    }
160 585d0ed9 bellard
161 585d0ed9 bellard
    /* initialize zlib engine */
162 28a5c9c8 bellard
    if(!(s->compressed_chunk = malloc(max_compressed_size+1)))
163 585d0ed9 bellard
        goto dmg_close;
164 28a5c9c8 bellard
    if(!(s->uncompressed_chunk = malloc(512*max_sectors_per_chunk)))
165 585d0ed9 bellard
        goto dmg_close;
166 585d0ed9 bellard
    if(inflateInit(&s->zstream) != Z_OK)
167 585d0ed9 bellard
        goto dmg_close;
168 585d0ed9 bellard
169 585d0ed9 bellard
    s->current_chunk = s->n_chunks;
170 585d0ed9 bellard
    
171 585d0ed9 bellard
    return 0;
172 585d0ed9 bellard
}
173 585d0ed9 bellard
174 585d0ed9 bellard
static inline int is_sector_in_chunk(BDRVDMGState* s,
175 585d0ed9 bellard
                uint32_t chunk_num,int sector_num)
176 585d0ed9 bellard
{
177 585d0ed9 bellard
    if(chunk_num>=s->n_chunks || s->sectors[chunk_num]>sector_num ||
178 585d0ed9 bellard
            s->sectors[chunk_num]+s->sectorcounts[chunk_num]<=sector_num)
179 585d0ed9 bellard
        return 0;
180 585d0ed9 bellard
    else
181 585d0ed9 bellard
        return -1;
182 585d0ed9 bellard
}
183 585d0ed9 bellard
184 585d0ed9 bellard
static inline uint32_t search_chunk(BDRVDMGState* s,int sector_num)
185 585d0ed9 bellard
{
186 585d0ed9 bellard
    /* binary search */
187 585d0ed9 bellard
    uint32_t chunk1=0,chunk2=s->n_chunks,chunk3;
188 585d0ed9 bellard
    while(chunk1!=chunk2) {
189 585d0ed9 bellard
        chunk3 = (chunk1+chunk2)/2;
190 585d0ed9 bellard
        if(s->sectors[chunk3]>sector_num)
191 585d0ed9 bellard
            chunk2 = chunk3;
192 585d0ed9 bellard
        else if(s->sectors[chunk3]+s->sectorcounts[chunk3]>sector_num)
193 585d0ed9 bellard
            return chunk3;
194 585d0ed9 bellard
        else
195 585d0ed9 bellard
            chunk1 = chunk3;
196 585d0ed9 bellard
    }
197 585d0ed9 bellard
    return s->n_chunks; /* error */
198 585d0ed9 bellard
}
199 585d0ed9 bellard
200 585d0ed9 bellard
static inline int dmg_read_chunk(BDRVDMGState *s,int sector_num)
201 585d0ed9 bellard
{
202 585d0ed9 bellard
    if(!is_sector_in_chunk(s,s->current_chunk,sector_num)) {
203 585d0ed9 bellard
        int ret;
204 585d0ed9 bellard
        uint32_t chunk = search_chunk(s,sector_num);
205 585d0ed9 bellard
206 585d0ed9 bellard
        if(chunk>=s->n_chunks)
207 585d0ed9 bellard
            return -1;
208 585d0ed9 bellard
209 585d0ed9 bellard
        s->current_chunk = s->n_chunks;
210 585d0ed9 bellard
        switch(s->types[chunk]) {
211 585d0ed9 bellard
        case 0x80000005: { /* zlib compressed */
212 585d0ed9 bellard
            int i;
213 585d0ed9 bellard
214 585d0ed9 bellard
            ret = lseek(s->fd, s->offsets[chunk], SEEK_SET);
215 585d0ed9 bellard
            if(ret<0)
216 585d0ed9 bellard
                return -1;
217 585d0ed9 bellard
218 585d0ed9 bellard
            /* we need to buffer, because only the chunk as whole can be
219 585d0ed9 bellard
             * inflated. */
220 585d0ed9 bellard
            i=0;
221 585d0ed9 bellard
            do {
222 585d0ed9 bellard
                ret = read(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i);
223 585d0ed9 bellard
                if(ret<0 && errno==EINTR)
224 585d0ed9 bellard
                    ret=0;
225 585d0ed9 bellard
                i+=ret;
226 585d0ed9 bellard
            } while(ret>=0 && ret+i<s->lengths[chunk]);
227 585d0ed9 bellard
228 585d0ed9 bellard
            if (ret != s->lengths[chunk])
229 585d0ed9 bellard
                return -1;
230 585d0ed9 bellard
        
231 585d0ed9 bellard
            s->zstream.next_in = s->compressed_chunk;
232 585d0ed9 bellard
            s->zstream.avail_in = s->lengths[chunk];
233 585d0ed9 bellard
            s->zstream.next_out = s->uncompressed_chunk;
234 585d0ed9 bellard
            s->zstream.avail_out = 512*s->sectorcounts[chunk];
235 585d0ed9 bellard
            ret = inflateReset(&s->zstream);
236 585d0ed9 bellard
            if(ret != Z_OK)
237 585d0ed9 bellard
                return -1;
238 585d0ed9 bellard
            ret = inflate(&s->zstream, Z_FINISH);
239 585d0ed9 bellard
            if(ret != Z_STREAM_END || s->zstream.total_out != 512*s->sectorcounts[chunk])
240 585d0ed9 bellard
                return -1;
241 585d0ed9 bellard
            break; }
242 585d0ed9 bellard
        case 1: /* copy */
243 585d0ed9 bellard
            ret = read(s->fd, s->uncompressed_chunk, s->lengths[chunk]);
244 585d0ed9 bellard
            if (ret != s->lengths[chunk])
245 585d0ed9 bellard
                return -1;
246 585d0ed9 bellard
            break;
247 585d0ed9 bellard
        case 2: /* zero */
248 585d0ed9 bellard
            memset(s->uncompressed_chunk, 0, 512*s->sectorcounts[chunk]);
249 585d0ed9 bellard
            break;
250 585d0ed9 bellard
        }
251 585d0ed9 bellard
        s->current_chunk = chunk;
252 585d0ed9 bellard
    }
253 585d0ed9 bellard
    return 0;
254 585d0ed9 bellard
}
255 585d0ed9 bellard
256 585d0ed9 bellard
static int dmg_read(BlockDriverState *bs, int64_t sector_num, 
257 585d0ed9 bellard
                    uint8_t *buf, int nb_sectors)
258 585d0ed9 bellard
{
259 585d0ed9 bellard
    BDRVDMGState *s = bs->opaque;
260 585d0ed9 bellard
    int i;
261 585d0ed9 bellard
262 585d0ed9 bellard
    for(i=0;i<nb_sectors;i++) {
263 585d0ed9 bellard
        uint32_t sector_offset_in_chunk;
264 585d0ed9 bellard
        if(dmg_read_chunk(s, sector_num+i) != 0)
265 585d0ed9 bellard
            return -1;
266 585d0ed9 bellard
        sector_offset_in_chunk = sector_num+i-s->sectors[s->current_chunk];
267 585d0ed9 bellard
        memcpy(buf+i*512,s->uncompressed_chunk+sector_offset_in_chunk*512,512);
268 585d0ed9 bellard
    }
269 585d0ed9 bellard
    return 0;
270 585d0ed9 bellard
}
271 585d0ed9 bellard
272 585d0ed9 bellard
static void dmg_close(BlockDriverState *bs)
273 585d0ed9 bellard
{
274 585d0ed9 bellard
    BDRVDMGState *s = bs->opaque;
275 585d0ed9 bellard
    close(s->fd);
276 585d0ed9 bellard
    if(s->n_chunks>0) {
277 585d0ed9 bellard
        free(s->types);
278 585d0ed9 bellard
        free(s->offsets);
279 585d0ed9 bellard
        free(s->lengths);
280 585d0ed9 bellard
        free(s->sectors);
281 585d0ed9 bellard
        free(s->sectorcounts);
282 585d0ed9 bellard
    }
283 585d0ed9 bellard
    free(s->compressed_chunk);
284 585d0ed9 bellard
    free(s->uncompressed_chunk);
285 585d0ed9 bellard
    inflateEnd(&s->zstream);
286 585d0ed9 bellard
}
287 585d0ed9 bellard
288 585d0ed9 bellard
BlockDriver bdrv_dmg = {
289 585d0ed9 bellard
    "dmg",
290 585d0ed9 bellard
    sizeof(BDRVDMGState),
291 585d0ed9 bellard
    dmg_probe,
292 585d0ed9 bellard
    dmg_open,
293 585d0ed9 bellard
    dmg_read,
294 585d0ed9 bellard
    NULL,
295 585d0ed9 bellard
    dmg_close,
296 585d0ed9 bellard
};