[fix, update] magic bytes, more commands. step mode make resets the chip
[fw/stlink] / stm32l / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include <time.h>
6 #include <sys/types.h>
7 #include <libusb-1.0/libusb.h>
8 #include "stlink-hw.h"
9
10 /* endianess related */
11 static inline unsigned int is_bigendian(void)
12 {
13   static volatile const unsigned int i = 1;
14   return *(volatile const char*) &i == 0;
15 }
16
17 static void write_uint32(unsigned char* buf, uint32_t ui)
18 {
19   if (!is_bigendian()) { // le -> le (don't swap)
20     buf[0] = ((unsigned char*) &ui)[0];
21     buf[1] = ((unsigned char*) &ui)[1];
22     buf[2] = ((unsigned char*) &ui)[2];
23     buf[3] = ((unsigned char*) &ui)[3];
24   } else {
25     buf[0] = ((unsigned char*) &ui)[3];
26     buf[1] = ((unsigned char*) &ui)[2];
27     buf[2] = ((unsigned char*) &ui)[1];
28     buf[3] = ((unsigned char*) &ui)[0];
29   }
30 }
31
32 static void write_uint16(unsigned char* buf, uint16_t ui)
33 {
34   if (!is_bigendian()) { // le -> le (don't swap)
35     buf[0] = ((unsigned char*) &ui)[0];
36     buf[1] = ((unsigned char*) &ui)[1];
37   } else {
38     buf[0] = ((unsigned char*) &ui)[1];
39     buf[1] = ((unsigned char*) &ui)[0];
40   }
41 }
42
43 static uint32_t read_uint32(const unsigned char *c, const int pt)
44 {
45   uint32_t ui;
46   char *p = (char *) &ui;
47
48   if (!is_bigendian()) { // le -> le (don't swap)
49     p[0] = c[pt];
50     p[1] = c[pt + 1];
51     p[2] = c[pt + 2];
52     p[3] = c[pt + 3];
53   } else {
54     p[0] = c[pt + 3];
55     p[1] = c[pt + 2];
56     p[2] = c[pt + 1];
57     p[3] = c[pt];
58   }
59   return ui;
60 }
61
62 static uint16_t read_uint16(const unsigned char *c, const int pt)
63 {
64   uint32_t ui;
65   char *p = (char *) &ui;
66
67   if (!is_bigendian()) { // le -> le (don't swap)
68     p[0] = c[pt];
69     p[1] = c[pt + 1];
70   } else {
71     p[0] = c[pt + 1];
72     p[1] = c[pt];
73   }
74   return ui;
75 }
76
77 /* libusb transport layer */
78
79 libusb_context* libusb_ctx =  NULL;
80
81 struct stlink_libusb
82 {
83   libusb_device_handle* usb_handle;
84   struct libusb_transfer* req_trans;
85   struct libusb_transfer* rep_trans;
86   unsigned int ep_req;
87   unsigned int ep_rep;
88 };
89
90 struct trans_ctx
91 {
92 #define TRANS_FLAGS_IS_DONE (1 << 0)
93 #define TRANS_FLAGS_HAS_ERROR (1 << 1)
94   volatile unsigned long flags;
95 };
96
97 static void on_trans_done(struct libusb_transfer* trans)
98 {
99   struct trans_ctx* const ctx = trans->user_data;
100
101   if (trans->status != LIBUSB_TRANSFER_COMPLETED)
102     ctx->flags |= TRANS_FLAGS_HAS_ERROR;
103
104   ctx->flags |= TRANS_FLAGS_IS_DONE;
105 }
106
107
108 static int submit_wait(struct libusb_transfer* trans)
109 {
110   struct timeval start;
111   struct timeval now;
112   struct timeval diff;
113   struct trans_ctx trans_ctx;
114   enum libusb_error error;
115
116   trans_ctx.flags = 0;
117
118   /* brief intrusion inside the libusb interface */
119   trans->callback = on_trans_done;
120   trans->user_data = &trans_ctx;
121
122   if ((error = libusb_submit_transfer(trans)))
123   {
124     printf("libusb_submit_transfer(%d)\n", error);
125     return -1;
126   }
127
128   gettimeofday(&start, NULL);
129
130   while (trans_ctx.flags == 0)
131   {
132     struct timeval timeout;
133     timeout.tv_sec = 3;
134     timeout.tv_usec = 0;
135     if (libusb_handle_events_timeout(libusb_ctx, &timeout))
136     {
137       printf("libusb_handle_events()\n");
138       return -1;
139     }
140
141     gettimeofday(&now, NULL);
142     timersub(&now, &start, &diff);
143     if (diff.tv_sec >= 3)
144     {
145       printf("libusb_handle_events() timeout\n");
146       return -1;
147     }
148   }
149
150   if (trans_ctx.flags & TRANS_FLAGS_HAS_ERROR)
151   {
152     printf("libusb_handle_events() | has_error\n");
153     return -1;
154   }
155
156   return 0;
157 }
158
159 static ssize_t send_recv
160 (
161  struct stlink_libusb* handle,
162  unsigned char* txbuf, size_t txsize,
163  unsigned char* rxbuf, size_t rxsize
164 )
165 {
166   /* note: txbuf and rxbuf can point to the same area */
167
168   libusb_fill_bulk_transfer
169   (
170    handle->req_trans,
171    handle->usb_handle,
172    handle->ep_req,
173    txbuf, txsize,
174    NULL, NULL,
175    0
176   );
177
178   printf("submit_wait(req)\n");
179
180   if (submit_wait(handle->req_trans)) return -1;
181
182   /* send_only */
183   if (rxsize == 0) return 0;
184
185   /* read the response */
186
187   libusb_fill_bulk_transfer
188   (
189    handle->rep_trans,
190    handle->usb_handle,
191    handle->ep_rep,
192    rxbuf, rxsize,
193    NULL, NULL,
194    0
195   );
196
197   printf("submit_wait(rep)\n");
198
199   if (submit_wait(handle->rep_trans)) return -1;
200
201   return handle->rep_trans->actual_length;
202 }
203
204
205 static inline int send_only
206 (struct stlink_libusb* handle, unsigned char* txbuf, size_t txsize)
207 {
208   return send_recv(handle, txbuf, txsize, NULL, 0);
209 }
210
211
212 /* stlink layer independant interface */
213
214 enum transport_type
215 {
216   TRANSPORT_TYPE_ZERO = 0,
217 #if CONFIG_USE_LIBSG
218   TRANSPORT_TYPE_LIBSG,
219 #endif /* CONFIG_USE_LIBSG */
220 #if CONFIG_USE_LIBUSB
221   TRANSPORT_TYPE_LIBUSB,
222 #endif /* CONFIG_USE_LIBUSB */
223   TRANSPORT_TYPE_INVALID
224 };
225
226 struct stlink
227 {
228   enum transport_type tt;
229   union
230   {
231 #if CONFIG_USE_LIBUSB
232     struct stlink_libusb libusb;
233 #endif /* CONFIG_USE_LIBUSB */
234 #if CONFIG_USE_LIBSG
235     void* libsg;
236 #endif /* CONFIG_USE_LIBSG */
237   } transport;
238
239   unsigned char q_buf[64];
240
241   /* layer independant */
242   uint32_t core_id;
243 };
244
245 int stlink_initialize(enum transport_type tt)
246 {
247   switch (tt)
248   {
249 #if CONFIG_USE_LIBUSB
250   case TRANSPORT_TYPE_LIBUSB:
251     {
252       if (libusb_ctx != NULL) return -1;
253       if (libusb_init(&libusb_ctx))
254       {
255         printf("libusb_init()\n");
256         return -1;
257       }
258       break ;
259     }
260 #endif /* CONFIG_USE_LIBUSB */
261
262   default: break ;
263   }
264
265   return 0;
266 }
267
268 void stlink_finalize(enum transport_type tt)
269 {
270   switch (tt)
271   {
272 #if CONFIG_USE_LIBUSB
273   case TRANSPORT_TYPE_LIBUSB:
274     {
275       libusb_exit(libusb_ctx);
276       break ;
277     }
278 #endif /* CONFIG_USE_LIBUSB */
279
280   default: break;
281   }
282 }
283
284 #if CONFIG_USE_LIBUSB
285 static int is_stlink_device(libusb_device* dev)
286 {
287   struct libusb_device_descriptor desc;
288
289   if (libusb_get_device_descriptor(dev, &desc))
290     return 0;
291
292   printf("device: 0x%04x, 0x%04x\n", desc.idVendor, desc.idProduct);
293  
294   if (desc.idVendor != 0x0483)
295     return 0;
296
297   if (desc.idProduct != 0x3748)
298     return 0;
299
300   return 1;
301 }
302 #endif /* CONFIG_USE_LIBUSB */
303
304 /* fwd decl */
305 void stlink_close(struct stlink*);
306
307 struct stlink* stlink_quirk_open
308 (enum transport_type tt, const char *dev_name, const int verbose)
309 {
310   struct stlink* sl = NULL;
311
312   sl = malloc(sizeof(struct stlink));
313   if (sl == NULL) goto on_error;
314
315   sl->tt = tt;
316
317   switch (tt)
318   {
319 #if CONFIG_USE_LIBUSB
320   case TRANSPORT_TYPE_LIBUSB:
321     {
322       struct stlink_libusb* const slu = &sl->transport.libusb;
323
324       int error = -1;
325
326       libusb_device** devs = NULL;
327       libusb_device* dev;
328       ssize_t i;
329       ssize_t count;
330       int config;
331
332       count = libusb_get_device_list(libusb_ctx, &devs);
333       if (count < 0)
334       {
335         printf("libusb_get_device_list\n");
336         goto on_libusb_error;
337       }
338
339       for (i = 0; i < count; ++i)
340       {
341         dev = devs[i];
342         if (is_stlink_device(dev)) break;
343       }
344       if (i == count) return NULL;
345
346       if (libusb_open(dev, &slu->usb_handle))
347       {
348         printf("libusb_open()\n");
349         goto on_libusb_error;
350       }
351
352       if (libusb_get_configuration(slu->usb_handle, &config))
353       {
354         /* this may fail for a previous configured device */
355         printf("libusb_get_configuration()\n");
356         goto on_libusb_error;
357       }
358
359       if (config != 1)
360       {
361         printf("setting new configuration (%d -> 1)\n", config);
362         if (libusb_set_configuration(slu->usb_handle, 1))
363         {
364           /* this may fail for a previous configured device */
365           printf("libusb_set_configuration()\n");
366           goto on_libusb_error;
367         }
368       }
369
370       if (libusb_claim_interface(slu->usb_handle, 0))
371       {
372         printf("libusb_claim_interface()\n");
373         goto on_libusb_error;
374       }
375
376       slu->req_trans = libusb_alloc_transfer(0);
377       if (slu->req_trans == NULL)
378       {
379         printf("libusb_alloc_transfer\n");
380         goto on_libusb_error;
381       }
382
383       slu->rep_trans = libusb_alloc_transfer(0);
384       if (slu->rep_trans == NULL)
385       {
386         printf("libusb_alloc_transfer\n");
387         goto on_libusb_error;
388       }
389
390       slu->ep_rep = 1 /* ep rep */ | LIBUSB_ENDPOINT_IN;
391       slu->ep_req = 2 /* ep req */ | LIBUSB_ENDPOINT_OUT;
392
393       /* libusb_reset_device(slu->usb_handle); */
394
395       /* success */
396       error = 0;
397
398     on_libusb_error:
399       if (devs != NULL) libusb_free_device_list(devs, 1);
400
401       if (error == -1)
402       {
403         stlink_close(sl);
404         return NULL;
405       }
406
407       break ;
408     }
409 #endif /* CONFIG_USE_LIBUSB */
410
411 #if CONFIG_USE_LIBSG
412   case transport_type_libsg:
413     {
414       break ;
415     }
416 #endif /* CONFIG_USE_LIBSG */
417
418   default: break ;
419   }
420
421   /* success */
422   return sl;
423
424  on_error:
425   if (sl != NULL) free(sl);
426   return 0;
427 }
428
429 void stlink_close(struct stlink *sl)
430 {
431   switch (sl->tt)
432   {
433 #if CONFIG_USE_LIBUSB
434   case TRANSPORT_TYPE_LIBUSB:
435     {
436       struct stlink_libusb* const handle = &sl->transport.libusb;
437
438       if (handle->req_trans != NULL)
439         libusb_free_transfer(handle->req_trans);
440
441       if (handle->rep_trans != NULL)
442         libusb_free_transfer(handle->rep_trans);
443
444       if (handle->usb_handle != NULL)
445         libusb_close(handle->usb_handle);
446
447       break ;
448     }
449 #endif /* CONFIG_USE_LIBUSB */
450
451 #if CONFIG_USE_LIBSG
452   case TRANSPORT_TYPE_LIBSG:
453     {
454       break ;
455     }
456 #endif /* CONFIG_USE_LIBSG */
457
458   default: break ;
459   }
460
461   free(sl);
462 }
463
464 void stlink_version(struct stlink* sl)
465 {
466   switch (sl->tt)
467   {
468 #if CONFIG_USE_LIBUSB
469   case TRANSPORT_TYPE_LIBUSB:
470     {
471       struct stlink_libusb* const slu = &sl->transport.libusb;
472       unsigned char* const buf = sl->q_buf;
473       ssize_t size;
474
475       memset(buf, 0, sizeof(sl->q_buf));
476       buf[0] = STLINK_GET_VERSION;
477       buf[1] = 0x80;
478
479       size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf));
480       if (size == -1)
481       {
482         printf("[!] send_recv\n");
483         return ;
484       }
485
486 #if 1 /* DEBUG */
487       {
488         unsigned int i;
489         for (i = 0; i < size; ++i) printf("%02x", buf[i]);
490         printf("\n");
491       }
492 #endif /* DEBUG */
493
494       break ;
495     }
496 #endif /* CONFIG_USE_LIBUSB */
497
498   default: break ;
499   }
500 }
501
502 int stlink_current_mode(struct stlink *sl)
503 {
504   int mode = -1;
505
506   switch (sl->tt)
507   {
508 #if CONFIG_USE_LIBUSB
509   case TRANSPORT_TYPE_LIBUSB:
510     {
511       struct stlink_libusb* const slu = &sl->transport.libusb;
512       unsigned char* const buf = sl->q_buf; 
513       ssize_t size;
514
515       memset(buf, 0, sizeof(sl->q_buf));
516
517       buf[0] = STLINK_GET_CURRENT_MODE;
518
519       size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf));
520       if (size == -1)
521       {
522         printf("[!] send_recv\n");
523         return -1;
524       }
525
526       /* mode = (int)read_uint16(buf, 0); */
527       mode = (int)buf[0];
528
529 #if 1 /* DEBUG */
530       printf("mode == 0x%x\n", mode);
531 #endif /* DEBUG */
532
533       break ;
534     }
535 #endif /* CONFIG_USE_LIBUSB */
536
537   default: break ;
538   }
539
540   return mode;
541 }
542
543 void stlink_core_id(struct stlink *sl)
544 {
545   switch (sl->tt)
546   {
547 #if CONFIG_USE_LIBUSB
548   case TRANSPORT_TYPE_LIBUSB:
549     {
550       struct stlink_libusb* const slu = &sl->transport.libusb;
551       unsigned char* const buf = sl->q_buf; 
552       ssize_t size;
553
554       memset(buf, 0, sizeof(sl->q_buf));
555       buf[0] = STLINK_DEBUG_COMMAND;
556       buf[1] = STLINK_DEBUG_READCOREID;
557
558       size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf));
559       if (size == -1)
560       {
561         printf("[!] send_recv\n");
562         return ;
563       }
564
565       sl->core_id = read_uint32(buf, 0);
566
567 #if 1 /* DEBUG */
568       printf("core_id == 0x%x\n", sl->core_id);
569 #endif /* DEBUG */
570
571       break ;
572     }
573 #endif /* CONFIG_USE_LIBUSB */
574
575   default: break ;
576   }
577 }
578
579 void stlink_status(struct stlink *sl)
580 {
581   switch (sl->tt)
582   {
583 #if CONFIG_USE_LIBUSB
584   case TRANSPORT_TYPE_LIBUSB:
585     {
586       struct stlink_libusb* const slu = &sl->transport.libusb;
587       unsigned char* const buf = sl->q_buf;
588       ssize_t size;
589
590       memset(buf, 0, sizeof(sl->q_buf));
591
592       buf[0] = STLINK_DEBUG_COMMAND;
593       buf[1] = STLINK_DEBUG_GETSTATUS;
594
595       size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf));
596       if (size == -1)
597       {
598         printf("[!] send_recv\n");
599         return ;
600       }
601
602       /* todo: stlink_core_stat */
603
604 #if 1 /* DEBUG */
605       printf("status == 0x%x\n", buf[0]);
606 #endif /* DEBUG */
607
608       break ;
609     }
610 #endif /* CONFIG_USE_LIBUSB */
611
612   default: break ;
613   }
614 }
615
616 void stlink_enter_swd_mode(struct stlink *sl)
617 {
618   switch (sl->tt)
619   {
620 #if CONFIG_USE_LIBUSB
621   case TRANSPORT_TYPE_LIBUSB:
622     {
623       struct stlink_libusb* const slu = &sl->transport.libusb;
624       unsigned char* const buf = sl->q_buf;
625       ssize_t size;
626
627       memset(buf, 0, sizeof(sl->q_buf));
628
629       buf[0] = STLINK_DEBUG_COMMAND;
630       buf[1] = 0x30; /* magic byte */
631       buf[2] = STLINK_DEBUG_ENTER_JTAG;
632
633       size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf));
634       if (size == -1)
635       {
636         printf("[!] send_recv\n");
637         return ;
638       }
639
640       break ;
641     }
642 #endif /* CONFIG_USE_LIBUSB */
643
644   default: break ;
645   }
646 }
647
648 void stlink_exit_dfu_mode(struct stlink *sl)
649 {
650   switch (sl->tt)
651   {
652 #if CONFIG_USE_LIBUSB
653   case TRANSPORT_TYPE_LIBUSB:
654     {
655       struct stlink_libusb* const slu = &sl->transport.libusb;
656       unsigned char* const buf = sl->q_buf;
657       ssize_t size;
658
659       memset(buf, 0, sizeof(sl->q_buf));
660       buf[0] = STLINK_DFU_COMMAND;
661       buf[1] = STLINK_DFU_EXIT;
662
663       size = send_only(slu, buf, 16);
664       if (size == -1)
665       {
666         printf("[!] send_recv\n");
667         return ;
668       }
669
670       break ;
671     }
672 #endif /* CONFIG_USE_LIBUSB */
673
674   default: break ;
675   }
676 }
677
678 void stlink_reset(struct stlink *sl)
679 {
680   switch (sl->tt)
681   {
682 #if CONFIG_USE_LIBUSB
683   case TRANSPORT_TYPE_LIBUSB:
684     {
685       struct stlink_libusb* const slu = &sl->transport.libusb;
686       unsigned char* const buf = sl->q_buf;
687       ssize_t size;
688
689       memset(buf, 0, sizeof(sl->q_buf));
690       buf[0] = STLINK_DEBUG_COMMAND;
691       buf[1] = STLINK_DEBUG_RESETSYS;
692
693       size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf));
694       if (size == -1)
695       {
696         printf("[!] send_recv\n");
697         return ;
698       }
699
700       break ;
701     }
702 #endif /* CONFIG_USE_LIBUSB */
703
704   default: break ;
705   }
706 }
707
708 void stlink_step(struct stlink *sl)
709 {
710   switch (sl->tt)
711   {
712 #if CONFIG_USE_LIBUSB
713   case TRANSPORT_TYPE_LIBUSB:
714     {
715       struct stlink_libusb* const slu = &sl->transport.libusb;
716       unsigned char* const buf = sl->q_buf;
717       ssize_t size;
718
719       memset(buf, 0, sizeof(sl->q_buf));
720       buf[0] = STLINK_DEBUG_COMMAND;
721       buf[1] = STLINK_DEBUG_STEPCORE;
722
723       size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf));
724       if (size == -1)
725       {
726         printf("[!] send_recv\n");
727         return ;
728       }
729
730       break ;
731     }
732 #endif /* CONFIG_USE_LIBUSB */
733
734   default: break ;
735   }
736 }
737
738 void stlink_run(struct stlink *sl)
739 {
740   switch (sl->tt)
741   {
742 #if CONFIG_USE_LIBUSB
743   case TRANSPORT_TYPE_LIBUSB:
744     {
745       struct stlink_libusb* const slu = &sl->transport.libusb;
746       unsigned char* const buf = sl->q_buf;
747       ssize_t size;
748
749       memset(buf, 0, sizeof(sl->q_buf));
750       buf[0] = STLINK_DEBUG_COMMAND;
751       buf[1] = STLINK_DEBUG_RUNCORE;
752
753       size = send_recv(slu, buf, 16, buf, sizeof(sl->q_buf));
754       if (size == -1)
755       {
756         printf("[!] send_recv\n");
757         return ;
758       }
759
760       break ;
761     }
762 #endif /* CONFIG_USE_LIBUSB */
763
764   default: break ;
765   }
766 }
767
768 void stlink_exit_debug_mode(struct stlink *sl)
769 {
770   switch (sl->tt)
771   {
772 #if CONFIG_USE_LIBUSB
773   case TRANSPORT_TYPE_LIBUSB:
774     {
775       struct stlink_libusb* const slu = &sl->transport.libusb;
776       unsigned char* const buf = sl->q_buf;
777       ssize_t size;
778
779       memset(buf, 0, sizeof(sl->q_buf));
780       buf[0] = STLINK_DEBUG_COMMAND;
781       buf[1] = STLINK_DEBUG_EXIT;
782
783       size = send_only(slu, buf, 16);
784       if (size == -1)
785       {
786         printf("[!] send_only\n");
787         return ;
788       }
789
790       break ;
791     }
792 #endif /* CONFIG_USE_LIBUSB */
793
794   default: break ;
795   }
796 }
797
798
799 /* main */
800
801 int main(int ac, char** av)
802 {
803   struct stlink* sl;
804
805   stlink_initialize(TRANSPORT_TYPE_LIBUSB);
806   sl = stlink_quirk_open(TRANSPORT_TYPE_LIBUSB, NULL, 0);
807   if (sl != NULL)
808   {
809     printf("-- version\n");
810     stlink_version(sl);
811
812     if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
813     {
814       printf("-- exit_dfu_mode\n");
815       stlink_exit_dfu_mode(sl);
816     }
817
818     printf("-- enter_swd_mode\n");
819     stlink_enter_swd_mode(sl);
820
821     printf("-- current_mode\n");
822     stlink_current_mode(sl);
823
824     printf("-- core_id\n");
825     stlink_core_id(sl);
826
827     printf("-- status\n");
828     stlink_status(sl);
829
830     printf("-- reset\n");
831     stlink_reset(sl);
832
833     printf("-- status\n");
834     stlink_status(sl);
835
836     printf("-- step\n");
837     stlink_step(sl);
838     getchar();
839
840     printf("-- run\n");
841     stlink_run(sl);
842
843     printf("-- exit_debug_mode\n");
844     stlink_exit_debug_mode(sl);
845
846     stlink_close(sl);
847   }
848   stlink_finalize(TRANSPORT_TYPE_LIBUSB);
849
850   return 0;
851 }