16109abcc940363306e9d44c80e984bcc05a57e7
[reactos.git] / reactos / drivers / filesystems / fs_rec / ext2.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS File System Recognizer
4 * FILE: drivers/filesystems/fs_rec/ext2.c
5 * PURPOSE: EXT2 Recognizer
6 * PROGRAMMER: Eric Kohl
7 * Pierre Schweitzer (pierre@reactos.org)
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include "fs_rec.h"
13 #include "ext2.h"
14
15 #define NDEBUG
16 #include <debug.h>
17
18 /* FUNCTIONS ****************************************************************/
19
20 BOOLEAN
21 NTAPI
22 FsRecIsExt2Volume(IN PEXT2_SUPER_BLOCK SuperBlock)
23 {
24 /* Just check for magic */
25 return (SuperBlock->Magic == EXT2_SUPER_MAGIC);
26 }
27
28 NTSTATUS
29 NTAPI
30 FsRecExt2FsControl(IN PDEVICE_OBJECT DeviceObject,
31 IN PIRP Irp)
32 {
33 PIO_STACK_LOCATION Stack;
34 NTSTATUS Status;
35 PDEVICE_OBJECT MountDevice;
36 PEXT2_SUPER_BLOCK Spb = NULL;
37 ULONG SectorSize;
38 LARGE_INTEGER Offset;
39 BOOLEAN DeviceError = FALSE;
40 PAGED_CODE();
41
42 /* Get the I/O Stack and check the function type */
43 Stack = IoGetCurrentIrpStackLocation(Irp);
44 switch (Stack->MinorFunction)
45 {
46 case IRP_MN_MOUNT_VOLUME:
47
48 /* Assume failure */
49 Status = STATUS_UNRECOGNIZED_VOLUME;
50
51 /* Get the device object and request the sector size */
52 MountDevice = Stack->Parameters.MountVolume.DeviceObject;
53 if (FsRecGetDeviceSectorSize(MountDevice, &SectorSize))
54 {
55 /* Try to read the superblock */
56 Offset.QuadPart = 0x400;
57 if (FsRecReadBlock(MountDevice,
58 &Offset,
59 0x400,
60 SectorSize,
61 (PVOID)&Spb,
62 &DeviceError))
63 {
64 /* Check if it's an actual EXT2 volume */
65 if (FsRecIsExt2Volume(Spb))
66 {
67 /* It is! */
68 Status = STATUS_FS_DRIVER_REQUIRED;
69 }
70 }
71
72 /* Free the boot sector if we have one */
73 ExFreePool(Spb);
74 }
75 else
76 {
77 /* We have some sort of failure in the storage stack */
78 DeviceError = TRUE;
79 }
80
81 /* Check if we have an error on the stack */
82 if (DeviceError)
83 {
84 /* Was this because of a floppy? */
85 if (MountDevice->Characteristics & FILE_FLOPPY_DISKETTE)
86 {
87 /* Let the FS try anyway */
88 Status = STATUS_FS_DRIVER_REQUIRED;
89 }
90 }
91
92 break;
93
94 case IRP_MN_LOAD_FILE_SYSTEM:
95
96 /* Load the file system */
97 Status = FsRecLoadFileSystem(DeviceObject,
98 L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\Ext2fs");
99 break;
100
101 default:
102
103 /* Invalid request */
104 Status = STATUS_INVALID_DEVICE_REQUEST;
105 }
106
107 /* Return Status */
108 return Status;
109 }
110
111 /* EOF */