forked from cheat-engine/cheat-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
293 lines (206 loc) · 6.42 KB
/
main.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
/*
* main.c
*
* Created on: Nov 10, 2017
* Author: eric
*/
#include <efi.h>
#include <efilib.h>
#include "helpers.h"
#include "dbvmoffload.h"
EFI_SYSTEM_TABLE *st;
EFI_BOOT_SERVICES *bs;
EFI_RUNTIME_SERVICES *rs;
EFI_LOAD_FILE_INTERFACE *file;
EFI_GUID fileprotocol=LOAD_FILE_PROTOCOL;
EFI_STATUS OpenProtocol(
IN EFI_HANDLE Handle,
IN EFI_GUID *Protocol,
OUT VOID **Interface OPTIONAL,
IN EFI_HANDLE AgentHandle,
IN EFI_HANDLE ControllerHandle,
IN UINT32 Attributes
)
{
return uefi_call_wrapper(bs->OpenProtocol, 6, Handle, Protocol, Interface, AgentHandle, ControllerHandle, Attributes);
}
EFI_STATUS LocateProtocol(IN EFI_GUID *Protocol,
IN VOID *Registration OPTIONAL,
OUT VOID **Interface)
{
return uefi_call_wrapper(bs->LocateProtocol, 3, Protocol, Registration, Interface);
}
EFI_STATUS GetVariable(
IN CHAR16 *VariableName,
IN EFI_GUID *VendorGuid,
OUT UINT32 *Attributes OPTIONAL,
IN OUT UINTN *DataSize,
OUT VOID *Data
)
{
return uefi_call_wrapper(rs->GetVariable, 5, VariableName, VendorGuid, Attributes, DataSize, Data);
}
EFI_STATUS SetVariable(
IN CHAR16 *VariableName,
IN EFI_GUID *VendorGuid,
IN UINT32 Attributes,
IN UINTN DataSize,
IN VOID *Data
)
{
return uefi_call_wrapper(rs->SetVariable, 5, VariableName, VendorGuid, Attributes, DataSize, Data);
}
void initFunctions(EFI_HANDLE Handle, EFI_SYSTEM_TABLE *SystemTable)
{
st=SystemTable;
bs=st->BootServices;
rs=st->RuntimeServices;
}
EFI_STATUS GetNextMonotonicCount(OUT UINT64 *Count)
{
return uefi_call_wrapper(bs->GetNextMonotonicCount, 1, Count);
}
EFI_STATUS AllocatePages(
IN EFI_ALLOCATE_TYPE Type,
IN EFI_MEMORY_TYPE MemoryType,
IN UINTN NoPages,
OUT EFI_PHYSICAL_ADDRESS *Memory
)
{
return uefi_call_wrapper(bs->AllocatePages, 4, Type, MemoryType, NoPages, Memory);
}
VOID *AllocatePersistentMemory(int size)
{
EFI_PHYSICAL_ADDRESS pa=0;
int pages=size/4096;
if (size % 4096)
pages++;
if (AllocatePages(AllocateAnyPages, EfiRuntimeServicesData, pages, &pa)==EFI_SUCCESS)
return (VOID *)pa;
else
return NULL;
}
void printDevicePath(EFI_DEVICE_PATH *path)
{
Print(L"type=%d\nsubtype=%d\nlength[0]=%d\nlength[2]=%d\n",path->Type,path->SubType, path->Length[0], path->Length[1]);
if ((path->Type==4) && (path->SubType==4))
{
CHAR16 *s;
FILEPATH_DEVICE_PATH *fp=(FILEPATH_DEVICE_PATH*)path;
s=&fp->PathName[0];
Print(L"Path=");
Print(s);
Print(L"\n");
}
}
/*
inline uint64_t rdmsr(uint32_t msr_id)
{
uint64_t msr_value;
asm volatile ( "rdmsr" : "=A" (msr_value) : "c" (msr_id) );
return msr_value;
}*/
EFI_STATUS
EFIAPI
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
EFI_PHYSICAL_ADDRESS dbvmimage;
CHAR16 something[200];
//UINT64 c=testfunction();
EFI_STATUS s;
InitializeLib(ImageHandle, SystemTable);
initFunctions(ImageHandle, SystemTable);
Print(L"efi_main at %lx\n",(UINT64)efi_main);
//s=SystemTable->BootServices->AllocatePages(AllocateAnyPages, EfiLoaderData, 4, &pa);
s=AllocatePages(AllocateAnyPages,EfiRuntimeServicesCode,1024,&dbvmimage); //4MB
if (s!=EFI_SUCCESS)
{
Print(L"Failed allocating memory for the VMM\n");
return s;
}
Print(L"dbvm image space allocated at s=%x\n",s);
ZeroMem((void *)dbvmimage, 1024*4096);
/*
pa2=0xfffff;
s=AllocatePages(AllocateMaxAddress,EfiRuntimeServicesCode,4,&pa2);
Print(L"AllocatePages: s=%x\n",s);
Print(L"after pa2=%lx\n",pa2);
*/
//EFI_INVALID_PARAMETER
// s=SystemTable->BootServices->Exit(ImageHandle,1,0, NULL);
//Print(L"Exit: s=%x\n",s);
CHAR16 filepath[128];
FILEPATH_DEVICE_PATH *vmdisk;
EFI_HANDLE dh;
SIMPLE_READ_FILE srh;
SetMem(filepath, 128*2,0xce);
EFI_GUID lip=LOADED_IMAGE_PROTOCOL;
EFI_LOADED_IMAGE *li=NULL;
s=OpenProtocol(ImageHandle, &lip, (void **)&li, ImageHandle, NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
Print(L"OpenProtocol: s=%d\n",s);
vmdisk=(FILEPATH_DEVICE_PATH *)FileDevicePath(li->DeviceHandle,L"vmdisk.img");
EFI_DEVICE_PATH *d=(EFI_DEVICE_PATH *)vmdisk;
Print(L"calling OpenSimpleReadFile\n",s);
//bs->LocateDevicePath
s=OpenSimpleReadFile(FALSE,NULL, 0, &d, &dh, &srh);
Print(L"OpenSimpleReadFile: s=%d\n",s);
if (s==EFI_SUCCESS)
{
UINTN size=2; //16384*4096;
UINT16 startsector;
s=ReadSimpleReadFile(srh, 8, &size, &startsector);
if (s!=EFI_SUCCESS)
{
Print(L"Failure reading vmdisk.img offset 8\n");
return s;
}
Print(L"startsector=%d\n",startsector);
size=1024*4096;
s=ReadSimpleReadFile(srh,startsector*512,&size,(void *)dbvmimage);
Print(L"ReadSimpleReadFile: s=%d size=%d\n",s,size);
CloseSimpleReadFile(srh);
if (size % 4096)
size=(size+4096)-(size % 4096);
InitializeDBVM(dbvmimage, size);
}
FreePool(vmdisk);
UINT64 base=(UINT64)dbvmimage; //filepath;
int i;
for (i=0; i<128; i++)
{
Print(L"%x ",*(unsigned char *)(base+i));
//*(char *)i=0xce;
}
Print(L"\n");
//offload current state into DBVM
//OpenSimpleReadFile(1,NULL,0 )
#define DB_SETUP_GUID { 0xEC87D643, 0xEBA4, 0x4BB5, {0xa1, 0xe5, 0x3f, 0x3e, 0x36, 0xb2, 0x0d, 0xa9} }
Print(L"Reading msr 0x10:\n");
s=readMSR(0x10);
Print(L"readMSR s=%x\n",s);
s=readMSR(0x10);
Print(L"readMSR s=%x\n",s);
s=readMSR(0x10);
Print(L"readMSR s=%x\n",s);
s=readMSR(0xc80);
Print(L"readMSR s=%x\n",s);
// s=rdmsr(0xc80);
// Print(L"readMSR s=%x\n",s);
{
EFI_LOADED_IMAGE *loaded_image = NULL;
s = uefi_call_wrapper(SystemTable->BootServices->HandleProtocol, 3, ImageHandle, &LoadedImageProtocol, (void **)&loaded_image);
Print(L"Image base: 0x%lx\n", loaded_image->ImageBase);
}
//get the memory map
// asm volatile (".byte 0xf1");
Input(L"Type something : ", something, 200);
Print(L"\n");
if (StrnCmp(something,L"Q",2)!=0)
{
Print(L"launching DBVM\n");
LaunchDBVM();
}
Print(L"Something is %S", something);
SystemTable->BootServices->Exit(ImageHandle, 1,0,NULL);
return EFI_SUCCESS;
}