enable EFER usage in i386 emulation - more cpuid bits
[qemu/qemu_0_9_1_stable.git] / block-dmg.c
blob582e3cb2f0e282ed02bf3993dfad401ce9330788
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "vl.h"
25 #include "block_int.h"
26 #include "bswap.h"
27 #include <zlib.h>
29 typedef struct BDRVDMGState {
30 int fd;
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 */
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 char* compressed_chunk;
48 char* uncompressed_chunk;
49 z_stream zstream;
50 } BDRVDMGState;
52 static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
54 int len=strlen(filename);
55 if(len>4 && !strcmp(filename+len-4,".dmg"))
56 return 2;
57 return 0;
60 static off_t read_off(int fd)
62 uint64_t buffer;
63 if(read(fd,&buffer,8)<8)
64 return 0;
65 return be64_to_cpu(buffer);
68 static off_t read_uint32(int fd)
70 uint32_t buffer;
71 if(read(fd,&buffer,4)<4)
72 return 0;
73 return be32_to_cpu(buffer);
76 static int dmg_open(BlockDriverState *bs, const char *filename)
78 BDRVDMGState *s = bs->opaque;
79 off_t info_begin,info_end,last_in_offset,last_out_offset;
80 uint32_t count;
81 uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
83 s->fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
84 if (s->fd < 0)
85 return -1;
86 bs->read_only = 1;
87 s->n_chunks = 0;
88 s->offsets = s->lengths = s->sectors = s->sectorcounts = 0;
90 /* read offset of info blocks */
91 if(lseek(s->fd,-0x1d8,SEEK_END)<0) {
92 dmg_close:
93 close(s->fd);
94 return -1;
96 info_begin=read_off(s->fd);
97 if(info_begin==0)
98 goto dmg_close;
99 if(lseek(s->fd,info_begin,SEEK_SET)<0)
100 goto dmg_close;
101 if(read_uint32(s->fd)!=0x100)
102 goto dmg_close;
103 if((count = read_uint32(s->fd))==0)
104 goto dmg_close;
105 info_end = info_begin+count;
106 if(lseek(s->fd,0xf8,SEEK_CUR)<0)
107 goto dmg_close;
109 /* read offsets */
110 last_in_offset = last_out_offset = 0;
111 while(lseek(s->fd,0,SEEK_CUR)<info_end) {
112 uint32_t type;
114 count = read_uint32(s->fd);
115 if(count==0)
116 goto dmg_close;
117 type = read_uint32(s->fd);
118 if(type!=0x6d697368 || count<244)
119 lseek(s->fd,count-4,SEEK_CUR);
120 else {
121 int new_size, chunk_count;
122 if(lseek(s->fd,200,SEEK_CUR)<0)
123 goto dmg_close;
124 chunk_count = (count-204)/40;
125 new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
126 s->types = realloc(s->types, new_size/2);
127 s->offsets = realloc(s->offsets, new_size);
128 s->lengths = realloc(s->lengths, new_size);
129 s->sectors = realloc(s->sectors, new_size);
130 s->sectorcounts = realloc(s->sectorcounts, new_size);
132 for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) {
133 s->types[i] = read_uint32(s->fd);
134 if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
135 if(s->types[i]==0xffffffff) {
136 last_in_offset = s->offsets[i-1]+s->lengths[i-1];
137 last_out_offset = s->sectors[i-1]+s->sectorcounts[i-1];
139 chunk_count--;
140 i--;
141 if(lseek(s->fd,36,SEEK_CUR)<0)
142 goto dmg_close;
143 continue;
145 read_uint32(s->fd);
146 s->sectors[i] = last_out_offset+read_off(s->fd);
147 s->sectorcounts[i] = read_off(s->fd);
148 s->offsets[i] = last_in_offset+read_off(s->fd);
149 s->lengths[i] = read_off(s->fd);
150 if(s->lengths[i]>max_compressed_size)
151 max_compressed_size = s->lengths[i];
152 if(s->sectorcounts[i]>max_sectors_per_chunk)
153 max_sectors_per_chunk = s->sectorcounts[i];
155 s->n_chunks+=chunk_count;
159 /* initialize zlib engine */
160 if(!(s->compressed_chunk=(char*)malloc(max_compressed_size+1)))
161 goto dmg_close;
162 if(!(s->uncompressed_chunk=(char*)malloc(512*max_sectors_per_chunk)))
163 goto dmg_close;
164 if(inflateInit(&s->zstream) != Z_OK)
165 goto dmg_close;
167 s->current_chunk = s->n_chunks;
169 return 0;
172 static inline int is_sector_in_chunk(BDRVDMGState* s,
173 uint32_t chunk_num,int sector_num)
175 if(chunk_num>=s->n_chunks || s->sectors[chunk_num]>sector_num ||
176 s->sectors[chunk_num]+s->sectorcounts[chunk_num]<=sector_num)
177 return 0;
178 else
179 return -1;
182 static inline uint32_t search_chunk(BDRVDMGState* s,int sector_num)
184 /* binary search */
185 uint32_t chunk1=0,chunk2=s->n_chunks,chunk3;
186 while(chunk1!=chunk2) {
187 chunk3 = (chunk1+chunk2)/2;
188 if(s->sectors[chunk3]>sector_num)
189 chunk2 = chunk3;
190 else if(s->sectors[chunk3]+s->sectorcounts[chunk3]>sector_num)
191 return chunk3;
192 else
193 chunk1 = chunk3;
195 return s->n_chunks; /* error */
198 static inline int dmg_read_chunk(BDRVDMGState *s,int sector_num)
200 if(!is_sector_in_chunk(s,s->current_chunk,sector_num)) {
201 int ret;
202 uint32_t chunk = search_chunk(s,sector_num);
204 if(chunk>=s->n_chunks)
205 return -1;
207 s->current_chunk = s->n_chunks;
208 switch(s->types[chunk]) {
209 case 0x80000005: { /* zlib compressed */
210 int i;
212 ret = lseek(s->fd, s->offsets[chunk], SEEK_SET);
213 if(ret<0)
214 return -1;
216 /* we need to buffer, because only the chunk as whole can be
217 * inflated. */
218 i=0;
219 do {
220 ret = read(s->fd, s->compressed_chunk+i, s->lengths[chunk]-i);
221 if(ret<0 && errno==EINTR)
222 ret=0;
223 i+=ret;
224 } while(ret>=0 && ret+i<s->lengths[chunk]);
226 if (ret != s->lengths[chunk])
227 return -1;
229 s->zstream.next_in = s->compressed_chunk;
230 s->zstream.avail_in = s->lengths[chunk];
231 s->zstream.next_out = s->uncompressed_chunk;
232 s->zstream.avail_out = 512*s->sectorcounts[chunk];
233 ret = inflateReset(&s->zstream);
234 if(ret != Z_OK)
235 return -1;
236 ret = inflate(&s->zstream, Z_FINISH);
237 if(ret != Z_STREAM_END || s->zstream.total_out != 512*s->sectorcounts[chunk])
238 return -1;
239 break; }
240 case 1: /* copy */
241 ret = read(s->fd, s->uncompressed_chunk, s->lengths[chunk]);
242 if (ret != s->lengths[chunk])
243 return -1;
244 break;
245 case 2: /* zero */
246 memset(s->uncompressed_chunk, 0, 512*s->sectorcounts[chunk]);
247 break;
249 s->current_chunk = chunk;
251 return 0;
254 static int dmg_read(BlockDriverState *bs, int64_t sector_num,
255 uint8_t *buf, int nb_sectors)
257 BDRVDMGState *s = bs->opaque;
258 int i;
260 for(i=0;i<nb_sectors;i++) {
261 uint32_t sector_offset_in_chunk;
262 if(dmg_read_chunk(s, sector_num+i) != 0)
263 return -1;
264 sector_offset_in_chunk = sector_num+i-s->sectors[s->current_chunk];
265 memcpy(buf+i*512,s->uncompressed_chunk+sector_offset_in_chunk*512,512);
267 return 0;
270 static void dmg_close(BlockDriverState *bs)
272 BDRVDMGState *s = bs->opaque;
273 close(s->fd);
274 if(s->n_chunks>0) {
275 free(s->types);
276 free(s->offsets);
277 free(s->lengths);
278 free(s->sectors);
279 free(s->sectorcounts);
281 free(s->compressed_chunk);
282 free(s->uncompressed_chunk);
283 inflateEnd(&s->zstream);
286 BlockDriver bdrv_dmg = {
287 "dmg",
288 sizeof(BDRVDMGState),
289 dmg_probe,
290 dmg_open,
291 dmg_read,
292 NULL,
293 dmg_close,