1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
|
/* $Id: dvmgpt.cpp $ */
/** @file
* IPRT Disk Volume Management API (DVM) - GPT format backend.
*/
/*
* Copyright (C) 2011-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/dvm.h>
#include <iprt/assert.h>
#include <iprt/asm.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <iprt/utf16.h>
#include <iprt/uuid.h>
#include "internal/dvm.h"
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/** The GPT signature. */
#define RTDVM_GPT_SIGNATURE "EFI PART"
/**
* GPT on disk header.
*/
typedef struct GPTHDR
{
/** 0x00: Signature ("EFI PART"). */
char abSignature[8];
/** 0x08: Revision. */
uint32_t u32Revision;
/** 0x0c: Header size. */
uint32_t cbHeader;
/** 0x10: CRC of header. */
uint32_t u32Crc;
} GPTHDR;
/** Pointer to a GPT header. */
typedef struct GPTHDR *PGPTHDR;
AssertCompileSize(GPTHDR, 20);
/**
* Complete GPT table header for revision 1.0.
*/
#pragma pack(1)
typedef struct GPTHDRREV1
{
/** 0x00: Header. */
GPTHDR Hdr;
/** 0x14: Reserved. */
uint32_t u32Reserved;
/** 0x18: Current LBA. */
uint64_t u64LbaCurrent;
/** 0x20: Backup LBA. */
uint64_t u64LbaBackup;
/** 0x28:First usable LBA for partitions. */
uint64_t u64LbaFirstPartition;
/** 0x30: Last usable LBA for partitions. */
uint64_t u64LbaLastPartition;
/** 0x38: Disk UUID. */
RTUUID DiskUuid;
/** 0x48: LBA of first partition entry. */
uint64_t u64LbaPartitionEntries;
/** 0x50: Number of partition entries. */
uint32_t cPartitionEntries;
/** 0x54: Partition entry size. */
uint32_t cbPartitionEntry;
/** 0x58: CRC of partition entries. */
uint32_t u32CrcPartitionEntries;
} GPTHDRREV1;
/** Pointer to a revision 1.0 GPT header. */
typedef GPTHDRREV1 *PGPTHDRREV1;
#pragma pack()
AssertCompileSize(GPTHDRREV1, 92);
/**
* GPT partition table entry.
*/
typedef struct GPTENTRY
{
/** 0x00: Partition type UUID. */
RTUUID UuidType;
/** 0x10: Partition UUID. */
RTUUID UuidPartition;
/** 0x20: First LBA. */
uint64_t u64LbaFirst;
/** 0x28: Last LBA. */
uint64_t u64LbaLast;
/** 0x30: Attribute flags. */
uint64_t u64Flags;
/** 0x38: Partition name (UTF-16LE code units). */
RTUTF16 aPartitionName[36];
} GPTENTRY;
/** Pointer to a GPT entry. */
typedef struct GPTENTRY *PGPTENTRY;
AssertCompileSize(GPTENTRY, 128);
/** Partition flags - System partition. */
#define RTDVM_GPT_ENTRY_SYSTEM RT_BIT_64(0)
/** Partition flags - Partition is readonly. */
#define RTDVM_GPT_ENTRY_READONLY RT_BIT_64(60)
/** Partition flags - Partition is hidden. */
#define RTDVM_GPT_ENTRY_HIDDEN RT_BIT_64(62)
/** Partition flags - Don't automount this partition. */
#define RTDVM_GPT_ENTRY_NO_AUTOMOUNT RT_BIT_64(63)
/**
* GPT volume manager data.
*/
typedef struct RTDVMFMTINTERNAL
{
/** Pointer to the underlying disk. */
PCRTDVMDISK pDisk;
/** GPT header. */
GPTHDRREV1 HdrRev1;
/** GPT array. */
PGPTENTRY paGptEntries;
/** Number of occupied partition entries. */
uint32_t cPartitions;
} RTDVMFMTINTERNAL;
/** Pointer to the MBR volume manager. */
typedef RTDVMFMTINTERNAL *PRTDVMFMTINTERNAL;
/**
* GPT volume data.
*/
typedef struct RTDVMVOLUMEFMTINTERNAL
{
/** Pointer to the volume manager. */
PRTDVMFMTINTERNAL pVolMgr;
/** Partition table entry index. */
uint32_t idxEntry;
/** Start offset of the volume. */
uint64_t offStart;
/** Size of the volume. */
uint64_t cbVolume;
/** Pointer to the GPT entry in the array. */
PGPTENTRY pGptEntry;
} RTDVMVOLUMEFMTINTERNAL;
/** Pointer to an MBR volume. */
typedef RTDVMVOLUMEFMTINTERNAL *PRTDVMVOLUMEFMTINTERNAL;
/**
* GPT partition type to DVM volume type mapping entry.
*/
typedef struct RTDVMGPTPARTTYPE2VOLTYPE
{
/** Type UUID. */
const char *pcszUuid;
/** DVM volume type. */
RTDVMVOLTYPE enmVolType;
} RTDVMGPTPARTTYPE2VOLTYPE;
/** Pointer to a MBR FS Type to volume type mapping entry. */
typedef RTDVMGPTPARTTYPE2VOLTYPE *PRTDVMGPTPARTTYPE2VOLTYPE;
/** Converts a LBA number to the byte offset. */
#define RTDVM_GPT_LBA2BYTE(lba, disk) ((lba) * (disk)->cbSector)
/** Converts a Byte offset to the LBA number. */
#define RTDVM_GPT_BYTE2LBA(lba, disk) ((lba) / (disk)->cbSector)
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/**
* Mapping of partition types to DVM volume types.
*
* From http://en.wikipedia.org/wiki/GUID_Partition_Table
*/
static const RTDVMGPTPARTTYPE2VOLTYPE g_aPartType2DvmVolTypes[] =
{
{ "C12A7328-F81F-11D2-BA4B-00A0C93EC93B", RTDVMVOLTYPE_EFI_SYSTEM },
{ "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", RTDVMVOLTYPE_WIN_BASIC },
{ "E3C9E316-0B5C-4DB8-817D-F92DF00215AE", RTDVMVOLTYPE_WIN_MSR },
{ "5808C8AA-7E8F-42E0-85D2-E1E90434CFB3", RTDVMVOLTYPE_WIN_LDM_META },
{ "AF9B60A0-1431-4F62-BC68-3311714A69AD", RTDVMVOLTYPE_WIN_LDM_DATA },
{ "DE94BBA4-06D1-4D40-A16A-BFD50179D6AC", RTDVMVOLTYPE_WIN_RECOVERY },
{ "E75CAF8F-F680-4CEE-AFA3-B001E56EFC2D", RTDVMVOLTYPE_WIN_STORAGE_SPACES },
{ "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F", RTDVMVOLTYPE_LINUX_SWAP },
{ "0FC63DAF-8483-4772-8E79-3D69D8477DE4", RTDVMVOLTYPE_LINUX_NATIVE },
{ "44479540-F297-41B2-9AF7-D131D5F0458A", RTDVMVOLTYPE_LINUX_NATIVE }, /* x86 root */
{ "4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709", RTDVMVOLTYPE_LINUX_NATIVE }, /* AMD64 root */
{ "69DAD710-2CE4-4E3C-B16C-21A1D49ABED3", RTDVMVOLTYPE_LINUX_NATIVE }, /* ARM32 root */
{ "B921B045-1DF0-41C3-AF44-4C6F280D3FAE", RTDVMVOLTYPE_LINUX_NATIVE }, /* ARM64 root */
{ "E6D6D379-F507-44C2-A23C-238F2A3DF928", RTDVMVOLTYPE_LINUX_LVM },
{ "A19D880F-05FC-4D3B-A006-743F0F84911E", RTDVMVOLTYPE_LINUX_SOFTRAID },
{ "83BD6B9D-7F41-11DC-BE0B-001560B84F0F", RTDVMVOLTYPE_FREEBSD }, /* Boot */
{ "516E7CB4-6ECF-11D6-8FF8-00022D09712B", RTDVMVOLTYPE_FREEBSD }, /* Data */
{ "516E7CB5-6ECF-11D6-8FF8-00022D09712B", RTDVMVOLTYPE_FREEBSD }, /* Swap */
{ "516E7CB6-6ECF-11D6-8FF8-00022D09712B", RTDVMVOLTYPE_FREEBSD }, /* UFS */
{ "516E7CB8-6ECF-11D6-8FF8-00022D09712B", RTDVMVOLTYPE_FREEBSD }, /* Vinum */
{ "516E7CBA-6ECF-11D6-8FF8-00022D09712B", RTDVMVOLTYPE_FREEBSD }, /* ZFS */
{ "49F48D32-B10E-11DC-B99B-0019D1879648", RTDVMVOLTYPE_NETBSD }, /* Swap */
{ "49F48D5A-B10E-11DC-B99B-0019D1879648", RTDVMVOLTYPE_NETBSD }, /* FFS */
{ "49F48D82-B10E-11DC-B99B-0019D1879648", RTDVMVOLTYPE_NETBSD }, /* LFS */
{ "49F48DAA-B10E-11DC-B99B-0019D1879648", RTDVMVOLTYPE_NETBSD }, /* Raid */
{ "2DB519C4-B10F-11DC-B99B-0019D1879648", RTDVMVOLTYPE_NETBSD }, /* Concatenated */
{ "2DB519EC-B10F-11DC-B99B-0019D1879648", RTDVMVOLTYPE_NETBSD }, /* Encrypted */
{ "48465300-0000-11AA-AA11-00306543ECAC", RTDVMVOLTYPE_DARWIN_HFS },
{ "7C3457EF-0000-11AA-AA11-00306543ECAC", RTDVMVOLTYPE_DARWIN_APFS },
{ "6A82CB45-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* Boot */
{ "6A85CF4D-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* Root */
{ "6A87C46F-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* Swap */
{ "6A8B642B-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* Backup */
{ "6A898CC3-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* /usr */
{ "6A8EF2E9-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* /var */
{ "6A90BA39-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* /home */
{ "6A9283A5-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* Alternate sector */
{ "37AFFC90-EF7D-4E96-91C3-2D7AE055B174", RTDVMVOLTYPE_IBM_GPFS },
};
static DECLCALLBACK(int) rtDvmFmtGptProbe(PCRTDVMDISK pDisk, uint32_t *puScore)
{
int rc = VINF_SUCCESS;
GPTHDR Hdr;
*puScore = RTDVM_MATCH_SCORE_UNSUPPORTED;
if (rtDvmDiskGetSectors(pDisk) >= 2)
{
/* Read from the disk and check for the signature. */
rc = rtDvmDiskRead(pDisk, RTDVM_GPT_LBA2BYTE(1, pDisk), &Hdr, sizeof(GPTHDR));
if ( RT_SUCCESS(rc)
&& !strncmp(&Hdr.abSignature[0], RTDVM_GPT_SIGNATURE, RT_ELEMENTS(Hdr.abSignature))
&& RT_LE2H_U32(Hdr.u32Revision) == 0x00010000
&& RT_LE2H_U32(Hdr.cbHeader) == sizeof(GPTHDRREV1))
*puScore = RTDVM_MATCH_SCORE_PERFECT;
}
return rc;
}
static DECLCALLBACK(int) rtDvmFmtGptOpen(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
{
int rc = VINF_SUCCESS;
PRTDVMFMTINTERNAL pThis = NULL;
pThis = (PRTDVMFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMFMTINTERNAL));
if (pThis)
{
pThis->pDisk = pDisk;
pThis->cPartitions = 0;
/* Read the complete GPT header and convert to host endianess. */
rc = rtDvmDiskRead(pDisk, RTDVM_GPT_LBA2BYTE(1, pDisk), &pThis->HdrRev1, sizeof(pThis->HdrRev1));
if (RT_SUCCESS(rc))
{
pThis->HdrRev1.Hdr.u32Revision = RT_LE2H_U32(pThis->HdrRev1.Hdr.u32Revision);
pThis->HdrRev1.Hdr.cbHeader = RT_LE2H_U32(pThis->HdrRev1.Hdr.cbHeader);
pThis->HdrRev1.Hdr.u32Crc = RT_LE2H_U32(pThis->HdrRev1.Hdr.u32Crc);
pThis->HdrRev1.u64LbaCurrent = RT_LE2H_U64(pThis->HdrRev1.u64LbaCurrent);
pThis->HdrRev1.u64LbaBackup = RT_LE2H_U64(pThis->HdrRev1.u64LbaBackup);
pThis->HdrRev1.u64LbaFirstPartition = RT_LE2H_U64(pThis->HdrRev1.u64LbaFirstPartition);
pThis->HdrRev1.u64LbaLastPartition = RT_LE2H_U64(pThis->HdrRev1.u64LbaLastPartition);
/** @todo Disk UUID */
pThis->HdrRev1.u64LbaPartitionEntries = RT_LE2H_U64(pThis->HdrRev1.u64LbaPartitionEntries);
pThis->HdrRev1.cPartitionEntries = RT_LE2H_U32(pThis->HdrRev1.cPartitionEntries);
pThis->HdrRev1.cbPartitionEntry = RT_LE2H_U32(pThis->HdrRev1.cbPartitionEntry);
pThis->HdrRev1.u32CrcPartitionEntries = RT_LE2H_U32(pThis->HdrRev1.u32CrcPartitionEntries);
if (pThis->HdrRev1.cbPartitionEntry == sizeof(GPTENTRY))
{
pThis->paGptEntries = (PGPTENTRY)RTMemAllocZ(pThis->HdrRev1.cPartitionEntries * pThis->HdrRev1.cbPartitionEntry);
if (pThis->paGptEntries)
{
rc = rtDvmDiskRead(pDisk, RTDVM_GPT_LBA2BYTE(pThis->HdrRev1.u64LbaPartitionEntries, pDisk),
pThis->paGptEntries, pThis->HdrRev1.cPartitionEntries * pThis->HdrRev1.cbPartitionEntry);
if (RT_SUCCESS(rc))
{
/* Count the occupied entries. */
for (unsigned i = 0; i < pThis->HdrRev1.cPartitionEntries; i++)
if (!RTUuidIsNull(&pThis->paGptEntries[i].UuidType))
{
/* Convert to host endianess. */
/** @todo Uuids */
pThis->paGptEntries[i].u64LbaFirst = RT_LE2H_U64(pThis->paGptEntries[i].u64LbaFirst);
pThis->paGptEntries[i].u64LbaLast = RT_LE2H_U64(pThis->paGptEntries[i].u64LbaLast);
pThis->paGptEntries[i].u64Flags = RT_LE2H_U64(pThis->paGptEntries[i].u64Flags);
for (unsigned cwc = 0; cwc < RT_ELEMENTS(pThis->paGptEntries[i].aPartitionName); cwc++)
pThis->paGptEntries[i].aPartitionName[cwc] = RT_LE2H_U16(pThis->paGptEntries[i].aPartitionName[cwc]);
pThis->cPartitions++;
}
}
if (RT_FAILURE(rc))
RTMemFree(pThis->paGptEntries);
}
else
rc = VERR_NO_MEMORY;
}
else
rc = VERR_NOT_SUPPORTED;
if (RT_SUCCESS(rc))
*phVolMgrFmt = pThis;
else
RTMemFree(pThis);
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
static DECLCALLBACK(int) rtDvmFmtGptInitialize(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
{
NOREF(pDisk); NOREF(phVolMgrFmt);
return VERR_NOT_IMPLEMENTED;
}
static DECLCALLBACK(void) rtDvmFmtGptClose(RTDVMFMT hVolMgrFmt)
{
PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
pThis->pDisk = NULL;
memset(&pThis->HdrRev1, 0, sizeof(pThis->HdrRev1));
RTMemFree(pThis->paGptEntries);
pThis->paGptEntries = NULL;
RTMemFree(pThis);
}
static DECLCALLBACK(int) rtDvmFmtGptQueryRangeUse(RTDVMFMT hVolMgrFmt,
uint64_t off, uint64_t cbRange,
bool *pfUsed)
{
PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
NOREF(cbRange);
if (off < 33*pThis->pDisk->cbSector)
*pfUsed = true;
else
*pfUsed = false;
return VINF_SUCCESS;
}
static DECLCALLBACK(uint32_t) rtDvmFmtGptGetValidVolumes(RTDVMFMT hVolMgrFmt)
{
PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
return pThis->cPartitions;
}
static DECLCALLBACK(uint32_t) rtDvmFmtGptGetMaxVolumes(RTDVMFMT hVolMgrFmt)
{
PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
return pThis->HdrRev1.cPartitionEntries;
}
/**
* Creates a new volume.
*
* @returns IPRT status code.
* @param pThis The MBR volume manager data.
* @param pGptEntry The GPT entry.
* @param idx The index in the partition array.
* @param phVolFmt Where to store the volume data on success.
*/
static int rtDvmFmtMbrVolumeCreate(PRTDVMFMTINTERNAL pThis, PGPTENTRY pGptEntry,
uint32_t idx, PRTDVMVOLUMEFMT phVolFmt)
{
int rc = VINF_SUCCESS;
PRTDVMVOLUMEFMTINTERNAL pVol = (PRTDVMVOLUMEFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMVOLUMEFMTINTERNAL));
if (pVol)
{
pVol->pVolMgr = pThis;
pVol->idxEntry = idx;
pVol->pGptEntry = pGptEntry;
pVol->offStart = RTDVM_GPT_LBA2BYTE(pGptEntry->u64LbaFirst, pThis->pDisk);
pVol->cbVolume = RTDVM_GPT_LBA2BYTE(pGptEntry->u64LbaLast - pGptEntry->u64LbaFirst + 1, pThis->pDisk);
*phVolFmt = pVol;
}
else
rc = VERR_NO_MEMORY;
return rc;
}
static DECLCALLBACK(int) rtDvmFmtGptQueryFirstVolume(RTDVMFMT hVolMgrFmt, PRTDVMVOLUMEFMT phVolFmt)
{
PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
if (pThis->cPartitions != 0)
{
PGPTENTRY pGptEntry = &pThis->paGptEntries[0];
/* Search for the first non empty entry. */
for (unsigned i = 0; i < pThis->HdrRev1.cPartitionEntries; i++)
{
if (!RTUuidIsNull(&pGptEntry->UuidType))
return rtDvmFmtMbrVolumeCreate(pThis, pGptEntry, i, phVolFmt);
pGptEntry++;
}
AssertFailed();
}
return VERR_DVM_MAP_EMPTY;
}
static DECLCALLBACK(int) rtDvmFmtGptQueryNextVolume(RTDVMFMT hVolMgrFmt, RTDVMVOLUMEFMT hVolFmt, PRTDVMVOLUMEFMT phVolFmtNext)
{
PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
PGPTENTRY pGptEntry = pVol->pGptEntry + 1;
for (unsigned i = pVol->idxEntry + 1; i < pThis->HdrRev1.cPartitionEntries; i++)
{
if (!RTUuidIsNull(&pGptEntry->UuidType))
return rtDvmFmtMbrVolumeCreate(pThis, pGptEntry, i, phVolFmtNext);
pGptEntry++;
}
return VERR_DVM_MAP_NO_VOLUME;
}
static DECLCALLBACK(void) rtDvmFmtGptVolumeClose(RTDVMVOLUMEFMT hVolFmt)
{
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
pVol->pVolMgr = NULL;
pVol->offStart = 0;
pVol->cbVolume = 0;
pVol->pGptEntry = NULL;
RTMemFree(pVol);
}
static DECLCALLBACK(uint64_t) rtDvmFmtGptVolumeGetSize(RTDVMVOLUMEFMT hVolFmt)
{
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
return pVol->cbVolume;
}
static DECLCALLBACK(int) rtDvmFmtGptVolumeQueryName(RTDVMVOLUMEFMT hVolFmt, char **ppszVolName)
{
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
*ppszVolName = NULL;
return RTUtf16ToUtf8Ex(&pVol->pGptEntry->aPartitionName[0], RT_ELEMENTS(pVol->pGptEntry->aPartitionName),
ppszVolName, 0, NULL);
}
static DECLCALLBACK(RTDVMVOLTYPE) rtDvmFmtGptVolumeGetType(RTDVMVOLUMEFMT hVolFmt)
{
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
for (unsigned i = 0; i < RT_ELEMENTS(g_aPartType2DvmVolTypes); i++)
if (!RTUuidCompareStr(&pVol->pGptEntry->UuidType, g_aPartType2DvmVolTypes[i].pcszUuid))
return g_aPartType2DvmVolTypes[i].enmVolType;
return RTDVMVOLTYPE_UNKNOWN;
}
static DECLCALLBACK(uint64_t) rtDvmFmtGptVolumeGetFlags(RTDVMVOLUMEFMT hVolFmt)
{
NOREF(hVolFmt); /* No supported flags for now. */
return 0;
}
static DECLCALLBACK(bool) rtDvmFmtGptVolumeIsRangeIntersecting(RTDVMVOLUMEFMT hVolFmt,
uint64_t offStart, size_t cbRange,
uint64_t *poffVol,
uint64_t *pcbIntersect)
{
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
if (RTDVM_RANGE_IS_INTERSECTING(pVol->offStart, pVol->cbVolume, offStart))
{
*poffVol = offStart - pVol->offStart;
*pcbIntersect = RT_MIN(cbRange, pVol->offStart + pVol->cbVolume - offStart);
return true;
}
return false;
}
static DECLCALLBACK(int) rtDvmFmtGptVolumeRead(RTDVMVOLUMEFMT hVolFmt, uint64_t off, void *pvBuf, size_t cbRead)
{
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
AssertReturn(off + cbRead <= pVol->cbVolume, VERR_INVALID_PARAMETER);
return rtDvmDiskRead(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbRead);
}
static DECLCALLBACK(int) rtDvmFmtGptVolumeWrite(RTDVMVOLUMEFMT hVolFmt, uint64_t off, const void *pvBuf, size_t cbWrite)
{
PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
AssertReturn(off + cbWrite <= pVol->cbVolume, VERR_INVALID_PARAMETER);
return rtDvmDiskWrite(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbWrite);
}
RTDVMFMTOPS g_rtDvmFmtGpt =
{
/* pszFmt */
"GPT",
/* enmFormat, */
RTDVMFORMATTYPE_GPT,
/* pfnProbe */
rtDvmFmtGptProbe,
/* pfnOpen */
rtDvmFmtGptOpen,
/* pfnInitialize */
rtDvmFmtGptInitialize,
/* pfnClose */
rtDvmFmtGptClose,
/* pfnQueryRangeUse */
rtDvmFmtGptQueryRangeUse,
/* pfnGetValidVolumes */
rtDvmFmtGptGetValidVolumes,
/* pfnGetMaxVolumes */
rtDvmFmtGptGetMaxVolumes,
/* pfnQueryFirstVolume */
rtDvmFmtGptQueryFirstVolume,
/* pfnQueryNextVolume */
rtDvmFmtGptQueryNextVolume,
/* pfnVolumeClose */
rtDvmFmtGptVolumeClose,
/* pfnVolumeGetSize */
rtDvmFmtGptVolumeGetSize,
/* pfnVolumeQueryName */
rtDvmFmtGptVolumeQueryName,
/* pfnVolumeGetType */
rtDvmFmtGptVolumeGetType,
/* pfnVolumeGetFlags */
rtDvmFmtGptVolumeGetFlags,
/* pfnVolumeIsRangeIntersecting */
rtDvmFmtGptVolumeIsRangeIntersecting,
/* pfnVolumeRead */
rtDvmFmtGptVolumeRead,
/* pfnVolumeWrite */
rtDvmFmtGptVolumeWrite
};
|