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