forked from ufrisk/LeechCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleechcore.c
1170 lines (1110 loc) · 40.7 KB
/
leechcore.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
// leechcore.c : core implementation of the the LeechCore physical memory acquisition library.
//
// (c) Ulf Frisk, 2020-2022
// Author: Ulf Frisk, [email protected]
//
#include "leechcore.h"
#include "leechcore_device.h"
#include "leechcore_internal.h"
#include "oscompatibility.h"
#include "util.h"
#include "version.h"
//-----------------------------------------------------------------------------
// Global Context and DLL Attach/Detach:
//-----------------------------------------------------------------------------
typedef struct tdLC_MAIN_CONTEXT {
CRITICAL_SECTION Lock;
HANDLE FLink;
} LC_MAIN_CONTEXT, *PLC_MAIN_CONTEXT;
LC_MAIN_CONTEXT g_ctx = { 0 };
_Success_(return) BOOL Device3380_Open(_Inout_ PLC_CONTEXT ctxLC, _Out_opt_ PPLC_CONFIG_ERRORINFO ppLcCreateErrorInfo);
_Success_(return) BOOL DeviceFile_Open(_Inout_ PLC_CONTEXT ctxLC, _Out_opt_ PPLC_CONFIG_ERRORINFO ppLcCreateErrorInfo);
_Success_(return) BOOL DeviceFPGA_Open(_Inout_ PLC_CONTEXT ctxLC, _Out_opt_ PPLC_CONFIG_ERRORINFO ppLcCreateErrorInfo);
_Success_(return) BOOL DevicePMEM_Open(_Inout_ PLC_CONTEXT ctxLC, _Out_opt_ PPLC_CONFIG_ERRORINFO ppLcCreateErrorInfo);
_Success_(return) BOOL DeviceVMWare_Open(_Inout_ PLC_CONTEXT ctxLC, _Out_opt_ PPLC_CONFIG_ERRORINFO ppLcCreateErrorInfo);
_Success_(return) BOOL DeviceTMD_Open(_Inout_ PLC_CONTEXT ctxLC, _Out_opt_ PPLC_CONFIG_ERRORINFO ppLcCreateErrorInfo);
_Success_(return) BOOL LeechRpc_Open(_Inout_ PLC_CONTEXT ctxLC, _Out_opt_ PPLC_CONFIG_ERRORINFO ppLcCreateErrorInfo);
VOID LcCloseAll();
_Success_(return) BOOL LcReadContigious_Initialize(_In_ PLC_CONTEXT ctxLC);
VOID LcReadContigious_Close(_In_ PLC_CONTEXT ctxLC);
#ifdef _WIN32
BOOL WINAPI DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ PVOID lpvReserved)
{
if(fdwReason == DLL_PROCESS_ATTACH) {
ZeroMemory(&g_ctx, sizeof(LC_MAIN_CONTEXT));
InitializeCriticalSection(&g_ctx.Lock);
}
if(fdwReason == DLL_PROCESS_DETACH) {
LcCloseAll();
DeleteCriticalSection(&g_ctx.Lock);
ZeroMemory(&g_ctx, sizeof(LC_MAIN_CONTEXT));
}
return TRUE;
}
#endif /* _WIN32 */
#ifdef LINUX
__attribute__((constructor)) VOID LcAttach()
{
ZeroMemory(&g_ctx, sizeof(LC_MAIN_CONTEXT));
InitializeCriticalSection(&g_ctx.Lock);
}
__attribute__((destructor)) VOID LcDetach()
{
LcCloseAll();
DeleteCriticalSection(&g_ctx.Lock);
ZeroMemory(&g_ctx, sizeof(LC_MAIN_CONTEXT));
}
#endif /* LINUX */
//-----------------------------------------------------------------------------
// Initialize / Close / Core functionality:
//-----------------------------------------------------------------------------
VOID LcLockAcquire(_In_ PLC_CONTEXT ctxLC)
{
if(!ctxLC->fMultiThread) { EnterCriticalSection(&ctxLC->Lock); }
}
VOID LcLockRelease(_In_ PLC_CONTEXT ctxLC)
{
if(!ctxLC->fMultiThread) { LeaveCriticalSection(&ctxLC->Lock); }
}
QWORD LcCallStart()
{
QWORD tmNow;
QueryPerformanceCounter((PLARGE_INTEGER)&tmNow);
return tmNow;
}
VOID LcCallEnd(_In_ PLC_CONTEXT ctxLC, _In_ DWORD fId, _In_ QWORD tmCallStart)
{
QWORD tmNow;
QueryPerformanceCounter((PLARGE_INTEGER)&tmNow);
InterlockedIncrement64(&ctxLC->CallStat.Call[fId].c);
InterlockedAdd64(&ctxLC->CallStat.Call[fId].tm, tmNow - tmCallStart);
}
/*
* Close a LeechCore handle and free any resources no longer needed.
*/
EXPORTED_FUNCTION VOID LcClose(_In_opt_ _Post_ptr_invalid_ HANDLE hLC)
{
PLC_CONTEXT ctxParent;
PLC_CONTEXT ctxLC = (PLC_CONTEXT)hLC;
if(!ctxLC || (ctxLC->version != LC_CONTEXT_VERSION)) { return; }
EnterCriticalSection(&g_ctx.Lock);
if(0 == --ctxLC->dwHandleCount) {
// detach from handles list
if(g_ctx.FLink == ctxLC) {
g_ctx.FLink = ctxLC->FLink;
} else {
ctxParent = (PLC_CONTEXT)g_ctx.FLink;
while(ctxParent) {
if(ctxParent->FLink == ctxLC) {
ctxParent->FLink = ctxLC->FLink;
break;
}
ctxParent = (PLC_CONTEXT)ctxParent->FLink;
}
}
LcLockAcquire(ctxLC);
LcReadContigious_Close(ctxLC);
if(ctxLC->pfnClose) { ctxLC->pfnClose(ctxLC); }
LcLockRelease(ctxLC);
ctxLC->version = 0;
DeleteCriticalSection(&ctxLC->Lock);
if(ctxLC->hDeviceModule) { FreeLibrary(ctxLC->hDeviceModule); }
LocalFree(ctxLC->pMemMap);
LocalFree(ctxLC);
}
LeaveCriticalSection(&g_ctx.Lock);
}
/*
* Close all LeechCore devices and contexts. This is done on DLL unload.
*/
VOID LcCloseAll()
{
EnterCriticalSection(&g_ctx.Lock);
while(g_ctx.FLink) {
LcClose(g_ctx.FLink);
}
LeaveCriticalSection(&g_ctx.Lock);
}
/*
* Create helper function to parse optional device configuration parameters.
* -- ctxLC
*/
VOID LcCreate_FetchDeviceParameter(_Inout_ PLC_CONTEXT ctxLC)
{
PLC_DEVICE_PARAMETER_ENTRY pe;
CHAR szDevice[MAX_PATH] = { 0 };
LPSTR szDelim, szParameters, szToken, szTokenContext = NULL;
memcpy(szDevice, ctxLC->Config.szDevice, _countof(szDevice));
if(!(szParameters = strstr(szDevice, "://"))) { return; }
szParameters += 3;
while((szToken = strtok_s(szParameters, ",:;", &szTokenContext)) && (ctxLC->cDeviceParameter < LC_DEVICE_PARAMETER_MAX_ENTRIES)) {
szParameters = NULL;
if(!(szDelim = strstr(szToken, "="))) { continue; }
pe = &ctxLC->pDeviceParameter[ctxLC->cDeviceParameter];
strncpy_s(pe->szName, _countof(pe->szName), szToken, szDelim - szToken);
strncpy_s(pe->szValue, _countof(pe->szValue), szDelim + 1, _TRUNCATE);
pe->qwValue = Util_GetNumericA(pe->szValue);
if((0 == pe->qwValue) && !_stricmp(pe->szValue, "true")) {
pe->qwValue = 1;
}
ctxLC->cDeviceParameter++;
}
}
/*
* Retrieve a device parameter by its name (if exists).
* -- ctxLc
* -- szName
* -- return
*/
EXPORTED_FUNCTION PLC_DEVICE_PARAMETER_ENTRY LcDeviceParameterGet(_In_ PLC_CONTEXT ctxLC, _In_ LPSTR szName)
{
for(DWORD i = 0; i < ctxLC->cDeviceParameter; i++) {
if(!_stricmp(szName, ctxLC->pDeviceParameter[i].szName)) {
return &ctxLC->pDeviceParameter[i];
}
}
return NULL;
}
/*
* Retrieve the numeric value of a device parameter (if exists).
* -- ctxLc
* -- szName
* -- return = the numeric value of the device parameter - 0 on fail.
*/
EXPORTED_FUNCTION QWORD LcDeviceParameterGetNumeric(_In_ PLC_CONTEXT ctxLC, _In_ LPSTR szName)
{
PLC_DEVICE_PARAMETER_ENTRY p = LcDeviceParameterGet(ctxLC, szName);
return p ? p->qwValue : 0;
}
/*
* Create helper function to fetch the correct device (and its create function).
* -- ctxLC
*/
VOID LcCreate_FetchDevice(_Inout_ PLC_CONTEXT ctx)
{
CHAR c, szModule[2 * MAX_PATH] = { 0 };
DWORD cszDevice = 0;
// 1: check against built-in devices:
if(0 == _strnicmp("rpc://", ctx->Config.szRemote, 6)) {
strncpy_s(ctx->Config.szDeviceName, sizeof(ctx->Config.szDeviceName), "rpc", _TRUNCATE);
ctx->pfnCreate = LeechRpc_Open;
return;
}
if(0 == _strnicmp("pipe://", ctx->Config.szRemote, 7)) {
strncpy_s(ctx->Config.szDeviceName, sizeof(ctx->Config.szDeviceName), "pipe", _TRUNCATE);
ctx->pfnCreate = LeechRpc_Open;
return;
}
if(ctx->Config.szRemote[0]) { return; }
if((0 == _strnicmp("file", ctx->Config.szDevice, 4)) || (0 == _strnicmp("livekd", ctx->Config.szDevice, 6)) || (0 == _strnicmp("dumpit", ctx->Config.szDevice, 6))) {
strncpy_s(ctx->Config.szDeviceName, sizeof(ctx->Config.szDeviceName), "file", _TRUNCATE);
ctx->pfnCreate = DeviceFile_Open;
return;
}
if((0 == _strnicmp("fpga", ctx->Config.szDevice, 4)) || (0 == _strnicmp("rawudp://", ctx->Config.szDevice, 9))) {
strncpy_s(ctx->Config.szDeviceName, sizeof(ctx->Config.szDeviceName), "fpga", _TRUNCATE);
ctx->pfnCreate = DeviceFPGA_Open;
return;
}
if(0 == _strnicmp("usb3380", ctx->Config.szDevice, 7)) {
strncpy_s(ctx->Config.szDeviceName, sizeof(ctx->Config.szDeviceName), "usb3380", _TRUNCATE);
ctx->pfnCreate = Device3380_Open;
return;
}
if(0 == _stricmp("totalmeltdown", ctx->Config.szDevice)) {
strncpy_s(ctx->Config.szDeviceName, sizeof(ctx->Config.szDeviceName), "totalmeltdown", _TRUNCATE);
ctx->pfnCreate = DeviceTMD_Open;
return;
}
if(0 == _strnicmp("pmem", ctx->Config.szDevice, 4)) {
strncpy_s(ctx->Config.szDeviceName, sizeof(ctx->Config.szDeviceName), "pmem", _TRUNCATE);
ctx->pfnCreate = DevicePMEM_Open;
return;
}
if(0 == _strnicmp("vmware", ctx->Config.szDevice, 4)) {
strncpy_s(ctx->Config.szDeviceName, sizeof(ctx->Config.szDeviceName), "vmware", _TRUNCATE);
ctx->pfnCreate = DeviceVMWare_Open;
return;
}
// 2: check against separate device modules:
// 2.1: count device name length (and 'sanitize' againt disallowed chars).
while((c = ctx->Config.szDevice[cszDevice]) && (c != ':')) {
if(((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9'))) {
cszDevice++;
} else {
cszDevice = 0;
break;
}
}
// 2.2: try load module:
if(cszDevice && (cszDevice < 16)) {
Util_GetPathLib(szModule);
strcat_s(szModule, sizeof(szModule), "leechcore_device_");
strncat_s(szModule, sizeof(szModule), ctx->Config.szDevice, cszDevice);
strcat_s(szModule, sizeof(szModule), LC_LIBRARY_FILETYPE);
if((ctx->hDeviceModule = LoadLibraryA(szModule))) {
if((ctx->pfnCreate = (BOOL(*)(PLC_CONTEXT, PPLC_CONFIG_ERRORINFO))GetProcAddress(ctx->hDeviceModule, "LcPluginCreate"))) {
strncpy_s(ctx->Config.szDeviceName, sizeof(ctx->Config.szDeviceName), ctx->Config.szDevice, cszDevice);
return;
} else {
FreeLibrary(ctx->hDeviceModule);
ctx->hDeviceModule = NULL;
}
}
}
// 3: assume file is to be opened if no match for device name is found:
strncpy_s(ctx->Config.szDeviceName, sizeof(ctx->Config.szDeviceName), "file", _TRUNCATE);
ctx->pfnCreate = DeviceFile_Open;
}
#define ADDRDETECT_MAX 0x10
VOID LcCreate_MemMapInitAddressDetect_AddDefaultRange(_Inout_ PLC_CONTEXT ctxLC, _In_ QWORD paMax)
{
paMax = (paMax + 0xfff) & ~0xfff;
if(ctxLC->Config.fVolatile) {
LcMemMap_AddRange(ctxLC, 0, min(paMax, 0x000a0000), 0);
if(paMax > 0x00100000) {
LcMemMap_AddRange(ctxLC, 0x00100000, paMax - 0x00100000, 0x00100000);
}
} else {
LcMemMap_AddRange(ctxLC, 0, paMax, 0);
}
}
/*
* Create helper function to initialize memory map and auto-detect max address.
* -- ctxLC
*/
VOID LcCreate_MemMapInitAddressDetect(_Inout_ PLC_CONTEXT ctxLC)
{
BOOL fFPGA, fCheckTiny = FALSE;
PPMEM_SCATTER ppMEMs;
QWORD i, paCurrent = 0x100000000, cbChunk = 0x100000000;
if(LcMemMap_IsInitialized(ctxLC)) { return; }
if(ctxLC->Config.paMax) {
if(ctxLC->Config.paMax > 0x000000fffffff000) {
ctxLC->Config.paMax = 0x000000fffffff000;
}
LcCreate_MemMapInitAddressDetect_AddDefaultRange(ctxLC, ctxLC->Config.paMax);
return;
}
if(!LcAllocScatter1(ADDRDETECT_MAX + 1, &ppMEMs)) { return; }
// 1: detect topmost 4GB aligned address in 64GB scatter reads
while(TRUE) {
for(i = 0; i < ADDRDETECT_MAX; i++) {
ppMEMs[i]->qwA = paCurrent + i * cbChunk;
ppMEMs[i]->f = FALSE;
ppMEMs[i]->cb = 0x8;
}
LcReadScatter(ctxLC, ADDRDETECT_MAX, ppMEMs);
for(i = 0; i < ADDRDETECT_MAX; i++) {
if(ppMEMs[i]->f) {
paCurrent = ppMEMs[i]->qwA;
}
}
if(!ppMEMs[ADDRDETECT_MAX - 1]->f) {
break;
}
}
// 2: detect exact topmost address in progressively smaller scatter reads
fFPGA = (0 == _stricmp("fpga", ctxLC->Config.szDeviceName));
while(cbChunk > 0x1000) {
cbChunk = cbChunk >> 4;
if(fFPGA && (cbChunk == 0x1000)) {
// detect need for "tiny" PCIe algorithm of 128 bytes TLP.
ppMEMs[ADDRDETECT_MAX]->qwA = paCurrent;
fCheckTiny = TRUE;
}
for(i = 0; i < ADDRDETECT_MAX; i++) {
ppMEMs[i]->qwA = paCurrent + i * cbChunk;
ppMEMs[i]->f = FALSE;
}
LcReadScatter(ctxLC, ADDRDETECT_MAX + (fCheckTiny ? 0 : 1), ppMEMs);
for(i = 0; i < ADDRDETECT_MAX; i++) {
if(ppMEMs[i]->f) {
paCurrent = ppMEMs[i]->qwA;
}
}
if(fCheckTiny && !ppMEMs[ADDRDETECT_MAX]->f) {
ctxLC->pfnSetOption(ctxLC, LC_OPT_FPGA_ALGO_TINY, 1);
lcprintfv(ctxLC, "FPGA: TINY PCIe TLP algrithm auto-selected!\n");
}
}
// 3: finish
if(paCurrent == 0x100000000) { paCurrent -= 0x1000; }
LcCreate_MemMapInitAddressDetect_AddDefaultRange(ctxLC, paCurrent + 0x1000);
LocalFree(ppMEMs);
}
/*
* Create a new LeechCore device according to the supplied configuration.
* CALLER LcMemFree: ppLcCreateErrorInfo
* -- pLcCreateConfig
* -- ppLcCreateErrorInfo = ptr to receive function allocated struct with error
* information upon function failure. This info may contain a user message
* requesting user action as an example.
* -- return
*/
_Success_(return != NULL)
EXPORTED_FUNCTION HANDLE LcCreateEx(_Inout_ PLC_CONFIG pLcCreateConfig, _Out_opt_ PPLC_CONFIG_ERRORINFO ppLcCreateErrorInfo)
{
PLC_CONTEXT ctxLC = NULL;
QWORD qwExistingHandle = 0, tmStart = LcCallStart();
if(ppLcCreateErrorInfo) { *ppLcCreateErrorInfo = NULL; }
if(!pLcCreateConfig || (pLcCreateConfig->dwVersion != LC_CONFIG_VERSION)) { return NULL; }
// check if open existing (primary) device:
if(!pLcCreateConfig->szRemote[0] && (0 == _strnicmp("existing", pLcCreateConfig->szDevice, 8))) {
if(0 == _strnicmp("existing://", pLcCreateConfig->szDevice, 11)) {
qwExistingHandle = Util_GetNumericA(pLcCreateConfig->szDevice + 11);
}
EnterCriticalSection(&g_ctx.Lock);
ctxLC = (PLC_CONTEXT)g_ctx.FLink;
while(qwExistingHandle && ctxLC && (qwExistingHandle != (QWORD)ctxLC)) {
ctxLC = ctxLC->FLink;
}
if(qwExistingHandle && (qwExistingHandle != (QWORD)ctxLC)) {
ctxLC = NULL;
}
if(ctxLC) {
memcpy(pLcCreateConfig, &ctxLC->Config, sizeof(LC_CONFIG));
InterlockedIncrement(&ctxLC->dwHandleCount);
}
LeaveCriticalSection(&g_ctx.Lock);
return ctxLC;
}
// initialize new leechcore context:
if(!(ctxLC = LocalAlloc(LMEM_ZEROINIT, sizeof(LC_CONTEXT)))) { return NULL; }
pLcCreateConfig->fRemote = FALSE;
memcpy(&ctxLC->Config, pLcCreateConfig, sizeof(LC_CONFIG));
InitializeCriticalSection(&ctxLC->Lock);
ctxLC->version = LC_CONTEXT_VERSION;
ctxLC->dwHandleCount = 1;
ctxLC->cMemMapMax = 0x20;
ctxLC->pMemMap = LocalAlloc(LMEM_ZEROINIT, ctxLC->cMemMapMax * sizeof(LC_MEMMAP_ENTRY));
ctxLC->fPrintf[0] = (ctxLC->Config.dwPrintfVerbosity & LC_CONFIG_PRINTF_ENABLED) ? TRUE : FALSE;
ctxLC->fPrintf[1] = (ctxLC->Config.dwPrintfVerbosity & LC_CONFIG_PRINTF_V) ? TRUE : FALSE;
ctxLC->fPrintf[2] = (ctxLC->Config.dwPrintfVerbosity & LC_CONFIG_PRINTF_VV) ? TRUE : FALSE;
ctxLC->fPrintf[3] = (ctxLC->Config.dwPrintfVerbosity & LC_CONFIG_PRINTF_VVV) ? TRUE : FALSE;
LcCreate_FetchDeviceParameter(ctxLC);
LcCreate_FetchDevice(ctxLC);
if(!ctxLC->pfnCreate || !ctxLC->pfnCreate(ctxLC, ppLcCreateErrorInfo) || !LcReadContigious_Initialize(ctxLC)) {
LcClose(ctxLC);
return NULL;
}
if(!ctxLC->Config.fRemote) {
LcCreate_MemMapInitAddressDetect(ctxLC);
ctxLC->Config.paMax = LcMemMap_GetMaxAddress(ctxLC);
ctxLC->Config.fWritable = (ctxLC->pfnWriteScatter != NULL) || (ctxLC->pfnWriteContigious != NULL);
}
ctxLC->CallStat.dwVersion = LC_STATISTICS_VERSION;
QueryPerformanceFrequency((PLARGE_INTEGER)&ctxLC->CallStat.qwFreq);
memcpy(pLcCreateConfig, &ctxLC->Config, sizeof(LC_CONFIG));
lcprintfvv(ctxLC, "LeechCore v%i.%i.%i: Open Device: %s\n", VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION, ctxLC->Config.szDeviceName);
// add new leechcore context to global list and return:
EnterCriticalSection(&g_ctx.Lock);
ctxLC->FLink = g_ctx.FLink;
g_ctx.FLink = ctxLC;
LeaveCriticalSection(&g_ctx.Lock);
LcCallEnd(ctxLC, LC_STATISTICS_ID_OPEN, tmStart);
return ctxLC;
}
_Success_(return != NULL)
EXPORTED_FUNCTION HANDLE LcCreate(_Inout_ PLC_CONFIG pLcCreateConfig)
{
return LcCreateEx(pLcCreateConfig, NULL);
}
//-----------------------------------------------------------------------------
// Allocate/Free MEM_SCATTER:
//-----------------------------------------------------------------------------
/*
* Free LeechCore allocated memory such as memory allocated by the
* LcAllocScatter functions.
* -- pv
*/
EXPORTED_FUNCTION VOID LcMemFree(_Frees_ptr_opt_ PVOID pv)
{
LocalFree(pv);
}
/*
* Allocate and pre-initialize empty MEMs including a 0x1000 buffer for each
* pMEM. The result should be freed by LcFree when its no longer needed.
* -- cMEMs
* -- pppMEMs = pointer to receive ppMEMs
* -- return
*/
_Success_(return)
EXPORTED_FUNCTION BOOL LcAllocScatter1(_In_ DWORD cMEMs, _Out_ PPMEM_SCATTER *pppMEMs)
{
DWORD i, o = 0;
PBYTE pb, pbData;
PMEM_SCATTER pMEMs, *ppMEMs;
if(!(pb = LocalAlloc(LMEM_ZEROINIT, cMEMs * (sizeof(PMEM_SCATTER) + sizeof(MEM_SCATTER) + 0x1000)))) { return FALSE; }
ppMEMs = (PPMEM_SCATTER)pb;
pMEMs = (PMEM_SCATTER)(pb + cMEMs * (sizeof(PMEM_SCATTER)));
pbData = pb + cMEMs * (sizeof(PMEM_SCATTER) + sizeof(MEM_SCATTER));
for(i = 0; i < cMEMs; i++) {
ppMEMs[i] = pMEMs + i;
pMEMs[i].version = MEM_SCATTER_VERSION;
pMEMs[i].cb = 0x1000;
pMEMs[i].pb = pbData + o;
o += 0x1000;
}
*pppMEMs = ppMEMs;
return TRUE;
}
/*
* Allocate and pre-initialize empty MEMs excluding the 0x1000 buffer which
* will be accounted towards the pbData buffer in a contiguous way.
* The result should be freed by LcFree when its no longer needed.
* -- cbData = size of pbData (must be cMEMs * 0x1000)
* -- pbData = buffer used for MEM.pb
* -- cMEMs
* -- pppMEMs = pointer to receive ppMEMs
* -- return
*/
_Success_(return)
EXPORTED_FUNCTION BOOL LcAllocScatter2(_In_ DWORD cbData, _Inout_updates_opt_(cbData) PBYTE pbData, _In_ DWORD cMEMs, _Out_ PPMEM_SCATTER *pppMEMs)
{
DWORD i, o = 0;
PBYTE pb;
PMEM_SCATTER pMEMs, *ppMEMs;
if(cbData > (cMEMs << 12)) { return FALSE; }
if(!(pb = LocalAlloc(LMEM_ZEROINIT, cMEMs * (sizeof(PMEM_SCATTER) + sizeof(MEM_SCATTER))))) { return FALSE; }
ppMEMs = (PPMEM_SCATTER)pb;
pMEMs = (PMEM_SCATTER)(pb + cMEMs * (sizeof(PMEM_SCATTER)));
for(i = 0; i < cMEMs; i++) {
ppMEMs[i] = pMEMs + i;
pMEMs[i].version = MEM_SCATTER_VERSION;
pMEMs[i].cb = 0x1000;
pMEMs[i].pb = pbData + o;
o += 0x1000;
}
*pppMEMs = ppMEMs;
return TRUE;
}
/*
* Allocate and pre-initialize empty MEMs excluding the 0x1000 buffer which
* will be accounted towards the pbData buffer in a contiguous way.
* -- pbDataFirstPage = optional buffer of first page
* -- pbDataLastPage = optional buffer of last page
* -- cbData = size of pbData
* -- pbData = buffer used for MEM.pb except first/last if exists
* -- cMEMs
* -- pppMEMs = pointer to receive ppMEMs
* -- return
*/
_Success_(return)
EXPORTED_FUNCTION BOOL LcAllocScatter3(_Inout_updates_opt_(0x1000) PBYTE pbDataFirstPage, _Inout_updates_opt_(0x1000) PBYTE pbDataLastPage, _In_ DWORD cbData, _Inout_updates_opt_(cbData) PBYTE pbData, _In_ DWORD cMEMs, _Out_ PPMEM_SCATTER *pppMEMs)
{
DWORD i, o = 0;
PBYTE pb;
PMEM_SCATTER pMEMs, *ppMEMs;
if(pbDataFirstPage) { cbData += 0x1000; }
if(pbDataLastPage) { cbData += 0x1000; }
if(cbData > (cMEMs << 12)) { return FALSE; }
if(!(pb = LocalAlloc(LMEM_ZEROINIT, cMEMs * (sizeof(PMEM_SCATTER) + sizeof(MEM_SCATTER))))) { return FALSE; }
ppMEMs = (PPMEM_SCATTER)pb;
pMEMs = (PMEM_SCATTER)(pb + cMEMs * (sizeof(PMEM_SCATTER)));
for(i = 0; i < cMEMs; i++) {
ppMEMs[i] = pMEMs + i;
pMEMs[i].version = MEM_SCATTER_VERSION;
pMEMs[i].cb = 0x1000;
if(pbDataFirstPage && (i == 0)) {
pMEMs[i].pb = pbDataFirstPage;
} else if(pbDataLastPage && (i == cMEMs - 1)) {
pMEMs[i].pb = pbDataLastPage;
} else {
pMEMs[i].pb = pbData + o;
o += 0x1000;
}
}
*pppMEMs = ppMEMs;
return TRUE;
}
// ----------------------------------------------------------------------------
// READ CONTIGIOUS FUNCTIONALITY BELOW:
// ----------------------------------------------------------------------------
/*
* Perform a contigious read from an underlying device instance.
* -- ctxRC
*/
VOID LcReadContigious_DeviceRead(PLC_READ_CONTIGIOUS_CONTEXT ctxRC)
{
DWORD i, o, cbRead;
PMEM_SCATTER pMEM;
ctxRC->ctxLC->pfnReadContigious(ctxRC);
cbRead = ctxRC->cbRead;
for(i = 0, o = 0; ((i < ctxRC->cMEMs) && (cbRead >= ctxRC->ppMEMs[i]->cb)); i++) {
pMEM = ctxRC->ppMEMs[i];
memcpy(pMEM->pb, ctxRC->pb + o, pMEM->cb);
pMEM->f = TRUE;
o += pMEM->cb;
cbRead -= pMEM->cb;
}
}
/*
* Main thread loop for multi-chunked/threaded linear reads.
* -- ctxRC
* -- return
*/
DWORD LcReadContigious_ThreadProc(PLC_READ_CONTIGIOUS_CONTEXT ctxRC)
{
while(ctxRC->ctxLC->RC.fActive) {
WaitForSingleObject(ctxRC->hEventWakeup, INFINITE);
if(!ctxRC->ctxLC->RC.fActive) { break; }
LcReadContigious_DeviceRead(ctxRC);
SetEvent(ctxRC->hEventFinish);
}
SetEvent(ctxRC->hEventFinish);
return 0;
}
/*
* Perform a read of the linear memory specified onto the supplied MEMs.
* -- ctxLC
* -- cMEMs
* -- ppMEMs
* -- paBase
* -- cb
* -- fSingleThreaded
*/
VOID LcReadContigious_Read(_In_ PLC_CONTEXT ctxLC, _In_ DWORD cMEMs, _Inout_ PPMEM_SCATTER ppMEMs, _In_ QWORD paBase, _In_ DWORD cb, _In_ BOOL fSingleThreaded)
{
DWORD i;
PLC_READ_CONTIGIOUS_CONTEXT ctxRC;
if(!ctxLC->RC.fActive) { return; }
if(fSingleThreaded) {
ctxRC = ctxLC->RC.ctx[0];
} else {
i = WaitForMultipleObjects(ctxLC->ReadContigious.cThread, ctxLC->RC.hEventFinish, FALSE, INFINITE) - WAIT_OBJECT_0;
if(!ctxLC->RC.fActive || (i >= ctxLC->ReadContigious.cThread)) { return; }
ctxRC = ctxLC->RC.ctx[i];
ResetEvent(ctxRC->hEventFinish);
}
ctxRC->cbRead = 0;
ctxRC->cMEMs = cMEMs;
ctxRC->ppMEMs = ppMEMs;
ctxRC->paBase = paBase;
ctxRC->cb = cb;
if(fSingleThreaded) {
LcReadContigious_DeviceRead(ctxRC);
} else {
SetEvent(ctxRC->hEventWakeup);
}
}
/*
* Condense scattered MEMs into as large linear read-chunks as possible and
* schedule these chunks for reading using either single-threaded read or
* multi-threaded read - as configured and as optimal.
* MEMs are assumed to have their memory map translation/validation completed.
* NB! MUST BE CALLED SINGLE THREADED (per device instance).
* -- ctxLC
* -- cMEMs
* -- ppMEMs
*/
VOID LcReadContigious_ReadScatterGather(_In_ PLC_CONTEXT ctxLC, _In_ DWORD cMEMs, _Inout_ PPMEM_SCATTER ppMEMs)
{
PMEM_SCATTER pMEM;
QWORD i, iBase = 0, paBase = 0;
DWORD cbChunkSizeLimit, c = 0, cbCurrent = 0;
BOOL fSingleThreaded, fFirst = TRUE;
fSingleThreaded = (ctxLC->ReadContigious.cThread == 1);
cbChunkSizeLimit = ctxLC->ReadContigious.cbChunkSize;
if((ctxLC->ReadContigious.cThread > 1) && ctxLC->ReadContigious.fLoadBalance) {
cbChunkSizeLimit = min(cbChunkSizeLimit, max(0x00010000, 0x1000 * (cMEMs / ctxLC->ReadContigious.cThread)));
}
for(i = 0; i < cMEMs; i++) {
pMEM = ppMEMs[i];
if(!MEM_SCATTER_ADDR_ISVALID(pMEM)) { continue; }
if(c == 0) {
if(pMEM->cb && !pMEM->f) {
c = 1;
iBase = i;
paBase = pMEM->qwA;
cbCurrent = pMEM->cb;
}
} else if((paBase + cbCurrent == pMEM->qwA) && (cbCurrent < cbChunkSizeLimit)) {
c++;
cbCurrent += pMEM->cb;
} else {
fFirst = FALSE;
LcReadContigious_Read(ctxLC, c, ppMEMs + iBase, paBase, cbCurrent, fSingleThreaded);
c = 0;
if(pMEM->cb && !pMEM->f) {
c = 1;
iBase = i;
paBase = pMEM->qwA;
cbCurrent = pMEM->cb;
}
}
}
fSingleThreaded = fSingleThreaded || fFirst;
if(c) {
LcReadContigious_Read(ctxLC, c, ppMEMs + iBase, paBase, cbCurrent, fSingleThreaded);
}
if(!fSingleThreaded && ctxLC->RC.fActive) {
WaitForMultipleObjects(ctxLC->ReadContigious.cThread, ctxLC->RC.hEventFinish, TRUE, INFINITE);
}
}
/*
* Try closing the ReadContigious sub-system for a specific device instance.
* -- ctxLC
*/
VOID LcReadContigious_Close(_In_ PLC_CONTEXT ctxLC)
{
DWORD i;
PLC_READ_CONTIGIOUS_CONTEXT ctxRC;
ctxLC->RC.fActive = FALSE;
for(i = 0; i < ctxLC->ReadContigious.cThread; i++) {
if(!ctxLC->RC.ctx[i] || !ctxLC->RC.ctx[i]->hEventWakeup) { break; }
SetEvent(ctxLC->RC.ctx[i]->hEventWakeup);
}
for(i = 0; i < ctxLC->ReadContigious.cThread; i++) {
if(!ctxLC->RC.ctx[i]) { break; }
ctxRC = ctxLC->RC.ctx[i];
ctxLC->RC.ctx[i] = NULL;
if(ctxRC->hEventWakeup) { SetEvent(ctxRC->hEventWakeup); }
if(ctxRC->hEventFinish) { WaitForSingleObject(ctxRC->hEventFinish, INFINITE); }
if(ctxRC->hEventFinish) { CloseHandle(ctxRC->hEventFinish); }
if(ctxRC->hEventWakeup) { CloseHandle(ctxRC->hEventWakeup); }
if(ctxRC->hThread) { CloseHandle(ctxRC->hThread); }
LocalFree(ctxRC);
}
}
/*
* Initialize the ReadContigious sub-system for a specific device instance.
* -- ctxLC
* -- return
*/
_Success_(return)
BOOL LcReadContigious_Initialize(_In_ PLC_CONTEXT ctxLC)
{
DWORD i;
PLC_READ_CONTIGIOUS_CONTEXT ctxRC;
if(!ctxLC->pfnReadContigious) { return TRUE; }
if(!ctxLC->ReadContigious.cThread) { ctxLC->ReadContigious.cThread = 1; } // default: single-threaded.
if(!ctxLC->ReadContigious.cbChunkSize) { ctxLC->ReadContigious.cbChunkSize = 0x01000000; } // default: 16MB buffer / thread.
ctxLC->ReadContigious.cThread = min(8, ctxLC->ReadContigious.cThread); // max 8 threads in parallel.
ctxLC->ReadContigious.cbChunkSize = min(0x01000000, ctxLC->ReadContigious.cbChunkSize); // max 16MB buffer / thread.
ctxLC->RC.fActive = TRUE;
for(i = 0; i < ctxLC->ReadContigious.cThread; i++) {
if(!(ctxRC = ctxLC->RC.ctx[i] = LocalAlloc(0, sizeof(LC_READ_CONTIGIOUS_CONTEXT) + ctxLC->ReadContigious.cbChunkSize + 0x1000))) { goto fail; }
ZeroMemory(ctxRC, sizeof(LC_READ_CONTIGIOUS_CONTEXT));
ctxRC->ctxLC = ctxLC;
if(ctxLC->ReadContigious.cThread > 1) {
ctxRC->iRL = i;
if(!(ctxRC->hEventWakeup = CreateEvent(NULL, FALSE, FALSE, FALSE))) { goto fail; }
if(!(ctxRC->hEventFinish = ctxLC->RC.hEventFinish[i] = CreateEvent(NULL, TRUE, TRUE, FALSE))) { goto fail; }
if(!(ctxRC->hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)LcReadContigious_ThreadProc, ctxRC, 0, NULL))) { goto fail; }
}
}
return TRUE;
fail:
LcReadContigious_Close(ctxLC);
return FALSE;
}
// ----------------------------------------------------------------------------
// READ / WRITE FUNCTIONALITY BELOW:
// ----------------------------------------------------------------------------
/*
* Read memory in a scattered non-contiguous way. This is recommended for reads.
* -- hLC
* -- cMEMs
* -- ppMEMs
*/
EXPORTED_FUNCTION VOID LcReadScatter(_In_ HANDLE hLC, _In_ DWORD cMEMs, _Inout_ PPMEM_SCATTER ppMEMs)
{
PLC_CONTEXT ctxLC = (PLC_CONTEXT)hLC;
QWORD i, tmStart = LcCallStart();
if(!ctxLC || ctxLC->version != LC_CONTEXT_VERSION) { return; }
if(ctxLC->Config.fRemote && ctxLC->pfnReadScatter) {
// REMOTE
ctxLC->pfnReadScatter(ctxLC, cMEMs, ppMEMs);
} else {
// LOCAL LEECHCORE
// 1: TRANSLATE
for(i = 0; i < cMEMs; i++) {
MEM_SCATTER_STACK_PUSH(ppMEMs[i], ppMEMs[i]->qwA);
}
LcMemMap_TranslateMEMs(ctxLC, cMEMs, ppMEMs);
// 2: FETCH
LcLockAcquire(ctxLC);
if(ctxLC->pfnReadScatter) {
ctxLC->pfnReadScatter(ctxLC, cMEMs, ppMEMs);
} else if(ctxLC->RC.fActive) {
LcReadContigious_ReadScatterGather(ctxLC, cMEMs, ppMEMs);
}
LcLockRelease(ctxLC);
// 3: RESTORE
for(i = 0; i < cMEMs; i++) {
ppMEMs[i]->qwA = MEM_SCATTER_STACK_POP(ppMEMs[i]);
}
}
LcCallEnd(ctxLC, LC_STATISTICS_ID_READSCATTER, tmStart);
}
/*
* Read memory in a contiguous way. Note that if multiple memory segments are
* to be read LcReadScatter() may be more efficient.
* -- hLC,
* -- pa
* -- cb
* -- pb
* -- return
*/
_Success_(return)
EXPORTED_FUNCTION BOOL LcRead(_In_ HANDLE hLC, _In_ QWORD pa, _In_ DWORD cb, _Out_writes_(cb) PBYTE pb)
{
QWORD i, o, paBase, cMEMs;
PPMEM_SCATTER ppMEMs = NULL;
BOOL fFirst, fLast, f, fResult = FALSE;
BYTE pbFirst[0x1000] = { 0 }, pbLast[0x1000] = { 0 };
PLC_CONTEXT ctxLC = (PLC_CONTEXT)hLC;
QWORD tmStart = LcCallStart();
if(!ctxLC || ctxLC->version != LC_CONTEXT_VERSION) { return FALSE; }
if(cb == 0) { return TRUE; }
cMEMs = ((pa & 0xfff) + cb + 0xfff) >> 12;
if(cMEMs == 0) { return FALSE; }
fFirst = (pa & 0xfff) || (cb < 0x1000);
fLast = (cMEMs > 1) && ((pa + cb) & 0xfff);
f = LcAllocScatter3(
fFirst ? pbFirst : NULL,
fLast ? pbLast : NULL,
cb - (fFirst ? 0x1000 - (pa & 0xfff) : 0) - (fLast ? (pa + cb) & 0xfff : 0),
pb + ((pa & 0xfff) ? 0x1000 - (pa & 0xfff) : 0),
(DWORD)cMEMs,
&ppMEMs
);
if(!f) { goto fail; }
paBase = pa & ~0xfff;
for(i = 0; i < cMEMs; i++) {
ppMEMs[i]->qwA = paBase + (i << 12);
}
LcReadScatter(hLC, (DWORD)cMEMs, ppMEMs);
for(i = 0; i < cMEMs; i++) {
if(!ppMEMs[i]->f) { goto fail; }
}
if(fFirst) {
o = pa & 0xfff;
memcpy(pb, ppMEMs[0]->pb + o, min(cb, 0x1000 - (SIZE_T)o));
}
if(fLast) {
o = ppMEMs[cMEMs - 1]->qwA;
memcpy(pb + (SIZE_T)(o - pa), ppMEMs[cMEMs - 1]->pb, (SIZE_T)(pa + cb - o));
}
fResult = TRUE;
fail:
LocalFree(ppMEMs);
LcCallEnd(ctxLC, LC_STATISTICS_ID_READ, tmStart);
return fResult;
}
/*
* Write scatter memory in a contigious way - helper function for LcWriteScatter_GatherContigious().
* -- ctxLC
* -- cMEMs
* -- ppMEMs
* -- cbWrite
*/
VOID LcWriteScatter_GatherContigious2(_In_ PLC_CONTEXT ctxLC, _In_ DWORD cMEMs, _Inout_ PPMEM_SCATTER ppMEMs, _In_ DWORD cbWrite)
{
DWORD i;
if(ctxLC->pfnWriteContigious(ctxLC, ppMEMs[0]->qwA, cbWrite, ppMEMs[0]->pb)) {
for(i = 0; i < cMEMs; i++) {
ppMEMs[i]->f = TRUE;
}
}
}
/*
* Write scatter memory in a contigious way.
* -- ctxLC
* -- cMEMs
* -- ppMEMs
*/
VOID LcWriteScatter_GatherContigious(_In_ PLC_CONTEXT ctxLC, _In_ DWORD cMEMs, _Inout_ PPMEM_SCATTER ppMEMs)
{
DWORD c = 0, cbCurrent;
QWORD i, iBase = 0, paBase;
PMEM_SCATTER pMEM;
for(i = 0; i < cMEMs; i++) {
pMEM = ppMEMs[i];
if(pMEM->f || !MEM_SCATTER_ADDR_ISVALID(pMEM)) { continue; }
if(c == 0) {
c = 1;
iBase = i;
paBase = pMEM->qwA;
cbCurrent = pMEM->cb;
} else if(paBase + cbCurrent == pMEM->qwA) {
c++;
cbCurrent += pMEM->cb;
} else {
LcWriteScatter_GatherContigious2(ctxLC, c, ppMEMs + iBase, cbCurrent);
c = 1;
iBase = i;
paBase = pMEM->qwA;
cbCurrent = pMEM->cb;
}
}
if(c) {
LcWriteScatter_GatherContigious2(ctxLC, c, ppMEMs + iBase, cbCurrent);
}
}
/*
* Write memory in a scattered non-contiguous way.
* -- hLC
* -- cMEMs
* -- ppMEMs
*/
EXPORTED_FUNCTION VOID LcWriteScatter(_In_ HANDLE hLC, _In_ DWORD cMEMs, _Inout_ PPMEM_SCATTER ppMEMs)
{
PLC_CONTEXT ctxLC = (PLC_CONTEXT)hLC;
QWORD i, tmStart = LcCallStart();
if(!ctxLC || ctxLC->version != LC_CONTEXT_VERSION) { return; }
if(!ctxLC->pfnWriteScatter && !ctxLC->pfnWriteContigious) { return; }
if(!cMEMs) { return; }
if(ctxLC->Config.fRemote && ctxLC->pfnWriteScatter) {
// REMOTE
ctxLC->pfnWriteScatter(ctxLC, cMEMs, ppMEMs);
} else {
// LOCAL LEECHCORE
// 1: TRANSLATE
for(i = 0; i < cMEMs; i++) {
MEM_SCATTER_STACK_PUSH(ppMEMs[i], ppMEMs[i]->qwA);
}
LcMemMap_TranslateMEMs(ctxLC, cMEMs, ppMEMs);
// 2: FETCH
LcLockAcquire(ctxLC);
if(ctxLC->pfnWriteScatter) {
ctxLC->pfnWriteScatter(ctxLC, cMEMs, ppMEMs);
} else {
LcWriteScatter_GatherContigious(ctxLC, cMEMs, ppMEMs);
}
LcLockRelease(ctxLC);
// 3: RESTORE
for(i = 0; i < cMEMs; i++) {
ppMEMs[i]->qwA = MEM_SCATTER_STACK_POP(ppMEMs[i]);
}
}
LcCallEnd(ctxLC, LC_STATISTICS_ID_WRITESCATTER, tmStart);
}
/*
* Write memory in a contiguous way.
* -- hLC
* -- pa
* -- cb
* -- pb
* -- return
*/
_Success_(return)
EXPORTED_FUNCTION BOOL LcWrite(_In_ HANDLE hLC, _In_ QWORD pa, _In_ DWORD cb, _In_reads_(cb) PBYTE pb)
{
BOOL fResult = FALSE;
PBYTE pbBuffer = NULL;
DWORD i = 0, oA = 0, cbP, cMEMs;
PMEM_SCATTER pMEM, pMEMs, *ppMEMs;
PLC_CONTEXT ctxLC = (PLC_CONTEXT)hLC;
QWORD tmStart = LcCallStart();
if(!ctxLC || ctxLC->version != LC_CONTEXT_VERSION) { goto fail; }
// allocate
cMEMs = (DWORD)(((pa & 0xfff) + cb + 0xfff) >> 12);
if(!(pbBuffer = (PBYTE)LocalAlloc(LMEM_ZEROINIT, cMEMs * (sizeof(MEM_SCATTER) + sizeof(PMEM_SCATTER))))) { goto fail; }
pMEMs = (PMEM_SCATTER)pbBuffer;
ppMEMs = (PPMEM_SCATTER)(pbBuffer + cMEMs * sizeof(MEM_SCATTER));
// prepare pages
while(oA < cb) {
cbP = 0x1000 - ((pa + oA) & 0xfff);
cbP = min(cbP, cb - oA);
ppMEMs[i] = pMEM = pMEMs + i;
pMEM->version = MEM_SCATTER_VERSION;
pMEM->qwA = pa + oA;
pMEM->cb = cbP;
pMEM->pb = pb + oA;
oA += cbP;
i++;
}
// write and verify result
LcWriteScatter(hLC, cMEMs, ppMEMs);
for(i = 0; i < cMEMs; i++) {
if(!ppMEMs[i]->f) {
break;
}
}
fResult = TRUE;
fail:
LocalFree(pbBuffer);
LcCallEnd(ctxLC, LC_STATISTICS_ID_WRITE, tmStart);
return fResult;
}
// ----------------------------------------------------------------------------
// GET / SET / COMMAND FUNCTIONALITY BELOW:
// ----------------------------------------------------------------------------
/*
* Helper function for LcGetOption.
*/
_Success_(return)
BOOL LcGetOption_DoWork(_In_ PLC_CONTEXT ctxLC, _In_ QWORD fOption, _Out_ PQWORD pqwValue)
{
*pqwValue = 0;
switch(fOption & 0xffffffff00000000) {
case LC_OPT_CORE_PRINTF_ENABLE:
*pqwValue = ctxLC->fPrintf[LC_PRINTF_ENABLE] ? 1 : 0;
return TRUE;