-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBrainLILO.cpp
162 lines (128 loc) · 3.38 KB
/
BrainLILO.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
#define BRAINLILO_API extern "C" __declspec(dllexport)
#include "BrainLILO.h"
#include "BrainLILODrv.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"LIN0:", 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"BrainLILO: installing LILODriver...");
DWORD err;
HANDLE driver = NULL;
wchar_t driverPath[1024];
getThisDllDirectoryPath(driverPath);
TCHAR newDriverPath[128];
getThisDllDirectoryPath(driverPath);
wcscat(driverPath, L"\\BrainLILODrv.dll");
wcscpy(newDriverPath, L"\\Windows\\BrainLILODrv.dll");
{
wchar_t buf[1024];
swprintf(buf, L"BrainLILO: copying \"%ls\" to \"%ls\"", driverPath, newDriverPath);
OutputDebugString(buf);
}
if (!CopyFile(driverPath, newDriverPath, FALSE))
{
if (GetFileAttributes(newDriverPath) == (DWORD)-1)
{
OutputDebugString(L"BrainLILO: failed to copy");
return;
}
}
OutputDebugString(L"BrainLILO: registering BrainLILODriver...");
try
{
driver = RegisterDevice(L"LIN", 0, L"\\Windows\\BrainLILODrv.dll", 0);
if (driver == INVALID_HANDLE_VALUE)
driver = NULL;
if (!driver)
{
driver = RegisterDevice(L"LIN", 0, L"BrainLILODrv.dll", 0);
if (driver == INVALID_HANDLE_VALUE)
driver = NULL;
}
err = GetLastError();
}
catch (DWORD e)
{
err = e;
}
if (!driver && (err != 0x964))
{
OutputDebugString(L"BrainLILO: failed to install...");
return;
}
}
static void openDriver()
{
if (g_helperHandle)
return;
g_helperHandle =
CreateFile(L"LIN0:", 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;
}
BRAINLILO_API BOOL RKDoSoftReset()
{
initialize();
loadDriverIfNeeded();
openDriver();
return DeviceIoControl(g_helperHandle, IOCTL_LIN_DO_LINUX, NULL, 0, NULL, 0, NULL, NULL);
}