-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResetKit.cpp
224 lines (180 loc) · 4.56 KB
/
ResetKit.cpp
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
#define RESETKIT_API extern "C" __declspec(dllexport)
#include "ResetKit.h"
#include "ResetKitHelper.h"
#include <string.h>
static bool g_initialized = false;
static HANDLE g_helperHandle = NULL;
static HINSTANCE g_hInstance;
static void initialize() {
if (g_initialized)
return;
g_initialized = true;
}
static void getThisDllDirectoryPath(LPWSTR buffer) {
// retrive the path of the application.
GetModuleFileName(g_hInstance, buffer, 512);
const size_t notFound = (size_t)-1;
size_t i = 0;
size_t j = notFound;
while (buffer[i]) {
if (buffer[i] == L'/' || buffer[i] == L'\\') {
j = i;
}
i++;
}
if (j == notFound)
return;
buffer[j] = 0;
}
static bool isDriverLoaded() {
HANDLE handle;
handle = CreateFile(L"RKH0:", GENERIC_READ | GENERIC_WRITE | FILE_WRITE_ATTRIBUTES, 0, NULL,
OPEN_EXISTING, 0, NULL);
if (handle == INVALID_HANDLE_VALUE)
handle = NULL;
if (handle) {
CloseHandle(handle);
return true;
}
return false;
}
static void loadDriverIfNeeded() {
if (isDriverLoaded())
return;
OutputDebugString(L"ResetKit: installing ResetKitHelper...");
DWORD err;
HANDLE driver = NULL;
wchar_t driverPath[1024];
getThisDllDirectoryPath(driverPath);
wcscat(driverPath, L"\\ResetKitHelper.dll");
LPCWSTR newDriverPath;
newDriverPath = L"\\Windows\\ResetKitHelper.dll";
{
wchar_t buf[1024];
swprintf(buf, L"ResetKit: copying \"%ls\" to \"%ls\"", driverPath, newDriverPath);
OutputDebugString(buf);
}
if (!CopyFile(driverPath, newDriverPath, FALSE)) {
if (GetFileAttributes(newDriverPath) == (DWORD)-1) {
OutputDebugString(L"ResetKit: failed to copy");
return;
}
}
/*
addDriverToRegistry();
OutputDebugString(L"ResetKit: activating ResetKitHelper...");
ActivateDevice(L"Drivers\\ResetKitHelper", NULL);
{
wchar_t buf[256];
DWORD err=GetLastError();
if(err){
swprintf(buf, L"ResetKit: ActivateDevice error: 0x%08x",
(int)err); OutputDebugString(buf);
}
}
*/
OutputDebugString(L"ResetKit: registering ResetKitHelper...");
try {
driver = RegisterDevice(L"RKH", 0, L"\\Windows\\ResetKitHelper.dll", 0);
if (driver == INVALID_HANDLE_VALUE)
driver = NULL;
if (!driver) {
driver = RegisterDevice(L"RKH", 0, L"ResetKitHelper.dll", 0);
if (driver == INVALID_HANDLE_VALUE)
driver = NULL;
}
err = GetLastError();
} catch (DWORD e) {
err = e;
}
if (!driver && (err != 0x964)) {
OutputDebugString(L"ResetKit: failed to install...");
return;
}
// addDriverToRegistry();
}
static void openDriver() {
if (g_helperHandle)
return;
g_helperHandle = CreateFile(L"RKH0:", GENERIC_READ | GENERIC_WRITE | FILE_WRITE_ATTRIBUTES,
0, NULL, OPEN_EXISTING, 0, NULL);
if (g_helperHandle == INVALID_HANDLE_VALUE)
g_helperHandle = NULL;
}
extern "C" BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD ul_reason_for_call, LPVOID lpReserved) {
switch (ul_reason_for_call) {
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
g_hInstance = hInstance;
initialize();
loadDriverIfNeeded();
openDriver();
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
RESETKIT_API DWORD RKDeviceGeneration() {
DWORD outBuf[1];
DWORD retSize;
initialize();
loadDriverIfNeeded();
openDriver();
if (DeviceIoControl(g_helperHandle, IOCTL_RKH_GET_DEVICE_GENERATION, NULL, 0, outBuf, 4,
&retSize, NULL)) {
SetLastError(0);
return outBuf[0];
} else {
return FALSE;
}
}
RESETKIT_API BOOL RKCanSoftReset() {
DWORD outBuf[1];
DWORD retSize;
initialize();
loadDriverIfNeeded();
openDriver();
if (DeviceIoControl(g_helperHandle, IOCTL_RKH_CAN_SOFT_RESET, NULL, 0, outBuf, 4, &retSize,
NULL)) {
SetLastError(0);
return outBuf[0] != 0;
} else {
return FALSE;
}
}
RESETKIT_API BOOL RKCanHardReset() {
DWORD outBuf[1];
DWORD retSize;
initialize();
loadDriverIfNeeded();
openDriver();
if (DeviceIoControl(g_helperHandle, IOCTL_RKH_CAN_HARD_RESET, NULL, 0, outBuf, 4, &retSize,
NULL)) {
SetLastError(0);
return outBuf[0] != 0;
} else {
return FALSE;
}
}
RESETKIT_API BOOL RKDoSoftReset() {
initialize();
loadDriverIfNeeded();
openDriver();
return DeviceIoControl(g_helperHandle, IOCTL_RKH_DO_SOFT_RESET, NULL, 0, NULL, 0, NULL,
NULL);
}
RESETKIT_API BOOL RKDoHardReset() {
initialize();
loadDriverIfNeeded();
openDriver();
return DeviceIoControl(g_helperHandle, IOCTL_RKH_DO_HARD_RESET, NULL, 0, NULL, 0, NULL,
NULL);
}
RESETKIT_API BOOL RKInstallDicProtect() {
initialize();
loadDriverIfNeeded();
openDriver();
return DeviceIoControl(g_helperHandle, IOCTL_RKH_INSTALL_DICPROTECT, NULL, 0, NULL, 0, NULL,
NULL);
}