VirtualBox

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

Last change on this file since 41353 was 41353, checked in by vboxsync, 13 years ago

Storage/VHDX: Fix sparc build

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