Remove some debug output for USB drivers
[reactos.git] / reactos / drivers / fs / fs_rec / fat.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002,2003 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /*
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS kernel
22 * FILE: drivers/fs/fs_rec/vfat.c
23 * PURPOSE: Filesystem recognizer driver
24 * PROGRAMMER: Eric Kohl
25 */
26
27 /* INCLUDES *****************************************************************/
28
29 #define NDEBUG
30 #include <debug.h>
31
32 #include "fs_rec.h"
33
34
35 /* FUNCTIONS ****************************************************************/
36
37 static NTSTATUS
38 FsRecIsFatVolume(IN PDEVICE_OBJECT DeviceObject)
39 {
40 NTSTATUS Status;
41 PARTITION_INFORMATION PartitionInfo;
42 DISK_GEOMETRY DiskGeometry;
43 ULONG Size;
44 struct _BootSector* Boot;
45 BOOLEAN RecognizedFS = FALSE;
46 Size = sizeof(DISK_GEOMETRY);
47
48 Status = FsRecDeviceIoControl(DeviceObject,
49 IOCTL_DISK_GET_DRIVE_GEOMETRY,
50 NULL,
51 0,
52 &DiskGeometry,
53 &Size);
54 if (!NT_SUCCESS(Status))
55 {
56 DPRINT("FsRecDeviceIoControl faild (%x)\n", Status);
57 return Status;
58 }
59 if (DiskGeometry.MediaType == FixedMedia || DiskGeometry.MediaType == RemovableMedia)
60 {
61 // We have found a hard disk
62 Size = sizeof(PARTITION_INFORMATION);
63 Status = FsRecDeviceIoControl(DeviceObject,
64 IOCTL_DISK_GET_PARTITION_INFO,
65 NULL,
66 0,
67 &PartitionInfo,
68 &Size);
69 if (!NT_SUCCESS(Status))
70 {
71 DPRINT("FsRecDeviceIoControl faild (%x)\n", Status);
72 return Status;
73 }
74
75 if (PartitionInfo.PartitionType)
76 {
77 if (PartitionInfo.PartitionType == PARTITION_FAT_12 ||
78 PartitionInfo.PartitionType == PARTITION_FAT_16 ||
79 PartitionInfo.PartitionType == PARTITION_HUGE ||
80 PartitionInfo.PartitionType == PARTITION_FAT32 ||
81 PartitionInfo.PartitionType == PARTITION_FAT32_XINT13 ||
82 PartitionInfo.PartitionType == PARTITION_XINT13)
83 {
84 RecognizedFS = TRUE;
85 }
86 }
87 else if (DiskGeometry.MediaType == RemovableMedia &&
88 PartitionInfo.PartitionNumber > 0 &&
89 PartitionInfo.StartingOffset.QuadPart == 0 &&
90 PartitionInfo.PartitionLength.QuadPart > 0)
91 {
92 /* This is possible a removable media formated as super floppy */
93 RecognizedFS = TRUE;
94 }
95 }
96 if (DiskGeometry.MediaType > Unknown && DiskGeometry.MediaType < RemovableMedia )
97 {
98 RecognizedFS = TRUE;
99 }
100 if (RecognizedFS == FALSE)
101 {
102 return STATUS_UNRECOGNIZED_VOLUME;
103 }
104
105 Boot = ExAllocatePool(NonPagedPool, DiskGeometry.BytesPerSector);
106 if (Boot == NULL)
107 {
108 return STATUS_INSUFFICIENT_RESOURCES;
109 }
110
111 Status = FsRecReadSectors(DeviceObject,
112 0,
113 1,
114 DiskGeometry.BytesPerSector,
115 (PUCHAR) Boot);
116 if (!NT_SUCCESS(Status))
117 {
118 return Status;
119 }
120
121 if (Boot->Signatur1 != 0xaa55)
122 {
123 RecognizedFS=FALSE;
124 }
125 if (RecognizedFS &&
126 Boot->BytesPerSector != 512 &&
127 Boot->BytesPerSector != 1024 &&
128 Boot->BytesPerSector != 2048 &&
129 Boot->BytesPerSector == 4096)
130 {
131 RecognizedFS=FALSE;
132 }
133
134 if (RecognizedFS &&
135 Boot->FATCount != 1 &&
136 Boot->FATCount != 2)
137 {
138 RecognizedFS=FALSE;
139 }
140
141 if (RecognizedFS &&
142 Boot->Media != 0xf0 &&
143 Boot->Media != 0xf8 &&
144 Boot->Media != 0xf9 &&
145 Boot->Media != 0xfa &&
146 Boot->Media != 0xfb &&
147 Boot->Media != 0xfc &&
148 Boot->Media != 0xfd &&
149 Boot->Media != 0xfe &&
150 Boot->Media != 0xff)
151 {
152 RecognizedFS=FALSE;
153 }
154
155 if (RecognizedFS &&
156 Boot->SectorsPerCluster != 1 &&
157 Boot->SectorsPerCluster != 2 &&
158 Boot->SectorsPerCluster != 4 &&
159 Boot->SectorsPerCluster != 8 &&
160 Boot->SectorsPerCluster != 16 &&
161 Boot->SectorsPerCluster != 32 &&
162 Boot->SectorsPerCluster != 64 &&
163 Boot->SectorsPerCluster != 128)
164 {
165 RecognizedFS=FALSE;
166 }
167
168 if (RecognizedFS &&
169 Boot->BytesPerSector * Boot->SectorsPerCluster > 32 * 1024)
170 {
171 RecognizedFS=FALSE;
172 }
173
174
175 ExFreePool(Boot);
176 return RecognizedFS ? STATUS_SUCCESS : STATUS_UNRECOGNIZED_VOLUME;
177 }
178
179
180 NTSTATUS
181 FsRecVfatFsControl(IN PDEVICE_OBJECT DeviceObject,
182 IN PIRP Irp)
183 {
184 PIO_STACK_LOCATION Stack;
185 static UNICODE_STRING RegistryPath =
186 RTL_CONSTANT_STRING(L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\Vfatfs");
187 NTSTATUS Status;
188
189 Stack = IoGetCurrentIrpStackLocation(Irp);
190
191 switch (Stack->MinorFunction)
192 {
193 case IRP_MN_MOUNT_VOLUME:
194 DPRINT("FAT: IRP_MN_MOUNT_VOLUME\n");
195 Status = FsRecIsFatVolume(Stack->Parameters.MountVolume.DeviceObject);
196 if (NT_SUCCESS(Status))
197 {
198 DPRINT("Identified FAT volume\n");
199 Status = STATUS_FS_DRIVER_REQUIRED;
200 }
201 break;
202
203 case IRP_MN_LOAD_FILE_SYSTEM:
204 DPRINT("FAT: IRP_MN_LOAD_FILE_SYSTEM\n");
205 Status = ZwLoadDriver(&RegistryPath);
206 if (!NT_SUCCESS(Status))
207 {
208 DPRINT("ZwLoadDriver failed (Status %x)\n", Status);
209 }
210 else
211 {
212 IoUnregisterFileSystem(DeviceObject);
213 }
214 break;
215
216 default:
217 DPRINT("FAT: Unknown minor function %lx\n", Stack->MinorFunction);
218 Status = STATUS_INVALID_DEVICE_REQUEST;
219 break;
220 }
221 return(Status);
222 }
223
224 /* EOF */