4 #include <rosrtl/string.h>
5 #include "../../lib/bzip2/bzlib.h"
7 NTSTATUS STDCALL
RamdrvDispatchDeviceControl(PDEVICE_OBJECT DeviceObject
,
10 PIO_STACK_LOCATION IrpStack
;
11 ULONG ControlCode
, InputLength
, OutputLength
;
14 DPRINT("RamdrvDispatchDeviceControl\n");
16 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
17 ControlCode
= IrpStack
->Parameters
.DeviceIoControl
.IoControlCode
;
18 InputLength
= IrpStack
->Parameters
.DeviceIoControl
.InputBufferLength
;
19 OutputLength
= IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
;
23 case IOCTL_DISK_GET_DRIVE_GEOMETRY
:
24 if (OutputLength
< sizeof(DISK_GEOMETRY
))
26 Status
= STATUS_INVALID_PARAMETER
;
30 PDISK_GEOMETRY Geometry
= Irp
->AssociatedIrp
.SystemBuffer
;
31 Geometry
->MediaType
= F3_1Pt44_512
;
32 Geometry
->Cylinders
.QuadPart
= 80;
33 Geometry
->TracksPerCylinder
= 2 * 18;
34 Geometry
->SectorsPerTrack
= 18;
35 Geometry
->BytesPerSector
= 512;
36 Status
= STATUS_SUCCESS
;
37 Irp
->IoStatus
.Information
= sizeof(DISK_GEOMETRY
);
41 Status
= STATUS_INVALID_DEVICE_REQUEST
;
43 Irp
->IoStatus
.Status
= Status
;
44 IoCompleteRequest(Irp
, NT_SUCCESS(Status
) ? IO_DISK_INCREMENT
: IO_NO_INCREMENT
);
48 NTSTATUS STDCALL
RamdrvDispatchReadWrite(PDEVICE_OBJECT DeviceObject
,
51 PRAMDRV_DEVICE_EXTENSION devext
= (PRAMDRV_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
52 PIO_STACK_LOCATION Stk
= IoGetCurrentIrpStackLocation( Irp
);
54 if( Stk
->Parameters
.Read
.ByteOffset
.u
.HighPart
||
55 Stk
->Parameters
.Read
.ByteOffset
.u
.LowPart
>= devext
->Size
)
57 Irp
->IoStatus
.Status
= STATUS_END_OF_FILE
;
58 Irp
->IoStatus
.Information
= 0;
59 IoCompleteRequest( Irp
, 0 );
60 return STATUS_END_OF_FILE
;
62 if( (Stk
->Parameters
.Read
.ByteOffset
.u
.LowPart
+ Stk
->Parameters
.Read
.Length
) > devext
->Size
)
63 Stk
->Parameters
.Read
.Length
= devext
->Size
- Stk
->Parameters
.Read
.ByteOffset
.u
.LowPart
;
64 if( Stk
->MajorFunction
== IRP_MJ_READ
)
65 RtlCopyMemory( MmGetSystemAddressForMdl( Irp
->MdlAddress
),
66 devext
->Buffer
+ Stk
->Parameters
.Read
.ByteOffset
.u
.LowPart
,
67 Stk
->Parameters
.Read
.Length
);
68 else RtlCopyMemory( devext
->Buffer
+ Stk
->Parameters
.Read
.ByteOffset
.u
.LowPart
,
69 MmGetSystemAddressForMdl( Irp
->MdlAddress
),
70 Stk
->Parameters
.Read
.Length
);
71 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
72 Irp
->IoStatus
.Information
= Stk
->Parameters
.Read
.Length
;
73 IoCompleteRequest( Irp
, 0 );
74 return STATUS_SUCCESS
;
77 NTSTATUS STDCALL
RamdrvDispatchOpenClose(PDEVICE_OBJECT DeviceObject
,
80 DPRINT("RamdrvDispatchOpenClose\n");
81 return STATUS_SUCCESS
;
84 NTSTATUS STDCALL
DriverEntry(IN PDRIVER_OBJECT DriverObject
,
85 IN PUNICODE_STRING RegistryPath
)
87 UNICODE_STRING DeviceName
= ROS_STRING_INITIALIZER(L
"\\Device\\Ramdisk");
89 PDEVICE_OBJECT DeviceObject
;
90 PRAMDRV_DEVICE_EXTENSION devext
;
91 UNICODE_STRING LinkName
;
93 OBJECT_ATTRIBUTES objattr
;
95 LARGE_INTEGER allocsize
;
98 unsigned int dstlen
= 1024 * 1440;
99 FILE_STANDARD_INFORMATION finfo
;
102 DPRINT("Ramdisk driver\n");
104 /* Export other driver entry points... */
105 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = RamdrvDispatchOpenClose
;
106 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = RamdrvDispatchOpenClose
;
107 DriverObject
->MajorFunction
[IRP_MJ_READ
] = RamdrvDispatchReadWrite
;
108 DriverObject
->MajorFunction
[IRP_MJ_WRITE
] = RamdrvDispatchReadWrite
;
109 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = RamdrvDispatchDeviceControl
;
112 // create device and symbolic link
113 Status
= IoCreateDevice( DriverObject
,
114 sizeof( RAMDRV_DEVICE_EXTENSION
),
120 if( !NT_SUCCESS( Status
) )
122 DeviceObject
->Flags
|= DO_DIRECT_IO
;
123 devext
= (PRAMDRV_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
124 devext
->Size
= 1440 * 1024;
125 devext
->Buffer
= ExAllocatePool( PagedPool
, devext
->Size
);
126 if( !devext
->Buffer
)
128 Status
= STATUS_INSUFFICIENT_RESOURCES
;
131 RtlRosInitUnicodeStringFromLiteral( &LinkName
, L
"\\??\\Z:" );
132 IoCreateSymbolicLink( &LinkName
, &DeviceName
);
134 RtlRosInitUnicodeStringFromLiteral( &LinkName
, L
"\\Device\\Floppy0\\ramdisk.bz2" );
135 InitializeObjectAttributes( &objattr
,
140 allocsize
.u
.LowPart
= allocsize
.u
.HighPart
= 0;
142 Status
= ZwOpenFile( &file
,
147 FILE_NO_INTERMEDIATE_BUFFERING
);
149 if( !NT_SUCCESS( Status
) )
151 DPRINT( "Failed to open floppy\n" );
155 InitializeObjectAttributes( &objattr
,
160 Status
= ZwCreateEvent( &event
,
165 if( !NT_SUCCESS( Status
) )
167 DPRINT( "Failed to create event\n" );
171 Status
= ZwQueryInformationFile( file
,
175 FileStandardInformation
);
177 if( !NT_SUCCESS( Status
) )
179 DPRINT1( "Failed to query file information\n" );
182 tbuff
= ExAllocatePool( PagedPool
, finfo
.EndOfFile
.u
.LowPart
);
185 DPRINT1( "Failed to allocate buffer of size %d\n", finfo
.EndOfFile
.u
.LowPart
);
186 Status
= STATUS_INSUFFICIENT_RESOURCES
;
190 Status
= ZwReadFile( file
,
196 finfo
.EndOfFile
.u
.LowPart
,
200 if( !NT_SUCCESS( Status
) )
202 DPRINT( "Failed to read floppy\n" );
205 Status
= ZwWaitForSingleObject( event
, FALSE
, 0 );
206 if( Status
!= STATUS_WAIT_0
|| !NT_SUCCESS( iosb
.Status
) )
208 DPRINT( "Failed to read floppy\n" );
211 DPRINT( "RAMDRV: Read in %d bytes, decompressing now\n", iosb
.Information
);
212 err
= BZ2_bzBuffToBuffDecompress( devext
->Buffer
,
220 DPRINT( "RAMDRV: Image Decompressed\n");
222 else DbgPrint( "RAMDRV: Failed to decomparess image, error: %d\n", err
);
226 return STATUS_SUCCESS
;
235 ExFreePool( devext
->Buffer
);
238 IoDeleteDevice( DeviceObject
);