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
"\\ArcName\\virt(0)disk(0)ram(0)" );
92 IoAssignArcName( &LinkName
, &DeviceName
);
93 RtlInitUnicodeString( &LinkName
, L
"\\??\\Z:" );
94 IoCreateSymbolicLink( &LinkName
, &DeviceName
);
96 RtlInitUnicodeString( &LinkName
, L
"\\Device\\Floppy0\\ramdisk.bz2" );
97 InitializeObjectAttributes( &objattr
,
102 allocsize
.u
.LowPart
= allocsize
.u
.HighPart
= 0;
104 Status
= NtOpenFile( &file
,
109 FILE_NO_INTERMEDIATE_BUFFERING
);
111 if( !NT_SUCCESS( Status
) )
113 DPRINT( "Failed to open floppy\n" );
117 InitializeObjectAttributes( &objattr
,
122 Status
= NtCreateEvent( &event
,
127 if( !NT_SUCCESS( Status
) )
129 DPRINT( "Failed to create event\n" );
133 Status
= NtQueryInformationFile( file
,
137 FileStandardInformation
);
139 if( !NT_SUCCESS( Status
) )
141 DPRINT1( "Failed to query file information\n" );
144 tbuff
= ExAllocatePool( PagedPool
, finfo
.EndOfFile
.u
.LowPart
);
147 DPRINT1( "Failed to allocate buffer of size %d\n", finfo
.EndOfFile
.u
.LowPart
);
148 Status
= STATUS_INSUFFICIENT_RESOURCES
;
152 Status
= NtReadFile( file
,
158 finfo
.EndOfFile
.u
.LowPart
,
162 if( !NT_SUCCESS( Status
) )
164 DPRINT( "Failed to read floppy\n" );
167 Status
= NtWaitForSingleObject( event
, FALSE
, 0 );
168 if( Status
!= STATUS_WAIT_0
|| !NT_SUCCESS( iosb
.Status
) )
170 DPRINT( "Failed to read floppy\n" );
173 DPRINT( "RAMDRV: Read in %d bytes, decompressing now\n", iosb
.Information
);
174 err
= BZ2_bzBuffToBuffDecompress( devext
->Buffer
,
181 DPRINT( "RAMDRV: Image Decompressed\n");
182 else DbgPrint( "RAMDRV: Failed to decomparess image, error: %d\n", err
);
186 return STATUS_SUCCESS
;
195 ExFreePool( devext
->Buffer
);
198 IoDeleteDevice( DeviceObject
);