VirtualBox

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

Last change on this file since 96973 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 91.0 KB
Line 
1/* $Id: VHDX.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * VHDX - VHDX Disk image, Core Code.
4 */
5
6/*
7 * Copyright (C) 2012-2022 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 * @returns nothing.
620 * @param enmConv Direction of the conversion.
621 * @param pFileIdentifierConv Where to store the converted file identifier.
622 * @param pFileIdentifier The file identifier to convert.
623 *
624 * @note It is safe to use the same pointer for pFileIdentifierConv and pFileIdentifier.
625 */
626DECLINLINE(void) vhdxConvFileIdentifierEndianess(VHDXECONV enmConv, PVhdxFileIdentifier pFileIdentifierConv,
627 PVhdxFileIdentifier pFileIdentifier)
628{
629 pFileIdentifierConv->u64Signature = SET_ENDIAN_U64(pFileIdentifier->u64Signature);
630 for (unsigned i = 0; i < RT_ELEMENTS(pFileIdentifierConv->awszCreator); i++)
631 pFileIdentifierConv->awszCreator[i] = SET_ENDIAN_U16(pFileIdentifier->awszCreator[i]);
632}
633
634/**
635 * Converts a UUID between file and host endianness.
636 *
637 * @returns nothing.
638 * @param enmConv Direction of the conversion.
639 * @param pUuidConv Where to store the converted UUID.
640 * @param pUuid The UUID to convert.
641 *
642 * @note It is safe to use the same pointer for pUuidConv and pUuid.
643 */
644DECLINLINE(void) vhdxConvUuidEndianess(VHDXECONV enmConv, PRTUUID pUuidConv, PRTUUID pUuid)
645{
646 RT_NOREF1(enmConv);
647#if 1
648 /** @todo r=andy Code looks temporary disabled to me, fixes strict release builds:
649 * "accessing 16 bytes at offsets 0 and 0 overlaps 16 bytes at offset 0 [-Werror=restrict]" */
650 RTUUID uuidTmp;
651 memcpy(&uuidTmp, pUuid, sizeof(RTUUID));
652 memcpy(pUuidConv, &uuidTmp, sizeof(RTUUID));
653#else
654 pUuidConv->Gen.u32TimeLow = SET_ENDIAN_U32(pUuid->Gen.u32TimeLow);
655 pUuidConv->Gen.u16TimeMid = SET_ENDIAN_U16(pUuid->Gen.u16TimeMid);
656 pUuidConv->Gen.u16TimeHiAndVersion = SET_ENDIAN_U16(pUuid->Gen.u16TimeHiAndVersion);
657 pUuidConv->Gen.u8ClockSeqHiAndReserved = pUuid->Gen.u8ClockSeqHiAndReserved;
658 pUuidConv->Gen.u8ClockSeqLow = pUuid->Gen.u8ClockSeqLow;
659 for (unsigned i = 0; i < RT_ELEMENTS(pUuidConv->Gen.au8Node); i++)
660 pUuidConv->Gen.au8Node[i] = pUuid->Gen.au8Node[i];
661#endif
662}
663
664/**
665 * Converts a VHDX header between file and host endianness.
666 *
667 * @returns nothing.
668 * @param enmConv Direction of the conversion.
669 * @param pHdrConv Where to store the converted header.
670 * @param pHdr The VHDX header to convert.
671 *
672 * @note It is safe to use the same pointer for pHdrConv and pHdr.
673 */
674DECLINLINE(void) vhdxConvHeaderEndianess(VHDXECONV enmConv, PVhdxHeader pHdrConv, PVhdxHeader pHdr)
675{
676 pHdrConv->u32Signature = SET_ENDIAN_U32(pHdr->u32Signature);
677 pHdrConv->u32Checksum = SET_ENDIAN_U32(pHdr->u32Checksum);
678 pHdrConv->u64SequenceNumber = SET_ENDIAN_U64(pHdr->u64SequenceNumber);
679 vhdxConvUuidEndianess(enmConv, &pHdrConv->UuidFileWrite, &pHdrConv->UuidFileWrite);
680 vhdxConvUuidEndianess(enmConv, &pHdrConv->UuidDataWrite, &pHdrConv->UuidDataWrite);
681 vhdxConvUuidEndianess(enmConv, &pHdrConv->UuidLog, &pHdrConv->UuidLog);
682 pHdrConv->u16LogVersion = SET_ENDIAN_U16(pHdr->u16LogVersion);
683 pHdrConv->u16Version = SET_ENDIAN_U16(pHdr->u16Version);
684 pHdrConv->u32LogLength = SET_ENDIAN_U32(pHdr->u32LogLength);
685 pHdrConv->u64LogOffset = SET_ENDIAN_U64(pHdr->u64LogOffset);
686}
687
688/**
689 * Converts a VHDX region table header between file and host endianness.
690 *
691 * @returns nothing.
692 * @param enmConv Direction of the conversion.
693 * @param pRegTblHdrConv Where to store the converted header.
694 * @param pRegTblHdr The VHDX region table header to convert.
695 *
696 * @note It is safe to use the same pointer for pRegTblHdrConv and pRegTblHdr.
697 */
698DECLINLINE(void) vhdxConvRegionTblHdrEndianess(VHDXECONV enmConv, PVhdxRegionTblHdr pRegTblHdrConv,
699 PVhdxRegionTblHdr pRegTblHdr)
700{
701 pRegTblHdrConv->u32Signature = SET_ENDIAN_U32(pRegTblHdr->u32Signature);
702 pRegTblHdrConv->u32Checksum = SET_ENDIAN_U32(pRegTblHdr->u32Checksum);
703 pRegTblHdrConv->u32EntryCount = SET_ENDIAN_U32(pRegTblHdr->u32EntryCount);
704 pRegTblHdrConv->u32Reserved = SET_ENDIAN_U32(pRegTblHdr->u32Reserved);
705}
706
707/**
708 * Converts a VHDX region table entry between file and host endianness.
709 *
710 * @returns nothing.
711 * @param enmConv Direction of the conversion.
712 * @param pRegTblEntConv Where to store the converted region table entry.
713 * @param pRegTblEnt The VHDX region table entry to convert.
714 *
715 * @note It is safe to use the same pointer for pRegTblEntConv and pRegTblEnt.
716 */
717DECLINLINE(void) vhdxConvRegionTblEntryEndianess(VHDXECONV enmConv, PVhdxRegionTblEntry pRegTblEntConv,
718 PVhdxRegionTblEntry pRegTblEnt)
719{
720 vhdxConvUuidEndianess(enmConv, &pRegTblEntConv->UuidObject, &pRegTblEnt->UuidObject);
721 pRegTblEntConv->u64FileOffset = SET_ENDIAN_U64(pRegTblEnt->u64FileOffset);
722 pRegTblEntConv->u32Length = SET_ENDIAN_U32(pRegTblEnt->u32Length);
723 pRegTblEntConv->u32Flags = SET_ENDIAN_U32(pRegTblEnt->u32Flags);
724}
725
726#if 0 /* unused */
727
728/**
729 * Converts a VHDX log entry header between file and host endianness.
730 *
731 * @returns nothing.
732 * @param enmConv Direction of the conversion.
733 * @param pLogEntryHdrConv Where to store the converted log entry header.
734 * @param pLogEntryHdr The VHDX log entry header to convert.
735 *
736 * @note It is safe to use the same pointer for pLogEntryHdrConv and pLogEntryHdr.
737 */
738DECLINLINE(void) vhdxConvLogEntryHdrEndianess(VHDXECONV enmConv, PVhdxLogEntryHdr pLogEntryHdrConv,
739 PVhdxLogEntryHdr pLogEntryHdr)
740{
741 pLogEntryHdrConv->u32Signature = SET_ENDIAN_U32(pLogEntryHdr->u32Signature);
742 pLogEntryHdrConv->u32Checksum = SET_ENDIAN_U32(pLogEntryHdr->u32Checksum);
743 pLogEntryHdrConv->u32EntryLength = SET_ENDIAN_U32(pLogEntryHdr->u32EntryLength);
744 pLogEntryHdrConv->u32Tail = SET_ENDIAN_U32(pLogEntryHdr->u32Tail);
745 pLogEntryHdrConv->u64SequenceNumber = SET_ENDIAN_U64(pLogEntryHdr->u64SequenceNumber);
746 pLogEntryHdrConv->u32DescriptorCount = SET_ENDIAN_U32(pLogEntryHdr->u32DescriptorCount);
747 pLogEntryHdrConv->u32Reserved = SET_ENDIAN_U32(pLogEntryHdr->u32Reserved);
748 vhdxConvUuidEndianess(enmConv, &pLogEntryHdrConv->UuidLog, &pLogEntryHdr->UuidLog);
749 pLogEntryHdrConv->u64FlushedFileOffset = SET_ENDIAN_U64(pLogEntryHdr->u64FlushedFileOffset);
750}
751
752/**
753 * Converts a VHDX log zero descriptor between file and host endianness.
754 *
755 * @returns nothing.
756 * @param enmConv Direction of the conversion.
757 * @param pLogZeroDescConv Where to store the converted log zero descriptor.
758 * @param pLogZeroDesc The VHDX log zero descriptor to convert.
759 *
760 * @note It is safe to use the same pointer for pLogZeroDescConv and pLogZeroDesc.
761 */
762DECLINLINE(void) vhdxConvLogZeroDescEndianess(VHDXECONV enmConv, PVhdxLogZeroDesc pLogZeroDescConv,
763 PVhdxLogZeroDesc pLogZeroDesc)
764{
765 pLogZeroDescConv->u32ZeroSignature = SET_ENDIAN_U32(pLogZeroDesc->u32ZeroSignature);
766 pLogZeroDescConv->u32Reserved = SET_ENDIAN_U32(pLogZeroDesc->u32Reserved);
767 pLogZeroDescConv->u64ZeroLength = SET_ENDIAN_U64(pLogZeroDesc->u64ZeroLength);
768 pLogZeroDescConv->u64FileOffset = SET_ENDIAN_U64(pLogZeroDesc->u64FileOffset);
769 pLogZeroDescConv->u64SequenceNumber = SET_ENDIAN_U64(pLogZeroDesc->u64SequenceNumber);
770}
771
772
773/**
774 * Converts a VHDX log data descriptor between file and host endianness.
775 *
776 * @returns nothing.
777 * @param enmConv Direction of the conversion.
778 * @param pLogDataDescConv Where to store the converted log data descriptor.
779 * @param pLogDataDesc The VHDX log data descriptor to convert.
780 *
781 * @note It is safe to use the same pointer for pLogDataDescConv and pLogDataDesc.
782 */
783DECLINLINE(void) vhdxConvLogDataDescEndianess(VHDXECONV enmConv, PVhdxLogDataDesc pLogDataDescConv,
784 PVhdxLogDataDesc pLogDataDesc)
785{
786 pLogDataDescConv->u32DataSignature = SET_ENDIAN_U32(pLogDataDesc->u32DataSignature);
787 pLogDataDescConv->u32TrailingBytes = SET_ENDIAN_U32(pLogDataDesc->u32TrailingBytes);
788 pLogDataDescConv->u64LeadingBytes = SET_ENDIAN_U64(pLogDataDesc->u64LeadingBytes);
789 pLogDataDescConv->u64FileOffset = SET_ENDIAN_U64(pLogDataDesc->u64FileOffset);
790 pLogDataDescConv->u64SequenceNumber = SET_ENDIAN_U64(pLogDataDesc->u64SequenceNumber);
791}
792
793
794/**
795 * Converts a VHDX log data sector between file and host endianness.
796 *
797 * @returns nothing.
798 * @param enmConv Direction of the conversion.
799 * @param pLogDataSectorConv Where to store the converted log data sector.
800 * @param pLogDataSector The VHDX log data sector to convert.
801 *
802 * @note It is safe to use the same pointer for pLogDataSectorConv and pLogDataSector.
803 */
804DECLINLINE(void) vhdxConvLogDataSectorEndianess(VHDXECONV enmConv, PVhdxLogDataSector pLogDataSectorConv,
805 PVhdxLogDataSector pLogDataSector)
806{
807 pLogDataSectorConv->u32DataSignature = SET_ENDIAN_U32(pLogDataSector->u32DataSignature);
808 pLogDataSectorConv->u32SequenceHigh = SET_ENDIAN_U32(pLogDataSector->u32SequenceHigh);
809 pLogDataSectorConv->u32SequenceLow = SET_ENDIAN_U32(pLogDataSector->u32SequenceLow);
810}
811
812#endif /* unused */
813
814/**
815 * Converts a BAT between file and host endianess.
816 *
817 * @returns nothing.
818 * @param enmConv Direction of the conversion.
819 * @param paBatEntriesConv Where to store the converted BAT.
820 * @param paBatEntries The VHDX BAT to convert.
821 * @param cBatEntries Number of entries in the BAT.
822 *
823 * @note It is safe to use the same pointer for paBatEntriesConv and paBatEntries.
824 */
825DECLINLINE(void) vhdxConvBatTableEndianess(VHDXECONV enmConv, PVhdxBatEntry paBatEntriesConv,
826 PVhdxBatEntry paBatEntries, uint32_t cBatEntries)
827{
828 for (uint32_t i = 0; i < cBatEntries; i++)
829 paBatEntriesConv[i].u64BatEntry = SET_ENDIAN_U64(paBatEntries[i].u64BatEntry);
830}
831
832/**
833 * Converts a VHDX metadata table header between file and host endianness.
834 *
835 * @returns nothing.
836 * @param enmConv Direction of the conversion.
837 * @param pMetadataTblHdrConv Where to store the converted metadata table header.
838 * @param pMetadataTblHdr The VHDX metadata table header to convert.
839 *
840 * @note It is safe to use the same pointer for pMetadataTblHdrConv and pMetadataTblHdr.
841 */
842DECLINLINE(void) vhdxConvMetadataTblHdrEndianess(VHDXECONV enmConv, PVhdxMetadataTblHdr pMetadataTblHdrConv,
843 PVhdxMetadataTblHdr pMetadataTblHdr)
844{
845 pMetadataTblHdrConv->u64Signature = SET_ENDIAN_U64(pMetadataTblHdr->u64Signature);
846 pMetadataTblHdrConv->u16Reserved = SET_ENDIAN_U16(pMetadataTblHdr->u16Reserved);
847 pMetadataTblHdrConv->u16EntryCount = SET_ENDIAN_U16(pMetadataTblHdr->u16EntryCount);
848 for (unsigned i = 0; i < RT_ELEMENTS(pMetadataTblHdr->u32Reserved2); i++)
849 pMetadataTblHdrConv->u32Reserved2[i] = SET_ENDIAN_U32(pMetadataTblHdr->u32Reserved2[i]);
850}
851
852/**
853 * Converts a VHDX metadata table entry between file and host endianness.
854 *
855 * @returns nothing.
856 * @param enmConv Direction of the conversion.
857 * @param pMetadataTblEntryConv Where to store the converted metadata table entry.
858 * @param pMetadataTblEntry The VHDX metadata table entry to convert.
859 *
860 * @note It is safe to use the same pointer for pMetadataTblEntryConv and pMetadataTblEntry.
861 */
862DECLINLINE(void) vhdxConvMetadataTblEntryEndianess(VHDXECONV enmConv, PVhdxMetadataTblEntry pMetadataTblEntryConv,
863 PVhdxMetadataTblEntry pMetadataTblEntry)
864{
865 vhdxConvUuidEndianess(enmConv, &pMetadataTblEntryConv->UuidItem, &pMetadataTblEntry->UuidItem);
866 pMetadataTblEntryConv->u32Offset = SET_ENDIAN_U32(pMetadataTblEntry->u32Offset);
867 pMetadataTblEntryConv->u32Length = SET_ENDIAN_U32(pMetadataTblEntry->u32Length);
868 pMetadataTblEntryConv->u32Flags = SET_ENDIAN_U32(pMetadataTblEntry->u32Flags);
869 pMetadataTblEntryConv->u32Reserved = SET_ENDIAN_U32(pMetadataTblEntry->u32Reserved);
870}
871
872/**
873 * Converts a VHDX file parameters item between file and host endianness.
874 *
875 * @returns nothing.
876 * @param enmConv Direction of the conversion.
877 * @param pFileParamsConv Where to store the converted file parameters item entry.
878 * @param pFileParams The VHDX file parameters item to convert.
879 *
880 * @note It is safe to use the same pointer for pFileParamsConv and pFileParams.
881 */
882DECLINLINE(void) vhdxConvFileParamsEndianess(VHDXECONV enmConv, PVhdxFileParameters pFileParamsConv,
883 PVhdxFileParameters pFileParams)
884{
885 pFileParamsConv->u32BlockSize = SET_ENDIAN_U32(pFileParams->u32BlockSize);
886 pFileParamsConv->u32Flags = SET_ENDIAN_U32(pFileParams->u32Flags);
887}
888
889/**
890 * Converts a VHDX virtual disk size item between file and host endianness.
891 *
892 * @returns nothing.
893 * @param enmConv Direction of the conversion.
894 * @param pVDiskSizeConv Where to store the converted virtual disk size item entry.
895 * @param pVDiskSize The VHDX virtual disk size item to convert.
896 *
897 * @note It is safe to use the same pointer for pVDiskSizeConv and pVDiskSize.
898 */
899DECLINLINE(void) vhdxConvVDiskSizeEndianess(VHDXECONV enmConv, PVhdxVDiskSize pVDiskSizeConv,
900 PVhdxVDiskSize pVDiskSize)
901{
902 pVDiskSizeConv->u64VDiskSize = SET_ENDIAN_U64(pVDiskSize->u64VDiskSize);
903}
904
905#if 0 /* unused */
906
907/**
908 * Converts a VHDX page 83 data item between file and host endianness.
909 *
910 * @returns nothing.
911 * @param enmConv Direction of the conversion.
912 * @param pPage83DataConv Where to store the converted page 83 data item entry.
913 * @param pPage83Data The VHDX page 83 data item to convert.
914 *
915 * @note It is safe to use the same pointer for pPage83DataConv and pPage83Data.
916 */
917DECLINLINE(void) vhdxConvPage83DataEndianess(VHDXECONV enmConv, PVhdxPage83Data pPage83DataConv,
918 PVhdxPage83Data pPage83Data)
919{
920 vhdxConvUuidEndianess(enmConv, &pPage83DataConv->UuidPage83Data, &pPage83Data->UuidPage83Data);
921}
922#endif /* unused */
923
924/**
925 * Converts a VHDX logical sector size item between file and host endianness.
926 *
927 * @returns nothing.
928 * @param enmConv Direction of the conversion.
929 * @param pVDiskLogSectSizeConv Where to store the converted logical sector size item entry.
930 * @param pVDiskLogSectSize The VHDX logical sector size item to convert.
931 *
932 * @note It is safe to use the same pointer for pVDiskLogSectSizeConv and pVDiskLogSectSize.
933 */
934DECLINLINE(void) vhdxConvVDiskLogSectSizeEndianess(VHDXECONV enmConv, PVhdxVDiskLogicalSectorSize pVDiskLogSectSizeConv,
935 PVhdxVDiskLogicalSectorSize pVDiskLogSectSize)
936{
937 pVDiskLogSectSizeConv->u32LogicalSectorSize = SET_ENDIAN_U32(pVDiskLogSectSize->u32LogicalSectorSize);
938}
939
940#if 0 /* unused */
941
942/**
943 * Converts a VHDX physical sector size item between file and host endianness.
944 *
945 * @returns nothing.
946 * @param enmConv Direction of the conversion.
947 * @param pVDiskPhysSectSizeConv Where to store the converted physical sector size item entry.
948 * @param pVDiskPhysSectSize The VHDX physical sector size item to convert.
949 *
950 * @note It is safe to use the same pointer for pVDiskPhysSectSizeConv and pVDiskPhysSectSize.
951 */
952DECLINLINE(void) vhdxConvVDiskPhysSectSizeEndianess(VHDXECONV enmConv, PVhdxVDiskPhysicalSectorSize pVDiskPhysSectSizeConv,
953 PVhdxVDiskPhysicalSectorSize pVDiskPhysSectSize)
954{
955 pVDiskPhysSectSizeConv->u64PhysicalSectorSize = SET_ENDIAN_U64(pVDiskPhysSectSize->u64PhysicalSectorSize);
956}
957
958
959/**
960 * Converts a VHDX parent locator header item between file and host endianness.
961 *
962 * @returns nothing.
963 * @param enmConv Direction of the conversion.
964 * @param pParentLocatorHdrConv Where to store the converted parent locator header item entry.
965 * @param pParentLocatorHdr The VHDX parent locator header item to convert.
966 *
967 * @note It is safe to use the same pointer for pParentLocatorHdrConv and pParentLocatorHdr.
968 */
969DECLINLINE(void) vhdxConvParentLocatorHeaderEndianness(VHDXECONV enmConv, PVhdxParentLocatorHeader pParentLocatorHdrConv,
970 PVhdxParentLocatorHeader pParentLocatorHdr)
971{
972 vhdxConvUuidEndianess(enmConv, &pParentLocatorHdrConv->UuidLocatorType, &pParentLocatorHdr->UuidLocatorType);
973 pParentLocatorHdrConv->u16Reserved = SET_ENDIAN_U16(pParentLocatorHdr->u16Reserved);
974 pParentLocatorHdrConv->u16KeyValueCount = SET_ENDIAN_U16(pParentLocatorHdr->u16KeyValueCount);
975}
976
977
978/**
979 * Converts a VHDX parent locator entry between file and host endianness.
980 *
981 * @returns nothing.
982 * @param enmConv Direction of the conversion.
983 * @param pParentLocatorEntryConv Where to store the converted parent locator entry.
984 * @param pParentLocatorEntry The VHDX parent locator entry to convert.
985 *
986 * @note It is safe to use the same pointer for pParentLocatorEntryConv and pParentLocatorEntry.
987 */
988DECLINLINE(void) vhdxConvParentLocatorEntryEndianess(VHDXECONV enmConv, PVhdxParentLocatorEntry pParentLocatorEntryConv,
989 PVhdxParentLocatorEntry pParentLocatorEntry)
990{
991 pParentLocatorEntryConv->u32KeyOffset = SET_ENDIAN_U32(pParentLocatorEntry->u32KeyOffset);
992 pParentLocatorEntryConv->u32ValueOffset = SET_ENDIAN_U32(pParentLocatorEntry->u32ValueOffset);
993 pParentLocatorEntryConv->u16KeyLength = SET_ENDIAN_U16(pParentLocatorEntry->u16KeyLength);
994 pParentLocatorEntryConv->u16ValueLength = SET_ENDIAN_U16(pParentLocatorEntry->u16ValueLength);
995}
996
997#endif /* unused */
998
999/**
1000 * Internal. Free all allocated space for representing an image except pImage,
1001 * and optionally delete the image from disk.
1002 */
1003static int vhdxFreeImage(PVHDXIMAGE pImage, bool fDelete)
1004{
1005 int rc = VINF_SUCCESS;
1006
1007 /* Freeing a never allocated image (e.g. because the open failed) is
1008 * not signalled as an error. After all nothing bad happens. */
1009 if (pImage)
1010 {
1011 if (pImage->pStorage)
1012 {
1013 rc = vdIfIoIntFileClose(pImage->pIfIo, pImage->pStorage);
1014 pImage->pStorage = NULL;
1015 }
1016
1017 if (pImage->paBat)
1018 {
1019 RTMemFree(pImage->paBat);
1020 pImage->paBat = NULL;
1021 }
1022
1023 if (fDelete && pImage->pszFilename)
1024 vdIfIoIntFileDelete(pImage->pIfIo, pImage->pszFilename);
1025 }
1026
1027 LogFlowFunc(("returns %Rrc\n", rc));
1028 return rc;
1029}
1030
1031/**
1032 * Loads all required fields from the given VHDX header.
1033 * The header must be converted to the host endianess and validated already.
1034 *
1035 * @returns VBox status code.
1036 * @param pImage Image instance data.
1037 * @param pHdr The header to load.
1038 */
1039static int vhdxLoadHeader(PVHDXIMAGE pImage, PVhdxHeader pHdr)
1040{
1041 int rc = VINF_SUCCESS;
1042
1043 LogFlowFunc(("pImage=%#p pHdr=%#p\n", pImage, pHdr));
1044
1045 /*
1046 * Most fields in the header are not required because the backend implements
1047 * readonly access only so far.
1048 * We just have to check that the log is empty, we have to refuse to load the
1049 * image otherwsie because replaying the log is not implemented.
1050 */
1051 if (pHdr->u16Version == VHDX_HEADER_VHDX_VERSION)
1052 {
1053 /* Check that the log UUID is zero. */
1054 pImage->uVersion = pHdr->u16Version;
1055 if (!RTUuidIsNull(&pHdr->UuidLog))
1056 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1057 "VHDX: Image \'%s\' has a non empty log which is not supported",
1058 pImage->pszFilename);
1059 }
1060 else
1061 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1062 "VHDX: Image \'%s\' uses an unsupported version (%u) of the VHDX format",
1063 pImage->pszFilename, pHdr->u16Version);
1064
1065 LogFlowFunc(("return rc=%Rrc\n", rc));
1066 return rc;
1067}
1068
1069/**
1070 * Determines the current header and loads it.
1071 *
1072 * @returns VBox status code.
1073 * @param pImage Image instance data.
1074 */
1075static int vhdxFindAndLoadCurrentHeader(PVHDXIMAGE pImage)
1076{
1077 PVhdxHeader pHdr1, pHdr2;
1078 uint32_t u32ChkSum = 0;
1079 uint32_t u32ChkSumSaved = 0;
1080 bool fHdr1Valid = false;
1081 bool fHdr2Valid = false;
1082 int rc = VINF_SUCCESS;
1083
1084 LogFlowFunc(("pImage=%#p\n", pImage));
1085
1086 /*
1087 * The VHDX format defines two headers at different offsets to provide failure
1088 * consistency. Only one header is current. This can be determined using the
1089 * sequence number and checksum fields in the header.
1090 */
1091 pHdr1 = (PVhdxHeader)RTMemAllocZ(sizeof(VhdxHeader));
1092 pHdr2 = (PVhdxHeader)RTMemAllocZ(sizeof(VhdxHeader));
1093
1094 if (pHdr1 && pHdr2)
1095 {
1096 /* Read the first header. */
1097 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, VHDX_HEADER1_OFFSET,
1098 pHdr1, sizeof(*pHdr1));
1099 if (RT_SUCCESS(rc))
1100 {
1101 vhdxConvHeaderEndianess(VHDXECONV_F2H, pHdr1, pHdr1);
1102
1103 /* Validate checksum. */
1104 u32ChkSumSaved = pHdr1->u32Checksum;
1105 pHdr1->u32Checksum = 0;
1106 u32ChkSum = RTCrc32C(pHdr1, sizeof(VhdxHeader));
1107
1108 if ( pHdr1->u32Signature == VHDX_HEADER_SIGNATURE
1109 && u32ChkSum == u32ChkSumSaved)
1110 fHdr1Valid = true;
1111 }
1112
1113 /* Try to read the second header in any case (even if reading the first failed). */
1114 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, VHDX_HEADER2_OFFSET,
1115 pHdr2, sizeof(*pHdr2));
1116 if (RT_SUCCESS(rc))
1117 {
1118 vhdxConvHeaderEndianess(VHDXECONV_F2H, pHdr2, pHdr2);
1119
1120 /* Validate checksum. */
1121 u32ChkSumSaved = pHdr2->u32Checksum;
1122 pHdr2->u32Checksum = 0;
1123 u32ChkSum = RTCrc32C(pHdr2, sizeof(VhdxHeader));
1124
1125 if ( pHdr2->u32Signature == VHDX_HEADER_SIGNATURE
1126 && u32ChkSum == u32ChkSumSaved)
1127 fHdr2Valid = true;
1128 }
1129
1130 /* Determine the current header. */
1131 if (fHdr1Valid != fHdr2Valid)
1132 {
1133 /* Only one header is valid - use it. */
1134 rc = vhdxLoadHeader(pImage, fHdr1Valid ? pHdr1 : pHdr2);
1135 }
1136 else if (!fHdr1Valid && !fHdr2Valid)
1137 {
1138 /* Crap, both headers are corrupt, refuse to load the image. */
1139 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1140 "VHDX: Can not load the image because both headers are corrupt");
1141 }
1142 else
1143 {
1144 /* Both headers are valid. Use the sequence number to find the current one. */
1145 if (pHdr1->u64SequenceNumber > pHdr2->u64SequenceNumber)
1146 rc = vhdxLoadHeader(pImage, pHdr1);
1147 else
1148 rc = vhdxLoadHeader(pImage, pHdr2);
1149 }
1150 }
1151 else
1152 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1153 "VHDX: Out of memory while allocating memory for the header");
1154
1155 if (pHdr1)
1156 RTMemFree(pHdr1);
1157 if (pHdr2)
1158 RTMemFree(pHdr2);
1159
1160 LogFlowFunc(("returns rc=%Rrc\n", rc));
1161 return rc;
1162}
1163
1164/**
1165 * Loads the BAT region.
1166 *
1167 * @returns VBox status code.
1168 * @param pImage Image instance data.
1169 * @param offRegion Start offset of the region.
1170 * @param cbRegion Size of the region.
1171 */
1172static int vhdxLoadBatRegion(PVHDXIMAGE pImage, uint64_t offRegion,
1173 size_t cbRegion)
1174{
1175 int rc = VINF_SUCCESS;
1176 uint32_t cDataBlocks;
1177 uint32_t uChunkRatio;
1178 uint32_t cSectorBitmapBlocks;
1179 uint32_t cBatEntries;
1180 uint32_t cbBatEntries;
1181 PVhdxBatEntry paBatEntries = NULL;
1182
1183 LogFlowFunc(("pImage=%#p\n", pImage));
1184
1185 /* Calculate required values first. */
1186 uint64_t uChunkRatio64 = (RT_BIT_64(23) * pImage->cbLogicalSector) / pImage->cbBlock;
1187 uChunkRatio = (uint32_t)uChunkRatio64; Assert(uChunkRatio == uChunkRatio64);
1188 uint64_t cDataBlocks64 = pImage->cbSize / pImage->cbBlock;
1189 cDataBlocks = (uint32_t)cDataBlocks64; Assert(cDataBlocks == cDataBlocks64);
1190
1191 if (pImage->cbSize % pImage->cbBlock)
1192 cDataBlocks++;
1193
1194 cSectorBitmapBlocks = cDataBlocks / uChunkRatio;
1195 if (cDataBlocks % uChunkRatio)
1196 cSectorBitmapBlocks++;
1197
1198 cBatEntries = cDataBlocks + (cDataBlocks - 1)/uChunkRatio;
1199 cbBatEntries = cBatEntries * sizeof(VhdxBatEntry);
1200
1201 if (cbBatEntries <= cbRegion)
1202 {
1203 /*
1204 * Load the complete BAT region first, convert to host endianess and process
1205 * it afterwards. The SB entries can be removed because they are not needed yet.
1206 */
1207 paBatEntries = (PVhdxBatEntry)RTMemAlloc(cbBatEntries);
1208 if (paBatEntries)
1209 {
1210 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offRegion,
1211 paBatEntries, cbBatEntries);
1212 if (RT_SUCCESS(rc))
1213 {
1214 vhdxConvBatTableEndianess(VHDXECONV_F2H, paBatEntries, paBatEntries,
1215 cBatEntries);
1216
1217 /* Go through the table and validate it. */
1218 for (unsigned i = 0; i < cBatEntries; i++)
1219 {
1220 if ( i != 0
1221 && (i % uChunkRatio) == 0)
1222 {
1223/**
1224 * Disabled the verification because there are images out there with the sector bitmap
1225 * marked as present. The entry is never accessed and the image is readonly anyway,
1226 * so no harm done.
1227 */
1228#if 0
1229 /* Sector bitmap block. */
1230 if ( VHDX_BAT_ENTRY_GET_STATE(paBatEntries[i].u64BatEntry)
1231 != VHDX_BAT_ENTRY_SB_BLOCK_NOT_PRESENT)
1232 {
1233 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1234 "VHDX: Sector bitmap block at entry %u of image \'%s\' marked as present, violation of the specification",
1235 i, pImage->pszFilename);
1236 break;
1237 }
1238#endif
1239 }
1240 else
1241 {
1242 /* Payload block. */
1243 if ( VHDX_BAT_ENTRY_GET_STATE(paBatEntries[i].u64BatEntry)
1244 == VHDX_BAT_ENTRY_PAYLOAD_BLOCK_PARTIALLY_PRESENT)
1245 {
1246 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1247 "VHDX: Payload block at entry %u of image \'%s\' marked as partially present, violation of the specification",
1248 i, pImage->pszFilename);
1249 break;
1250 }
1251 }
1252 }
1253
1254 if (RT_SUCCESS(rc))
1255 {
1256 pImage->paBat = paBatEntries;
1257 pImage->uChunkRatio = uChunkRatio;
1258 }
1259 }
1260 else
1261 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1262 "VHDX: Error reading the BAT from image \'%s\'",
1263 pImage->pszFilename);
1264 }
1265 else
1266 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1267 "VHDX: Out of memory allocating memory for %u BAT entries of image \'%s\'",
1268 cBatEntries, pImage->pszFilename);
1269 }
1270 else
1271 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1272 "VHDX: Mismatch between calculated number of BAT entries and region size (expected %u got %u) for image \'%s\'",
1273 cbBatEntries, cbRegion, pImage->pszFilename);
1274
1275 if ( RT_FAILURE(rc)
1276 && paBatEntries)
1277 RTMemFree(paBatEntries);
1278
1279 LogFlowFunc(("returns rc=%Rrc\n", rc));
1280 return rc;
1281}
1282
1283/**
1284 * Load the file parameters metadata item from the file.
1285 *
1286 * @returns VBox status code.
1287 * @param pImage Image instance data.
1288 * @param offItem File offset where the data is stored.
1289 * @param cbItem Size of the item in the file.
1290 */
1291static int vhdxLoadFileParametersMetadata(PVHDXIMAGE pImage, uint64_t offItem, size_t cbItem)
1292{
1293 int rc = VINF_SUCCESS;
1294
1295 LogFlowFunc(("pImage=%#p offItem=%llu cbItem=%zu\n", pImage, offItem, cbItem));
1296
1297 if (cbItem != sizeof(VhdxFileParameters))
1298 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1299 "VHDX: File parameters item size mismatch (expected %u got %zu) in image \'%s\'",
1300 sizeof(VhdxFileParameters), cbItem, pImage->pszFilename);
1301 else
1302 {
1303 VhdxFileParameters FileParameters;
1304
1305 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offItem,
1306 &FileParameters, sizeof(FileParameters));
1307 if (RT_SUCCESS(rc))
1308 {
1309 vhdxConvFileParamsEndianess(VHDXECONV_F2H, &FileParameters, &FileParameters);
1310 pImage->cbBlock = FileParameters.u32BlockSize;
1311
1312 /** @todo No support for differencing images yet. */
1313 if (FileParameters.u32Flags & VHDX_FILE_PARAMETERS_FLAGS_HAS_PARENT)
1314 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1315 "VHDX: Image \'%s\' is a differencing image which is not supported yet",
1316 pImage->pszFilename);
1317 }
1318 else
1319 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1320 "VHDX: Reading the file parameters metadata item from image \'%s\' failed",
1321 pImage->pszFilename);
1322 }
1323
1324 LogFlowFunc(("returns rc=%Rrc\n", rc));
1325 return rc;
1326}
1327
1328/**
1329 * Load the virtual disk size metadata item from the file.
1330 *
1331 * @returns VBox status code.
1332 * @param pImage Image instance data.
1333 * @param offItem File offset where the data is stored.
1334 * @param cbItem Size of the item in the file.
1335 */
1336static int vhdxLoadVDiskSizeMetadata(PVHDXIMAGE pImage, uint64_t offItem, size_t cbItem)
1337{
1338 int rc = VINF_SUCCESS;
1339
1340 LogFlowFunc(("pImage=%#p offItem=%llu cbItem=%zu\n", pImage, offItem, cbItem));
1341
1342 if (cbItem != sizeof(VhdxVDiskSize))
1343 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1344 "VHDX: Virtual disk size item size mismatch (expected %u got %zu) in image \'%s\'",
1345 sizeof(VhdxVDiskSize), cbItem, pImage->pszFilename);
1346 else
1347 {
1348 VhdxVDiskSize VDiskSize;
1349
1350 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offItem,
1351 &VDiskSize, sizeof(VDiskSize));
1352 if (RT_SUCCESS(rc))
1353 {
1354 vhdxConvVDiskSizeEndianess(VHDXECONV_F2H, &VDiskSize, &VDiskSize);
1355 pImage->cbSize = VDiskSize.u64VDiskSize;
1356 }
1357 else
1358 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1359 "VHDX: Reading the virtual disk size metadata item from image \'%s\' failed",
1360 pImage->pszFilename);
1361 }
1362
1363 LogFlowFunc(("returns rc=%Rrc\n", rc));
1364 return rc;
1365}
1366
1367/**
1368 * Load the logical sector size metadata item from the file.
1369 *
1370 * @returns VBox status code.
1371 * @param pImage Image instance data.
1372 * @param offItem File offset where the data is stored.
1373 * @param cbItem Size of the item in the file.
1374 */
1375static int vhdxLoadVDiskLogSectorSizeMetadata(PVHDXIMAGE pImage, uint64_t offItem, size_t cbItem)
1376{
1377 int rc = VINF_SUCCESS;
1378
1379 LogFlowFunc(("pImage=%#p offItem=%llu cbItem=%zu\n", pImage, offItem, cbItem));
1380
1381 if (cbItem != sizeof(VhdxVDiskLogicalSectorSize))
1382 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1383 "VHDX: Virtual disk logical sector size item size mismatch (expected %u got %zu) in image \'%s\'",
1384 sizeof(VhdxVDiskLogicalSectorSize), cbItem, pImage->pszFilename);
1385 else
1386 {
1387 VhdxVDiskLogicalSectorSize VDiskLogSectSize;
1388
1389 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offItem,
1390 &VDiskLogSectSize, sizeof(VDiskLogSectSize));
1391 if (RT_SUCCESS(rc))
1392 {
1393 vhdxConvVDiskLogSectSizeEndianess(VHDXECONV_F2H, &VDiskLogSectSize,
1394 &VDiskLogSectSize);
1395 pImage->cbLogicalSector = VDiskLogSectSize.u32LogicalSectorSize;
1396 }
1397 else
1398 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1399 "VHDX: Reading the virtual disk logical sector size metadata item from image \'%s\' failed",
1400 pImage->pszFilename);
1401 }
1402
1403 LogFlowFunc(("returns rc=%Rrc\n", rc));
1404 return rc;
1405}
1406
1407/**
1408 * Loads the metadata region.
1409 *
1410 * @returns VBox status code.
1411 * @param pImage Image instance data.
1412 * @param offRegion Start offset of the region.
1413 * @param cbRegion Size of the region.
1414 */
1415static int vhdxLoadMetadataRegion(PVHDXIMAGE pImage, uint64_t offRegion,
1416 size_t cbRegion)
1417{
1418 VhdxMetadataTblHdr MetadataTblHdr;
1419 int rc = VINF_SUCCESS;
1420
1421 LogFlowFunc(("pImage=%#p\n", pImage));
1422
1423 /* Load the header first. */
1424 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offRegion,
1425 &MetadataTblHdr, sizeof(MetadataTblHdr));
1426 if (RT_SUCCESS(rc))
1427 {
1428 vhdxConvMetadataTblHdrEndianess(VHDXECONV_F2H, &MetadataTblHdr, &MetadataTblHdr);
1429
1430 /* Validate structure. */
1431 if (MetadataTblHdr.u64Signature != VHDX_METADATA_TBL_HDR_SIGNATURE)
1432 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1433 "VHDX: Incorrect metadata table header signature for image \'%s\'",
1434 pImage->pszFilename);
1435 else if (MetadataTblHdr.u16EntryCount > VHDX_METADATA_TBL_HDR_ENTRY_COUNT_MAX)
1436 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1437 "VHDX: Incorrect entry count in metadata table header of image \'%s\'",
1438 pImage->pszFilename);
1439 else if (cbRegion < (MetadataTblHdr.u16EntryCount * sizeof(VhdxMetadataTblEntry) + sizeof(VhdxMetadataTblHdr)))
1440 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1441 "VHDX: Metadata table of image \'%s\' exceeds region size",
1442 pImage->pszFilename);
1443
1444 if (RT_SUCCESS(rc))
1445 {
1446 uint64_t offMetadataTblEntry = offRegion + sizeof(VhdxMetadataTblHdr);
1447
1448 for (unsigned i = 0; i < MetadataTblHdr.u16EntryCount; i++)
1449 {
1450 uint64_t offMetadataItem = 0;
1451 VHDXMETADATAITEM enmMetadataItem = VHDXMETADATAITEM_UNKNOWN;
1452 VhdxMetadataTblEntry MetadataTblEntry;
1453
1454 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, offMetadataTblEntry,
1455 &MetadataTblEntry, sizeof(MetadataTblEntry));
1456 if (RT_FAILURE(rc))
1457 {
1458 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1459 "VHDX: Reading metadata table entry from image \'%s\' failed",
1460 pImage->pszFilename);
1461 break;
1462 }
1463
1464 vhdxConvMetadataTblEntryEndianess(VHDXECONV_F2H, &MetadataTblEntry, &MetadataTblEntry);
1465
1466 /* Check whether the flags match the expectations. */
1467 for (unsigned idxProp = 0; idxProp < RT_ELEMENTS(s_aVhdxMetadataItemProps); idxProp++)
1468 {
1469 if (!RTUuidCompareStr(&MetadataTblEntry.UuidItem,
1470 s_aVhdxMetadataItemProps[idxProp].pszItemUuid))
1471 {
1472 /*
1473 * Check for specification violations and bail out, except
1474 * for the required flag of the physical sector size metadata item.
1475 * Early images had the required flag not set opposed to the specification.
1476 * We don't want to brerak those images.
1477 */
1478 if ( !!(MetadataTblEntry.u32Flags & VHDX_METADATA_TBL_ENTRY_FLAGS_IS_USER)
1479 != s_aVhdxMetadataItemProps[idxProp].fIsUser)
1480 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1481 "VHDX: User flag of metadata item does not meet expectations \'%s\'",
1482 pImage->pszFilename);
1483 else if ( !!(MetadataTblEntry.u32Flags & VHDX_METADATA_TBL_ENTRY_FLAGS_IS_VDISK)
1484 != s_aVhdxMetadataItemProps[idxProp].fIsVDisk)
1485 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1486 "VHDX: Virtual disk flag of metadata item does not meet expectations \'%s\'",
1487 pImage->pszFilename);
1488 else if ( !!(MetadataTblEntry.u32Flags & VHDX_METADATA_TBL_ENTRY_FLAGS_IS_REQUIRED)
1489 != s_aVhdxMetadataItemProps[idxProp].fIsRequired
1490 && (s_aVhdxMetadataItemProps[idxProp].enmMetadataItem != VHDXMETADATAITEM_PHYSICAL_SECTOR_SIZE))
1491 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1492 "VHDX: Required flag of metadata item does not meet expectations \'%s\'",
1493 pImage->pszFilename);
1494 else
1495 enmMetadataItem = s_aVhdxMetadataItemProps[idxProp].enmMetadataItem;
1496
1497 break;
1498 }
1499 }
1500
1501 if (RT_FAILURE(rc))
1502 break;
1503
1504 offMetadataItem = offRegion + MetadataTblEntry.u32Offset;
1505
1506 switch (enmMetadataItem)
1507 {
1508 case VHDXMETADATAITEM_FILE_PARAMS:
1509 {
1510 rc = vhdxLoadFileParametersMetadata(pImage, offMetadataItem,
1511 MetadataTblEntry.u32Length);
1512 break;
1513 }
1514 case VHDXMETADATAITEM_VDISK_SIZE:
1515 {
1516 rc = vhdxLoadVDiskSizeMetadata(pImage, offMetadataItem,
1517 MetadataTblEntry.u32Length);
1518 break;
1519 }
1520 case VHDXMETADATAITEM_PAGE83_DATA:
1521 {
1522 /*
1523 * Nothing to do here for now (marked as required but
1524 * there is no API to pass this information to the caller)
1525 * so far.
1526 */
1527 break;
1528 }
1529 case VHDXMETADATAITEM_LOGICAL_SECTOR_SIZE:
1530 {
1531 rc = vhdxLoadVDiskLogSectorSizeMetadata(pImage, offMetadataItem,
1532 MetadataTblEntry.u32Length);
1533 break;
1534 }
1535 case VHDXMETADATAITEM_PHYSICAL_SECTOR_SIZE:
1536 {
1537 /*
1538 * Nothing to do here for now (marked as required but
1539 * there is no API to pass this information to the caller)
1540 * so far.
1541 */
1542 break;
1543 }
1544 case VHDXMETADATAITEM_PARENT_LOCATOR:
1545 {
1546 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1547 "VHDX: Image \'%s\' is a differencing image which is not supported yet",
1548 pImage->pszFilename);
1549 break;
1550 }
1551 case VHDXMETADATAITEM_UNKNOWN:
1552 default:
1553 if (MetadataTblEntry.u32Flags & VHDX_METADATA_TBL_ENTRY_FLAGS_IS_REQUIRED)
1554 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1555 "VHDX: Unsupported but required metadata item in image \'%s\'",
1556 pImage->pszFilename);
1557 }
1558
1559 if (RT_FAILURE(rc))
1560 break;
1561
1562 offMetadataTblEntry += sizeof(MetadataTblEntry);
1563 }
1564 }
1565 }
1566 else
1567 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1568 "VHDX: Reading the metadata table header for image \'%s\' failed",
1569 pImage->pszFilename);
1570
1571 LogFlowFunc(("returns rc=%Rrc\n", rc));
1572 return rc;
1573}
1574
1575/**
1576 * Loads the region table and the associated regions.
1577 *
1578 * @returns VBox status code.
1579 * @param pImage Image instance data.
1580 */
1581static int vhdxLoadRegionTable(PVHDXIMAGE pImage)
1582{
1583 uint8_t *pbRegionTbl = NULL;
1584 int rc = VINF_SUCCESS;
1585
1586 LogFlowFunc(("pImage=%#p\n", pImage));
1587
1588 /* Load the complete region table into memory. */
1589 pbRegionTbl = (uint8_t *)RTMemTmpAlloc(VHDX_REGION_TBL_SIZE_MAX);
1590 if (pbRegionTbl)
1591 {
1592 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, VHDX_REGION_TBL_HDR_OFFSET,
1593 pbRegionTbl, VHDX_REGION_TBL_SIZE_MAX);
1594 if (RT_SUCCESS(rc))
1595 {
1596 PVhdxRegionTblHdr pRegionTblHdr;
1597 VhdxRegionTblHdr RegionTblHdr;
1598 uint32_t u32ChkSum = 0;
1599
1600 /*
1601 * Copy the region table header to a dedicated structure where we can
1602 * convert it to host endianess.
1603 */
1604 memcpy(&RegionTblHdr, pbRegionTbl, sizeof(RegionTblHdr));
1605 vhdxConvRegionTblHdrEndianess(VHDXECONV_F2H, &RegionTblHdr, &RegionTblHdr);
1606
1607 /* Set checksum field to 0 during crc computation. */
1608 pRegionTblHdr = (PVhdxRegionTblHdr)pbRegionTbl;
1609 pRegionTblHdr->u32Checksum = 0;
1610
1611 /* Verify the region table integrity. */
1612 u32ChkSum = RTCrc32C(pbRegionTbl, VHDX_REGION_TBL_SIZE_MAX);
1613
1614 if (RegionTblHdr.u32Signature != VHDX_REGION_TBL_HDR_SIGNATURE)
1615 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1616 "VHDX: Invalid signature for region table header of image \'%s\'",
1617 pImage->pszFilename);
1618 else if (u32ChkSum != RegionTblHdr.u32Checksum)
1619 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1620 "VHDX: CRC32 checksum mismatch for the region table of image \'%s\' (expected %#x got %#x)",
1621 pImage->pszFilename, RegionTblHdr.u32Checksum, u32ChkSum);
1622 else if (RegionTblHdr.u32EntryCount > VHDX_REGION_TBL_HDR_ENTRY_COUNT_MAX)
1623 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1624 "VHDX: Invalid entry count field in the region table header of image \'%s\'",
1625 pImage->pszFilename);
1626
1627 if (RT_SUCCESS(rc))
1628 {
1629 /* Parse the region table entries. */
1630 PVhdxRegionTblEntry pRegTblEntry = (PVhdxRegionTblEntry)(pbRegionTbl + sizeof(VhdxRegionTblHdr));
1631 VhdxRegionTblEntry RegTblEntryBat; /* BAT region table entry. */
1632 bool fBatRegPresent = false;
1633 RT_ZERO(RegTblEntryBat); /* Maybe uninitialized, gcc. */
1634
1635 for (unsigned i = 0; i < RegionTblHdr.u32EntryCount; i++)
1636 {
1637 vhdxConvRegionTblEntryEndianess(VHDXECONV_F2H, pRegTblEntry, pRegTblEntry);
1638
1639 /* Check the uuid for known regions. */
1640 if (!RTUuidCompareStr(&pRegTblEntry->UuidObject, VHDX_REGION_TBL_ENTRY_UUID_BAT))
1641 {
1642 /*
1643 * Save the BAT region and process it later.
1644 * It may come before the metadata region but needs the block size.
1645 */
1646 if (pRegTblEntry->u32Flags & VHDX_REGION_TBL_ENTRY_FLAGS_IS_REQUIRED)
1647 {
1648 fBatRegPresent = true;
1649 RegTblEntryBat.u32Length = pRegTblEntry->u32Length;
1650 RegTblEntryBat.u64FileOffset = pRegTblEntry->u64FileOffset;
1651 }
1652 else
1653 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1654 "VHDX: BAT region not marked as required in image \'%s\'",
1655 pImage->pszFilename);
1656 }
1657 else if (!RTUuidCompareStr(&pRegTblEntry->UuidObject, VHDX_REGION_TBL_ENTRY_UUID_METADATA))
1658 {
1659 if (pRegTblEntry->u32Flags & VHDX_REGION_TBL_ENTRY_FLAGS_IS_REQUIRED)
1660 rc = vhdxLoadMetadataRegion(pImage, pRegTblEntry->u64FileOffset, pRegTblEntry->u32Length);
1661 else
1662 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1663 "VHDX: Metadata region not marked as required in image \'%s\'",
1664 pImage->pszFilename);
1665 }
1666 else if (pRegTblEntry->u32Flags & VHDX_REGION_TBL_ENTRY_FLAGS_IS_REQUIRED)
1667 {
1668 /* The region is not known but marked as required, fail to load the image. */
1669 rc = vdIfError(pImage->pIfError, VERR_NOT_SUPPORTED, RT_SRC_POS,
1670 "VHDX: Unknown required region in image \'%s\'",
1671 pImage->pszFilename);
1672 }
1673
1674 if (RT_FAILURE(rc))
1675 break;
1676
1677 pRegTblEntry++;
1678 }
1679
1680 if (fBatRegPresent)
1681 rc = vhdxLoadBatRegion(pImage, RegTblEntryBat.u64FileOffset, RegTblEntryBat.u32Length);
1682 else
1683 rc = vdIfError(pImage->pIfError, VERR_VD_GEN_INVALID_HEADER, RT_SRC_POS,
1684 "VHDX: BAT region in image \'%s\' is missing",
1685 pImage->pszFilename);
1686 }
1687 }
1688 else
1689 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1690 "VHDX: Reading the region table for image \'%s\' failed",
1691 pImage->pszFilename);
1692 }
1693 else
1694 rc = vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
1695 "VHDX: Out of memory allocating memory for the region table of image \'%s\'",
1696 pImage->pszFilename);
1697
1698 if (pbRegionTbl)
1699 RTMemTmpFree(pbRegionTbl);
1700
1701 LogFlowFunc(("returns rc=%Rrc\n", rc));
1702 return rc;
1703}
1704
1705/**
1706 * Internal: Open an image, constructing all necessary data structures.
1707 */
1708static int vhdxOpenImage(PVHDXIMAGE pImage, unsigned uOpenFlags)
1709{
1710 uint64_t cbFile = 0;
1711 VhdxFileIdentifier FileIdentifier;
1712 int rc = VINF_SUCCESS;
1713
1714 LogFlowFunc(("pImage=%#p uOpenFlags=%#x\n", pImage, uOpenFlags));
1715 pImage->uOpenFlags = uOpenFlags;
1716
1717 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
1718 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
1719 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
1720
1721 /* Refuse write access, it is not implemented so far. */
1722 if (!(uOpenFlags & VD_OPEN_FLAGS_READONLY))
1723 return VERR_NOT_SUPPORTED;
1724
1725 /*
1726 * Open the image.
1727 */
1728 rc = vdIfIoIntFileOpen(pImage->pIfIo, pImage->pszFilename,
1729 VDOpenFlagsToFileOpenFlags(uOpenFlags,
1730 false /* fCreate */),
1731 &pImage->pStorage);
1732
1733 /* Do NOT signal an appropriate error here, as the VD layer has the
1734 * choice of retrying the open if it failed. */
1735 if (RT_SUCCESS(rc))
1736 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
1737
1738 if (RT_SUCCESS(rc))
1739 {
1740 if (cbFile > sizeof(FileIdentifier))
1741 {
1742 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pImage->pStorage, VHDX_FILE_IDENTIFIER_OFFSET,
1743 &FileIdentifier, sizeof(FileIdentifier));
1744 if (RT_SUCCESS(rc))
1745 {
1746 vhdxConvFileIdentifierEndianess(VHDXECONV_F2H, &FileIdentifier,
1747 &FileIdentifier);
1748 if (FileIdentifier.u64Signature != VHDX_FILE_IDENTIFIER_SIGNATURE)
1749 rc = VERR_VD_GEN_INVALID_HEADER;
1750 else
1751 rc = vhdxFindAndLoadCurrentHeader(pImage);
1752
1753 /* Load the region table. */
1754 if (RT_SUCCESS(rc))
1755 rc = vhdxLoadRegionTable(pImage);
1756 }
1757 }
1758 else
1759 rc = VERR_VD_GEN_INVALID_HEADER;
1760 }
1761
1762 if (RT_SUCCESS(rc))
1763 {
1764 PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
1765 pImage->RegionList.fFlags = 0;
1766 pImage->RegionList.cRegions = 1;
1767
1768 pRegion->offRegion = 0; /* Disk start. */
1769 pRegion->cbBlock = pImage->cbLogicalSector;
1770 pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
1771 pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
1772 pRegion->cbData = pImage->cbLogicalSector;
1773 pRegion->cbMetadata = 0;
1774 pRegion->cRegionBlocksOrBytes = pImage->cbSize;
1775 }
1776 else
1777 vhdxFreeImage(pImage, false);
1778
1779 LogFlowFunc(("returns rc=%Rrc\n", rc));
1780 return rc;
1781}
1782
1783
1784/** @copydoc VDIMAGEBACKEND::pfnProbe */
1785static DECLCALLBACK(int) vhdxProbe(const char *pszFilename, PVDINTERFACE pVDIfsDisk,
1786 PVDINTERFACE pVDIfsImage, VDTYPE enmDesiredType, VDTYPE *penmType)
1787{
1788 RT_NOREF(pVDIfsDisk, enmDesiredType);
1789 LogFlowFunc(("pszFilename=\"%s\" pVDIfsDisk=%#p pVDIfsImage=%#p\n", pszFilename, pVDIfsDisk, pVDIfsImage));
1790 PVDIOSTORAGE pStorage = NULL;
1791 uint64_t cbFile;
1792 int rc = VINF_SUCCESS;
1793 VhdxFileIdentifier FileIdentifier;
1794
1795 PVDINTERFACEIOINT pIfIo = VDIfIoIntGet(pVDIfsImage);
1796 AssertPtrReturn(pIfIo, VERR_INVALID_PARAMETER);
1797
1798 if ( !RT_VALID_PTR(pszFilename)
1799 || !*pszFilename)
1800 rc = VERR_INVALID_PARAMETER;
1801 else
1802 {
1803 /*
1804 * Open the file and read the file identifier.
1805 */
1806 rc = vdIfIoIntFileOpen(pIfIo, pszFilename,
1807 VDOpenFlagsToFileOpenFlags(VD_OPEN_FLAGS_READONLY,
1808 false /* fCreate */),
1809 &pStorage);
1810 if (RT_SUCCESS(rc))
1811 {
1812 rc = vdIfIoIntFileGetSize(pIfIo, pStorage, &cbFile);
1813 if (RT_SUCCESS(rc))
1814 {
1815 if (cbFile > sizeof(FileIdentifier))
1816 {
1817 rc = vdIfIoIntFileReadSync(pIfIo, pStorage, VHDX_FILE_IDENTIFIER_OFFSET,
1818 &FileIdentifier, sizeof(FileIdentifier));
1819 if (RT_SUCCESS(rc))
1820 {
1821 vhdxConvFileIdentifierEndianess(VHDXECONV_F2H, &FileIdentifier,
1822 &FileIdentifier);
1823 if (FileIdentifier.u64Signature != VHDX_FILE_IDENTIFIER_SIGNATURE)
1824 rc = VERR_VD_GEN_INVALID_HEADER;
1825 else
1826 *penmType = VDTYPE_HDD;
1827 }
1828 }
1829 else
1830 rc = VERR_VD_GEN_INVALID_HEADER;
1831 }
1832 }
1833
1834 if (pStorage)
1835 vdIfIoIntFileClose(pIfIo, pStorage);
1836 }
1837
1838 LogFlowFunc(("returns %Rrc\n", rc));
1839 return rc;
1840}
1841
1842/** @copydoc VDIMAGEBACKEND::pfnOpen */
1843static DECLCALLBACK(int) vhdxOpen(const char *pszFilename, unsigned uOpenFlags,
1844 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1845 VDTYPE enmType, void **ppBackendData)
1846{
1847 LogFlowFunc(("pszFilename=\"%s\" uOpenFlags=%#x pVDIfsDisk=%#p pVDIfsImage=%#p enmType=%u ppBackendData=%#p\n", pszFilename, uOpenFlags, pVDIfsDisk, pVDIfsImage, enmType, ppBackendData));
1848 int rc;
1849 PVHDXIMAGE pImage;
1850
1851 NOREF(enmType); /**< @todo r=klaus make use of the type info. */
1852
1853 /* Check open flags. All valid flags are supported. */
1854 if ( uOpenFlags & ~VD_OPEN_FLAGS_MASK
1855 || !RT_VALID_PTR(pszFilename)
1856 || !*pszFilename)
1857 rc = VERR_INVALID_PARAMETER;
1858 else
1859 {
1860 pImage = (PVHDXIMAGE)RTMemAllocZ(RT_UOFFSETOF(VHDXIMAGE, RegionList.aRegions[1]));
1861 if (!pImage)
1862 rc = VERR_NO_MEMORY;
1863 else
1864 {
1865 pImage->pszFilename = pszFilename;
1866 pImage->pStorage = NULL;
1867 pImage->pVDIfsDisk = pVDIfsDisk;
1868 pImage->pVDIfsImage = pVDIfsImage;
1869
1870 rc = vhdxOpenImage(pImage, uOpenFlags);
1871 if (RT_SUCCESS(rc))
1872 *ppBackendData = pImage;
1873 else
1874 RTMemFree(pImage);
1875 }
1876 }
1877
1878 LogFlowFunc(("returns %Rrc (pBackendData=%#p)\n", rc, *ppBackendData));
1879 return rc;
1880}
1881
1882/** @interface_method_impl{VDIMAGEBACKEND,pfnCreate} */
1883static DECLCALLBACK(int) vhdxCreate(const char *pszFilename, uint64_t cbSize,
1884 unsigned uImageFlags, const char *pszComment,
1885 PCVDGEOMETRY pPCHSGeometry, PCVDGEOMETRY pLCHSGeometry,
1886 PCRTUUID pUuid, unsigned uOpenFlags,
1887 unsigned uPercentStart, unsigned uPercentSpan,
1888 PVDINTERFACE pVDIfsDisk, PVDINTERFACE pVDIfsImage,
1889 PVDINTERFACE pVDIfsOperation, VDTYPE enmType,
1890 void **ppBackendData)
1891{
1892 RT_NOREF8(pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags);
1893 RT_NOREF7(uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData);
1894 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",
1895 pszFilename, cbSize, uImageFlags, pszComment, pPCHSGeometry, pLCHSGeometry, pUuid, uOpenFlags, uPercentStart, uPercentSpan, pVDIfsDisk, pVDIfsImage, pVDIfsOperation, enmType, ppBackendData));
1896 int rc = VERR_NOT_SUPPORTED;
1897
1898 LogFlowFunc(("returns %Rrc\n", rc));
1899 return rc;
1900}
1901
1902/** @copydoc VDIMAGEBACKEND::pfnRename */
1903static DECLCALLBACK(int) vhdxRename(void *pBackendData, const char *pszFilename)
1904{
1905 LogFlowFunc(("pBackendData=%#p pszFilename=%#p\n", pBackendData, pszFilename));
1906 int rc = VINF_SUCCESS;
1907 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
1908
1909 /* Check arguments. */
1910 if ( !pImage
1911 || !pszFilename
1912 || !*pszFilename)
1913 rc = VERR_INVALID_PARAMETER;
1914 else
1915 {
1916 /* Close the image. */
1917 rc = vhdxFreeImage(pImage, false);
1918 if (RT_SUCCESS(rc))
1919 {
1920 /* Rename the file. */
1921 rc = vdIfIoIntFileMove(pImage->pIfIo, pImage->pszFilename, pszFilename, 0);
1922 if (RT_FAILURE(rc))
1923 {
1924 /* The move failed, try to reopen the original image. */
1925 int rc2 = vhdxOpenImage(pImage, pImage->uOpenFlags);
1926 if (RT_FAILURE(rc2))
1927 rc = rc2;
1928 }
1929 else
1930 {
1931 /* Update pImage with the new information. */
1932 pImage->pszFilename = pszFilename;
1933
1934 /* Open the old image with new name. */
1935 rc = vhdxOpenImage(pImage, pImage->uOpenFlags);
1936 }
1937 }
1938 }
1939
1940 LogFlowFunc(("returns %Rrc\n", rc));
1941 return rc;
1942}
1943
1944/** @copydoc VDIMAGEBACKEND::pfnClose */
1945static DECLCALLBACK(int) vhdxClose(void *pBackendData, bool fDelete)
1946{
1947 LogFlowFunc(("pBackendData=%#p fDelete=%d\n", pBackendData, fDelete));
1948 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
1949 int rc;
1950
1951 rc = vhdxFreeImage(pImage, fDelete);
1952 RTMemFree(pImage);
1953
1954 LogFlowFunc(("returns %Rrc\n", rc));
1955 return rc;
1956}
1957
1958/** @copydoc VDIMAGEBACKEND::pfnRead */
1959static DECLCALLBACK(int) vhdxRead(void *pBackendData, uint64_t uOffset, size_t cbToRead,
1960 PVDIOCTX pIoCtx, size_t *pcbActuallyRead)
1961{
1962 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToRead=%zu pcbActuallyRead=%#p\n",
1963 pBackendData, uOffset, pIoCtx, cbToRead, pcbActuallyRead));
1964 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
1965 int rc = VINF_SUCCESS;
1966
1967 AssertPtr(pImage);
1968 Assert(uOffset % 512 == 0);
1969 Assert(cbToRead % 512 == 0);
1970
1971 if ( uOffset + cbToRead > pImage->cbSize
1972 || cbToRead == 0)
1973 rc = VERR_INVALID_PARAMETER;
1974 else
1975 {
1976 uint32_t idxBat = (uint32_t)(uOffset / pImage->cbBlock); Assert(idxBat == uOffset / pImage->cbBlock);
1977 uint32_t offRead = uOffset % pImage->cbBlock;
1978 uint64_t uBatEntry;
1979
1980 idxBat += idxBat / pImage->uChunkRatio; /* Add interleaving sector bitmap entries. */
1981 uBatEntry = pImage->paBat[idxBat].u64BatEntry;
1982
1983 cbToRead = RT_MIN(cbToRead, pImage->cbBlock - offRead);
1984
1985 switch (VHDX_BAT_ENTRY_GET_STATE(uBatEntry))
1986 {
1987 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_NOT_PRESENT:
1988 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_UNDEFINED:
1989 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_ZERO:
1990 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_UNMAPPED:
1991 {
1992 vdIfIoIntIoCtxSet(pImage->pIfIo, pIoCtx, 0, cbToRead);
1993 break;
1994 }
1995 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_FULLY_PRESENT:
1996 {
1997 uint64_t offFile = VHDX_BAT_ENTRY_GET_FILE_OFFSET(uBatEntry) + offRead;
1998 rc = vdIfIoIntFileReadUser(pImage->pIfIo, pImage->pStorage, offFile,
1999 pIoCtx, cbToRead);
2000 break;
2001 }
2002 case VHDX_BAT_ENTRY_PAYLOAD_BLOCK_PARTIALLY_PRESENT:
2003 default:
2004 rc = VERR_INVALID_PARAMETER;
2005 break;
2006 }
2007
2008 if (pcbActuallyRead)
2009 *pcbActuallyRead = cbToRead;
2010 }
2011
2012 LogFlowFunc(("returns %Rrc\n", rc));
2013 return rc;
2014}
2015
2016/** @copydoc VDIMAGEBACKEND::pfnWrite */
2017static DECLCALLBACK(int) vhdxWrite(void *pBackendData, uint64_t uOffset, size_t cbToWrite,
2018 PVDIOCTX pIoCtx, size_t *pcbWriteProcess, size_t *pcbPreRead,
2019 size_t *pcbPostRead, unsigned fWrite)
2020{
2021 RT_NOREF5(pIoCtx, pcbWriteProcess, pcbPreRead, pcbPostRead, fWrite);
2022 LogFlowFunc(("pBackendData=%#p uOffset=%llu pIoCtx=%#p cbToWrite=%zu pcbWriteProcess=%#p pcbPreRead=%#p pcbPostRead=%#p\n",
2023 pBackendData, uOffset, pIoCtx, cbToWrite, pcbWriteProcess, pcbPreRead, pcbPostRead));
2024 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2025 int rc;
2026
2027 AssertPtr(pImage);
2028 Assert(uOffset % 512 == 0);
2029 Assert(cbToWrite % 512 == 0);
2030
2031 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2032 rc = VERR_VD_IMAGE_READ_ONLY;
2033 else if ( uOffset + cbToWrite > pImage->cbSize
2034 || cbToWrite == 0)
2035 rc = VERR_INVALID_PARAMETER;
2036 else
2037 rc = VERR_NOT_SUPPORTED;
2038
2039 LogFlowFunc(("returns %Rrc\n", rc));
2040 return rc;
2041}
2042
2043/** @copydoc VDIMAGEBACKEND::pfnFlush */
2044static DECLCALLBACK(int) vhdxFlush(void *pBackendData, PVDIOCTX pIoCtx)
2045{
2046 RT_NOREF1(pIoCtx);
2047 LogFlowFunc(("pBackendData=%#p pIoCtx=%#p\n", pBackendData, pIoCtx));
2048 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2049 int rc;
2050
2051 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2052 rc = VERR_VD_IMAGE_READ_ONLY;
2053 else
2054 rc = VERR_NOT_SUPPORTED;
2055
2056 LogFlowFunc(("returns %Rrc\n", rc));
2057 return rc;
2058}
2059
2060/** @copydoc VDIMAGEBACKEND::pfnGetVersion */
2061static DECLCALLBACK(unsigned) vhdxGetVersion(void *pBackendData)
2062{
2063 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2064 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2065
2066 AssertPtr(pImage);
2067
2068 if (pImage)
2069 return pImage->uVersion;
2070 else
2071 return 0;
2072}
2073
2074/** @copydoc VDIMAGEBACKEND::pfnGetFileSize */
2075static DECLCALLBACK(uint64_t) vhdxGetFileSize(void *pBackendData)
2076{
2077 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2078 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2079 uint64_t cb = 0;
2080
2081 AssertPtr(pImage);
2082
2083 if (pImage)
2084 {
2085 uint64_t cbFile;
2086 if (pImage->pStorage)
2087 {
2088 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pImage->pStorage, &cbFile);
2089 if (RT_SUCCESS(rc))
2090 cb = cbFile;
2091 }
2092 }
2093
2094 LogFlowFunc(("returns %lld\n", cb));
2095 return cb;
2096}
2097
2098/** @copydoc VDIMAGEBACKEND::pfnGetPCHSGeometry */
2099static DECLCALLBACK(int) vhdxGetPCHSGeometry(void *pBackendData,
2100 PVDGEOMETRY pPCHSGeometry)
2101{
2102 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p\n", pBackendData, pPCHSGeometry));
2103 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2104 int rc;
2105
2106 AssertPtr(pImage);
2107
2108 if (pImage)
2109 {
2110 if (pImage->PCHSGeometry.cCylinders)
2111 {
2112 *pPCHSGeometry = pImage->PCHSGeometry;
2113 rc = VINF_SUCCESS;
2114 }
2115 else
2116 rc = VERR_VD_GEOMETRY_NOT_SET;
2117 }
2118 else
2119 rc = VERR_VD_NOT_OPENED;
2120
2121 LogFlowFunc(("returns %Rrc (PCHS=%u/%u/%u)\n", rc, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2122 return rc;
2123}
2124
2125/** @copydoc VDIMAGEBACKEND::pfnSetPCHSGeometry */
2126static DECLCALLBACK(int) vhdxSetPCHSGeometry(void *pBackendData,
2127 PCVDGEOMETRY pPCHSGeometry)
2128{
2129 LogFlowFunc(("pBackendData=%#p pPCHSGeometry=%#p PCHS=%u/%u/%u\n", pBackendData, pPCHSGeometry, pPCHSGeometry->cCylinders, pPCHSGeometry->cHeads, pPCHSGeometry->cSectors));
2130 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2131 int rc = VINF_SUCCESS;
2132
2133 AssertPtr(pImage);
2134
2135 if (pImage)
2136 {
2137 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2138 rc = VERR_VD_IMAGE_READ_ONLY;
2139 else
2140 pImage->PCHSGeometry = *pPCHSGeometry;
2141 }
2142 else
2143 rc = VERR_VD_NOT_OPENED;
2144
2145 LogFlowFunc(("returns %Rrc\n", rc));
2146 return rc;
2147}
2148
2149/** @copydoc VDIMAGEBACKEND::pfnGetLCHSGeometry */
2150static DECLCALLBACK(int) vhdxGetLCHSGeometry(void *pBackendData,
2151 PVDGEOMETRY pLCHSGeometry)
2152{
2153 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p\n", pBackendData, pLCHSGeometry));
2154 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2155 int rc = VINF_SUCCESS;
2156
2157 AssertPtr(pImage);
2158
2159 if (pImage)
2160 {
2161 if (pImage->LCHSGeometry.cCylinders)
2162 *pLCHSGeometry = pImage->LCHSGeometry;
2163 else
2164 rc = VERR_VD_GEOMETRY_NOT_SET;
2165 }
2166 else
2167 rc = VERR_VD_NOT_OPENED;
2168
2169 LogFlowFunc(("returns %Rrc (LCHS=%u/%u/%u)\n", rc, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2170 return rc;
2171}
2172
2173/** @copydoc VDIMAGEBACKEND::pfnSetLCHSGeometry */
2174static DECLCALLBACK(int) vhdxSetLCHSGeometry(void *pBackendData,
2175 PCVDGEOMETRY pLCHSGeometry)
2176{
2177 LogFlowFunc(("pBackendData=%#p pLCHSGeometry=%#p LCHS=%u/%u/%u\n", pBackendData, pLCHSGeometry, pLCHSGeometry->cCylinders, pLCHSGeometry->cHeads, pLCHSGeometry->cSectors));
2178 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2179 int rc = VINF_SUCCESS;
2180
2181 AssertPtr(pImage);
2182
2183 if (pImage)
2184 {
2185 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2186 rc = VERR_VD_IMAGE_READ_ONLY;
2187 else
2188 pImage->LCHSGeometry = *pLCHSGeometry;
2189 }
2190 else
2191 rc = VERR_VD_NOT_OPENED;
2192
2193 LogFlowFunc(("returns %Rrc\n", rc));
2194 return rc;
2195}
2196
2197/** @copydoc VDIMAGEBACKEND::pfnQueryRegions */
2198static DECLCALLBACK(int) vhdxQueryRegions(void *pBackendData, PCVDREGIONLIST *ppRegionList)
2199{
2200 LogFlowFunc(("pBackendData=%#p ppRegionList=%#p\n", pBackendData, ppRegionList));
2201 PVHDXIMAGE pThis = (PVHDXIMAGE)pBackendData;
2202
2203 AssertPtrReturn(pThis, VERR_VD_NOT_OPENED);
2204
2205 *ppRegionList = &pThis->RegionList;
2206 LogFlowFunc(("returns %Rrc\n", VINF_SUCCESS));
2207 return VINF_SUCCESS;
2208}
2209
2210/** @copydoc VDIMAGEBACKEND::pfnRegionListRelease */
2211static DECLCALLBACK(void) vhdxRegionListRelease(void *pBackendData, PCVDREGIONLIST pRegionList)
2212{
2213 RT_NOREF1(pRegionList);
2214 LogFlowFunc(("pBackendData=%#p pRegionList=%#p\n", pBackendData, pRegionList));
2215 PVHDXIMAGE pThis = (PVHDXIMAGE)pBackendData;
2216 AssertPtr(pThis); RT_NOREF(pThis);
2217
2218 /* Nothing to do here. */
2219}
2220
2221/** @copydoc VDIMAGEBACKEND::pfnGetImageFlags */
2222static DECLCALLBACK(unsigned) vhdxGetImageFlags(void *pBackendData)
2223{
2224 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2225 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2226 unsigned uImageFlags;
2227
2228 AssertPtr(pImage);
2229
2230 if (pImage)
2231 uImageFlags = pImage->uImageFlags;
2232 else
2233 uImageFlags = 0;
2234
2235 LogFlowFunc(("returns %#x\n", uImageFlags));
2236 return uImageFlags;
2237}
2238
2239/** @copydoc VDIMAGEBACKEND::pfnGetOpenFlags */
2240static DECLCALLBACK(unsigned) vhdxGetOpenFlags(void *pBackendData)
2241{
2242 LogFlowFunc(("pBackendData=%#p\n", pBackendData));
2243 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2244 unsigned uOpenFlags;
2245
2246 AssertPtr(pImage);
2247
2248 if (pImage)
2249 uOpenFlags = pImage->uOpenFlags;
2250 else
2251 uOpenFlags = 0;
2252
2253 LogFlowFunc(("returns %#x\n", uOpenFlags));
2254 return uOpenFlags;
2255}
2256
2257/** @copydoc VDIMAGEBACKEND::pfnSetOpenFlags */
2258static DECLCALLBACK(int) vhdxSetOpenFlags(void *pBackendData, unsigned uOpenFlags)
2259{
2260 LogFlowFunc(("pBackendData=%#p\n uOpenFlags=%#x", pBackendData, uOpenFlags));
2261 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2262 int rc = VINF_SUCCESS;
2263
2264 /* Image must be opened and the new flags must be valid. */
2265 if (!pImage || (uOpenFlags & ~(VD_OPEN_FLAGS_READONLY | VD_OPEN_FLAGS_INFO | VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS)))
2266 rc = VERR_INVALID_PARAMETER;
2267 else
2268 {
2269 /* Implement this operation via reopening the image. */
2270 rc = vhdxFreeImage(pImage, false);
2271 if (RT_SUCCESS(rc))
2272 rc = vhdxOpenImage(pImage, uOpenFlags);
2273 }
2274
2275 LogFlowFunc(("returns %Rrc\n", rc));
2276 return rc;
2277}
2278
2279/** @copydoc VDIMAGEBACKEND::pfnGetComment */
2280VD_BACKEND_CALLBACK_GET_COMMENT_DEF_NOT_SUPPORTED(vhdxGetComment);
2281
2282/** @copydoc VDIMAGEBACKEND::pfnSetComment */
2283VD_BACKEND_CALLBACK_SET_COMMENT_DEF_NOT_SUPPORTED(vhdxSetComment, PVHDXIMAGE);
2284
2285/** @copydoc VDIMAGEBACKEND::pfnGetUuid */
2286VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(vhdxGetUuid);
2287
2288/** @copydoc VDIMAGEBACKEND::pfnSetUuid */
2289VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(vhdxSetUuid, PVHDXIMAGE);
2290
2291/** @copydoc VDIMAGEBACKEND::pfnGetModificationUuid */
2292VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(vhdxGetModificationUuid);
2293
2294/** @copydoc VDIMAGEBACKEND::pfnSetModificationUuid */
2295VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(vhdxSetModificationUuid, PVHDXIMAGE);
2296
2297/** @copydoc VDIMAGEBACKEND::pfnGetParentUuid */
2298VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(vhdxGetParentUuid);
2299
2300/** @copydoc VDIMAGEBACKEND::pfnSetParentUuid */
2301VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(vhdxSetParentUuid, PVHDXIMAGE);
2302
2303/** @copydoc VDIMAGEBACKEND::pfnGetParentModificationUuid */
2304VD_BACKEND_CALLBACK_GET_UUID_DEF_NOT_SUPPORTED(vhdxGetParentModificationUuid);
2305
2306/** @copydoc VDIMAGEBACKEND::pfnSetParentModificationUuid */
2307VD_BACKEND_CALLBACK_SET_UUID_DEF_NOT_SUPPORTED(vhdxSetParentModificationUuid, PVHDXIMAGE);
2308
2309/** @copydoc VDIMAGEBACKEND::pfnDump */
2310static DECLCALLBACK(void) vhdxDump(void *pBackendData)
2311{
2312 PVHDXIMAGE pImage = (PVHDXIMAGE)pBackendData;
2313
2314 AssertPtr(pImage);
2315 if (pImage)
2316 {
2317 vdIfErrorMessage(pImage->pIfError, "Header: Geometry PCHS=%u/%u/%u LCHS=%u/%u/%u cbSector=%u\n",
2318 pImage->PCHSGeometry.cCylinders, pImage->PCHSGeometry.cHeads, pImage->PCHSGeometry.cSectors,
2319 pImage->LCHSGeometry.cCylinders, pImage->LCHSGeometry.cHeads, pImage->LCHSGeometry.cSectors,
2320 pImage->cbLogicalSector);
2321 }
2322}
2323
2324
2325const VDIMAGEBACKEND g_VhdxBackend =
2326{
2327 /* u32Version */
2328 VD_IMGBACKEND_VERSION,
2329 /* pszBackendName */
2330 "VHDX",
2331 /* uBackendCaps */
2332 VD_CAP_FILE | VD_CAP_VFS,
2333 /* paFileExtensions */
2334 s_aVhdxFileExtensions,
2335 /* paConfigInfo */
2336 NULL,
2337 /* pfnProbe */
2338 vhdxProbe,
2339 /* pfnOpen */
2340 vhdxOpen,
2341 /* pfnCreate */
2342 vhdxCreate,
2343 /* pfnRename */
2344 vhdxRename,
2345 /* pfnClose */
2346 vhdxClose,
2347 /* pfnRead */
2348 vhdxRead,
2349 /* pfnWrite */
2350 vhdxWrite,
2351 /* pfnFlush */
2352 vhdxFlush,
2353 /* pfnDiscard */
2354 NULL,
2355 /* pfnGetVersion */
2356 vhdxGetVersion,
2357 /* pfnGetFileSize */
2358 vhdxGetFileSize,
2359 /* pfnGetPCHSGeometry */
2360 vhdxGetPCHSGeometry,
2361 /* pfnSetPCHSGeometry */
2362 vhdxSetPCHSGeometry,
2363 /* pfnGetLCHSGeometry */
2364 vhdxGetLCHSGeometry,
2365 /* pfnSetLCHSGeometry */
2366 vhdxSetLCHSGeometry,
2367 /* pfnQueryRegions */
2368 vhdxQueryRegions,
2369 /* pfnRegionListRelease */
2370 vhdxRegionListRelease,
2371 /* pfnGetImageFlags */
2372 vhdxGetImageFlags,
2373 /* pfnGetOpenFlags */
2374 vhdxGetOpenFlags,
2375 /* pfnSetOpenFlags */
2376 vhdxSetOpenFlags,
2377 /* pfnGetComment */
2378 vhdxGetComment,
2379 /* pfnSetComment */
2380 vhdxSetComment,
2381 /* pfnGetUuid */
2382 vhdxGetUuid,
2383 /* pfnSetUuid */
2384 vhdxSetUuid,
2385 /* pfnGetModificationUuid */
2386 vhdxGetModificationUuid,
2387 /* pfnSetModificationUuid */
2388 vhdxSetModificationUuid,
2389 /* pfnGetParentUuid */
2390 vhdxGetParentUuid,
2391 /* pfnSetParentUuid */
2392 vhdxSetParentUuid,
2393 /* pfnGetParentModificationUuid */
2394 vhdxGetParentModificationUuid,
2395 /* pfnSetParentModificationUuid */
2396 vhdxSetParentModificationUuid,
2397 /* pfnDump */
2398 vhdxDump,
2399 /* pfnGetTimestamp */
2400 NULL,
2401 /* pfnGetParentTimestamp */
2402 NULL,
2403 /* pfnSetParentTimestamp */
2404 NULL,
2405 /* pfnGetParentFilename */
2406 NULL,
2407 /* pfnSetParentFilename */
2408 NULL,
2409 /* pfnComposeLocation */
2410 genericFileComposeLocation,
2411 /* pfnComposeName */
2412 genericFileComposeName,
2413 /* pfnCompact */
2414 NULL,
2415 /* pfnResize */
2416 NULL,
2417 /* pfnRepair */
2418 NULL,
2419 /* pfnTraverseMetadata */
2420 NULL,
2421 /* u32VersionEnd */
2422 VD_IMGBACKEND_VERSION
2423};
Note: See TracBrowser for help on using the repository browser.

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