/
main.c
412 lines (326 loc) · 10.8 KB
/
main.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
//64DD Dumper
//Based on pfs SDK demo
#include <ultra64.h>
#include <PR/leo.h>
#include "thread.h"
#include "textlib.h"
#include "ncode.h"
#include <math.h>
#include <string.h>
#include "fat32.h"
#include "ci.h"
#include "asicdrive.h"
#include "leohax.h"
#define NUM_MESSAGE 1
#define CONT_VALID 1
#define CONT_INVALID 0
#define WAIT_CNT 5
extern OSMesgQueue retraceMessageQ;
extern u16 *bitmap_buf;
extern u16 bitmap_buf1[];
extern u16 bitmap_buf2[];
extern u8 backup_buffer[];
OSMesgQueue pifMesgQueue;
OSMesg dummyMessage;
OSMesg pifMesgBuf[NUM_MESSAGE];
static OSContStatus contstat[MAXCONTROLLERS];
static OSContPad contdata[MAXCONTROLLERS];
static int controller[MAXCONTROLLERS];
OSPfs pfs0;
//DMA PI
#define DMA_QUEUE_SIZE 2
OSMesgQueue dmaMessageQ;
OSMesg dmaMessageBuf[DMA_QUEUE_SIZE];
OSIoMesg dmaIOMessageBuf;
OSIoMesg dmaIoMesgBuf;
OSPiHandle *pi_handle;
OSPiHandle *pi_ddrom_handle;
OSPiHandle *LeoDiskHandle;
#include "leotest.h"
#define NUM_LEO_MESGS 8
static OSMesg LeoMessages[NUM_LEO_MESGS];
static u8 blockData[0x1000];
static u32 IPLoffset;
//FAT
//fat_dirent de_root;
fat_dirent de_current;
fat_dirent de_next;
char fat_message[50];
short fail;
int fs_fail()
{
char wait_message[50];
sprintf(wait_message, "\n Error (%i): %s", fail, fat_message);
draw_puts(wait_message);
return 0;
}
void fat_start_ipl(void)
{
char filenamestr[20];
char filenamelogstr[20];
char console_text[50];
fat_dirent de_root;
fat_dirent user_dump;
float fw = ((float)ciGetRevision())/100.0f;
u32 device_magic = ciGetMagic();
u32 is64drive = (ciGetMagic() == 0x55444556);
if(fw < 2.00 || fw > 20.00 || !is64drive){
draw_puts("\n - ERROR WRITING TO MEMORY CARD:\n 64DRIVE with FIRMWARE 2.00 or later is required!");
while(1);
}
sprintf(console_text, "\n - 64drive with firmware %.2f found", fw);
draw_puts(console_text);
sprintf(filenamestr, "64DD_IPL.n64");
sprintf(console_text, "\n - Writing %s to memory card", filenamestr);
draw_puts(console_text);
// get FAT going
if(fat_init() != 0){
fail = 100;
fs_fail();
return;
}
// start in root directory
fat_root_dirent(&de_root);
if(fail != 0){
fs_fail();
return;
}
if( fat_find_create(filenamestr, &de_root, &user_dump, 0, 1) != 0){
sprintf(fat_message, "Failed to create image dump"); fail = 3;
fs_fail(); return;
}
if( fat_set_size(&user_dump, 0x400000) != 0) {
sprintf(fat_message, "Failed to resize dump"); fail = 4;
fs_fail(); return;
}
loadRamToRom(0x0, user_dump.start_cluster);
}
void fat_start_h8(void)
{
char filenamestr[20];
char filenamelogstr[20];
char console_text[50];
fat_dirent de_root;
fat_dirent user_dump;
float fw = ((float)ciGetRevision())/100.0f;
u32 device_magic = ciGetMagic();
u32 is64drive = (ciGetMagic() == 0x55444556);
if(fw < 2.00 || fw > 20.00 || !is64drive){
draw_puts("\n - ERROR WRITING TO MEMORY CARD:\n 64DRIVE with FIRMWARE 2.00 or later is required!");
while(1);
}
//sprintf(console_text, "\n - 64drive with firmware %.2f found", fw);
//draw_puts(console_text);
sprintf(filenamestr, "64DD_H8ROM.rom");
sprintf(console_text, "\n - Writing %s to memory card", filenamestr);
draw_puts(console_text);
// get FAT going
if(fat_init() != 0){
fail = 100;
fs_fail();
return;
}
// start in root directory
fat_root_dirent(&de_root);
if(fail != 0){
fs_fail();
return;
}
if( fat_find_create(filenamestr, &de_root, &user_dump, 0, 1) != 0){
sprintf(fat_message, "Failed to create image dump"); fail = 3;
fs_fail(); return;
}
if( fat_set_size(&user_dump, 0x8000) != 0) {
sprintf(fat_message, "Failed to resize dump"); fail = 4;
fs_fail(); return;
}
loadRamToRom(0x0, user_dump.start_cluster);
}
void fat_start_reg(void)
{
char filenamestr[20];
char filenamelogstr[20];
char console_text[50];
fat_dirent de_root;
fat_dirent user_dump;
float fw = ((float)ciGetRevision())/100.0f;
u32 device_magic = ciGetMagic();
u32 is64drive = (ciGetMagic() == 0x55444556);
if(fw < 2.00 || fw > 20.00 || !is64drive){
draw_puts("\n - ERROR WRITING TO MEMORY CARD:\n 64DRIVE with FIRMWARE 2.00 or later is required!");
while(1);
}
//sprintf(console_text, "\n - 64drive with firmware %.2f found", fw);
//draw_puts(console_text);
sprintf(filenamestr, "64DD_REGDUMP.bin");
sprintf(console_text, "\n - Writing %s to memory card", filenamestr);
draw_puts(console_text);
// get FAT going
if(fat_init() != 0){
fail = 100;
fs_fail();
return;
}
// start in root directory
fat_root_dirent(&de_root);
if(fail != 0){
fs_fail();
return;
}
if( fat_find_create(filenamestr, &de_root, &user_dump, 0, 1) != 0){
sprintf(fat_message, "Failed to create image dump"); fail = 3;
fs_fail(); return;
}
if( fat_set_size(&user_dump, 0x1000) != 0) {
sprintf(fat_message, "Failed to resize dump"); fail = 4;
fs_fail(); return;
}
loadRamToRom(0x0, user_dump.start_cluster);
}
void
mainproc(void *arg)
{
int i, j;
int cont_no = 0;
int error, readerror;
u8 bitpattern;
u16 button = 0, oldbutton = 0, newbutton = 0;
u32 stat;
char console_text[50];
osCreateMesgQueue(&pifMesgQueue, pifMesgBuf, NUM_MESSAGE);
osSetEventMesg(OS_EVENT_SI, &pifMesgQueue, dummyMessage);
osContInit(&pifMesgQueue, &bitpattern, &contstat[0]);
for (i = 0; i < MAXCONTROLLERS; i++) {
if ((bitpattern & (1<<i)) &&
((contstat[i].type & CONT_TYPE_MASK) == CONT_TYPE_NORMAL)) {
controller[i] = CONT_VALID;
} else {
controller[i] = CONT_INVALID;
}
}
osCreateMesgQueue(&dmaMessageQ, dmaMessageBuf, 1);
pi_handle = osCartRomInit();
pi_ddrom_handle = osDriveRomInit();
LeoDiskHandle = osLeoDiskInit();
bzero(blockData, 0x1000);
readerror = -1;
init_draw();
setcolor(0,255,0);
draw_puts("If you see this for a long period of time,\nsomething fucked up. Sorry.");
haxAll();
LeoCJCreateLeoManager((OSPri)OS_PRIORITY_LEOMGR-1, (OSPri)OS_PRIORITY_LEOMGR, LeoMessages, NUM_LEO_MESGS);
LeoResetClear();
setbgcolor(15,15,15);
clear_draw();
setcolor(0,255,0);
draw_puts("\f\n 64DD IPL Dumper v0.03 by LuigiBlood\n ----------------------------------------\n");
setcolor(255,255,255);
draw_puts(" PRESS START TO DUMP");
while(1) {
osContStartReadData(&pifMesgQueue);
osRecvMesg(&pifMesgQueue, NULL, OS_MESG_BLOCK);
osContGetReadData(&contdata[0]);
if (contdata[cont_no].errno & CONT_NO_RESPONSE_ERROR) {
button = oldbutton;
} else {
oldbutton = button;
button = contdata[cont_no].button;
}
newbutton = ~oldbutton & button;
if (newbutton & START_BUTTON)
{
//DUMP!!
//64drive, enable write to SDRAM/ROM
srand(osGetCount()); // necessary to generate unique short 8.3 filenames on memory card
ciEnableRomWrites();
draw_puts("\f\n\n\n\n\n Let's dump! Don't turn off the console!\n\n");
osInvalDCache((void *)&blockData, (s32) 0x1000);
dmaIoMesgBuf.hdr.pri = OS_MESG_PRI_NORMAL;
dmaIoMesgBuf.hdr.retQueue = &dmaMessageQ;
dmaIoMesgBuf.dramAddr = &blockData;
dmaIoMesgBuf.devAddr = IPLoffset;
dmaIoMesgBuf.size = 0x1000;
draw_puts("\n - Dumping 64DD IPL...");
for (IPLoffset = 0; IPLoffset < 0x400000; IPLoffset += 0x1000)
{
//read 64DD IPL
osWritebackDCacheAll();
dmaIoMesgBuf.hdr.pri = OS_MESG_PRI_NORMAL;
dmaIoMesgBuf.hdr.retQueue = &dmaMessageQ;
dmaIoMesgBuf.dramAddr = (void *)&blockData;
dmaIoMesgBuf.devAddr = 0xA6000000 + IPLoffset;
dmaIoMesgBuf.size = 0x1000;
osEPiStartDma(pi_ddrom_handle, &dmaIoMesgBuf, OS_READ);
osRecvMesg(&dmaMessageQ, NULL, OS_MESG_BLOCK);
//Write to 64drive
osWritebackDCacheAll();
dmaIoMesgBuf.hdr.pri = OS_MESG_PRI_NORMAL;
dmaIoMesgBuf.hdr.retQueue = &dmaMessageQ;
dmaIoMesgBuf.dramAddr = (void *)&blockData;
dmaIoMesgBuf.devAddr = 0xB0000000 + IPLoffset;
dmaIoMesgBuf.size = 0x1000;
osEPiStartDma(pi_handle, &dmaIoMesgBuf, OS_WRITE);
osRecvMesg(&dmaMessageQ, NULL, OS_MESG_BLOCK);
}
//DONE!! NOW WRITE TO SD
fat_start_ipl();
//Register Dump
draw_puts("\n - Dumping 64DD Registers...");
for (i = 0; i < 0x1000; i += 4)
{
IPLoffset = ReadLeoReg(ASIC_IO_BASE + i);
blockData[i + 0] = (IPLoffset >> 24) & 0xFF;
blockData[i + 1] = (IPLoffset >> 16) & 0xFF;
blockData[i + 2] = (IPLoffset >> 8) & 0xFF;
blockData[i + 3] = (IPLoffset >> 0) & 0xFF;
}
//Write to 64drive
osWritebackDCacheAll();
dmaIoMesgBuf.hdr.pri = OS_MESG_PRI_NORMAL;
dmaIoMesgBuf.hdr.retQueue = &dmaMessageQ;
dmaIoMesgBuf.dramAddr = (void *)&blockData;
dmaIoMesgBuf.devAddr = 0xB0000000;
dmaIoMesgBuf.size = 0x1000;
osEPiStartDma(pi_handle, &dmaIoMesgBuf, OS_WRITE);
osRecvMesg(&dmaMessageQ, NULL, OS_MESG_BLOCK);
//DONE!! NOW WRITE TO SD
fat_start_reg();
//Unlock Commands
draw_puts("\n - Unlocking 64DD H8 Commands...");
WriteLeoReg(ASIC_DATA, 0x4C450000);
WriteLeoReg(ASIC_CMD, 0x00500000);
leoWait_mecha_cmd_done();
WriteLeoReg(ASIC_DATA, 0x4F210000);
WriteLeoReg(ASIC_CMD, 0x00500000);
leoWait_mecha_cmd_done();
draw_puts("\n - Dumping 64DD H8 ROM...");
//Read H8 ROM
for (IPLoffset = 0; IPLoffset < 0x8000; IPLoffset += 0x1000)
{
for (i = 0; i < 0x1000; i++)
{
//Set Address
WriteLeoReg(ASIC_DATA, (IPLoffset + i) << 16);
WriteLeoReg(ASIC_CMD, 0x00300000);
leoWait_mecha_cmd_done();
//Read Byte
WriteLeoReg(ASIC_CMD, 0x00310000);
leoWait_mecha_cmd_done();
blockData[i] = (ReadLeoReg(ASIC_DATA) >> 16) & 0xFF;
}
//Write to 64drive
osWritebackDCacheAll();
dmaIoMesgBuf.hdr.pri = OS_MESG_PRI_NORMAL;
dmaIoMesgBuf.hdr.retQueue = &dmaMessageQ;
dmaIoMesgBuf.dramAddr = (void *)&blockData;
dmaIoMesgBuf.devAddr = 0xB0000000 + IPLoffset;
dmaIoMesgBuf.size = 0x1000;
osEPiStartDma(pi_handle, &dmaIoMesgBuf, OS_WRITE);
osRecvMesg(&dmaMessageQ, NULL, OS_MESG_BLOCK);
}
fat_start_h8();
draw_puts("\n - DONE !!\n");
for(;;);
}
}
}