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