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;
234 theseven 279
                size = lang_descriptor.bLength;
15 theseven 280
                break;
281
            case 1:
282
                string_devicename.bLength = 14;
283
                addr = &string_devicename;
234 theseven 284
                size = string_devicename.bLength;
285
                break;
15 theseven 286
            case 2:
235 theseven 287
                string_devicename.bLength = 32;
15 theseven 288
                addr = &string_devicename;
234 theseven 289
                size = string_devicename.bLength;
15 theseven 290
                break;
291
            }
292
            break;
293
        }
294
        break;
295
    case USB_REQ_GET_CONFIGURATION:
296
        ctrlresp[0] = 1;
297
        addr = ctrlresp;
298
        size = 1;
299
        break;
300
    case USB_REQ_SET_CONFIGURATION:
301
        usb_drv_cancel_all_transfers();
302
        usb_setup_dbg_listener();
303
        size = 0;
304
        break;
305
    }
306
    if (!size) usb_drv_send_nonblocking(0, NULL, 0);
307
    else if (size == -1)
308
    {
309
        usb_drv_stall(0, true, true);
310
        usb_drv_stall(0, true, false);
311
    }
312
    else
313
    {
314
        usb_drv_recv(0, NULL, 0);
315
        usb_drv_send_nonblocking(0, addr, size > req->wLength ? req->wLength : size);
316
    }
317
}
318
 
29 theseven 319
bool set_dbgaction(enum dbgaction_t action, int addsize)
15 theseven 320
{
321
    if (dbgaction != DBGACTION_IDLE)
322
    {
323
        dbgsendbuf[0] = 3;
29 theseven 324
        usb_drv_send_nonblocking(dbgendpoints[1], dbgsendbuf, 16 + addsize);
15 theseven 325
        return true;
326
    }
327
    dbgaction = action;
328
    wakeup_signal(&dbgwakeup);
329
    return false;
330
}
331
 
332
void reset() __attribute__((noreturn));
333
 
