xref: /openbmc/qemu/block/dmg.c (revision 8917c3bd)
1 /*
2  * QEMU Block driver for DMG images
3  *
4  * Copyright (c) 2004 Johannes E. Schindelin
5  *
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  */
24 #include "qemu-common.h"
25 #include "block/block_int.h"
26 #include "qemu/bswap.h"
27 #include "qemu/module.h"
28 #include <zlib.h>
29 
30 typedef struct BDRVDMGState {
31     CoMutex lock;
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
38      * 0<=i<n_chunks */
39 
40     uint32_t n_chunks;
41     uint32_t* types;
42     uint64_t* offsets;
43     uint64_t* lengths;
44     uint64_t* sectors;
45     uint64_t* sectorcounts;
46     uint32_t current_chunk;
47     uint8_t *compressed_chunk;
48     uint8_t *uncompressed_chunk;
49     z_stream zstream;
50 } BDRVDMGState;
51 
52 static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
53 {
54     int len=strlen(filename);
55     if(len>4 && !strcmp(filename+len-4,".dmg"))
56 	return 2;
57     return 0;
58 }
59 
60 static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
61 {
62     uint64_t buffer;
63     int ret;
64 
65     ret = bdrv_pread(bs->file, offset, &buffer, 8);
66     if (ret < 0) {
67         return ret;
68     }
69 
70     *result = be64_to_cpu(buffer);
71     return 0;
72 }
73 
74 static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
75 {
76     uint32_t buffer;
77     int ret;
78 
79     ret = bdrv_pread(bs->file, offset, &buffer, 4);
80     if (ret < 0) {
81         return ret;
82     }
83 
84     *result = be32_to_cpu(buffer);
85     return 0;
86 }
87 
88 static int dmg_open(BlockDriverState *bs, int flags)
89 {
90     BDRVDMGState *s = bs->opaque;
91     uint64_t info_begin,info_end,last_in_offset,last_out_offset;
92     uint32_t count, tmp;
93     uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
94     int64_t offset;
95     int ret;
96 
97     bs->read_only = 1;
98     s->n_chunks = 0;
99     s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
100 
101     /* read offset of info blocks */
102     offset = bdrv_getlength(bs->file);
103     if (offset < 0) {
104         ret = offset;
105         goto fail;
106     }
107     offset -= 0x1d8;
108 
109     ret = read_uint64(bs, offset, &info_begin);
110     if (ret < 0) {
111         goto fail;
112     } else if (info_begin == 0) {
113         ret = -EINVAL;
114         goto fail;
115     }
116 
117     ret = read_uint32(bs, info_begin, &tmp);
118     if (ret < 0) {
119         goto fail;
120     } else if (tmp != 0x100) {
121         ret = -EINVAL;
122         goto fail;
123     }
124 
125     ret = read_uint32(bs, info_begin + 4, &count);
126     if (ret < 0) {
127         goto fail;
128     } else if (count == 0) {
129         ret = -EINVAL;
130         goto fail;
131     }
132     info_end = info_begin + count;
133 
134     offset = info_begin + 0x100;
135 
136     /* read offsets */
137     last_in_offset = last_out_offset = 0;
138     while (offset < info_end) {
139         uint32_t type;
140 
141         ret = read_uint32(bs, offset, &count);
142         if (ret < 0) {
143             goto fail;
144         } else if (count == 0) {
145             ret = -EINVAL;
146             goto fail;
147         }
148         offset += 4;
149 
150         ret = read_uint32(bs, offset, &type);
151         if (ret < 0) {
152             goto fail;
153         }
154 
155 	if (type == 0x6d697368 && count >= 244) {
156 	    int new_size, chunk_count;
157 
158             offset += 4;
159             offset += 200;
160 
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);
168 
169             for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
170                 ret = read_uint32(bs, offset, &s->types[i]);
171                 if (ret < 0) {
172                     goto fail;
173                 }
174 		offset += 4;
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];
179 		    }
180 		    chunk_count--;
181 		    i--;
182 		    offset += 36;
183 		    continue;
184 		}
185 		offset += 4;
186 
187                 ret = read_uint64(bs, offset, &s->sectors[i]);
188                 if (ret < 0) {
189                     goto fail;
190                 }
191                 s->sectors[i] += last_out_offset;
192                 offset += 8;
193 
194                 ret = read_uint64(bs, offset, &s->sectorcounts[i]);
195                 if (ret < 0) {
196                     goto fail;
197                 }
198                 offset += 8;
199 
200                 ret = read_uint64(bs, offset, &s->offsets[i]);
201                 if (ret < 0) {
202                     goto fail;
203                 }
204                 s->offsets[i] += last_in_offset;
205                 offset += 8;
206 
207                 ret = read_uint64(bs, offset, &s->lengths[i]);
208                 if (ret < 0) {
209                     goto fail;
210                 }
211                 offset += 8;
212 
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];
217 	    }
218 	    s->n_chunks+=chunk_count;
219 	}
220     }
221 
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) {
226         ret = -EINVAL;
227         goto fail;
228     }
229 
230     s->current_chunk = s->n_chunks;
231 
232     qemu_co_mutex_init(&s->lock);
233     return 0;
234 
235 fail:
236     g_free(s->types);
237     g_free(s->offsets);
238     g_free(s->lengths);
239     g_free(s->sectors);
240     g_free(s->sectorcounts);
241     g_free(s->compressed_chunk);
242     g_free(s->uncompressed_chunk);
243     return ret;
244 }
245 
246 static inline int is_sector_in_chunk(BDRVDMGState* s,
247 		uint32_t chunk_num,int sector_num)
248 {
249     if(chunk_num>=s->n_chunks || s->sectors[chunk_num]>sector_num ||
250 	    s->sectors[chunk_num]+s->sectorcounts[chunk_num]<=sector_num)
251 	return 0;
252     else
253 	return -1;
254 }
255 
256 static inline uint32_t search_chunk(BDRVDMGState* s,int sector_num)
257 {
258     /* binary search */
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)
263 	    chunk2 = chunk3;
264 	else if(s->sectors[chunk3]+s->sectorcounts[chunk3]>sector_num)
265 	    return chunk3;
266 	else
267 	    chunk1 = chunk3;
268     }
269     return s->n_chunks; /* error */
270 }
271 
272 static inline int dmg_read_chunk(BlockDriverState *bs, int sector_num)
273 {
274     BDRVDMGState *s = bs->opaque;
275 
276     if(!is_sector_in_chunk(s,s->current_chunk,sector_num)) {
277 	int ret;
278 	uint32_t chunk = search_chunk(s,sector_num);
279 
280 	if(chunk>=s->n_chunks)
281 	    return -1;
282 
283 	s->current_chunk = s->n_chunks;
284 	switch(s->types[chunk]) {
285 	case 0x80000005: { /* zlib compressed */
286 	    int i;
287 
288 	    /* we need to buffer, because only the chunk as whole can be
289 	     * inflated. */
290 	    i=0;
291 	    do {
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)
295 		    ret=0;
296 		i+=ret;
297 	    } while(ret>=0 && ret+i<s->lengths[chunk]);
298 
299 	    if (ret != s->lengths[chunk])
300 		return -1;
301 
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);
307 	    if(ret != Z_OK)
308 		return -1;
309 	    ret = inflate(&s->zstream, Z_FINISH);
310 	    if(ret != Z_STREAM_END || s->zstream.total_out != 512*s->sectorcounts[chunk])
311 		return -1;
312 	    break; }
313 	case 1: /* copy */
314 	    ret = bdrv_pread(bs->file, s->offsets[chunk],
315                              s->uncompressed_chunk, s->lengths[chunk]);
316 	    if (ret != s->lengths[chunk])
317 		return -1;
318 	    break;
319 	case 2: /* zero */
320 	    memset(s->uncompressed_chunk, 0, 512*s->sectorcounts[chunk]);
321 	    break;
322 	}
323 	s->current_chunk = chunk;
324     }
325     return 0;
326 }
327 
328 static int dmg_read(BlockDriverState *bs, int64_t sector_num,
329                     uint8_t *buf, int nb_sectors)
330 {
331     BDRVDMGState *s = bs->opaque;
332     int i;
333 
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)
337 	    return -1;
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);
340     }
341     return 0;
342 }
343 
344 static coroutine_fn int dmg_co_read(BlockDriverState *bs, int64_t sector_num,
345                                     uint8_t *buf, int nb_sectors)
346 {
347     int ret;
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);
352     return ret;
353 }
354 
355 static void dmg_close(BlockDriverState *bs)
356 {
357     BDRVDMGState *s = bs->opaque;
358 
359     g_free(s->types);
360     g_free(s->offsets);
361     g_free(s->lengths);
362     g_free(s->sectors);
363     g_free(s->sectorcounts);
364     g_free(s->compressed_chunk);
365     g_free(s->uncompressed_chunk);
366 
367     inflateEnd(&s->zstream);
368 }
369 
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,
377 };
378 
379 static void bdrv_dmg_init(void)
380 {
381     bdrv_register(&bdrv_dmg);
382 }
383 
384 block_init(bdrv_dmg_init);
385