1 | /* $Id: dvmbsdlabel.cpp 69616 2017-11-08 13:58:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Disk Volume Management API (DVM) - BSD disklabel 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 | #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/asm.h>
|
---|
33 | #include "internal/dvm.h"
|
---|
34 |
|
---|
35 |
|
---|
36 | /*********************************************************************************************************************************
|
---|
37 | * Structures and Typedefs *
|
---|
38 | *********************************************************************************************************************************/
|
---|
39 |
|
---|
40 | /*
|
---|
41 | * Below are the on disk structures of a bsd disklabel as found in
|
---|
42 | * /usr/include/sys/disklabel.h from a FreeBSD system.
|
---|
43 | *
|
---|
44 | * Everything is stored in little endian on the disk.
|
---|
45 | */
|
---|
46 |
|
---|
47 | /** BSD disklabel magic. */
|
---|
48 | #define RTDVM_BSDLBL_MAGIC UINT32_C(0x82564557)
|
---|
49 | /** Maximum number of partitions in the label. */
|
---|
50 | #define RTDVM_BSDLBL_MAX_PARTITIONS 8
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * A BSD disk label partition.
|
---|
54 | */
|
---|
55 | #pragma pack(1)
|
---|
56 | typedef struct BsdLabelPartition
|
---|
57 | {
|
---|
58 | /** Number of sectors in the partition. */
|
---|
59 | uint32_t cSectors;
|
---|
60 | /** Start sector. */
|
---|
61 | uint32_t offSectorStart;
|
---|
62 | /** Filesystem fragment size. */
|
---|
63 | uint32_t cbFsFragment;
|
---|
64 | /** Filesystem type. */
|
---|
65 | uint8_t bFsType;
|
---|
66 | /** Filesystem fragments per block. */
|
---|
67 | uint8_t cFsFragmentsPerBlock;
|
---|
68 | /** Filesystem cylinders per group. */
|
---|
69 | uint16_t cFsCylPerGroup;
|
---|
70 | } BsdLabelPartition;
|
---|
71 | #pragma pack()
|
---|
72 | AssertCompileSize(BsdLabelPartition, 16);
|
---|
73 | /** Pointer to a BSD disklabel partition structure. */
|
---|
74 | typedef BsdLabelPartition *PBsdLabelPartition;
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * On disk BSD label structure.
|
---|
78 | */
|
---|
79 | #pragma pack(1)
|
---|
80 | typedef struct BsdLabel
|
---|
81 | {
|
---|
82 | /** Magic identifying the BSD disk label. */
|
---|
83 | uint32_t u32Magic;
|
---|
84 | /** Drive type */
|
---|
85 | uint16_t u16DriveType;
|
---|
86 | /** Subtype depending on the drive type above. */
|
---|
87 | uint16_t u16SubType;
|
---|
88 | /** Type name. */
|
---|
89 | uint8_t abTypeName[16];
|
---|
90 | /** Pack identifier. */
|
---|
91 | uint8_t abPackName[16];
|
---|
92 | /** Number of bytes per sector. */
|
---|
93 | uint32_t cbSector;
|
---|
94 | /** Number of sectors per track. */
|
---|
95 | uint32_t cSectorsPerTrack;
|
---|
96 | /** Number of tracks per cylinder. */
|
---|
97 | uint32_t cTracksPerCylinder;
|
---|
98 | /** Number of data cylinders pre unit. */
|
---|
99 | uint32_t cDataCylindersPerUnit;
|
---|
100 | /** Number of data sectors per cylinder. */
|
---|
101 | uint32_t cDataSectorsPerCylinder;
|
---|
102 | /** Number of data sectors per unit (unit as in disk drive?). */
|
---|
103 | uint32_t cSectorsPerUnit;
|
---|
104 | /** Number of spare sectors per track. */
|
---|
105 | uint16_t cSpareSectorsPerTrack;
|
---|
106 | /** Number of spare sectors per cylinder. */
|
---|
107 | uint16_t cSpareSectorsPerCylinder;
|
---|
108 | /** Number of alternate cylinders per unit. */
|
---|
109 | uint32_t cSpareCylindersPerUnit;
|
---|
110 | /** Rotational speed of the disk drive in rotations per minute. */
|
---|
111 | uint16_t cRotationsPerMinute;
|
---|
112 | /** Sector interleave. */
|
---|
113 | uint16_t uSectorInterleave;
|
---|
114 | /** Sector 0 skew, per track. */
|
---|
115 | uint16_t uSectorSkewPerTrack;
|
---|
116 | /** Sector 0 skew, per cylinder. */
|
---|
117 | uint16_t uSectorSkewPerCylinder;
|
---|
118 | /** Head switch time in us. */
|
---|
119 | uint32_t usHeadSwitch;
|
---|
120 | /** Time of a track-to-track seek in us. */
|
---|
121 | uint32_t usTrackSeek;
|
---|
122 | /** Flags. */
|
---|
123 | uint32_t fFlags;
|
---|
124 | /** Drive type sepcific information. */
|
---|
125 | uint32_t au32DriveData[5];
|
---|
126 | /** Reserved. */
|
---|
127 | uint32_t au32Reserved[5];
|
---|
128 | /** The magic number again. */
|
---|
129 | uint32_t u32Magic2;
|
---|
130 | /** Checksum (xor of the whole structure). */
|
---|
131 | uint16_t u16ChkSum;
|
---|
132 | /** Number of partitions in the array. */
|
---|
133 | uint16_t cPartitions;
|
---|
134 | /** Boot area size in bytes. */
|
---|
135 | uint32_t cbBootArea;
|
---|
136 | /** Maximum size of the filesystem super block. */
|
---|
137 | uint32_t cbFsSuperBlock;
|
---|
138 | /** The partition array. */
|
---|
139 | BsdLabelPartition aPartitions[RTDVM_BSDLBL_MAX_PARTITIONS];
|
---|
140 | } BsdLabel;
|
---|
141 | #pragma pack()
|
---|
142 | AssertCompileSize(BsdLabel, 148 + RTDVM_BSDLBL_MAX_PARTITIONS * 16);
|
---|
143 | /** Pointer to a BSD disklabel structure. */
|
---|
144 | typedef BsdLabel *PBsdLabel;
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * BSD disk label volume manager data.
|
---|
148 | */
|
---|
149 | typedef struct RTDVMFMTINTERNAL
|
---|
150 | {
|
---|
151 | /** Pointer to the underlying disk. */
|
---|
152 | PCRTDVMDISK pDisk;
|
---|
153 | /** Number of used partitions. */
|
---|
154 | uint32_t cPartitions;
|
---|
155 | /** Saved BSD disklabel structure. */
|
---|
156 | BsdLabel DiskLabel;
|
---|
157 | } RTDVMFMTINTERNAL;
|
---|
158 | /** Pointer to the MBR volume manager. */
|
---|
159 | typedef RTDVMFMTINTERNAL *PRTDVMFMTINTERNAL;
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * MBR volume data.
|
---|
163 | */
|
---|
164 | typedef struct RTDVMVOLUMEFMTINTERNAL
|
---|
165 | {
|
---|
166 | /** Pointer to the volume manager. */
|
---|
167 | PRTDVMFMTINTERNAL pVolMgr;
|
---|
168 | /** Partition table entry index. */
|
---|
169 | uint32_t idxEntry;
|
---|
170 | /** Start offset of the volume. */
|
---|
171 | uint64_t offStart;
|
---|
172 | /** Size of the volume. */
|
---|
173 | uint64_t cbVolume;
|
---|
174 | /** Pointer to the raw partition table entry. */
|
---|
175 | PBsdLabelPartition pBsdPartitionEntry;
|
---|
176 | } RTDVMVOLUMEFMTINTERNAL;
|
---|
177 | /** Pointer to an MBR volume. */
|
---|
178 | typedef RTDVMVOLUMEFMTINTERNAL *PRTDVMVOLUMEFMTINTERNAL;
|
---|
179 |
|
---|
180 | /** Converts a LBA number to the byte offset. */
|
---|
181 | #define RTDVM_BSDLBL_LBA2BYTE(lba, disk) ((lba) * (disk)->cbSector)
|
---|
182 | /** Converts a Byte offset to the LBA number. */
|
---|
183 | #define RTDVM_BSDLBL_BYTE2LBA(lba, disk) ((lba) / (disk)->cbSector)
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Calculates the checksum of the entire bsd disklabel structure.
|
---|
187 | *
|
---|
188 | * @returns The checksum.
|
---|
189 | * @param pBsdLabel BSD disklabel to get the checksum for.
|
---|
190 | */
|
---|
191 | static uint16_t rtDvmFmtBsdLblDiskLabelChkSum(PBsdLabel pBsdLabel)
|
---|
192 | {
|
---|
193 | uint16_t uChkSum = 0;
|
---|
194 | uint16_t *pCurr = (uint16_t *)pBsdLabel;
|
---|
195 | uint16_t *pEnd = (uint16_t *)&pBsdLabel->aPartitions[pBsdLabel->cPartitions];
|
---|
196 |
|
---|
197 | while (pCurr < pEnd)
|
---|
198 | uChkSum ^= *pCurr++;
|
---|
199 |
|
---|
200 | return uChkSum;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Converts a partition entry to the host endianness.
|
---|
205 | *
|
---|
206 | * @returns nothing.
|
---|
207 | * @param pPartition The partition to decode.
|
---|
208 | */
|
---|
209 | static void rtDvmFmtBsdLblDiskLabelDecodePartition(PBsdLabelPartition pPartition)
|
---|
210 | {
|
---|
211 | pPartition->cSectors = RT_LE2H_U32(pPartition->cSectors);
|
---|
212 | pPartition->offSectorStart = RT_LE2H_U32(pPartition->offSectorStart);
|
---|
213 | pPartition->cbFsFragment = RT_LE2H_U32(pPartition->cbFsFragment);
|
---|
214 | pPartition->cFsCylPerGroup = RT_LE2H_U16(pPartition->cFsCylPerGroup);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * Converts the on disk BSD label to the host endianness.
|
---|
219 | *
|
---|
220 | * @returns Whether the given label structure is a valid BSD disklabel.
|
---|
221 | * @param pBsdLabel Pointer to the BSD disklabel to decode.
|
---|
222 | */
|
---|
223 | static bool rtDvmFmtBsdLblDiskLabelDecode(PBsdLabel pBsdLabel)
|
---|
224 | {
|
---|
225 | pBsdLabel->u32Magic = RT_LE2H_U32(pBsdLabel->u32Magic);
|
---|
226 | pBsdLabel->u16DriveType = RT_LE2H_U16(pBsdLabel->u16DriveType);
|
---|
227 | pBsdLabel->u16SubType = RT_LE2H_U16(pBsdLabel->u16SubType);
|
---|
228 | pBsdLabel->cbSector = RT_LE2H_U32(pBsdLabel->cbSector);
|
---|
229 | pBsdLabel->cSectorsPerTrack = RT_LE2H_U32(pBsdLabel->cSectorsPerTrack);
|
---|
230 | pBsdLabel->cTracksPerCylinder = RT_LE2H_U32(pBsdLabel->cTracksPerCylinder);
|
---|
231 | pBsdLabel->cDataCylindersPerUnit = RT_LE2H_U32(pBsdLabel->cDataCylindersPerUnit);
|
---|
232 | pBsdLabel->cDataSectorsPerCylinder = RT_LE2H_U32(pBsdLabel->cDataSectorsPerCylinder);
|
---|
233 | pBsdLabel->cSectorsPerUnit = RT_LE2H_U32(pBsdLabel->cSectorsPerUnit);
|
---|
234 | pBsdLabel->cSpareSectorsPerTrack = RT_LE2H_U16(pBsdLabel->cSpareSectorsPerTrack);
|
---|
235 | pBsdLabel->cSpareSectorsPerCylinder = RT_LE2H_U16(pBsdLabel->cSpareSectorsPerCylinder);
|
---|
236 | pBsdLabel->cSpareCylindersPerUnit = RT_LE2H_U32(pBsdLabel->cSpareCylindersPerUnit);
|
---|
237 | pBsdLabel->cRotationsPerMinute = RT_LE2H_U16(pBsdLabel->cRotationsPerMinute);
|
---|
238 | pBsdLabel->uSectorInterleave = RT_LE2H_U16(pBsdLabel->uSectorInterleave);
|
---|
239 | pBsdLabel->uSectorSkewPerTrack = RT_LE2H_U16(pBsdLabel->uSectorSkewPerTrack);
|
---|
240 | pBsdLabel->uSectorSkewPerCylinder = RT_LE2H_U16(pBsdLabel->uSectorSkewPerCylinder);
|
---|
241 | pBsdLabel->usHeadSwitch = RT_LE2H_U16(pBsdLabel->usHeadSwitch);
|
---|
242 | pBsdLabel->usTrackSeek = RT_LE2H_U16(pBsdLabel->usTrackSeek);
|
---|
243 | pBsdLabel->fFlags = RT_LE2H_U32(pBsdLabel->fFlags);
|
---|
244 |
|
---|
245 | for (unsigned i = 0; i < RT_ELEMENTS(pBsdLabel->au32DriveData); i++)
|
---|
246 | pBsdLabel->au32DriveData[i] = RT_LE2H_U32(pBsdLabel->au32DriveData[i]);
|
---|
247 | for (unsigned i = 0; i < RT_ELEMENTS(pBsdLabel->au32Reserved); i++)
|
---|
248 | pBsdLabel->au32Reserved[i] = RT_LE2H_U32(pBsdLabel->au32Reserved[i]);
|
---|
249 |
|
---|
250 | pBsdLabel->u32Magic2 = RT_LE2H_U32(pBsdLabel->u32Magic2);
|
---|
251 | pBsdLabel->u16ChkSum = RT_LE2H_U16(pBsdLabel->u16ChkSum);
|
---|
252 | pBsdLabel->cPartitions = RT_LE2H_U16(pBsdLabel->cPartitions);
|
---|
253 | pBsdLabel->cbBootArea = RT_LE2H_U32(pBsdLabel->cbBootArea);
|
---|
254 | pBsdLabel->cbFsSuperBlock = RT_LE2H_U32(pBsdLabel->cbFsSuperBlock);
|
---|
255 |
|
---|
256 | /* Check the magics now. */
|
---|
257 | if ( pBsdLabel->u32Magic != RTDVM_BSDLBL_MAGIC
|
---|
258 | || pBsdLabel->u32Magic2 != RTDVM_BSDLBL_MAGIC
|
---|
259 | || pBsdLabel->cPartitions != RTDVM_BSDLBL_MAX_PARTITIONS)
|
---|
260 | return false;
|
---|
261 |
|
---|
262 | /* Convert the partitions array. */
|
---|
263 | for (unsigned i = 0; i < RT_ELEMENTS(pBsdLabel->aPartitions); i++)
|
---|
264 | rtDvmFmtBsdLblDiskLabelDecodePartition(&pBsdLabel->aPartitions[i]);
|
---|
265 |
|
---|
266 | /* Check the checksum now. */
|
---|
267 | uint16_t u16ChkSumSaved = pBsdLabel->u16ChkSum;
|
---|
268 |
|
---|
269 | pBsdLabel->u16ChkSum = 0;
|
---|
270 | if (u16ChkSumSaved != rtDvmFmtBsdLblDiskLabelChkSum(pBsdLabel))
|
---|
271 | return false;
|
---|
272 |
|
---|
273 | pBsdLabel->u16ChkSum = u16ChkSumSaved;
|
---|
274 | return true;
|
---|
275 | }
|
---|
276 |
|
---|
277 | static DECLCALLBACK(int) rtDvmFmtBsdLblProbe(PCRTDVMDISK pDisk, uint32_t *puScore)
|
---|
278 | {
|
---|
279 | BsdLabel DiskLabel;
|
---|
280 | int rc = VINF_SUCCESS;
|
---|
281 |
|
---|
282 | *puScore = RTDVM_MATCH_SCORE_UNSUPPORTED;
|
---|
283 |
|
---|
284 | if (pDisk->cbDisk >= sizeof(BsdLabel))
|
---|
285 | {
|
---|
286 | /* Read from the disk and check for the disk label structure. */
|
---|
287 | rc = rtDvmDiskRead(pDisk, RTDVM_BSDLBL_LBA2BYTE(1, pDisk), &DiskLabel, sizeof(BsdLabel));
|
---|
288 | if ( RT_SUCCESS(rc)
|
---|
289 | && rtDvmFmtBsdLblDiskLabelDecode(&DiskLabel))
|
---|
290 | *puScore = RTDVM_MATCH_SCORE_PERFECT;
|
---|
291 | }
|
---|
292 | return rc;
|
---|
293 | }
|
---|
294 |
|
---|
295 | static DECLCALLBACK(int) rtDvmFmtBsdLblOpen(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
|
---|
296 | {
|
---|
297 | int rc = VINF_SUCCESS;
|
---|
298 | PRTDVMFMTINTERNAL pThis = NULL;
|
---|
299 |
|
---|
300 | pThis = (PRTDVMFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMFMTINTERNAL));
|
---|
301 | if (pThis)
|
---|
302 | {
|
---|
303 | pThis->pDisk = pDisk;
|
---|
304 | pThis->cPartitions = 0;
|
---|
305 |
|
---|
306 | /* Read from the disk and check for the disk label structure. */
|
---|
307 | rc = rtDvmDiskRead(pDisk, RTDVM_BSDLBL_LBA2BYTE(1, pDisk), &pThis->DiskLabel, sizeof(BsdLabel));
|
---|
308 | if ( RT_SUCCESS(rc)
|
---|
309 | && rtDvmFmtBsdLblDiskLabelDecode(&pThis->DiskLabel))
|
---|
310 | {
|
---|
311 | /* Count number of used entries. */
|
---|
312 | for (unsigned i = 0; i < pThis->DiskLabel.cPartitions; i++)
|
---|
313 | if (pThis->DiskLabel.aPartitions[i].cSectors)
|
---|
314 | pThis->cPartitions++;
|
---|
315 |
|
---|
316 | *phVolMgrFmt = pThis;
|
---|
317 | }
|
---|
318 | else
|
---|
319 | {
|
---|
320 | RTMemFree(pThis);
|
---|
321 | rc = VERR_INVALID_MAGIC;
|
---|
322 | }
|
---|
323 | }
|
---|
324 | else
|
---|
325 | rc = VERR_NO_MEMORY;
|
---|
326 |
|
---|
327 | return rc;
|
---|
328 | }
|
---|
329 |
|
---|
330 | static DECLCALLBACK(int) rtDvmFmtBsdLblInitialize(PCRTDVMDISK pDisk, PRTDVMFMT phVolMgrFmt)
|
---|
331 | {
|
---|
332 | NOREF(pDisk); NOREF(phVolMgrFmt);
|
---|
333 | return VERR_NOT_IMPLEMENTED;
|
---|
334 | }
|
---|
335 |
|
---|
336 | static DECLCALLBACK(void) rtDvmFmtBsdLblClose(RTDVMFMT hVolMgrFmt)
|
---|
337 | {
|
---|
338 | PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
|
---|
339 |
|
---|
340 | pThis->pDisk = NULL;
|
---|
341 | pThis->cPartitions = 0;
|
---|
342 | memset(&pThis->DiskLabel, 0, sizeof(BsdLabel));
|
---|
343 | RTMemFree(pThis);
|
---|
344 | }
|
---|
345 |
|
---|
346 | static DECLCALLBACK(int) rtDvmFmtBsdLblQueryRangeUse(RTDVMFMT hVolMgrFmt,
|
---|
347 | uint64_t off, uint64_t cbRange,
|
---|
348 | bool *pfUsed)
|
---|
349 | {
|
---|
350 | PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
|
---|
351 |
|
---|
352 | NOREF(cbRange);
|
---|
353 |
|
---|
354 | if (off <= RTDVM_BSDLBL_LBA2BYTE(1, pThis->pDisk))
|
---|
355 | *pfUsed = true;
|
---|
356 | else
|
---|
357 | *pfUsed = false;
|
---|
358 |
|
---|
359 | return VINF_SUCCESS;
|
---|
360 | }
|
---|
361 |
|
---|
362 | static DECLCALLBACK(uint32_t) rtDvmFmtBsdLblGetValidVolumes(RTDVMFMT hVolMgrFmt)
|
---|
363 | {
|
---|
364 | PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
|
---|
365 | return pThis->cPartitions;
|
---|
366 | }
|
---|
367 |
|
---|
368 | static DECLCALLBACK(uint32_t) rtDvmFmtBsdLblGetMaxVolumes(RTDVMFMT hVolMgrFmt)
|
---|
369 | {
|
---|
370 | PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
|
---|
371 | return pThis->DiskLabel.cPartitions;
|
---|
372 | }
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * Creates a new volume.
|
---|
376 | *
|
---|
377 | * @returns IPRT status code.
|
---|
378 | * @param pThis The MBR volume manager data.
|
---|
379 | * @param pbBsdLblEntry The raw MBR entry data.
|
---|
380 | * @param idx The index in the partition table.
|
---|
381 | * @param phVolFmt Where to store the volume data on success.
|
---|
382 | */
|
---|
383 | static int rtDvmFmtBsdLblVolumeCreate(PRTDVMFMTINTERNAL pThis, PBsdLabelPartition pBsdPartitionEntry,
|
---|
384 | uint32_t idx, PRTDVMVOLUMEFMT phVolFmt)
|
---|
385 | {
|
---|
386 | int rc = VINF_SUCCESS;
|
---|
387 | PRTDVMVOLUMEFMTINTERNAL pVol = (PRTDVMVOLUMEFMTINTERNAL)RTMemAllocZ(sizeof(RTDVMVOLUMEFMTINTERNAL));
|
---|
388 |
|
---|
389 | if (pVol)
|
---|
390 | {
|
---|
391 | pVol->pVolMgr = pThis;
|
---|
392 | pVol->idxEntry = idx;
|
---|
393 | pVol->pBsdPartitionEntry = pBsdPartitionEntry;
|
---|
394 | pVol->offStart = (uint64_t)pBsdPartitionEntry->offSectorStart * pThis->DiskLabel.cbSector;
|
---|
395 | pVol->cbVolume = (uint64_t)pBsdPartitionEntry->cSectors * pThis->DiskLabel.cbSector;
|
---|
396 |
|
---|
397 | *phVolFmt = pVol;
|
---|
398 | }
|
---|
399 | else
|
---|
400 | rc = VERR_NO_MEMORY;
|
---|
401 |
|
---|
402 | return rc;
|
---|
403 | }
|
---|
404 |
|
---|
405 | static DECLCALLBACK(int) rtDvmFmtBsdLblQueryFirstVolume(RTDVMFMT hVolMgrFmt, PRTDVMVOLUMEFMT phVolFmt)
|
---|
406 | {
|
---|
407 | int rc = VINF_SUCCESS;
|
---|
408 | PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
|
---|
409 |
|
---|
410 | if (pThis->cPartitions != 0)
|
---|
411 | {
|
---|
412 | /* Search for the first non empty entry. */
|
---|
413 | for (unsigned i = 0; i < pThis->DiskLabel.cPartitions; i++)
|
---|
414 | {
|
---|
415 | if (pThis->DiskLabel.aPartitions[i].cSectors)
|
---|
416 | {
|
---|
417 | rc = rtDvmFmtBsdLblVolumeCreate(pThis, &pThis->DiskLabel.aPartitions[i], i, phVolFmt);
|
---|
418 | break;
|
---|
419 | }
|
---|
420 | }
|
---|
421 | }
|
---|
422 | else
|
---|
423 | rc = VERR_DVM_MAP_EMPTY;
|
---|
424 |
|
---|
425 | return rc;
|
---|
426 | }
|
---|
427 |
|
---|
428 | static DECLCALLBACK(int) rtDvmFmtBsdLblQueryNextVolume(RTDVMFMT hVolMgrFmt, RTDVMVOLUMEFMT hVolFmt, PRTDVMVOLUMEFMT phVolFmtNext)
|
---|
429 | {
|
---|
430 | int rc = VERR_DVM_MAP_NO_VOLUME;
|
---|
431 | PRTDVMFMTINTERNAL pThis = hVolMgrFmt;
|
---|
432 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
433 | PBsdLabelPartition pBsdPartitionEntry = pVol->pBsdPartitionEntry + 1;
|
---|
434 |
|
---|
435 | for (unsigned i = pVol->idxEntry + 1; i < pThis->DiskLabel.cPartitions; i++)
|
---|
436 | {
|
---|
437 | if (pBsdPartitionEntry->cSectors)
|
---|
438 | {
|
---|
439 | rc = rtDvmFmtBsdLblVolumeCreate(pThis, pBsdPartitionEntry, i, phVolFmtNext);
|
---|
440 | break;
|
---|
441 | }
|
---|
442 | pBsdPartitionEntry++;
|
---|
443 | }
|
---|
444 |
|
---|
445 | return rc;
|
---|
446 | }
|
---|
447 |
|
---|
448 | static DECLCALLBACK(void) rtDvmFmtBsdLblVolumeClose(RTDVMVOLUMEFMT hVolFmt)
|
---|
449 | {
|
---|
450 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
451 |
|
---|
452 | pVol->pVolMgr = NULL;
|
---|
453 | pVol->offStart = 0;
|
---|
454 | pVol->cbVolume = 0;
|
---|
455 | pVol->pBsdPartitionEntry = NULL;
|
---|
456 |
|
---|
457 | RTMemFree(pVol);
|
---|
458 | }
|
---|
459 |
|
---|
460 | static DECLCALLBACK(uint64_t) rtDvmFmtBsdLblVolumeGetSize(RTDVMVOLUMEFMT hVolFmt)
|
---|
461 | {
|
---|
462 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
463 |
|
---|
464 | return pVol->cbVolume;
|
---|
465 | }
|
---|
466 |
|
---|
467 | static DECLCALLBACK(int) rtDvmFmtBsdLblVolumeQueryName(RTDVMVOLUMEFMT hVolFmt, char **ppszVolName)
|
---|
468 | {
|
---|
469 | NOREF(hVolFmt); NOREF(ppszVolName);
|
---|
470 | return VERR_NOT_SUPPORTED;
|
---|
471 | }
|
---|
472 |
|
---|
473 | static DECLCALLBACK(RTDVMVOLTYPE) rtDvmFmtBsdLblVolumeGetType(RTDVMVOLUMEFMT hVolFmt)
|
---|
474 | {
|
---|
475 | NOREF(hVolFmt);
|
---|
476 | return RTDVMVOLTYPE_UNKNOWN;
|
---|
477 | }
|
---|
478 |
|
---|
479 | static DECLCALLBACK(uint64_t) rtDvmFmtBsdLblVolumeGetFlags(RTDVMVOLUMEFMT hVolFmt)
|
---|
480 | {
|
---|
481 | NOREF(hVolFmt);
|
---|
482 | return 0;
|
---|
483 | }
|
---|
484 |
|
---|
485 | static DECLCALLBACK(bool) rtDvmFmtBsdLblVolumeIsRangeIntersecting(RTDVMVOLUMEFMT hVolFmt,
|
---|
486 | uint64_t offStart, size_t cbRange,
|
---|
487 | uint64_t *poffVol,
|
---|
488 | uint64_t *pcbIntersect)
|
---|
489 | {
|
---|
490 | bool fIntersect = false;
|
---|
491 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
492 |
|
---|
493 | if (RTDVM_RANGE_IS_INTERSECTING(pVol->offStart, pVol->cbVolume, offStart))
|
---|
494 | {
|
---|
495 | fIntersect = true;
|
---|
496 | *poffVol = offStart - pVol->offStart;
|
---|
497 | *pcbIntersect = RT_MIN(cbRange, pVol->offStart + pVol->cbVolume - offStart);
|
---|
498 | }
|
---|
499 |
|
---|
500 | return fIntersect;
|
---|
501 | }
|
---|
502 |
|
---|
503 | static DECLCALLBACK(int) rtDvmFmtBsdLblVolumeRead(RTDVMVOLUMEFMT hVolFmt, uint64_t off, void *pvBuf, size_t cbRead)
|
---|
504 | {
|
---|
505 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
506 | AssertReturn(off + cbRead <= pVol->cbVolume, VERR_INVALID_PARAMETER);
|
---|
507 |
|
---|
508 | return rtDvmDiskRead(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbRead);
|
---|
509 | }
|
---|
510 |
|
---|
511 | static DECLCALLBACK(int) rtDvmFmtBsdLblVolumeWrite(RTDVMVOLUMEFMT hVolFmt, uint64_t off, const void *pvBuf, size_t cbWrite)
|
---|
512 | {
|
---|
513 | PRTDVMVOLUMEFMTINTERNAL pVol = hVolFmt;
|
---|
514 | AssertReturn(off + cbWrite <= pVol->cbVolume, VERR_INVALID_PARAMETER);
|
---|
515 |
|
---|
516 | return rtDvmDiskWrite(pVol->pVolMgr->pDisk, pVol->offStart + off, pvBuf, cbWrite);
|
---|
517 | }
|
---|
518 |
|
---|
519 | DECLHIDDEN(RTDVMFMTOPS) g_rtDvmFmtBsdLbl =
|
---|
520 | {
|
---|
521 | /* pcszFmt */
|
---|
522 | "BsdLabel",
|
---|
523 | /* enmFormat, */
|
---|
524 | RTDVMFORMATTYPE_BSD_LABLE,
|
---|
525 | /* pfnProbe */
|
---|
526 | rtDvmFmtBsdLblProbe,
|
---|
527 | /* pfnOpen */
|
---|
528 | rtDvmFmtBsdLblOpen,
|
---|
529 | /* pfnInitialize */
|
---|
530 | rtDvmFmtBsdLblInitialize,
|
---|
531 | /* pfnClose */
|
---|
532 | rtDvmFmtBsdLblClose,
|
---|
533 | /* pfnQueryRangeUse */
|
---|
534 | rtDvmFmtBsdLblQueryRangeUse,
|
---|
535 | /* pfnGetValidVolumes */
|
---|
536 | rtDvmFmtBsdLblGetValidVolumes,
|
---|
537 | /* pfnGetMaxVolumes */
|
---|
538 | rtDvmFmtBsdLblGetMaxVolumes,
|
---|
539 | /* pfnQueryFirstVolume */
|
---|
540 | rtDvmFmtBsdLblQueryFirstVolume,
|
---|
541 | /* pfnQueryNextVolume */
|
---|
542 | rtDvmFmtBsdLblQueryNextVolume,
|
---|
543 | /* pfnVolumeClose */
|
---|
544 | rtDvmFmtBsdLblVolumeClose,
|
---|
545 | /* pfnVolumeGetSize */
|
---|
546 | rtDvmFmtBsdLblVolumeGetSize,
|
---|
547 | /* pfnVolumeQueryName */
|
---|
548 | rtDvmFmtBsdLblVolumeQueryName,
|
---|
549 | /* pfnVolumeGetType */
|
---|
550 | rtDvmFmtBsdLblVolumeGetType,
|
---|
551 | /* pfnVolumeGetFlags */
|
---|
552 | rtDvmFmtBsdLblVolumeGetFlags,
|
---|
553 | /* pfnVolumeIsRangeIntersecting */
|
---|
554 | rtDvmFmtBsdLblVolumeIsRangeIntersecting,
|
---|
555 | /* pfnVolumeRead */
|
---|
556 | rtDvmFmtBsdLblVolumeRead,
|
---|
557 | /* pfnVolumeWrite */
|
---|
558 | rtDvmFmtBsdLblVolumeWrite
|
---|
559 | };
|
---|
560 |
|
---|