-
Notifications
You must be signed in to change notification settings - Fork 4
/
gsuploader.c
498 lines (405 loc) · 13.7 KB
/
gsuploader.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include <stdio.h>
#include <string.h>
#include "mips.h"
#define UPLOAD_ADDR 0xA0300000 //Where to upload your code, and also where execution starts after embedded code runs.
int UploadFile(FILE *infile, unsigned long address);
long GetFileSizez(FILE *input);
unsigned char GAMESHARK_COMMS_INIT_GOT[4];
unsigned char GAMESHARK_CHECK_GSBUTTON_GOT[4];
#ifdef LINUX
#include <sys/io.h>
#define _outp(x,y) outb(y,x)
#define _inp(x) inb(x)
#else
//Dev-Cpp conio.h does not define these.
void _outp(unsigned short address, unsigned char data);
unsigned char _inp(unsigned short address);
#endif
/* Embedded pre-setup code
This is a lazy way of doing things, but it works. This code gets executed and sets
up the environment prior to loading your usercode.
*/
unsigned long codebuf_pre[]=
{
/* Stop GameShark traps */
MTC0(MIPS_R0, 18),
NOP,
MTC0(MIPS_R0, 19),
NOP,
/* Disable Interrupts */
MFC0(MIPS_T0, 12),
MIPS_ADDIU(MIPS_T1, MIPS_R0, 0xfffe),
MIPS_AND(MIPS_T0, MIPS_T0, MIPS_T1),
MTC0(MIPS_T0, 12),
/* Modify EPC */
LUI(MIPS_K0, UPLOAD_ADDR>>16),
ORI(MIPS_K0, MIPS_K0, UPLOAD_ADDR),
NOP,
MTC0(MIPS_K0, 14),
NOP,
/* Patch back modified code handler */
LUI(MIPS_K1, 0x3c1a),
ORI(MIPS_K1, MIPS_K1, 0x8000),
NOP,
LUI(MIPS_K0, 0xa07c),
ORI(MIPS_K0, MIPS_K0, 0x5c00),
NOP,
SW(MIPS_K1, 0, MIPS_K0),
NOP,
SYNC,
NOP,
/* Halt RSP */
LUI(MIPS_T1, 2),
LUI(MIPS_T0, 0xa404),
ORI(MIPS_T0, MIPS_T0, 0x0010),
NOP,
SW(MIPS_T1, 0, MIPS_T0),
NOP,
/* Halt RDP */
LUI(MIPS_T1, 1|4|0x10|0x40|0x80|0x100|0x200),
LUI(MIPS_T0, 0xa410),
ORI(MIPS_T0, MIPS_T0, 0x000c),
NOP,
SW(MIPS_T1, 0, MIPS_T0),
NOP,
/* Return from interrupt - execute code */
ERET,
NOP,
/* Callback function: Start GScomms */
GAMESHARK_COMMS_INIT, //GScomms Marker
NOP,
MFC0(MIPS_T0, 12),
MIPS_ADDIU(MIPS_T1, MIPS_R0, 0xfffe),
MIPS_AND(MIPS_T0, MIPS_T0, MIPS_T1),
MTC0(MIPS_T0, 12),
LUI(MIPS_V0,0xa000),
ORI(MIPS_A2,MIPS_V0,0x180),
LW(MIPS_A0,0,MIPS_A2),
ORI(MIPS_V1,MIPS_V0,0x120),
SW(MIPS_A0,0, MIPS_V1),
ORI(MIPS_A1,MIPS_V0,0x184),
LW(MIPS_A0,0,MIPS_A1),
ORI(MIPS_V1,MIPS_V0,0x124),
SW(MIPS_A0,0,MIPS_V1),
ORI(MIPS_A0,MIPS_V0,0x188),
LW(MIPS_A3,0,MIPS_A0),
ORI(MIPS_V1,MIPS_V0,0x128),
SW(MIPS_A3,0,MIPS_V1),
ORI(MIPS_V1,MIPS_V0,0x18c),
LW(MIPS_A3,0,MIPS_V1),
ORI(MIPS_V0,MIPS_V0,0x12c),
SW(MIPS_A3,0,MIPS_V0),
LUI(MIPS_V0,0x3c1a),
ORI(MIPS_V0,MIPS_V0,0xa079),
SW(MIPS_V0,0,MIPS_A2),
LUI(MIPS_V0,0x275a),
ORI(MIPS_V0,MIPS_V0,0x4aec),
SW( MIPS_V0,0,MIPS_A1),
LUI(MIPS_V0,0x340),
ORI(MIPS_V0,MIPS_V0,0x8),
SW(MIPS_V0,0,MIPS_A0),
SW(MIPS_R0,0,MIPS_V1),
LUI(MIPS_T1,0xa000),
ORI(MIPS_T1,MIPS_T1,0x120),
CACHE(MIPS_T1, 0x10, 0),
NOP,
LUI(MIPS_T1,0xa000),
ORI(MIPS_T1,MIPS_T1,0x124),
CACHE(MIPS_T1, 0x10, 0),
NOP,
LUI(MIPS_T1,0xa000),
ORI(MIPS_T1,MIPS_T1,0x128),
CACHE(MIPS_T1, 0x10, 0),
NOP,
LUI(MIPS_T1,0xa000),
ORI(MIPS_T1,MIPS_T1,0x12c),
CACHE(MIPS_T1, 0x10, 0),
NOP,
LUI(MIPS_T1,0xa000),
ORI(MIPS_T1,MIPS_T1,0x180),
CACHE(MIPS_T1, 0x10, 0),
NOP,
LUI(MIPS_T1,0xa000),
ORI(MIPS_T1,MIPS_T1,0x184),
CACHE(MIPS_T1, 0x10, 0),
NOP,
LUI(MIPS_T1,0xa000),
ORI(MIPS_T1,MIPS_T1,0x188),
CACHE(MIPS_T1, 0x10, 0),
NOP,
LUI(MIPS_T1,0xa000),
ORI(MIPS_T1,MIPS_T1,0x18c),
CACHE(MIPS_T1, 0x10, 0),
MFC0(MIPS_T0, 12),
ORI(MIPS_T0, MIPS_T0, 1),
MTC0(MIPS_T0, 12),
JR(MIPS_RA),
NOP,
GAMESHARK_CHECK_GSBUTTON,
LUI(MIPS_V0, 0xbe40),
LBU(MIPS_V0, 0, MIPS_V0),
SRA(MIPS_V0, MIPS_V0, 0x2),
XORI(MIPS_V0, MIPS_V0, 0x1),
ANDI(MIPS_V0, MIPS_V0, 0x1),
JR(MIPS_RA),
NOP,
};
int main(int argc, char ** argv)
{
int i = 0;
unsigned char *pointprebuf=(unsigned char *)&codebuf_pre;
unsigned char codebuf[sizeof(codebuf_pre)];
#ifdef LINUX
int ret = ioperm(LPT1, 10, 1);
if (ret < 0)
err(1, "ioperm");
#endif
printf("\nN64 HomeBrew Loader - ppcasm\n(Based on HCS GSUpload - http://here.is/halleyscomet)\n\n");
if(argc!=2)
{
printf("Wrong Usage:\n(Homebrew Uploader): %s <binary>\n(Disassembler/Debugger): %s -d", argv[0], argv[0]);
Out32(LPT1, 0);
return 1;
}
if(!strcmp(argv[1], "-d"))
{
//debugger();
return 0;
}
if(UPLOAD_ADDR-sizeof(codebuf_pre)<=0x80000190||UPLOAD_ADDR-sizeof(codebuf_pre)<=0xA0000190)
{
printf("\n\nWARNING: Possible pre-setup code and interrupt handler collision!\n\n");
}
if(GLOBAL_OFFSET_TABLE<=0x80000190||GLOBAL_OFFSET_TABLE<=0xA0000190)
{
printf("\n\nWARNING: Possible Global Offset Table and interrupt handler collision!\n\n");
}
//export_funcs(); //Build GOT export list for built in functions.
FILE* infile=fopen(argv[1], "rb");
if(!infile)
{
printf("error opening %s\n", argv[1]);
return 1;
}
printf("File Size: %d bytes...\n", (int)GetFileSizez(infile));
for(i=0;i<=sizeof(codebuf_pre)/sizeof(unsigned long);i++)
{
codebuf_pre[i] = (codebuf_pre[i]<<24&0xff000000)|(codebuf_pre[i]<<8&0x00ff0000)|(codebuf_pre[i]>>8&0x0000ff00)|(codebuf_pre[i]>>24&0xff);
}
for(i=0;i<=sizeof(codebuf);i++)
{
codebuf[i]=pointprebuf[i];
}
/*Upload binary to specified address.*/
if(UploadFile(infile,UPLOAD_ADDR))
{
printf("Uploading file failed...\n");
Out32(LPT1, 0);
return 1;
}
/*Set up embedded code to right before usercode.*/
unsigned long EMBED_ADDR = UPLOAD_ADDR-sizeof(codebuf);
/*Upload embedded code right before usercode.*/
if(Upload(codebuf, sizeof(codebuf), EMBED_ADDR))
{
printf("Failed to upload embedded code...\n");
Out32(LPT1, 0);
return 1;
}
printf("Uploaded embedded code to: 0x%08x.\n", (unsigned int)EMBED_ADDR);
/*Make synthetic jump instruction based on address.*/
unsigned long instruction=makejump(EMBED_ADDR);
/*Unload jump instruction into byte buffer for easy transfer.*/
unsigned char insn[4];
insn[0]=instruction>>24&0xff;
insn[1]=instruction>>16&0xff;
insn[2]=instruction>>8&0xff;
insn[3]=instruction>>0&0xff;
/*Inject synthetic jump instruction into code handler to insure it runs.*/
if(Upload(insn,4,INSN_PATCH_ADDR))
{
printf("Instruction patch failed...\n");
Out32(LPT1, 0);
return 1;
}
printf("Done.\n");
fclose(infile);
return 0;
}
void export_funcs(void)
{
printf("\nDbg: Function Callbacks:\n");
printf("\nGlobal Offset Table base address: %x\n", GLOBAL_OFFSET_TABLE);
int i = 0;
for(i=0;i<=sizeof(codebuf_pre)/4;i++)
{
if(codebuf_pre[i]==GAMESHARK_COMMS_INIT)
{
int address = UPLOAD_ADDR-sizeof(codebuf_pre)+i*4+4;
unsigned long make_jump_got = J(address);
GAMESHARK_COMMS_INIT_GOT[0]=make_jump_got>>24&0xff;
GAMESHARK_COMMS_INIT_GOT[1]=make_jump_got>>16&0xff;
GAMESHARK_COMMS_INIT_GOT[2]=make_jump_got>>8&0xff;
GAMESHARK_COMMS_INIT_GOT[3]=make_jump_got>>0&0xff;
if(Upload(GAMESHARK_COMMS_INIT_GOT,4,GLOBAL_OFFSET_TABLE+GOT_OFFSET))
{
printf("GOT patch failed...\n");
Out32(LPT1, 0);
return;
}
printf("0x%08x: void gscomms_init(void) - GOT: %x\n", address, GLOBAL_OFFSET_TABLE+GOT_OFFSET);
GOT_OFFSET+=4;
}
if(codebuf_pre[i]==GAMESHARK_CHECK_GSBUTTON)
{
int address = UPLOAD_ADDR-sizeof(codebuf_pre)+i*4+4;
unsigned long make_jump_got = J(address);
GAMESHARK_CHECK_GSBUTTON_GOT[0]=make_jump_got>>24&0xff;
GAMESHARK_CHECK_GSBUTTON_GOT[1]=make_jump_got>>16&0xff;
GAMESHARK_CHECK_GSBUTTON_GOT[2]=make_jump_got>>8&0xff;
GAMESHARK_CHECK_GSBUTTON_GOT[3]=make_jump_got>>0&0xff;
if(Upload(GAMESHARK_CHECK_GSBUTTON_GOT,4,GLOBAL_OFFSET_TABLE+GOT_OFFSET))
{
printf("GOT patch failed...\n");
Out32(LPT1, 0);
return;
}
printf("0x%08x: int check_gsbutton(void) - GOT: %x\n", address, GLOBAL_OFFSET_TABLE+GOT_OFFSET);
GOT_OFFSET+=4;
}
}
printf("\n");
return;
}
long GetFileSizez(FILE *input)
{
long current, end;
current = ftell(input);
fseek(input, 0, 2);
end = ftell(input);
fseek(input, current, 0);
return end;
}
int InitGSComms(void) {
int timeout=TMOUT;
while (GSFcn1(3) && timeout) {
timeout--;
}
if (!timeout) {printf("init failed\n"); return 1;}
if (!CheckGSPresence()) {printf("init failed2\n"); return 1;}
return 0;
}
int CloseGSComms(void) {
if (GSFcn1(3)) return 1;
if (!CheckGSPresence()) return 1;
ReadWriteByte(0x64);
return 0;
}
int Read(unsigned char * buffer, unsigned long size, unsigned long address) {
unsigned long c=0;
if (InitGSComms()) return 1;
ReadWriteByte(1);
ReadWrite32(address);
ReadWrite32(size);
for (c=0; c < size; c++) buffer[c]=ReadWriteByte(0);
for (c=0; c < 8; c++) ReadWriteByte(0);
if (CloseGSComms()) return 1;
return 0;
}
int Upload(const unsigned char * buffer, unsigned long size, unsigned long address) {
unsigned long c=0;
if (InitGSComms()) return 1;
ReadWriteByte(2);
ReadWrite32(address);
ReadWrite32(size);
for (c=0; c < size; c++) ReadWriteByte(buffer[c]);
for (c=0; c < 8; c++) ReadWriteByte(0);
if (CloseGSComms()) return 1;
return 0;
}
int UploadFile(FILE * infile, unsigned long address) {
unsigned long c=0;
unsigned long size;
char buf;
if (fseek(infile,0,SEEK_END)) return 1;
size=ftell(infile);
rewind(infile);
if (InitGSComms()) return 1;
printf("Uploading File to %x | 00%%", (int)address);
ReadWriteByte(2);
ReadWrite32(address);
ReadWrite32(size);
for (c=0; c < size; c++) {
fread(&buf,1,1,infile);
if (c & 0x400) printf("\b\b\b%02lu%%", c*100/size);
ReadWriteByte(buf);
}
for (c=0; c < 8; c++) ReadWriteByte(0);
if (CloseGSComms()) return 1;
printf("\b\b\bDone.\n");
return 0;
}
/* my guess: used to get the GS and PC sync'd */
int GSFcn1(int x) {
int timeout=TMOUT;
unsigned char result;
while (timeout) {
result<<=4;
result|=SendNibble(x);
/* when we recieve a 6, then a 7, we're sync'd */
if (result==0x67) break;
SendNibble(x);
SendNibble(x>>4);
timeout--;
}
if (!timeout) return 1;
return 0;
}
int SendNibble(int x) {
int timeout=TMOUT,retval;
Out32(LPT1,(x&0x0f)|0x10);
while (timeout && Inp32(LPT1+1)&8) timeout--;
retval=(Inp32(LPT1+1)&(0xf0^0x80))>>4;
Out32(LPT1,0);
timeout=TMOUT;
while (timeout && Inp32(LPT1+1)&8) timeout--;
if (!timeout) {/*printf("SendNibble timed out2\n");*/ return 0;}
else return retval;
}
int ReadWriteNibble(int x) {
int retval;
Out32(LPT1,(x&0x0f)|0x10);
while ((~Inp32(LPT1+1))&8);
while ((~Inp32(LPT1+1))&8);
retval=(Inp32(LPT1+1)&(0xf0^0x80))>>4;
Out32(LPT1,0);
while (Inp32(LPT1+1)&8);
while (Inp32(LPT1+1)&8);
return retval;
}
int ReadWriteByte(int x) {
return (ReadWriteNibble(x>>4)<<4)|ReadWriteNibble(x);
}
unsigned long ReadWrite32(unsigned long x) {
return ((unsigned long)ReadWriteNibble(x>>28)<<28)|
((unsigned long)ReadWriteNibble(x>>24)<<24)|
((unsigned long)ReadWriteNibble(x>>20)<<20)|
((unsigned long)ReadWriteNibble(x>>16)<<16)|
((unsigned long)ReadWriteNibble(x>>12)<<12)|
((unsigned long)ReadWriteNibble(x>>8)<<8)|
((unsigned long)ReadWriteNibble(x>>4)<<4)|
(unsigned long)ReadWriteNibble(x);
}
/* perhaps this is what actually freezes operation? */
int CheckGSPresence(void) {
int timeout=TMOUT,result;
while (timeout) {
result=(ReadWriteNibble(0x47>>4)<<4)|ReadWriteNibble(0x47);
timeout--;
if (result != 0x67) continue;
result=(ReadWriteNibble(0x54>>4)<<4)|ReadWriteNibble(0x54);
if (result == 0x74) break;
}
if (timeout) return 1;
return 0;
}