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