VHDX.cpp revision c5397a397db4649847120db7615898569cc41669
/* $Id$ */
/** @file
* VHDX - VHDX Disk image, Core Code.
*/
/*
* Copyright (C) 2012-2015 Oracle Corporation
*
* 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 (GPL) 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_VD_VHDX
#include <VBox/vd-plugin.h>
#include "VDBackends.h"
/*******************************************************************************
* On disk data structures *
*******************************************************************************/
/**
* VHDX file type identifier.
*/
#pragma pack(1)
typedef struct VhdxFileIdentifier
{
/** Signature. */
/** Creator ID - UTF-16 string (not neccessarily null terminated). */
#pragma pack()
/** Pointer to an on disk VHDX file type identifier. */
typedef VhdxFileIdentifier *PVhdxFileIdentifier;
/** VHDX file type identifier signature ("vhdxfile"). */
/** Start offset of the VHDX file type identifier. */
#define VHDX_FILE_IDENTIFIER_OFFSET UINT64_C(0)
/**
* VHDX header.
*/
#pragma pack(1)
typedef struct VhdxHeader
{
/** Signature. */
/** Checksum. */
/** Sequence number. */
/** File write UUID. */
/** Data write UUID. */
/** Log UUID. */
/** Version of the log format. */
/** VHDX format version.. */
/** Length of the log region. */
/** Start offset of the log offset in the file. */
/** Reserved bytes. */
} VhdxHeader;
#pragma pack()
/** Pointer to an on disk VHDX header. */
typedef VhdxHeader *PVhdxHeader;
/** VHDX header signature ("head"). */
/** Start offset of the first VHDX header. */
#define VHDX_HEADER1_OFFSET _64K
/** Start offset of the second VHDX header. */
#define VHDX_HEADER2_OFFSET _128K
/** Current Log format version. */
#define VHDX_HEADER_LOG_VERSION UINT16_C(0)
/** Current VHDX format version. */
/**
* VHDX region table header
*/
#pragma pack(1)
typedef struct VhdxRegionTblHdr
{
/** Signature. */
/** Checksum. */
/** Number of region table entries following this header. */
/** Reserved. */
#pragma pack()
/** Pointer to an on disk VHDX region table header. */
typedef VhdxRegionTblHdr *PVhdxRegionTblHdr;
/** VHDX region table header signature. */
/** Maximum number of entries which can follow. */
/** Offset where the region table is stored (192 KB). */
/** Maximum size of the region table. */
#define VHDX_REGION_TBL_SIZE_MAX _64K
/**
* VHDX region table entry.
*/
#pragma pack(1)
typedef struct VhdxRegionTblEntry
{
/** Object UUID. */
/** File offset of the region. */
/** Length of the region in bytes. */
/** Flags for this object. */
#pragma pack()
/** Pointer to an on disk VHDX region table entry. */
typedef struct VhdxRegionTblEntry *PVhdxRegionTblEntry;
/** Flag whether this region is required. */
#define VHDX_REGION_TBL_ENTRY_FLAGS_IS_REQUIRED RT_BIT_32(0)
/** UUID for the BAT region. */
#define VHDX_REGION_TBL_ENTRY_UUID_BAT "2dc27766-f623-4200-9d64-115e9bfd4a08"
/** UUID for the metadata region. */
#define VHDX_REGION_TBL_ENTRY_UUID_METADATA "8b7ca206-4790-4b9a-b8fe-575f050f886e"
/**
* VHDX Log entry header.
*/
#pragma pack(1)
typedef struct VhdxLogEntryHdr
{
/** Signature. */
/** Checksum. */
/** Total length of the entry in bytes. */
/** Tail of the log entries. */
/** Sequence number. */
/** Number of descriptors in this log entry. */
/** Reserved. */
/** Log UUID. */
/** VHDX file size in bytes while the log entry was written. */
/** File size in bytes all allocated file structures fit into when the
* log entry was written. */
#pragma pack()
/** Pointer to an on disk VHDX log entry header. */
typedef struct VhdxLogEntryHdr *PVhdxLogEntryHdr;
/** VHDX log entry signature ("loge"). */
/**
* VHDX log zero descriptor.
*/
#pragma pack(1)
typedef struct VhdxLogZeroDesc
{
/** Signature of this descriptor. */
/** Reserved. */
/** Length of the section to zero. */
/** File offset to write zeros to. */
/** Sequence number (must macht the field in the log entry header). */
#pragma pack()
/** Pointer to an on disk VHDX log zero descriptor. */
typedef struct VhdxLogZeroDesc *PVhdxLogZeroDesc;
/** Signature of a VHDX log zero descriptor ("zero"). */
/**
* VHDX log data descriptor.
*/
#pragma pack(1)
typedef struct VhdxLogDataDesc
{
/** Signature of this descriptor. */
/** Trailing 4 bytes removed from the update. */
/** Leading 8 bytes removed from the update. */
/** File offset to write zeros to. */
/** Sequence number (must macht the field in the log entry header). */
#pragma pack()
/** Pointer to an on disk VHDX log data descriptor. */
typedef struct VhdxLogDataDesc *PVhdxLogDataDesc;
/** Signature of a VHDX log data descriptor ("desc"). */
/**
* VHDX log data sector.
*/
#pragma pack(1)
typedef struct VhdxLogDataSector
{
/** Signature of the data sector. */
/** 4 most significant bytes of the sequence number. */
/** Raw data associated with the update. */
/** 4 least significant bytes of the sequence number. */
#pragma pack()
/** Pointer to an on disk VHDX log data sector. */
typedef VhdxLogDataSector *PVhdxLogDataSector;
/** Signature of a VHDX log data sector ("data"). */
/**
* VHDX BAT entry.
*/
#pragma pack(1)
typedef struct VhdxBatEntry
{
/** The BAT entry, contains state and offset. */
} VhdxBatEntry;
#pragma pack()
typedef VhdxBatEntry *PVhdxBatEntry;
/** Return the BAT state from a given entry. */
/** Get the FileOffsetMB field from a given BAT entry. */
/** Get a byte offset from the BAT entry. */
#define VHDX_BAT_ENTRY_GET_FILE_OFFSET(bat) (VHDX_BAT_ENTRY_GET_FILE_OFFSET_MB(bat) * (uint64_t)_1M)
/** Block not present and the data is undefined. */
#define VHDX_BAT_ENTRY_PAYLOAD_BLOCK_NOT_PRESENT (0)
/** Data in this block is undefined. */
#define VHDX_BAT_ENTRY_PAYLOAD_BLOCK_UNDEFINED (1)
/** Data in this block contains zeros. */
#define VHDX_BAT_ENTRY_PAYLOAD_BLOCK_ZERO (2)
/** Block was unmapped by the application or system and data is either zero or
* the data before the block was unmapped. */
#define VHDX_BAT_ENTRY_PAYLOAD_BLOCK_UNMAPPED (3)
/** Block data is in the file pointed to by the FileOffsetMB field. */
#define VHDX_BAT_ENTRY_PAYLOAD_BLOCK_FULLY_PRESENT (6)
/** Block is partially present, use sector bitmap to get present sectors. */
#define VHDX_BAT_ENTRY_PAYLOAD_BLOCK_PARTIALLY_PRESENT (7)
/** The sector bitmap block is undefined and not allocated in the file. */
#define VHDX_BAT_ENTRY_SB_BLOCK_NOT_PRESENT (0)
/** The sector bitmap block is defined at the file location. */
#define VHDX_BAT_ENTRY_SB_BLOCK_PRESENT (6)
/**
* VHDX Metadata tabl header.
*/
#pragma pack(1)
typedef struct VhdxMetadataTblHdr
{
/** Signature. */
/** Reserved. */
/** Number of entries in the table. */
/** Reserved */
#pragma pack()
/** Pointer to an on disk metadata table header. */
typedef VhdxMetadataTblHdr *PVhdxMetadataTblHdr;
/** Signature of a VHDX metadata table header ("metadata"). */
/** Maximum number of entries the metadata table can have. */
/**
* VHDX Metadata table entry.
*/
#pragma pack(1)
typedef struct VhdxMetadataTblEntry
{
/** Item UUID. */
/** Offset of the metadata item. */
/** Length of the metadata item. */
/** Flags for the metadata item. */
/** Reserved. */
#pragma pack()
/** Pointer to an on disk metadata table entry. */
typedef VhdxMetadataTblEntry *PVhdxMetadataTblEntry;
/** FLag whether the metadata item is system or user metadata. */
#define VHDX_METADATA_TBL_ENTRY_FLAGS_IS_USER RT_BIT_32(0)
/** FLag whether the metadata item is file or virtual disk metadata. */
/** FLag whether the backend must understand the metadata item to load the image. */
/** File parameters item UUID. */
#define VHDX_METADATA_TBL_ENTRY_ITEM_FILE_PARAMS "caa16737-fa36-4d43-b3b6-33f0aa44e76b"
/** Virtual disk size item UUID. */
#define VHDX_METADATA_TBL_ENTRY_ITEM_VDISK_SIZE "2fa54224-cd1b-4876-b211-5dbed83bf4b8"
/** Page 83 UUID. */
#define VHDX_METADATA_TBL_ENTRY_ITEM_PAGE83_DATA "beca12ab-b2e6-4523-93ef-c309e000c746"
/** Logical sector size UUID. */
#define VHDX_METADATA_TBL_ENTRY_ITEM_LOG_SECT_SIZE "8141bf1d-a96f-4709-ba47-f233a8faab5f"
/** Physical sector size UUID. */
#define VHDX_METADATA_TBL_ENTRY_ITEM_PHYS_SECT_SIZE "cda348c7-445d-4471-9cc9-e9885251c556"
/** Parent locator UUID. */
#define VHDX_METADATA_TBL_ENTRY_ITEM_PARENT_LOCATOR "a8d35f2d-b30b-454d-abf7-d3d84834ab0c"
/**
* VHDX File parameters metadata item.
*/
#pragma pack(1)
typedef struct VhdxFileParameters
{
/** Block size. */
/** Flags. */
#pragma pack()
/** Pointer to an on disk VHDX file parameters metadata item. */
typedef struct VhdxFileParameters *PVhdxFileParameters;
/** Flag whether to leave blocks allocated in the file or if it is possible to unmap them. */
/** Flag whether this file has a parent VHDX file. */
/**
* VHDX virtual disk size metadata item.
*/
#pragma pack(1)
typedef struct VhdxVDiskSize
{
/** Virtual disk size. */
#pragma pack()
/** Pointer to an on disk VHDX virtual disk size metadata item. */
typedef struct VhdxVDiskSize *PVhdxVDiskSize;
/**
* VHDX page 83 data metadata item.
*/
#pragma pack(1)
typedef struct VhdxPage83Data
{
/** UUID for the SCSI device. */
#pragma pack()
/** Pointer to an on disk VHDX vpage 83 data metadata item. */
typedef struct VhdxPage83Data *PVhdxPage83Data;
/**
* VHDX virtual disk logical sector size.
*/
#pragma pack(1)
typedef struct VhdxVDiskLogicalSectorSize
{
/** Logical sector size. */
#pragma pack()
/** Pointer to an on disk VHDX virtual disk logical sector size metadata item. */
typedef struct VhdxVDiskLogicalSectorSize *PVhdxVDiskLogicalSectorSize;
/**
* VHDX virtual disk physical sector size.
*/
#pragma pack(1)
typedef struct VhdxVDiskPhysicalSectorSize
{
/** Physical sector size. */
#pragma pack()
/** Pointer to an on disk VHDX virtual disk physical sector size metadata item. */
typedef struct VhdxVDiskPhysicalSectorSize *PVhdxVDiskPhysicalSectorSize;
/**
* VHDX parent locator header.
*/
#pragma pack(1)
typedef struct VhdxParentLocatorHeader
{
/** Locator type UUID. */
/** Reserved. */
/** Number of key value pairs. */
#pragma pack()
/** Pointer to an on disk VHDX parent locator header metadata item. */
typedef struct VhdxParentLocatorHeader *PVhdxParentLocatorHeader;
/** VHDX parent locator type. */
#define VHDX_PARENT_LOCATOR_TYPE_VHDX "b04aefb7-d19e-4a81-b789-25b8e9445913"
/**
* VHDX parent locator entry.
*/
#pragma pack(1)
typedef struct VhdxParentLocatorEntry
{
/** Offset of the key. */
/** Offset of the value. */
/** Length of the key. */
/** Length of the value. */
#pragma pack()
/** Pointer to an on disk VHDX parent locator entry. */
typedef struct VhdxParentLocatorEntry *PVhdxParentLocatorEntry;
/*******************************************************************************
* Constants And Macros, Structures and Typedefs *
*******************************************************************************/
typedef enum VHDXMETADATAITEM
{
VHDXMETADATAITEM_32BIT_HACK = 0x7fffffff
/**
* Table to validate the metadata item UUIDs and the flags.
*/
typedef struct VHDXMETADATAITEMPROPS
{
/** Item UUID. */
const char *pszItemUuid;
/** Flag whether this is a user or system metadata item. */
bool fIsUser;
/** Flag whether this is a virtual disk or file metadata item. */
bool fIsVDisk;
/** Flag whether this metadata item is required to load the file. */
bool fIsRequired;
/** Metadata item enum associated with this UUID. */
/**
* VHDX image data structure.
*/
typedef struct VHDXIMAGE
{
/** Image name. */
const char *pszFilename;
/** Storage handle. */
/** Pointer to the per-disk VD interface list. */
/** Pointer to the per-image VD interface list. */
/** Error interface. */
/** I/O interface. */
/** Open flags passed by VBoxHD layer. */
unsigned uOpenFlags;
/** Image flags defined during creation or determined during open. */
unsigned uImageFlags;
/** Version of the VHDX image format. */
unsigned uVersion;
/** Total size of the image. */
/** Logical sector size of the image. */
/** Block size of the image. */
/** Physical geometry of this image. */
/** Logical geometry of this image. */
/** The BAT. */
/** Chunk ratio. */
} VHDXIMAGE, *PVHDXIMAGE;
/**
* Endianess conversion direction.
*/
typedef enum VHDXECONV
{
/** Host to file endianess. */
VHDXECONV_H2F = 0,
/** File to host endianess. */
} VHDXECONV;
/** Macros for endianess conversion. */
/*******************************************************************************
* Static Variables *
*******************************************************************************/
/**
* NULL-terminated array of supported file extensions.
*/
static const VDFILEEXTENSION s_aVhdxFileExtensions[] =
{
{"vhdx", VDTYPE_HDD},
};
/**
* Static table to verify the metadata item properties and the flags.
*/
static const VHDXMETADATAITEMPROPS s_aVhdxMetadataItemProps[] =
{
/* pcszItemUuid fIsUser, fIsVDisk, fIsRequired, enmMetadataItem */
{VHDX_METADATA_TBL_ENTRY_ITEM_FILE_PARAMS, false, false, true, VHDXMETADATAITEM_FILE_PARAMS},
{VHDX_METADATA_TBL_ENTRY_ITEM_VDISK_SIZE, false, true, true, VHDXMETADATAITEM_VDISK_SIZE},
{VHDX_METADATA_TBL_ENTRY_ITEM_PAGE83_DATA, false, true, true, VHDXMETADATAITEM_PAGE83_DATA},
{VHDX_METADATA_TBL_ENTRY_ITEM_LOG_SECT_SIZE, false, true, true, VHDXMETADATAITEM_LOGICAL_SECTOR_SIZE},
{VHDX_METADATA_TBL_ENTRY_ITEM_PHYS_SECT_SIZE, false, true, true, VHDXMETADATAITEM_PHYSICAL_SECTOR_SIZE},
{VHDX_METADATA_TBL_ENTRY_ITEM_PARENT_LOCATOR, false, false, true, VHDXMETADATAITEM_PARENT_LOCATOR}
};
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
/**
* Converts the file identifier between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pFileIdentifierConv Where to store the converted file identifier.
* @param pFileIdentifier The file identifier to convert.
*
* @note It is safe to use the same pointer for pFileIdentifierConv and pFileIdentifier.
*/
DECLINLINE(void) vhdxConvFileIdentifierEndianess(VHDXECONV enmConv, PVhdxFileIdentifier pFileIdentifierConv,
{
}
/**
* Converts a UUID between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pUuidConv Where to store the converted UUID.
* @param pUuid The UUID to convert.
*
* @note It is safe to use the same pointer for pUuidConv and pUuid.
*/
{
#if 1
#else
#endif
}
/**
* Converts a VHDX header between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pHdrConv Where to store the converted header.
* @param pHdr The VHDX header to convert.
*
* @note It is safe to use the same pointer for pHdrConv and pHdr.
*/
{
}
/**
* Converts a VHDX region table header between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pRegTblHdrConv Where to store the converted header.
* @param pRegTblHdr The VHDX region table header to convert.
*
* @note It is safe to use the same pointer for pRegTblHdrConv and pRegTblHdr.
*/
{
}
/**
* Converts a VHDX region table entry between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pRegTblEntConv Where to store the converted region table entry.
* @param pRegTblEnt The VHDX region table entry to convert.
*
* @note It is safe to use the same pointer for pRegTblEntConv and pRegTblEnt.
*/
DECLINLINE(void) vhdxConvRegionTblEntryEndianess(VHDXECONV enmConv, PVhdxRegionTblEntry pRegTblEntConv,
{
}
/**
* Converts a VHDX log entry header between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pLogEntryHdrConv Where to store the converted log entry header.
* @param pLogEntryHdr The VHDX log entry header to convert.
*
* @note It is safe to use the same pointer for pLogEntryHdrConv and pLogEntryHdr.
*/
{
}
/**
* Converts a VHDX log zero descriptor between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pLogZeroDescConv Where to store the converted log zero descriptor.
* @param pLogZeroDesc The VHDX log zero descriptor to convert.
*
* @note It is safe to use the same pointer for pLogZeroDescConv and pLogZeroDesc.
*/
{
}
/**
* Converts a VHDX log data descriptor between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pLogDataDescConv Where to store the converted log data descriptor.
* @param pLogDataDesc The VHDX log data descriptor to convert.
*
* @note It is safe to use the same pointer for pLogDataDescConv and pLogDataDesc.
*/
{
}
/**
* Converts a VHDX log data sector between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pLogDataSectorConv Where to store the converted log data sector.
* @param pLogDataSector The VHDX log data sector to convert.
*
* @note It is safe to use the same pointer for pLogDataSectorConv and pLogDataSector.
*/
DECLINLINE(void) vhdxConvLogDataSectorEndianess(VHDXECONV enmConv, PVhdxLogDataSector pLogDataSectorConv,
{
}
/**
* Converts a BAT between file and host endianess.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param paBatEntriesConv Where to store the converted BAT.
* @param paBatEntries The VHDX BAT to convert.
*
* @note It is safe to use the same pointer for paBatEntriesConv and paBatEntries.
*/
{
for (uint32_t i = 0; i < cBatEntries; i++)
}
/**
* Converts a VHDX metadata table header between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pMetadataTblHdrConv Where to store the converted metadata table header.
* @param pMetadataTblHdr The VHDX metadata table header to convert.
*
* @note It is safe to use the same pointer for pMetadataTblHdrConv and pMetadataTblHdr.
*/
DECLINLINE(void) vhdxConvMetadataTblHdrEndianess(VHDXECONV enmConv, PVhdxMetadataTblHdr pMetadataTblHdrConv,
{
}
/**
* Converts a VHDX metadata table entry between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pMetadataTblEntryConv Where to store the converted metadata table entry.
* @param pMetadataTblEntry The VHDX metadata table entry to convert.
*
* @note It is safe to use the same pointer for pMetadataTblEntryConv and pMetadataTblEntry.
*/
DECLINLINE(void) vhdxConvMetadataTblEntryEndianess(VHDXECONV enmConv, PVhdxMetadataTblEntry pMetadataTblEntryConv,
{
}
/**
* Converts a VHDX file parameters item between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pFileParamsConv Where to store the converted file parameters item entry.
* @param pFileParams The VHDX file parameters item to convert.
*
* @note It is safe to use the same pointer for pFileParamsConv and pFileParams.
*/
DECLINLINE(void) vhdxConvFileParamsEndianess(VHDXECONV enmConv, PVhdxFileParameters pFileParamsConv,
{
}
/**
* Converts a VHDX virtual disk size item between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pVDiskSizeConv Where to store the converted virtual disk size item entry.
* @param pVDiskSize The VHDX virtual disk size item to convert.
*
* @note It is safe to use the same pointer for pVDiskSizeConv and pVDiskSize.
*/
{
}
/**
* Converts a VHDX page 83 data item between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pPage83DataConv Where to store the converted page 83 data item entry.
* @param pPage83Data The VHDX page 83 data item to convert.
*
* @note It is safe to use the same pointer for pPage83DataConv and pPage83Data.
*/
{
}
/**
* Converts a VHDX logical sector size item between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pVDiskLogSectSizeConv Where to store the converted logical sector size item entry.
* @param pVDiskLogSectSize The VHDX logical sector size item to convert.
*
* @note It is safe to use the same pointer for pVDiskLogSectSizeConv and pVDiskLogSectSize.
*/
DECLINLINE(void) vhdxConvVDiskLogSectSizeEndianess(VHDXECONV enmConv, PVhdxVDiskLogicalSectorSize pVDiskLogSectSizeConv,
{
pVDiskLogSectSizeConv->u32LogicalSectorSize = SET_ENDIAN_U32(pVDiskLogSectSize->u32LogicalSectorSize);
}
/**
* Converts a VHDX physical sector size item between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pVDiskPhysSectSizeConv Where to store the converted physical sector size item entry.
* @param pVDiskPhysSectSize The VHDX physical sector size item to convert.
*
* @note It is safe to use the same pointer for pVDiskPhysSectSizeConv and pVDiskPhysSectSize.
*/
DECLINLINE(void) vhdxConvVDiskPhysSectSizeEndianess(VHDXECONV enmConv, PVhdxVDiskPhysicalSectorSize pVDiskPhysSectSizeConv,
{
pVDiskPhysSectSizeConv->u64PhysicalSectorSize = SET_ENDIAN_U64(pVDiskPhysSectSize->u64PhysicalSectorSize);
}
/**
* Converts a VHDX parent locator header item between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pParentLocatorHdrConv Where to store the converted parent locator header item entry.
* @param pParentLocatorHdr The VHDX parent locator header item to convert.
*
* @note It is safe to use the same pointer for pParentLocatorHdrConv and pParentLocatorHdr.
*/
DECLINLINE(void) vhdxConvParentLocatorHeaderEndianness(VHDXECONV enmConv, PVhdxParentLocatorHeader pParentLocatorHdrConv,
{
vhdxConvUuidEndianess(enmConv, &pParentLocatorHdrConv->UuidLocatorType, &pParentLocatorHdr->UuidLocatorType);
}
/**
* Converts a VHDX parent locator entry between file and host endianness.
*
* @returns nothing.
* @param enmConv Direction of the conversion.
* @param pParentLocatorEntryConv Where to store the converted parent locator entry.
* @param pParentLocatorEntry The VHDX parent locator entry to convert.
*
* @note It is safe to use the same pointer for pParentLocatorEntryConv and pParentLocatorEntry.
*/
DECLINLINE(void) vhdxConvParentLocatorEntryEndianess(VHDXECONV enmConv, PVhdxParentLocatorEntry pParentLocatorEntryConv,
{
}
/**
* Internal. Free all allocated space for representing an image except pImage,
* and optionally delete the image from disk.
*/
{
int rc = VINF_SUCCESS;
/* Freeing a never allocated image (e.g. because the open failed) is
* not signalled as an error. After all nothing bad happens. */
if (pImage)
{
{
}
{
}
}
return rc;
}
/**
* Loads all required fields from the given VHDX header.
* The header must be converted to the host endianess and validated already.
*
* @returns VBox status code.
* @param pImage Image instance data.
* @param pHdr The header to load.
*/
{
int rc = VINF_SUCCESS;
/*
* Most fields in the header are not required because the backend implements
* readonly access only so far.
* We just have to check that the log is empty, we have to refuse to load the
* image otherwsie because replaying the log is not implemented.
*/
{
/* Check that the log UUID is zero. */
"VHDX: Image \'%s\' has a non empty log which is not supported",
}
else
"VHDX: Image \'%s\' uses an unsupported version (%u) of the VHDX format",
return rc;
}
/**
* Determines the current header and loads it.
*
* @returns VBox status code.
* @param pImage Image instance data.
*/
{
uint32_t u32ChkSumSaved = 0;
bool fHdr1Valid = false;
bool fHdr2Valid = false;
int rc = VINF_SUCCESS;
/*
* The VHDX format defines two headers at different offsets to provide failure
* consistency. Only one header is current. This can be determined using the
* sequence number and checksum fields in the header.
*/
{
/* Read the first header. */
if (RT_SUCCESS(rc))
{
/* Validate checksum. */
pHdr1->u32Checksum = 0;
&& u32ChkSum == u32ChkSumSaved)
fHdr1Valid = true;
}
/* Try to read the second header in any case (even if reading the first failed). */
if (RT_SUCCESS(rc))
{
/* Validate checksum. */
pHdr2->u32Checksum = 0;
&& u32ChkSum == u32ChkSumSaved)
fHdr2Valid = true;
}
/* Determine the current header. */
if (fHdr1Valid != fHdr2Valid)
{
/* Only one header is valid - use it. */
}
else if (!fHdr1Valid && !fHdr2Valid)
{
/* Crap, both headers are corrupt, refuse to load the image. */
"VHDX: Can not load the image because both headers are corrupt");
}
else
{
/* Both headers are valid. Use the sequence number to find the current one. */
else
}
}
else
"VHDX: Out of memory while allocating memory for the header");
if (pHdr1)
if (pHdr2)
return rc;
}
/**
* Loads the BAT region.
*
* @returns VBox status code.
* @param pImage Image instance data.
* @param offRegion Start offset of the region.
* @param cbRegion Size of the region.
*/
{
int rc = VINF_SUCCESS;
/* Calculate required values first. */
cDataBlocks++;
if (cDataBlocks % uChunkRatio)
if (cbBatEntries <= cbRegion)
{
/*
* Load the complete BAT region first, convert to host endianess and process
* it afterwards. The SB entries can be removed because they are not needed yet.
*/
if (paBatEntries)
{
if (RT_SUCCESS(rc))
{
/* Go through the table and validate it. */
for (unsigned i = 0; i < cBatEntries; i++)
{
if ( i != 0
&& (i % uChunkRatio) == 0)
{
/**
* Disabled the verification because there are images out there with the sector bitmap
* marked as present. The entry is never accessed and the image is readonly anyway,
* so no harm done.
*/
#if 0
/* Sector bitmap block. */
{
"VHDX: Sector bitmap block at entry %u of image \'%s\' marked as present, violation of the specification",
i, pImage->pszFilename);
break;
}
#endif
}
else
{
/* Payload block. */
{
"VHDX: Payload block at entry %u of image \'%s\' marked as partially present, violation of the specification",
i, pImage->pszFilename);
break;
}
}
}
if (RT_SUCCESS(rc))
{
}
}
else
"VHDX: Error reading the BAT from image \'%s\'",
}
else
"VHDX: Out of memory allocating memory for %u BAT entries of image \'%s\'",
}
else
"VHDX: Mismatch between calculated number of BAT entries and region size (expected %u got %u) for image \'%s\'",
if ( RT_FAILURE(rc)
&& paBatEntries)
return rc;
}
/**
* Load the file parameters metadata item from the file.
*
* @returns VBox status code.
* @param pImage Image instance data.
* @param offItem File offset where the data is stored.
* @param cbItem Size of the item in the file.
*/
{
int rc = VINF_SUCCESS;
if (cbItem != sizeof(VhdxFileParameters))
"VHDX: File parameters item size mismatch (expected %u got %zu) in image \'%s\'",
else
{
&FileParameters, sizeof(FileParameters));
if (RT_SUCCESS(rc))
{
/* @todo: No support for differencing images yet. */
"VHDX: Image \'%s\' is a differencing image which is not supported yet",
}
else
"VHDX: Reading the file parameters metadata item from image \'%s\' failed",
}
return rc;
}
/**
* Load the virtual disk size metadata item from the file.
*
* @returns VBox status code.
* @param pImage Image instance data.
* @param offItem File offset where the data is stored.
* @param cbItem Size of the item in the file.
*/
{
int rc = VINF_SUCCESS;
if (cbItem != sizeof(VhdxVDiskSize))
"VHDX: Virtual disk size item size mismatch (expected %u got %zu) in image \'%s\'",
else
{
if (RT_SUCCESS(rc))
{
}
else
"VHDX: Reading the virtual disk size metadata item from image \'%s\' failed",
}
return rc;
}
/**
* Load the logical sector size metadata item from the file.
*
* @returns VBox status code.
* @param pImage Image instance data.
* @param offItem File offset where the data is stored.
* @param cbItem Size of the item in the file.
*/
{
int rc = VINF_SUCCESS;
if (cbItem != sizeof(VhdxVDiskLogicalSectorSize))
"VHDX: Virtual disk logical sector size item size mismatch (expected %u got %zu) in image \'%s\'",
else
{
&VDiskLogSectSize, sizeof(VDiskLogSectSize));
if (RT_SUCCESS(rc))
{
}
else
"VHDX: Reading the virtual disk logical sector size metadata item from image \'%s\' failed",
}
return rc;
}
/**
* Loads the metadata region.
*
* @returns VBox status code.
* @param pImage Image instance data.
* @param offRegion Start offset of the region.
* @param cbRegion Size of the region.
*/
{
int rc = VINF_SUCCESS;
/* Load the header first. */
&MetadataTblHdr, sizeof(MetadataTblHdr));
if (RT_SUCCESS(rc))
{
/* Validate structure. */
"VHDX: Incorrect metadata table header signature for image \'%s\'",
"VHDX: Incorrect entry count in metadata table header of image \'%s\'",
else if (cbRegion < (MetadataTblHdr.u16EntryCount * sizeof(VhdxMetadataTblEntry) + sizeof(VhdxMetadataTblHdr)))
"VHDX: Metadata table of image \'%s\' exceeds region size",
if (RT_SUCCESS(rc))
{
for (unsigned i = 0; i < MetadataTblHdr.u16EntryCount; i++)
{
uint64_t offMetadataItem = 0;
&MetadataTblEntry, sizeof(MetadataTblEntry));
if (RT_FAILURE(rc))
{
"VHDX: Reading metadata table entry from image \'%s\' failed",
break;
}
/* Check whether the flags match the expectations. */
{
{
/*
* Check for specification violations and bail out, except
* for the required flag of the physical sector size metadata item.
* Early images had the required flag not set opposed to the specification.
* We don't want to brerak those images.
*/
"VHDX: User flag of metadata item does not meet expectations \'%s\'",
"VHDX: Virtual disk flag of metadata item does not meet expectations \'%s\'",
"VHDX: Required flag of metadata item does not meet expectations \'%s\'",
else
break;
}
}
if (RT_FAILURE(rc))
break;
switch (enmMetadataItem)
{
{
break;
}
{
break;
}
{
/*
* Nothing to do here for now (marked as required but
* there is no API to pass this information to the caller)
* so far.
*/
break;
}
{
break;
}
{
/*
* Nothing to do here for now (marked as required but
* there is no API to pass this information to the caller)
* so far.
*/
break;
}
{
"VHDX: Image \'%s\' is a differencing image which is not supported yet",
break;
}
case VHDXMETADATAITEM_UNKNOWN:
default:
"VHDX: Unsupported but required metadata item in image \'%s\'",
}
if (RT_FAILURE(rc))
break;
offMetadataTblEntry += sizeof(MetadataTblEntry);
}
}
}
else
"VHDX: Reading the metadata table header for image \'%s\' failed",
return rc;
}
/**
* Loads the region table and the associated regions.
*
* @returns VBox status code.
* @param pImage Image instance data.
*/
{
int rc = VINF_SUCCESS;
/* Load the complete region table into memory. */
if (pbRegionTbl)
{
if (RT_SUCCESS(rc))
{
uint32_t u32ChkSumSaved = 0;
/*
* Copy the region table header to a dedicated structure where we can
* convert it to host endianess.
*/
/* Set checksum field to 0 during crc computation. */
pRegionTblHdr->u32Checksum = 0;
/* Verify the region table integrity. */
"VHDX: Invalid signature for region table header of image \'%s\'",
"VHDX: CRC32 checksum mismatch for the region table of image \'%s\' (expected %#x got %#x)",
"VHDX: Invalid entry count field in the region table header of image \'%s\'",
if (RT_SUCCESS(rc))
{
/* Parse the region table entries. */
bool fBatRegPresent = false;
for (unsigned i = 0; i < RegionTblHdr.u32EntryCount; i++)
{
/* Check the uuid for known regions. */
{
/*
* Save the BAT region and process it later.
* It may come before the metadata region but needs the block size.
*/
{
fBatRegPresent = true;
}
else
"VHDX: BAT region not marked as required in image \'%s\'",
}
{
else
"VHDX: Metadata region not marked as required in image \'%s\'",
}
{
/* The region is not known but marked as required, fail to load the image. */
"VHDX: Unknown required region in image \'%s\'",
}
if (RT_FAILURE(rc))
break;
pRegTblEntry++;
}
if (fBatRegPresent)
else
"VHDX: BAT region in image \'%s\' is missing",
}
}
else
"VHDX: Reading the region table for image \'%s\' failed",
}
else
"VHDX: Out of memory allocating memory for the region table of image \'%s\'",
if (pbRegionTbl)
return rc;
}
/**
* Internal: Open an image, constructing all necessary data structures.
*/
{
int rc = VINF_SUCCESS;
/* Refuse write access, it is not implemented so far. */
if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
return VERR_NOT_SUPPORTED;
/*
* Open the image.
*/
false /* fCreate */),
/* Do NOT signal an appropriate error here, as the VD layer has the
* choice of retrying the open if it failed. */
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
if (cbFile > sizeof(FileIdentifier))
{
&FileIdentifier, sizeof(FileIdentifier));
if (RT_SUCCESS(rc))
{
else
/* Load the region table. */
if (RT_SUCCESS(rc))
}
}
else
}
if (RT_FAILURE(rc))
vhdxFreeImage(pImage, false);
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
{
LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
int rc = VINF_SUCCESS;
if ( !VALID_PTR(pszFilename)
|| !*pszFilename)
else
{
/*
* Open the file and read the file identifier.
*/
false /* fCreate */),
&pStorage);
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
if (cbFile > sizeof(FileIdentifier))
{
&FileIdentifier, sizeof(FileIdentifier));
if (RT_SUCCESS(rc))
{
else
*penmType = VDTYPE_HDD;
}
}
else
}
if (pStorage)
}
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnOpen */
{
LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
int rc;
/* Check open flags. All valid flags are supported. */
if ( uOpenFlags & ~VD_OPEN_FLAGS_MASK
|| !VALID_PTR(pszFilename)
|| !*pszFilename)
else
{
if (!pImage)
rc = VERR_NO_MEMORY;
else
{
if (RT_SUCCESS(rc))
*ppBackendData = pImage;
else
}
}
return rc;
}
/** @interface_method_impl{VBOXHDDBACKEND,pfnCreate} */
unsigned uImageFlags, const char *pszComment,
unsigned uPercentStart, unsigned uPercentSpan,
void **ppBackendData)
{
LogFlowFunc(("pszFilename=\"%s\" cbSize=%llu uImageFlags=%#x pszComment=\"%s\" pPCHSGeometry=%#p pLCHSGeometry=%#p Uuid=%RTuuid uOpenFlags=%#x uPercentStart=%u uPercentSpan=%u pVDIfsDisk=%#p pVDIfsImage=%#p pVDIfsOperation=%#p enmType=%u ppBackendData=%#p",
pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
int rc = VERR_NOT_SUPPORTED;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnRename */
{
int rc = VINF_SUCCESS;
/* Check arguments. */
if ( !pImage
|| !pszFilename
|| !*pszFilename)
else
{
/* Close the image. */
if (RT_SUCCESS(rc))
{
/* Rename the file. */
if (RT_FAILURE(rc))
{
/* The move failed, try to reopen the original image. */
if (RT_FAILURE(rc2))
}
else
{
/* Update pImage with the new information. */
/* Open the old image with new name. */
}
}
}
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnClose */
{
int rc;
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnRead */
{
LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
int rc = VINF_SUCCESS;
|| cbToRead == 0)
else
{
uint32_t idxBat = (uint32_t)(uOffset / pImage->cbBlock); Assert(idxBat == uOffset / pImage->cbBlock);
switch (VHDX_BAT_ENTRY_GET_STATE(uBatEntry))
{
{
break;
}
{
break;
}
default:
break;
}
if (pcbActuallyRead)
}
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnWrite */
{
LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
int rc;
|| cbToWrite == 0)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnFlush */
{
int rc;
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
static unsigned vhdxGetVersion(void *pBackendData)
{
if (pImage)
else
return 0;
}
/** @copydoc VBOXHDDBACKEND::pfnGetSectorSize */
{
return cb;
}
/** @copydoc VBOXHDDBACKEND::pfnGetSize */
{
return cb;
}
/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
{
if (pImage)
{
{
if (RT_SUCCESS(rc))
}
}
return cb;
}
/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
static int vhdxGetPCHSGeometry(void *pBackendData,
{
int rc;
if (pImage)
{
{
rc = VINF_SUCCESS;
}
else
}
else
LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
static int vhdxSetPCHSGeometry(void *pBackendData,
{
LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
int rc = VINF_SUCCESS;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
static int vhdxGetLCHSGeometry(void *pBackendData,
{
int rc = VINF_SUCCESS;
if (pImage)
{
else
}
else
LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
static int vhdxSetLCHSGeometry(void *pBackendData,
{
LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
int rc = VINF_SUCCESS;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
static unsigned vhdxGetImageFlags(void *pBackendData)
{
unsigned uImageFlags;
if (pImage)
else
uImageFlags = 0;
return uImageFlags;
}
/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
static unsigned vhdxGetOpenFlags(void *pBackendData)
{
unsigned uOpenFlags;
if (pImage)
else
uOpenFlags = 0;
return uOpenFlags;
}
/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
{
int rc = VINF_SUCCESS;
/* Image must be opened and the new flags must be valid. */
if (!pImage || (uOpenFlags & ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
else
{
/* Implement this operation via reopening the image. */
if (RT_SUCCESS(rc))
}
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetComment */
{
LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetComment */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
{
int rc;
if (pImage)
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
{
int rc;
if (pImage)
{
else
}
else
return rc;
}
/** @copydoc VBOXHDDBACKEND::pfnDump */
static void vhdxDump(void *pBackendData)
{
if (pImage)
{
}
}
const VBOXHDDBACKEND g_VhdxBackend =
{
/* pszBackendName */
"VHDX",
/* cbSize */
sizeof(VBOXHDDBACKEND),
/* uBackendCaps */
/* paFileExtensions */
/* paConfigInfo */
NULL,
/* pfnCheckIfValid */
/* pfnOpen */
/* pfnCreate */
/* pfnRename */
/* pfnClose */
/* pfnRead */
/* pfnWrite */
/* pfnFlush */
/* pfnDiscard */
NULL,
/* pfnGetVersion */
/* pfnGetSectorSize */
/* pfnGetSize */
/* pfnGetFileSize */
/* pfnGetPCHSGeometry */
/* pfnSetPCHSGeometry */
/* pfnGetLCHSGeometry */
/* pfnSetLCHSGeometry */
/* pfnGetImageFlags */
/* pfnGetOpenFlags */
/* pfnSetOpenFlags */
/* pfnGetComment */
/* pfnSetComment */
/* pfnGetUuid */
/* pfnSetUuid */
/* pfnGetModificationUuid */
/* pfnSetModificationUuid */
/* pfnGetParentUuid */
/* pfnSetParentUuid */
/* pfnGetParentModificationUuid */
/* pfnSetParentModificationUuid */
/* pfnDump */
/* pfnGetTimeStamp */
NULL,
/* pfnGetParentTimeStamp */
NULL,
/* pfnSetParentTimeStamp */
NULL,
/* pfnGetParentFilename */
NULL,
/* pfnSetParentFilename */
NULL,
/* pfnComposeLocation */
/* pfnComposeName */
/* pfnCompact */
NULL,
/* pfnResize */
NULL,
/* pfnRepair */
NULL,
/* pfnTraverseMetadata */
};