-
Notifications
You must be signed in to change notification settings - Fork 7
/
dbgd.c
357 lines (294 loc) · 9.47 KB
/
dbgd.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
#include <hal/xbox.h>
#include <windows.h>
#include <lwip/api.h>
#include <lwip/arch.h>
#include <lwip/debug.h>
#include <lwip/dhcp.h>
#include <lwip/init.h>
#include <lwip/netif.h>
#include <lwip/opt.h>
#include <lwip/sys.h>
#include <lwip/tcpip.h>
#include <netif/etharp.h>
#include <pbkit/pbkit.h>
#include <pktdrv.h>
#include <protobuf-c/protobuf-c.h>
#include <stdlib.h>
#include <xboxkrnl/xboxkrnl.h>
#include <hal/debug.h>
#include "dbg.pb-c.h"
#include "net.h"
#include "dbgd.h"
#define DBGD_PORT 9269
#ifndef HTTPD_DEBUG
#define HTTPD_DEBUG LWIP_DBG_OFF
#endif
static void* get_transfer_buffer(uint32_t size) {
static uint32_t buffer_size = 0;
static void* buffer = NULL;
if (size > buffer_size) {
if (buffer != NULL) {
free(buffer);
}
buffer = malloc(size);
buffer_size = size;
}
return buffer;
}
static int dbgd_sysinfo(Dbg__Request *req, Dbg__Response *res);
static int dbgd_reboot(Dbg__Request *req, Dbg__Response *res);
static int dbgd_malloc(Dbg__Request *req, Dbg__Response *res);
static int dbgd_free(Dbg__Request *req, Dbg__Response *res);
static int dbgd_mem_read(Dbg__Request *req, Dbg__Response *res);
static int dbgd_mem_write(Dbg__Request *req, Dbg__Response *res);
static int dbgd_debug_print(Dbg__Request *req, Dbg__Response *res);
static int dbgd_show_debug_screen(Dbg__Request *req, Dbg__Response *res);
static int dbgd_show_front_screen(Dbg__Request *req, Dbg__Response *res);
static int dbgd_call(Dbg__Request *req, Dbg__Response *res);
typedef int (*dbgd_req_handler)(Dbg__Request *req, Dbg__Response *res);
static dbgd_req_handler handlers[DBG__REQUEST__TYPE__COUNT] = {
&dbgd_sysinfo,
&dbgd_reboot,
&dbgd_malloc,
&dbgd_free,
&dbgd_mem_read,
&dbgd_mem_write,
&dbgd_debug_print,
&dbgd_show_debug_screen,
&dbgd_show_front_screen,
&dbgd_call
};
static void dbgd_serve(struct netconn *conn)
{
Dbg__Request *req;
Dbg__Response res;
err_t err;
size_t res_packed_size;
struct netbuf *inbuf;
uint16_t msglen;
uint16_t port = 0;
uint8_t *buf;
uint8_t *res_packed;
ip_addr_t naddr;
netconn_peer(conn, &naddr, &port);
debugPrint("[%s connected]\n", ip4addr_ntoa(ip_2_ip4(&naddr)));
while (1) {
/* Read the data from the port, blocking if nothing yet there.
We assume the request (the part we care about) is in one netbuf */
err = netconn_recv(conn, &inbuf);
if (err != ERR_OK) {
break;
}
netbuf_data(inbuf, (void**)&buf, &msglen);
req = dbg__request__unpack(NULL, msglen, buf);
dbg__response__init(&res);
if (req == NULL) {
debugPrint("error: request was null!\n");
netbuf_delete(inbuf);
break;
}
if (req->type >= DBG__REQUEST__TYPE__COUNT) {
/* Error! Unsupported request type */
res.type = DBG__RESPONSE__TYPE__ERROR_UNSUPPORTED;
break;
} else {
res.type = handlers[req->type](req, &res);
}
switch (res.type) {
case DBG__RESPONSE__TYPE__OK:
res.msg = "Ok";
break;
case DBG__RESPONSE__TYPE__ERROR_INCOMPLETE_REQUEST:
res.msg = "Incomplete Request";
break;
case DBG__RESPONSE__TYPE__ERROR_UNSUPPORTED:
res.msg = "Unsupported";
break;
default:
break;
}
/* Send packed response */
res_packed_size = dbg__response__get_packed_size(&res);
res_packed = malloc(res_packed_size);
dbg__response__pack(&res, res_packed);
netconn_write(conn, res_packed, res_packed_size, NETCONN_COPY);
free(res_packed);
dbg__request__free_unpacked(req, NULL);
/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer) */
netbuf_delete(inbuf);
}
/* Close the connection */
netconn_close(conn);
debugPrint("[%s disconnected]\n", ip4addr_ntoa(ip_2_ip4(&naddr)));
}
static void dbgd_thread(void *arg)
{
struct netconn *conn, *newconn;
err_t err;
LWIP_UNUSED_ARG(arg);
conn = netconn_new(NETCONN_TCP);
LWIP_ERROR("invalid conn", (conn != NULL), return;);
netconn_bind(conn, NULL, DBGD_PORT);
netconn_listen(conn);
do {
err = netconn_accept(conn, &newconn);
if (err == ERR_OK) {
dbgd_serve(newconn);
netconn_delete(newconn);
}
} while(err == ERR_OK);
LWIP_DEBUGF(HTTPD_DEBUG, ("dbgd_thread: netconn_accept received error %d, shutting down", err));
netconn_close(conn);
netconn_delete(conn);
}
void dbgd_init(void)
{
sys_thread_new("dbgd",
dbgd_thread,
NULL,
DEFAULT_THREAD_STACKSIZE,
DEFAULT_THREAD_PRIO);
}
static int dbgd_sysinfo(Dbg__Request *req, Dbg__Response *res)
{
static Dbg__SysInfo xb_info;
dbg__sys_info__init(&xb_info);
xb_info.tick_count = GetTickCount();
res->info = &xb_info;
return DBG__RESPONSE__TYPE__OK;
}
static int dbgd_reboot(Dbg__Request *req, Dbg__Response *res)
{
XReboot();
asm volatile ("jmp .");
return DBG__RESPONSE__TYPE__OK;
}
static int dbgd_malloc(Dbg__Request *req, Dbg__Response *res)
{
if (!req->has_size)
return DBG__RESPONSE__TYPE__ERROR_INCOMPLETE_REQUEST;
res->address = (uint32_t)malloc(req->size);
res->has_address = 1;
return DBG__RESPONSE__TYPE__OK;
}
static int dbgd_free(Dbg__Request *req, Dbg__Response *res)
{
if (!req->has_address)
return DBG__RESPONSE__TYPE__ERROR_INCOMPLETE_REQUEST;
free((void*)req->address);
return DBG__RESPONSE__TYPE__OK;
}
static int dbgd_mem_read(Dbg__Request *req, Dbg__Response *res)
{
if (!req->has_address || !req->has_size)
return DBG__RESPONSE__TYPE__ERROR_INCOMPLETE_REQUEST;
res->address = req->address;
res->has_address = 1;
res->data.len = req->size;
res->data.data = get_transfer_buffer(res->data.len);
res->has_data = 1;
unsigned int i = 0;
unsigned int s = req->size;
while(s >= 4) {
*(uint32_t*)&res->data.data[i] = *(volatile uint32_t*)(req->address + i);
i += 4;
s -= 4;
}
while(s >= 2) {
*(uint16_t*)&res->data.data[i] = *(volatile uint16_t*)(req->address + i);
i += 2;
s -= 2;
}
while(s >= 1) {
*(uint8_t*)&res->data.data[i] = *(volatile uint8_t*)(req->address + i);
i += 1;
s -= 1;
}
return DBG__RESPONSE__TYPE__OK;
}
static int dbgd_mem_write(Dbg__Request *req, Dbg__Response *res)
{
if (!req->has_address || !req->has_data)
return DBG__RESPONSE__TYPE__ERROR_INCOMPLETE_REQUEST;
unsigned int i = 0;
unsigned int s = req->data.len;
while(s >= 4) {
*(volatile uint32_t*)(req->address + i) = *(uint32_t*)&req->data.data[i];
i += 4;
s -= 4;
}
while(s >= 2) {
*(volatile uint16_t*)(req->address + i) = *(uint16_t*)&req->data.data[i];
i += 2;
s -= 2;
}
while(s >= 1) {
*(volatile uint8_t*)(req->address + i) = *(uint8_t*)&req->data.data[i];
i += 1;
s -= 1;
}
return DBG__RESPONSE__TYPE__OK;
}
static int dbgd_debug_print(Dbg__Request *req, Dbg__Response *res)
{
debugPrint("%s\n", req->msg);
return DBG__RESPONSE__TYPE__OK;
}
static int dbgd_show_debug_screen(Dbg__Request *req, Dbg__Response *res)
{
pb_show_debug_screen();
return DBG__RESPONSE__TYPE__OK;
}
static int dbgd_show_front_screen(Dbg__Request *req, Dbg__Response *res)
{
pb_show_front_screen();
return DBG__RESPONSE__TYPE__OK;
}
static int dbgd_call(Dbg__Request *req, Dbg__Response *res)
{
if (!req->has_address)
return DBG__RESPONSE__TYPE__ERROR_INCOMPLETE_REQUEST;
// These variables will be used as parameters for inline assembly.
// We make them static as the stack pointer will be changed.
// So we want to address them globally.
static uint32_t stack_pointer;
static uint32_t stack_backup;
static uint32_t address;
// Allocate a stack for working and space for supplied data
size_t stack_size = 0x1000;
if (req->has_data) {
stack_size += req->data.len;
}
uint8_t* stack_data = malloc(stack_size);
// Push optional stack contents, starting at top of stack
stack_pointer = (uint32_t)&stack_data[stack_size];
if (req->has_data) {
stack_pointer -= req->data.len;
memcpy((void*)stack_pointer, req->data.data, req->data.len);
}
// Set address to call
address = req->address;
asm("pusha\n"
"mov %%esp, %[stack_backup]\n" // Keep copy of original stack
"mov %[stack_pointer], %%esp\n"
"call *%[address]\n" // Call the function
"mov %[stack_pointer], %%esp\n" // push all register values to stack
"pusha\n"
"mov %[stack_backup], %%esp\n" // Recover original stack
"popa\n"
: // No outputs
: [stack_pointer] "m" (stack_pointer),
[stack_backup] "m" (stack_backup),
[address] "m" (address)
: "memory");
// Get transfer buffer for a set of registers from pusha
res->data.len = 32;
res->data.data = get_transfer_buffer(res->data.len);
res->has_data = 1;
// Copy pusha data into return buffer
stack_pointer -= res->data.len;
memcpy(res->data.data, (void*)stack_pointer, res->data.len);
free(stack_data);
return DBG__RESPONSE__TYPE__OK;
}