Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | 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
175
            bulk_state[bulk_ctrlreq_ep].addr = (void*)buf[1];
176
            bulk_state[bulk_ctrlreq_ep].size = buf[2];
177
            usbdebug_bulk_xfer_complete(data, 0, bulk_ctrlreq_ep, 0);  // Convenient way to start a transfer.
178
            usb_set_stall(data, ep0out, true);
179
            usb_ep0_start_tx(data, NULL, 0, NULL);
180
            break;
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;
446
    if (dbgmemaddr)
447
    {
944 theseven 448
        usb_ep0_start_tx(dbgusb, dbgbuf, bytes,
449
                         bytes < 64 ? usb_ep0_short_tx_callback : read_console_callback);
896 theseven 450
    }
944 theseven 451
    else usb_ep0_start_tx(dbgusb, dbgbuf, bytes, NULL);
896 theseven 452
    return true;
453
}
454
 
892 theseven 455
int usbdebug_handle_setup(const struct usb_instance* data, int interface, union usb_ep0_buffer* request, const void** response)
456
{
457
    int size = -1;
944 theseven 458
    union usb_endpoint_number ep0out = { .number = 0, .direction = USB_ENDPOINT_DIRECTION_OUT };
892 theseven 459
    switch (request->setup.bmRequestType.type)
460
    {
461
    case USB_SETUP_BMREQUESTTYPE_TYPE_VENDOR:
462
        switch (request->setup.bRequest.raw)
463
        {
464
        case 0x00:
465
            switch (data->buffer->setup.bmRequestType.direction)
466
            {
467
            case USB_SETUP_BMREQUESTTYPE_DIRECTION_OUT:
468
                dbgstate = DBGSTATE_SETUP;
469
                dbgmemlen = 0;
944 theseven 470
                usb_ep0_start_rx(dbgusb, true, 64, usbdebug_handle_data);
892 theseven 471
                return -3;
472
            case USB_SETUP_BMREQUESTTYPE_DIRECTION_IN:
473
                switch (dbgstate)
474
                {
475
                case DBGSTATE_RESPOND:
476
                {
944 theseven 477
                    dbgmemlen = MIN(dbgmemlen, data->buffer->setup.wLength - 16);
892 theseven 478
                    int len = MIN(48, dbgmemlen);
479
                    if (len) memcpy(&dbgbuf[4], dbgmemaddr, len);
480
                    dbgmemlen -= len;
481
                    if (dbgmemlen)
482
                    {
944 theseven 483
                        usb_ep0_start_rx(dbgusb, false, 0, NULL);
484
                        dbgusb->state->ep0_tx_ptr = dbgmemaddr + 48;
485
                        dbgusb->state->ep0_tx_len = dbgmemlen;
486
                        usb_ep0_start_tx(dbgusb, dbgbuf, len + 16,
487
                                         len < 48 ? usb_ep0_short_tx_callback : usb_ep0_tx_callback);
892 theseven 488
                        return -3;
489
                    }
490
                    dbgstate = DBGSTATE_IDLE;
491
                    *response = dbgbuf;
492
                    return len + 16;
493
                }
896 theseven 494
                case DBGSTATE_READ_CONSOLE:
495
                {
944 theseven 496
                    dbgmemaddr = (void*)MIN((int)dbgmemaddr, data->buffer->setup.wLength - 16);
896 theseven 497
                    dbgconsoleattached = true;
498
                    dbgmemlen = dbgconsendwriteidx - dbgconsendreadidx;
499
                    if (dbgmemlen < 0) dbgmemlen += sizeof(dbgconsendbuf);
500
                    int used = dbgmemlen;
501
                    if (dbgmemlen > (int)dbgmemaddr) dbgmemlen = (int)dbgmemaddr;
502
                    int bytes = MIN(48, dbgmemlen);
503
                    dbgbuf[0] = 1;
504
                    dbgbuf[1] = dbgmemlen;
505
                    dbgbuf[2] = sizeof(dbgconsendbuf);
506
                    dbgbuf[3] = used - dbgmemlen;
507
                    if (bytes)
508
                    {
509
                        int readbytes = bytes;
510
                        char* outptr = (char*)&dbgbuf[4];
511
                        if (dbgconsendreadidx + bytes >= sizeof(dbgconsendbuf))
512
                        {
513
                            readbytes = sizeof(dbgconsendbuf) - dbgconsendreadidx;
514
                            memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
515
                            dbgconsendreadidx = 0;
516
                            outptr = &outptr[readbytes];
517
                            readbytes = bytes - readbytes;
518
                        }
519
                        if (readbytes) memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
520
                        dbgconsendreadidx += readbytes;
521
                        wakeup_signal(&dbgconsendwakeup);
522
                    }
523
                    dbgmemlen -= bytes;
524
                    bytes = MIN(48, (int)dbgmemaddr);
525
                    dbgmemaddr -= bytes;
526
                    if (dbgmemaddr)
527
                    {
944 theseven 528
                        dbgusb->state->ep0_tx_ptr = (void*)true;
529
                        usb_ep0_start_rx(dbgusb, false, 0, NULL);
530
                        usb_ep0_start_tx(dbgusb, dbgbuf, bytes + 16,
531
                                         bytes < 48 ? usb_ep0_short_tx_callback : read_console_callback);
896 theseven 532
                        return -3;
533
                    }
534
                    dbgstate = DBGSTATE_IDLE;
535
                    *response = dbgbuf;
536
                    return bytes + 16;
537
                }
892 theseven 538
                default: return -2;
539
                }
540
                break;
541
            }
542
            break;
543
        default: break;
544
        }
545
        break;
546
        default: break;
547
    }
548
    return size;
549
}
550
 