334
void usb_handle_transfer_complete(int endpoint, int dir, int status, int length)
335
{
336
    void* addr = dbgsendbuf;
337
    int size = 0;
338
    if (endpoint == dbgendpoints[0])
339
    {
226 theseven 340
#ifdef USB_HAVE_TARGET_SPECIFIC_REQUESTS
341
        if (dbgrecvbuf[0] >= 0xffff0000)
342
        {
343
            if (!set_dbgaction(DBGACTION_TARGETSPECIFIC, 0))
344
                memcpy(dbgasyncsendbuf, dbgrecvbuf, sizeof(dbgasyncsendbuf));
345
            usb_setup_dbg_listener();
346
            return;
347
        }
348
#endif
15 theseven 349
        switch (dbgrecvbuf[0])
350
        {
28 theseven 351
        case 1:  // GET INFO
15 theseven 352
            dbgsendbuf[0] = 1;
353
            size = 16;
28 theseven 354
            switch (dbgrecvbuf[1])
355
            {
356
            case 0:  // GET VERSION INFO
85 theseven 357
                dbgsendbuf[1] = VERSION_SVN_INT;
358
                dbgsendbuf[2] = VERSION_MAJOR | (VERSION_MINOR << 8)
28 theseven 359
                              | (VERSION_PATCH << 16) | (1 << 24);
85 theseven 360
                dbgsendbuf[3] = PLATFORM_ID;
28 theseven 361
                break;
362
            case 1:  // GET PACKET SIZE INFO
363
                dbgsendbuf[1] = 0x02000200;
364
                dbgsendbuf[2] = usb_drv_get_max_out_size();
365
                dbgsendbuf[3] = usb_drv_get_max_in_size();
366
                break;
35 theseven 367
            case 2:  // GET USER MEMORY INFO
368
                dbgsendbuf[1] = (uint32_t)&_initstart;
369
                dbgsendbuf[2] = (uint32_t)&_sdramstart;
370
                break;
28 theseven 371
            default:
372
                dbgsendbuf[0] = 2;
373
            }
15 theseven 374
            break;
375
        case 2:  // RESET
29 theseven 376
            if (dbgrecvbuf[1])
377
            {
378
                if (set_dbgaction(DBGACTION_RESET, 0)) break;
379
                dbgsendbuf[0] = 1;
380
                size = 16;
381
            }
382
            else reset();
15 theseven 383
            break;
384
        case 3:  // POWER OFF
29 theseven 385
            if (set_dbgaction(DBGACTION_POWEROFF, 0)) break;
386
            dbgactiontype = dbgrecvbuf[1];
387
            dbgsendbuf[0] = 1;
388
            size = 16;
15 theseven 389
            break;
390
        case 4:  // READ MEMORY
391
            dbgsendbuf[0] = 1;
392
            memcpy(&dbgsendbuf[4], (const void*)dbgrecvbuf[1], dbgrecvbuf[2]);
393
            size = dbgrecvbuf[2] + 16;
394
            break;
395
        case 5:  // WRITE MEMORY
396
            dbgsendbuf[0] = 1;
397
            memcpy((void*)dbgrecvbuf[1], &dbgrecvbuf[4], dbgrecvbuf[2]);
398
            size = 16;
399
            break;
400
        case 6:  // READ DMA
401
            dbgsendbuf[0] = 1;
402
            usb_drv_send_nonblocking(dbgendpoints[1], dbgsendbuf, 16);
403
            usb_drv_send_nonblocking(dbgendpoints[3], (const void*)dbgrecvbuf[1], dbgrecvbuf[2]);
404
            break;
405
        case 7:  // WRITE DMA
406
            dbgsendbuf[0] = 1;
407
            size = 16;
408
            usb_drv_recv(dbgendpoints[2], (void*)dbgrecvbuf[1], dbgrecvbuf[2]);
409
            break;
85 theseven 410
#ifdef HAVE_I2C
15 theseven 411
        case 8:  // READ I2C
29 theseven 412
            if (set_dbgaction(DBGACTION_I2CRECV, dbgrecvbuf[1] >> 24)) break;
15 theseven 413
            dbgi2cbus = dbgrecvbuf[1] & 0xff;
414
            dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
29 theseven 415
            dbgactionaddr = (dbgrecvbuf[1] >> 16) & 0xff;
416
            dbgactionlength = dbgrecvbuf[1] >> 24;
175 theseven 417
            if (!dbgactionlength) dbgactionlength = 256;
15 theseven 418
            break;
419
        case 9:  // WRITE I2C
29 theseven 420
            if (set_dbgaction(DBGACTION_I2CSEND, 0)) break;
15 theseven 421
            dbgi2cbus = dbgrecvbuf[1] & 0xff;
422
            dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
29 theseven 423
            dbgactionaddr = (dbgrecvbuf[1] >> 16) & 0xff;
424
            dbgactionlength = dbgrecvbuf[1] >> 24;
175 theseven 425
            if (!dbgactionlength) dbgactionlength = 256;
212 theseven 426
            memcpy(dbgasyncsendbuf, &dbgrecvbuf[4], dbgactionlength);
15 theseven 427
            break;
85 theseven 428
#endif
25 theseven 429
        case 10:  // READ CONSOLE
430
            dbgconsoleattached = true;
431
            int bytes = dbgconsendwriteidx - dbgconsendreadidx;
432
            if (bytes >= sizeof(dbgconsendbuf)) bytes -= sizeof(dbgconsendbuf);
433
            if (bytes)
434
            {
435
                if (bytes < 0) bytes += sizeof(dbgconsendbuf);
436
                if (bytes > dbgrecvbuf[1]) bytes = dbgrecvbuf[1];
437
                int readbytes = bytes;
438
                char* outptr = (char*)&dbgsendbuf[4];
439
                if (dbgconsendreadidx + bytes >= sizeof(dbgconsendbuf))
440
                {
441
                    readbytes = sizeof(dbgconsendbuf) - dbgconsendreadidx;
442
                    memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
443
                    dbgconsendreadidx = 0;
444
                    outptr = &outptr[readbytes];
445
                    readbytes = bytes - readbytes;
446
                }
447
                if (readbytes) memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
448
                dbgconsendreadidx += readbytes;
26 theseven 449
                wakeup_signal(&dbgconsendwakeup);
25 theseven 450
            }
451
            dbgsendbuf[0] = 1;
452
            dbgsendbuf[1] = bytes;
453
            dbgsendbuf[2] = sizeof(dbgconsendbuf);
454
            dbgsendbuf[3] = dbgconsendwriteidx - dbgconsendreadidx;
455
            size = 16 + dbgrecvbuf[1];
456
            break;
26 theseven 457
        case 11:  // WRITE CONSOLE
458
            bytes = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
459
            if (bytes < 0) bytes += sizeof(dbgconrecvbuf);
460
            if (bytes)
461
            {
462
                if (bytes > dbgrecvbuf[1]) bytes = dbgrecvbuf[1];
463
                int writebytes = bytes;
464
                char* readptr = (char*)&dbgrecvbuf[4];
465
                if (dbgconrecvwriteidx + bytes >= sizeof(dbgconrecvbuf))
466
                {
467
                    writebytes = sizeof(dbgconrecvbuf) - dbgconrecvwriteidx;
468
                    memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
469
                    dbgconrecvwriteidx = 0;
470
                    readptr = &readptr[writebytes];
471
                    writebytes = bytes - writebytes;
472
                }
473
                if (writebytes) memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
474
                dbgconrecvwriteidx += writebytes;
475
                wakeup_signal(&dbgconrecvwakeup);
476
            }
477
            dbgsendbuf[0] = 1;
478
            dbgsendbuf[1] = bytes;
479
            dbgsendbuf[2] = sizeof(dbgconrecvbuf);
480
            dbgsendbuf[3] = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
481
            size = 16;
482
            break;
29 theseven 483
        case 12:  // CWRITE
484
            if (set_dbgaction(DBGACTION_CWRITE, 0)) break;
485
            dbgactionconsoles = dbgrecvbuf[1];
486
            dbgactionlength = dbgrecvbuf[2];
487
            memcpy(dbgasyncsendbuf, &dbgrecvbuf[4], dbgactionlength);
488
            break;
489
        case 13:  // CREAD
490
            if (set_dbgaction(DBGACTION_CREAD, dbgrecvbuf[2])) break;
491
            dbgactionconsoles = dbgrecvbuf[1];
492
            dbgactionlength = dbgrecvbuf[2];
493
            break;
494
        case 14:  // CFLUSH
495
            if (set_dbgaction(DBGACTION_CFLUSH, 0)) break;
496
            dbgactionconsoles = dbgrecvbuf[1];
497
            break;
31 theseven 498
        case 15:  // GET PROCESS INFO
499
            dbgsendbuf[0] = 1;
500
            dbgsendbuf[1] = SCHEDULER_THREAD_INFO_VERSION;
501
            dbgsendbuf[2] = MAX_THREADS * sizeof(struct scheduler_thread);
136 theseven 502
            memcpy(&dbgsendbuf[4], (void*)(((uint32_t)&scheduler_threads) + dbgrecvbuf[1]),
57 theseven 503
                   dbgrecvbuf[2]);
504
            size = dbgrecvbuf[2] + 16;
31 theseven 505
            break;
34 theseven 506
        case 16:  // FREEZE SCHEDULER
54 theseven 507
            dbgsendbuf[1] = scheduler_freeze(dbgrecvbuf[1]);
34 theseven 508
            dbgsendbuf[0] = 1;
509
            size = 16;
510
            break;
35 theseven 511
        case 17:  // SUSPEND THREAD
54 theseven 512
            if (dbgrecvbuf[1])
513
            {
514
                if (thread_suspend(dbgrecvbuf[2]) == -4) dbgsendbuf[1] = 1;
515
                else dbgsendbuf[1] = 0;
516
            }
517
            else
518
            {
519
                if (thread_resume(dbgrecvbuf[2]) == -5) dbgsendbuf[1] = 0;
520
                else dbgsendbuf[1] = 1;
521
            }
35 theseven 522
            dbgsendbuf[0] = 1;
523
            size = 16;
524
            break;
525
        case 18:  // KILL THREAD
526
            thread_terminate(dbgrecvbuf[1]);
527
            dbgsendbuf[0] = 1;
528
            size = 16;
529
            break;
57 theseven 530
        case 19:  // KILL THREAD
35 theseven 531
            dbgsendbuf[0] = 1;
532
            dbgsendbuf[1] = thread_create((const char*)dbgsendbuf[1], (const void*)dbgsendbuf[2],
533
                                          (char*)dbgsendbuf[3], dbgsendbuf[4], dbgsendbuf[5],
534
                                          dbgsendbuf[6], dbgsendbuf[7]);
535
            size = 16;
536
            break;
537
        case 20:  // FLUSH CACHE
538
            clean_dcache();
539
            invalidate_icache();
540
            dbgsendbuf[0] = 1;
541
            size = 16;
542
            break;
92 theseven 543
        case 21:  // EXECIMAGE
544
            if (set_dbgaction(DBGACTION_EXECIMAGE, 0)) break;
545
            dbgactionaddr = dbgrecvbuf[1];
546
            break;
95 theseven 547
#ifdef HAVE_BOOTFLASH
548
        case 22:  // READ BOOT FLASH
549
            if (set_dbgaction(DBGACTION_READBOOTFLASH, 0)) break;
550
            dbgactionaddr = dbgrecvbuf[1];
551
            dbgactionoffset = dbgrecvbuf[2];
552
            dbgactionlength = dbgrecvbuf[3];
553
            break;
554
        case 23:  // WRITE BOOT FLASH
555
            if (set_dbgaction(DBGACTION_WRITEBOOTFLASH, 0)) break;
556
            dbgactionaddr = dbgrecvbuf[1];
557
            dbgactionoffset = dbgrecvbuf[2];
558
            dbgactionlength = dbgrecvbuf[3];
559
            break;
560
#endif
127 theseven 561
        case 24:  // EXECFIRMWARE
562
            if (set_dbgaction(DBGACTION_EXECFIRMWARE, 0)) break;
563
            dbgactionaddr = dbgrecvbuf[1];
564
            break;
157 theseven 565
#ifdef HAVE_HWKEYAES
566
        case 25:  // HWKEYAES
567
            if (set_dbgaction(DBGACTION_HWKEYAES, 0)) break;
568
            dbgactiontype = ((uint8_t*)dbgrecvbuf)[4];
569
            dbgactionoffset = ((uint16_t*)dbgrecvbuf)[3];
570
            dbgactionaddr = dbgrecvbuf[2];
571
            dbgactionlength = dbgrecvbuf[3];
179 theseven 572
            break;
157 theseven 573
#endif
574
#ifdef HAVE_HMACSHA1
575
        case 26:  // HMACSHA1
576
            if (set_dbgaction(DBGACTION_HMACSHA1, 0)) break;
577
            dbgactionaddr = dbgrecvbuf[1];
578
            dbgactionlength = dbgrecvbuf[2];
579
            dbgactionoffset = dbgrecvbuf[3];
179 theseven 580
            break;
157 theseven 581
#endif
15 theseven 582
        default:
583
            dbgsendbuf[0] = 2;
584
            size = 16;
585
        }
586
        usb_setup_dbg_listener();
587
        if (size) usb_drv_send_nonblocking(dbgendpoints[1], addr, size);
588
    }
589
}
590
 
