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