551
void dbgthread(void* arg0, void* arg1, void* arg2, void* arg3)
552
{
553
    struct scheduler_thread* t;
554
    while (1)
555
    {
556
        wakeup_wait(&dbgwakeup, TIMEOUT_BLOCK);
557
        for (t = head_thread; t; t = t->thread_next)
558
            if (t->state == THREAD_DEFUNCT)
559
            {
560
                if (t->block_type == THREAD_DEFUNCT_STKOV)
561
                {
562
                    if (t->name) cprintf(1, "\n*PANIC*\nStack overflow! (%s)\n", t->name);
563
                    else cprintf(1, "\n*PANIC*\nStack overflow! (%08X)\n", t);
564
                }
565
                t->state = THREAD_DEFUNCT_ACK;
566
            }
567
        uint32_t mode = enter_critical_section();
568
        uint32_t buf[16];
569
        if (dbgstate == DBGSTATE_ASYNC)
570
        {
571
            memcpy(buf, dbgbuf, 64);
572
            leave_critical_section(mode);
573
            void* addr = &buf[4];
574
            int len = 0;
575
            switch (buf[0])
576
            {
577
                case 2:  // RESET
578
                    shutdown(false);
579
                    reset();
580
                case 3:  // POWER OFF
581
                    if (buf[1]) shutdown(true);
582
                    power_off();
583
                    buf[0] = 1;
584
                    break;
585
#ifdef HAVE_I2C
586
                case 8:  // READ I2C
587
                    len = buf[1] >> 24;
588
                    i2c_recv(buf[1] & 0xff, (buf[1] >> 8) & 0xff, (buf[1] >> 16) & 0xff,
589
                             (uint8_t*)&buf[4], len);
590
                    buf[0] = 1;
591
                    break;
592
                case 9:  // WRITE I2C
593
                    i2c_send(buf[1] & 0xff, (buf[1] >> 8) & 0xff, (buf[1] >> 16) & 0xff,
594
                             (uint8_t*)&buf[4], buf[1] >> 24);
595
                    buf[0] = 1;
596
                    break;
597
#endif
598
                case 12:  // CWRITE
599
                    cwrite(buf[1], (const char*)&buf[4], buf[2]);
600
                    buf[0] = 1;
601
                    break;
602
                case 13:  // CREAD
603
                    buf[0] = 1;
604
                    buf[1] = cread(buf[1], (char*)&buf[4], buf[2], 0);
605
                    break;
606
                case 14:  // CFLUSH
607
                    cflush(buf[1]);
608
                    buf[0] = 1;
609
                    break;
610
                case 21:  // EXECIMAGE
611
                {
612
                    int argc = buf[2] >> 24;
613
                    if (!buf[3])
614
                    {
615
                        buf[3] = (uint32_t)&buf[4];
616
                        int i;
617
                        for (i = 0; i < argc; i++) buf[i + 4] += buf[3];
618
                    }
619
                    buf[0] = 1;
620
                    buf[1] = (uint32_t)execimage((void*)buf[1], buf[2] & 0x10000, argc, (const char* const*)buf[3]);
621
                    break;
622
                }
623
#ifdef HAVE_BOOTFLASH
624
                case 22:  // READ BOOT FLASH
625
                    bootflash_readraw((void*)buf[1], buf[2], buf[3]);
626
                    buf[0] = 1;
627
                    break;
628
                case 23:  // WRITE BOOT FLASH
629
                    bootflash_writeraw((void*)buf[1], buf[2], buf[3]);
630
                    buf[0] = 1;
631
                    break;
632
#endif
633
                case 24:  // EXECFIRMWARE
634
                    shutdown(false);
635
                    execfirmware((void*)buf[1], (void*)buf[2], (size_t)buf[3]);
636
                    buf[0] = 1;
637
                    break;
638
#ifdef HAVE_HWKEYAES
639
                case 25:  // HWKEYAES
640
                    hwkeyaes((enum hwkeyaes_direction)((uint8_t*)buf)[4], ((uint16_t*)buf)[3], (void*)buf[2], buf[3]);
641
                    buf[0] = 1;
642
                    break;
643
#endif
644
#ifdef HAVE_HMACSHA1
645
                case 26:  // HMACSHA1
646
                    hmacsha1((void*)buf[1], buf[2], (void*)buf[3]);
647
                    buf[0] = 1;
648
                    break;
649
#endif
650
#ifdef HAVE_STORAGE
651
                case 27:  // STORAGE_GET_INFO
652
                    buf[0] = 1;
653
                    storage_get_info(buf[1], (struct storage_info*)&buf[4]);
654
                    buf[1] = 1;
933 theseven 655
                    len = (sizeof(struct storage_info) + 3) / 4 * 4;
892 theseven 656
                    break;
657
                case 28:  // STORAGE_READ_SECTORS_MD
658
                    buf[0] = 1;
659
                    buf[1] = (uint32_t)storage_read_sectors_md(buf[1], buf[2] | (((uint64_t)(buf[3]) << 32)),
660
                                                               buf[4], (void*)(buf[5]));
661
                    break;
662
                case 29:  // STORAGE_WRITE_SECTORS_MD
663
                    buf[0] = 1;
664
                    buf[1] = (uint32_t)storage_write_sectors_md(buf[1], buf[2] | (((uint64_t)(buf[3]) << 32)),
665
                                                                buf[4], (void*)(buf[5]));
666
                    break;
667
                case 30:  // FILE_OPEN
668
                {
669
                    buf[0] = 1;
670
                    if (!buf[3]) buf[3] = (uint32_t)&buf[4];
671
                    int fd = file_open((char*)buf[3], (int)buf[1]);
672
                    if (fd > 0) reown_file(fd, KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
673
                    buf[1] = (uint32_t)fd;
674
                    break;
675
                }
676
                case 31:  // FILESIZE
677
                    buf[0] = 1;
678
                    buf[1] = (uint32_t)filesize((int)buf[1]);
679
                    break;
680
                case 32:  // READ
681
                    buf[0] = 1;
682
                    buf[1] = (uint32_t)read((int)buf[1], (void*)buf[2], (size_t)buf[3]);
683
                    break;
684
                case 33:  // WRITE
685
                    buf[0] = 1;
686
                    buf[1] = (uint32_t)write((int)buf[1], (void*)buf[2], (size_t)buf[3]);
687
                    break;
688
                case 34:  // LSEEK
689
                    buf[0] = 1;
690
                    buf[1] = (uint32_t)lseek((int)buf[1], (off_t)buf[2], (int)buf[3]);
691
                    break;
692
                case 35:  // FTRUNCATE
693
                    buf[0] = 1;
694
                    buf[1] = (uint32_t)ftruncate((int)buf[1], (off_t)buf[2]);
695
                    break;
696
                case 36:  // FSYNC
697
                    buf[0] = 1;
698
                    buf[1] = (uint32_t)fsync((int)buf[1]);
699
                    break;
700
                case 37:  // CLOSE
701
                    buf[0] = 1;
702
                    buf[1] = (uint32_t)close((int)buf[1]);
703
                    break;
704
                case 38:  // CLOSE_MONITOR_FILES
705
                    buf[0] = 1;
706
                    buf[1] = (uint32_t)close_all_of_process(KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
707
                    break;
708
                case 39:  // RELEASE_FILES
709
                    buf[0] = 1;
710
                    buf[1] = (uint32_t)release_files((int)buf[1]);
711
                    break;
712
                case 40:  // REMOVE
713
                    buf[0] = 1;
714
                    if (!buf[3]) buf[3] = (uint32_t)&buf[4];
715
                    buf[1] = (uint32_t)remove((char*)buf[3]);
716
                    break;
717
                case 41:  // RENAME
718
                    buf[0] = 1;
719
                    buf[1] = (uint32_t)rename((char*)buf[2], (char*)buf[3]);
720
                    break;
721
                case 42:  // OPENDIR
722
                {
723
                    buf[0] = 1;
724
                    if (!buf[3]) buf[3] = (uint32_t)&buf[4];
725
                    DIR* dir = opendir((char*)buf[3]);
726
                    if (dir > 0) reown_dir(dir, KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
727
                    buf[1] = (uint32_t)dir;
728
                    break;
729
                }
730
                case 43:  // READDIR
731
                    buf[0] = 1;
732
                    buf[3] = (uint32_t)readdir((DIR*)buf[1]);
733
                    buf[1] = 1;
734
                    buf[2] = MAX_PATH;
735
                    break;
736
                case 44:  // CLOSEDIR
737
                    buf[0] = 1;
738
                    buf[1] = (uint32_t)closedir((DIR*)buf[1]);
739
                    break;
740
                case 45:  // CLOSE_MONITOR_DIRS
741
                    buf[0] = 1;
742
                    buf[1] = (uint32_t)closedir_all_of_process(KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
743
                    break;
744
                case 46:  // RELEASE_DIRS
745
                    buf[0] = 1;
746
                    buf[1] = (uint32_t)release_dirs((int)buf[1]);
747
                    break;
748
                case 47:  // MKDIR
749
                    buf[0] = 1;
750
                    if (!buf[3]) buf[3] = (uint32_t)&buf[4];
751
                    buf[1] = (uint32_t)mkdir((char*)buf[3]);
752
                    break;
753
                case 48:  // RMDIR
754
                    buf[0] = 1;
755
                    if (!buf[3]) buf[3] = (uint32_t)&buf[4];
756
                    buf[1] = (uint32_t)rmdir((char*)buf[3]);
757
                    break;
758
                case 49:  // ERRNO
759
                    buf[0] = 1;
760
                    buf[1] = (uint32_t)errno;
761
                    break;
762
#ifdef HAVE_HOTSWAP
763
                case 50:  // DISK_MOUNT
764
                    buf[0] = 1;
765
                    buf[1] = (uint32_t)disk_mount((int)buf[1]);
766
                    break;
767
                case 51:  // DISK_UNMOUNT
768
                    buf[0] = 1;
769
                    buf[1] = (uint32_t)disk_unmount((int)buf[1]);
770
                    break;
771
#endif
772
                case 58:  // FAT_ENABLE_FLUSHING
773
                    buf[0] = 1;
774
                    fat_enable_flushing((bool)buf[1]);
775
                    break;
776
                case 59:  // FAT_SIZE
777
                    buf[0] = 1;
778
                    fat_size_mv(buf[1], &buf[1], &buf[2]);
779
                    break;
780
#endif
781
                case 52:  // MALLOC
782
                    buf[0] = 1;
783
                    buf[1] = (uint32_t)malloc((size_t)buf[1]);
784
                    if (buf[1]) reownalloc(buf[1], KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
785
                    break;
786
                case 53:  // MEMALIGN
787
                    buf[0] = 1;
788
                    buf[1] = (uint32_t)memalign((size_t)buf[1], (size_t)buf[2]);
789
                    if (buf[1]) reownalloc(buf[1], KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
790
                    break;
791
                case 54:  // REALLOC
792
                    buf[0] = 1;
793
                    buf[1] = (uint32_t)realloc((void*)buf[1], (size_t)buf[2]);
794
                    break;
795
                case 55:  // REOWNALLOC
796
                    buf[0] = 1;
797
                    reownalloc((void*)buf[1], (void*)buf[2]);
798
                    break;
799
                case 56:  // FREE
800
                    buf[0] = 1;
801
                    free((void*)buf[1]);
802
                    break;
803
                case 57:  // FREE MONITOR ALLOCATIONS
804
                    buf[0] = 1;
805
                    buf[1] = (uint32_t)free_all_of_thread(KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
806
                    break;
807
#ifdef HAVE_RTC
808
                case 60:  // RTC READ
809
                    buf[0] = 1;
810
                    rtc_read_datetime((struct rtc_datetime*)&buf[1]);
811
                    break;
812
                case 61:  // RTC WRITE
813
                    buf[0] = 1;
814
                    rtc_write_datetime((const struct rtc_datetime*)&buf[1]);
815
                    break;
816
#endif
931 theseven 817
                default:
892 theseven 818
#ifdef USB_HAVE_TARGET_SPECIFIC_REQUESTS
931 theseven 819
                    if (buf[0] >= 0xffff0000)
820
                        len = usb_target_handle_request(buf, sizeof(buf), &addr);
932 theseven 821
                    else buf[0] = 2;
822
#else
823
                    buf[0] = 2;
892 theseven 824
#endif
825
                    break;
826
            }
827
            mode = enter_critical_section();
828
            if (dbgstate == DBGSTATE_ASYNC)
829
            {
944 theseven 830
                usb_ep0_start_tx(dbgusb, NULL, 0, NULL);
892 theseven 831
                dbgstate = DBGSTATE_RESPOND;
832
                dbgmemaddr = addr;
833
                dbgmemlen = len;
933 theseven 834
                memcpy(dbgbuf, buf, 16);
892 theseven 835
            }
836
        }
837
        dbgbusy = false;
838
        leave_critical_section(mode);
839
    }
840
}
841
 
842
void usbdebug_init(void)
843
{
844
    wakeup_init(&dbgwakeup);
845
    dbgconsendreadidx = 0;
846
    dbgconsendwriteidx = 0;
847
    dbgconrecvreadidx = 0;
848
    dbgconrecvwriteidx = 0;
849
    wakeup_init(&dbgconsendwakeup);
850
    wakeup_init(&dbgconrecvwakeup);
851
    dbgenabled = false;
852
    dbgbusy = false;
853
    dbgstate = DBGSTATE_IDLE;
854
    dbgconsoleattached = false;
855
    thread_create(&dbgthread_handle, "monitor worker", dbgthread, dbgstack,
856
                  sizeof(dbgstack), CORE_THREAD, 255, true, NULL, NULL, NULL, NULL);
857
}
858
 
859
int dbgconsole_getfree() ICODE_ATTR;
860
int dbgconsole_getfree()
861
{
862
    int free = dbgconsendreadidx - dbgconsendwriteidx - 1;
863
    if (free < 0) free += sizeof(dbgconsendbuf);
864
    return free;
865
}
866
 
867
int dbgconsole_makespace(int length, bool safe) ICODE_ATTR;
868
int dbgconsole_makespace(int length, bool safe)
869
{
870
    int free = dbgconsole_getfree();
871
    while (!free && dbgconsoleattached && !safe)
872
    {
873
        dbgconsoleattached = false;
874
        wakeup_wait(&dbgconsendwakeup, 2000000);
875
        free = dbgconsole_getfree();
876
    }
877
    if (free) return free > length ? length : free;
878
    if (length > sizeof(dbgconsendbuf) - 17) length = sizeof(dbgconsendbuf) - 17;
879
    uint32_t mode = enter_critical_section();
880
    dbgconsendreadidx += length;
881
    if (dbgconsendreadidx >= sizeof(dbgconsendbuf))
882
        dbgconsendreadidx -= sizeof(dbgconsendbuf);
883
    int offset = 0;
884
    int idx = dbgconsendreadidx;
885
    if (idx + 16 >= sizeof(dbgconsendbuf))
886
    {
887
        offset = sizeof(dbgconsendbuf) - dbgconsendreadidx;
888
        memcpy(&dbgconsendbuf[dbgconsendreadidx], dbgconoverflowstr, offset);
889
        idx = 0;
890
    }
891
    if (offset != 16) memcpy(&dbgconsendbuf[idx], &dbgconoverflowstr[offset], 16 - offset);
892
    leave_critical_section(mode);
893
    return length;
894
}
895
 
896
void dbgconsole_putc_internal(char string, bool safe)
897
{
898
    dbgconsole_makespace(1, safe);
899
    dbgconsendbuf[dbgconsendwriteidx++] = string;
900
    if (dbgconsendwriteidx >= sizeof(dbgconsendbuf))
901
        dbgconsendwriteidx -= sizeof(dbgconsendbuf);
902
}
903
 
904
void dbgconsole_putc(char string)
905
{
906
    dbgconsole_putc_internal(string, false);
907
}
908
 
909
void dbgconsole_sputc(char string)
910
{
911
    dbgconsole_putc_internal(string, true);
912
}
913
 
914
void dbgconsole_write_internal(const char* string, size_t length, bool safe)
915
{
916
    while (length)
917
    {
918
        int space = dbgconsole_makespace(length, safe);
919
        if (dbgconsendwriteidx + space >= sizeof(dbgconsendbuf))
920
        {
921
            int bytes = sizeof(dbgconsendbuf) - dbgconsendwriteidx;
922
            memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, bytes);
923
            dbgconsendwriteidx = 0;
924
            string = &string[bytes];
925
            space -= bytes;
926
            length -= bytes;
927
        }
928
        if (space) memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, space);
929
        dbgconsendwriteidx += space;
930
        string = &string[space];
931
        length -= space;
932
    }
933
}
934
 
935
void dbgconsole_write(const char* string, size_t length)
936
{
937
    dbgconsole_write_internal(string, length, false);
938
}
939
 
940
void dbgconsole_swrite(const char* string, size_t length)
941
{
942
    dbgconsole_write_internal(string, length, true);
943
}
944
 
945
void dbgconsole_puts(const char* string)
946
{
947
    dbgconsole_write(string, strlen(string));
948
}
949
 
950
void dbgconsole_sputs(const char* string)
951
{
952
    dbgconsole_swrite(string, strlen(string));
953
}
954
 
955
int dbgconsole_getavailable() ICODE_ATTR;
956
int dbgconsole_getavailable()
957
{
958
    int available = dbgconrecvwriteidx - dbgconrecvreadidx;
959
    if (available < 0) available += sizeof(dbgconrecvbuf);
960
    return available;
961
}
962
 
963
int dbgconsole_getc(int timeout)
964
{
965
    if (!dbgconsole_getavailable())
966
    {
967
        wakeup_wait(&dbgconrecvwakeup, TIMEOUT_NONE);
968
        if (!dbgconsole_getavailable())
969
        {
970
            wakeup_wait(&dbgconrecvwakeup, timeout);
971
            if (!dbgconsole_getavailable()) return -1;
972
        }
973
    }
974
    int byte = dbgconrecvbuf[dbgconrecvreadidx++];
975
    if (dbgconrecvreadidx >= sizeof(dbgconrecvbuf))
976
        dbgconrecvreadidx -= sizeof(dbgconrecvbuf);
977
    return byte;
978
}
979
 
980
int dbgconsole_read(char* buffer, size_t length, int timeout)
981
{
982
    if (!length) return 0;
983
    int available = dbgconsole_getavailable();
984
    if (!available)
985
    {
986
        wakeup_wait(&dbgconrecvwakeup, TIMEOUT_NONE);
987
        int available = dbgconsole_getavailable();
988
        if (!available)
989
        {
990
            wakeup_wait(&dbgconrecvwakeup, timeout);
991
            int available = dbgconsole_getavailable();
992
            if (!available) return 0;
993
        }
994
    }
995
    if (available > length) available = length;
996
    int left = available;
997
    if (dbgconrecvreadidx + available >= sizeof(dbgconrecvbuf))
998
    {
999
        int bytes = sizeof(dbgconrecvbuf) - dbgconrecvreadidx;
1000
        memcpy(buffer, &dbgconrecvbuf[dbgconrecvreadidx], bytes);
1001
        dbgconrecvreadidx = 0;
1002
        buffer = &buffer[bytes];
1003
        left -= bytes;
1004
    }
1005
    if (left) memcpy(buffer, &dbgconrecvbuf[dbgconrecvreadidx], left);
1006
    dbgconrecvreadidx += left;
1007
    return available;
1008
}
1009