VirtualBox

source: vbox/trunk/src/VBox/Storage/VMDK.cpp@ 102493

Last change on this file since 102493 was 99963, checked in by vboxsync, 18 months ago

VBox/types.h,pdmnetifs.h: Replaced PDMDATASEG with RTSGSEG since the two structures are identical. This should make it easy to create a RTSGBUF for a PDMSCATTERGATHER buffer (just limit the operations to cbUsed bytes).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 379.2 KB
Line 
1/* $Id: VMDK.cpp 99963 2023-05-24 23:27:54Z vboxsync $ */
2/** @file
3 * VMDK disk image, core code.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_VD_VMDK
33#include <VBox/log.h> /* before VBox/vd-ifs.h */
34#include <VBox/vd-plugin.h>
35#include <VBox/err.h>
36
37#include <iprt/assert.h>
38#include <iprt/alloc.h>
39#include <iprt/base64.h>
40#include <iprt/ctype.h>
41#include <iprt/crc.h>
42#include <iprt/dvm.h>
43#include <iprt/uuid.h>
44#include <iprt/path.h>
45#include <iprt/rand.h>
46#include <iprt/sg.h>
47#include <iprt/sort.h>
48#include <iprt/string.h>
49#include <iprt/zip.h>
50#include <iprt/asm.h>
51#ifdef RT_OS_WINDOWS
52# include <iprt/utf16.h>
53# include <iprt/uni.h>
54# include <iprt/uni.h>
55# include <iprt/nt/nt-and-windows.h>
56# include <winioctl.h>
57#endif
58#ifdef RT_OS_LINUX
59# include <errno.h>
60# include <sys/stat.h>
61# include <iprt/dir.h>
62# include <iprt/symlink.h>
63# include <iprt/linux/sysfs.h>
64#endif
65#ifdef RT_OS_FREEBSD
66#include <libgeom.h>
67#include <sys/stat.h>
68#include <stdlib.h>
69#endif
70#ifdef RT_OS_SOLARIS
71#include <sys/dkio.h>
72#include <sys/vtoc.h>
73#include <sys/efi_partition.h>
74#include <unistd.h>
75#include <errno.h>
76#endif
77#ifdef RT_OS_DARWIN
78# include <sys/stat.h>
79# include <sys/disk.h>
80# include <errno.h>
81/* The following structure and IOCTLs are defined in znu bsd/sys/disk.h but
82 inside KERNEL ifdefs and thus stripped from the SDK edition of the header.
83 While we could try include the header from the Kernel.framework, it's a lot
84 easier to just add the structure and 4 defines here. */
85typedef struct
86{
87 uint64_t offset;
88 uint64_t length;
89 uint8_t reserved0128[12];
90 dev_t dev;
91} dk_physical_extent_t;
92# define DKIOCGETBASE _IOR( 'd', 73, uint64_t)
93# define DKIOCLOCKPHYSICALEXTENTS _IO( 'd', 81)
94# define DKIOCGETPHYSICALEXTENT _IOWR('d', 82, dk_physical_extent_t)
95# define DKIOCUNLOCKPHYSICALEXTENTS _IO( 'd', 83)
96#endif /* RT_OS_DARWIN */
97
98#include "VDBackends.h"
99
100
101/*********************************************************************************************************************************
102* Constants And Macros, Structures and Typedefs *
103*********************************************************************************************************************************/
104
105/** Maximum encoded string size (including NUL) we allow for VMDK images.
106 * Deliberately not set high to avoid running out of descriptor space. */
107#define VMDK_ENCODED_COMMENT_MAX 1024
108
109/** VMDK descriptor DDB entry for PCHS cylinders. */
110#define VMDK_DDB_GEO_PCHS_CYLINDERS "ddb.geometry.cylinders"
111
112/** VMDK descriptor DDB entry for PCHS heads. */
113#define VMDK_DDB_GEO_PCHS_HEADS "ddb.geometry.heads"
114
115/** VMDK descriptor DDB entry for PCHS sectors. */
116#define VMDK_DDB_GEO_PCHS_SECTORS "ddb.geometry.sectors"
117
118/** VMDK descriptor DDB entry for LCHS cylinders. */
119#define VMDK_DDB_GEO_LCHS_CYLINDERS "ddb.geometry.biosCylinders"
120
121/** VMDK descriptor DDB entry for LCHS heads. */
122#define VMDK_DDB_GEO_LCHS_HEADS "ddb.geometry.biosHeads"
123
124/** VMDK descriptor DDB entry for LCHS sectors. */
125#define VMDK_DDB_GEO_LCHS_SECTORS "ddb.geometry.biosSectors"
126
127/** VMDK descriptor DDB entry for image UUID. */
128#define VMDK_DDB_IMAGE_UUID "ddb.uuid.image"
129
130/** VMDK descriptor DDB entry for image modification UUID. */
131#define VMDK_DDB_MODIFICATION_UUID "ddb.uuid.modification"
132
133/** VMDK descriptor DDB entry for parent image UUID. */
134#define VMDK_DDB_PARENT_UUID "ddb.uuid.parent"
135
136/** VMDK descriptor DDB entry for parent image modification UUID. */
137#define VMDK_DDB_PARENT_MODIFICATION_UUID "ddb.uuid.parentmodification"
138
139/** No compression for streamOptimized files. */
140#define VMDK_COMPRESSION_NONE 0
141
142/** Deflate compression for streamOptimized files. */
143#define VMDK_COMPRESSION_DEFLATE 1
144
145/** Marker that the actual GD value is stored in the footer. */
146#define VMDK_GD_AT_END 0xffffffffffffffffULL
147
148/** Marker for end-of-stream in streamOptimized images. */
149#define VMDK_MARKER_EOS 0
150
151/** Marker for grain table block in streamOptimized images. */
152#define VMDK_MARKER_GT 1
153
154/** Marker for grain directory block in streamOptimized images. */
155#define VMDK_MARKER_GD 2
156
157/** Marker for footer in streamOptimized images. */
158#define VMDK_MARKER_FOOTER 3
159
160/** Marker for unknown purpose in streamOptimized images.
161 * Shows up in very recent images created by vSphere, but only sporadically.
162 * They "forgot" to document that one in the VMDK specification. */
163#define VMDK_MARKER_UNSPECIFIED 4
164
165/** Dummy marker for "don't check the marker value". */
166#define VMDK_MARKER_IGNORE 0xffffffffU
167
168/**
169 * Magic number for hosted images created by VMware Workstation 4, VMware
170 * Workstation 5, VMware Server or VMware Player. Not necessarily sparse.
171 */
172#define VMDK_SPARSE_MAGICNUMBER 0x564d444b /* 'V' 'M' 'D' 'K' */
173
174/** VMDK sector size in bytes. */
175#define VMDK_SECTOR_SIZE 512
176/** Max string buffer size for uint64_t with null term */
177#define UINT64_MAX_BUFF_SIZE 21
178/** Grain directory entry size in bytes */
179#define VMDK_GRAIN_DIR_ENTRY_SIZE 4
180/** Grain table size in bytes */
181#define VMDK_GRAIN_TABLE_SIZE 2048
182
183/**
184 * VMDK hosted binary extent header. The "Sparse" is a total misnomer, as
185 * this header is also used for monolithic flat images.
186 */
187#pragma pack(1)
188typedef struct SparseExtentHeader
189{
190 uint32_t magicNumber;
191 uint32_t version;
192 uint32_t flags;
193 uint64_t capacity;
194 uint64_t grainSize;
195 uint64_t descriptorOffset;
196 uint64_t descriptorSize;
197 uint32_t numGTEsPerGT;
198 uint64_t rgdOffset;
199 uint64_t gdOffset;
200 uint64_t overHead;
201 bool uncleanShutdown;
202 char singleEndLineChar;
203 char nonEndLineChar;
204 char doubleEndLineChar1;
205 char doubleEndLineChar2;
206 uint16_t compressAlgorithm;
207 uint8_t pad[433];
208} SparseExtentHeader;
209#pragma pack()
210
211/** The maximum allowed descriptor size in the extent header in sectors. */
212#define VMDK_SPARSE_DESCRIPTOR_SIZE_MAX UINT64_C(20480) /* 10MB */
213
214/** VMDK capacity for a single chunk when 2G splitting is turned on. Should be
215 * divisible by the default grain size (64K) */
216#define VMDK_2G_SPLIT_SIZE (2047 * 1024 * 1024)
217
218/** VMDK streamOptimized file format marker. The type field may or may not
219 * be actually valid, but there's always data to read there. */
220#pragma pack(1)
221typedef struct VMDKMARKER
222{
223 uint64_t uSector;
224 uint32_t cbSize;
225 uint32_t uType;
226} VMDKMARKER, *PVMDKMARKER;
227#pragma pack()
228
229
230/** Convert sector number/size to byte offset/size. */
231#define VMDK_SECTOR2BYTE(u) ((uint64_t)(u) << 9)
232
233/** Convert byte offset/size to sector number/size. */
234#define VMDK_BYTE2SECTOR(u) ((u) >> 9)
235
236/**
237 * VMDK extent type.
238 */
239typedef enum VMDKETYPE
240{
241 /** Hosted sparse extent. */
242 VMDKETYPE_HOSTED_SPARSE = 1,
243 /** Flat extent. */
244 VMDKETYPE_FLAT,
245 /** Zero extent. */
246 VMDKETYPE_ZERO,
247 /** VMFS extent, used by ESX. */
248 VMDKETYPE_VMFS
249} VMDKETYPE, *PVMDKETYPE;
250
251/**
252 * VMDK access type for a extent.
253 */
254typedef enum VMDKACCESS
255{
256 /** No access allowed. */
257 VMDKACCESS_NOACCESS = 0,
258 /** Read-only access. */
259 VMDKACCESS_READONLY,
260 /** Read-write access. */
261 VMDKACCESS_READWRITE
262} VMDKACCESS, *PVMDKACCESS;
263
264/** Forward declaration for PVMDKIMAGE. */
265typedef struct VMDKIMAGE *PVMDKIMAGE;
266
267/**
268 * Extents files entry. Used for opening a particular file only once.
269 */
270typedef struct VMDKFILE
271{
272 /** Pointer to file path. Local copy. */
273 const char *pszFilename;
274 /** Pointer to base name. Local copy. */
275 const char *pszBasename;
276 /** File open flags for consistency checking. */
277 unsigned fOpen;
278 /** Handle for sync/async file abstraction.*/
279 PVDIOSTORAGE pStorage;
280 /** Reference counter. */
281 unsigned uReferences;
282 /** Flag whether the file should be deleted on last close. */
283 bool fDelete;
284 /** Pointer to the image we belong to (for debugging purposes). */
285 PVMDKIMAGE pImage;
286 /** Pointer to next file descriptor. */
287 struct VMDKFILE *pNext;
288 /** Pointer to the previous file descriptor. */
289 struct VMDKFILE *pPrev;
290} VMDKFILE, *PVMDKFILE;
291
292/**
293 * VMDK extent data structure.
294 */
295typedef struct VMDKEXTENT
296{
297 /** File handle. */
298 PVMDKFILE pFile;
299 /** Base name of the image extent. */
300 const char *pszBasename;
301 /** Full name of the image extent. */
302 const char *pszFullname;
303 /** Number of sectors in this extent. */
304 uint64_t cSectors;
305 /** Number of sectors per block (grain in VMDK speak). */
306 uint64_t cSectorsPerGrain;
307 /** Starting sector number of descriptor. */
308 uint64_t uDescriptorSector;
309 /** Size of descriptor in sectors. */
310 uint64_t cDescriptorSectors;
311 /** Starting sector number of grain directory. */
312 uint64_t uSectorGD;
313 /** Starting sector number of redundant grain directory. */
314 uint64_t uSectorRGD;
315 /** Total number of metadata sectors. */
316 uint64_t cOverheadSectors;
317 /** Nominal size (i.e. as described by the descriptor) of this extent. */
318 uint64_t cNominalSectors;
319 /** Sector offset (i.e. as described by the descriptor) of this extent. */
320 uint64_t uSectorOffset;
321 /** Number of entries in a grain table. */
322 uint32_t cGTEntries;
323 /** Number of sectors reachable via a grain directory entry. */
324 uint32_t cSectorsPerGDE;
325 /** Number of entries in the grain directory. */
326 uint32_t cGDEntries;
327 /** Pointer to the next free sector. Legacy information. Do not use. */
328 uint32_t uFreeSector;
329 /** Number of this extent in the list of images. */
330 uint32_t uExtent;
331 /** Pointer to the descriptor (NULL if no descriptor in this extent). */
332 char *pDescData;
333 /** Pointer to the grain directory. */
334 uint32_t *pGD;
335 /** Pointer to the redundant grain directory. */
336 uint32_t *pRGD;
337 /** VMDK version of this extent. 1=1.0/1.1 */
338 uint32_t uVersion;
339 /** Type of this extent. */
340 VMDKETYPE enmType;
341 /** Access to this extent. */
342 VMDKACCESS enmAccess;
343 /** Flag whether this extent is marked as unclean. */
344 bool fUncleanShutdown;
345 /** Flag whether the metadata in the extent header needs to be updated. */
346 bool fMetaDirty;
347 /** Flag whether there is a footer in this extent. */
348 bool fFooter;
349 /** Compression type for this extent. */
350 uint16_t uCompression;
351 /** Append position for writing new grain. Only for sparse extents. */
352 uint64_t uAppendPosition;
353 /** Last grain which was accessed. Only for streamOptimized extents. */
354 uint32_t uLastGrainAccess;
355 /** Starting sector corresponding to the grain buffer. */
356 uint32_t uGrainSectorAbs;
357 /** Grain number corresponding to the grain buffer. */
358 uint32_t uGrain;
359 /** Actual size of the compressed data, only valid for reading. */
360 uint32_t cbGrainStreamRead;
361 /** Size of compressed grain buffer for streamOptimized extents. */
362 size_t cbCompGrain;
363 /** Compressed grain buffer for streamOptimized extents, with marker. */
364 void *pvCompGrain;
365 /** Decompressed grain buffer for streamOptimized extents. */
366 void *pvGrain;
367 /** Reference to the image in which this extent is used. Do not use this
368 * on a regular basis to avoid passing pImage references to functions
369 * explicitly. */
370 struct VMDKIMAGE *pImage;
371} VMDKEXTENT, *PVMDKEXTENT;
372
373/**
374 * Grain table cache size. Allocated per image.
375 */
376#define VMDK_GT_CACHE_SIZE 256
377
378/**
379 * Grain table block size. Smaller than an actual grain table block to allow
380 * more grain table blocks to be cached without having to allocate excessive
381 * amounts of memory for the cache.
382 */
383#define VMDK_GT_CACHELINE_SIZE 128
384
385
386/**
387 * Maximum number of lines in a descriptor file. Not worth the effort of
388 * making it variable. Descriptor files are generally very short (~20 lines),
389 * with the exception of sparse files split in 2G chunks, which need for the
390 * maximum size (almost 2T) exactly 1025 lines for the disk database.
391 */
392#define VMDK_DESCRIPTOR_LINES_MAX 1100U
393
394/**
395 * Parsed descriptor information. Allows easy access and update of the
396 * descriptor (whether separate file or not). Free form text files suck.
397 */
398typedef struct VMDKDESCRIPTOR
399{
400 /** Line number of first entry of the disk descriptor. */
401 unsigned uFirstDesc;
402 /** Line number of first entry in the extent description. */
403 unsigned uFirstExtent;
404 /** Line number of first disk database entry. */
405 unsigned uFirstDDB;
406 /** Total number of lines. */
407 unsigned cLines;
408 /** Total amount of memory available for the descriptor. */
409 size_t cbDescAlloc;
410 /** Set if descriptor has been changed and not yet written to disk. */
411 bool fDirty;
412 /** Array of pointers to the data in the descriptor. */
413 char *aLines[VMDK_DESCRIPTOR_LINES_MAX];
414 /** Array of line indices pointing to the next non-comment line. */
415 unsigned aNextLines[VMDK_DESCRIPTOR_LINES_MAX];
416} VMDKDESCRIPTOR, *PVMDKDESCRIPTOR;
417
418
419/**
420 * Cache entry for translating extent/sector to a sector number in that
421 * extent.
422 */
423typedef struct VMDKGTCACHEENTRY
424{
425 /** Extent number for which this entry is valid. */
426 uint32_t uExtent;
427 /** GT data block number. */
428 uint64_t uGTBlock;
429 /** Data part of the cache entry. */
430 uint32_t aGTData[VMDK_GT_CACHELINE_SIZE];
431} VMDKGTCACHEENTRY, *PVMDKGTCACHEENTRY;
432
433/**
434 * Cache data structure for blocks of grain table entries. For now this is a
435 * fixed size direct mapping cache, but this should be adapted to the size of
436 * the sparse image and maybe converted to a set-associative cache. The
437 * implementation below implements a write-through cache with write allocate.
438 */
439typedef struct VMDKGTCACHE
440{
441 /** Cache entries. */
442 VMDKGTCACHEENTRY aGTCache[VMDK_GT_CACHE_SIZE];
443 /** Number of cache entries (currently unused). */
444 unsigned cEntries;
445} VMDKGTCACHE, *PVMDKGTCACHE;
446
447/**
448 * Complete VMDK image data structure. Mainly a collection of extents and a few
449 * extra global data fields.
450 */
451typedef struct VMDKIMAGE
452{
453 /** Image name. */
454 const char *pszFilename;
455 /** Descriptor file if applicable. */
456 PVMDKFILE pFile;
457
458 /** Pointer to the per-disk VD interface list. */
459 PVDINTERFACE pVDIfsDisk;
460 /** Pointer to the per-image VD interface list. */
461 PVDINTERFACE pVDIfsImage;
462
463 /** Error interface. */
464 PVDINTERFACEERROR pIfError;
465 /** I/O interface. */
466 PVDINTERFACEIOINT pIfIo;
467
468
469 /** Pointer to the image extents. */
470 PVMDKEXTENT pExtents;
471 /** Number of image extents. */
472 unsigned cExtents;
473 /** Pointer to the files list, for opening a file referenced multiple
474 * times only once (happens mainly with raw partition access). */
475 PVMDKFILE pFiles;
476
477 /**
478 * Pointer to an array of segment entries for async I/O.
479 * This is an optimization because the task number to submit is not known
480 * and allocating/freeing an array in the read/write functions every time
481 * is too expensive.
482 */
483 PRTSGSEG paSegments;
484 /** Entries available in the segments array. */
485 unsigned cSegments;
486
487 /** Open flags passed by VBoxHD layer. */
488 unsigned uOpenFlags;
489 /** Image flags defined during creation or determined during open. */
490 unsigned uImageFlags;
491 /** Total size of the image. */
492 uint64_t cbSize;
493 /** Physical geometry of this image. */
494 VDGEOMETRY PCHSGeometry;
495 /** Logical geometry of this image. */
496 VDGEOMETRY LCHSGeometry;
497 /** Image UUID. */
498 RTUUID ImageUuid;
499 /** Image modification UUID. */
500 RTUUID ModificationUuid;
501 /** Parent image UUID. */
502 RTUUID ParentUuid;
503 /** Parent image modification UUID. */
504 RTUUID ParentModificationUuid;
505
506 /** Pointer to grain table cache, if this image contains sparse extents. */
507 PVMDKGTCACHE pGTCache;
508 /** Pointer to the descriptor (NULL if no separate descriptor file). */
509 char *pDescData;
510 /** Allocation size of the descriptor file. */
511 size_t cbDescAlloc;
512 /** Parsed descriptor file content. */
513 VMDKDESCRIPTOR Descriptor;
514 /** The static region list. */
515 VDREGIONLIST RegionList;
516} VMDKIMAGE;
517
518
519/** State for the input/output callout of the inflate reader/deflate writer. */
520typedef struct VMDKCOMPRESSIO
521{
522 /* Image this operation relates to. */
523 PVMDKIMAGE pImage;
524 /* Current read position. */
525 ssize_t iOffset;
526 /* Size of the compressed grain buffer (available data). */
527 size_t cbCompGrain;
528 /* Pointer to the compressed grain buffer. */
529 void *pvCompGrain;
530} VMDKCOMPRESSIO;
531
532
533/** Tracks async grain allocation. */
534typedef struct VMDKGRAINALLOCASYNC
535{
536 /** Flag whether the allocation failed. */
537 bool fIoErr;
538 /** Current number of transfers pending.
539 * If reached 0 and there is an error the old state is restored. */
540 unsigned cIoXfersPending;
541 /** Sector number */
542 uint64_t uSector;
543 /** Flag whether the grain table needs to be updated. */
544 bool fGTUpdateNeeded;
545 /** Extent the allocation happens. */
546 PVMDKEXTENT pExtent;
547 /** Position of the new grain, required for the grain table update. */
548 uint64_t uGrainOffset;
549 /** Grain table sector. */
550 uint64_t uGTSector;
551 /** Backup grain table sector. */
552 uint64_t uRGTSector;
553} VMDKGRAINALLOCASYNC, *PVMDKGRAINALLOCASYNC;
554
555/**
556 * State information for vmdkRename() and helpers.
557 */
558typedef struct VMDKRENAMESTATE
559{
560 /** Array of old filenames. */
561 char **apszOldName;
562 /** Array of new filenames. */
563 char **apszNewName;
564 /** Array of new lines in the extent descriptor. */
565 char **apszNewLines;
566 /** Name of the old descriptor file if not a sparse image. */
567 char *pszOldDescName;
568 /** Flag whether we called vmdkFreeImage(). */
569 bool fImageFreed;
570 /** Flag whther the descriptor is embedded in the image (sparse) or
571 * in a separate file. */
572 bool fEmbeddedDesc;
573 /** Number of extents in the image. */
574 unsigned cExtents;
575 /** New base filename. */
576 char *pszNewBaseName;
577 /** The old base filename. */
578 char *pszOldBaseName;
579 /** New full filename. */
580 char *pszNewFullName;
581 /** Old full filename. */
582 char *pszOldFullName;
583 /** The old image name. */
584 const char *pszOldImageName;
585 /** Copy of the original VMDK descriptor. */
586 VMDKDESCRIPTOR DescriptorCopy;
587 /** Copy of the extent state for sparse images. */
588 VMDKEXTENT ExtentCopy;
589} VMDKRENAMESTATE;
590/** Pointer to a VMDK rename state. */
591typedef VMDKRENAMESTATE *PVMDKRENAMESTATE;
592
593
594/*********************************************************************************************************************************
595* Static Variables *
596*********************************************************************************************************************************/
597
598/** NULL-terminated array of supported file extensions. */
599static const VDFILEEXTENSION s_aVmdkFileExtensions[] =
600{
601 {"vmdk", VDTYPE_HDD},
602 {NULL, VDTYPE_INVALID}
603};
604
605/** NULL-terminated array of configuration option. */
606static const VDCONFIGINFO s_aVmdkConfigInfo[] =
607{
608 /* Options for VMDK raw disks */
609 { "RawDrive", NULL, VDCFGVALUETYPE_STRING, 0 },
610 { "Partitions", NULL, VDCFGVALUETYPE_STRING, 0 },
611 { "BootSector", NULL, VDCFGVALUETYPE_BYTES, 0 },
612 { "Relative", NULL, VDCFGVALUETYPE_INTEGER, 0 },
613
614 /* End of options list */
615 { NULL, NULL, VDCFGVALUETYPE_INTEGER, 0 }
616};
617
618
619/*********************************************************************************************************************************
620* Internal Functions *
621*********************************************************************************************************************************/
622
623static void vmdkFreeStreamBuffers(PVMDKEXTENT pExtent);
624static int vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
625 bool fDelete);
626
627static int vmdkCreateExtents(PVMDKIMAGE pImage, unsigned cExtents);
628static int vmdkFlushImage(PVMDKIMAGE pImage, PVDIOCTX pIoCtx);
629static int vmdkSetImageComment(PVMDKIMAGE pImage, const char *pszComment);
630static int vmdkFreeImage(PVMDKIMAGE pImage, bool fDelete, bool fFlush);
631
632static DECLCALLBACK(int) vmdkAllocGrainComplete(void *pBackendData, PVDIOCTX pIoCtx,
633 void *pvUser, int rcReq);
634
635/**
636 * Internal: open a file (using a file descriptor cache to ensure each file
637 * is only opened once - anything else can cause locking problems).
638 */
639static int vmdkFileOpen(PVMDKIMAGE pImage, PVMDKFILE *ppVmdkFile,
640 const char *pszBasename, const char *pszFilename, uint32_t fOpen)
641{
642 int rc = VINF_SUCCESS;
643 PVMDKFILE pVmdkFile;
644
645 for (pVmdkFile = pImage->pFiles;
646 pVmdkFile != NULL;
647 pVmdkFile = pVmdkFile->pNext)
648 {
649 if (!strcmp(pszFilename, pVmdkFile->pszFilename))
650 {
651 Assert(fOpen == pVmdkFile->fOpen);
652 pVmdkFile->uReferences++;
653
654 *ppVmdkFile = pVmdkFile;
655
656 return rc;
657 }
658 }
659
660 /* If we get here, there's no matching entry in the cache. */
661 pVmdkFile = (PVMDKFILE)RTMemAllocZ(sizeof(VMDKFILE));
662 if (!pVmdkFile)
663 {
664 *ppVmdkFile = NULL;
665 return VERR_NO_MEMORY;
666 }
667
668 pVmdkFile->pszFilename = RTStrDup(pszFilename);
669 if (!pVmdkFile->pszFilename)
670 {
671 RTMemFree(pVmdkFile);
672 *ppVmdkFile = NULL;
673 return VERR_NO_MEMORY;
674 }
675
676 if (pszBasename)
677 {
678 pVmdkFile->pszBasename = RTStrDup(pszBasename);
679 if (!pVmdkFile->pszBasename)
680 {
681 RTStrFree((char *)(void *)pVmdkFile->pszFilename);
682 RTMemFree(pVmdkFile);
683 *ppVmdkFile = NULL;
684 return VERR_NO_MEMORY;
685 }
686 }
687
688 pVmdkFile->fOpen = fOpen;
689
690 rc = vdIfIoIntFileOpen(pImage->pIfIo, pszFilename, fOpen,
691 &pVmdkFile->pStorage);
692 if (RT_SUCCESS(rc))
693 {
694 pVmdkFile->uReferences = 1;
695 pVmdkFile->pImage = pImage;
696 pVmdkFile->pNext = pImage->pFiles;
697 if (pImage->pFiles)
698 pImage->pFiles->pPrev = pVmdkFile;
699 pImage->pFiles = pVmdkFile;
700 *ppVmdkFile = pVmdkFile;
701 }
702 else
703 {
704 RTStrFree((char *)(void *)pVmdkFile->pszFilename);
705 RTMemFree(pVmdkFile);
706 *ppVmdkFile = NULL;
707 }
708
709 return rc;
710}
711
712/**
713 * Internal: close a file, updating the file descriptor cache.
714 */
715static int vmdkFileClose(PVMDKIMAGE pImage, PVMDKFILE *ppVmdkFile, bool fDelete)
716{
717 int rc = VINF_SUCCESS;
718 PVMDKFILE pVmdkFile = *ppVmdkFile;
719
720 AssertPtr(pVmdkFile);
721
722 pVmdkFile->fDelete |= fDelete;
723 Assert(pVmdkFile->uReferences);
724 pVmdkFile->uReferences--;
725 if (pVmdkFile->uReferences == 0)
726 {
727 PVMDKFILE pPrev;
728 PVMDKFILE pNext;
729
730 /* Unchain the element from the list. */
731 pPrev = pVmdkFile->pPrev;
732 pNext = pVmdkFile->pNext;
733
734 if (pNext)
735 pNext->pPrev = pPrev;
736 if (pPrev)
737 pPrev->pNext = pNext;
738 else
739 pImage->pFiles = pNext;
740
741 rc = vdIfIoIntFileClose(pImage->pIfIo, pVmdkFile->pStorage);
742
743 bool fFileDel = pVmdkFile->fDelete;
744 if ( pVmdkFile->pszBasename
745 && fFileDel)
746 {
747 const char *pszSuffix = RTPathSuffix(pVmdkFile->pszBasename);
748 if ( RTPathHasPath(pVmdkFile->pszBasename)
749 || !pszSuffix
750 || ( strcmp(pszSuffix, ".vmdk")
751 && strcmp(pszSuffix, ".bin")
752 && strcmp(pszSuffix, ".img")))
753 fFileDel = false;
754 }
755
756 if (fFileDel)
757 {
758 int rc2 = vdIfIoIntFileDelete(pImage->pIfIo, pVmdkFile->pszFilename);
759 if (RT_SUCCESS(rc))
760 rc = rc2;
761 }
762 else if (pVmdkFile->fDelete)
763 LogRel(("VMDK: Denying deletion of %s\n", pVmdkFile->pszBasename));
764 RTStrFree((char *)(void *)pVmdkFile->pszFilename);
765 if (pVmdkFile->pszBasename)
766 RTStrFree((char *)(void *)pVmdkFile->pszBasename);
767 RTMemFree(pVmdkFile);
768 }
769
770 *ppVmdkFile = NULL;
771 return rc;
772}
773
774/*#define VMDK_USE_BLOCK_DECOMP_API - test and enable */
775#ifndef VMDK_USE_BLOCK_DECOMP_API
776static DECLCALLBACK(int) vmdkFileInflateHelper(void *pvUser, void *pvBuf, size_t cbBuf, size_t *pcbBuf)
777{
778 VMDKCOMPRESSIO *pInflateState = (VMDKCOMPRESSIO *)pvUser;
779 size_t cbInjected = 0;
780
781 Assert(cbBuf);
782 if (pInflateState->iOffset < 0)
783 {
784 *(uint8_t *)pvBuf = RTZIPTYPE_ZLIB;
785 pvBuf = (uint8_t *)pvBuf + 1;
786 cbBuf--;
787 cbInjected = 1;
788 pInflateState->iOffset = RT_UOFFSETOF(VMDKMARKER, uType);
789 }
790 if (!cbBuf)
791 {
792 if (pcbBuf)
793 *pcbBuf = cbInjected;
794 return VINF_SUCCESS;
795 }
796 cbBuf = RT_MIN(cbBuf, pInflateState->cbCompGrain - pInflateState->iOffset);
797 memcpy(pvBuf,
798 (uint8_t *)pInflateState->pvCompGrain + pInflateState->iOffset,
799 cbBuf);
800 pInflateState->iOffset += cbBuf;
801 Assert(pcbBuf);
802 *pcbBuf = cbBuf + cbInjected;
803 return VINF_SUCCESS;
804}
805#endif
806
807/**
808 * Internal: read from a file and inflate the compressed data,
809 * distinguishing between async and normal operation
810 */
811DECLINLINE(int) vmdkFileInflateSync(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
812 uint64_t uOffset, void *pvBuf,
813 size_t cbToRead, const void *pcvMarker,
814 uint64_t *puLBA, uint32_t *pcbMarkerData)
815{
816 int rc;
817#ifndef VMDK_USE_BLOCK_DECOMP_API
818 PRTZIPDECOMP pZip = NULL;
819#endif
820 VMDKMARKER *pMarker = (VMDKMARKER *)pExtent->pvCompGrain;
821 size_t cbCompSize, cbActuallyRead;
822
823 if (!pcvMarker)
824 {
825 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
826 uOffset, pMarker, RT_UOFFSETOF(VMDKMARKER, uType));
827 if (RT_FAILURE(rc))
828 return rc;
829 }
830 else
831 {
832 memcpy(pMarker, pcvMarker, RT_UOFFSETOF(VMDKMARKER, uType));
833 /* pcvMarker endianness has already been partially transformed, fix it */
834 pMarker->uSector = RT_H2LE_U64(pMarker->uSector);
835 pMarker->cbSize = RT_H2LE_U32(pMarker->cbSize);
836 }
837
838 cbCompSize = RT_LE2H_U32(pMarker->cbSize);
839 if (cbCompSize == 0)
840 {
841 AssertMsgFailed(("VMDK: corrupted marker\n"));
842 return VERR_VD_VMDK_INVALID_FORMAT;
843 }
844
845 /* Sanity check - the expansion ratio should be much less than 2. */
846 Assert(cbCompSize < 2 * cbToRead);
847 if (cbCompSize >= 2 * cbToRead)
848 return VERR_VD_VMDK_INVALID_FORMAT;
849
850 /* Compressed grain marker. Data follows immediately. */
851 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
852 uOffset + RT_UOFFSETOF(VMDKMARKER, uType),
853 (uint8_t *)pExtent->pvCompGrain
854 + RT_UOFFSETOF(VMDKMARKER, uType),
855 RT_ALIGN_Z( cbCompSize
856 + RT_UOFFSETOF(VMDKMARKER, uType),
857 512)
858 - RT_UOFFSETOF(VMDKMARKER, uType));
859
860 if (puLBA)
861 *puLBA = RT_LE2H_U64(pMarker->uSector);
862 if (pcbMarkerData)
863 *pcbMarkerData = RT_ALIGN( cbCompSize
864 + RT_UOFFSETOF(VMDKMARKER, uType),
865 512);
866
867#ifdef VMDK_USE_BLOCK_DECOMP_API
868 rc = RTZipBlockDecompress(RTZIPTYPE_ZLIB, 0 /*fFlags*/,
869 pExtent->pvCompGrain, cbCompSize + RT_UOFFSETOF(VMDKMARKER, uType), NULL,
870 pvBuf, cbToRead, &cbActuallyRead);
871#else
872 VMDKCOMPRESSIO InflateState;
873 InflateState.pImage = pImage;
874 InflateState.iOffset = -1;
875 InflateState.cbCompGrain = cbCompSize + RT_UOFFSETOF(VMDKMARKER, uType);
876 InflateState.pvCompGrain = pExtent->pvCompGrain;
877
878 rc = RTZipDecompCreate(&pZip, &InflateState, vmdkFileInflateHelper);
879 if (RT_FAILURE(rc))
880 return rc;
881 rc = RTZipDecompress(pZip, pvBuf, cbToRead, &cbActuallyRead);
882 RTZipDecompDestroy(pZip);
883#endif /* !VMDK_USE_BLOCK_DECOMP_API */
884 if (RT_FAILURE(rc))
885 {
886 if (rc == VERR_ZIP_CORRUPTED)
887 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: Compressed image is corrupted '%s'"), pExtent->pszFullname);
888 return rc;
889 }
890 if (cbActuallyRead != cbToRead)
891 rc = VERR_VD_VMDK_INVALID_FORMAT;
892 return rc;
893}
894
895static DECLCALLBACK(int) vmdkFileDeflateHelper(void *pvUser, const void *pvBuf, size_t cbBuf)
896{
897 VMDKCOMPRESSIO *pDeflateState = (VMDKCOMPRESSIO *)pvUser;
898
899 Assert(cbBuf);
900 if (pDeflateState->iOffset < 0)
901 {
902 pvBuf = (const uint8_t *)pvBuf + 1;
903 cbBuf--;
904 pDeflateState->iOffset = RT_UOFFSETOF(VMDKMARKER, uType);
905 }
906 if (!cbBuf)
907 return VINF_SUCCESS;
908 if (pDeflateState->iOffset + cbBuf > pDeflateState->cbCompGrain)
909 return VERR_BUFFER_OVERFLOW;
910 memcpy((uint8_t *)pDeflateState->pvCompGrain + pDeflateState->iOffset,
911 pvBuf, cbBuf);
912 pDeflateState->iOffset += cbBuf;
913 return VINF_SUCCESS;
914}
915
916/**
917 * Internal: deflate the uncompressed data and write to a file,
918 * distinguishing between async and normal operation
919 */
920DECLINLINE(int) vmdkFileDeflateSync(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
921 uint64_t uOffset, const void *pvBuf,
922 size_t cbToWrite, uint64_t uLBA,
923 uint32_t *pcbMarkerData)
924{
925 int rc;
926 PRTZIPCOMP pZip = NULL;
927 VMDKCOMPRESSIO DeflateState;
928
929 DeflateState.pImage = pImage;
930 DeflateState.iOffset = -1;
931 DeflateState.cbCompGrain = pExtent->cbCompGrain;
932 DeflateState.pvCompGrain = pExtent->pvCompGrain;
933
934 rc = RTZipCompCreate(&pZip, &DeflateState, vmdkFileDeflateHelper,
935 RTZIPTYPE_ZLIB, RTZIPLEVEL_DEFAULT);
936 if (RT_FAILURE(rc))
937 return rc;
938 rc = RTZipCompress(pZip, pvBuf, cbToWrite);
939 if (RT_SUCCESS(rc))
940 rc = RTZipCompFinish(pZip);
941 RTZipCompDestroy(pZip);
942 if (RT_SUCCESS(rc))
943 {
944 Assert( DeflateState.iOffset > 0
945 && (size_t)DeflateState.iOffset <= DeflateState.cbCompGrain);
946
947 /* pad with zeroes to get to a full sector size */
948 uint32_t uSize = DeflateState.iOffset;
949 if (uSize % 512)
950 {
951 uint32_t uSizeAlign = RT_ALIGN(uSize, 512);
952 memset((uint8_t *)pExtent->pvCompGrain + uSize, '\0',
953 uSizeAlign - uSize);
954 uSize = uSizeAlign;
955 }
956
957 if (pcbMarkerData)
958 *pcbMarkerData = uSize;
959
960 /* Compressed grain marker. Data follows immediately. */
961 VMDKMARKER *pMarker = (VMDKMARKER *)pExtent->pvCompGrain;
962 pMarker->uSector = RT_H2LE_U64(uLBA);
963 pMarker->cbSize = RT_H2LE_U32( DeflateState.iOffset
964 - RT_UOFFSETOF(VMDKMARKER, uType));
965 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
966 uOffset, pMarker, uSize);
967 if (RT_FAILURE(rc))
968 return rc;
969 }
970 return rc;
971}
972
973
974/**
975 * Internal: check if all files are closed, prevent leaking resources.
976 */
977static int vmdkFileCheckAllClose(PVMDKIMAGE pImage)
978{
979 int rc = VINF_SUCCESS, rc2;
980 PVMDKFILE pVmdkFile;
981
982 Assert(pImage->pFiles == NULL);
983 for (pVmdkFile = pImage->pFiles;
984 pVmdkFile != NULL;
985 pVmdkFile = pVmdkFile->pNext)
986 {
987 LogRel(("VMDK: leaking reference to file \"%s\"\n",
988 pVmdkFile->pszFilename));
989 pImage->pFiles = pVmdkFile->pNext;
990
991 rc2 = vmdkFileClose(pImage, &pVmdkFile, pVmdkFile->fDelete);
992
993 if (RT_SUCCESS(rc))
994 rc = rc2;
995 }
996 return rc;
997}
998
999/**
1000 * Internal: truncate a string (at a UTF8 code point boundary) and encode the
1001 * critical non-ASCII characters.
1002 */
1003static char *vmdkEncodeString(const char *psz)
1004{
1005 char szEnc[VMDK_ENCODED_COMMENT_MAX + 3];
1006 char *pszDst = szEnc;
1007
1008 AssertPtr(psz);
1009
1010 for (; *psz; psz = RTStrNextCp(psz))
1011 {
1012 char *pszDstPrev = pszDst;
1013 RTUNICP Cp = RTStrGetCp(psz);
1014 if (Cp == '\\')
1015 {
1016 pszDst = RTStrPutCp(pszDst, Cp);
1017 pszDst = RTStrPutCp(pszDst, Cp);
1018 }
1019 else if (Cp == '\n')
1020 {
1021 pszDst = RTStrPutCp(pszDst, '\\');
1022 pszDst = RTStrPutCp(pszDst, 'n');
1023 }
1024 else if (Cp == '\r')
1025 {
1026 pszDst = RTStrPutCp(pszDst, '\\');
1027 pszDst = RTStrPutCp(pszDst, 'r');
1028 }
1029 else
1030 pszDst = RTStrPutCp(pszDst, Cp);
1031 if (pszDst - szEnc >= VMDK_ENCODED_COMMENT_MAX - 1)
1032 {
1033 pszDst = pszDstPrev;
1034 break;
1035 }
1036 }
1037 *pszDst = '\0';
1038 return RTStrDup(szEnc);
1039}
1040
1041/**
1042 * Internal: decode a string and store it into the specified string.
1043 */
1044static int vmdkDecodeString(const char *pszEncoded, char *psz, size_t cb)
1045{
1046 int rc = VINF_SUCCESS;
1047 char szBuf[4];
1048
1049 if (!cb)
1050 return VERR_BUFFER_OVERFLOW;
1051
1052 AssertPtr(psz);
1053
1054 for (; *pszEncoded; pszEncoded = RTStrNextCp(pszEncoded))
1055 {
1056 char *pszDst = szBuf;
1057 RTUNICP Cp = RTStrGetCp(pszEncoded);
1058 if (Cp == '\\')
1059 {
1060 pszEncoded = RTStrNextCp(pszEncoded);
1061 RTUNICP CpQ = RTStrGetCp(pszEncoded);
1062 if (CpQ == 'n')
1063 RTStrPutCp(pszDst, '\n');
1064 else if (CpQ == 'r')
1065 RTStrPutCp(pszDst, '\r');
1066 else if (CpQ == '\0')
1067 {
1068 rc = VERR_VD_VMDK_INVALID_HEADER;
1069 break;
1070 }
1071 else
1072 RTStrPutCp(pszDst, CpQ);
1073 }
1074 else
1075 pszDst = RTStrPutCp(pszDst, Cp);
1076
1077 /* Need to leave space for terminating NUL. */
1078 if ((size_t)(pszDst - szBuf) + 1 >= cb)
1079 {
1080 rc = VERR_BUFFER_OVERFLOW;
1081 break;
1082 }
1083 memcpy(psz, szBuf, pszDst - szBuf);
1084 psz += pszDst - szBuf;
1085 }
1086 *psz = '\0';
1087 return rc;
1088}
1089
1090/**
1091 * Internal: free all buffers associated with grain directories.
1092 */
1093static void vmdkFreeGrainDirectory(PVMDKEXTENT pExtent)
1094{
1095 if (pExtent->pGD)
1096 {
1097 RTMemFree(pExtent->pGD);
1098 pExtent->pGD = NULL;
1099 }
1100 if (pExtent->pRGD)
1101 {
1102 RTMemFree(pExtent->pRGD);
1103 pExtent->pRGD = NULL;
1104 }
1105}
1106
1107/**
1108 * Internal: allocate the compressed/uncompressed buffers for streamOptimized
1109 * images.
1110 */
1111static int vmdkAllocStreamBuffers(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
1112{
1113 int rc = VINF_SUCCESS;
1114
1115 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
1116 {
1117 /* streamOptimized extents need a compressed grain buffer, which must
1118 * be big enough to hold uncompressible data (which needs ~8 bytes
1119 * more than the uncompressed data), the marker and padding. */
1120 pExtent->cbCompGrain = RT_ALIGN_Z( VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain)
1121 + 8 + sizeof(VMDKMARKER), 512);
1122 pExtent->pvCompGrain = RTMemAlloc(pExtent->cbCompGrain);
1123 if (RT_LIKELY(pExtent->pvCompGrain))
1124 {
1125 /* streamOptimized extents need a decompressed grain buffer. */
1126 pExtent->pvGrain = RTMemAlloc(VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
1127 if (!pExtent->pvGrain)
1128 rc = VERR_NO_MEMORY;
1129 }
1130 else
1131 rc = VERR_NO_MEMORY;
1132 }
1133
1134 if (RT_FAILURE(rc))
1135 vmdkFreeStreamBuffers(pExtent);
1136 return rc;
1137}
1138
1139/**
1140 * Internal: allocate all buffers associated with grain directories.
1141 */
1142static int vmdkAllocGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
1143{
1144 RT_NOREF1(pImage);
1145 int rc = VINF_SUCCESS;
1146 size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
1147
1148 pExtent->pGD = (uint32_t *)RTMemAllocZ(cbGD);
1149 if (RT_LIKELY(pExtent->pGD))
1150 {
1151 if (pExtent->uSectorRGD)
1152 {
1153 pExtent->pRGD = (uint32_t *)RTMemAllocZ(cbGD);
1154 if (RT_UNLIKELY(!pExtent->pRGD))
1155 rc = VERR_NO_MEMORY;
1156 }
1157 }
1158 else
1159 rc = VERR_NO_MEMORY;
1160
1161 if (RT_FAILURE(rc))
1162 vmdkFreeGrainDirectory(pExtent);
1163 return rc;
1164}
1165
1166/**
1167 * Converts the grain directory from little to host endianess.
1168 *
1169 * @param pGD The grain directory.
1170 * @param cGDEntries Number of entries in the grain directory to convert.
1171 */
1172DECLINLINE(void) vmdkGrainDirectoryConvToHost(uint32_t *pGD, uint32_t cGDEntries)
1173{
1174 uint32_t *pGDTmp = pGD;
1175
1176 for (uint32_t i = 0; i < cGDEntries; i++, pGDTmp++)
1177 *pGDTmp = RT_LE2H_U32(*pGDTmp);
1178}
1179
1180/**
1181 * Read the grain directory and allocated grain tables verifying them against
1182 * their back up copies if available.
1183 *
1184 * @returns VBox status code.
1185 * @param pImage Image instance data.
1186 * @param pExtent The VMDK extent.
1187 */
1188static int vmdkReadGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
1189{
1190 int rc = VINF_SUCCESS;
1191 size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
1192
1193 AssertReturn(( pExtent->enmType == VMDKETYPE_HOSTED_SPARSE
1194 && pExtent->uSectorGD != VMDK_GD_AT_END
1195 && pExtent->uSectorRGD != VMDK_GD_AT_END), VERR_INTERNAL_ERROR);
1196
1197 rc = vmdkAllocGrainDirectory(pImage, pExtent);
1198 if (RT_SUCCESS(rc))
1199 {
1200 /* The VMDK 1.1 spec seems to talk about compressed grain directories,
1201 * but in reality they are not compressed. */
1202 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
1203 VMDK_SECTOR2BYTE(pExtent->uSectorGD),
1204 pExtent->pGD, cbGD);
1205 if (RT_SUCCESS(rc))
1206 {
1207 vmdkGrainDirectoryConvToHost(pExtent->pGD, pExtent->cGDEntries);
1208
1209 if ( pExtent->uSectorRGD
1210 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_SKIP_CONSISTENCY_CHECKS))
1211 {
1212 /* The VMDK 1.1 spec seems to talk about compressed grain directories,
1213 * but in reality they are not compressed. */
1214 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
1215 VMDK_SECTOR2BYTE(pExtent->uSectorRGD),
1216 pExtent->pRGD, cbGD);
1217 if (RT_SUCCESS(rc))
1218 {
1219 vmdkGrainDirectoryConvToHost(pExtent->pRGD, pExtent->cGDEntries);
1220
1221 /* Check grain table and redundant grain table for consistency. */
1222 size_t cbGT = pExtent->cGTEntries * sizeof(uint32_t);
1223 size_t cbGTBuffers = cbGT; /* Start with space for one GT. */
1224 size_t cbGTBuffersMax = _1M;
1225
1226 uint32_t *pTmpGT1 = (uint32_t *)RTMemAlloc(cbGTBuffers);
1227 uint32_t *pTmpGT2 = (uint32_t *)RTMemAlloc(cbGTBuffers);
1228
1229 if ( !pTmpGT1
1230 || !pTmpGT2)
1231 rc = VERR_NO_MEMORY;
1232
1233 size_t i = 0;
1234 uint32_t *pGDTmp = pExtent->pGD;
1235 uint32_t *pRGDTmp = pExtent->pRGD;
1236
1237 /* Loop through all entries. */
1238 while (i < pExtent->cGDEntries)
1239 {
1240 uint32_t uGTStart = *pGDTmp;
1241 uint32_t uRGTStart = *pRGDTmp;
1242 size_t cbGTRead = cbGT;
1243
1244 /* If no grain table is allocated skip the entry. */
1245 if (*pGDTmp == 0 && *pRGDTmp == 0)
1246 {
1247 i++;
1248 continue;
1249 }
1250
1251 if (*pGDTmp == 0 || *pRGDTmp == 0 || *pGDTmp == *pRGDTmp)
1252 {
1253 /* Just one grain directory entry refers to a not yet allocated
1254 * grain table or both grain directory copies refer to the same
1255 * grain table. Not allowed. */
1256 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
1257 N_("VMDK: inconsistent references to grain directory in '%s'"), pExtent->pszFullname);
1258 break;
1259 }
1260
1261 i++;
1262 pGDTmp++;
1263 pRGDTmp++;
1264
1265 /*
1266 * Read a few tables at once if adjacent to decrease the number
1267 * of I/O requests. Read at maximum 1MB at once.
1268 */
1269 while ( i < pExtent->cGDEntries
1270 && cbGTRead < cbGTBuffersMax)
1271 {
1272 /* If no grain table is allocated skip the entry. */
1273 if (*pGDTmp == 0 && *pRGDTmp == 0)
1274 {
1275 i++;
1276 continue;
1277 }
1278
1279 if (*pGDTmp == 0 || *pRGDTmp == 0 || *pGDTmp == *pRGDTmp)
1280 {
1281 /* Just one grain directory entry refers to a not yet allocated
1282 * grain table or both grain directory copies refer to the same
1283 * grain table. Not allowed. */
1284 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
1285 N_("VMDK: inconsistent references to grain directory in '%s'"), pExtent->pszFullname);
1286 break;
1287 }
1288
1289 /* Check that the start offsets are adjacent.*/
1290 if ( VMDK_SECTOR2BYTE(uGTStart) + cbGTRead != VMDK_SECTOR2BYTE(*pGDTmp)
1291 || VMDK_SECTOR2BYTE(uRGTStart) + cbGTRead != VMDK_SECTOR2BYTE(*pRGDTmp))
1292 break;
1293
1294 i++;
1295 pGDTmp++;
1296 pRGDTmp++;
1297 cbGTRead += cbGT;
1298 }
1299
1300 /* Increase buffers if required. */
1301 if ( RT_SUCCESS(rc)
1302 && cbGTBuffers < cbGTRead)
1303 {
1304 uint32_t *pTmp;
1305 pTmp = (uint32_t *)RTMemRealloc(pTmpGT1, cbGTRead);
1306 if (pTmp)
1307 {
1308 pTmpGT1 = pTmp;
1309 pTmp = (uint32_t *)RTMemRealloc(pTmpGT2, cbGTRead);
1310 if (pTmp)
1311 pTmpGT2 = pTmp;
1312 else
1313 rc = VERR_NO_MEMORY;
1314 }
1315 else
1316 rc = VERR_NO_MEMORY;
1317
1318 if (rc == VERR_NO_MEMORY)
1319 {
1320 /* Reset to the old values. */
1321 rc = VINF_SUCCESS;
1322 i -= cbGTRead / cbGT;
1323 cbGTRead = cbGT;
1324
1325 /* Don't try to increase the buffer again in the next run. */
1326 cbGTBuffersMax = cbGTBuffers;
1327 }
1328 }
1329
1330 if (RT_SUCCESS(rc))
1331 {
1332 /* The VMDK 1.1 spec seems to talk about compressed grain tables,
1333 * but in reality they are not compressed. */
1334 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
1335 VMDK_SECTOR2BYTE(uGTStart),
1336 pTmpGT1, cbGTRead);
1337 if (RT_FAILURE(rc))
1338 {
1339 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1340 N_("VMDK: error reading grain table in '%s'"), pExtent->pszFullname);
1341 break;
1342 }
1343 /* The VMDK 1.1 spec seems to talk about compressed grain tables,
1344 * but in reality they are not compressed. */
1345 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
1346 VMDK_SECTOR2BYTE(uRGTStart),
1347 pTmpGT2, cbGTRead);
1348 if (RT_FAILURE(rc))
1349 {
1350 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1351 N_("VMDK: error reading backup grain table in '%s'"), pExtent->pszFullname);
1352 break;
1353 }
1354 if (memcmp(pTmpGT1, pTmpGT2, cbGTRead))
1355 {
1356 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
1357 N_("VMDK: inconsistency between grain table and backup grain table in '%s'"), pExtent->pszFullname);
1358 break;
1359 }
1360 }
1361 } /* while (i < pExtent->cGDEntries) */
1362
1363 /** @todo figure out what to do for unclean VMDKs. */
1364 if (pTmpGT1)
1365 RTMemFree(pTmpGT1);
1366 if (pTmpGT2)
1367 RTMemFree(pTmpGT2);
1368 }
1369 else
1370 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1371 N_("VMDK: could not read redundant grain directory in '%s'"), pExtent->pszFullname);
1372 }
1373 }
1374 else
1375 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
1376 N_("VMDK: could not read grain directory in '%s': %Rrc"), pExtent->pszFullname, rc);
1377 }
1378
1379 if (RT_FAILURE(rc))
1380 vmdkFreeGrainDirectory(pExtent);
1381 return rc;
1382}
1383
1384/**
1385 * Creates a new grain directory for the given extent at the given start sector.
1386 *
1387 * @returns VBox status code.
1388 * @param pImage Image instance data.
1389 * @param pExtent The VMDK extent.
1390 * @param uStartSector Where the grain directory should be stored in the image.
1391 * @param fPreAlloc Flag whether to pre allocate the grain tables at this point.
1392 */
1393static int vmdkCreateGrainDirectory(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
1394 uint64_t uStartSector, bool fPreAlloc)
1395{
1396 int rc = VINF_SUCCESS;
1397 unsigned i;
1398 size_t cbGD = pExtent->cGDEntries * sizeof(uint32_t);
1399 size_t cbGDRounded = RT_ALIGN_64(cbGD, 512);
1400 size_t cbGTRounded;
1401 uint64_t cbOverhead;
1402
1403 if (fPreAlloc)
1404 {
1405 cbGTRounded = RT_ALIGN_64(pExtent->cGDEntries * pExtent->cGTEntries * sizeof(uint32_t), 512);
1406 cbOverhead = VMDK_SECTOR2BYTE(uStartSector) + cbGDRounded + cbGTRounded;
1407 }
1408 else
1409 {
1410 /* Use a dummy start sector for layout computation. */
1411 if (uStartSector == VMDK_GD_AT_END)
1412 uStartSector = 1;
1413 cbGTRounded = 0;
1414 cbOverhead = VMDK_SECTOR2BYTE(uStartSector) + cbGDRounded;
1415 }
1416
1417 /* For streamOptimized extents there is only one grain directory,
1418 * and for all others take redundant grain directory into account. */
1419 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
1420 {
1421 cbOverhead = RT_ALIGN_64(cbOverhead,
1422 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
1423 }
1424 else
1425 {
1426 cbOverhead += cbGDRounded + cbGTRounded;
1427 cbOverhead = RT_ALIGN_64(cbOverhead,
1428 VMDK_SECTOR2BYTE(pExtent->cSectorsPerGrain));
1429 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pExtent->pFile->pStorage, cbOverhead);
1430 }
1431
1432 if (RT_SUCCESS(rc))
1433 {
1434 pExtent->uAppendPosition = cbOverhead;
1435 pExtent->cOverheadSectors = VMDK_BYTE2SECTOR(cbOverhead);
1436
1437 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
1438 {
1439 pExtent->uSectorRGD = 0;
1440 pExtent->uSectorGD = uStartSector;
1441 }
1442 else
1443 {
1444 pExtent->uSectorRGD = uStartSector;
1445 pExtent->uSectorGD = uStartSector + VMDK_BYTE2SECTOR(cbGDRounded + cbGTRounded);
1446 }
1447
1448 rc = vmdkAllocStreamBuffers(pImage, pExtent);
1449 if (RT_SUCCESS(rc))
1450 {
1451 rc = vmdkAllocGrainDirectory(pImage, pExtent);
1452 if ( RT_SUCCESS(rc)
1453 && fPreAlloc)
1454 {
1455 uint32_t uGTSectorLE;
1456 uint64_t uOffsetSectors;
1457
1458 if (pExtent->pRGD)
1459 {
1460 uOffsetSectors = pExtent->uSectorRGD + VMDK_BYTE2SECTOR(cbGDRounded);
1461 for (i = 0; i < pExtent->cGDEntries; i++)
1462 {
1463 pExtent->pRGD[i] = uOffsetSectors;
1464 uGTSectorLE = RT_H2LE_U64(uOffsetSectors);
1465 /* Write the redundant grain directory entry to disk. */
1466 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
1467 VMDK_SECTOR2BYTE(pExtent->uSectorRGD) + i * sizeof(uGTSectorLE),
1468 &uGTSectorLE, sizeof(uGTSectorLE));
1469 if (RT_FAILURE(rc))
1470 {
1471 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write new redundant grain directory entry in '%s'"), pExtent->pszFullname);
1472 break;
1473 }
1474 uOffsetSectors += VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
1475 }
1476 }
1477
1478 if (RT_SUCCESS(rc))
1479 {
1480 uOffsetSectors = pExtent->uSectorGD + VMDK_BYTE2SECTOR(cbGDRounded);
1481 for (i = 0; i < pExtent->cGDEntries; i++)
1482 {
1483 pExtent->pGD[i] = uOffsetSectors;
1484 uGTSectorLE = RT_H2LE_U64(uOffsetSectors);
1485 /* Write the grain directory entry to disk. */
1486 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pExtent->pFile->pStorage,
1487 VMDK_SECTOR2BYTE(pExtent->uSectorGD) + i * sizeof(uGTSectorLE),
1488 &uGTSectorLE, sizeof(uGTSectorLE));
1489 if (RT_FAILURE(rc))
1490 {
1491 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot write new grain directory entry in '%s'"), pExtent->pszFullname);
1492 break;
1493 }
1494 uOffsetSectors += VMDK_BYTE2SECTOR(pExtent->cGTEntries * sizeof(uint32_t));
1495 }
1496 }
1497 }
1498 }
1499 }
1500
1501 if (RT_FAILURE(rc))
1502 vmdkFreeGrainDirectory(pExtent);
1503 return rc;
1504}
1505
1506/**
1507 * Unquotes the given string returning the result in a separate buffer.
1508 *
1509 * @returns VBox status code.
1510 * @param pImage The VMDK image state.
1511 * @param pszStr The string to unquote.
1512 * @param ppszUnquoted Where to store the return value, use RTMemTmpFree to
1513 * free.
1514 * @param ppszNext Where to store the pointer to any character following
1515 * the quoted value, optional.
1516 */
1517static int vmdkStringUnquote(PVMDKIMAGE pImage, const char *pszStr,
1518 char **ppszUnquoted, char **ppszNext)
1519{
1520 const char *pszStart = pszStr;
1521 char *pszQ;
1522 char *pszUnquoted;
1523
1524 /* Skip over whitespace. */
1525 while (*pszStr == ' ' || *pszStr == '\t')
1526 pszStr++;
1527
1528 if (*pszStr != '"')
1529 {
1530 pszQ = (char *)pszStr;
1531 while (*pszQ && *pszQ != ' ' && *pszQ != '\t')
1532 pszQ++;
1533 }
1534 else
1535 {
1536 pszStr++;
1537 pszQ = (char *)strchr(pszStr, '"');
1538 if (pszQ == NULL)
1539 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrectly quoted value in descriptor in '%s' (raw value %s)"),
1540 pImage->pszFilename, pszStart);
1541 }
1542
1543 pszUnquoted = (char *)RTMemTmpAlloc(pszQ - pszStr + 1);
1544 if (!pszUnquoted)
1545 return VERR_NO_MEMORY;
1546 memcpy(pszUnquoted, pszStr, pszQ - pszStr);
1547 pszUnquoted[pszQ - pszStr] = '\0';
1548 *ppszUnquoted = pszUnquoted;
1549 if (ppszNext)
1550 *ppszNext = pszQ + 1;
1551 return VINF_SUCCESS;
1552}
1553
1554static int vmdkDescInitStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1555 const char *pszLine)
1556{
1557 char *pEnd = pDescriptor->aLines[pDescriptor->cLines];
1558 ssize_t cbDiff = strlen(pszLine) + 1;
1559
1560 if ( pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1
1561 && pEnd - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff)
1562 return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
1563
1564 memcpy(pEnd, pszLine, cbDiff);
1565 pDescriptor->cLines++;
1566 pDescriptor->aLines[pDescriptor->cLines] = pEnd + cbDiff;
1567 pDescriptor->fDirty = true;
1568
1569 return VINF_SUCCESS;
1570}
1571
1572static bool vmdkDescGetStr(PVMDKDESCRIPTOR pDescriptor, unsigned uStart,
1573 const char *pszKey, const char **ppszValue)
1574{
1575 size_t cbKey = strlen(pszKey);
1576 const char *pszValue;
1577
1578 while (uStart != 0)
1579 {
1580 if (!strncmp(pDescriptor->aLines[uStart], pszKey, cbKey))
1581 {
1582 /* Key matches, check for a '=' (preceded by whitespace). */
1583 pszValue = pDescriptor->aLines[uStart] + cbKey;
1584 while (*pszValue == ' ' || *pszValue == '\t')
1585 pszValue++;
1586 if (*pszValue == '=')
1587 {
1588 *ppszValue = pszValue + 1;
1589 break;
1590 }
1591 }
1592 uStart = pDescriptor->aNextLines[uStart];
1593 }
1594 return !!uStart;
1595}
1596
1597static int vmdkDescSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1598 unsigned uStart,
1599 const char *pszKey, const char *pszValue)
1600{
1601 char *pszTmp = NULL; /* (MSC naturally cannot figure this isn't used uninitialized) */
1602 size_t cbKey = strlen(pszKey);
1603 unsigned uLast = 0;
1604
1605 while (uStart != 0)
1606 {
1607 if (!strncmp(pDescriptor->aLines[uStart], pszKey, cbKey))
1608 {
1609 /* Key matches, check for a '=' (preceded by whitespace). */
1610 pszTmp = pDescriptor->aLines[uStart] + cbKey;
1611 while (*pszTmp == ' ' || *pszTmp == '\t')
1612 pszTmp++;
1613 if (*pszTmp == '=')
1614 {
1615 pszTmp++;
1616 /** @todo r=bird: Doesn't skipping trailing blanks here just cause unecessary
1617 * bloat and potentially out of space error? */
1618 while (*pszTmp == ' ' || *pszTmp == '\t')
1619 pszTmp++;
1620 break;
1621 }
1622 }
1623 if (!pDescriptor->aNextLines[uStart])
1624 uLast = uStart;
1625 uStart = pDescriptor->aNextLines[uStart];
1626 }
1627 if (uStart)
1628 {
1629 if (pszValue)
1630 {
1631 /* Key already exists, replace existing value. */
1632 size_t cbOldVal = strlen(pszTmp);
1633 size_t cbNewVal = strlen(pszValue);
1634 ssize_t cbDiff = cbNewVal - cbOldVal;
1635 /* Check for buffer overflow. */
1636 if ( pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[0]
1637 > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff)
1638 return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
1639
1640 memmove(pszTmp + cbNewVal, pszTmp + cbOldVal,
1641 pDescriptor->aLines[pDescriptor->cLines] - pszTmp - cbOldVal);
1642 memcpy(pszTmp, pszValue, cbNewVal + 1);
1643 for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
1644 pDescriptor->aLines[i] += cbDiff;
1645 }
1646 else
1647 {
1648 memmove(pDescriptor->aLines[uStart], pDescriptor->aLines[uStart+1],
1649 pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[uStart+1] + 1);
1650 for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
1651 {
1652 pDescriptor->aLines[i-1] = pDescriptor->aLines[i];
1653 if (pDescriptor->aNextLines[i])
1654 pDescriptor->aNextLines[i-1] = pDescriptor->aNextLines[i] - 1;
1655 else
1656 pDescriptor->aNextLines[i-1] = 0;
1657 }
1658 pDescriptor->cLines--;
1659 /* Adjust starting line numbers of following descriptor sections. */
1660 if (uStart < pDescriptor->uFirstExtent)
1661 pDescriptor->uFirstExtent--;
1662 if (uStart < pDescriptor->uFirstDDB)
1663 pDescriptor->uFirstDDB--;
1664 }
1665 }
1666 else
1667 {
1668 /* Key doesn't exist, append after the last entry in this category. */
1669 if (!pszValue)
1670 {
1671 /* Key doesn't exist, and it should be removed. Simply a no-op. */
1672 return VINF_SUCCESS;
1673 }
1674 cbKey = strlen(pszKey);
1675 size_t cbValue = strlen(pszValue);
1676 ssize_t cbDiff = cbKey + 1 + cbValue + 1;
1677 /* Check for buffer overflow. */
1678 if ( (pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1)
1679 || ( pDescriptor->aLines[pDescriptor->cLines]
1680 - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff))
1681 return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
1682 for (unsigned i = pDescriptor->cLines + 1; i > uLast + 1; i--)
1683 {
1684 pDescriptor->aLines[i] = pDescriptor->aLines[i - 1];
1685 if (pDescriptor->aNextLines[i - 1])
1686 pDescriptor->aNextLines[i] = pDescriptor->aNextLines[i - 1] + 1;
1687 else
1688 pDescriptor->aNextLines[i] = 0;
1689 }
1690 uStart = uLast + 1;
1691 pDescriptor->aNextLines[uLast] = uStart;
1692 pDescriptor->aNextLines[uStart] = 0;
1693 pDescriptor->cLines++;
1694 pszTmp = pDescriptor->aLines[uStart];
1695 memmove(pszTmp + cbDiff, pszTmp,
1696 pDescriptor->aLines[pDescriptor->cLines] - pszTmp);
1697 memcpy(pDescriptor->aLines[uStart], pszKey, cbKey);
1698 pDescriptor->aLines[uStart][cbKey] = '=';
1699 memcpy(pDescriptor->aLines[uStart] + cbKey + 1, pszValue, cbValue + 1);
1700 for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
1701 pDescriptor->aLines[i] += cbDiff;
1702
1703 /* Adjust starting line numbers of following descriptor sections. */
1704 if (uStart <= pDescriptor->uFirstExtent)
1705 pDescriptor->uFirstExtent++;
1706 if (uStart <= pDescriptor->uFirstDDB)
1707 pDescriptor->uFirstDDB++;
1708 }
1709 pDescriptor->fDirty = true;
1710 return VINF_SUCCESS;
1711}
1712
1713static int vmdkDescBaseGetU32(PVMDKDESCRIPTOR pDescriptor, const char *pszKey,
1714 uint32_t *puValue)
1715{
1716 const char *pszValue;
1717
1718 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDesc, pszKey,
1719 &pszValue))
1720 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1721 return RTStrToUInt32Ex(pszValue, NULL, 10, puValue);
1722}
1723
1724/**
1725 * Returns the value of the given key as a string allocating the necessary memory.
1726 *
1727 * @returns VBox status code.
1728 * @retval VERR_VD_VMDK_VALUE_NOT_FOUND if the value could not be found.
1729 * @param pImage The VMDK image state.
1730 * @param pDescriptor The descriptor to fetch the value from.
1731 * @param pszKey The key to get the value from.
1732 * @param ppszValue Where to store the return value, use RTMemTmpFree to
1733 * free.
1734 */
1735static int vmdkDescBaseGetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1736 const char *pszKey, char **ppszValue)
1737{
1738 const char *pszValue;
1739 char *pszValueUnquoted;
1740
1741 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDesc, pszKey,
1742 &pszValue))
1743 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1744 int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
1745 if (RT_FAILURE(rc))
1746 return rc;
1747 *ppszValue = pszValueUnquoted;
1748 return rc;
1749}
1750
1751static int vmdkDescBaseSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1752 const char *pszKey, const char *pszValue)
1753{
1754 char *pszValueQuoted;
1755
1756 RTStrAPrintf(&pszValueQuoted, "\"%s\"", pszValue);
1757 if (!pszValueQuoted)
1758 return VERR_NO_STR_MEMORY;
1759 int rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc, pszKey,
1760 pszValueQuoted);
1761 RTStrFree(pszValueQuoted);
1762 return rc;
1763}
1764
1765static void vmdkDescExtRemoveDummy(PVMDKIMAGE pImage,
1766 PVMDKDESCRIPTOR pDescriptor)
1767{
1768 RT_NOREF1(pImage);
1769 unsigned uEntry = pDescriptor->uFirstExtent;
1770 ssize_t cbDiff;
1771
1772 if (!uEntry)
1773 return;
1774
1775 cbDiff = strlen(pDescriptor->aLines[uEntry]) + 1;
1776 /* Move everything including \0 in the entry marking the end of buffer. */
1777 memmove(pDescriptor->aLines[uEntry], pDescriptor->aLines[uEntry + 1],
1778 pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[uEntry + 1] + 1);
1779 for (unsigned i = uEntry + 1; i <= pDescriptor->cLines; i++)
1780 {
1781 pDescriptor->aLines[i - 1] = pDescriptor->aLines[i] - cbDiff;
1782 if (pDescriptor->aNextLines[i])
1783 pDescriptor->aNextLines[i - 1] = pDescriptor->aNextLines[i] - 1;
1784 else
1785 pDescriptor->aNextLines[i - 1] = 0;
1786 }
1787 pDescriptor->cLines--;
1788 if (pDescriptor->uFirstDDB)
1789 pDescriptor->uFirstDDB--;
1790
1791 return;
1792}
1793
1794static void vmdkDescExtRemoveByLine(PVMDKIMAGE pImage,
1795 PVMDKDESCRIPTOR pDescriptor, unsigned uLine)
1796{
1797 RT_NOREF1(pImage);
1798 unsigned uEntry = uLine;
1799 ssize_t cbDiff;
1800 if (!uEntry)
1801 return;
1802 cbDiff = strlen(pDescriptor->aLines[uEntry]) + 1;
1803 /* Move everything including \0 in the entry marking the end of buffer. */
1804 memmove(pDescriptor->aLines[uEntry], pDescriptor->aLines[uEntry + 1],
1805 pDescriptor->aLines[pDescriptor->cLines] - pDescriptor->aLines[uEntry + 1] + 1);
1806 for (unsigned i = uEntry; i <= pDescriptor->cLines; i++)
1807 {
1808 if (i != uEntry)
1809 pDescriptor->aLines[i - 1] = pDescriptor->aLines[i] - cbDiff;
1810 if (pDescriptor->aNextLines[i])
1811 pDescriptor->aNextLines[i - 1] = pDescriptor->aNextLines[i] - 1;
1812 else
1813 pDescriptor->aNextLines[i - 1] = 0;
1814 }
1815 pDescriptor->cLines--;
1816 if (pDescriptor->uFirstDDB)
1817 pDescriptor->uFirstDDB--;
1818 return;
1819}
1820
1821static int vmdkDescExtInsert(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1822 VMDKACCESS enmAccess, uint64_t cNominalSectors,
1823 VMDKETYPE enmType, const char *pszBasename,
1824 uint64_t uSectorOffset)
1825{
1826 static const char *apszAccess[] = { "NOACCESS", "RDONLY", "RW" };
1827 static const char *apszType[] = { "", "SPARSE", "FLAT", "ZERO", "VMFS" };
1828 char *pszTmp;
1829 unsigned uStart = pDescriptor->uFirstExtent, uLast = 0;
1830 char szExt[1024];
1831 ssize_t cbDiff;
1832
1833 Assert((unsigned)enmAccess < RT_ELEMENTS(apszAccess));
1834 Assert((unsigned)enmType < RT_ELEMENTS(apszType));
1835
1836 /* Find last entry in extent description. */
1837 while (uStart)
1838 {
1839 if (!pDescriptor->aNextLines[uStart])
1840 uLast = uStart;
1841 uStart = pDescriptor->aNextLines[uStart];
1842 }
1843
1844 if (enmType == VMDKETYPE_ZERO)
1845 {
1846 RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s ", apszAccess[enmAccess],
1847 cNominalSectors, apszType[enmType]);
1848 }
1849 else if (enmType == VMDKETYPE_FLAT)
1850 {
1851 RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s \"%s\" %llu",
1852 apszAccess[enmAccess], cNominalSectors,
1853 apszType[enmType], pszBasename, uSectorOffset);
1854 }
1855 else
1856 {
1857 RTStrPrintf(szExt, sizeof(szExt), "%s %llu %s \"%s\"",
1858 apszAccess[enmAccess], cNominalSectors,
1859 apszType[enmType], pszBasename);
1860 }
1861 cbDiff = strlen(szExt) + 1;
1862
1863 /* Check for buffer overflow. */
1864 if ( (pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1)
1865 || ( pDescriptor->aLines[pDescriptor->cLines]
1866 - pDescriptor->aLines[0] > (ptrdiff_t)pDescriptor->cbDescAlloc - cbDiff))
1867 {
1868 if ((pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_SPLIT_2G)
1869 && !(pDescriptor->cLines >= VMDK_DESCRIPTOR_LINES_MAX - 1))
1870 {
1871 pImage->cbDescAlloc *= 2;
1872 pDescriptor->cbDescAlloc *= 2;
1873 }
1874 else
1875 return vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
1876 }
1877
1878 for (unsigned i = pDescriptor->cLines + 1; i > uLast + 1; i--)
1879 {
1880 pDescriptor->aLines[i] = pDescriptor->aLines[i - 1];
1881 if (pDescriptor->aNextLines[i - 1])
1882 pDescriptor->aNextLines[i] = pDescriptor->aNextLines[i - 1] + 1;
1883 else
1884 pDescriptor->aNextLines[i] = 0;
1885 }
1886 uStart = uLast + 1;
1887 pDescriptor->aNextLines[uLast] = uStart;
1888 pDescriptor->aNextLines[uStart] = 0;
1889 pDescriptor->cLines++;
1890 pszTmp = pDescriptor->aLines[uStart];
1891 memmove(pszTmp + cbDiff, pszTmp,
1892 pDescriptor->aLines[pDescriptor->cLines] - pszTmp);
1893 memcpy(pDescriptor->aLines[uStart], szExt, cbDiff);
1894 for (unsigned i = uStart + 1; i <= pDescriptor->cLines; i++)
1895 pDescriptor->aLines[i] += cbDiff;
1896
1897 /* Adjust starting line numbers of following descriptor sections. */
1898 if (uStart <= pDescriptor->uFirstDDB)
1899 pDescriptor->uFirstDDB++;
1900
1901 pDescriptor->fDirty = true;
1902 return VINF_SUCCESS;
1903}
1904
1905/**
1906 * Returns the value of the given key from the DDB as a string allocating
1907 * the necessary memory.
1908 *
1909 * @returns VBox status code.
1910 * @retval VERR_VD_VMDK_VALUE_NOT_FOUND if the value could not be found.
1911 * @param pImage The VMDK image state.
1912 * @param pDescriptor The descriptor to fetch the value from.
1913 * @param pszKey The key to get the value from.
1914 * @param ppszValue Where to store the return value, use RTMemTmpFree to
1915 * free.
1916 */
1917static int vmdkDescDDBGetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1918 const char *pszKey, char **ppszValue)
1919{
1920 const char *pszValue;
1921 char *pszValueUnquoted;
1922
1923 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey,
1924 &pszValue))
1925 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1926 int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
1927 if (RT_FAILURE(rc))
1928 return rc;
1929 *ppszValue = pszValueUnquoted;
1930 return rc;
1931}
1932
1933static int vmdkDescDDBGetU32(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1934 const char *pszKey, uint32_t *puValue)
1935{
1936 const char *pszValue;
1937 char *pszValueUnquoted;
1938
1939 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey,
1940 &pszValue))
1941 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1942 int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
1943 if (RT_FAILURE(rc))
1944 return rc;
1945 rc = RTStrToUInt32Ex(pszValueUnquoted, NULL, 10, puValue);
1946 RTMemTmpFree(pszValueUnquoted);
1947 return rc;
1948}
1949
1950static int vmdkDescDDBGetUuid(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1951 const char *pszKey, PRTUUID pUuid)
1952{
1953 const char *pszValue;
1954 char *pszValueUnquoted;
1955
1956 if (!vmdkDescGetStr(pDescriptor, pDescriptor->uFirstDDB, pszKey,
1957 &pszValue))
1958 return VERR_VD_VMDK_VALUE_NOT_FOUND;
1959 int rc = vmdkStringUnquote(pImage, pszValue, &pszValueUnquoted, NULL);
1960 if (RT_FAILURE(rc))
1961 return rc;
1962 rc = RTUuidFromStr(pUuid, pszValueUnquoted);
1963 RTMemTmpFree(pszValueUnquoted);
1964 return rc;
1965}
1966
1967static int vmdkDescDDBSetStr(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1968 const char *pszKey, const char *pszVal)
1969{
1970 int rc;
1971 char *pszValQuoted;
1972
1973 if (pszVal)
1974 {
1975 RTStrAPrintf(&pszValQuoted, "\"%s\"", pszVal);
1976 if (!pszValQuoted)
1977 return VERR_NO_STR_MEMORY;
1978 }
1979 else
1980 pszValQuoted = NULL;
1981 rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey,
1982 pszValQuoted);
1983 if (pszValQuoted)
1984 RTStrFree(pszValQuoted);
1985 return rc;
1986}
1987
1988static int vmdkDescDDBSetUuid(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
1989 const char *pszKey, PCRTUUID pUuid)
1990{
1991 char *pszUuid;
1992
1993 RTStrAPrintf(&pszUuid, "\"%RTuuid\"", pUuid);
1994 if (!pszUuid)
1995 return VERR_NO_STR_MEMORY;
1996 int rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey,
1997 pszUuid);
1998 RTStrFree(pszUuid);
1999 return rc;
2000}
2001
2002static int vmdkDescDDBSetU32(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDescriptor,
2003 const char *pszKey, uint32_t uValue)
2004{
2005 char *pszValue;
2006
2007 RTStrAPrintf(&pszValue, "\"%d\"", uValue);
2008 if (!pszValue)
2009 return VERR_NO_STR_MEMORY;
2010 int rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDDB, pszKey,
2011 pszValue);
2012 RTStrFree(pszValue);
2013 return rc;
2014}
2015
2016/**
2017 * Splits the descriptor data into individual lines checking for correct line
2018 * endings and descriptor size.
2019 *
2020 * @returns VBox status code.
2021 * @param pImage The image instance.
2022 * @param pDesc The descriptor.
2023 * @param pszTmp The raw descriptor data from the image.
2024 */
2025static int vmdkDescSplitLines(PVMDKIMAGE pImage, PVMDKDESCRIPTOR pDesc, char *pszTmp)
2026{
2027 unsigned cLine = 0;
2028 int rc = VINF_SUCCESS;
2029
2030 while ( RT_SUCCESS(rc)
2031 && *pszTmp != '\0')
2032 {
2033 pDesc->aLines[cLine++] = pszTmp;
2034 if (cLine >= VMDK_DESCRIPTOR_LINES_MAX)
2035 {
2036 vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: descriptor too big in '%s'"), pImage->pszFilename);
2037 rc = VERR_VD_VMDK_INVALID_HEADER;
2038 break;
2039 }
2040
2041 while (*pszTmp != '\0' && *pszTmp != '\n')
2042 {
2043 if (*pszTmp == '\r')
2044 {
2045 if (*(pszTmp + 1) != '\n')
2046 {
2047 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: unsupported end of line in descriptor in '%s'"), pImage->pszFilename);
2048 break;
2049 }
2050 else
2051 {
2052 /* Get rid of CR character. */
2053 *pszTmp = '\0';
2054 }
2055 }
2056 pszTmp++;
2057 }
2058
2059 if (RT_FAILURE(rc))
2060 break;
2061
2062 /* Get rid of LF character. */
2063 if (*pszTmp == '\n')
2064 {
2065 *pszTmp = '\0';
2066 pszTmp++;
2067 }
2068 }
2069
2070 if (RT_SUCCESS(rc))
2071 {
2072 pDesc->cLines = cLine;
2073 /* Pointer right after the end of the used part of the buffer. */
2074 pDesc->aLines[cLine] = pszTmp;
2075 }
2076
2077 return rc;
2078}
2079
2080static int vmdkPreprocessDescriptor(PVMDKIMAGE pImage, char *pDescData,
2081 size_t cbDescData, PVMDKDESCRIPTOR pDescriptor)
2082{
2083 pDescriptor->cbDescAlloc = cbDescData;
2084 int rc = vmdkDescSplitLines(pImage, pDescriptor, pDescData);
2085 if (RT_SUCCESS(rc))
2086 {
2087 if ( strcmp(pDescriptor->aLines[0], "# Disk DescriptorFile")
2088 && strcmp(pDescriptor->aLines[0], "# Disk Descriptor File")
2089 && strcmp(pDescriptor->aLines[0], "#Disk Descriptor File")
2090 && strcmp(pDescriptor->aLines[0], "#Disk DescriptorFile"))
2091 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2092 N_("VMDK: descriptor does not start as expected in '%s'"), pImage->pszFilename);
2093 else
2094 {
2095 unsigned uLastNonEmptyLine = 0;
2096
2097 /* Initialize those, because we need to be able to reopen an image. */
2098 pDescriptor->uFirstDesc = 0;
2099 pDescriptor->uFirstExtent = 0;
2100 pDescriptor->uFirstDDB = 0;
2101 for (unsigned i = 0; i < pDescriptor->cLines; i++)
2102 {
2103 if (*pDescriptor->aLines[i] != '#' && *pDescriptor->aLines[i] != '\0')
2104 {
2105 if ( !strncmp(pDescriptor->aLines[i], "RW", 2)
2106 || !strncmp(pDescriptor->aLines[i], "RDONLY", 6)
2107 || !strncmp(pDescriptor->aLines[i], "NOACCESS", 8) )
2108 {
2109 /* An extent descriptor. */
2110 if (!pDescriptor->uFirstDesc || pDescriptor->uFirstDDB)
2111 {
2112 /* Incorrect ordering of entries. */
2113 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2114 N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
2115 break;
2116 }
2117 if (!pDescriptor->uFirstExtent)
2118 {
2119 pDescriptor->uFirstExtent = i;
2120 uLastNonEmptyLine = 0;
2121 }
2122 }
2123 else if (!strncmp(pDescriptor->aLines[i], "ddb.", 4))
2124 {
2125 /* A disk database entry. */
2126 if (!pDescriptor->uFirstDesc || !pDescriptor->uFirstExtent)
2127 {
2128 /* Incorrect ordering of entries. */
2129 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2130 N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
2131 break;
2132 }
2133 if (!pDescriptor->uFirstDDB)
2134 {
2135 pDescriptor->uFirstDDB = i;
2136 uLastNonEmptyLine = 0;
2137 }
2138 }
2139 else
2140 {
2141 /* A normal entry. */
2142 if (pDescriptor->uFirstExtent || pDescriptor->uFirstDDB)
2143 {
2144 /* Incorrect ordering of entries. */
2145 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2146 N_("VMDK: incorrect ordering of entries in descriptor in '%s'"), pImage->pszFilename);
2147 break;
2148 }
2149 if (!pDescriptor->uFirstDesc)
2150 {
2151 pDescriptor->uFirstDesc = i;
2152 uLastNonEmptyLine = 0;
2153 }
2154 }
2155 if (uLastNonEmptyLine)
2156 pDescriptor->aNextLines[uLastNonEmptyLine] = i;
2157 uLastNonEmptyLine = i;
2158 }
2159 }
2160 }
2161 }
2162
2163 return rc;
2164}
2165
2166static int vmdkDescSetPCHSGeometry(PVMDKIMAGE pImage,
2167 PCVDGEOMETRY pPCHSGeometry)
2168{
2169 int rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2170 VMDK_DDB_GEO_PCHS_CYLINDERS,
2171 pPCHSGeometry->cCylinders);
2172 if (RT_FAILURE(rc))
2173 return rc;
2174 rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2175 VMDK_DDB_GEO_PCHS_HEADS,
2176 pPCHSGeometry->cHeads);
2177 if (RT_FAILURE(rc))
2178 return rc;
2179 rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2180 VMDK_DDB_GEO_PCHS_SECTORS,
2181 pPCHSGeometry->cSectors);
2182 return rc;
2183}
2184
2185static int vmdkDescSetLCHSGeometry(PVMDKIMAGE pImage,
2186 PCVDGEOMETRY pLCHSGeometry)
2187{
2188 int rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2189 VMDK_DDB_GEO_LCHS_CYLINDERS,
2190 pLCHSGeometry->cCylinders);
2191 if (RT_FAILURE(rc))
2192 return rc;
2193 rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2194 VMDK_DDB_GEO_LCHS_HEADS,
2195
2196 pLCHSGeometry->cHeads);
2197 if (RT_FAILURE(rc))
2198 return rc;
2199 rc = vmdkDescDDBSetU32(pImage, &pImage->Descriptor,
2200 VMDK_DDB_GEO_LCHS_SECTORS,
2201 pLCHSGeometry->cSectors);
2202 return rc;
2203}
2204
2205static int vmdkCreateDescriptor(PVMDKIMAGE pImage, char *pDescData,
2206 size_t cbDescData, PVMDKDESCRIPTOR pDescriptor)
2207{
2208 pDescriptor->uFirstDesc = 0;
2209 pDescriptor->uFirstExtent = 0;
2210 pDescriptor->uFirstDDB = 0;
2211 pDescriptor->cLines = 0;
2212 pDescriptor->cbDescAlloc = cbDescData;
2213 pDescriptor->fDirty = false;
2214 pDescriptor->aLines[pDescriptor->cLines] = pDescData;
2215 memset(pDescriptor->aNextLines, '\0', sizeof(pDescriptor->aNextLines));
2216
2217 int rc = vmdkDescInitStr(pImage, pDescriptor, "# Disk DescriptorFile");
2218 if (RT_SUCCESS(rc))
2219 rc = vmdkDescInitStr(pImage, pDescriptor, "version=1");
2220 if (RT_SUCCESS(rc))
2221 {
2222 pDescriptor->uFirstDesc = pDescriptor->cLines - 1;
2223 rc = vmdkDescInitStr(pImage, pDescriptor, "");
2224 }
2225 if (RT_SUCCESS(rc))
2226 rc = vmdkDescInitStr(pImage, pDescriptor, "# Extent description");
2227 if (RT_SUCCESS(rc))
2228 rc = vmdkDescInitStr(pImage, pDescriptor, "NOACCESS 0 ZERO ");
2229 if (RT_SUCCESS(rc))
2230 {
2231 pDescriptor->uFirstExtent = pDescriptor->cLines - 1;
2232 rc = vmdkDescInitStr(pImage, pDescriptor, "");
2233 }
2234 if (RT_SUCCESS(rc))
2235 {
2236 /* The trailing space is created by VMware, too. */
2237 rc = vmdkDescInitStr(pImage, pDescriptor, "# The disk Data Base ");
2238 }
2239 if (RT_SUCCESS(rc))
2240 rc = vmdkDescInitStr(pImage, pDescriptor, "#DDB");
2241 if (RT_SUCCESS(rc))
2242 rc = vmdkDescInitStr(pImage, pDescriptor, "");
2243 if (RT_SUCCESS(rc))
2244 rc = vmdkDescInitStr(pImage, pDescriptor, "ddb.virtualHWVersion = \"4\"");
2245 if (RT_SUCCESS(rc))
2246 {
2247 pDescriptor->uFirstDDB = pDescriptor->cLines - 1;
2248
2249 /* Now that the framework is in place, use the normal functions to insert
2250 * the remaining keys. */
2251 char szBuf[9];
2252 RTStrPrintf(szBuf, sizeof(szBuf), "%08x", RTRandU32());
2253 rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc,
2254 "CID", szBuf);
2255 }
2256 if (RT_SUCCESS(rc))
2257 rc = vmdkDescSetStr(pImage, pDescriptor, pDescriptor->uFirstDesc,
2258 "parentCID", "ffffffff");
2259 if (RT_SUCCESS(rc))
2260 rc = vmdkDescDDBSetStr(pImage, pDescriptor, "ddb.adapterType", "ide");
2261
2262 return rc;
2263}
2264
2265static int vmdkParseDescriptor(PVMDKIMAGE pImage, char *pDescData, size_t cbDescData)
2266{
2267 int rc;
2268 unsigned cExtents;
2269 unsigned uLine;
2270 unsigned i;
2271
2272 rc = vmdkPreprocessDescriptor(pImage, pDescData, cbDescData,
2273 &pImage->Descriptor);
2274 if (RT_FAILURE(rc))
2275 return rc;
2276
2277 /* Check version, must be 1. */
2278 uint32_t uVersion;
2279 rc = vmdkDescBaseGetU32(&pImage->Descriptor, "version", &uVersion);
2280 if (RT_FAILURE(rc))
2281 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error finding key 'version' in descriptor in '%s'"), pImage->pszFilename);
2282 if (uVersion != 1)
2283 return vdIfError(pImage->pIfError, VERR_VD_VMDK_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VMDK: unsupported format version in descriptor in '%s'"), pImage->pszFilename);
2284
2285 /* Get image creation type and determine image flags. */
2286 char *pszCreateType = NULL; /* initialized to make gcc shut up */
2287 rc = vmdkDescBaseGetStr(pImage, &pImage->Descriptor, "createType",
2288 &pszCreateType);
2289 if (RT_FAILURE(rc))
2290 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot get image type from descriptor in '%s'"), pImage->pszFilename);
2291 if ( !strcmp(pszCreateType, "twoGbMaxExtentSparse")
2292 || !strcmp(pszCreateType, "twoGbMaxExtentFlat"))
2293 pImage->uImageFlags |= VD_VMDK_IMAGE_FLAGS_SPLIT_2G;
2294 else if ( !strcmp(pszCreateType, "partitionedDevice")
2295 || !strcmp(pszCreateType, "fullDevice"))
2296 pImage->uImageFlags |= VD_VMDK_IMAGE_FLAGS_RAWDISK;
2297 else if (!strcmp(pszCreateType, "streamOptimized"))
2298 pImage->uImageFlags |= VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED;
2299 else if (!strcmp(pszCreateType, "vmfs"))
2300 pImage->uImageFlags |= VD_IMAGE_FLAGS_FIXED | VD_VMDK_IMAGE_FLAGS_ESX;
2301 RTMemTmpFree(pszCreateType);
2302
2303 /* Count the number of extent config entries. */
2304 for (uLine = pImage->Descriptor.uFirstExtent, cExtents = 0;
2305 uLine != 0;
2306 uLine = pImage->Descriptor.aNextLines[uLine], cExtents++)
2307 /* nothing */;
2308
2309 if (!pImage->pDescData && cExtents != 1)
2310 {
2311 /* Monolithic image, must have only one extent (already opened). */
2312 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: monolithic image may only have one extent in '%s'"), pImage->pszFilename);
2313 }
2314
2315 if (pImage->pDescData)
2316 {
2317 /* Non-monolithic image, extents need to be allocated. */
2318 rc = vmdkCreateExtents(pImage, cExtents);
2319 if (RT_FAILURE(rc))
2320 return rc;
2321 }
2322
2323 for (i = 0, uLine = pImage->Descriptor.uFirstExtent;
2324 i < cExtents; i++, uLine = pImage->Descriptor.aNextLines[uLine])
2325 {
2326 char *pszLine = pImage->Descriptor.aLines[uLine];
2327
2328 /* Access type of the extent. */
2329 if (!strncmp(pszLine, "RW", 2))
2330 {
2331 pImage->pExtents[i].enmAccess = VMDKACCESS_READWRITE;
2332 pszLine += 2;
2333 }
2334 else if (!strncmp(pszLine, "RDONLY", 6))
2335 {
2336 pImage->pExtents[i].enmAccess = VMDKACCESS_READONLY;
2337 pszLine += 6;
2338 }
2339 else if (!strncmp(pszLine, "NOACCESS", 8))
2340 {
2341 pImage->pExtents[i].enmAccess = VMDKACCESS_NOACCESS;
2342 pszLine += 8;
2343 }
2344 else
2345 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2346 if (*pszLine++ != ' ')
2347 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2348
2349 /* Nominal size of the extent. */
2350 rc = RTStrToUInt64Ex(pszLine, &pszLine, 10,
2351 &pImage->pExtents[i].cNominalSectors);
2352 if (RT_FAILURE(rc))
2353 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2354 if (*pszLine++ != ' ')
2355 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2356
2357 /* Type of the extent. */
2358 if (!strncmp(pszLine, "SPARSE", 6))
2359 {
2360 pImage->pExtents[i].enmType = VMDKETYPE_HOSTED_SPARSE;
2361 pszLine += 6;
2362 }
2363 else if (!strncmp(pszLine, "FLAT", 4))
2364 {
2365 pImage->pExtents[i].enmType = VMDKETYPE_FLAT;
2366 pszLine += 4;
2367 }
2368 else if (!strncmp(pszLine, "ZERO", 4))
2369 {
2370 pImage->pExtents[i].enmType = VMDKETYPE_ZERO;
2371 pszLine += 4;
2372 }
2373 else if (!strncmp(pszLine, "VMFS", 4))
2374 {
2375 pImage->pExtents[i].enmType = VMDKETYPE_VMFS;
2376 pszLine += 4;
2377 }
2378 else
2379 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2380
2381 if (pImage->pExtents[i].enmType == VMDKETYPE_ZERO)
2382 {
2383 /* This one has no basename or offset. */
2384 if (*pszLine == ' ')
2385 pszLine++;
2386 if (*pszLine != '\0')
2387 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2388 pImage->pExtents[i].pszBasename = NULL;
2389 }
2390 else
2391 {
2392 /* All other extent types have basename and optional offset. */
2393 if (*pszLine++ != ' ')
2394 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2395
2396 /* Basename of the image. Surrounded by quotes. */
2397 char *pszBasename;
2398 rc = vmdkStringUnquote(pImage, pszLine, &pszBasename, &pszLine);
2399 if (RT_FAILURE(rc))
2400 return rc;
2401 pImage->pExtents[i].pszBasename = pszBasename;
2402 if (*pszLine == ' ')
2403 {
2404 pszLine++;
2405 if (*pszLine != '\0')
2406 {
2407 /* Optional offset in extent specified. */
2408 rc = RTStrToUInt64Ex(pszLine, &pszLine, 10,
2409 &pImage->pExtents[i].uSectorOffset);
2410 if (RT_FAILURE(rc))
2411 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2412 }
2413 }
2414
2415 if (*pszLine != '\0')
2416 return vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: parse error in extent description in '%s'"), pImage->pszFilename);
2417 }
2418 }
2419
2420 /* Determine PCHS geometry (autogenerate if necessary). */
2421 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2422 VMDK_DDB_GEO_PCHS_CYLINDERS,
2423 &pImage->PCHSGeometry.cCylinders);
2424 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2425 pImage->PCHSGeometry.cCylinders = 0;
2426 else if (RT_FAILURE(rc))
2427 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting PCHS geometry from extent description in '%s'"), pImage->pszFilename);
2428 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2429 VMDK_DDB_GEO_PCHS_HEADS,
2430 &pImage->PCHSGeometry.cHeads);
2431 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2432 pImage->PCHSGeometry.cHeads = 0;
2433 else if (RT_FAILURE(rc))
2434 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting PCHS geometry from extent description in '%s'"), pImage->pszFilename);
2435 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2436 VMDK_DDB_GEO_PCHS_SECTORS,
2437 &pImage->PCHSGeometry.cSectors);
2438 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2439 pImage->PCHSGeometry.cSectors = 0;
2440 else if (RT_FAILURE(rc))
2441 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting PCHS geometry from extent description in '%s'"), pImage->pszFilename);
2442 if ( pImage->PCHSGeometry.cCylinders == 0
2443 || pImage->PCHSGeometry.cHeads == 0
2444 || pImage->PCHSGeometry.cHeads > 16
2445 || pImage->PCHSGeometry.cSectors == 0
2446 || pImage->PCHSGeometry.cSectors > 63)
2447 {
2448 /* Mark PCHS geometry as not yet valid (can't do the calculation here
2449 * as the total image size isn't known yet). */
2450 pImage->PCHSGeometry.cCylinders = 0;
2451 pImage->PCHSGeometry.cHeads = 16;
2452 pImage->PCHSGeometry.cSectors = 63;
2453 }
2454
2455 /* Determine LCHS geometry (set to 0 if not specified). */
2456 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2457 VMDK_DDB_GEO_LCHS_CYLINDERS,
2458 &pImage->LCHSGeometry.cCylinders);
2459 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2460 pImage->LCHSGeometry.cCylinders = 0;
2461 else if (RT_FAILURE(rc))
2462 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting LCHS geometry from extent description in '%s'"), pImage->pszFilename);
2463 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2464 VMDK_DDB_GEO_LCHS_HEADS,
2465 &pImage->LCHSGeometry.cHeads);
2466 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2467 pImage->LCHSGeometry.cHeads = 0;
2468 else if (RT_FAILURE(rc))
2469 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting LCHS geometry from extent description in '%s'"), pImage->pszFilename);
2470 rc = vmdkDescDDBGetU32(pImage, &pImage->Descriptor,
2471 VMDK_DDB_GEO_LCHS_SECTORS,
2472 &pImage->LCHSGeometry.cSectors);
2473 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2474 pImage->LCHSGeometry.cSectors = 0;
2475 else if (RT_FAILURE(rc))
2476 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting LCHS geometry from extent description in '%s'"), pImage->pszFilename);
2477 if ( pImage->LCHSGeometry.cCylinders == 0
2478 || pImage->LCHSGeometry.cHeads == 0
2479 || pImage->LCHSGeometry.cSectors == 0)
2480 {
2481 pImage->LCHSGeometry.cCylinders = 0;
2482 pImage->LCHSGeometry.cHeads = 0;
2483 pImage->LCHSGeometry.cSectors = 0;
2484 }
2485
2486 /* Get image UUID. */
2487 rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor, VMDK_DDB_IMAGE_UUID,
2488 &pImage->ImageUuid);
2489 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2490 {
2491 /* Image without UUID. Probably created by VMware and not yet used
2492 * by VirtualBox. Can only be added for images opened in read/write
2493 * mode, so don't bother producing a sensible UUID otherwise. */
2494 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2495 RTUuidClear(&pImage->ImageUuid);
2496 else
2497 {
2498 rc = RTUuidCreate(&pImage->ImageUuid);
2499 if (RT_FAILURE(rc))
2500 return rc;
2501 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
2502 VMDK_DDB_IMAGE_UUID, &pImage->ImageUuid);
2503 if (RT_FAILURE(rc))
2504 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing image UUID in descriptor in '%s'"), pImage->pszFilename);
2505 }
2506 }
2507 else if (RT_FAILURE(rc))
2508 return rc;
2509
2510 /* Get image modification UUID. */
2511 rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor,
2512 VMDK_DDB_MODIFICATION_UUID,
2513 &pImage->ModificationUuid);
2514 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2515 {
2516 /* Image without UUID. Probably created by VMware and not yet used
2517 * by VirtualBox. Can only be added for images opened in read/write
2518 * mode, so don't bother producing a sensible UUID otherwise. */
2519 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2520 RTUuidClear(&pImage->ModificationUuid);
2521 else
2522 {
2523 rc = RTUuidCreate(&pImage->ModificationUuid);
2524 if (RT_FAILURE(rc))
2525 return rc;
2526 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
2527 VMDK_DDB_MODIFICATION_UUID,
2528 &pImage->ModificationUuid);
2529 if (RT_FAILURE(rc))
2530 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing image modification UUID in descriptor in '%s'"), pImage->pszFilename);
2531 }
2532 }
2533 else if (RT_FAILURE(rc))
2534 return rc;
2535
2536 /* Get UUID of parent image. */
2537 rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor, VMDK_DDB_PARENT_UUID,
2538 &pImage->ParentUuid);
2539 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2540 {
2541 /* Image without UUID. Probably created by VMware and not yet used
2542 * by VirtualBox. Can only be added for images opened in read/write
2543 * mode, so don't bother producing a sensible UUID otherwise. */
2544 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2545 RTUuidClear(&pImage->ParentUuid);
2546 else
2547 {
2548 rc = RTUuidClear(&pImage->ParentUuid);
2549 if (RT_FAILURE(rc))
2550 return rc;
2551 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
2552 VMDK_DDB_PARENT_UUID, &pImage->ParentUuid);
2553 if (RT_FAILURE(rc))
2554 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent UUID in descriptor in '%s'"), pImage->pszFilename);
2555 }
2556 }
2557 else if (RT_FAILURE(rc))
2558 return rc;
2559
2560 /* Get parent image modification UUID. */
2561 rc = vmdkDescDDBGetUuid(pImage, &pImage->Descriptor,
2562 VMDK_DDB_PARENT_MODIFICATION_UUID,
2563 &pImage->ParentModificationUuid);
2564 if (rc == VERR_VD_VMDK_VALUE_NOT_FOUND)
2565 {
2566 /* Image without UUID. Probably created by VMware and not yet used
2567 * by VirtualBox. Can only be added for images opened in read/write
2568 * mode, so don't bother producing a sensible UUID otherwise. */
2569 if (pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2570 RTUuidClear(&pImage->ParentModificationUuid);
2571 else
2572 {
2573 RTUuidClear(&pImage->ParentModificationUuid);
2574 rc = vmdkDescDDBSetUuid(pImage, &pImage->Descriptor,
2575 VMDK_DDB_PARENT_MODIFICATION_UUID,
2576 &pImage->ParentModificationUuid);
2577 if (RT_FAILURE(rc))
2578 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error storing parent modification UUID in descriptor in '%s'"), pImage->pszFilename);
2579 }
2580 }
2581 else if (RT_FAILURE(rc))
2582 return rc;
2583
2584 return VINF_SUCCESS;
2585}
2586
2587/**
2588 * Internal : Prepares the descriptor to write to the image.
2589 */
2590static int vmdkDescriptorPrepare(PVMDKIMAGE pImage, uint64_t cbLimit,
2591 void **ppvData, size_t *pcbData)
2592{
2593 int rc = VINF_SUCCESS;
2594
2595 /*
2596 * Allocate temporary descriptor buffer.
2597 * In case there is no limit allocate a default
2598 * and increase if required.
2599 */
2600 size_t cbDescriptor = cbLimit ? cbLimit : 4 * _1K;
2601 char *pszDescriptor = (char *)RTMemAllocZ(cbDescriptor);
2602 size_t offDescriptor = 0;
2603
2604 if (!pszDescriptor)
2605 return VERR_NO_MEMORY;
2606
2607 for (unsigned i = 0; i < pImage->Descriptor.cLines; i++)
2608 {
2609 const char *psz = pImage->Descriptor.aLines[i];
2610 size_t cb = strlen(psz);
2611
2612 /*
2613 * Increase the descriptor if there is no limit and
2614 * there is not enough room left for this line.
2615 */
2616 if (offDescriptor + cb + 1 > cbDescriptor)
2617 {
2618 if (cbLimit)
2619 {
2620 rc = vdIfError(pImage->pIfError, VERR_BUFFER_OVERFLOW, RT_SRC_POS, N_("VMDK: descriptor too long in '%s'"), pImage->pszFilename);
2621 break;
2622 }
2623 else
2624 {
2625 char *pszDescriptorNew = NULL;
2626 LogFlow(("Increasing descriptor cache\n"));
2627
2628 pszDescriptorNew = (char *)RTMemRealloc(pszDescriptor, cbDescriptor + cb + 4 * _1K);
2629 if (!pszDescriptorNew)
2630 {
2631 rc = VERR_NO_MEMORY;
2632 break;
2633 }
2634 pszDescriptor = pszDescriptorNew;
2635 cbDescriptor += cb + 4 * _1K;
2636 }
2637 }
2638
2639 if (cb > 0)
2640 {
2641 memcpy(pszDescriptor + offDescriptor, psz, cb);
2642 offDescriptor += cb;
2643 }
2644
2645 memcpy(pszDescriptor + offDescriptor, "\n", 1);
2646 offDescriptor++;
2647 }
2648
2649 if (RT_SUCCESS(rc))
2650 {
2651 *ppvData = pszDescriptor;
2652 *pcbData = offDescriptor;
2653 }
2654 else if (pszDescriptor)
2655 RTMemFree(pszDescriptor);
2656
2657 return rc;
2658}
2659
2660/**
2661 * Internal: write/update the descriptor part of the image.
2662 */
2663static int vmdkWriteDescriptor(PVMDKIMAGE pImage, PVDIOCTX pIoCtx)
2664{
2665 int rc = VINF_SUCCESS;
2666 uint64_t cbLimit;
2667 uint64_t uOffset;
2668 PVMDKFILE pDescFile;
2669 void *pvDescriptor = NULL;
2670 size_t cbDescriptor;
2671
2672 if (pImage->pDescData)
2673 {
2674 /* Separate descriptor file. */
2675 uOffset = 0;
2676 cbLimit = 0;
2677 pDescFile = pImage->pFile;
2678 }
2679 else
2680 {
2681 /* Embedded descriptor file. */
2682 uOffset = VMDK_SECTOR2BYTE(pImage->pExtents[0].uDescriptorSector);
2683 cbLimit = VMDK_SECTOR2BYTE(pImage->pExtents[0].cDescriptorSectors);
2684 pDescFile = pImage->pExtents[0].pFile;
2685 }
2686 /* Bail out if there is no file to write to. */
2687 if (pDescFile == NULL)
2688 return VERR_INVALID_PARAMETER;
2689
2690 rc = vmdkDescriptorPrepare(pImage, cbLimit, &pvDescriptor, &cbDescriptor);
2691 if (RT_SUCCESS(rc))
2692 {
2693 rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pDescFile->pStorage,
2694 uOffset, pvDescriptor,
2695 cbLimit ? cbLimit : cbDescriptor,
2696 pIoCtx, NULL, NULL);
2697 if ( RT_FAILURE(rc)
2698 && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
2699 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error writing descriptor in '%s'"), pImage->pszFilename);
2700 }
2701
2702 if (RT_SUCCESS(rc) && !cbLimit)
2703 {
2704 rc = vdIfIoIntFileSetSize(pImage->pIfIo, pDescFile->pStorage, cbDescriptor);
2705 if (RT_FAILURE(rc))
2706 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error truncating descriptor in '%s'"), pImage->pszFilename);
2707 }
2708
2709 if (RT_SUCCESS(rc))
2710 pImage->Descriptor.fDirty = false;
2711
2712 if (pvDescriptor)
2713 RTMemFree(pvDescriptor);
2714 return rc;
2715
2716}
2717
2718/**
2719 * Internal: validate the consistency check values in a binary header.
2720 */
2721static int vmdkValidateHeader(PVMDKIMAGE pImage, PVMDKEXTENT pExtent, const SparseExtentHeader *pHeader)
2722{
2723 int rc = VINF_SUCCESS;
2724 if (RT_LE2H_U32(pHeader->magicNumber) != VMDK_SPARSE_MAGICNUMBER)
2725 {
2726 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: incorrect magic in sparse extent header in '%s'"), pExtent->pszFullname);
2727 return rc;
2728 }
2729 if (RT_LE2H_U32(pHeader->version) != 1 && RT_LE2H_U32(pHeader->version) != 3)
2730 {
2731 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_UNSUPPORTED_VERSION, RT_SRC_POS, N_("VMDK: incorrect version in sparse extent header in '%s', not a VMDK 1.0/1.1 conforming file"), pExtent->pszFullname);
2732 return rc;
2733 }
2734 if ( (RT_LE2H_U32(pHeader->flags) & 1)
2735 && ( pHeader->singleEndLineChar != '\n'
2736 || pHeader->nonEndLineChar != ' '
2737 || pHeader->doubleEndLineChar1 != '\r'
2738 || pHeader->doubleEndLineChar2 != '\n') )
2739 {
2740 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: corrupted by CR/LF translation in '%s'"), pExtent->pszFullname);
2741 return rc;
2742 }
2743 if (RT_LE2H_U64(pHeader->descriptorSize) > VMDK_SPARSE_DESCRIPTOR_SIZE_MAX)
2744 {
2745 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: descriptor size out of bounds (%llu vs %llu) '%s'"),
2746 pExtent->pszFullname, RT_LE2H_U64(pHeader->descriptorSize), VMDK_SPARSE_DESCRIPTOR_SIZE_MAX);
2747 return rc;
2748 }
2749 return rc;
2750}
2751
2752/**
2753 * Internal: read metadata belonging to an extent with binary header, i.e.
2754 * as found in monolithic files.
2755 */
2756static int vmdkReadBinaryMetaExtent(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
2757 bool fMagicAlreadyRead)
2758{
2759 SparseExtentHeader Header;
2760 int rc;
2761
2762 if (!fMagicAlreadyRead)
2763 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage, 0,
2764 &Header, sizeof(Header));
2765 else
2766 {
2767 Header.magicNumber = RT_H2LE_U32(VMDK_SPARSE_MAGICNUMBER);
2768 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
2769 RT_UOFFSETOF(SparseExtentHeader, version),
2770 &Header.version,
2771 sizeof(Header)
2772 - RT_UOFFSETOF(SparseExtentHeader, version));
2773 }
2774
2775 if (RT_SUCCESS(rc))
2776 {
2777 rc = vmdkValidateHeader(pImage, pExtent, &Header);
2778 if (RT_SUCCESS(rc))
2779 {
2780 uint64_t cbFile = 0;
2781
2782 if ( (RT_LE2H_U32(Header.flags) & RT_BIT(17))
2783 && RT_LE2H_U64(Header.gdOffset) == VMDK_GD_AT_END)
2784 pExtent->fFooter = true;
2785
2786 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2787 || ( pExtent->fFooter
2788 && !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL)))
2789 {
2790 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pExtent->pFile->pStorage, &cbFile);
2791 if (RT_FAILURE(rc))
2792 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: cannot get size of '%s'"), pExtent->pszFullname);
2793 }
2794
2795 if (RT_SUCCESS(rc))
2796 {
2797 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
2798 pExtent->uAppendPosition = RT_ALIGN_64(cbFile, 512);
2799
2800 if ( pExtent->fFooter
2801 && ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2802 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL)))
2803 {
2804 /* Read the footer, which comes before the end-of-stream marker. */
2805 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
2806 cbFile - 2*512, &Header,
2807 sizeof(Header));
2808 if (RT_FAILURE(rc))
2809 {
2810 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error reading extent footer in '%s'"), pExtent->pszFullname);
2811 rc = VERR_VD_VMDK_INVALID_HEADER;
2812 }
2813
2814 if (RT_SUCCESS(rc))
2815 rc = vmdkValidateHeader(pImage, pExtent, &Header);
2816 /* Prohibit any writes to this extent. */
2817 pExtent->uAppendPosition = 0;
2818 }
2819
2820 if (RT_SUCCESS(rc))
2821 {
2822 pExtent->uVersion = RT_LE2H_U32(Header.version);
2823 pExtent->enmType = VMDKETYPE_HOSTED_SPARSE; /* Just dummy value, changed later. */
2824 pExtent->cSectors = RT_LE2H_U64(Header.capacity);
2825 pExtent->cSectorsPerGrain = RT_LE2H_U64(Header.grainSize);
2826 pExtent->uDescriptorSector = RT_LE2H_U64(Header.descriptorOffset);
2827 pExtent->cDescriptorSectors = RT_LE2H_U64(Header.descriptorSize);
2828 pExtent->cGTEntries = RT_LE2H_U32(Header.numGTEsPerGT);
2829 pExtent->cOverheadSectors = RT_LE2H_U64(Header.overHead);
2830 pExtent->fUncleanShutdown = !!Header.uncleanShutdown;
2831 pExtent->uCompression = RT_LE2H_U16(Header.compressAlgorithm);
2832 if (RT_LE2H_U32(Header.flags) & RT_BIT(1))
2833 {
2834 pExtent->uSectorRGD = RT_LE2H_U64(Header.rgdOffset);
2835 pExtent->uSectorGD = RT_LE2H_U64(Header.gdOffset);
2836 }
2837 else
2838 {
2839 pExtent->uSectorGD = RT_LE2H_U64(Header.gdOffset);
2840 pExtent->uSectorRGD = 0;
2841 }
2842
2843 if (pExtent->uDescriptorSector && !pExtent->cDescriptorSectors)
2844 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2845 N_("VMDK: inconsistent embedded descriptor config in '%s'"), pExtent->pszFullname);
2846
2847 if ( RT_SUCCESS(rc)
2848 && ( pExtent->uSectorGD == VMDK_GD_AT_END
2849 || pExtent->uSectorRGD == VMDK_GD_AT_END)
2850 && ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2851 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL)))
2852 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2853 N_("VMDK: cannot resolve grain directory offset in '%s'"), pExtent->pszFullname);
2854
2855 if (RT_SUCCESS(rc))
2856 {
2857 uint64_t cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
2858 if (!cSectorsPerGDE || cSectorsPerGDE > UINT32_MAX)
2859 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2860 N_("VMDK: incorrect grain directory size in '%s'"), pExtent->pszFullname);
2861 else
2862 {
2863 pExtent->cSectorsPerGDE = cSectorsPerGDE;
2864 pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
2865
2866 /* Fix up the number of descriptor sectors, as some flat images have
2867 * really just one, and this causes failures when inserting the UUID
2868 * values and other extra information. */
2869 if (pExtent->cDescriptorSectors != 0 && pExtent->cDescriptorSectors < 4)
2870 {
2871 /* Do it the easy way - just fix it for flat images which have no
2872 * other complicated metadata which needs space too. */
2873 if ( pExtent->uDescriptorSector + 4 < pExtent->cOverheadSectors
2874 && pExtent->cGTEntries * pExtent->cGDEntries == 0)
2875 pExtent->cDescriptorSectors = 4;
2876 }
2877 }
2878 }
2879 }
2880 }
2881 }
2882 }
2883 else
2884 {
2885 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error reading extent header in '%s'"), pExtent->pszFullname);
2886 rc = VERR_VD_VMDK_INVALID_HEADER;
2887 }
2888
2889 if (RT_FAILURE(rc))
2890 vmdkFreeExtentData(pImage, pExtent, false);
2891
2892 return rc;
2893}
2894
2895/**
2896 * Internal: read additional metadata belonging to an extent. For those
2897 * extents which have no additional metadata just verify the information.
2898 */
2899static int vmdkReadMetaExtent(PVMDKIMAGE pImage, PVMDKEXTENT pExtent)
2900{
2901 int rc = VINF_SUCCESS;
2902
2903/* disabled the check as there are too many truncated vmdk images out there */
2904#ifdef VBOX_WITH_VMDK_STRICT_SIZE_CHECK
2905 uint64_t cbExtentSize;
2906 /* The image must be a multiple of a sector in size and contain the data
2907 * area (flat images only). If not, it means the image is at least
2908 * truncated, or even seriously garbled. */
2909 rc = vdIfIoIntFileGetSize(pImage->pIfIo, pExtent->pFile->pStorage, &cbExtentSize);
2910 if (RT_FAILURE(rc))
2911 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error getting size in '%s'"), pExtent->pszFullname);
2912 else if ( cbExtentSize != RT_ALIGN_64(cbExtentSize, 512)
2913 && (pExtent->enmType != VMDKETYPE_FLAT || pExtent->cNominalSectors + pExtent->uSectorOffset > VMDK_BYTE2SECTOR(cbExtentSize)))
2914 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2915 N_("VMDK: file size is not a multiple of 512 in '%s', file is truncated or otherwise garbled"), pExtent->pszFullname);
2916#endif /* VBOX_WITH_VMDK_STRICT_SIZE_CHECK */
2917 if ( RT_SUCCESS(rc)
2918 && pExtent->enmType == VMDKETYPE_HOSTED_SPARSE)
2919 {
2920 /* The spec says that this must be a power of two and greater than 8,
2921 * but probably they meant not less than 8. */
2922 if ( (pExtent->cSectorsPerGrain & (pExtent->cSectorsPerGrain - 1))
2923 || pExtent->cSectorsPerGrain < 8)
2924 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2925 N_("VMDK: invalid extent grain size %u in '%s'"), pExtent->cSectorsPerGrain, pExtent->pszFullname);
2926 else
2927 {
2928 /* This code requires that a grain table must hold a power of two multiple
2929 * of the number of entries per GT cache entry. */
2930 if ( (pExtent->cGTEntries & (pExtent->cGTEntries - 1))
2931 || pExtent->cGTEntries < VMDK_GT_CACHELINE_SIZE)
2932 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS,
2933 N_("VMDK: grain table cache size problem in '%s'"), pExtent->pszFullname);
2934 else
2935 {
2936 rc = vmdkAllocStreamBuffers(pImage, pExtent);
2937 if (RT_SUCCESS(rc))
2938 {
2939 /* Prohibit any writes to this streamOptimized extent. */
2940 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
2941 pExtent->uAppendPosition = 0;
2942
2943 if ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
2944 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
2945 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
2946 rc = vmdkReadGrainDirectory(pImage, pExtent);
2947 else
2948 {
2949 pExtent->uGrainSectorAbs = pExtent->cOverheadSectors;
2950 pExtent->cbGrainStreamRead = 0;
2951 }
2952 }
2953 }
2954 }
2955 }
2956
2957 if (RT_FAILURE(rc))
2958 vmdkFreeExtentData(pImage, pExtent, false);
2959
2960 return rc;
2961}
2962
2963/**
2964 * Internal: write/update the metadata for a sparse extent.
2965 */
2966static int vmdkWriteMetaSparseExtent(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
2967 uint64_t uOffset, PVDIOCTX pIoCtx)
2968{
2969 SparseExtentHeader Header;
2970
2971 memset(&Header, '\0', sizeof(Header));
2972 Header.magicNumber = RT_H2LE_U32(VMDK_SPARSE_MAGICNUMBER);
2973 Header.version = RT_H2LE_U32(pExtent->uVersion);
2974 Header.flags = RT_H2LE_U32(RT_BIT(0));
2975 if (pExtent->pRGD)
2976 Header.flags |= RT_H2LE_U32(RT_BIT(1));
2977 if (pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
2978 Header.flags |= RT_H2LE_U32(RT_BIT(16) | RT_BIT(17));
2979 Header.capacity = RT_H2LE_U64(pExtent->cSectors);
2980 Header.grainSize = RT_H2LE_U64(pExtent->cSectorsPerGrain);
2981 Header.descriptorOffset = RT_H2LE_U64(pExtent->uDescriptorSector);
2982 Header.descriptorSize = RT_H2LE_U64(pExtent->cDescriptorSectors);
2983 Header.numGTEsPerGT = RT_H2LE_U32(pExtent->cGTEntries);
2984 if (pExtent->fFooter && uOffset == 0)
2985 {
2986 if (pExtent->pRGD)
2987 {
2988 Assert(pExtent->uSectorRGD);
2989 Header.rgdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
2990 Header.gdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
2991 }
2992 else
2993 Header.gdOffset = RT_H2LE_U64(VMDK_GD_AT_END);
2994 }
2995 else
2996 {
2997 if (pExtent->pRGD)
2998 {
2999 Assert(pExtent->uSectorRGD);
3000 Header.rgdOffset = RT_H2LE_U64(pExtent->uSectorRGD);
3001 Header.gdOffset = RT_H2LE_U64(pExtent->uSectorGD);
3002 }
3003 else
3004 Header.gdOffset = RT_H2LE_U64(pExtent->uSectorGD);
3005 }
3006 Header.overHead = RT_H2LE_U64(pExtent->cOverheadSectors);
3007 Header.uncleanShutdown = pExtent->fUncleanShutdown;
3008 Header.singleEndLineChar = '\n';
3009 Header.nonEndLineChar = ' ';
3010 Header.doubleEndLineChar1 = '\r';
3011 Header.doubleEndLineChar2 = '\n';
3012 Header.compressAlgorithm = RT_H2LE_U16(pExtent->uCompression);
3013
3014 int rc = vdIfIoIntFileWriteMeta(pImage->pIfIo, pExtent->pFile->pStorage,
3015 uOffset, &Header, sizeof(Header),
3016 pIoCtx, NULL, NULL);
3017 if (RT_FAILURE(rc) && (rc != VERR_VD_ASYNC_IO_IN_PROGRESS))
3018 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error writing extent header in '%s'"), pExtent->pszFullname);
3019 return rc;
3020}
3021
3022/**
3023 * Internal: free the buffers used for streamOptimized images.
3024 */
3025static void vmdkFreeStreamBuffers(PVMDKEXTENT pExtent)
3026{
3027 if (pExtent->pvCompGrain)
3028 {
3029 RTMemFree(pExtent->pvCompGrain);
3030 pExtent->pvCompGrain = NULL;
3031 }
3032 if (pExtent->pvGrain)
3033 {
3034 RTMemFree(pExtent->pvGrain);
3035 pExtent->pvGrain = NULL;
3036 }
3037}
3038
3039/**
3040 * Internal: free the memory used by the extent data structure, optionally
3041 * deleting the referenced files.
3042 *
3043 * @returns VBox status code.
3044 * @param pImage Pointer to the image instance data.
3045 * @param pExtent The extent to free.
3046 * @param fDelete Flag whether to delete the backing storage.
3047 */
3048static int vmdkFreeExtentData(PVMDKIMAGE pImage, PVMDKEXTENT pExtent,
3049 bool fDelete)
3050{
3051 int rc = VINF_SUCCESS;
3052
3053 vmdkFreeGrainDirectory(pExtent);
3054 if (pExtent->pDescData)
3055 {
3056 RTMemFree(pExtent->pDescData);
3057 pExtent->pDescData = NULL;
3058 }
3059 if (pExtent->pFile != NULL)
3060 {
3061 /* Do not delete raw extents, these have full and base names equal. */
3062 rc = vmdkFileClose(pImage, &pExtent->pFile,
3063 fDelete
3064 && pExtent->pszFullname
3065 && pExtent->pszBasename
3066 && strcmp(pExtent->pszFullname, pExtent->pszBasename));
3067 }
3068 if (pExtent->pszBasename)
3069 {
3070 RTMemTmpFree((void *)pExtent->pszBasename);
3071 pExtent->pszBasename = NULL;
3072 }
3073 if (pExtent->pszFullname)
3074 {
3075 RTStrFree((char *)(void *)pExtent->pszFullname);
3076 pExtent->pszFullname = NULL;
3077 }
3078 vmdkFreeStreamBuffers(pExtent);
3079
3080 return rc;
3081}
3082
3083/**
3084 * Internal: allocate grain table cache if necessary for this image.
3085 */
3086static int vmdkAllocateGrainTableCache(PVMDKIMAGE pImage)
3087{
3088 PVMDKEXTENT pExtent;
3089
3090 /* Allocate grain table cache if any sparse extent is present. */
3091 for (unsigned i = 0; i < pImage->cExtents; i++)
3092 {
3093 pExtent = &pImage->pExtents[i];
3094 if (pExtent->enmType == VMDKETYPE_HOSTED_SPARSE)
3095 {
3096 /* Allocate grain table cache. */
3097 pImage->pGTCache = (PVMDKGTCACHE)RTMemAllocZ(sizeof(VMDKGTCACHE));
3098 if (!pImage->pGTCache)
3099 return VERR_NO_MEMORY;
3100 for (unsigned j = 0; j < VMDK_GT_CACHE_SIZE; j++)
3101 {
3102 PVMDKGTCACHEENTRY pGCE = &pImage->pGTCache->aGTCache[j];
3103 pGCE->uExtent = UINT32_MAX;
3104 }
3105 pImage->pGTCache->cEntries = VMDK_GT_CACHE_SIZE;
3106 break;
3107 }
3108 }
3109
3110 return VINF_SUCCESS;
3111}
3112
3113/**
3114 * Internal: allocate the given number of extents.
3115 */
3116static int vmdkCreateExtents(PVMDKIMAGE pImage, unsigned cExtents)
3117{
3118 int rc = VINF_SUCCESS;
3119 PVMDKEXTENT pExtents = (PVMDKEXTENT)RTMemAllocZ(cExtents * sizeof(VMDKEXTENT));
3120 if (pExtents)
3121 {
3122 for (unsigned i = 0; i < cExtents; i++)
3123 {
3124 pExtents[i].pFile = NULL;
3125 pExtents[i].pszBasename = NULL;
3126 pExtents[i].pszFullname = NULL;
3127 pExtents[i].pGD = NULL;
3128 pExtents[i].pRGD = NULL;
3129 pExtents[i].pDescData = NULL;
3130 pExtents[i].uVersion = 1;
3131 pExtents[i].uCompression = VMDK_COMPRESSION_NONE;
3132 pExtents[i].uExtent = i;
3133 pExtents[i].pImage = pImage;
3134 }
3135 pImage->pExtents = pExtents;
3136 pImage->cExtents = cExtents;
3137 }
3138 else
3139 rc = VERR_NO_MEMORY;
3140
3141 return rc;
3142}
3143
3144/**
3145 * Internal: Create an additional file backed extent in split images.
3146 * Supports split sparse and flat images.
3147 *
3148 * @returns VBox status code.
3149 * @param pImage VMDK image instance.
3150 * @param cbSize Desiried size in bytes of new extent.
3151 */
3152static int vmdkAddFileBackedExtent(PVMDKIMAGE pImage, uint64_t cbSize)
3153{
3154 int rc = VINF_SUCCESS;
3155 unsigned uImageFlags = pImage->uImageFlags;
3156
3157 /* Check for unsupported image type. */
3158 if ((uImageFlags & VD_VMDK_IMAGE_FLAGS_ESX)
3159 || (uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
3160 || (uImageFlags & VD_VMDK_IMAGE_FLAGS_RAWDISK))
3161 {
3162 return VERR_NOT_SUPPORTED;
3163 }
3164
3165 /* Allocate array of extents and copy existing extents to it. */
3166 PVMDKEXTENT pNewExtents = (PVMDKEXTENT)RTMemAllocZ((pImage->cExtents + 1) * sizeof(VMDKEXTENT));
3167 if (!pNewExtents)
3168 {
3169 return VERR_NO_MEMORY;
3170 }
3171
3172 memcpy(pNewExtents, pImage->pExtents, pImage->cExtents * sizeof(VMDKEXTENT));
3173
3174 /* Locate newly created extent and populate default metadata. */
3175 PVMDKEXTENT pExtent = &pNewExtents[pImage->cExtents];
3176
3177 pExtent->pFile = NULL;
3178 pExtent->pszBasename = NULL;
3179 pExtent->pszFullname = NULL;
3180 pExtent->pGD = NULL;
3181 pExtent->pRGD = NULL;
3182 pExtent->pDescData = NULL;
3183 pExtent->uVersion = 1;
3184 pExtent->uCompression = VMDK_COMPRESSION_NONE;
3185 pExtent->uExtent = pImage->cExtents;
3186 pExtent->pImage = pImage;
3187 pExtent->cNominalSectors = VMDK_BYTE2SECTOR(cbSize);
3188 pExtent->enmAccess = VMDKACCESS_READWRITE;
3189 pExtent->uSectorOffset = 0;
3190 pExtent->fMetaDirty = true;
3191
3192 /* Apply image type specific meta data. */
3193 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
3194 {
3195 pExtent->enmType = VMDKETYPE_FLAT;
3196 }
3197 else
3198 {
3199 uint64_t cSectorsPerGDE, cSectorsPerGD;
3200 pExtent->enmType = VMDKETYPE_HOSTED_SPARSE;
3201 pExtent->cSectors = VMDK_BYTE2SECTOR(RT_ALIGN_64(cbSize, _64K));
3202 pExtent->cSectorsPerGrain = VMDK_BYTE2SECTOR(_64K);
3203 pExtent->cGTEntries = 512;
3204 cSectorsPerGDE = pExtent->cGTEntries * pExtent->cSectorsPerGrain;
3205 pExtent->cSectorsPerGDE = cSectorsPerGDE;
3206 pExtent->cGDEntries = (pExtent->cSectors + cSectorsPerGDE - 1) / cSectorsPerGDE;
3207 cSectorsPerGD = (pExtent->cGDEntries + (512 / sizeof(uint32_t) - 1)) / (512 / sizeof(uint32_t));
3208 }
3209
3210 /* Allocate and set file name for extent. */
3211 char *pszBasenameSubstr = RTPathFilename(pImage->pszFilename);
3212 AssertPtr(pszBasenameSubstr);
3213
3214 char *pszBasenameSuff = RTPathSuffix(pszBasenameSubstr);
3215 char *pszBasenameBase = RTStrDup(pszBasenameSubstr);
3216 RTPathStripSuffix(pszBasenameBase);
3217 char *pszTmp;
3218 size_t cbTmp;
3219
3220 if (pImage->uImageFlags & VD_IMAGE_FLAGS_FIXED)
3221 RTStrAPrintf(&pszTmp, "%s-f%03d%s", pszBasenameBase,
3222 pExtent->uExtent + 1, pszBasenameSuff);
3223 else
3224 RTStrAPrintf(&pszTmp, "%s-s%03d%s", pszBasenameBase, pExtent->uExtent + 1,
3225 pszBasenameSuff);
3226
3227 RTStrFree(pszBasenameBase);
3228 if (!pszTmp)
3229 return VERR_NO_STR_MEMORY;
3230 cbTmp = strlen(pszTmp) + 1;
3231 char *pszBasename = (char *)RTMemTmpAlloc(cbTmp);
3232 if (!pszBasename)
3233 {
3234 RTStrFree(pszTmp);
3235 return VERR_NO_MEMORY;
3236 }
3237
3238 memcpy(pszBasename, pszTmp, cbTmp);
3239 RTStrFree(pszTmp);
3240
3241 pExtent->pszBasename = pszBasename;
3242
3243 char *pszBasedirectory = RTStrDup(pImage->pszFilename);
3244 if (!pszBasedirectory)
3245 return VERR_NO_STR_MEMORY;
3246 RTPathStripFilename(pszBasedirectory);
3247 char *pszFullname = RTPathJoinA(pszBasedirectory, pExtent->pszBasename);
3248 RTStrFree(pszBasedirectory);
3249 if (!pszFullname)
3250 return VERR_NO_STR_MEMORY;
3251 pExtent->pszFullname = pszFullname;
3252
3253 /* Create file for extent. */
3254 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszBasename, pExtent->pszFullname,
3255 VDOpenFlagsToFileOpenFlags(pImage->uOpenFlags,
3256 true /* fCreate */));
3257 if (RT_FAILURE(rc))
3258 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new file '%s'"), pExtent->pszFullname);
3259
3260 if (uImageFlags & VD_IMAGE_FLAGS_FIXED)
3261 {
3262 /* For flat images: Pre allocate file space. */
3263 rc = vdIfIoIntFileSetAllocationSize(pImage->pIfIo, pExtent->pFile->pStorage, cbSize,
3264 0 /* fFlags */, NULL, 0, 0);
3265 if (RT_FAILURE(rc))
3266 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not set size of new file '%s'"), pExtent->pszFullname);
3267 }
3268 else
3269 {
3270 /* For sparse images: Allocate new grain directories/tables. */
3271 /* fPreAlloc should never be false because VMware can't use such images. */
3272 rc = vmdkCreateGrainDirectory(pImage, pExtent,
3273 RT_MAX( pExtent->uDescriptorSector
3274 + pExtent->cDescriptorSectors,
3275 1),
3276 true /* fPreAlloc */);
3277 if (RT_FAILURE(rc))
3278 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not create new grain directory in '%s'"), pExtent->pszFullname);
3279 }
3280
3281 /* Insert new extent into descriptor file. */
3282 rc = vmdkDescExtInsert(pImage, &pImage->Descriptor, pExtent->enmAccess,
3283 pExtent->cNominalSectors, pExtent->enmType,
3284 pExtent->pszBasename, pExtent->uSectorOffset);
3285 if (RT_FAILURE(rc))
3286 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: could not insert the extent list into descriptor in '%s'"), pImage->pszFilename);
3287
3288 pImage->pExtents = pNewExtents;
3289 pImage->cExtents++;
3290
3291 return rc;
3292}
3293
3294/**
3295 * Reads and processes the descriptor embedded in sparse images.
3296 *
3297 * @returns VBox status code.
3298 * @param pImage VMDK image instance.
3299 * @param pFile The sparse file handle.
3300 */
3301static int vmdkDescriptorReadSparse(PVMDKIMAGE pImage, PVMDKFILE pFile)
3302{
3303 /* It's a hosted single-extent image. */
3304 int rc = vmdkCreateExtents(pImage, 1);
3305 if (RT_SUCCESS(rc))
3306 {
3307 /* The opened file is passed to the extent. No separate descriptor
3308 * file, so no need to keep anything open for the image. */
3309 PVMDKEXTENT pExtent = &pImage->pExtents[0];
3310 pExtent->pFile = pFile;
3311 pImage->pFile = NULL;
3312 pExtent->pszFullname = RTPathAbsDup(pImage->pszFilename);
3313 if (RT_LIKELY(pExtent->pszFullname))
3314 {
3315 /* As we're dealing with a monolithic image here, there must
3316 * be a descriptor embedded in the image file. */
3317 rc = vmdkReadBinaryMetaExtent(pImage, pExtent, true /* fMagicAlreadyRead */);
3318 if ( RT_SUCCESS(rc)
3319 && pExtent->uDescriptorSector
3320 && pExtent->cDescriptorSectors)
3321 {
3322 /* HACK: extend the descriptor if it is unusually small and it fits in
3323 * the unused space after the image header. Allows opening VMDK files
3324 * with extremely small descriptor in read/write mode.
3325 *
3326 * The previous version introduced a possible regression for VMDK stream
3327 * optimized images from VMware which tend to have only a single sector sized
3328 * descriptor. Increasing the descriptor size resulted in adding the various uuid
3329 * entries required to make it work with VBox but for stream optimized images
3330 * the updated binary header wasn't written to the disk creating a mismatch
3331 * between advertised and real descriptor size.
3332 *
3333 * The descriptor size will be increased even if opened readonly now if there
3334 * enough room but the new value will not be written back to the image.
3335 */
3336 if ( pExtent->cDescriptorSectors < 3
3337 && (int64_t)pExtent->uSectorGD - pExtent->uDescriptorSector >= 4
3338 && (!pExtent->uSectorRGD || (int64_t)pExtent->uSectorRGD - pExtent->uDescriptorSector >= 4))
3339 {
3340 uint64_t cDescriptorSectorsOld = pExtent->cDescriptorSectors;
3341
3342 pExtent->cDescriptorSectors = 4;
3343 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
3344 {
3345 /*
3346 * Update the on disk number now to make sure we don't introduce inconsistencies
3347 * in case of stream optimized images from VMware where the descriptor is just
3348 * one sector big (the binary header is not written to disk for complete
3349 * stream optimized images in vmdkFlushImage()).
3350 */
3351 uint64_t u64DescSizeNew = RT_H2LE_U64(pExtent->cDescriptorSectors);
3352 rc = vdIfIoIntFileWriteSync(pImage->pIfIo, pFile->pStorage,
3353 RT_UOFFSETOF(SparseExtentHeader, descriptorSize),
3354 &u64DescSizeNew, sizeof(u64DescSizeNew));
3355 if (RT_FAILURE(rc))
3356 {
3357 LogFlowFunc(("Increasing the descriptor size failed with %Rrc\n", rc));
3358 /* Restore the old size and carry on. */
3359 pExtent->cDescriptorSectors = cDescriptorSectorsOld;
3360 }
3361 }
3362 }
3363 /* Read the descriptor from the extent. */
3364 pExtent->pDescData = (char *)RTMemAllocZ(VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors));
3365 if (RT_LIKELY(pExtent->pDescData))
3366 {
3367 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pExtent->pFile->pStorage,
3368 VMDK_SECTOR2BYTE(pExtent->uDescriptorSector),
3369 pExtent->pDescData,
3370 VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors));
3371 if (RT_SUCCESS(rc))
3372 {
3373 rc = vmdkParseDescriptor(pImage, pExtent->pDescData,
3374 VMDK_SECTOR2BYTE(pExtent->cDescriptorSectors));
3375 if ( RT_SUCCESS(rc)
3376 && ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
3377 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_ASYNC_IO)))
3378 {
3379 rc = vmdkReadMetaExtent(pImage, pExtent);
3380 if (RT_SUCCESS(rc))
3381 {
3382 /* Mark the extent as unclean if opened in read-write mode. */
3383 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
3384 && !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
3385 {
3386 pExtent->fUncleanShutdown = true;
3387 pExtent->fMetaDirty = true;
3388 }
3389 }
3390 }
3391 else if (RT_SUCCESS(rc))
3392 rc = VERR_NOT_SUPPORTED;
3393 }
3394 else
3395 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: read error for descriptor in '%s'"), pExtent->pszFullname);
3396 }
3397 else
3398 rc = VERR_NO_MEMORY;
3399 }
3400 else if (RT_SUCCESS(rc))
3401 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: monolithic image without descriptor in '%s'"), pImage->pszFilename);
3402 }
3403 else
3404 rc = VERR_NO_MEMORY;
3405 }
3406
3407 return rc;
3408}
3409
3410/**
3411 * Reads the descriptor from a pure text file.
3412 *
3413 * @returns VBox status code.
3414 * @param pImage VMDK image instance.
3415 * @param pFile The descriptor file handle.
3416 */
3417static int vmdkDescriptorReadAscii(PVMDKIMAGE pImage, PVMDKFILE pFile)
3418{
3419 /* Allocate at least 10K, and make sure that there is 5K free space
3420 * in case new entries need to be added to the descriptor. Never
3421 * allocate more than 128K, because that's no valid descriptor file
3422 * and will result in the correct "truncated read" error handling. */
3423 uint64_t cbFileSize;
3424 int rc = vdIfIoIntFileGetSize(pImage->pIfIo, pFile->pStorage, &cbFileSize);
3425 if ( RT_SUCCESS(rc)
3426 && cbFileSize >= 50)
3427 {
3428 uint64_t cbSize = cbFileSize;
3429 if (cbSize % VMDK_SECTOR2BYTE(10))
3430 cbSize += VMDK_SECTOR2BYTE(20) - cbSize % VMDK_SECTOR2BYTE(10);
3431 else
3432 cbSize += VMDK_SECTOR2BYTE(10);
3433 cbSize = RT_MIN(cbSize, _128K);
3434 pImage->cbDescAlloc = RT_MAX(VMDK_SECTOR2BYTE(20), cbSize);
3435 pImage->pDescData = (char *)RTMemAllocZ(pImage->cbDescAlloc);
3436 if (RT_LIKELY(pImage->pDescData))
3437 {
3438 rc = vdIfIoIntFileReadSync(pImage->pIfIo, pFile->pStorage, 0, pImage->pDescData,
3439 RT_MIN(pImage->cbDescAlloc, cbFileSize));
3440 if (RT_SUCCESS(rc))
3441 {
3442#if 0 /** @todo Revisit */
3443 cbRead += sizeof(u32Magic);
3444 if (cbRead == pImage->cbDescAlloc)
3445 {
3446 /* Likely the read is truncated. Better fail a bit too early
3447 * (normally the descriptor is much smaller than our buffer). */
3448 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: cannot read descriptor in '%s'"), pImage->pszFilename);
3449 goto out;
3450 }
3451#endif
3452 rc = vmdkParseDescriptor(pImage, pImage->pDescData,
3453 pImage->cbDescAlloc);
3454 if (RT_SUCCESS(rc))
3455 {
3456 for (unsigned i = 0; i < pImage->cExtents && RT_SUCCESS(rc); i++)
3457 {
3458 PVMDKEXTENT pExtent = &pImage->pExtents[i];
3459 if (pExtent->pszBasename)
3460 {
3461 /* Hack to figure out whether the specified name in the
3462 * extent descriptor is absolute. Doesn't always work, but
3463 * should be good enough for now. */
3464 char *pszFullname;
3465 /** @todo implement proper path absolute check. */
3466 if (pExtent->pszBasename[0] == RTPATH_SLASH)
3467 {
3468 pszFullname = RTStrDup(pExtent->pszBasename);
3469 if (!pszFullname)
3470 {
3471 rc = VERR_NO_MEMORY;
3472 break;
3473 }
3474 }
3475 else
3476 {
3477 char *pszDirname = RTStrDup(pImage->pszFilename);
3478 if (!pszDirname)
3479 {
3480 rc = VERR_NO_MEMORY;
3481 break;
3482 }
3483 RTPathStripFilename(pszDirname);
3484 pszFullname = RTPathJoinA(pszDirname, pExtent->pszBasename);
3485 RTStrFree(pszDirname);
3486 if (!pszFullname)
3487 {
3488 rc = VERR_NO_STR_MEMORY;
3489 break;
3490 }
3491 }
3492 pExtent->pszFullname = pszFullname;
3493 }
3494 else
3495 pExtent->pszFullname = NULL;
3496
3497 unsigned uOpenFlags = pImage->uOpenFlags | ((pExtent->enmAccess == VMDKACCESS_READONLY) ? VD_OPEN_FLAGS_READONLY : 0);
3498 switch (pExtent->enmType)
3499 {
3500 case VMDKETYPE_HOSTED_SPARSE:
3501 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszBasename, pExtent->pszFullname,
3502 VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */));
3503 if (RT_FAILURE(rc))
3504 {
3505 /* Do NOT signal an appropriate error here, as the VD
3506 * layer has the choice of retrying the open if it
3507 * failed. */
3508 break;
3509 }
3510 rc = vmdkReadBinaryMetaExtent(pImage, pExtent,
3511 false /* fMagicAlreadyRead */);
3512 if (RT_FAILURE(rc))
3513 break;
3514 rc = vmdkReadMetaExtent(pImage, pExtent);
3515 if (RT_FAILURE(rc))
3516 break;
3517
3518 /* Mark extent as unclean if opened in read-write mode. */
3519 if (!(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY))
3520 {
3521 pExtent->fUncleanShutdown = true;
3522 pExtent->fMetaDirty = true;
3523 }
3524 break;
3525 case VMDKETYPE_VMFS:
3526 case VMDKETYPE_FLAT:
3527 rc = vmdkFileOpen(pImage, &pExtent->pFile, pExtent->pszBasename, pExtent->pszFullname,
3528 VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */));
3529 if (RT_FAILURE(rc))
3530 {
3531 /* Do NOT signal an appropriate error here, as the VD
3532 * layer has the choice of retrying the open if it
3533 * failed. */
3534 break;
3535 }
3536 break;
3537 case VMDKETYPE_ZERO:
3538 /* Nothing to do. */
3539 break;
3540 default:
3541 AssertMsgFailed(("unknown vmdk extent type %d\n", pExtent->enmType));
3542 }
3543 }
3544 }
3545 }
3546 else
3547 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: read error for descriptor in '%s'"), pImage->pszFilename);
3548 }
3549 else
3550 rc = VERR_NO_MEMORY;
3551 }
3552 else if (RT_SUCCESS(rc))
3553 rc = vdIfError(pImage->pIfError, VERR_VD_VMDK_INVALID_HEADER, RT_SRC_POS, N_("VMDK: descriptor in '%s' is too short"), pImage->pszFilename);
3554
3555 return rc;
3556}
3557
3558/**
3559 * Read and process the descriptor based on the image type.
3560 *
3561 * @returns VBox status code.
3562 * @param pImage VMDK image instance.
3563 * @param pFile VMDK file handle.
3564 */
3565static int vmdkDescriptorRead(PVMDKIMAGE pImage, PVMDKFILE pFile)
3566{
3567 uint32_t u32Magic;
3568
3569 /* Read magic (if present). */
3570 int rc = vdIfIoIntFileReadSync(pImage->pIfIo, pFile->pStorage, 0,
3571 &u32Magic, sizeof(u32Magic));
3572 if (RT_SUCCESS(rc))
3573 {
3574 /* Handle the file according to its magic number. */
3575 if (RT_LE2H_U32(u32Magic) == VMDK_SPARSE_MAGICNUMBER)
3576 rc = vmdkDescriptorReadSparse(pImage, pFile);
3577 else
3578 rc = vmdkDescriptorReadAscii(pImage, pFile);
3579 }
3580 else
3581 {
3582 vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: error reading the magic number in '%s'"), pImage->pszFilename);
3583 rc = VERR_VD_VMDK_INVALID_HEADER;
3584 }
3585
3586 return rc;
3587}
3588
3589/**
3590 * Internal: Open an image, constructing all necessary data structures.
3591 */
3592static int vmdkOpenImage(PVMDKIMAGE pImage, unsigned uOpenFlags)
3593{
3594 pImage->uOpenFlags = uOpenFlags;
3595 pImage->pIfError = VDIfErrorGet(pImage->pVDIfsDisk);
3596 pImage->pIfIo = VDIfIoIntGet(pImage->pVDIfsImage);
3597 AssertPtrReturn(pImage->pIfIo, VERR_INVALID_PARAMETER);
3598
3599 /*
3600 * Open the image.
3601 * We don't have to check for asynchronous access because
3602 * we only support raw access and the opened file is a description
3603 * file were no data is stored.
3604 */
3605 PVMDKFILE pFile;
3606 int rc = vmdkFileOpen(pImage, &pFile, NULL, pImage->pszFilename,
3607 VDOpenFlagsToFileOpenFlags(uOpenFlags, false /* fCreate */));
3608 if (RT_SUCCESS(rc))
3609 {
3610 pImage->pFile = pFile;
3611
3612 rc = vmdkDescriptorRead(pImage, pFile);
3613 if (RT_SUCCESS(rc))
3614 {
3615 /* Determine PCHS geometry if not set. */
3616 if (pImage->PCHSGeometry.cCylinders == 0)
3617 {
3618 uint64_t cCylinders = VMDK_BYTE2SECTOR(pImage->cbSize)
3619 / pImage->PCHSGeometry.cHeads
3620 / pImage->PCHSGeometry.cSectors;
3621 pImage->PCHSGeometry.cCylinders = (unsigned)RT_MIN(cCylinders, 16383);
3622 if ( !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
3623 && !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED))
3624 {
3625 rc = vmdkDescSetPCHSGeometry(pImage, &pImage->PCHSGeometry);
3626 AssertRC(rc);
3627 }
3628 }
3629
3630 /* Update the image metadata now in case has changed. */
3631 rc = vmdkFlushImage(pImage, NULL);
3632 if (RT_SUCCESS(rc))
3633 {
3634 /* Figure out a few per-image constants from the extents. */
3635 pImage->cbSize = 0;
3636 for (unsigned i = 0; i < pImage->cExtents; i++)
3637 {
3638 PVMDKEXTENT pExtent = &pImage->pExtents[i];
3639 if (pExtent->enmType == VMDKETYPE_HOSTED_SPARSE)
3640 {
3641 /* Here used to be a check whether the nominal size of an extent
3642 * is a multiple of the grain size. The spec says that this is
3643 * always the case, but unfortunately some files out there in the
3644 * wild violate the spec (e.g. ReactOS 0.3.1). */
3645 }
3646 else if ( pExtent->enmType == VMDKETYPE_FLAT
3647 || pExtent->enmType == VMDKETYPE_ZERO)
3648 pImage->uImageFlags |= VD_IMAGE_FLAGS_FIXED;
3649
3650 pImage->cbSize += VMDK_SECTOR2BYTE(pExtent->cNominalSectors);
3651 }
3652
3653 if ( !(pImage->uImageFlags & VD_VMDK_IMAGE_FLAGS_STREAM_OPTIMIZED)
3654 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_READONLY)
3655 || !(pImage->uOpenFlags & VD_OPEN_FLAGS_SEQUENTIAL))
3656 rc = vmdkAllocateGrainTableCache(pImage);
3657 }
3658 }
3659 }
3660 /* else: Do NOT signal an appropriate error here, as the VD layer has the
3661 * choice of retrying the open if it failed. */
3662
3663 if (RT_SUCCESS(rc))
3664 {
3665 PVDREGIONDESC pRegion = &pImage->RegionList.aRegions[0];
3666 pImage->RegionList.fFlags = 0;
3667 pImage->RegionList.cRegions = 1;
3668
3669 pRegion->offRegion = 0; /* Disk start. */
3670 pRegion->cbBlock = 512;
3671 pRegion->enmDataForm = VDREGIONDATAFORM_RAW;
3672 pRegion->enmMetadataForm = VDREGIONMETADATAFORM_NONE;
3673 pRegion->cbData = 512;
3674 pRegion->cbMetadata = 0;
3675 pRegion->cRegionBlocksOrBytes = pImage->cbSize;
3676 }
3677 else
3678 vmdkFreeImage(pImage, false, false /*fFlush*/); /* Don't try to flush anything if opening failed. */
3679 return rc;
3680}
3681
3682/**
3683 * Frees a raw descriptor.
3684 * @internal
3685 */
3686static int vmdkRawDescFree(PVDISKRAW pRawDesc)
3687{
3688 if (!pRawDesc)
3689 return VINF_SUCCESS;
3690
3691 RTStrFree(pRawDesc->pszRawDisk);
3692 pRawDesc->pszRawDisk = NULL;
3693
3694 /* Partitions: */
3695 for (unsigned i = 0; i < pRawDesc->cPartDescs; i++)
3696 {
3697 RTStrFree(pRawDesc->pPartDescs[i].pszRawDevice);
3698 pRawDesc->pPartDescs[i].pszRawDevice = NULL;
3699
3700 RTMemFree(pRawDesc->pPartDescs[i].pvPartitionData);
3701 pRawDesc->pPartDescs[i].pvPartitionData = NULL;
3702 }
3703
3704 RTMemFree(pRawDesc->pPartDescs);
3705 pRawDesc->pPartDescs = NULL;
3706
3707 RTMemFree(pRawDesc);
3708 return VINF_SUCCESS;
3709}
3710
3711/**
3712 * Helper that grows the raw partition descriptor table by @a cToAdd entries,
3713 * returning the pointer to the first new entry.
3714 * @internal
3715 */
3716static int vmdkRawDescAppendPartDesc(PVMDKIMAGE pImage, PVDISKRAW pRawDesc, uint32_t cToAdd, PVDISKRAWPARTDESC *ppRet)
3717{
3718 uint32_t const cOld = pRawDesc->cPartDescs;
3719 uint32_t const cNew = cOld + cToAdd;
3720 PVDISKRAWPARTDESC paNew = (PVDISKRAWPARTDESC)RTMemReallocZ(pRawDesc->pPartDescs,
3721 cOld * sizeof(pRawDesc->pPartDescs[0]),
3722 cNew * sizeof(pRawDesc->pPartDescs[0]));
3723 if (paNew)
3724 {
3725 pRawDesc->cPartDescs = cNew;
3726 pRawDesc->pPartDescs = paNew;
3727
3728 *ppRet = &paNew[cOld];
3729 return VINF_SUCCESS;
3730 }
3731 *ppRet = NULL;
3732 return vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS,
3733 N_("VMDK: Image path: '%s'. Out of memory growing the partition descriptors (%u -> %u)."),
3734 pImage->pszFilename, cOld, cNew);
3735}
3736
3737/**
3738 * @callback_method_impl{FNRTSORTCMP}
3739 */
3740static DECLCALLBACK(int) vmdkRawDescPartComp(void const *pvElement1, void const *pvElement2, void *pvUser)
3741{
3742 RT_NOREF(pvUser);
3743 int64_t const iDelta = ((PVDISKRAWPARTDESC)pvElement1)->offStartInVDisk - ((PVDISKRAWPARTDESC)pvElement2)->offStartInVDisk;
3744 return iDelta < 0 ? -1 : iDelta > 0 ? 1 : 0;
3745}
3746
3747/**
3748 * Post processes the partition descriptors.
3749 *
3750 * Sorts them and check that they don't overlap.
3751 */
3752static int vmdkRawDescPostProcessPartitions(PVMDKIMAGE pImage, PVDISKRAW pRawDesc, uint64_t cbSize)
3753{
3754 /*
3755 * Sort data areas in ascending order of start.
3756 */
3757 RTSortShell(pRawDesc->pPartDescs, pRawDesc->cPartDescs, sizeof(pRawDesc->pPartDescs[0]), vmdkRawDescPartComp, NULL);
3758
3759 /*
3760 * Check that we don't have overlapping descriptors. If we do, that's an
3761 * indication that the drive is corrupt or that the RTDvm code is buggy.
3762 */
3763 VDISKRAWPARTDESC const *paPartDescs = pRawDesc->pPartDescs;
3764 for (uint32_t i = 0; i < pRawDesc->cPartDescs; i++)
3765 {
3766 uint64_t offLast = paPartDescs[i].offStartInVDisk + paPartDescs[i].cbData;
3767 if (offLast <= paPartDescs[i].offStartInVDisk)
3768 return vdIfError(pImage->pIfError, VERR_FILESYSTEM_CORRUPT /*?*/, RT_SRC_POS,
3769 N_("VMDK: Image path: '%s'. Bogus partition descriptor #%u (%#RX64 LB %#RX64%s): Wrap around or zero"),
3770 pImage->pszFilename, i, paPartDescs[i].offStartInVDisk, paPartDescs[i].cbData,
3771 paPartDescs[i].pvPartitionData ? " (data)" : "");
3772 offLast -= 1;
3773
3774 if (i + 1 < pRawDesc->cPartDescs && offLast >= paPartDescs[i + 1].offStartInVDisk)
3775 return vdIfError(pImage->pIfError, VERR_FILESYSTEM_CORRUPT /*?*/, RT_SRC_POS,
3776 N_("VMDK: Image path: '%s'. Partition descriptor #%u (%#RX64 LB %#RX64%s) overlaps with the next (%#RX64 LB %#RX64%s)"),
3777 pImage->pszFilename, i, paPartDescs[i].offStartInVDisk, paPartDescs[i].cbData,
3778 paPartDescs[i].pvPartitionData ? " (data)" : "", paPartDescs[i + 1].offStartInVDisk,
3779 paPartDescs[i + 1].cbData, paPartDescs[i + 1].pvPartitionData ? " (data)" : "");
3780 if (offLast >= cbSize)
3781 return vdIfError(pImage->pIfError, VERR_FILESYSTEM_CORRUPT /*?*/, RT_SRC_POS,
3782 N_("VMDK: Image path: '%s'. Partition descriptor #%u (%#RX64 LB %#RX64%s) goes beyond the end of the drive (%#RX64)"),
3783 pImage->pszFilename, i, paPartDescs[i].offStartInVDisk, paPartDescs[i].cbData,
3784 paPartDescs[i].pvPartitionData ? " (data)" : "", cbSize);
3785 }
3786
3787 return VINF_SUCCESS;
3788}
3789
3790
3791#ifdef RT_OS_LINUX
3792/**
3793 * Searches the dir specified in @a pszBlockDevDir for subdirectories with a
3794 * 'dev' file matching @a uDevToLocate.
3795 *
3796 * This is used both
3797 *
3798 * @returns IPRT status code, errors have been reported properly.
3799 * @param pImage For error reporting.
3800 * @param pszBlockDevDir Input: Path to the directory search under.
3801 * Output: Path to the directory containing information
3802 * for @a uDevToLocate.
3803 * @param cbBlockDevDir The size of the buffer @a pszBlockDevDir points to.
3804 * @param uDevToLocate The device number of the block device info dir to
3805 * locate.
3806 * @param pszDevToLocate For error reporting.
3807 */
3808static int vmdkFindSysBlockDevPath(PVMDKIMAGE pImage, char *pszBlockDevDir, size_t cbBlockDevDir,
3809 dev_t uDevToLocate, const char *pszDevToLocate)
3810{
3811 size_t const cchDir = RTPathEnsureTrailingSeparator(pszBlockDevDir, cbBlockDevDir);
3812 AssertReturn(cchDir > 0, VERR_BUFFER_OVERFLOW);
3813
3814 RTDIR hDir = NIL_RTDIR;
3815 int rc = RTDirOpen(&hDir, pszBlockDevDir);
3816 if (RT_SUCCESS(rc))
3817 {
3818 for (;;)
3819 {
3820 RTDIRENTRY Entry;
3821 rc = RTDirRead(hDir, &Entry, NULL);
3822 if (RT_SUCCESS(rc))
3823 {
3824 /* We're interested in directories and symlinks. */
3825 if ( Entry.enmType == RTDIRENTRYTYPE_DIRECTORY
3826 || Entry.enmType == RTDIRENTRYTYPE_SYMLINK
3827 || Entry.enmType == RTDIRENTRYTYPE_UNKNOWN)
3828 {
3829 rc = RTStrCopy(&pszBlockDevDir[cchDir], cbBlockDevDir - cchDir, Entry.szName);
3830 AssertContinue(RT_SUCCESS(rc)); /* should not happen! */
3831
3832 dev_t uThisDevNo = ~uDevToLocate;
3833 rc = RTLinuxSysFsReadDevNumFile(&uThisDevNo, "%s/dev", pszBlockDevDir);
3834 if (RT_SUCCESS(rc) && uThisDevNo == uDevToLocate)
3835 break;
3836 }
3837 }
3838 else
3839 {
3840 pszBlockDevDir[cchDir] = '\0';
3841 if (rc == VERR_NO_MORE_FILES)
3842 rc = vdIfError(pImage->pIfError, VERR_NOT_FOUND, RT_SRC_POS,
3843 N_("VMDK: Image path: '%s'. Failed to locate device corresponding to '%s' under '%s'"),
3844 pImage->pszFilename, pszDevToLocate, pszBlockDevDir);
3845 else
3846 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
3847 N_("VMDK: Image path: '%s'. RTDirRead failed enumerating '%s': %Rrc"),
3848 pImage->pszFilename, pszBlockDevDir, rc);
3849 break;
3850 }
3851 }
3852 RTDirClose(hDir);
3853 }
3854 else
3855 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
3856 N_("VMDK: Image path: '%s'. Failed to open dir '%s' for listing: %Rrc"),
3857 pImage->pszFilename, pszBlockDevDir, rc);
3858 return rc;
3859}
3860#endif /* RT_OS_LINUX */
3861
3862#ifdef RT_OS_FREEBSD
3863
3864
3865/**
3866 * Reads the config data from the provider and returns offset and size
3867 *
3868 * @return IPRT status code
3869 * @param pProvider GEOM provider representing partition
3870 * @param pcbOffset Placeholder for the offset of the partition
3871 * @param pcbSize Placeholder for the size of the partition
3872 */
3873static int vmdkReadPartitionsParamsFromProvider(gprovider *pProvider, uint64_t *pcbOffset, uint64_t *pcbSize)
3874{
3875 gconfig *pConfEntry;
3876 int rc = VERR_NOT_FOUND;
3877
3878 /*
3879 * Required parameters are located in the list containing key/value pairs.
3880 * Both key and value are in text form. Manuals tells nothing about the fact
3881 * that the both parameters should be present in the list. Thus, there are
3882 * cases when only one parameter is presented. To handle such cases we treat
3883 * absent params as zero allowing the caller decide the case is either correct
3884 * or an error.
3885 */
3886 uint64_t cbOffset = 0;
3887 uint64_t cbSize = 0;
3888 LIST_FOREACH(pConfEntry, &pProvider->lg_config, lg_config)
3889 {
3890 if (RTStrCmp(pConfEntry->lg_name, "offset") == 0)
3891 {
3892 cbOffset = RTStrToUInt64(pConfEntry->lg_val);
3893 rc = VINF_SUCCESS;
3894 }
3895 else if (RTStrCmp(pConfEntry->lg_name, "length") == 0)
3896 {
3897 cbSize = RTStrToUInt64(pConfEntry->lg_val);
3898 rc = VINF_SUCCESS;
3899 }
3900 }
3901 if (RT_SUCCESS(rc))
3902 {
3903 *pcbOffset = cbOffset;
3904 *pcbSize = cbSize;
3905 }
3906 return rc;
3907}
3908
3909
3910/**
3911 * Searches the partition specified by name and calculates its size and absolute offset.
3912 *
3913 * @return IPRT status code.
3914 * @param pParentClass Class containing pParentGeom
3915 * @param pszParentGeomName Name of the parent geom where we are looking for provider
3916 * @param pszProviderName Name of the provider we are looking for
3917 * @param pcbAbsoluteOffset Placeholder for the absolute offset of the partition, i.e. offset from the beginning of the disk
3918 * @param psbSize Placeholder for the size of the partition.
3919 */
3920static int vmdkFindPartitionParamsByName(gclass *pParentClass, const char *pszParentGeomName, const char *pszProviderName,
3921 uint64_t *pcbAbsoluteOffset, uint64_t *pcbSize)
3922{
3923 AssertReturn(pParentClass, VERR_INVALID_PARAMETER);
3924 AssertReturn(pszParentGeomName, VERR_INVALID_PARAMETER);
3925 AssertReturn(pszProviderName, VERR_INVALID_PARAMETER);
3926 AssertReturn(pcbAbsoluteOffset, VERR_INVALID_PARAMETER);
3927 AssertReturn(pcbSize, VERR_INVALID_PARAMETER);
3928
3929 ggeom *pParentGeom;
3930 int rc = VERR_NOT_FOUND;
3931 LIST_FOREACH(pParentGeom, &pParentClass->lg_geom, lg_geom)
3932 {
3933 if (RTStrCmp(pParentGeom->lg_name, pszParentGeomName) == 0)
3934 {
3935 rc = VINF_SUCCESS;
3936 break;
3937 }
3938 }
3939 if (RT_FAILURE(rc))
3940 return rc;
3941
3942 gprovider *pProvider;
3943 /*
3944 * First, go over providers without handling EBR or BSDLabel
3945 * partitions for case when looking provider is child
3946 * of the givng geom, to reduce searching time
3947 */
3948 LIST_FOREACH(pProvider, &pParentGeom->lg_provider, lg_provider)
3949 {
3950 if (RTStrCmp(pProvider->lg_name, pszProviderName) == 0)
3951 return vmdkReadPartitionsParamsFromProvider(pProvider, pcbAbsoluteOffset, pcbSize);
3952 }
3953
3954 /*
3955 * No provider found. Go over the parent geom again
3956 * and make recursions if geom represents EBR or BSDLabel.
3957 * In this case given parent geom contains only EBR or BSDLabel
3958 * partition itself and their own partitions are in the separate
3959 * geoms. Also, partition offsets are relative to geom, so
3960 * we have to add offset from child provider with parent geoms
3961 * provider
3962 */
3963
3964 LIST_FOREACH(pProvider, &pParentGeom->lg_provider, lg_provider)
3965 {
3966 uint64_t cbOffset = 0;
3967 uint64_t cbSize = 0;
3968 rc = vmdkReadPartitionsParamsFromProvider(pProvider, &cbOffset, &cbSize);
3969 if (RT_FAILURE(rc))
3970 return rc;
3971
3972 uint64_t cbProviderOffset = 0;
3973 uint64_t cbProviderSize = 0;
3974 rc = vmdkFindPartitionParamsByName(pParentClass, pProvider->lg_name, pszProviderName, &cbProviderOffset, &cbProviderSize);
3975 if (RT_SUCCESS(rc))
3976 {
3977 *pcbAbsoluteOffset = cbOffset + cbProviderOffset;
3978 *pcbSize = cbProviderSize;
3979 return rc;
3980 }
3981 }
3982
3983 return VERR_NOT_FOUND;
3984}
3985#endif
3986
3987
3988/**
3989 * Attempts to verify the raw partition path.
3990 *
3991 * We don't want to trust RTDvm and the partition device node morphing blindly.
3992 */
3993static int vmdkRawDescVerifyPartitionPath(PVMDKIMAGE pImage, PVDISKRAWPARTDESC pPartDesc, uint32_t idxPartition,
3994 const char *pszRawDrive, RTFILE hRawDrive, uint32_t cbSector, RTDVMVOLUME hVol)
3995{
3996 RT_NOREF(pImage, pPartDesc, idxPartition, pszRawDrive, hRawDrive, cbSector, hVol);
3997
3998 /*
3999 * Try open the raw partition device.
4000 */
4001 RTFILE hRawPart = NIL_RTFILE;
4002 int rc = RTFileOpen(&hRawPart, pPartDesc->pszRawDevice, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
4003 if (RT_FAILURE(rc))
4004 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4005 N_("VMDK: Image path: '%s'. Failed to open partition #%u on '%s' via '%s' (%Rrc)"),
4006 pImage->pszFilename, idxPartition, pszRawDrive, pPartDesc->pszRawDevice, rc);
4007
4008 /*
4009 * Compare the partition UUID if we can get it.
4010 */
4011#ifdef RT_OS_WINDOWS
4012 DWORD cbReturned;
4013
4014 /* 1. Get the device numbers for both handles, they should have the same disk. */
4015 STORAGE_DEVICE_NUMBER DevNum1;
4016 RT_ZERO(DevNum1);
4017 if (!DeviceIoControl((HANDLE)RTFileToNative(hRawDrive), IOCTL_STORAGE_GET_DEVICE_NUMBER,
4018 NULL /*pvInBuffer*/, 0 /*cbInBuffer*/, &DevNum1, sizeof(DevNum1), &cbReturned, NULL /*pOverlapped*/))
4019 rc = vdIfError(pImage->pIfError, RTErrConvertFromWin32(GetLastError()), RT_SRC_POS,
4020 N_("VMDK: Image path: '%s'. IOCTL_STORAGE_GET_DEVICE_NUMBER failed on '%s': %u"),
4021 pImage->pszFilename, pszRawDrive, GetLastError());
4022
4023 STORAGE_DEVICE_NUMBER DevNum2;
4024 RT_ZERO(DevNum2);
4025 if (!DeviceIoControl((HANDLE)RTFileToNative(hRawPart), IOCTL_STORAGE_GET_DEVICE_NUMBER,
4026 NULL /*pvInBuffer*/, 0 /*cbInBuffer*/, &DevNum2, sizeof(DevNum2), &cbReturned, NULL /*pOverlapped*/))
4027 rc = vdIfError(pImage->pIfError, RTErrConvertFromWin32(GetLastError()), RT_SRC_POS,
4028 N_("VMDK: Image path: '%s'. IOCTL_STORAGE_GET_DEVICE_NUMBER failed on '%s': %u"),
4029 pImage->pszFilename, pPartDesc->pszRawDevice, GetLastError());
4030 if ( RT_SUCCESS(rc)
4031 && ( DevNum1.DeviceNumber != DevNum2.DeviceNumber
4032 || DevNum1.DeviceType != DevNum2.DeviceType))
4033 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4034 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s' (%#x != %#x || %#x != %#x)"),
4035 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4036 DevNum1.DeviceNumber, DevNum2.DeviceNumber, DevNum1.DeviceType, DevNum2.DeviceType);
4037 if (RT_SUCCESS(rc))
4038 {
4039 /* Get the partitions from the raw drive and match up with the volume info
4040 from RTDvm. The partition number is found in DevNum2. */
4041 DWORD cbNeeded = 0;
4042 if ( DeviceIoControl((HANDLE)RTFileToNative(hRawDrive), IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
4043 NULL /*pvInBuffer*/, 0 /*cbInBuffer*/, NULL, 0, &cbNeeded, NULL /*pOverlapped*/)
4044 || cbNeeded < RT_UOFFSETOF_DYN(DRIVE_LAYOUT_INFORMATION_EX, PartitionEntry[1]))
4045 cbNeeded = RT_UOFFSETOF_DYN(DRIVE_LAYOUT_INFORMATION_EX, PartitionEntry[64]);
4046 cbNeeded += sizeof(PARTITION_INFORMATION_EX) * 2; /* just in case */
4047 DRIVE_LAYOUT_INFORMATION_EX *pLayout = (DRIVE_LAYOUT_INFORMATION_EX *)RTMemTmpAllocZ(cbNeeded);
4048 if (pLayout)
4049 {
4050 cbReturned = 0;
4051 if (DeviceIoControl((HANDLE)RTFileToNative(hRawDrive), IOCTL_DISK_GET_DRIVE_LAYOUT_EX,
4052 NULL /*pvInBuffer*/, 0 /*cbInBuffer*/, pLayout, cbNeeded, &cbReturned, NULL /*pOverlapped*/))
4053 {
4054 /* Find the entry with the given partition number (it's not an index, array contains empty MBR entries ++). */
4055 unsigned iEntry = 0;
4056 while ( iEntry < pLayout->PartitionCount
4057 && pLayout->PartitionEntry[iEntry].PartitionNumber != DevNum2.PartitionNumber)
4058 iEntry++;
4059 if (iEntry < pLayout->PartitionCount)
4060 {
4061 /* Compare the basics */
4062 PARTITION_INFORMATION_EX const * const pLayoutEntry = &pLayout->PartitionEntry[iEntry];
4063 if (pLayoutEntry->StartingOffset.QuadPart != (int64_t)pPartDesc->offStartInVDisk)
4064 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4065 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': StartingOffset %RU64, expected %RU64"),
4066 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4067 pLayoutEntry->StartingOffset.QuadPart, pPartDesc->offStartInVDisk);
4068 else if (pLayoutEntry->PartitionLength.QuadPart != (int64_t)pPartDesc->cbData)
4069 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4070 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': PartitionLength %RU64, expected %RU64"),
4071 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4072 pLayoutEntry->PartitionLength.QuadPart, pPartDesc->cbData);
4073 /** @todo We could compare the MBR type, GPT type and ID. */
4074 RT_NOREF(hVol);
4075 }
4076 else
4077 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4078 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': PartitionCount (%#x vs %#x)"),
4079 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4080 DevNum2.PartitionNumber, pLayout->PartitionCount);
4081# ifndef LOG_ENABLED
4082 if (RT_FAILURE(rc))
4083# endif
4084 {
4085 LogRel(("VMDK: Windows reports %u partitions for '%s':\n", pLayout->PartitionCount, pszRawDrive));
4086 PARTITION_INFORMATION_EX const *pEntry = &pLayout->PartitionEntry[0];
4087 for (DWORD i = 0; i < pLayout->PartitionCount; i++, pEntry++)
4088 {
4089 LogRel(("VMDK: #%u/%u: %016RU64 LB %016RU64 style=%d rewrite=%d",
4090 i, pEntry->PartitionNumber, pEntry->StartingOffset.QuadPart, pEntry->PartitionLength.QuadPart,
4091 pEntry->PartitionStyle, pEntry->RewritePartition));
4092 if (pEntry->PartitionStyle == PARTITION_STYLE_MBR)
4093 LogRel((" type=%#x boot=%d rec=%d hidden=%u\n", pEntry->Mbr.PartitionType, pEntry->Mbr.BootIndicator,
4094 pEntry->Mbr.RecognizedPartition, pEntry->Mbr.HiddenSectors));
4095 else if (pEntry->PartitionStyle == PARTITION_STYLE_GPT)
4096 LogRel((" type=%RTuuid id=%RTuuid aatrib=%RX64 name=%.36ls\n", &pEntry->Gpt.PartitionType,
4097 &pEntry->Gpt.PartitionId, pEntry->Gpt.Attributes, &pEntry->Gpt.Name[0]));
4098 else
4099 LogRel(("\n"));
4100 }
4101 LogRel(("VMDK: Looked for partition #%u (%u, '%s') at %RU64 LB %RU64\n", DevNum2.PartitionNumber,
4102 idxPartition, pPartDesc->pszRawDevice, pPartDesc->offStartInVDisk, pPartDesc->cbData));
4103 }
4104 }
4105 else
4106 rc = vdIfError(pImage->pIfError, RTErrConvertFromWin32(GetLastError()), RT_SRC_POS,
4107 N_("VMDK: Image path: '%s'. IOCTL_DISK_GET_DRIVE_LAYOUT_EX failed on '%s': %u (cb %u, cbRet %u)"),
4108 pImage->pszFilename, pPartDesc->pszRawDevice, GetLastError(), cbNeeded, cbReturned);
4109 RTMemTmpFree(pLayout);
4110 }
4111 else
4112 rc = VERR_NO_TMP_MEMORY;
4113 }
4114
4115#elif defined(RT_OS_LINUX)
4116 RT_NOREF(hVol);
4117
4118 /* Stat the two devices first to get their device numbers. (We probably
4119 could make some assumptions here about the major & minor number assignments
4120 for legacy nodes, but it doesn't hold up for nvme, so we'll skip that.) */
4121 struct stat StDrive, StPart;
4122 if (fstat((int)RTFileToNative(hRawDrive), &StDrive) != 0)
4123 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4124 N_("VMDK: Image path: '%s'. fstat failed on '%s': %d"), pImage->pszFilename, pszRawDrive, errno);
4125 else if (fstat((int)RTFileToNative(hRawPart), &StPart) != 0)
4126 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4127 N_("VMDK: Image path: '%s'. fstat failed on '%s': %d"), pImage->pszFilename, pPartDesc->pszRawDevice, errno);
4128 else
4129 {
4130 /* Scan the directories immediately under /sys/block/ for one with a
4131 'dev' file matching the drive's device number: */
4132 char szSysPath[RTPATH_MAX];
4133 rc = RTLinuxConstructPath(szSysPath, sizeof(szSysPath), "block/");
4134 AssertRCReturn(rc, rc); /* this shall not fail */
4135 if (RTDirExists(szSysPath))
4136 {
4137 rc = vmdkFindSysBlockDevPath(pImage, szSysPath, sizeof(szSysPath), StDrive.st_rdev, pszRawDrive);
4138
4139 /* Now, scan the directories under that again for a partition device
4140 matching the hRawPart device's number: */
4141 if (RT_SUCCESS(rc))
4142 rc = vmdkFindSysBlockDevPath(pImage, szSysPath, sizeof(szSysPath), StPart.st_rdev, pPartDesc->pszRawDevice);
4143
4144 /* Having found the /sys/block/device/partition/ path, we can finally
4145 read the partition attributes and compare with hVol. */
4146 if (RT_SUCCESS(rc))
4147 {
4148 /* partition number: */
4149 int64_t iLnxPartition = 0;
4150 rc = RTLinuxSysFsReadIntFile(10, &iLnxPartition, "%s/partition", szSysPath);
4151 if (RT_SUCCESS(rc) && iLnxPartition != idxPartition)
4152 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4153 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Partition number %RI64, expected %RU32"),
4154 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, iLnxPartition, idxPartition);
4155 /* else: ignore failure? */
4156
4157 /* start offset: */
4158 uint32_t const cbLnxSector = 512; /* It's hardcoded in the Linux kernel */
4159 if (RT_SUCCESS(rc))
4160 {
4161 int64_t offLnxStart = -1;
4162 rc = RTLinuxSysFsReadIntFile(10, &offLnxStart, "%s/start", szSysPath);
4163 offLnxStart *= cbLnxSector;
4164 if (RT_SUCCESS(rc) && offLnxStart != (int64_t)pPartDesc->offStartInVDisk)
4165 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4166 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Start offset %RI64, expected %RU64"),
4167 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, offLnxStart, pPartDesc->offStartInVDisk);
4168 /* else: ignore failure? */
4169 }
4170
4171 /* the size: */
4172 if (RT_SUCCESS(rc))
4173 {
4174 int64_t cbLnxData = -1;
4175 rc = RTLinuxSysFsReadIntFile(10, &cbLnxData, "%s/size", szSysPath);
4176 cbLnxData *= cbLnxSector;
4177 if (RT_SUCCESS(rc) && cbLnxData != (int64_t)pPartDesc->cbData)
4178 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4179 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Size %RI64, expected %RU64"),
4180 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbLnxData, pPartDesc->cbData);
4181 /* else: ignore failure? */
4182 }
4183 }
4184 }
4185 /* else: We've got nothing to work on, so only do content comparison. */
4186 }
4187
4188#elif defined(RT_OS_FREEBSD)
4189 char szDriveDevName[256];
4190 char* pszDevName = fdevname_r(RTFileToNative(hRawDrive), szDriveDevName, 256);
4191 if (pszDevName == NULL)
4192 rc = vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
4193 N_("VMDK: Image path: '%s'. '%s' is not a drive path"), pImage->pszFilename, pszRawDrive);
4194 char szPartDevName[256];
4195 if (RT_SUCCESS(rc))
4196 {
4197 pszDevName = fdevname_r(RTFileToNative(hRawPart), szPartDevName, 256);
4198 if (pszDevName == NULL)
4199 rc = vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
4200 N_("VMDK: Image path: '%s'. '%s' is not a partition path"), pImage->pszFilename, pPartDesc->pszRawDevice);
4201 }
4202 if (RT_SUCCESS(rc))
4203 {
4204 gmesh geomMesh;
4205 int err = geom_gettree(&geomMesh);
4206 if (err == 0)
4207 {
4208 /* Find root class containg partitions info */
4209 gclass* pPartClass;
4210 LIST_FOREACH(pPartClass, &geomMesh.lg_class, lg_class)
4211 {
4212 if (RTStrCmp(pPartClass->lg_name, "PART") == 0)
4213 break;
4214 }
4215 if (pPartClass == NULL || RTStrCmp(pPartClass->lg_name, "PART") != 0)
4216 rc = vdIfError(pImage->pIfError, VERR_GENERAL_FAILURE, RT_SRC_POS,
4217 N_("VMDK: Image path: '%s'. 'PART' class not found in the GEOM tree"), pImage->pszFilename);
4218
4219
4220 if (RT_SUCCESS(rc))
4221 {
4222 /* Find provider representing partition device */
4223 uint64_t cbOffset;
4224 uint64_t cbSize;
4225 rc = vmdkFindPartitionParamsByName(pPartClass, szDriveDevName, szPartDevName, &cbOffset, &cbSize);
4226 if (RT_SUCCESS(rc))
4227 {
4228 if (cbOffset != pPartDesc->offStartInVDisk)
4229 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4230 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Start offset %RU64, expected %RU64"),
4231 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbOffset, pPartDesc->offStartInVDisk);
4232 if (cbSize != pPartDesc->cbData)
4233 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4234 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Size %RU64, expected %RU64"),
4235 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbSize, pPartDesc->cbData);
4236 }
4237 else
4238 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4239 N_("VMDK: Image path: '%s'. Error getting geom provider for the partition '%s' of the drive '%s' in the GEOM tree: %Rrc"),
4240 pImage->pszFilename, pPartDesc->pszRawDevice, pszRawDrive, rc);
4241 }
4242
4243 geom_deletetree(&geomMesh);
4244 }
4245 else
4246 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(err), RT_SRC_POS,
4247 N_("VMDK: Image path: '%s'. geom_gettree failed: %d"), pImage->pszFilename, err);
4248 }
4249
4250#elif defined(RT_OS_SOLARIS)
4251 RT_NOREF(hVol);
4252
4253 dk_cinfo dkiDriveInfo;
4254 dk_cinfo dkiPartInfo;
4255 if (ioctl(RTFileToNative(hRawDrive), DKIOCINFO, (caddr_t)&dkiDriveInfo) == -1)
4256 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4257 N_("VMDK: Image path: '%s'. DKIOCINFO failed on '%s': %d"), pImage->pszFilename, pszRawDrive, errno);
4258 else if (ioctl(RTFileToNative(hRawPart), DKIOCINFO, (caddr_t)&dkiPartInfo) == -1)
4259 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4260 N_("VMDK: Image path: '%s'. DKIOCINFO failed on '%s': %d"), pImage->pszFilename, pszRawDrive, errno);
4261 else if ( dkiDriveInfo.dki_ctype != dkiPartInfo.dki_ctype
4262 || dkiDriveInfo.dki_cnum != dkiPartInfo.dki_cnum
4263 || dkiDriveInfo.dki_addr != dkiPartInfo.dki_addr
4264 || dkiDriveInfo.dki_unit != dkiPartInfo.dki_unit
4265 || dkiDriveInfo.dki_slave != dkiPartInfo.dki_slave)
4266 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4267 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s' (%#x != %#x || %#x != %#x || %#x != %#x || %#x != %#x || %#x != %#x)"),
4268 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4269 dkiDriveInfo.dki_ctype, dkiPartInfo.dki_ctype, dkiDriveInfo.dki_cnum, dkiPartInfo.dki_cnum,
4270 dkiDriveInfo.dki_addr, dkiPartInfo.dki_addr, dkiDriveInfo.dki_unit, dkiPartInfo.dki_unit,
4271 dkiDriveInfo.dki_slave, dkiPartInfo.dki_slave);
4272 else
4273 {
4274 uint64_t cbOffset = 0;
4275 uint64_t cbSize = 0;
4276 dk_gpt *pEfi = NULL;
4277 int idxEfiPart = efi_alloc_and_read(RTFileToNative(hRawPart), &pEfi);
4278 if (idxEfiPart >= 0)
4279 {
4280 if ((uint32_t)dkiPartInfo.dki_partition + 1 == idxPartition)
4281 {
4282 cbOffset = pEfi->efi_parts[idxEfiPart].p_start * pEfi->efi_lbasize;
4283 cbSize = pEfi->efi_parts[idxEfiPart].p_size * pEfi->efi_lbasize;
4284 }
4285 else
4286 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4287 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s' (%#x != %#x)"),
4288 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4289 idxPartition, (uint32_t)dkiPartInfo.dki_partition + 1);
4290 efi_free(pEfi);
4291 }
4292 else
4293 {
4294 /*
4295 * Manual says the efi_alloc_and_read returns VT_EINVAL if no EFI partition table found.
4296 * Actually, the function returns any error, e.g. VT_ERROR. Thus, we are not sure, is it
4297 * real error or just no EFI table found. Therefore, let's try to obtain partition info
4298 * using another way. If there is an error, it returns errno which will be handled below.
4299 */
4300
4301 uint32_t numPartition = (uint32_t)dkiPartInfo.dki_partition;
4302 if (numPartition > NDKMAP)
4303 numPartition -= NDKMAP;
4304 if (numPartition != idxPartition)
4305 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4306 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s' (%#x != %#x)"),
4307 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4308 idxPartition, numPartition);
4309 else
4310 {
4311 dk_minfo_ext mediaInfo;
4312 if (ioctl(RTFileToNative(hRawPart), DKIOCGMEDIAINFOEXT, (caddr_t)&mediaInfo) == -1)
4313 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4314 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s'. Can not obtain partition info: %d"),
4315 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4316 else
4317 {
4318 extpart_info extPartInfo;
4319 if (ioctl(RTFileToNative(hRawPart), DKIOCEXTPARTINFO, (caddr_t)&extPartInfo) != -1)
4320 {
4321 cbOffset = (uint64_t)extPartInfo.p_start * mediaInfo.dki_lbsize;
4322 cbSize = (uint64_t)extPartInfo.p_length * mediaInfo.dki_lbsize;
4323 }
4324 else
4325 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4326 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s'. Can not obtain partition info: %d"),
4327 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4328 }
4329 }
4330 }
4331 if (RT_SUCCESS(rc) && cbOffset != pPartDesc->offStartInVDisk)
4332 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4333 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Start offset %RI64, expected %RU64"),
4334 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbOffset, pPartDesc->offStartInVDisk);
4335
4336 if (RT_SUCCESS(rc) && cbSize != pPartDesc->cbData)
4337 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4338 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Size %RI64, expected %RU64"),
4339 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbSize, pPartDesc->cbData);
4340 }
4341
4342#elif defined(RT_OS_DARWIN)
4343 /* Stat the drive get its device number. */
4344 struct stat StDrive;
4345 if (fstat((int)RTFileToNative(hRawDrive), &StDrive) != 0)
4346 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4347 N_("VMDK: Image path: '%s'. fstat failed on '%s' (errno=%d)"), pImage->pszFilename, pszRawDrive, errno);
4348 else
4349 {
4350 if (ioctl(RTFileToNative(hRawPart), DKIOCLOCKPHYSICALEXTENTS, NULL) == -1)
4351 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4352 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s': Unable to lock the partition (errno=%d)"),
4353 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4354 else
4355 {
4356 uint32_t cbBlockSize = 0;
4357 uint64_t cbOffset = 0;
4358 uint64_t cbSize = 0;
4359 if (ioctl(RTFileToNative(hRawPart), DKIOCGETBLOCKSIZE, (caddr_t)&cbBlockSize) == -1)
4360 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4361 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s': Unable to obtain the sector size of the partition (errno=%d)"),
4362 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4363 else if (ioctl(RTFileToNative(hRawPart), DKIOCGETBASE, (caddr_t)&cbOffset) == -1)
4364 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4365 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s': Unable to obtain the start offset of the partition (errno=%d)"),
4366 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4367 else if (ioctl(RTFileToNative(hRawPart), DKIOCGETBLOCKCOUNT, (caddr_t)&cbSize) == -1)
4368 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4369 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s': Unable to obtain the size of the partition (errno=%d)"),
4370 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4371 else
4372 {
4373 cbSize *= (uint64_t)cbBlockSize;
4374 dk_physical_extent_t dkPartExtent = {0};
4375 dkPartExtent.offset = 0;
4376 dkPartExtent.length = cbSize;
4377 if (ioctl(RTFileToNative(hRawPart), DKIOCGETPHYSICALEXTENT, (caddr_t)&dkPartExtent) == -1)
4378 rc = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4379 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s': Unable to obtain partition info (errno=%d)"),
4380 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4381 else
4382 {
4383 if (dkPartExtent.dev != StDrive.st_rdev)
4384 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4385 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Drive does not contain the partition"),
4386 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive);
4387 else if (cbOffset != pPartDesc->offStartInVDisk)
4388 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4389 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Start offset %RU64, expected %RU64"),
4390 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbOffset, pPartDesc->offStartInVDisk);
4391 else if (cbSize != pPartDesc->cbData)
4392 rc = vdIfError(pImage->pIfError, VERR_MISMATCH, RT_SRC_POS,
4393 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s': Size %RU64, expected %RU64"),
4394 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cbSize, pPartDesc->cbData);
4395 }
4396 }
4397
4398 if (ioctl(RTFileToNative(hRawPart), DKIOCUNLOCKPHYSICALEXTENTS, NULL) == -1)
4399 {
4400 int rc2 = vdIfError(pImage->pIfError, RTErrConvertFromErrno(errno), RT_SRC_POS,
4401 N_("VMDK: Image path: '%s'. Partition #%u number ('%s') verification failed on '%s': Unable to unlock the partition (errno=%d)"),
4402 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, errno);
4403 if (RT_SUCCESS(rc))
4404 rc = rc2;
4405 }
4406 }
4407 }
4408
4409#else
4410 RT_NOREF(hVol); /* PORTME */
4411 rc = VERR_NOT_SUPPORTED;
4412#endif
4413 if (RT_SUCCESS(rc))
4414 {
4415 /*
4416 * Compare the first 32 sectors of the partition.
4417 *
4418 * This might not be conclusive, but for partitions formatted with the more
4419 * common file systems it should be as they have a superblock copy at or near
4420 * the start of the partition (fat, fat32, ntfs, and ext4 does at least).
4421 */
4422 size_t const cbToCompare = (size_t)RT_MIN(pPartDesc->cbData / cbSector, 32) * cbSector;
4423 uint8_t *pbSector1 = (uint8_t *)RTMemTmpAlloc(cbToCompare * 2);
4424 if (pbSector1 != NULL)
4425 {
4426 uint8_t *pbSector2 = pbSector1 + cbToCompare;
4427
4428 /* Do the comparing, we repeat if it fails and the data might be volatile. */
4429 uint64_t uPrevCrc1 = 0;
4430 uint64_t uPrevCrc2 = 0;
4431 uint32_t cStable = 0;
4432 for (unsigned iTry = 0; iTry < 256; iTry++)
4433 {
4434 rc = RTFileReadAt(hRawDrive, pPartDesc->offStartInVDisk, pbSector1, cbToCompare, NULL);
4435 if (RT_SUCCESS(rc))
4436 {
4437 rc = RTFileReadAt(hRawPart, pPartDesc->offStartInDevice, pbSector2, cbToCompare, NULL);
4438 if (RT_SUCCESS(rc))
4439 {
4440 if (memcmp(pbSector1, pbSector2, cbToCompare) != 0)
4441 {
4442 rc = VERR_MISMATCH;
4443
4444 /* Do data stability checks before repeating: */
4445 uint64_t const uCrc1 = RTCrc64(pbSector1, cbToCompare);
4446 uint64_t const uCrc2 = RTCrc64(pbSector2, cbToCompare);
4447 if ( uPrevCrc1 != uCrc1
4448 || uPrevCrc2 != uCrc2)
4449 cStable = 0;
4450 else if (++cStable > 4)
4451 break;
4452 uPrevCrc1 = uCrc1;
4453 uPrevCrc2 = uCrc2;
4454 continue;
4455 }
4456 rc = VINF_SUCCESS;
4457 }
4458 else
4459 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4460 N_("VMDK: Image path: '%s'. Error reading %zu bytes from '%s' at offset %RU64 (%Rrc)"),
4461 pImage->pszFilename, cbToCompare, pPartDesc->pszRawDevice, pPartDesc->offStartInDevice, rc);
4462 }
4463 else
4464 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4465 N_("VMDK: Image path: '%s'. Error reading %zu bytes from '%s' at offset %RU64 (%Rrc)"),
4466 pImage->pszFilename, cbToCompare, pszRawDrive, pPartDesc->offStartInVDisk, rc);
4467 break;
4468 }
4469 if (rc == VERR_MISMATCH)
4470 {
4471 /* Find the first mismatching bytes: */
4472 size_t offMissmatch = 0;
4473 while (offMissmatch < cbToCompare && pbSector1[offMissmatch] == pbSector2[offMissmatch])
4474 offMissmatch++;
4475 int cbSample = (int)RT_MIN(cbToCompare - offMissmatch, 16);
4476
4477 if (cStable > 0)
4478 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4479 N_("VMDK: Image path: '%s'. Partition #%u path ('%s') verification failed on '%s' (cStable=%d @%#zx: %.*Rhxs vs %.*Rhxs)"),
4480 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive, cStable,
4481 offMissmatch, cbSample, &pbSector1[offMissmatch], cbSample, &pbSector2[offMissmatch]);
4482 else
4483 {
4484 LogRel(("VMDK: Image path: '%s'. Partition #%u path ('%s') verification undecided on '%s' because of unstable data! (@%#zx: %.*Rhxs vs %.*Rhxs)\n",
4485 pImage->pszFilename, idxPartition, pPartDesc->pszRawDevice, pszRawDrive,
4486 offMissmatch, cbSample, &pbSector1[offMissmatch], cbSample, &pbSector2[offMissmatch]));
4487 rc = -rc;
4488 }
4489 }
4490
4491 RTMemTmpFree(pbSector1);
4492 }
4493 else
4494 rc = vdIfError(pImage->pIfError, VERR_NO_TMP_MEMORY, RT_SRC_POS,
4495 N_("VMDK: Image path: '%s'. Failed to allocate %zu bytes for a temporary read buffer\n"),
4496 pImage->pszFilename, cbToCompare * 2);
4497 }
4498 RTFileClose(hRawPart);
4499 return rc;
4500}
4501
4502#ifdef RT_OS_WINDOWS
4503/**
4504 * Construct the device name for the given partition number.
4505 */
4506static int vmdkRawDescWinMakePartitionName(PVMDKIMAGE pImage, const char *pszRawDrive, RTFILE hRawDrive, uint32_t idxPartition,
4507 char **ppszRawPartition)
4508{
4509 int rc = VINF_SUCCESS;
4510 DWORD cbReturned = 0;
4511 STORAGE_DEVICE_NUMBER DevNum;
4512 RT_ZERO(DevNum);
4513 if (DeviceIoControl((HANDLE)RTFileToNative(hRawDrive), IOCTL_STORAGE_GET_DEVICE_NUMBER,
4514 NULL /*pvInBuffer*/, 0 /*cbInBuffer*/, &DevNum, sizeof(DevNum), &cbReturned, NULL /*pOverlapped*/))
4515 RTStrAPrintf(ppszRawPartition, "\\\\.\\Harddisk%uPartition%u", DevNum.DeviceNumber, idxPartition);
4516 else
4517 rc = vdIfError(pImage->pIfError, RTErrConvertFromWin32(GetLastError()), RT_SRC_POS,
4518 N_("VMDK: Image path: '%s'. IOCTL_STORAGE_GET_DEVICE_NUMBER failed on '%s': %u"),
4519 pImage->pszFilename, pszRawDrive, GetLastError());
4520 return rc;
4521}
4522#endif /* RT_OS_WINDOWS */
4523
4524/**
4525 * Worker for vmdkMakeRawDescriptor that adds partition descriptors when the
4526 * 'Partitions' configuration value is present.
4527 *
4528 * @returns VBox status code, error message has been set on failure.
4529 *
4530 * @note Caller is assumed to clean up @a pRawDesc and release
4531 * @a *phVolToRelease.
4532 * @internal
4533 */
4534static int vmdkRawDescDoPartitions(PVMDKIMAGE pImage, RTDVM hVolMgr, PVDISKRAW pRawDesc,
4535 RTFILE hRawDrive, const char *pszRawDrive, uint32_t cbSector,
4536 uint32_t fPartitions, uint32_t fPartitionsReadOnly, bool fRelative,
4537 PRTDVMVOLUME phVolToRelease)
4538{
4539 *phVolToRelease = NIL_RTDVMVOLUME;
4540
4541 /* Check sanity/understanding. */
4542 Assert(fPartitions);
4543 Assert((fPartitions & fPartitionsReadOnly) == fPartitionsReadOnly); /* RO should be a sub-set */
4544
4545 /*
4546 * Allocate on descriptor for each volume up front.
4547 */
4548 uint32_t const cVolumes = RTDvmMapGetValidVolumes(hVolMgr);
4549
4550 PVDISKRAWPARTDESC paPartDescs = NULL;
4551 int rc = vmdkRawDescAppendPartDesc(pImage, pRawDesc, cVolumes, &paPartDescs);
4552 AssertRCReturn(rc, rc);
4553
4554 /*
4555 * Enumerate the partitions (volumes) on the disk and create descriptors for each of them.
4556 */
4557 uint32_t fPartitionsLeft = fPartitions;
4558 RTDVMVOLUME hVol = NIL_RTDVMVOLUME; /* the current volume, needed for getting the next. */
4559 for (uint32_t i = 0; i < cVolumes; i++)
4560 {
4561 /*
4562 * Get the next/first volume and release the current.
4563 */
4564 RTDVMVOLUME hVolNext = NIL_RTDVMVOLUME;
4565 if (i == 0)
4566 rc = RTDvmMapQueryFirstVolume(hVolMgr, &hVolNext);
4567 else
4568 rc = RTDvmMapQueryNextVolume(hVolMgr, hVol, &hVolNext);
4569 if (RT_FAILURE(rc))
4570 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4571 N_("VMDK: Image path: '%s'. Volume enumeration failed at volume #%u on '%s' (%Rrc)"),
4572 pImage->pszFilename, i, pszRawDrive, rc);
4573 uint32_t cRefs = RTDvmVolumeRelease(hVol);
4574 Assert(cRefs != UINT32_MAX); RT_NOREF(cRefs);
4575 *phVolToRelease = hVol = hVolNext;
4576
4577 /*
4578 * Depending on the fPartitions selector and associated read-only mask,
4579 * the guest either gets read-write or read-only access (bits set)
4580 * or no access (selector bit clear, access directed to the VMDK).
4581 */
4582 paPartDescs[i].cbData = RTDvmVolumeGetSize(hVol);
4583
4584 uint64_t offVolumeEndIgnored = 0;
4585 rc = RTDvmVolumeQueryRange(hVol, &paPartDescs[i].offStartInVDisk, &offVolumeEndIgnored);
4586 if (RT_FAILURE(rc))
4587 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4588 N_("VMDK: Image path: '%s'. Failed to get location of volume #%u on '%s' (%Rrc)"),
4589 pImage->pszFilename, i, pszRawDrive, rc);
4590 Assert(paPartDescs[i].cbData == offVolumeEndIgnored + 1 - paPartDescs[i].offStartInVDisk);
4591
4592 /* Note! The index must match IHostDrivePartition::number. */
4593 uint32_t idxPartition = RTDvmVolumeGetIndex(hVol, RTDVMVOLIDX_HOST);
4594 if ( idxPartition < 32
4595 && (fPartitions & RT_BIT_32(idxPartition)))
4596 {
4597 fPartitionsLeft &= ~RT_BIT_32(idxPartition);
4598 if (fPartitionsReadOnly & RT_BIT_32(idxPartition))
4599 paPartDescs[i].uFlags |= VDISKRAW_READONLY;
4600
4601 if (!fRelative)
4602 {
4603 /*
4604 * Accessing the drive thru the main device node (pRawDesc->pszRawDisk).
4605 */
4606 paPartDescs[i].offStartInDevice = paPartDescs[i].offStartInVDisk;
4607 paPartDescs[i].pszRawDevice = RTStrDup(pszRawDrive);
4608 AssertPtrReturn(paPartDescs[i].pszRawDevice, VERR_NO_STR_MEMORY);
4609 }
4610 else
4611 {
4612 /*
4613 * Relative means access the partition data via the device node for that
4614 * partition, allowing the sysadmin/OS to allow a user access to individual
4615 * partitions without necessarily being able to compromise the host OS.
4616 * Obviously, the creation of the VMDK requires read access to the main
4617 * device node for the drive, but that's a one-time thing and can be done
4618 * by the sysadmin. Here data starts at offset zero in the device node.
4619 */
4620 paPartDescs[i].offStartInDevice = 0;
4621
4622#if defined(RT_OS_DARWIN) || defined(RT_OS_FREEBSD)
4623 /* /dev/rdisk1 -> /dev/rdisk1s2 (s=slice) */
4624 RTStrAPrintf(&paPartDescs[i].pszRawDevice, "%ss%u", pszRawDrive, idxPartition);
4625#elif defined(RT_OS_LINUX)
4626 /* Two naming schemes here: /dev/nvme0n1 -> /dev/nvme0n1p1; /dev/sda -> /dev/sda1 */
4627 RTStrAPrintf(&paPartDescs[i].pszRawDevice,
4628 RT_C_IS_DIGIT(pszRawDrive[strlen(pszRawDrive) - 1]) ? "%sp%u" : "%s%u", pszRawDrive, idxPartition);
4629#elif defined(RT_OS_WINDOWS)
4630 rc = vmdkRawDescWinMakePartitionName(pImage, pszRawDrive, hRawDrive, idxPartition, &paPartDescs[i].pszRawDevice);
4631 AssertRCReturn(rc, rc);
4632#elif defined(RT_OS_SOLARIS)
4633 if (pRawDesc->enmPartitioningType == VDISKPARTTYPE_MBR)
4634 {
4635 /*
4636 * MBR partitions have device nodes in form /dev/(r)dsk/cXtYdZpK
4637 * where X is the controller,
4638 * Y is target (SCSI device number),
4639 * Z is disk number,
4640 * K is partition number,
4641 * where p0 is the whole disk
4642 * p1-pN are the partitions of the disk
4643 */
4644 const char *pszRawDrivePath = pszRawDrive;
4645 char szDrivePath[RTPATH_MAX];
4646 size_t cbRawDrive = strlen(pszRawDrive);
4647 if ( cbRawDrive > 1 && strcmp(&pszRawDrive[cbRawDrive - 2], "p0") == 0)
4648 {
4649 memcpy(szDrivePath, pszRawDrive, cbRawDrive - 2);
4650 szDrivePath[cbRawDrive - 2] = '\0';
4651 pszRawDrivePath = szDrivePath;
4652 }
4653 RTStrAPrintf(&paPartDescs[i].pszRawDevice, "%sp%u", pszRawDrivePath, idxPartition);
4654 }
4655 else /* GPT */
4656 {
4657 /*
4658 * GPT partitions have device nodes in form /dev/(r)dsk/cXtYdZsK
4659 * where X is the controller,
4660 * Y is target (SCSI device number),
4661 * Z is disk number,
4662 * K is partition number, zero based. Can be only from 0 to 6.
4663 * Thus, only partitions numbered 0 through 6 have device nodes.
4664 */
4665 if (idxPartition > 7)
4666 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
4667 N_("VMDK: Image path: '%s'. the partition #%u on '%s' has no device node and can not be specified with 'Relative' property"),
4668 pImage->pszFilename, idxPartition, pszRawDrive);
4669 RTStrAPrintf(&paPartDescs[i].pszRawDevice, "%ss%u", pszRawDrive, idxPartition - 1);
4670 }
4671#else
4672 AssertFailedReturn(VERR_INTERNAL_ERROR_4); /* The option parsing code should have prevented this - PORTME */
4673#endif
4674 AssertPtrReturn(paPartDescs[i].pszRawDevice, VERR_NO_STR_MEMORY);
4675
4676 rc = vmdkRawDescVerifyPartitionPath(pImage, &paPartDescs[i], idxPartition, pszRawDrive, hRawDrive, cbSector, hVol);
4677 AssertRCReturn(rc, rc);
4678 }
4679 }
4680 else
4681 {
4682 /* Not accessible to the guest. */
4683 paPartDescs[i].offStartInDevice = 0;
4684 paPartDescs[i].pszRawDevice = NULL;
4685 }
4686 } /* for each volume */
4687
4688 RTDvmVolumeRelease(hVol);
4689 *phVolToRelease = NIL_RTDVMVOLUME;
4690
4691 /*
4692 * Check that we found all the partitions the user selected.
4693 */
4694 if (fPartitionsLeft)
4695 {
4696 char szLeft[3 * sizeof(fPartitions) * 8];
4697 size_t cchLeft = 0;
4698 for (unsigned i = 0; i < sizeof(fPartitions) * 8; i++)
4699 if (fPartitionsLeft & RT_BIT_32(i))
4700 cchLeft += RTStrPrintf(&szLeft[cchLeft], sizeof(szLeft) - cchLeft, cchLeft ? "%u" : ",%u", i);
4701 return vdIfError(pImage->pIfError, VERR_INVALID_PARAMETER, RT_SRC_POS,
4702 N_("VMDK: Image path: '%s'. Not all the specified partitions for drive '%s' was found: %s"),
4703 pImage->pszFilename, pszRawDrive, szLeft);
4704 }
4705
4706 return VINF_SUCCESS;
4707}
4708
4709/**
4710 * Worker for vmdkMakeRawDescriptor that adds partition descriptors with copies
4711 * of the partition tables and associated padding areas when the 'Partitions'
4712 * configuration value is present.
4713 *
4714 * The guest is not allowed access to the partition tables, however it needs
4715 * them to be able to access the drive. So, create descriptors for each of the
4716 * tables and attach the current disk content. vmdkCreateRawImage() will later
4717 * write the content to the VMDK. Any changes the guest later makes to the
4718 * partition tables will then go to the VMDK copy, rather than the host drive.
4719 *
4720 * @returns VBox status code, error message has been set on failure.
4721 *
4722 * @note Caller is assumed to clean up @a pRawDesc
4723 * @internal
4724 */
4725static int vmdkRawDescDoCopyPartitionTables(PVMDKIMAGE pImage, RTDVM hVolMgr, PVDISKRAW pRawDesc,
4726 const char *pszRawDrive, RTFILE hRawDrive, void *pvBootSector, size_t cbBootSector)
4727{
4728 /*
4729 * Query the locations.
4730 */
4731 /* Determin how many locations there are: */
4732 size_t cLocations = 0;
4733 int rc = RTDvmMapQueryTableLocations(hVolMgr, RTDVMMAPQTABLOC_F_INCLUDE_LEGACY, NULL, 0, &cLocations);
4734 if (rc != VERR_BUFFER_OVERFLOW)
4735 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4736 N_("VMDK: Image path: '%s'. RTDvmMapQueryTableLocations failed on '%s' (%Rrc)"),
4737 pImage->pszFilename, pszRawDrive, rc);
4738 AssertReturn(cLocations > 0 && cLocations < _16M, VERR_INTERNAL_ERROR_5);
4739
4740 /* We can allocate the partition descriptors here to save an intentation level. */
4741 PVDISKRAWPARTDESC paPartDescs = NULL;
4742 rc = vmdkRawDescAppendPartDesc(pImage, pRawDesc, (uint32_t)cLocations, &paPartDescs);
4743 AssertRCReturn(rc, rc);
4744
4745 /* Allocate the result table and repeat the location table query: */
4746 PRTDVMTABLELOCATION paLocations = (PRTDVMTABLELOCATION)RTMemAllocZ(sizeof(paLocations[0]) * cLocations);
4747 if (!paLocations)
4748 return vdIfError(pImage->pIfError, VERR_NO_MEMORY, RT_SRC_POS, N_("VMDK: Image path: '%s'. Failed to allocate %zu bytes"),
4749 pImage->pszFilename, sizeof(paLocations[0]) * cLocations);
4750 rc = RTDvmMapQueryTableLocations(hVolMgr, RTDVMMAPQTABLOC_F_INCLUDE_LEGACY, paLocations, cLocations, NULL);
4751 if (RT_SUCCESS(rc))
4752 {
4753 /*
4754 * Translate them into descriptors.
4755 *
4756 * We restrict the amount of partition alignment padding to 4MiB as more
4757 * will just be a waste of space. The use case for including the padding
4758 * are older boot loaders and boot manager (including one by a team member)
4759 * that put data and code in the 62 sectors between the MBR and the first
4760 * partition (total of 63). Later CHS was abandond and partition started
4761 * being aligned on power of two sector boundraries (typically 64KiB or
4762 * 1MiB depending on the media size).
4763 */
4764 for (size_t i = 0; i < cLocations && RT_SUCCESS(rc); i++)
4765 {
4766 Assert(paLocations[i].cb > 0);
4767 if (paLocations[i].cb <= _64M)
4768 {
4769 /* Create the partition descriptor entry: */
4770 //paPartDescs[i].pszRawDevice = NULL;
4771 //paPartDescs[i].offStartInDevice = 0;
4772 //paPartDescs[i].uFlags = 0;
4773 paPartDescs[i].offStartInVDisk = paLocations[i].off;
4774 paPartDescs[i].cbData = paLocations[i].cb;
4775 if (paPartDescs[i].cbData < _4M)
4776 paPartDescs[i].cbData = RT_MIN(paPartDescs[i].cbData + paLocations[i].cbPadding, _4M);
4777 paPartDescs[i].pvPartitionData = RTMemAllocZ((size_t)paPartDescs[i].cbData);
4778 if (paPartDescs[i].pvPartitionData)
4779 {
4780 /* Read the content from the drive: */
4781 rc = RTFileReadAt(hRawDrive, paPartDescs[i].offStartInVDisk, paPartDescs[i].pvPartitionData,
4782 (size_t)paPartDescs[i].cbData, NULL);
4783 if (RT_SUCCESS(rc))
4784 {
4785 /* Do we have custom boot sector code? */
4786 if (pvBootSector && cbBootSector && paPartDescs[i].offStartInVDisk == 0)
4787 {
4788 /* Note! Old code used to quietly drop the bootsector if it was considered too big.
4789 Instead we fail as we weren't able to do what the user requested us to do.
4790 Better if the user knows than starts questioning why the guest isn't
4791 booting as expected. */
4792 if (cbBootSector <= paPartDescs[i].cbData)
4793 memcpy(paPartDescs[i].pvPartitionData, pvBootSector, cbBootSector);
4794 else
4795 rc = vdIfError(pImage->pIfError, VERR_TOO_MUCH_DATA, RT_SRC_POS,
4796 N_("VMDK: Image path: '%s'. The custom boot sector is too big: %zu bytes, %RU64 bytes available"),
4797 pImage->pszFilename, cbBootSector, paPartDescs[i].cbData);
4798 }
4799 }
4800 else
4801 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4802 N_("VMDK: Image path: '%s'. Failed to read partition at off %RU64 length %zu from '%s' (%Rrc)"),
4803 pImage->pszFilename, paPartDescs[i].offStartInVDisk,
4804 (size_t)paPartDescs[i].cbData, pszRawDrive, rc);
4805 }
4806 else
4807 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4808 N_("VMDK: Image path: '%s'. Failed to allocate %zu bytes for copying the partition table at off %RU64"),
4809 pImage->pszFilename, (size_t)paPartDescs[i].cbData, paPartDescs[i].offStartInVDisk);
4810 }
4811 else
4812 rc = vdIfError(pImage->pIfError, VERR_TOO_MUCH_DATA, RT_SRC_POS,
4813 N_("VMDK: Image path: '%s'. Partition table #%u at offset %RU64 in '%s' is to big: %RU64 bytes"),
4814 pImage->pszFilename, i, paLocations[i].off, pszRawDrive, paLocations[i].cb);
4815 }
4816 }
4817 else
4818 rc = vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4819 N_("VMDK: Image path: '%s'. RTDvmMapQueryTableLocations failed on '%s' (%Rrc)"),
4820 pImage->pszFilename, pszRawDrive, rc);
4821 RTMemFree(paLocations);
4822 return rc;
4823}
4824
4825/**
4826 * Opens the volume manager for the raw drive when in selected-partition mode.
4827 *
4828 * @param pImage The VMDK image (for errors).
4829 * @param hRawDrive The raw drive handle.
4830 * @param pszRawDrive The raw drive device path (for errors).
4831 * @param cbSector The sector size.
4832 * @param phVolMgr Where to return the handle to the volume manager on
4833 * success.
4834 * @returns VBox status code, errors have been reported.
4835 * @internal
4836 */
4837static int vmdkRawDescOpenVolMgr(PVMDKIMAGE pImage, RTFILE hRawDrive, const char *pszRawDrive, uint32_t cbSector, PRTDVM phVolMgr)
4838{
4839 *phVolMgr = NIL_RTDVM;
4840
4841 RTVFSFILE hVfsFile = NIL_RTVFSFILE;
4842 int rc = RTVfsFileFromRTFile(hRawDrive, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE, true /*fLeaveOpen*/, &hVfsFile);
4843 if (RT_FAILURE(rc))
4844 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4845 N_("VMDK: Image path: '%s'. RTVfsFileFromRTFile failed for '%s' handle (%Rrc)"),
4846 pImage->pszFilename, pszRawDrive, rc);
4847
4848 RTDVM hVolMgr = NIL_RTDVM;
4849 rc = RTDvmCreate(&hVolMgr, hVfsFile, cbSector, 0 /*fFlags*/);
4850
4851 RTVfsFileRelease(hVfsFile);
4852
4853 if (RT_FAILURE(rc))
4854 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4855 N_("VMDK: Image path: '%s'. Failed to create volume manager instance for '%s' (%Rrc)"),
4856 pImage->pszFilename, pszRawDrive, rc);
4857
4858 rc = RTDvmMapOpen(hVolMgr);
4859 if (RT_SUCCESS(rc))
4860 {
4861 *phVolMgr = hVolMgr;
4862 return VINF_SUCCESS;
4863 }
4864 RTDvmRelease(hVolMgr);
4865 return vdIfError(pImage->pIfError, rc, RT_SRC_POS, N_("VMDK: Image path: '%s'. RTDvmMapOpen failed for '%s' (%Rrc)"),
4866 pImage->pszFilename, pszRawDrive, rc);
4867}
4868
4869/**
4870 * Opens the raw drive device and get the sizes for it.
4871 *
4872 * @param pImage The image (for error reporting).
4873 * @param pszRawDrive The device/whatever to open.
4874 * @param phRawDrive Where to return the file handle.
4875 * @param pcbRawDrive Where to return the size.
4876 * @param pcbSector Where to return the sector size.
4877 * @returns IPRT status code, errors have been reported.
4878 * @internal
4879 */
4880static int vmkdRawDescOpenDevice(PVMDKIMAGE pImage, const char *pszRawDrive,
4881 PRTFILE phRawDrive, uint64_t *pcbRawDrive, uint32_t *pcbSector)
4882{
4883 /*
4884 * Open the device for the raw drive.
4885 */
4886 RTFILE hRawDrive = NIL_RTFILE;
4887 int rc = RTFileOpen(&hRawDrive, pszRawDrive, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
4888 if (RT_FAILURE(rc))
4889 return vdIfError(pImage->pIfError, rc, RT_SRC_POS,
4890 N_("VMDK: Image path: '%s'. Failed to open the raw drive '%s' for reading (%Rrc)"),