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