591
void usb_handle_bus_reset(void)
592
{
593
    dbgendpoints[0] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
594
    dbgendpoints[1] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
595
    dbgendpoints[2] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
596
    dbgendpoints[3] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
597
    config_bundle.endpoint1_descriptor.bEndpointAddress = dbgendpoints[0];
598
    config_bundle.endpoint2_descriptor.bEndpointAddress = dbgendpoints[1];
599
    config_bundle.endpoint3_descriptor.bEndpointAddress = dbgendpoints[2];
600
    config_bundle.endpoint4_descriptor.bEndpointAddress = dbgendpoints[3];
601
    usb_setup_dbg_listener();
602
}
603
 
604
void dbgthread(void)
605
{
606
    int i;
25 theseven 607
    int t;
15 theseven 608
    while (1)
609
    {
610
        wakeup_wait(&dbgwakeup, TIMEOUT_BLOCK);
611
        for (i = 0; i < MAX_THREADS; i++)
612
            if (scheduler_threads[i].state == THREAD_DEFUNCT)
613
            {
614
                if (scheduler_threads[i].block_type == THREAD_DEFUNCT_STKOV)
35 theseven 615
                {
616
                    if (scheduler_threads[i].name)
617
                        cprintf(1, "\n*PANIC*\nStack overflow! (%s)\n",
618
                                scheduler_threads[i].name);
619
                    else cprintf(1, "\n*PANIC*\nStack overflow! (ID %d)\n", i);
620
                }
15 theseven 621
                scheduler_threads[i].state = THREAD_DEFUNCT_ACK;
622
            }
623
        if (dbgaction != DBGACTION_IDLE)
624
        {
625
            switch (dbgaction)
626
            {
85 theseven 627
#ifdef HAVE_I2C
15 theseven 628
            case DBGACTION_I2CSEND:
29 theseven 629
                i2c_send(dbgi2cbus, dbgi2cslave, dbgactionaddr,
630
                         (uint8_t*)dbgasyncsendbuf, dbgactionlength);
15 theseven 631
                dbgasyncsendbuf[0] = 1;
632
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
633
                break;
634
            case DBGACTION_I2CRECV:
29 theseven 635
                i2c_recv(dbgi2cbus, dbgi2cslave, dbgactionaddr,
636
                         (uint8_t*)(&dbgasyncsendbuf[4]), dbgactionlength);
15 theseven 637
                dbgasyncsendbuf[0] = 1;
29 theseven 638
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16 + dbgactionlength);
15 theseven 639
                break;
85 theseven 640
#endif
15 theseven 641
            case DBGACTION_POWEROFF:
127 theseven 642
                if (dbgactiontype) shutdown(true);
54 theseven 643
                power_off();
15 theseven 644
                break;
29 theseven 645
            case DBGACTION_RESET:
127 theseven 646
                shutdown(false);
29 theseven 647
                reset();
648
                break;
649
            case DBGACTION_CWRITE:
650
                cwrite(dbgactionconsoles, (const char*)dbgasyncsendbuf, dbgactionlength);
651
                dbgasyncsendbuf[0] = 1;
652
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
653
                break;
654
            case DBGACTION_CREAD:
655
                dbgasyncsendbuf[0] = 1;
30 theseven 656
                dbgasyncsendbuf[1] = cread(dbgactionconsoles, (char*)&dbgasyncsendbuf[4],
657
                                           dbgactionlength, 0);
226 theseven 658
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16 + dbgactionlength);
29 theseven 659
                break;
660
            case DBGACTION_CFLUSH:
661
                cflush(dbgactionconsoles);
662
                dbgasyncsendbuf[0] = 1;
663
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
664
                break;
92 theseven 665
            case DBGACTION_EXECIMAGE:
666
                dbgasyncsendbuf[0] = 1;
667
                dbgasyncsendbuf[1] = execimage((void*)dbgactionaddr);
668
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
669
                break;
127 theseven 670
            case DBGACTION_EXECFIRMWARE:
671
                shutdown(false);
672
                dbgasyncsendbuf[0] = 1;
673
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
674
                execfirmware((void*)dbgactionaddr);
95 theseven 675
#ifdef HAVE_BOOTFLASH
676
            case DBGACTION_READBOOTFLASH:
677
                bootflash_readraw((void*)dbgactionaddr, dbgactionoffset, dbgactionlength);
678
                dbgasyncsendbuf[0] = 1;
679
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
680
                break;
681
            case DBGACTION_WRITEBOOTFLASH:
682
                bootflash_writeraw((void*)dbgactionaddr, dbgactionoffset, dbgactionlength);
683
                dbgasyncsendbuf[0] = 1;
684
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
685
                break;
686
#endif
157 theseven 687
#ifdef HAVE_HWKEYAES
688
            case DBGACTION_HWKEYAES:
689
                hwkeyaes((enum hwkeyaes_direction) dbgactiontype, dbgactionoffset,
690
                         (void*)dbgactionaddr, dbgactionlength);
691
                dbgasyncsendbuf[0] = 1;
692
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
693
                break;
694
#endif
695
#ifdef HAVE_HMACSHA1
696
            case DBGACTION_HMACSHA1:
697
                hmacsha1((void*)dbgactionaddr, dbgactionlength, (void*)dbgactionoffset);
698
                dbgasyncsendbuf[0] = 1;
699
                usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
700
                break;
701
#endif
226 theseven 702
#ifdef USB_HAVE_TARGET_SPECIFIC_REQUESTS
703
            case DBGACTION_TARGETSPECIFIC:
704
            {
705
                int size = usb_target_handle_request(dbgasyncsendbuf, sizeof(dbgasyncsendbuf));
706
                if (size) usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, size);
707
                break;
15 theseven 708
            }
226 theseven 709
#endif
710
            }
