VirtualBox

source: vbox/trunk/src/VBox/Storage/VHDX.cpp@ 60441

Last change on this file since 60441 was 58132, checked in by vboxsync, 9 years ago

*: Doxygen fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 93.1 KB
Line 
1/* $Id: VHDX.cpp 58132 2015-10-09 00:09:37Z vboxsync $ */
2/** @file
3 * VHDX - VHDX Disk image, Core Code.
4 */
5
6/*
7 * Copyright (C) 2012-2015 Oracle Corporation
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
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.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_VD_VHDX
23#include <VBox/vd-plugin.h>
24#include <VBox/err.h>
25
26#include <VBox/log.h>
27#include <iprt/asm.h>
28#include <iprt/assert.h>
29#include <iprt/alloc.h>
30#include <iprt/path.h>
31#include <iprt/uuid.h>
32#include <iprt/crc.h>
33
34#include "VDBackends.h"
35
36
37/*********************************************************************************************************************************
38* On disk data structures *
39*********************************************************************************************************************************/
40
41/**
42 * VHDX file type identifier.
43 */
44#pragma pack(1)
45typedef struct VhdxFileIdentifier
46{
47 /** Signature. */
48 uint64_t u64Signature;
49 /** Creator ID - UTF-16 string (not neccessarily null terminated). */
50 uint16_t awszCreator[256];
51} VhdxFileIdentifier;
52#pragma pack()
53/** Pointer to an on disk VHDX file type identifier. */
54typedef VhdxFileIdentifier *PVhdxFileIdentifier;
55
56/** VHDX file type identifier signature ("vhdxfile"). */
57#define VHDX_FILE_IDENTIFIER_SIGNATURE UINT64_C(0x656c696678646876)
58/** Start offset of the VHDX file type identifier. */
59#define VHDX_FILE_IDENTIFIER_OFFSET UINT64_C(0)
60
61/**
62 * VHDX header.
63 */
64#pragma pack(1)
65typedef struct VhdxHeader
66{
67 /** Signature. */
68 uint32_t u32Signature;
69 /** Checksum. */
70 uint32_t u32Checksum;
71 /** Sequence number. */
72 uint64_t u64SequenceNumber;
73 /** File write UUID. */
74 RTUUID UuidFileWrite;
75 /** Data write UUID. */
76 RTUUID UuidDataWrite;
77 /** Log UUID. */
78 RTUUID UuidLog;
79 /** Version of the log format. */
80 uint16_t u16LogVersion;
81 /** VHDX format version.. */
82 uint16_t u16Version;
83 /** Length of the log region. */
84 uint32_t u32LogLength;
85 /** Start offset of the log offset in the file. */
86 uint64_t u64LogOffset;
87 /** Reserved bytes. */
88 uint8_t u8Reserved[4016];
89} VhdxHeader;
90#pragma pack()
91/** Pointer to an on disk VHDX header. */
92typedef VhdxHeader *PVhdxHeader;
93
94/** VHDX header signature ("head"). */
95#define VHDX_HEADER_SIGNATURE UINT32_C(0x64616568)
96/** Start offset of the first VHDX header. */
97#define VHDX_HEADER1_OFFSET _64K
98/** Start offset of the second VHDX header. */
99#define VHDX_HEADER2_OFFSET _128K
100/** Current Log format version. */
101#define VHDX_HEADER_LOG_VERSION UINT16_C(0)
102/** Current VHDX format version. */
103#define VHDX_HEADER_VHDX_VERSION UINT16_C(1)
104
105/**
106 * VHDX region table header
107 */
108#pragma pack(1)
109typedef struct VhdxRegionTblHdr
110{
111 /** Signature. */
112 uint32_t u32Signature;
113 /** Checksum. */
114 uint32_t u32Checksum;
115 /** Number of region table entries following this header. */
116 uint32_t u32EntryCount;
117 /** Reserved. */
118 uint32_t u32Reserved;
119} VhdxRegionTblHdr;
120#pragma pack()
121/** Pointer to an on disk VHDX region table header. */
122typedef VhdxRegionTblHdr *PVhdxRegionTblHdr;
123
124/** VHDX region table header signature. */
125#define VHDX_REGION_TBL_HDR_SIGNATURE UINT32_C(0x69676572)
126/** Maximum number of entries which can follow. */
127#define VHDX_REGION_TBL_HDR_ENTRY_COUNT_MAX UINT32_C(2047)
128/** Offset where the region table is stored (192 KB). */
129#define VHDX_REGION_TBL_HDR_OFFSET UINT64_C(196608)
130/** Maximum size of the region table. */
131#define VHDX_REGION_TBL_SIZE_MAX _64K
132
133/**
134 * VHDX region table entry.
135 */
136#pragma pack(1)
137typedef struct VhdxRegionTblEntry
138{
139 /** Object UUID. */
140 RTUUID UuidObject;
141 /** File offset of the region. */
142 uint64_t u64FileOffset;
143 /** Length of the region in bytes. */
144 uint32_t u32Length;
145 /** Flags for this object. */
146 uint32_t u32Flags;
147} VhdxRegionTblEntry;
148#pragma pack()
149/** Pointer to an on disk VHDX region table entry. */
150typedef struct VhdxRegionTblEntry *PVhdxRegionTblEntry;
151
152/** Flag whether this region is required. */
153#define VHDX_REGION_TBL_ENTRY_FLAGS_IS_REQUIRED RT_BIT_32(0)
154/** UUID for the BAT region. */
155#define VHDX_REGION_TBL_ENTRY_UUID_BAT "2dc27766-f623-4200-9d64-115e9bfd4a08"
156/** UUID for the metadata region. */
157#define VHDX_REGION_TBL_ENTRY_UUID_METADATA "8b7ca206-4790-4b9a-b8fe-575f050f886e"
158
159/**
160 * VHDX Log entry header.
161 */
162#pragma pack(1)
163typedef struct VhdxLogEntryHdr
164{
165 /** Signature. */
166 uint32_t u32Signature;
167 /** Checksum. */
168 uint32_t u32Checksum;
169 /** Total length of the entry in bytes. */
170 uint32_t u32EntryLength;
171 /** Tail of the log entries. */
172 uint32_t u32Tail;
173 /** Sequence number. */
174 uint64_t u64SequenceNumber;
175 /** Number of descriptors in this log entry. */
176 uint32_t u32DescriptorCount;
177 /** Reserved. */
178 uint32_t u32Reserved;
179 /** Log UUID. */
180 RTUUID UuidLog;
181 /** VHDX file size in bytes while the log entry was written. */
182 uint64_t u64FlushedFileOffset;
183 /** File size in bytes all allocated file structures fit into when the
184 * log entry was written. */
185 uint64_t u64LastFileOffset;
186} VhdxLogEntryHdr;
187#pragma pack()
188/** Pointer to an on disk VHDX log entry header. */
189typedef struct VhdxLogEntryHdr *PVhdxLogEntryHdr;
190
191/** VHDX log entry signature ("loge"). */
192#define VHDX_LOG_ENTRY_HEADER_SIGNATURE UINT32_C(0x65676f6c)
193
194/**
195 * VHDX log zero descriptor.
196 */
197#pragma pack(1)
198typedef struct VhdxLogZeroDesc
199{
200 /** Signature of this descriptor. */
201 uint32_t u32ZeroSignature;
202 /** Reserved. */
203 uint32_t u32Reserved;
204 /** Length of the section to zero. */
205 uint64_t u64ZeroLength;
206 /** File offset to write zeros to. */
207 uint64_t u64FileOffset;
208 /** Sequence number (must macht the field in the log entry header). */
209 uint64_t u64SequenceNumber;
210} VhdxLogZeroDesc;
211#pragma pack()
212/** Pointer to an on disk VHDX log zero descriptor. */
213typedef struct VhdxLogZeroDesc *PVhdxLogZeroDesc;
214
215/** Signature of a VHDX log zero descriptor ("zero"). */
216#define VHDX_LOG_ZERO_DESC_SIGNATURE UINT32_C(0x6f72657a)
217
218/**
219 * VHDX log data descriptor.
220 */
221#pragma pack(1)
222typedef struct VhdxLogDataDesc
223{
224 /** Signature of this descriptor. */
225 uint32_t u32DataSignature;
226 /** Trailing 4 bytes removed from the update. */
227 uint32_t u32TrailingBytes;
228 /** Leading 8 bytes removed from the update. */
229 uint64_t u64LeadingBytes;
230 /** File offset to write zeros to. */
231 uint64_t u64FileOffset;
232 /** Sequence number (must macht the field in the log entry header). */
233 uint64_t u64SequenceNumber;
234} VhdxLogDataDesc;
235#pragma pack()
236/** Pointer to an on disk VHDX log data descriptor. */
237typedef struct VhdxLogDataDesc *PVhdxLogDataDesc;
238
239/** Signature of a VHDX log data descriptor ("desc"). */
240#define VHDX_LOG_DATA_DESC_SIGNATURE UINT32_C(0x63736564)
241
242/**
243 * VHDX log data sector.
244 */
245#pragma pack(1)
246typedef struct VhdxLogDataSector
247{
248 /** Signature of the data sector. */
249 uint32_t u32DataSignature;
250 /** 4 most significant bytes of the sequence number. */
251 uint32_t u32SequenceHigh;
252 /** Raw data associated with the update. */
253 uint8_t u8Data[4084];
254 /** 4 least significant bytes of the sequence number. */
255 uint32_t u32SequenceLow;
256} VhdxLogDataSector;
257#pragma pack()
258/** Pointer to an on disk VHDX log data sector. */
259typedef VhdxLogDataSector *PVhdxLogDataSector;
260
261/** Signature of a VHDX log data sector ("data"). */
262#define VHDX_LOG_DATA_SECTOR_SIGNATURE UINT32_C(0x61746164)
263
264/**
265 * VHDX BAT entry.
266 */
267#pragma pack(1)
268typedef struct VhdxBatEntry
269{
270 /** The BAT entry, contains state and offset. */
271 uint64_t u64BatEntry;
272} VhdxBatEntry;
273#pragma pack()
274typedef VhdxBatEntry *PVhdxBatEntry;
275
276/** Return the BAT state from a given entry. */
277#define VHDX_BAT_ENTRY_GET_STATE(bat) ((bat) & UINT64_C(0x7))
278/** Get the FileOffsetMB field from a given BAT entry. */
279#define VHDX_BAT_ENTRY_GET_FILE_OFFSET_MB(bat) (((bat) & UINT64_C(0xfffffffffff00000)) >> 20)
280/** Get a byte offset from the BAT entry. */
281#define VHDX_BAT_ENTRY_GET_FILE_OFFSET(bat) (VHDX_BAT_ENTRY_GET_FILE_OFFSET_MB(bat) * (uint64_t)_1M)
282
283/** Block not present and the data is undefined. */
284#define VHDX_BAT_ENTRY_PAYLOAD_BLOCK_NOT_PRESENT (0)
285/** Data in this block is undefined. */
286#define VHDX_BAT_ENTRY_PAYLOAD_BLOCK_UNDEFINED (1)
287/** Data in this block contains zeros. */
288#define VHDX_BAT_ENTRY_PAYLOAD_BLOCK_ZERO (2)
289/** Block was unmapped by the application or system and data is either zero or
290 * the data before the block was unmapped. */
291#define VHDX_BAT_ENTRY_PAYLOAD_BLOCK_UNMAPPED (3)
292/** Block data is in the file pointed to by the FileOffsetMB field. */
293#define VHDX_BAT_ENTRY_PAYLOAD_BLOCK_FULLY_PRESENT (6)
294/** Block is partially present, use sector bitmap to get present sectors. */
295#define VHDX_BAT_ENTRY_PAYLOAD_BLOCK_PARTIALLY_PRESENT (7)
296
297/** The sector bitmap block is undefined and not allocated in the file. */
298#define VHDX_BAT_ENTRY_SB_BLOCK_NOT_PRESENT (0)
299/** The sector bitmap block is defined at the file location. */
300#define VHDX_BAT_ENTRY_SB_BLOCK_PRESENT (6)
301
302/**
303 * VHDX Metadata tabl header.
304 */
305#pragma pack(1)
306typedef struct VhdxMetadataTblHdr
307{
308 /** Signature. */
309 uint64_t u64Signature;
310 /** Reserved. */
311 uint16_t u16Reserved;
312 /** Number of entries in the table. */
313 uint16_t u16EntryCount;
314 /** Reserved */
315 uint32_t u32Reserved2[5];
316} VhdxMetadataTblHdr;
317#pragma pack()
318/** Pointer to an on disk metadata table header. */
319typedef VhdxMetadataTblHdr *PVhdxMetadataTblHdr;
320
321/** Signature of a VHDX metadata table header ("metadata"). */
322#define VHDX_METADATA_TBL_HDR_SIGNATURE UINT64_C(0x617461646174656d)
323/** Maximum number of entries the metadata table can have. */
324#define VHDX_METADATA_TBL_HDR_ENTRY_COUNT_MAX UINT16_C(2047)
325
326/**
327 * VHDX Metadata table entry.
328 */
329#pragma pack(1)
330typedef struct VhdxMetadataTblEntry
331{
332 /** Item UUID. */
333 RTUUID UuidItem;
334 /** Offset of the metadata item. */
335 uint32_t u32Offset;
336 /** Length of the metadata item. */
337 uint32_t u32Length;
338 /** Flags for the metadata item. */
339 uint32_t u32Flags;
340 /** Reserved. */
341 uint32_t u32Reserved;
342} VhdxMetadataTblEntry;
343#pragma pack()
344/** Pointer to an on disk metadata table entry. */
345typedef VhdxMetadataTblEntry *PVhdxMetadataTblEntry;
346
347/** FLag whether the metadata item is system or user metadata. */
348#define VHDX_METADATA_TBL_ENTRY_FLAGS_IS_USER RT_BIT_32(0)
349/** FLag whether the metadata item is file or virtual disk metadata. */
350#define VHDX_METADATA_TBL_ENTRY_FLAGS_IS_VDISK RT_BIT_32(1)
351/** FLag whether the backend must understand the metadata item to load the image. */
352#define VHDX_METADATA_TBL_ENTRY_FLAGS_IS_REQUIRED RT_BIT_32(2)
353
354/** File parameters item UUID. */
355#define VHDX_METADATA_TBL_ENTRY_ITEM_FILE_PARAMS "caa16737-fa36-4d43-b3b6-33f0aa44e76b"
356/** Virtual disk size item UUID. */
357#define VHDX_METADATA_TBL_ENTRY_ITEM_VDISK_SIZE "2fa54224-cd1b-4876-b211-5dbed83bf4b8"
358/** Page 83 UUID. */
359#define VHDX_METADATA_TBL_ENTRY_ITEM_PAGE83_DATA "beca12ab-b2e6-4523-93ef-c309e000c746"
360/** Logical sector size UUID. */
361#define VHDX_METADATA_TBL_ENTRY_ITEM_LOG_SECT_SIZE "8141bf1d-a96f-4709-ba47-f233a8faab5f"
362/** Physical sector size UUID. */
363#define VHDX_METADATA_TBL_ENTRY_ITEM_PHYS_SECT_SIZE "cda348c7-445d-4471-9cc9-e9885251c556"
364/** Parent locator UUID. */
365#define VHDX_METADATA_TBL_ENTRY_ITEM_PARENT_LOCATOR "a8d35f2d-b30b-454d-abf7-d3d84834ab0c"
366
367/**
368 * VHDX File parameters metadata item.
369 */
370#pragma pack(1)
371typedef struct VhdxFileParameters
372{
373 /** Block size. */
374 uint32_t u32BlockSize;
375 /** Flags. */
376 uint32_t u32Flags;
377} VhdxFileParameters;
378#pragma pack()
379/** Pointer to an on disk VHDX file parameters metadata item. */
380typedef struct VhdxFileParameters *PVhdxFileParameters;
381
382/** Flag whether to leave blocks allocated in the file or if it is possible to unmap them. */
383#define VHDX_FILE_PARAMETERS_FLAGS_LEAVE_BLOCKS_ALLOCATED RT_BIT_32(0)
384/** Flag whether this file has a parent VHDX file. */
385#define VHDX_FILE_PARAMETERS_FLAGS_HAS_PARENT RT_BIT_32(1)
386
387/**
388 * VHDX virtual disk size metadata item.
389 */
390#pragma pack(1)
391typedef struct VhdxVDiskSize
392{
393 /** Virtual disk size. */
394 uint64_t u64VDiskSize;
395} VhdxVDiskSize;
396#pragma pack()
397/** Pointer to an on disk VHDX virtual disk size metadata item. */
398typedef struct VhdxVDiskSize *PVhdxVDiskSize;
399
400/**
401 * VHDX page 83 data metadata item.
402 */
403#pragma pack(1)
404typedef struct VhdxPage83Data
405{
406 /** UUID for the SCSI device. */
407 RTUUID UuidPage83Data;
408} VhdxPage83Data;
409#pragma pack()
410/** Pointer to an on disk VHDX vpage 83 data metadata item. */
411typedef struct VhdxPage83Data *PVhdxPage83Data;
412
413/**
414 * VHDX virtual disk logical sector size.
415 */
416#pragma pack(1)
417typedef struct VhdxVDiskLogicalSectorSize
418{
419 /** Logical sector size. */
420 uint32_t u32LogicalSectorSize;
421} VhdxVDiskLogicalSectorSize;
422#pragma pack()
423/** Pointer to an on disk VHDX virtual disk logical sector size metadata item. */
424typedef struct VhdxVDiskLogicalSectorSize *PVhdxVDiskLogicalSectorSize;
425
426/**
427 * VHDX virtual disk physical sector size.
428 */
429#pragma pack(1)
430typedef struct VhdxVDiskPhysicalSectorSize
431{
432 /** Physical sector size. */
433 uint64_t u64PhysicalSectorSize;
434} VhdxVDiskPhysicalSectorSize;
435#pragma pack()
436/** Pointer to an on disk VHDX virtual disk physical sector size metadata item. */
437typedef struct VhdxVDiskPhysicalSectorSize *PVhdxVDiskPhysicalSectorSize;
438
439/**
440 * VHDX parent locator header.
441 */
442#pragma pack(1)
443typedef struct VhdxParentLocatorHeader
444{
445 /** Locator type UUID. */
446 RTUUID UuidLocatorType;
447 /** Reserved. */
448 uint16_t u16Reserved;
449 /** Number of key value pairs. */
450 uint16_t u16KeyValueCount;
451} VhdxParentLocatorHeader;
452#pragma pack()
453/** Pointer to an on disk VHDX parent locator header metadata item. */
454typedef struct VhdxParentLocatorHeader *PVhdxParentLocatorHeader;
455
456/** VHDX parent locator type. */
457#define VHDX_PARENT_LOCATOR_TYPE_VHDX "b04aefb7-d19e-4a81-b789-25b8e9445913"
458
459/**
460 * VHDX parent locator entry.
461 */
462#pragma pack(1)
463typedef struct VhdxParentLocatorEntry
464{
465 /** Offset of the key. */
466 uint32_t u32KeyOffset;
467 /** Offset of the value. */
468 uint32_t u32ValueOffset;
469 /** Length of the key. */
470 uint16_t u16KeyLength;
471 /** Length of the value. */
472 uint16_t u16ValueLength;
473} VhdxParentLocatorEntry;
474#pragma pack()
475/** Pointer to an on disk VHDX parent locator entry. */
476typedef struct VhdxParentLocatorEntry *PVhdxParentLocatorEntry;
477
478
479/*********************************************************************************************************************************
480* Constants And Macros, Structures and Typedefs *
481*********************************************************************************************************************************/
482
483typedef enum VHDXMETADATAITEM
484{
485 VHDXMETADATAITEM_UNKNOWN = 0,
486 VHDXMETADATAITEM_FILE_PARAMS,
487 VHDXMETADATAITEM_VDISK_SIZE,
488 VHDXMETADATAITEM_PAGE83_DATA,
489 VHDXMETADATAITEM_LOGICAL_SECTOR_SIZE,
490 VHDXMETADATAITEM_PHYSICAL_SECTOR_SIZE,
491 VHDXMETADATAITEM_PARENT_LOCATOR,
492 VHDXMETADATAITEM_32BIT_HACK = 0x7fffffff
493} VHDXMETADATAITEM;
494
495/**
496 * Table to validate the metadata item UUIDs and the flags.
497 */
498typedef struct VHDXMETADATAITEMPROPS
499{
500 /** Item UUID. */
501 const char *pszItemUuid;
502 /** Flag whether this is a user or system metadata item. */
503 bool fIsUser;
504 /** Flag whether this is a virtual disk or file metadata item. */
505 bool fIsVDisk;
506 /** Flag whether this metadata item is required to load the file. */
507 bool fIsRequired;
508 /** Metadata item enum associated with this UUID. */
509 VHDXMETADATAITEM enmMetadataItem;
510} VHDXMETADATAITEMPROPS;
511
512/**
513 * VHDX image data structure.
514 */
515typedef struct VHDXIMAGE
516{
517 /** Image name. */
518 const char *pszFilename;
519 /** Storage handle. */
520 PVDIOSTORAGE pStorage;
521
522 /** Pointer to the per-disk VD interface list. */
523 PVDINTERFACE pVDIfsDisk;
524 /** Pointer to the per-image VD interface list. */
525 PVDINTERFACE pVDIfsImage;
526 /** Error interface. */
527 PVDINTERFACEERROR pIfError;
528 /** I/O interface. */
529 PVDINTERFACEIOINT pIfIo;
530
531 /** Open flags passed by VBoxHD layer. */
532 unsigned uOpenFlags;
533 /** Image flags defined during creation or determined during open. */
534 unsigned uImageFlags;
535 /** Version of the VHDX image format. */
536 unsigned uVersion;
537 /** Total size of the image. */
538 uint64_t cbSize;
539 /** Logical sector size of the image. */
540 uint32_t cbLogicalSector;
541 /** Block size of the image. */
542 size_t cbBlock;
543 /** Physical geometry of this image. */
544 VDGEOMETRY PCHSGeometry;
545 /** Logical geometry of this image. */
546 VDGEOMETRY LCHSGeometry;
547
548 /** The BAT. */
549 PVhdxBatEntry paBat;
550 /** Chunk ratio. */
551 uint32_t uChunkRatio;
552
553} VHDXIMAGE, *PVHDXIMAGE;
554
555/**
556 * Endianess conversion direction.
557 */
558typedef enum VHDXECONV
559{
560 /** Host to file endianess. */
561 VHDXECONV_H2F = 0,
562 /** File to host endianess. */
563 VHDXECONV_F2H
564} VHDXECONV;
565
566/** Macros for endianess conversion. */
567#define SET_ENDIAN_U16(u16) (enmConv == VHDXECONV_H2F ? RT_H2LE_U16(u16) : RT_LE2H_U16(u16))
568#define SET_ENDIAN_U32(u32) (enmConv == VHDXECONV_H2F ? RT_H2LE_U32(u32) : RT_LE2H_U32(u32))
569#define SET_ENDIAN_U64(u64) (enmConv == VHDXECONV_H2F ? RT_H2LE_U64(u64) : RT_LE2H_U64(u64))
570
571
572/*********************************************************************************************************************************
573* Static Variables *
574*********************************************************************************************************************************/
575
576/**
577 * NULL-terminated array of supported file extensions.
578 */
579static const VDFILEEXTENSION s_aVhdxFileExtensions[] =
580{
581 {"vhdx", VDTYPE_HDD},
582 {NULL, VDTYPE_INVALID}
583};
584
585/**
586 * Static table to verify the metadata item properties and the flags.
587 */
588static const VHDXMETADATAITEMPROPS s_aVhdxMetadataItemProps[] =
589{
590 /* pcszItemUuid fIsUser, fIsVDisk, fIsRequired, enmMetadataItem */
591 {VHDX_METADATA_TBL_ENTRY_ITEM_FILE_PARAMS, false, false, true, VHDXMETADATAITEM_FILE_PARAMS},
592 {VHDX_METADATA_TBL_ENTRY_ITEM_VDISK_SIZE, false, true, true, VHDXMETADATAITEM_VDISK_SIZE},
593 {VHDX_METADATA_TBL_ENTRY_ITEM_PAGE83_DATA, false, true, true, VHDXMETADATAITEM_PAGE83_DATA},
594 {VHDX_METADATA_TBL_ENTRY_ITEM_LOG_SECT_SIZE, false, true, true, VHDXMETADATAITEM_LOGICAL_SECTOR_SIZE},
595 {VHDX_METADATA_TBL_ENTRY_ITEM_PHYS_SECT_SIZE, false, true, true, VHDXMETADATAITEM_PHYSICAL_SECTOR_SIZE},
596 {VHDX_METADATA_TBL_ENTRY_ITEM_PARENT_LOCATOR, false, false, true, VHDXMETADATAITEM_PARENT_LOCATOR}
597};
598
599
600/*********************************************************************************************************************************
601* Internal Functions *
602*********************************************************************************************************************************/
603
604/**
605 * Converts the file identifier between file and host endianness.
606 *
607 * @returns nothing.
608 * @param enmConv Direction of the conversion.
609 * @param pFileIdentifierConv Where to store the converted file identifier.
610 * @param pFileIdentifier The file identifier to convert.
611 *
612 * @note It is safe to use the same pointer for pFileIdentifierConv and pFileIdentifier.
613 */
614DECLINLINE(void) vhdxConvFileIdentifierEndianess(VHDXECONV enmConv, PVhdxFileIdentifier pFileIdentifierConv,
615 PVhdxFileIdentifier pFileIdentifier)
616{
617 pFileIdentifierConv->u64Signature = SET_ENDIAN_U64(pFileIdentifier->u64Signature);
618 for (unsigned i = 0; i < RT_ELEMENTS(pFileIdentifierConv->awszCreator); i++)
619 pFileIdentifierConv->awszCreator[i] = SET_ENDIAN_U16(pFileIdentifier->awszCreator[i]);
620}
621
622/**
623 * Converts a UUID between file and host endianness.
624 *
625 * @returns nothing.
626 * @param enmConv Direction of the conversion.
627 * @param pUuidConv Where to store the converted UUID.
628 * @param pUuid The UUID to convert.
629 *
630 * @note It is safe to use the same pointer for pUuidConv and pUuid.
631 */
632DECLINLINE(void) vhdxConvUuidEndianess(VHDXECONV enmConv, PRTUUID pUuidConv, PRTUUID pUuid)
633{
634#if 1
635 memcpy(pUuidConv, pUuid, sizeof(RTUUID));
636#else
637 pUuidConv->Gen.u32TimeLow = SET_ENDIAN_U32(pUuid->Gen.u32TimeLow);
638 pUuidConv->Gen.u16TimeMid = SET_ENDIAN_U16(pUuid->Gen.u16TimeMid);
639 pUuidConv->Gen.u16TimeHiAndVersion = SET_ENDIAN_U16(pUuid->Gen.u16TimeHiAndVersion);
640 pUuidConv->Gen.u8ClockSeqHiAndReserved = pUuid->Gen.u8ClockSeqHiAndReserved;
641 pUuidConv->Gen.u8ClockSeqLow = pUuid->Gen.u8ClockSeqLow;
642 for (unsigned i = 0; i < RT_ELEMENTS(pUuidConv->Gen.au8Node); i++)
643 pUuidConv->Gen.au8Node[i] = pUuid->Gen.au8Node[i];
644#endif
645}
646
647/**
648 * Converts a VHDX header between file and host endianness.
649 *
650 * @returns nothing.
651 * @param enmConv Direction of the conversion.
652 * @param pHdrConv Where to store the converted header.
653 * @param pHdr The VHDX header to convert.
654 *
655 * @note It is safe to use the same pointer for pHdrConv and pHdr.
656 */
657DECLINLINE(void) vhdxConvHeaderEndianess(VHDXECONV enmConv, PVhdxHeader pHdrConv, PVhdxHeader pHdr)
658{
659 pHdrConv->u32Signature = SET_ENDIAN_U32(pHdr->u32Signature);
660 pHdrConv->u32Checksum = SET_ENDIAN_U32(pHdr->u32Checksum);
661 pHdrConv->u64SequenceNumber = SET_ENDIAN_U64(pHdr->u64SequenceNumber);
662 vhdxConvUuidEndianess(enmConv, &pHdrConv->UuidFileWrite, &pHdrConv->UuidFileWrite);
663 vhdxConvUuidEndianess(enmConv, &pHdrConv->UuidDataWrite, &pHdrConv->UuidDataWrite);
664 vhdxConvUuidEndianess(enmConv, &pHdrConv->UuidLog, &pHdrConv->UuidLog);
665 pHdrConv->u16LogVersion = SET_ENDIAN_U16(pHdr->u16LogVersion);
666 pHdrConv->u16Version = SET_ENDIAN_U16(pHdr->u16Version);
667 pHdrConv->u32LogLength = SET_ENDIAN_U32(pHdr->u32LogLength);
668 pHdrConv->u64LogOffset = SET_ENDIAN_U64(pHdr->u64LogOffset);
669}
670
671/**
672 * Converts a VHDX region table header between file and host endianness.
673 *
674 * @returns nothing.
675 * @param enmConv Direction of the conversion.
676 * @param pRegTblHdrConv Where to store the converted header.
677 * @param pRegTblHdr The VHDX region table header to convert.
678 *
679 * @note It is safe to use the same pointer for pRegTblHdrConv and pRegTblHdr.
680 */
681DECLINLINE(void) vhdxConvRegionTblHdrEndianess(VHDXECONV enmConv, PVhdxRegionTblHdr pRegTblHdrConv,
682 PVhdxRegionTblHdr pRegTblHdr)
683{
684 pRegTblHdrConv->u32Signature = SET_ENDIAN_U32(pRegTblHdr->u32Signature);
685 pRegTblHdrConv->u32Checksum = SET_ENDIAN_U32(pRegTblHdr->u32Checksum);
686 pRegTblHdrConv->u32EntryCount = SET_ENDIAN_U32(pRegTblHdr->u32EntryCount);
687 pRegTblHdrConv->u32Reserved = SET_ENDIAN_U32(pRegTblHdr->u32Reserved);
688}
689
690/**
691 * Converts a VHDX region table entry between file and host endianness.
692 *
693 * @returns nothing.
694 * @param enmConv Direction of the conversion.
695 * @param pRegTblEntConv Where to store the converted region table entry.
696 * @param pRegTblEnt The VHDX region table entry to convert.
697 *
698 * @note It is safe to use the same pointer for pRegTblEntConv and pRegTblEnt.
699 */
700DECLINLINE(void) vhdxConvRegionTblEntryEndianess(VHDXECONV enmConv, PVhdxRegionTblEntry pRegTblEntConv,
701 PVhdxRegionTblEntry pRegTblEnt)
702{
703 vhdxConvUuidEndianess(enmConv, &pRegTblEntConv->UuidObject, &pRegTblEnt->UuidObject);
704 pRegTblEntConv->u64FileOffset = SET_ENDIAN_U64(pRegTblEnt->u64FileOffset);
705 pRegTblEntConv->u32Length = SET_ENDIAN_U32(pRegTblEnt->u32Length);
706 pRegTblEntConv->u32Flags = SET_ENDIAN_U32(pRegTblEnt->u32Flags);
707}
708
709/**
710 * Converts a VHDX log entry header between file and host endianness.
711 *
712 * @returns nothing.
713 * @param enmConv Direction of the conversion.
714 * @param pLogEntryHdrConv Where to store the converted log entry header.
715 * @param pLogEntryHdr The VHDX log entry header to convert.
716 *
717 * @note It is safe to use the same pointer for pLogEntryHdrConv and pLogEntryHdr.
718 */
719DECLINLINE(void) vhdxConvLogEntryHdrEndianess(VHDXECONV enmConv, PVhdxLogEntryHdr pLogEntryHdrConv,
720 PVhdxLogEntryHdr pLogEntryHdr)
721{
722 pLogEntryHdrConv->u32Signature = SET_ENDIAN_U32(pLogEntryHdr->u32Signature);
723 pLogEntryHdrConv->u32Checksum = SET_ENDIAN_U32(pLogEntryHdr->u32Checksum);
724 pLogEntryHdrConv->u32EntryLength = SET_ENDIAN_U32(pLogEntryHdr->u32EntryLength);
725 pLogEntryHdrConv->u32Tail = SET_ENDIAN_U32(pLogEntryHdr->u32Tail);
726 pLogEntryHdrConv->u64SequenceNumber = SET_ENDIAN_U64(pLogEntryHdr->u64SequenceNumber);
727 pLogEntryHdrConv->u32DescriptorCount = SET_ENDIAN_U32(pLogEntryHdr->u32DescriptorCount);
728 pLogEntryHdrConv->u32Reserved = SET_ENDIAN_U32(pLogEntryHdr->u32Reserved);
729 vhdxConvUuidEndianess(enmConv, &pLogEntryHdrConv->UuidLog, &pLogEntryHdr->UuidLog);
730 pLogEntryHdrConv->u64FlushedFileOffset = SET_ENDIAN_U64(pLogEntryHdr->u64FlushedFileOffset);
731}
732
733/**
734 * Converts a VHDX log zero descriptor between file and host endianness.
735 *
736 * @returns nothing.
737 * @param enmConv Direction of the conversion.
738 * @param pLogZeroDescConv Where to store the converted log zero descriptor.
739 * @param pLogZeroDesc The VHDX log zero descriptor to convert.
740 *
741 * @note It is safe to use the same pointer for pLogZeroDescConv and pLogZeroDesc.
742 */
743DECLINLINE(void) vhdxConvLogZeroDescEndianess(VHDXECONV enmConv, PVhdxLogZeroDesc pLogZeroDescConv,
744 PVhdxLogZeroDesc pLogZeroDesc)
745{
746 pLogZeroDescConv->u32ZeroSignature = SET_ENDIAN_U32(pLogZeroDesc->u32ZeroSignature);
747 pLogZeroDescConv->u32Reserved = SET_ENDIAN_U32(pLogZeroDesc->u32Reserved);
748 pLogZeroDescConv->u64ZeroLength = SET_ENDIAN_U64(pLogZeroDesc->u64ZeroLength);
749 pLogZeroDescConv->u64FileOffset = SET_ENDIAN_U64(pLogZeroDesc->u64FileOffset);
750 pLogZeroDescConv->u64SequenceNumber = SET_ENDIAN_U64(pLogZeroDesc->u64SequenceNumber);
751}
752
753/**
754 * Converts a VHDX log data descriptor between file and host endianness.
755 *
756 * @returns nothing.
757 * @param enmConv Direction of the conversion.
758 * @param pLogDataDescConv Where to store the converted log data descriptor.
759 * @param pLogDataDesc The VHDX log data descriptor to convert.
760 *
761 * @note It is safe to use the same pointer for pLogDataDescConv and pLogDataDesc.
762 */
763DECLINLINE(void) vhdxConvLogDataDescEndianess(VHDXECONV enmConv, PVhdxLogDataDesc pLogDataDescConv,
764 PVhdxLogDataDesc pLogDataDesc)
765{
766 pLogDataDescConv->u32DataSignature = SET_ENDIAN_U32(pLogDataDesc->u32DataSignature);
767 pLogDataDescConv->u32TrailingBytes = SET_ENDIAN_U32(pLogDataDesc->u32TrailingBytes);
768 pLogDataDescConv->u64LeadingBytes = SET_ENDIAN_U64(pLogDataDesc->u64LeadingBytes);
769 pLogDataDescConv->u64FileOffset = SET_ENDIAN_U64(pLogDataDesc->u64FileOffset);
770 pLogDataDescConv->u64SequenceNumber = SET_ENDIAN_U64(pLogDataDesc->u64SequenceNumber);
771}
772
773/**
774 * Converts a VHDX log data sector between file and host endianness.
775 *
776 * @returns nothing.
777 * @param enmConv Direction of the conversion.
778 * @param pLogDataSectorConv Where to store the converted log data sector.
779 * @param pLogDataSector The VHDX log data sector to convert.
780 *
781 * @note It is safe to use the same pointer for pLogDataSectorConv and pLogDataSector.
782 */
783DECLINLINE(void) vhdxConvLogDataSectorEndianess(VHDXECONV enmConv, PVhdxLogDataSector pLogDataSectorConv,
784 PVhdxLogDataSector pLogDataSector)
785{
786 pLogDataSectorConv->u32DataSignature = SET_ENDIAN_U32(pLogDataSector->u32DataSignature);
787 pLogDataSectorConv->u32SequenceHigh = SET_ENDIAN_U32(pLogDataSector->u32SequenceHigh);
788 pLogDataSectorConv->u32SequenceLow = SET_ENDIAN_U32(pLogDataSector->u32SequenceLow);
789}
790
791/**
792 * Converts a BAT between file and host endianess.
793 *
794 * @returns nothing.
795 * @param enmConv Direction of the conversion.
796 * @param paBatEntriesConv Where to store the converted BAT.
797 * @param paBatEntries The VHDX BAT to convert.
798 *
799 * @note It is safe to use the same pointer for paBatEntriesConv and paBatEntries.
800 */
801DECLINLINE(void) vhdxConvBatTableEndianess(VHDXECONV enmConv, PVhdxBatEntry paBatEntriesConv,
802 PVhdxBatEntry paBatEntries, uint32_t cBatEntries)
803{
804 for (uint32_t i = 0; i < cBatEntries; i++)
805 paBatEntriesConv[i].u64BatEntry = SET_ENDIAN_U64(paBatEntries[i].u64BatEntry);
806}
807
808/**
809 * Converts a VHDX metadata table header between file and host endianness.
810 *
811 * @returns nothing.
812 * @param enmConv Direction of the conversion.
813 * @param pMetadataTblHdrConv Where to store the converted metadata table header.
814 * @param pMetadataTblHdr The VHDX metadata table header to convert.
815 *
816 * @note It is safe to use the same pointer for pMetadataTblHdrConv and pMetadataTblHdr.
817 */
818DECLINLINE(void) vhdxConvMetadataTblHdrEndianess(VHDXECONV enmConv, PVhdxMetadataTblHdr pMetadataTblHdrConv,
819 PVhdxMetadataTblHdr pMetadataTblHdr)
820{
821 pMetadataTblHdrConv->u64Signature = SET_ENDIAN_U64(pMetadataTblHdr->u64Signature);
822 pMetadataTblHdrConv->u16Reserved = SET_ENDIAN_U16(pMetadataTblHdr->u16Reserved);
823 pMetadataTblHdrConv->u16EntryCount = SET_ENDIAN_U16(pMetadataTblHdr->u16EntryCount);
824 for (unsigned i = 0; i < RT_ELEMENTS(pMetadataTblHdr->u32Reserved2); i++)
825 pMetadataTblHdrConv->u32Reserved2[i] = SET_ENDIAN_U32(pMetadataTblHdr->u32Reserved2[i]);
826}
827
828/**
829 * Converts a VHDX metadata table entry between file and host endianness.
830 *
831 * @returns nothing.
832 * @param enmConv Direction of the conversion.
833 * @param pMetadataTblEntryConv Where to store the converted metadata table entry.
834 * @param pMetadataTblEntry The VHDX metadata table entry to convert.
835 *
836 * @note It is safe to use the same pointer for pMetadataTblEntryConv and pMetadataTblEntry.
837 */
838DECLINLINE(void) vhdxConvMetadataTblEntryEndianess(VHDXECONV enmConv, PVhdxMetadataTblEntry pMetadataTblEntryConv,
839 PVhdxMetadataTblEntry pMetadataTblEntry)
840{
841 vhdxConvUuidEndianess(enmConv, &pMetadataTblEntryConv->UuidItem, &pMetadataTblEntry->UuidItem);
842 pMetadataTblEntryConv->u32Offset = SET_ENDIAN_U32(pMetadataTblEntry->u32Offset);
843 pMetadataTblEntryConv->u32Length = SET_ENDIAN_U32(pMetadataTblEntry->u32Length);
844 pMetadataTblEntryConv->u32Flags = SET_ENDIAN_U32(pMetadataTblEntry->u32Flags);
845 pMetadataTblEntryConv->u32Reserved = SET_ENDIAN_U32(pMetadataTblEntry->u32Reserved);
846}
847
848/**
849 * Converts a VHDX file parameters item between file and host endianness.
850 *
851 * @returns nothing.
852 * @param enmConv Direction of the conversion.
853 * @param pFileParamsConv Where to store the converted file parameters item entry.
854 * @param pFileParams The VHDX file parameters item to convert.
855 *
856 * @note It is safe to use the same pointer for pFileParamsConv and pFileParams.
857 */
858DECLINLINE(void) vhdxConvFileParamsEndianess(VHDXECONV enmConv, PVhdxFileParameters pFileParamsConv,
859 PVhdxFileParameters pFileParams)
860{
861 pFileParamsConv->u32BlockSize = SET_ENDIAN_U32(pFileParams->u32BlockSize);
862 pFileParamsConv->u32Flags = SET_ENDIAN_U32(pFileParams->u32Flags);
863}
864
865/**
866 * Converts a VHDX virtual disk size item between file and host endianness.
867 *
868 * @returns nothing.
869 * @param enmConv Direction of the conversion.
870 * @param pVDiskSizeConv Where to store the converted virtual disk size item entry.
871 * @param pVDiskSize The VHDX virtual disk size item to convert.
872 *
873 * @note It is safe to use the same pointer for pVDiskSizeConv and pVDiskSize.
874 */
875DECLINLINE(void) vhdxConvVDiskSizeEndianess(VHDXECONV enmConv, PVhdxVDiskSize pVDiskSizeConv,
876 PVhdxVDiskSize pVDiskSize)
877{
878 pVDiskSizeConv->u64VDiskSize = SET_ENDIAN_U64(pVDiskSize->u64VDiskSize);
879}
880
881/**
882 * Converts a VHDX page 83 data item between file and host endianness.
883 *
884 * @returns nothing.
885 * @param enmConv Direction of the conversion.
886 * @param pPage83DataConv Where to store the converted page 83 data item entry.
887 * @param pPage83Data The VHDX page 83 data item to convert.
888 *
889 * @note It is safe to use the same pointer for pPage83DataConv and pPage83Data.
890 */
891DECLINLINE(void) vhdxConvPage83DataEndianess(VHDXECONV enmConv, PVhdxPage83Data pPage83DataConv,
892 PVhdxPage83Data pPage83Data)
893{
894 vhdxConvUuidEndianess(enmConv, &pPage83DataConv->UuidPage83Data, &pPage83Data->UuidPage83Data);
895}
896
897/**
898 * Converts a VHDX logical sector size item between file and host endianness.
899 *
900 * @returns nothing.
901 * @param enmConv Direction of the conversion.
902 * @param pVDiskLogSectSizeConv Where to store the converted logical sector size item entry.
903 * @param pVDiskLogSectSize The VHDX logical sector size item to convert.
904 *
905 * @note It is safe to use the same pointer for pVDiskLogSectSizeConv and pVDiskLogSectSize.
906 */
907DECLINLINE(void) vhdxConvVDiskLogSectSizeEndianess(VHDXECONV enmConv, PVhdxVDiskLogicalSectorSize pVDiskLogSectSizeConv,
908 PVhdxVDiskLogicalSectorSize pVDiskLogSectSize)
909{
910 pVDiskLogSectSizeConv->u32LogicalSectorSize = SET_ENDIAN_U32(pVDiskLogSectSize->u32LogicalSectorSize);
911}
912
913/**
914 * Converts a VHDX physical sector size item between file and host endianness.
915 *
916 * @returns nothing.
917 * @param enmConv Direction of the conversion.
918 * @param pVDiskPhysSectSizeConv Where to store the converted physical sector size item entry.
919 * @param pVDiskPhysSectSize The VHDX physical sector size item to convert.
920 *
921 * @note It is safe to use the same pointer for pVDiskPhysSectSizeConv and pVDiskPhysSectSize.
922 */
923DECLINLINE(void) vhdxConvVDiskPhysSectSizeEndianess(VHDXECONV enmConv, PVhdxVDiskPhysicalSectorSize pVDiskPhysSectSizeConv,
924 PVhdxVDiskPhysicalSectorSize pVDiskPhysSectSize)
925{
926 pVDiskPhysSectSizeConv->u64PhysicalSectorSize = SET_ENDIAN_U64(pVDiskPhysSectSize->u64PhysicalSectorSize);
927}
928
929/**
930 * Converts a VHDX parent locator header item between file and host endianness.
931 *
932 * @returns nothing.
933 * @param enmConv Direction of the conversion.
934 * @param pParentLocatorHdrConv Where to store the converted parent locator header item entry.
935 * @param pParentLocatorHdr The VHDX parent locator header item to convert.
936 *
937 * @note It is safe to use the same pointer for pParentLocatorHdrConv and pParentLocatorHdr.
938 */
939DECLINLINE(void) vhdxConvParentLocatorHeaderEndianness(VHDXECONV enmConv, PVhdxParentLocatorHeader pParentLocatorHdrConv,
940 PVhdxParentLocatorHeader pParentLocatorHdr)
941{
942 vhdxConvUuidEndianess(enmConv, &pParentLocatorHdrConv->UuidLocatorType, &pParentLocatorHdr->UuidLocatorType);
943 pParentLocatorHdrConv->u16Reserved = SET_ENDIAN_U16(pParentLocatorHdr->u16Reserved);
944 pParentLocatorHdrConv->u16KeyValueCount = SET_ENDIAN_U16(pParentLocatorHdr->u16KeyValueCount);
945}
946
947/**
948 * Converts a VHDX parent locator entry between file and host endianness.
949 *
950 * @returns nothing.
951 * @param enmConv Direction of the conversion.
952 * @param pParentLocatorEntryConv Where to store the converted parent locator entry.
953 * @param pParentLocatorEntry The VHDX parent locator entry to convert.
954 *
955 * @note It is safe to use the same pointer for pParentLocatorEntryConv and pParentLocatorEntry.
956 */
957DECLINLINE(void) vhdxConvParentLocatorEntryEndianess(VHDXECONV enmConv, PVhdxParentLocatorEntry pParentLocatorEntryConv,
958 PVhdxParentLocatorEntry pParentLocatorEntry)
959{
960 pParentLocatorEntryConv->u32KeyOffset = SET_ENDIAN_U32(pParentLocatorEntry->u32KeyOffset);
961 pParentLocatorEntryConv->u32ValueOffset = SET_ENDIAN_U32(pParentLocatorEntry->u32ValueOffset);
962 pParentLocatorEntryConv->u16KeyLength = SET_ENDIAN_U16(pParentLocatorEntry->u16KeyLength);
963 pParentLocatorEntryConv->u16ValueLength = SET_ENDIAN_U16(pParentLocatorEntry->u16ValueLength);
964}
965
966/**
967 * Internal. Free all allocated space for representing an image except pImage,
968 * and optionally delete the image from disk.
969 */
970static int vhdxFreeImage(PVHDXIMAGE pImage, bool fDelete)
971{
972 int rc = VINF_SUCCESS;
973
974 /* Freeing a never allocated image (e.g. because the open failed) is
975 * not signalled as an error. After all nothing bad happens. */
976 if (pImage)
977 {
978 if (pImage->pStorage)
979 {
980 rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
981 pImage->pStorage = NULL;
982 }
983
984 if (pImage->paBat)
985 {
986 RTMemFree(pImage->paBat);
987 pImage->paBat = NULL;
988 }
989
990 if (fDelete && pImage->pszFilename)
991 vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
992 }
993
994 LogFlowFunc(("returns %Rrc\n", rc));
995 return rc;
996}
997
998/**
999 * Loads all required fields from the given VHDX header.
1000 * The header must be converted to the host endianess and validated already.
1001 *
1002 * @returns VBox status code.
1003 * @param pImage Image instance data.
1004 * @param pHdr The header to load.
1005 */
1006static int vhdxLoadHeader(PVHDXIMAGE pImage, PVhdxHeader pHdr)
1007{
1008 int rc = VINF_SUCCESS;
1009
1010 LogFlowFunc(("pImage=%#p pHdr=%#p\n", pImage, pHdr));
1011
1012 /*
1013 * Most fields in the header are not required because the backend implements
1014 * readonly access only so far.
1015 * We just have to check that the log is empty, we have to refuse to load the
1016 * image otherwsie because replaying the log is not implemented.
1017 */
1018 if (pHdr->u16Version == VHDX_HEADER_VHDX_VERSION)
1019 {
1020 /* Check that the log UUID is zero. */
1021 pImage->uVersion = pHdr->u16Version;
1022 if (!RTUuidIsNull(&pHdr->UuidLog))
1023 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1024 "VHDX: Image \'%s\' has a non empty log which is not supported",
1025 pImage->pszFilename);
1026 }
1027 else
1028 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1029 "VHDX: Image \'%s\' uses an unsupported version (%u) of the VHDX format",
1030 pImage->pszFilename, pHdr->u16Version);
1031
1032 LogFlowFunc(("return rc=%Rrc\n", rc));
1033 return rc;
1034}
1035
1036/**
1037 * Determines the current header and loads it.
1038 *
1039 * @returns VBox status code.
1040 * @param pImage Image instance data.
1041 */
1042static int vhdxFindAndLoadCurrentHeader(PVHDXIMAGE pImage)
1043{
1044 PVhdxHeader pHdr1, pHdr2;
1045 uint32_t u32ChkSum = 0;
1046 uint32_t u32ChkSumSaved = 0;
1047 bool fHdr1Valid = false;
1048 bool fHdr2Valid = false;
1049 int rc = VINF_SUCCESS;
1050
1051 LogFlowFunc(("pImage=%#p\n", pImage));
1052
1053 /*
1054 * The VHDX format defines two headers at different offsets to provide failure
1055 * consistency. Only one header is current. This can be determined using the
1056 * sequence number and checksum fields in the header.
1057 */
1058 pHdr1 = (PVhdxHeader)RTMemAllocZ(sizeof(VhdxHeader));
1059 pHdr2 = (PVhdxHeader)RTMemAllocZ(sizeof(VhdxHeader));
1060
1061 if (pHdr1 && pHdr2)
1062 {
1063 /* Read the first header. */
1064 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, VHDX_HEADER1_OFFSET,
1065 pHdr1, sizeof(*pHdr1));
1066 if (RT_SUCCESS(rc))
1067 {
1068 vhdxConvHeaderEndianess(VHDXECONV_F2H, pHdr1, pHdr1);
1069
1070 /* Validate checksum. */
1071 u32ChkSumSaved = pHdr1->u32Checksum;
1072 pHdr1->u32Checksum = 0;
1073 u32ChkSum = RTCrc32C(pHdr1, sizeof(VhdxHeader));
1074
1075 if ( pHdr1->u32Signature == VHDX_HEADER_SIGNATURE
1076 && u32ChkSum == u32ChkSumSaved)
1077 fHdr1Valid = true;
1078 }
1079
1080 /* Try to read the second header in any case (even if reading the first failed). */
1081 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, VHDX_HEADER2_OFFSET,
1082 pHdr2, sizeof(*pHdr2));
1083 if (RT_SUCCESS(rc))
1084 {
1085 vhdxConvHeaderEndianess(VHDXECONV_F2H, pHdr2, pHdr2);
1086
1087 /* Validate checksum. */
1088 u32ChkSumSaved = pHdr2->u32Checksum;
1089 pHdr2->u32Checksum = 0;
1090 u32ChkSum = RTCrc32C(pHdr2, sizeof(VhdxHeader));
1091
1092 if ( pHdr2->u32Signature == VHDX_HEADER_SIGNATURE
1093 && u32ChkSum == u32ChkSumSaved)
1094 fHdr2Valid = true;
1095 }
1096
1097 /* Determine the current header. */
1098 if (fHdr1Valid != fHdr2Valid)
1099 {
1100 /* Only one header is valid - use it. */
1101 rc = vhdxLoadHeader(pImage, fHdr1Valid ? pHdr1 : pHdr2);
1102 }
1103 else if (!fHdr1Valid && !fHdr2Valid)
1104 {
1105 /* Crap, both headers are corrupt, refuse to load the image. */
1106 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1107 "VHDX: Can not load the image because both headers are corrupt");
1108 }
1109 else
1110 {
1111 /* Both headers are valid. Use the sequence number to find the current one. */
1112 if (pHdr1->u64SequenceNumber > pHdr2->u64SequenceNumber)
1113 rc = vhdxLoadHeader(pImage, pHdr1);
1114 else
1115 rc = vhdxLoadHeader(pImage, pHdr2);
1116 }
1117 }
1118 else
1119 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1120 "VHDX: Out of memory while allocating memory for the header");
1121
1122 if (pHdr1)
1123 RTMemFree(pHdr1);
1124 if (pHdr2)
1125 RTMemFree(pHdr2);
1126
1127 LogFlowFunc(("returns rc=%Rrc\n", rc));
1128 return rc;
1129}
1130
1131/**
1132 * Loads the BAT region.
1133 *
1134 * @returns VBox status code.
1135 * @param pImage Image instance data.
1136 * @param offRegion Start offset of the region.
1137 * @param cbRegion Size of the region.
1138 */
1139static int vhdxLoadBatRegion(PVHDXIMAGE pImage, uint64_t offRegion,
1140 size_t cbRegion)
1141{
1142 int rc = VINF_SUCCESS;
1143 uint32_t cDataBlocks;
1144 uint32_t uChunkRatio;
1145 uint32_t cSectorBitmapBlocks;
1146 uint32_t cBatEntries;
1147 uint32_t cbBatEntries;
1148 PVhdxBatEntry paBatEntries = NULL;
1149
1150 LogFlowFunc(("pImage=%#p\n", pImage));
1151
1152 /* Calculate required values first. */
1153 uint64_t uChunkRatio64 = (RT_BIT_64(23) * pImage->cbLogicalSector) / pImage->cbBlock;
1154 uChunkRatio = (uint32_t)uChunkRatio64; Assert(uChunkRatio == uChunkRatio64);
1155 uint64_t cDataBlocks64 = pImage->cbSize / pImage->cbBlock;
1156 cDataBlocks = (uint32_t)cDataBlocks64; Assert(cDataBlocks == cDataBlocks64);
1157
1158 if (pImage->cbSize % pImage->cbBlock)
1159 cDataBlocks++;
1160
1161 cSectorBitmapBlocks = cDataBlocks / uChunkRatio;
1162 if (cDataBlocks % uChunkRatio)
1163 cSectorBitmapBlocks++;
1164
1165 cBatEntries = cDataBlocks + (cDataBlocks - 1)/uChunkRatio;
1166 cbBatEntries = cBatEntries * sizeof(VhdxBatEntry);
1167
1168 if (cbBatEntries <= cbRegion)
1169 {
1170 /*
1171 * Load the complete BAT region first, convert to host endianess and process
1172 * it afterwards. The SB entries can be removed because they are not needed yet.
1173 */
1174 paBatEntries = (PVhdxBatEntry)RTMemAlloc(cbBatEntries);
1175 if (paBatEntries)
1176 {
1177 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offRegion,
1178 paBatEntries, cbBatEntries);
1179 if (RT_SUCCESS(rc))
1180 {
1181 vhdxConvBatTableEndianess(VHDXECONV_F2H, paBatEntries, paBatEntries,
1182 cBatEntries);
1183
1184 /* Go through the table and validate it. */
1185 for (unsigned i = 0; i < cBatEntries; i++)
1186 {
1187 if ( i != 0
1188 && (i % uChunkRatio) == 0)
1189 {
1190/**
1191 * Disabled the verification because there are images out there with the sector bitmap
1192 * marked as present. The entry is never accessed and the image is readonly anyway,
1193 * so no harm done.
1194 */
1195#if 0
1196 /* Sector bitmap block. */
1197 if ( VHDX_BAT_ENTRY_GET_STATE(paBatEntries[i].u64BatEntry)
1198 != VHDX_BAT_ENTRY_SB_BLOCK_NOT_PRESENT)
1199 {
1200 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1201 "VHDX: Sector bitmap block at entry %u of image \'%s\' marked as present, violation of the specification",
1202 i, pImage->pszFilename);
1203 break;
1204 }
1205#endif
1206 }
1207 else
1208 {
1209 /* Payload block. */
1210 if ( VHDX_BAT_ENTRY_GET_STATE(paBatEntries[i].u64BatEntry)
1211 == VHDX_BAT_ENTRY_PAYLOAD_BLOCK_PARTIALLY_PRESENT)
1212 {
1213 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1214 "VHDX: Payload block at entry %u of image \'%s\' marked as partially present, violation of the specification",
1215 i, pImage->pszFilename);
1216 break;
1217 }
1218 }
1219 }
1220
1221 if (RT_SUCCESS(rc))
1222 {
1223 pImage->paBat = paBatEntries;
1224 pImage->uChunkRatio = uChunkRatio;
1225 }
1226 }
1227 else
1228 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1229 "VHDX: Error reading the BAT from image \'%s\'",
1230 pImage->pszFilename);
1231 }
1232 else
1233 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1234 "VHDX: Out of memory allocating memory for %u BAT entries of image \'%s\'",
1235 cBatEntries, pImage->pszFilename);
1236 }
1237 else
1238 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1239 "VHDX: Mismatch between calculated number of BAT entries and region size (expected %u got %u) for image \'%s\'",
1240 cbBatEntries, cbRegion, pImage->pszFilename);
1241
1242 if ( RT_FAILURE(rc)
1243 && paBatEntries)
1244 RTMemFree(paBatEntries);
1245
1246 LogFlowFunc(("returns rc=%Rrc\n", rc));
1247 return rc;
1248}
1249
1250/**
1251 * Load the file parameters metadata item from the file.
1252 *
1253 * @returns VBox status code.
1254 * @param pImage Image instance data.
1255 * @param offItem File offset where the data is stored.
1256 * @param cbItem Size of the item in the file.
1257 */
1258static int vhdxLoadFileParametersMetadata(PVHDXIMAGE pImage, uint64_t offItem, size_t cbItem)
1259{
1260 int rc = VINF_SUCCESS;
1261
1262 LogFlowFunc(("pImage=%#p offItem=%llu cbItem=%zu\n", pImage, offItem, cbItem));
1263
1264 if (cbItem != sizeof(VhdxFileParameters))
1265 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1266 "VHDX: File parameters item size mismatch (expected %u got %zu) in image \'%s\'",
1267 sizeof(VhdxFileParameters), cbItem, pImage->pszFilename);
1268 else
1269 {
1270 VhdxFileParameters FileParameters;
1271
1272 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offItem,
1273 &FileParameters, sizeof(FileParameters));
1274 if (RT_SUCCESS(rc))
1275 {
1276 vhdxConvFileParamsEndianess(VHDXECONV_F2H, &FileParameters, &FileParameters);
1277 pImage->cbBlock = FileParameters.u32BlockSize;
1278
1279 /* @todo: No support for differencing images yet. */
1280 if (FileParameters.u32Flags & VHDX_FILE_PARAMETERS_FLAGS_HAS_PARENT)
1281 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1282 "VHDX: Image \'%s\' is a differencing image which is not supported yet",
1283 pImage->pszFilename);
1284 }
1285 else
1286 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1287 "VHDX: Reading the file parameters metadata item from image \'%s\' failed",
1288 pImage->pszFilename);
1289 }
1290
1291 LogFlowFunc(("returns rc=%Rrc\n", rc));
1292 return rc;
1293}
1294
1295/**
1296 * Load the virtual disk size metadata item from the file.
1297 *
1298 * @returns VBox status code.
1299 * @param pImage Image instance data.
1300 * @param offItem File offset where the data is stored.
1301 * @param cbItem Size of the item in the file.
1302 */
1303static int vhdxLoadVDiskSizeMetadata(PVHDXIMAGE pImage, uint64_t offItem, size_t cbItem)
1304{
1305 int rc = VINF_SUCCESS;
1306
1307 LogFlowFunc(("pImage=%#p offItem=%llu cbItem=%zu\n", pImage, offItem, cbItem));
1308
1309 if (cbItem != sizeof(VhdxVDiskSize))
1310 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1311 "VHDX: Virtual disk size item size mismatch (expected %u got %zu) in image \'%s\'",
1312 sizeof(VhdxVDiskSize), cbItem, pImage->pszFilename);
1313 else
1314 {
1315 VhdxVDiskSize VDiskSize;
1316
1317 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offItem,
1318 &VDiskSize, sizeof(VDiskSize));
1319 if (RT_SUCCESS(rc))
1320 {
1321 vhdxConvVDiskSizeEndianess(VHDXECONV_F2H, &VDiskSize, &VDiskSize);
1322 pImage->cbSize = VDiskSize.u64VDiskSize;
1323 }
1324 else
1325 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1326 "VHDX: Reading the virtual disk size metadata item from image \'%s\' failed",
1327 pImage->pszFilename);
1328 }
1329
1330 LogFlowFunc(("returns rc=%Rrc\n", rc));
1331 return rc;
1332}
1333
1334/**
1335 * Load the logical sector size metadata item from the file.
1336 *
1337 * @returns VBox status code.
1338 * @param pImage Image instance data.
1339 * @param offItem File offset where the data is stored.
1340 * @param cbItem Size of the item in the file.
1341 */
1342static int vhdxLoadVDiskLogSectorSizeMetadata(PVHDXIMAGE pImage, uint64_t offItem, size_t cbItem)
1343{
1344 int rc = VINF_SUCCESS;
1345
1346 LogFlowFunc(("pImage=%#p offItem=%llu cbItem=%zu\n", pImage, offItem, cbItem));
1347
1348 if (cbItem != sizeof(VhdxVDiskLogicalSectorSize))
1349 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1350 "VHDX: Virtual disk logical sector size item size mismatch (expected %u got %zu) in image \'%s\'",
1351 sizeof(VhdxVDiskLogicalSectorSize), cbItem, pImage->pszFilename);
1352 else
1353 {
1354 VhdxVDiskLogicalSectorSize VDiskLogSectSize;
1355
1356 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offItem,
1357 &VDiskLogSectSize, sizeof(VDiskLogSectSize));
1358 if (RT_SUCCESS(rc))
1359 {
1360 vhdxConvVDiskLogSectSizeEndianess(VHDXECONV_F2H, &VDiskLogSectSize,
1361 &VDiskLogSectSize);
1362 pImage->cbLogicalSector = VDiskLogSectSize.u32LogicalSectorSize;
1363 }
1364 else
1365 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1366 "VHDX: Reading the virtual disk logical sector size metadata item from image \'%s\' failed",
1367 pImage->pszFilename);
1368 }
1369
1370 LogFlowFunc(("returns rc=%Rrc\n", rc));
1371 return rc;
1372}
1373
1374/**
1375 * Loads the metadata region.
1376 *
1377 * @returns VBox status code.
1378 * @param pImage Image instance data.
1379 * @param offRegion Start offset of the region.
1380 * @param cbRegion Size of the region.
1381 */
1382static int vhdxLoadMetadataRegion(PVHDXIMAGE pImage, uint64_t offRegion,
1383 size_t cbRegion)
1384{
1385 VhdxMetadataTblHdr MetadataTblHdr;
1386 int rc = VINF_SUCCESS;
1387
1388 LogFlowFunc(("pImage=%#p\n", pImage));
1389
1390 /* Load the header first. */
1391 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offRegion,
1392 &MetadataTblHdr, sizeof(MetadataTblHdr));
1393 if (RT_SUCCESS(rc))
1394 {
1395 vhdxConvMetadataTblHdrEndianess(VHDXECONV_F2H, &MetadataTblHdr, &MetadataTblHdr);
1396
1397 /* Validate structure. */
1398 if (MetadataTblHdr.u64Signature != VHDX_METADATA_TBL_HDR_SIGNATURE)
1399 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1400 "VHDX: Incorrect metadata table header signature for image \'%s\'",
1401 pImage->pszFilename);
1402 else if (MetadataTblHdr.u16EntryCount > VHDX_METADATA_TBL_HDR_ENTRY_COUNT_MAX)
1403 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1404 "VHDX: Incorrect entry count in metadata table header of image \'%s\'",
1405 pImage->pszFilename);
1406 else if (cbRegion < (MetadataTblHdr.u16EntryCount * sizeof(VhdxMetadataTblEntry) + sizeof(VhdxMetadataTblHdr)))
1407 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1408 "VHDX: Metadata table of image \'%s\' exceeds region size",
1409 pImage->pszFilename);
1410
1411 if (RT_SUCCESS(rc))
1412 {
1413 uint64_t offMetadataTblEntry = offRegion + sizeof(VhdxMetadataTblHdr);
1414
1415 for (unsigned i = 0; i < MetadataTblHdr.u16EntryCount; i++)
1416 {
1417 uint64_t offMetadataItem = 0;
1418 VHDXMETADATAITEM enmMetadataItem = VHDXMETADATAITEM_UNKNOWN;
1419 VhdxMetadataTblEntry MetadataTblEntry;
1420
1421 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offMetadataTblEntry,
1422 &MetadataTblEntry, sizeof(MetadataTblEntry));
1423 if (RT_FAILURE(rc))
1424 {
1425 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1426 "VHDX: Reading metadata table entry from image \'%s\' failed",
1427 pImage->pszFilename);
1428 break;
1429 }
1430
1431 vhdxConvMetadataTblEntryEndianess(VHDXECONV_F2H, &MetadataTblEntry, &MetadataTblEntry);
1432
1433 /* Check whether the flags match the expectations. */
1434 for (unsigned idxProp = 0; idxProp < RT_ELEMENTS(s_aVhdxMetadataItemProps); idxProp++)
1435 {
1436 if (!RTUuidCompareStr(&MetadataTblEntry.UuidItem,
1437 s_aVhdxMetadataItemProps[idxProp].pszItemUuid))
1438 {
1439 /*
1440 * Check for specification violations and bail out, except
1441 * for the required flag of the physical sector size metadata item.
1442 * Early images had the required flag not set opposed to the specification.
1443 * We don't want to brerak those images.
1444 */
1445 if ( !!(MetadataTblEntry.u32Flags & VHDX_METADATA_TBL_ENTRY_FLAGS_IS_USER)
1446 != s_aVhdxMetadataItemProps[idxProp].fIsUser)
1447 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1448 "VHDX: User flag of metadata item does not meet expectations \'%s\'",
1449 pImage->pszFilename);
1450 else if ( !!(MetadataTblEntry.u32Flags & VHDX_METADATA_TBL_ENTRY_FLAGS_IS_VDISK)
1451 != s_aVhdxMetadataItemProps[idxProp].fIsVDisk)
1452 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1453 "VHDX: Virtual disk flag of metadata item does not meet expectations \'%s\'",
1454 pImage->pszFilename);
1455 else if ( !!(MetadataTblEntry.u32Flags & VHDX_METADATA_TBL_ENTRY_FLAGS_IS_REQUIRED)
1456 != s_aVhdxMetadataItemProps[idxProp].fIsRequired
1457 && (s_aVhdxMetadataItemProps[idxProp].enmMetadataItem != VHDXMETADATAITEM_PHYSICAL_SECTOR_SIZE))
1458 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1459 "VHDX: Required flag of metadata item does not meet expectations \'%s\'",
1460 pImage->pszFilename);
1461 else
1462 enmMetadataItem = s_aVhdxMetadataItemProps[idxProp].enmMetadataItem;
1463
1464 break;
1465 }
1466 }
1467
1468 if (RT_FAILURE(rc))
1469 break;
1470
1471 offMetadataItem = offRegion + MetadataTblEntry.u32Offset;
1472
1473 switch (enmMetadataItem)
1474 {
1475 case VHDXMETADATAITEM_FILE_PARAMS:
1476 {
1477 rc = vhdxLoadFileParametersMetadata(pImage, offMetadataItem,
1478 MetadataTblEntry.u32Length);
1479 break;
1480 }
1481 case VHDXMETADATAITEM_VDISK_SIZE:
1482 {
1483 rc = vhdxLoadVDiskSizeMetadata(pImage, offMetadataItem,
1484 MetadataTblEntry.u32Length);
1485 break;
1486 }
1487 case VHDXMETADATAITEM_PAGE83_DATA:
1488 {
1489 /*
1490 * Nothing to do here for now (marked as required but
1491 * there is no API to pass this information to the caller)
1492 * so far.
1493 */
1494 break;
1495 }
1496 case VHDXMETADATAITEM_LOGICAL_SECTOR_SIZE:
1497 {
1498 rc = vhdxLoadVDiskLogSectorSizeMetadata(pImage, offMetadataItem,
1499 MetadataTblEntry.u32Length);
1500 break;
1501 }
1502 case VHDXMETADATAITEM_PHYSICAL_SECTOR_SIZE:
1503 {
1504 /*
1505 * Nothing to do here for now (marked as required but
1506 * there is no API to pass this information to the caller)
1507 * so far.
1508 */
1509 break;
1510 }
1511 case VHDXMETADATAITEM_PARENT_LOCATOR:
1512 {
1513 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1514 "VHDX: Image \'%s\' is a differencing image which is not supported yet",
1515 pImage->pszFilename);
1516 break;
1517 }
1518 case VHDXMETADATAITEM_UNKNOWN:
1519 default:
1520 if (MetadataTblEntry.u32Flags & VHDX_METADATA_TBL_ENTRY_FLAGS_IS_REQUIRED)
1521 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1522 "VHDX: Unsupported but required metadata item in image \'%s\'",
1523 pImage->pszFilename);
1524 }
1525
1526 if (RT_FAILURE(rc))
1527 break;
1528
1529 offMetadataTblEntry += sizeof(MetadataTblEntry);
1530 }
1531 }
1532 }
1533 else
1534 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1535 "VHDX: Reading the metadata table header for image \'%s\' failed",
1536 pImage->pszFilename);
1537
1538 LogFlowFunc(("returns rc=%Rrc\n", rc));
1539 return rc;
1540}
1541
1542/**
1543 * Loads the region table and the associated regions.
1544 *
1545 * @returns VBox status code.
1546 * @param pImage Image instance data.
1547 */
1548static int vhdxLoadRegionTable(PVHDXIMAGE pImage)
1549{
1550 uint8_t *pbRegionTbl = NULL;
1551 int rc = VINF_SUCCESS;
1552
1553 LogFlowFunc(("pImage=%#p\n", pImage));
1554
1555 /* Load the complete region table into memory. */
1556 pbRegionTbl = (uint8_t *)RTMemTmpAlloc(VHDX_REGION_TBL_SIZE_MAX);
1557 if (pbRegionTbl)
1558 {
1559 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, VHDX_REGION_TBL_HDR_OFFSET,
1560 pbRegionTbl, VHDX_REGION_TBL_SIZE_MAX);
1561 if (RT_SUCCESS(rc))
1562 {
1563 PVhdxRegionTblHdr pRegionTblHdr;
1564 VhdxRegionTblHdr RegionTblHdr;
1565 uint32_t u32ChkSumSaved = 0;
1566 uint32_t u32ChkSum = 0;
1567
1568 /*
1569 * Copy the region table header to a dedicated structure where we can
1570 * convert it to host endianess.
1571 */
1572 memcpy(&RegionTblHdr, pbRegionTbl, sizeof(RegionTblHdr));
1573 vhdxConvRegionTblHdrEndianess(VHDXECONV_F2H, &RegionTblHdr, &RegionTblHdr);
1574
1575 /* Set checksum field to 0 during crc computation. */
1576 pRegionTblHdr = (PVhdxRegionTblHdr)pbRegionTbl;
1577 pRegionTblHdr->u32Checksum = 0;
1578
1579 /* Verify the region table integrity. */
1580 u32ChkSum = RTCrc32C(pbRegionTbl, VHDX_REGION_TBL_SIZE_MAX);
1581
1582 if (RegionTblHdr.u32Signature != VHDX_REGION_TBL_HDR_SIGNATURE)
1583 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1584 "VHDX: Invalid signature for region table header of image \'%s\'",
1585 pImage->pszFilename);
1586 else if (u32ChkSum != RegionTblHdr.u32Checksum)
1587 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1588 "VHDX: CRC32 checksum mismatch for the region table of image \'%s\' (expected %#x got %#x)",
1589 pImage->pszFilename, RegionTblHdr.u32Checksum, u32ChkSum);
1590 else if (RegionTblHdr.u32EntryCount > VHDX_REGION_TBL_HDR_ENTRY_COUNT_MAX)
1591 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1592 "VHDX: Invalid entry count field in the region table header of image \'%s\'",
1593 pImage->pszFilename);
1594
1595 if (RT_SUCCESS(rc))
1596 {
1597 /* Parse the region table entries. */
1598 PVhdxRegionTblEntry pRegTblEntry = (PVhdxRegionTblEntry)(pbRegionTbl + sizeof(VhdxRegionTblHdr));
1599 VhdxRegionTblEntry RegTblEntryBat; /* BAT region table entry. */
1600 bool fBatRegPresent = false;
1601 RT_ZERO(RegTblEntryBat); /* Maybe uninitialized, gcc. */
1602
1603 for (unsigned i = 0; i < RegionTblHdr.u32EntryCount; i++)
1604 {
1605 vhdxConvRegionTblEntryEndianess(VHDXECONV_F2H, pRegTblEntry, pRegTblEntry);
1606
1607 /* Check the uuid for known regions. */
1608 if (!RTUuidCompareStr(&pRegTblEntry->UuidObject, VHDX_REGION_TBL_ENTRY_UUID_BAT))
1609 {
1610 /*
1611 * Save the BAT region and process it later.
1612 * It may come before the metadata region but needs the block size.
1613 */
1614 if (pRegTblEntry->u32Flags & VHDX_REGION_TBL_ENTRY_FLAGS_IS_REQUIRED)
1615 {
1616 fBatRegPresent = true;
1617 RegTblEntryBat.u32Length = pRegTblEntry->u32Length;
1618 RegTblEntryBat.u64FileOffset = pRegTblEntry->u64FileOffset;
1619 }
1620 else
1621 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1622 "VHDX: BAT region not marked as required in image \'%s\'",
1623 pImage->pszFilename);
1624 }
1625 else if (!RTUuidCompareStr(&pRegTblEntry->UuidObject, VHDX_REGION_TBL_ENTRY_UUID_METADATA))
1626 {
1627 if (pRegTblEntry->u32Flags & VHDX_REGION_TBL_ENTRY_FLAGS_IS_REQUIRED)
1628 rc = vhdxLoadMetadataRegion(pImage, pRegTblEntry->u64FileOffset, pRegTblEntry->u32Length);
1629 else
1630 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1631 "VHDX: Metadata region not marked as required in image \'%s\'",
1632 pImage->pszFilename);
1633 }
1634 else if (pRegTblEntry->u32Flags & VHDX_REGION_TBL_ENTRY_FLAGS_IS_REQUIRED)
1635 {
1636 /* The region is not known but marked as required, fail to load the image. */
1637 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1638 "VHDX: Unknown required region in image \'%s\'",
1639 pImage->pszFilename);
1640 }
1641
1642 if (RT_FAILURE(rc))
1643 break;
1644
1645 pRegTblEntry++;
1646 }
1647
1648 if (fBatRegPresent)
1649 rc = vhdxLoadBatRegion(pImage, RegTblEntryBat.u64FileOffset, RegTblEntryBat.u32Length);
1650 else
1651 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1652 "VHDX: BAT region in image \'%s\' is missing",
1653 pImage->pszFilename);
1654 }
1655 }
1656 else
1657 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1658 "VHDX: Reading the region table for image \'%s\' failed",
1659 pImage->pszFilename);
1660 }
1661 else
1662 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1663 "VHDX: Out of memory allocating memory for the region table of image \'%s\'",
1664 pImage->pszFilename);
1665
1666 if (pbRegionTbl)
1667 RTMemTmpFree(pbRegionTbl);
1668
1669 LogFlowFunc(("returns rc=%Rrc\n", rc));
1670 return rc;
1671}
1672
1673/**
1674 * Internal: Open an image, constructing all necessary data structures.
1675 */
1676static int vhdxOpenImage(PVHDXIMAGE pImage, unsigned uOpenFlags)
1677{
1678 uint64_t cbFile = 0;
1679 VhdxFileIdentifier FileIdentifier;
1680 int rc = VINF_SUCCESS;
1681
1682 LogFlowFunc(("pImage=%#p uOpenFlags=%#x\n", pImage, uOpenFlags));
1683 pImage->uOpenFlags = uOpenFlags;
1684
1685 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
1686 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
1687 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
1688
1689 /* Refuse write access, it is not implemented so far. */
1690 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
1691 return VERR_NOT_SUPPORTED;
1692
1693 /*
1694 * Open the image.
1695 */
1696 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
1697 VDOpenFlagsToFileOpenFlags(uOpenFlags,
1698 false /* fCreate */),
1699 &pImage->pStorage);
1700
1701 /* Do NOT signal an appropriate error here, as the VD layer has the
1702 * choice of retrying the open if it failed. */
1703 if (RT_SUCCESS(rc))
1704 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
1705
1706 if (RT_SUCCESS(rc))
1707 {
1708 if (cbFile > sizeof(FileIdentifier))
1709 {
1710 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, VHDX_FILE_IDENTIFIER_OFFSET,
1711 &FileIdentifier, sizeof(FileIdentifier));
1712 if (RT_SUCCESS(rc))
1713 {
1714 vhdxConvFileIdentifierEndianess(VHDXECONV_F2H, &FileIdentifier,
1715 &FileIdentifier);
1716 if (FileIdentifier.u64Signature != VHDX_FILE_IDENTIFIER_SIGNATURE)
1717 rc = VERR_VD_GEN_INVALID_HEADER;
1718 else
1719 rc = vhdxFindAndLoadCurrentHeader(pImage);
1720
1721 /* Load the region table. */
1722 if (RT_SUCCESS(rc))
1723 rc = vhdxLoadRegionTable(pImage);
1724 }
1725 }
1726 else
1727 rc = VERR_VD_GEN_INVALID_HEADER;
1728 }
1729
1730 if (RT_FAILURE(rc))
1731 vhdxFreeImage(pImage, false);
1732
1733 LogFlowFunc(("returns rc=%Rrc\n", rc));
1734 return rc;
1735}
1736
1737
1738/** @copydoc VBOXHDDBACKEND::pfnCheckIfValid */
1739static DECLCALLBACK(int) vhdxCheckIfValid(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
1740 PVDINTERFACE pVDIfsImage, VDTYPE *penmType)
1741{
1742 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
1743 PVDIOSTORAGE pStorage = NULL;
1744 uint64_t cbFile;
1745 int rc = VINF_SUCCESS;
1746 VhdxFileIdentifier FileIdentifier;
1747
1748 PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
1749 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
1750
1751 if ( !VALID_PTR(pszFilename)
1752 || !*pszFilename)
1753 rc = VERR_INVALID_PARAMETER;
1754 else
1755 {
1756 /*
1757 * Open the file and read the file identifier.
1758 */
1759 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
1760 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
1761 false /* fCreate */),
1762 &pStorage);
1763 if (RT_SUCCESS(rc))
1764 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
1765
1766 if (RT_SUCCESS(rc))
1767 {
1768 if (cbFile > sizeof(FileIdentifier))
1769 {
1770 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, VHDX_FILE_IDENTIFIER_OFFSET,
1771 &FileIdentifier, sizeof(FileIdentifier));
1772 if (RT_SUCCESS(rc))
1773 {
1774 vhdxConvFileIdentifierEndianess(VHDXECONV_F2H, &FileIdentifier,
1775 &FileIdentifier);
1776 if (FileIdentifier.u64Signature != VHDX_FILE_IDENTIFIER_SIGNATURE)
1777 rc = VERR_VD_GEN_INVALID_HEADER;
1778 else
1779 *penmType = VDTYPE_HDD;
1780 }
1781 }
1782 else
1783 rc = VERR_VD_GEN_INVALID_HEADER;
1784 }
1785
1786 if (pStorage)
1787 vdIfIoIntFileClose(pIfIo, pStorage);
1788 }
1789
1790 LogFlowFunc(("returns %Rrc\n", rc));
1791 return rc;
1792}
1793
1794/** @copydoc VBOXHDDBACKEND::pfnOpen */
1795static DECLCALLBACK(int) vhdxOpen(const char *pszFilename, unsigned uOpenFlags,
1796 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1797 VDTYPE enmType, void **ppBackendData)
1798{
1799 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
1800 int rc;
1801 PVHDXIMAGE pImage;
1802
1803 NOREF(enmType); /**< @todo r=klaus make use of the type info. */
1804
1805 /* Check open flags. All valid flags are supported. */
1806 if ( uOpenFlags & ~VD_OPEN_FLAGS_MASK
1807 || !VALID_PTR(pszFilename)
1808 || !*pszFilename)
1809 rc = VERR_INVALID_PARAMETER;
1810 else
1811 {
1812 pImage = (PVHDXIMAGE)RTMemAllocZ(sizeof(VHDXIMAGE));
1813 if (!pImage)
1814 rc = VERR_NO_MEMORY;
1815 else
1816 {
1817 pImage->pszFilename = pszFilename;
1818 pImage->pStorage = NULL;
1819 pImage->pVDIfsDisk = pVDIfsDisk;
1820 pImage->pVDIfsImage = pVDIfsImage;
1821
1822 rc = vhdxOpenImage(pImage, uOpenFlags);
1823 if (RT_SUCCESS(rc))
1824 *ppBackendData = pImage;
1825 else
1826 RTMemFree(pImage);
1827 }
1828 }
1829
1830 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1831 return rc;
1832}
1833
1834/** @interface_method_impl{VBOXHDDBACKEND,pfnCreate} */
1835static DECLCALLBACK(int) vhdxCreate(const char *pszFilename, uint64_t cbSize,
1836 unsigned uImageFlags, const char *pszComment,
1837 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1838 PCRTUUID pUuid, unsigned uOpenFlags,
1839 unsigned uPercentStart, unsigned uPercentSpan,
1840 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1841 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
1842 void **ppBackendData)
1843{
1844 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",
1845 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
1846 int rc = VERR_NOT_SUPPORTED;
1847
1848 LogFlowFunc(("returns %Rrc\n", rc));
1849 return rc;
1850}
1851
1852/** @copydoc VBOXHDDBACKEND::pfnRename */
1853static DECLCALLBACK(int) vhdxRename(void *pBackendData, const char *pszFilename)
1854{
1855 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1856 int rc = VINF_SUCCESS;
1857 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
1858
1859 /* Check arguments. */
1860 if ( !pImage
1861 || !pszFilename
1862 || !*pszFilename)
1863 rc = VERR_INVALID_PARAMETER;
1864 else
1865 {
1866 /* Close the image. */
1867 rc = vhdxFreeImage(pImage, false);
1868 if (RT_SUCCESS(rc))
1869 {
1870 /* Rename the file. */
1871 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
1872 if (RT_FAILURE(rc))
1873 {
1874 /* The move failed, try to reopen the original image. */
1875 int rc2 = vhdxOpenImage(pImage, pImage->uOpenFlags);
1876 if (RT_FAILURE(rc2))
1877 rc = rc2;
1878 }
1879 else
1880 {
1881 /* Update pImage with the new information. */
1882 pImage->pszFilename = pszFilename;
1883
1884 /* Open the old image with new name. */
1885 rc = vhdxOpenImage(pImage, pImage->uOpenFlags);
1886 }
1887 }
1888 }
1889
1890 LogFlowFunc(("returns %Rrc\n", rc));
1891 return rc;
1892}
1893
1894/** @copydoc VBOXHDDBACKEND::pfnClose */
1895static DECLCALLBACK(int) vhdxClose(void *pBackendData, bool fDelete)
1896{
1897 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1898 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
1899 int rc;
1900
1901 rc = vhdxFreeImage(pImage, fDelete);
1902 RTMemFree(pImage);
1903
1904 LogFlowFunc(("returns %Rrc\n", rc));
1905 return rc;
1906}
1907
1908/** @copydoc VBOXHDDBACKEND::pfnRead */
1909static DECLCALLBACK(int) vhdxRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
1910 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1911{
1912 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
1913 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
1914 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
1915 int rc = VINF_SUCCESS;
1916
1917 AssertPtr(pImage);
1918 Assert(uOffset % 512 == 0);
1919 Assert(cbToRead % 512 == 0);
1920
1921 if ( uOffset + cbToRead > pImage->cbSize
1922 || cbToRead == 0)
1923 rc = VERR_INVALID_PARAMETER;
1924 else
1925 {
1926 uint32_t idxBat = (uint32_t)(uOffset / pImage->cbBlock); Assert(idxBat == uOffset / pImage->cbBlock);
1927 uint32_t offRead = uOffset % pImage->cbBlock;
1928 uint64_t uBatEntry;
1929
1930 idxBat += idxBat / pImage->uChunkRatio; /* Add interleaving sector bitmap entries. */
1931 uBatEntry = pImage->paBat[idxBat].u64BatEntry;
1932
1933 cbToRead = RT_MIN(cbToRead, pImage->cbBlock - offRead);
1934
1935 switch (VHDX_BAT_ENTRY_GET_STATE(uBatEntry))
1936 {
1937 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_NOT_PRESENT:
1938 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_UNDEFINED:
1939 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_ZERO:
1940 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_UNMAPPED:
1941 {
1942 vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
1943 break;
1944 }
1945 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_FULLY_PRESENT:
1946 {
1947 uint64_t offFile = VHDX_BAT_ENTRY_GET_FILE_OFFSET(uBatEntry) + offRead;
1948 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, offFile,
1949 pIoCtx, cbToRead);
1950 break;
1951 }
1952 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_PARTIALLY_PRESENT:
1953 default:
1954 rc = VERR_INVALID_PARAMETER;
1955 break;
1956 }
1957
1958 if (pcbActuallyRead)
1959 *pcbActuallyRead = cbToRead;
1960 }
1961
1962 LogFlowFunc(("returns %Rrc\n", rc));
1963 return rc;
1964}
1965
1966/** @copydoc VBOXHDDBACKEND::pfnWrite */
1967static DECLCALLBACK(int) vhdxWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
1968 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
1969 size_t *pcbPostRead, unsigned fWrite)
1970{
1971 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
1972 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
1973 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
1974 int rc;
1975
1976 AssertPtr(pImage);
1977 Assert(uOffset % 512 == 0);
1978 Assert(cbToWrite % 512 == 0);
1979
1980 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
1981 rc = VERR_VD_IMAGE_READ_ONLY;
1982 else if ( uOffset + cbToWrite > pImage->cbSize
1983 || cbToWrite == 0)
1984 rc = VERR_INVALID_PARAMETER;
1985 else
1986 rc = VERR_NOT_SUPPORTED;
1987
1988 LogFlowFunc(("returns %Rrc\n", rc));
1989 return rc;
1990}
1991
1992/** @copydoc VBOXHDDBACKEND::pfnFlush */
1993static DECLCALLBACK(int) vhdxFlush(void *pBackendData, PVDIOCTX pIoCtx)
1994{
1995 LogFlowFunc(("pBackendData=%#p pIoCtx=%#p\n", pBackendData, pIoCtx));
1996 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
1997 int rc;
1998
1999 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2000 rc = VERR_VD_IMAGE_READ_ONLY;
2001 else
2002 rc = VERR_NOT_SUPPORTED;
2003
2004 LogFlowFunc(("returns %Rrc\n", rc));
2005 return rc;
2006}
2007
2008/** @copydoc VBOXHDDBACKEND::pfnGetVersion */
2009static DECLCALLBACK(unsigned) vhdxGetVersion(void *pBackendData)
2010{
2011 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2012 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2013
2014 AssertPtr(pImage);
2015
2016 if (pImage)
2017 return pImage->uVersion;
2018 else
2019 return 0;
2020}
2021
2022/** @copydoc VBOXHDDBACKEND::pfnGetSectorSize */
2023static DECLCALLBACK(uint32_t) vhdxGetSectorSize(void *pBackendData)
2024{
2025 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2026 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2027 uint32_t cb = 0;
2028
2029 AssertPtr(pImage);
2030
2031 if (pImage && pImage->pStorage)
2032 cb = pImage->cbLogicalSector;
2033
2034 LogFlowFunc(("returns %u\n", cb));
2035 return cb;
2036}
2037
2038/** @copydoc VBOXHDDBACKEND::pfnGetSize */
2039static DECLCALLBACK(uint64_t) vhdxGetSize(void *pBackendData)
2040{
2041 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2042 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2043 uint64_t cb = 0;
2044
2045 AssertPtr(pImage);
2046
2047 if (pImage && pImage->pStorage)
2048 cb = pImage->cbSize;
2049
2050 LogFlowFunc(("returns %llu\n", cb));
2051 return cb;
2052}
2053
2054/** @copydoc VBOXHDDBACKEND::pfnGetFileSize */
2055static DECLCALLBACK(uint64_t) vhdxGetFileSize(void *pBackendData)
2056{
2057 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2058 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2059 uint64_t cb = 0;
2060
2061 AssertPtr(pImage);
2062
2063 if (pImage)
2064 {
2065 uint64_t cbFile;
2066 if (pImage->pStorage)
2067 {
2068 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
2069 if (RT_SUCCESS(rc))
2070 cb = cbFile;
2071 }
2072 }
2073
2074 LogFlowFunc(("returns %lld\n", cb));
2075 return cb;
2076}
2077
2078/** @copydoc VBOXHDDBACKEND::pfnGetPCHSGeometry */
2079static DECLCALLBACK(int) vhdxGetPCHSGeometry(void *pBackendData,
2080 PVDGEOMETRY pPCHSGeometry)
2081{
2082 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
2083 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2084 int rc;
2085
2086 AssertPtr(pImage);
2087
2088 if (pImage)
2089 {
2090 if (pImage->PCHSGeometry.cCylinders)
2091 {
2092 *pPCHSGeometry = pImage->PCHSGeometry;
2093 rc = VINF_SUCCESS;
2094 }
2095 else
2096 rc = VERR_VD_GEOMETRY_NOT_SET;
2097 }
2098 else
2099 rc = VERR_VD_NOT_OPENED;
2100
2101 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2102 return rc;
2103}
2104
2105/** @copydoc VBOXHDDBACKEND::pfnSetPCHSGeometry */
2106static DECLCALLBACK(int) vhdxSetPCHSGeometry(void *pBackendData,
2107 PCVDGEOMETRY pPCHSGeometry)
2108{
2109 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2110 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2111 int rc = VINF_SUCCESS;
2112
2113 AssertPtr(pImage);
2114
2115 if (pImage)
2116 {
2117 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2118 rc = VERR_VD_IMAGE_READ_ONLY;
2119 else
2120 pImage->PCHSGeometry = *pPCHSGeometry;
2121 }
2122 else
2123 rc = VERR_VD_NOT_OPENED;
2124
2125 LogFlowFunc(("returns %Rrc\n", rc));
2126 return rc;
2127}
2128
2129/** @copydoc VBOXHDDBACKEND::pfnGetLCHSGeometry */
2130static DECLCALLBACK(int) vhdxGetLCHSGeometry(void *pBackendData,
2131 PVDGEOMETRY pLCHSGeometry)
2132{
2133 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
2134 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2135 int rc = VINF_SUCCESS;
2136
2137 AssertPtr(pImage);
2138
2139 if (pImage)
2140 {
2141 if (pImage->LCHSGeometry.cCylinders)
2142 *pLCHSGeometry = pImage->LCHSGeometry;
2143 else
2144 rc = VERR_VD_GEOMETRY_NOT_SET;
2145 }
2146 else
2147 rc = VERR_VD_NOT_OPENED;
2148
2149 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2150 return rc;
2151}
2152
2153/** @copydoc VBOXHDDBACKEND::pfnSetLCHSGeometry */
2154static DECLCALLBACK(int) vhdxSetLCHSGeometry(void *pBackendData,
2155 PCVDGEOMETRY pLCHSGeometry)
2156{
2157 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2158 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2159 int rc = VINF_SUCCESS;
2160
2161 AssertPtr(pImage);
2162
2163 if (pImage)
2164 {
2165 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2166 rc = VERR_VD_IMAGE_READ_ONLY;
2167 else
2168 pImage->LCHSGeometry = *pLCHSGeometry;
2169 }
2170 else
2171 rc = VERR_VD_NOT_OPENED;
2172
2173 LogFlowFunc(("returns %Rrc\n", rc));
2174 return rc;
2175}
2176
2177/** @copydoc VBOXHDDBACKEND::pfnGetImageFlags */
2178static DECLCALLBACK(unsigned) vhdxGetImageFlags(void *pBackendData)
2179{
2180 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2181 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2182 unsigned uImageFlags;
2183
2184 AssertPtr(pImage);
2185
2186 if (pImage)
2187 uImageFlags = pImage->uImageFlags;
2188 else
2189 uImageFlags = 0;
2190
2191 LogFlowFunc(("returns %#x\n", uImageFlags));
2192 return uImageFlags;
2193}
2194
2195/** @copydoc VBOXHDDBACKEND::pfnGetOpenFlags */
2196static DECLCALLBACK(unsigned) vhdxGetOpenFlags(void *pBackendData)
2197{
2198 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2199 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2200 unsigned uOpenFlags;
2201
2202 AssertPtr(pImage);
2203
2204 if (pImage)
2205 uOpenFlags = pImage->uOpenFlags;
2206 else
2207 uOpenFlags = 0;
2208
2209 LogFlowFunc(("returns %#x\n", uOpenFlags));
2210 return uOpenFlags;
2211}
2212
2213/** @copydoc VBOXHDDBACKEND::pfnSetOpenFlags */
2214static DECLCALLBACK(int) vhdxSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2215{
2216 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
2217 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2218 int rc = VINF_SUCCESS;
2219
2220 /* Image must be opened and the new flags must be valid. */
2221 if (!pImage || (uOpenFlags & ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
2222 rc = VERR_INVALID_PARAMETER;
2223 else
2224 {
2225 /* Implement this operation via reopening the image. */
2226 rc = vhdxFreeImage(pImage, false);
2227 if (RT_SUCCESS(rc))
2228 rc = vhdxOpenImage(pImage, uOpenFlags);
2229 }
2230
2231 LogFlowFunc(("returns %Rrc\n", rc));
2232 return rc;
2233}
2234
2235/** @copydoc VBOXHDDBACKEND::pfnGetComment */
2236static DECLCALLBACK(int) vhdxGetComment(void *pBackendData, char *pszComment,
2237 size_t cbComment)
2238{
2239 LogFlowFunc(("pBackendData=%#p pszComment=%#p cbComment=%zu\n", pBackendData, pszComment, cbComment));
2240 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2241 int rc;
2242
2243 AssertPtr(pImage);
2244
2245 if (pImage)
2246 rc = VERR_NOT_SUPPORTED;
2247 else
2248 rc = VERR_VD_NOT_OPENED;
2249
2250 LogFlowFunc(("returns %Rrc comment='%s'\n", rc, pszComment));
2251 return rc;
2252}
2253
2254/** @copydoc VBOXHDDBACKEND::pfnSetComment */
2255static DECLCALLBACK(int) vhdxSetComment(void *pBackendData, const char *pszComment)
2256{
2257 LogFlowFunc(("pBackendData=%#p pszComment=\"%s\"\n", pBackendData, pszComment));
2258 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2259 int rc;
2260
2261 AssertPtr(pImage);
2262
2263 if (pImage)
2264 {
2265 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2266 rc = VERR_VD_IMAGE_READ_ONLY;
2267 else
2268 rc = VERR_NOT_SUPPORTED;
2269 }
2270 else
2271 rc = VERR_VD_NOT_OPENED;
2272
2273 LogFlowFunc(("returns %Rrc\n", rc));
2274 return rc;
2275}
2276
2277/** @copydoc VBOXHDDBACKEND::pfnGetUuid */
2278static DECLCALLBACK(int) vhdxGetUuid(void *pBackendData, PRTUUID pUuid)
2279{
2280 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2281 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2282 int rc;
2283
2284 AssertPtr(pImage);
2285
2286 if (pImage)
2287 rc = VERR_NOT_SUPPORTED;
2288 else
2289 rc = VERR_VD_NOT_OPENED;
2290
2291 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2292 return rc;
2293}
2294
2295/** @copydoc VBOXHDDBACKEND::pfnSetUuid */
2296static DECLCALLBACK(int) vhdxSetUuid(void *pBackendData, PCRTUUID pUuid)
2297{
2298 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2299 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2300 int rc;
2301
2302 LogFlowFunc(("%RTuuid\n", pUuid));
2303 AssertPtr(pImage);
2304
2305 if (pImage)
2306 {
2307 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2308 rc = VERR_NOT_SUPPORTED;
2309 else
2310 rc = VERR_VD_IMAGE_READ_ONLY;
2311 }
2312 else
2313 rc = VERR_VD_NOT_OPENED;
2314
2315 LogFlowFunc(("returns %Rrc\n", rc));
2316 return rc;
2317}
2318
2319/** @copydoc VBOXHDDBACKEND::pfnGetModificationUuid */
2320static DECLCALLBACK(int) vhdxGetModificationUuid(void *pBackendData, PRTUUID pUuid)
2321{
2322 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2323 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2324 int rc;
2325
2326 AssertPtr(pImage);
2327
2328 if (pImage)
2329 rc = VERR_NOT_SUPPORTED;
2330 else
2331 rc = VERR_VD_NOT_OPENED;
2332
2333 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2334 return rc;
2335}
2336
2337/** @copydoc VBOXHDDBACKEND::pfnSetModificationUuid */
2338static DECLCALLBACK(int) vhdxSetModificationUuid(void *pBackendData, PCRTUUID pUuid)
2339{
2340 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2341 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2342 int rc;
2343
2344 AssertPtr(pImage);
2345
2346 if (pImage)
2347 {
2348 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2349 rc = VERR_NOT_SUPPORTED;
2350 else
2351 rc = VERR_VD_IMAGE_READ_ONLY;
2352 }
2353 else
2354 rc = VERR_VD_NOT_OPENED;
2355
2356 LogFlowFunc(("returns %Rrc\n", rc));
2357 return rc;
2358}
2359
2360/** @copydoc VBOXHDDBACKEND::pfnGetParentUuid */
2361static DECLCALLBACK(int) vhdxGetParentUuid(void *pBackendData, PRTUUID pUuid)
2362{
2363 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2364 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2365 int rc;
2366
2367 AssertPtr(pImage);
2368
2369 if (pImage)
2370 rc = VERR_NOT_SUPPORTED;
2371 else
2372 rc = VERR_VD_NOT_OPENED;
2373
2374 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2375 return rc;
2376}
2377
2378/** @copydoc VBOXHDDBACKEND::pfnSetParentUuid */
2379static DECLCALLBACK(int) vhdxSetParentUuid(void *pBackendData, PCRTUUID pUuid)
2380{
2381 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2382 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2383 int rc;
2384
2385 AssertPtr(pImage);
2386
2387 if (pImage)
2388 {
2389 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2390 rc = VERR_NOT_SUPPORTED;
2391 else
2392 rc = VERR_VD_IMAGE_READ_ONLY;
2393 }
2394 else
2395 rc = VERR_VD_NOT_OPENED;
2396
2397 LogFlowFunc(("returns %Rrc\n", rc));
2398 return rc;
2399}
2400
2401/** @copydoc VBOXHDDBACKEND::pfnGetParentModificationUuid */
2402static DECLCALLBACK(int) vhdxGetParentModificationUuid(void *pBackendData, PRTUUID pUuid)
2403{
2404 LogFlowFunc(("pBackendData=%#p pUuid=%#p\n", pBackendData, pUuid));
2405 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2406 int rc;
2407
2408 AssertPtr(pImage);
2409
2410 if (pImage)
2411 rc = VERR_NOT_SUPPORTED;
2412 else
2413 rc = VERR_VD_NOT_OPENED;
2414
2415 LogFlowFunc(("returns %Rrc (%RTuuid)\n", rc, pUuid));
2416 return rc;
2417}
2418
2419/** @copydoc VBOXHDDBACKEND::pfnSetParentModificationUuid */
2420static DECLCALLBACK(int) vhdxSetParentModificationUuid(void *pBackendData, PCRTUUID pUuid)
2421{
2422 LogFlowFunc(("pBackendData=%#p Uuid=%RTuuid\n", pBackendData, pUuid));
2423 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2424 int rc;
2425
2426 AssertPtr(pImage);
2427
2428 if (pImage)
2429 {
2430 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2431 rc = VERR_NOT_SUPPORTED;
2432 else
2433 rc = VERR_VD_IMAGE_READ_ONLY;
2434 }
2435 else
2436 rc = VERR_VD_NOT_OPENED;
2437
2438 LogFlowFunc(("returns %Rrc\n", rc));
2439 return rc;
2440}
2441
2442/** @copydoc VBOXHDDBACKEND::pfnDump */
2443static DECLCALLBACK(void) vhdxDump(void *pBackendData)
2444{
2445 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2446
2447 AssertPtr(pImage);
2448 if (pImage)
2449 {
2450 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%u\n",
2451 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
2452 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
2453 pImage->cbLogicalSector);
2454 }
2455}
2456
2457
2458const VBOXHDDBACKEND g_VhdxBackend =
2459{
2460 /* pszBackendName */
2461 "VHDX",
2462 /* cbSize */
2463 sizeof(VBOXHDDBACKEND),
2464 /* uBackendCaps */
2465 VD_CAP_FILE | VD_CAP_VFS,
2466 /* paFileExtensions */
2467 s_aVhdxFileExtensions,
2468 /* paConfigInfo */
2469 NULL,
2470 /* pfnCheckIfValid */
2471 vhdxCheckIfValid,
2472 /* pfnOpen */
2473 vhdxOpen,
2474 /* pfnCreate */
2475 vhdxCreate,
2476 /* pfnRename */
2477 vhdxRename,
2478 /* pfnClose */
2479 vhdxClose,
2480 /* pfnRead */
2481 vhdxRead,
2482 /* pfnWrite */
2483 vhdxWrite,
2484 /* pfnFlush */
2485 vhdxFlush,
2486 /* pfnDiscard */
2487 NULL,
2488 /* pfnGetVersion */
2489 vhdxGetVersion,
2490 /* pfnGetSectorSize */
2491 vhdxGetSectorSize,
2492 /* pfnGetSize */
2493 vhdxGetSize,
2494 /* pfnGetFileSize */
2495 vhdxGetFileSize,
2496 /* pfnGetPCHSGeometry */
2497 vhdxGetPCHSGeometry,
2498 /* pfnSetPCHSGeometry */
2499 vhdxSetPCHSGeometry,
2500 /* pfnGetLCHSGeometry */
2501 vhdxGetLCHSGeometry,
2502 /* pfnSetLCHSGeometry */
2503 vhdxSetLCHSGeometry,
2504 /* pfnGetImageFlags */
2505 vhdxGetImageFlags,
2506 /* pfnGetOpenFlags */
2507 vhdxGetOpenFlags,
2508 /* pfnSetOpenFlags */
2509 vhdxSetOpenFlags,
2510 /* pfnGetComment */
2511 vhdxGetComment,
2512 /* pfnSetComment */
2513 vhdxSetComment,
2514 /* pfnGetUuid */
2515 vhdxGetUuid,
2516 /* pfnSetUuid */
2517 vhdxSetUuid,
2518 /* pfnGetModificationUuid */
2519 vhdxGetModificationUuid,
2520 /* pfnSetModificationUuid */
2521 vhdxSetModificationUuid,
2522 /* pfnGetParentUuid */
2523 vhdxGetParentUuid,
2524 /* pfnSetParentUuid */
2525 vhdxSetParentUuid,
2526 /* pfnGetParentModificationUuid */
2527 vhdxGetParentModificationUuid,
2528 /* pfnSetParentModificationUuid */
2529 vhdxSetParentModificationUuid,
2530 /* pfnDump */
2531 vhdxDump,
2532 /* pfnGetTimestamp */
2533 NULL,
2534 /* pfnGetParentTimestamp */
2535 NULL,
2536 /* pfnSetParentTimestamp */
2537 NULL,
2538 /* pfnGetParentFilename */
2539 NULL,
2540 /* pfnSetParentFilename */
2541 NULL,
2542 /* pfnComposeLocation */
2543 genericFileComposeLocation,
2544 /* pfnComposeName */
2545 genericFileComposeName,
2546 /* pfnCompact */
2547 NULL,
2548 /* pfnResize */
2549 NULL,
2550 /* pfnRepair */
2551 NULL,
2552 /* pfnTraverseMetadata */
2553 NULL
2554};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette