Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emBIOS.
7
//
8
//    emBIOS 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
//    emBIOS 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 emBIOS.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "panic.h"
26
#include "usb.h"
27
#include "usb_ch9.h"
28
#include "usbdrv.h"
29
#include "thread.h"
30
#include "console.h"
31
#include "util.h"
25 theseven 32
#include "contextswitch.h"
54 theseven 33
#include "power.h"
35 theseven 34
#include "mmu.h"
29 theseven 35
#include "shutdown.h"
92 theseven 36
#include "execimage.h"
85 theseven 37
#ifdef HAVE_I2C
38
#include "i2c.h"
39
#endif
95 theseven 40
#ifdef HAVE_BOOTFLASH
41
#include "bootflash.h"
42
#endif
157 theseven 43
#ifdef HAVE_HWKEYAES
44
#include "hwkeyaes.h"
45
#endif
46
#ifdef HAVE_HMACSHA1
47
#include "hmacsha1.h"
48
#endif
226 theseven 49
#ifdef USB_HAVE_TARGET_SPECIFIC_REQUESTS
50
#include "usbtarget.h"
51
#endif
15 theseven 52
 
53
 
54
static uint8_t ctrlresp[2] CACHEALIGN_ATTR;
55
static uint32_t dbgrecvbuf[0x80] CACHEALIGN_ATTR;
56
static uint32_t dbgsendbuf[0x80] CACHEALIGN_ATTR;
57
static uint32_t dbgasyncsendbuf[0x80] CACHEALIGN_ATTR;
58
static char dbgendpoints[4] IBSS_ATTR;
59
 
60
enum dbgaction_t
61
{
62
    DBGACTION_IDLE = 0,
63
    DBGACTION_I2CSEND,
64
    DBGACTION_I2CRECV,
29 theseven 65
    DBGACTION_RESET,
66
    DBGACTION_POWEROFF,
67
    DBGACTION_CWRITE,
68
    DBGACTION_CREAD,
92 theseven 69
    DBGACTION_CFLUSH,
95 theseven 70
    DBGACTION_EXECIMAGE,
127 theseven 71
    DBGACTION_EXECFIRMWARE,
95 theseven 72
    DBGACTION_READBOOTFLASH,
157 theseven 73
    DBGACTION_WRITEBOOTFLASH,
74
    DBGACTION_HWKEYAES,
226 theseven 75
    DBGACTION_HMACSHA1,
76
    DBGACTION_TARGETSPECIFIC
15 theseven 77
};
78
 
79
static uint32_t dbgstack[0x100] STACK_ATTR;
80
struct wakeup dbgwakeup IBSS_ATTR;
81
extern struct scheduler_thread* scheduler_threads;
82
static enum dbgaction_t dbgaction IBSS_ATTR;
29 theseven 83
static int dbgi2cbus;
84
static int dbgi2cslave;
85
static int dbgactionaddr;
95 theseven 86
static int dbgactionoffset;
29 theseven 87
static int dbgactionlength;
88
static int dbgactionconsoles;
89
static int dbgactiontype;
25 theseven 90
static char dbgconsendbuf[4096];
91
static char dbgconrecvbuf[1024];
92
static int dbgconsendreadidx IBSS_ATTR;
93
static int dbgconsendwriteidx IBSS_ATTR;
94
static int dbgconrecvreadidx IBSS_ATTR;
95
static int dbgconrecvwriteidx IBSS_ATTR;
96
static struct wakeup dbgconsendwakeup IBSS_ATTR;
97
static struct wakeup dbgconrecvwakeup IBSS_ATTR;
98
static bool dbgconsoleattached IBSS_ATTR;
15 theseven 99
 
25 theseven 100
static const char dbgconoverflowstr[] = "\n\n[overflowed]\n\n";
15 theseven 101
 
35 theseven 102
extern int _initstart;   // These aren't ints at all, but gcc complains about void types being
103
extern int _sdramstart;  // used here, and we only need the address, so forget about it...
25 theseven 104
 
35 theseven 105
 
15 theseven 106
static struct usb_device_descriptor CACHEALIGN_ATTR device_descriptor =
107
{
108
    .bLength            = sizeof(struct usb_device_descriptor),
109
    .bDescriptorType    = USB_DT_DEVICE,
110
    .bcdUSB             = 0x0200,
111
    .bDeviceClass       = USB_CLASS_VENDOR_SPEC,
112
    .bDeviceSubClass    = 0xff,
113
    .bDeviceProtocol    = 0xff,
114
    .bMaxPacketSize0    = 64,
115
    .idVendor           = 0xffff,
116
    .idProduct          = 0xe000,
117
    .bcdDevice          = 0x0001,
118
    .iManufacturer      = 1,
119
    .iProduct           = 2,
120
    .iSerialNumber      = 0,
121
    .bNumConfigurations = 1
122
};
123
 
124
static struct usb_config_bundle
125
{
126
    struct usb_config_descriptor config_descriptor;
127
    struct usb_interface_descriptor interface_descriptor;
128
    struct usb_endpoint_descriptor endpoint1_descriptor;
129
    struct usb_endpoint_descriptor endpoint2_descriptor;
130
    struct usb_endpoint_descriptor endpoint3_descriptor;
131
    struct usb_endpoint_descriptor endpoint4_descriptor;
132
} __attribute__((packed)) CACHEALIGN_ATTR config_bundle = 
133
{
134
    .config_descriptor =
135
    {
136
        .bLength             = sizeof(struct usb_config_descriptor),
137
        .bDescriptorType     = USB_DT_CONFIG,
138
        .wTotalLength        = sizeof(struct usb_config_descriptor)
139
                             + sizeof(struct usb_interface_descriptor)
140
                             + sizeof(struct usb_endpoint_descriptor) * 4,
141
        .bNumInterfaces      = 1,
142
        .bConfigurationValue = 1,
143
        .iConfiguration      = 0,
144
        .bmAttributes        = USB_CONFIG_ATT_ONE,
145
        .bMaxPower           = 250
146
    },
147
    .interface_descriptor =
148
    {
149
        .bLength             = sizeof(struct usb_interface_descriptor),
150
        .bDescriptorType     = USB_DT_INTERFACE,
151
        .bInterfaceNumber    = 0,
152
        .bAlternateSetting   = 0,
153
        .bNumEndpoints       = 4,
154
        .bInterfaceClass     = USB_CLASS_VENDOR_SPEC,
155
        .bInterfaceSubClass  = 0xff,
156
        .bInterfaceProtocol  = 0xff,
157
        .iInterface          = 0
158
    },
159
    .endpoint1_descriptor =
160
    {
161
        .bLength             = sizeof(struct usb_endpoint_descriptor),
162
        .bDescriptorType     = USB_DT_ENDPOINT,
163
        .bEndpointAddress    = 0,
164
        .bmAttributes        = USB_ENDPOINT_XFER_BULK,
165
        .wMaxPacketSize      = 0,
166
        .bInterval           = 1
167
    },
168
    .endpoint2_descriptor =
169
    {
170
        .bLength             = sizeof(struct usb_endpoint_descriptor),
171
        .bDescriptorType     = USB_DT_ENDPOINT,
172
        .bEndpointAddress    = 0,
173
        .bmAttributes        = USB_ENDPOINT_XFER_BULK,
174
        .wMaxPacketSize      = 0,
175
        .bInterval           = 1
176
    },
177
    .endpoint3_descriptor =
178
    {
179
        .bLength             = sizeof(struct usb_endpoint_descriptor),
180
        .bDescriptorType     = USB_DT_ENDPOINT,
181
        .bEndpointAddress    = 0,
182
        .bmAttributes        = USB_ENDPOINT_XFER_BULK,
183
        .wMaxPacketSize      = 0,
184
        .bInterval           = 1
185
    },
186
    .endpoint4_descriptor =
187
    {
188
        .bLength             = sizeof(struct usb_endpoint_descriptor),
189
        .bDescriptorType     = USB_DT_ENDPOINT,
190
        .bEndpointAddress    = 0,
191
        .bmAttributes        = USB_ENDPOINT_XFER_BULK,
192
        .wMaxPacketSize      = 0,
193
        .bInterval           = 1
194
    }
195
};
196
 
197
static struct usb_string_descriptor CACHEALIGN_ATTR string_devicename =
198
{
199
    32,
200
    USB_DT_STRING,
201
    {'e', 'm', 'B', 'I', 'O', 'S', ' ', 'D', 'e', 'b', 'u', 'g', 'g', 'e', 'r'}
202
};
203
 
204
static const struct usb_string_descriptor CACHEALIGN_ATTR lang_descriptor =
205
{
206
    4,
207
    USB_DT_STRING,
208
    {0x0409}
209
};
210
 
211
 
212
void usb_setup_dbg_listener()
213
{
214
    usb_drv_recv(dbgendpoints[0], dbgrecvbuf, usb_drv_port_speed() ? 512 : 64);
215
}
216
 
