Subversion Repositories freemyipod

Rev

Rev 969 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
892 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emCORE.
7
//
8
//    emCORE is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emCORE is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "usbdebug.h"
26
#include "panic.h"
27
#include "usb.h"
28
#include "thread.h"
29
#include "console.h"
30
#include "util.h"
31
#include "contextswitch.h"
32
#include "power.h"
33
#include "mmu.h"
34
#include "shutdown.h"
35
#include "execimage.h"
36
#ifdef HAVE_I2C
37
#include "i2c.h"
38
#endif
39
#ifdef HAVE_BOOTFLASH
40
#include "bootflash.h"
41
#endif
42
#ifdef HAVE_HWKEYAES
43
#include "hwkeyaes.h"
44
#endif
45
#ifdef HAVE_HMACSHA1
46
#include "hmacsha1.h"
47
#endif
48
#ifdef USB_HAVE_TARGET_SPECIFIC_REQUESTS
49
#include "usbtarget.h"
50
#endif
51
#ifdef HAVE_STORAGE
52
#include "storage.h"
53
#include "disk.h"
54
#include "file.h"
55
#include "dir.h"
56
#include "libc/include/errno.h"
57
#endif
58
 
59
 
60
static uint32_t dbgbuf[16] CACHEALIGN_ATTR;
61
 
62
enum dbgstate_t
63
{
64
    DBGSTATE_IDLE = 0,
65
    DBGSTATE_SETUP,
66
    DBGSTATE_WRITE_MEM,
896 theseven 67
    DBGSTATE_WRITE_CONSOLE,
892 theseven 68
    DBGSTATE_ASYNC,
69
    DBGSTATE_RESPOND,
896 theseven 70
    DBGSTATE_READ_CONSOLE,
892 theseven 71
};
72
 
73
static struct scheduler_thread dbgthread_handle IBSS_ATTR;
74
static uint32_t dbgstack[0x200] STACK_ATTR;
75
struct wakeup dbgwakeup IBSS_ATTR;
76
static bool dbgenabled IBSS_ATTR;
77
static bool dbgbusy IBSS_ATTR;
78
static const struct usb_instance* dbgusb IBSS_ATTR;
79
static enum dbgstate_t dbgstate IBSS_ATTR;
80
static void* dbgmemaddr IBSS_ATTR;
81
static uint32_t dbgmemlen IBSS_ATTR;
82
static char dbgconsendbuf[4096];
83
static char dbgconrecvbuf[1024];
84
static int dbgconsendreadidx IBSS_ATTR;
85
static int dbgconsendwriteidx IBSS_ATTR;
86
static int dbgconrecvreadidx IBSS_ATTR;
87
static int dbgconrecvwriteidx IBSS_ATTR;
88
static struct wakeup dbgconsendwakeup IBSS_ATTR;
89
static struct wakeup dbgconrecvwakeup IBSS_ATTR;
90
static bool dbgconsoleattached IBSS_ATTR;
949 theseven 91
static int maxpacket IBSS_ATTR;
92
static struct bulk_state
93
{
94
    void* addr;
95
    int size;
96
} bulk_state[2] IBSS_ATTR;
97
static int bulk_ctrlreq_ep IBSS_ATTR;
892 theseven 98
 
99
static const char dbgconoverflowstr[] = "\n\n[overflowed]\n\n";
100
 
101
extern int _poolstart;   // These aren't ints at all, but gcc complains about void types being
102
extern int _poolend;     // used here, and we only need the address, so just make it happy...
103
 
104
 
105
void reset() __attribute__((noreturn));
106
 
107
void usbdebug_enable(const struct usb_instance* data, int interface, int altsetting)
108
{
109
    dbgusb = data;
110
    dbgstate = DBGSTATE_IDLE;
111
    dbgenabled = true;
112
}
113
 
114
void usbdebug_disable(const struct usb_instance* data, int interface, int altsetting)
115
{
116
    dbgenabled = false;
117
    dbgstate = DBGSTATE_IDLE;
118
}
119
 
950 theseven 120
void usbdebug_bus_reset(const struct usb_instance* data, int configuration, int interface, int highspeed)
949 theseven 121
{
122
    maxpacket = highspeed ? 512 : 64;
123
}
124
 
125
void usbdebug_bulk_enable(const struct usb_instance* data, int interface, int altsetting)
126
{
127
    usbdebug_enable(data, interface, altsetting);
128
    union usb_endpoint_number outep = { .number = USBDEBUG_ENDPOINT_OUT, .direction = USB_ENDPOINT_DIRECTION_OUT };
129
    union usb_endpoint_number inep = { .number = USBDEBUG_ENDPOINT_IN, .direction = USB_ENDPOINT_DIRECTION_IN };
130
    usb_configure_ep(data, outep, USB_ENDPOINT_TYPE_BULK, maxpacket);
131
    usb_configure_ep(data, inep, USB_ENDPOINT_TYPE_BULK, maxpacket);
132
}
133
 
134
void usbdebug_bulk_disable(const struct usb_instance* data, int interface, int altsetting)
135
{
136
    union usb_endpoint_number outep = { .number = USBDEBUG_ENDPOINT_OUT, .direction = USB_ENDPOINT_DIRECTION_OUT };
137
    union usb_endpoint_number inep = { .number = USBDEBUG_ENDPOINT_IN, .direction = USB_ENDPOINT_DIRECTION_IN };
138
    usb_unconfigure_ep(data, outep);
139
    usb_unconfigure_ep(data, inep);
140
    usbdebug_disable(data, interface, altsetting);
141
}
142
 
