ceplugin 0.6.0

Rust bindings to the Cheat Engine plugin SDK
Documentation
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
/*
 * This sourcefile holds the code for setting up and querying other cpu cores
 * on the system
 */

#include "multicore.h"
#include "common.h"
#include "apic.h"
#include "mm.h"

volatile unsigned int cpucount;

typedef struct _MPFPS
{
        DWORD signature; // 0x5f504d5f : '_MP_'
        DWORD MPConfigPointer; //
        BYTE Length;
        BYTE Version;
        BYTE Checksum;
        BYTE MPFEATURE1;
        BYTE MPFEATURE2;
        BYTE MPFEATURE3;
        BYTE MPFEATURE4;
        BYTE MPFEATURE5;
} __attribute__((__packed__)) *PMPFPS;

typedef struct _MPCONFIG
{
        DWORD signature; // 0x504d4350 : 'PCMP'
        WORD BaseTableLength; //
        BYTE SpecRev;
        BYTE Checksum;
        char OEMID[8];
        char PRODUCTID[12];
        DWORD OEMTablePointer;
        WORD OEMTableSize;
        WORD EntryCount;
        DWORD AddressofLocalAPIC;
        WORD ExtendedTableLength;
        BYTE ExtendedTableChecksum;
        BYTE Filler;
} __attribute__((__packed__)) *PMPCONFIG;

typedef struct _MPCONFCPU
{
				BYTE EntryType;
        BYTE LocalAPICID;
        BYTE LocalAPICVersion;
        unsigned CPUEnabledBit : 1;
        unsigned CPUBootstrapProcessorBit: 1;
        unsigned reserved : 6;
        DWORD CPUSignature;
        DWORD CPUFeatureFlags;
} __attribute__((__packed__)) *PMPCONFCPU;

typedef struct _RSD_PTR
{
        char signature[8]; //'RSD PTR '
        unsigned char checksum;
        char OEMID[6];
        char reserved;
        DWORD rsdtaddress;
} __attribute__((__packed__)) *PRSD_PTR;

typedef struct _RSDT
{
        DWORD signature; //RSDT
        DWORD length;
        char revision;
        unsigned char checksum;
        char OEMID[6];
        char OEM_TABLE_ID[8];
        char OEM_REVISION[4];
        char Creator_ID[4];
        DWORD Creator_revision;
        DWORD Entries[];
} __attribute__((__packed__)) *PRSDT;

typedef struct _APICDT
{
        DWORD signature; //APIC
        DWORD length;
        char revision;
        unsigned char checksum;
        char OEMID[6];
        char OEM_TABLE_ID[8];
        char OEM_REVISION[4];
        char Creator_ID[4];
        DWORD Creator_revision;
        DWORD LocalAPIC;
        DWORD flags;
        BYTE APICStructure[];
} __attribute__((__packed__)) *PAPICDT;

int checkcrc(UINT64 PhysicalAddress, int size)
{
  //everything counted up must be 0 as 1 byte
  unsigned char *x=(unsigned char *)MapPhysicalMemory(PhysicalAddress, 0x08000000);
  unsigned char total=0;
  int i;

  for (i=0; i<size; i++)
    total=total+x[i];

  return (total==0);

}

void *find_RSDP_inrange(BYTE *address,int size)
{
  int i;

  for (i=0; i<size; i++, address++)
  {
    if (*(QWORD*)address==0x2052545020445352ULL)
    {
      displayline("POSSIBLE FOUND: %8\n",(QWORD)address);

      if (checkcrc((UINT64)address,20))
        return address;
      else
        displayline("Invalid crc\n");


    }
  }
  return NULL;
}

void *find_MP_inrange(BYTE *address,int size)
{
	int i;
	PMPFPS mp;
	PMPCONFIG mpc;

	for (i=0; i<size; i++, address++)
	{
		if (*(DWORD*)address==0x5f504d5f)
		{
			//displayline("POSSIBLE FOUND: %8\n",(QWORD)address);
			mp=(PMPFPS)address;
			mpc=(PMPCONFIG)((QWORD)mp->MPConfigPointer);

			if ((QWORD)mpc<0x400000)
			{
				if (mpc->signature==0x504d4350)
				{
					return mp; //it's valid
				}
			}

		}
	}
	return NULL;
}

