4 #include "../../lib/bzip2/bzlib.h"
6 NTSTATUS STDCALL
RamdrvDispatchDeviceControl(PDEVICE_OBJECT DeviceObject
,
9 PIO_STACK_LOCATION IrpStack
;
10 ULONG ControlCode
, InputLength
, OutputLength
;
13 DPRINT("RamdrvDispatchDeviceControl\n");
15 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
16 ControlCode
= IrpStack
->Parameters
.DeviceIoControl
.IoControlCode
;
17 InputLength
= IrpStack
->Parameters
.DeviceIoControl
.InputBufferLength
;
18 OutputLength
= IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
;
22 case IOCTL_DISK_GET_DRIVE_GEOMETRY
:
23 if (OutputLength
< sizeof(DISK_GEOMETRY
))
25 Status
= STATUS_INVALID_PARAMETER
;
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
);
40 Status
= STATUS_INVALID_DEVICE_REQUEST
;
42 Irp
->IoStatus
.Status
= Status
;
43 IoCompleteRequest(Irp
, NT_SUCCESS(Status
) ? IO_DISK_INCREMENT
: IO_NO_INCREMENT
);
47 NTSTATUS STDCALL
RamdrvDispatchReadWrite(PDEVICE_OBJECT DeviceObject
,
50 PRAMDRV_DEVICE_EXTENSION devext
= (PRAMDRV_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
51 PIO_STACK_LOCATION Stk
= IoGetCurrentIrpStackLocation( Irp
);
53 if( Stk
->Parameters
.Read
.ByteOffset
.u
.HighPart
||
54 Stk
->Parameters
.Read
.ByteOffset
.u
.LowPart
>= devext
->Size
)
56 Irp
->IoStatus
.Status
= STATUS_END_OF_FILE
;
57 Irp
->IoStatus
.Information
= 0;
58 IoCompleteRequest( Irp
, 0 );
59 return STATUS_END_OF_FILE
;
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
;
76 NTSTATUS STDCALL
RamdrvDispatchOpenClose(PDEVICE_OBJECT DeviceObject
,
79 DPRINT("RamdrvDispatchOpenClose\n");
80 return STATUS_SUCCESS
;
83 NTSTATUS STDCALL
DriverEntry(IN PDRIVER_OBJECT DriverObject
,
84 IN PUNICODE_STRING RegistryPath
)
86 UNICODE_STRING DeviceName
= UNICODE_STRING_INITIALIZER(L
"\\Device\\Ramdisk");
88 PDEVICE_OBJECT DeviceObject
;
89 PRAMDRV_DEVICE_EXTENSION devext
;
90 UNICODE_STRING LinkName
;
92 OBJECT_ATTRIBUTES objattr
;
94 LARGE_INTEGER allocsize
;
97 unsigned int dstlen
= 1024 * 1440;
98 FILE_STANDARD_INFORMATION finfo
;
101 DPRINT("Ramdisk driver\n");
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
;
111 // create device and symbolic link
112 Status
= IoCreateDevice( DriverObject
,
113 sizeof( RAMDRV_DEVICE_EXTENSION
),
119 if( !NT_SUCCESS( 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
)
127 Status
= STATUS_INSUFFICIENT_RESOURCES
;
130 RtlInitUnicodeStringFromLiteral( &LinkName
, L
"\\??\\Z:" );
131 IoCreateSymbolicLink( &LinkName
, &DeviceName
);
133 RtlInitUnicodeStringFromLiteral( &LinkName
, L
"\\Device\\Floppy0\\ramdisk.bz2" );
134 InitializeObjectAttributes( &objattr
,
139 allocsize
.u
.LowPart
= allocsize
.u
.HighPart
= 0;
141 Status
= NtOpenFile( &file
,
146 FILE_NO_INTERMEDIATE_BUFFERING
);
148 if( !NT_SUCCESS( Status
) )
150 DPRINT( "Failed to open floppy\n" );
154 InitializeObjectAttributes( &objattr
,
159 Status
= NtCreateEvent( &event
,
164 if( !NT_SUCCESS( Status
) )
166 DPRINT( "Failed to create event\n" );
170 Status
= NtQueryInformationFile( file
,
174 FileStandardInformation
);
176 if( !NT_SUCCESS( Status
) )
178 DPRINT1( "Failed to query file information\n" );
181 tbuff
= ExAllocatePool( PagedPool
, finfo
.EndOfFile
.u
.LowPart
);
184 DPRINT1( "Failed to allocate buffer of size %d\n", finfo
.EndOfFile
.u
.LowPart
);
185 Status
= STATUS_INSUFFICIENT_RESOURCES
;
189 Status
= NtReadFile( file
,
195 finfo
.EndOfFile
.u
.LowPart
,
199 if( !NT_SUCCESS( Status
) )
201 DPRINT( "Failed to read floppy\n" );
204 Status
= NtWaitForSingleObject( event
, FALSE
, 0 );
205 if( Status
!= STATUS_WAIT_0
|| !NT_SUCCESS( iosb
.Status
) )
207 DPRINT( "Failed to read floppy\n" );
210 DPRINT( "RAMDRV: Read in %d bytes, decompressing now\n", iosb
.Information
);
211 err
= BZ2_bzBuffToBuffDecompress( devext
->Buffer
,
219 DPRINT( "RAMDRV: Image Decompressed\n");
221 else DbgPrint( "RAMDRV: Failed to decomparess image, error: %d\n", err
);
225 return STATUS_SUCCESS
;
234 ExFreePool( devext
->Buffer
);
237 IoDeleteDevice( DeviceObject
);