1 | /* $Id: dvmgpt.cpp 73156 2018-07-16 12:37:19Z 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 | { "C12A7328-F81F-11D2-BA4B-00A0C93EC93B", RTDVMVOLTYPE_EFI_SYSTEM },
|
---|
197 |
|
---|
198 | { "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7", RTDVMVOLTYPE_WIN_BASIC },
|
---|
199 | { "E3C9E316-0B5C-4DB8-817D-F92DF00215AE", RTDVMVOLTYPE_WIN_MSR },
|
---|
200 | { "5808C8AA-7E8F-42E0-85D2-E1E90434CFB3", RTDVMVOLTYPE_WIN_LDM_META },
|
---|
201 | { "AF9B60A0-1431-4F62-BC68-3311714A69AD", RTDVMVOLTYPE_WIN_LDM_DATA },
|
---|
202 | { "DE94BBA4-06D1-4D40-A16A-BFD50179D6AC", RTDVMVOLTYPE_WIN_RECOVERY },
|
---|
203 | { "E75CAF8F-F680-4CEE-AFA3-B001E56EFC2D", RTDVMVOLTYPE_WIN_STORAGE_SPACES },
|
---|
204 |
|
---|
205 | { "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F", RTDVMVOLTYPE_LINUX_SWAP },
|
---|
206 | { "0FC63DAF-8483-4772-8E79-3D69D8477DE4", RTDVMVOLTYPE_LINUX_NATIVE },
|
---|
207 | { "44479540-F297-41B2-9AF7-D131D5F0458A", RTDVMVOLTYPE_LINUX_NATIVE }, /* x86 root */
|
---|
208 | { "4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709", RTDVMVOLTYPE_LINUX_NATIVE }, /* AMD64 root */
|
---|
209 | { "69DAD710-2CE4-4E3C-B16C-21A1D49ABED3", RTDVMVOLTYPE_LINUX_NATIVE }, /* ARM32 root */
|
---|
210 | { "B921B045-1DF0-41C3-AF44-4C6F280D3FAE", RTDVMVOLTYPE_LINUX_NATIVE }, /* ARM64 root */
|
---|
211 | { "E6D6D379-F507-44C2-A23C-238F2A3DF928", RTDVMVOLTYPE_LINUX_LVM },
|
---|
212 | { "A19D880F-05FC-4D3B-A006-743F0F84911E", RTDVMVOLTYPE_LINUX_SOFTRAID },
|
---|
213 |
|
---|
214 | { "83BD6B9D-7F41-11DC-BE0B-001560B84F0F", RTDVMVOLTYPE_FREEBSD }, /* Boot */
|
---|
215 | { "516E7CB4-6ECF-11D6-8FF8-00022D09712B", RTDVMVOLTYPE_FREEBSD }, /* Data */
|
---|
216 | { "516E7CB5-6ECF-11D6-8FF8-00022D09712B", RTDVMVOLTYPE_FREEBSD }, /* Swap */
|
---|
217 | { "516E7CB6-6ECF-11D6-8FF8-00022D09712B", RTDVMVOLTYPE_FREEBSD }, /* UFS */
|
---|
218 | { "516E7CB8-6ECF-11D6-8FF8-00022D09712B", RTDVMVOLTYPE_FREEBSD }, /* Vinum */
|
---|
219 | { "516E7CBA-6ECF-11D6-8FF8-00022D09712B", RTDVMVOLTYPE_FREEBSD }, /* ZFS */
|
---|
220 |
|
---|
221 | { "49F48D32-B10E-11DC-B99B-0019D1879648", RTDVMVOLTYPE_NETBSD }, /* Swap */
|
---|
222 | { "49F48D5A-B10E-11DC-B99B-0019D1879648", RTDVMVOLTYPE_NETBSD }, /* FFS */
|
---|
223 | { "49F48D82-B10E-11DC-B99B-0019D1879648", RTDVMVOLTYPE_NETBSD }, /* LFS */
|
---|
224 | { "49F48DAA-B10E-11DC-B99B-0019D1879648", RTDVMVOLTYPE_NETBSD }, /* Raid */
|
---|
225 | { "2DB519C4-B10F-11DC-B99B-0019D1879648", RTDVMVOLTYPE_NETBSD }, /* Concatenated */
|
---|
226 | { "2DB519EC-B10F-11DC-B99B-0019D1879648", RTDVMVOLTYPE_NETBSD }, /* Encrypted */
|
---|
227 |
|
---|
228 | { "48465300-0000-11AA-AA11-00306543ECAC", RTDVMVOLTYPE_DARWIN_HFS },
|
---|
229 | { "7C3457EF-0000-11AA-AA11-00306543ECAC", RTDVMVOLTYPE_DARWIN_APFS },
|
---|
230 |
|
---|
231 | { "6A82CB45-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* Boot */
|
---|
232 | { "6A85CF4D-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* Root */
|
---|
233 | { "6A87C46F-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* Swap */
|
---|
234 | { "6A8B642B-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* Backup */
|
---|
235 | { "6A898CC3-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* /usr */
|
---|
236 | { "6A8EF2E9-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* /var */
|
---|
237 | { "6A90BA39-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* /home */
|
---|
238 | { "6A9283A5-1DD2-11B2-99A6-080020736631", RTDVMVOLTYPE_SOLARIS }, /* Alternate sector */
|
---|
239 |
|
---|
240 | { "37AFFC90-EF7D-4E96-91C3-2D7AE055B174", RTDVMVOLTYPE_IBM_GPFS },
|
---|
241 | };
|
---|
242 |
|
---|
243 | static DECLCALLBACK(int) rtDvmFmtGptProbe(PCRTDVMDISK pDisk, uint32_t *puScore)
|
---|
244 | {
|
---|
245 | int rc = VINF_SUCCESS;
|
---|
246 | GPTHDR Hdr;
|
---|
247 |
|
---|
248 | *puScore = RTDVM_MATCH_SCORE_UNSUPPORTED;
|
---|
249 |
|
---|
250 | if (rtDvmDiskGetSectors(pDisk) >= 2)
|
---|
251 | {
|
---|
252 | /* Read from the disk and check for the signature. */
|
---|
253 | rc = rtDvmDiskRead(pDisk, RTDVM_GPT_LBA2BYTE(1, pDisk), &Hdr, sizeof(GPTHDR));
|
---|
254 | if ( RT_SUCCESS(rc)
|
---|
255 | && !strncmp(&Hdr.abSignature[0], RTDVM_GPT_SIGNATURE, RT_ELEMENTS(Hdr.abSignature))
|
---|
256 | && RT_LE2H_U32(Hdr.u32Revision) == 0x00010000
|
---|
257 | && RT_LE2H_U32(Hdr.cbHeader) == sizeof(GPTHDRREV1))
|
---|
258 | *puScore = RTDVM_MATCH_SCORE_PERFECT;
|
---|
259 | }
|
---|
260 |
|
---|
261 | return rc;
|
---|
262 | }
|
---|
263 |
|
---|
264 | static DECLCALLBACK(int) rtDvmFmtGptOpen(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
|
---|
265 | {
|
---|
266 | int rc = VINF_SUCCESS;
|
---|
267 | PRTDVMFMTINTERNAL pThis = NULL;
|
---|
268 |
|
---|
269 | pThis = (PRTDVMFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMFMTINTERNAL));
|
---|
270 | if (pThis)
|
---|
271 | {
|
---|
272 | pThis->pDisk = pDisk;
|
---|
273 | pThis->cPartitions = 0;
|
---|
274 |
|
---|
275 | /* Read the complete GPT header and convert to host endianess. */
|
---|
276 | rc = rtDvmDiskRead(pDisk, RTDVM_GPT_LBA2BYTE(1, pDisk), &pThis->HdrRev1, sizeof(pThis->HdrRev1));
|
---|
277 | if (RT_SUCCESS(rc))
|
---|
278 | {
|
---|
279 | pThis->HdrRev1.Hdr.u32Revision = RT_LE2H_U32(pThis->HdrRev1.Hdr.u32Revision);
|
---|
280 | pThis->HdrRev1.Hdr.cbHeader = RT_LE2H_U32(pThis->HdrRev1.Hdr.cbHeader);
|
---|
281 | pThis->HdrRev1.Hdr.u32Crc = RT_LE2H_U32(pThis->HdrRev1.Hdr.u32Crc);
|
---|
282 | pThis->HdrRev1.u64LbaCurrent = RT_LE2H_U64(pThis->HdrRev1.u64LbaCurrent);
|
---|
283 | pThis->HdrRev1.u64LbaBackup = RT_LE2H_U64(pThis->HdrRev1.u64LbaBackup);
|
---|
284 | pThis->HdrRev1.u64LbaFirstPartition = RT_LE2H_U64(pThis->HdrRev1.u64LbaFirstPartition);
|
---|
285 | pThis->HdrRev1.u64LbaLastPartition = RT_LE2H_U64(pThis->HdrRev1.u64LbaLastPartition);
|
---|
286 | /** @todo Disk UUID */
|
---|
287 | pThis->HdrRev1.u64LbaPartitionEntries = RT_LE2H_U64(pThis->HdrRev1.u64LbaPartitionEntries);
|
---|
288 | pThis->HdrRev1.cPartitionEntries = RT_LE2H_U32(pThis->HdrRev1.cPartitionEntries);
|
---|
289 | pThis->HdrRev1.cbPartitionEntry = RT_LE2H_U32(pThis->HdrRev1.cbPartitionEntry);
|
---|
290 | pThis->HdrRev1.u32CrcPartitionEntries = RT_LE2H_U32(pThis->HdrRev1.u32CrcPartitionEntries);
|
---|
291 |
|
---|
292 | if (pThis->HdrRev1.cbPartitionEntry == sizeof(GPTENTRY))
|
---|
293 | {
|
---|
294 | pThis->paGptEntries = (PGPTENTRY)RTMemAllocZ(pThis->HdrRev1.cPartitionEntries * pThis->HdrRev1.cbPartitionEntry);
|
---|
295 | if (pThis->paGptEntries)
|
---|
296 | {
|
---|
297 | rc = rtDvmDiskRead(pDisk, RTDVM_GPT_LBA2BYTE(pThis->HdrRev1.u64LbaPartitionEntries, pDisk),
|
---|
298 | pThis->paGptEntries, pThis->HdrRev1.cPartitionEntries * pThis->HdrRev1.cbPartitionEntry);
|
---|
299 | if (RT_SUCCESS(rc))
|
---|
300 | {
|
---|
301 | /* Count the occupied entries. */
|
---|
302 | for (unsigned i = 0; i < pThis->HdrRev1.cPartitionEntries; i++)
|
---|
303 | if (!RTUuidIsNull(&pThis->paGptEntries[i].UuidType))
|
---|
304 | {
|
---|
305 | /* Convert to host endianess. */
|
---|
306 | /** @todo Uuids */
|
---|
307 | pThis->paGptEntries[i].u64LbaFirst = RT_LE2H_U64(pThis->paGptEntries[i].u64LbaFirst);
|
---|
308 | pThis->paGptEntries[i].u64LbaLast = RT_LE2H_U64(pThis->paGptEntries[i].u64LbaLast);
|
---|
309 | pThis->paGptEntries[i].u64Flags = RT_LE2H_U64(pThis->paGptEntries[i].u64Flags);
|
---|
310 | for (unsigned cwc = 0; cwc < RT_ELEMENTS(pThis->paGptEntries[i].aPartitionName); cwc++)
|
---|
311 | pThis->paGptEntries[i].aPartitionName[cwc] = RT_LE2H_U16(pThis->paGptEntries[i].aPartitionName[cwc]);
|
---|
312 |
|
---|
313 | pThis->cPartitions++;
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | if (RT_FAILURE(rc))
|
---|
318 | RTMemFree(pThis->paGptEntries);
|
---|
319 | }
|
---|
320 | else
|
---|
321 | rc = VERR_NO_MEMORY;
|
---|
322 | }
|
---|
323 | else
|
---|
324 | rc = VERR_NOT_SUPPORTED;
|
---|
325 |
|
---|
326 | if (RT_SUCCESS(rc))
|
---|
327 | *phVolMgrFmt = pThis;
|
---|
328 | else
|
---|
329 | RTMemFree(pThis);
|
---|
330 | }
|
---|
331 | }
|
---|
332 | else
|
---|
333 | rc = VERR_NO_MEMORY;
|
---|
334 |
|
---|
335 | return rc;
|
---|
336 | }
|
---|
337 |
|
---|
338 | static DECLCALLBACK(int) rtDvmFmtGptInitialize(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
|
---|
339 | {
|
---|
340 | NOREF(pDisk); NOREF(phVolMgrFmt);
|
---|
341 | return VERR_NOT_IMPLEMENTED;
|
---|
342 | }
|
---|
343 |
|
---|
344 | static DECLCALLBACK(void) rtDvmFmtGptClose(RTDVMFMT hVolMgrFmt)
|
---|
345 | {
|
---|
346 | PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
|
---|
347 |
|
---|
348 | pThis->pDisk = NULL;
|
---|
349 | memset(&pThis->HdrRev1, 0, sizeof(pThis->HdrRev1));
|
---|
350 | RTMemFree(pThis->paGptEntries);
|
---|
351 |
|
---|
352 | pThis->paGptEntries = NULL;
|
---|
353 | RTMemFree(pThis);
|
---|
354 | }
|
---|
355 |
|
---|
356 | static DECLCALLBACK(int) rtDvmFmtGptQueryRangeUse(RTDVMFMT hVolMgrFmt,
|
---|
357 | uint64_t off, uint64_t cbRange,
|
---|
358 | bool *pfUsed)
|
---|
359 | {
|
---|
360 | PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
|
---|
361 |
|
---|
362 | NOREF(cbRange);
|
---|
363 |
|
---|
364 | if (off < 33*pThis->pDisk->cbSector)
|
---|
365 | *pfUsed = true;
|
---|
366 | else
|
---|
367 | *pfUsed = false;
|
---|
368 |
|
---|
369 | return VINF_SUCCESS;
|
---|
370 | }
|
---|
371 |
|
---|
372 | static DECLCALLBACK(uint32_t) rtDvmFmtGptGetValidVolumes(RTDVMFMT hVolMgrFmt)
|
---|
373 | {
|
---|
374 | PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
|
---|
375 |
|
---|
376 | return pThis->cPartitions;
|
---|
377 | }
|
---|
378 |
|
---|
379 | static DECLCALLBACK(uint32_t) rtDvmFmtGptGetMaxVolumes(RTDVMFMT hVolMgrFmt)
|
---|
380 | {
|
---|
381 | PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
|
---|
382 |
|
---|
383 | return pThis->HdrRev1.cPartitionEntries;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * Creates a new volume.
|
---|
388 | *
|
---|
389 | * @returns IPRT status code.
|
---|
390 | * @param pThis The MBR volume manager data.
|
---|
391 | * @param pGptEntry The GPT entry.
|
---|
392 | * @param idx The index in the partition array.
|
---|
393 | * @param phVolFmt Where to store the volume data on success.
|
---|
394 | */
|
---|
395 | static int rtDvmFmtMbrVolumeCreate(PRTDVMFMTINTERNAL pThis, PGPTENTRY pGptEntry,
|
---|
396 | uint32_t idx, PRTDVMVOLUMEFMT phVolFmt)
|
---|
397 | {
|
---|
398 | int rc = VINF_SUCCESS;
|
---|
399 | PRTDVMVOLUMEFMTINTERNAL pVol = (PRTDVMVOLUMEFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMVOLUMEFMTINTERNAL));
|
---|
400 |
|
---|
401 | if (pVol)
|
---|
402 | {
|
---|
403 | pVol->pVolMgr = pThis;
|
---|
404 | pVol->idxEntry = idx;
|
---|
405 | pVol->pGptEntry = pGptEntry;
|
---|
406 | pVol->offStart = RTDVM_GPT_LBA2BYTE(pGptEntry->u64LbaFirst, pThis->pDisk);
|
---|
407 | pVol->cbVolume = RTDVM_GPT_LBA2BYTE(pGptEntry->u64LbaLast - pGptEntry->u64LbaFirst + 1, pThis->pDisk);
|
---|
408 |
|
---|
409 | *phVolFmt = pVol;
|
---|
410 | }
|
---|
411 | else
|
---|
412 | rc = VERR_NO_MEMORY;
|
---|
413 |
|
---|
414 | return rc;
|
---|
415 | }
|
---|
416 |
|
---|
417 | static DECLCALLBACK(int) rtDvmFmtGptQueryFirstVolume(RTDVMFMT hVolMgrFmt, PRTDVMVOLUMEFMT phVolFmt)
|
---|
418 | {
|
---|
419 | PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
|
---|
420 |
|
---|
421 | if (pThis->cPartitions != 0)
|
---|
422 | {
|
---|
423 | PGPTENTRY pGptEntry = &pThis->paGptEntries[0];
|
---|
424 |
|
---|
425 | /* Search for the first non empty entry. */
|
---|
426 | for (unsigned i = 0; i < pThis->HdrRev1.cPartitionEntries; i++)
|
---|
427 | {
|
---|
428 | if (!RTUuidIsNull(&pGptEntry->UuidType))
|
---|
429 | return rtDvmFmtMbrVolumeCreate(pThis, pGptEntry, i, phVolFmt);
|
---|
430 | pGptEntry++;
|
---|
431 | }
|
---|
432 | AssertFailed();
|
---|
433 | }
|
---|
434 | return VERR_DVM_MAP_EMPTY;
|
---|
435 | }
|
---|
436 |
|
---|
437 | static DECLCALLBACK(int) rtDvmFmtGptQueryNextVolume(RTDVMFMT hVolMgrFmt, RTDVMVOLUMEFMT hVolFmt, PRTDVMVOLUMEFMT phVolFmtNext)
|
---|
438 | {
|
---|
439 | PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
|
---|
440 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
441 | PGPTENTRY pGptEntry = pVol->pGptEntry + 1;
|
---|
442 |
|
---|
443 | for (unsigned i = pVol->idxEntry + 1; i < pThis->HdrRev1.cPartitionEntries; i++)
|
---|
444 | {
|
---|
445 | if (!RTUuidIsNull(&pGptEntry->UuidType))
|
---|
446 | return rtDvmFmtMbrVolumeCreate(pThis, pGptEntry, i, phVolFmtNext);
|
---|
447 | pGptEntry++;
|
---|
448 | }
|
---|
449 |
|
---|
450 | return VERR_DVM_MAP_NO_VOLUME;
|
---|
451 | }
|
---|
452 |
|
---|
453 | static DECLCALLBACK(void) rtDvmFmtGptVolumeClose(RTDVMVOLUMEFMT hVolFmt)
|
---|
454 | {
|
---|
455 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
456 |
|
---|
457 | pVol->pVolMgr = NULL;
|
---|
458 | pVol->offStart = 0;
|
---|
459 | pVol->cbVolume = 0;
|
---|
460 | pVol->pGptEntry = NULL;
|
---|
461 |
|
---|
462 | RTMemFree(pVol);
|
---|
463 | }
|
---|
464 |
|
---|
465 | static DECLCALLBACK(uint64_t) rtDvmFmtGptVolumeGetSize(RTDVMVOLUMEFMT hVolFmt)
|
---|
466 | {
|
---|
467 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
468 |
|
---|
469 | return pVol->cbVolume;
|
---|
470 | }
|
---|
471 |
|
---|
472 | static DECLCALLBACK(int) rtDvmFmtGptVolumeQueryName(RTDVMVOLUMEFMT hVolFmt, char **ppszVolName)
|
---|
473 | {
|
---|
474 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
475 |
|
---|
476 | *ppszVolName = NULL;
|
---|
477 | return RTUtf16ToUtf8Ex(&pVol->pGptEntry->aPartitionName[0], RT_ELEMENTS(pVol->pGptEntry->aPartitionName),
|
---|
478 | ppszVolName, 0, NULL);
|
---|
479 | }
|
---|
480 |
|
---|
481 | static DECLCALLBACK(RTDVMVOLTYPE) rtDvmFmtGptVolumeGetType(RTDVMVOLUMEFMT hVolFmt)
|
---|
482 | {
|
---|
483 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
484 |
|
---|
485 | for (unsigned i = 0; i < RT_ELEMENTS(g_aPartType2DvmVolTypes); i++)
|
---|
486 | if (!RTUuidCompareStr(&pVol->pGptEntry->UuidType, g_aPartType2DvmVolTypes[i].pcszUuid))
|
---|
487 | return g_aPartType2DvmVolTypes[i].enmVolType;
|
---|
488 |
|
---|
489 | return RTDVMVOLTYPE_UNKNOWN;
|
---|
490 | }
|
---|
491 |
|
---|
492 | static DECLCALLBACK(uint64_t) rtDvmFmtGptVolumeGetFlags(RTDVMVOLUMEFMT hVolFmt)
|
---|
493 | {
|
---|
494 | NOREF(hVolFmt); /* No supported flags for now. */
|
---|
495 | return 0;
|
---|
496 | }
|
---|
497 |
|
---|
498 | static DECLCALLBACK(bool) rtDvmFmtGptVolumeIsRangeIntersecting(RTDVMVOLUMEFMT hVolFmt,
|
---|
499 | uint64_t offStart, size_t cbRange,
|
---|
500 | uint64_t *poffVol,
|
---|
501 | uint64_t *pcbIntersect)
|
---|
502 | {
|
---|
503 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
504 |
|
---|
505 | if (RTDVM_RANGE_IS_INTERSECTING(pVol->offStart, pVol->cbVolume, offStart))
|
---|
506 | {
|
---|
507 | *poffVol = offStart - pVol->offStart;
|
---|
508 | *pcbIntersect = RT_MIN(cbRange, pVol->offStart + pVol->cbVolume - offStart);
|
---|
509 | return true;
|
---|
510 | }
|
---|
511 | return false;
|
---|
512 | }
|
---|
513 |
|
---|
514 | static DECLCALLBACK(int) rtDvmFmtGptVolumeRead(RTDVMVOLUMEFMT hVolFmt, uint64_t off, void *pvBuf, size_t cbRead)
|
---|
515 | {
|
---|
516 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
517 | AssertReturn(off + cbRead <= pVol->cbVolume, VERR_INVALID_PARAMETER);
|
---|
518 |
|
---|
519 | return rtDvmDiskRead(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbRead);
|
---|
520 | }
|
---|
521 |
|
---|
522 | static DECLCALLBACK(int) rtDvmFmtGptVolumeWrite(RTDVMVOLUMEFMT hVolFmt, uint64_t off, const void *pvBuf, size_t cbWrite)
|
---|
523 | {
|
---|
524 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
525 | AssertReturn(off + cbWrite <= pVol->cbVolume, VERR_INVALID_PARAMETER);
|
---|
526 |
|
---|
527 | return rtDvmDiskWrite(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbWrite);
|
---|
528 | }
|
---|
529 |
|
---|
530 | RTDVMFMTOPS g_rtDvmFmtGpt =
|
---|
531 | {
|
---|
532 | /* pszFmt */
|
---|
533 | "GPT",
|
---|
534 | /* enmFormat, */
|
---|
535 | RTDVMFORMATTYPE_GPT,
|
---|
536 | /* pfnProbe */
|
---|
537 | rtDvmFmtGptProbe,
|
---|
538 | /* pfnOpen */
|
---|
539 | rtDvmFmtGptOpen,
|
---|
540 | /* pfnInitialize */
|
---|
541 | rtDvmFmtGptInitialize,
|
---|
542 | /* pfnClose */
|
---|
543 | rtDvmFmtGptClose,
|
---|
544 | /* pfnQueryRangeUse */
|
---|
545 | rtDvmFmtGptQueryRangeUse,
|
---|
546 | /* pfnGetValidVolumes */
|
---|
547 | rtDvmFmtGptGetValidVolumes,
|
---|
548 | /* pfnGetMaxVolumes */
|
---|
549 | rtDvmFmtGptGetMaxVolumes,
|
---|
550 | /* pfnQueryFirstVolume */
|
---|
551 | rtDvmFmtGptQueryFirstVolume,
|
---|
552 | /* pfnQueryNextVolume */
|
---|
553 | rtDvmFmtGptQueryNextVolume,
|
---|
554 | /* pfnVolumeClose */
|
---|
555 | rtDvmFmtGptVolumeClose,
|
---|
556 | /* pfnVolumeGetSize */
|
---|
557 | rtDvmFmtGptVolumeGetSize,
|
---|
558 | /* pfnVolumeQueryName */
|
---|
559 | rtDvmFmtGptVolumeQueryName,
|
---|
560 | /* pfnVolumeGetType */
|
---|
561 | rtDvmFmtGptVolumeGetType,
|
---|
562 | /* pfnVolumeGetFlags */
|
---|
563 | rtDvmFmtGptVolumeGetFlags,
|
---|
564 | /* pfnVolumeIsRangeIntersecting */
|
---|
565 | rtDvmFmtGptVolumeIsRangeIntersecting,
|
---|
566 | /* pfnVolumeRead */
|
---|
567 | rtDvmFmtGptVolumeRead,
|
---|
568 | /* pfnVolumeWrite */
|
---|
569 | rtDvmFmtGptVolumeWrite
|
---|
570 | };
|
---|
571 |
|
---|