VirtualBox

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

Last change on this file since 102855 was 99739, checked in by vboxsync, 18 months ago

*: doxygen corrections (mostly about removing @returns from functions returning void).

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