217
void usb_handle_control_request(struct usb_ctrlrequest* req)
218
{
219
    const void* addr;
220
    int size = -1;
221
    switch (req->bRequest)
222
    {
223
    case USB_REQ_GET_STATUS:
224
        if (req->bRequestType == USB_DIR_IN) ctrlresp[0] = 1;
225
        else ctrlresp[0] = 0;
226
        ctrlresp[1] = 0;
227
        addr = ctrlresp;
228
        size = 2;
229
        break;
230
    case USB_REQ_CLEAR_FEATURE:
231
        if (req->bRequestType == USB_RECIP_ENDPOINT && req->wValue == USB_ENDPOINT_HALT)
232
            usb_drv_stall(req->wIndex & 0xf, false, req->wIndex >> 7);
233
        size = 0;
234
        break;
235
    case USB_REQ_SET_FEATURE:
236
        size = 0;
237
        break;
238
    case USB_REQ_SET_ADDRESS:
239
        size = 0;
240
        usb_drv_cancel_all_transfers();
241
        usb_drv_set_address(req->wValue);
242
        usb_setup_dbg_listener();
243
        break;
244
    case USB_REQ_GET_DESCRIPTOR:
245
        switch (req->wValue >> 8)
246
        {
247
        case USB_DT_DEVICE:
248
            addr = &device_descriptor;
249
            size = sizeof(device_descriptor);
250
            break;
251
        case USB_DT_OTHER_SPEED_CONFIG:
252
        case USB_DT_CONFIG:
253
            if ((req->wValue >> 8) == USB_DT_CONFIG)
254
            {
255
                int maxpacket = usb_drv_port_speed() ? 512 : 64;
256
                config_bundle.endpoint1_descriptor.wMaxPacketSize = maxpacket;
257
                config_bundle.endpoint2_descriptor.wMaxPacketSize = maxpacket;
258
                config_bundle.endpoint3_descriptor.wMaxPacketSize = maxpacket;
259
                config_bundle.endpoint4_descriptor.wMaxPacketSize = maxpacket;
260
                config_bundle.config_descriptor.bDescriptorType = USB_DT_CONFIG;
261
            }
262
            else
263
            {
264
                int maxpacket = usb_drv_port_speed() ? 64 : 512;
265
                config_bundle.endpoint1_descriptor.wMaxPacketSize = maxpacket;
266
                config_bundle.endpoint2_descriptor.wMaxPacketSize = maxpacket;
267
                config_bundle.endpoint3_descriptor.wMaxPacketSize = maxpacket;
268
                config_bundle.endpoint4_descriptor.wMaxPacketSize = maxpacket;
269
                config_bundle.config_descriptor.bDescriptorType = USB_DT_OTHER_SPEED_CONFIG;
270
            }
271
            addr = &config_bundle;
272
            size = sizeof(config_bundle);
273
            break;
274
        case USB_DT_STRING:
275
            switch (req->wValue & 0xff)
276
            {
277
            case 0:
278
                addr = &lang_descriptor;
279
                size = sizeof(lang_descriptor);
280
                break;
281
            case 1:
282
                string_devicename.bLength = 14;
283
                addr = &string_devicename;
284
                size = 14;
285
            case 2:
286
                string_devicename.bLength = sizeof(string_devicename);
287
                addr = &string_devicename;
288
                size = sizeof(string_devicename);
289
                break;
290
            }
291
            break;
292
        }
293
        break;
294
    case USB_REQ_GET_CONFIGURATION:
295
        ctrlresp[0] = 1;
296
        addr = ctrlresp;
297
        size = 1;
298
        break;
299
    case USB_REQ_SET_CONFIGURATION:
300
        usb_drv_cancel_all_transfers();
301
        usb_setup_dbg_listener();
302
        size = 0;
303
        break;
304
    }
305
    if (!size) usb_drv_send_nonblocking(0, NULL, 0);
306
    else if (size == -1)
307
    {
308
        usb_drv_stall(0, true, true);
309
        usb_drv_stall(0, true, false);
310
    }
311
    else
312
    {
313
        usb_drv_recv(0, NULL, 0);
314
        usb_drv_send_nonblocking(0, addr, size > req->wLength ? req->wLength : size);
315
    }
316
}
317
 
29 theseven 318
bool set_dbgaction(enum dbgaction_t action, int addsize)
15 theseven 319
{
320
    if (dbgaction != DBGACTION_IDLE)
321
    {
322
        dbgsendbuf[0] = 3;
29 theseven 323
        usb_drv_send_nonblocking(dbgendpoints[1], dbgsendbuf, 16 + addsize);
15 theseven 324
        return true;
325
    }
326
    dbgaction = action;
327
    wakeup_signal(&dbgwakeup);
328
    return false;
329
}
330
 
331
void reset() __attribute__((noreturn));
332
 
