1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
| #include "Driver.h" #include "ntintsafe.h" #ifdef ALLOC_PRAGMA #pragma alloc_text(INIT, DriverEntry) #pragma alloc_text(PAGE, RamDiskEvtDeviceAdd) #pragma alloc_text(PAGE, RamDiskEvtDeviceContextCleanup) #pragma alloc_text(PAGE, RamDiskQueryDiskRegParameters) #pragma alloc_text(PAGE, RamDiskFormatDisk) #endif
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) { WDF_DRIVER_CONFIG config; KdPrint(("Windows Ramdisk Driver - Driver Framework Edition.\n")); KdPrint(("Built %s %s\n", __DATE__, __TIME__)); WDF_DRIVER_CONFIG_INIT(&config, RamDiskEvtDeviceAdd); return WdfDriverCreate(DriverObject, RegistryPath, WDF_NO_OBJECT_ATTRIBUTES, &config, WDF_NO_HANDLE); };
VOID RamDiskEvtIoRead(IN WDFQUEUE Queue, IN WDFREQUEST Request, IN size_t Length) { PDEVICE_EXTENSION devExt = QueueGetExtension(Queue)->DeviceExtension; NTSTATUS Status = STATUS_INVALID_PARAMETER; WDF_REQUEST_PARAMETERS Parameters; LARGE_INTEGER ByteOffset; WDFMEMORY hMemory; WDF_REQUEST_PARAMETERS_INIT(&Parameters); WdfRequestGetParameters(Request, &Parameters); ByteOffset.QuadPart = Parameters.Parameters.Read.DeviceOffset; if (RamDiskCheckParameters(devExt, ByteOffset, Length)) { Status = WdfRequestRetrieveOutputMemory(Request, &hMemory); if (NT_SUCCESS(Status)) Status = WdfMemoryCopyFromBuffer(hMemory, 0, devExt->DiskImage + ByteOffset.LowPart, Length); }; WdfRequestCompleteWithInformation(Request, Status, (ULONG_PTR)Length); return; };
VOID RamDiskEvtIoWrite(IN WDFQUEUE Queue, IN WDFREQUEST Request, IN size_t Length) { PDEVICE_EXTENSION devExt = QueueGetExtension(Queue)->DeviceExtension; NTSTATUS Status = STATUS_INVALID_PARAMETER; WDF_REQUEST_PARAMETERS Parameters; LARGE_INTEGER ByteOffset; WDFMEMORY hMemory; WDF_REQUEST_PARAMETERS_INIT(&Parameters); WdfRequestGetParameters(Request, &Parameters); ByteOffset.QuadPart = Parameters.Parameters.Write.DeviceOffset; if (RamDiskCheckParameters(devExt, ByteOffset, Length)) { Status = WdfRequestRetrieveInputMemory(Request, &hMemory); if (NT_SUCCESS(Status)) Status = WdfMemoryCopyToBuffer(hMemory, 0, devExt->DiskImage + ByteOffset.LowPart, Length); }; WdfRequestCompleteWithInformation(Request, Status, (ULONG_PTR)Length); return; };
VOID RamDiskEvtIoDeviceControl(IN WDFQUEUE Queue, IN WDFREQUEST Request, IN size_t OutputBufferLength, IN size_t InputBufferLength, IN ULONG IoControlCode) { NTSTATUS Status = STATUS_INVALID_DEVICE_REQUEST; ULONG_PTR information = 0; size_t bufSize; PDEVICE_EXTENSION devExt = QueueGetExtension(Queue)->DeviceExtension; UNREFERENCED_PARAMETER(OutputBufferLength); UNREFERENCED_PARAMETER(InputBufferLength); switch (IoControlCode) { case IOCTL_DISK_GET_PARTITION_INFO: { PPARTITION_INFORMATION outputBuffer; PBOOT_SECTOR bootSector = (PBOOT_SECTOR)devExt->DiskImage; information = sizeof(PARTITION_INFORMATION); Status = WdfRequestRetrieveOutputBuffer(Request, sizeof(PARTITION_INFORMATION), &outputBuffer, &bufSize); if (NT_SUCCESS(Status)) { outputBuffer->PartitionType = (bootSector->bsFileSystemType[4] == '6') ? PARTITION_FAT_16 : PARTITION_FAT_12; outputBuffer->BootIndicator = FALSE; outputBuffer->RecognizedPartition = TRUE; outputBuffer->RewritePartition = FALSE; outputBuffer->StartingOffset.QuadPart = 0; outputBuffer->PartitionLength.QuadPart = devExt->DiskRegInfo.DiskSize; outputBuffer->HiddenSectors = (ULONG)(1L); outputBuffer->PartitionNumber = (ULONG)(-1L); Status = STATUS_SUCCESS; }; }; break; case IOCTL_DISK_GET_DRIVE_GEOMETRY: { PDISK_GEOMETRY outputBuffer; information = sizeof(DISK_GEOMETRY); Status = WdfRequestRetrieveOutputBuffer(Request, sizeof(DISK_GEOMETRY), &outputBuffer, &bufSize); if (NT_SUCCESS(Status)) { RtlCopyMemory(outputBuffer, &(devExt->DiskGeometry), sizeof(DISK_GEOMETRY)); Status = STATUS_SUCCESS; }; }; break; case IOCTL_DISK_CHECK_VERIFY: case IOCTL_DISK_IS_WRITABLE: Status = STATUS_SUCCESS; break; }; WdfRequestCompleteWithInformation(Request, Status, information); return; };
VOID RamDiskEvtDeviceContextCleanup(IN WDFDEVICE Device) { PDEVICE_EXTENSION pDeviceExtension = DeviceGetExtension(Device); PAGED_CODE(); if (pDeviceExtension->DiskImage) ExFreePool(pDeviceExtension->DiskImage); return; };
NTSTATUS RamDiskEvtDeviceAdd(IN WDFDRIVER Driver, IN PWDFDEVICE_INIT DeviceInit) { WDF_OBJECT_ATTRIBUTES deviceAttributes; NTSTATUS status; WDFDEVICE device; WDF_OBJECT_ATTRIBUTES queueAttributes; WDF_IO_QUEUE_CONFIG ioQueueConfig; PDEVICE_EXTENSION pDeviceExtension; PQUEUE_EXTENSION pQueueContext = NULL; WDFQUEUE queue; DECLARE_CONST_UNICODE_STRING(ntDeviceName, NT_DEVICE_NAME); PAGED_CODE(); UNREFERENCED_PARAMETER(Driver);
status = WdfDeviceInitAssignName(DeviceInit, &ntDeviceName); if (!NT_SUCCESS(status)) return status; WdfDeviceInitSetDeviceType(DeviceInit, FILE_DEVICE_DISK); WdfDeviceInitSetIoType(DeviceInit, WdfDeviceIoDirect); WdfDeviceInitSetExclusive(DeviceInit, FALSE); WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&deviceAttributes, DEVICE_EXTENSION); deviceAttributes.EvtCleanupCallback = RamDiskEvtDeviceContextCleanup; status = WdfDeviceCreate(&DeviceInit, &deviceAttributes, &device); if (!NT_SUCCESS(status)) return status; pDeviceExtension = DeviceGetExtension(device);
WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&ioQueueConfig, WdfIoQueueDispatchSequential); ioQueueConfig.EvtIoDeviceControl = RamDiskEvtIoDeviceControl; ioQueueConfig.EvtIoRead = RamDiskEvtIoRead; ioQueueConfig.EvtIoWrite = RamDiskEvtIoWrite; WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&queueAttributes, QUEUE_EXTENSION); status = WdfIoQueueCreate(device, &ioQueueConfig, &queueAttributes, &queue); if (!NT_SUCCESS(status)) return status; pQueueContext = QueueGetExtension(queue); pQueueContext->DeviceExtension = pDeviceExtension;
pDeviceExtension->DiskRegInfo.DriveLetter.Buffer = (PWSTR)&pDeviceExtension->DriveLetterBuffer; pDeviceExtension->DiskRegInfo.DriveLetter.MaximumLength = sizeof(pDeviceExtension->DriveLetterBuffer); RamDiskQueryDiskRegParameters(WdfDriverGetRegistryPath(WdfDeviceGetDriver(device)), &pDeviceExtension->DiskRegInfo);
pDeviceExtension->DiskImage = ExAllocatePoolWithTag(NonPagedPool, pDeviceExtension->DiskRegInfo.DiskSize, RAMDISK_TAG); if (pDeviceExtension->DiskImage) { UNICODE_STRING deviceName; UNICODE_STRING win32Name; RamDiskFormatDisk(pDeviceExtension); status = STATUS_SUCCESS; RtlInitUnicodeString(&win32Name, DOS_DEVICE_NAME); pDeviceExtension->SymbolicLink.Buffer = (PWSTR)&pDeviceExtension->DosDeviceNameBuffer; pDeviceExtension->SymbolicLink.MaximumLength = sizeof(pDeviceExtension->DosDeviceNameBuffer); pDeviceExtension->SymbolicLink.Length = win32Name.Length; RtlCopyUnicodeString(&pDeviceExtension->SymbolicLink, &win32Name); RtlAppendUnicodeStringToString(&pDeviceExtension->SymbolicLink, &pDeviceExtension->DiskRegInfo.DriveLetter); status = WdfDeviceCreateSymbolicLink(device, &pDeviceExtension->SymbolicLink); }; return status; };
VOID RamDiskQueryDiskRegParameters(__in PWSTR RegistryPath, __in PDISK_INFO DiskRegInfo) { RTL_QUERY_REGISTRY_TABLE rtlQueryRegTbl[5 + 1]; NTSTATUS Status; DISK_INFO defDiskRegInfo; PAGED_CODE(); ASSERT(RegistryPath != NULL); defDiskRegInfo.DiskSize = DEFAULT_DISK_SIZE; defDiskRegInfo.RootDirEntries = DEFAULT_ROOT_DIR_ENTRIES; defDiskRegInfo.SectorsPerCluster = DEFAULT_SECTORS_PER_CLUSTER; RtlInitUnicodeString(&defDiskRegInfo.DriveLetter, DEFAULT_DRIVE_LETTER); RtlZeroMemory(rtlQueryRegTbl, sizeof(rtlQueryRegTbl)); rtlQueryRegTbl[0].Flags = RTL_QUERY_REGISTRY_SUBKEY; rtlQueryRegTbl[0].Name = L"Parameters"; rtlQueryRegTbl[0].EntryContext = NULL; rtlQueryRegTbl[0].DefaultType = (ULONG_PTR)NULL; rtlQueryRegTbl[0].DefaultData = NULL; rtlQueryRegTbl[0].DefaultLength = (ULONG_PTR)NULL; rtlQueryRegTbl[1].Flags = RTL_QUERY_REGISTRY_DIRECT; rtlQueryRegTbl[1].Name = L"DiskSize"; rtlQueryRegTbl[1].EntryContext = &DiskRegInfo->DiskSize; rtlQueryRegTbl[1].DefaultType = REG_DWORD; rtlQueryRegTbl[1].DefaultData = &defDiskRegInfo.DiskSize; rtlQueryRegTbl[1].DefaultLength = sizeof(ULONG); rtlQueryRegTbl[2].Flags = RTL_QUERY_REGISTRY_DIRECT; rtlQueryRegTbl[2].Name = L"RootDirEntries"; rtlQueryRegTbl[2].EntryContext = &DiskRegInfo->RootDirEntries; rtlQueryRegTbl[2].DefaultType = REG_DWORD; rtlQueryRegTbl[2].DefaultData = &defDiskRegInfo.RootDirEntries; rtlQueryRegTbl[2].DefaultLength = sizeof(ULONG); rtlQueryRegTbl[3].Flags = RTL_QUERY_REGISTRY_DIRECT; rtlQueryRegTbl[3].Name = L"SectorsPerCluster"; rtlQueryRegTbl[3].EntryContext = &DiskRegInfo->SectorsPerCluster; rtlQueryRegTbl[3].DefaultType = REG_DWORD; rtlQueryRegTbl[3].DefaultData = &defDiskRegInfo.SectorsPerCluster; rtlQueryRegTbl[3].DefaultLength = sizeof(ULONG); rtlQueryRegTbl[4].Flags = RTL_QUERY_REGISTRY_DIRECT; rtlQueryRegTbl[4].Name = L"DriveLetter"; rtlQueryRegTbl[4].EntryContext = &DiskRegInfo->DriveLetter; rtlQueryRegTbl[4].DefaultType = REG_SZ; rtlQueryRegTbl[4].DefaultData = defDiskRegInfo.DriveLetter.Buffer; rtlQueryRegTbl[4].DefaultLength = 0; Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE | RTL_REGISTRY_OPTIONAL, RegistryPath, rtlQueryRegTbl, NULL, NULL); if (NT_SUCCESS(Status) == FALSE) { DiskRegInfo->DiskSize = defDiskRegInfo.DiskSize; DiskRegInfo->RootDirEntries = defDiskRegInfo.RootDirEntries; DiskRegInfo->SectorsPerCluster = defDiskRegInfo.SectorsPerCluster; RtlCopyUnicodeString(&DiskRegInfo->DriveLetter, &defDiskRegInfo.DriveLetter); }; KdPrint(("DiskSize = 0x%lx\n", DiskRegInfo->DiskSize)); KdPrint(("RootDirEntries = 0x%lx\n", DiskRegInfo->RootDirEntries)); KdPrint(("SectorsPerCluster = 0x%lx\n", DiskRegInfo->SectorsPerCluster)); KdPrint(("DriveLetter = %wZ\n", &(DiskRegInfo->DriveLetter))); return; };
NTSTATUS RamDiskFormatDisk(IN PDEVICE_EXTENSION devExt){ PBOOT_SECTOR bootSector = (PBOOT_SECTOR)devExt->DiskImage; PUCHAR firstFatSector; ULONG rootDirEntries; ULONG sectorsPerCluster; USHORT fatType; USHORT fatEntries; USHORT fatSectorCnt; PDIR_ENTRY rootDir; PAGED_CODE(); ASSERT(sizeof(BOOT_SECTOR) == 512); ASSERT(devExt->DiskImage != NULL); RtlZeroMemory(devExt->DiskImage, devExt->DiskRegInfo.DiskSize);
devExt->DiskGeometry.BytesPerSector = 512; devExt->DiskGeometry.SectorsPerTrack = 32; devExt->DiskGeometry.TracksPerCylinder = 2; devExt->DiskGeometry.Cylinders.QuadPart = devExt->DiskRegInfo.DiskSize / 512 / 32 / 2; devExt->DiskGeometry.MediaType = RAMDISK_MEDIA_TYPE; KdPrint(("Cylinders: %ld\n TracksPerCylinder: %ld\n SectorsPerTrack: %ld\n BytesPerSector: %ld\n",devExt->DiskGeometry.Cylinders.QuadPart, devExt->DiskGeometry.TracksPerCylinder,devExt->DiskGeometry.SectorsPerTrack, devExt->DiskGeometry.BytesPerSector));
rootDirEntries = devExt->DiskRegInfo.RootDirEntries; sectorsPerCluster = devExt->DiskRegInfo.SectorsPerCluster; if (rootDirEntries & (DIR_ENTRIES_PER_SECTOR - 1)) rootDirEntries =(rootDirEntries + (DIR_ENTRIES_PER_SECTOR - 1)) &~(DIR_ENTRIES_PER_SECTOR - 1); KdPrint(("Root dir entries: %ld\n Sectors/cluster: %ld\n",rootDirEntries, sectorsPerCluster));
bootSector->bsJump[0] = 0xeb; bootSector->bsJump[1] = 0x3c; bootSector->bsJump[2] = 0x90; bootSector->bsOemName[0] = 'R'; bootSector->bsOemName[1] = 'a'; bootSector->bsOemName[2] = 'j'; bootSector->bsOemName[3] = 'u'; bootSector->bsOemName[4] = 'R'; bootSector->bsOemName[5] = 'a'; bootSector->bsOemName[6] = 'm'; bootSector->bsOemName[7] = ' '; bootSector->bsBytesPerSec = (SHORT)devExt->DiskGeometry.BytesPerSector; bootSector->bsResSectors = 1; bootSector->bsFATs = 1; bootSector->bsRootDirEnts = (USHORT)rootDirEntries; bootSector->bsSectors = (USHORT)(devExt->DiskRegInfo.DiskSize /devExt->DiskGeometry.BytesPerSector); bootSector->bsMedia = (UCHAR)devExt->DiskGeometry.MediaType; bootSector->bsSecPerClus = (UCHAR)sectorsPerCluster;
fatEntries =(bootSector->bsSectors - bootSector->bsResSectors -bootSector->bsRootDirEnts / DIR_ENTRIES_PER_SECTOR) /bootSector->bsSecPerClus + 2; if (fatEntries > 4087) { fatType = 16; fatSectorCnt = (fatEntries * 2 + 511) / 512; fatEntries = fatEntries + fatSectorCnt; fatSectorCnt = (fatEntries * 2 + 511) / 512; } else { fatType = 12; fatSectorCnt = (((fatEntries * 3 + 1) / 2) + 511) / 512; fatEntries = fatEntries + fatSectorCnt; fatSectorCnt = (((fatEntries * 3 + 1) / 2) + 511) / 512; };
bootSector->bsFATsecs = fatSectorCnt; bootSector->bsSecPerTrack = (USHORT)devExt->DiskGeometry.SectorsPerTrack; bootSector->bsHeads = (USHORT)devExt->DiskGeometry.TracksPerCylinder; bootSector->bsBootSignature = 0x29; bootSector->bsVolumeID = 0x12345678; bootSector->bsLabel[0] = 'R'; bootSector->bsLabel[1] = 'a'; bootSector->bsLabel[2] = 'm'; bootSector->bsLabel[3] = 'D'; bootSector->bsLabel[4] = 'i'; bootSector->bsLabel[5] = 's'; bootSector->bsLabel[6] = 'k'; bootSector->bsLabel[7] = ' '; bootSector->bsLabel[8] = ' '; bootSector->bsLabel[9] = ' '; bootSector->bsLabel[10] = ' '; bootSector->bsFileSystemType[0] = 'F'; bootSector->bsFileSystemType[1] = 'A'; bootSector->bsFileSystemType[2] = 'T'; bootSector->bsFileSystemType[3] = '1'; bootSector->bsFileSystemType[4] = '?'; bootSector->bsFileSystemType[5] = ' '; bootSector->bsFileSystemType[6] = ' '; bootSector->bsFileSystemType[7] = ' '; bootSector->bsFileSystemType[4] = (fatType == 16) ? '6' : '2'; bootSector->bsSig2[0] = 0x55; bootSector->bsSig2[1] = 0xAA;
firstFatSector = (PUCHAR)(bootSector + 1); firstFatSector[0] = (UCHAR)devExt->DiskGeometry.MediaType; firstFatSector[1] = 0xFF; firstFatSector[2] = 0xFF; if (fatType == 16) firstFatSector[3] = 0xFF;
rootDir = (PDIR_ENTRY)(bootSector + 1 + fatSectorCnt); rootDir->deName[0] = 'M'; rootDir->deName[1] = 'S'; rootDir->deName[2] = '-'; rootDir->deName[3] = 'R'; rootDir->deName[4] = 'A'; rootDir->deName[5] = 'M'; rootDir->deName[6] = 'D'; rootDir->deName[7] = 'R'; rootDir->deExtension[0] = 'I'; rootDir->deExtension[1] = 'V'; rootDir->deExtension[2] = 'E'; rootDir->deAttributes = DIR_ATTR_VOLUME; return STATUS_SUCCESS; };
BOOLEAN RamDiskCheckParameters(IN PDEVICE_EXTENSION devExt, IN LARGE_INTEGER ByteOffset, IN size_t Length) { if (devExt->DiskRegInfo.DiskSize < Length || ByteOffset.QuadPart < 0 || ((ULONGLONG)ByteOffset.QuadPart > (devExt->DiskRegInfo.DiskSize - Length)) || (Length & (devExt->DiskGeometry.BytesPerSector - 1))) { KdPrint(("Error invalid parameter\n""ByteOffset: %x\n""Length: %d\n", ByteOffset, Length)); return FALSE; }; return TRUE; };
|