2004-12-12 12:24:44 +01:00
|
|
|
/*
|
|
|
|
* QEMU Block driver for DMG images
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2004-12-12 12:24:44 +01:00
|
|
|
* Copyright (c) 2004 Johannes E. Schindelin
|
2007-09-16 23:08:06 +02:00
|
|
|
*
|
2004-12-12 12:24:44 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2007-11-11 03:51:17 +01:00
|
|
|
#include "qemu-common.h"
|
2004-12-12 12:24:44 +01:00
|
|
|
#include "block_int.h"
|
|
|
|
#include "bswap.h"
|
2009-05-10 00:03:42 +02:00
|
|
|
#include "module.h"
|
2004-12-12 12:24:44 +01:00
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
typedef struct BDRVDMGState {
|
|
|
|
/* each chunk contains a certain number of sectors,
|
|
|
|
* offsets[i] is the offset in the .dmg file,
|
|
|
|
* lengths[i] is the length of the compressed chunk,
|
|
|
|
* sectors[i] is the sector beginning at offsets[i],
|
|
|
|
* sectorcounts[i] is the number of sectors in that chunk,
|
|
|
|
* the sectors array is ordered
|
|
|
|
* 0<=i<n_chunks */
|
|
|
|
|
|
|
|
uint32_t n_chunks;
|
|
|
|
uint32_t* types;
|
|
|
|
uint64_t* offsets;
|
|
|
|
uint64_t* lengths;
|
|
|
|
uint64_t* sectors;
|
|
|
|
uint64_t* sectorcounts;
|
|
|
|
uint32_t current_chunk;
|
2006-02-01 22:43:52 +01:00
|
|
|
uint8_t *compressed_chunk;
|
|
|
|
uint8_t *uncompressed_chunk;
|
2004-12-12 12:24:44 +01:00
|
|
|
z_stream zstream;
|
|
|
|
} BDRVDMGState;
|
|
|
|
|
|
|
|
static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
|
|
|
|
{
|
|
|
|
int len=strlen(filename);
|
|
|
|
if(len>4 && !strcmp(filename+len-4,".dmg"))
|
|
|
|
return 2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-12 16:31:49 +02:00
|
|
|
static off_t read_off(BlockDriverState *bs, int64_t offset)
|
2004-12-12 12:24:44 +01:00
|
|
|
{
|
|
|
|
uint64_t buffer;
|
2010-05-12 16:31:49 +02:00
|
|
|
if (bdrv_pread(bs->file, offset, &buffer, 8) < 8)
|
2004-12-12 12:24:44 +01:00
|
|
|
return 0;
|
|
|
|
return be64_to_cpu(buffer);
|
|
|
|
}
|
|
|
|
|
2010-05-12 16:31:49 +02:00
|
|
|
static off_t read_uint32(BlockDriverState *bs, int64_t offset)
|
2004-12-12 12:24:44 +01:00
|
|
|
{
|
|
|
|
uint32_t buffer;
|
2010-05-12 16:31:49 +02:00
|
|
|
if (bdrv_pread(bs->file, offset, &buffer, 4) < 4)
|
2004-12-12 12:24:44 +01:00
|
|
|
return 0;
|
|
|
|
return be32_to_cpu(buffer);
|
|
|
|
}
|
|
|
|
|
2010-05-12 16:31:49 +02:00
|
|
|
static int dmg_open(BlockDriverState *bs, int flags)
|
2004-12-12 12:24:44 +01:00
|
|
|
{
|
|
|
|
BDRVDMGState *s = bs->opaque;
|
|
|
|
off_t info_begin,info_end,last_in_offset,last_out_offset;
|
|
|
|
uint32_t count;
|
|
|
|
uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
|
2010-05-12 16:31:35 +02:00
|
|
|
int64_t offset;
|
2004-12-12 12:24:44 +01:00
|
|
|
|
|
|
|
bs->read_only = 1;
|
|
|
|
s->n_chunks = 0;
|
2009-03-07 16:32:56 +01:00
|
|
|
s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-12-12 12:24:44 +01:00
|
|
|
/* read offset of info blocks */
|
2010-05-12 16:31:49 +02:00
|
|
|
offset = bdrv_getlength(bs->file);
|
2010-05-12 16:31:35 +02:00
|
|
|
if (offset < 0) {
|
2010-01-11 14:06:54 +01:00
|
|
|
goto fail;
|
2004-12-12 12:24:44 +01:00
|
|
|
}
|
2010-05-12 16:31:49 +02:00
|
|
|
offset -= 0x1d8;
|
2010-01-11 14:06:54 +01:00
|
|
|
|
2010-05-12 16:31:49 +02:00
|
|
|
info_begin = read_off(bs, offset);
|
2010-05-12 16:31:35 +02:00
|
|
|
if (info_begin == 0) {
|
2010-01-11 14:06:54 +01:00
|
|
|
goto fail;
|
2010-05-12 16:31:35 +02:00
|
|
|
}
|
|
|
|
|
2010-05-12 16:31:49 +02:00
|
|
|
if (read_uint32(bs, info_begin) != 0x100) {
|
2010-05-12 16:31:35 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2010-05-12 16:31:49 +02:00
|
|
|
count = read_uint32(bs, info_begin + 4);
|
2010-05-12 16:31:35 +02:00
|
|
|
if (count == 0) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
info_end = info_begin + count;
|
|
|
|
|
|
|
|
offset = info_begin + 0x100;
|
2004-12-12 12:24:44 +01:00
|
|
|
|
|
|
|
/* read offsets */
|
|
|
|
last_in_offset = last_out_offset = 0;
|
2010-05-12 16:31:35 +02:00
|
|
|
while (offset < info_end) {
|
2005-01-04 00:39:08 +01:00
|
|
|
uint32_t type;
|
|
|
|
|
2010-05-12 16:31:49 +02:00
|
|
|
count = read_uint32(bs, offset);
|
2004-12-12 12:24:44 +01:00
|
|
|
if(count==0)
|
2010-01-11 14:06:54 +01:00
|
|
|
goto fail;
|
2010-05-12 16:31:35 +02:00
|
|
|
offset += 4;
|
|
|
|
|
2010-05-12 16:31:49 +02:00
|
|
|
type = read_uint32(bs, offset);
|
2010-05-12 16:31:35 +02:00
|
|
|
if (type == 0x6d697368 && count >= 244) {
|
2004-12-12 12:24:44 +01:00
|
|
|
int new_size, chunk_count;
|
2010-05-12 16:31:35 +02:00
|
|
|
|
|
|
|
offset += 4;
|
|
|
|
offset += 200;
|
|
|
|
|
2004-12-12 12:24:44 +01:00
|
|
|
chunk_count = (count-204)/40;
|
|
|
|
new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
|
2011-08-21 05:09:37 +02:00
|
|
|
s->types = g_realloc(s->types, new_size/2);
|
|
|
|
s->offsets = g_realloc(s->offsets, new_size);
|
|
|
|
s->lengths = g_realloc(s->lengths, new_size);
|
|
|
|
s->sectors = g_realloc(s->sectors, new_size);
|
|
|
|
s->sectorcounts = g_realloc(s->sectorcounts, new_size);
|
2004-12-12 12:24:44 +01:00
|
|
|
|
|
|
|
for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) {
|
2010-05-12 16:31:49 +02:00
|
|
|
s->types[i] = read_uint32(bs, offset);
|
2010-05-12 16:31:35 +02:00
|
|
|
offset += 4;
|
2004-12-12 12:24:44 +01:00
|
|
|
if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
|
|
|
|
if(s->types[i]==0xffffffff) {
|
|
|
|
last_in_offset = s->offsets[i-1]+s->lengths[i-1];
|
|
|
|
last_out_offset = s->sectors[i-1]+s->sectorcounts[i-1];
|
|
|
|
}
|
|
|
|
chunk_count--;
|
|
|
|
i--;
|
2010-05-12 16:31:35 +02:00
|
|
|
offset += 36;
|
2004-12-12 12:24:44 +01:00
|
|
|
continue;
|
|
|
|
}
|
2010-05-12 16:31:35 +02:00
|
|
|
offset += 4;
|
|
|
|
|
2010-05-12 16:31:49 +02:00
|
|
|
s->sectors[i] = last_out_offset+read_off(bs, offset);
|
2010-05-12 16:31:35 +02:00
|
|
|
offset += 8;
|
|
|
|
|
2010-05-12 16:31:49 +02:00
|
|
|
s->sectorcounts[i] = read_off(bs, offset);
|
2010-05-12 16:31:35 +02:00
|
|
|
offset += 8;
|
|
|
|
|
2010-05-12 16:31:49 +02:00
|
|
|
s->offsets[i] = last_in_offset+read_off(bs, offset);
|
2010-05-12 16:31:35 +02:00
|
|
|
offset += 8;
|
|
|
|
|
2010-05-12 16:31:49 +02:00
|
|
|
s->lengths[i] = read_off(bs, offset);
|
2010-05-12 16:31:35 +02:00
|
|
|
offset += 8;
|
|
|
|
|
2004-12-12 12:24:44 +01:00
|
|
|
if(s->lengths[i]>max_compressed_size)
|
|
|
|
max_compressed_size = s->lengths[i];
|
|
|
|
if(s->sectorcounts[i]>max_sectors_per_chunk)
|
|
|
|
max_sectors_per_chunk = s->sectorcounts[i];
|
|
|
|
}
|
|
|
|
s->n_chunks+=chunk_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize zlib engine */
|
2011-08-21 05:09:37 +02:00
|
|
|
s->compressed_chunk = g_malloc(max_compressed_size+1);
|
|
|
|
s->uncompressed_chunk = g_malloc(512*max_sectors_per_chunk);
|
2004-12-12 12:24:44 +01:00
|
|
|
if(inflateInit(&s->zstream) != Z_OK)
|
2010-01-11 14:06:54 +01:00
|
|
|
goto fail;
|
2004-12-12 12:24:44 +01:00
|
|
|
|
|
|
|
s->current_chunk = s->n_chunks;
|
2007-09-17 10:09:54 +02:00
|
|
|
|
2004-12-12 12:24:44 +01:00
|
|
|
return 0;
|
2010-01-11 14:06:54 +01:00
|
|
|
fail:
|
|
|
|
return -1;
|
2004-12-12 12:24:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int is_sector_in_chunk(BDRVDMGState* s,
|
|
|
|
uint32_t chunk_num,int sector_num)
|
|
|
|
{
|
|
|
|
if(chunk_num>=s->n_chunks || s->sectors[chunk_num]>sector_num ||
|
|
|
|
s->sectors[chunk_num]+s->sectorcounts[chunk_num]<=sector_num)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t search_chunk(BDRVDMGState* s,int sector_num)
|
|
|
|
{
|
|
|
|
/* binary search */
|
|
|
|
uint32_t chunk1=0,chunk2=s->n_chunks,chunk3;
|
|
|
|
while(chunk1!=chunk2) {
|
|
|
|
chunk3 = (chunk1+chunk2)/2;
|
|
|
|
if(s->sectors[chunk3]>sector_num)
|
|
|
|
chunk2 = chunk3;
|
|
|
|
else if(s->sectors[chunk3]+s->sectorcounts[chunk3]>sector_num)
|
|
|
|
return chunk3;
|
|
|
|
else
|
|
|
|
chunk1 = chunk3;
|
|
|
|
}
|
|
|
|
return s->n_chunks; /* error */
|
|
|
|
}
|
|
|
|
|
2010-05-12 16:31:49 +02:00
|
|
|
static inline int dmg_read_chunk(BlockDriverState *bs, int sector_num)
|
2004-12-12 12:24:44 +01:00
|
|
|
{
|
2010-05-12 16:31:49 +02:00
|
|
|
BDRVDMGState *s = bs->opaque;
|
|
|
|
|
2004-12-12 12:24:44 +01:00
|
|
|
if(!is_sector_in_chunk(s,s->current_chunk,sector_num)) {
|
|
|
|
int ret;
|
|
|
|
uint32_t chunk = search_chunk(s,sector_num);
|
|
|
|
|
|
|
|
if(chunk>=s->n_chunks)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
s->current_chunk = s->n_chunks;
|
|
|
|
switch(s->types[chunk]) {
|
|
|
|
case 0x80000005: { /* zlib compressed */
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* we need to buffer, because only the chunk as whole can be
|
|
|
|
* inflated. */
|
|
|
|
i=0;
|
|
|
|
do {
|
2010-05-12 16:31:49 +02:00
|
|
|
ret = bdrv_pread(bs->file, s->offsets[chunk] + i,
|
|
|
|
s->compressed_chunk+i, s->lengths[chunk]-i);
|
2004-12-12 12:24:44 +01:00
|
|
|
if(ret<0 && errno==EINTR)
|
|
|
|
ret=0;
|
|
|
|
i+=ret;
|
|
|
|
} while(ret>=0 && ret+i<s->lengths[chunk]);
|
|
|
|
|
|
|
|
if (ret != s->lengths[chunk])
|
|
|
|
return -1;
|
2007-09-16 23:08:06 +02:00
|
|
|
|
2004-12-12 12:24:44 +01:00
|
|
|
s->zstream.next_in = s->compressed_chunk;
|
|
|
|
s->zstream.avail_in = s->lengths[chunk];
|
|
|
|
s->zstream.next_out = s->uncompressed_chunk;
|
|
|
|
s->zstream.avail_out = 512*s->sectorcounts[chunk];
|
|
|
|
ret = inflateReset(&s->zstream);
|
|
|
|
if(ret != Z_OK)
|
|
|
|
return -1;
|
|
|
|
ret = inflate(&s->zstream, Z_FINISH);
|
|
|
|
if(ret != Z_STREAM_END || s->zstream.total_out != 512*s->sectorcounts[chunk])
|
|
|
|
return -1;
|
|
|
|
break; }
|
|
|
|
case 1: /* copy */
|
2010-05-12 16:31:49 +02:00
|
|
|
ret = bdrv_pread(bs->file, s->offsets[chunk],
|
|
|
|
s->uncompressed_chunk, s->lengths[chunk]);
|
2004-12-12 12:24:44 +01:00
|
|
|
if (ret != s->lengths[chunk])
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
case 2: /* zero */
|
|
|
|
memset(s->uncompressed_chunk, 0, 512*s->sectorcounts[chunk]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s->current_chunk = chunk;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-16 23:08:06 +02:00
|
|
|
static int dmg_read(BlockDriverState *bs, int64_t sector_num,
|
2004-12-12 12:24:44 +01:00
|
|
|
uint8_t *buf, int nb_sectors)
|
|
|
|
{
|
|
|
|
BDRVDMGState *s = bs->opaque;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0;i<nb_sectors;i++) {
|
|
|
|
uint32_t sector_offset_in_chunk;
|
2010-05-12 16:31:49 +02:00
|
|
|
if(dmg_read_chunk(bs, sector_num+i) != 0)
|
2004-12-12 12:24:44 +01:00
|
|
|
return -1;
|
|
|
|
sector_offset_in_chunk = sector_num+i-s->sectors[s->current_chunk];
|
|
|
|
memcpy(buf+i*512,s->uncompressed_chunk+sector_offset_in_chunk*512,512);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dmg_close(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
BDRVDMGState *s = bs->opaque;
|
|
|
|
if(s->n_chunks>0) {
|
|
|
|
free(s->types);
|
|
|
|
free(s->offsets);
|
|
|
|
free(s->lengths);
|
|
|
|
free(s->sectors);
|
|
|
|
free(s->sectorcounts);
|
|
|
|
}
|
|
|
|
free(s->compressed_chunk);
|
|
|
|
free(s->uncompressed_chunk);
|
|
|
|
inflateEnd(&s->zstream);
|
|
|
|
}
|
|
|
|
|
2009-05-10 00:03:42 +02:00
|
|
|
static BlockDriver bdrv_dmg = {
|
2009-03-07 23:00:29 +01:00
|
|
|
.format_name = "dmg",
|
|
|
|
.instance_size = sizeof(BDRVDMGState),
|
|
|
|
.bdrv_probe = dmg_probe,
|
2010-05-12 16:31:49 +02:00
|
|
|
.bdrv_open = dmg_open,
|
2009-03-07 23:00:29 +01:00
|
|
|
.bdrv_read = dmg_read,
|
|
|
|
.bdrv_close = dmg_close,
|
2004-12-12 12:24:44 +01:00
|
|
|
};
|
2009-05-10 00:03:42 +02:00
|
|
|
|
|
|
|
static void bdrv_dmg_init(void)
|
|
|
|
{
|
|
|
|
bdrv_register(&bdrv_dmg);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_dmg_init);
|