333
void usb_handle_transfer_complete(int endpoint, int dir, int status, int length)
334
{
335
    void* addr = dbgsendbuf;
336
    int size = 0;
337
    if (endpoint == dbgendpoints[0])
338
    {
226 theseven 339
#ifdef USB_HAVE_TARGET_SPECIFIC_REQUESTS
340
        if (dbgrecvbuf[0] >= 0xffff0000)
341
        {
342
            if (!set_dbgaction(DBGACTION_TARGETSPECIFIC, 0))
343
                memcpy(dbgasyncsendbuf, dbgrecvbuf, sizeof(dbgasyncsendbuf));
344
            usb_setup_dbg_listener();
345
            return;
346
        }
347
#endif
15 theseven 348
        switch (dbgrecvbuf[0])
349
        {
28 theseven 350
        case 1:  // GET INFO
15 theseven 351
            dbgsendbuf[0] = 1;
352
            size = 16;
28 theseven 353
            switch (dbgrecvbuf[1])
354
            {
355
            case 0:  // GET VERSION INFO
85 theseven 356
                dbgsendbuf[1] = VERSION_SVN_INT;
357
                dbgsendbuf[2] = VERSION_MAJOR | (VERSION_MINOR << 8)
28 theseven 358
                              | (VERSION_PATCH << 16) | (1 << 24);
85 theseven 359
                dbgsendbuf[3] = PLATFORM_ID;
28 theseven 360
                break;
361
            case 1:  // GET PACKET SIZE INFO
362
                dbgsendbuf[1] = 0x02000200;
363
                dbgsendbuf[2] = usb_drv_get_max_out_size();
364
                dbgsendbuf[3] = usb_drv_get_max_in_size();
365
                break;
35 theseven 366
            case 2:  // GET USER MEMORY INFO
367
                dbgsendbuf[1] = (uint32_t)&_initstart;
368
                dbgsendbuf[2] = (uint32_t)&_sdramstart;
369
                break;
28 theseven 370
            default:
371
                dbgsendbuf[0] = 2;
372
            }
15 theseven 373
            break;
374
        case 2:  // RESET
29 theseven 375
            if (dbgrecvbuf[1])
376
            {
377
                if (set_dbgaction(DBGACTION_RESET, 0)) break;
378
                dbgsendbuf[0] = 1;
379
                size = 16;
380
            }
381
            else reset();
15 theseven 382
            break;
383
        case 3:  // POWER OFF
29 theseven 384
            if (set_dbgaction(DBGACTION_POWEROFF, 0)) break;
385
            dbgactiontype = dbgrecvbuf[1];
386
            dbgsendbuf[0] = 1;
387
            size = 16;
15 theseven 388
            break;
389
        case 4:  // READ MEMORY
390
            dbgsendbuf[0] = 1;
391
            memcpy(&dbgsendbuf[4], (const void*)dbgrecvbuf[1], dbgrecvbuf[2]);
392
            size = dbgrecvbuf[2] + 16;
393
            break;
394
        case 5:  // WRITE MEMORY
395
            dbgsendbuf[0] = 1;
396
            memcpy((void*)dbgrecvbuf[1], &dbgrecvbuf[4], dbgrecvbuf[2]);
397
            size = 16;
398
            break;
399
        case 6:  // READ DMA
400
            dbgsendbuf[0] = 1;
401
            usb_drv_send_nonblocking(dbgendpoints[1], dbgsendbuf, 16);
402
            usb_drv_send_nonblocking(dbgendpoints[3], (const void*)dbgrecvbuf[1], dbgrecvbuf[2]);
403
            break;
404
        case 7:  // WRITE DMA
405
            dbgsendbuf[0] = 1;
406
            size = 16;
407
            usb_drv_recv(dbgendpoints[2], (void*)dbgrecvbuf[1], dbgrecvbuf[2]);
408
            break;
85 theseven 409
#ifdef HAVE_I2C
15 theseven 410
        case 8:  // READ I2C
29 theseven 411
            if (set_dbgaction(DBGACTION_I2CRECV, dbgrecvbuf[1] >> 24)) break;
15 theseven 412
            dbgi2cbus = dbgrecvbuf[1] & 0xff;
413
            dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
29 theseven 414
            dbgactionaddr = (dbgrecvbuf[1] >> 16) & 0xff;
415
            dbgactionlength = dbgrecvbuf[1] >> 24;
175 theseven 416
            if (!dbgactionlength) dbgactionlength = 256;
15 theseven 417
            break;
418
        case 9:  // WRITE I2C
29 theseven 419
            if (set_dbgaction(DBGACTION_I2CSEND, 0)) break;
15 theseven 420
            dbgi2cbus = dbgrecvbuf[1] & 0xff;
421
            dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
29 theseven 422
            dbgactionaddr = (dbgrecvbuf[1] >> 16) & 0xff;
423
            dbgactionlength = dbgrecvbuf[1] >> 24;
175 theseven 424
            if (!dbgactionlength) dbgactionlength = 256;
212 theseven 425
            memcpy(dbgasyncsendbuf, &dbgrecvbuf[4], dbgactionlength);
15 theseven 426
            break;
85 theseven 427
#endif
25 theseven 428
        case 10:  // READ CONSOLE
429
            dbgconsoleattached = true;
430
            int bytes = dbgconsendwriteidx - dbgconsendreadidx;
431
            if (bytes >= sizeof(dbgconsendbuf)) bytes -= sizeof(dbgconsendbuf);
432
            if (bytes)
433
            {
434
                if (bytes < 0) bytes += sizeof(dbgconsendbuf);
435
                if (bytes > dbgrecvbuf[1]) bytes = dbgrecvbuf[1];
436
                int readbytes = bytes;
437
                char* outptr = (char*)&dbgsendbuf[4];
438
                if (dbgconsendreadidx + bytes >= sizeof(dbgconsendbuf))
439
                {
440
                    readbytes = sizeof(dbgconsendbuf) - dbgconsendreadidx;
441
                    memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
442
                    dbgconsendreadidx = 0;
443
                    outptr = &outptr[readbytes];
444
                    readbytes = bytes - readbytes;
445
                }
446
                if (readbytes) memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
447
                dbgconsendreadidx += readbytes;
26 theseven 448
                wakeup_signal(&dbgconsendwakeup);
25 theseven 449
            }
450
            dbgsendbuf[0] = 1;
451
            dbgsendbuf[1] = bytes;
452
            dbgsendbuf[2] = sizeof(dbgconsendbuf);
453
            dbgsendbuf[3] = dbgconsendwriteidx - dbgconsendreadidx;
454
            size = 16 + dbgrecvbuf[1];
455
            break;
26 theseven 456
        case 11:  // WRITE CONSOLE
457
            bytes = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
458
            if (bytes < 0) bytes += sizeof(dbgconrecvbuf);
459
            if (bytes)
460
            {
461
                if (bytes > dbgrecvbuf[1]) bytes = dbgrecvbuf[1];
462
                int writebytes = bytes;
463
                char* readptr = (char*)&dbgrecvbuf[4];
464
                if (dbgconrecvwriteidx + bytes >= sizeof(dbgconrecvbuf))
465
                {
466
                    writebytes = sizeof(dbgconrecvbuf) - dbgconrecvwriteidx;
467
                    memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
468
                    dbgconrecvwriteidx = 0;
469
                    readptr = &readptr[writebytes];
470
                    writebytes = bytes - writebytes;
471
                }
472
                if (writebytes) memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
473
                dbgconrecvwriteidx += writebytes;
474
                wakeup_signal(&dbgconrecvwakeup);
475
            }
476
            dbgsendbuf[0] = 1;
477
            dbgsendbuf[1] = bytes;
478
            dbgsendbuf[2] = sizeof(dbgconrecvbuf);
479
            dbgsendbuf[3] = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
480
            size = 16;
481
            break;
29 theseven 482
        case 12:  // CWRITE
483
            if (set_dbgaction(DBGACTION_CWRITE, 0)) break;
484
            dbgactionconsoles = dbgrecvbuf[1];
485
            dbgactionlength = dbgrecvbuf[2];
486
            memcpy(dbgasyncsendbuf, &dbgrecvbuf[4], dbgactionlength);
487
            break;
488
        case 13:  // CREAD
489
            if (set_dbgaction(DBGACTION_CREAD, dbgrecvbuf[2])) break;
490
            dbgactionconsoles = dbgrecvbuf[1];
491
            dbgactionlength = dbgrecvbuf[2];
492
            break;
493
        case 14:  // CFLUSH
494
            if (set_dbgaction(DBGACTION_CFLUSH, 0)) break;
495
            dbgactionconsoles = dbgrecvbuf[1];
496
            break;
31 theseven 497
        case 15:  // GET PROCESS INFO
498
            dbgsendbuf[0] = 1;
499
            dbgsendbuf[1] = SCHEDULER_THREAD_INFO_VERSION;
500
            dbgsendbuf[2] = MAX_THREADS * sizeof(struct scheduler_thread);
136 theseven 501
            memcpy(&dbgsendbuf[4], (void*)(((uint32_t)&scheduler_threads) + dbgrecvbuf[1]),
57 theseven 502
                   dbgrecvbuf[2]);
503
            size = dbgrecvbuf[2] + 16;
31 theseven 504
            break;
34 theseven 505
        case 16:  // FREEZE SCHEDULER
54 theseven 506
            dbgsendbuf[1] = scheduler_freeze(dbgrecvbuf[1]);
34 theseven 507
            dbgsendbuf[0] = 1;
508
            size = 16;
509
            break;
35 theseven 510
        case 17:  // SUSPEND THREAD
54 theseven 511
            if (dbgrecvbuf[1])
512
            {
513
                if (thread_suspend(dbgrecvbuf[2]) == -4) dbgsendbuf[1] = 1;
514
                else dbgsendbuf[1] = 0;
515
            }
516
            else
517
            {
518
                if (thread_resume(dbgrecvbuf[2]) == -5) dbgsendbuf[1] = 0;
519
                else dbgsendbuf[1] = 1;
520
            }
35 theseven 521
            dbgsendbuf[0] = 1;
522
            size = 16;
523
            break;
524
        case 18:  // KILL THREAD
525
            thread_terminate(dbgrecvbuf[1]);
526
            dbgsendbuf[0] = 1;
527
            size = 16;
528
            break;
57 theseven 529
        case 19:  // KILL THREAD
35 theseven 530
            dbgsendbuf[0] = 1;
531
            dbgsendbuf[1] = thread_create((const char*)dbgsendbuf[1], (const void*)dbgsendbuf[2],
532
                                          (char*)dbgsendbuf[3], dbgsendbuf[4], dbgsendbuf[5],
533
                                          dbgsendbuf[6], dbgsendbuf[7]);
534
            size = 16;
535
            break;
536
        case 20:  // FLUSH CACHE
537
            clean_dcache();
538
            invalidate_icache();
539
            dbgsendbuf[0] = 1;
540
            size = 16;
541
            break;
92 theseven 542
        case 21:  // EXECIMAGE
543
            if (set_dbgaction(DBGACTION_EXECIMAGE, 0)) break;
544
            dbgactionaddr = dbgrecvbuf[1];
545
            break;
95 theseven 546
#ifdef HAVE_BOOTFLASH
547
        case 22:  // READ BOOT FLASH
548
            if (set_dbgaction(DBGACTION_READBOOTFLASH, 0)) break;
549
            dbgactionaddr = dbgrecvbuf[1];
550
            dbgactionoffset = dbgrecvbuf[2];
551
            dbgactionlength = dbgrecvbuf[3];
552
            break;
553
        case 23:  // WRITE BOOT FLASH
554
            if (set_dbgaction(DBGACTION_WRITEBOOTFLASH, 0)) break;
555
            dbgactionaddr = dbgrecvbuf[1];
556
            dbgactionoffset = dbgrecvbuf[2];
557
            dbgactionlength = dbgrecvbuf[3];
558
            break;
559
#endif
127 theseven 560
        case 24:  // EXECFIRMWARE
561
            if (set_dbgaction(DBGACTION_EXECFIRMWARE, 0)) break;
562
            dbgactionaddr = dbgrecvbuf[1];
563
            break;
157 theseven 564
#ifdef HAVE_HWKEYAES
565
        case 25:  // HWKEYAES
566
            if (set_dbgaction(DBGACTION_HWKEYAES, 0)) break;
567
            dbgactiontype = ((uint8_t*)dbgrecvbuf)[4];
568
            dbgactionoffset = ((uint16_t*)dbgrecvbuf)[3];
569
            dbgactionaddr = dbgrecvbuf[2];
570
            dbgactionlength = dbgrecvbuf[3];
179 theseven 571
            break;
157 theseven 572
#endif
573
#ifdef HAVE_HMACSHA1
574
        case 26:  // HMACSHA1
575
            if (set_dbgaction(DBGACTION_HMACSHA1, 0)) break;
576
            dbgactionaddr = dbgrecvbuf[1];
577
            dbgactionlength = dbgrecvbuf[2];
578
            dbgactionoffset = dbgrecvbuf[3];
179 theseven 579
            break;
157 theseven 580
#endif
15 theseven 581
        default:
582
            dbgsendbuf[0] = 2;
583
            size = 16;
584
        }
585
        usb_setup_dbg_listener();
586
        if (size) usb_drv_send_nonblocking(dbgendpoints[1], addr, size);
587
    }
588
}
589
 