15 theseven 711
            dbgaction = DBGACTION_IDLE;
712
        }
713
    }
714
}
715
 
716
void usb_init(void)
717
{
718
    dbgaction = DBGACTION_IDLE;
719
    wakeup_init(&dbgwakeup);
25 theseven 720
    dbgconsendreadidx = 0;
721
    dbgconsendwriteidx = 0;
722
    dbgconrecvreadidx = 0;
723
    dbgconrecvwriteidx = 0;
724
    wakeup_init(&dbgconsendwakeup);
725
    wakeup_init(&dbgconrecvwakeup);
85 theseven 726
    dbgconsoleattached = false;
167 theseven 727
    thread_create("monitor worker", dbgthread, dbgstack, sizeof(dbgstack), CORE_THREAD, 255, true);
15 theseven 728
    usb_drv_init();
729
}
25 theseven 730
 
731
int dbgconsole_getfree() ICODE_ATTR;
732
int dbgconsole_getfree()
733
{
734
    int free = dbgconsendreadidx - dbgconsendwriteidx - 1;
735
    if (free < 0) free += sizeof(dbgconsendbuf);
736
    return free;
737
}
738
 
739
int dbgconsole_makespace(int length) ICODE_ATTR;
740
int dbgconsole_makespace(int length)
741
{
742
    int free = dbgconsole_getfree();
743
    while (!free && dbgconsoleattached)
744
    {
745
        if (wakeup_wait(&dbgconsendwakeup, 2000000) == THREAD_TIMEOUT)
746
            dbgconsoleattached = false;
747
        free = dbgconsole_getfree();
748
    }
749
    if (free) return free > length ? length : free;
750
    if (length > sizeof(dbgconsendbuf) - 17) length = sizeof(dbgconsendbuf) - 17;
751
    uint32_t mode = enter_critical_section();
752
    dbgconsendreadidx += length;
753
    if (dbgconsendreadidx >= sizeof(dbgconsendbuf))
754
        dbgconsendreadidx -= sizeof(dbgconsendbuf);
755
    int offset = 0;
756
    int idx = dbgconsendreadidx;
757
    if (idx + 16 >= sizeof(dbgconsendbuf))
758
    {
759
        offset = sizeof(dbgconsendbuf) - dbgconsendreadidx;
760
        memcpy(&dbgconsendbuf[dbgconsendreadidx], dbgconoverflowstr, offset);
761
        idx = 0;
762
    }
763
    if (offset != 16) memcpy(&dbgconsendbuf[idx], &dbgconoverflowstr[offset], 16 - offset);
764
    leave_critical_section(mode);
765
    return length;
766
}
767
 
