00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
#include "cmp.h"
00026
00027
#ifdef ALLOC_PRAGMA
00028
#pragma alloc_text(PAGE,HvpBuildMap)
00029
#pragma alloc_text(PAGE,HvpFreeMap)
00030
#pragma alloc_text(PAGE,HvpAllocateMap)
00031
#pragma alloc_text(PAGE,HvpBuildMapAndCopy)
00032
#pragma alloc_text(PAGE,HvpEnlistFreeCells)
00033
#pragma alloc_text(PAGE,HvpInitMap)
00034
#pragma alloc_text(PAGE,HvpCleanMap)
00035
#pragma alloc_text(PAGE,HvpEnlistBinInMap)
00036
#endif
00037
00038
extern struct {
00039 PHHIVE Hive;
00040 ULONG
Status;
00041 ULONG
Space;
00042 HCELL_INDEX MapPoint;
00043 PHBIN BinPoint;
00044 }
HvCheckHiveDebug;
00045
00046
NTSTATUS
00047 HvpBuildMapAndCopy(
00048
PHHIVE Hive,
00049 PVOID Image,
00050 PHCELL_INDEX TailDisplay OPTIONAL
00051 )
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 {
00085
PHBASE_BLOCK BaseBlock;
00086 ULONG Length;
00087 ULONG MapSlots;
00088 ULONG Tables;
00089
PHMAP_TABLE t =
NULL;
00090
PHMAP_DIRECTORY d =
NULL;
00091
PHBIN Bin;
00092
PHBIN NewBins;
00093
PHBIN CurrentBin;
00094 ULONG
Offset;
00095 ULONG_PTR Address;
00096
PHMAP_ENTRY Me;
00097
NTSTATUS Status;
00098 PULONG Vector;
00099 ULONG
Size;
00100
00101
00102
CMLOG(
CML_FLOW,
CMS_HIVE) {
00103 KdPrint((
"HvpBuildMap:\n"));
00104 KdPrint((
"\tHive=%08lx",
Hive));
00105 }
00106
00107
00108
00109
00110
00111 BaseBlock =
Hive->
BaseBlock;
00112 Length = BaseBlock->
Length;
00113
if ((Length %
HBLOCK_SIZE) != 0 ) {
00114
Status = STATUS_REGISTRY_CORRUPT;
00115
goto ErrorExit1;
00116 }
00117 MapSlots = Length /
HBLOCK_SIZE;
00118
if( MapSlots > 0 ) {
00119 Tables = (MapSlots-1) /
HTABLE_SLOTS;
00120 }
else {
00121 Tables = 0;
00122 }
00123
00124
Hive->Storage[
Stable].Length = Length;
00125
00126
00127
00128
00129
00130
if (
Hive->
DirtyVector.Buffer ==
NULL) {
00131 Vector = (PULONG)((
Hive->
Allocate)(
ROUND_UP(Length/
HSECTOR_SIZE/8,
sizeof(ULONG)),
TRUE));
00132
if (Vector ==
NULL) {
00133
Status = STATUS_NO_MEMORY;
00134
goto ErrorExit1;
00135 }
00136 RtlZeroMemory(Vector, Length /
HSECTOR_SIZE / 8);
00137
RtlInitializeBitMap(&
Hive->
DirtyVector, Vector, Length /
HSECTOR_SIZE);
00138
Hive->
DirtyAlloc = (Length/
HSECTOR_SIZE/8);
00139 }
00140
00141
00142
00143
00144
if (Tables == 0) {
00145
00146
00147
00148
00149
t = (
Hive->
Allocate)(
sizeof(
HMAP_TABLE),
FALSE);
00150
if (
t ==
NULL) {
00151
Status = STATUS_INSUFFICIENT_RESOURCES;
00152
goto ErrorExit1;
00153 }
00154 RtlZeroMemory(
t,
sizeof(
HMAP_TABLE));
00155
Hive->Storage[
Stable].Map =
00156 (
PHMAP_DIRECTORY)&(
Hive->Storage[
Stable].SmallDir);
00157
Hive->Storage[
Stable].SmallDir =
t;
00158
00159 }
else {
00160
00161
00162
00163
00164 d = (
PHMAP_DIRECTORY)(
Hive->
Allocate)(
sizeof(
HMAP_DIRECTORY),
FALSE);
00165
if (d ==
NULL) {
00166
Status = STATUS_INSUFFICIENT_RESOURCES;
00167
goto ErrorExit1;
00168 }
00169 RtlZeroMemory(d,
sizeof(
HMAP_DIRECTORY));
00170
00171
00172
00173
00174
if (
HvpAllocateMap(
Hive, d, 0, Tables) ==
FALSE) {
00175
Status = STATUS_INSUFFICIENT_RESOURCES;
00176
goto ErrorExit2;
00177 }
00178
Hive->Storage[
Stable].Map = d;
00179
Hive->Storage[
Stable].SmallDir = 0;
00180 }
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
Offset = 0;
00196
Size = 0;
00197
Bin = (
PHBIN)Image;
00198
00199
while (
Bin < (
PHBIN)((PUCHAR)(Image) + Length)) {
00200
00201
if ( (
Bin->
Size > (Length-
Offset)) ||
00202 (
Bin->
Signature !=
HBIN_SIGNATURE) ||
00203 (
Bin->
FileOffset != (
Offset+
Size))
00204 )
00205 {
00206
00207
00208
00209
Status = STATUS_REGISTRY_CORRUPT;
00210
goto ErrorExit2;
00211 }
00212
00213
Size +=
Bin->
Size;
00214
if ((
Size <
PAGE_SIZE) &&
00215 (
Size + Length -
Offset >
PAGE_SIZE)) {
00216
00217
00218
00219
00220
00221
00222
Bin = (
PHBIN)((ULONG_PTR)
Bin +
Bin->
Size);
00223
00224
continue;
00225
00226 }
00227
00228
00229
00230
00231
00232 NewBins = (
PHBIN)(
Hive->
Allocate)(
Size,
FALSE);
00233
if (NewBins==
NULL) {
00234
Status = STATUS_INSUFFICIENT_RESOURCES;
00235
goto ErrorExit2;
00236 }
00237 RtlCopyMemory(NewBins,
00238 (PUCHAR)Image+
Offset,
00239
Size);
00240 NewBins->
MemAlloc =
Size;
00241
00242
00243
00244
00245 Address = (ULONG_PTR)NewBins;
00246
do {
00247 CurrentBin = (
PHBIN)Address;
00248
do {
00249 Me =
HvpGetCellMap(
Hive,
Offset);
00250
VALIDATE_CELL_MAP(__LINE__,Me,
Hive,
Offset);
00251 Me->
BlockAddress = Address;
00252 Me->
BinAddress = (ULONG_PTR)CurrentBin;
00253
00254
if (CurrentBin == NewBins) {
00255 Me->
BinAddress |=
HMAP_NEWALLOC;
00256 }
else {
00257 CurrentBin->
MemAlloc = 0;
00258 }
00259 Address +=
HBLOCK_SIZE;
00260
Offset +=
HBLOCK_SIZE;
00261 }
while ( Address < ((ULONG_PTR)CurrentBin + CurrentBin->
Size ));
00262
00263
if (
Hive->
ReadOnly ==
FALSE) {
00264
00265
00266
00267
00268
if ( !
HvpEnlistFreeCells(
Hive,
00269 CurrentBin,
00270 CurrentBin->
FileOffset,
00271 TailDisplay
00272 )) {
00273
Status = STATUS_REGISTRY_CORRUPT;
00274
goto ErrorExit2;
00275 }
00276
00277 }
00278
00279 }
while ( Address < (ULONG_PTR)NewBins +
Size );
00280
00281
Bin = (
PHBIN)((ULONG_PTR)
Bin +
Bin->
Size);
00282
Size = 0;
00283 }
00284
00285
return STATUS_SUCCESS;
00286
00287
00288 ErrorExit2:
00289
if (d !=
NULL) {
00290
00291
00292
00293
00294
00295
HvpFreeMap(
Hive, d, 0, Tables);
00296 (
Hive->
Free)(d,
sizeof(
HMAP_DIRECTORY));
00297 }
00298
00299 ErrorExit1:
00300
return Status;
00301 }
00302
00303
NTSTATUS
00304 HvpInitMap(
00305
PHHIVE Hive
00306 )
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329 {
00330
PHBASE_BLOCK BaseBlock;
00331 ULONG Length;
00332 ULONG MapSlots;
00333 ULONG Tables;
00334
PHMAP_TABLE t =
NULL;
00335
PHMAP_DIRECTORY d =
NULL;
00336
NTSTATUS Status;
00337 PULONG Vector;
00338
00339
00340
CMLOG(
CML_FLOW,
CMS_HIVE) {
00341 KdPrint((
"HvpInitMap:\n"));
00342 KdPrint((
"\tHive=%08lx",
Hive));
00343 }
00344
00345
00346
00347
00348 BaseBlock =
Hive->
BaseBlock;
00349 Length = BaseBlock->
Length;
00350
if ((Length %
HBLOCK_SIZE) != 0) {
00351
Status = STATUS_REGISTRY_CORRUPT;
00352
goto ErrorExit1;
00353 }
00354 MapSlots = Length /
HBLOCK_SIZE;
00355
if( MapSlots > 0 ) {
00356 Tables = (MapSlots-1) /
HTABLE_SLOTS;
00357 }
else {
00358 Tables = 0;
00359 }
00360
00361
Hive->Storage[
Stable].Length = Length;
00362
00363
00364
00365
00366
00367
if (
Hive->
DirtyVector.Buffer ==
NULL) {
00368 Vector = (PULONG)((
Hive->
Allocate)(
ROUND_UP(Length/
HSECTOR_SIZE/8,
sizeof(ULONG)),
TRUE));
00369
if (Vector ==
NULL) {
00370
Status = STATUS_NO_MEMORY;
00371
goto ErrorExit1;
00372 }
00373 RtlZeroMemory(Vector, Length /
HSECTOR_SIZE / 8);
00374
RtlInitializeBitMap(&
Hive->
DirtyVector, Vector, Length /
HSECTOR_SIZE);
00375
Hive->
DirtyAlloc = (Length/
HSECTOR_SIZE/8);
00376 }
00377
00378
00379
00380
00381
if (Tables == 0) {
00382
00383
00384
00385
00386
t = (
Hive->
Allocate)(
sizeof(
HMAP_TABLE),
FALSE);
00387
if (
t ==
NULL) {
00388
Status = STATUS_INSUFFICIENT_RESOURCES;
00389
goto ErrorExit1;
00390 }
00391 RtlZeroMemory(
t,
sizeof(
HMAP_TABLE));
00392
Hive->Storage[
Stable].Map =
00393 (
PHMAP_DIRECTORY)&(
Hive->Storage[
Stable].SmallDir);
00394
Hive->Storage[
Stable].SmallDir =
t;
00395
00396 }
else {
00397
00398
00399
00400
00401 d = (
PHMAP_DIRECTORY)(
Hive->
Allocate)(
sizeof(
HMAP_DIRECTORY),
FALSE);
00402
if (d ==
NULL) {
00403
Status = STATUS_INSUFFICIENT_RESOURCES;
00404
goto ErrorExit1;
00405 }
00406 RtlZeroMemory(d,
sizeof(
HMAP_DIRECTORY));
00407
00408
00409
00410
00411
if (
HvpAllocateMap(
Hive, d, 0, Tables) ==
FALSE) {
00412
Status = STATUS_INSUFFICIENT_RESOURCES;
00413
goto ErrorExit2;
00414 }
00415
Hive->Storage[
Stable].Map = d;
00416
Hive->Storage[
Stable].SmallDir = 0;
00417 }
00418
00419
return STATUS_SUCCESS;
00420
00421 ErrorExit2:
00422
if (d !=
NULL) {
00423
00424
00425
00426
00427
00428
HvpFreeMap(
Hive, d, 0, Tables);
00429 (
Hive->
Free)(d,
sizeof(
HMAP_DIRECTORY));
00430 }
00431
00432 ErrorExit1:
00433
return Status;
00434 }
00435
00436
NTSTATUS
00437 HvpEnlistBinInMap(
00438
PHHIVE Hive,
00439 ULONG Length,
00440
PHBIN Bin,
00441 ULONG Offset,
00442 PHCELL_INDEX TailDisplay OPTIONAL
00443 )
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468 {
00469
NTSTATUS Status;
00470 ULONG BinOffset;
00471 ULONG_PTR Address;
00472
PHMAP_ENTRY Me;
00473
00474
CMLOG(
CML_FLOW,
CMS_HIVE) {
00475 KdPrint((
"HvpEnlistBinInMap:\n"));
00476 KdPrint((
"\tHive=%08lx\t Offset=%08lx",
Hive,
Offset));
00477 }
00478
00479
00480
00481
00482
Bin->
MemAlloc =
Bin->
Size;
00483
00484
CMLOG(
CML_BIN,
CMS_BIN_MAP) {
00485 KdPrint((
"HvpEnlistBinInMap: BinAddress = 0x%08lx\t Size = 0x%lx\n",
Bin,
Bin->
Size));
00486 }
00487
00488
00489
00490
00491 BinOffset =
Offset;
00492
for (Address = (ULONG_PTR)
Bin;
00493 Address < ((ULONG_PTR)
Bin +
Bin->
Size);
00494 Address +=
HBLOCK_SIZE
00495 )
00496 {
00497 Me =
HvpGetCellMap(
Hive,
Offset);
00498
VALIDATE_CELL_MAP(__LINE__,Me,
Hive,
Offset);
00499 Me->
BlockAddress = Address;
00500 Me->
BinAddress = (ULONG_PTR)
Bin;
00501
if (
Offset == BinOffset) {
00502 Me->
BinAddress |=
HMAP_NEWALLOC;
00503 }
00504
Offset +=
HBLOCK_SIZE;
00505 }
00506
00507
if (
Hive->
ReadOnly ==
FALSE) {
00508
00509
00510
00511
00512
if ( !
HvpEnlistFreeCells(
Hive,
Bin, BinOffset,TailDisplay)) {
00513
HvCheckHiveDebug.Hive =
Hive;
00514
HvCheckHiveDebug.Status = 0xA002;
00515
HvCheckHiveDebug.Space = Length;
00516
HvCheckHiveDebug.MapPoint = BinOffset;
00517
HvCheckHiveDebug.BinPoint =
Bin;
00518
Status = STATUS_REGISTRY_CORRUPT;
00519
goto ErrorExit;
00520 }
00521
00522 }
00523
00524
00525
00526
00527
ASSERT(
Offset == (BinOffset +
Bin->
Size));
00528
00529
return STATUS_SUCCESS;
00530
00531
ErrorExit:
00532
return Status;
00533 }
00534
00535
NTSTATUS
00536 HvpBuildMap(
00537
PHHIVE Hive,
00538 PVOID Image,
00539 PHCELL_INDEX TailDisplay OPTIONAL
00540 )
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566 {
00567
PHBIN Bin;
00568 ULONG
Offset;
00569
NTSTATUS Status;
00570 ULONG Length;
00571
00572
00573
CMLOG(
CML_FLOW,
CMS_HIVE) {
00574 KdPrint((
"HvpBuildMap:\n"));
00575 KdPrint((
"\tHive=%08lx",
Hive));
00576 }
00577
00578
00579
00580
00581
Status =
HvpInitMap(
Hive);
00582
00583
if( !
NT_SUCCESS(
Status) ) {
00584
00585
00586
00587
return Status;
00588 }
00589
00590
00591
00592
00593
Offset = 0;
00594
Bin = (
PHBIN)Image;
00595 Length =
Hive->Storage[
Stable].Length;
00596
00597
while (
Bin < (
PHBIN)((PUCHAR)(Image) + Length)) {
00598
00599
00600
00601
00602
if ( (
Bin->
Size > Length) ||
00603 (
Bin->
Size <
HBLOCK_SIZE) ||
00604 (
Bin->
Signature !=
HBIN_SIGNATURE) ||
00605 (
Bin->
FileOffset !=
Offset)) {
00606
00607
00608
00609
Status = STATUS_REGISTRY_CORRUPT;
00610
HvCheckHiveDebug.Hive =
Hive;
00611
HvCheckHiveDebug.Status = 0xA001;
00612
HvCheckHiveDebug.Space = Length;
00613
HvCheckHiveDebug.MapPoint =
Offset;
00614
HvCheckHiveDebug.BinPoint =
Bin;
00615
goto ErrorExit;
00616 }
00617
00618
00619
00620
00621
Status =
HvpEnlistBinInMap(
Hive, Length,
Bin,
Offset, TailDisplay);
00622
00623
if( !
NT_SUCCESS(
Status) ) {
00624
goto ErrorExit;
00625 }
00626
00627
00628
00629
00630
Offset +=
Bin->
Size;
00631
00632
Bin = (
PHBIN)((ULONG_PTR)
Bin +
Bin->
Size);
00633 }
00634
00635
return STATUS_SUCCESS;
00636
00637
00638
ErrorExit:
00639
00640
00641
00642
HvpCleanMap(
Hive );
00643
00644
return Status;
00645 }
00646
00647
00648 BOOLEAN
00649 HvpEnlistFreeCells(
00650
PHHIVE Hive,
00651
PHBIN Bin,
00652 ULONG BinOffset,
00653 PHCELL_INDEX TailDisplay OPTIONAL
00654 )
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679 {
00680
PHCELL p;
00681 ULONG celloffset;
00682 ULONG size;
00683
HCELL_INDEX cellindex;
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694 celloffset =
sizeof(
HBIN);
00695 p = (
PHCELL)((PUCHAR)
Bin +
sizeof(
HBIN));
00696
00697
while (p < (
PHCELL)((PUCHAR)
Bin +
Bin->
Size)) {
00698
00699
00700
00701
00702
if (p->
Size >= 0) {
00703
00704 size = (ULONG)p->
Size;
00705
00706
if ( (size >
Bin->
Size) ||
00707 ( (
PHCELL)(size + (PUCHAR)p) >
00708 (
PHCELL)((PUCHAR)
Bin +
Bin->
Size) ) ||
00709 ((size %
HCELL_PAD(
Hive)) != 0) ||
00710 (size == 0) )
00711 {
00712
return FALSE;
00713 }
00714
00715
00716
00717
00718
00719 celloffset = (ULONG)((PUCHAR)p - (PUCHAR)
Bin);
00720 cellindex = BinOffset + celloffset;
00721
00722
00723
00724
00725
00726
HvpEnlistFreeCell(
Hive, cellindex, size,
Stable,
FALSE,TailDisplay);
00727
00728 }
else {
00729
00730 size = (ULONG)(p->
Size * -1);
00731
00732
if ( (size >
Bin->
Size) ||
00733 ( (
PHCELL)(size + (PUCHAR)p) >
00734 (
PHCELL)((PUCHAR)
Bin +
Bin->
Size) ) ||
00735 ((size %
HCELL_PAD(
Hive)) != 0) ||
00736 (size == 0) )
00737 {
00738
return FALSE;
00739 }
00740
00741 }
00742
00743
ASSERT(size >= 0);
00744 p = (
PHCELL)((PUCHAR)p + size);
00745 }
00746
00747
return TRUE;
00748 }
00749
00750
VOID
00751 HvpCleanMap(
00752
PHHIVE Hive
00753 )
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768 {
00769 ULONG Length;
00770 ULONG MapSlots;
00771 ULONG Tables;
00772
PHMAP_DIRECTORY d =
NULL;
00773
00774
00775
00776
00777 Length =
Hive->Storage[
Stable].Length;
00778 MapSlots = Length /
HBLOCK_SIZE;
00779
if( MapSlots > 0 ) {
00780 Tables = (MapSlots-1) /
HTABLE_SLOTS;
00781 }
else {
00782 Tables = 0;
00783 }
00784
00785
if(
Hive->Storage[
Stable].SmallDir == 0 ) {
00786
00787
00788
00789
00790 d =
Hive->Storage[
Stable].Map;
00791
if( d !=
NULL ) {
00792
HvpFreeMap(
Hive, d, 0, Tables);
00793 (
Hive->
Free)(d,
sizeof(
HMAP_DIRECTORY));
00794 }
00795 }
else {
00796
00797
00798
00799 (
Hive->
Free)(
Hive->Storage[
Stable].SmallDir,
sizeof(
HMAP_TABLE));
00800 }
00801
00802
Hive->Storage[
Stable].SmallDir =
NULL;
00803
Hive->Storage[
Stable].Map =
NULL;
00804 }
00805
00806
VOID
00807 HvpFreeMap(
00808
PHHIVE Hive,
00809
PHMAP_DIRECTORY Dir,
00810 ULONG Start,
00811 ULONG End
00812 )
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835 {
00836 ULONG i;
00837
00838
if (
End >=
HDIRECTORY_SLOTS) {
00839
End =
HDIRECTORY_SLOTS - 1;
00840 }
00841
00842
for (i =
Start; i <=
End; i++) {
00843
if (Dir->
Directory[i] !=
NULL) {
00844 (
Hive->
Free)(Dir->
Directory[i],
sizeof(
HMAP_TABLE));
00845 Dir->
Directory[i] =
NULL;
00846 }
00847 }
00848
return;
00849 }
00850
00851
00852 BOOLEAN
00853 HvpAllocateMap(
00854
PHHIVE Hive,
00855
PHMAP_DIRECTORY Dir,
00856 ULONG Start,
00857 ULONG End
00858 )
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884 {
00885 ULONG i;
00886 PVOID
t;
00887
00888
for (i =
Start; i <=
End; i++) {
00889
ASSERT(Dir->
Directory[i] ==
NULL);
00890
t = (PVOID)((
Hive->
Allocate)(
sizeof(
HMAP_TABLE),
FALSE));
00891
if (
t ==
NULL) {
00892
return FALSE;
00893 }
00894 RtlZeroMemory(
t,
sizeof(
HMAP_TABLE));
00895 Dir->
Directory[i] = (
PHMAP_TABLE)
t;
00896 }
00897
return TRUE;
00898 }