590
void usb_handle_bus_reset(void)
591
{
592
    dbgendpoints[0] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
593
    dbgendpoints[1] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
594
    dbgendpoints[2] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
595
    dbgendpoints[3] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
596
    config_bundle.endpoint1_descriptor.bEndpointAddress = dbgendpoints[0];
597
    config_bundle.endpoint2_descriptor.bEndpointAddress = dbgendpoints[1];
598
    config_bundle.endpoint3_descriptor.bEndpointAddress = dbgendpoints[2];
599
    config_bundle.endpoint4_descriptor.bEndpointAddress = dbgendpoints[3];
600
    usb_setup_dbg_listener();
601
}
602
 
603
void dbgthread(void)
604
{
605
    int i;
25 theseven 606
    int t;
15 theseven 607
    while (1)
608
    {
609
        wakeup_wait(&dbgwakeup, TIMEOUT_BLOCK);
610
        for (i = 0; i < MAX_THREADS; i++)
611
            if (scheduler_threads[i].state == THREAD_DEFUNCT)
612
            {
613
                if (scheduler_threads[i].block_type == THREAD_DEFUNCT_STKOV)
35 theseven 614
                {
615
                    if (scheduler_threads[i].name)
616
                        cprintf(1, "\n*PANIC*\nStack overflow! (%s)\n",
617
                                scheduler_threads[i].name);
618
                    else cprintf(1, "\n*PANIC*\nStack overflow! (ID %d)\n", i);
619
                }
15 theseven 620
                scheduler_threads[i].state = THREAD_DEFUNCT_ACK;
621
            }
622
        if (dbgaction != DBGACTION_IDLE)
623
        {
624
            switch (dbgaction)
625
            {
85 theseven 626
#ifdef HAVE_I2C
15 theseven 627
            case DBGACTION_I2CSEND:
29 theseven 628
                i2c_send(dbgi2cbus, dbgi2cslave, dbgactionaddr,
629
                         (uint8_t*)dbgasyncsendbuf, dbgactionlength);
15 theseven 630
                dbgasyncsendbuf[0] = 1;
631
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
632
                break;
633
            case DBGACTION_I2CRECV:
29 theseven 634
                i2c_recv(dbgi2cbus, dbgi2cslave, dbgactionaddr,
635
                         (uint8_t*)(&dbgasyncsendbuf[4]), dbgactionlength);
15 theseven 636
                dbgasyncsendbuf[0] = 1;
29 theseven 637
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16 + dbgactionlength);
15 theseven 638
                break;
85 theseven 639
#endif
15 theseven 640
            case DBGACTION_POWEROFF:
127 theseven 641
                if (dbgactiontype) shutdown(true);
54 theseven 642
                power_off();
15 theseven 643
                break;
29 theseven 644
            case DBGACTION_RESET:
127 theseven 645
                shutdown(false);
29 theseven 646
                reset();
647
                break;
648
            case DBGACTION_CWRITE:
649
                cwrite(dbgactionconsoles, (const char*)dbgasyncsendbuf, dbgactionlength);
650
                dbgasyncsendbuf[0] = 1;
651
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
652
                break;
653
            case DBGACTION_CREAD:
654
                dbgasyncsendbuf[0] = 1;
30 theseven 655
                dbgasyncsendbuf[1] = cread(dbgactionconsoles, (char*)&dbgasyncsendbuf[4],
656
                                           dbgactionlength, 0);
226 theseven 657
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16 + dbgactionlength);
29 theseven 658
                break;
659
            case DBGACTION_CFLUSH:
660
                cflush(dbgactionconsoles);
661
                dbgasyncsendbuf[0] = 1;
662
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
663
                break;
92 theseven 664
            case DBGACTION_EXECIMAGE:
665
                dbgasyncsendbuf[0] = 1;
666
                dbgasyncsendbuf[1] = execimage((void*)dbgactionaddr);
667
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
668
                break;
127 theseven 669
            case DBGACTION_EXECFIRMWARE:
670
                shutdown(false);
671
                dbgasyncsendbuf[0] = 1;
672
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
673
                execfirmware((void*)dbgactionaddr);
95 theseven 674
#ifdef HAVE_BOOTFLASH
675
            case DBGACTION_READBOOTFLASH:
676
                bootflash_readraw((void*)dbgactionaddr, dbgactionoffset, dbgactionlength);
677
                dbgasyncsendbuf[0] = 1;
678
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
679
                break;
680
            case DBGACTION_WRITEBOOTFLASH:
681
                bootflash_writeraw((void*)dbgactionaddr, dbgactionoffset, dbgactionlength);
682
                dbgasyncsendbuf[0] = 1;
683
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
684
                break;
685
#endif
157 theseven 686
#ifdef HAVE_HWKEYAES
687
            case DBGACTION_HWKEYAES:
688
                hwkeyaes((enum hwkeyaes_direction) dbgactiontype, dbgactionoffset,
689
                         (void*)dbgactionaddr, dbgactionlength);
690
                dbgasyncsendbuf[0] = 1;
691
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
692
                break;
693
#endif
694
#ifdef HAVE_HMACSHA1
695
            case DBGACTION_HMACSHA1:
696
                hmacsha1((void*)dbgactionaddr, dbgactionlength, (void*)dbgactionoffset);
697
                dbgasyncsendbuf[0] = 1;
698
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
699
                break;
700
#endif
226 theseven 701
#ifdef USB_HAVE_TARGET_SPECIFIC_REQUESTS
702
            case DBGACTION_TARGETSPECIFIC:
703
            {
704
                int size = usb_target_handle_request(dbgasyncsendbuf, sizeof(dbgasyncsendbuf));
705
                if (size) usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, size);
706
                break;
15 theseven 707
            }
226 theseven 708
#endif
709
            }