143
void usbdebug_bulk_xfer_complete(const struct usb_instance* data, int interface, int endpoint, int bytesleft)
144
{
145
    struct bulk_state* state = &bulk_state[endpoint];
146
    if (!bytesleft && state->size)
147
    {
148
        int size;
149
        if (endpoint)
150
        {
151
            union usb_endpoint_number ep = { .number = USBDEBUG_ENDPOINT_IN, .direction = USB_ENDPOINT_DIRECTION_IN };
152
            size = MIN(state->size, maxpacket * usb_get_max_transfer_size(data, ep));
950 theseven 153
            usb_start_tx(data, ep, state->addr, size);
949 theseven 154
        }
155
        else
156
        {
157
            union usb_endpoint_number ep = { .number = USBDEBUG_ENDPOINT_OUT, .direction = USB_ENDPOINT_DIRECTION_OUT };
158
            size = MIN(state->size, maxpacket * usb_get_max_transfer_size(data, ep));
950 theseven 159
            usb_start_rx(data, ep, state->addr, size);
949 theseven 160
        }
161
        state->addr += size;
162
        state->size -= size;
163
    }
164
}
165
 
166
bool usbdebug_bulk_handle_data(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft)
167
{
168
    uint32_t* buf = (uint32_t*)data->buffer->raw;
169
    union usb_endpoint_number ep0out = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_OUT };
170
    union usb_endpoint_number ep0in = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_IN };
171
    usb_ep0_start_rx(data, false, 0, NULL);
172
    switch (buf[0])
173
    {
174
    case 1:  // START MEMORY TRANSFER
964 theseven 175
        bulk_state[bulk_ctrlreq_ep].addr = (void*)buf[1];
176
        bulk_state[bulk_ctrlreq_ep].size = buf[2];
177
        usb_set_stall(data, ep0out, true);
178
        usb_ep0_start_tx(data, NULL, 0, NULL);
179
        usbdebug_bulk_xfer_complete(data, 0, bulk_ctrlreq_ep, 0);  // Convenient way to start a transfer.
180
        break;
949 theseven 181
    default:
182
        usb_set_stall(data, ep0out, true);
183
        usb_set_stall(data, ep0in, true);
184
        break;
185
    }
186
    return true;
187
}
188
 
189
int usbdebug_bulk_ctrl_request(const struct usb_instance* data, int interface, int endpoint, union usb_ep0_buffer* request, const void** response)
190
{
191
    int size = -1;
192
    union usb_endpoint_number ep0out = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_OUT };
193
    switch (request->setup.bmRequestType.type)
194
    {
195
    case USB_SETUP_BMREQUESTTYPE_TYPE_VENDOR:
196
        switch (request->setup.bRequest.raw)
197
        {
198
        case 0x00:
199
            switch (data->buffer->setup.bmRequestType.direction)
200
            {
201
            case USB_SETUP_BMREQUESTTYPE_DIRECTION_OUT:
202
                bulk_ctrlreq_ep = endpoint;
203
                usb_ep0_start_rx(data, true, 64, usbdebug_bulk_handle_data);
204
                return -3;
950 theseven 205
            default: break;
949 theseven 206
            }
207
            break;
208
        default: break;
209
        }
210
        break;
211
        default: break;
212
    }
213
    return size;
214
}
215
 
944 theseven 216
bool usbdebug_handle_data(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft)
892 theseven 217
{
944 theseven 218
    union usb_endpoint_number ep0in = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_IN };
219
    union usb_endpoint_number ep0out = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_OUT };
892 theseven 220
    uint32_t* buf = (uint32_t*)data->buffer->raw;
944 theseven 221
    usb_ep0_start_rx(dbgusb, false, 0, NULL);
892 theseven 222
    switch (dbgstate)
