4 #include "../../../lib/bzip2/bzlib.h"
6 NTSTATUS STDCALL
RamdrvDispatchReadWrite(PDEVICE_OBJECT DeviceObject
,
9 PRAMDRV_DEVICE_EXTENSION devext
= (PRAMDRV_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
10 PIO_STACK_LOCATION Stk
= IoGetCurrentIrpStackLocation( Irp
);
12 if( Stk
->Parameters
.Read
.ByteOffset
.u
.HighPart
||
13 Stk
->Parameters
.Read
.ByteOffset
.u
.LowPart
>= devext
->Size
)
15 Irp
->IoStatus
.Status
= STATUS_END_OF_FILE
;
16 Irp
->IoStatus
.Information
= 0;
17 IoCompleteRequest( Irp
, 0 );
18 return STATUS_END_OF_FILE
;
20 if( (Stk
->Parameters
.Read
.ByteOffset
.u
.LowPart
+ Stk
->Parameters
.Read
.Length
) > devext
->Size
)
21 Stk
->Parameters
.Read
.Length
= devext
->Size
- Stk
->Parameters
.Read
.ByteOffset
.u
.LowPart
;
22 if( Stk
->MajorFunction
== IRP_MJ_READ
)
23 RtlCopyMemory( MmGetSystemAddressForMdl( Irp
->MdlAddress
),
24 devext
->Buffer
+ Stk
->Parameters
.Read
.ByteOffset
.u
.LowPart
,
25 Stk
->Parameters
.Read
.Length
);
26 else RtlCopyMemory( devext
->Buffer
+ Stk
->Parameters
.Read
.ByteOffset
.u
.LowPart
,
27 MmGetSystemAddressForMdl( Irp
->MdlAddress
),
28 Stk
->Parameters
.Read
.Length
);
29 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
30 Irp
->IoStatus
.Information
= Stk
->Parameters
.Read
.Length
;
31 IoCompleteRequest( Irp
, 0 );
32 return STATUS_SUCCESS
;
35 NTSTATUS STDCALL
RamdrvDispatchOpenClose(PDEVICE_OBJECT DeviceObject
,
38 DPRINT("RamdrvDispatchOpenClose\n");
39 return STATUS_SUCCESS
;
42 NTSTATUS STDCALL
DriverEntry(IN PDRIVER_OBJECT DriverObject
,
43 IN PUNICODE_STRING RegistryPath
)
45 UNICODE_STRING DeviceName
;
47 PDEVICE_OBJECT DeviceObject
;
48 PRAMDRV_DEVICE_EXTENSION devext
;
49 UNICODE_STRING LinkName
;
51 OBJECT_ATTRIBUTES objattr
;
53 LARGE_INTEGER allocsize
;
56 unsigned int dstlen
= 1024 * 1440;
57 FILE_STANDARD_INFORMATION finfo
;
60 DPRINT("Ramdisk driver\n");
62 /* Export other driver entry points... */
63 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = RamdrvDispatchOpenClose
;
64 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = RamdrvDispatchOpenClose
;
65 DriverObject
->MajorFunction
[IRP_MJ_READ
] = RamdrvDispatchReadWrite
;
66 DriverObject
->MajorFunction
[IRP_MJ_WRITE
] = RamdrvDispatchReadWrite
;
67 // DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] =
68 // RamdrvDispatchDeviceControl;
71 // create device and symbolic link
72 RtlInitUnicodeString( &DeviceName
, L
"\\Device\\Ramdisk" );
73 Status
= IoCreateDevice( DriverObject
,
74 sizeof( RAMDRV_DEVICE_EXTENSION
),
80 if( !NT_SUCCESS( Status
) )
82 DeviceObject
->Flags
|= DO_DIRECT_IO
;
83 devext
= (PRAMDRV_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
84 devext
->Size
= 1440 * 1024;
85 devext
->Buffer
= ExAllocatePool( PagedPool
, devext
->Size
);
88 Status
= STATUS_INSUFFICIENT_RESOURCES
;
91 RtlInitUnicodeString( &LinkName
, L
"\\??\\Z:" );
92 IoCreateSymbolicLink( &LinkName
, &DeviceName
);
94 RtlInitUnicodeString( &LinkName
, L
"\\Device\\Floppy0\\ramdisk.bz2" );
95 InitializeObjectAttributes( &objattr
,
100 allocsize
.u
.LowPart
= allocsize
.u
.HighPart
= 0;
102 Status
= NtOpenFile( &file
,
107 FILE_NO_INTERMEDIATE_BUFFERING
);
109 if( !NT_SUCCESS( Status
) )
111 DPRINT( "Failed to open floppy\n" );
115 InitializeObjectAttributes( &objattr
,
120 Status
= NtCreateEvent( &event
,
125 if( !NT_SUCCESS( Status
) )
127 DPRINT( "Failed to create event\n" );
131 Status
= NtQueryInformationFile( file
,
135 FileStandardInformation
);
137 if( !NT_SUCCESS( Status
) )
139 DPRINT1( "Failed to query file information\n" );
142 tbuff
= ExAllocatePool( PagedPool
, finfo
.EndOfFile
.u
.LowPart
);
145 DPRINT1( "Failed to allocate buffer of size %d\n", finfo
.EndOfFile
.u
.LowPart
);
146 Status
= STATUS_INSUFFICIENT_RESOURCES
;
150 Status
= NtReadFile( file
,
156 finfo
.EndOfFile
.u
.LowPart
,
160 if( !NT_SUCCESS( Status
) )
162 DPRINT( "Failed to read floppy\n" );
165 Status
= NtWaitForSingleObject( event
, FALSE
, 0 );
166 if( Status
!= STATUS_WAIT_0
|| !NT_SUCCESS( iosb
.Status
) )
168 DPRINT( "Failed to read floppy\n" );
171 DPRINT( "RAMDRV: Read in %d bytes, decompressing now\n", iosb
.Information
);
172 err
= BZ2_bzBuffToBuffDecompress( devext
->Buffer
,
179 DPRINT( "RAMDRV: Image Decompressed\n");
180 else DbgPrint( "RAMDRV: Failed to decomparess image, error: %d\n", err
);
184 return STATUS_SUCCESS
;
193 ExFreePool( devext
->Buffer
);
196 IoDeleteDevice( DeviceObject
);