15 theseven 710
            dbgaction = DBGACTION_IDLE;
711
        }
712
    }
713
}
714
 
715
void usb_init(void)
716
{
717
    dbgaction = DBGACTION_IDLE;
718
    wakeup_init(&dbgwakeup);
25 theseven 719
    dbgconsendreadidx = 0;
720
    dbgconsendwriteidx = 0;
721
    dbgconrecvreadidx = 0;
722
    dbgconrecvwriteidx = 0;
723
    wakeup_init(&dbgconsendwakeup);
724
    wakeup_init(&dbgconrecvwakeup);
85 theseven 725
    dbgconsoleattached = false;
167 theseven 726
    thread_create("monitor worker", dbgthread, dbgstack, sizeof(dbgstack), CORE_THREAD, 255, true);
15 theseven 727
    usb_drv_init();
728
}
25 theseven 729
 
730
int dbgconsole_getfree() ICODE_ATTR;
731
int dbgconsole_getfree()
732
{
733
    int free = dbgconsendreadidx - dbgconsendwriteidx - 1;
734
    if (free < 0) free += sizeof(dbgconsendbuf);
735
    return free;
736
}
737
 
738
int dbgconsole_makespace(int length) ICODE_ATTR;
739
int dbgconsole_makespace(int length)
740
{
741
    int free = dbgconsole_getfree();
742
    while (!free && dbgconsoleattached)
743
    {
744
        if (wakeup_wait(&dbgconsendwakeup, 2000000) == THREAD_TIMEOUT)
745
            dbgconsoleattached = false;
746
        free = dbgconsole_getfree();
747
    }
748
    if (free) return free > length ? length : free;
749
    if (length > sizeof(dbgconsendbuf) - 17) length = sizeof(dbgconsendbuf) - 17;
750
    uint32_t mode = enter_critical_section();
751
    dbgconsendreadidx += length;
752
    if (dbgconsendreadidx >= sizeof(dbgconsendbuf))
753
        dbgconsendreadidx -= sizeof(dbgconsendbuf);
754
    int offset = 0;
755
    int idx = dbgconsendreadidx;
756
    if (idx + 16 >= sizeof(dbgconsendbuf))
757
    {
758
        offset = sizeof(dbgconsendbuf) - dbgconsendreadidx;
759
        memcpy(&dbgconsendbuf[dbgconsendreadidx], dbgconoverflowstr, offset);
760
        idx = 0;
761
    }
762
    if (offset != 16) memcpy(&dbgconsendbuf[idx], &dbgconoverflowstr[offset], 16 - offset);
763
    leave_critical_section(mode);
764
    return length;
765
}
766
 
