| 15 |
theseven |
1 |
//
|
|
|
2 |
//
|
|
|
3 |
// Copyright 2010 TheSeven
|
|
|
4 |
//
|
|
|
5 |
//
|
| 427 |
farthen |
6 |
// This file is part of emCORE.
|
| 15 |
theseven |
7 |
//
|
| 427 |
farthen |
8 |
// emCORE is free software: you can redistribute it and/or
|
| 15 |
theseven |
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 |
//
|
| 427 |
farthen |
13 |
// emCORE is distributed in the hope that it will be useful,
|
| 15 |
theseven |
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
|
| 427 |
farthen |
19 |
// with emCORE. If not, see <http://www.gnu.org/licenses/>.
|
| 15 |
theseven |
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"
|
| 685 |
theseven |
37 |
#include "dbgconsole.h"
|
| 85 |
theseven |
38 |
#ifdef HAVE_I2C
|
|
|
39 |
#include "i2c.h"
|
|
|
40 |
#endif
|
| 95 |
theseven |
41 |
#ifdef HAVE_BOOTFLASH
|
|
|
42 |
#include "bootflash.h"
|
|
|
43 |
#endif
|
| 157 |
theseven |
44 |
#ifdef HAVE_HWKEYAES
|
|
|
45 |
#include "hwkeyaes.h"
|
|
|
46 |
#endif
|
|
|
47 |
#ifdef HAVE_HMACSHA1
|
|
|
48 |
#include "hmacsha1.h"
|
|
|
49 |
#endif
|
| 226 |
theseven |
50 |
#ifdef USB_HAVE_TARGET_SPECIFIC_REQUESTS
|
|
|
51 |
#include "usbtarget.h"
|
|
|
52 |
#endif
|
| 346 |
theseven |
53 |
#ifdef HAVE_STORAGE
|
|
|
54 |
#include "storage.h"
|
|
|
55 |
#include "disk.h"
|
|
|
56 |
#include "file.h"
|
|
|
57 |
#include "dir.h"
|
| 347 |
theseven |
58 |
#include "libc/include/errno.h"
|
| 346 |
theseven |
59 |
#endif
|
| 15 |
theseven |
60 |
|
|
|
61 |
|
|
|
62 |
static uint8_t ctrlresp[2] CACHEALIGN_ATTR;
|
|
|
63 |
static uint32_t dbgrecvbuf[0x80] CACHEALIGN_ATTR;
|
|
|
64 |
static uint32_t dbgsendbuf[0x80] CACHEALIGN_ATTR;
|
|
|
65 |
static uint32_t dbgasyncsendbuf[0x80] CACHEALIGN_ATTR;
|
|
|
66 |
static char dbgendpoints[4] IBSS_ATTR;
|
|
|
67 |
|
|
|
68 |
enum dbgaction_t
|
|
|
69 |
{
|
|
|
70 |
DBGACTION_IDLE = 0,
|
|
|
71 |
DBGACTION_I2CSEND,
|
|
|
72 |
DBGACTION_I2CRECV,
|
| 29 |
theseven |
73 |
DBGACTION_RESET,
|
|
|
74 |
DBGACTION_POWEROFF,
|
|
|
75 |
DBGACTION_CWRITE,
|
|
|
76 |
DBGACTION_CREAD,
|
| 92 |
theseven |
77 |
DBGACTION_CFLUSH,
|
| 95 |
theseven |
78 |
DBGACTION_EXECIMAGE,
|
| 127 |
theseven |
79 |
DBGACTION_EXECFIRMWARE,
|
| 95 |
theseven |
80 |
DBGACTION_READBOOTFLASH,
|
| 157 |
theseven |
81 |
DBGACTION_WRITEBOOTFLASH,
|
|
|
82 |
DBGACTION_HWKEYAES,
|
| 226 |
theseven |
83 |
DBGACTION_HMACSHA1,
|
| 346 |
theseven |
84 |
DBGACTION_TARGETSPECIFIC,
|
| 438 |
theseven |
85 |
DBGACTION_STORAGE,
|
|
|
86 |
DBGACTION_MALLOC,
|
|
|
87 |
DBGACTION_MEMALIGN,
|
|
|
88 |
DBGACTION_REALLOC,
|
|
|
89 |
DBGACTION_REOWNALLOC,
|
| 471 |
theseven |
90 |
DBGACTION_FREE,
|
| 772 |
user890104 |
91 |
DBGACTION_FREEMONITOR,
|
|
|
92 |
DBGACTION_RTCREAD,
|
|
|
93 |
DBGACTION_RTCWRITE,
|
| 15 |
theseven |
94 |
};
|
|
|
95 |
|
| 429 |
theseven |
96 |
static struct scheduler_thread dbgthread_handle IBSS_ATTR;
|
| 346 |
theseven |
97 |
static uint32_t dbgstack[0x200] STACK_ATTR;
|
| 15 |
theseven |
98 |
struct wakeup dbgwakeup IBSS_ATTR;
|
|
|
99 |
static enum dbgaction_t dbgaction IBSS_ATTR;
|
| 29 |
theseven |
100 |
static int dbgi2cbus;
|
|
|
101 |
static int dbgi2cslave;
|
|
|
102 |
static int dbgactionaddr;
|
| 95 |
theseven |
103 |
static int dbgactionoffset;
|
| 29 |
theseven |
104 |
static int dbgactionlength;
|
|
|
105 |
static int dbgactionconsoles;
|
|
|
106 |
static int dbgactiontype;
|
| 25 |
theseven |
107 |
static char dbgconsendbuf[4096];
|
|
|
108 |
static char dbgconrecvbuf[1024];
|
|
|
109 |
static int dbgconsendreadidx IBSS_ATTR;
|
|
|
110 |
static int dbgconsendwriteidx IBSS_ATTR;
|
|
|
111 |
static int dbgconrecvreadidx IBSS_ATTR;
|
|
|
112 |
static int dbgconrecvwriteidx IBSS_ATTR;
|
|
|
113 |
static struct wakeup dbgconsendwakeup IBSS_ATTR;
|
|
|
114 |
static struct wakeup dbgconrecvwakeup IBSS_ATTR;
|
|
|
115 |
static bool dbgconsoleattached IBSS_ATTR;
|
| 15 |
theseven |
116 |
|
| 25 |
theseven |
117 |
static const char dbgconoverflowstr[] = "\n\n[overflowed]\n\n";
|
| 15 |
theseven |
118 |
|
| 438 |
theseven |
119 |
extern int _poolstart; // These aren't ints at all, but gcc complains about void types being
|
|
|
120 |
extern int _poolend; // used here, and we only need the address, so just make it happy...
|
| 25 |
theseven |
121 |
|
| 35 |
theseven |
122 |
|
| 756 |
theseven |
123 |
static struct usb_device_descriptor device_descriptor CACHEALIGN_ATTR =
|
| 15 |
theseven |
124 |
{
|
|
|
125 |
.bLength = sizeof(struct usb_device_descriptor),
|
|
|
126 |
.bDescriptorType = USB_DT_DEVICE,
|
|
|
127 |
.bcdUSB = 0x0200,
|
|
|
128 |
.bDeviceClass = USB_CLASS_VENDOR_SPEC,
|
|
|
129 |
.bDeviceSubClass = 0xff,
|
|
|
130 |
.bDeviceProtocol = 0xff,
|
|
|
131 |
.bMaxPacketSize0 = 64,
|
|
|
132 |
.idVendor = 0xffff,
|
|
|
133 |
.idProduct = 0xe000,
|
|
|
134 |
.bcdDevice = 0x0001,
|
|
|
135 |
.iManufacturer = 1,
|
|
|
136 |
.iProduct = 2,
|
|
|
137 |
.iSerialNumber = 0,
|
|
|
138 |
.bNumConfigurations = 1
|
|
|
139 |
};
|
|
|
140 |
|
|
|
141 |
static struct usb_config_bundle
|
|
|
142 |
{
|
|
|
143 |
struct usb_config_descriptor config_descriptor;
|
|
|
144 |
struct usb_interface_descriptor interface_descriptor;
|
|
|
145 |
struct usb_endpoint_descriptor endpoint1_descriptor;
|
|
|
146 |
struct usb_endpoint_descriptor endpoint2_descriptor;
|
|
|
147 |
struct usb_endpoint_descriptor endpoint3_descriptor;
|
|
|
148 |
struct usb_endpoint_descriptor endpoint4_descriptor;
|
| 763 |
user890104 |
149 |
} __attribute__((packed)) config_bundle CACHEALIGN_ATTR =
|
| 15 |
theseven |
150 |
{
|
|
|
151 |
.config_descriptor =
|
|
|
152 |
{
|
|
|
153 |
.bLength = sizeof(struct usb_config_descriptor),
|
|
|
154 |
.bDescriptorType = USB_DT_CONFIG,
|
|
|
155 |
.wTotalLength = sizeof(struct usb_config_descriptor)
|
|
|
156 |
+ sizeof(struct usb_interface_descriptor)
|
|
|
157 |
+ sizeof(struct usb_endpoint_descriptor) * 4,
|
|
|
158 |
.bNumInterfaces = 1,
|
|
|
159 |
.bConfigurationValue = 1,
|
|
|
160 |
.iConfiguration = 0,
|
|
|
161 |
.bmAttributes = USB_CONFIG_ATT_ONE,
|
|
|
162 |
.bMaxPower = 250
|
|
|
163 |
},
|
|
|
164 |
.interface_descriptor =
|
|
|
165 |
{
|
|
|
166 |
.bLength = sizeof(struct usb_interface_descriptor),
|
|
|
167 |
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
168 |
.bInterfaceNumber = 0,
|
|
|
169 |
.bAlternateSetting = 0,
|
|
|
170 |
.bNumEndpoints = 4,
|
|
|
171 |
.bInterfaceClass = USB_CLASS_VENDOR_SPEC,
|
|
|
172 |
.bInterfaceSubClass = 0xff,
|
|
|
173 |
.bInterfaceProtocol = 0xff,
|
|
|
174 |
.iInterface = 0
|
|
|
175 |
},
|
|
|
176 |
.endpoint1_descriptor =
|
|
|
177 |
{
|
|
|
178 |
.bLength = sizeof(struct usb_endpoint_descriptor),
|
|
|
179 |
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
180 |
.bEndpointAddress = 0,
|
|
|
181 |
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
182 |
.wMaxPacketSize = 0,
|
|
|
183 |
.bInterval = 1
|
|
|
184 |
},
|
|
|
185 |
.endpoint2_descriptor =
|
|
|
186 |
{
|
|
|
187 |
.bLength = sizeof(struct usb_endpoint_descriptor),
|
|
|
188 |
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
189 |
.bEndpointAddress = 0,
|
|
|
190 |
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
191 |
.wMaxPacketSize = 0,
|
|
|
192 |
.bInterval = 1
|
|
|
193 |
},
|
|
|
194 |
.endpoint3_descriptor =
|
|
|
195 |
{
|
|
|
196 |
.bLength = sizeof(struct usb_endpoint_descriptor),
|
|
|
197 |
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
198 |
.bEndpointAddress = 0,
|
|
|
199 |
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
200 |
.wMaxPacketSize = 0,
|
|
|
201 |
.bInterval = 1
|
|
|
202 |
},
|
|
|
203 |
.endpoint4_descriptor =
|
|
|
204 |
{
|
|
|
205 |
.bLength = sizeof(struct usb_endpoint_descriptor),
|
|
|
206 |
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
207 |
.bEndpointAddress = 0,
|
|
|
208 |
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
209 |
.wMaxPacketSize = 0,
|
|
|
210 |
.bInterval = 1
|
|
|
211 |
}
|
|
|
212 |
};
|
|
|
213 |
|
| 756 |
theseven |
214 |
static struct usb_string_descriptor string_vendor CACHEALIGN_ATTR =
|
| 15 |
theseven |
215 |
{
|
| 696 |
theseven |
216 |
30,
|
|
|
217 |
USB_DT_STRING,
|
|
|
218 |
{'f', 'r', 'e', 'e', 'm', 'y', 'i', 'p', 'o', 'd', '.', 'o', 'r', 'g'}
|
|
|
219 |
};
|
|
|
220 |
|
| 756 |
theseven |
221 |
static struct usb_string_descriptor string_product CACHEALIGN_ATTR =
|
| 696 |
theseven |
222 |
{
|
| 15 |
theseven |
223 |
32,
|
|
|
224 |
USB_DT_STRING,
|
| 631 |
theseven |
225 |
{'e', 'm', 'C', 'O', 'R', 'E', ' ', 'D', 'e', 'b', 'u', 'g', 'g', 'e', 'r'}
|
| 15 |
theseven |
226 |
};
|
|
|
227 |
|
| 756 |
theseven |
228 |
static const struct usb_string_descriptor lang_descriptor CACHEALIGN_ATTR =
|
| 15 |
theseven |
229 |
{
|
|
|
230 |
4,
|
|
|
231 |
USB_DT_STRING,
|
|
|
232 |
{0x0409}
|
|
|
233 |
};
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
void usb_setup_dbg_listener()
|
|
|
237 |
{
|
| 464 |
theseven |
238 |
usb_drv_recv(dbgendpoints[0], dbgrecvbuf, 512);
|
| 15 |
theseven |
239 |
}
|
|
|
240 |
|
|
|
241 |
void usb_handle_control_request(struct usb_ctrlrequest* req)
|
|
|
242 |
{
|
|
|
243 |
const void* addr;
|
|
|
244 |
int size = -1;
|
|
|
245 |
switch (req->bRequest)
|
|
|
246 |
{
|
|
|
247 |
case USB_REQ_GET_STATUS:
|
|
|
248 |
if (req->bRequestType == USB_DIR_IN) ctrlresp[0] = 1;
|
|
|
249 |
else ctrlresp[0] = 0;
|
|
|
250 |
ctrlresp[1] = 0;
|
|
|
251 |
addr = ctrlresp;
|
|
|
252 |
size = 2;
|
|
|
253 |
break;
|
|
|
254 |
case USB_REQ_CLEAR_FEATURE:
|
|
|
255 |
if (req->bRequestType == USB_RECIP_ENDPOINT && req->wValue == USB_ENDPOINT_HALT)
|
|
|
256 |
usb_drv_stall(req->wIndex & 0xf, false, req->wIndex >> 7);
|
|
|
257 |
size = 0;
|
|
|
258 |
break;
|
|
|
259 |
case USB_REQ_SET_FEATURE:
|
|
|
260 |
size = 0;
|
|
|
261 |
break;
|
|
|
262 |
case USB_REQ_SET_ADDRESS:
|
|
|
263 |
size = 0;
|
|
|
264 |
usb_drv_cancel_all_transfers();
|
|
|
265 |
usb_drv_set_address(req->wValue);
|
|
|
266 |
usb_setup_dbg_listener();
|
|
|
267 |
break;
|
|
|
268 |
case USB_REQ_GET_DESCRIPTOR:
|
|
|
269 |
switch (req->wValue >> 8)
|
|
|
270 |
{
|
|
|
271 |
case USB_DT_DEVICE:
|
|
|
272 |
addr = &device_descriptor;
|
|
|
273 |
size = sizeof(device_descriptor);
|
|
|
274 |
break;
|
|
|
275 |
case USB_DT_OTHER_SPEED_CONFIG:
|
| 696 |
theseven |
276 |
|
| 15 |
theseven |
277 |
case USB_DT_CONFIG:
|
|
|
278 |
if ((req->wValue >> 8) == USB_DT_CONFIG)
|
|
|
279 |
{
|
|
|
280 |
int maxpacket = usb_drv_port_speed() ? 512 : 64;
|
|
|
281 |
config_bundle.endpoint1_descriptor.wMaxPacketSize = maxpacket;
|
|
|
282 |
config_bundle.endpoint2_descriptor.wMaxPacketSize = maxpacket;
|
|
|
283 |
config_bundle.endpoint3_descriptor.wMaxPacketSize = maxpacket;
|
|
|
284 |
config_bundle.endpoint4_descriptor.wMaxPacketSize = maxpacket;
|
|
|
285 |
config_bundle.config_descriptor.bDescriptorType = USB_DT_CONFIG;
|
|
|
286 |
}
|
|
|
287 |
else
|
|
|
288 |
{
|
|
|
289 |
int maxpacket = usb_drv_port_speed() ? 64 : 512;
|
|
|
290 |
config_bundle.endpoint1_descriptor.wMaxPacketSize = maxpacket;
|
|
|
291 |
config_bundle.endpoint2_descriptor.wMaxPacketSize = maxpacket;
|
|
|
292 |
config_bundle.endpoint3_descriptor.wMaxPacketSize = maxpacket;
|
|
|
293 |
config_bundle.endpoint4_descriptor.wMaxPacketSize = maxpacket;
|
|
|
294 |
config_bundle.config_descriptor.bDescriptorType = USB_DT_OTHER_SPEED_CONFIG;
|
|
|
295 |
}
|
|
|
296 |
addr = &config_bundle;
|
|
|
297 |
size = sizeof(config_bundle);
|
|
|
298 |
break;
|
|
|
299 |
case USB_DT_STRING:
|
|
|
300 |
switch (req->wValue & 0xff)
|
|
|
301 |
{
|
|
|
302 |
case 0:
|
|
|
303 |
addr = &lang_descriptor;
|
| 234 |
theseven |
304 |
size = lang_descriptor.bLength;
|
| 15 |
theseven |
305 |
break;
|
|
|
306 |
case 1:
|
| 696 |
theseven |
307 |
addr = &string_vendor;
|
|
|
308 |
size = string_vendor.bLength;
|
| 234 |
theseven |
309 |
break;
|
| 15 |
theseven |
310 |
case 2:
|
| 696 |
theseven |
311 |
addr = &string_product;
|
|
|
312 |
size = string_product.bLength;
|
| 15 |
theseven |
313 |
break;
|
|
|
314 |
}
|
|
|
315 |
break;
|
|
|
316 |
}
|
|
|
317 |
break;
|
|
|
318 |
case USB_REQ_GET_CONFIGURATION:
|
|
|
319 |
ctrlresp[0] = 1;
|
|
|
320 |
addr = ctrlresp;
|
|
|
321 |
size = 1;
|
|
|
322 |
break;
|
|
|
323 |
case USB_REQ_SET_CONFIGURATION:
|
|
|
324 |
usb_drv_cancel_all_transfers();
|
|
|
325 |
usb_setup_dbg_listener();
|
|
|
326 |
size = 0;
|
|
|
327 |
break;
|
|
|
328 |
}
|
|
|
329 |
if (!size) usb_drv_send_nonblocking(0, NULL, 0);
|
|
|
330 |
else if (size == -1)
|
|
|
331 |
{
|
|
|
332 |
usb_drv_stall(0, true, true);
|
|
|
333 |
usb_drv_stall(0, true, false);
|
|
|
334 |
}
|
|
|
335 |
else
|
|
|
336 |
{
|
|
|
337 |
usb_drv_recv(0, NULL, 0);
|
|
|
338 |
usb_drv_send_nonblocking(0, addr, size > req->wLength ? req->wLength : size);
|
|
|
339 |
}
|
|
|
340 |
}
|
|
|
341 |
|
| 29 |
theseven |
342 |
bool set_dbgaction(enum dbgaction_t action, int addsize)
|
| 15 |
theseven |
343 |
{
|
|
|
344 |
if (dbgaction != DBGACTION_IDLE)
|
|
|
345 |
{
|
|
|
346 |
dbgsendbuf[0] = 3;
|
| 29 |
theseven |
347 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgsendbuf, 16 + addsize);
|
| 15 |
theseven |
348 |
return true;
|
|
|
349 |
}
|
|
|
350 |
dbgaction = action;
|
|
|
351 |
wakeup_signal(&dbgwakeup);
|
|
|
352 |
return false;
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
void reset() __attribute__((noreturn));
|
|
|
356 |
|
|
|
357 |
void usb_handle_transfer_complete(int endpoint, int dir, int status, int length)
|
|
|
358 |
{
|
|
|
359 |
void* addr = dbgsendbuf;
|
|
|
360 |
int size = 0;
|
|
|
361 |
if (endpoint == dbgendpoints[0])
|
|
|
362 |
{
|
| 226 |
theseven |
363 |
#ifdef USB_HAVE_TARGET_SPECIFIC_REQUESTS
|
|
|
364 |
if (dbgrecvbuf[0] >= 0xffff0000)
|
|
|
365 |
{
|
|
|
366 |
if (!set_dbgaction(DBGACTION_TARGETSPECIFIC, 0))
|
|
|
367 |
memcpy(dbgasyncsendbuf, dbgrecvbuf, sizeof(dbgasyncsendbuf));
|
|
|
368 |
usb_setup_dbg_listener();
|
|
|
369 |
return;
|
|
|
370 |
}
|
|
|
371 |
#endif
|
| 15 |
theseven |
372 |
switch (dbgrecvbuf[0])
|
|
|
373 |
{
|
| 28 |
theseven |
374 |
case 1: // GET INFO
|
| 15 |
theseven |
375 |
dbgsendbuf[0] = 1;
|
|
|
376 |
size = 16;
|
| 28 |
theseven |
377 |
switch (dbgrecvbuf[1])
|
|
|
378 |
{
|
|
|
379 |
case 0: // GET VERSION INFO
|
| 85 |
theseven |
380 |
dbgsendbuf[1] = VERSION_SVN_INT;
|
|
|
381 |
dbgsendbuf[2] = VERSION_MAJOR | (VERSION_MINOR << 8)
|
| 429 |
theseven |
382 |
| (VERSION_PATCH << 16) | (2 << 24);
|
| 85 |
theseven |
383 |
dbgsendbuf[3] = PLATFORM_ID;
|
| 28 |
theseven |
384 |
break;
|
|
|
385 |
case 1: // GET PACKET SIZE INFO
|
|
|
386 |
dbgsendbuf[1] = 0x02000200;
|
|
|
387 |
dbgsendbuf[2] = usb_drv_get_max_out_size();
|
|
|
388 |
dbgsendbuf[3] = usb_drv_get_max_in_size();
|
|
|
389 |
break;
|
| 35 |
theseven |
390 |
case 2: // GET USER MEMORY INFO
|
| 438 |
theseven |
391 |
dbgsendbuf[1] = (uint32_t)&_poolstart;
|
|
|
392 |
dbgsendbuf[2] = (uint32_t)&_poolend;
|
| 35 |
theseven |
393 |
break;
|
| 28 |
theseven |
394 |
default:
|
|
|
395 |
dbgsendbuf[0] = 2;
|
|
|
396 |
}
|
| 15 |
theseven |
397 |
break;
|
|
|
398 |
case 2: // RESET
|
| 29 |
theseven |
399 |
if (dbgrecvbuf[1])
|
|
|
400 |
{
|
|
|
401 |
if (set_dbgaction(DBGACTION_RESET, 0)) break;
|
|
|
402 |
dbgsendbuf[0] = 1;
|
|
|
403 |
size = 16;
|
|
|
404 |
}
|
|
|
405 |
else reset();
|
| 15 |
theseven |
406 |
break;
|
|
|
407 |
case 3: // POWER OFF
|
| 29 |
theseven |
408 |
if (set_dbgaction(DBGACTION_POWEROFF, 0)) break;
|
|
|
409 |
dbgactiontype = dbgrecvbuf[1];
|
|
|
410 |
dbgsendbuf[0] = 1;
|
|
|
411 |
size = 16;
|
| 15 |
theseven |
412 |
break;
|
|
|
413 |
case 4: // READ MEMORY
|
|
|
414 |
dbgsendbuf[0] = 1;
|
|
|
415 |
memcpy(&dbgsendbuf[4], (const void*)dbgrecvbuf[1], dbgrecvbuf[2]);
|
|
|
416 |
size = dbgrecvbuf[2] + 16;
|
|
|
417 |
break;
|
|
|
418 |
case 5: // WRITE MEMORY
|
|
|
419 |
dbgsendbuf[0] = 1;
|
|
|
420 |
memcpy((void*)dbgrecvbuf[1], &dbgrecvbuf[4], dbgrecvbuf[2]);
|
|
|
421 |
size = 16;
|
|
|
422 |
break;
|
|
|
423 |
case 6: // READ DMA
|
|
|
424 |
dbgsendbuf[0] = 1;
|
|
|
425 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgsendbuf, 16);
|
|
|
426 |
usb_drv_send_nonblocking(dbgendpoints[3], (const void*)dbgrecvbuf[1], dbgrecvbuf[2]);
|
|
|
427 |
break;
|
|
|
428 |
case 7: // WRITE DMA
|
|
|
429 |
dbgsendbuf[0] = 1;
|
|
|
430 |
size = 16;
|
|
|
431 |
usb_drv_recv(dbgendpoints[2], (void*)dbgrecvbuf[1], dbgrecvbuf[2]);
|
|
|
432 |
break;
|
| 85 |
theseven |
433 |
#ifdef HAVE_I2C
|
| 15 |
theseven |
434 |
case 8: // READ I2C
|
| 29 |
theseven |
435 |
if (set_dbgaction(DBGACTION_I2CRECV, dbgrecvbuf[1] >> 24)) break;
|
| 15 |
theseven |
436 |
dbgi2cbus = dbgrecvbuf[1] & 0xff;
|
|
|
437 |
dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
|
| 29 |
theseven |
438 |
dbgactionaddr = (dbgrecvbuf[1] >> 16) & 0xff;
|
|
|
439 |
dbgactionlength = dbgrecvbuf[1] >> 24;
|
| 175 |
theseven |
440 |
if (!dbgactionlength) dbgactionlength = 256;
|
| 15 |
theseven |
441 |
break;
|
|
|
442 |
case 9: // WRITE I2C
|
| 29 |
theseven |
443 |
if (set_dbgaction(DBGACTION_I2CSEND, 0)) break;
|
| 15 |
theseven |
444 |
dbgi2cbus = dbgrecvbuf[1] & 0xff;
|
|
|
445 |
dbgi2cslave = (dbgrecvbuf[1] >> 8) & 0xff;
|
| 29 |
theseven |
446 |
dbgactionaddr = (dbgrecvbuf[1] >> 16) & 0xff;
|
|
|
447 |
dbgactionlength = dbgrecvbuf[1] >> 24;
|
| 175 |
theseven |
448 |
if (!dbgactionlength) dbgactionlength = 256;
|
| 212 |
theseven |
449 |
memcpy(dbgasyncsendbuf, &dbgrecvbuf[4], dbgactionlength);
|
| 15 |
theseven |
450 |
break;
|
| 85 |
theseven |
451 |
#endif
|
| 25 |
theseven |
452 |
case 10: // READ CONSOLE
|
|
|
453 |
dbgconsoleattached = true;
|
|
|
454 |
int bytes = dbgconsendwriteidx - dbgconsendreadidx;
|
| 336 |
theseven |
455 |
int used = 0;
|
| 25 |
theseven |
456 |
if (bytes)
|
|
|
457 |
{
|
|
|
458 |
if (bytes < 0) bytes += sizeof(dbgconsendbuf);
|
| 336 |
theseven |
459 |
used = bytes;
|
| 25 |
theseven |
460 |
if (bytes > dbgrecvbuf[1]) bytes = dbgrecvbuf[1];
|
|
|
461 |
int readbytes = bytes;
|
|
|
462 |
char* outptr = (char*)&dbgsendbuf[4];
|
|
|
463 |
if (dbgconsendreadidx + bytes >= sizeof(dbgconsendbuf))
|
|
|
464 |
{
|
|
|
465 |
readbytes = sizeof(dbgconsendbuf) - dbgconsendreadidx;
|
|
|
466 |
memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
|
|
|
467 |
dbgconsendreadidx = 0;
|
|
|
468 |
outptr = &outptr[readbytes];
|
|
|
469 |
readbytes = bytes - readbytes;
|
|
|
470 |
}
|
|
|
471 |
if (readbytes) memcpy(outptr, &dbgconsendbuf[dbgconsendreadidx], readbytes);
|
|
|
472 |
dbgconsendreadidx += readbytes;
|
| 26 |
theseven |
473 |
wakeup_signal(&dbgconsendwakeup);
|
| 25 |
theseven |
474 |
}
|
|
|
475 |
dbgsendbuf[0] = 1;
|
|
|
476 |
dbgsendbuf[1] = bytes;
|
|
|
477 |
dbgsendbuf[2] = sizeof(dbgconsendbuf);
|
| 336 |
theseven |
478 |
dbgsendbuf[3] = used - bytes;
|
| 25 |
theseven |
479 |
size = 16 + dbgrecvbuf[1];
|
|
|
480 |
break;
|
| 26 |
theseven |
481 |
case 11: // WRITE CONSOLE
|
|
|
482 |
bytes = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
|
|
|
483 |
if (bytes < 0) bytes += sizeof(dbgconrecvbuf);
|
|
|
484 |
if (bytes)
|
|
|
485 |
{
|
|
|
486 |
if (bytes > dbgrecvbuf[1]) bytes = dbgrecvbuf[1];
|
|
|
487 |
int writebytes = bytes;
|
|
|
488 |
char* readptr = (char*)&dbgrecvbuf[4];
|
|
|
489 |
if (dbgconrecvwriteidx + bytes >= sizeof(dbgconrecvbuf))
|
|
|
490 |
{
|
|
|
491 |
writebytes = sizeof(dbgconrecvbuf) - dbgconrecvwriteidx;
|
|
|
492 |
memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
|
|
|
493 |
dbgconrecvwriteidx = 0;
|
|
|
494 |
readptr = &readptr[writebytes];
|
|
|
495 |
writebytes = bytes - writebytes;
|
|
|
496 |
}
|
|
|
497 |
if (writebytes) memcpy(&dbgconrecvbuf[dbgconrecvwriteidx], readptr, writebytes);
|
|
|
498 |
dbgconrecvwriteidx += writebytes;
|
|
|
499 |
wakeup_signal(&dbgconrecvwakeup);
|
|
|
500 |
}
|
|
|
501 |
dbgsendbuf[0] = 1;
|
|
|
502 |
dbgsendbuf[1] = bytes;
|
|
|
503 |
dbgsendbuf[2] = sizeof(dbgconrecvbuf);
|
|
|
504 |
dbgsendbuf[3] = dbgconrecvreadidx - dbgconrecvwriteidx - 1;
|
|
|
505 |
size = 16;
|
|
|
506 |
break;
|
| 29 |
theseven |
507 |
case 12: // CWRITE
|
|
|
508 |
if (set_dbgaction(DBGACTION_CWRITE, 0)) break;
|
|
|
509 |
dbgactionconsoles = dbgrecvbuf[1];
|
|
|
510 |
dbgactionlength = dbgrecvbuf[2];
|
|
|
511 |
memcpy(dbgasyncsendbuf, &dbgrecvbuf[4], dbgactionlength);
|
|
|
512 |
break;
|
|
|
513 |
case 13: // CREAD
|
|
|
514 |
if (set_dbgaction(DBGACTION_CREAD, dbgrecvbuf[2])) break;
|
|
|
515 |
dbgactionconsoles = dbgrecvbuf[1];
|
|
|
516 |
dbgactionlength = dbgrecvbuf[2];
|
|
|
517 |
break;
|
|
|
518 |
case 14: // CFLUSH
|
|
|
519 |
if (set_dbgaction(DBGACTION_CFLUSH, 0)) break;
|
|
|
520 |
dbgactionconsoles = dbgrecvbuf[1];
|
|
|
521 |
break;
|
| 31 |
theseven |
522 |
case 15: // GET PROCESS INFO
|
|
|
523 |
dbgsendbuf[0] = 1;
|
|
|
524 |
dbgsendbuf[1] = SCHEDULER_THREAD_INFO_VERSION;
|
| 429 |
theseven |
525 |
dbgsendbuf[2] = (uint32_t)head_thread;
|
|
|
526 |
size = 16;
|
| 31 |
theseven |
527 |
break;
|
| 34 |
theseven |
528 |
case 16: // FREEZE SCHEDULER
|
| 54 |
theseven |
529 |
dbgsendbuf[1] = scheduler_freeze(dbgrecvbuf[1]);
|
| 595 |
theseven |
530 |
scheduler_switch(NULL, NULL);
|
| 34 |
theseven |
531 |
dbgsendbuf[0] = 1;
|
|
|
532 |
size = 16;
|
|
|
533 |
break;
|
| 35 |
theseven |
534 |
case 17: // SUSPEND THREAD
|
| 54 |
theseven |
535 |
if (dbgrecvbuf[1])
|
|
|
536 |
{
|
| 429 |
theseven |
537 |
if (thread_suspend((struct scheduler_thread*)(dbgrecvbuf[2])) == ALREADY_SUSPENDED)
|
|
|
538 |
dbgsendbuf[1] = 1;
|
| 54 |
theseven |
539 |
else dbgsendbuf[1] = 0;
|
|
|
540 |
}
|
|
|
541 |
else
|
|
|
542 |
{
|
| 429 |
theseven |
543 |
if (thread_resume((struct scheduler_thread*)(dbgrecvbuf[2])) == ALREADY_RESUMED)
|
|
|
544 |
dbgsendbuf[1] = 0;
|
| 54 |
theseven |
545 |
else dbgsendbuf[1] = 1;
|
|
|
546 |
}
|
| 35 |
theseven |
547 |
dbgsendbuf[0] = 1;
|
|
|
548 |
size = 16;
|
|
|
549 |
break;
|
|
|
550 |
case 18: // KILL THREAD
|
| 429 |
theseven |
551 |
thread_terminate((struct scheduler_thread*)(dbgrecvbuf[1]));
|
| 35 |
theseven |
552 |
dbgsendbuf[0] = 1;
|
|
|
553 |
size = 16;
|
|
|
554 |
break;
|
| 438 |
theseven |
555 |
case 19: // CREATE THREAD
|
| 35 |
theseven |
556 |
dbgsendbuf[0] = 1;
|
| 429 |
theseven |
557 |
dbgsendbuf[1] = (uint32_t)thread_create(NULL, (const char*)(dbgsendbuf[1]),
|
|
|
558 |
(const void*)(dbgsendbuf[2]),
|
|
|
559 |
(char*)(dbgsendbuf[3]),
|
|
|
560 |
dbgsendbuf[4], (enum thread_type)dbgsendbuf[5],
|
|
|
561 |
dbgsendbuf[6], dbgsendbuf[7]);
|
| 35 |
theseven |
562 |
size = 16;
|
|
|
563 |
break;
|
|
|
564 |
case 20: // FLUSH CACHE
|
|
|
565 |
clean_dcache();
|
|
|
566 |
invalidate_icache();
|
|
|
567 |
dbgsendbuf[0] = 1;
|
|
|
568 |
size = 16;
|
|
|
569 |
break;
|
| 92 |
theseven |
570 |
case 21: // EXECIMAGE
|
|
|
571 |
if (set_dbgaction(DBGACTION_EXECIMAGE, 0)) break;
|
|
|
572 |
dbgactionaddr = dbgrecvbuf[1];
|
| 453 |
theseven |
573 |
dbgactiontype = dbgrecvbuf[2];
|
| 92 |
theseven |
574 |
break;
|
| 95 |
theseven |
575 |
#ifdef HAVE_BOOTFLASH
|
|
|
576 |
case 22: // READ BOOT FLASH
|
|
|
577 |
if (set_dbgaction(DBGACTION_READBOOTFLASH, 0)) break;
|
|
|
578 |
dbgactionaddr = dbgrecvbuf[1];
|
|
|
579 |
dbgactionoffset = dbgrecvbuf[2];
|
|
|
580 |
dbgactionlength = dbgrecvbuf[3];
|
|
|
581 |
break;
|
|
|
582 |
case 23: // WRITE BOOT FLASH
|
|
|
583 |
if (set_dbgaction(DBGACTION_WRITEBOOTFLASH, 0)) break;
|
|
|
584 |
dbgactionaddr = dbgrecvbuf[1];
|
|
|
585 |
dbgactionoffset = dbgrecvbuf[2];
|
|
|
586 |
dbgactionlength = dbgrecvbuf[3];
|
|
|
587 |
break;
|
|
|
588 |
#endif
|
| 127 |
theseven |
589 |
case 24: // EXECFIRMWARE
|
|
|
590 |
if (set_dbgaction(DBGACTION_EXECFIRMWARE, 0)) break;
|
|
|
591 |
dbgactionaddr = dbgrecvbuf[1];
|
| 438 |
theseven |
592 |
dbgactionoffset = dbgrecvbuf[2];
|
|
|
593 |
dbgactionlength = dbgrecvbuf[3];
|
| 127 |
theseven |
594 |
break;
|
| 157 |
theseven |
595 |
#ifdef HAVE_HWKEYAES
|
|
|
596 |
case 25: // HWKEYAES
|
|
|
597 |
if (set_dbgaction(DBGACTION_HWKEYAES, 0)) break;
|
|
|
598 |
dbgactiontype = ((uint8_t*)dbgrecvbuf)[4];
|
|
|
599 |
dbgactionoffset = ((uint16_t*)dbgrecvbuf)[3];
|
|
|
600 |
dbgactionaddr = dbgrecvbuf[2];
|
|
|
601 |
dbgactionlength = dbgrecvbuf[3];
|
| 179 |
theseven |
602 |
break;
|
| 157 |
theseven |
603 |
#endif
|
|
|
604 |
#ifdef HAVE_HMACSHA1
|
|
|
605 |
case 26: // HMACSHA1
|
|
|
606 |
if (set_dbgaction(DBGACTION_HMACSHA1, 0)) break;
|
|
|
607 |
dbgactionaddr = dbgrecvbuf[1];
|
|
|
608 |
dbgactionlength = dbgrecvbuf[2];
|
|
|
609 |
dbgactionoffset = dbgrecvbuf[3];
|
| 179 |
theseven |
610 |
break;
|
| 157 |
theseven |
611 |
#endif
|
| 346 |
theseven |
612 |
#ifdef HAVE_STORAGE
|
|
|
613 |
case 27: // STORAGE_GET_INFO
|
|
|
614 |
case 28: // STORAGE_READ_SECTORS_MD
|
|
|
615 |
case 29: // STORAGE_WRITE_SECTORS_MD
|
|
|
616 |
case 30: // FILE_OPEN
|
|
|
617 |
case 31: // FILESIZE
|
|
|
618 |
case 32: // READ
|
|
|
619 |
case 33: // WRITE
|
|
|
620 |
case 34: // LSEEK
|
|
|
621 |
case 35: // FTRUNCATE
|
|
|
622 |
case 36: // FSYNC
|
|
|
623 |
case 37: // CLOSE
|
|
|
624 |
case 38: // CLOSE_MONITOR_FILES
|
|
|
625 |
case 39: // RELEASE_FILES
|
|
|
626 |
case 40: // REMOVE
|
|
|
627 |
case 41: // RENAME
|
|
|
628 |
case 42: // OPENDIR
|
|
|
629 |
case 43: // READDIR
|
|
|
630 |
case 44: // CLOSEDIR
|
|
|
631 |
case 45: // CLOSE_MONITOR_DIRS
|
|
|
632 |
case 46: // RELEASE_DIRS
|
|
|
633 |
case 47: // MKDIR
|
|
|
634 |
case 48: // RMDIR
|
|
|
635 |
case 49: // ERRNO
|
|
|
636 |
#ifdef HAVE_HOTSWAP
|
|
|
637 |
case 50: // DISK_MOUNT
|
|
|
638 |
case 51: // DISK_UNMOUNT
|
|
|
639 |
#endif
|
| 479 |
theseven |
640 |
case 58: // FAT_ENABLE_FLUSHING
|
| 634 |
theseven |
641 |
case 59: // FAT_SIZE
|
| 346 |
theseven |
642 |
if (!set_dbgaction(DBGACTION_STORAGE, 0))
|
|
|
643 |
memcpy(dbgasyncsendbuf, dbgrecvbuf, sizeof(dbgasyncsendbuf));
|
|
|
644 |
break;
|
|
|
645 |
#endif
|
| 438 |
theseven |
646 |
case 52: // MALLOC
|
|
|
647 |
if (set_dbgaction(DBGACTION_MALLOC, 0)) break;
|
|
|
648 |
dbgactionlength = dbgrecvbuf[1];
|
|
|
649 |
break;
|
|
|
650 |
case 53: // MEMALIGN
|
|
|
651 |
if (set_dbgaction(DBGACTION_MEMALIGN, 0)) break;
|
|
|
652 |
dbgactionoffset = dbgrecvbuf[1];
|
|
|
653 |
dbgactionlength = dbgrecvbuf[2];
|
|
|
654 |
break;
|
|
|
655 |
case 54: // REALLOC
|
|
|
656 |
if (set_dbgaction(DBGACTION_REALLOC, 0)) break;
|
|
|
657 |
dbgactionaddr = dbgrecvbuf[1];
|
|
|
658 |
dbgactionlength = dbgrecvbuf[2];
|
|
|
659 |
break;
|
|
|
660 |
case 55: // REOWNALLOC
|
|
|
661 |
if (set_dbgaction(DBGACTION_REOWNALLOC, 0)) break;
|
|
|
662 |
dbgactionaddr = dbgrecvbuf[1];
|
|
|
663 |
dbgactionoffset = dbgrecvbuf[2];
|
|
|
664 |
break;
|
|
|
665 |
case 56: // FREE
|
|
|
666 |
if (set_dbgaction(DBGACTION_FREE, 0)) break;
|
|
|
667 |
dbgactionaddr = dbgrecvbuf[1];
|
|
|
668 |
break;
|
| 471 |
theseven |
669 |
case 57: // FREE MONITOR ALLOCATIONS
|
|
|
670 |
if (set_dbgaction(DBGACTION_FREEMONITOR, 0)) break;
|
|
|
671 |
break;
|
| 772 |
user890104 |
672 |
#ifdef HAVE_RTC
|
|
|
673 |
case 60: // RTC READ
|
|
|
674 |
if (set_dbgaction(DBGACTION_RTCREAD, 0)) break;
|
|
|
675 |
break;
|
|
|
676 |
case 61: // RTC WRITE
|
|
|
677 |
if (set_dbgaction(DBGACTION_RTCWRITE, 0)) break;
|
|
|
678 |
memcpy(dbgasyncsendbuf, &dbgrecvbuf[1], 7);
|
|
|
679 |
break;
|
|
|
680 |
#endif
|
| 15 |
theseven |
681 |
default:
|
|
|
682 |
dbgsendbuf[0] = 2;
|
|
|
683 |
size = 16;
|
|
|
684 |
}
|
|
|
685 |
usb_setup_dbg_listener();
|
|
|
686 |
if (size) usb_drv_send_nonblocking(dbgendpoints[1], addr, size);
|
|
|
687 |
}
|
|
|
688 |
}
|
|
|
689 |
|
|
|
690 |
void usb_handle_bus_reset(void)
|
|
|
691 |
{
|
|
|
692 |
dbgendpoints[0] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
|
|
|
693 |
dbgendpoints[1] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
|
|
|
694 |
dbgendpoints[2] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_OUT);
|
|
|
695 |
dbgendpoints[3] = usb_drv_request_endpoint(USB_ENDPOINT_XFER_BULK, USB_DIR_IN);
|
|
|
696 |
config_bundle.endpoint1_descriptor.bEndpointAddress = dbgendpoints[0];
|
|
|
697 |
config_bundle.endpoint2_descriptor.bEndpointAddress = dbgendpoints[1];
|
|
|
698 |
config_bundle.endpoint3_descriptor.bEndpointAddress = dbgendpoints[2];
|
|
|
699 |
config_bundle.endpoint4_descriptor.bEndpointAddress = dbgendpoints[3];
|
|
|
700 |
usb_setup_dbg_listener();
|
|
|
701 |
}
|
|
|
702 |
|
|
|
703 |
void dbgthread(void)
|
|
|
704 |
{
|
| 429 |
theseven |
705 |
struct scheduler_thread* t;
|
| 15 |
theseven |
706 |
while (1)
|
|
|
707 |
{
|
|
|
708 |
wakeup_wait(&dbgwakeup, TIMEOUT_BLOCK);
|
| 429 |
theseven |
709 |
for (t = head_thread; t; t = t->thread_next)
|
|
|
710 |
if (t->state == THREAD_DEFUNCT)
|
| 15 |
theseven |
711 |
{
|
| 429 |
theseven |
712 |
if (t->block_type == THREAD_DEFUNCT_STKOV)
|
| 35 |
theseven |
713 |
{
|
| 429 |
theseven |
714 |
if (t->name) cprintf(1, "\n*PANIC*\nStack overflow! (%s)\n", t->name);
|
|
|
715 |
else cprintf(1, "\n*PANIC*\nStack overflow! (%08X)\n", t);
|
| 35 |
theseven |
716 |
}
|
| 429 |
theseven |
717 |
t->state = THREAD_DEFUNCT_ACK;
|
| 15 |
theseven |
718 |
}
|
|
|
719 |
if (dbgaction != DBGACTION_IDLE)
|
|
|
720 |
{
|
|
|
721 |
switch (dbgaction)
|
|
|
722 |
{
|
| 85 |
theseven |
723 |
#ifdef HAVE_I2C
|
| 15 |
theseven |
724 |
case DBGACTION_I2CSEND:
|
| 29 |
theseven |
725 |
i2c_send(dbgi2cbus, dbgi2cslave, dbgactionaddr,
|
|
|
726 |
(uint8_t*)dbgasyncsendbuf, dbgactionlength);
|
| 15 |
theseven |
727 |
dbgasyncsendbuf[0] = 1;
|
|
|
728 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
729 |
break;
|
|
|
730 |
case DBGACTION_I2CRECV:
|
| 29 |
theseven |
731 |
i2c_recv(dbgi2cbus, dbgi2cslave, dbgactionaddr,
|
|
|
732 |
(uint8_t*)(&dbgasyncsendbuf[4]), dbgactionlength);
|
| 15 |
theseven |
733 |
dbgasyncsendbuf[0] = 1;
|
| 29 |
theseven |
734 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16 + dbgactionlength);
|
| 15 |
theseven |
735 |
break;
|
| 85 |
theseven |
736 |
#endif
|
| 15 |
theseven |
737 |
case DBGACTION_POWEROFF:
|
| 127 |
theseven |
738 |
if (dbgactiontype) shutdown(true);
|
| 54 |
theseven |
739 |
power_off();
|
| 15 |
theseven |
740 |
break;
|
| 29 |
theseven |
741 |
case DBGACTION_RESET:
|
| 439 |
theseven |
742 |
shutdown(false);
|
| 29 |
theseven |
743 |
reset();
|
|
|
744 |
break;
|
|
|
745 |
case DBGACTION_CWRITE:
|
|
|
746 |
cwrite(dbgactionconsoles, (const char*)dbgasyncsendbuf, dbgactionlength);
|
|
|
747 |
dbgasyncsendbuf[0] = 1;
|
|
|
748 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
749 |
break;
|
|
|
750 |
case DBGACTION_CREAD:
|
|
|
751 |
dbgasyncsendbuf[0] = 1;
|
| 30 |
theseven |
752 |
dbgasyncsendbuf[1] = cread(dbgactionconsoles, (char*)&dbgasyncsendbuf[4],
|
|
|
753 |
dbgactionlength, 0);
|
| 226 |
theseven |
754 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16 + dbgactionlength);
|
| 29 |
theseven |
755 |
break;
|
|
|
756 |
case DBGACTION_CFLUSH:
|
|
|
757 |
cflush(dbgactionconsoles);
|
|
|
758 |
dbgasyncsendbuf[0] = 1;
|
|
|
759 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
760 |
break;
|
| 92 |
theseven |
761 |
case DBGACTION_EXECIMAGE:
|
|
|
762 |
dbgasyncsendbuf[0] = 1;
|
| 453 |
theseven |
763 |
dbgasyncsendbuf[1] = (uint32_t)execimage((void*)dbgactionaddr, dbgactiontype);
|
| 92 |
theseven |
764 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
765 |
break;
|
| 127 |
theseven |
766 |
case DBGACTION_EXECFIRMWARE:
|
|
|
767 |
dbgasyncsendbuf[0] = 1;
|
|
|
768 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
| 439 |
theseven |
769 |
shutdown(false);
|
| 438 |
theseven |
770 |
execfirmware((void*)dbgactionaddr, (void*)dbgactionoffset,
|
|
|
771 |
(size_t)dbgactionlength);
|
| 95 |
theseven |
772 |
#ifdef HAVE_BOOTFLASH
|
|
|
773 |
case DBGACTION_READBOOTFLASH:
|
|
|
774 |
bootflash_readraw((void*)dbgactionaddr, dbgactionoffset, dbgactionlength);
|
|
|
775 |
dbgasyncsendbuf[0] = 1;
|
|
|
776 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
777 |
break;
|
|
|
778 |
case DBGACTION_WRITEBOOTFLASH:
|
|
|
779 |
bootflash_writeraw((void*)dbgactionaddr, dbgactionoffset, dbgactionlength);
|
|
|
780 |
dbgasyncsendbuf[0] = 1;
|
|
|
781 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
782 |
break;
|
|
|
783 |
#endif
|
| 157 |
theseven |
784 |
#ifdef HAVE_HWKEYAES
|
|
|
785 |
case DBGACTION_HWKEYAES:
|
|
|
786 |
hwkeyaes((enum hwkeyaes_direction) dbgactiontype, dbgactionoffset,
|
|
|
787 |
(void*)dbgactionaddr, dbgactionlength);
|
|
|
788 |
dbgasyncsendbuf[0] = 1;
|
|
|
789 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
790 |
break;
|
|
|
791 |
#endif
|
|
|
792 |
#ifdef HAVE_HMACSHA1
|
|
|
793 |
case DBGACTION_HMACSHA1:
|
|
|
794 |
hmacsha1((void*)dbgactionaddr, dbgactionlength, (void*)dbgactionoffset);
|
|
|
795 |
dbgasyncsendbuf[0] = 1;
|
|
|
796 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
797 |
break;
|
|
|
798 |
#endif
|
| 226 |
theseven |
799 |
#ifdef USB_HAVE_TARGET_SPECIFIC_REQUESTS
|
|
|
800 |
case DBGACTION_TARGETSPECIFIC:
|
|
|
801 |
{
|
|
|
802 |
int size = usb_target_handle_request(dbgasyncsendbuf, sizeof(dbgasyncsendbuf));
|
|
|
803 |
if (size) usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, size);
|
|
|
804 |
break;
|
| 15 |
theseven |
805 |
}
|
| 226 |
theseven |
806 |
#endif
|
| 346 |
theseven |
807 |
#ifdef HAVE_STORAGE
|
|
|
808 |
case DBGACTION_STORAGE:
|
|
|
809 |
switch(dbgasyncsendbuf[0])
|
|
|
810 |
{
|
|
|
811 |
case 27: // STORAGE_GET_INFO
|
|
|
812 |
dbgasyncsendbuf[0] = 1;
|
|
|
813 |
storage_get_info(dbgasyncsendbuf[1],
|
|
|
814 |
(struct storage_info*)&dbgasyncsendbuf[4]);
|
|
|
815 |
dbgasyncsendbuf[1] = 1;
|
|
|
816 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf,
|
|
|
817 |
16 + sizeof(struct storage_info));
|
|
|
818 |
break;
|
|
|
819 |
case 28: // STORAGE_READ_SECTORS_MD
|
|
|
820 |
{
|
|
|
821 |
dbgasyncsendbuf[0] = 1;
|
|
|
822 |
int rc = storage_read_sectors_md(dbgasyncsendbuf[1],
|
|
|
823 |
dbgasyncsendbuf[2]
|
|
|
824 |
| (((uint64_t)(dbgasyncsendbuf[3]) << 32)),
|
|
|
825 |
dbgasyncsendbuf[4],
|
|
|
826 |
(void*)(dbgasyncsendbuf[5]));
|
|
|
827 |
dbgasyncsendbuf[1] = (uint32_t)rc;
|
|
|
828 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
829 |
break;
|
|
|
830 |
}
|
|
|
831 |
case 29: // STORAGE_WRITE_SECTORS_MD
|
|
|
832 |
{
|
|
|
833 |
dbgasyncsendbuf[0] = 1;
|
|
|
834 |
int rc = storage_write_sectors_md(dbgasyncsendbuf[1],
|
|
|
835 |
dbgasyncsendbuf[2]
|
|
|
836 |
| (((uint64_t)(dbgasyncsendbuf[3]) << 32)),
|
|
|
837 |
dbgasyncsendbuf[4],
|
|
|
838 |
(void*)(dbgasyncsendbuf[5]));
|
|
|
839 |
dbgasyncsendbuf[1] = (uint32_t)rc;
|
|
|
840 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
841 |
break;
|
|
|
842 |
}
|
|
|
843 |
case 30: // FILE_OPEN
|
|
|
844 |
{
|
|
|
845 |
dbgasyncsendbuf[0] = 1;
|
|
|
846 |
int fd = file_open((char*)(&dbgasyncsendbuf[4]), (int)(dbgasyncsendbuf[1]));
|
| 814 |
theseven |
847 |
if (fd > 0) reown_file(fd, KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
|
| 346 |
theseven |
848 |
dbgasyncsendbuf[1] = (uint32_t)fd;
|
|
|
849 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
850 |
break;
|
|
|
851 |
}
|
|
|
852 |
case 31: // FILESIZE
|
|
|
853 |
dbgasyncsendbuf[0] = 1;
|
|
|
854 |
dbgasyncsendbuf[1] = (uint32_t)filesize((int)(dbgasyncsendbuf[1]));
|
|
|
855 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
856 |
break;
|
|
|
857 |
case 32: // READ
|
|
|
858 |
dbgasyncsendbuf[0] = 1;
|
|
|
859 |
dbgasyncsendbuf[1] = (uint32_t)read((int)(dbgasyncsendbuf[1]),
|
|
|
860 |
(void*)(dbgasyncsendbuf[2]),
|
|
|
861 |
(size_t)(dbgasyncsendbuf[3]));
|
|
|
862 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
863 |
break;
|
|
|
864 |
case 33: // WRITE
|
|
|
865 |
dbgasyncsendbuf[0] = 1;
|
|
|
866 |
dbgasyncsendbuf[1] = (uint32_t)write((int)(dbgasyncsendbuf[1]),
|
|
|
867 |
(void*)(dbgasyncsendbuf[2]),
|
|
|
868 |
(size_t)(dbgasyncsendbuf[3]));
|
|
|
869 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
870 |
break;
|
|
|
871 |
case 34: // LSEEK
|
|
|
872 |
dbgasyncsendbuf[0] = 1;
|
|
|
873 |
dbgasyncsendbuf[1] = (uint32_t)lseek((int)(dbgasyncsendbuf[1]),
|
|
|
874 |
(off_t)(dbgasyncsendbuf[2]),
|
|
|
875 |
(int)(dbgasyncsendbuf[3]));
|
|
|
876 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
877 |
break;
|
|
|
878 |
case 35: // FTRUNCATE
|
|
|
879 |
dbgasyncsendbuf[0] = 1;
|
|
|
880 |
dbgasyncsendbuf[1] = (uint32_t)ftruncate((int)(dbgasyncsendbuf[1]),
|
|
|
881 |
(off_t)(dbgasyncsendbuf[2]));
|
|
|
882 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
883 |
break;
|
|
|
884 |
case 36: // FSYNC
|
|
|
885 |
dbgasyncsendbuf[0] = 1;
|
|
|
886 |
dbgasyncsendbuf[1] = (uint32_t)fsync((int)(dbgasyncsendbuf[1]));
|
|
|
887 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
888 |
break;
|
|
|
889 |
case 37: // CLOSE
|
|
|
890 |
dbgasyncsendbuf[0] = 1;
|
|
|
891 |
dbgasyncsendbuf[1] = (uint32_t)close((int)(dbgasyncsendbuf[1]));
|
|
|
892 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
893 |
break;
|
|
|
894 |
case 38: // CLOSE_MONITOR_FILES
|
|
|
895 |
dbgasyncsendbuf[0] = 1;
|
| 814 |
theseven |
896 |
int rc = close_all_of_process(KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
|
|
|
897 |
dbgasyncsendbuf[1] = (uint32_t)rc;
|
| 346 |
theseven |
898 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
899 |
break;
|
|
|
900 |
case 39: // RELEASE_FILES
|
|
|
901 |
dbgasyncsendbuf[0] = 1;
|
|
|
902 |
dbgasyncsendbuf[1] = (uint32_t)release_files((int)(dbgasyncsendbuf[1]));
|
|
|
903 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
904 |
break;
|
|
|
905 |
case 40: // REMOVE
|
|
|
906 |
dbgasyncsendbuf[0] = 1;
|
|
|
907 |
dbgasyncsendbuf[1] = (uint32_t)remove((char*)(&dbgasyncsendbuf[4]));
|
|
|
908 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
909 |
break;
|
|
|
910 |
case 41: // RENAME
|
|
|
911 |
dbgasyncsendbuf[0] = 1;
|
|
|
912 |
dbgasyncsendbuf[1] = (uint32_t)rename((char*)(&dbgasyncsendbuf[4]),
|
|
|
913 |
(char*)(&dbgasyncsendbuf[66]));
|
|
|
914 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
915 |
break;
|
|
|
916 |
case 42: // OPENDIR
|
|
|
917 |
dbgasyncsendbuf[0] = 1;
|
| 814 |
theseven |
918 |
DIR* dir = opendir((char*)(&dbgasyncsendbuf[4]));
|
|
|
919 |
if (dir > 0) reown_dir(dir, KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
|
|
|
920 |
dbgasyncsendbuf[1] = (uint32_t)dir;
|
| 346 |
theseven |
921 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
922 |
break;
|
|
|
923 |
case 43: // READDIR
|
|
|
924 |
dbgasyncsendbuf[0] = 1;
|
|
|
925 |
dbgasyncsendbuf[3] = (uint32_t)readdir((DIR*)(dbgasyncsendbuf[1]));
|
|
|
926 |
dbgasyncsendbuf[1] = 1;
|
|
|
927 |
dbgasyncsendbuf[2] = MAX_PATH;
|
|
|
928 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
929 |
break;
|
|
|
930 |
case 44: // CLOSEDIR
|
|
|
931 |
dbgasyncsendbuf[0] = 1;
|
|
|
932 |
dbgasyncsendbuf[1] = (uint32_t)closedir((DIR*)(dbgasyncsendbuf[1]));
|
|
|
933 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
934 |
break;
|
|
|
935 |
case 45: // CLOSE_MONITOR_DIRS
|
|
|
936 |
dbgasyncsendbuf[0] = 1;
|
| 814 |
theseven |
937 |
rc = closedir_all_of_process(KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
|
|
|
938 |
dbgasyncsendbuf[1] = (uint32_t)rc;
|
| 346 |
theseven |
939 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
940 |
break;
|
|
|
941 |
case 46: // RELEASE_DIRS
|
|
|
942 |
dbgasyncsendbuf[0] = 1;
|
|
|
943 |
dbgasyncsendbuf[1] = (uint32_t)release_dirs((int)(dbgasyncsendbuf[1]));
|
|
|
944 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
945 |
break;
|
|
|
946 |
case 47: // MKDIR
|
|
|
947 |
dbgasyncsendbuf[0] = 1;
|
|
|
948 |
dbgasyncsendbuf[1] = (uint32_t)mkdir((char*)(&dbgasyncsendbuf[4]));
|
|
|
949 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
950 |
break;
|
|
|
951 |
case 48: // RMDIR
|
|
|
952 |
dbgasyncsendbuf[0] = 1;
|
|
|
953 |
dbgasyncsendbuf[1] = (uint32_t)rmdir((char*)(&dbgasyncsendbuf[4]));
|
|
|
954 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
955 |
break;
|
|
|
956 |
case 49: // ERRNO
|
|
|
957 |
dbgasyncsendbuf[0] = 1;
|
|
|
958 |
dbgasyncsendbuf[1] = (uint32_t)errno;
|
|
|
959 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
960 |
break;
|
|
|
961 |
#ifdef HAVE_HOTSWAP
|
|
|
962 |
case 50: // DISK_MOUNT
|
|
|
963 |
dbgasyncsendbuf[0] = 1;
|
|
|
964 |
dbgasyncsendbuf[1] = (uint32_t)disk_mount((int)(dbgasyncsendbuf[1]));
|
|
|
965 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
966 |
break;
|
|
|
967 |
case 51: // DISK_UNMOUNT
|
|
|
968 |
dbgasyncsendbuf[0] = 1;
|
|
|
969 |
dbgasyncsendbuf[1] = (uint32_t)disk_unmount((int)(dbgasyncsendbuf[1]));
|
|
|
970 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
971 |
break;
|
|
|
972 |
#endif
|
| 479 |
theseven |
973 |
case 58: // FAT_ENABLE_FLUSHING
|
|
|
974 |
dbgasyncsendbuf[0] = 1;
|
|
|
975 |
fat_enable_flushing((bool)(dbgasyncsendbuf[1]));
|
|
|
976 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
977 |
break;
|
| 634 |
theseven |
978 |
case 59: // FAT_SIZE
|
|
|
979 |
dbgasyncsendbuf[0] = 1;
|
|
|
980 |
fat_size_mv(dbgasyncsendbuf[1], &dbgasyncsendbuf[1], &dbgasyncsendbuf[2]);
|
|
|
981 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
982 |
break;
|
| 346 |
theseven |
983 |
}
|
|
|
984 |
break;
|
|
|
985 |
#endif
|
| 438 |
theseven |
986 |
case DBGACTION_MALLOC:
|
|
|
987 |
dbgasyncsendbuf[0] = 1;
|
|
|
988 |
dbgasyncsendbuf[1] = (uint32_t)malloc((size_t)dbgactionlength);
|
| 814 |
theseven |
989 |
if (dbgasyncsendbuf[1])
|
|
|
990 |
reownalloc(dbgasyncsendbuf[1], KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
|
| 438 |
theseven |
991 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
992 |
break;
|
|
|
993 |
case DBGACTION_MEMALIGN:
|
|
|
994 |
dbgasyncsendbuf[0] = 1;
|
|
|
995 |
dbgasyncsendbuf[1] = (uint32_t)memalign((size_t)dbgactionoffset,
|
|
|
996 |
(size_t)dbgactionlength);
|
| 814 |
theseven |
997 |
if (dbgasyncsendbuf[1])
|
|
|
998 |
reownalloc(dbgasyncsendbuf[1], KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
|
| 438 |
theseven |
999 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
1000 |
break;
|
|
|
1001 |
case DBGACTION_REALLOC:
|
|
|
1002 |
dbgasyncsendbuf[0] = 1;
|
|
|
1003 |
dbgasyncsendbuf[1] = (uint32_t)realloc((void*)dbgactionaddr,
|
|
|
1004 |
(size_t)dbgactionlength);
|
|
|
1005 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
1006 |
break;
|
|
|
1007 |
case DBGACTION_REOWNALLOC:
|
|
|
1008 |
dbgasyncsendbuf[0] = 1;
|
|
|
1009 |
reownalloc((void*)dbgactionaddr, (void*)dbgactionoffset);
|
|
|
1010 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
1011 |
break;
|
|
|
1012 |
case DBGACTION_FREE:
|
|
|
1013 |
dbgasyncsendbuf[0] = 1;
|
|
|
1014 |
free((void*)dbgactionaddr);
|
|
|
1015 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
1016 |
break;
|
| 471 |
theseven |
1017 |
case DBGACTION_FREEMONITOR:
|
|
|
1018 |
dbgasyncsendbuf[0] = 1;
|
| 814 |
theseven |
1019 |
int rc = free_all_of_thread(KERNEL_OWNER(KERNEL_OWNER_USB_MONITOR));
|
|
|
1020 |
dbgasyncsendbuf[1] = (uint32_t)rc;
|
| 471 |
theseven |
1021 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
1022 |
break;
|
| 772 |
user890104 |
1023 |
#ifdef HAVE_RTC
|
|
|
1024 |
case DBGACTION_RTCREAD:
|
|
|
1025 |
dbgasyncsendbuf[0] = 1;
|
|
|
1026 |
rtc_read_datetime((struct rtc_datetime*)(&dbgasyncsendbuf[1]));
|
|
|
1027 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
1028 |
break;
|
|
|
1029 |
case DBGACTION_RTCWRITE:
|
|
|
1030 |
rtc_write_datetime((const struct rtc_datetime*)(dbgasyncsendbuf));
|
|
|
1031 |
dbgasyncsendbuf[0] = 1;
|
|
|
1032 |
usb_drv_send_nonblocking(dbgendpoints[1], dbgasyncsendbuf, 16);
|
|
|
1033 |
break;
|
|
|
1034 |
#endif
|
| 226 |
theseven |
1035 |
}
|
| 15 |
theseven |
1036 |
dbgaction = DBGACTION_IDLE;
|
|
|
1037 |
}
|
|
|
1038 |
}
|
|
|
1039 |
}
|
|
|
1040 |
|
|
|
1041 |
void usb_init(void)
|
|
|
1042 |
{
|
|
|
1043 |
dbgaction = DBGACTION_IDLE;
|
|
|
1044 |
wakeup_init(&dbgwakeup);
|
| 25 |
theseven |
1045 |
dbgconsendreadidx = 0;
|
|
|
1046 |
dbgconsendwriteidx = 0;
|
|
|
1047 |
dbgconrecvreadidx = 0;
|
|
|
1048 |
dbgconrecvwriteidx = 0;
|
|
|
1049 |
wakeup_init(&dbgconsendwakeup);
|
|
|
1050 |
wakeup_init(&dbgconrecvwakeup);
|
| 85 |
theseven |
1051 |
dbgconsoleattached = false;
|
| 429 |
theseven |
1052 |
thread_create(&dbgthread_handle, "monitor worker", dbgthread, dbgstack,
|
|
|
1053 |
sizeof(dbgstack), CORE_THREAD, 255, true);
|
| 15 |
theseven |
1054 |
usb_drv_init();
|
|
|
1055 |
}
|
| 25 |
theseven |
1056 |
|
|
|
1057 |
int dbgconsole_getfree() ICODE_ATTR;
|
|
|
1058 |
int dbgconsole_getfree()
|
|
|
1059 |
{
|
|
|
1060 |
int free = dbgconsendreadidx - dbgconsendwriteidx - 1;
|
|
|
1061 |
if (free < 0) free += sizeof(dbgconsendbuf);
|
|
|
1062 |
return free;
|
|
|
1063 |
}
|
|
|
1064 |
|
| 704 |
theseven |
1065 |
int dbgconsole_makespace(int length, bool safe) ICODE_ATTR;
|
|
|
1066 |
int dbgconsole_makespace(int length, bool safe)
|
| 25 |
theseven |
1067 |
{
|
|
|
1068 |
int free = dbgconsole_getfree();
|
| 704 |
theseven |
1069 |
while (!free && dbgconsoleattached && !safe)
|
| 25 |
theseven |
1070 |
{
|
| 336 |
theseven |
1071 |
dbgconsoleattached = false;
|
|
|
1072 |
wakeup_wait(&dbgconsendwakeup, 2000000);
|
| 25 |
theseven |
1073 |
free = dbgconsole_getfree();
|
|
|
1074 |
}
|
|
|
1075 |
if (free) return free > length ? length : free;
|
|
|
1076 |
if (length > sizeof(dbgconsendbuf) - 17) length = sizeof(dbgconsendbuf) - 17;
|
|
|
1077 |
uint32_t mode = enter_critical_section();
|
|
|
1078 |
dbgconsendreadidx += length;
|
|
|
1079 |
if (dbgconsendreadidx >= sizeof(dbgconsendbuf))
|
|
|
1080 |
dbgconsendreadidx -= sizeof(dbgconsendbuf);
|
|
|
1081 |
int offset = 0;
|
|
|
1082 |
int idx = dbgconsendreadidx;
|
|
|
1083 |
if (idx + 16 >= sizeof(dbgconsendbuf))
|
|
|
1084 |
{
|
|
|
1085 |
offset = sizeof(dbgconsendbuf) - dbgconsendreadidx;
|
|
|
1086 |
memcpy(&dbgconsendbuf[dbgconsendreadidx], dbgconoverflowstr, offset);
|
|
|
1087 |
idx = 0;
|
|
|
1088 |
}
|
|
|
1089 |
if (offset != 16) memcpy(&dbgconsendbuf[idx], &dbgconoverflowstr[offset], 16 - offset);
|
|
|
1090 |
leave_critical_section(mode);
|
|
|
1091 |
return length;
|
|
|
1092 |
}
|
|
|
1093 |
|
| 704 |
theseven |
1094 |
void dbgconsole_putc_internal(char string, bool safe)
|
| 25 |
theseven |
1095 |
{
|
| 704 |
theseven |
1096 |
dbgconsole_makespace(1, safe);
|
| 25 |
theseven |
1097 |
dbgconsendbuf[dbgconsendwriteidx++] = string;
|
|
|
1098 |
if (dbgconsendwriteidx >= sizeof(dbgconsendbuf))
|
|
|
1099 |
dbgconsendwriteidx -= sizeof(dbgconsendbuf);
|
|
|
1100 |
}
|
|
|
1101 |
|
| 704 |
theseven |
1102 |
void dbgconsole_putc(char string)
|
| 25 |
theseven |
1103 |
{
|
| 704 |
theseven |
1104 |
dbgconsole_putc_internal(string, false);
|
|
|
1105 |
}
|
|
|
1106 |
|
|
|
1107 |
void dbgconsole_sputc(char string)
|
|
|
1108 |
{
|
|
|
1109 |
dbgconsole_putc_internal(string, true);
|
|
|
1110 |
}
|
|
|
1111 |
|
|
|
1112 |
void dbgconsole_write_internal(const char* string, size_t length, bool safe)
|
|
|
1113 |
{
|
| 25 |
theseven |
1114 |
while (length)
|
|
|
1115 |
{
|
| 704 |
theseven |
1116 |
int space = dbgconsole_makespace(length, safe);
|
| 25 |
theseven |
1117 |
if (dbgconsendwriteidx + space >= sizeof(dbgconsendbuf))
|
|
|
1118 |
{
|
|
|
1119 |
int bytes = sizeof(dbgconsendbuf) - dbgconsendwriteidx;
|
|
|
1120 |
memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, bytes);
|
|
|
1121 |
dbgconsendwriteidx = 0;
|
|
|
1122 |
string = &string[bytes];
|
|
|
1123 |
space -= bytes;
|
|
|
1124 |
length -= bytes;
|
|
|
1125 |
}
|
|
|
1126 |
if (space) memcpy(&dbgconsendbuf[dbgconsendwriteidx], string, space);
|
|
|
1127 |
dbgconsendwriteidx += space;
|
|
|
1128 |
string = &string[space];
|
|
|
1129 |
length -= space;
|
|
|
1130 |
}
|
|
|
1131 |
}
|
|
|
1132 |
|
| 704 |
theseven |
1133 |
void dbgconsole_write(const char* string, size_t length)
|
|
|
1134 |
{
|
|
|
1135 |
dbgconsole_write_internal(string, length, false);
|
|
|
1136 |
}
|
|
|
1137 |
|
|
|
1138 |
void dbgconsole_swrite(const char* string, size_t length)
|
|
|
1139 |
{
|
|
|
1140 |
dbgconsole_write_internal(string, length, true);
|
|
|
1141 |
}
|
|
|
1142 |
|
| 25 |
theseven |
1143 |
void dbgconsole_puts(const char* string)
|
|
|
1144 |
{
|
|
|
1145 |
dbgconsole_write(string, strlen(string));
|
|
|
1146 |
}
|
| 26 |
theseven |
1147 |
|
| 704 |
theseven |
1148 |
void dbgconsole_sputs(const char* string)
|
|
|
1149 |
{
|
|
|
1150 |
dbgconsole_swrite(string, strlen(string));
|
|
|
1151 |
}
|
|
|
1152 |
|
| 26 |
theseven |
1153 |
int dbgconsole_getavailable() ICODE_ATTR;
|
|
|
1154 |
int dbgconsole_getavailable()
|
|
|
1155 |
{
|
|
|
1156 |
int available = dbgconrecvwriteidx - dbgconrecvreadidx;
|
|
|
1157 |
if (available < 0) available += sizeof(dbgconrecvbuf);
|
|
|
1158 |
return available;
|
|
|
1159 |
}
|
|
|
1160 |
|
|
|
1161 |
int dbgconsole_getc(int timeout)
|
|
|
1162 |
{
|
|
|
1163 |
if (!dbgconsole_getavailable())
|
|
|
1164 |
{
|
|
|
1165 |
wakeup_wait(&dbgconrecvwakeup, TIMEOUT_NONE);
|
|
|
1166 |
if (!dbgconsole_getavailable())
|
|
|
1167 |
{
|
|
|
1168 |
wakeup_wait(&dbgconrecvwakeup, timeout);
|
|
|
1169 |
if (!dbgconsole_getavailable()) return -1;
|
|
|
1170 |
}
|
|
|
1171 |
}
|
|
|
1172 |
int byte = dbgconrecvbuf[dbgconrecvreadidx++];
|
|
|
1173 |
if (dbgconrecvreadidx >= sizeof(dbgconrecvbuf))
|
|
|
1174 |
dbgconrecvreadidx -= sizeof(dbgconrecvbuf);
|
|
|
1175 |
return byte;
|
|
|
1176 |
}
|
|
|
1177 |
|
|
|
1178 |
int dbgconsole_read(char* buffer, size_t length, int timeout)
|
|
|
1179 |
{
|
|
|
1180 |
if (!length) return 0;
|
|
|
1181 |
int available = dbgconsole_getavailable();
|
|
|
1182 |
if (!available)
|
|
|
1183 |
{
|
|
|
1184 |
wakeup_wait(&dbgconrecvwakeup, TIMEOUT_NONE);
|
|
|
1185 |
int available = dbgconsole_getavailable();
|
|
|
1186 |
if (!available)
|
|
|
1187 |
{
|
|
|
1188 |
wakeup_wait(&dbgconrecvwakeup, timeout);
|
|
|
1189 |
int available = dbgconsole_getavailable();
|
|
|
1190 |
if (!available) return 0;
|
|
|
1191 |
}
|
|
|
1192 |
}
|
|
|
1193 |
if (available > length) available = length;
|
|
|
1194 |
int left = available;
|
|
|
1195 |
if (dbgconrecvreadidx + available >= sizeof(dbgconrecvbuf))
|
|
|
1196 |
{
|
|
|
1197 |
int bytes = sizeof(dbgconrecvbuf) - dbgconrecvreadidx;
|
|
|
1198 |
memcpy(buffer, &dbgconrecvbuf[dbgconrecvreadidx], bytes);
|
|
|
1199 |
dbgconrecvreadidx = 0;
|
|
|
1200 |
buffer = &buffer[bytes];
|
|
|
1201 |
left -= bytes;
|
|
|
1202 |
}
|
|
|
1203 |
if (left) memcpy(buffer, &dbgconrecvbuf[dbgconrecvreadidx], left);
|
|
|
1204 |
dbgconrecvreadidx += left;
|
|
|
1205 |
return available;
|
|
|
1206 |
}
|
| 240 |
theseven |
1207 |
|
|
|
1208 |
void usb_exit(void)
|
|
|
1209 |
{
|
|
|
1210 |
usb_drv_exit();
|
|
|
1211 |
}
|