VirtualBox

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

Last change on this file was 103523, checked in by vboxsync, 3 months ago

Storage/VHDX.cpp: Some unused variable fixes, bugref:3409

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 90.5 KB
Line 
1/* $Id: VHDX.cpp 103523 2024-02-22 11:15:48Z 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 cBatEntries;
1160 uint32_t cbBatEntries;
1161 PVhdxBatEntry paBatEntries = NULL;
1162
1163 LogFlowFunc(("pImage=%#p\n", pImage));
1164
1165 /* Calculate required values first. */
1166 uint64_t uChunkRatio64 = (RT_BIT_64(23) * pImage->cbLogicalSector) / pImage->cbBlock;
1167 uChunkRatio = (uint32_t)uChunkRatio64; Assert(uChunkRatio == uChunkRatio64);
1168 uint64_t cDataBlocks64 = pImage->cbSize / pImage->cbBlock;
1169 cDataBlocks = (uint32_t)cDataBlocks64; Assert(cDataBlocks == cDataBlocks64);
1170
1171 if (pImage->cbSize % pImage->cbBlock)
1172 cDataBlocks++;
1173
1174 cBatEntries = cDataBlocks + (cDataBlocks - 1)/uChunkRatio;
1175 cbBatEntries = cBatEntries * sizeof(VhdxBatEntry);
1176
1177 if (cbBatEntries <= cbRegion)
1178 {
1179 /*
1180 * Load the complete BAT region first, convert to host endianess and process
1181 * it afterwards. The SB entries can be removed because they are not needed yet.
1182 */
1183 paBatEntries = (PVhdxBatEntry)RTMemAlloc(cbBatEntries);
1184 if (paBatEntries)
1185 {
1186 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offRegion,
1187 paBatEntries, cbBatEntries);
1188 if (RT_SUCCESS(rc))
1189 {
1190 vhdxConvBatTableEndianess(VHDXECONV_F2H, paBatEntries, paBatEntries,
1191 cBatEntries);
1192
1193 /* Go through the table and validate it. */
1194 for (unsigned i = 0; i < cBatEntries; i++)
1195 {
1196 if ( i != 0
1197 && (i % uChunkRatio) == 0)
1198 {
1199/**
1200 * Disabled the verification because there are images out there with the sector bitmap
1201 * marked as present. The entry is never accessed and the image is readonly anyway,
1202 * so no harm done.
1203 */
1204#if 0
1205 /* Sector bitmap block. */
1206 if ( VHDX_BAT_ENTRY_GET_STATE(paBatEntries[i].u64BatEntry)
1207 != VHDX_BAT_ENTRY_SB_BLOCK_NOT_PRESENT)
1208 {
1209 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1210 "VHDX: Sector bitmap block at entry %u of image \'%s\' marked as present, violation of the specification",
1211 i, pImage->pszFilename);
1212 break;
1213 }
1214#endif
1215 }
1216 else
1217 {
1218 /* Payload block. */
1219 if ( VHDX_BAT_ENTRY_GET_STATE(paBatEntries[i].u64BatEntry)
1220 == VHDX_BAT_ENTRY_PAYLOAD_BLOCK_PARTIALLY_PRESENT)
1221 {
1222 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1223 "VHDX: Payload block at entry %u of image \'%s\' marked as partially present, violation of the specification",
1224 i, pImage->pszFilename);
1225 break;
1226 }
1227 }
1228 }
1229
1230 if (RT_SUCCESS(rc))
1231 {
1232 pImage->paBat = paBatEntries;
1233 pImage->uChunkRatio = uChunkRatio;
1234 }
1235 }
1236 else
1237 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1238 "VHDX: Error reading the BAT from image \'%s\'",
1239 pImage->pszFilename);
1240 }
1241 else
1242 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1243 "VHDX: Out of memory allocating memory for %u BAT entries of image \'%s\'",
1244 cBatEntries, pImage->pszFilename);
1245 }
1246 else
1247 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1248 "VHDX: Mismatch between calculated number of BAT entries and region size (expected %u got %u) for image \'%s\'",
1249 cbBatEntries, cbRegion, pImage->pszFilename);
1250
1251 if ( RT_FAILURE(rc)
1252 && paBatEntries)
1253 RTMemFree(paBatEntries);
1254
1255 LogFlowFunc(("returns rc=%Rrc\n", rc));
1256 return rc;
1257}
1258
1259/**
1260 * Load the file parameters metadata item from the file.
1261 *
1262 * @returns VBox status code.
1263 * @param pImage Image instance data.
1264 * @param offItem File offset where the data is stored.
1265 * @param cbItem Size of the item in the file.
1266 */
1267static int vhdxLoadFileParametersMetadata(PVHDXIMAGE pImage, uint64_t offItem, size_t cbItem)
1268{
1269 int rc = VINF_SUCCESS;
1270
1271 LogFlowFunc(("pImage=%#p offItem=%llu cbItem=%zu\n", pImage, offItem, cbItem));
1272
1273 if (cbItem != sizeof(VhdxFileParameters))
1274 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1275 "VHDX: File parameters item size mismatch (expected %u got %zu) in image \'%s\'",
1276 sizeof(VhdxFileParameters), cbItem, pImage->pszFilename);
1277 else
1278 {
1279 VhdxFileParameters FileParameters;
1280
1281 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offItem,
1282 &FileParameters, sizeof(FileParameters));
1283 if (RT_SUCCESS(rc))
1284 {
1285 vhdxConvFileParamsEndianess(VHDXECONV_F2H, &FileParameters, &FileParameters);
1286 pImage->cbBlock = FileParameters.u32BlockSize;
1287
1288 /** @todo No support for differencing images yet. */
1289 if (FileParameters.u32Flags & VHDX_FILE_PARAMETERS_FLAGS_HAS_PARENT)
1290 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1291 "VHDX: Image \'%s\' is a differencing image which is not supported yet",
1292 pImage->pszFilename);
1293 }
1294 else
1295 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1296 "VHDX: Reading the file parameters metadata item from image \'%s\' failed",
1297 pImage->pszFilename);
1298 }
1299
1300 LogFlowFunc(("returns rc=%Rrc\n", rc));
1301 return rc;
1302}
1303
1304/**
1305 * Load the virtual disk size metadata item from the file.
1306 *
1307 * @returns VBox status code.
1308 * @param pImage Image instance data.
1309 * @param offItem File offset where the data is stored.
1310 * @param cbItem Size of the item in the file.
1311 */
1312static int vhdxLoadVDiskSizeMetadata(PVHDXIMAGE pImage, uint64_t offItem, size_t cbItem)
1313{
1314 int rc = VINF_SUCCESS;
1315
1316 LogFlowFunc(("pImage=%#p offItem=%llu cbItem=%zu\n", pImage, offItem, cbItem));
1317
1318 if (cbItem != sizeof(VhdxVDiskSize))
1319 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1320 "VHDX: Virtual disk size item size mismatch (expected %u got %zu) in image \'%s\'",
1321 sizeof(VhdxVDiskSize), cbItem, pImage->pszFilename);
1322 else
1323 {
1324 VhdxVDiskSize VDiskSize;
1325
1326 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offItem,
1327 &VDiskSize, sizeof(VDiskSize));
1328 if (RT_SUCCESS(rc))
1329 {
1330 vhdxConvVDiskSizeEndianess(VHDXECONV_F2H, &VDiskSize, &VDiskSize);
1331 pImage->cbSize = VDiskSize.u64VDiskSize;
1332 }
1333 else
1334 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1335 "VHDX: Reading the virtual disk size metadata item from image \'%s\' failed",
1336 pImage->pszFilename);
1337 }
1338
1339 LogFlowFunc(("returns rc=%Rrc\n", rc));
1340 return rc;
1341}
1342
1343/**
1344 * Load the logical sector size metadata item from the file.
1345 *
1346 * @returns VBox status code.
1347 * @param pImage Image instance data.
1348 * @param offItem File offset where the data is stored.
1349 * @param cbItem Size of the item in the file.
1350 */
1351static int vhdxLoadVDiskLogSectorSizeMetadata(PVHDXIMAGE pImage, uint64_t offItem, size_t cbItem)
1352{
1353 int rc = VINF_SUCCESS;
1354
1355 LogFlowFunc(("pImage=%#p offItem=%llu cbItem=%zu\n", pImage, offItem, cbItem));
1356
1357 if (cbItem != sizeof(VhdxVDiskLogicalSectorSize))
1358 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1359 "VHDX: Virtual disk logical sector size item size mismatch (expected %u got %zu) in image \'%s\'",
1360 sizeof(VhdxVDiskLogicalSectorSize), cbItem, pImage->pszFilename);
1361 else
1362 {
1363 VhdxVDiskLogicalSectorSize VDiskLogSectSize;
1364
1365 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offItem,
1366 &VDiskLogSectSize, sizeof(VDiskLogSectSize));
1367 if (RT_SUCCESS(rc))
1368 {
1369 vhdxConvVDiskLogSectSizeEndianess(VHDXECONV_F2H, &VDiskLogSectSize,
1370 &VDiskLogSectSize);
1371 pImage->cbLogicalSector = VDiskLogSectSize.u32LogicalSectorSize;
1372 }
1373 else
1374 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1375 "VHDX: Reading the virtual disk logical sector size metadata item from image \'%s\' failed",
1376 pImage->pszFilename);
1377 }
1378
1379 LogFlowFunc(("returns rc=%Rrc\n", rc));
1380 return rc;
1381}
1382
1383/**
1384 * Loads the metadata region.
1385 *
1386 * @returns VBox status code.
1387 * @param pImage Image instance data.
1388 * @param offRegion Start offset of the region.
1389 * @param cbRegion Size of the region.
1390 */
1391static int vhdxLoadMetadataRegion(PVHDXIMAGE pImage, uint64_t offRegion,
1392 size_t cbRegion)
1393{
1394 VhdxMetadataTblHdr MetadataTblHdr;
1395 int rc = VINF_SUCCESS;
1396
1397 LogFlowFunc(("pImage=%#p\n", pImage));
1398
1399 /* Load the header first. */
1400 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offRegion,
1401 &MetadataTblHdr, sizeof(MetadataTblHdr));
1402 if (RT_SUCCESS(rc))
1403 {
1404 vhdxConvMetadataTblHdrEndianess(VHDXECONV_F2H, &MetadataTblHdr, &MetadataTblHdr);
1405
1406 /* Validate structure. */
1407 if (MetadataTblHdr.u64Signature != VHDX_METADATA_TBL_HDR_SIGNATURE)
1408 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1409 "VHDX: Incorrect metadata table header signature for image \'%s\'",
1410 pImage->pszFilename);
1411 else if (MetadataTblHdr.u16EntryCount > VHDX_METADATA_TBL_HDR_ENTRY_COUNT_MAX)
1412 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1413 "VHDX: Incorrect entry count in metadata table header of image \'%s\'",
1414 pImage->pszFilename);
1415 else if (cbRegion < (MetadataTblHdr.u16EntryCount * sizeof(VhdxMetadataTblEntry) + sizeof(VhdxMetadataTblHdr)))
1416 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1417 "VHDX: Metadata table of image \'%s\' exceeds region size",
1418 pImage->pszFilename);
1419
1420 if (RT_SUCCESS(rc))
1421 {
1422 uint64_t offMetadataTblEntry = offRegion + sizeof(VhdxMetadataTblHdr);
1423
1424 for (unsigned i = 0; i < MetadataTblHdr.u16EntryCount; i++)
1425 {
1426 uint64_t offMetadataItem = 0;
1427 VHDXMETADATAITEM enmMetadataItem = VHDXMETADATAITEM_UNKNOWN;
1428 VhdxMetadataTblEntry MetadataTblEntry;
1429
1430 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offMetadataTblEntry,
1431 &MetadataTblEntry, sizeof(MetadataTblEntry));
1432 if (RT_FAILURE(rc))
1433 {
1434 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1435 "VHDX: Reading metadata table entry from image \'%s\' failed",
1436 pImage->pszFilename);
1437 break;
1438 }
1439
1440 vhdxConvMetadataTblEntryEndianess(VHDXECONV_F2H, &MetadataTblEntry, &MetadataTblEntry);
1441
1442 /* Check whether the flags match the expectations. */
1443 for (unsigned idxProp = 0; idxProp < RT_ELEMENTS(s_aVhdxMetadataItemProps); idxProp++)
1444 {
1445 if (!RTUuidCompareStr(&MetadataTblEntry.UuidItem,
1446 s_aVhdxMetadataItemProps[idxProp].pszItemUuid))
1447 {
1448 /*
1449 * Check for specification violations and bail out, except
1450 * for the required flag of the physical sector size metadata item.
1451 * Early images had the required flag not set opposed to the specification.
1452 * We don't want to brerak those images.
1453 */
1454 if ( !!(MetadataTblEntry.u32Flags & VHDX_METADATA_TBL_ENTRY_FLAGS_IS_USER)
1455 != s_aVhdxMetadataItemProps[idxProp].fIsUser)
1456 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1457 "VHDX: User flag of metadata item does not meet expectations \'%s\'",
1458 pImage->pszFilename);
1459 else if ( !!(MetadataTblEntry.u32Flags & VHDX_METADATA_TBL_ENTRY_FLAGS_IS_VDISK)
1460 != s_aVhdxMetadataItemProps[idxProp].fIsVDisk)
1461 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1462 "VHDX: Virtual disk flag of metadata item does not meet expectations \'%s\'",
1463 pImage->pszFilename);
1464 else if ( !!(MetadataTblEntry.u32Flags & VHDX_METADATA_TBL_ENTRY_FLAGS_IS_REQUIRED)
1465 != s_aVhdxMetadataItemProps[idxProp].fIsRequired
1466 && (s_aVhdxMetadataItemProps[idxProp].enmMetadataItem != VHDXMETADATAITEM_PHYSICAL_SECTOR_SIZE))
1467 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1468 "VHDX: Required flag of metadata item does not meet expectations \'%s\'",
1469 pImage->pszFilename);
1470 else
1471 enmMetadataItem = s_aVhdxMetadataItemProps[idxProp].enmMetadataItem;
1472
1473 break;
1474 }
1475 }
1476
1477 if (RT_FAILURE(rc))
1478 break;
1479
1480 offMetadataItem = offRegion + MetadataTblEntry.u32Offset;
1481
1482 switch (enmMetadataItem)
1483 {
1484 case VHDXMETADATAITEM_FILE_PARAMS:
1485 {
1486 rc = vhdxLoadFileParametersMetadata(pImage, offMetadataItem,
1487 MetadataTblEntry.u32Length);
1488 break;
1489 }
1490 case VHDXMETADATAITEM_VDISK_SIZE:
1491 {
1492 rc = vhdxLoadVDiskSizeMetadata(pImage, offMetadataItem,
1493 MetadataTblEntry.u32Length);
1494 break;
1495 }
1496 case VHDXMETADATAITEM_PAGE83_DATA:
1497 {
1498 /*
1499 * Nothing to do here for now (marked as required but
1500 * there is no API to pass this information to the caller)
1501 * so far.
1502 */
1503 break;
1504 }
1505 case VHDXMETADATAITEM_LOGICAL_SECTOR_SIZE:
1506 {
1507 rc = vhdxLoadVDiskLogSectorSizeMetadata(pImage, offMetadataItem,
1508 MetadataTblEntry.u32Length);
1509 break;
1510 }
1511 case VHDXMETADATAITEM_PHYSICAL_SECTOR_SIZE:
1512 {
1513 /*
1514 * Nothing to do here for now (marked as required but
1515 * there is no API to pass this information to the caller)
1516 * so far.
1517 */
1518 break;
1519 }
1520 case VHDXMETADATAITEM_PARENT_LOCATOR:
1521 {
1522 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1523 "VHDX: Image \'%s\' is a differencing image which is not supported yet",
1524 pImage->pszFilename);
1525 break;
1526 }
1527 case VHDXMETADATAITEM_UNKNOWN:
1528 default:
1529 if (MetadataTblEntry.u32Flags & VHDX_METADATA_TBL_ENTRY_FLAGS_IS_REQUIRED)
1530 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1531 "VHDX: Unsupported but required metadata item in image \'%s\'",
1532 pImage->pszFilename);
1533 }
1534
1535 if (RT_FAILURE(rc))
1536 break;
1537
1538 offMetadataTblEntry += sizeof(MetadataTblEntry);
1539 }
1540 }
1541 }
1542 else
1543 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1544 "VHDX: Reading the metadata table header for image \'%s\' failed",
1545 pImage->pszFilename);
1546
1547 LogFlowFunc(("returns rc=%Rrc\n", rc));
1548 return rc;
1549}
1550
1551/**
1552 * Loads the region table and the associated regions.
1553 *
1554 * @returns VBox status code.
1555 * @param pImage Image instance data.
1556 */
1557static int vhdxLoadRegionTable(PVHDXIMAGE pImage)
1558{
1559 uint8_t *pbRegionTbl = NULL;
1560 int rc = VINF_SUCCESS;
1561
1562 LogFlowFunc(("pImage=%#p\n", pImage));
1563
1564 /* Load the complete region table into memory. */
1565 pbRegionTbl = (uint8_t *)RTMemTmpAlloc(VHDX_REGION_TBL_SIZE_MAX);
1566 if (pbRegionTbl)
1567 {
1568 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, VHDX_REGION_TBL_HDR_OFFSET,
1569 pbRegionTbl, VHDX_REGION_TBL_SIZE_MAX);
1570 if (RT_SUCCESS(rc))
1571 {
1572 PVhdxRegionTblHdr pRegionTblHdr;
1573 VhdxRegionTblHdr RegionTblHdr;
1574 uint32_t u32ChkSum = 0;
1575
1576 /*
1577 * Copy the region table header to a dedicated structure where we can
1578 * convert it to host endianess.
1579 */
1580 memcpy(&RegionTblHdr, pbRegionTbl, sizeof(RegionTblHdr));
1581 vhdxConvRegionTblHdrEndianess(VHDXECONV_F2H, &RegionTblHdr, &RegionTblHdr);
1582
1583 /* Set checksum field to 0 during crc computation. */
1584 pRegionTblHdr = (PVhdxRegionTblHdr)pbRegionTbl;
1585 pRegionTblHdr->u32Checksum = 0;
1586
1587 /* Verify the region table integrity. */
1588 u32ChkSum = RTCrc32C(pbRegionTbl, VHDX_REGION_TBL_SIZE_MAX);
1589
1590 if (RegionTblHdr.u32Signature != VHDX_REGION_TBL_HDR_SIGNATURE)
1591 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1592 "VHDX: Invalid signature for region table header of image \'%s\'",
1593 pImage->pszFilename);
1594 else if (u32ChkSum != RegionTblHdr.u32Checksum)
1595 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1596 "VHDX: CRC32 checksum mismatch for the region table of image \'%s\' (expected %#x got %#x)",
1597 pImage->pszFilename, RegionTblHdr.u32Checksum, u32ChkSum);
1598 else if (RegionTblHdr.u32EntryCount > VHDX_REGION_TBL_HDR_ENTRY_COUNT_MAX)
1599 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1600 "VHDX: Invalid entry count field in the region table header of image \'%s\'",
1601 pImage->pszFilename);
1602
1603 if (RT_SUCCESS(rc))
1604 {
1605 /* Parse the region table entries. */
1606 PVhdxRegionTblEntry pRegTblEntry = (PVhdxRegionTblEntry)(pbRegionTbl + sizeof(VhdxRegionTblHdr));
1607 VhdxRegionTblEntry RegTblEntryBat; /* BAT region table entry. */
1608 bool fBatRegPresent = false;
1609 RT_ZERO(RegTblEntryBat); /* Maybe uninitialized, gcc. */
1610
1611 for (unsigned i = 0; i < RegionTblHdr.u32EntryCount; i++)
1612 {
1613 vhdxConvRegionTblEntryEndianess(VHDXECONV_F2H, pRegTblEntry, pRegTblEntry);
1614
1615 /* Check the uuid for known regions. */
1616 if (!RTUuidCompareStr(&pRegTblEntry->UuidObject, VHDX_REGION_TBL_ENTRY_UUID_BAT))
1617 {
1618 /*
1619 * Save the BAT region and process it later.
1620 * It may come before the metadata region but needs the block size.
1621 */
1622 if (pRegTblEntry->u32Flags & VHDX_REGION_TBL_ENTRY_FLAGS_IS_REQUIRED)
1623 {
1624 fBatRegPresent = true;
1625 RegTblEntryBat.u32Length = pRegTblEntry->u32Length;
1626 RegTblEntryBat.u64FileOffset = pRegTblEntry->u64FileOffset;
1627 }
1628 else
1629 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1630 "VHDX: BAT region not marked as required in image \'%s\'",
1631 pImage->pszFilename);
1632 }
1633 else if (!RTUuidCompareStr(&pRegTblEntry->UuidObject, VHDX_REGION_TBL_ENTRY_UUID_METADATA))
1634 {
1635 if (pRegTblEntry->u32Flags & VHDX_REGION_TBL_ENTRY_FLAGS_IS_REQUIRED)
1636 rc = vhdxLoadMetadataRegion(pImage, pRegTblEntry->u64FileOffset, pRegTblEntry->u32Length);
1637 else
1638 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1639 "VHDX: Metadata region not marked as required in image \'%s\'",
1640 pImage->pszFilename);
1641 }
1642 else if (pRegTblEntry->u32Flags & VHDX_REGION_TBL_ENTRY_FLAGS_IS_REQUIRED)
1643 {
1644 /* The region is not known but marked as required, fail to load the image. */
1645 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1646 "VHDX: Unknown required region in image \'%s\'",
1647 pImage->pszFilename);
1648 }
1649
1650 if (RT_FAILURE(rc))
1651 break;
1652
1653 pRegTblEntry++;
1654 }
1655
1656 if (fBatRegPresent)
1657 rc = vhdxLoadBatRegion(pImage, RegTblEntryBat.u64FileOffset, RegTblEntryBat.u32Length);
1658 else
1659 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1660 "VHDX: BAT region in image \'%s\' is missing",
1661 pImage->pszFilename);
1662 }
1663 }
1664 else
1665 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1666 "VHDX: Reading the region table for image \'%s\' failed",
1667 pImage->pszFilename);
1668 }
1669 else
1670 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1671 "VHDX: Out of memory allocating memory for the region table of image \'%s\'",
1672 pImage->pszFilename);
1673
1674 if (pbRegionTbl)
1675 RTMemTmpFree(pbRegionTbl);
1676
1677 LogFlowFunc(("returns rc=%Rrc\n", rc));
1678 return rc;
1679}
1680
1681/**
1682 * Internal: Open an image, constructing all necessary data structures.
1683 */
1684static int vhdxOpenImage(PVHDXIMAGE pImage, unsigned uOpenFlags)
1685{
1686 uint64_t cbFile = 0;
1687 VhdxFileIdentifier FileIdentifier;
1688 int rc = VINF_SUCCESS;
1689
1690 LogFlowFunc(("pImage=%#p uOpenFlags=%#x\n", pImage, uOpenFlags));
1691 pImage->uOpenFlags = uOpenFlags;
1692
1693 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
1694 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
1695 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
1696
1697 /* Refuse write access, it is not implemented so far. */
1698 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
1699 return VERR_NOT_SUPPORTED;
1700
1701 /*
1702 * Open the image.
1703 */
1704 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
1705 VDOpenFlagsToFileOpenFlags(uOpenFlags,
1706 false /* fCreate */),
1707 &pImage->pStorage);
1708
1709 /* Do NOT signal an appropriate error here, as the VD layer has the
1710 * choice of retrying the open if it failed. */
1711 if (RT_SUCCESS(rc))
1712 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
1713
1714 if (RT_SUCCESS(rc))
1715 {
1716 if (cbFile > sizeof(FileIdentifier))
1717 {
1718 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, VHDX_FILE_IDENTIFIER_OFFSET,
1719 &FileIdentifier, sizeof(FileIdentifier));
1720 if (RT_SUCCESS(rc))
1721 {
1722 vhdxConvFileIdentifierEndianess(VHDXECONV_F2H, &FileIdentifier,
1723 &FileIdentifier);
1724 if (FileIdentifier.u64Signature != VHDX_FILE_IDENTIFIER_SIGNATURE)
1725 rc = VERR_VD_GEN_INVALID_HEADER;
1726 else
1727 rc = vhdxFindAndLoadCurrentHeader(pImage);
1728
1729 /* Load the region table. */
1730 if (RT_SUCCESS(rc))
1731 rc = vhdxLoadRegionTable(pImage);
1732 }
1733 }
1734 else
1735 rc = VERR_VD_GEN_INVALID_HEADER;
1736 }
1737
1738 if (RT_SUCCESS(rc))
1739 {
1740 PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
1741 pImage->RegionList.fFlags = 0;
1742 pImage->RegionList.cRegions = 1;
1743
1744 pRegion->offRegion = 0; /* Disk start. */
1745 pRegion->cbBlock = pImage->cbLogicalSector;
1746 pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
1747 pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
1748 pRegion->cbData = pImage->cbLogicalSector;
1749 pRegion->cbMetadata = 0;
1750 pRegion->cRegionBlocksOrBytes = pImage->cbSize;
1751 }
1752 else
1753 vhdxFreeImage(pImage, false);
1754
1755 LogFlowFunc(("returns rc=%Rrc\n", rc));
1756 return rc;
1757}
1758
1759
1760/** @copydoc VDIMAGEBACKEND::pfnProbe */
1761static DECLCALLBACK(int) vhdxProbe(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
1762 PVDINTERFACE pVDIfsImage, VDTYPE enmDesiredType, VDTYPE *penmType)
1763{
1764 RT_NOREF(pVDIfsDisk, enmDesiredType);
1765 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
1766 PVDIOSTORAGE pStorage = NULL;
1767 uint64_t cbFile;
1768 int rc = VINF_SUCCESS;
1769 VhdxFileIdentifier FileIdentifier;
1770
1771 PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
1772 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
1773
1774 if ( !RT_VALID_PTR(pszFilename)
1775 || !*pszFilename)
1776 rc = VERR_INVALID_PARAMETER;
1777 else
1778 {
1779 /*
1780 * Open the file and read the file identifier.
1781 */
1782 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
1783 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
1784 false /* fCreate */),
1785 &pStorage);
1786 if (RT_SUCCESS(rc))
1787 {
1788 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
1789 if (RT_SUCCESS(rc))
1790 {
1791 if (cbFile > sizeof(FileIdentifier))
1792 {
1793 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, VHDX_FILE_IDENTIFIER_OFFSET,
1794 &FileIdentifier, sizeof(FileIdentifier));
1795 if (RT_SUCCESS(rc))
1796 {
1797 vhdxConvFileIdentifierEndianess(VHDXECONV_F2H, &FileIdentifier,
1798 &FileIdentifier);
1799 if (FileIdentifier.u64Signature != VHDX_FILE_IDENTIFIER_SIGNATURE)
1800 rc = VERR_VD_GEN_INVALID_HEADER;
1801 else
1802 *penmType = VDTYPE_HDD;
1803 }
1804 }
1805 else
1806 rc = VERR_VD_GEN_INVALID_HEADER;
1807 }
1808 }
1809
1810 if (pStorage)
1811 vdIfIoIntFileClose(pIfIo, pStorage);
1812 }
1813
1814 LogFlowFunc(("returns %Rrc\n", rc));
1815 return rc;
1816}
1817
1818/** @copydoc VDIMAGEBACKEND::pfnOpen */
1819static DECLCALLBACK(int) vhdxOpen(const char *pszFilename, unsigned uOpenFlags,
1820 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1821 VDTYPE enmType, void **ppBackendData)
1822{
1823 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
1824 int rc;
1825 PVHDXIMAGE pImage;
1826
1827 NOREF(enmType); /**< @todo r=klaus make use of the type info. */
1828
1829 /* Check open flags. All valid flags are supported. */
1830 if ( uOpenFlags & ~VD_OPEN_FLAGS_MASK
1831 || !RT_VALID_PTR(pszFilename)
1832 || !*pszFilename)
1833 rc = VERR_INVALID_PARAMETER;
1834 else
1835 {
1836 pImage = (PVHDXIMAGE)RTMemAllocZ(RT_UOFFSETOF(VHDXIMAGE, RegionList.aRegions[1]));
1837 if (!pImage)
1838 rc = VERR_NO_MEMORY;
1839 else
1840 {
1841 pImage->pszFilename = pszFilename;
1842 pImage->pStorage = NULL;
1843 pImage->pVDIfsDisk = pVDIfsDisk;
1844 pImage->pVDIfsImage = pVDIfsImage;
1845
1846 rc = vhdxOpenImage(pImage, uOpenFlags);
1847 if (RT_SUCCESS(rc))
1848 *ppBackendData = pImage;
1849 else
1850 RTMemFree(pImage);
1851 }
1852 }
1853
1854 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1855 return rc;
1856}
1857
1858/** @interface_method_impl{VDIMAGEBACKEND,pfnCreate} */
1859static DECLCALLBACK(int) vhdxCreate(const char *pszFilename, uint64_t cbSize,
1860 unsigned uImageFlags, const char *pszComment,
1861 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1862 PCRTUUID pUuid, unsigned uOpenFlags,
1863 unsigned uPercentStart, unsigned uPercentSpan,
1864 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1865 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
1866 void **ppBackendData)
1867{
1868 RT_NOREF8(pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags);
1869 RT_NOREF7(uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData);
1870 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",
1871 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
1872 int rc = VERR_NOT_SUPPORTED;
1873
1874 LogFlowFunc(("returns %Rrc\n", rc));
1875 return rc;
1876}
1877
1878/** @copydoc VDIMAGEBACKEND::pfnRename */
1879static DECLCALLBACK(int) vhdxRename(void *pBackendData, const char *pszFilename)
1880{
1881 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1882 int rc = VINF_SUCCESS;
1883 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
1884
1885 /* Check arguments. */
1886 if ( !pImage
1887 || !pszFilename
1888 || !*pszFilename)
1889 rc = VERR_INVALID_PARAMETER;
1890 else
1891 {
1892 /* Close the image. */
1893 rc = vhdxFreeImage(pImage, false);
1894 if (RT_SUCCESS(rc))
1895 {
1896 /* Rename the file. */
1897 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
1898 if (RT_FAILURE(rc))
1899 {
1900 /* The move failed, try to reopen the original image. */
1901 int rc2 = vhdxOpenImage(pImage, pImage->uOpenFlags);
1902 if (RT_FAILURE(rc2))
1903 rc = rc2;
1904 }
1905 else
1906 {
1907 /* Update pImage with the new information. */
1908 pImage->pszFilename = pszFilename;
1909
1910 /* Open the old image with new name. */
1911 rc = vhdxOpenImage(pImage, pImage->uOpenFlags);
1912 }
1913 }
1914 }
1915
1916 LogFlowFunc(("returns %Rrc\n", rc));
1917 return rc;
1918}
1919
1920/** @copydoc VDIMAGEBACKEND::pfnClose */
1921static DECLCALLBACK(int) vhdxClose(void *pBackendData, bool fDelete)
1922{
1923 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1924 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
1925 int rc;
1926
1927 rc = vhdxFreeImage(pImage, fDelete);
1928 RTMemFree(pImage);
1929
1930 LogFlowFunc(("returns %Rrc\n", rc));
1931 return rc;
1932}
1933
1934/** @copydoc VDIMAGEBACKEND::pfnRead */
1935static DECLCALLBACK(int) vhdxRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
1936 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1937{
1938 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
1939 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
1940 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
1941 int rc = VINF_SUCCESS;
1942
1943 AssertPtr(pImage);
1944 Assert(uOffset % 512 == 0);
1945 Assert(cbToRead % 512 == 0);
1946
1947 if ( uOffset + cbToRead > pImage->cbSize
1948 || cbToRead == 0)
1949 rc = VERR_INVALID_PARAMETER;
1950 else
1951 {
1952 uint32_t idxBat = (uint32_t)(uOffset / pImage->cbBlock); Assert(idxBat == uOffset / pImage->cbBlock);
1953 uint32_t offRead = uOffset % pImage->cbBlock;
1954 uint64_t uBatEntry;
1955
1956 idxBat += idxBat / pImage->uChunkRatio; /* Add interleaving sector bitmap entries. */
1957 uBatEntry = pImage->paBat[idxBat].u64BatEntry;
1958
1959 cbToRead = RT_MIN(cbToRead, pImage->cbBlock - offRead);
1960
1961 switch (VHDX_BAT_ENTRY_GET_STATE(uBatEntry))
1962 {
1963 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_NOT_PRESENT:
1964 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_UNDEFINED:
1965 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_ZERO:
1966 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_UNMAPPED:
1967 {
1968 vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
1969 break;
1970 }
1971 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_FULLY_PRESENT:
1972 {
1973 uint64_t offFile = VHDX_BAT_ENTRY_GET_FILE_OFFSET(uBatEntry) + offRead;
1974 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, offFile,
1975 pIoCtx, cbToRead);
1976 break;
1977 }
1978 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_PARTIALLY_PRESENT:
1979 default:
1980 rc = VERR_INVALID_PARAMETER;
1981 break;
1982 }
1983
1984 if (pcbActuallyRead)
1985 *pcbActuallyRead = cbToRead;
1986 }
1987
1988 LogFlowFunc(("returns %Rrc\n", rc));
1989 return rc;
1990}
1991
1992/** @copydoc VDIMAGEBACKEND::pfnWrite */
1993static DECLCALLBACK(int) vhdxWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
1994 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
1995 size_t *pcbPostRead, unsigned fWrite)
1996{
1997 RT_NOREF5(pIoCtx, pcbWriteProcess, pcbPreRead, pcbPostRead, fWrite);
1998 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
1999 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
2000 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2001 int rc;
2002
2003 AssertPtr(pImage);
2004 Assert(uOffset % 512 == 0);
2005 Assert(cbToWrite % 512 == 0);
2006
2007 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2008 rc = VERR_VD_IMAGE_READ_ONLY;
2009 else if ( uOffset + cbToWrite > pImage->cbSize
2010 || cbToWrite == 0)
2011 rc = VERR_INVALID_PARAMETER;
2012 else
2013 rc = VERR_NOT_SUPPORTED;
2014
2015 LogFlowFunc(("returns %Rrc\n", rc));
2016 return rc;
2017}
2018
2019/** @copydoc VDIMAGEBACKEND::pfnFlush */
2020static DECLCALLBACK(int) vhdxFlush(void *pBackendData, PVDIOCTX pIoCtx)
2021{
2022 RT_NOREF1(pIoCtx);
2023 LogFlowFunc(("pBackendData=%#p pIoCtx=%#p\n", pBackendData, pIoCtx));
2024 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2025 int rc;
2026
2027 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2028 rc = VERR_VD_IMAGE_READ_ONLY;
2029 else
2030 rc = VERR_NOT_SUPPORTED;
2031
2032 LogFlowFunc(("returns %Rrc\n", rc));
2033 return rc;
2034}
2035
2036/** @copydoc VDIMAGEBACKEND::pfnGetVersion */
2037static DECLCALLBACK(unsigned) vhdxGetVersion(void *pBackendData)
2038{
2039 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2040 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2041
2042 AssertPtr(pImage);
2043
2044 if (pImage)
2045 return pImage->uVersion;
2046 else
2047 return 0;
2048}
2049
2050/** @copydoc VDIMAGEBACKEND::pfnGetFileSize */
2051static DECLCALLBACK(uint64_t) vhdxGetFileSize(void *pBackendData)
2052{
2053 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2054 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2055 uint64_t cb = 0;
2056
2057 AssertPtr(pImage);
2058
2059 if (pImage)
2060 {
2061 uint64_t cbFile;
2062 if (pImage->pStorage)
2063 {
2064 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
2065 if (RT_SUCCESS(rc))
2066 cb = cbFile;
2067 }
2068 }
2069
2070 LogFlowFunc(("returns %lld\n", cb));
2071 return cb;
2072}
2073
2074/** @copydoc VDIMAGEBACKEND::pfnGetPCHSGeometry */
2075static DECLCALLBACK(int) vhdxGetPCHSGeometry(void *pBackendData,
2076 PVDGEOMETRY pPCHSGeometry)
2077{
2078 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
2079 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2080 int rc;
2081
2082 AssertPtr(pImage);
2083
2084 if (pImage)
2085 {
2086 if (pImage->PCHSGeometry.cCylinders)
2087 {
2088 *pPCHSGeometry = pImage->PCHSGeometry;
2089 rc = VINF_SUCCESS;
2090 }
2091 else
2092 rc = VERR_VD_GEOMETRY_NOT_SET;
2093 }
2094 else
2095 rc = VERR_VD_NOT_OPENED;
2096
2097 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2098 return rc;
2099}
2100
2101/** @copydoc VDIMAGEBACKEND::pfnSetPCHSGeometry */
2102static DECLCALLBACK(int) vhdxSetPCHSGeometry(void *pBackendData,
2103 PCVDGEOMETRY pPCHSGeometry)
2104{
2105 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2106 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2107 int rc = VINF_SUCCESS;
2108
2109 AssertPtr(pImage);
2110
2111 if (pImage)
2112 {
2113 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2114 rc = VERR_VD_IMAGE_READ_ONLY;
2115 else
2116 pImage->PCHSGeometry = *pPCHSGeometry;
2117 }
2118 else
2119 rc = VERR_VD_NOT_OPENED;
2120
2121 LogFlowFunc(("returns %Rrc\n", rc));
2122 return rc;
2123}
2124
2125/** @copydoc VDIMAGEBACKEND::pfnGetLCHSGeometry */
2126static DECLCALLBACK(int) vhdxGetLCHSGeometry(void *pBackendData,
2127 PVDGEOMETRY pLCHSGeometry)
2128{
2129 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
2130 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2131 int rc = VINF_SUCCESS;
2132
2133 AssertPtr(pImage);
2134
2135 if (pImage)
2136 {
2137 if (pImage->LCHSGeometry.cCylinders)
2138 *pLCHSGeometry = pImage->LCHSGeometry;
2139 else
2140 rc = VERR_VD_GEOMETRY_NOT_SET;
2141 }
2142 else
2143 rc = VERR_VD_NOT_OPENED;
2144
2145 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2146 return rc;
2147}
2148
2149/** @copydoc VDIMAGEBACKEND::pfnSetLCHSGeometry */
2150static DECLCALLBACK(int) vhdxSetLCHSGeometry(void *pBackendData,
2151 PCVDGEOMETRY pLCHSGeometry)
2152{
2153 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2154 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2155 int rc = VINF_SUCCESS;
2156
2157 AssertPtr(pImage);
2158
2159 if (pImage)
2160 {
2161 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2162 rc = VERR_VD_IMAGE_READ_ONLY;
2163 else
2164 pImage->LCHSGeometry = *pLCHSGeometry;
2165 }
2166 else
2167 rc = VERR_VD_NOT_OPENED;
2168
2169 LogFlowFunc(("returns %Rrc\n", rc));
2170 return rc;
2171}
2172
2173/** @copydoc VDIMAGEBACKEND::pfnQueryRegions */
2174static DECLCALLBACK(int) vhdxQueryRegions(void *pBackendData, PCVDREGIONLIST *ppRegionList)
2175{
2176 LogFlowFunc(("pBackendData=%#p ppRegionList=%#p\n", pBackendData, ppRegionList));
2177 PVHDXIMAGE pThis = (PVHDXIMAGE)pBackendData;
2178
2179 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2180
2181 *ppRegionList = &pThis->RegionList;
2182 LogFlowFunc(("returns %Rrc\n", VINF_SUCCESS));
2183 return VINF_SUCCESS;
2184}
2185
2186/** @copydoc VDIMAGEBACKEND::pfnRegionListRelease */
2187static DECLCALLBACK(void) vhdxRegionListRelease(void *pBackendData, PCVDREGIONLIST pRegionList)
2188{
2189 RT_NOREF1(pRegionList);
2190 LogFlowFunc(("pBackendData=%#p pRegionList=%#p\n", pBackendData, pRegionList));
2191 PVHDXIMAGE pThis = (PVHDXIMAGE)pBackendData;
2192 AssertPtr(pThis); RT_NOREF(pThis);
2193
2194 /* Nothing to do here. */
2195}
2196
2197/** @copydoc VDIMAGEBACKEND::pfnGetImageFlags */
2198static DECLCALLBACK(unsigned) vhdxGetImageFlags(void *pBackendData)
2199{
2200 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2201 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2202 unsigned uImageFlags;
2203
2204 AssertPtr(pImage);
2205
2206 if (pImage)
2207 uImageFlags = pImage->uImageFlags;
2208 else
2209 uImageFlags = 0;
2210
2211 LogFlowFunc(("returns %#x\n", uImageFlags));
2212 return uImageFlags;
2213}
2214
2215/** @copydoc VDIMAGEBACKEND::pfnGetOpenFlags */
2216static DECLCALLBACK(unsigned) vhdxGetOpenFlags(void *pBackendData)
2217{
2218 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2219 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2220 unsigned uOpenFlags;
2221
2222 AssertPtr(pImage);
2223
2224 if (pImage)
2225 uOpenFlags = pImage->uOpenFlags;
2226 else
2227 uOpenFlags = 0;
2228
2229 LogFlowFunc(("returns %#x\n", uOpenFlags));
2230 return uOpenFlags;
2231}
2232
2233/** @copydoc VDIMAGEBACKEND::pfnSetOpenFlags */
2234static DECLCALLBACK(int) vhdxSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2235{
2236 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
2237 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2238 int rc = VINF_SUCCESS;
2239
2240 /* Image must be opened and the new flags must be valid. */
2241 if (!pImage || (uOpenFlags & ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
2242 rc = VERR_INVALID_PARAMETER;
2243 else
2244 {
2245 /* Implement this operation via reopening the image. */
2246 rc = vhdxFreeImage(pImage, false);
2247 if (RT_SUCCESS(rc))
2248 rc = vhdxOpenImage(pImage, uOpenFlags);
2249 }
2250
2251 LogFlowFunc(("returns %Rrc\n", rc));
2252 return rc;
2253}
2254
2255/** @copydoc VDIMAGEBACKEND::pfnGetComment */
2256VD_BACKEND_CALLBACK_GET_COMMENT_DEF_NOT_SUPPORTED(vhdxGetComment);
2257
2258/** @copydoc VDIMAGEBACKEND::pfnSetComment */
2259VD_BACKEND_CALLBACK_SET_COMMENT_DEF_NOT_SUPPORTED(vhdxSetComment, PVHDXIMAGE);
2260
2261/** @copydoc VDIMAGEBACKEND::pfnGetUuid */
2262VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(vhdxGetUuid);
2263
2264/** @copydoc VDIMAGEBACKEND::pfnSetUuid */
2265VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(vhdxSetUuid, PVHDXIMAGE);
2266
2267/** @copydoc VDIMAGEBACKEND::pfnGetModificationUuid */
2268VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(vhdxGetModificationUuid);
2269
2270/** @copydoc VDIMAGEBACKEND::pfnSetModificationUuid */
2271VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(vhdxSetModificationUuid, PVHDXIMAGE);
2272
2273/** @copydoc VDIMAGEBACKEND::pfnGetParentUuid */
2274VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(vhdxGetParentUuid);
2275
2276/** @copydoc VDIMAGEBACKEND::pfnSetParentUuid */
2277VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(vhdxSetParentUuid, PVHDXIMAGE);
2278
2279/** @copydoc VDIMAGEBACKEND::pfnGetParentModificationUuid */
2280VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(vhdxGetParentModificationUuid);
2281
2282/** @copydoc VDIMAGEBACKEND::pfnSetParentModificationUuid */
2283VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(vhdxSetParentModificationUuid, PVHDXIMAGE);
2284
2285/** @copydoc VDIMAGEBACKEND::pfnDump */
2286static DECLCALLBACK(void) vhdxDump(void *pBackendData)
2287{
2288 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2289
2290 AssertPtr(pImage);
2291 if (pImage)
2292 {
2293 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%u\n",
2294 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
2295 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
2296 pImage->cbLogicalSector);
2297 }
2298}
2299
2300
2301const VDIMAGEBACKEND g_VhdxBackend =
2302{
2303 /* u32Version */
2304 VD_IMGBACKEND_VERSION,
2305 /* pszBackendName */
2306 "VHDX",
2307 /* uBackendCaps */
2308 VD_CAP_FILE | VD_CAP_VFS,
2309 /* paFileExtensions */
2310 s_aVhdxFileExtensions,
2311 /* paConfigInfo */
2312 NULL,
2313 /* pfnProbe */
2314 vhdxProbe,
2315 /* pfnOpen */
2316 vhdxOpen,
2317 /* pfnCreate */
2318 vhdxCreate,
2319 /* pfnRename */
2320 vhdxRename,
2321 /* pfnClose */
2322 vhdxClose,
2323 /* pfnRead */
2324 vhdxRead,
2325 /* pfnWrite */
2326 vhdxWrite,
2327 /* pfnFlush */
2328 vhdxFlush,
2329 /* pfnDiscard */
2330 NULL,
2331 /* pfnGetVersion */
2332 vhdxGetVersion,
2333 /* pfnGetFileSize */
2334 vhdxGetFileSize,
2335 /* pfnGetPCHSGeometry */
2336 vhdxGetPCHSGeometry,
2337 /* pfnSetPCHSGeometry */
2338 vhdxSetPCHSGeometry,
2339 /* pfnGetLCHSGeometry */
2340 vhdxGetLCHSGeometry,
2341 /* pfnSetLCHSGeometry */
2342 vhdxSetLCHSGeometry,
2343 /* pfnQueryRegions */
2344 vhdxQueryRegions,
2345 /* pfnRegionListRelease */
2346 vhdxRegionListRelease,
2347 /* pfnGetImageFlags */
2348 vhdxGetImageFlags,
2349 /* pfnGetOpenFlags */
2350 vhdxGetOpenFlags,
2351 /* pfnSetOpenFlags */
2352 vhdxSetOpenFlags,
2353 /* pfnGetComment */
2354 vhdxGetComment,
2355 /* pfnSetComment */
2356 vhdxSetComment,
2357 /* pfnGetUuid */
2358 vhdxGetUuid,
2359 /* pfnSetUuid */
2360 vhdxSetUuid,
2361 /* pfnGetModificationUuid */
2362 vhdxGetModificationUuid,
2363 /* pfnSetModificationUuid */
2364 vhdxSetModificationUuid,
2365 /* pfnGetParentUuid */
2366 vhdxGetParentUuid,
2367 /* pfnSetParentUuid */
2368 vhdxSetParentUuid,
2369 /* pfnGetParentModificationUuid */
2370 vhdxGetParentModificationUuid,
2371 /* pfnSetParentModificationUuid */
2372 vhdxSetParentModificationUuid,
2373 /* pfnDump */
2374 vhdxDump,
2375 /* pfnGetTimestamp */
2376 NULL,
2377 /* pfnGetParentTimestamp */
2378 NULL,
2379 /* pfnSetParentTimestamp */
2380 NULL,
2381 /* pfnGetParentFilename */
2382 NULL,
2383 /* pfnSetParentFilename */
2384 NULL,
2385 /* pfnComposeLocation */
2386 genericFileComposeLocation,
2387 /* pfnComposeName */
2388 genericFileComposeName,
2389 /* pfnCompact */
2390 NULL,
2391 /* pfnResize */
2392 NULL,
2393 /* pfnRepair */
2394 NULL,
2395 /* pfnTraverseMetadata */
2396 NULL,
2397 /* u32VersionEnd */
2398 VD_IMGBACKEND_VERSION
2399};
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use