unsigned int initAPcpus(void)
{
	int found=0;
	int hasHT;

	int isAMD;

	{
	  UINT64 a,b,c,d;
    a=0;
    _cpuid(&a,&b,&c,&d);
    isAMD=((b==0x68747541) && (d==0x69746e65) && (c==0x444d4163));



	  if (!isAMD)
	  {
	    a=1;
	    _cpuid(&a,&b,&c,&d);

      displayline("Feature flag=%x\n",d);
      hasHT=(d >> 28) & 1;
      if (hasHT)
        displayline("hasHT==1\n");
	  }
	  else
	    hasHT=0;
	}

	displayline("Find EBDA:");

	WORD EDBAsegment=*(WORD*)0x40e;
	QWORD EDBAlocation=EDBAsegment<<4;
	displayline("%8\n",EDBAlocation);

	displayline("Checking RSDP tables\n");
	PRSD_PTR rsd_ptr=NULL;


	rsd_ptr=find_RSDP_inrange((void*)EDBAlocation,1024);
	if (!rsd_ptr)
	  rsd_ptr=find_RSDP_inrange((void*)0x9fc00,1024);

	if (!rsd_ptr)
	  rsd_ptr=find_RSDP_inrange((void*)0xf0000,65536);

	if (rsd_ptr)
	{
	  PRSDT rsdt;
	  displayline("RSDP is located at %x\n",rsd_ptr->rsdtaddress);

	  rsdt=(PRSDT)MapPhysicalMemory(rsd_ptr->rsdtaddress,0x08000000);
	  if (checkcrc(rsd_ptr->rsdtaddress, rsdt->length))
	  {
	    if (rsdt->signature==0x54445352)
	    {
	      int i;
	      int entrycount=(rsdt->length-36)/4;
	      displayline("Entrycount=%d\n",entrycount);

	      for (i=0; i<entrycount; i++)
	      {
	        PAPICDT apicdt;
	        displayline("Entry %d: %x\n",i,rsdt->Entries[i]);

	        apicdt=(PAPICDT)MapPhysicalMemory(rsdt->Entries[i],0x0a000000);
	        if (apicdt->signature==0x43495041)
	        {
	          int j=0;
	          int APICStructure_size=apicdt->length-44;
	          displayline("This one holds APIC info\n");

	          while (j<APICStructure_size)
	          {
	            //parse the structures
	            switch (apicdt->APICStructure[j])
	            {
	              case 0:
	              {
	                //Processor Local APIC structure (only one i'm interested in)
	                displayline("Processor Local APIC structure\n");
	                found++;

	                if (found==2) //multi or threaded, launch them
	                    initcpus(apicdt->LocalAPIC);


	                break;
	              }

	              case 1:
	              {
	                //IO APIC structure
	                displayline("IO APIC structure\n");

	                break;
	              }

	              case 2:
	              {
	                displayline("Interrupt Source Override\n");

	                break;
	              }

	              case 3:
	              {
	                displayline("Non-maskable Interrupt Source\n");

	                break;
	              }

	              case 4:
	              {
	                displayline("Local APIC NMI Structure\n");
	                break;
	              }

	              default:
	              {
	                displayline("Unknown Structure\n");
	                break;
	              }

	            }

	            j+=apicdt->APICStructure[j+1]; //Length

	          }


	        }



	      }


	    }
	    else displayline("Unexpected signature for rsdt (%x)\n",rsdt->signature);

	  }
	  else
	    displayline("rsdt is invalid\n");


	}
	else
	  displayline("No RSD_PTR found\n");



	if (found>0)
	  return found;

	//still here
	displayline("Falling back to MP table");
	PMPFPS mp;
	PMPCONFIG mpc;
	PMPCONFCPU mpcpu;


	//check first KB of EDBA
	mp=find_MP_inrange((void*)EDBAlocation,1024);
	if (mp!=NULL)
	{
		displayline("EBDA: FOUND: %8\n",mp);
	}
	else
	{
		displayline("EBDA: NOT FOUND\n");
		displayline("Last KB Base (0x9fc00->0x9ffff) : Scanning...");
		mp=find_MP_inrange((void*)0x9fc00,1024);
		if (mp!=NULL)
		{
			displayline("FOUND: %8\n",mp);
		}
		else
		{
			displayline("NOT FOUND\n");
			displayline("ROM (0xf0000->0xfffff) : Scanning...");
			mp=find_MP_inrange((void*)0xf0000,65536);
			if (mp!=NULL)
			{
				displayline("FOUND: %8\n",mp);
			}
			else
			{
				displayline("NOT FOUND\n");
				displayline("ALL (0x0->0xfffff) : Scanning...");
				mp=find_MP_inrange((void*)0,1024*1024);
				if (mp!=NULL)
				{
					displayline("FOUND: %8\n",mp);
				}
				else
				{
					displayline("NOT FOUND\n");
					displayline("NO MULTIPROCESS SUPPORT\n");
				}
			}
		}
	}

	if (mp!=NULL)
	{
		int i;
		BYTE *p;
		mpc=(PMPCONFIG)((QWORD)mp->MPConfigPointer);
		displayline("Configuration table at %8\n",mpc);
		displayline("Base table length=%d\n",mpc->BaseTableLength);
		displayline("Specification=1.%d\n",mpc->SpecRev);
		displayline("OEM ID=");
		for (i=0; i<8; i++)
			displayline("%c",mpc->OEMID[i]);
		displayline("\n");

		displayline("Product ID=");
		for (i=0; i<12; i++)
			displayline("%c",mpc->PRODUCTID[i]);
		displayline("\n");

		displayline("OEM Table Pointer=%8\n",mpc->OEMTablePointer);
		displayline("OEM Table Size=%d\n",mpc->OEMTableSize);

		displayline("Entry Count=%d\n",mpc->EntryCount);
		displayline("Address of Local APIC=%8\n",mpc->AddressofLocalAPIC);


		displayline("Extended Table Length=%d\n",mpc->ExtendedTableLength);
		displayline("Extended Table Checksum=%2\n",mpc->ExtendedTableChecksum);

		displayline("sizeof(struct _MPCONFIG)=%d\n",sizeof(struct _MPCONFIG));

		i=0;
		p=(BYTE *)((QWORD)mpc+sizeof(struct _MPCONFIG));
		while (i<(mpc->EntryCount))
		{
		  //displayline("i=%d Checking %8\n",i,p);

		  switch (p[0])
		  {
		    case 0:
		      mpcpu=(PMPCONFCPU)p;
		      displayline("CPU entry at %8\n",p);
		      displayline("Local APICID=%d\n",mpcpu->LocalAPICID);
		      displayline("Local APICID version=%d\n",mpcpu->LocalAPICVersion);
		      displayline("CPU Enabled=%d\n",mpcpu->CPUEnabledBit);
		      displayline("CPU Bootstrap Processor=%d\n",mpcpu->CPUBootstrapProcessorBit);
		      displayline("CPU Signature=%8\n",mpcpu->CPUSignature);
		      displayline("CPU Feature Flags=%8\n",mpcpu->CPUFeatureFlags);

		      if (mpcpu->CPUEnabledBit)
		      {
		        found++;
		        if (hasHT) //quick assumption, could be wrong but it's the closest I can guess without acpi
		          found++;
		      }

		      p+=20;
		      break;

		    case 1:
		      //displayline("BUS entry at %8\n",p);
		      p+=8;
		      break;

		    case 2:
		      //displayline("IO APIC entry at %8\n",p);
		      p+=8;
		      break;

		    case 3:
		      //displayline("IO interrupt assignment entry at %8\n",p);
		      p+=8;
		      break;

		    case 4:
		      //displayline("Local interrupt assignment entry at %8\n",p);
		      p+=8;
		      break;

		    default:
		      //displayline("error\n");
		      p+=8;
		      break;
		  }

		  i++;
		}
	  displayline("before initcpus. found=%d\n\r", found);
	  if (found>1)
	    initcpus(mpc->AddressofLocalAPIC);




	}

	displayline("before return. found=%d\n\r", found);
	return (found?found:1);
}