-
Notifications
You must be signed in to change notification settings - Fork 8
/
entry.c
221 lines (192 loc) · 5.08 KB
/
entry.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
#include <stdint.h>
#include <stddef.h>
typedef struct {
int (*set_baud)(unsigned baud);
int unknown[3];
uint32_t (*get_timer)(void);
int (*check_timer)(uint32_t start, int delay);
uint32_t (*timer_to_us)(uint32_t timer);
uint32_t (*us_to_timer)(uint32_t delay_us);
uint32_t (*timer_to_ms)(uint32_t timer);
uint32_t (*ms_to_timer)(uint32_t delay_ms);
uint32_t (*usleep)(uint32_t delay_us);
uint32_t (*msleep)(uint32_t delay_ms);
} timer_t;
typedef struct {
int unknown[4];
int (*recv8)(void);
int (*send8)(int val, int /* 1 */);
int (*recv16)(void);
int (*send16)(int val, int /* 1 */);
int (*recv32)(void);
int (*send32)(int val, int /* 1 */);
int (*recv_buf)(void *buf, int size, int with_checksum);
int (*send_buf)(void *buf, int size, int unused /* 0 */);
} usbio_t;
enum {
CMD_LEGACY_WRITE = 0xa1,
CMD_LEGACY_READ = 0xa2,
CMD_READ16 = 0xd0,
CMD_READ32 = 0xd1,
CMD_WRITE16 = 0xd2,
CMD_WRITE16_NO_ECHO = 0xd3,
CMD_WRITE32 = 0xd4,
CMD_JUMP_DA = 0xd5,
CMD_SEND_DA = 0xd7
};
enum {
FLAG_32BIT = 1,
FLAG_LEGACY = 2,
FLAG_NOECHO = 4
};
static void cmd_read(usbio_t *io, int flags) {
uint32_t addr, size, i;
addr = io->recv32(); io->send32(addr, 1);
size = io->recv32(); io->send32(size, 1);
if (!(flags & FLAG_LEGACY)) io->send16(0, 1);
if (flags & FLAG_32BIT)
for (i = 0; i < size; i++)
io->send32(((uint32_t*)addr)[i], 1);
else
for (i = 0; i < size; i++)
io->send16(((uint16_t*)addr)[i], 1);
if (!(flags & FLAG_LEGACY)) io->send16(0, 1);
}
static void cmd_write(usbio_t *io, int flags) {
uint32_t addr, size, i;
addr = io->recv32(); io->send32(addr, 1);
size = io->recv32(); io->send32(size, 1);
if (!(flags & FLAG_LEGACY)) io->send16(1, 1);
if (flags & FLAG_32BIT)
for (i = 0; i < size; i++) {
uint32_t val = io->recv32();
io->send32(val, 1);
((uint32_t*)addr)[i] = val;
}
else
for (i = 0; i < size; i++) {
uint32_t val = io->recv16();
if (!(flags & FLAG_NOECHO))
io->send16(val, 1);
((uint16_t*)addr)[i] = val;
}
if (!(flags & FLAG_LEGACY)) io->send16(1, 1);
}
static void cmd_send_da(usbio_t *io) {
uint32_t addr, size, sig_len, i, chk;
addr = io->recv32(); io->send32(addr, 1);
size = io->recv32(); io->send32(size, 1);
sig_len = io->recv32(); io->send32(sig_len, 1);
io->send16(0, 1);
#if 1
(void)i; (void)chk;
io->recv_buf((void*)addr, size, 1);
#else
io->recv_buf((void*)addr, size, 0);
chk = 0;
for (i = 0; i < (size & -2); i++)
chk ^= *(uint16_t*)(addr + i);
if (size & 1) chk ^= *(uint8_t*)(addr + i);
io->send16(chk, 1);
#endif
io->send16(0, 1);
}
static void cmd_jump_da(usbio_t *io) {
uint32_t addr;
addr = io->recv32(); io->send32(addr, 1);
io->send16(0, 1);
((void(*)(void*))addr)(io);
}
#define MEM4(addr) *(volatile uint32_t*)(addr)
static inline uint32_t comm_check(volatile uint32_t *addr) {
uint32_t a0 = addr[0], a1 = addr[1];
// a0 = timer, a1 = usbio
if ((a0 >> 16) == 0xfff0)
if (a1 - a0 == sizeof(timer_t) && (uint32_t)addr - a1 == sizeof(usbio_t))
return (uint32_t)a1;
return 0;
}
#define METHOD 1
#if METHOD == 1
/* TODO: better to have own USB code, so not depend on the BROM version */
static usbio_t *find_usbio(void) {
volatile uint32_t *addr = (uint32_t*)0xfff00000;
volatile uint32_t *end = addr + 0x10000 / 4;
for (; addr < end; addr++) {
uint32_t found = comm_check(addr);
if (found)
return (usbio_t*)found;
}
return NULL;
}
#elif METHOD == 2
// alternative way, how stable is it?
// arg4 = da_arg, arg5 = arg_size
static usbio_t *usbio_from_arg(uint32_t arg4, uint32_t arg5) {
uint32_t *addr, a0, a1, i;
do {
if (arg4 < 0x8e4) break;
addr = *(uint32_t**)(arg4 + 0x8e0);
if (((uint32_t)addr >> 16) != 0xfff0) break;
a0 = addr[0]; a1 = addr[1];
if ((a0 >> 16) != 0xfff0) break;
if ((a1 >> 16) != 0xfff0) break;
for (i = 0; i < sizeof(timer_t); i += 4)
if ((*(uint32_t*)(a0 + i) >> 16) != 0xfff0) return NULL;
for (i = 0; i < sizeof(usbio_t); i += 4)
if ((*(uint32_t*)(a1 + i) >> 16) != 0xfff0) return NULL;
return (usbio_t*)a1;
} while (0);
return NULL;
}
#endif
uint32_t entry_main(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint32_t arg3,
uint32_t arg4, uint32_t arg5) {
#if METHOD == 1
usbio_t *io = find_usbio();
#elif METHOD == 2
usbio_t *io = usbio_from_arg(arg4, arg5);
#else
usbio_t *io = ((usbio_t**)(arg4 + 0x8e0))[1];
#endif
if (!io) for (;;);
// io->send32(0x12345678, 1);
// io->send32(arg4, 1);
// io->send32(arg5, 1);
// enable flash ROM
// MEM4(0xa0510000) |= 2;
for (;;) {
unsigned cmd;
cmd = io->recv8();
io->send8(cmd, 1);
switch (cmd) {
case CMD_LEGACY_READ:
cmd_read(io, FLAG_LEGACY);
break;
case CMD_READ16:
cmd_read(io, 0);
break;
case CMD_READ32:
cmd_read(io, FLAG_32BIT);
break;
case CMD_LEGACY_WRITE:
cmd_write(io, FLAG_LEGACY);
break;
case CMD_WRITE16:
cmd_write(io, 0);
break;
case CMD_WRITE16_NO_ECHO:
cmd_write(io, FLAG_NOECHO);
break;
case CMD_WRITE32:
cmd_write(io, FLAG_32BIT);
break;
case CMD_JUMP_DA:
cmd_jump_da(io);
break;
case CMD_SEND_DA:
cmd_send_da(io);
break;
}
}
}