223
    {
224
    case DBGSTATE_SETUP:
225
        switch (buf[0])
226
        {
227
        case 1:  // GET INFO
228
            dbgbuf[0] = 1;
229
            switch (buf[1])
230
            {
231
            case 0:  // GET VERSION INFO
232
                dbgbuf[1] = VERSION_SVN_INT;
233
                dbgbuf[2] = VERSION_MAJOR | (VERSION_MINOR << 8) | (VERSION_PATCH << 16) | (2 << 24);
234
                dbgbuf[3] = PLATFORM_ID;
235
                break;
236
            case 1:  // GET USER MEMORY INFO
237
                dbgbuf[1] = (uint32_t)&_poolstart;
238
                dbgbuf[2] = (uint32_t)&_poolend;
239
                break;
240
            default:
241
                dbgbuf[0] = 2;
242
            }
243
            break;
244
        case 4:  // READ MEMORY
245
            dbgbuf[0] = 1;
246
            dbgmemaddr = (void*)buf[1];
247
            dbgmemlen = buf[2];
248
            break;
249
        case 5:  // WRITE MEMORY
250
        {
251
            dbgmemaddr = (void*)buf[1];
252
            dbgmemlen = buf[2];
253
            int len = MIN(48, dbgmemlen);
254
            dbgmemlen -= len;
255
            memcpy(dbgmemaddr, &buf[4], len);
256
            if (dbgmemlen)
257
            {
258
                dbgmemaddr += len;
259
                dbgstate = DBGSTATE_WRITE_MEM;
944 theseven 260
                usb_ep0_start_rx(dbgusb, true, MIN(64, dbgmemlen), usbdebug_handle_data);
892 theseven 261
                return true;
262
            }
263
            dbgbuf[0] = 1;
264
            break;
265
        }
266
        case 10:  // READ CONSOLE
896 theseven 267
            dbgmemaddr = (void*)buf[1];
268
            dbgstate = DBGSTATE_READ_CONSOLE;
944 theseven 269
            usb_set_stall(dbgusb, ep0out, true);
270
            usb_ep0_start_tx(dbgusb, NULL, 0, NULL);
896 theseven 271
            return true;
892 theseven 272
            break;
273
        case 11:  // WRITE CONSOLE
896 theseven 274
        {
275
            int total = 0;
276
            int bytes = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
892 theseven 277
            if (bytes < 0) bytes += sizeof(dbgconrecvbuf);
278
            if (bytes)
279
            {
280
                if (bytes > buf[1]) bytes = buf[1];
896 theseven 281
                total = bytes;
282
                if (bytes > 48) bytes = 48;
892 theseven 283
                int writebytes = bytes;
284
                char* readptr = (char*)&buf[4];
285
                if (dbgconrecvwriteidx + bytes >= sizeof(dbgconrecvbuf))
286
                {
287
                    writebytes = sizeof(dbgconrecvbuf) - dbgconrecvwriteidx;
288
                    memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
289
                    dbgconrecvwriteidx = 0;
290
                    readptr = &readptr[writebytes];
291
                    writebytes = bytes - writebytes;
292
                }
293
                if (writebytes) memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
294
                dbgconrecvwriteidx += writebytes;
295
                wakeup_signal(&dbgconrecvwakeup);
296
            }
297
            dbgbuf[0] = 1;
896 theseven 298
            dbgbuf[1] = total;
892 theseven 299
            dbgbuf[2] = sizeof(dbgconrecvbuf);
896 theseven 300
            if (total > 48)
301
            {
302
                dbgmemlen = total - 48;
303
                dbgstate = DBGSTATE_WRITE_CONSOLE;
944 theseven 304
                usb_ep0_start_rx(dbgusb, true, MIN(64, dbgmemlen), usbdebug_handle_data);
896 theseven 305
                return true;
306
            }
892 theseven 307
            dbgbuf[3] = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
308
            break;
896 theseven 309
        }
892 theseven 310
        case 15:  // GET PROCESS INFO
311
            dbgbuf[0] = 1;
312
            dbgbuf[1] = SCHEDULER_THREAD_INFO_VERSION;
313
            dbgbuf[2] = (uint32_t)head_thread;
314
            break;
315
        case 16:  // FREEZE SCHEDULER
316
            dbgbuf[1] = scheduler_freeze(buf[1]);
317
            scheduler_switch(NULL, NULL);
318
            dbgbuf[0] = 1;
319
            break;
320
        case 17:  // SUSPEND THREAD
321
            if (buf[1])
322
            {
323
                if (thread_suspend((struct scheduler_thread*)(buf[2])) == ALREADY_SUSPENDED)
324
                    dbgbuf[1] = 1;
325
                else dbgbuf[1] = 0;
326
            }
327
            else
328
            {
329
                if (thread_resume((struct scheduler_thread*)(buf[2])) == ALREADY_RESUMED)
330
                    dbgbuf[1] = 0;
331
                else dbgbuf[1] = 1;
332
            }
333
            dbgbuf[0] = 1;
334
            break;
335
        case 18:  // KILL THREAD
336
            thread_terminate((struct scheduler_thread*)(buf[1]));
337
            dbgbuf[0] = 1;
338
            break;
339
        case 19:  // CREATE THREAD
340
            dbgbuf[0] = 1;
341
            dbgbuf[1] = (uint32_t)thread_create(NULL, (const char*)(buf[1]), (const void*)(buf[2]),
342
                                                (char*)(buf[3]), buf[4], (enum thread_type)buf[5],
343
                                                buf[6], buf[7], (void*)buf[8], (void*)buf[9],
344
                                                (void*)buf[10], (void*)buf[11]);
345
            break;
346
        case 20:  // FLUSH CACHE
347
            clean_dcache();
348
            invalidate_icache();
349
            buf[0] = 1;
350
            break;
351
        case 2:  // RESET
352
            if (!buf[1]) reset();
353
        default:
354
            if (!dbgbusy)
355
            {
356
                memcpy(dbgbuf, buf, 64);
357
                dbgstate = DBGSTATE_ASYNC;
358
                dbgbusy = 1;
359
                wakeup_signal(&dbgwakeup);
360
                return true;
361
            }
362
            buf[0] = 3;
363
            break;
364
        }
365
        break;
366
    case DBGSTATE_WRITE_MEM:
367
    {
896 theseven 368
        int len = MIN(64 - bytesleft, dbgmemlen);
892 theseven 369
        dbgmemlen -= len;
370
        memcpy(dbgmemaddr, buf, len);
896 theseven 371
        if (dbgmemlen && !bytesleft)
892 theseven 372
        {
373
            dbgmemaddr += len;
944 theseven 374
            usb_ep0_start_rx(dbgusb, true, MIN(64, dbgmemlen), usbdebug_handle_data);
892 theseven 375
            return true;
376
        }
377
        dbgbuf[0] = 1;
378
        break;
379
    }
896 theseven 380
    case DBGSTATE_WRITE_CONSOLE:
381
    {
382
        int bytes = MIN(64 - bytesleft, dbgmemlen);
383
        dbgmemlen -= bytes;
384
        if (bytes)
385
        {
386
            int writebytes = bytes;
387
            char* readptr = (char*)buf;
388
            if (dbgconrecvwriteidx + bytes >= sizeof(dbgconrecvbuf))
389
            {
390
                writebytes = sizeof(dbgconrecvbuf) - dbgconrecvwriteidx;
391
                memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
392
                dbgconrecvwriteidx = 0;
393
                readptr = &readptr[writebytes];
394
                writebytes = bytes - writebytes;
395
            }
396
            if (writebytes) memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
397
            dbgconrecvwriteidx += writebytes;
398
            wakeup_signal(&dbgconrecvwakeup);
399
            if (dbgmemlen && !bytesleft)
400
            {
944 theseven 401
                usb_ep0_start_rx(dbgusb, true, MIN(64, dbgmemlen), usbdebug_handle_data);
896 theseven 402
                return true;
403
            }
404
        }
405
        dbgbuf[3] = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
406
        if (dbgbuf[3] < 0) dbgbuf[3] += sizeof(dbgconrecvbuf);
407
    }
892 theseven 408
    default: break;
409
    }
