fixed compiler warnings and cvsignore files
[reactos.git] / reactos / drivers / dd / ramdrv / ramdrv.c
1 #include <ntddk.h>
2 #include "ramdrv.h"
3 #include <debug.h>
4 #include "../../lib/bzip2/bzlib.h"
5
6 NTSTATUS STDCALL RamdrvDispatchDeviceControl(PDEVICE_OBJECT DeviceObject,
7 PIRP Irp)
8 {
9 PIO_STACK_LOCATION IrpStack;
10 ULONG ControlCode, InputLength, OutputLength;
11 NTSTATUS Status;
12
13 DPRINT("RamdrvDispatchDeviceControl\n");
14
15 IrpStack = IoGetCurrentIrpStackLocation(Irp);
16 ControlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode;
17 InputLength = IrpStack->Parameters.DeviceIoControl.InputBufferLength;
18 OutputLength = IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
19
20 switch (ControlCode)
21 {
22 case IOCTL_DISK_GET_DRIVE_GEOMETRY:
23 if (OutputLength < sizeof(DISK_GEOMETRY))
24 {
25 Status = STATUS_INVALID_PARAMETER;
26 }
27 else
28 {
29 PDISK_GEOMETRY Geometry = Irp->AssociatedIrp.SystemBuffer;
30 Geometry->MediaType = F3_1Pt44_512;
31 Geometry->Cylinders.QuadPart = 80;
32 Geometry->TracksPerCylinder = 2 * 18;
33 Geometry->SectorsPerTrack = 18;
34 Geometry->BytesPerSector = 512;
35 Status = STATUS_SUCCESS;
36 Irp->IoStatus.Information = sizeof(DISK_GEOMETRY);
37 }
38 break;
39 default:
40 Status = STATUS_INVALID_DEVICE_REQUEST;
41 }
42 Irp->IoStatus.Status = Status;
43 IoCompleteRequest(Irp, NT_SUCCESS(Status) ? IO_DISK_INCREMENT : IO_NO_INCREMENT);
44 return Status;
45 }
46
47 NTSTATUS STDCALL RamdrvDispatchReadWrite(PDEVICE_OBJECT DeviceObject,
48 PIRP Irp)
49 {
50 PRAMDRV_DEVICE_EXTENSION devext = (PRAMDRV_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
51 PIO_STACK_LOCATION Stk = IoGetCurrentIrpStackLocation( Irp );
52
53 if( Stk->Parameters.Read.ByteOffset.u.HighPart ||
54 Stk->Parameters.Read.ByteOffset.u.LowPart >= devext->Size )
55 {
56 Irp->IoStatus.Status = STATUS_END_OF_FILE;
57 Irp->IoStatus.Information = 0;
58 IoCompleteRequest( Irp, 0 );
59 return STATUS_END_OF_FILE;
60 }
61 if( (Stk->Parameters.Read.ByteOffset.u.LowPart + Stk->Parameters.Read.Length) > devext->Size )
62 Stk->Parameters.Read.Length = devext->Size - Stk->Parameters.Read.ByteOffset.u.LowPart;
63 if( Stk->MajorFunction == IRP_MJ_READ )
64 RtlCopyMemory( MmGetSystemAddressForMdl( Irp->MdlAddress ),
65 devext->Buffer + Stk->Parameters.Read.ByteOffset.u.LowPart,
66 Stk->Parameters.Read.Length );
67 else RtlCopyMemory( devext->Buffer + Stk->Parameters.Read.ByteOffset.u.LowPart,
68 MmGetSystemAddressForMdl( Irp->MdlAddress ),
69 Stk->Parameters.Read.Length );
70 Irp->IoStatus.Status = STATUS_SUCCESS;
71 Irp->IoStatus.Information = Stk->Parameters.Read.Length;
72 IoCompleteRequest( Irp, 0 );
73 return STATUS_SUCCESS;
74 }
75
76 NTSTATUS STDCALL RamdrvDispatchOpenClose(PDEVICE_OBJECT DeviceObject,
77 PIRP Irp)
78 {
79 DPRINT("RamdrvDispatchOpenClose\n");
80 return STATUS_SUCCESS;
81 }
82
83 NTSTATUS STDCALL DriverEntry(IN PDRIVER_OBJECT DriverObject,
84 IN PUNICODE_STRING RegistryPath)
85 {
86 UNICODE_STRING DeviceName = UNICODE_STRING_INITIALIZER(L"\\Device\\Ramdisk");
87 NTSTATUS Status;
88 PDEVICE_OBJECT DeviceObject;
89 PRAMDRV_DEVICE_EXTENSION devext;
90 UNICODE_STRING LinkName;
91 HANDLE file;
92 OBJECT_ATTRIBUTES objattr;
93 IO_STATUS_BLOCK iosb;
94 LARGE_INTEGER allocsize;
95 HANDLE event;
96 void *tbuff;
97 unsigned int dstlen = 1024 * 1440;
98 FILE_STANDARD_INFORMATION finfo;
99 DWORD err;
100
101 DPRINT("Ramdisk driver\n");
102
103 /* Export other driver entry points... */
104 DriverObject->MajorFunction[IRP_MJ_CREATE] = (PDRIVER_DISPATCH)RamdrvDispatchOpenClose;
105 DriverObject->MajorFunction[IRP_MJ_CLOSE] = (PDRIVER_DISPATCH)RamdrvDispatchOpenClose;
106 DriverObject->MajorFunction[IRP_MJ_READ] = (PDRIVER_DISPATCH)RamdrvDispatchReadWrite;
107 DriverObject->MajorFunction[IRP_MJ_WRITE] = (PDRIVER_DISPATCH)RamdrvDispatchReadWrite;
108 DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = (PDRIVER_DISPATCH)RamdrvDispatchDeviceControl;
109
110
111 // create device and symbolic link
112 Status = IoCreateDevice( DriverObject,
113 sizeof( RAMDRV_DEVICE_EXTENSION ),
114 &DeviceName,
115 FILE_DEVICE_DISK,
116 0,
117 FALSE,
118 &DeviceObject );
119 if( !NT_SUCCESS( Status ) )
120 return Status;
121 DeviceObject->Flags |= DO_DIRECT_IO;
122 devext = (PRAMDRV_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
123 devext->Size = 1440 * 1024;
124 devext->Buffer = ExAllocatePool( PagedPool, devext->Size );
125 if( !devext->Buffer )
126 {
127 Status = STATUS_INSUFFICIENT_RESOURCES;
128 goto cleandevice;
129 }
130 RtlInitUnicodeStringFromLiteral( &LinkName, L"\\??\\Z:" );
131 IoCreateSymbolicLink( &LinkName, &DeviceName );
132
133 RtlInitUnicodeStringFromLiteral( &LinkName, L"\\Device\\Floppy0\\ramdisk.bz2" );
134 InitializeObjectAttributes( &objattr,
135 &LinkName,
136 0,
137 0,
138 0 );
139 allocsize.u.LowPart = allocsize.u.HighPart = 0;
140
141 Status = NtOpenFile( &file,
142 GENERIC_READ,
143 &objattr,
144 &iosb,
145 FILE_SHARE_READ,
146 FILE_NO_INTERMEDIATE_BUFFERING );
147
148 if( !NT_SUCCESS( Status ) )
149 {
150 DPRINT( "Failed to open floppy\n" );
151 goto cleanbuffer;
152 }
153
154 InitializeObjectAttributes( &objattr,
155 0,
156 0,
157 0,
158 0 );
159 Status = NtCreateEvent( &event,
160 0,
161 &objattr,
162 TRUE,
163 FALSE );
164 if( !NT_SUCCESS( Status ) )
165 {
166 DPRINT( "Failed to create event\n" );
167 goto cleanfile;
168 }
169
170 Status = NtQueryInformationFile( file,
171 &iosb,
172 &finfo,
173 sizeof( finfo ),
174 FileStandardInformation );
175
176 if( !NT_SUCCESS( Status ) )
177 {
178 DPRINT1( "Failed to query file information\n" );
179 goto cleanevent;
180 }
181 tbuff = ExAllocatePool( PagedPool, finfo.EndOfFile.u.LowPart );
182 if( !tbuff )
183 {
184 DPRINT1( "Failed to allocate buffer of size %d\n", finfo.EndOfFile.u.LowPart );
185 Status = STATUS_INSUFFICIENT_RESOURCES;
186 goto cleanevent;
187 }
188
189 Status = NtReadFile( file,
190 event,
191 0,
192 0,
193 &iosb,
194 tbuff,
195 finfo.EndOfFile.u.LowPart,
196 &allocsize,
197 0 );
198
199 if( !NT_SUCCESS( Status ) )
200 {
201 DPRINT( "Failed to read floppy\n" );
202 goto cleantbuff;
203 }
204 Status = NtWaitForSingleObject( event, FALSE, 0 );
205 if( Status != STATUS_WAIT_0 || !NT_SUCCESS( iosb.Status ) )
206 {
207 DPRINT( "Failed to read floppy\n" );
208 goto cleantbuff;
209 }
210 DPRINT( "RAMDRV: Read in %d bytes, decompressing now\n", iosb.Information );
211 err = BZ2_bzBuffToBuffDecompress( devext->Buffer,
212 &dstlen,
213 tbuff,
214 iosb.Information,
215 1,
216 0 );
217 if( err == 0 )
218 {
219 DPRINT( "RAMDRV: Image Decompressed\n");
220 }
221 else DbgPrint( "RAMDRV: Failed to decomparess image, error: %d\n", err );
222 ExFreePool( tbuff );
223 NtClose( file );
224 NtClose( event );
225 return STATUS_SUCCESS;
226
227 cleantbuff:
228 ExFreePool( tbuff );
229 cleanevent:
230 NtClose( event );
231 cleanfile:
232 NtClose( file );
233 cleanbuffer:
234 ExFreePool( devext->Buffer );
235
236 cleandevice:
237 IoDeleteDevice( DeviceObject );
238 for(;;);
239
240 return Status;
241 }
242