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