410
    dbgstate = DBGSTATE_RESPOND;
944 theseven 411
    usb_set_stall(dbgusb, ep0out, true);
412
    usb_ep0_start_tx(dbgusb, NULL, 0, NULL);
892 theseven 413
    return true;
414
}
415
 
944 theseven 416
bool read_console_callback(const struct usb_instance* data, union usb_endpoint_number epnum, int bytesleft)
896 theseven 417
{
944 theseven 418
    if (bytesleft || !dbgmemaddr)
419
    {
420
        usb_ep0_start_rx(dbgusb, true, 0, usb_ep0_ack_callback);
421
        if (!dbgmemaddr) usb_ep0_start_tx(dbgusb, NULL, 0, usb_ep0_short_tx_callback);
422
        dbgusb->state->ep0_tx_ptr = NULL;
423
        return false;
424
    }
896 theseven 425
    dbgconsoleattached = true;
426
    int bytes = MIN(64, dbgmemlen);
427
    if (bytes)
428
    {
429
        int readbytes = bytes;
430
        char* outptr = (char*)dbgbuf;
431
        if (dbgconsendreadidx + bytes >= sizeof(dbgconsendbuf))
432
        {
433
            readbytes = sizeof(dbgconsendbuf) - dbgconsendreadidx;
434
            memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
435
            dbgconsendreadidx = 0;
436
            outptr = &outptr[readbytes];
437
            readbytes = bytes - readbytes;
438
        }
439
        if (readbytes) memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
440
        dbgconsendreadidx += readbytes;
441
        wakeup_signal(&dbgconsendwakeup);
442
        dbgmemlen -= bytes;
443
    }
444
    bytes = MIN(64, (int)dbgmemaddr);
445
    dbgmemaddr -= bytes;
969 theseven 446
    usb_ep0_start_tx(dbgusb, dbgbuf, bytes,
447
                     bytes < 64 ? usb_ep0_short_tx_callback : read_console_callback);
896 theseven 448
    return true;
449
}
450
 
892 theseven 451
int usbdebug_handle_setup(const struct usb_instance* data, int interface, union usb_ep0_buffer* request, const void** response)
452
{
453
    int size = -1;
944 theseven 454
    union usb_endpoint_number ep0out = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_OUT };
892 theseven 455
    switch (request->setup.bmRequestType.type)
456
    {
457
    case USB_SETUP_BMREQUESTTYPE_TYPE_VENDOR:
458
        switch (request->setup.bRequest.raw)
459
        {
460
        case 0x00:
461
            switch (data->buffer->setup.bmRequestType.direction)
462
            {
463
            case USB_SETUP_BMREQUESTTYPE_DIRECTION_OUT:
464
                dbgstate = DBGSTATE_SETUP;
465
                dbgmemlen = 0;
944 theseven 466
                usb_ep0_start_rx(dbgusb, true, 64, usbdebug_handle_data);
892 theseven 467
                return -3;
468
            case USB_SETUP_BMREQUESTTYPE_DIRECTION_IN:
469
                switch (dbgstate)
470
                {
471
                case DBGSTATE_RESPOND:
472
                {
944 theseven 473
                    dbgmemlen = MIN(dbgmemlen, data->buffer->setup.wLength - 16);
892 theseven 474
                    int len = MIN(48, dbgmemlen);
475
                    if (len) memcpy(&dbgbuf[4], dbgmemaddr, len);
476
                    dbgmemlen -= len;
477
                    if (dbgmemlen)
478
                    {
944 theseven 479
                        usb_ep0_start_rx(dbgusb, false, 0, NULL);
480
                        dbgusb->state->ep0_tx_ptr = dbgmemaddr + 48;
481
                        dbgusb->state->ep0_tx_len = dbgmemlen;
482
                        usb_ep0_start_tx(dbgusb, dbgbuf, len + 16,
483
                                         len < 48 ? usb_ep0_short_tx_callback : usb_ep0_tx_callback);
892 theseven 484
                        return -3;
485
                    }
486
                    dbgstate = DBGSTATE_IDLE;
487
                    *response = dbgbuf;
488
                    return len + 16;
489
                }
896 theseven 490
                case DBGSTATE_READ_CONSOLE:
491
                {
944 theseven 492
                    dbgmemaddr = (void*)MIN((int)dbgmemaddr, data->buffer->setup.wLength - 16);
896 theseven 493
                    dbgconsoleattached = true;
494
                    dbgmemlen = dbgconsendwriteidx - dbgconsendreadidx;
495
                    if (dbgmemlen < 0) dbgmemlen += sizeof(dbgconsendbuf);
496
                    int used = dbgmemlen;
497
                    if (dbgmemlen > (int)dbgmemaddr) dbgmemlen = (int)dbgmemaddr;
498
                    int bytes = MIN(48, dbgmemlen);
499
                    dbgbuf[0] = 1;
500
                    dbgbuf[1] = dbgmemlen;
501
                    dbgbuf[2] = sizeof(dbgconsendbuf);
502
                    dbgbuf[3] = used - dbgmemlen;
503
                    if (bytes)
504
                    {
505
                        int readbytes = bytes;
506
                        char* outptr = (char*)&dbgbuf[4];
507
                        if (dbgconsendreadidx + bytes >= sizeof(dbgconsendbuf))
508
                        {
509
                            readbytes = sizeof(dbgconsendbuf) - dbgconsendreadidx;
510
                            memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
511
                            dbgconsendreadidx = 0;
512
                            outptr = &outptr[readbytes];
513
                            readbytes = bytes - readbytes;
514
                        }
515
                        if (readbytes) memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
516
                        dbgconsendreadidx += readbytes;
517
                        wakeup_signal(&dbgconsendwakeup);
518
                    }
519
                    dbgmemlen -= bytes;
520
                    bytes = MIN(48, (int)dbgmemaddr);
521
                    dbgmemaddr -= bytes;
522
                    if (dbgmemaddr)
523
                    {
944 theseven 524
                        dbgusb->state->ep0_tx_ptr = (void*)true;
525
                        usb_ep0_start_rx(dbgusb, false, 0, NULL);
526
                        usb_ep0_start_tx(dbgusb, dbgbuf, bytes + 16,
527
                                         bytes < 48 ? usb_ep0_short_tx_callback : read_console_callback);
896 theseven 528
                        return -3;
529
                    }
530
                    dbgstate = DBGSTATE_IDLE;
531
                    *response = dbgbuf;
532
                    return bytes + 16;
533
                }
892 theseven 534
                default: return -2;
535
                }
536
                break;
537
            }
538
            break;
539
        default: break;
540
        }
541
        break;
542
        default: break;
543
    }
