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
{
31 /* each chunk contains a certain number of sectors,
32 * offsets[i] is the offset in the .dmg file,
33 * lengths[i] is the length of the compressed chunk,
34 * sectors[i] is the sector beginning at offsets[i],
35 * sectorcounts[i] is the number of sectors in that chunk,
36 * the sectors array is ordered
44 uint64_t* sectorcounts
;
45 uint32_t current_chunk
;
46 uint8_t *compressed_chunk
;
47 uint8_t *uncompressed_chunk
;
51 static int dmg_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
53 int len
=strlen(filename
);
54 if(len
>4 && !strcmp(filename
+len
-4,".dmg"))
59 static off_t
read_off(BlockDriverState
*bs
, int64_t offset
)
62 if (bdrv_pread(bs
->file
, offset
, &buffer
, 8) < 8)
64 return be64_to_cpu(buffer
);
67 static off_t
read_uint32(BlockDriverState
*bs
, int64_t offset
)
70 if (bdrv_pread(bs
->file
, offset
, &buffer
, 4) < 4)
72 return be32_to_cpu(buffer
);
75 static int dmg_open(BlockDriverState
*bs
, int flags
)
77 BDRVDMGState
*s
= bs
->opaque
;
78 off_t info_begin
,info_end
,last_in_offset
,last_out_offset
;
80 uint32_t max_compressed_size
=1,max_sectors_per_chunk
=1,i
;
85 s
->offsets
= s
->lengths
= s
->sectors
= s
->sectorcounts
= NULL
;
87 /* read offset of info blocks */
88 offset
= bdrv_getlength(bs
->file
);
94 info_begin
= read_off(bs
, offset
);
95 if (info_begin
== 0) {
99 if (read_uint32(bs
, info_begin
) != 0x100) {
103 count
= read_uint32(bs
, info_begin
+ 4);
107 info_end
= info_begin
+ count
;
109 offset
= info_begin
+ 0x100;
112 last_in_offset
= last_out_offset
= 0;
113 while (offset
< info_end
) {
116 count
= read_uint32(bs
, offset
);
121 type
= read_uint32(bs
, offset
);
122 if (type
== 0x6d697368 && count
>= 244) {
123 int new_size
, chunk_count
;
128 chunk_count
= (count
-204)/40;
129 new_size
= sizeof(uint64_t) * (s
->n_chunks
+ chunk_count
);
130 s
->types
= qemu_realloc(s
->types
, new_size
/2);
131 s
->offsets
= qemu_realloc(s
->offsets
, new_size
);
132 s
->lengths
= qemu_realloc(s
->lengths
, new_size
);
133 s
->sectors
= qemu_realloc(s
->sectors
, new_size
);
134 s
->sectorcounts
= qemu_realloc(s
->sectorcounts
, new_size
);
136 for(i
=s
->n_chunks
;i
<s
->n_chunks
+chunk_count
;i
++) {
137 s
->types
[i
] = read_uint32(bs
, offset
);
139 if(s
->types
[i
]!=0x80000005 && s
->types
[i
]!=1 && s
->types
[i
]!=2) {
140 if(s
->types
[i
]==0xffffffff) {
141 last_in_offset
= s
->offsets
[i
-1]+s
->lengths
[i
-1];
142 last_out_offset
= s
->sectors
[i
-1]+s
->sectorcounts
[i
-1];
151 s
->sectors
[i
] = last_out_offset
+read_off(bs
, offset
);
154 s
->sectorcounts
[i
] = read_off(bs
, offset
);
157 s
->offsets
[i
] = last_in_offset
+read_off(bs
, offset
);
160 s
->lengths
[i
] = read_off(bs
, offset
);
163 if(s
->lengths
[i
]>max_compressed_size
)
164 max_compressed_size
= s
->lengths
[i
];
165 if(s
->sectorcounts
[i
]>max_sectors_per_chunk
)
166 max_sectors_per_chunk
= s
->sectorcounts
[i
];
168 s
->n_chunks
+=chunk_count
;
172 /* initialize zlib engine */
173 s
->compressed_chunk
= qemu_malloc(max_compressed_size
+1);
174 s
->uncompressed_chunk
= qemu_malloc(512*max_sectors_per_chunk
);
175 if(inflateInit(&s
->zstream
) != Z_OK
)
178 s
->current_chunk
= s
->n_chunks
;
185 static inline int is_sector_in_chunk(BDRVDMGState
* s
,
186 uint32_t chunk_num
,int sector_num
)
188 if(chunk_num
>=s
->n_chunks
|| s
->sectors
[chunk_num
]>sector_num
||
189 s
->sectors
[chunk_num
]+s
->sectorcounts
[chunk_num
]<=sector_num
)
195 static inline uint32_t search_chunk(BDRVDMGState
* s
,int sector_num
)
198 uint32_t chunk1
=0,chunk2
=s
->n_chunks
,chunk3
;
199 while(chunk1
!=chunk2
) {
200 chunk3
= (chunk1
+chunk2
)/2;
201 if(s
->sectors
[chunk3
]>sector_num
)
203 else if(s
->sectors
[chunk3
]+s
->sectorcounts
[chunk3
]>sector_num
)
208 return s
->n_chunks
; /* error */
211 static inline int dmg_read_chunk(BlockDriverState
*bs
, int sector_num
)
213 BDRVDMGState
*s
= bs
->opaque
;
215 if(!is_sector_in_chunk(s
,s
->current_chunk
,sector_num
)) {
217 uint32_t chunk
= search_chunk(s
,sector_num
);
219 if(chunk
>=s
->n_chunks
)
222 s
->current_chunk
= s
->n_chunks
;
223 switch(s
->types
[chunk
]) {
224 case 0x80000005: { /* zlib compressed */
227 /* we need to buffer, because only the chunk as whole can be
231 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
] + i
,
232 s
->compressed_chunk
+i
, s
->lengths
[chunk
]-i
);
233 if(ret
<0 && errno
==EINTR
)
236 } while(ret
>=0 && ret
+i
<s
->lengths
[chunk
]);
238 if (ret
!= s
->lengths
[chunk
])
241 s
->zstream
.next_in
= s
->compressed_chunk
;
242 s
->zstream
.avail_in
= s
->lengths
[chunk
];
243 s
->zstream
.next_out
= s
->uncompressed_chunk
;
244 s
->zstream
.avail_out
= 512*s
->sectorcounts
[chunk
];
245 ret
= inflateReset(&s
->zstream
);
248 ret
= inflate(&s
->zstream
, Z_FINISH
);
249 if(ret
!= Z_STREAM_END
|| s
->zstream
.total_out
!= 512*s
->sectorcounts
[chunk
])
253 ret
= bdrv_pread(bs
->file
, s
->offsets
[chunk
],
254 s
->uncompressed_chunk
, s
->lengths
[chunk
]);
255 if (ret
!= s
->lengths
[chunk
])
259 memset(s
->uncompressed_chunk
, 0, 512*s
->sectorcounts
[chunk
]);
262 s
->current_chunk
= chunk
;
267 static int dmg_read(BlockDriverState
*bs
, int64_t sector_num
,
268 uint8_t *buf
, int nb_sectors
)
270 BDRVDMGState
*s
= bs
->opaque
;
273 for(i
=0;i
<nb_sectors
;i
++) {
274 uint32_t sector_offset_in_chunk
;
275 if(dmg_read_chunk(bs
, sector_num
+i
) != 0)
277 sector_offset_in_chunk
= sector_num
+i
-s
->sectors
[s
->current_chunk
];
278 memcpy(buf
+i
*512,s
->uncompressed_chunk
+sector_offset_in_chunk
*512,512);
283 static void dmg_close(BlockDriverState
*bs
)
285 BDRVDMGState
*s
= bs
->opaque
;
291 free(s
->sectorcounts
);
293 free(s
->compressed_chunk
);
294 free(s
->uncompressed_chunk
);
295 inflateEnd(&s
->zstream
);
298 static BlockDriver bdrv_dmg
= {
299 .format_name
= "dmg",
300 .instance_size
= sizeof(BDRVDMGState
),
301 .bdrv_probe
= dmg_probe
,
302 .bdrv_open
= dmg_open
,
303 .bdrv_read
= dmg_read
,
304 .bdrv_close
= dmg_close
,
307 static void bdrv_dmg_init(void)
309 bdrv_register(&bdrv_dmg
);
312 block_init(bdrv_dmg_init
);