1 | /* $Id: vfsmount.cpp 69861 2017-11-28 19:01:35Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Virtual File System, Mounting.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-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 | #define LOG_GROUP RTLOGGROUP_VFS
|
---|
32 | #include <iprt/vfs.h>
|
---|
33 |
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/file.h>
|
---|
38 | #include <iprt/fsvfs.h>
|
---|
39 | #include <iprt/mem.h>
|
---|
40 | #include <iprt/log.h>
|
---|
41 | #include <iprt/string.h>
|
---|
42 | #include <iprt/vfslowlevel.h>
|
---|
43 |
|
---|
44 | #include <iprt/formats/fat.h>
|
---|
45 | #include <iprt/formats/iso9660.h>
|
---|
46 | #include <iprt/formats/udf.h>
|
---|
47 | #include <iprt/formats/ext2.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Structures and Typedefs *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | /** Buffer structure for the detection routines. */
|
---|
54 | typedef union RTVFSMOUNTBUF
|
---|
55 | {
|
---|
56 | uint8_t ab[2048];
|
---|
57 | uint32_t au32[2048/4];
|
---|
58 | FATBOOTSECTOR Bootsector;
|
---|
59 | ISO9660VOLDESCHDR IsoHdr;
|
---|
60 | } RTVFSMOUNTBUF;
|
---|
61 | AssertCompileSize(RTVFSMOUNTBUF, 2048);
|
---|
62 | typedef RTVFSMOUNTBUF *PRTVFSMOUNTBUF;
|
---|
63 |
|
---|
64 |
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Checks if the given 2K sector at offset 32KB looks like ISO-9660 or UDF.
|
---|
68 | *
|
---|
69 | * @returns true if likely ISO or UDF, otherwise false.
|
---|
70 | * @param pVolDescHdr Whatever is at offset 32KB. 2KB buffer.
|
---|
71 | */
|
---|
72 | static bool rtVfsMountIsIsoFs(PCISO9660VOLDESCHDR pVolDescHdr)
|
---|
73 | {
|
---|
74 | if ( memcmp(pVolDescHdr->achStdId, RT_STR_TUPLE(ISO9660VOLDESC_STD_ID)) == 0
|
---|
75 | && pVolDescHdr->bDescType <= ISO9660VOLDESC_TYPE_PARTITION
|
---|
76 | && pVolDescHdr->bDescVersion != 0
|
---|
77 | && pVolDescHdr->bDescVersion <= 3 /* don't be too picky, just increase the likelyhood */ )
|
---|
78 | return true;
|
---|
79 |
|
---|
80 | if ( memcmp(pVolDescHdr->achStdId, RT_STR_TUPLE(UDF_EXT_VOL_DESC_STD_ID_BEGIN)) == 0
|
---|
81 | && pVolDescHdr->bDescType == UDF_EXT_VOL_DESC_TYPE
|
---|
82 | && pVolDescHdr->bDescVersion == UDF_EXT_VOL_DESC_VERSION)
|
---|
83 | return true;
|
---|
84 |
|
---|
85 | return false;
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Check if the given bootsector is a NTFS boot sector.
|
---|
91 | *
|
---|
92 | * @returns true if NTFS, false if not.
|
---|
93 | * @param pBootSector The boot sector to inspect.
|
---|
94 | */
|
---|
95 | static bool rtVfsMountIsNtfs(PCFATBOOTSECTOR pBootSector)
|
---|
96 | {
|
---|
97 | if (memcmp(pBootSector->achOemName, RT_STR_TUPLE("NTFS ")) != 0)
|
---|
98 | return false;
|
---|
99 |
|
---|
100 | uint16_t cbSector = RT_LE2H_U16(pBootSector->Bpb.Bpb331.cbSector);
|
---|
101 | if ( cbSector < 0x100
|
---|
102 | || cbSector >= 0x1000
|
---|
103 | || (cbSector & 0xff) != 0)
|
---|
104 | {
|
---|
105 | Log2(("rtVfsMountIsNtfs: cbSector=%#x: out of range\n", cbSector));
|
---|
106 | return false;
|
---|
107 | }
|
---|
108 |
|
---|
109 | if ( !RT_IS_POWER_OF_TWO(pBootSector->Bpb.Bpb331.cSectorsPerCluster)
|
---|
110 | || pBootSector->Bpb.Bpb331.cSectorsPerCluster == 0
|
---|
111 | || pBootSector->Bpb.Bpb331.cSectorsPerCluster > 128)
|
---|
112 | {
|
---|
113 | Log2(("rtVfsMountIsNtfs: cSectorsPerCluster=%#x: out of range\n", pBootSector->Bpb.Bpb331.cSectorsPerCluster));
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 |
|
---|
117 | if ((uint32_t)pBootSector->Bpb.Bpb331.cSectorsPerCluster * cbSector > _64K)
|
---|
118 | {
|
---|
119 | Log2(("rtVfsMountIsNtfs: cSectorsPerCluster=%#x * cbSector=%#x => %#x: out of range\n",
|
---|
120 | pBootSector->Bpb.Bpb331.cSectorsPerCluster, cbSector,
|
---|
121 | (uint32_t)pBootSector->Bpb.Bpb331.cSectorsPerCluster * cbSector));
|
---|
122 | return false;
|
---|
123 | }
|
---|
124 |
|
---|
125 | if ( pBootSector->Bpb.Bpb331.cReservedSectors != 0
|
---|
126 | || pBootSector->Bpb.Bpb331.cMaxRootDirEntries != 0
|
---|
127 | || pBootSector->Bpb.Bpb331.cTotalSectors16 != 0
|
---|
128 | || pBootSector->Bpb.Bpb331.cTotalSectors32 != 0
|
---|
129 | || pBootSector->Bpb.Bpb331.cSectorsPerFat != 0
|
---|
130 | || pBootSector->Bpb.Bpb331.cFats != 0)
|
---|
131 | {
|
---|
132 | Log2(("rtVfsMountIsNtfs: cReservedSectors=%#x cMaxRootDirEntries=%#x cTotalSectors=%#x cTotalSectors32=%#x cSectorsPerFat=%#x cFats=%#x: should all be zero, but one or more aren't\n",
|
---|
133 | RT_LE2H_U16(pBootSector->Bpb.Bpb331.cReservedSectors),
|
---|
134 | RT_LE2H_U16(pBootSector->Bpb.Bpb331.cMaxRootDirEntries),
|
---|
135 | RT_LE2H_U16(pBootSector->Bpb.Bpb331.cTotalSectors16),
|
---|
136 | RT_LE2H_U32(pBootSector->Bpb.Bpb331.cTotalSectors32),
|
---|
137 | RT_LE2H_U16(pBootSector->Bpb.Bpb331.cSectorsPerFat),
|
---|
138 | pBootSector->Bpb.Bpb331.cFats));
|
---|
139 | return false;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /** @todo NTFS specific checks: MFT cluster number, cluster per index block. */
|
---|
143 |
|
---|
144 | return true;
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Check if the given bootsector is a HPFS boot sector.
|
---|
150 | *
|
---|
151 | * @returns true if NTFS, false if not.
|
---|
152 | * @param pBootSector The boot sector to inspect.
|
---|
153 | * @param hVfsFileIn The volume file.
|
---|
154 | * @param pBuf2 A 2nd buffer.
|
---|
155 | */
|
---|
156 | static bool rtVfsMountIsHpfs(PCFATBOOTSECTOR pBootSector, RTVFSFILE hVfsFileIn, PRTVFSMOUNTBUF pBuf2)
|
---|
157 | {
|
---|
158 | if (memcmp(pBootSector->Bpb.Ebpb.achType, RT_STR_TUPLE("HPFS ")) != 0)
|
---|
159 | return false;
|
---|
160 |
|
---|
161 | /* Superblock is at sector 16, spare superblock at 17. */
|
---|
162 | int rc = RTVfsFileReadAt(hVfsFileIn, 16 * 512, pBuf2, 512 * 2, NULL);
|
---|
163 | if (RT_FAILURE(rc))
|
---|
164 | {
|
---|
165 | Log2(("rtVfsMountIsHpfs: Error reading superblock: %Rrc\n", rc));
|
---|
166 | return false;
|
---|
167 | }
|
---|
168 |
|
---|
169 | if ( RT_LE2H_U32(pBuf2->au32[0]) != UINT32_C(0xf995e849)
|
---|
170 | || RT_LE2H_U32(pBuf2->au32[1]) != UINT32_C(0xfa53e9c5)
|
---|
171 | || RT_LE2H_U32(pBuf2->au32[512/4 + 0]) != UINT32_C(0xf9911849)
|
---|
172 | || RT_LE2H_U32(pBuf2->au32[512/4 + 1]) != UINT32_C(0xfa5229c5))
|
---|
173 | {
|
---|
174 | Log2(("rtVfsMountIsHpfs: Superblock or spare superblock signature mismatch: %#x %#x %#x %#x\n",
|
---|
175 | RT_LE2H_U32(pBuf2->au32[0]), RT_LE2H_U32(pBuf2->au32[1]),
|
---|
176 | RT_LE2H_U32(pBuf2->au32[512/4 + 0]), RT_LE2H_U32(pBuf2->au32[512/4 + 1]) ));
|
---|
177 | return false;
|
---|
178 | }
|
---|
179 |
|
---|
180 | return true;
|
---|
181 | }
|
---|
182 |
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Check if the given bootsector is a FAT boot sector.
|
---|
186 | *
|
---|
187 | * @returns true if NTFS, false if not.
|
---|
188 | * @param pBootSector The boot sector to inspect.
|
---|
189 | * @param pbRaw Pointer to the raw boot sector buffer.
|
---|
190 | * @param cbRaw Number of bytes read starting with the boot
|
---|
191 | * sector (which @a pbRaw points to).
|
---|
192 | * @param hVfsFileIn The volume file.
|
---|
193 | * @param pBuf2 A 2nd buffer.
|
---|
194 | */
|
---|
195 | static bool rtVfsMountIsFat(PCFATBOOTSECTOR pBootSector, uint8_t const *pbRaw, size_t cbRaw,
|
---|
196 | RTVFSFILE hVfsFileIn, PRTVFSMOUNTBUF pBuf2)
|
---|
197 | {
|
---|
198 | Assert(cbRaw >= 1024);
|
---|
199 |
|
---|
200 | /*
|
---|
201 | * Check the DOS signature first. The PC-DOS 1.0 boot floppy does not have
|
---|
202 | * a signature and we ASSUME this is the case for all floppies formated by it.
|
---|
203 | */
|
---|
204 | if (pBootSector->uSignature != FATBOOTSECTOR_SIGNATURE)
|
---|
205 | {
|
---|
206 | if (pBootSector->uSignature != 0)
|
---|
207 | return false;
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * PC-DOS 1.0 does a 2fh byte short jump w/o any NOP following it.
|
---|
211 | * Instead the following are three words and a 9 byte build date
|
---|
212 | * string. The remaining space is zero filled.
|
---|
213 | *
|
---|
214 | * Note! No idea how this would look like for 8" floppies, only got 5"1/4'.
|
---|
215 | *
|
---|
216 | * ASSUME all non-BPB disks are using this format.
|
---|
217 | */
|
---|
218 | if ( pBootSector->abJmp[0] != 0xeb /* jmp rel8 */
|
---|
219 | || pBootSector->abJmp[1] < 0x2f
|
---|
220 | || pBootSector->abJmp[1] >= 0x80
|
---|
221 | || pBootSector->abJmp[2] == 0x90 /* nop */)
|
---|
222 | {
|
---|
223 | Log2(("rtVfsMountIsFat: No DOS v1.0 bootsector either - invalid jmp: %.3Rhxs\n", pBootSector->abJmp));
|
---|
224 | return false;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /* Check the FAT ID so we can tell if this is double or single sided, as well as being a valid FAT12 start. */
|
---|
228 | if ( (pbRaw[512] != 0xfe && pbRaw[0] != 0xff)
|
---|
229 | || pbRaw[512 + 1] != 0xff
|
---|
230 | || pbRaw[512 + 2] != 0xff)
|
---|
231 | {
|
---|
232 | Log2(("rtVfsMountIsFat: No DOS v1.0 bootsector either - unexpected start of FAT: %.3Rhxs\n", &pbRaw[512]));
|
---|
233 | return false;
|
---|
234 | }
|
---|
235 |
|
---|
236 | uint32_t const offJump = 2 + pBootSector->abJmp[1];
|
---|
237 | uint32_t const offFirstZero = 2 /*jmp */ + 3 * 2 /* words */ + 9 /* date string */;
|
---|
238 | Assert(offFirstZero >= RT_UOFFSETOF(FATBOOTSECTOR, Bpb));
|
---|
239 | uint32_t const cbZeroPad = RT_MIN(offJump - offFirstZero,
|
---|
240 | sizeof(pBootSector->Bpb.Bpb20) - (offFirstZero - RT_OFFSETOF(FATBOOTSECTOR, Bpb)));
|
---|
241 |
|
---|
242 | if (!ASMMemIsAllU8((uint8_t const *)pBootSector + offFirstZero, cbZeroPad, 0))
|
---|
243 | {
|
---|
244 | Log2(("rtVfsMountIsFat: No DOS v1.0 bootsector either - expected zero padding %#x LB %#x: %.*Rhxs\n",
|
---|
245 | offFirstZero, cbZeroPad, cbZeroPad, (uint8_t const *)pBootSector + offFirstZero));
|
---|
246 | return false;
|
---|
247 | }
|
---|
248 | }
|
---|
249 | else
|
---|
250 | {
|
---|
251 | /*
|
---|
252 | * DOS 2.0 or later.
|
---|
253 | *
|
---|
254 | * Start by checking if we've got a known jump instruction first, because
|
---|
255 | * that will give us a max (E)BPB size hint.
|
---|
256 | */
|
---|
257 | uint8_t offJmp = UINT8_MAX;
|
---|
258 | if ( pBootSector->abJmp[0] == 0xeb
|
---|
259 | && pBootSector->abJmp[1] <= 0x7f)
|
---|
260 | offJmp = pBootSector->abJmp[1] + 2;
|
---|
261 | else if ( pBootSector->abJmp[0] == 0x90
|
---|
262 | && pBootSector->abJmp[1] == 0xeb
|
---|
263 | && pBootSector->abJmp[2] <= 0x7f)
|
---|
264 | offJmp = pBootSector->abJmp[2] + 3;
|
---|
265 | else if ( pBootSector->abJmp[0] == 0xe9
|
---|
266 | && pBootSector->abJmp[2] <= 0x7f)
|
---|
267 | offJmp = RT_MIN(127, RT_MAKE_U16(pBootSector->abJmp[1], pBootSector->abJmp[2]));
|
---|
268 | uint8_t const cbMaxBpb = offJmp - RT_OFFSETOF(FATBOOTSECTOR, Bpb);
|
---|
269 | if (cbMaxBpb < sizeof(FATBPB20))
|
---|
270 | {
|
---|
271 | Log2(("rtVfsMountIsFat: DOS signature, but jmp too short for any BPB: %#x (max %#x BPB)\n", offJmp, cbMaxBpb));
|
---|
272 | return false;
|
---|
273 | }
|
---|
274 |
|
---|
275 | if ( pBootSector->Bpb.Bpb20.cFats == 0
|
---|
276 | || pBootSector->Bpb.Bpb20.cFats > 4)
|
---|
277 | {
|
---|
278 | if (pBootSector->Bpb.Bpb20.cFats == 0)
|
---|
279 | Log2(("rtVfsMountIsFat: DOS signature, number of FATs is zero, so not FAT file system\n"));
|
---|
280 | else
|
---|
281 | Log2(("rtVfsMountIsFat: DOS signature, too many FATs: %#x\n", pBootSector->Bpb.Bpb20.cFats));
|
---|
282 | return false;
|
---|
283 | }
|
---|
284 |
|
---|
285 | if (!FATBPB_MEDIA_IS_VALID(pBootSector->Bpb.Bpb20.bMedia))
|
---|
286 | {
|
---|
287 | Log2(("rtVfsMountIsFat: DOS signature, invalid media byte: %#x\n", pBootSector->Bpb.Bpb20.bMedia));
|
---|
288 | return false;
|
---|
289 | }
|
---|
290 |
|
---|
291 | uint16_t cbSector = RT_LE2H_U16(pBootSector->Bpb.Bpb20.cbSector);
|
---|
292 | if ( cbSector != 512
|
---|
293 | && cbSector != 4096
|
---|
294 | && cbSector != 1024
|
---|
295 | && cbSector != 128)
|
---|
296 | {
|
---|
297 | Log2(("rtVfsMountIsFat: DOS signature, unsupported sector size: %#x\n", cbSector));
|
---|
298 | return false;
|
---|
299 | }
|
---|
300 |
|
---|
301 | if ( !RT_IS_POWER_OF_TWO(pBootSector->Bpb.Bpb20.cSectorsPerCluster)
|
---|
302 | || !pBootSector->Bpb.Bpb20.cSectorsPerCluster)
|
---|
303 | {
|
---|
304 | Log2(("rtVfsMountIsFat: DOS signature, cluster size not non-zero power of two: %#x",
|
---|
305 | pBootSector->Bpb.Bpb20.cSectorsPerCluster));
|
---|
306 | return false;
|
---|
307 | }
|
---|
308 |
|
---|
309 | uint16_t const cReservedSectors = RT_LE2H_U16(pBootSector->Bpb.Bpb20.cReservedSectors);
|
---|
310 | if ( cReservedSectors == 0
|
---|
311 | || cReservedSectors >= _32K)
|
---|
312 | {
|
---|
313 | Log2(("rtVfsMountIsFat: DOS signature, bogus reserved sector count: %#x\n", cReservedSectors));
|
---|
314 | return false;
|
---|
315 | }
|
---|
316 |
|
---|
317 | /*
|
---|
318 | * Match the media byte with the first FAT byte and check that the next
|
---|
319 | * 4 bits are set. (To match further bytes in the FAT we'd need to
|
---|
320 | * determin the FAT type, which is too much hazzle to do here.)
|
---|
321 | */
|
---|
322 | uint8_t const *pbFat;
|
---|
323 | if ((size_t)cReservedSectors * cbSector < cbRaw)
|
---|
324 | pbFat = &pbRaw[cReservedSectors * cbSector];
|
---|
325 | else
|
---|
326 | {
|
---|
327 | int rc = RTVfsFileReadAt(hVfsFileIn, cReservedSectors * cbSector, pBuf2, 512, NULL);
|
---|
328 | if (RT_FAILURE(rc))
|
---|
329 | {
|
---|
330 | Log2(("rtVfsMountIsFat: error reading first FAT sector at %#x: %Rrc\n", cReservedSectors * cbSector, rc));
|
---|
331 | return false;
|
---|
332 | }
|
---|
333 | pbFat = pBuf2->ab;
|
---|
334 | }
|
---|
335 | if (*pbFat != pBootSector->Bpb.Bpb20.bMedia)
|
---|
336 | {
|
---|
337 | Log2(("rtVfsMountIsFat: Media byte and FAT ID mismatch: %#x vs %#x (%.8Rhxs)\n",
|
---|
338 | pbFat[0], pBootSector->Bpb.Bpb20.bMedia, pbFat));
|
---|
339 | return false;
|
---|
340 | }
|
---|
341 | if ((pbFat[1] & 0xf) != 0xf)
|
---|
342 | {
|
---|
343 | Log2(("rtVfsMountIsFat: Media byte and FAT ID mismatch: %#x vs %#x (%.8Rhxs)\n",
|
---|
344 | pbFat[0], pBootSector->Bpb.Bpb20.bMedia, pbFat));
|
---|
345 | return false;
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | return true;
|
---|
350 | }
|
---|
351 |
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Check if the given bootsector is an HPFS boot sector.
|
---|
355 | *
|
---|
356 | * @returns true if NTFS, false if not.
|
---|
357 | * @param pSuperBlock The ext2 superblock.
|
---|
358 | */
|
---|
359 | static bool rtVfsMountIsExt2(PCEXT2SUPERBLOCK pSuperBlock)
|
---|
360 | {
|
---|
361 | if (RT_LE2H_U16(pSuperBlock->u16Signature) != EXT2_SIGNATURE)
|
---|
362 | return false;
|
---|
363 |
|
---|
364 | uint32_t cShift = RT_LE2H_U32(pSuperBlock->cBitsShiftLeftBlockSize);
|
---|
365 | if (cShift > 54)
|
---|
366 | {
|
---|
367 | Log2(("rtVfsMountIsExt2: cBitsShiftLeftBlockSize=%#x: out of range\n", cShift));
|
---|
368 | return false;
|
---|
369 | }
|
---|
370 |
|
---|
371 | cShift = RT_LE2H_U32(pSuperBlock->cBitsShiftLeftFragmentSize);
|
---|
372 | if (cShift > 54)
|
---|
373 | {
|
---|
374 | Log2(("rtVfsMountIsExt2: cBitsShiftLeftFragmentSize=%#x: out of range\n", cShift));
|
---|
375 | return false;
|
---|
376 | }
|
---|
377 |
|
---|
378 | /* Some more checks here would be nice actually since a 16-bit word and a
|
---|
379 | couple of field limits doesn't feel all that conclusive. */
|
---|
380 |
|
---|
381 | return true;
|
---|
382 | }
|
---|
383 |
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Does the file system detection and mounting.
|
---|
387 | *
|
---|
388 | * Since we only support a handful of file systems at the moment and the
|
---|
389 | * interface isn't yet extensible in any way, we combine the file system
|
---|
390 | * recognition code for all. This reduces the number of reads we need to do and
|
---|
391 | * avoids unnecessary processing.
|
---|
392 | *
|
---|
393 | * @returns IPRT status code.
|
---|
394 | * @param hVfsFileIn The volume file.
|
---|
395 | * @param fFlags RTVFSMTN_F_XXX.
|
---|
396 | * @param pBuf Pointer to the primary buffer
|
---|
397 | * @param pBuf2 Pointer to the secondary buffer.
|
---|
398 | * @param phVfs Where to return the .
|
---|
399 | * @param pErrInfo Where to return additional error information.
|
---|
400 | * Optional.
|
---|
401 | */
|
---|
402 | static int rtVfsMountInner(RTVFSFILE hVfsFileIn, uint32_t fFlags, RTVFSMOUNTBUF *pBuf,
|
---|
403 | RTVFSMOUNTBUF *pBuf2, PRTVFS phVfs, PRTERRINFO pErrInfo)
|
---|
404 | {
|
---|
405 | AssertCompile(sizeof(*pBuf) >= ISO9660_SECTOR_SIZE);
|
---|
406 |
|
---|
407 | /* Start by checking for ISO-9660 and UDFS since these may have confusing
|
---|
408 | data at the start of the volume. */
|
---|
409 | int rc = RTVfsFileReadAt(hVfsFileIn, _32K, pBuf, ISO9660_SECTOR_SIZE, NULL);
|
---|
410 | if (RT_SUCCESS(rc))
|
---|
411 | {
|
---|
412 | if (rtVfsMountIsIsoFs(&pBuf->IsoHdr))
|
---|
413 | {
|
---|
414 | Log(("RTVfsMount: Detected ISO-9660 or UDF.\n"));
|
---|
415 | return RTFsIso9660VolOpen(hVfsFileIn, 0 /*fFlags*/, phVfs, pErrInfo);
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | /* Now read the boot sector and whatever the next 1536 bytes may contain.
|
---|
420 | With ext2 superblock at 1024, we can recognize quite a bit thru this read. */
|
---|
421 | rc = RTVfsFileReadAt(hVfsFileIn, 0, pBuf, sizeof(*pBuf), NULL);
|
---|
422 | if (RT_FAILURE(rc))
|
---|
423 | return RTErrInfoSet(pErrInfo, rc, "Error reading boot sector");
|
---|
424 |
|
---|
425 | if (rtVfsMountIsNtfs(&pBuf->Bootsector))
|
---|
426 | return RTFsNtfsVolOpen(hVfsFileIn, fFlags, 0 /*fNtfsFlags*/, phVfs, pErrInfo);
|
---|
427 |
|
---|
428 | if (rtVfsMountIsHpfs(&pBuf->Bootsector, hVfsFileIn, pBuf2))
|
---|
429 | return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "HPFS not yet supported");
|
---|
430 |
|
---|
431 | if (rtVfsMountIsFat(&pBuf->Bootsector, pBuf->ab, sizeof(*pBuf), hVfsFileIn, pBuf2))
|
---|
432 | {
|
---|
433 | Log(("RTVfsMount: Detected ISO-9660 or UDF.\n"));
|
---|
434 | return RTFsFatVolOpen(hVfsFileIn, RT_BOOL(fFlags & RTVFSMNT_F_READ_ONLY), 0 /*offBootSector*/, phVfs, pErrInfo);
|
---|
435 | }
|
---|
436 |
|
---|
437 | AssertCompile(sizeof(*pBuf) >= 1024 + sizeof(EXT2SUPERBLOCK));
|
---|
438 | if (rtVfsMountIsExt2((PCEXT2SUPERBLOCK)&pBuf->ab[1024]))
|
---|
439 | {
|
---|
440 | Log(("RTVfsMount: Detected ISO-9660 or UDF.\n"));
|
---|
441 | return RTFsExt2VolOpen(hVfsFileIn, fFlags, 0 /*fExt2Flags*/, phVfs, pErrInfo);
|
---|
442 | }
|
---|
443 |
|
---|
444 | return VERR_VFS_UNSUPPORTED_FORMAT;
|
---|
445 | }
|
---|
446 |
|
---|
447 |
|
---|
448 | RTDECL(int) RTVfsMountVol(RTVFSFILE hVfsFileIn, uint32_t fFlags, PRTVFS phVfs, PRTERRINFO pErrInfo)
|
---|
449 | {
|
---|
450 | AssertReturn(!(fFlags & ~RTVFSMNT_F_VALID_MASK), VERR_INVALID_FLAGS);
|
---|
451 | AssertPtrReturn(hVfsFileIn, VERR_INVALID_HANDLE);
|
---|
452 | AssertPtrReturn(phVfs, VERR_INVALID_HANDLE);
|
---|
453 |
|
---|
454 | *phVfs = NIL_RTVFS;
|
---|
455 |
|
---|
456 | RTVFSMOUNTBUF *pBufs = (RTVFSMOUNTBUF *)RTMemTmpAlloc(sizeof(*pBufs) * 2);
|
---|
457 | AssertReturn(pBufs, VERR_NO_TMP_MEMORY);
|
---|
458 |
|
---|
459 | int rc = rtVfsMountInner(hVfsFileIn, fFlags, pBufs, pBufs + 1, phVfs, pErrInfo);
|
---|
460 |
|
---|
461 | RTMemTmpFree(pBufs);
|
---|
462 |
|
---|
463 | return rc;
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnValidate}
|
---|
469 | */
|
---|
470 | static DECLCALLBACK(int) rtVfsChainMountVol_Validate(PCRTVFSCHAINELEMENTREG pProviderReg, PRTVFSCHAINSPEC pSpec,
|
---|
471 | PRTVFSCHAINELEMSPEC pElement, uint32_t *poffError, PRTERRINFO pErrInfo)
|
---|
472 | {
|
---|
473 | RT_NOREF(pProviderReg);
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * Basic checks.
|
---|
477 | */
|
---|
478 | if (pElement->enmTypeIn != RTVFSOBJTYPE_FILE)
|
---|
479 | return pElement->enmTypeIn == RTVFSOBJTYPE_INVALID ? VERR_VFS_CHAIN_CANNOT_BE_FIRST_ELEMENT : VERR_VFS_CHAIN_TAKES_FILE;
|
---|
480 | if ( pElement->enmType != RTVFSOBJTYPE_VFS
|
---|
481 | && pElement->enmType != RTVFSOBJTYPE_DIR)
|
---|
482 | return VERR_VFS_CHAIN_ONLY_DIR_OR_VFS;
|
---|
483 | if (pElement->cArgs > 1)
|
---|
484 | return VERR_VFS_CHAIN_AT_MOST_ONE_ARG;
|
---|
485 |
|
---|
486 | /*
|
---|
487 | * Parse the flag if present, save in pElement->uProvider.
|
---|
488 | */
|
---|
489 | bool fReadOnly = (pSpec->fOpenFile & RTFILE_O_ACCESS_MASK) == RTFILE_O_READ;
|
---|
490 | if (pElement->cArgs > 0)
|
---|
491 | {
|
---|
492 | const char *psz = pElement->paArgs[0].psz;
|
---|
493 | if (*psz)
|
---|
494 | {
|
---|
495 | if (!strcmp(psz, "ro"))
|
---|
496 | fReadOnly = true;
|
---|
497 | else if (!strcmp(psz, "rw"))
|
---|
498 | fReadOnly = false;
|
---|
499 | else
|
---|
500 | {
|
---|
501 | *poffError = pElement->paArgs[0].offSpec;
|
---|
502 | return RTErrInfoSet(pErrInfo, VERR_VFS_CHAIN_INVALID_ARGUMENT, "Expected 'ro' or 'rw' as argument");
|
---|
503 | }
|
---|
504 | }
|
---|
505 | }
|
---|
506 |
|
---|
507 | pElement->uProvider = fReadOnly ? RTVFSMNT_F_READ_ONLY : 0;
|
---|
508 | return VINF_SUCCESS;
|
---|
509 | }
|
---|
510 |
|
---|
511 |
|
---|
512 | /**
|
---|
513 | * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnInstantiate}
|
---|
514 | */
|
---|
515 | static DECLCALLBACK(int) rtVfsChainMountVol_Instantiate(PCRTVFSCHAINELEMENTREG pProviderReg, PCRTVFSCHAINSPEC pSpec,
|
---|
516 | PCRTVFSCHAINELEMSPEC pElement, RTVFSOBJ hPrevVfsObj,
|
---|
517 | PRTVFSOBJ phVfsObj, uint32_t *poffError, PRTERRINFO pErrInfo)
|
---|
518 | {
|
---|
519 | RT_NOREF(pProviderReg, pSpec, poffError);
|
---|
520 |
|
---|
521 | int rc;
|
---|
522 | RTVFSFILE hVfsFileIn = RTVfsObjToFile(hPrevVfsObj);
|
---|
523 | if (hVfsFileIn != NIL_RTVFSFILE)
|
---|
524 | {
|
---|
525 | RTVFS hVfs;
|
---|
526 | rc = RTVfsMountVol(hVfsFileIn, (uint32_t)pElement->uProvider, &hVfs, pErrInfo);
|
---|
527 | RTVfsFileRelease(hVfsFileIn);
|
---|
528 | if (RT_SUCCESS(rc))
|
---|
529 | {
|
---|
530 | *phVfsObj = RTVfsObjFromVfs(hVfs);
|
---|
531 | RTVfsRelease(hVfs);
|
---|
532 | if (*phVfsObj != NIL_RTVFSOBJ)
|
---|
533 | return VINF_SUCCESS;
|
---|
534 | rc = VERR_VFS_CHAIN_CAST_FAILED;
|
---|
535 | }
|
---|
536 | }
|
---|
537 | else
|
---|
538 | rc = VERR_VFS_CHAIN_CAST_FAILED;
|
---|
539 | return rc;
|
---|
540 | }
|
---|
541 |
|
---|
542 |
|
---|
543 | /**
|
---|
544 | * @interface_method_impl{RTVFSCHAINELEMENTREG,pfnCanReuseElement}
|
---|
545 | */
|
---|
546 | static DECLCALLBACK(bool) rtVfsChainMountVol_CanReuseElement(PCRTVFSCHAINELEMENTREG pProviderReg,
|
---|
547 | PCRTVFSCHAINSPEC pSpec, PCRTVFSCHAINELEMSPEC pElement,
|
---|
548 | PCRTVFSCHAINSPEC pReuseSpec, PCRTVFSCHAINELEMSPEC pReuseElement)
|
---|
549 | {
|
---|
550 | RT_NOREF(pProviderReg, pSpec, pReuseSpec);
|
---|
551 | if ( pElement->paArgs[0].uProvider == pReuseElement->paArgs[0].uProvider
|
---|
552 | || !pReuseElement->paArgs[0].uProvider)
|
---|
553 | return true;
|
---|
554 | return false;
|
---|
555 | }
|
---|
556 |
|
---|
557 |
|
---|
558 | /** VFS chain element 'file'. */
|
---|
559 | static RTVFSCHAINELEMENTREG g_rtVfsChainMountVolReg =
|
---|
560 | {
|
---|
561 | /* uVersion = */ RTVFSCHAINELEMENTREG_VERSION,
|
---|
562 | /* fReserved = */ 0,
|
---|
563 | /* pszName = */ "mount",
|
---|
564 | /* ListEntry = */ { NULL, NULL },
|
---|
565 | /* pszHelp = */ "Open a file system, requires a file object on the left side.\n"
|
---|
566 | "First argument is an optional 'ro' (read-only) or 'rw' (read-write) flag.\n",
|
---|
567 | /* pfnValidate = */ rtVfsChainMountVol_Validate,
|
---|
568 | /* pfnInstantiate = */ rtVfsChainMountVol_Instantiate,
|
---|
569 | /* pfnCanReuseElement = */ rtVfsChainMountVol_CanReuseElement,
|
---|
570 | /* uEndMarker = */ RTVFSCHAINELEMENTREG_VERSION
|
---|
571 | };
|
---|
572 |
|
---|
573 | RTVFSCHAIN_AUTO_REGISTER_ELEMENT_PROVIDER(&g_rtVfsChainMountVolReg, rtVfsChainMountVolReg);
|
---|
574 |
|
---|