/
loader.c
305 lines (257 loc) · 9.16 KB
/
loader.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
294
295
296
297
298
299
300
301
302
303
304
305
#include "loader.h"
#define RW_MEM_MAP 0xA0000000
#if VER == 300
#include "codehandler310.h" //TODO ???
#define INSTALL_ADDR 0x011D3000
#define MAIN_JMP_ADDR 0x0101894C
#elif VER == 310
#include "codehandler310.h"
#define INSTALL_ADDR 0x011D3000
#define MAIN_JMP_ADDR 0x0101894C
#elif VER == 400
#include "codehandler400.h"
#define INSTALL_ADDR 0x011DD000
#define MAIN_JMP_ADDR 0x0101BD4C
#elif VER == 410
#include "codehandler410.h"
#define INSTALL_ADDR 0x011DD000
#define MAIN_JMP_ADDR 0x0101C55C
#elif VER == 500
#include "codehandler500.h"
#define INSTALL_ADDR 0x011DD000
#define MAIN_JMP_ADDR 0x0101C55C
#elif VER == 532
#include "codehandler532.h"
#define INSTALL_ADDR 0x011DD000
#define MAIN_JMP_ADDR 0x0101C55C
#elif VER == 550
#include "codehandler550.h"
#define INSTALL_ADDR 0x011DD000
#define MAIN_JMP_ADDR 0x0101C56C
#endif
#define assert(x) \
do { \
if (!(x)) \
OSFatal("Assertion failed " #x ".\n"); \
} while (0)
#define ALIGN_BACKWARD(x,align) \
((typeof(x))(((unsigned int)(x)) & (~(align-1))))
int doBL( unsigned int dst, unsigned int src );
void doOSScreenInit(unsigned int coreinit_handle);
inline void doOSScreenClear();
void doOSScreenPrintPos(char *buf, int pos);
void doVPADWait();
void _main(){
/* Get a handle to coreinit.rpl. */
unsigned int coreinit_handle;
OSDynLoad_Acquire("coreinit.rpl", &coreinit_handle);
/* Get for later socket patch */
unsigned int nsysnet_handle;
OSDynLoad_Acquire("nsysnet.rpl", &nsysnet_handle);
/* Get for IP address print */
unsigned int nn_ac_handle;
OSDynLoad_Acquire("nn_ac.rpl", &nn_ac_handle);
/* Load a few useful symbols. */
void*(*OSEffectiveToPhysical)(const void *);
void*(*OSAllocFromSystem)(uint32_t size, int align);
void (*OSFreeToSystem)(void *ptr);
void (*DCFlushRange)(const void *, int);
void (*ICInvalidateRange)(const void *, int);
void (*_Exit)(void) __attribute__ ((noreturn));
OSDynLoad_FindExport(coreinit_handle, 0, "OSEffectiveToPhysical", &OSEffectiveToPhysical);
OSDynLoad_FindExport(coreinit_handle, 0, "OSAllocFromSystem", &OSAllocFromSystem);
OSDynLoad_FindExport(coreinit_handle, 0, "OSFreeToSystem", &OSFreeToSystem);
OSDynLoad_FindExport(coreinit_handle, 0, "DCFlushRange", &DCFlushRange);
OSDynLoad_FindExport(coreinit_handle, 0, "ICInvalidateRange", &ICInvalidateRange);
OSDynLoad_FindExport(coreinit_handle, 0, "_Exit", &_Exit);
assert(OSEffectiveToPhysical);
assert(OSAllocFromSystem);
assert(OSFreeToSystem);
assert(DCFlushRange);
assert(ICInvalidateRange);
assert(_Exit);
/* Socket functions */
unsigned int *socket_lib_finish;
OSDynLoad_FindExport(nsysnet_handle, 0, "socket_lib_finish", &socket_lib_finish);
assert(socket_lib_finish);
/* AC functions */
int(*ACGetAssignedAddress)(unsigned int *addr);
OSDynLoad_FindExport(nn_ac_handle, 0, "ACGetAssignedAddress", &ACGetAssignedAddress);
assert(ACGetAssignedAddress);
/* IM functions */
int(*IM_SetDeviceState)(int fd, void *mem, int state, int a, int b);
int(*IM_Close)(int fd);
int(*IM_Open)();
OSDynLoad_FindExport(coreinit_handle, 0, "IM_SetDeviceState", &IM_SetDeviceState);
OSDynLoad_FindExport(coreinit_handle, 0, "IM_Close", &IM_Close);
OSDynLoad_FindExport(coreinit_handle, 0, "IM_Open", &IM_Open);
assert(IM_SetDeviceState);
assert(IM_Close);
assert(IM_Open);
/* Restart system to get lib access */
int fd = IM_Open();
void *mem = OSAllocFromSystem(0x100, 64);
memset(mem, 0, 0x100);
/* set restart flag to force quit browser */
IM_SetDeviceState(fd, mem, 3, 0, 0);
IM_Close(fd);
OSFreeToSystem(mem);
/* wait a bit for browser end */
unsigned int t1 = 0x1FFFFFFF;
while(t1--){};
doOSScreenInit(coreinit_handle);
doOSScreenClear();
doOSScreenPrintPos("TCPGecko Installer", 0);
/* Make sure the kernel exploit has been run */
if (OSEffectiveToPhysical((void *)0xA0000000) == (void *)0){
doOSScreenPrintPos("You must execute the kernel exploit before installing TCPGecko.", 1);
doOSScreenPrintPos("Returning to the home menu...",2);
t1 = 0x3FFFFFFF;
while(t1--) ;
doOSScreenClear();
_Exit();
}else{
doOSScreenPrintPos("Trying to install TCPGecko...", 1);
// Make sure everything has kern I/O
kern_write((void*)KERN_SYSCALL_TBL_1 + (0x34 * 4), KERN_CODE_READ);
kern_write((void*)KERN_SYSCALL_TBL_2 + (0x34 * 4), KERN_CODE_READ);
kern_write((void*)KERN_SYSCALL_TBL_3 + (0x34 * 4), KERN_CODE_READ);
kern_write((void*)KERN_SYSCALL_TBL_4 + (0x34 * 4), KERN_CODE_READ);
kern_write((void*)KERN_SYSCALL_TBL_5 + (0x34 * 4), KERN_CODE_READ);
kern_write((void*)KERN_SYSCALL_TBL_1 + (0x35 * 4), KERN_CODE_WRITE);
kern_write((void*)KERN_SYSCALL_TBL_2 + (0x35 * 4), KERN_CODE_WRITE);
kern_write((void*)KERN_SYSCALL_TBL_3 + (0x35 * 4), KERN_CODE_WRITE);
kern_write((void*)KERN_SYSCALL_TBL_4 + (0x35 * 4), KERN_CODE_WRITE);
kern_write((void*)KERN_SYSCALL_TBL_5 + (0x35 * 4), KERN_CODE_WRITE);
/* Our main writable area */
unsigned int physWriteLoc = (unsigned int)OSEffectiveToPhysical((void*)RW_MEM_MAP);
/* Install codehandler */
unsigned int phys_codehandler_loc = (unsigned int)OSEffectiveToPhysical((void*)INSTALL_ADDR);
void *codehandler_loc = (unsigned int*)(RW_MEM_MAP + (phys_codehandler_loc - physWriteLoc));
memcpy(codehandler_loc, codehandler_text_bin, codehandler_text_bin_len);
DCFlushRange(codehandler_loc, codehandler_text_bin_len);
ICInvalidateRange(codehandler_loc, codehandler_text_bin_len);
/* Patch coreinit jump */
unsigned int phys_main_jmp_loc = (unsigned int)OSEffectiveToPhysical((void*)MAIN_JMP_ADDR);
unsigned int *main_jmp_loc = (unsigned int*)(RW_MEM_MAP + (phys_main_jmp_loc - physWriteLoc));
*main_jmp_loc = doBL(INSTALL_ADDR, MAIN_JMP_ADDR);
DCFlushRange(ALIGN_BACKWARD(main_jmp_loc, 32), 0x20);
ICInvalidateRange(ALIGN_BACKWARD(main_jmp_loc, 32), 0x20);
/* Patch Socket Function */
unsigned int phys_socket_loc = (unsigned int)OSEffectiveToPhysical(socket_lib_finish);
unsigned int *socket_loc = (unsigned int*)(RW_MEM_MAP + (phys_socket_loc - physWriteLoc));
socket_loc[0] = 0x38600000;
socket_loc[1] = 0x4E800020;
DCFlushRange(ALIGN_BACKWARD(socket_loc, 32), 0x40);
ICInvalidateRange(ALIGN_BACKWARD(socket_loc, 32), 0x40);
/* The fix for Splatoon and such */
kern_write((void*)(KERN_ADDRESS_TBL + (0x12 * 4)), 0x00000000);
kern_write((void*)(KERN_ADDRESS_TBL + (0x13 * 4)), 0x14000000);
/* All good! */
unsigned int addr;
ACGetAssignedAddress(&addr);
char buf[64];
__os_snprintf(buf,64,"Success! Your Gecko IP is %i.%i.%i.%i.", (addr>>24)&0xFF,(addr>>16)&0xFF,(addr>>8)&0xFF,addr&0xFF);
doOSScreenPrintPos(buf, 2);
doOSScreenPrintPos("Press any button to return to the home menu.", 3);
doVPADWait();
}
doOSScreenClear();
_Exit();
}
int doBL( unsigned int dst, unsigned int src ){
unsigned int newval = (dst - src);
newval &= 0x03FFFFFC;
newval |= 0x48000001;
return newval;
}
/* for internal and gcc usage */
void* memset(void* dst, const uint8_t val, uint32_t size){
uint32_t i;
for (i = 0; i < size; i++){
((uint8_t*) dst)[i] = val;
}
return dst;
}
void* memcpy(void* dst, const void* src, uint32_t size){
uint32_t i;
for (i = 0; i < size; i++){
((uint8_t*) dst)[i] = ((const uint8_t*) src)[i];
}
return dst;
}
/* Write a 32-bit word with kernel permissions */
void kern_write(void *addr, uint32_t value){
asm(
"li 3,1\n"
"li 4,0\n"
"mr 5,%1\n"
"li 6,0\n"
"li 7,0\n"
"lis 8,1\n"
"mr 9,%0\n"
"mr %1,1\n"
"li 0,0x3500\n"
"sc\n"
"nop\n"
"mr 1,%1\n"
:
: "r"(addr), "r"(value)
: "memory", "ctr", "lr", "0", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12"
);
}
/* OSScreen helper functions */
void doOSScreenInit(unsigned int coreinit_handle){
/* OSScreen functions */
void(*OSScreenInit)();
unsigned int(*OSScreenGetBufferSizeEx)(unsigned int bufferNum);
unsigned int(*OSScreenSetBufferEx)(unsigned int bufferNum, void * addr);
OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenInit", &OSScreenInit);
OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenGetBufferSizeEx", &OSScreenGetBufferSizeEx);
OSDynLoad_FindExport(coreinit_handle, 0, "OSScreenSetBufferEx", &OSScreenSetBufferEx);
assert(OSScreenInit);
assert(OSScreenGetBufferSizeEx);
assert(OSScreenSetBufferEx);
/* Call the Screen initilzation function */
OSScreenInit();
/* Grab the buffer size for each screen (TV and gamepad) */
int buf0_size = OSScreenGetBufferSizeEx(0);
int buf1_size = OSScreenGetBufferSizeEx(1);
/* Set the buffer area */
OSScreenSetBufferEx(0, (void *)0xF4000000);
OSScreenSetBufferEx(1, (void *)0xF4000000 + buf0_size);
}
inline void doOSScreenClear(){
/* Clear both framebuffers */
int ii;
for (ii = 0; ii < 2; ii++){
fillScreen(0,0,0,0);
flipBuffers();
}
}
void doOSScreenPrintPos(char *buf, int pos){
int i;
for(i=0;i<2;i++){
drawString(0,pos,buf);
flipBuffers();
}
}
void doVPADWait(){
unsigned int vpad_handle;
OSDynLoad_Acquire("vpad.rpl", &vpad_handle);
/* VPAD functions */
int(*VPADRead)(int controller, VPADData *buffer, unsigned int num, int *error);
OSDynLoad_FindExport(vpad_handle, 0, "VPADRead", &VPADRead);
assert(VPADRead);
int error;
VPADData vpad_data;
/* Read initial vpad status */
VPADRead(0, &vpad_data, 1, &error);
while(1){
VPADRead(0, &vpad_data, 1, &error);
if(vpad_data.btn_hold){
break;
}
}
}