767
void dbgconsole_putc(char string)
768
{
769
    dbgconsole_makespace(1);
770
    dbgconsendbuf[dbgconsendwriteidx++] = string;
771
    if (dbgconsendwriteidx >= sizeof(dbgconsendbuf))
772
        dbgconsendwriteidx -= sizeof(dbgconsendbuf);
773
}
774
 
775
void dbgconsole_write(const char* string, size_t length)
776
{
777
    while (length)
778
    {
779
        int space = dbgconsole_makespace(length);
780
        if (dbgconsendwriteidx + space >= sizeof(dbgconsendbuf))
781
        {
782
            int bytes = sizeof(dbgconsendbuf) - dbgconsendwriteidx;
783
            memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, bytes);
784
            dbgconsendwriteidx = 0;
785
            string = &string[bytes];
786
            space -= bytes;
787
            length -= bytes;
788
        }
789
        if (space) memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, space);
790
        dbgconsendwriteidx += space;
791
        string = &string[space];
792
        length -= space;
793
    }
794
}
795
 
796
void dbgconsole_puts(const char* string)
797
{
798
    dbgconsole_write(string, strlen(string));
799
}
26 theseven 800
 
801
int dbgconsole_getavailable() ICODE_ATTR;
802
int dbgconsole_getavailable()
803
{
804
    int available = dbgconrecvwriteidx - dbgconrecvreadidx;
805
    if (available < 0) available += sizeof(dbgconrecvbuf);
806
    return available;
807
}
808
 