768
void dbgconsole_putc(char string)
769
{
770
    dbgconsole_makespace(1);
771
    dbgconsendbuf[dbgconsendwriteidx++] = string;
772
    if (dbgconsendwriteidx >= sizeof(dbgconsendbuf))
773
        dbgconsendwriteidx -= sizeof(dbgconsendbuf);
774
}
775
 
776
void dbgconsole_write(const char* string, size_t length)
777
{
778
    while (length)
779
    {
780
        int space = dbgconsole_makespace(length);
781
        if (dbgconsendwriteidx + space >= sizeof(dbgconsendbuf))
782
        {
783
            int bytes = sizeof(dbgconsendbuf) - dbgconsendwriteidx;
784
            memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, bytes);
785
            dbgconsendwriteidx = 0;
786
            string = &string[bytes];
787
            space -= bytes;
788
            length -= bytes;
789
        }
790
        if (space) memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, space);
791
        dbgconsendwriteidx += space;
792
        string = &string[space];
793
        length -= space;
794
    }
795
}
796
 
797
void dbgconsole_puts(const char* string)
798
{
799
    dbgconsole_write(string, strlen(string));
800
}
26 theseven 801
 
802
int dbgconsole_getavailable() ICODE_ATTR;
803
int dbgconsole_getavailable()
804
{
805
    int available = dbgconrecvwriteidx - dbgconrecvreadidx;
806
    if (available < 0) available += sizeof(dbgconrecvbuf);
807
    return available;
808
}
809
 
