Removed or changed debug prints printed at boot.
[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 RamdrvDispatchReadWrite(PDEVICE_OBJECT DeviceObject,
7 PIRP Irp)
8 {
9 PRAMDRV_DEVICE_EXTENSION devext = (PRAMDRV_DEVICE_EXTENSION)DeviceObject->DeviceExtension;
10 PIO_STACK_LOCATION Stk = IoGetCurrentIrpStackLocation( Irp );
11
12 if( Stk->Parameters.Read.ByteOffset.u.HighPart ||
13 Stk->Parameters.Read.ByteOffset.u.LowPart >= devext->Size )
14 {
15 Irp->IoStatus.Status = STATUS_END_OF_FILE;
16 Irp->IoStatus.Information = 0;
17 IoCompleteRequest( Irp, 0 );
18 return STATUS_END_OF_FILE;
19 }
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;
33 }
34
35 NTSTATUS STDCALL RamdrvDispatchOpenClose(PDEVICE_OBJECT DeviceObject,
36 PIRP Irp)
37 {
38 DPRINT("RamdrvDispatchOpenClose\n");
39 return STATUS_SUCCESS;
40 }
41
42 NTSTATUS STDCALL DriverEntry(IN PDRIVER_OBJECT DriverObject,
43 IN PUNICODE_STRING RegistryPath)
44 {
45 UNICODE_STRING DeviceName;
46 NTSTATUS Status;
47 PDEVICE_OBJECT DeviceObject;
48 PRAMDRV_DEVICE_EXTENSION devext;
49 UNICODE_STRING LinkName;
50 HANDLE file;
51 OBJECT_ATTRIBUTES objattr;
52 IO_STATUS_BLOCK iosb;
53 LARGE_INTEGER allocsize;
54 HANDLE event;
55 void *tbuff;
56 unsigned int dstlen = 1024 * 1440;
57 FILE_STANDARD_INFORMATION finfo;
58 DWORD err;
59
60 DPRINT("Ramdisk driver\n");
61
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;
69
70
71 // create device and symbolic link
72 RtlInitUnicodeString( &DeviceName, L"\\Device\\Ramdisk" );
73 Status = IoCreateDevice( DriverObject,
74 sizeof( RAMDRV_DEVICE_EXTENSION ),
75 &DeviceName,
76 FILE_DEVICE_DISK,
77 0,
78 FALSE,
79 &DeviceObject );
80 if( !NT_SUCCESS( Status ) )
81 return 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 );
86 if( !devext->Buffer )
87 {
88 Status = STATUS_INSUFFICIENT_RESOURCES;
89 goto cleandevice;
90 }
91 RtlInitUnicodeString( &LinkName, L"\\ArcName\\virt(0)disk(0)ram(0)" );
92 IoAssignArcName( &LinkName, &DeviceName );
93 RtlInitUnicodeString( &LinkName, L"\\??\\Z:" );
94 IoCreateSymbolicLink( &LinkName, &DeviceName );
95
96 RtlInitUnicodeString( &LinkName, L"\\Device\\Floppy0\\ramdisk.bz2" );
97 InitializeObjectAttributes( &objattr,
98 &LinkName,
99 0,
100 0,
101 0 );
102 allocsize.u.LowPart = allocsize.u.HighPart = 0;
103
104 Status = NtOpenFile( &file,
105 GENERIC_READ,
106 &objattr,
107 &iosb,
108 FILE_SHARE_READ,
109 FILE_NO_INTERMEDIATE_BUFFERING );
110
111 if( !NT_SUCCESS( Status ) )
112 {
113 DPRINT( "Failed to open floppy\n" );
114 goto cleanbuffer;
115 }
116
117 InitializeObjectAttributes( &objattr,
118 0,
119 0,
120 0,
121 0 );
122 Status = NtCreateEvent( &event,
123 0,
124 &objattr,
125 TRUE,
126 FALSE );
127 if( !NT_SUCCESS( Status ) )
128 {
129 DPRINT( "Failed to create event\n" );
130 goto cleanfile;
131 }
132
133 Status = NtQueryInformationFile( file,
134 &iosb,
135 &finfo,
136 sizeof( finfo ),
137 FileStandardInformation );
138
139 if( !NT_SUCCESS( Status ) )
140 {
141 DPRINT1( "Failed to query file information\n" );
142 goto cleanevent;
143 }
144 tbuff = ExAllocatePool( PagedPool, finfo.EndOfFile.u.LowPart );
145 if( !tbuff )
146 {
147 DPRINT1( "Failed to allocate buffer of size %d\n", finfo.EndOfFile.u.LowPart );
148 Status = STATUS_INSUFFICIENT_RESOURCES;
149 goto cleanevent;
150 }
151
152 Status = NtReadFile( file,
153 event,
154 0,
155 0,
156 &iosb,
157 tbuff,
158 finfo.EndOfFile.u.LowPart,
159 &allocsize,
160 0 );
161
162 if( !NT_SUCCESS( Status ) )
163 {
164 DPRINT( "Failed to read floppy\n" );
165 goto cleantbuff;
166 }
167 Status = NtWaitForSingleObject( event, FALSE, 0 );
168 if( Status != STATUS_WAIT_0 || !NT_SUCCESS( iosb.Status ) )
169 {
170 DPRINT( "Failed to read floppy\n" );
171 goto cleantbuff;
172 }
173 DPRINT( "RAMDRV: Read in %d bytes, decompressing now\n", iosb.Information );
174 err = BZ2_bzBuffToBuffDecompress( devext->Buffer,
175 &dstlen,
176 tbuff,
177 iosb.Information,
178 1,
179 0 );
180 if( err == 0 )
181 DPRINT( "RAMDRV: Image Decompressed\n");
182 else DbgPrint( "RAMDRV: Failed to decomparess image, error: %d\n", err );
183 ExFreePool( tbuff );
184 NtClose( file );
185 NtClose( event );
186 return STATUS_SUCCESS;
187
188 cleantbuff:
189 ExFreePool( tbuff );
190 cleanevent:
191 NtClose( event );
192 cleanfile:
193 NtClose( file );
194 cleanbuffer:
195 ExFreePool( devext->Buffer );
196
197 cleandevice:
198 IoDeleteDevice( DeviceObject );
199 for(;;);
200
201 return Status;
202 }
203