544
    return size;
545
}
546
 
547
void dbgthread(void* arg0, void* arg1, void* arg2, void* arg3)
548
{
549
    struct scheduler_thread* t;
550
    while (1)
551
    {
552
        wakeup_wait(&dbgwakeup, TIMEOUT_BLOCK);
553
        for (t = head_thread; t; t = t->thread_next)
554
            if (t->state == THREAD_DEFUNCT)
555
            {
556
                if (t->block_type == THREAD_DEFUNCT_STKOV)
557
                {
558
                    if (t->name) cprintf(1, "\n*PANIC*\nStack overflow! (%s)\n", t->name);
559
                    else cprintf(1, "\n*PANIC*\nStack overflow! (%08X)\n", t);
560
                }
561
                t->state = THREAD_DEFUNCT_ACK;
562
            }
563
        uint32_t mode = enter_critical_section();
564
        uint32_t buf[16];
565
        if (dbgstate == DBGSTATE_ASYNC)
566
        {
567
            memcpy(buf, dbgbuf, 64);
568
            leave_critical_section(mode);
569
            void* addr = &buf[4];
570
            int len = 0;
571
            switch (buf[0])
572
            {
573
                case 2:  // RESET
574
                    shutdown(false);
575
                    reset();
576
                case 3:  // POWER OFF
577
                    if (buf[1]) shutdown(true);
578
                    power_off();
579
                    buf[0] = 1;
580
                    break;
581
#ifdef HAVE_I2C
582
                case 8:  // READ I2C
583
                    len = buf[1] >> 24;
584
                    i2c_recv(buf[1] & 0xff, (buf[1] >> 8) & 0xff, (buf[1] >> 16) & 0xff,
585
                             (uint8_t*)&buf[4], len);
586
                    buf[0] = 1;
587
                    break;
588
                case 9:  // WRITE I2C
589
                    i2c_send(buf[1] & 0xff, (buf[1] >> 8) & 0xff, (buf[1] >> 16) & 0xff,
590
                             (uint8_t*)&buf[4], buf[1] >> 24);
591
                    buf[0] = 1;
592
                    break;
593
#endif
594
                case 12:  // CWRITE
595
                    cwrite(buf[1], (const char*)&buf[4], buf[2]);
596
                    buf[0] = 1;
597
                    break;
598
                case 13:  // CREAD
599
                    buf[0] = 1;
600
                    buf[1] = cread(buf[1], (char*)&buf[4], buf[2], 0);
601
                    break;
602
                case 14:  // CFLUSH
603
                    cflush(buf[1]);
604
                    buf[0] = 1;
605
                    break;
606
                case 21:  // EXECIMAGE
607
                {
608
                    int argc = buf[2] >> 24;
609
                    if (!buf[3])
610
                    {
611
                        buf[3] = (uint32_t)&buf[4];
612
                        int i;
613
                        for (i = 0; i < argc; i++) buf[i + 4] += buf[3];
614
                    }
615
                    buf[0] = 1;
616
                    buf[1] = (uint32_t)execimage((void*)buf[1], buf[2] & 0x10000, argc, (const char* const*)buf[3]);
617
                    break;
618
                }
619
#ifdef HAVE_BOOTFLASH
620
                case 22:  // READ BOOT FLASH
621
                    bootflash_readraw((void*)buf[1], buf[2], buf[3]);
622
                    buf[0] = 1;
623
                    break;
624
                case 23:  // WRITE BOOT FLASH
625
                    bootflash_writeraw((void*)buf[1], buf[2], buf[3]);
626
                    buf[0] = 1;
627
                    break;
628
#endif
629
                case 24:  // EXECFIRMWARE
630
                    shutdown(false);
631
                    execfirmware((void*)buf[1], (void*)buf[2], (size_t)buf[3]);
632
                    buf[0] = 1;
633
                    break;
634
#ifdef HAVE_HWKEYAES
635
                case 25:  // HWKEYAES
636
                    hwkeyaes((enum hwkeyaes_direction)((uint8_t*)buf)[4], ((uint16_t*)buf)[3], (void*)buf[2], buf[3]);
637
                    buf[0] = 1;
638
                    break;
639
#endif
640
#ifdef HAVE_HMACSHA1
641
                case 26:  // HMACSHA1
642
                    hmacsha1((void*)buf[1], buf[2], (void*)buf[3]);
643
                    buf[0] = 1;
644
                    break;
645
#endif
646
#ifdef HAVE_STORAGE
647
                case 27:  // STORAGE_GET_INFO
648
                    buf[0] = 1;
649
                    storage_get_info(buf[1], (struct storage_info*)&buf[4]);
650
                    buf[1] = 1;
933 theseven 651
                    len = (sizeof(struct storage_info) + 3) / 4 * 4;
892 theseven 652
                    break;
653
                case 28:  // STORAGE_READ_SECTORS_MD
654
                    buf[0] = 1;
655
                    buf[1] = (uint32_t)storage_read_sectors_md(buf[1], buf[2] | (((uint64_t)(buf[3]) << 32)),
656
                                                               buf[4], (void*)(buf[5]));
657
                    break;
658
                case 29:  // STORAGE_WRITE_SECTORS_MD
659
                    buf[0] = 1;
660
                    buf[1] = (uint32_t)storage_write_sectors_md(buf[1], buf[2] | (((uint64_t)(buf[3]) << 32)),
661
                                                                buf[4], (void*)(buf[5]));
662
                    break;
663
                case 30:  // FILE_OPEN
664
                {
665
                    buf[0] = 1;
666
                    if (!buf[3]) buf[3] = (uint32_t)&buf[4];
667
                    int fd = file_open((char*)buf[3], (int)buf[1]);
668
                    if (fd > 0) reown_file(fd, KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
669
                    buf[1] = (uint32_t)fd;
670
                    break;
671
                }
672
                case 31:  // FILESIZE
673
                    buf[0] = 1;
674
                    buf[1] = (uint32_t)filesize((int)buf[1]);
675
                    break;
676
                case 32:  // READ
677
                    buf[0] = 1;
678
                    buf[1] = (uint32_t)read((int)buf[1], (void*)buf[2], (size_t)buf[3]);
679
                    break;
680
                case 33:  // WRITE
681
                    buf[0] = 1;
682
                    buf[1] = (uint32_t)write((int)buf[1], (void*)buf[2], (size_t)buf[3]);
683
                    break;
684
                case 34:  // LSEEK
685
                    buf[0] = 1;
686
                    buf[1] = (uint32_t)lseek((int)buf[1], (off_t)buf[2], (int)buf[3]);
687
                    break;
688
                case 35:  // FTRUNCATE
689
                    buf[0] = 1;
690
                    buf[1] = (uint32_t)ftruncate((int)buf[1], (off_t)buf[2]);
691
                    break;
692
                case 36:  // FSYNC
693
                    buf[0] = 1;
694
                    buf[1] = (uint32_t)fsync((int)buf[1]);
695
                    break;
696
                case 37:  // CLOSE
697
                    buf[0] = 1;
698
                    buf[1] = (uint32_t)close((int)buf[1]);
699
                    break;
700
                case 38:  // CLOSE_MONITOR_FILES
701
                    buf[0] = 1;
702
                    buf[1] = (uint32_t)close_all_of_process(KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
703
                    break;
704
                case 39:  // RELEASE_FILES
705
                    buf[0] = 1;
706
                    buf[1] = (uint32_t)release_files((int)buf[1]);
707
                    break;
708
                case 40:  // REMOVE
709
                    buf[0] = 1;
710
                    if (!buf[3]) buf[3] = (uint32_t)&buf[4];
711
                    buf[1] = (uint32_t)remove((char*)buf[3]);
712
                    break;
713
                case 41:  // RENAME
714
                    buf[0] = 1;
715
                    buf[1] = (uint32_t)rename((char*)buf[2], (char*)buf[3]);
716
                    break;
717
                case 42:  // OPENDIR
718
                {
719
                    buf[0] = 1;
720
                    if (!buf[3]) buf[3] = (uint32_t)&buf[4];
721
                    DIR* dir = opendir((char*)buf[3]);
722
                    if (dir > 0) reown_dir(dir, KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
723
                    buf[1] = (uint32_t)dir;
724
                    break;
725
                }
726
                case 43:  // READDIR
727
                    buf[0] = 1;
728
                    buf[3] = (uint32_t)readdir((DIR*)buf[1]);
729
                    buf[1] = 1;
730
                    buf[2] = MAX_PATH;
731
                    break;
732
                case 44:  // CLOSEDIR
733
                    buf[0] = 1;
734
                    buf[1] = (uint32_t)closedir((DIR*)buf[1]);
735
                    break;
736
                case 45:  // CLOSE_MONITOR_DIRS
737
                    buf[0] = 1;
738
                    buf[1] = (uint32_t)closedir_all_of_process(KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
739
                    break;
740
                case 46:  // RELEASE_DIRS
741
                    buf[0] = 1;
742
                    buf[1] = (uint32_t)release_dirs((int)buf[1]);
743
                    break;
744
                case 47:  // MKDIR
745
                    buf[0] = 1;
746
                    if (!buf[3]) buf[3] = (uint32_t)&buf[4];
747
                    buf[1] = (uint32_t)mkdir((char*)buf[3]);
748
                    break;
749
                case 48:  // RMDIR
750
                    buf[0] = 1;
751
                    if (!buf[3]) buf[3] = (uint32_t)&buf[4];
752
                    buf[1] = (uint32_t)rmdir((char*)buf[3]);
753
                    break;
754
                case 49:  // ERRNO
755
                    buf[0] = 1;
756
                    buf[1] = (uint32_t)errno;
757
                    break;
758
#ifdef HAVE_HOTSWAP
759
                case 50:  // DISK_MOUNT
760
                    buf[0] = 1;
761
                    buf[1] = (uint32_t)disk_mount((int)buf[1]);
762
                    break;
763
                case 51:  // DISK_UNMOUNT
764
                    buf[0] = 1;
765
                    buf[1] = (uint32_t)disk_unmount((int)buf[1]);
766
                    break;
767
#endif
768
                case 58:  // FAT_ENABLE_FLUSHING
769
                    buf[0] = 1;
770
                    fat_enable_flushing((bool)buf[1]);
771
                    break;
772
                case 59:  // FAT_SIZE
773
                    buf[0] = 1;
774
                    fat_size_mv(buf[1], &buf[1], &buf[2]);
775
                    break;
776
#endif
777
                case 52:  // MALLOC
778
                    buf[0] = 1;
779
                    buf[1] = (uint32_t)malloc((size_t)buf[1]);
780
                    if (buf[1]) reownalloc(buf[1], KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
781
                    break;
782
                case 53:  // MEMALIGN
783
                    buf[0] = 1;
784
                    buf[1] = (uint32_t)memalign((size_t)buf[1], (size_t)buf[2]);
785
                    if (buf[1]) reownalloc(buf[1], KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
786
                    break;
787
                case 54:  // REALLOC
788
                    buf[0] = 1;
789
                    buf[1] = (uint32_t)realloc((void*)buf[1], (size_t)buf[2]);
790
                    break;
791
                case 55:  // REOWNALLOC
792
                    buf[0] = 1;
793
                    reownalloc((void*)buf[1], (void*)buf[2]);
794
                    break;
795
                case 56:  // FREE
796
                    buf[0] = 1;
797
                    free((void*)buf[1]);
798
                    break;
799
                case 57:  // FREE MONITOR ALLOCATIONS
800
                    buf[0] = 1;
801
                    buf[1] = (uint32_t)free_all_of_thread(KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
802
                    break;
803
#ifdef HAVE_RTC
804
                case 60:  // RTC READ
805
                    buf[0] = 1;
806
                    rtc_read_datetime((struct rtc_datetime*)&buf[1]);
807
                    break;
808
                case 61:  // RTC WRITE
809
                    buf[0] = 1;
810
                    rtc_write_datetime((const struct rtc_datetime*)&buf[1]);
811
                    break;
812
#endif
931 theseven 813
                default:
892 theseven 814
#ifdef USB_HAVE_TARGET_SPECIFIC_REQUESTS
931 theseven 815
                    if (buf[0] >= 0xffff0000)
816
                        len = usb_target_handle_request(buf, sizeof(buf), &addr);
932 theseven 817
                    else buf[0] = 2;
818
#else
819
                    buf[0] = 2;
892 theseven 820
#endif
821
                    break;
822
            }
823
            mode = enter_critical_section();
824
            if (dbgstate == DBGSTATE_ASYNC)
825
            {
944 theseven 826
                usb_ep0_start_tx(dbgusb, NULL, 0, NULL);
892 theseven 827
                dbgstate = DBGSTATE_RESPOND;
828
                dbgmemaddr = addr;
829
                dbgmemlen = len;
933 theseven 830
                memcpy(dbgbuf, buf, 16);
892 theseven 831
            }
832
        }
833
        dbgbusy = false;
834
        leave_critical_section(mode);
835
    }
836
}
837
 
838
void usbdebug_init(void)
839
{
840
    wakeup_init(&dbgwakeup);
841
    dbgconsendreadidx = 0;
842
    dbgconsendwriteidx = 0;
843
    dbgconrecvreadidx = 0;
844
    dbgconrecvwriteidx = 0;
845
    wakeup_init(&dbgconsendwakeup);
846
    wakeup_init(&dbgconrecvwakeup);
847
    dbgenabled = false;
848
    dbgbusy = false;
849
    dbgstate = DBGSTATE_IDLE;
850
    dbgconsoleattached = false;
851
    thread_create(&dbgthread_handle, "monitor worker", dbgthread, dbgstack,
852
                  sizeof(dbgstack), CORE_THREAD, 255, true, NULL, NULL, NULL, NULL);
853
}
854
 
855
int dbgconsole_getfree() ICODE_ATTR;
856
int dbgconsole_getfree()
857
{
858
    int free = dbgconsendreadidx - dbgconsendwriteidx - 1;
859
    if (free < 0) free += sizeof(dbgconsendbuf);
860
    return free;
861
}
862
 
863
int dbgconsole_makespace(int length, bool safe) ICODE_ATTR;
864
int dbgconsole_makespace(int length, bool safe)
865
{
866
    int free = dbgconsole_getfree();
867
    while (!free && dbgconsoleattached && !safe)
868
    {
869
        dbgconsoleattached = false;
870
        wakeup_wait(&dbgconsendwakeup, 2000000);
871
        free = dbgconsole_getfree();
872
    }
873
    if (free) return free > length ? length : free;
874
    if (length > sizeof(dbgconsendbuf) - 17) length = sizeof(dbgconsendbuf) - 17;
875
    uint32_t mode = enter_critical_section();
876
    dbgconsendreadidx += length;
877
    if (dbgconsendreadidx >= sizeof(dbgconsendbuf))
878
        dbgconsendreadidx -= sizeof(dbgconsendbuf);
879
    int offset = 0;
880
    int idx = dbgconsendreadidx;
881
    if (idx + 16 >= sizeof(dbgconsendbuf))
882
    {
883
        offset = sizeof(dbgconsendbuf) - dbgconsendreadidx;
884
        memcpy(&dbgconsendbuf[dbgconsendreadidx], dbgconoverflowstr, offset);
885
        idx = 0;
886
    }
887
    if (offset != 16) memcpy(&dbgconsendbuf[idx], &dbgconoverflowstr[offset], 16 - offset);
888
    leave_critical_section(mode);
889
    return length;
890
}
891
 
892
void dbgconsole_putc_internal(char string, bool safe)
893
{
894
    dbgconsole_makespace(1, safe);
895
    dbgconsendbuf[dbgconsendwriteidx++] = string;
896
    if (dbgconsendwriteidx >= sizeof(dbgconsendbuf))
897
        dbgconsendwriteidx -= sizeof(dbgconsendbuf);
898
}
899
 
900
void dbgconsole_putc(char string)
901
{
902
    dbgconsole_putc_internal(string, false);
903
}
904
 
905
void dbgconsole_sputc(char string)
906
{
907
    dbgconsole_putc_internal(string, true);
908
}
909
 
910
void dbgconsole_write_internal(const char* string, size_t length, bool safe)
911
{
912
    while (length)
913
    {
914
        int space = dbgconsole_makespace(length, safe);
915
        if (dbgconsendwriteidx + space >= sizeof(dbgconsendbuf))
916
        {
917
            int bytes = sizeof(dbgconsendbuf) - dbgconsendwriteidx;
918
            memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, bytes);
919
            dbgconsendwriteidx = 0;
920
            string = &string[bytes];
921
            space -= bytes;
922
            length -= bytes;
923
        }
924
        if (space) memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, space);
925
        dbgconsendwriteidx += space;
926
        string = &string[space];
927
        length -= space;
928
    }
929
}
930
 
931
void dbgconsole_write(const char* string, size_t length)
932
{
933
    dbgconsole_write_internal(string, length, false);
934
}
935
 
936
void dbgconsole_swrite(const char* string, size_t length)
937
{
938
    dbgconsole_write_internal(string, length, true);
939
}
940
 
941
void dbgconsole_puts(const char* string)
942
{
943
    dbgconsole_write(string, strlen(string));
944
}
945
 
946
void dbgconsole_sputs(const char* string)
947
{
948
    dbgconsole_swrite(string, strlen(string));
949
}
950
 
951
int dbgconsole_getavailable() ICODE_ATTR;
952
int dbgconsole_getavailable()
953
{
954
    int available = dbgconrecvwriteidx - dbgconrecvreadidx;
955
    if (available < 0) available += sizeof(dbgconrecvbuf);
956
    return available;
957
}
958
 
959
int dbgconsole_getc(int timeout)
960
{
961
    if (!dbgconsole_getavailable())
962
    {
963
        wakeup_wait(&dbgconrecvwakeup, TIMEOUT_NONE);
964
        if (!dbgconsole_getavailable())
965
        {
966
            wakeup_wait(&dbgconrecvwakeup, timeout);
967
            if (!dbgconsole_getavailable()) return -1;
968
        }
969
    }
970
    int byte = dbgconrecvbuf[dbgconrecvreadidx++];
971
    if (dbgconrecvreadidx >= sizeof(dbgconrecvbuf))
972
        dbgconrecvreadidx -= sizeof(dbgconrecvbuf);
973
    return byte;
974
}
975
 
976
int dbgconsole_read(char* buffer, size_t length, int timeout)
977
{
978
    if (!length) return 0;
979
    int available = dbgconsole_getavailable();
980
    if (!available)
981
    {
982
        wakeup_wait(&dbgconrecvwakeup, TIMEOUT_NONE);
983
        int available = dbgconsole_getavailable();
984
        if (!available)
985
        {
986
            wakeup_wait(&dbgconrecvwakeup, timeout);
987
            int available = dbgconsole_getavailable();
988
            if (!available) return 0;
989
        }
990
    }
991
    if (available > length) available = length;
992
    int left = available;
993
    if (dbgconrecvreadidx + available >= sizeof(dbgconrecvbuf))
994
    {
995
        int bytes = sizeof(dbgconrecvbuf) - dbgconrecvreadidx;
996
        memcpy(buffer, &dbgconrecvbuf[dbgconrecvreadidx], bytes);
997
        dbgconrecvreadidx = 0;
998
        buffer = &buffer[bytes];
999
        left -= bytes;
1000
    }
1001
    if (left) memcpy(buffer, &dbgconrecvbuf[dbgconrecvreadidx], left);
1002
    dbgconrecvreadidx += left;
1003
    return available;
1004
}
1005