2 * QEMU Block driver for DMG images
4 * Copyright (c) 2004 Johannes E. Schindelin
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
24 #include "qemu-common.h"
25 #include "block_int.h"
30 typedef struct BDRVDMGState
{
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
46 uint64_t* sectorcounts
;
47 uint32_t current_chunk
;
48 uint8_t *compressed_chunk
;
49 uint8_t *uncompressed_chunk
;
53 static int dmg_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
55 int len
=strlen(filename
);
56 if(len
>4 && !strcmp(filename
+len
-4,".dmg"))
61 static off_t
read_off(int fd
, int64_t offset
)
64 if (pread(fd
, &buffer
, 8, offset
) < 8)
66 return be64_to_cpu(buffer
);
69 static off_t
read_uint32(int fd
, int64_t offset
)
72 if (pread(fd
, &buffer
, 4, offset
) < 4)
74 return be32_to_cpu(buffer
);
77 static int dmg_open(BlockDriverState
*bs
, const char *filename
, int flags
)
79 BDRVDMGState
*s
= bs
->opaque
;
80 off_t info_begin
,info_end
,last_in_offset
,last_out_offset
;
82 uint32_t max_compressed_size
=1,max_sectors_per_chunk
=1,i
;
85 s
->fd
= open(filename
, O_RDONLY
| O_BINARY
);
90 s
->offsets
= s
->lengths
= s
->sectors
= s
->sectorcounts
= NULL
;
92 /* read offset of info blocks */
93 offset
= lseek(s
->fd
, -0x1d8, SEEK_END
);
98 info_begin
= read_off(s
->fd
, offset
);
99 if (info_begin
== 0) {
103 if (read_uint32(s
->fd
, info_begin
) != 0x100) {
107 count
= read_uint32(s
->fd
, info_begin
+ 4);
111 info_end
= info_begin
+ count
;
113 offset
= info_begin
+ 0x100;
116 last_in_offset
= last_out_offset
= 0;
117 while (offset
< info_end
) {
120 count
= read_uint32(s
->fd
, offset
);
125 type
= read_uint32(s
->fd
, offset
);
126 if (type
== 0x6d697368 && count
>= 244) {
127 int new_size
, chunk_count
;
132 chunk_count
= (count
-204)/40;
133 new_size
= sizeof(uint64_t) * (s
->n_chunks
+ chunk_count
);
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
);
140 for(i
=s
->n_chunks
;i
<s
->n_chunks
+chunk_count
;i
++) {
141 s
->types
[i
] = read_uint32(s
->fd
, offset
);
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];
155 s
->sectors
[i
] = last_out_offset
+read_off(s
->fd
, offset
);
158 s
->sectorcounts
[i
] = read_off(s
->fd
, offset
);
161 s
->offsets
[i
] = last_in_offset
+read_off(s
->fd
, offset
);
164 s
->lengths
[i
] = read_off(s
->fd
, offset
);
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
];
172 s
->n_chunks
+=chunk_count
;
176 /* initialize zlib engine */
177 s
->compressed_chunk
= qemu_malloc(max_compressed_size
+1);
178 s
->uncompressed_chunk
= qemu_malloc(512*max_sectors_per_chunk
);
179 if(inflateInit(&s
->zstream
) != Z_OK
)
182 s
->current_chunk
= s
->n_chunks
;
190 static inline int is_sector_in_chunk(BDRVDMGState
* s
,
191 uint32_t chunk_num
,int sector_num
)
193 if(chunk_num
>=s
->n_chunks
|| s
->sectors
[chunk_num
]>sector_num
||
194 s
->sectors
[chunk_num
]+s
->sectorcounts
[chunk_num
]<=sector_num
)
200 static inline uint32_t search_chunk(BDRVDMGState
* s
,int sector_num
)
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
)
208 else if(s
->sectors
[chunk3
]+s
->sectorcounts
[chunk3
]>sector_num
)
213 return s
->n_chunks
; /* error */
216 static inline int dmg_read_chunk(BDRVDMGState
*s
,int sector_num
)
218 if(!is_sector_in_chunk(s
,s
->current_chunk
,sector_num
)) {
220 uint32_t chunk
= search_chunk(s
,sector_num
);
222 if(chunk
>=s
->n_chunks
)
225 s
->current_chunk
= s
->n_chunks
;
226 switch(s
->types
[chunk
]) {
227 case 0x80000005: { /* zlib compressed */
230 /* we need to buffer, because only the chunk as whole can be
234 ret
= pread(s
->fd
, s
->compressed_chunk
+i
, s
->lengths
[chunk
]-i
,
235 s
->offsets
[chunk
] + i
);
236 if(ret
<0 && errno
==EINTR
)
239 } while(ret
>=0 && ret
+i
<s
->lengths
[chunk
]);
241 if (ret
!= s
->lengths
[chunk
])
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
);
251 ret
= inflate(&s
->zstream
, Z_FINISH
);
252 if(ret
!= Z_STREAM_END
|| s
->zstream
.total_out
!= 512*s
->sectorcounts
[chunk
])
256 ret
= pread(s
->fd
, s
->uncompressed_chunk
, s
->lengths
[chunk
],
258 if (ret
!= s
->lengths
[chunk
])
262 memset(s
->uncompressed_chunk
, 0, 512*s
->sectorcounts
[chunk
]);
265 s
->current_chunk
= chunk
;
270 static int dmg_read(BlockDriverState
*bs
, int64_t sector_num
,
271 uint8_t *buf
, int nb_sectors
)
273 BDRVDMGState
*s
= bs
->opaque
;
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)
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);
286 static void dmg_close(BlockDriverState
*bs
)
288 BDRVDMGState
*s
= bs
->opaque
;
295 free(s
->sectorcounts
);
297 free(s
->compressed_chunk
);
298 free(s
->uncompressed_chunk
);
299 inflateEnd(&s
->zstream
);
302 static BlockDriver bdrv_dmg
= {
303 .format_name
= "dmg",
304 .instance_size
= sizeof(BDRVDMGState
),
305 .bdrv_probe
= dmg_probe
,
306 .bdrv_file_open
= dmg_open
,
307 .bdrv_read
= dmg_read
,
308 .bdrv_close
= dmg_close
,
311 static void bdrv_dmg_init(void)
313 bdrv_register(&bdrv_dmg
);
316 block_init(bdrv_dmg_init
);