VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/vfs/vfsmount.cpp@ 69853

Last change on this file since 69853 was 69853, checked in by vboxsync, 7 years ago

nits

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.8 KB
Line 
1/* $Id: vfsmount.cpp 69853 2017-11-28 09:38:18Z 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.215389.xyz. 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/fsvfs.h>
38#include <iprt/mem.h>
39#include <iprt/log.h>
40#include <iprt/string.h>
41
42#include <iprt/formats/fat.h>
43#include <iprt/formats/iso9660.h>
44#include <iprt/formats/udf.h>
45#include <iprt/formats/ext2.h>
46
47
48/*********************************************************************************************************************************
49* Structures and Typedefs *
50*********************************************************************************************************************************/
51/** Buffer structure for the detection routines. */
52typedef union RTVFSMOUNTBUF
53{
54 uint8_t ab[2048];
55 uint32_t au32[2048/4];
56 FATBOOTSECTOR Bootsector;
57 ISO9660VOLDESCHDR IsoHdr;
58} RTVFSMOUNTBUF;
59AssertCompileSize(RTVFSMOUNTBUF, 2048);
60typedef RTVFSMOUNTBUF *PRTVFSMOUNTBUF;
61
62
63
64/**
65 * Checks if the given 2K sector at offset 32KB looks like ISO-9660 or UDF.
66 *
67 * @returns true if likely ISO or UDF, otherwise false.
68 * @param pVolDescHdr Whatever is at offset 32KB. 2KB buffer.
69 */
70static bool rtVfsMountIsIsoFs(PCISO9660VOLDESCHDR pVolDescHdr)
71{
72 if ( memcmp(pVolDescHdr->achStdId, RT_STR_TUPLE(ISO9660VOLDESC_STD_ID)) == 0
73 && pVolDescHdr->bDescType <= ISO9660VOLDESC_TYPE_PARTITION
74 && pVolDescHdr->bDescVersion != 0
75 && pVolDescHdr->bDescVersion <= 3 /* don't be too picky, just increase the likelyhood */ )
76 return true;
77
78 if ( memcmp(pVolDescHdr->achStdId, RT_STR_TUPLE(UDF_EXT_VOL_DESC_STD_ID_BEGIN)) == 0
79 && pVolDescHdr->bDescType == UDF_EXT_VOL_DESC_TYPE
80 && pVolDescHdr->bDescVersion == UDF_EXT_VOL_DESC_VERSION)
81 return true;
82
83 return false;
84}
85
86
87/**
88 * Check if the given bootsector is a NTFS boot sector.
89 *
90 * @returns true if NTFS, false if not.
91 * @param pBootSector The boot sector to inspect.
92 */
93static bool rtVfsMountIsNtfs(PCFATBOOTSECTOR pBootSector)
94{
95 if (memcmp(pBootSector->achOemName, RT_STR_TUPLE("NTFS ")) != 0)
96 return false;
97
98 uint16_t cbSector = RT_LE2H_U16(pBootSector->Bpb.Bpb331.cbSector);
99 if ( cbSector < 0x100
100 || cbSector >= 0x1000
101 || (cbSector & 0xff) != 0)
102 {
103 Log2(("rtVfsMountIsNtfs: cbSector=%#x: out of range\n", cbSector));
104 return false;
105 }
106
107 if ( !RT_IS_POWER_OF_TWO(pBootSector->Bpb.Bpb331.cSectorsPerCluster)
108 || pBootSector->Bpb.Bpb331.cSectorsPerCluster == 0
109 || pBootSector->Bpb.Bpb331.cSectorsPerCluster > 128)
110 {
111 Log2(("rtVfsMountIsNtfs: cSectorsPerCluster=%#x: out of range\n", pBootSector->Bpb.Bpb331.cSectorsPerCluster));
112 return false;
113 }
114
115 if ((uint32_t)pBootSector->Bpb.Bpb331.cSectorsPerCluster * cbSector > _64K)
116 {
117 Log2(("rtVfsMountIsNtfs: cSectorsPerCluster=%#x * cbSector=%#x => %#x: out of range\n",
118 pBootSector->Bpb.Bpb331.cSectorsPerCluster, cbSector,
119 (uint32_t)pBootSector->Bpb.Bpb331.cSectorsPerCluster * cbSector));
120 return false;
121 }
122
123 if ( pBootSector->Bpb.Bpb331.cReservedSectors != 0
124 || pBootSector->Bpb.Bpb331.cMaxRootDirEntries != 0
125 || pBootSector->Bpb.Bpb331.cTotalSectors16 != 0
126 || pBootSector->Bpb.Bpb331.cTotalSectors32 != 0
127 || pBootSector->Bpb.Bpb331.cSectorsPerFat != 0
128 || pBootSector->Bpb.Bpb331.cFats != 0)
129 {
130 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",
131 RT_LE2H_U16(pBootSector->Bpb.Bpb331.cReservedSectors),
132 RT_LE2H_U16(pBootSector->Bpb.Bpb331.cMaxRootDirEntries),
133 RT_LE2H_U16(pBootSector->Bpb.Bpb331.cTotalSectors16),
134 RT_LE2H_U32(pBootSector->Bpb.Bpb331.cTotalSectors32),
135 RT_LE2H_U16(pBootSector->Bpb.Bpb331.cSectorsPerFat),
136 pBootSector->Bpb.Bpb331.cFats));
137 return false;
138 }
139
140 /** @todo NTFS specific checks: MFT cluster number, cluster per index block. */
141
142 return true;
143}
144
145
146/**
147 * Check if the given bootsector is a HPFS boot sector.
148 *
149 * @returns true if NTFS, false if not.
150 * @param pBootSector The boot sector to inspect.
151 * @param hVfsFileIn The volume file.
152 * @param pBuf2 A 2nd buffer.
153 */
154static bool rtVfsMountIsHpfs(PCFATBOOTSECTOR pBootSector, RTVFSFILE hVfsFileIn, PRTVFSMOUNTBUF pBuf2)
155{
156 if (memcmp(pBootSector->Bpb.Ebpb.achType, RT_STR_TUPLE("HPFS ")) != 0)
157 return false;
158
159 /* Superblock is at sector 16, spare superblock at 17. */
160 int rc = RTVfsFileReadAt(hVfsFileIn, 16 * 512, pBuf2, 512 * 2, NULL);
161 if (RT_FAILURE(rc))
162 {
163 Log2(("rtVfsMountIsHpfs: Error reading superblock: %Rrc\n", rc));
164 return false;
165 }
166
167 if ( RT_LE2H_U32(pBuf2->au32[0]) != UINT32_C(0xf995e849)
168 || RT_LE2H_U32(pBuf2->au32[1]) != UINT32_C(0xfa53e9c5)
169 || RT_LE2H_U32(pBuf2->au32[512/4 + 0]) != UINT32_C(0xf9911849)
170 || RT_LE2H_U32(pBuf2->au32[512/4 + 1]) != UINT32_C(0xfa5229c5))
171 {
172 Log2(("rtVfsMountIsHpfs: Superblock or spare superblock signature mismatch: %#x %#x %#x %#x\n",
173 RT_LE2H_U32(pBuf2->au32[0]), RT_LE2H_U32(pBuf2->au32[1]),
174 RT_LE2H_U32(pBuf2->au32[512/4 + 0]), RT_LE2H_U32(pBuf2->au32[512/4 + 1]) ));
175 return false;
176 }
177
178 return true;
179}
180
181
182/**
183 * Check if the given bootsector is a FAT boot sector.
184 *
185 * @returns true if NTFS, false if not.
186 * @param pBootSector The boot sector to inspect.
187 * @param pbRaw Pointer to the raw boot sector buffer.
188 * @param cbRaw Number of bytes read starting with the boot
189 * sector (which @a pbRaw points to).
190 * @param hVfsFileIn The volume file.
191 * @param pBuf2 A 2nd buffer.
192 */
193static bool rtVfsMountIsFat(PCFATBOOTSECTOR pBootSector, uint8_t const *pbRaw, size_t cbRaw,
194 RTVFSFILE hVfsFileIn, PRTVFSMOUNTBUF pBuf2)
195{
196 Assert(cbRaw >= 1024);
197
198 /*
199 * Check the DOS signature first. The PC-DOS 1.0 boot floppy does not have
200 * a signature and we ASSUME this is the case for all floppies formated by it.
201 */
202 if (pBootSector->uSignature != FATBOOTSECTOR_SIGNATURE)
203 {
204 if (pBootSector->uSignature != 0)
205 return false;
206
207 /*
208 * PC-DOS 1.0 does a 2fh byte short jump w/o any NOP following it.
209 * Instead the following are three words and a 9 byte build date
210 * string. The remaining space is zero filled.
211 *
212 * Note! No idea how this would look like for 8" floppies, only got 5"1/4'.
213 *
214 * ASSUME all non-BPB disks are using this format.
215 */
216 if ( pBootSector->abJmp[0] != 0xeb /* jmp rel8 */
217 || pBootSector->abJmp[1] < 0x2f
218 || pBootSector->abJmp[1] >= 0x80
219 || pBootSector->abJmp[2] == 0x90 /* nop */)
220 {
221 Log2(("rtVfsMountIsFat: No DOS v1.0 bootsector either - invalid jmp: %.3Rhxs\n", pBootSector->abJmp));
222 return false;
223 }
224
225 /* Check the FAT ID so we can tell if this is double or single sided, as well as being a valid FAT12 start. */
226 if ( (pbRaw[512] != 0xfe && pbRaw[0] != 0xff)
227 || pbRaw[512 + 1] != 0xff
228 || pbRaw[512 + 2] != 0xff)
229 {
230 Log2(("rtVfsMountIsFat: No DOS v1.0 bootsector either - unexpected start of FAT: %.3Rhxs\n", &pbRaw[512]));
231 return false;
232 }
233
234 uint32_t const offJump = 2 + pBootSector->abJmp[1];
235 uint32_t const offFirstZero = 2 /*jmp */ + 3 * 2 /* words */ + 9 /* date string */;
236 Assert(offFirstZero >= RT_UOFFSETOF(FATBOOTSECTOR, Bpb));
237 uint32_t const cbZeroPad = RT_MIN(offJump - offFirstZero,
238 sizeof(pBootSector->Bpb.Bpb20) - (offFirstZero - RT_OFFSETOF(FATBOOTSECTOR, Bpb)));
239
240 if (!ASMMemIsAllU8((uint8_t const *)pBootSector + offFirstZero, cbZeroPad, 0))
241 {
242 Log2(("rtVfsMountIsFat: No DOS v1.0 bootsector either - expected zero padding %#x LB %#x: %.*Rhxs\n",
243 offFirstZero, cbZeroPad, cbZeroPad, (uint8_t const *)pBootSector + offFirstZero));
244 return false;
245 }
246 }
247 else
248 {
249 /*
250 * DOS 2.0 or later.
251 *
252 * Start by checking if we've got a known jump instruction first, because
253 * that will give us a max (E)BPB size hint.
254 */
255 uint8_t offJmp = UINT8_MAX;
256 if ( pBootSector->abJmp[0] == 0xeb
257 && pBootSector->abJmp[1] <= 0x7f)
258 offJmp = pBootSector->abJmp[1] + 2;
259 else if ( pBootSector->abJmp[0] == 0x90
260 && pBootSector->abJmp[1] == 0xeb
261 && pBootSector->abJmp[2] <= 0x7f)
262 offJmp = pBootSector->abJmp[2] + 3;
263 else if ( pBootSector->abJmp[0] == 0xe9
264 && pBootSector->abJmp[2] <= 0x7f)
265 offJmp = RT_MIN(127, RT_MAKE_U16(pBootSector->abJmp[1], pBootSector->abJmp[2]));
266 uint8_t const cbMaxBpb = offJmp - RT_OFFSETOF(FATBOOTSECTOR, Bpb);
267 if (cbMaxBpb < sizeof(FATBPB20))
268 {
269 Log2(("rtVfsMountIsFat: DOS signature, but jmp too short for any BPB: %#x (max %#x BPB)\n", offJmp, cbMaxBpb));
270 return false;
271 }
272
273 if ( pBootSector->Bpb.Bpb20.cFats == 0
274 || pBootSector->Bpb.Bpb20.cFats > 4)
275 {
276 if (pBootSector->Bpb.Bpb20.cFats == 0)
277 Log2(("rtVfsMountIsFat: DOS signature, number of FATs is zero, so not FAT file system\n"));
278 else
279 Log2(("rtVfsMountIsFat: DOS signature, too many FATs: %#x\n", pBootSector->Bpb.Bpb20.cFats));
280 return false;
281 }
282
283 if (!FATBPB_MEDIA_IS_VALID(pBootSector->Bpb.Bpb20.bMedia))
284 {
285 Log2(("rtVfsMountIsFat: DOS signature, invalid media byte: %#x\n", pBootSector->Bpb.Bpb20.bMedia));
286 return false;
287 }
288
289 uint16_t cbSector = RT_LE2H_U16(pBootSector->Bpb.Bpb20.cbSector);
290 if ( cbSector != 512
291 && cbSector != 4096
292 && cbSector != 1024
293 && cbSector != 128)
294 {
295 Log2(("rtVfsMountIsFat: DOS signature, unsupported sector size: %#x\n", cbSector));
296 return false;
297 }
298
299 if ( !RT_IS_POWER_OF_TWO(pBootSector->Bpb.Bpb20.cSectorsPerCluster)
300 || !pBootSector->Bpb.Bpb20.cSectorsPerCluster)
301 {
302 Log2(("rtVfsMountIsFat: DOS signature, cluster size not non-zero power of two: %#x",
303 pBootSector->Bpb.Bpb20.cSectorsPerCluster));
304 return false;
305 }
306
307 uint16_t const cReservedSectors = RT_LE2H_U16(pBootSector->Bpb.Bpb20.cReservedSectors);
308 if ( cReservedSectors == 0
309 || cReservedSectors >= _32K)
310 {
311 Log2(("rtVfsMountIsFat: DOS signature, bogus reserved sector count: %#x\n", cReservedSectors));
312 return false;
313 }
314
315 /*
316 * Match the media byte with the first FAT byte and check that the next
317 * 4 bits are set. (To match further bytes in the FAT we'd need to
318 * determin the FAT type, which is too much hazzle to do here.)
319 */
320 uint8_t const *pbFat;
321 if ((size_t)cReservedSectors * cbSector < cbRaw)
322 pbFat = &pbRaw[cReservedSectors * cbSector];
323 else
324 {
325 int rc = RTVfsFileReadAt(hVfsFileIn, cReservedSectors * cbSector, pBuf2, 512, NULL);
326 if (RT_FAILURE(rc))
327 {
328 Log2(("rtVfsMountIsFat: error reading first FAT sector at %#x: %Rrc\n", cReservedSectors * cbSector, rc));
329 return false;
330 }
331 pbFat = pBuf2->ab;
332 }
333 if (*pbFat != pBootSector->Bpb.Bpb20.bMedia)
334 {
335 Log2(("rtVfsMountIsFat: Media byte and FAT ID mismatch: %#x vs %#x (%.8Rhxs)\n",
336 pbFat[0], pBootSector->Bpb.Bpb20.bMedia, pbFat));
337 return false;
338 }
339 if ((pbFat[1] & 0xf) != 0xf)
340 {
341 Log2(("rtVfsMountIsFat: Media byte and FAT ID mismatch: %#x vs %#x (%.8Rhxs)\n",
342 pbFat[0], pBootSector->Bpb.Bpb20.bMedia, pbFat));
343 return false;
344 }
345 }
346
347 return true;
348}
349
350
351/**
352 * Check if the given bootsector is an HPFS boot sector.
353 *
354 * @returns true if NTFS, false if not.
355 * @param pSuperBlock The ext2 superblock.
356 */
357static bool rtVfsMountIsExt2(PCEXT2SUPERBLOCK pSuperBlock)
358{
359 if (RT_LE2H_U16(pSuperBlock->u16Signature) != EXT2_SIGNATURE)
360 return false;
361
362 uint32_t cShift = RT_LE2H_U32(pSuperBlock->cBitsShiftLeftBlockSize);
363 if (cShift > 54)
364 {
365 Log2(("rtVfsMountIsExt2: cBitsShiftLeftBlockSize=%#x: out of range\n", cShift));
366 return false;
367 }
368
369 cShift = RT_LE2H_U32(pSuperBlock->cBitsShiftLeftFragmentSize);
370 if (cShift > 54)
371 {
372 Log2(("rtVfsMountIsExt2: cBitsShiftLeftFragmentSize=%#x: out of range\n", cShift));
373 return false;
374 }
375
376 /* Some more checks here would be nice actually since a 16-bit word and a
377 couple of field limits doesn't feel all that conclusive. */
378
379 return true;
380}
381
382
383/**
384 * Does the file system detection and mounting.
385 *
386 * Since we only support a handful of file systems at the moment and the
387 * interface isn't yet extensible in any way, we combine the file system
388 * recognition code for all. This reduces the number of reads we need to do and
389 * avoids unnecessary processing.
390 *
391 * @returns IPRT status code.
392 * @param hVfsFileIn The volume file.
393 * @param fFlags RTVFSMTN_F_XXX.
394 * @param pBuf Pointer to the primary buffer
395 * @param pBuf2 Pointer to the secondary buffer.
396 * @param phVfs Where to return the .
397 * @param pErrInfo Where to return additional error information.
398 * Optional.
399 */
400static int rtVfsMountInner(RTVFSFILE hVfsFileIn, uint32_t fFlags, RTVFSMOUNTBUF *pBuf,
401 RTVFSMOUNTBUF *pBuf2, PRTVFS phVfs, PRTERRINFO pErrInfo)
402{
403 AssertCompile(sizeof(*pBuf) >= ISO9660_SECTOR_SIZE);
404
405 /* Start by checking for ISO-9660 and UDFS since these may have confusing
406 data at the start of the volume. */
407 int rc = RTVfsFileReadAt(hVfsFileIn, _32K, pBuf, ISO9660_SECTOR_SIZE, NULL);
408 if (RT_SUCCESS(rc))
409 {
410 if (rtVfsMountIsIsoFs(&pBuf->IsoHdr))
411 {
412 Log(("RTVfsMount: Detected ISO-9660 or UDF.\n"));
413 return RTFsIso9660VolOpen(hVfsFileIn, 0 /*fFlags*/, phVfs, pErrInfo);
414 }
415 }
416
417 /* Now read the boot sector and whatever the next 1536 bytes may contain.
418 With ext2 superblock at 1024, we can recognize quite a bit thru this read. */
419 rc = RTVfsFileReadAt(hVfsFileIn, 0, pBuf, sizeof(*pBuf), NULL);
420 if (RT_FAILURE(rc))
421 return RTErrInfoSet(pErrInfo, rc, "Error reading boot sector");
422
423 if (rtVfsMountIsNtfs(&pBuf->Bootsector))
424 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "NTFS not yet supported");
425
426 if (rtVfsMountIsHpfs(&pBuf->Bootsector, hVfsFileIn, pBuf2))
427 return RTERRINFO_LOG_SET(pErrInfo, VERR_VFS_UNSUPPORTED_FORMAT, "HPFS not yet supported");
428
429 if (rtVfsMountIsFat(&pBuf->Bootsector, pBuf->ab, sizeof(*pBuf), hVfsFileIn, pBuf2))
430 {
431 Log(("RTVfsMount: Detected ISO-9660 or UDF.\n"));
432 return RTFsFatVolOpen(hVfsFileIn, RT_BOOL(fFlags & RTVFSMNT_F_READ_ONLY), 0 /*offBootSector*/, phVfs, pErrInfo);
433 }
434
435 AssertCompile(sizeof(*pBuf) >= 1024 + sizeof(EXT2SUPERBLOCK));
436 if (rtVfsMountIsExt2((PCEXT2SUPERBLOCK)&pBuf->ab[1024]))
437 {
438 Log(("RTVfsMount: Detected ISO-9660 or UDF.\n"));
439 return RTFsExt2VolOpen(hVfsFileIn, fFlags, 0 /*fExt2Flags*/, phVfs, pErrInfo);
440 }
441
442 return VERR_VFS_UNSUPPORTED_FORMAT;
443}
444
445
446RTDECL(int) RTVfsMountVol(RTVFSFILE hVfsFileIn, uint32_t fFlags, PRTVFS phVfs, PRTERRINFO pErrInfo)
447{
448 AssertReturn(!(fFlags & ~RTVFSMNT_F_VALID_MASK), VERR_INVALID_FLAGS);
449 AssertPtrReturn(hVfsFileIn, VERR_INVALID_HANDLE);
450 AssertPtrReturn(phVfs, VERR_INVALID_HANDLE);
451
452 *phVfs = NIL_RTVFS;
453
454 RTVFSMOUNTBUF *pBufs = (RTVFSMOUNTBUF *)RTMemTmpAlloc(sizeof(*pBufs) * 2);
455 AssertReturn(pBufs, VERR_NO_TMP_MEMORY);
456
457 int rc = rtVfsMountInner(hVfsFileIn, fFlags, pBufs, pBufs + 1, phVfs, pErrInfo);
458
459 RTMemTmpFree(pBufs);
460
461 return rc;
462}
463
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette