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