VirtualBox

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

Last change on this file since 63206 was 62743, checked in by vboxsync, 8 years ago

Storage: warnings.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use