810
int dbgconsole_getc(int timeout)
811
{
812
    if (!dbgconsole_getavailable())
813
    {
814
        wakeup_wait(&dbgconrecvwakeup, TIMEOUT_NONE);
815
        if (!dbgconsole_getavailable())
816
        {
817
            wakeup_wait(&dbgconrecvwakeup, timeout);
818
            if (!dbgconsole_getavailable()) return -1;
819
        }
820
    }
821
    int byte = dbgconrecvbuf[dbgconrecvreadidx++];
822
    if (dbgconrecvreadidx >= sizeof(dbgconrecvbuf))
823
        dbgconrecvreadidx -= sizeof(dbgconrecvbuf);
824
    return byte;
825
}
826
 
827
int dbgconsole_read(char* buffer, size_t length, int timeout)
828
{
829
    if (!length) return 0;
830
    int available = dbgconsole_getavailable();
831
    if (!available)
832
    {
833
        wakeup_wait(&dbgconrecvwakeup, TIMEOUT_NONE);
834
        int available = dbgconsole_getavailable();
835
        if (!available)
836
        {
837
            wakeup_wait(&dbgconrecvwakeup, timeout);
838
            int available = dbgconsole_getavailable();
839
            if (!available) return 0;
840
        }
841
    }
842
    if (available > length) available = length;
843
    int left = available;
844
    if (dbgconrecvreadidx + available >= sizeof(dbgconrecvbuf))
845
    {
846
        int bytes = sizeof(dbgconrecvbuf) - dbgconrecvreadidx;
847
        memcpy(buffer, &dbgconrecvbuf[dbgconrecvreadidx], bytes);
848
        dbgconrecvreadidx = 0;
849
        buffer = &buffer[bytes];
850
        left -= bytes;
851
    }
852
    if (left) memcpy(buffer, &dbgconrecvbuf[dbgconrecvreadidx], left);
853
    dbgconrecvreadidx += left;
854
    return available;
855
}
240 theseven 856
 
857
void usb_exit(void)
858
{
859
    usb_drv_exit();
860
}