VDICore.h revision a0240ff4f7663045c848fdbc192ea3d4d9f70a11
/** $Id$ */
/** @file
* Virtual Disk Image (VDI), Core Code Header (internal).
*/
/*
* Copyright (C) 2006 InnoTek Systemberatung GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License as published by the Free Software Foundation,
* in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
* distribution. VirtualBox OSE is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY of any kind.
*
* If you received this file as part of a commercial VirtualBox
* distribution, then only the terms of your commercial VirtualBox
* license agreement apply instead of the previous paragraph.
*/
#ifndef __VDICore_h__
/*******************************************************************************
* Header Files *
*******************************************************************************/
/*******************************************************************************
* Constants And Macros, Structures and Typedefs *
*******************************************************************************/
/** Image info, not handled anyhow.
* Must be less than 64 bytes in length, including the trailing 0.
*/
#define VDI_IMAGE_FILE_INFO "<<< InnoTek VirtualBox Disk Image >>>\n"
/** The Sector size.
* Currently we support only 512 bytes sectors.
*/
#define VDI_GEOMETRY_SECTOR_SIZE (512)
/** 512 = 2^^9 */
#define VDI_GEOMETRY_SECTOR_SHIFT (9)
/**
* Harddisk geometry.
*/
#pragma pack(1)
typedef struct VDIDISKGEOMETRY
{
/** Cylinders. */
/** Heads. */
/** Sectors per track. */
/** Sector size. (bytes per sector) */
#pragma pack()
/** Image signature. */
#define VDI_IMAGE_SIGNATURE (0xbeda107f)
/**
* Pre-Header to be stored in image file - used for version control.
*/
#pragma pack(1)
typedef struct VDIPREHEADER
{
/** Just text info about image type, for eyes only. */
char szFileInfo[64];
/** The image signature (VDI_IMAGE_SIGNATURE). */
/** The image version (VDI_IMAGE_VERSION). */
#pragma pack()
/**
* Size of szComment field of HDD image header.
*/
#define VDI_IMAGE_COMMENT_SIZE 256
/**
* Header to be stored in image file, VDI_IMAGE_VERSION_MAJOR = 0.
* Prepended by VDIPREHEADER.
*/
#pragma pack(1)
typedef struct VDIHEADER0
{
/** The image type (VDI_IMAGE_TYPE_*). */
/** Image flags (VDI_IMAGE_FLAGS_*). */
/** Image comment. (UTF-8) */
char szComment[VDI_IMAGE_COMMENT_SIZE];
/** Image geometry. */
/** Size of disk (in bytes). */
/** Block size. (For instance VDI_IMAGE_BLOCK_SIZE.) */
/** Number of blocks. */
/** Number of allocated blocks. */
/** UUID of image. */
/** UUID of image's last modification. */
/** Only for secondary images - UUID of primary image. */
} VDIHEADER0, *PVDIHEADER0;
#pragma pack()
/**
* Header to be stored in image file, VDI_IMAGE_VERSION_MAJOR = 1.
* Prepended by VDIPREHEADER.
*/
#pragma pack(1)
typedef struct VDIHEADER1
{
/** Size of this structure in bytes. */
/** The image type (VDI_IMAGE_TYPE_*). */
/** Image flags (VDI_IMAGE_FLAGS_*). */
/** Image comment. (UTF-8) */
char szComment[VDI_IMAGE_COMMENT_SIZE];
/** Offset of Blocks array from the begining of image file.
* Should be sector-aligned for HDD access optimization. */
/** Offset of image data from the begining of image file.
* Should be sector-aligned for HDD access optimization. */
/** Image geometry. */
/** BIOS HDD translation mode, see PDMBIOSTRANSLATION. */
/** Size of disk (in bytes). */
/** Block size. (For instance VDI_IMAGE_BLOCK_SIZE.) Should be a power of 2! */
/** Size of additional service information of every data block.
* Prepended before block data. May be 0.
* Should be a power of 2 and sector-aligned for optimization reasons. */
/** Number of blocks. */
/** Number of allocated blocks. */
/** UUID of image. */
/** UUID of image's last modification. */
/** Only for secondary images - UUID of previous image. */
/** Only for secondary images - UUID of previous image's last modification. */
} VDIHEADER1, *PVDIHEADER1;
#pragma pack()
/**
* Header structure for all versions.
*/
typedef struct VDIHEADER
{
unsigned uVersion;
union
{
} u;
} VDIHEADER, *PVDIHEADER;
/** Block 'pointer'. */
typedef uint32_t VDIIMAGEBLOCKPOINTER;
/** Pointer to a block 'pointer'. */
typedef VDIIMAGEBLOCKPOINTER *PVDIIMAGEBLOCKPOINTER;
/**
* Block marked as free is not allocated in image file, read from this
* block may returns any random data.
*/
#define VDI_IMAGE_BLOCK_FREE ((VDIIMAGEBLOCKPOINTER)~0)
/**
* Block marked as zero is not allocated in image file, read from this
* block returns zeroes.
*/
/**
* Block 'pointer' >= VDI_IMAGE_BLOCK_UNALLOCATED indicates block is not
* allocated in image file.
*/
/*******************************************************************************
* Internal Functions for header access *
*******************************************************************************/
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
}
AssertFailed();
return (VDIIMAGETYPE)0;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
}
AssertFailed();
return 0;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
}
AssertFailed();
return NULL;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
case 0: return (sizeof(VDIPREHEADER) + sizeof(VDIHEADER0));
}
AssertFailed();
return 0;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
case 0: return sizeof(VDIPREHEADER) + sizeof(VDIHEADER0) + \
}
AssertFailed();
return 0;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
}
AssertFailed();
return NULL;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
case 0: return PDMBIOSTRANSLATION_AUTO;
}
AssertFailed();
return PDMBIOSTRANSLATION_NONE;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
case 0: return;
}
AssertFailed();
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
}
AssertFailed();
return 0;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
}
AssertFailed();
return 0;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
case 0: return 0;
}
AssertFailed();
return 0;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
}
AssertFailed();
return 0;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
}
AssertFailed();
return 0;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
}
AssertFailed();
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
}
AssertFailed();
return NULL;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
}
AssertFailed();
return NULL;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
}
AssertFailed();
return NULL;
}
{
switch (GET_MAJOR_HEADER_VERSION(ph))
{
}
AssertFailed();
return NULL;
}
/**
* Default image block size, may be changed by setBlockSize/getBlockSize.
*
* Note: for speed reasons block size should be a power of 2 !
*/
#define VDI_IMAGE_DEFAULT_BLOCK_SIZE _1M
/**
* fModified bit flags.
*/
#define VDI_IMAGE_MODIFIED_FLAG BIT(0)
/**
* Image structure
*/
typedef struct VDIIMAGEDESC
{
/** Link to parent image descriptor, if any. */
struct VDIIMAGEDESC *pPrev;
/** Link to child image descriptor, if any. */
struct VDIIMAGEDESC *pNext;
/** File handle. */
/** True if the image is operating in readonly mode. */
bool fReadOnly;
/** Image open flags, VDI_OPEN_FLAGS_*. */
unsigned fOpen;
/** Image pre-header. */
/** Image header. */
/** Pointer to a block array. */
/** fFlags copy from image header, for speed optimization. */
unsigned fFlags;
/** Start offset of block array in image file, here for speed optimization. */
unsigned offStartBlocks;
/** Start offset of data in image file, here for speed optimization. */
unsigned offStartData;
/** Block mask for getting the offset into a block from a byte hdd offset. */
unsigned uBlockMask;
/** Block shift value for converting byte hdd offset into paBlock index. */
unsigned uShiftOffset2Index;
/** Block shift value for converting block index into offset in image. */
unsigned uShiftIndex2Offset;
/** Offset of data from the beginning of block. */
unsigned offStartBlockData;
/** Image is modified flags (VDI_IMAGE_MODIFIED*). */
unsigned fModified;
/** Container filename. (UTF-8)
* @todo Make this variable length to save a bunch of bytes. (low prio) */
char szFilename[RTPATH_MAX];
/**
* Default work buffer size, may be changed by setBufferSize() method.
*
* For best speed performance it must be equal to image block size.
*/
/** VDIDISK Signature. */
#define VDIDISK_SIGNATURE (0xbedafeda)
/**
* VBox HDD Container main structure, private part.
*/
struct VDIDISK
{
/** Structure signature (VDIDISK_SIGNATURE). */
/** Number of opened images. */
unsigned cImages;
/** Base image. */
/** Last opened image in the chain.
* The same as pBase if only one image is used or the last opened diff image. */
/** Default block size for newly created images. */
unsigned cbBlock;
/** Working buffer size, allocated only while committing data,
* copying block from primary image to secondary and saving previously
* zero block. Buffer deallocated after operation complete.
* @remark For best performance buffer size must be equal to image's
* block size, however it may be decreased for memory saving.
*/
unsigned cbBuf;
/** Flag whether zero writes should be handled normally or optimized
* away if possible. */
bool fHonorZeroWrites;
/** The media interface. */
/** Pointer to the driver instance. */
};
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
#endif