809
int dbgconsole_getc(int timeout)
810
{
811
    if (!dbgconsole_getavailable())
812
    {
813
        wakeup_wait(&dbgconrecvwakeup, TIMEOUT_NONE);
814
        if (!dbgconsole_getavailable())
815
        {
816
            wakeup_wait(&dbgconrecvwakeup, timeout);
817
            if (!dbgconsole_getavailable()) return -1;
818
        }
819
    }
820
    int byte = dbgconrecvbuf[dbgconrecvreadidx++];
821
    if (dbgconrecvreadidx >= sizeof(dbgconrecvbuf))
822
        dbgconrecvreadidx -= sizeof(dbgconrecvbuf);
823
    return byte;
824
}
825
 
826
int dbgconsole_read(char* buffer, size_t length, int timeout)
827
{
828
    if (!length) return 0;
829
    int available = dbgconsole_getavailable();
830
    if (!available)
831
    {
832
        wakeup_wait(&dbgconrecvwakeup, TIMEOUT_NONE);
833
        int available = dbgconsole_getavailable();
834
        if (!available)
835
        {
836
            wakeup_wait(&dbgconrecvwakeup, timeout);
837
            int available = dbgconsole_getavailable();
838
            if (!available) return 0;
839
        }
840
    }
841
    if (available > length) available = length;
842
    int left = available;
843
    if (dbgconrecvreadidx + available >= sizeof(dbgconrecvbuf))
844
    {
845
        int bytes = sizeof(dbgconrecvbuf) - dbgconrecvreadidx;
846
        memcpy(buffer, &dbgconrecvbuf[dbgconrecvreadidx], bytes);
847
        dbgconrecvreadidx = 0;
848
        buffer = &buffer[bytes];
849
        left -= bytes;
850
    }
851
    if (left) memcpy(buffer, &dbgconrecvbuf[dbgconrecvreadidx], left);
852
    dbgconrecvreadidx += left;
853
    return available;
854
}