Implement _stlink_usb_write_mem32|8
[fw/stlink] / src / stlink-usb.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdint.h>
6 #include <time.h>
7 #include <sys/types.h>
8 #include <libusb-1.0/libusb.h>
9 #include "stlink-common.h"
10 #include "stlink-usb.h"
11
12 void _stlink_usb_close(stlink_t* sl) {
13     struct stlink_libusb * const handle = sl->backend_data;
14     // maybe we couldn't even get the usb device?
15     if (handle != NULL) {
16         if (handle->req_trans != NULL)
17             libusb_free_transfer(handle->req_trans);
18
19         if (handle->rep_trans != NULL)
20             libusb_free_transfer(handle->rep_trans);
21
22         if (handle->usb_handle != NULL)
23             libusb_close(handle->usb_handle);
24
25         libusb_exit(handle->libusb_ctx);
26         free(handle);
27     }
28 }
29
30
31 struct trans_ctx {
32 #define TRANS_FLAGS_IS_DONE (1 << 0)
33 #define TRANS_FLAGS_HAS_ERROR (1 << 1)
34     volatile unsigned long flags;
35 };
36
37 static void on_trans_done(struct libusb_transfer * trans) {
38     struct trans_ctx * const ctx = trans->user_data;
39
40     if (trans->status != LIBUSB_TRANSFER_COMPLETED)
41         ctx->flags |= TRANS_FLAGS_HAS_ERROR;
42
43     ctx->flags |= TRANS_FLAGS_IS_DONE;
44 }
45
46 int submit_wait(struct stlink_libusb *slu, struct libusb_transfer * trans) {
47     struct timeval start;
48     struct timeval now;
49     struct timeval diff;
50     struct trans_ctx trans_ctx;
51     enum libusb_error error;
52
53     trans_ctx.flags = 0;
54
55     /* brief intrusion inside the libusb interface */
56     trans->callback = on_trans_done;
57     trans->user_data = &trans_ctx;
58
59     if ((error = libusb_submit_transfer(trans))) {
60         printf("libusb_submit_transfer(%d)\n", error);
61         return -1;
62     }
63
64     gettimeofday(&start, NULL);
65
66     while (trans_ctx.flags == 0) {
67         struct timeval timeout;
68         timeout.tv_sec = 3;
69         timeout.tv_usec = 0;
70         if (libusb_handle_events_timeout(slu->libusb_ctx, &timeout)) {
71             printf("libusb_handle_events()\n");
72             return -1;
73         }
74
75         gettimeofday(&now, NULL);
76         timersub(&now, &start, &diff);
77         if (diff.tv_sec >= 3) {
78             printf("libusb_handle_events() timeout\n");
79             return -1;
80         }
81     }
82
83     if (trans_ctx.flags & TRANS_FLAGS_HAS_ERROR) {
84         printf("libusb_handle_events() | has_error\n");
85         return -1;
86     }
87
88     return 0;
89 }
90
91 ssize_t send_recv(struct stlink_libusb* handle,
92         unsigned char* txbuf, size_t txsize,
93         unsigned char* rxbuf, size_t rxsize) {
94     /* note: txbuf and rxbuf can point to the same area */
95
96     libusb_fill_bulk_transfer(handle->req_trans, handle->usb_handle,
97             handle->ep_req,
98             txbuf, txsize,
99             NULL, NULL,
100             0
101             );
102
103     printf("submit_wait(req)\n");
104
105     if (submit_wait(handle, handle->req_trans)) return -1;
106
107     /* send_only */
108     if (rxsize == 0) return 0;
109
110     /* read the response */
111
112     libusb_fill_bulk_transfer(handle->rep_trans, handle->usb_handle,
113             handle->ep_rep, rxbuf, rxsize, NULL, NULL, 0);
114
115     printf("submit_wait(rep)\n");
116
117     if (submit_wait(handle, handle->rep_trans)) return -1;
118
119     return handle->rep_trans->actual_length;
120 }
121
122 static inline int send_only
123 (struct stlink_libusb* handle, unsigned char* txbuf, size_t txsize) {
124     return send_recv(handle, txbuf, txsize, NULL, 0);
125 }
126
127
128 // KARL - fixme, common code! (or, one per backend)
129 // candidate for common code...
130
131
132 static int is_stlink_device(libusb_device * dev) {
133     struct libusb_device_descriptor desc;
134
135     if (libusb_get_device_descriptor(dev, &desc))
136         return 0;
137
138     printf("device: 0x%04x, 0x%04x\n", desc.idVendor, desc.idProduct);
139
140     if (desc.idVendor != USB_ST_VID)
141         return 0;
142
143     if (desc.idProduct != USB_STLINK_32L_PID)
144         return 0;
145
146     return 1;
147 }
148
149 void _stlink_usb_version(stlink_t *sl) {
150     struct stlink_libusb * const slu = sl->backend_data;
151     unsigned char* const buf = sl->q_buf;
152     ssize_t size;
153
154     memset(buf, 0, sizeof (sl->q_buf));
155     buf[0] = STLINK_GET_VERSION;
156     buf[1] = 0x80;
157
158     size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
159     if (size == -1) {
160         printf("[!] send_recv\n");
161         return;
162     }
163
164 #if 1 /* DEBUG */
165     {
166         unsigned int i;
167         for (i = 0; i < size; ++i) printf("%02x", buf[i]);
168         printf("\n");
169     }
170 #endif /* DEBUG */
171 }
172
173 void _stlink_usb_write_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
174     struct stlink_libusb * const slu = sl->backend_data;
175     unsigned char* const buf = sl->q_buf;
176     unsigned char *cmd_buf = sl->c_buf;
177
178     memset(cmd_buf, 0, STLINK_CMD_SIZE);
179     cmd_buf[0] = STLINK_DEBUG_COMMAND;
180     cmd_buf[1] =  STLINK_DEBUG_WRITEMEM_32BIT;
181     write_uint32(cmd_buf + 2, addr);
182     write_uint16(cmd_buf + 6, len);
183     send_only(slu, cmd_buf, STLINK_CMD_SIZE);
184
185     assert((len & 3) == 0); 
186     stlink_print_data(sl);
187     send_only(slu, buf, len);
188
189 }
190
191 void _stlink_usb_write_mem8(stlink_t *sl, uint32_t addr, uint16_t len) {
192     struct stlink_libusb * const slu = sl->backend_data;
193     unsigned char* const buf = sl->q_buf;
194     unsigned char *cmd_buf = sl->c_buf;
195
196     memset(cmd_buf, 0, STLINK_CMD_SIZE);
197     cmd_buf[0] = STLINK_DEBUG_COMMAND;
198     cmd_buf[1] =  STLINK_DEBUG_WRITEMEM_8BIT;
199     write_uint32(cmd_buf + 2, addr);
200     write_uint16(cmd_buf + 6, len);
201     send_only(slu, cmd_buf, STLINK_CMD_SIZE);
202
203     stlink_print_data(sl);
204     send_only(slu, buf, len);
205 }
206
207
208 int _stlink_usb_current_mode(stlink_t * sl) {
209     struct stlink_libusb * const slu = sl->backend_data;
210     unsigned char* const buf = sl->q_buf;
211     ssize_t size;
212     memset(buf, 0, sizeof (sl->q_buf));
213     buf[0] = STLINK_GET_CURRENT_MODE;
214     size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
215     if (size == -1) {
216         printf("[!] send_recv\n");
217         return -1;
218     }
219     return sl->q_buf[0];
220 }
221
222 void _stlink_usb_core_id(stlink_t * sl) {
223     struct stlink_libusb * const slu = sl->backend_data;
224     unsigned char* const buf = sl->q_buf;
225     ssize_t size;
226
227     memset(buf, 0, sizeof (sl->q_buf));
228     buf[0] = STLINK_DEBUG_COMMAND;
229     buf[1] = STLINK_DEBUG_READCOREID;
230
231     size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
232     if (size == -1) {
233         printf("[!] send_recv\n");
234         return;
235     }
236
237     sl->core_id = read_uint32(buf, 0);
238 }
239
240 void _stlink_usb_status(stlink_t * sl) {
241     struct stlink_libusb * const slu = sl->backend_data;
242     unsigned char* const buf = sl->q_buf;
243     ssize_t size;
244
245     memset(buf, 0, sizeof (sl->q_buf));
246
247     buf[0] = STLINK_DEBUG_COMMAND;
248     buf[1] = STLINK_DEBUG_GETSTATUS;
249
250     size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
251     if (size == -1) {
252         printf("[!] send_recv\n");
253         return;
254     }
255
256     /* todo: stlink_core_stat */
257
258     // FIXME - decode into sl->core_stat
259 #if 1 /* DEBUG */
260     printf("status == 0x%x\n", buf[0]);
261 #endif /* DEBUG */
262
263 }
264
265 void _stlink_usb_force_debug(stlink_t *sl) {
266     struct stlink_libusb *slu = sl->backend_data;
267     unsigned char* const buf = sl->q_buf;
268     ssize_t size;
269
270     memset(buf, 0, sizeof (sl->q_buf));
271
272     buf[0] = STLINK_DEBUG_COMMAND;
273     buf[1] = STLINK_DEBUG_FORCEDEBUG;
274     size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
275     if (size == -1) {
276         printf("[!] send_recv\n");
277         return;
278     }
279 }
280
281
282 void _stlink_usb_enter_swd_mode(stlink_t * sl) {
283     struct stlink_libusb * const slu = sl->backend_data;
284     unsigned char* const buf = sl->q_buf;
285     ssize_t size;
286
287     memset(buf, 0, sizeof (sl->q_buf));
288
289     buf[0] = STLINK_DEBUG_COMMAND;
290     buf[1] = STLINK_SWD_ENTER;
291     buf[2] = STLINK_DEBUG_ENTER_SWD;
292
293     size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
294     if (size == -1) {
295         printf("[!] send_recv\n");
296         return;
297     }
298 }
299
300 void _stlink_usb_exit_dfu_mode(stlink_t* sl) {
301     struct stlink_libusb * const slu = sl->backend_data;
302     unsigned char* const buf = sl->q_buf;
303     ssize_t size;
304
305     memset(buf, 0, sizeof (sl->q_buf));
306     buf[0] = STLINK_DFU_COMMAND;
307     buf[1] = STLINK_DFU_EXIT;
308
309     size = send_only(slu, buf, 16);
310     if (size == -1) {
311         printf("[!] send_recv\n");
312         return;
313     }
314 }
315
316 /**
317  * TODO - not convinced this does anything...
318  * @param sl
319  */
320 void _stlink_usb_reset(stlink_t * sl) {
321     struct stlink_libusb * const slu = sl->backend_data;
322     unsigned char* const buf = sl->q_buf;
323     ssize_t size;
324
325     memset(buf, 0, sizeof (sl->q_buf));
326     buf[0] = STLINK_DEBUG_COMMAND;
327     buf[1] = STLINK_DEBUG_RESETSYS;
328
329     size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
330     if (size == -1) {
331         printf("[!] send_recv\n");
332         return;
333     }
334 }
335
336
337 void _stlink_usb_step(stlink_t* sl) {
338     struct stlink_libusb * const slu = sl->backend_data;
339     unsigned char* const buf = sl->q_buf;
340     ssize_t size;
341
342     memset(buf, 0, sizeof (sl->q_buf));
343     buf[0] = STLINK_DEBUG_COMMAND;
344     buf[1] = STLINK_DEBUG_STEPCORE;
345
346     size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
347     if (size == -1) {
348         printf("[!] send_recv\n");
349         return;
350     }
351 }
352
353 /**
354  * This seems to do a good job of restarting things from the beginning?
355  * @param sl
356  */
357 void _stlink_usb_run(stlink_t* sl) {
358     struct stlink_libusb * const slu = sl->backend_data;
359     unsigned char* const buf = sl->q_buf;
360     ssize_t size;
361
362     memset(buf, 0, sizeof (sl->q_buf));
363     buf[0] = STLINK_DEBUG_COMMAND;
364     buf[1] = STLINK_DEBUG_RUNCORE;
365
366     size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
367     if (size == -1) {
368         printf("[!] send_recv\n");
369         return;
370     }
371
372 }
373
374 void _stlink_usb_exit_debug_mode(stlink_t *sl) {
375     struct stlink_libusb * const slu = sl->backend_data;
376     unsigned char* const buf = sl->q_buf;
377     ssize_t size;
378
379     memset(buf, 0, sizeof (sl->q_buf));
380     buf[0] = STLINK_DEBUG_COMMAND;
381     buf[1] = STLINK_DEBUG_EXIT;
382
383     size = send_only(slu, buf, 16);
384     if (size == -1) {
385         printf("[!] send_only\n");
386         return;
387     }
388 }
389
390 void _stlink_usb_read_mem32(stlink_t *sl, uint32_t addr, uint16_t len) {
391     struct stlink_libusb * const slu = sl->backend_data;
392     unsigned char* const buf = sl->q_buf;
393     ssize_t size;
394
395     /* assume len < sizeof(sl->q_buf) */
396     assert(len < sizeof(sl->q_buf));  // makes a compiler warning? always true?
397
398     memset(buf, 0, sizeof (sl->q_buf));
399     buf[0] = STLINK_DEBUG_COMMAND;
400     buf[1] = STLINK_DEBUG_READMEM_32BIT;
401     write_uint32(buf + 2, addr);
402     /* windows usb logs show only one byte is used for length ... */
403     // Presumably, this is because usb transfers can't be 16 bits worth of bytes long...
404     assert (len < 256);
405     buf[6] = (uint8_t) len;
406
407     size = send_recv(slu, buf, STLINK_CMD_SIZE, buf, sizeof (sl->q_buf));
408     if (size == -1) {
409         printf("[!] send_recv\n");
410         return;
411     }
412
413     sl->q_len = (size_t) size;
414
415     stlink_print_data(sl);
416 }
417
418 void _stlink_usb_read_all_regs(stlink_t *sl, reg *regp) {
419     DD(sl, "oops! read_all_regs not implemented for USB!\n");
420 }
421
422 void _stlink_usb_read_reg(stlink_t *sl, int r_idx, reg *regp) {
423     DD(sl, "oops! read_reg not implemented for USB! Wanted to read reg %d\n",
424             r_idx);
425 }
426
427 void _stlink_usb_write_reg(stlink_t *sl, uint32_t reg, int idx) {
428     DD(sl, "oops! write_reg not implemented for USB! Wanted to write %#x to %d\n",
429             reg, idx);
430 }
431
432
433
434 stlink_backend_t _stlink_usb_backend = {
435     _stlink_usb_close,
436     _stlink_usb_exit_debug_mode,
437     _stlink_usb_enter_swd_mode,
438     NULL,  // no enter_jtag_mode here...
439     _stlink_usb_exit_dfu_mode,
440     _stlink_usb_core_id,
441     _stlink_usb_reset,
442     _stlink_usb_run,
443     _stlink_usb_status,
444     _stlink_usb_version,
445     _stlink_usb_read_mem32,
446     _stlink_usb_write_mem32,
447     _stlink_usb_write_mem8,
448     _stlink_usb_read_all_regs,
449     _stlink_usb_read_reg,
450     _stlink_usb_write_reg,
451     _stlink_usb_step,
452     _stlink_usb_current_mode,
453     _stlink_usb_force_debug
454 };
455
456
457 stlink_t* stlink_open_usb(const char *dev_name, const int verbose) {
458     stlink_t* sl = NULL;
459     struct stlink_libusb* slu = NULL;
460
461     sl = malloc(sizeof (stlink_t));
462     slu = malloc(sizeof (struct stlink_libusb));
463     if (sl == NULL) goto on_error;
464     if (slu == NULL) goto on_error;
465
466     sl->verbose = verbose;
467     
468     if (slu->libusb_ctx != NULL) {
469         fprintf(stderr, "reopening with an existing context? undefined behaviour!\n");
470         goto on_error;
471     } else {
472         if (libusb_init(&(slu->libusb_ctx))) {
473             fprintf(stderr, "failed to init libusb context, wrong version of libraries?\n");
474             goto on_error;
475         }
476     }
477
478     int error = -1;
479
480     libusb_device** devs = NULL;
481     libusb_device* dev;
482     ssize_t i;
483     ssize_t count;
484     int config;
485
486     count = libusb_get_device_list(slu->libusb_ctx, &devs);
487     if (count < 0) {
488         printf("libusb_get_device_list\n");
489         goto on_libusb_error;
490     }
491
492     for (i = 0; i < count; ++i) {
493         dev = devs[i];
494         if (is_stlink_device(dev)) break;
495     }
496     if (i == count) return NULL;
497
498     if (libusb_open(dev, &(slu->usb_handle))) {
499         printf("libusb_open()\n");
500         goto on_libusb_error;
501     }
502
503     if (libusb_get_configuration(slu->usb_handle, &config)) {
504         /* this may fail for a previous configured device */
505         printf("libusb_get_configuration()\n");
506         goto on_libusb_error;
507     }
508
509     if (config != 1) {
510         printf("setting new configuration (%d -> 1)\n", config);
511         if (libusb_set_configuration(slu->usb_handle, 1)) {
512             /* this may fail for a previous configured device */
513             printf("libusb_set_configuration()\n");
514             goto on_libusb_error;
515         }
516     }
517
518     if (libusb_claim_interface(slu->usb_handle, 0)) {
519         printf("libusb_claim_interface()\n");
520         goto on_libusb_error;
521     }
522
523     slu->req_trans = libusb_alloc_transfer(0);
524     if (slu->req_trans == NULL) {
525         printf("libusb_alloc_transfer\n");
526         goto on_libusb_error;
527     }
528
529     slu->rep_trans = libusb_alloc_transfer(0);
530     if (slu->rep_trans == NULL) {
531         printf("libusb_alloc_transfer\n");
532         goto on_libusb_error;
533     }
534
535     slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
536     slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
537
538     /* libusb_reset_device(slu->usb_handle); */
539
540     /* success */
541     error = 0;
542
543 on_libusb_error:
544     if (devs != NULL) {
545         libusb_free_device_list(devs, 1);
546         fprintf(stderr, "freed libusb device list\n");
547     }
548
549     if (error == -1) {
550         stlink_close(sl);
551         return NULL;
552     }
553
554     sl->backend = &_stlink_usb_backend;
555     sl->backend_data = slu;
556     /* success */
557     return sl;
558
559 on_error:
560     if (sl != NULL) free(sl);
561     if (slu != NULL) free(slu);
562     return 0;
563 }
564