-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBrainLILODrv.cpp
370 lines (306 loc) · 10.1 KB
/
BrainLILODrv.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
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
#include <fstream>
#include <regex>
#include <stdint.h>
#include <stdlib.h>
#include <windows.h>
#if defined(VERSION_MAJOR) && defined(VERSION_MINOR) && defined(VERSION_PATCH)
#define CONFIG_VERSION TEXT(VERSION_MAJOR) L"." TEXT(VERSION_MINOR) L"." TEXT(VERSION_PATCH)
#else
#define CONFIG_VERSION L"unspecified"
#endif
#define FSNOTIFY_POWER_OFF 1
#define FSNOTIFY_POWER_ON 0
#define BRAINLILODRV_API __declspec(dllexport)
#include "BrainLILODrv.h"
#include "models.h"
typedef void (*FileSystemPowerFunctionProc)(DWORD);
static FileSystemPowerFunctionProc FileSystemPowerFunction;
// GEN1
// VA
#define BOOTLOADER_PRELOADADDR_GEN1 (uint8_t *)0xa0250000
#define BOOTLOADER_LOADADDR_GEN1 (uint8_t *)0xa3f00000
#define PHYSICAL_INVOKER_INSTALLADDR_GEN1 (uint8_t *)0xb0000000
#define TMPA910_REMAP_REGISTER_ADDRESS (uint32_t *)0xaa000004
// PA
#define BOOTLOADER_LOADADDR_GEN1_PA (uint8_t *)0x43f00000
// DEFAULT(GEN2)
// VA
#define BOOTLOADER_PRELOADADDR (uint8_t *)0xa0000000
#define BOOTLOADER_LOADADDR (uint8_t *)0xa0200000
#define PHYSICAL_INVOKER_INSTALLADDR (uint8_t *)0xa8000000
// PA
#define BOOTLOADER_LOADADDR_PA (uint8_t *)0x40200000
// VA
uint8_t *bootloader_preload_address = NULL;
uint8_t *bootloader_load_address = NULL;
uint8_t *physical_invoker_install_address = NULL;
// PA
uint8_t *bootloader_load_address_pa = NULL;
DWORD fileSize;
int row;
int screenW;
int screenH;
static void outputDebugMessage(const wchar_t *format, ...)
{
wchar_t buffer[256] = {0};
va_list args;
RECT rcScreen = {.left = 0, .top = 0, .right = screenW, .bottom = screenH};
va_start(args, format);
vswprintf(buffer, format, args);
va_end(args);
OutputDebugString(buffer);
ExtTextOut(GetDC(NULL), 0, row * 14, ETO_CLIPPED, &rcScreen, buffer, wcslen(buffer), NULL);
row++;
}
static void disableInterrupts()
{
asm volatile("mrs r0, cpsr\n"
"orr r0,r0,#0x80\n"
"msr cpsr_c,r0\n"
"mov r0,#1" ::
: "r0");
}
extern "C" void EDNA2_physicalInvoker();
static void EnableMemoryRemapGen1()
{
*TMPA910_REMAP_REGISTER_ADDRESS = 1;
outputDebugMessage(L"BrainLILO: memory remap enable for Gen1\n");
}
static void EDNA2_installPhysicalInvoker(BrainGen gen)
{
if (gen == Gen1)
{
EnableMemoryRemapGen1();
}
outputDebugMessage(L"BrainLILO: copying PhysicalInvoker to 0x%p from 0x%p\n", physical_invoker_install_address,
&EDNA2_physicalInvoker);
memcpy(physical_invoker_install_address, (const void *)&EDNA2_physicalInvoker, 64 * 4);
// clearCache();
}
__attribute__((noreturn)) static void EDNA2_runPhysicalInvoker()
{
// r0=info
asm volatile("msr cpsr_c, #211\n" // to supervisor mode
"mrc p15,0,r0,c1,c0,0\n" // read ctrl regs
"bic r0, r0, #8192\n" // reset vector to lower
"mcr p15,0,r0,c1,c0,0\n" // write ctrl regs
);
memcpy(bootloader_load_address, bootloader_preload_address, fileSize);
asm volatile("ldr r0, =0x0000\n"
"ldr r1, =0x0000\n"
"ldr r2, =0x0000\n"
"ldr r3, =0x0000\n"
"ldr r4, =0x0000\n"
"ldr r5, =0x0000\n"
"ldr r6, =0x0000\n"
"ldr r7, =0x0000\n"
"mov r8, %[textbase]\n"
"ldr r9, =0x0000\n"
"mrc p15,0,r10,c1,c0,0\n" // read ctrl regs
"bic r10, r10, #5\n" // disable MMU/DCache
"mcr p15,0,r10,c1,c0,0\n" // write ctrl regs
"swi #0\n" // jump!
: /* no outputs */
: [textbase] "r"(bootloader_load_address_pa)
: "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10");
// never reach here
while (true)
;
}
__attribute__((noreturn)) static DWORD EDNA2_callKernelEntryPoint(BrainGen gen)
{
outputDebugMessage(L"BrainLILO: disabling interrupts");
disableInterrupts();
outputDebugMessage(L"BrainLILO: injecting code to internal ram");
EDNA2_installPhysicalInvoker(gen);
outputDebugMessage(L"BrainLILO: invoking");
Sleep(1000);
EDNA2_runPhysicalInvoker();
}
static void ShowMessage(std::wstring msg, std::wstring title, UINT typ)
{
MessageBox(NULL, msg.c_str(), title.c_str(), typ);
}
static void SetAddress(BrainGen gen)
{
switch (gen)
{
case Gen1:
// VA
bootloader_preload_address = BOOTLOADER_PRELOADADDR_GEN1;
bootloader_load_address = BOOTLOADER_LOADADDR_GEN1;
physical_invoker_install_address = PHYSICAL_INVOKER_INSTALLADDR_GEN1;
// PA
bootloader_load_address_pa = BOOTLOADER_LOADADDR_GEN1_PA;
break;
default:
// Gen2?
// VA
bootloader_preload_address = BOOTLOADER_PRELOADADDR;
bootloader_load_address = BOOTLOADER_LOADADDR;
physical_invoker_install_address = PHYSICAL_INVOKER_INSTALLADDR;
// PA
bootloader_load_address_pa = BOOTLOADER_LOADADDR_PA;
}
}
static BrainGen SelectGen(std::wstring model)
{
BrainGen brainGen = UnknownGen;
if (model == L"gen1.bin")
{
brainGen = Gen1;
}
else if (model == L"u-boot.bin")
{
brainGen = UnknownGen;
}
else
{
brainGen = Gen2Or3;
}
return brainGen;
}
static bool doLinux()
{
std::wifstream iVersion;
std::wstring line, model;
std::wregex modelRe(L"[A-Z]{2}-[A-Z0-9]+");
std::wsmatch match;
BrainGen brain_gen = UnknownGen;
std::wstring fn(L"\\Storage Card\\loader\\");
HANDLE hUBoot;
DWORD wReadSize;
outputDebugMessage(L"BrainLILO: Version " CONFIG_VERSION);
iVersion.open("\\NAND\\version.txt");
while (getline(iVersion, line))
{
if (regex_search(line, match, modelRe))
{
model = match[0].str();
break;
}
}
if (model.length() == 0)
{
outputDebugMessage(L"BrainLILO: failed to match the model name");
MessageBox(NULL, L"Failed to match the model name", L"BrainLILO", MB_ICONWARNING);
return false;
}
outputDebugMessage(L"BrainLILO: internal model name: %s", model.c_str());
auto iter = models.find(model);
if (iter != models.end())
{
model = iter->second;
}
else
{
outputDebugMessage(L"BrainLILO: internal model name %s is unknown, falling back to u-boot.bin", model.c_str());
model = L"u-boot.bin";
}
fn += model;
outputDebugMessage(L"BrainLILO: opening Bootloader file: %s", fn.c_str());
brain_gen = SelectGen(model);
SetAddress(brain_gen);
hUBoot = CreateFile(fn.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hUBoot == INVALID_HANDLE_VALUE)
{
outputDebugMessage(L"BrainLILO: could not open the bootloader: %s", fn.c_str());
ShowMessage(L"Could not open the bootloader: " + fn, L"BrainLILO", MB_ICONWARNING);
return false;
}
outputDebugMessage(L"BrainLILO: bootloader file handle 0x%p", hUBoot);
fileSize = GetFileSize(hUBoot, NULL);
outputDebugMessage(L"BrainLILO: bootloader file size %d Byte", fileSize);
outputDebugMessage(L"BrainLILO: preloading bootloader to 0x%p...", bootloader_preload_address);
if (!ReadFile(hUBoot, (void *)bootloader_preload_address, fileSize, &wReadSize, NULL))
{
outputDebugMessage(L"BrainLILO: could not read the bootloader");
ShowMessage(L"Could not read the bootloader", L"BrainLILO", MB_ICONWARNING);
return false;
}
outputDebugMessage(L"BrainLILO: bootloader copied! Closing file handle...");
CloseHandle(hUBoot);
outputDebugMessage(L"BrainLILO: notifying power off to filesystems...");
if (FileSystemPowerFunction)
FileSystemPowerFunction(FSNOTIFY_POWER_OFF);
outputDebugMessage(L"BrainLILO: starting bootloader call sequence...");
EDNA2_callKernelEntryPoint(brain_gen);
return true;
}
extern "C" BRAINLILODRV_API BOOL LIN_IOControl(DWORD handle, DWORD dwIoControlCode, DWORD *pInBuf, DWORD nInBufSize,
DWORD *pOutBuf, DWORD nOutBufSize, PDWORD pBytesReturned)
{
SetLastError(0);
switch (dwIoControlCode)
{
case IOCTL_LIN_DO_LINUX:
if (!doLinux())
{
if (FileSystemPowerFunction)
FileSystemPowerFunction(FSNOTIFY_POWER_ON);
return FALSE;
}
return TRUE;
}
return FALSE;
}
extern "C" BRAINLILODRV_API BOOL LIN_Read(DWORD handle, LPVOID pBuffer, DWORD dwNumBytes)
{
SetLastError(ERROR_INVALID_FUNCTION);
return FALSE;
}
extern "C" BRAINLILODRV_API BOOL LIN_Write(DWORD handle, LPVOID pBuffer, DWORD dwNumBytes)
{
SetLastError(ERROR_INVALID_FUNCTION);
return FALSE;
}
extern "C" BRAINLILODRV_API DWORD LIN_Seek(DWORD handle, long lDistance, DWORD dwMoveMethod)
{
SetLastError(ERROR_INVALID_FUNCTION);
return FALSE;
}
extern "C" BRAINLILODRV_API void LIN_PowerUp(void)
{
OutputDebugString(L"BrainLILO: resuming.");
}
extern "C" BRAINLILODRV_API void LIN_PowerDown(void)
{
}
extern "C" BRAINLILODRV_API DWORD LIN_Init(LPCTSTR pContext, DWORD dwBusContext)
{
void *ctx;
ctx = (void *)LocalAlloc(LPTR, sizeof(4));
screenW = GetSystemMetrics(SM_CXVIRTUALSCREEN);
screenH = GetSystemMetrics(SM_CYVIRTUALSCREEN);
return (DWORD)ctx;
}
extern "C" BRAINLILODRV_API DWORD LIN_Open(DWORD dwData, DWORD dwAccess, DWORD dwShareMode)
{
void *hnd = (void *)LocalAlloc(LPTR, 4);
row = 0;
return (DWORD)hnd;
}
extern "C" BRAINLILODRV_API BOOL LIN_Close(DWORD handle)
{
LocalFree((void *)handle);
return TRUE;
}
extern "C" BRAINLILODRV_API BOOL LIN_Deinit(DWORD dwContext)
{
LocalFree((void *)dwContext);
return TRUE;
}
extern "C" BOOL APIENTRY DllMainCRTStartup(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
FileSystemPowerFunction =
(FileSystemPowerFunctionProc)GetProcAddress(LoadLibrary(L"COREDLL"), L"FileSystemPowerFunction");
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}