-
Notifications
You must be signed in to change notification settings - Fork 74
/
eram.c
3925 lines (3708 loc) · 120 KB
/
eram.c
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ERAM.C RAM disk ERAM for WindowsNT/2000/2003/XP/7
Copyright (c) 1999-2020 by *Error15 & Zero3K
Translated into English by Katayama Hirofumi MZ.
*/
/* Update History:
v1.00
Initial creation.
v1.01
over 32MB support (upto 64MB).
Win2000 beta 3 support.
v1.10
Win2000 operation check.
v1.11
When failure of memory allocation, make the RAM amount indication dumped
Fixed calculation of the number of clusters around boundary of FAT12 and FAT16.
v2.00
Handle the memory after MAXMEM=nn with switching in 64KB units.
v2.01
Enabled 1GB allocation by increasing allocation unit 32.
v2.02
You can specify the start address of memory starting from MAXMEM=nn.
It can also be swappable by treating the Drive type as a local disk.
Event log message added.
v2.10
FAT32 support.
Made it return the partition at "FAT16 over 32MB".
Able to allocate 2GB even in FAT16 by increasing Allocation Unit64...
Made it set the current date to the volume serial number.
v2.11
WinXP support.
v2.12
Stand-by support.
Calling IoReportResourceUsage makes it impossible to standby (even if you later release it).
IoReportResourceForDetection does not pass over 16MB of internal memory request.
You can HalTranslateBusAddress without calling IoReportResourceUsage.
Even without HalTranslateBusAddress, the internal memory shows the same address.
Standby (power remains on): RAM is ok even outside OS management.
Hibernate (power off): RAM is not allowed under OS management (ok for OS management).
v2.20
Fixed that calculation of external memory amount was signed (no actual damage).
Add the calculation not to wrap 4GB around detection of external memory.
Fixed that option character comparator didn't correctly support Unicode.
Fixed the FAT16 max cluster from 65535 to 65525.
Corrected the FAT32 maximum clusters to 268435455 (= 0xFFFFFFF) (Win2000=4177918).
Maximum capacity limited to maximum cluster x Allocation Unit (Strictly afford but ignored).
Maximum capacity limited to 4GB.
On NT systems, the maximum capacity of FAT16 is 4GB, but the display of chkdsk etc. was strange.
Stop rounding up by cluster restriction and change to rounding down.
Changed FAT32 clusters from 65527 to 65526.
Modified to be testable even with models with less memory.
Made it possible to forcibly allocate it when forced to use external memory.
(On older hardware, the address bus might not be fully 32bit-decoded).
Modified to be testable using memory-mapped file (Filesystem driver load premise).
In file I/O, ZwXXX seems to be unable to cope with conflicts well, so I used mapped file.
Output file change support.
Changed to target only the management area by clearing the area at startup.
Option 32bit-nized.
Made it automatically enable FAT32 at Windows2000+.
Disabled "Verify processing" (Implemented the size check only)
Fixed wrong sector/drive info.
Windows Server 2003 support.
IOCTL_DISK_GET_LENGTH_INFO processing added.
XP and later don't calculate the disk capacity, so this correspondence was likely needed.
IOCTL_DISK_SET_PARTITION_INFO processing added.
It seems that conversion or formatting to NTFS seems to work.
Fix the FAT12 max clusters from 4087 to 4086.
Volume label change support.
Unable to specify *?/|.,;:+=[]()&^<>" and control codes.
Simplified the conversion from UNICODE-to-ANSI-to-UNICODE to ANSI-to-UNICODE.
Made it possible to set the real device without swap relation.
Addition of correction function when selecting multiple memories.
FAT12/16: Corrected malfunctioning when the root directory sector exceeded all sectors.
TEMP directory creation feature added.
This time (again?) lacking the following features:
NTFS format.
Because it is troublesome, please escape with formatting or conversion.
When formatting at startup, it may be possible to avoid it with the following settings:
Around "Run":
cmd.exe /c convert drv: /fs:ntfs < %systemroot%\system32\eramconv.txt
Around system32\eramconv.txt:
volume-label
y
n
Sometimes folders are opened and conversion can not be done.
If you set HDD (=swappable) formatting can also be used.
Corporation with volume manager.
I left it as it if not a PnP driver because it seems difficult.
Mount point (NTFS 5) can not be used.
Unable to use hardlink (symlink rktools linkd)
XP: Unable to put the page file.
It seemed impossible to deal with it unrecognized by the mount manager.
"Start" can not be set to 0, even if it is set to Primary Disk.
In XP and later, you can do without page file, so wish to avoid it.
/BURNMEMORY=n support.
http://msdn.microsoft.com/library/en-us/ddtools/hh/ddtools/bootini_9omr.asp
The means of acquiring memory mounting amount at driver loading is unknown.
HKLM\HARDWARE\RESOURCEMAP\System Resources\Physical Memory\ is not done at all.
v2.21 (preliminary version)
ACPI:ACPI Reclaim/NVS memory exclusion.
Enabled to get the NT device name from the registry (for forcibly for the case of using multiple ERAMs).
You can make HKLM\System\CurrentControlSet\Services\ERAM ERAM2 or similar, and
reboot with setting value "DevName" (REG_SZ) to \Device\ERAM2.
In Win2000 or later, it is not displayed in the device manager but it can be put in INF for NT.
v2.22 (preliminary version)
ACPI: Added error processing at ACPI Reclaim/NVS memory exclusion.
over4GB: The measures against MAXMEM working over 4GB when /MAXMEM=n and /NOLOWMEM coexisted (Thanks for TENMU SHINRYUUSAI)
When the physical address of the pool is over 4GB, it is regarded as /PAE designation.
When /PAE designation is taken, the /NOLOWMEM specification is also searched.
When /NOLOWMEM is specified, it is regarded as equivalent to /MAXMEM=16.
v2.23 (preliminary version)
over4GB: The measures against MAXMEM working over 4GB When /MAXMEM=n and /NOLOWMEM coexist (Thanks for TENMU SHINRYUUSAI).
/PAE:v2.22 made it 16, but 16 was repaired by check so it was fixed to 17.
YET:over4GB: The measures against the effect of /NOLOWMEM even without /PAE (Pointed out by TENMU SHINRYUUSAI).
We can search the /NOLOWMEM designation without /PAE designation?
It seems that it can not detect the limit value because it is used for PagedPool...
YET:over4GB: allocate RAM in LME status (?)
YET:SMBIOS:get maximum amount of memory
YET:SUM check
v2.24
Fixes BSODs that were occurring when using it in a 64-bit version of Windows.
Improved the Control Panel Applet.
v2.30
Fixes BSODs that were occurring when using more than 4 GB of RAM as the Ramdisk size.
*/
#pragma warning(disable : 4100 4115 4201 4214 4514 )
#include <ntddk.h>
#include <ntdddisk.h>
#include <devioctl.h>
#include <ntddstor.h>
#include <ntiologc.h>
#include "eram.h"
#include "eramum.h"
#pragma pack(1)
/* EramCreateClose
Open/Close Request Entry
Parameters
pDevObj The pointer to device object.
pIrp The pointer to IRP packet.
Return Value
Results.
*/
NTSTATUS EramCreateClose(
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp
)
{
KdPrint(("EramCreateClose start\n"));
/* Set success */
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = 0;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
KdPrint(("EramCreateClose end\n"));
return STATUS_SUCCESS;
}
/* EramDeviceControl
Device Control Request Entry
Parameters
pDevObj The pointer to device object.
pIrp The pointer to IRP packet.
Return Value
Results.
*/
NTSTATUS EramDeviceControl(
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp
)
{
/* local variables */
PERAM_EXTENSION pEramExt;
PIO_STACK_LOCATION pIrpSp;
NTSTATUS ntStat;
//KdPrint(("EramDeviceControl start\n"));
/* Get the head pointer of the structure */
pEramExt = pDevObj->DeviceExtension;
/* Get the pointer to the stack */
pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
/* Set failure */
pIrp->IoStatus.Status = STATUS_INVALID_DEVICE_REQUEST;
pIrp->IoStatus.Information = 0;
switch (pIrpSp->Parameters.DeviceIoControl.IoControlCode) /* Branching by request types */
{
case IOCTL_DISK_GET_MEDIA_TYPES: /* media type getting (array) */
case IOCTL_DISK_GET_DRIVE_GEOMETRY: /* media type getting (1) */
/* geometry getting */
EramDeviceControlGeometry(pEramExt, pIrp, pIrpSp->Parameters.DeviceIoControl.OutputBufferLength);
break;
case IOCTL_DISK_GET_PARTITION_INFO: /* Get the partition info */
/* Partition info getting processing */
EramDeviceControlGetPartInfo(pEramExt, pIrp, pIrpSp->Parameters.DeviceIoControl.OutputBufferLength);
break;
case IOCTL_DISK_SET_PARTITION_INFO: /* Get the partition info */
/* Partition Info Settings processing */
EramDeviceControlSetPartInfo(pEramExt, pIrp, pIrpSp->Parameters.DeviceIoControl.InputBufferLength);
break;
case IOCTL_DISK_VERIFY: /* Verify */
/* Verify processing */
EramDeviceControlVerify(pEramExt, pIrp, pIrpSp->Parameters.DeviceIoControl.InputBufferLength);
break;
case IOCTL_DISK_CHECK_VERIFY: /* Disk check (Win2000 beta3+) */
/* Verify processing */
EramDeviceControlDiskCheckVerify(pEramExt, pIrp, pIrpSp->Parameters.DeviceIoControl.OutputBufferLength);
break;
case IOCTL_DISK_GET_LENGTH_INFO: /* Disk size getting (Win2000+ [necessary when formatting/conversion at WinXP and later]) */
/* Disk size getting processing */
EramDeviceControlGetLengthInfo(pEramExt, pIrp, pIrpSp->Parameters.DeviceIoControl.OutputBufferLength);
break;
default: /* misc. */
/* Ignore */
KdPrint(("Eram IOCTL 0x%x\n", (UINT)(pIrpSp->Parameters.DeviceIoControl.IoControlCode)));
break;
}
/* Set status */
ntStat = pIrp->IoStatus.Status;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
//KdPrint(("EramDeviceControl end\n"));
return ntStat;
}
/* EramDeviceControlGeometry
Geometry Getting Process
Parameters
pEramExt The pointer to an ERAM_EXTENTION structure.
pIrp The pointer to IRP packet.
uLen The buffer size.
Return Value
No return value.
*/
VOID EramDeviceControlGeometry(
PERAM_EXTENSION pEramExt,
IN PIRP pIrp,
IN ULONG uLen
)
{
/* local variables */
PDISK_GEOMETRY pGeom;
if (uLen < sizeof(*pGeom)) /* size lacking */
{
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
KdPrint(("EramDeviceControlGeometry:size too small\n"));
return;
}
/* Disk Geometry setting */
pGeom = (PDISK_GEOMETRY)(pIrp->AssociatedIrp.SystemBuffer);
pGeom->MediaType = FixedMedia; /* Media type: Fixed disk */
pGeom->Cylinders.QuadPart = (ULONGLONG)(pEramExt->uAllSector);
pGeom->TracksPerCylinder = 1;
pGeom->SectorsPerTrack = 1; /* sectors per bank */
pGeom->BytesPerSector = SECTOR;
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = sizeof(*pGeom);
}
/* EramDeviceControlGetPartInfo
Partition info getting processing.
Parameters
pEramExt The pointer to an ERAM_EXTENTION structure.
pIrp The pointer to IRP packet.
uLen The buffer size.
Return Value
No return value.
*/
VOID EramDeviceControlGetPartInfo(
PERAM_EXTENSION pEramExt,
IN PIRP pIrp,
IN ULONG uLen
)
{
/* local variables */
PPARTITION_INFORMATION pPart;
if (uLen < sizeof(*pPart)) /* lacking size */
{
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
KdPrint(("EramDeviceControlGetPartInfo:size too small\n"));
return;
}
/* Partition Info Settings */
pPart = (PPARTITION_INFORMATION)(pIrp->AssociatedIrp.SystemBuffer);
pPart->PartitionType = pEramExt->FAT_size;
pPart->BootIndicator = FALSE; /* Refuse boot */
pPart->RecognizedPartition = TRUE; /* Partition detected */
pPart->RewritePartition = FALSE; /* unrewritable partition */
pPart->StartingOffset.QuadPart = (ULONGLONG)(0); /* Partition starting position */
pPart->PartitionLength.QuadPart = UInt32x32To64(pEramExt->uAllSector, SECTOR); /* the length */
pPart->HiddenSectors = pEramExt->bsHiddenSecs; /* the number of hidden sectors */
pPart->PartitionNumber = 1; /* The number of partitions */
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = sizeof(PARTITION_INFORMATION);
}
/* EramDeviceControlSetPartInfo
Partition info setting processing.
Parameters
pEramExt The pointer to an ERAM_EXTENTION structure.
pIrp The poitner to IRP packet.
uLen The buffer size.
Return Value
No return value.
*/
VOID EramDeviceControlSetPartInfo(
PERAM_EXTENSION pEramExt,
IN PIRP pIrp,
IN ULONG uLen
)
{
/* local variables */
PSET_PARTITION_INFORMATION pPart;
if (uLen < sizeof(*pPart)) /* lacking size */
{
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
KdPrint(("EramDeviceControlSetPartInfo:size too small\n"));
return;
}
/* Partition Info Settings */
pPart = (PSET_PARTITION_INFORMATION)(pIrp->AssociatedIrp.SystemBuffer);
pEramExt->FAT_size = pPart->PartitionType;
pIrp->IoStatus.Status = STATUS_SUCCESS;
}
/* EramDeviceControlVerify
Verify processing.
Parameters
pEramExt The pointer to an ERAM_EXTENTION structure.
pIrp The pointer to IRP packet.
uLen The buffer size.
Return Value
No return value.
*/
VOID EramDeviceControlVerify(
PERAM_EXTENSION pEramExt,
IN PIRP pIrp,
IN ULONG uLen
)
{
/* local variables */
PVERIFY_INFORMATION pVerify;
//KdPrint(("EramDeviceControlVerify start\n"));
if (uLen < sizeof(*pVerify)) /* lacking size */
{
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
KdPrint(("EramDeviceControlVerify:size too small\n"));
return;
}
/* Verify Info Settings TODO: i think its already using 64bit math to calculate stuff here */
pVerify = pIrp->AssociatedIrp.SystemBuffer;
//KdPrint(("Eram offset 0x%x%08x, len 0x%x\n", pVerify->StartingOffset.HighPart, pVerify->StartingOffset.LowPart, pVerify->Length));
if ((((ULONGLONG)(pVerify->StartingOffset.QuadPart) + (ULONGLONG)(pVerify->Length)) > UInt32x32To64(pEramExt->uAllSector, SECTOR))||
((pVerify->StartingOffset.LowPart & (SECTOR-1)) != 0)||
((pVerify->Length & (SECTOR-1)) != 0)) /* Disk capacity exceeded or the starting position or the length is not a multiple of the sector size */
{
/* Return error */
KdPrint(("Eram Invalid I/O parameter\n"));
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
return;
}
if ((pEramExt->uOptflag.Bits.External != 0)&& /* OS-Unmanaged Memory usage */
(pEramExt->uExternalStart != 0)&& /* OS-Unmanaged Memory setting */
((pEramExt->uExternalStart + (ULONGLONG)(pVerify->StartingOffset.QuadPart) + (ULONGLONG)(pVerify->Length)) >=
pEramExt->uExternalEnd))
{
//KdPrint(("Eram Invalid I/O address space\n"));
pIrp->IoStatus.Status = STATUS_DISK_CORRUPT_ERROR;
return;
}
pIrp->IoStatus.Status = STATUS_SUCCESS;
//KdPrint(("EramDeviceControlVerify end\n"));
}
/* EramDeviceControlDiskCheckVerify
Disk Replacement Confirming Process.
Parameters
pEdskExt The pointer to an EDSK_EXTENTION structure.
pIrp The pointer to IRP packet.
uLen The buffer size.
Return Value
No return value.
*/
VOID EramDeviceControlDiskCheckVerify(
PERAM_EXTENSION pEramExt,
IN PIRP pIrp,
IN ULONG uLen
)
{
/* local variables */
PULONG puOpt;
pIrp->IoStatus.Status = STATUS_SUCCESS;
if (uLen == 0) /* aux info not needed */
{
return;
}
if (uLen < sizeof(*puOpt)) /* lacking size */
{
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
KdPrint(("EramDeviceControlDiskCheckVerify:size too small\n"));
return;
}
/* Aux info settings */
puOpt = (PULONG)(pIrp->AssociatedIrp.SystemBuffer);
*puOpt = 0;
pIrp->IoStatus.Information = sizeof(*puOpt);
}
/* EramDeviceControlGetLengthInfo
Disk Size Getting Process.
Parameters
pEdskExt The pointer to an EDSK_EXTENTION structure.
pIrp The pointer to IRP packet.
uLen The buffer size.
Return Value
No return value.
*/
VOID EramDeviceControlGetLengthInfo(
PERAM_EXTENSION pEramExt,
IN PIRP pIrp,
IN ULONG uLen
)
{
/* local variables */
PGET_LENGTH_INFORMATION pInfo;
if (uLen < sizeof(*pInfo)) /* lacking size */
{
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
KdPrint(("EramDeviceControlGetLengthInfo:size too small\n"));
return;
}
/* Size Info Settings */
pInfo = (PGET_LENGTH_INFORMATION)(pIrp->AssociatedIrp.SystemBuffer);
pInfo->Length.QuadPart = UInt32x32To64(pEramExt->uAllSector, SECTOR); /* the length */
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = sizeof(*pInfo);
KdPrint(("EramDeviceControlGetLengthInfo length 0x%x\n", (pEramExt->uAllSector * SECTOR)));
}
/* EramReadWrite
Read/Write/Verify Request Entry.
Parameters
pDevObj The pointer to device object.
pIrp The pointer to IRP packet.
Return Value
Results.
*/
NTSTATUS EramReadWrite(
IN PDEVICE_OBJECT pDevObj,
IN PIRP pIrp
)
{
/* local variables */
PERAM_EXTENSION pEramExt;
PIO_STACK_LOCATION pIrpSp;
PUCHAR pTransAddr;
NTSTATUS ntStat;
//KdPrint(("EramReadWrite start\n"));
/* Get the pointer to the first structure */
pEramExt = pDevObj->DeviceExtension;
/* Get the pointer to the stack */
pIrpSp = IoGetCurrentIrpStackLocation(pIrp);
if ((((ULONGLONG)(pIrpSp->Parameters.Read.ByteOffset.QuadPart) + (ULONGLONG)(pIrpSp->Parameters.Read.Length)) > UInt32x32To64(pEramExt->uAllSector, SECTOR))||
((pIrpSp->Parameters.Read.ByteOffset.QuadPart & (SECTOR-1)) != 0)||
((pIrpSp->Parameters.Read.Length & (SECTOR-1)) != 0)) /* Disk capacity exceeded or the starting position / the length is not a multiple of the sector size */
{
KdPrint(("Invalid I/O parameter, offset 0x%x, length 0x%x, OP=0x%x(R=0x%x, W=0x%x), limit=0x%x\n", pIrpSp->Parameters.Read.ByteOffset.LowPart, pIrpSp->Parameters.Read.Length, pIrpSp->MajorFunction, IRP_MJ_READ, IRP_MJ_WRITE, (pEramExt->uAllSector * SECTOR)));
/* Return error */
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
return STATUS_INVALID_PARAMETER;
}
/* address initialization */
pTransAddr = NULL;
if (pIrp->MdlAddress != NULL) /* with address */
{
/* address translation */
pTransAddr = MmGetSystemAddressForMdlSafe(pIrp->MdlAddress,NormalPagePriority);
}
/* Set success */
ntStat = STATUS_SUCCESS;
/* Set the data length */
pIrp->IoStatus.Information = 0;
switch (pIrpSp->MajorFunction) /* Branch by functions */
{
case IRP_MJ_READ: /* reading */
//KdPrint(("ERam Read start\n"));
/* Validate the address */
if (pTransAddr == NULL)
{
KdPrint(("Eram MmGetSystemAddressForMdlSafe failed\n"));
ntStat = STATUS_INVALID_PARAMETER;
break;
}
/* Set the length of reading */
pIrp->IoStatus.Information = pIrpSp->Parameters.Read.Length;
/* reading */
ntStat = (*(pEramExt->EramRead))(pEramExt, pIrp, pIrpSp, pTransAddr);
//KdPrint(("Eram Read end\n"));
break;
case IRP_MJ_WRITE: /* writing */
//KdPrint(("Write start\n"));
/* Validate the address */
if (pTransAddr == NULL)
{
KdPrint(("Eram MmGetSystemAddressForMdlSafe failed\n"));
ntStat = STATUS_INVALID_PARAMETER;
break;
}
/* Set the length of reading */
pIrp->IoStatus.Information = pIrpSp->Parameters.Write.Length;
/* writing */
ntStat = (*(pEramExt->EramWrite))(pEramExt, pIrp, pIrpSp, pTransAddr);
//KdPrint(("Write end\n"));
break;
default:
KdPrint(("Eram RW default\n"));
pIrp->IoStatus.Information = 0;
break;
}
if (ntStat != STATUS_PENDING) /* Not pending */
{
/* Set status */
pIrp->IoStatus.Status = ntStat;
/* I/O complete */
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
}
//KdPrint(("EramReadWrite end\n"));
return ntStat;
}
/* EramUnloadDriver
Entry Point for the time of Device Stop
Parameters
pDrvObj The pointert to device representative object.
Return Value
No return value.
*/
VOID EramUnloadDriver(
IN PDRIVER_OBJECT pDrvObj
)
{
/* local variables */
PDEVICE_OBJECT pDevObj;
PERAM_EXTENSION pEramExt;
KdPrint(("EramUnloadDriver start\n"));
pDevObj = pDrvObj->DeviceObject;
pEramExt = (pDevObj != NULL) ? pDevObj->DeviceExtension : NULL;
/* Delete the device */
EramUnloadDevice(pDrvObj, pDevObj, pEramExt);
KdPrint(("EramUnloadDriver end\n"));
}
/* EramUnloadDevice
Device Deletion
Parameters
pDrvObj The pointer to device representative object.
pDevObj The pointer to device object.
pEramExt The pointer to an ERAM_EXTENTION structure.
Return Value
No return value.
*/
VOID EramUnloadDevice(
IN PDRIVER_OBJECT pDrvObj,
IN PDEVICE_OBJECT pDevObj,
IN PERAM_EXTENSION pEramExt
)
{
/* local variables */
LARGE_INTEGER llTime;
KdPrint(("EramUnloadDevice start\n"));
if (pEramExt != NULL) /* Device has already created */
{
KdPrint(("Eram Device exist\n"));
/* Notify thread termination */
pEramExt->bThreadStop = TRUE;
if (pEramExt->pThreadObject != NULL) /* Thread exists */
{
/* Decrement semaphore */
KeReleaseSemaphore(&(pEramExt->IrpSem), 0, 1, TRUE);
/* Wait 30 seconds for thread termination */
llTime.QuadPart = (LONGLONG)(-30 * 10000000);
/* Wait for thread termination */
KeWaitForSingleObject(&(pEramExt->pThreadObject), Executive, KernelMode, FALSE, &llTime);
/* Decrement the reference count of thread */
ObDereferenceObject(&(pEramExt->pThreadObject));
pEramExt->pThreadObject = NULL;
}
/* external file closing */
if (pEramExt->hSection != NULL)
{
KdPrint(("Eram File section close\n"));
ExtFileUnmap(pEramExt);
ZwClose(pEramExt->hSection);
pEramExt->hSection = NULL;
}
if (pEramExt->hFile != NULL)
{
ZwClose(pEramExt->hFile);
pEramExt->hFile = NULL;
}
/* memory map release */
ResourceRelease(pDrvObj, pEramExt);
if (pEramExt->Win32Name.Buffer != NULL) /* Win32 name has already created */
{
/* Win32 link release */
IoDeleteSymbolicLink(&(pEramExt->Win32Name));
/* Win32 name area release */
ExFreePool(pEramExt->Win32Name.Buffer);
pEramExt->Win32Name.Buffer = NULL;
}
}
if (pDevObj != NULL) /* Device exists */
{
/* Delete the device */
IoDeleteDevice(pDevObj);
}
KdPrint(("EramUnloadDevice end\n"));
}
/* ResourceRelease
Memory Map Deletion.
Parameters
pDrvObj The pointer to device representative object.
pEramExt The pointer to an ERAM_EXTENTION structure.
Return Value
No return value.
*/
VOID ResourceRelease(
IN PDRIVER_OBJECT pDrvObj,
IN PERAM_EXTENSION pEramExt
)
{
KdPrint(("ERam ResourceRelease start\n"));
if (pEramExt->uOptflag.Bits.External != 0) /* OS-Unmanaged Memory usage */
{
/* Resource release */
ReleaseMemResource(pDrvObj, pEramExt);
}
else if (pEramExt->pPageBase != NULL) /* Memory allocating */
{
/* memory release */
ExFreePool(pEramExt->pPageBase);
pEramExt->pPageBase = NULL;
}
KdPrint(("Eram ResourceRelease end\n"));
}
/* ReleaseMemResource
OS-Unmanaged Memory Map Deletion.
Parameters
pDrvObj The pointer to device representative object.
pEramExt The pointer to an ERAM_EXTENTION structure.
Return Value
No return value.
*/
VOID ReleaseMemResource(
IN PDRIVER_OBJECT pDrvObj,
IN PERAM_EXTENSION pEramExt
)
{
/* local variables */
CM_RESOURCE_LIST ResList; /* Resource list */
BOOLEAN bResConf;
/* Unmap */
ExtUnmap(pEramExt);
if (pEramExt->uOptflag.Bits.SkipReportUsage == 0)
{
/* Driver resource release (Not likely released in 2000) */
RtlZeroBytes(&ResList, sizeof(ResList));
IoReportResourceUsage(NULL, pDrvObj, &ResList, sizeof(ResList), NULL, NULL, 0, FALSE, &bResConf);
RtlZeroBytes(&(pEramExt->MapAdr), sizeof(pEramExt->MapAdr));
}
}
/* EramReportEvent
System Event Log Output.
Parameters
pIoObject Device object pDevObj or driver object pDrvObj.
ntErrorCode The event ID.
pszString The string to be appended. NULL if omitted.
Return Value
Results.
*/
BOOLEAN EramReportEvent(
IN PVOID pIoObject,
IN NTSTATUS ntErrorCode,
IN PSTR pszString
)
{
/* local variables */
ANSI_STRING AnsiStr;
UNICODE_STRING UniStr;
BOOLEAN bStat;
if ((pszString != NULL)&&
(*pszString != L'\0'))
{
RtlInitAnsiString(&AnsiStr, pszString);
/* UNICODE-stringify and dump */
if (RtlAnsiStringToUnicodeString(&UniStr, &AnsiStr, TRUE) == STATUS_SUCCESS)
{
bStat = EramReportEventW(pIoObject, ntErrorCode, UniStr.Buffer);
RtlFreeUnicodeString(&UniStr);
return bStat;
}
}
return EramReportEventW(pIoObject, ntErrorCode, NULL);
}
/* EramReportEventW
System Event Log Output.
Parameters
pIoObject Device object pDevObj or driver object pDrvObj.
ntErrorCode The event ID.
pwStr The Unicode string to be appended. NULL if omitted.
Return Value
Results.
*/
BOOLEAN EramReportEventW(
IN PVOID pIoObject,
IN NTSTATUS ntErrorCode,
IN PWSTR pwStr
)
{
/* local variables */
PIO_ERROR_LOG_PACKET pPacket;
ULONG uSize;
UNICODE_STRING UniStr;
KdPrint(("EramReportEventW start, event:%ls\n", (PWSTR)((pwStr != NULL) ? pwStr : (PWSTR)(L""))));
/* packet size initialization */
uSize = sizeof(IO_ERROR_LOG_PACKET);
if (pwStr != NULL) /* With appending string */
{
RtlInitUnicodeString(&UniStr, pwStr);
/* packet size adding */
uSize += (UniStr.Length + sizeof(WCHAR));
}
if (uSize > ERROR_LOG_MAXIMUM_SIZE) /* string too long */
{
KdPrint(("Eram String too long\n"));
return FALSE;
}
/* packet allocation */
pPacket = IoAllocateErrorLogEntry(pIoObject, (UCHAR)uSize);
if (pPacket == NULL) /* allocation failed */
{
KdPrint(("Eram IoAllocateErrorLogEntry failed\n"));
return FALSE;
}
/* standard data part initialization */
RtlZeroBytes(pPacket, uSize);
pPacket->ErrorCode = ntErrorCode;
if (pwStr != NULL) /* with appending string */
{
/* Set the number of strings */
pPacket->NumberOfStrings = 1;
/* Set the starting position of string */
pPacket->StringOffset = sizeof(IO_ERROR_LOG_PACKET);
/* Copy the Unicode string */
RtlCopyBytes(&(pPacket[1]), UniStr.Buffer, UniStr.Length);
}
/* Log output */
IoWriteErrorLogEntry(pPacket);
KdPrint(("EramReportEventW end\n"));
return TRUE;
}
/* ReadPool
OS-Managed Memory Reading.
Parameters
pEramExt The pointer to an ERAM_EXTENTION structure.
pIrp The pointer to IRP packet.
pIrpSp The pointer to stack info.
lpDest The pointer to storage area.
Return Value
Results.
*/
NTSTATUS ReadPool(
IN PERAM_EXTENSION pEramExt,
IN PIRP pIrp,
IN PIO_STACK_LOCATION pIrpSp,
IN PUCHAR lpDest
)
{
/* local variables */
PUCHAR lpSrc;
lpSrc = (PUCHAR)((PBYTE)pEramExt->pPageBase + (ULONG)pIrpSp->Parameters.Read.ByteOffset.QuadPart);
RtlCopyBytes(lpDest, lpSrc, pIrpSp->Parameters.Read.Length);
return STATUS_SUCCESS;
}
/* WritePool
OS-Managed Memory Writing.
Parameters
pEramExt The pointer to an ERAM_EXTENTION structure.
pIrp The pointer to IRP packet.
pIrpSp The pointer to stack info.
lpSrc The pointer to data area.
Return Value
Results.
*/
NTSTATUS WritePool(
IN PERAM_EXTENSION pEramExt,
IN PIRP pIrp,
IN PIO_STACK_LOCATION pIrpSp,
IN PUCHAR lpSrc
)
{
/* local variables */
PUCHAR lpDest;
lpDest = (PUCHAR)((PBYTE)pEramExt->pPageBase + (ULONG)pIrpSp->Parameters.Write.ByteOffset.QuadPart);
RtlCopyBytes(lpDest, lpSrc, pIrpSp->Parameters.Write.Length);
return STATUS_SUCCESS;
}
/* ExtRead1
OS-Unmanaged Memory Reading (without check).
Parameters
pEramExt The pointer to an ERAM_EXTENTION structure.
pIrp The pointer to IRP packet.
pIrpSp The pointer to stack info.
lpDest The pointer to storage area.
Return Value
Results.
*/
NTSTATUS ExtRead1(
IN PERAM_EXTENSION pEramExt,
IN PIRP pIrp,
IN PIO_STACK_LOCATION pIrpSp,
IN PUCHAR lpDest
)
{
/* local variables */
PUCHAR lpSrc;
UINT uLen;
DWORD eax, ebx;
NTSTATUS ntStat;
SIZE_T uMemAdr;
ASSERT(pEramExt->uExternalStart != 0);
ASSERT(pEramExt->uExternalEnd != 0);
/* Mutex wait */
ExAcquireFastMutex(&(pEramExt->FastMutex));
uLen = pIrpSp->Parameters.Read.Length; /* Transfer size (a multiples of sector size) */
/* Calculate the sector number */
ebx = pIrpSp->Parameters.Read.ByteOffset.LowPart >> SECTOR_LOG2;
/* Calculate the memory position */
uMemAdr = pEramExt->uExternalStart + pIrpSp->Parameters.Read.ByteOffset.QuadPart;
ntStat = STATUS_SUCCESS;
while (uLen != 0)
{
if (uMemAdr >= pEramExt->uExternalEnd) /* Beyond real memory */
{
ntStat = STATUS_DISK_CORRUPT_ERROR;
break;
}
/* 64KB allocation */
if (ExtNext1(pEramExt, &eax, &ebx) == FALSE)
{
EramReportEvent(pEramExt->pDevObj, ERAM_ERROR_FUNCTIONERROR, "ExtNext1");
ntStat = STATUS_DISK_CORRUPT_ERROR;
break;
}
lpSrc = (PUCHAR)((pEramExt->pExtPage + eax)); //(ULONG)
/* data transfer */
RtlCopyBytes(lpDest, lpSrc, SECTOR);
lpDest += SECTOR;
uLen -= SECTOR;
uMemAdr += SECTOR;
}
/* Unmap */
ExtUnmap(pEramExt);
/* Mutex release */
ExReleaseFastMutex(&(pEramExt->FastMutex));
return ntStat;
}
/* ExtWrite1
OS-Unmanaged Memory Writing (without check).
Parameters
pEramExt The pointer to an ERAM_EXTENTION structure.
pIrp The pointer to IRP packet.
pIrpSp The pointer to stack info.
lpSrc The pointer to data area.
Return Value
Results.
*/
NTSTATUS ExtWrite1(
IN PERAM_EXTENSION pEramExt,
IN PIRP pIrp,
IN PIO_STACK_LOCATION pIrpSp,
IN PUCHAR lpSrc
)
{
/* local variables */
PUCHAR lpDest;
UINT uLen;
DWORD eax, ebx;
NTSTATUS ntStat;
ULONGPTR uMemAdr;
ASSERT(pEramExt->uExternalStart != 0);
ASSERT(pEramExt->uExternalEnd != 0);
/* Mutex wait */
ExAcquireFastMutex(&(pEramExt->FastMutex));
uLen = pIrpSp->Parameters.Write.Length;
/* Calculate the sector number */
ebx = pIrpSp->Parameters.Write.ByteOffset.LowPart >> SECTOR_LOG2;
/* Calculate the memory position */
uMemAdr = pEramExt->uExternalStart + pIrpSp->Parameters.Write.ByteOffset.LowPart;
ntStat = STATUS_SUCCESS;
while (uLen != 0)
{
if (uMemAdr >= pEramExt->uExternalEnd) /* Beyond real memory */
{
ntStat = STATUS_DISK_CORRUPT_ERROR;
break;
}
/* 64KB allocation */
if (ExtNext1(pEramExt, &eax, &ebx) == FALSE)
{
EramReportEvent(pEramExt->pDevObj, ERAM_ERROR_FUNCTIONERROR, "ExtNext1");
ntStat = STATUS_DISK_CORRUPT_ERROR;
break;
}
lpDest = (PUCHAR)((pEramExt->pExtPage + eax)); //(ULONG)
/* data transfer */
RtlCopyBytes(lpDest, lpSrc, SECTOR);
lpSrc += SECTOR;
uLen -= SECTOR;
uMemAdr += SECTOR;
}
/* Unmap */
ExtUnmap(pEramExt);
/* Mutex release */
ExReleaseFastMutex(&(pEramExt->FastMutex));
return ntStat;
}
/* ExtNext1
OS-Unmanaged: The corresponding sector allocation (without check)
Parameters
pEramExt The pointer to an ERAM_EXTENTION structure.
lpeax The pointer to the area to return the inner page offset.
lpebx The pointer to the sector number (incremented).
Return Value
Results.
*/
BOOLEAN ExtNext1(
IN PERAM_EXTENSION pEramExt,
IN OUT LPDWORD lpeax,
IN OUT LPDWORD lpebx
)
{
/* local variables */
DWORD eax, ebx, uMapAdr;
ebx = *lpebx;
/* calculate the bank number to be mapped */
uMapAdr = (ebx >> EXT_PAGE_SEC_LOG2) << EXT_PAGE_SIZE_LOG2;
/* map */
if (ExtMap(pEramExt, uMapAdr) == FALSE)