[NTFS] Fix 2 Clang-Cl warnings about CurrentKey->IndexEntry->Flags
[reactos.git] / drivers / filesystems / fs_rec / ffs.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS File System Recognizer
4 * FILE: drivers/filesystems/fs_rec/ffs.c
5 * PURPOSE: FFS Recognizer
6 * PROGRAMMER: Peter Hater
7 * Pierre Schweitzer (pierre@reactos.org)
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include "fs_rec.h"
13 #include "ffs.h"
14
15 #define NDEBUG
16 #include <debug.h>
17
18 /* FUNCTIONS ****************************************************************/
19
20 BOOLEAN
21 NTAPI
22 FsRecIsFfsDiskLabel(IN PFFSD_DISKLABEL dl)
23 {
24 return (dl->d_magic == DISKMAGIC);
25 }
26
27 BOOLEAN
28 NTAPI
29 FsRecIsFfs1Volume(IN PFFSD_SUPER_BLOCK sb)
30 {
31 return (sb->fs_magic == FS_UFS1_MAGIC);
32 }
33
34 BOOLEAN
35 NTAPI
36 FsRecIsFfs2Volume(IN PFFSD_SUPER_BLOCK sb)
37 {
38 return (sb->fs_magic == FS_UFS2_MAGIC);
39 }
40
41 NTSTATUS
42 NTAPI
43 FsRecFfsFsControl(IN PDEVICE_OBJECT DeviceObject,
44 IN PIRP Irp)
45 {
46 PIO_STACK_LOCATION Stack;
47 NTSTATUS Status;
48 PDEVICE_OBJECT MountDevice;
49 PFFSD_SUPER_BLOCK Spb = NULL;
50 PFFSD_DISKLABEL DiskLabel = NULL;
51 ULONGLONG FSOffset = 0;
52 int i;
53 ULONG SectorSize;
54 LARGE_INTEGER Offset;
55 BOOLEAN DeviceError = FALSE;
56 PAGED_CODE();
57
58 /* Get the I/O Stack and check the function type */
59 Stack = IoGetCurrentIrpStackLocation(Irp);
60 switch (Stack->MinorFunction)
61 {
62 case IRP_MN_MOUNT_VOLUME:
63
64 /* Assume failure */
65 Status = STATUS_UNRECOGNIZED_VOLUME;
66
67 /* Get the device object and request the sector size */
68 MountDevice = Stack->Parameters.MountVolume.DeviceObject;
69 if (FsRecGetDeviceSectorSize(MountDevice, &SectorSize))
70 {
71 /* Try to read the disklabel */
72 Offset.QuadPart = LABELSECTOR*SectorSize;
73 if (FsRecReadBlock(MountDevice,
74 &Offset,
75 SectorSize,
76 SectorSize,
77 (PVOID)&DiskLabel,
78 &DeviceError))
79 {
80 /* Check if it's an actual FFS disk label */
81 if (FsRecIsFfsDiskLabel(DiskLabel))
82 {
83 /* It is! */
84 for (i = 0; i < MAXPARTITIONS; i++)
85 {
86 if (DiskLabel->d_partitions[i].p_fstype == FS_BSDFFS)
87 {
88 /* Important */
89 FSOffset = DiskLabel->d_partitions[i].p_offset;
90 FSOffset = FSOffset * SectorSize;
91 /* Try to read the superblock */
92 Offset.QuadPart = FSOffset+SBLOCK_UFS1;
93 if (FsRecReadBlock(MountDevice,
94 &Offset,
95 SBLOCKSIZE,
96 SectorSize,
97 (PVOID)&Spb,
98 &DeviceError))
99 {
100 /* Check if it's an actual FFS volume */
101 if (FsRecIsFfs1Volume(Spb))
102 {
103 /* It is! */
104 Status = STATUS_FS_DRIVER_REQUIRED;
105 }
106 else
107 {
108 /* Free the boot sector if we have one */
109 ExFreePool(Spb);
110 Spb = NULL;
111
112 Offset.QuadPart = FSOffset+SBLOCK_UFS2;
113 if (FsRecReadBlock(MountDevice,
114 &Offset,
115 SBLOCKSIZE,
116 SectorSize,
117 (PVOID)&Spb,
118 &DeviceError))
119 {
120 /* Check if it's an actual FFS volume */
121 if (FsRecIsFfs2Volume(Spb))
122 {
123 /* It is! */
124 Status = STATUS_FS_DRIVER_REQUIRED;
125 }
126 }
127 }
128 }
129
130 /* Free the boot sector if we have one */
131 ExFreePool(Spb);
132 Spb = NULL;
133 }
134 }
135 }
136 else
137 {
138 /* Try to read the superblock */
139 Offset.QuadPart = FSOffset+SBLOCK_UFS1;
140 if (FsRecReadBlock(MountDevice,
141 &Offset,
142 SBLOCKSIZE,
143 SectorSize,
144 (PVOID)&Spb,
145 &DeviceError))
146 {
147 /* Check if it's an actual FFS volume */
148 if (FsRecIsFfs1Volume(Spb))
149 {
150 /* It is! */
151 Status = STATUS_FS_DRIVER_REQUIRED;
152 }
153 else
154 {
155 /* Free the boot sector if we have one */
156 ExFreePool(Spb);
157 Spb = NULL;
158
159 Offset.QuadPart = FSOffset+SBLOCK_UFS2;
160 if (FsRecReadBlock(MountDevice,
161 &Offset,
162 SBLOCKSIZE,
163 SectorSize,
164 (PVOID)&Spb,
165 &DeviceError))
166 {
167 /* Check if it's an actual FFS volume */
168 if (FsRecIsFfs2Volume(Spb))
169 {
170 /* It is! */
171 Status = STATUS_FS_DRIVER_REQUIRED;
172 }
173 }
174 }
175 }
176
177 /* Free the boot sector if we have one */
178 ExFreePool(Spb);
179 Spb = NULL;
180 }
181 }
182
183 /* Free the boot sector if we have one */
184 ExFreePool(DiskLabel);
185 }
186 else
187 {
188 /* We have some sort of failure in the storage stack */
189 DeviceError = TRUE;
190 }
191
192 /* Check if we have an error on the stack */
193 if (DeviceError)
194 {
195 /* Was this because of a floppy? */
196 if (MountDevice->Characteristics & FILE_FLOPPY_DISKETTE)
197 {
198 /* Let the FS try anyway */
199 Status = STATUS_FS_DRIVER_REQUIRED;
200 }
201 }
202
203 break;
204
205 case IRP_MN_LOAD_FILE_SYSTEM:
206
207 /* Load the file system */
208 Status = FsRecLoadFileSystem(DeviceObject,
209 L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\ffs");
210 break;
211
212 default:
213
214 /* Invalid request */
215 Status = STATUS_INVALID_DEVICE_REQUEST;
216 }
217
218 /* Return Status */
219 return Status;
220 }