-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgecko.c
386 lines (314 loc) · 7 KB
/
gecko.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
/*
mini - a Free Software replacement for the Nintendo/BroadOn IOS.
USBGecko support code
Copyright (c) 2008 Nuke - <[email protected]>
Copyright (C) 2008, 2009 Hector Martin "marcan" <[email protected]>
Copyright (C) 2008, 2009 Sven Peter <[email protected]>
Copyright (C) 2009 Andre Heider "dhewg" <[email protected]>
# This code is licensed to you under the terms of the GNU GPL, version 2;
# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*/
#ifdef CAN_HAZ_USBGECKO
#include "types.h"
#include "irq.h"
#include "start.h"
#include "vsprintf.h"
#include "string.h"
#include "utils.h"
#include "hollywood.h"
#include "elf.h"
#include "powerpc.h"
#include "powerpc_elf.h"
#include "gecko.h"
static u8 gecko_found = 0;
static u8 gecko_console_enabled = 0;
static u32 _gecko_command(u32 command)
{
u32 i;
// Memory Card Port B (Channel 1, Device 0, Frequency 3 (32Mhz Clock))
write32(EXI1_CSR, 0xd0);
write32(EXI1_DATA, command);
write32(EXI1_CR, 0x19);
while (read32(EXI1_CR) & 1);
i = read32(EXI1_DATA);
write32(EXI1_CSR, 0);
return i;
}
static u32 _gecko_getid(void)
{
u32 i;
// Memory Card Port B (Channel 1, Device 0, Frequency 3 (32Mhz Clock))
write32(EXI1_CSR, 0xd0);
write32(EXI1_DATA, 0);
write32(EXI1_CR, 0x19);
while (read32(EXI1_CR) & 1);
write32(EXI1_CR, 0x39);
while (read32(EXI1_CR) & 1);
i = read32(EXI1_DATA);
write32(EXI1_CSR, 0);
return i;
}
#ifndef NDEBUG
static u32 _gecko_sendbyte(u8 sendbyte)
{
u32 i = 0;
i = _gecko_command(0xB0000000 | (sendbyte<<20));
if (i&0x04000000)
return 1; // Return 1 if byte was sent
return 0;
}
#endif
static u32 _gecko_recvbyte(u8 *recvbyte)
{
u32 i = 0;
*recvbyte = 0;
i = _gecko_command(0xA0000000);
if (i&0x08000000) {
// Return 1 if byte was received
*recvbyte = (i>>16)&0xff;
return 1;
}
return 0;
}
#if !defined(NDEBUG) && defined(GECKO_SAFE)
static u32 _gecko_checksend(void)
{
u32 i = 0;
i = _gecko_command(0xC0000000);
if (i&0x04000000)
return 1; // Return 1 if safe to send
return 0;
}
#endif
static u32 _gecko_checkrecv(void)
{
u32 i = 0;
i = _gecko_command(0xD0000000);
if (i&0x04000000)
return 1; // Return 1 if safe to recv
return 0;
}
static int gecko_isalive(void)
{
u32 i;
i = _gecko_getid();
if (i != 0x00000000)
return 0;
i = _gecko_command(0x90000000);
if ((i & 0xFFFF0000) != 0x04700000)
return 0;
return 1;
}
static void gecko_flush(void)
{
u8 tmp;
while(_gecko_recvbyte(&tmp));
}
#if 0
static int gecko_recvbuffer(void *buffer, u32 size)
{
u32 left = size;
char *ptr = (char*)buffer;
while(left>0) {
if(!_gecko_recvbyte(ptr))
break;
ptr++;
left--;
}
return (size - left);
}
#endif
#if !defined(NDEBUG) && !defined(GECKO_SAFE)
static int gecko_sendbuffer(const void *buffer, u32 size)
{
u32 left = size;
char *ptr = (char*)buffer;
while(left>0) {
if(!_gecko_sendbyte(*ptr))
break;
if(*ptr == '\n') {
#ifdef GECKO_LFCR
_gecko_sendbyte('\r');
#endif
break;
}
ptr++;
left--;
}
return (size - left);
}
#endif
#if 0
static int gecko_recvbuffer_safe(void *buffer, u32 size)
{
u32 left = size;
char *ptr = (char*)buffer;
while(left>0) {
if(_gecko_checkrecv()) {
if(!_gecko_recvbyte(ptr))
break;
ptr++;
left--;
}
}
return (size - left);
}
#endif
#if !defined(NDEBUG) && defined(GECKO_SAFE)
static int gecko_sendbuffer_safe(const void *buffer, u32 size)
{
u32 left = size;
char *ptr = (char*)buffer;
if((read32(HW_EXICTRL) & EXICTRL_ENABLE_EXI) == 0)
return left;
while(left>0) {
if(_gecko_checksend()) {
if(!_gecko_sendbyte(*ptr))
break;
if(*ptr == '\n') {
#ifdef GECKO_LFCR
_gecko_sendbyte('\r');
#endif
break;
}
ptr++;
left--;
}
}
return (size - left);
}
#endif
void gecko_init(void)
{
write32(EXI0_CSR, 0);
write32(EXI1_CSR, 0);
write32(EXI2_CSR, 0);
if (!gecko_isalive())
return;
gecko_found = 1;
gecko_flush();
gecko_console_enabled = 1;
}
u8 gecko_enable_console(const u8 enable)
{
if (enable) {
if (gecko_isalive())
gecko_console_enabled = 1;
} else
gecko_console_enabled = 0;
return gecko_console_enabled;
}
#ifndef NDEBUG
int gecko_printf(const char *fmt, ...)
{
if (!gecko_console_enabled)
return 0;
va_list args;
char buffer[256];
int i;
va_start(args, fmt);
i = vsprintf(buffer, fmt, args);
va_end(args);
#ifdef GECKO_SAFE
return gecko_sendbuffer_safe(buffer, i);
#else
return gecko_sendbuffer(buffer, i);
#endif
}
#endif
// irq context
#define GECKO_STATE_NONE 0
#define GECKO_STATE_RECEIVE_BUFFER_SIZE 1
#define GECKO_STATE_RECEIVE_BUFFER 2
#define GECKO_BUFFER_MAX (20 * 1024 * 1024)
#define GECKO_CMD_BIN_ARM 0x4241524d
#define GECKO_CMD_BIN_PPC 0x42505043
static u32 _gecko_cmd = 0;
static u32 _gecko_cmd_start_time = 0;
static u32 _gecko_state = GECKO_STATE_NONE;
static u32 _gecko_receive_left = 0;
static u32 _gecko_receive_len = 0;
static u8 *_gecko_receive_buffer = NULL;
void gecko_process(void) {
u8 b;
if (!gecko_found)
return;
if (_gecko_cmd_start_time && read32(HW_TIMER) >
(_gecko_cmd_start_time + IRQ_ALARM_MS2REG(5000)))
goto cleanup;
switch (_gecko_state) {
case GECKO_STATE_NONE:
if (!_gecko_checkrecv() || !_gecko_recvbyte(&b))
return;
_gecko_cmd <<= 8;
_gecko_cmd |= b;
switch (_gecko_cmd) {
case GECKO_CMD_BIN_ARM:
_gecko_state = GECKO_STATE_RECEIVE_BUFFER_SIZE;
_gecko_receive_len = 0;
_gecko_receive_left = 4;
_gecko_receive_buffer = (u8 *) 0x0; // yarly
_gecko_cmd_start_time = read32(HW_TIMER);
break;
case GECKO_CMD_BIN_PPC:
_gecko_state = GECKO_STATE_RECEIVE_BUFFER_SIZE;
_gecko_receive_len = 0;
_gecko_receive_left = 4;
_gecko_receive_buffer = (u8 *) 0x10100000;
_gecko_cmd_start_time = read32(HW_TIMER);
break;
}
return;
case GECKO_STATE_RECEIVE_BUFFER_SIZE:
if (!_gecko_checkrecv() || !_gecko_recvbyte(&b))
return;
_gecko_receive_len <<= 8;
_gecko_receive_len |= b;
_gecko_receive_left--;
if (!_gecko_receive_left) {
if (_gecko_receive_len > GECKO_BUFFER_MAX)
goto cleanup;
_gecko_state = GECKO_STATE_RECEIVE_BUFFER;
_gecko_receive_left = _gecko_receive_len;
// sorry pal, that memory is mine now
powerpc_hang();
}
return;
case GECKO_STATE_RECEIVE_BUFFER:
while (_gecko_receive_left) {
if (!_gecko_checkrecv() || !_gecko_recvbyte(_gecko_receive_buffer))
return;
_gecko_receive_buffer++;
_gecko_receive_left--;
}
if (!_gecko_receive_left)
break;
return;
default:
gecko_printf("MINI/GECKO: internal error\n");
return;
}
ioshdr *h;
// done receiving, handle the command
switch (_gecko_cmd) {
case GECKO_CMD_BIN_ARM:
h = (ioshdr *) (u32 *) 0x0;
if (h->hdrsize != sizeof (ioshdr))
goto cleanup;
if (memcmp("\x7F" "ELF\x01\x02\x01",
(void *) (h->hdrsize + h->loadersize), 7))
goto cleanup;
ipc_enqueue_slow(IPC_DEV_SYS, IPC_SYS_JUMP, 1, h->hdrsize);
break;
case GECKO_CMD_BIN_PPC:
ipc_enqueue_slow(IPC_DEV_PPC, IPC_PPC_BOOT, 3,
1, (u32) 0x10100000, _gecko_receive_len);
break;
}
cleanup:
gecko_flush();
_gecko_cmd = 0;
_gecko_cmd_start_time = 0;
_gecko_state = GECKO_STATE_NONE;
}
#endif