1 | /* $Id: VDICore.h 12639 2008-09-22 13:19:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtual Disk Image (VDI), Core Code Header (internal).
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef __VDICore_h__
|
---|
23 |
|
---|
24 |
|
---|
25 | /*******************************************************************************
|
---|
26 | * Header Files *
|
---|
27 | *******************************************************************************/
|
---|
28 | #ifndef VBOX_VDICORE_VD
|
---|
29 | #include <VBox/VBoxHDD.h>
|
---|
30 | #else /* VBOX_VDICORE_VD */
|
---|
31 | #include <VBox/VBoxHDD-new.h>
|
---|
32 | #endif /* VBOX_VDICORE_VD */
|
---|
33 | #include <VBox/pdm.h>
|
---|
34 | #include <VBox/mm.h>
|
---|
35 | #include <VBox/err.h>
|
---|
36 |
|
---|
37 | #include <VBox/log.h>
|
---|
38 | #include <iprt/alloc.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/uuid.h>
|
---|
41 | #include <iprt/file.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 | #include <iprt/asm.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Constants And Macros, Structures and Typedefs *
|
---|
48 | *******************************************************************************/
|
---|
49 |
|
---|
50 | /** Image info, not handled anyhow.
|
---|
51 | * Must be less than 64 bytes in length, including the trailing 0.
|
---|
52 | */
|
---|
53 | #define VDI_IMAGE_FILE_INFO "<<< Sun xVM VirtualBox Disk Image >>>\n"
|
---|
54 |
|
---|
55 | /** The Sector size.
|
---|
56 | * Currently we support only 512 bytes sectors.
|
---|
57 | */
|
---|
58 | #define VDI_GEOMETRY_SECTOR_SIZE (512)
|
---|
59 | /** 512 = 2^^9 */
|
---|
60 | #define VDI_GEOMETRY_SECTOR_SHIFT (9)
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Harddisk geometry.
|
---|
64 | */
|
---|
65 | #pragma pack(1)
|
---|
66 | typedef struct VDIDISKGEOMETRY
|
---|
67 | {
|
---|
68 | /** Cylinders. */
|
---|
69 | uint32_t cCylinders;
|
---|
70 | /** Heads. */
|
---|
71 | uint32_t cHeads;
|
---|
72 | /** Sectors per track. */
|
---|
73 | uint32_t cSectors;
|
---|
74 | /** Sector size. (bytes per sector) */
|
---|
75 | uint32_t cbSector;
|
---|
76 | } VDIDISKGEOMETRY, *PVDIDISKGEOMETRY;
|
---|
77 | #pragma pack()
|
---|
78 |
|
---|
79 | /** Image signature. */
|
---|
80 | #define VDI_IMAGE_SIGNATURE (0xbeda107f)
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Pre-Header to be stored in image file - used for version control.
|
---|
84 | */
|
---|
85 | #pragma pack(1)
|
---|
86 | typedef struct VDIPREHEADER
|
---|
87 | {
|
---|
88 | /** Just text info about image type, for eyes only. */
|
---|
89 | char szFileInfo[64];
|
---|
90 | /** The image signature (VDI_IMAGE_SIGNATURE). */
|
---|
91 | uint32_t u32Signature;
|
---|
92 | /** The image version (VDI_IMAGE_VERSION). */
|
---|
93 | uint32_t u32Version;
|
---|
94 | } VDIPREHEADER, *PVDIPREHEADER;
|
---|
95 | #pragma pack()
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Size of szComment field of HDD image header.
|
---|
99 | */
|
---|
100 | #define VDI_IMAGE_COMMENT_SIZE 256
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Header to be stored in image file, VDI_IMAGE_VERSION_MAJOR = 0.
|
---|
104 | * Prepended by VDIPREHEADER.
|
---|
105 | */
|
---|
106 | #pragma pack(1)
|
---|
107 | typedef struct VDIHEADER0
|
---|
108 | {
|
---|
109 | /** The image type (VDI_IMAGE_TYPE_*). */
|
---|
110 | uint32_t u32Type;
|
---|
111 | /** Image flags (VDI_IMAGE_FLAGS_*). */
|
---|
112 | uint32_t fFlags;
|
---|
113 | /** Image comment. (UTF-8) */
|
---|
114 | char szComment[VDI_IMAGE_COMMENT_SIZE];
|
---|
115 | /** Legacy image geometry (previous code stored PCHS there). */
|
---|
116 | VDIDISKGEOMETRY LegacyGeometry;
|
---|
117 | /** Size of disk (in bytes). */
|
---|
118 | uint64_t cbDisk;
|
---|
119 | /** Block size. (For instance VDI_IMAGE_BLOCK_SIZE.) */
|
---|
120 | uint32_t cbBlock;
|
---|
121 | /** Number of blocks. */
|
---|
122 | uint32_t cBlocks;
|
---|
123 | /** Number of allocated blocks. */
|
---|
124 | uint32_t cBlocksAllocated;
|
---|
125 | /** UUID of image. */
|
---|
126 | RTUUID uuidCreate;
|
---|
127 | /** UUID of image's last modification. */
|
---|
128 | RTUUID uuidModify;
|
---|
129 | /** Only for secondary images - UUID of primary image. */
|
---|
130 | RTUUID uuidLinkage;
|
---|
131 | } VDIHEADER0, *PVDIHEADER0;
|
---|
132 | #pragma pack()
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Header to be stored in image file, VDI_IMAGE_VERSION_MAJOR = 1,
|
---|
136 | * VDI_IMAGE_VERSION_MINOR = 1. Prepended by VDIPREHEADER.
|
---|
137 | */
|
---|
138 | #pragma pack(1)
|
---|
139 | typedef struct VDIHEADER1
|
---|
140 | {
|
---|
141 | /** Size of this structure in bytes. */
|
---|
142 | uint32_t cbHeader;
|
---|
143 | /** The image type (VDI_IMAGE_TYPE_*). */
|
---|
144 | uint32_t u32Type;
|
---|
145 | /** Image flags (VDI_IMAGE_FLAGS_*). */
|
---|
146 | uint32_t fFlags;
|
---|
147 | /** Image comment. (UTF-8) */
|
---|
148 | char szComment[VDI_IMAGE_COMMENT_SIZE];
|
---|
149 | /** Offset of Blocks array from the begining of image file.
|
---|
150 | * Should be sector-aligned for HDD access optimization. */
|
---|
151 | uint32_t offBlocks;
|
---|
152 | /** Offset of image data from the begining of image file.
|
---|
153 | * Should be sector-aligned for HDD access optimization. */
|
---|
154 | uint32_t offData;
|
---|
155 | /** Legacy image geometry (previous code stored PCHS there). */
|
---|
156 | VDIDISKGEOMETRY LegacyGeometry;
|
---|
157 | /** Was BIOS HDD translation mode, now unused. */
|
---|
158 | uint32_t u32Dummy;
|
---|
159 | /** Size of disk (in bytes). */
|
---|
160 | uint64_t cbDisk;
|
---|
161 | /** Block size. (For instance VDI_IMAGE_BLOCK_SIZE.) Should be a power of 2! */
|
---|
162 | uint32_t cbBlock;
|
---|
163 | /** Size of additional service information of every data block.
|
---|
164 | * Prepended before block data. May be 0.
|
---|
165 | * Should be a power of 2 and sector-aligned for optimization reasons. */
|
---|
166 | uint32_t cbBlockExtra;
|
---|
167 | /** Number of blocks. */
|
---|
168 | uint32_t cBlocks;
|
---|
169 | /** Number of allocated blocks. */
|
---|
170 | uint32_t cBlocksAllocated;
|
---|
171 | /** UUID of image. */
|
---|
172 | RTUUID uuidCreate;
|
---|
173 | /** UUID of image's last modification. */
|
---|
174 | RTUUID uuidModify;
|
---|
175 | /** Only for secondary images - UUID of previous image. */
|
---|
176 | RTUUID uuidLinkage;
|
---|
177 | /** Only for secondary images - UUID of previous image's last modification. */
|
---|
178 | RTUUID uuidParentModify;
|
---|
179 | } VDIHEADER1, *PVDIHEADER1;
|
---|
180 | #pragma pack()
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Header to be stored in image file, VDI_IMAGE_VERSION_MAJOR = 1,
|
---|
184 | * VDI_IMAGE_VERSION_MINOR = 1, the slightly changed variant necessary as the
|
---|
185 | * old released code doesn't support changing the minor version at all.
|
---|
186 | */
|
---|
187 | #pragma pack(1)
|
---|
188 | typedef struct VDIHEADER1PLUS
|
---|
189 | {
|
---|
190 | /** Size of this structure in bytes. */
|
---|
191 | uint32_t cbHeader;
|
---|
192 | /** The image type (VDI_IMAGE_TYPE_*). */
|
---|
193 | uint32_t u32Type;
|
---|
194 | /** Image flags (VDI_IMAGE_FLAGS_*). */
|
---|
195 | uint32_t fFlags;
|
---|
196 | /** Image comment. (UTF-8) */
|
---|
197 | char szComment[VDI_IMAGE_COMMENT_SIZE];
|
---|
198 | /** Offset of Blocks array from the begining of image file.
|
---|
199 | * Should be sector-aligned for HDD access optimization. */
|
---|
200 | uint32_t offBlocks;
|
---|
201 | /** Offset of image data from the begining of image file.
|
---|
202 | * Should be sector-aligned for HDD access optimization. */
|
---|
203 | uint32_t offData;
|
---|
204 | /** Legacy image geometry (previous code stored PCHS there). */
|
---|
205 | VDIDISKGEOMETRY LegacyGeometry;
|
---|
206 | /** Was BIOS HDD translation mode, now unused. */
|
---|
207 | uint32_t u32Dummy;
|
---|
208 | /** Size of disk (in bytes). */
|
---|
209 | uint64_t cbDisk;
|
---|
210 | /** Block size. (For instance VDI_IMAGE_BLOCK_SIZE.) Should be a power of 2! */
|
---|
211 | uint32_t cbBlock;
|
---|
212 | /** Size of additional service information of every data block.
|
---|
213 | * Prepended before block data. May be 0.
|
---|
214 | * Should be a power of 2 and sector-aligned for optimization reasons. */
|
---|
215 | uint32_t cbBlockExtra;
|
---|
216 | /** Number of blocks. */
|
---|
217 | uint32_t cBlocks;
|
---|
218 | /** Number of allocated blocks. */
|
---|
219 | uint32_t cBlocksAllocated;
|
---|
220 | /** UUID of image. */
|
---|
221 | RTUUID uuidCreate;
|
---|
222 | /** UUID of image's last modification. */
|
---|
223 | RTUUID uuidModify;
|
---|
224 | /** Only for secondary images - UUID of previous image. */
|
---|
225 | RTUUID uuidLinkage;
|
---|
226 | /** Only for secondary images - UUID of previous image's last modification. */
|
---|
227 | RTUUID uuidParentModify;
|
---|
228 | /** LCHS image geometry (new field in VDI1.2 version. */
|
---|
229 | VDIDISKGEOMETRY LCHSGeometry;
|
---|
230 | } VDIHEADER1PLUS, *PVDIHEADER1PLUS;
|
---|
231 | #pragma pack()
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Header structure for all versions.
|
---|
235 | */
|
---|
236 | typedef struct VDIHEADER
|
---|
237 | {
|
---|
238 | unsigned uVersion;
|
---|
239 | union
|
---|
240 | {
|
---|
241 | VDIHEADER0 v0;
|
---|
242 | VDIHEADER1 v1;
|
---|
243 | VDIHEADER1PLUS v1plus;
|
---|
244 | } u;
|
---|
245 | } VDIHEADER, *PVDIHEADER;
|
---|
246 |
|
---|
247 | /** Block 'pointer'. */
|
---|
248 | typedef uint32_t VDIIMAGEBLOCKPOINTER;
|
---|
249 | /** Pointer to a block 'pointer'. */
|
---|
250 | typedef VDIIMAGEBLOCKPOINTER *PVDIIMAGEBLOCKPOINTER;
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Block marked as free is not allocated in image file, read from this
|
---|
254 | * block may returns any random data.
|
---|
255 | */
|
---|
256 | #define VDI_IMAGE_BLOCK_FREE ((VDIIMAGEBLOCKPOINTER)~0)
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Block marked as zero is not allocated in image file, read from this
|
---|
260 | * block returns zeroes.
|
---|
261 | */
|
---|
262 | #define VDI_IMAGE_BLOCK_ZERO ((VDIIMAGEBLOCKPOINTER)~1)
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Block 'pointer' >= VDI_IMAGE_BLOCK_UNALLOCATED indicates block is not
|
---|
266 | * allocated in image file.
|
---|
267 | */
|
---|
268 | #define VDI_IMAGE_BLOCK_UNALLOCATED (VDI_IMAGE_BLOCK_ZERO)
|
---|
269 | #define IS_VDI_IMAGE_BLOCK_ALLOCATED(bp) (bp < VDI_IMAGE_BLOCK_UNALLOCATED)
|
---|
270 |
|
---|
271 | #define GET_MAJOR_HEADER_VERSION(ph) (VDI_GET_VERSION_MAJOR((ph)->uVersion))
|
---|
272 | #define GET_MINOR_HEADER_VERSION(ph) (VDI_GET_VERSION_MINOR((ph)->uVersion))
|
---|
273 |
|
---|
274 | #ifdef VBOX_VDICORE_VD
|
---|
275 | /** @name VDI image types
|
---|
276 | * @{ */
|
---|
277 | typedef enum VDIIMAGETYPE
|
---|
278 | {
|
---|
279 | /** Normal dynamically growing base image file. */
|
---|
280 | VDI_IMAGE_TYPE_NORMAL = 1,
|
---|
281 | /** Preallocated base image file of a fixed size. */
|
---|
282 | VDI_IMAGE_TYPE_FIXED,
|
---|
283 | /** Dynamically growing image file for undo/commit changes support. */
|
---|
284 | VDI_IMAGE_TYPE_UNDO,
|
---|
285 | /** Dynamically growing image file for differencing support. */
|
---|
286 | VDI_IMAGE_TYPE_DIFF,
|
---|
287 |
|
---|
288 | /** First valid image type value. */
|
---|
289 | VDI_IMAGE_TYPE_FIRST = VDI_IMAGE_TYPE_NORMAL,
|
---|
290 | /** Last valid image type value. */
|
---|
291 | VDI_IMAGE_TYPE_LAST = VDI_IMAGE_TYPE_DIFF
|
---|
292 | } VDIIMAGETYPE;
|
---|
293 | /** Pointer to VDI image type. */
|
---|
294 | typedef VDIIMAGETYPE *PVDIIMAGETYPE;
|
---|
295 | /** @} */
|
---|
296 | #endif /* VBOX_VDICORE_VD */
|
---|
297 |
|
---|
298 | /*******************************************************************************
|
---|
299 | * Internal Functions for header access *
|
---|
300 | *******************************************************************************/
|
---|
301 | DECLINLINE(VDIIMAGETYPE) getImageType(PVDIHEADER ph)
|
---|
302 | {
|
---|
303 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
304 | {
|
---|
305 | case 0: return (VDIIMAGETYPE)ph->u.v0.u32Type;
|
---|
306 | case 1: return (VDIIMAGETYPE)ph->u.v1.u32Type;
|
---|
307 | }
|
---|
308 | AssertFailed();
|
---|
309 | return (VDIIMAGETYPE)0;
|
---|
310 | }
|
---|
311 |
|
---|
312 | #ifdef VBOX_VDICORE_VD
|
---|
313 | DECLINLINE(unsigned) getImageFlags(PVDIHEADER ph)
|
---|
314 | {
|
---|
315 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
316 | {
|
---|
317 | case 0:
|
---|
318 | /* VDI image flag conversion to VD image flags. */
|
---|
319 | return ph->u.v0.fFlags << 8;
|
---|
320 | case 1:
|
---|
321 | /* VDI image flag conversion to VD image flags. */
|
---|
322 | return ph->u.v1.fFlags << 8;
|
---|
323 | }
|
---|
324 | AssertFailed();
|
---|
325 | return 0;
|
---|
326 | }
|
---|
327 | #else /* !VBOX_VDICORE_VD */
|
---|
328 | DECLINLINE(unsigned) getImageFlags(PVDIHEADER ph)
|
---|
329 | {
|
---|
330 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
331 | {
|
---|
332 | case 0: return ph->u.v0.fFlags;
|
---|
333 | case 1: return ph->u.v1.fFlags;
|
---|
334 | }
|
---|
335 | AssertFailed();
|
---|
336 | return 0;
|
---|
337 | }
|
---|
338 | #endif /* !VBOX_VDICORE_VD */
|
---|
339 |
|
---|
340 | DECLINLINE(char *) getImageComment(PVDIHEADER ph)
|
---|
341 | {
|
---|
342 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
343 | {
|
---|
344 | case 0: return &ph->u.v0.szComment[0];
|
---|
345 | case 1: return &ph->u.v1.szComment[0];
|
---|
346 | }
|
---|
347 | AssertFailed();
|
---|
348 | return NULL;
|
---|
349 | }
|
---|
350 |
|
---|
351 | DECLINLINE(unsigned) getImageBlocksOffset(PVDIHEADER ph)
|
---|
352 | {
|
---|
353 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
354 | {
|
---|
355 | case 0: return (sizeof(VDIPREHEADER) + sizeof(VDIHEADER0));
|
---|
356 | case 1: return ph->u.v1.offBlocks;
|
---|
357 | }
|
---|
358 | AssertFailed();
|
---|
359 | return 0;
|
---|
360 | }
|
---|
361 |
|
---|
362 | DECLINLINE(unsigned) getImageDataOffset(PVDIHEADER ph)
|
---|
363 | {
|
---|
364 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
365 | {
|
---|
366 | case 0: return sizeof(VDIPREHEADER) + sizeof(VDIHEADER0) + \
|
---|
367 | (ph->u.v0.cBlocks * sizeof(VDIIMAGEBLOCKPOINTER));
|
---|
368 | case 1: return ph->u.v1.offData;
|
---|
369 | }
|
---|
370 | AssertFailed();
|
---|
371 | return 0;
|
---|
372 | }
|
---|
373 |
|
---|
374 | DECLINLINE(PVDIDISKGEOMETRY) getImageLCHSGeometry(PVDIHEADER ph)
|
---|
375 | {
|
---|
376 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
377 | {
|
---|
378 | case 0: return NULL;
|
---|
379 | case 1:
|
---|
380 | switch (GET_MINOR_HEADER_VERSION(ph))
|
---|
381 | {
|
---|
382 | case 1:
|
---|
383 | if (ph->u.v1.cbHeader < sizeof(ph->u.v1plus))
|
---|
384 | return NULL;
|
---|
385 | else
|
---|
386 | return &ph->u.v1plus.LCHSGeometry;
|
---|
387 | }
|
---|
388 | }
|
---|
389 | AssertFailed();
|
---|
390 | return NULL;
|
---|
391 | }
|
---|
392 |
|
---|
393 | DECLINLINE(uint64_t) getImageDiskSize(PVDIHEADER ph)
|
---|
394 | {
|
---|
395 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
396 | {
|
---|
397 | case 0: return ph->u.v0.cbDisk;
|
---|
398 | case 1: return ph->u.v1.cbDisk;
|
---|
399 | }
|
---|
400 | AssertFailed();
|
---|
401 | return 0;
|
---|
402 | }
|
---|
403 |
|
---|
404 | DECLINLINE(unsigned) getImageBlockSize(PVDIHEADER ph)
|
---|
405 | {
|
---|
406 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
407 | {
|
---|
408 | case 0: return ph->u.v0.cbBlock;
|
---|
409 | case 1: return ph->u.v1.cbBlock;
|
---|
410 | }
|
---|
411 | AssertFailed();
|
---|
412 | return 0;
|
---|
413 | }
|
---|
414 |
|
---|
415 | DECLINLINE(unsigned) getImageExtraBlockSize(PVDIHEADER ph)
|
---|
416 | {
|
---|
417 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
418 | {
|
---|
419 | case 0: return 0;
|
---|
420 | case 1: return ph->u.v1.cbBlockExtra;
|
---|
421 | }
|
---|
422 | AssertFailed();
|
---|
423 | return 0;
|
---|
424 | }
|
---|
425 |
|
---|
426 | DECLINLINE(unsigned) getImageBlocks(PVDIHEADER ph)
|
---|
427 | {
|
---|
428 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
429 | {
|
---|
430 | case 0: return ph->u.v0.cBlocks;
|
---|
431 | case 1: return ph->u.v1.cBlocks;
|
---|
432 | }
|
---|
433 | AssertFailed();
|
---|
434 | return 0;
|
---|
435 | }
|
---|
436 |
|
---|
437 | DECLINLINE(unsigned) getImageBlocksAllocated(PVDIHEADER ph)
|
---|
438 | {
|
---|
439 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
440 | {
|
---|
441 | case 0: return ph->u.v0.cBlocksAllocated;
|
---|
442 | case 1: return ph->u.v1.cBlocksAllocated;
|
---|
443 | }
|
---|
444 | AssertFailed();
|
---|
445 | return 0;
|
---|
446 | }
|
---|
447 |
|
---|
448 | DECLINLINE(void) setImageBlocksAllocated(PVDIHEADER ph, unsigned cBlocks)
|
---|
449 | {
|
---|
450 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
451 | {
|
---|
452 | case 0: ph->u.v0.cBlocksAllocated = cBlocks; return;
|
---|
453 | case 1: ph->u.v1.cBlocksAllocated = cBlocks; return;
|
---|
454 | }
|
---|
455 | AssertFailed();
|
---|
456 | }
|
---|
457 |
|
---|
458 | DECLINLINE(PRTUUID) getImageCreationUUID(PVDIHEADER ph)
|
---|
459 | {
|
---|
460 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
461 | {
|
---|
462 | case 0: return &ph->u.v0.uuidCreate;
|
---|
463 | case 1: return &ph->u.v1.uuidCreate;
|
---|
464 | }
|
---|
465 | AssertFailed();
|
---|
466 | return NULL;
|
---|
467 | }
|
---|
468 |
|
---|
469 | DECLINLINE(PRTUUID) getImageModificationUUID(PVDIHEADER ph)
|
---|
470 | {
|
---|
471 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
472 | {
|
---|
473 | case 0: return &ph->u.v0.uuidModify;
|
---|
474 | case 1: return &ph->u.v1.uuidModify;
|
---|
475 | }
|
---|
476 | AssertFailed();
|
---|
477 | return NULL;
|
---|
478 | }
|
---|
479 |
|
---|
480 | DECLINLINE(PRTUUID) getImageParentUUID(PVDIHEADER ph)
|
---|
481 | {
|
---|
482 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
483 | {
|
---|
484 | case 0: return &ph->u.v0.uuidLinkage;
|
---|
485 | case 1: return &ph->u.v1.uuidLinkage;
|
---|
486 | }
|
---|
487 | AssertFailed();
|
---|
488 | return NULL;
|
---|
489 | }
|
---|
490 |
|
---|
491 | DECLINLINE(PRTUUID) getImageParentModificationUUID(PVDIHEADER ph)
|
---|
492 | {
|
---|
493 | switch (GET_MAJOR_HEADER_VERSION(ph))
|
---|
494 | {
|
---|
495 | case 1: return &ph->u.v1.uuidParentModify;
|
---|
496 | }
|
---|
497 | AssertFailed();
|
---|
498 | return NULL;
|
---|
499 | }
|
---|
500 |
|
---|
501 | #ifndef VBOX_VDICORE_VD
|
---|
502 | /**
|
---|
503 | * Default image block size, may be changed by setBlockSize/getBlockSize.
|
---|
504 | *
|
---|
505 | * Note: for speed reasons block size should be a power of 2 !
|
---|
506 | */
|
---|
507 | #define VDI_IMAGE_DEFAULT_BLOCK_SIZE _1M
|
---|
508 | #endif /* !VBOX_VDICORE_VD */
|
---|
509 |
|
---|
510 | #ifndef VBOX_VDICORE_VD
|
---|
511 | /**
|
---|
512 | * fModified bit flags.
|
---|
513 | */
|
---|
514 | #define VDI_IMAGE_MODIFIED_FLAG RT_BIT(0)
|
---|
515 | #define VDI_IMAGE_MODIFIED_FIRST RT_BIT(1)
|
---|
516 | #define VDI_IMAGE_MODIFIED_DISABLE_UUID_UPDATE RT_BIT(2)
|
---|
517 | #endif /* !VBOX_VDICORE_VD */
|
---|
518 |
|
---|
519 | /**
|
---|
520 | * Image structure
|
---|
521 | */
|
---|
522 | typedef struct VDIIMAGEDESC
|
---|
523 | {
|
---|
524 | #ifndef VBOX_VDICORE_VD
|
---|
525 | /** Link to parent image descriptor, if any. */
|
---|
526 | struct VDIIMAGEDESC *pPrev;
|
---|
527 | /** Link to child image descriptor, if any. */
|
---|
528 | struct VDIIMAGEDESC *pNext;
|
---|
529 | #endif /* !VBOX_VDICORE_VD */
|
---|
530 | /** File handle. */
|
---|
531 | RTFILE File;
|
---|
532 | #ifndef VBOX_VDICORE_VD
|
---|
533 | /** True if the image is operating in readonly mode. */
|
---|
534 | bool fReadOnly;
|
---|
535 | /** Image open flags, VDI_OPEN_FLAGS_*. */
|
---|
536 | unsigned fOpen;
|
---|
537 | #else /* VBOX_VDICORE_VD */
|
---|
538 | /** Image open flags, VD__OPEN_FLAGS_*. */
|
---|
539 | unsigned uOpenFlags;
|
---|
540 | #endif /* VBOX_VDICORE_VD */
|
---|
541 | /** Image pre-header. */
|
---|
542 | VDIPREHEADER PreHeader;
|
---|
543 | /** Image header. */
|
---|
544 | VDIHEADER Header;
|
---|
545 | /** Pointer to a block array. */
|
---|
546 | PVDIIMAGEBLOCKPOINTER paBlocks;
|
---|
547 | #ifndef VBOX_VDICORE_VD
|
---|
548 | /** fFlags copy from image header, for speed optimization. */
|
---|
549 | unsigned fFlags;
|
---|
550 | #else /* VBOX_VDICORE_VD */
|
---|
551 | /** fFlags copy from image header, for speed optimization. */
|
---|
552 | unsigned uImageFlags;
|
---|
553 | #endif /* VBOX_VDICORE_VD */
|
---|
554 | /** Start offset of block array in image file, here for speed optimization. */
|
---|
555 | unsigned offStartBlocks;
|
---|
556 | /** Start offset of data in image file, here for speed optimization. */
|
---|
557 | unsigned offStartData;
|
---|
558 | /** Block mask for getting the offset into a block from a byte hdd offset. */
|
---|
559 | unsigned uBlockMask;
|
---|
560 | /** Block shift value for converting byte hdd offset into paBlock index. */
|
---|
561 | unsigned uShiftOffset2Index;
|
---|
562 | #ifndef VBOX_VDICORE_VD
|
---|
563 | /** Block shift value for converting block index into offset in image. */
|
---|
564 | unsigned uShiftIndex2Offset;
|
---|
565 | #endif /* !VBOX_VDICORE_VD */
|
---|
566 | /** Offset of data from the beginning of block. */
|
---|
567 | unsigned offStartBlockData;
|
---|
568 | #ifndef VBOX_VDICORE_VD
|
---|
569 | /** Image is modified flags (VDI_IMAGE_MODIFIED*). */
|
---|
570 | unsigned fModified;
|
---|
571 | /** Container filename. (UTF-8)
|
---|
572 | * @todo Make this variable length to save a bunch of bytes. (low prio) */
|
---|
573 | char szFilename[RTPATH_MAX];
|
---|
574 | #else /* VBOX_VDICORE_VD */
|
---|
575 | /** Total size of image block (including the extra data). */
|
---|
576 | unsigned cbTotalBlockData;
|
---|
577 | /** Container filename. (UTF-8) */
|
---|
578 | const char *pszFilename;
|
---|
579 | /** Physical geometry of this image (never actually stored). */
|
---|
580 | PDMMEDIAGEOMETRY PCHSGeometry;
|
---|
581 | /** Pointer to the per-disk VD interface list. */
|
---|
582 | PVDINTERFACE pVDIfsDisk;
|
---|
583 | /** Error interface. */
|
---|
584 | PVDINTERFACE pInterfaceError;
|
---|
585 | /** Error interface callback table. */
|
---|
586 | PVDINTERFACEERROR pInterfaceErrorCallbacks;
|
---|
587 | #endif /* VBOX_VDICORE_VD */
|
---|
588 | } VDIIMAGEDESC, *PVDIIMAGEDESC;
|
---|
589 |
|
---|
590 | #ifndef VBOX_VDICORE_VD
|
---|
591 | /**
|
---|
592 | * Default work buffer size, may be changed by setBufferSize() method.
|
---|
593 | *
|
---|
594 | * For best speed performance it must be equal to image block size.
|
---|
595 | */
|
---|
596 | #define VDIDISK_DEFAULT_BUFFER_SIZE (VDI_IMAGE_DEFAULT_BLOCK_SIZE)
|
---|
597 | #endif /* !VBOX_VDICORE_VD */
|
---|
598 |
|
---|
599 | /** VDIDISK Signature. */
|
---|
600 | #define VDIDISK_SIGNATURE (0xbedafeda)
|
---|
601 |
|
---|
602 | /**
|
---|
603 | * VBox HDD Container main structure, private part.
|
---|
604 | */
|
---|
605 | struct VDIDISK
|
---|
606 | {
|
---|
607 | /** Structure signature (VDIDISK_SIGNATURE). */
|
---|
608 | uint32_t u32Signature;
|
---|
609 |
|
---|
610 | /** Number of opened images. */
|
---|
611 | unsigned cImages;
|
---|
612 |
|
---|
613 | /** Base image. */
|
---|
614 | PVDIIMAGEDESC pBase;
|
---|
615 |
|
---|
616 | /** Last opened image in the chain.
|
---|
617 | * The same as pBase if only one image is used or the last opened diff image. */
|
---|
618 | PVDIIMAGEDESC pLast;
|
---|
619 |
|
---|
620 | /** Default block size for newly created images. */
|
---|
621 | unsigned cbBlock;
|
---|
622 |
|
---|
623 | /** Working buffer size, allocated only while committing data,
|
---|
624 | * copying block from primary image to secondary and saving previously
|
---|
625 | * zero block. Buffer deallocated after operation complete.
|
---|
626 | * @remark For best performance buffer size must be equal to image's
|
---|
627 | * block size, however it may be decreased for memory saving.
|
---|
628 | */
|
---|
629 | unsigned cbBuf;
|
---|
630 |
|
---|
631 | /** Flag whether zero writes should be handled normally or optimized
|
---|
632 | * away if possible. */
|
---|
633 | bool fHonorZeroWrites;
|
---|
634 |
|
---|
635 | /** The media interface. */
|
---|
636 | PDMIMEDIA IMedia;
|
---|
637 | /** Pointer to the driver instance. */
|
---|
638 | PPDMDRVINS pDrvIns;
|
---|
639 | };
|
---|
640 |
|
---|
641 |
|
---|
642 | /*******************************************************************************
|
---|
643 | * Internal Functions *
|
---|
644 | *******************************************************************************/
|
---|
645 | __BEGIN_DECLS
|
---|
646 |
|
---|
647 | #ifndef VBOX_VDICORE_VD
|
---|
648 | VBOXDDU_DECL(void) vdiInitVDIDisk(PVDIDISK pDisk);
|
---|
649 | VBOXDDU_DECL(void) VDIFlushImage(PVDIIMAGEDESC pImage);
|
---|
650 | VBOXDDU_DECL(int) vdiChangeImageMode(PVDIIMAGEDESC pImage, bool fReadOnly);
|
---|
651 | #endif /* !VBOX_VDICORE_VD */
|
---|
652 |
|
---|
653 | __END_DECLS
|
---|
654 |
|
---|
655 | #endif
|
---|