[12639] | 1 | /* $Id: VDICore.h 66486 2017-04-10 07:23:59Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[1565] | 3 | * Virtual Disk Image (VDI), Core Code Header (internal).
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[62482] | 7 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
[1] | 8 | *
|
---|
| 9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
[5999] | 12 | * General Public License (GPL) as published by the Free Software
|
---|
| 13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
[1] | 16 | */
|
---|
| 17 |
|
---|
[35346] | 18 | #ifndef ___VDICore_h___
|
---|
[1565] | 19 |
|
---|
| 20 |
|
---|
[1] | 21 | /*******************************************************************************
|
---|
| 22 | * Header Files *
|
---|
| 23 | *******************************************************************************/
|
---|
[33567] | 24 | #include <VBox/vd.h>
|
---|
[1] | 25 | #include <VBox/err.h>
|
---|
| 26 |
|
---|
| 27 | #include <VBox/log.h>
|
---|
| 28 | #include <iprt/alloc.h>
|
---|
| 29 | #include <iprt/assert.h>
|
---|
| 30 | #include <iprt/uuid.h>
|
---|
[150] | 31 | #include <iprt/string.h>
|
---|
[745] | 32 | #include <iprt/asm.h>
|
---|
[1] | 33 |
|
---|
| 34 |
|
---|
| 35 | /*******************************************************************************
|
---|
| 36 | * Constants And Macros, Structures and Typedefs *
|
---|
| 37 | *******************************************************************************/
|
---|
[2358] | 38 |
|
---|
| 39 | /** Image info, not handled anyhow.
|
---|
| 40 | * Must be less than 64 bytes in length, including the trailing 0.
|
---|
| 41 | */
|
---|
[32388] | 42 | #define VDI_IMAGE_FILE_INFO "<<< Oracle VM VirtualBox Disk Image >>>\n"
|
---|
[2358] | 43 |
|
---|
[1] | 44 | /** The Sector size.
|
---|
| 45 | * Currently we support only 512 bytes sectors.
|
---|
| 46 | */
|
---|
| 47 | #define VDI_GEOMETRY_SECTOR_SIZE (512)
|
---|
| 48 | /** 512 = 2^^9 */
|
---|
| 49 | #define VDI_GEOMETRY_SECTOR_SHIFT (9)
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * Harddisk geometry.
|
---|
| 53 | */
|
---|
| 54 | #pragma pack(1)
|
---|
| 55 | typedef struct VDIDISKGEOMETRY
|
---|
| 56 | {
|
---|
| 57 | /** Cylinders. */
|
---|
| 58 | uint32_t cCylinders;
|
---|
| 59 | /** Heads. */
|
---|
| 60 | uint32_t cHeads;
|
---|
| 61 | /** Sectors per track. */
|
---|
| 62 | uint32_t cSectors;
|
---|
| 63 | /** Sector size. (bytes per sector) */
|
---|
| 64 | uint32_t cbSector;
|
---|
| 65 | } VDIDISKGEOMETRY, *PVDIDISKGEOMETRY;
|
---|
| 66 | #pragma pack()
|
---|
| 67 |
|
---|
| 68 | /** Image signature. */
|
---|
| 69 | #define VDI_IMAGE_SIGNATURE (0xbeda107f)
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * Pre-Header to be stored in image file - used for version control.
|
---|
| 73 | */
|
---|
| 74 | #pragma pack(1)
|
---|
| 75 | typedef struct VDIPREHEADER
|
---|
| 76 | {
|
---|
| 77 | /** Just text info about image type, for eyes only. */
|
---|
| 78 | char szFileInfo[64];
|
---|
| 79 | /** The image signature (VDI_IMAGE_SIGNATURE). */
|
---|
| 80 | uint32_t u32Signature;
|
---|
| 81 | /** The image version (VDI_IMAGE_VERSION). */
|
---|
| 82 | uint32_t u32Version;
|
---|
| 83 | } VDIPREHEADER, *PVDIPREHEADER;
|
---|
| 84 | #pragma pack()
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * Size of szComment field of HDD image header.
|
---|
| 88 | */
|
---|
| 89 | #define VDI_IMAGE_COMMENT_SIZE 256
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * Header to be stored in image file, VDI_IMAGE_VERSION_MAJOR = 0.
|
---|
| 93 | * Prepended by VDIPREHEADER.
|
---|
| 94 | */
|
---|
| 95 | #pragma pack(1)
|
---|
| 96 | typedef struct VDIHEADER0
|
---|
| 97 | {
|
---|
| 98 | /** The image type (VDI_IMAGE_TYPE_*). */
|
---|
| 99 | uint32_t u32Type;
|
---|
| 100 | /** Image flags (VDI_IMAGE_FLAGS_*). */
|
---|
| 101 | uint32_t fFlags;
|
---|
| 102 | /** Image comment. (UTF-8) */
|
---|
| 103 | char szComment[VDI_IMAGE_COMMENT_SIZE];
|
---|
[6363] | 104 | /** Legacy image geometry (previous code stored PCHS there). */
|
---|
| 105 | VDIDISKGEOMETRY LegacyGeometry;
|
---|
[1] | 106 | /** Size of disk (in bytes). */
|
---|
| 107 | uint64_t cbDisk;
|
---|
| 108 | /** Block size. (For instance VDI_IMAGE_BLOCK_SIZE.) */
|
---|
| 109 | uint32_t cbBlock;
|
---|
| 110 | /** Number of blocks. */
|
---|
| 111 | uint32_t cBlocks;
|
---|
| 112 | /** Number of allocated blocks. */
|
---|
| 113 | uint32_t cBlocksAllocated;
|
---|
| 114 | /** UUID of image. */
|
---|
| 115 | RTUUID uuidCreate;
|
---|
| 116 | /** UUID of image's last modification. */
|
---|
| 117 | RTUUID uuidModify;
|
---|
| 118 | /** Only for secondary images - UUID of primary image. */
|
---|
| 119 | RTUUID uuidLinkage;
|
---|
| 120 | } VDIHEADER0, *PVDIHEADER0;
|
---|
| 121 | #pragma pack()
|
---|
| 122 |
|
---|
| 123 | /**
|
---|
[6363] | 124 | * Header to be stored in image file, VDI_IMAGE_VERSION_MAJOR = 1,
|
---|
| 125 | * VDI_IMAGE_VERSION_MINOR = 1. Prepended by VDIPREHEADER.
|
---|
[1] | 126 | */
|
---|
| 127 | #pragma pack(1)
|
---|
| 128 | typedef struct VDIHEADER1
|
---|
| 129 | {
|
---|
| 130 | /** Size of this structure in bytes. */
|
---|
| 131 | uint32_t cbHeader;
|
---|
| 132 | /** The image type (VDI_IMAGE_TYPE_*). */
|
---|
| 133 | uint32_t u32Type;
|
---|
| 134 | /** Image flags (VDI_IMAGE_FLAGS_*). */
|
---|
| 135 | uint32_t fFlags;
|
---|
| 136 | /** Image comment. (UTF-8) */
|
---|
| 137 | char szComment[VDI_IMAGE_COMMENT_SIZE];
|
---|
[33540] | 138 | /** Offset of Blocks array from the beginning of image file.
|
---|
[1] | 139 | * Should be sector-aligned for HDD access optimization. */
|
---|
| 140 | uint32_t offBlocks;
|
---|
[33540] | 141 | /** Offset of image data from the beginning of image file.
|
---|
[1] | 142 | * Should be sector-aligned for HDD access optimization. */
|
---|
| 143 | uint32_t offData;
|
---|
[6363] | 144 | /** Legacy image geometry (previous code stored PCHS there). */
|
---|
| 145 | VDIDISKGEOMETRY LegacyGeometry;
|
---|
[6291] | 146 | /** Was BIOS HDD translation mode, now unused. */
|
---|
| 147 | uint32_t u32Dummy;
|
---|
[1] | 148 | /** Size of disk (in bytes). */
|
---|
| 149 | uint64_t cbDisk;
|
---|
| 150 | /** Block size. (For instance VDI_IMAGE_BLOCK_SIZE.) Should be a power of 2! */
|
---|
| 151 | uint32_t cbBlock;
|
---|
| 152 | /** Size of additional service information of every data block.
|
---|
| 153 | * Prepended before block data. May be 0.
|
---|
| 154 | * Should be a power of 2 and sector-aligned for optimization reasons. */
|
---|
| 155 | uint32_t cbBlockExtra;
|
---|
| 156 | /** Number of blocks. */
|
---|
| 157 | uint32_t cBlocks;
|
---|
| 158 | /** Number of allocated blocks. */
|
---|
| 159 | uint32_t cBlocksAllocated;
|
---|
| 160 | /** UUID of image. */
|
---|
| 161 | RTUUID uuidCreate;
|
---|
| 162 | /** UUID of image's last modification. */
|
---|
| 163 | RTUUID uuidModify;
|
---|
| 164 | /** Only for secondary images - UUID of previous image. */
|
---|
| 165 | RTUUID uuidLinkage;
|
---|
| 166 | /** Only for secondary images - UUID of previous image's last modification. */
|
---|
| 167 | RTUUID uuidParentModify;
|
---|
| 168 | } VDIHEADER1, *PVDIHEADER1;
|
---|
| 169 | #pragma pack()
|
---|
| 170 |
|
---|
| 171 | /**
|
---|
[6363] | 172 | * Header to be stored in image file, VDI_IMAGE_VERSION_MAJOR = 1,
|
---|
| 173 | * VDI_IMAGE_VERSION_MINOR = 1, the slightly changed variant necessary as the
|
---|
| 174 | * old released code doesn't support changing the minor version at all.
|
---|
| 175 | */
|
---|
| 176 | #pragma pack(1)
|
---|
| 177 | typedef struct VDIHEADER1PLUS
|
---|
| 178 | {
|
---|
| 179 | /** Size of this structure in bytes. */
|
---|
| 180 | uint32_t cbHeader;
|
---|
| 181 | /** The image type (VDI_IMAGE_TYPE_*). */
|
---|
| 182 | uint32_t u32Type;
|
---|
| 183 | /** Image flags (VDI_IMAGE_FLAGS_*). */
|
---|
| 184 | uint32_t fFlags;
|
---|
| 185 | /** Image comment. (UTF-8) */
|
---|
| 186 | char szComment[VDI_IMAGE_COMMENT_SIZE];
|
---|
[33540] | 187 | /** Offset of blocks array from the beginning of image file.
|
---|
[6363] | 188 | * Should be sector-aligned for HDD access optimization. */
|
---|
| 189 | uint32_t offBlocks;
|
---|
[33540] | 190 | /** Offset of image data from the beginning of image file.
|
---|
[6363] | 191 | * Should be sector-aligned for HDD access optimization. */
|
---|
| 192 | uint32_t offData;
|
---|
| 193 | /** Legacy image geometry (previous code stored PCHS there). */
|
---|
| 194 | VDIDISKGEOMETRY LegacyGeometry;
|
---|
| 195 | /** Was BIOS HDD translation mode, now unused. */
|
---|
| 196 | uint32_t u32Dummy;
|
---|
| 197 | /** Size of disk (in bytes). */
|
---|
| 198 | uint64_t cbDisk;
|
---|
| 199 | /** Block size. (For instance VDI_IMAGE_BLOCK_SIZE.) Should be a power of 2! */
|
---|
| 200 | uint32_t cbBlock;
|
---|
| 201 | /** Size of additional service information of every data block.
|
---|
| 202 | * Prepended before block data. May be 0.
|
---|
| 203 | * Should be a power of 2 and sector-aligned for optimization reasons. */
|
---|
| 204 | uint32_t cbBlockExtra;
|
---|
| 205 | /** Number of blocks. */
|
---|
| 206 | uint32_t cBlocks;
|
---|
| 207 | /** Number of allocated blocks. */
|
---|
| 208 | uint32_t cBlocksAllocated;
|
---|
| 209 | /** UUID of image. */
|
---|
| 210 | RTUUID uuidCreate;
|
---|
| 211 | /** UUID of image's last modification. */
|
---|
| 212 | RTUUID uuidModify;
|
---|
| 213 | /** Only for secondary images - UUID of previous image. */
|
---|
| 214 | RTUUID uuidLinkage;
|
---|
| 215 | /** Only for secondary images - UUID of previous image's last modification. */
|
---|
| 216 | RTUUID uuidParentModify;
|
---|
| 217 | /** LCHS image geometry (new field in VDI1.2 version. */
|
---|
| 218 | VDIDISKGEOMETRY LCHSGeometry;
|
---|
| 219 | } VDIHEADER1PLUS, *PVDIHEADER1PLUS;
|
---|
| 220 | #pragma pack()
|
---|
| 221 |
|
---|
| 222 | /**
|
---|
[1] | 223 | * Header structure for all versions.
|
---|
| 224 | */
|
---|
| 225 | typedef struct VDIHEADER
|
---|
| 226 | {
|
---|
| 227 | unsigned uVersion;
|
---|
| 228 | union
|
---|
| 229 | {
|
---|
| 230 | VDIHEADER0 v0;
|
---|
| 231 | VDIHEADER1 v1;
|
---|
[6363] | 232 | VDIHEADER1PLUS v1plus;
|
---|
[1] | 233 | } u;
|
---|
| 234 | } VDIHEADER, *PVDIHEADER;
|
---|
| 235 |
|
---|
[32536] | 236 | /**
|
---|
| 237 | * File alignment boundary for both the block array and data area. Should be
|
---|
| 238 | * at least the size of a physical sector on disk for performance reasons.
|
---|
[43916] | 239 | * Bumped to 1MB because SSDs tend to have 8kb per page so we don't have to worry
|
---|
| 240 | * about proper alignment in the near future again. */
|
---|
| 241 | #define VDI_DATA_ALIGN _1M
|
---|
[32536] | 242 |
|
---|
[1] | 243 | /** Block 'pointer'. */
|
---|
| 244 | typedef uint32_t VDIIMAGEBLOCKPOINTER;
|
---|
| 245 | /** Pointer to a block 'pointer'. */
|
---|
| 246 | typedef VDIIMAGEBLOCKPOINTER *PVDIIMAGEBLOCKPOINTER;
|
---|
| 247 |
|
---|
| 248 | /**
|
---|
| 249 | * Block marked as free is not allocated in image file, read from this
|
---|
| 250 | * block may returns any random data.
|
---|
| 251 | */
|
---|
| 252 | #define VDI_IMAGE_BLOCK_FREE ((VDIIMAGEBLOCKPOINTER)~0)
|
---|
| 253 |
|
---|
| 254 | /**
|
---|
| 255 | * Block marked as zero is not allocated in image file, read from this
|
---|
| 256 | * block returns zeroes.
|
---|
| 257 | */
|
---|
| 258 | #define VDI_IMAGE_BLOCK_ZERO ((VDIIMAGEBLOCKPOINTER)~1)
|
---|
| 259 |
|
---|
| 260 | /**
|
---|
| 261 | * Block 'pointer' >= VDI_IMAGE_BLOCK_UNALLOCATED indicates block is not
|
---|
| 262 | * allocated in image file.
|
---|
| 263 | */
|
---|
| 264 | #define VDI_IMAGE_BLOCK_UNALLOCATED (VDI_IMAGE_BLOCK_ZERO)
|
---|
| 265 | #define IS_VDI_IMAGE_BLOCK_ALLOCATED(bp) (bp < VDI_IMAGE_BLOCK_UNALLOCATED)
|
---|
| 266 |
|
---|
| 267 | #define GET_MAJOR_HEADER_VERSION(ph) (VDI_GET_VERSION_MAJOR((ph)->uVersion))
|
---|
| 268 | #define GET_MINOR_HEADER_VERSION(ph) (VDI_GET_VERSION_MINOR((ph)->uVersion))
|
---|
| 269 |
|
---|
[7152] | 270 | /** @name VDI image types
|
---|
| 271 | * @{ */
|
---|
| 272 | typedef enum VDIIMAGETYPE
|
---|
| 273 | {
|
---|
| 274 | /** Normal dynamically growing base image file. */
|
---|
| 275 | VDI_IMAGE_TYPE_NORMAL = 1,
|
---|
| 276 | /** Preallocated base image file of a fixed size. */
|
---|
| 277 | VDI_IMAGE_TYPE_FIXED,
|
---|
| 278 | /** Dynamically growing image file for undo/commit changes support. */
|
---|
| 279 | VDI_IMAGE_TYPE_UNDO,
|
---|
| 280 | /** Dynamically growing image file for differencing support. */
|
---|
| 281 | VDI_IMAGE_TYPE_DIFF,
|
---|
[1565] | 282 |
|
---|
[7152] | 283 | /** First valid image type value. */
|
---|
| 284 | VDI_IMAGE_TYPE_FIRST = VDI_IMAGE_TYPE_NORMAL,
|
---|
| 285 | /** Last valid image type value. */
|
---|
| 286 | VDI_IMAGE_TYPE_LAST = VDI_IMAGE_TYPE_DIFF
|
---|
| 287 | } VDIIMAGETYPE;
|
---|
| 288 | /** Pointer to VDI image type. */
|
---|
| 289 | typedef VDIIMAGETYPE *PVDIIMAGETYPE;
|
---|
| 290 | /** @} */
|
---|
| 291 |
|
---|
[1] | 292 | /*******************************************************************************
|
---|
| 293 | * Internal Functions for header access *
|
---|
| 294 | *******************************************************************************/
|
---|
[1565] | 295 | DECLINLINE(VDIIMAGETYPE) getImageType(PVDIHEADER ph)
|
---|
[1] | 296 | {
|
---|
| 297 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 298 | {
|
---|
| 299 | case 0: return (VDIIMAGETYPE)ph->u.v0.u32Type;
|
---|
| 300 | case 1: return (VDIIMAGETYPE)ph->u.v1.u32Type;
|
---|
| 301 | }
|
---|
| 302 | AssertFailed();
|
---|
| 303 | return (VDIIMAGETYPE)0;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[1565] | 306 | DECLINLINE(unsigned) getImageFlags(PVDIHEADER ph)
|
---|
[1] | 307 | {
|
---|
| 308 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 309 | {
|
---|
[10781] | 310 | case 0:
|
---|
| 311 | /* VDI image flag conversion to VD image flags. */
|
---|
| 312 | return ph->u.v0.fFlags << 8;
|
---|
| 313 | case 1:
|
---|
| 314 | /* VDI image flag conversion to VD image flags. */
|
---|
| 315 | return ph->u.v1.fFlags << 8;
|
---|
| 316 | }
|
---|
| 317 | AssertFailed();
|
---|
| 318 | return 0;
|
---|
| 319 | }
|
---|
[1] | 320 |
|
---|
[1565] | 321 | DECLINLINE(char *) getImageComment(PVDIHEADER ph)
|
---|
[1] | 322 | {
|
---|
| 323 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 324 | {
|
---|
| 325 | case 0: return &ph->u.v0.szComment[0];
|
---|
| 326 | case 1: return &ph->u.v1.szComment[0];
|
---|
| 327 | }
|
---|
| 328 | AssertFailed();
|
---|
| 329 | return NULL;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
[1565] | 332 | DECLINLINE(unsigned) getImageBlocksOffset(PVDIHEADER ph)
|
---|
[1] | 333 | {
|
---|
| 334 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 335 | {
|
---|
| 336 | case 0: return (sizeof(VDIPREHEADER) + sizeof(VDIHEADER0));
|
---|
| 337 | case 1: return ph->u.v1.offBlocks;
|
---|
| 338 | }
|
---|
| 339 | AssertFailed();
|
---|
| 340 | return 0;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
[31804] | 343 | DECLINLINE(uint32_t) getImageDataOffset(PVDIHEADER ph)
|
---|
[1] | 344 | {
|
---|
| 345 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 346 | {
|
---|
| 347 | case 0: return sizeof(VDIPREHEADER) + sizeof(VDIHEADER0) + \
|
---|
| 348 | (ph->u.v0.cBlocks * sizeof(VDIIMAGEBLOCKPOINTER));
|
---|
| 349 | case 1: return ph->u.v1.offData;
|
---|
| 350 | }
|
---|
| 351 | AssertFailed();
|
---|
| 352 | return 0;
|
---|
| 353 | }
|
---|
| 354 |
|
---|
[31804] | 355 | DECLINLINE(void) setImageDataOffset(PVDIHEADER ph, uint32_t offData)
|
---|
| 356 | {
|
---|
| 357 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 358 | {
|
---|
| 359 | case 0: return;
|
---|
[31810] | 360 | case 1: ph->u.v1.offData = offData; return;
|
---|
[31804] | 361 | }
|
---|
| 362 | AssertFailed();
|
---|
| 363 | }
|
---|
| 364 |
|
---|
[6291] | 365 | DECLINLINE(PVDIDISKGEOMETRY) getImageLCHSGeometry(PVDIHEADER ph)
|
---|
[1] | 366 | {
|
---|
| 367 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 368 | {
|
---|
[6363] | 369 | case 0: return NULL;
|
---|
| 370 | case 1:
|
---|
| 371 | switch (GET_MINOR_HEADER_VERSION(ph))
|
---|
| 372 | {
|
---|
| 373 | case 1:
|
---|
[6374] | 374 | if (ph->u.v1.cbHeader < sizeof(ph->u.v1plus))
|
---|
[6363] | 375 | return NULL;
|
---|
[6374] | 376 | else
|
---|
[6363] | 377 | return &ph->u.v1plus.LCHSGeometry;
|
---|
| 378 | }
|
---|
[1] | 379 | }
|
---|
| 380 | AssertFailed();
|
---|
| 381 | return NULL;
|
---|
| 382 | }
|
---|
| 383 |
|
---|
[1565] | 384 | DECLINLINE(uint64_t) getImageDiskSize(PVDIHEADER ph)
|
---|
[1] | 385 | {
|
---|
| 386 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 387 | {
|
---|
| 388 | case 0: return ph->u.v0.cbDisk;
|
---|
| 389 | case 1: return ph->u.v1.cbDisk;
|
---|
| 390 | }
|
---|
| 391 | AssertFailed();
|
---|
| 392 | return 0;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[31804] | 395 | DECLINLINE(void) setImageDiskSize(PVDIHEADER ph, uint64_t cbDisk)
|
---|
| 396 | {
|
---|
| 397 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 398 | {
|
---|
| 399 | case 0: ph->u.v0.cbDisk = cbDisk; return;
|
---|
| 400 | case 1: ph->u.v1.cbDisk = cbDisk; return;
|
---|
| 401 | }
|
---|
| 402 | AssertFailed();
|
---|
| 403 | }
|
---|
| 404 |
|
---|
[1565] | 405 | DECLINLINE(unsigned) getImageBlockSize(PVDIHEADER ph)
|
---|
[1] | 406 | {
|
---|
| 407 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 408 | {
|
---|
| 409 | case 0: return ph->u.v0.cbBlock;
|
---|
| 410 | case 1: return ph->u.v1.cbBlock;
|
---|
| 411 | }
|
---|
| 412 | AssertFailed();
|
---|
| 413 | return 0;
|
---|
| 414 | }
|
---|
| 415 |
|
---|
[1565] | 416 | DECLINLINE(unsigned) getImageExtraBlockSize(PVDIHEADER ph)
|
---|
[1] | 417 | {
|
---|
| 418 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 419 | {
|
---|
| 420 | case 0: return 0;
|
---|
| 421 | case 1: return ph->u.v1.cbBlockExtra;
|
---|
| 422 | }
|
---|
| 423 | AssertFailed();
|
---|
| 424 | return 0;
|
---|
| 425 | }
|
---|
| 426 |
|
---|
[1565] | 427 | DECLINLINE(unsigned) getImageBlocks(PVDIHEADER ph)
|
---|
[1] | 428 | {
|
---|
| 429 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 430 | {
|
---|
| 431 | case 0: return ph->u.v0.cBlocks;
|
---|
| 432 | case 1: return ph->u.v1.cBlocks;
|
---|
| 433 | }
|
---|
| 434 | AssertFailed();
|
---|
| 435 | return 0;
|
---|
| 436 | }
|
---|
| 437 |
|
---|
[31804] | 438 | DECLINLINE(void) setImageBlocks(PVDIHEADER ph, unsigned cBlocks)
|
---|
| 439 | {
|
---|
| 440 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 441 | {
|
---|
| 442 | case 0: ph->u.v0.cBlocks = cBlocks; return;
|
---|
| 443 | case 1: ph->u.v1.cBlocks = cBlocks; return;
|
---|
| 444 | }
|
---|
| 445 | AssertFailed();
|
---|
| 446 | }
|
---|
| 447 |
|
---|
| 448 |
|
---|
[1565] | 449 | DECLINLINE(unsigned) getImageBlocksAllocated(PVDIHEADER ph)
|
---|
[1] | 450 | {
|
---|
| 451 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 452 | {
|
---|
| 453 | case 0: return ph->u.v0.cBlocksAllocated;
|
---|
| 454 | case 1: return ph->u.v1.cBlocksAllocated;
|
---|
| 455 | }
|
---|
| 456 | AssertFailed();
|
---|
| 457 | return 0;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
[1565] | 460 | DECLINLINE(void) setImageBlocksAllocated(PVDIHEADER ph, unsigned cBlocks)
|
---|
[1] | 461 | {
|
---|
| 462 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 463 | {
|
---|
[139] | 464 | case 0: ph->u.v0.cBlocksAllocated = cBlocks; return;
|
---|
| 465 | case 1: ph->u.v1.cBlocksAllocated = cBlocks; return;
|
---|
[1] | 466 | }
|
---|
| 467 | AssertFailed();
|
---|
| 468 | }
|
---|
| 469 |
|
---|
[62740] | 470 | #ifdef _MSC_VER
|
---|
| 471 | # pragma warning(disable:4366) /* (harmless "misalignment") */
|
---|
| 472 | #endif
|
---|
| 473 |
|
---|
[1565] | 474 | DECLINLINE(PRTUUID) getImageCreationUUID(PVDIHEADER ph)
|
---|
[1] | 475 | {
|
---|
| 476 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 477 | {
|
---|
| 478 | case 0: return &ph->u.v0.uuidCreate;
|
---|
| 479 | case 1: return &ph->u.v1.uuidCreate;
|
---|
| 480 | }
|
---|
| 481 | AssertFailed();
|
---|
| 482 | return NULL;
|
---|
| 483 | }
|
---|
| 484 |
|
---|
[1565] | 485 | DECLINLINE(PRTUUID) getImageModificationUUID(PVDIHEADER ph)
|
---|
[1] | 486 | {
|
---|
| 487 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 488 | {
|
---|
| 489 | case 0: return &ph->u.v0.uuidModify;
|
---|
| 490 | case 1: return &ph->u.v1.uuidModify;
|
---|
| 491 | }
|
---|
| 492 | AssertFailed();
|
---|
| 493 | return NULL;
|
---|
| 494 | }
|
---|
| 495 |
|
---|
[1565] | 496 | DECLINLINE(PRTUUID) getImageParentUUID(PVDIHEADER ph)
|
---|
[1] | 497 | {
|
---|
| 498 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 499 | {
|
---|
| 500 | case 0: return &ph->u.v0.uuidLinkage;
|
---|
| 501 | case 1: return &ph->u.v1.uuidLinkage;
|
---|
| 502 | }
|
---|
| 503 | AssertFailed();
|
---|
| 504 | return NULL;
|
---|
| 505 | }
|
---|
| 506 |
|
---|
[1565] | 507 | DECLINLINE(PRTUUID) getImageParentModificationUUID(PVDIHEADER ph)
|
---|
[1] | 508 | {
|
---|
| 509 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
| 510 | {
|
---|
| 511 | case 1: return &ph->u.v1.uuidParentModify;
|
---|
| 512 | }
|
---|
| 513 | AssertFailed();
|
---|
| 514 | return NULL;
|
---|
| 515 | }
|
---|
| 516 |
|
---|
[62740] | 517 | #ifdef _MSC_VER
|
---|
| 518 | # pragma warning(default:4366)
|
---|
| 519 | #endif
|
---|
| 520 |
|
---|
[1] | 521 | /**
|
---|
| 522 | * Image structure
|
---|
| 523 | */
|
---|
| 524 | typedef struct VDIIMAGEDESC
|
---|
| 525 | {
|
---|
[22966] | 526 | /** Opaque storage handle. */
|
---|
[27808] | 527 | PVDIOSTORAGE pStorage;
|
---|
[32536] | 528 | /** Image open flags, VD_OPEN_FLAGS_*. */
|
---|
[7152] | 529 | unsigned uOpenFlags;
|
---|
[1] | 530 | /** Image pre-header. */
|
---|
| 531 | VDIPREHEADER PreHeader;
|
---|
| 532 | /** Image header. */
|
---|
| 533 | VDIHEADER Header;
|
---|
| 534 | /** Pointer to a block array. */
|
---|
| 535 | PVDIIMAGEBLOCKPOINTER paBlocks;
|
---|
[38621] | 536 | /** Pointer to the block array for back resolving (used if discarding is enabled). */
|
---|
| 537 | unsigned *paBlocksRev;
|
---|
[1] | 538 | /** fFlags copy from image header, for speed optimization. */
|
---|
[7152] | 539 | unsigned uImageFlags;
|
---|
[1] | 540 | /** Start offset of block array in image file, here for speed optimization. */
|
---|
| 541 | unsigned offStartBlocks;
|
---|
| 542 | /** Start offset of data in image file, here for speed optimization. */
|
---|
| 543 | unsigned offStartData;
|
---|
| 544 | /** Block mask for getting the offset into a block from a byte hdd offset. */
|
---|
| 545 | unsigned uBlockMask;
|
---|
| 546 | /** Block shift value for converting byte hdd offset into paBlock index. */
|
---|
| 547 | unsigned uShiftOffset2Index;
|
---|
| 548 | /** Offset of data from the beginning of block. */
|
---|
| 549 | unsigned offStartBlockData;
|
---|
[7231] | 550 | /** Total size of image block (including the extra data). */
|
---|
| 551 | unsigned cbTotalBlockData;
|
---|
[7152] | 552 | /** Container filename. (UTF-8) */
|
---|
| 553 | const char *pszFilename;
|
---|
| 554 | /** Physical geometry of this image (never actually stored). */
|
---|
[32536] | 555 | VDGEOMETRY PCHSGeometry;
|
---|
[11444] | 556 | /** Pointer to the per-disk VD interface list. */
|
---|
| 557 | PVDINTERFACE pVDIfsDisk;
|
---|
[28620] | 558 | /** Pointer to the per-image VD interface list. */
|
---|
| 559 | PVDINTERFACE pVDIfsImage;
|
---|
[10715] | 560 | /** Error interface. */
|
---|
[38469] | 561 | PVDINTERFACEERROR pIfError;
|
---|
[27808] | 562 | /** I/O interface. */
|
---|
[38469] | 563 | PVDINTERFACEIOINT pIfIo;
|
---|
[40713] | 564 | /** Current size of the image (used for range validation when reading). */
|
---|
| 565 | uint64_t cbImage;
|
---|
[66486] | 566 | /** The static region list. */
|
---|
| 567 | VDREGIONLIST RegionList;
|
---|
[1] | 568 | } VDIIMAGEDESC, *PVDIIMAGEDESC;
|
---|
| 569 |
|
---|
[38876] | 570 | /**
|
---|
| 571 | * Async block discard states.
|
---|
| 572 | */
|
---|
| 573 | typedef enum VDIBLOCKDISCARDSTATE
|
---|
| 574 | {
|
---|
| 575 | /** Invalid. */
|
---|
| 576 | VDIBLOCKDISCARDSTATE_INVALID = 0,
|
---|
| 577 | /** Read the last block. */
|
---|
| 578 | VDIBLOCKDISCARDSTATE_READ_BLOCK,
|
---|
| 579 | /** Write block into the hole. */
|
---|
| 580 | VDIBLOCKDISCARDSTATE_WRITE_BLOCK,
|
---|
| 581 | /** Update metadata. */
|
---|
| 582 | VDIBLOCKDISCARDSTATE_UPDATE_METADATA,
|
---|
| 583 | /** 32bit hack. */
|
---|
| 584 | VDIBLOCKDISCARDSTATE_32BIT_HACK = 0x7fffffff
|
---|
| 585 | } VDIBLOCKDISCARDSTATE;
|
---|
| 586 |
|
---|
| 587 | /**
|
---|
| 588 | * Async block discard structure.
|
---|
| 589 | */
|
---|
| 590 | typedef struct VDIBLOCKDISCARDASYNC
|
---|
| 591 | {
|
---|
| 592 | /** State of the block discard. */
|
---|
| 593 | VDIBLOCKDISCARDSTATE enmState;
|
---|
| 594 | /** Pointer to the block data. */
|
---|
| 595 | void *pvBlock;
|
---|
| 596 | /** Block index in the block table. */
|
---|
| 597 | unsigned uBlock;
|
---|
| 598 | /** Block pointer to the block to discard. */
|
---|
| 599 | VDIIMAGEBLOCKPOINTER ptrBlockDiscard;
|
---|
| 600 | /** Index of the last block in the reverse block table. */
|
---|
| 601 | unsigned idxLastBlock;
|
---|
| 602 | /** Index of the last block in the block table (gathered from the reverse block table). */
|
---|
| 603 | unsigned uBlockLast;
|
---|
| 604 | } VDIBLOCKDISCARDASYNC, *PVDIBLOCKDISCARDASYNC;
|
---|
| 605 |
|
---|
[40713] | 606 | /**
|
---|
| 607 | * Async image expansion state.
|
---|
| 608 | */
|
---|
| 609 | typedef struct VDIASYNCBLOCKALLOC
|
---|
| 610 | {
|
---|
| 611 | /** Number of blocks allocated. */
|
---|
| 612 | unsigned cBlocksAllocated;
|
---|
| 613 | /** Block index to allocate. */
|
---|
| 614 | unsigned uBlock;
|
---|
| 615 | } VDIASYNCBLOCKALLOC, *PVDIASYNCBLOCKALLOC;
|
---|
| 616 |
|
---|
[40936] | 617 | /**
|
---|
| 618 | * Endianess conversion direction.
|
---|
| 619 | */
|
---|
| 620 | typedef enum VDIECONV
|
---|
| 621 | {
|
---|
| 622 | /** Host to file endianess. */
|
---|
| 623 | VDIECONV_H2F = 0,
|
---|
| 624 | /** File to host endianess. */
|
---|
| 625 | VDIECONV_F2H
|
---|
| 626 | } VDIECONV;
|
---|
| 627 |
|
---|
[38469] | 628 | #endif
|
---|
[1] | 629 |
|
---|